xref: /freebsd/sys/crypto/blake2/blake2-sw.c (revision 685dc743dc3b5645e34836464128e1c0558b404b)
1 /* This file is in the public domain. */
2 
3 #include <sys/cdefs.h>
4 #include <contrib/libb2/blake2.h>
5 #include <opencrypto/xform_auth.h>
6 
7 extern int blake2b_init_ref(blake2b_state *S, size_t outlen);
8 extern int blake2b_init_param_ref(blake2b_state *S, const blake2b_param *P);
9 extern int blake2b_init_key_ref(blake2b_state *S, size_t outlen,
10     const void *key, size_t keylen);
11 extern int blake2b_update_ref(blake2b_state *S, const uint8_t *in,
12     size_t inlen);
13 extern int blake2b_final_ref(blake2b_state *S, uint8_t *out, size_t outlen);
14 extern int blake2b_ref(uint8_t *out, const void *in, const void *key,
15     size_t outlen, size_t inlen, size_t keylen);
16 
17 extern int blake2s_init_ref(blake2s_state *S, size_t outlen);
18 extern int blake2s_init_param_ref(blake2s_state *S, const blake2s_param *P);
19 extern int blake2s_init_key_ref(blake2s_state *S, size_t outlen,
20     const void *key, size_t keylen);
21 extern int blake2s_update_ref(blake2s_state *S, const uint8_t *in,
22     size_t inlen);
23 extern int blake2s_final_ref(blake2s_state *S, uint8_t *out, size_t outlen);
24 extern int blake2s_ref(uint8_t *out, const void *in, const void *key,
25     size_t outlen, size_t inlen, size_t keylen);
26 
27 struct blake2b_xform_ctx {
28 	blake2b_state state;
29 };
30 CTASSERT(sizeof(union authctx) >= sizeof(struct blake2b_xform_ctx));
31 
32 static void
blake2b_xform_init(void * vctx)33 blake2b_xform_init(void *vctx)
34 {
35 	struct blake2b_xform_ctx *ctx = vctx;
36 	int rc;
37 
38 	rc = blake2b_init_ref(&ctx->state, BLAKE2B_OUTBYTES);
39 	if (rc != 0)
40 		panic("blake2b_init: invalid arguments");
41 }
42 
43 static void
blake2b_xform_setkey(void * vctx,const uint8_t * key,u_int klen)44 blake2b_xform_setkey(void *vctx, const uint8_t *key, u_int klen)
45 {
46 	struct blake2b_xform_ctx *ctx = vctx;
47 	int rc;
48 
49 	rc = blake2b_init_key_ref(&ctx->state, BLAKE2B_OUTBYTES, key,
50 	    klen);
51 	if (rc != 0)
52 		panic("blake2b_init_key: invalid arguments");
53 }
54 
55 static int
blake2b_xform_update(void * vctx,const void * data,u_int len)56 blake2b_xform_update(void *vctx, const void *data, u_int len)
57 {
58 	struct blake2b_xform_ctx *ctx = vctx;
59 	int rc;
60 
61 	rc = blake2b_update_ref(&ctx->state, data, len);
62 	if (rc != 0)
63 		return (EINVAL);
64 	return (0);
65 }
66 
67 static void
blake2b_xform_final(uint8_t * out,void * vctx)68 blake2b_xform_final(uint8_t *out, void *vctx)
69 {
70 	struct blake2b_xform_ctx *ctx = vctx;
71 	int rc;
72 
73 	rc = blake2b_final_ref(&ctx->state, out, BLAKE2B_OUTBYTES);
74 	if (rc != 0)
75 		panic("blake2b_final: invalid");
76 }
77 
78 const struct auth_hash auth_hash_blake2b = {
79 	.type = CRYPTO_BLAKE2B,
80 	.name = "Blake2b",
81 	.keysize = BLAKE2B_KEYBYTES,
82 	.hashsize = BLAKE2B_OUTBYTES,
83 	.ctxsize = sizeof(struct blake2b_xform_ctx),
84 	.Setkey = blake2b_xform_setkey,
85 	.Init = blake2b_xform_init,
86 	.Update = blake2b_xform_update,
87 	.Final = blake2b_xform_final,
88 };
89 
90 struct blake2s_xform_ctx {
91 	blake2s_state state;
92 };
93 CTASSERT(sizeof(union authctx) >= sizeof(struct blake2s_xform_ctx));
94 
95 static void
blake2s_xform_init(void * vctx)96 blake2s_xform_init(void *vctx)
97 {
98 	struct blake2s_xform_ctx *ctx = vctx;
99 	int rc;
100 
101 	rc = blake2s_init_ref(&ctx->state, BLAKE2S_OUTBYTES);
102 	if (rc != 0)
103 		panic("blake2s_init: invalid arguments");
104 }
105 
106 static void
blake2s_xform_setkey(void * vctx,const uint8_t * key,u_int klen)107 blake2s_xform_setkey(void *vctx, const uint8_t *key, u_int klen)
108 {
109 	struct blake2s_xform_ctx *ctx = vctx;
110 	int rc;
111 
112 	rc = blake2s_init_key_ref(&ctx->state, BLAKE2S_OUTBYTES, key,
113 	    klen);
114 	if (rc != 0)
115 		panic("blake2s_init_key: invalid arguments");
116 }
117 
118 static int
blake2s_xform_update(void * vctx,const void * data,u_int len)119 blake2s_xform_update(void *vctx, const void *data, u_int len)
120 {
121 	struct blake2s_xform_ctx *ctx = vctx;
122 	int rc;
123 
124 	rc = blake2s_update_ref(&ctx->state, data, len);
125 	if (rc != 0)
126 		return (EINVAL);
127 	return (0);
128 }
129 
130 static void
blake2s_xform_final(uint8_t * out,void * vctx)131 blake2s_xform_final(uint8_t *out, void *vctx)
132 {
133 	struct blake2s_xform_ctx *ctx = vctx;
134 	int rc;
135 
136 	rc = blake2s_final_ref(&ctx->state, out, BLAKE2S_OUTBYTES);
137 	if (rc != 0)
138 		panic("blake2s_final: invalid");
139 }
140 
141 const struct auth_hash auth_hash_blake2s = {
142 	.type = CRYPTO_BLAKE2S,
143 	.name = "Blake2s",
144 	.keysize = BLAKE2S_KEYBYTES,
145 	.hashsize = BLAKE2S_OUTBYTES,
146 	.ctxsize = sizeof(struct blake2s_xform_ctx),
147 	.Setkey = blake2s_xform_setkey,
148 	.Init = blake2s_xform_init,
149 	.Update = blake2s_xform_update,
150 	.Final = blake2s_xform_final,
151 };
152