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