/src/openssl/providers/implementations/macs/kmac_prov.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2018-2025 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the Apache License 2.0 (the "License"). You may not use |
5 | | * this file except in compliance with the License. You can obtain a copy |
6 | | * in the file LICENSE in the source distribution or at |
7 | | * https://www.openssl.org/source/license.html |
8 | | */ |
9 | | |
10 | | |
11 | | /* |
12 | | * See SP800-185 "Appendix A - KMAC, .... in Terms of Keccak[c]" |
13 | | * |
14 | | * Inputs are: |
15 | | * K = Key (len(K) < 2^2040 bits) |
16 | | * X = Input |
17 | | * L = Output length (0 <= L < 2^2040 bits) |
18 | | * S = Customization String Default="" (len(S) < 2^2040 bits) |
19 | | * |
20 | | * KMAC128(K, X, L, S) |
21 | | * { |
22 | | * newX = bytepad(encode_string(K), 168) || X || right_encode(L). |
23 | | * T = bytepad(encode_string("KMAC") || encode_string(S), 168). |
24 | | * return KECCAK[256](T || newX || 00, L). |
25 | | * } |
26 | | * |
27 | | * KMAC256(K, X, L, S) |
28 | | * { |
29 | | * newX = bytepad(encode_string(K), 136) || X || right_encode(L). |
30 | | * T = bytepad(encode_string("KMAC") || encode_string(S), 136). |
31 | | * return KECCAK[512](T || newX || 00, L). |
32 | | * } |
33 | | * |
34 | | * KMAC128XOF(K, X, L, S) |
35 | | * { |
36 | | * newX = bytepad(encode_string(K), 168) || X || right_encode(0). |
37 | | * T = bytepad(encode_string("KMAC") || encode_string(S), 168). |
38 | | * return KECCAK[256](T || newX || 00, L). |
39 | | * } |
40 | | * |
41 | | * KMAC256XOF(K, X, L, S) |
42 | | * { |
43 | | * newX = bytepad(encode_string(K), 136) || X || right_encode(0). |
44 | | * T = bytepad(encode_string("KMAC") || encode_string(S), 136). |
45 | | * return KECCAK[512](T || newX || 00, L). |
46 | | * } |
47 | | * |
48 | | */ |
49 | | |
50 | | #include <stdlib.h> |
51 | | #include <string.h> |
52 | | #include <openssl/core_dispatch.h> |
53 | | #include <openssl/core_names.h> |
54 | | #include <openssl/params.h> |
55 | | #include <openssl/evp.h> |
56 | | #include <openssl/err.h> |
57 | | #include <openssl/proverr.h> |
58 | | #include <openssl/fips_names.h> |
59 | | #include "prov/securitycheck.h" |
60 | | #include "prov/implementations.h" |
61 | | #include "prov/provider_ctx.h" |
62 | | #include "prov/provider_util.h" |
63 | | #include "prov/providercommon.h" |
64 | | #include "internal/cryptlib.h" /* ossl_assert */ |
65 | | |
66 | | /* |
67 | | * Forward declaration of everything implemented here. This is not strictly |
68 | | * necessary for the compiler, but provides an assurance that the signatures |
69 | | * of the functions in the dispatch table are correct. |
70 | | */ |
71 | | static OSSL_FUNC_mac_newctx_fn kmac128_new; |
72 | | static OSSL_FUNC_mac_newctx_fn kmac256_new; |
73 | | static OSSL_FUNC_mac_dupctx_fn kmac_dup; |
74 | | static OSSL_FUNC_mac_freectx_fn kmac_free; |
75 | | static OSSL_FUNC_mac_gettable_ctx_params_fn kmac_gettable_ctx_params; |
76 | | static OSSL_FUNC_mac_get_ctx_params_fn kmac_get_ctx_params; |
77 | | static OSSL_FUNC_mac_settable_ctx_params_fn kmac_settable_ctx_params; |
78 | | static OSSL_FUNC_mac_set_ctx_params_fn kmac_set_ctx_params; |
79 | | static OSSL_FUNC_mac_init_fn kmac_init; |
80 | | static OSSL_FUNC_mac_update_fn kmac_update; |
81 | | static OSSL_FUNC_mac_final_fn kmac_final; |
82 | | |
83 | | #define KMAC_MAX_BLOCKSIZE ((1600 - 128 * 2) / 8) /* 168 */ |
84 | | |
85 | | /* |
86 | | * Length encoding will be a 1 byte size + length in bits (3 bytes max) |
87 | | * This gives a range of 0..0XFFFFFF bits = 2097151 bytes). |
88 | | */ |
89 | 0 | #define KMAC_MAX_OUTPUT_LEN (0xFFFFFF / 8) |
90 | | #define KMAC_MAX_ENCODED_HEADER_LEN (1 + 3) |
91 | | |
92 | | /* |
93 | | * Restrict the maximum length of the customisation string. This must not |
94 | | * exceed 64 bits = 8k bytes. |
95 | | */ |
96 | 0 | #define KMAC_MAX_CUSTOM 512 |
97 | | |
98 | | /* Maximum size of encoded custom string */ |
99 | | #define KMAC_MAX_CUSTOM_ENCODED (KMAC_MAX_CUSTOM + KMAC_MAX_ENCODED_HEADER_LEN) |
100 | | |
101 | | /* Maximum key size in bytes = 512 (4096 bits) */ |
102 | 0 | #define KMAC_MAX_KEY 512 |
103 | 0 | #define KMAC_MIN_KEY 4 |
104 | | |
105 | | /* |
106 | | * Maximum Encoded Key size will be padded to a multiple of the blocksize |
107 | | * i.e KMAC_MAX_KEY + KMAC_MAX_ENCODED_HEADER_LEN = 512 + 4 |
108 | | * Padded to a multiple of KMAC_MAX_BLOCKSIZE |
109 | | */ |
110 | | #define KMAC_MAX_KEY_ENCODED (KMAC_MAX_BLOCKSIZE * 4) |
111 | | |
112 | | /* Fixed value of encode_string("KMAC") */ |
113 | | static const unsigned char kmac_string[] = { |
114 | | 0x01, 0x20, 0x4B, 0x4D, 0x41, 0x43 |
115 | | }; |
116 | | |
117 | | #define KMAC_FLAG_XOF_MODE 1 |
118 | | |
119 | | struct kmac_data_st { |
120 | | void *provctx; |
121 | | EVP_MD_CTX *ctx; |
122 | | PROV_DIGEST digest; |
123 | | size_t out_len; |
124 | | size_t key_len; |
125 | | size_t custom_len; |
126 | | /* If xof_mode = 1 then we use right_encode(0) */ |
127 | | int xof_mode; |
128 | | /* key and custom are stored in encoded form */ |
129 | | unsigned char key[KMAC_MAX_KEY_ENCODED]; |
130 | | unsigned char custom[KMAC_MAX_CUSTOM_ENCODED]; |
131 | | #ifdef FIPS_MODULE |
132 | | /* |
133 | | * 'internal' is set to 1 if KMAC is used inside another algorithm such as a |
134 | | * KDF. In this case it is the parent algorithm that is responsible for |
135 | | * performing any conditional FIPS indicator related checks for KMAC. |
136 | | */ |
137 | | int internal; |
138 | | #endif |
139 | | OSSL_FIPS_IND_DECLARE |
140 | | }; |
141 | | |
142 | | static int encode_string(unsigned char *out, size_t out_max_len, size_t *out_len, |
143 | | const unsigned char *in, size_t in_len); |
144 | | static int right_encode(unsigned char *out, size_t out_max_len, size_t *out_len, |
145 | | size_t bits); |
146 | | static int bytepad(unsigned char *out, size_t *out_len, |
147 | | const unsigned char *in1, size_t in1_len, |
148 | | const unsigned char *in2, size_t in2_len, |
149 | | size_t w); |
150 | | static int kmac_bytepad_encode_key(unsigned char *out, size_t out_max_len, |
151 | | size_t *out_len, |
152 | | const unsigned char *in, size_t in_len, |
153 | | size_t w); |
154 | | |
155 | | static void kmac_free(void *vmacctx) |
156 | 0 | { |
157 | 0 | struct kmac_data_st *kctx = vmacctx; |
158 | |
|
159 | 0 | if (kctx != NULL) { |
160 | 0 | EVP_MD_CTX_free(kctx->ctx); |
161 | 0 | ossl_prov_digest_reset(&kctx->digest); |
162 | 0 | OPENSSL_cleanse(kctx->key, kctx->key_len); |
163 | 0 | OPENSSL_cleanse(kctx->custom, kctx->custom_len); |
164 | 0 | OPENSSL_free(kctx); |
165 | 0 | } |
166 | 0 | } |
167 | | |
168 | | /* |
169 | | * We have KMAC implemented as a hash, which we can use instead of |
170 | | * reimplementing the EVP functionality with direct use of |
171 | | * keccak_mac_init() and friends. |
172 | | */ |
173 | | static struct kmac_data_st *kmac_new(void *provctx) |
174 | 0 | { |
175 | 0 | struct kmac_data_st *kctx; |
176 | |
|
177 | 0 | if (!ossl_prov_is_running()) |
178 | 0 | return NULL; |
179 | | |
180 | 0 | if ((kctx = OPENSSL_zalloc(sizeof(*kctx))) == NULL |
181 | 0 | || (kctx->ctx = EVP_MD_CTX_new()) == NULL) { |
182 | 0 | kmac_free(kctx); |
183 | 0 | return NULL; |
184 | 0 | } |
185 | 0 | kctx->provctx = provctx; |
186 | 0 | OSSL_FIPS_IND_INIT(kctx) |
187 | 0 | return kctx; |
188 | 0 | } |
189 | | |
190 | | #define kmac_new_list |
191 | | |
192 | | /* Machine generated by util/perl/OpenSSL/paramnames.pm */ |
193 | | #ifndef kmac_new_list |
194 | | static const OSSL_PARAM kmac_new_list[] = { |
195 | | OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_DIGEST, NULL, 0), |
196 | | OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_PROPERTIES, NULL, 0), |
197 | | OSSL_PARAM_END |
198 | | }; |
199 | | #endif |
200 | | |
201 | | #ifndef kmac_new_st |
202 | | struct kmac_new_st { |
203 | | OSSL_PARAM *digest; |
204 | | OSSL_PARAM *engine; |
205 | | OSSL_PARAM *propq; |
206 | | }; |
207 | | #endif |
208 | | |
209 | | #ifndef kmac_new_decoder |
210 | | static int kmac_new_decoder |
211 | | (const OSSL_PARAM *p, struct kmac_new_st *r) |
212 | 0 | { |
213 | 0 | const char *s; |
214 | |
|
215 | 0 | memset(r, 0, sizeof(*r)); |
216 | 0 | if (p != NULL) |
217 | 0 | for (; (s = p->key) != NULL; p++) |
218 | 0 | switch(s[0]) { |
219 | 0 | default: |
220 | 0 | break; |
221 | 0 | case 'd': |
222 | 0 | if (ossl_likely(strcmp("igest", s + 1) == 0)) { |
223 | | /* MAC_PARAM_DIGEST */ |
224 | 0 | if (ossl_unlikely(r->digest != NULL)) { |
225 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
226 | 0 | "param %s is repeated", s); |
227 | 0 | return 0; |
228 | 0 | } |
229 | 0 | r->digest = (OSSL_PARAM *)p; |
230 | 0 | } |
231 | 0 | break; |
232 | 0 | case 'e': |
233 | 0 | if (ossl_likely(strcmp("ngine", s + 1) == 0)) { |
234 | | /* ALG_PARAM_ENGINE */ |
235 | 0 | if (ossl_unlikely(r->engine != NULL)) { |
236 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
237 | 0 | "param %s is repeated", s); |
238 | 0 | return 0; |
239 | 0 | } |
240 | 0 | r->engine = (OSSL_PARAM *)p; |
241 | 0 | } |
242 | 0 | break; |
243 | 0 | case 'p': |
244 | 0 | if (ossl_likely(strcmp("roperties", s + 1) == 0)) { |
245 | | /* MAC_PARAM_PROPERTIES */ |
246 | 0 | if (ossl_unlikely(r->propq != NULL)) { |
247 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
248 | 0 | "param %s is repeated", s); |
249 | 0 | return 0; |
250 | 0 | } |
251 | 0 | r->propq = (OSSL_PARAM *)p; |
252 | 0 | } |
253 | 0 | } |
254 | 0 | return 1; |
255 | 0 | } |
256 | | #endif |
257 | | /* End of machine generated */ |
258 | | |
259 | | static void *kmac_fetch_new(void *provctx, const OSSL_PARAM *params) |
260 | 0 | { |
261 | 0 | struct kmac_data_st *kctx = kmac_new(provctx); |
262 | 0 | struct kmac_new_st p; |
263 | 0 | int md_size; |
264 | |
|
265 | 0 | if (kctx == NULL || !kmac_new_decoder(params, &p)) |
266 | 0 | return 0; |
267 | 0 | if (!ossl_prov_digest_load(&kctx->digest, p.digest, p.propq, p.engine, |
268 | 0 | PROV_LIBCTX_OF(provctx))) { |
269 | 0 | kmac_free(kctx); |
270 | 0 | return 0; |
271 | 0 | } |
272 | | |
273 | 0 | md_size = EVP_MD_get_size(ossl_prov_digest_md(&kctx->digest)); |
274 | 0 | if (md_size <= 0) { |
275 | 0 | kmac_free(kctx); |
276 | 0 | return 0; |
277 | 0 | } |
278 | 0 | kctx->out_len = (size_t)md_size; |
279 | 0 | return kctx; |
280 | 0 | } |
281 | | |
282 | | static void *kmac128_new(void *provctx) |
283 | 0 | { |
284 | 0 | static const OSSL_PARAM kmac128_params[] = { |
285 | 0 | OSSL_PARAM_utf8_string("digest", OSSL_DIGEST_NAME_KECCAK_KMAC128, |
286 | 0 | sizeof(OSSL_DIGEST_NAME_KECCAK_KMAC128)), |
287 | 0 | OSSL_PARAM_END |
288 | 0 | }; |
289 | 0 | return kmac_fetch_new(provctx, kmac128_params); |
290 | 0 | } |
291 | | |
292 | | static void *kmac256_new(void *provctx) |
293 | 0 | { |
294 | 0 | static const OSSL_PARAM kmac256_params[] = { |
295 | 0 | OSSL_PARAM_utf8_string("digest", OSSL_DIGEST_NAME_KECCAK_KMAC256, |
296 | 0 | sizeof(OSSL_DIGEST_NAME_KECCAK_KMAC256)), |
297 | 0 | OSSL_PARAM_END |
298 | 0 | }; |
299 | 0 | return kmac_fetch_new(provctx, kmac256_params); |
300 | 0 | } |
301 | | |
302 | | static void *kmac_dup(void *vsrc) |
303 | 0 | { |
304 | 0 | struct kmac_data_st *src = vsrc; |
305 | 0 | struct kmac_data_st *dst; |
306 | |
|
307 | 0 | if (!ossl_prov_is_running()) |
308 | 0 | return NULL; |
309 | | |
310 | 0 | dst = kmac_new(src->provctx); |
311 | 0 | if (dst == NULL) |
312 | 0 | return NULL; |
313 | | |
314 | 0 | if (!EVP_MD_CTX_copy(dst->ctx, src->ctx) |
315 | 0 | || !ossl_prov_digest_copy(&dst->digest, &src->digest)) { |
316 | 0 | kmac_free(dst); |
317 | 0 | return NULL; |
318 | 0 | } |
319 | | #ifdef FIPS_MODULE |
320 | | dst->internal = src->internal; |
321 | | #endif |
322 | 0 | dst->out_len = src->out_len; |
323 | 0 | dst->key_len = src->key_len; |
324 | 0 | dst->custom_len = src->custom_len; |
325 | 0 | dst->xof_mode = src->xof_mode; |
326 | 0 | memcpy(dst->key, src->key, src->key_len); |
327 | 0 | memcpy(dst->custom, src->custom, dst->custom_len); |
328 | 0 | OSSL_FIPS_IND_COPY(dst, src) |
329 | |
|
330 | 0 | return dst; |
331 | 0 | } |
332 | | |
333 | | static int kmac_setkey(struct kmac_data_st *kctx, const unsigned char *key, |
334 | | size_t keylen) |
335 | 0 | { |
336 | 0 | const EVP_MD *digest = ossl_prov_digest_md(&kctx->digest); |
337 | 0 | int w = EVP_MD_get_block_size(digest); |
338 | |
|
339 | 0 | if (keylen < KMAC_MIN_KEY || keylen > KMAC_MAX_KEY) { |
340 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH); |
341 | 0 | return 0; |
342 | 0 | } |
343 | | #ifdef FIPS_MODULE |
344 | | /* |
345 | | * Only do the key check if KMAC is fetched directly. |
346 | | * Other algorithms that embed KMAC such as SSKDF will ignore this check. |
347 | | */ |
348 | | if (!kctx->internal) { |
349 | | int approved = ossl_mac_check_key_size(keylen); |
350 | | |
351 | | if (!approved) { |
352 | | if (!OSSL_FIPS_IND_ON_UNAPPROVED(kctx, OSSL_FIPS_IND_SETTABLE1, |
353 | | PROV_LIBCTX_OF(kctx->provctx), |
354 | | "KMAC", "Key size", |
355 | | ossl_fips_config_kmac_key_check)) { |
356 | | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH); |
357 | | return 0; |
358 | | } |
359 | | } |
360 | | } |
361 | | #endif |
362 | 0 | if (w <= 0) { |
363 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_LENGTH); |
364 | 0 | return 0; |
365 | 0 | } |
366 | 0 | if (!kmac_bytepad_encode_key(kctx->key, sizeof(kctx->key), &kctx->key_len, |
367 | 0 | key, keylen, (size_t)w)) |
368 | 0 | return 0; |
369 | 0 | return 1; |
370 | 0 | } |
371 | | |
372 | | /* |
373 | | * The init() assumes that any ctrl methods are set beforehand for |
374 | | * md, key and custom. Setting the fields afterwards will have no |
375 | | * effect on the output mac. |
376 | | */ |
377 | | static int kmac_init(void *vmacctx, const unsigned char *key, |
378 | | size_t keylen, const OSSL_PARAM params[]) |
379 | 0 | { |
380 | 0 | struct kmac_data_st *kctx = vmacctx; |
381 | 0 | EVP_MD_CTX *ctx = kctx->ctx; |
382 | 0 | unsigned char *out; |
383 | 0 | size_t out_len, block_len; |
384 | 0 | int res, t; |
385 | |
|
386 | 0 | if (!ossl_prov_is_running() || !kmac_set_ctx_params(kctx, params)) |
387 | 0 | return 0; |
388 | | |
389 | 0 | if (key != NULL) { |
390 | 0 | if (!kmac_setkey(kctx, key, keylen)) |
391 | 0 | return 0; |
392 | 0 | } else if (kctx->key_len == 0) { |
393 | | /* Check key has been set */ |
394 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET); |
395 | 0 | return 0; |
396 | 0 | } |
397 | 0 | if (!EVP_DigestInit_ex(kctx->ctx, ossl_prov_digest_md(&kctx->digest), |
398 | 0 | NULL)) |
399 | 0 | return 0; |
400 | | |
401 | 0 | t = EVP_MD_get_block_size(ossl_prov_digest_md(&kctx->digest)); |
402 | 0 | if (t <= 0) { |
403 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_LENGTH); |
404 | 0 | return 0; |
405 | 0 | } |
406 | 0 | block_len = t; |
407 | | |
408 | | /* Set default custom string if it is not already set */ |
409 | 0 | if (kctx->custom_len == 0) { |
410 | 0 | const OSSL_PARAM cparams[] = { |
411 | 0 | OSSL_PARAM_octet_string(OSSL_MAC_PARAM_CUSTOM, "", 0), |
412 | 0 | OSSL_PARAM_END |
413 | 0 | }; |
414 | 0 | (void)kmac_set_ctx_params(kctx, cparams); |
415 | 0 | } |
416 | |
|
417 | 0 | if (!bytepad(NULL, &out_len, kmac_string, sizeof(kmac_string), |
418 | 0 | kctx->custom, kctx->custom_len, block_len)) { |
419 | 0 | ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR); |
420 | 0 | return 0; |
421 | 0 | } |
422 | 0 | out = OPENSSL_malloc(out_len); |
423 | 0 | if (out == NULL) |
424 | 0 | return 0; |
425 | 0 | res = bytepad(out, NULL, kmac_string, sizeof(kmac_string), |
426 | 0 | kctx->custom, kctx->custom_len, block_len) |
427 | 0 | && EVP_DigestUpdate(ctx, out, out_len) |
428 | 0 | && EVP_DigestUpdate(ctx, kctx->key, kctx->key_len); |
429 | 0 | OPENSSL_free(out); |
430 | 0 | return res; |
431 | 0 | } |
432 | | |
433 | | static int kmac_update(void *vmacctx, const unsigned char *data, |
434 | | size_t datalen) |
435 | 0 | { |
436 | 0 | struct kmac_data_st *kctx = vmacctx; |
437 | |
|
438 | 0 | return EVP_DigestUpdate(kctx->ctx, data, datalen); |
439 | 0 | } |
440 | | |
441 | | static int kmac_final(void *vmacctx, unsigned char *out, size_t *outl, |
442 | | size_t outsize) |
443 | 0 | { |
444 | 0 | struct kmac_data_st *kctx = vmacctx; |
445 | 0 | EVP_MD_CTX *ctx = kctx->ctx; |
446 | 0 | size_t lbits, len; |
447 | 0 | unsigned char encoded_outlen[KMAC_MAX_ENCODED_HEADER_LEN]; |
448 | 0 | int ok; |
449 | |
|
450 | 0 | if (!ossl_prov_is_running()) |
451 | 0 | return 0; |
452 | | |
453 | | /* KMAC XOF mode sets the encoded length to 0 */ |
454 | 0 | lbits = (kctx->xof_mode ? 0 : (kctx->out_len * 8)); |
455 | |
|
456 | 0 | ok = right_encode(encoded_outlen, sizeof(encoded_outlen), &len, lbits) |
457 | 0 | && EVP_DigestUpdate(ctx, encoded_outlen, len) |
458 | 0 | && EVP_DigestFinalXOF(ctx, out, kctx->out_len); |
459 | 0 | *outl = kctx->out_len; |
460 | 0 | return ok; |
461 | 0 | } |
462 | | |
463 | | /* Machine generated by util/perl/OpenSSL/paramnames.pm */ |
464 | | #ifndef kmac_get_ctx_params_list |
465 | | static const OSSL_PARAM kmac_get_ctx_params_list[] = { |
466 | | OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL), |
467 | | OSSL_PARAM_size_t(OSSL_MAC_PARAM_BLOCK_SIZE, NULL), |
468 | | # if defined(FIPS_MODULE) |
469 | | OSSL_PARAM_int(OSSL_ALG_PARAM_FIPS_APPROVED_INDICATOR, NULL), |
470 | | # endif |
471 | | OSSL_PARAM_END |
472 | | }; |
473 | | #endif |
474 | | |
475 | | #ifndef kmac_get_ctx_params_st |
476 | | struct kmac_get_ctx_params_st { |
477 | | OSSL_PARAM *bsize; |
478 | | # if defined(FIPS_MODULE) |
479 | | OSSL_PARAM *ind; |
480 | | # endif |
481 | | OSSL_PARAM *size; |
482 | | }; |
483 | | #endif |
484 | | |
485 | | #ifndef kmac_get_ctx_params_decoder |
486 | | static int kmac_get_ctx_params_decoder |
487 | | (const OSSL_PARAM *p, struct kmac_get_ctx_params_st *r) |
488 | 0 | { |
489 | 0 | const char *s; |
490 | |
|
491 | 0 | memset(r, 0, sizeof(*r)); |
492 | 0 | if (p != NULL) |
493 | 0 | for (; (s = p->key) != NULL; p++) |
494 | 0 | switch(s[0]) { |
495 | 0 | default: |
496 | 0 | break; |
497 | 0 | case 'b': |
498 | 0 | if (ossl_likely(strcmp("lock-size", s + 1) == 0)) { |
499 | | /* MAC_PARAM_BLOCK_SIZE */ |
500 | 0 | if (ossl_unlikely(r->bsize != NULL)) { |
501 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
502 | 0 | "param %s is repeated", s); |
503 | 0 | return 0; |
504 | 0 | } |
505 | 0 | r->bsize = (OSSL_PARAM *)p; |
506 | 0 | } |
507 | 0 | break; |
508 | 0 | case 'f': |
509 | | # if defined(FIPS_MODULE) |
510 | | if (ossl_likely(strcmp("ips-indicator", s + 1) == 0)) { |
511 | | /* ALG_PARAM_FIPS_APPROVED_INDICATOR */ |
512 | | if (ossl_unlikely(r->ind != NULL)) { |
513 | | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
514 | | "param %s is repeated", s); |
515 | | return 0; |
516 | | } |
517 | | r->ind = (OSSL_PARAM *)p; |
518 | | } |
519 | | # endif |
520 | 0 | break; |
521 | 0 | case 's': |
522 | 0 | if (ossl_likely(strcmp("ize", s + 1) == 0)) { |
523 | | /* MAC_PARAM_SIZE */ |
524 | 0 | if (ossl_unlikely(r->size != NULL)) { |
525 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
526 | 0 | "param %s is repeated", s); |
527 | 0 | return 0; |
528 | 0 | } |
529 | 0 | r->size = (OSSL_PARAM *)p; |
530 | 0 | } |
531 | 0 | } |
532 | 0 | return 1; |
533 | 0 | } |
534 | | #endif |
535 | | /* End of machine generated */ |
536 | | |
537 | | static const OSSL_PARAM *kmac_gettable_ctx_params(ossl_unused void *ctx, |
538 | | ossl_unused void *provctx) |
539 | 0 | { |
540 | 0 | return kmac_get_ctx_params_list; |
541 | 0 | } |
542 | | |
543 | | static int kmac_get_ctx_params(void *vmacctx, OSSL_PARAM params[]) |
544 | 0 | { |
545 | 0 | struct kmac_data_st *kctx = vmacctx; |
546 | 0 | struct kmac_get_ctx_params_st p; |
547 | 0 | int sz; |
548 | |
|
549 | 0 | if (kctx == NULL || !kmac_get_ctx_params_decoder(params, &p)) |
550 | 0 | return 0; |
551 | | |
552 | 0 | if (p.size != NULL && !OSSL_PARAM_set_size_t(p.size, kctx->out_len)) |
553 | 0 | return 0; |
554 | | |
555 | 0 | if (p.bsize != NULL) { |
556 | 0 | sz = EVP_MD_block_size(ossl_prov_digest_md(&kctx->digest)); |
557 | 0 | if (!OSSL_PARAM_set_int(p.bsize, sz)) |
558 | 0 | return 0; |
559 | 0 | } |
560 | | |
561 | 0 | if (!OSSL_FIPS_IND_GET_CTX_FROM_PARAM(kctx, p.ind)) |
562 | 0 | return 0; |
563 | | |
564 | 0 | return 1; |
565 | 0 | } |
566 | | |
567 | | /* Machine generated by util/perl/OpenSSL/paramnames.pm */ |
568 | | #ifndef kmac_set_ctx_params_list |
569 | | static const OSSL_PARAM kmac_set_ctx_params_list[] = { |
570 | | OSSL_PARAM_int(OSSL_MAC_PARAM_XOF, NULL), |
571 | | OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL), |
572 | | OSSL_PARAM_octet_string(OSSL_MAC_PARAM_KEY, NULL, 0), |
573 | | OSSL_PARAM_octet_string(OSSL_MAC_PARAM_CUSTOM, NULL, 0), |
574 | | # if defined(FIPS_MODULE) |
575 | | OSSL_PARAM_int(OSSL_MAC_PARAM_FIPS_KEY_CHECK, NULL), |
576 | | # endif |
577 | | # if defined(FIPS_MODULE) |
578 | | OSSL_PARAM_int(OSSL_MAC_PARAM_FIPS_NO_SHORT_MAC, NULL), |
579 | | # endif |
580 | | OSSL_PARAM_END |
581 | | }; |
582 | | #endif |
583 | | |
584 | | #ifndef kmac_set_ctx_params_st |
585 | | struct kmac_set_ctx_params_st { |
586 | | OSSL_PARAM *custom; |
587 | | # if defined(FIPS_MODULE) |
588 | | OSSL_PARAM *ind_k; |
589 | | # endif |
590 | | # if defined(FIPS_MODULE) |
591 | | OSSL_PARAM *ind_sht; |
592 | | # endif |
593 | | OSSL_PARAM *key; |
594 | | OSSL_PARAM *size; |
595 | | OSSL_PARAM *xof; |
596 | | }; |
597 | | #endif |
598 | | |
599 | | #ifndef kmac_set_ctx_params_decoder |
600 | | static int kmac_set_ctx_params_decoder |
601 | | (const OSSL_PARAM *p, struct kmac_set_ctx_params_st *r) |
602 | 0 | { |
603 | 0 | const char *s; |
604 | |
|
605 | 0 | memset(r, 0, sizeof(*r)); |
606 | 0 | if (p != NULL) |
607 | 0 | for (; (s = p->key) != NULL; p++) |
608 | 0 | switch(s[0]) { |
609 | 0 | default: |
610 | 0 | break; |
611 | 0 | case 'c': |
612 | 0 | if (ossl_likely(strcmp("ustom", s + 1) == 0)) { |
613 | | /* MAC_PARAM_CUSTOM */ |
614 | 0 | if (ossl_unlikely(r->custom != NULL)) { |
615 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
616 | 0 | "param %s is repeated", s); |
617 | 0 | return 0; |
618 | 0 | } |
619 | 0 | r->custom = (OSSL_PARAM *)p; |
620 | 0 | } |
621 | 0 | break; |
622 | 0 | case 'k': |
623 | 0 | switch(s[1]) { |
624 | 0 | default: |
625 | 0 | break; |
626 | 0 | case 'e': |
627 | 0 | switch(s[2]) { |
628 | 0 | default: |
629 | 0 | break; |
630 | 0 | case 'y': |
631 | 0 | switch(s[3]) { |
632 | 0 | default: |
633 | 0 | break; |
634 | 0 | case '-': |
635 | | # if defined(FIPS_MODULE) |
636 | | if (ossl_likely(strcmp("check", s + 4) == 0)) { |
637 | | /* MAC_PARAM_FIPS_KEY_CHECK */ |
638 | | if (ossl_unlikely(r->ind_k != NULL)) { |
639 | | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
640 | | "param %s is repeated", s); |
641 | | return 0; |
642 | | } |
643 | | r->ind_k = (OSSL_PARAM *)p; |
644 | | } |
645 | | # endif |
646 | 0 | break; |
647 | 0 | case '\0': |
648 | 0 | if (ossl_unlikely(r->key != NULL)) { |
649 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
650 | 0 | "param %s is repeated", s); |
651 | 0 | return 0; |
652 | 0 | } |
653 | 0 | r->key = (OSSL_PARAM *)p; |
654 | 0 | } |
655 | 0 | } |
656 | 0 | } |
657 | 0 | break; |
658 | 0 | case 'n': |
659 | | # if defined(FIPS_MODULE) |
660 | | if (ossl_likely(strcmp("o-short-mac", s + 1) == 0)) { |
661 | | /* MAC_PARAM_FIPS_NO_SHORT_MAC */ |
662 | | if (ossl_unlikely(r->ind_sht != NULL)) { |
663 | | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
664 | | "param %s is repeated", s); |
665 | | return 0; |
666 | | } |
667 | | r->ind_sht = (OSSL_PARAM *)p; |
668 | | } |
669 | | # endif |
670 | 0 | break; |
671 | 0 | case 's': |
672 | 0 | if (ossl_likely(strcmp("ize", s + 1) == 0)) { |
673 | | /* MAC_PARAM_SIZE */ |
674 | 0 | if (ossl_unlikely(r->size != NULL)) { |
675 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
676 | 0 | "param %s is repeated", s); |
677 | 0 | return 0; |
678 | 0 | } |
679 | 0 | r->size = (OSSL_PARAM *)p; |
680 | 0 | } |
681 | 0 | break; |
682 | 0 | case 'x': |
683 | 0 | if (ossl_likely(strcmp("of", s + 1) == 0)) { |
684 | | /* MAC_PARAM_XOF */ |
685 | 0 | if (ossl_unlikely(r->xof != NULL)) { |
686 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
687 | 0 | "param %s is repeated", s); |
688 | 0 | return 0; |
689 | 0 | } |
690 | 0 | r->xof = (OSSL_PARAM *)p; |
691 | 0 | } |
692 | 0 | } |
693 | 0 | return 1; |
694 | 0 | } |
695 | | #endif |
696 | | /* End of machine generated */ |
697 | | |
698 | | static const OSSL_PARAM *kmac_settable_ctx_params(ossl_unused void *ctx, |
699 | | ossl_unused void *provctx) |
700 | 0 | { |
701 | 0 | return kmac_set_ctx_params_list; |
702 | 0 | } |
703 | | |
704 | | /* |
705 | | * The following params can be set any time before final(): |
706 | | * - "outlen" or "size": The requested output length. |
707 | | * - "xof": If set, this indicates that right_encoded(0) |
708 | | * is part of the digested data, otherwise it |
709 | | * uses right_encoded(requested output length). |
710 | | * |
711 | | * All other params should be set before init(). |
712 | | */ |
713 | | static int kmac_set_ctx_params(void *vmacctx, const OSSL_PARAM *params) |
714 | 0 | { |
715 | 0 | struct kmac_data_st *kctx = vmacctx; |
716 | 0 | struct kmac_set_ctx_params_st p; |
717 | |
|
718 | 0 | if (kctx == NULL || !kmac_set_ctx_params_decoder(params, &p)) |
719 | 0 | return 0; |
720 | | |
721 | 0 | if (!OSSL_FIPS_IND_SET_CTX_FROM_PARAM(kctx, OSSL_FIPS_IND_SETTABLE0, |
722 | 0 | p.ind_sht)) |
723 | 0 | return 0; |
724 | 0 | if (!OSSL_FIPS_IND_SET_CTX_FROM_PARAM(kctx, OSSL_FIPS_IND_SETTABLE1, p.ind_k)) |
725 | 0 | return 0; |
726 | | |
727 | 0 | if (p.xof != NULL && !OSSL_PARAM_get_int(p.xof, &kctx->xof_mode)) |
728 | 0 | return 0; |
729 | | |
730 | 0 | if (p.size != NULL) { |
731 | 0 | size_t sz = 0; |
732 | |
|
733 | 0 | if (!OSSL_PARAM_get_size_t(p.size, &sz)) |
734 | 0 | return 0; |
735 | 0 | if (sz > KMAC_MAX_OUTPUT_LEN) { |
736 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_OUTPUT_LENGTH); |
737 | 0 | return 0; |
738 | 0 | } |
739 | | #ifdef FIPS_MODULE |
740 | | /* SP 800-185 8.4.2 mandates a minimum of 32 bits of output */ |
741 | | if (sz < 32 / 8) { |
742 | | if (!OSSL_FIPS_IND_ON_UNAPPROVED(kctx, OSSL_FIPS_IND_SETTABLE0, |
743 | | PROV_LIBCTX_OF(kctx->provctx), |
744 | | "KMAC", "length", |
745 | | ossl_fips_config_no_short_mac)) { |
746 | | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_OUTPUT_LENGTH); |
747 | | return 0; |
748 | | } |
749 | | } |
750 | | #endif |
751 | 0 | kctx->out_len = sz; |
752 | 0 | } |
753 | | |
754 | 0 | if (p.key != NULL) |
755 | 0 | if (p.key->data_type != OSSL_PARAM_OCTET_STRING |
756 | 0 | || !kmac_setkey(kctx, p.key->data, p.key->data_size)) |
757 | 0 | return 0; |
758 | | |
759 | 0 | if (p.custom != NULL) { |
760 | 0 | if (p.custom->data_type != OSSL_PARAM_OCTET_STRING) |
761 | 0 | return 0; |
762 | 0 | if (p.custom->data_size > KMAC_MAX_CUSTOM) { |
763 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_CUSTOM_LENGTH); |
764 | 0 | return 0; |
765 | 0 | } |
766 | 0 | if (!encode_string(kctx->custom, sizeof(kctx->custom), &kctx->custom_len, |
767 | 0 | p.custom->data, p.custom->data_size)) |
768 | 0 | return 0; |
769 | 0 | } |
770 | | |
771 | 0 | return 1; |
772 | 0 | } |
773 | | |
774 | | /* Encoding/Padding Methods. */ |
775 | | |
776 | | /* Returns the number of bytes required to store 'bits' into a byte array */ |
777 | | static unsigned int get_encode_size(size_t bits) |
778 | 0 | { |
779 | 0 | unsigned int cnt = 0, sz = sizeof(size_t); |
780 | |
|
781 | 0 | while (bits && (cnt < sz)) { |
782 | 0 | ++cnt; |
783 | 0 | bits >>= 8; |
784 | 0 | } |
785 | | /* If bits is zero 1 byte is required */ |
786 | 0 | if (cnt == 0) |
787 | 0 | cnt = 1; |
788 | 0 | return cnt; |
789 | 0 | } |
790 | | |
791 | | /* |
792 | | * Convert an integer into bytes . The number of bytes is appended |
793 | | * to the end of the buffer. Returns an array of bytes 'out' of size |
794 | | * *out_len. |
795 | | * |
796 | | * e.g if bits = 32, out[2] = { 0x20, 0x01 } |
797 | | */ |
798 | | static int right_encode(unsigned char *out, size_t out_max_len, size_t *out_len, |
799 | | size_t bits) |
800 | 0 | { |
801 | 0 | unsigned int len = get_encode_size(bits); |
802 | 0 | int i; |
803 | |
|
804 | 0 | if (len >= out_max_len) { |
805 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_LENGTH_TOO_LARGE); |
806 | 0 | return 0; |
807 | 0 | } |
808 | | |
809 | | /* MSB's are at the start of the bytes array */ |
810 | 0 | for (i = len - 1; i >= 0; --i) { |
811 | 0 | out[i] = (unsigned char)(bits & 0xFF); |
812 | 0 | bits >>= 8; |
813 | 0 | } |
814 | | /* Tack the length onto the end */ |
815 | 0 | out[len] = (unsigned char)len; |
816 | | |
817 | | /* The Returned length includes the tacked on byte */ |
818 | 0 | *out_len = len + 1; |
819 | 0 | return 1; |
820 | 0 | } |
821 | | |
822 | | /* |
823 | | * Encodes a string with a left encoded length added. Note that the |
824 | | * in_len is converted to bits (*8). |
825 | | * |
826 | | * e.g- in="KMAC" gives out[6] = { 0x01, 0x20, 0x4B, 0x4D, 0x41, 0x43 } |
827 | | * len bits K M A C |
828 | | */ |
829 | | static int encode_string(unsigned char *out, size_t out_max_len, size_t *out_len, |
830 | | const unsigned char *in, size_t in_len) |
831 | 0 | { |
832 | 0 | if (in == NULL) { |
833 | 0 | *out_len = 0; |
834 | 0 | } else { |
835 | 0 | size_t i, bits, len, sz; |
836 | |
|
837 | 0 | bits = 8 * in_len; |
838 | 0 | len = get_encode_size(bits); |
839 | 0 | sz = 1 + len + in_len; |
840 | |
|
841 | 0 | if (sz > out_max_len) { |
842 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_LENGTH_TOO_LARGE); |
843 | 0 | return 0; |
844 | 0 | } |
845 | | |
846 | 0 | out[0] = (unsigned char)len; |
847 | 0 | for (i = len; i > 0; --i) { |
848 | 0 | out[i] = (bits & 0xFF); |
849 | 0 | bits >>= 8; |
850 | 0 | } |
851 | 0 | memcpy(out + len + 1, in, in_len); |
852 | 0 | *out_len = sz; |
853 | 0 | } |
854 | 0 | return 1; |
855 | 0 | } |
856 | | |
857 | | /* |
858 | | * Returns a zero padded encoding of the inputs in1 and an optional |
859 | | * in2 (can be NULL). The padded output must be a multiple of the blocksize 'w'. |
860 | | * The value of w is in bytes (< 256). |
861 | | * |
862 | | * The returned output is: |
863 | | * zero_padded(multiple of w, (left_encode(w) || in1 [|| in2]) |
864 | | */ |
865 | | static int bytepad(unsigned char *out, size_t *out_len, |
866 | | const unsigned char *in1, size_t in1_len, |
867 | | const unsigned char *in2, size_t in2_len, size_t w) |
868 | 0 | { |
869 | 0 | size_t len; |
870 | 0 | unsigned char *p = out; |
871 | 0 | size_t sz = w; |
872 | |
|
873 | 0 | if (out == NULL) { |
874 | 0 | if (out_len == NULL) { |
875 | 0 | ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER); |
876 | 0 | return 0; |
877 | 0 | } |
878 | 0 | sz = 2 + in1_len + (in2 != NULL ? in2_len : 0); |
879 | 0 | *out_len = (sz + w - 1) / w * w; |
880 | 0 | return 1; |
881 | 0 | } |
882 | | |
883 | 0 | if (!ossl_assert(w <= 255)) |
884 | 0 | return 0; |
885 | | |
886 | | /* Left encoded w */ |
887 | 0 | *p++ = 1; |
888 | 0 | *p++ = (unsigned char)w; |
889 | | /* || in1 */ |
890 | 0 | memcpy(p, in1, in1_len); |
891 | 0 | p += in1_len; |
892 | | /* [ || in2 ] */ |
893 | 0 | if (in2 != NULL && in2_len > 0) { |
894 | 0 | memcpy(p, in2, in2_len); |
895 | 0 | p += in2_len; |
896 | 0 | } |
897 | | /* Figure out the pad size (divisible by w) */ |
898 | 0 | len = p - out; |
899 | 0 | sz = (len + w - 1) / w * w; |
900 | | /* zero pad the end of the buffer */ |
901 | 0 | if (sz != len) |
902 | 0 | memset(p, 0, sz - len); |
903 | 0 | if (out_len != NULL) |
904 | 0 | *out_len = sz; |
905 | 0 | return 1; |
906 | 0 | } |
907 | | |
908 | | /* Returns out = bytepad(encode_string(in), w) */ |
909 | | static int kmac_bytepad_encode_key(unsigned char *out, size_t out_max_len, |
910 | | size_t *out_len, |
911 | | const unsigned char *in, size_t in_len, |
912 | | size_t w) |
913 | 0 | { |
914 | 0 | unsigned char tmp[KMAC_MAX_KEY + KMAC_MAX_ENCODED_HEADER_LEN]; |
915 | 0 | size_t tmp_len; |
916 | |
|
917 | 0 | if (!encode_string(tmp, sizeof(tmp), &tmp_len, in, in_len)) |
918 | 0 | return 0; |
919 | 0 | if (!bytepad(NULL, out_len, tmp, tmp_len, NULL, 0, w)) |
920 | 0 | return 0; |
921 | 0 | if (!ossl_assert(*out_len <= out_max_len)) |
922 | 0 | return 0; |
923 | 0 | return bytepad(out, NULL, tmp, tmp_len, NULL, 0, w); |
924 | 0 | } |
925 | | |
926 | | #define IMPLEMENT_KMAC_TABLE(size, funcname, newname) \ |
927 | | const OSSL_DISPATCH ossl_kmac##size##_##funcname[] = \ |
928 | | { \ |
929 | | { OSSL_FUNC_MAC_NEWCTX, (void (*)(void))kmac##size##_##newname }, \ |
930 | | { OSSL_FUNC_MAC_DUPCTX, (void (*)(void))kmac_dup }, \ |
931 | | { OSSL_FUNC_MAC_FREECTX, (void (*)(void))kmac_free }, \ |
932 | | { OSSL_FUNC_MAC_INIT, (void (*)(void))kmac_init }, \ |
933 | | { OSSL_FUNC_MAC_UPDATE, (void (*)(void))kmac_update }, \ |
934 | | { OSSL_FUNC_MAC_FINAL, (void (*)(void))kmac_final }, \ |
935 | | { OSSL_FUNC_MAC_GETTABLE_CTX_PARAMS, \ |
936 | | (void (*)(void))kmac_gettable_ctx_params }, \ |
937 | | { OSSL_FUNC_MAC_GET_CTX_PARAMS, (void (*)(void))kmac_get_ctx_params }, \ |
938 | | { OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS, \ |
939 | | (void (*)(void))kmac_settable_ctx_params }, \ |
940 | | { OSSL_FUNC_MAC_SET_CTX_PARAMS, (void (*)(void))kmac_set_ctx_params }, \ |
941 | | OSSL_DISPATCH_END \ |
942 | | } |
943 | | |
944 | | #define KMAC_TABLE(size) IMPLEMENT_KMAC_TABLE(size, functions, new) |
945 | | |
946 | | KMAC_TABLE(128); |
947 | | KMAC_TABLE(256); |
948 | | |
949 | | #ifdef FIPS_MODULE |
950 | | # define KMAC_INTERNAL_TABLE(size) \ |
951 | | static OSSL_FUNC_mac_newctx_fn kmac##size##_internal_new; \ |
952 | | static void *kmac##size##_internal_new(void *provctx) \ |
953 | | { \ |
954 | | struct kmac_data_st *macctx = kmac##size##_new(provctx); \ |
955 | | \ |
956 | | if (macctx != NULL) \ |
957 | | macctx->internal = 1; \ |
958 | | return macctx; \ |
959 | | } \ |
960 | | IMPLEMENT_KMAC_TABLE(size, internal_functions, internal_new) |
961 | | |
962 | | KMAC_INTERNAL_TABLE(128); |
963 | | KMAC_INTERNAL_TABLE(256); |
964 | | #endif /* FIPS_MODULE */ |