1 /* This file is in the public domain. */
6 #include <contrib/libb2/blake2.h>
7 #include <opencrypto/xform_auth.h>
9 extern int blake2b_init_ref(blake2b_state *S, size_t outlen);
10 extern int blake2b_init_param_ref(blake2b_state *S, const blake2b_param *P);
11 extern int blake2b_init_key_ref(blake2b_state *S, size_t outlen,
12 const void *key, size_t keylen);
13 extern int blake2b_update_ref(blake2b_state *S, const uint8_t *in,
15 extern int blake2b_final_ref(blake2b_state *S, uint8_t *out, size_t outlen);
16 extern int blake2b_ref(uint8_t *out, const void *in, const void *key,
17 size_t outlen, size_t inlen, size_t keylen);
19 extern int blake2s_init_ref(blake2s_state *S, size_t outlen);
20 extern int blake2s_init_param_ref(blake2s_state *S, const blake2s_param *P);
21 extern int blake2s_init_key_ref(blake2s_state *S, size_t outlen,
22 const void *key, size_t keylen);
23 extern int blake2s_update_ref(blake2s_state *S, const uint8_t *in,
25 extern int blake2s_final_ref(blake2s_state *S, uint8_t *out, size_t outlen);
26 extern int blake2s_ref(uint8_t *out, const void *in, const void *key,
27 size_t outlen, size_t inlen, size_t keylen);
29 struct blake2b_xform_ctx {
31 uint8_t key[BLAKE2B_KEYBYTES];
34 CTASSERT(sizeof(union authctx) >= sizeof(struct blake2b_xform_ctx));
37 blake2b_xform_init(void *vctx)
39 struct blake2b_xform_ctx *ctx = vctx;
43 rc = blake2b_init_key_ref(&ctx->state, BLAKE2B_OUTBYTES,
46 rc = blake2b_init_ref(&ctx->state, BLAKE2B_OUTBYTES);
48 panic("blake2b_init_key: invalid arguments");
52 blake2b_xform_setkey(void *vctx, const uint8_t *key, uint16_t klen)
54 struct blake2b_xform_ctx *ctx = vctx;
56 if (klen > sizeof(ctx->key))
57 panic("invalid klen %u", (unsigned)klen);
58 memcpy(ctx->key, key, klen);
63 blake2b_xform_update(void *vctx, const uint8_t *data, uint16_t len)
65 struct blake2b_xform_ctx *ctx = vctx;
68 rc = blake2b_update_ref(&ctx->state, data, len);
75 blake2b_xform_final(uint8_t *out, void *vctx)
77 struct blake2b_xform_ctx *ctx = vctx;
80 rc = blake2b_final_ref(&ctx->state, out, BLAKE2B_OUTBYTES);
82 panic("blake2b_final: invalid");
85 struct auth_hash auth_hash_blake2b = {
86 .type = CRYPTO_BLAKE2B,
88 .keysize = BLAKE2B_KEYBYTES,
89 .hashsize = BLAKE2B_OUTBYTES,
90 .ctxsize = sizeof(struct blake2b_xform_ctx),
91 .Setkey = blake2b_xform_setkey,
92 .Init = blake2b_xform_init,
93 .Update = blake2b_xform_update,
94 .Final = blake2b_xform_final,
97 struct blake2s_xform_ctx {
99 uint8_t key[BLAKE2S_KEYBYTES];
102 CTASSERT(sizeof(union authctx) >= sizeof(struct blake2s_xform_ctx));
105 blake2s_xform_init(void *vctx)
107 struct blake2s_xform_ctx *ctx = vctx;
111 rc = blake2s_init_key_ref(&ctx->state, BLAKE2S_OUTBYTES,
112 ctx->key, ctx->klen);
114 rc = blake2s_init_ref(&ctx->state, BLAKE2S_OUTBYTES);
116 panic("blake2s_init_key: invalid arguments");
120 blake2s_xform_setkey(void *vctx, const uint8_t *key, uint16_t klen)
122 struct blake2s_xform_ctx *ctx = vctx;
124 if (klen > sizeof(ctx->key))
125 panic("invalid klen %u", (unsigned)klen);
126 memcpy(ctx->key, key, klen);
131 blake2s_xform_update(void *vctx, const uint8_t *data, uint16_t len)
133 struct blake2s_xform_ctx *ctx = vctx;
136 rc = blake2s_update_ref(&ctx->state, data, len);
143 blake2s_xform_final(uint8_t *out, void *vctx)
145 struct blake2s_xform_ctx *ctx = vctx;
148 rc = blake2s_final_ref(&ctx->state, out, BLAKE2S_OUTBYTES);
150 panic("blake2s_final: invalid");
153 struct auth_hash auth_hash_blake2s = {
154 .type = CRYPTO_BLAKE2S,
156 .keysize = BLAKE2S_KEYBYTES,
157 .hashsize = BLAKE2S_OUTBYTES,
158 .ctxsize = sizeof(struct blake2s_xform_ctx),
159 .Setkey = blake2s_xform_setkey,
160 .Init = blake2s_xform_init,
161 .Update = blake2s_xform_update,
162 .Final = blake2s_xform_final,