/src/openssl/providers/implementations/macs/kmac_prov.c
Line | Count | Source |
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 | | * See SP800-185 "Appendix A - KMAC, .... in Terms of Keccak[c]" |
12 | | * |
13 | | * Inputs are: |
14 | | * K = Key (len(K) < 2^2040 bits) |
15 | | * X = Input |
16 | | * L = Output length (0 <= L < 2^2040 bits) |
17 | | * S = Customization String Default="" (len(S) < 2^2040 bits) |
18 | | * |
19 | | * KMAC128(K, X, L, S) |
20 | | * { |
21 | | * newX = bytepad(encode_string(K), 168) || X || right_encode(L). |
22 | | * T = bytepad(encode_string("KMAC") || encode_string(S), 168). |
23 | | * return KECCAK[256](T || newX || 00, L). |
24 | | * } |
25 | | * |
26 | | * KMAC256(K, X, L, S) |
27 | | * { |
28 | | * newX = bytepad(encode_string(K), 136) || X || right_encode(L). |
29 | | * T = bytepad(encode_string("KMAC") || encode_string(S), 136). |
30 | | * return KECCAK[512](T || newX || 00, L). |
31 | | * } |
32 | | * |
33 | | * KMAC128XOF(K, X, L, S) |
34 | | * { |
35 | | * newX = bytepad(encode_string(K), 168) || X || right_encode(0). |
36 | | * T = bytepad(encode_string("KMAC") || encode_string(S), 168). |
37 | | * return KECCAK[256](T || newX || 00, L). |
38 | | * } |
39 | | * |
40 | | * KMAC256XOF(K, X, L, S) |
41 | | * { |
42 | | * newX = bytepad(encode_string(K), 136) || X || right_encode(0). |
43 | | * T = bytepad(encode_string("KMAC") || encode_string(S), 136). |
44 | | * return KECCAK[512](T || newX || 00, L). |
45 | | * } |
46 | | * |
47 | | */ |
48 | | |
49 | | #include <stdlib.h> |
50 | | #include <string.h> |
51 | | #include <openssl/core_dispatch.h> |
52 | | #include <openssl/core_names.h> |
53 | | #include <openssl/params.h> |
54 | | #include <openssl/evp.h> |
55 | | #include <openssl/err.h> |
56 | | #include <openssl/proverr.h> |
57 | | #include <openssl/fips_names.h> |
58 | | #include "prov/securitycheck.h" |
59 | | #include "prov/implementations.h" |
60 | | #include "prov/provider_ctx.h" |
61 | | #include "prov/provider_util.h" |
62 | | #include "prov/providercommon.h" |
63 | | #include "internal/cryptlib.h" /* ossl_assert */ |
64 | | #include "providers/implementations/macs/kmac_prov.inc" |
65 | | #include "crypto/sha.h" |
66 | | |
67 | | /* |
68 | | * Forward declaration of everything implemented here. This is not strictly |
69 | | * necessary for the compiler, but provides an assurance that the signatures |
70 | | * of the functions in the dispatch table are correct. |
71 | | */ |
72 | | static OSSL_FUNC_mac_newctx_fn kmac128_new; |
73 | | static OSSL_FUNC_mac_newctx_fn kmac256_new; |
74 | | static OSSL_FUNC_mac_dupctx_fn kmac_dup; |
75 | | static OSSL_FUNC_mac_freectx_fn kmac_free; |
76 | | static OSSL_FUNC_mac_gettable_ctx_params_fn kmac_gettable_ctx_params; |
77 | | static OSSL_FUNC_mac_get_ctx_params_fn kmac_get_ctx_params; |
78 | | static OSSL_FUNC_mac_settable_ctx_params_fn kmac_settable_ctx_params; |
79 | | static OSSL_FUNC_mac_set_ctx_params_fn kmac_set_ctx_params; |
80 | | static OSSL_FUNC_mac_init_fn kmac_init; |
81 | | static OSSL_FUNC_mac_update_fn kmac_update; |
82 | | static OSSL_FUNC_mac_final_fn kmac_final; |
83 | | |
84 | | #define KMAC_MAX_BLOCKSIZE ((1600 - 128 * 2) / 8) /* 168 */ |
85 | | |
86 | | /* |
87 | | * Length encoding will be a 1 byte size + length in bits (3 bytes max) |
88 | | * This gives a range of 0..0XFFFFFF bits = 2097151 bytes). |
89 | | */ |
90 | 0 | #define KMAC_MAX_OUTPUT_LEN (0xFFFFFF / 8) |
91 | | #define KMAC_MAX_ENCODED_HEADER_LEN (1 + 3) |
92 | | |
93 | | /* |
94 | | * Restrict the maximum length of the customisation string. This must not |
95 | | * exceed 64 bits = 8k bytes. |
96 | | */ |
97 | 0 | #define KMAC_MAX_CUSTOM 512 |
98 | | |
99 | | /* Maximum size of encoded custom string */ |
100 | | #define KMAC_MAX_CUSTOM_ENCODED (KMAC_MAX_CUSTOM + KMAC_MAX_ENCODED_HEADER_LEN) |
101 | | |
102 | | /* Maximum key size in bytes = 512 (4096 bits) */ |
103 | 0 | #define KMAC_MAX_KEY 512 |
104 | 0 | #define KMAC_MIN_KEY 4 |
105 | | |
106 | | /* |
107 | | * Maximum Encoded Key size will be padded to a multiple of the blocksize |
108 | | * i.e KMAC_MAX_KEY + KMAC_MAX_ENCODED_HEADER_LEN = 512 + 4 |
109 | | * Padded to a multiple of KMAC_MAX_BLOCKSIZE |
110 | | */ |
111 | | #define KMAC_MAX_KEY_ENCODED (KMAC_MAX_BLOCKSIZE * 4) |
112 | | |
113 | | /* Fixed value of encode_string("KMAC") */ |
114 | | static const unsigned char kmac_string[] = { |
115 | | 0x01, 0x20, 0x4B, 0x4D, 0x41, 0x43 |
116 | | }; |
117 | | |
118 | | #define KMAC_FLAG_XOF_MODE 1 |
119 | | |
120 | | struct kmac_data_st { |
121 | | void *provctx; |
122 | | EVP_MD_CTX *ctx; |
123 | | PROV_DIGEST digest; |
124 | | size_t out_len; |
125 | | size_t key_len; |
126 | | size_t custom_len; |
127 | | /* If xof_mode = 1 then we use right_encode(0) */ |
128 | | int xof_mode; |
129 | | /* key and custom are stored in encoded form */ |
130 | | unsigned char key[KMAC_MAX_KEY_ENCODED]; |
131 | | unsigned char custom[KMAC_MAX_CUSTOM_ENCODED]; |
132 | | #ifdef FIPS_MODULE |
133 | | /* |
134 | | * 'internal' is set to 1 if KMAC is used inside another algorithm such as a |
135 | | * KDF. In this case it is the parent algorithm that is responsible for |
136 | | * performing any conditional FIPS indicator related checks for KMAC. |
137 | | */ |
138 | | int internal; |
139 | | #endif |
140 | | OSSL_FIPS_IND_DECLARE |
141 | | }; |
142 | | |
143 | | static int kmac_bytepad_encode_key(unsigned char *out, size_t out_max_len, |
144 | | size_t *out_len, const unsigned char *in, size_t in_len, size_t w); |
145 | | |
146 | | static void kmac_free(void *vmacctx) |
147 | 0 | { |
148 | 0 | struct kmac_data_st *kctx = vmacctx; |
149 | |
|
150 | 0 | if (kctx != NULL) { |
151 | 0 | EVP_MD_CTX_free(kctx->ctx); |
152 | 0 | ossl_prov_digest_reset(&kctx->digest); |
153 | 0 | OPENSSL_cleanse(kctx->key, kctx->key_len); |
154 | 0 | OPENSSL_cleanse(kctx->custom, kctx->custom_len); |
155 | 0 | OPENSSL_free(kctx); |
156 | 0 | } |
157 | 0 | } |
158 | | |
159 | | /* |
160 | | * We have KMAC implemented as a hash, which we can use instead of |
161 | | * reimplementing the EVP functionality with direct use of |
162 | | * keccak_mac_init() and friends. |
163 | | */ |
164 | | static struct kmac_data_st *kmac_new(void *provctx) |
165 | 0 | { |
166 | 0 | struct kmac_data_st *kctx; |
167 | |
|
168 | 0 | if (!ossl_prov_is_running()) |
169 | 0 | return NULL; |
170 | | |
171 | 0 | if ((kctx = OPENSSL_zalloc(sizeof(*kctx))) == NULL |
172 | 0 | || (kctx->ctx = EVP_MD_CTX_new()) == NULL) { |
173 | 0 | kmac_free(kctx); |
174 | 0 | return NULL; |
175 | 0 | } |
176 | 0 | kctx->provctx = provctx; |
177 | 0 | OSSL_FIPS_IND_INIT(kctx) |
178 | 0 | return kctx; |
179 | 0 | } |
180 | | |
181 | | #define kmac_new_list |
182 | | |
183 | | static void *kmac_fetch_new(void *provctx, const OSSL_PARAM *params) |
184 | 0 | { |
185 | 0 | struct kmac_data_st *kctx = kmac_new(provctx); |
186 | 0 | struct kmac_new_st p; |
187 | 0 | int md_size; |
188 | |
|
189 | 0 | if (kctx == NULL) |
190 | 0 | return 0; |
191 | 0 | if (!kmac_new_decoder(params, &p)) |
192 | 0 | goto err; |
193 | 0 | if (!ossl_prov_digest_load(&kctx->digest, p.digest, p.propq, |
194 | 0 | PROV_LIBCTX_OF(provctx))) |
195 | 0 | goto err; |
196 | | |
197 | 0 | md_size = EVP_MD_get_size(ossl_prov_digest_md(&kctx->digest)); |
198 | 0 | if (md_size <= 0) |
199 | 0 | goto err; |
200 | 0 | kctx->out_len = (size_t)md_size; |
201 | 0 | return kctx; |
202 | | |
203 | 0 | err: |
204 | 0 | kmac_free(kctx); |
205 | 0 | return NULL; |
206 | 0 | } |
207 | | |
208 | | static void *kmac128_new(void *provctx) |
209 | 0 | { |
210 | 0 | static const OSSL_PARAM kmac128_params[] = { |
211 | 0 | OSSL_PARAM_utf8_string("digest", OSSL_DIGEST_NAME_KECCAK_KMAC128, |
212 | 0 | sizeof(OSSL_DIGEST_NAME_KECCAK_KMAC128)), |
213 | 0 | OSSL_PARAM_END |
214 | 0 | }; |
215 | 0 | return kmac_fetch_new(provctx, kmac128_params); |
216 | 0 | } |
217 | | |
218 | | static void *kmac256_new(void *provctx) |
219 | 0 | { |
220 | 0 | static const OSSL_PARAM kmac256_params[] = { |
221 | 0 | OSSL_PARAM_utf8_string("digest", OSSL_DIGEST_NAME_KECCAK_KMAC256, |
222 | 0 | sizeof(OSSL_DIGEST_NAME_KECCAK_KMAC256)), |
223 | 0 | OSSL_PARAM_END |
224 | 0 | }; |
225 | 0 | return kmac_fetch_new(provctx, kmac256_params); |
226 | 0 | } |
227 | | |
228 | | static void *kmac_dup(void *vsrc) |
229 | 0 | { |
230 | 0 | struct kmac_data_st *src = vsrc; |
231 | 0 | struct kmac_data_st *dst; |
232 | |
|
233 | 0 | if (!ossl_prov_is_running()) |
234 | 0 | return NULL; |
235 | | |
236 | 0 | dst = kmac_new(src->provctx); |
237 | 0 | if (dst == NULL) |
238 | 0 | return NULL; |
239 | | |
240 | 0 | if (!EVP_MD_CTX_copy(dst->ctx, src->ctx) |
241 | 0 | || !ossl_prov_digest_copy(&dst->digest, &src->digest)) { |
242 | 0 | kmac_free(dst); |
243 | 0 | return NULL; |
244 | 0 | } |
245 | | #ifdef FIPS_MODULE |
246 | | dst->internal = src->internal; |
247 | | #endif |
248 | 0 | dst->out_len = src->out_len; |
249 | 0 | dst->key_len = src->key_len; |
250 | 0 | dst->custom_len = src->custom_len; |
251 | 0 | dst->xof_mode = src->xof_mode; |
252 | 0 | memcpy(dst->key, src->key, src->key_len); |
253 | 0 | memcpy(dst->custom, src->custom, dst->custom_len); |
254 | 0 | OSSL_FIPS_IND_COPY(dst, src) |
255 | |
|
256 | 0 | return dst; |
257 | 0 | } |
258 | | |
259 | | static int kmac_setkey(struct kmac_data_st *kctx, const unsigned char *key, |
260 | | size_t keylen) |
261 | 0 | { |
262 | 0 | const EVP_MD *digest = ossl_prov_digest_md(&kctx->digest); |
263 | 0 | int w = EVP_MD_get_block_size(digest); |
264 | |
|
265 | 0 | if (keylen < KMAC_MIN_KEY || keylen > KMAC_MAX_KEY) { |
266 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH); |
267 | 0 | return 0; |
268 | 0 | } |
269 | | #ifdef FIPS_MODULE |
270 | | /* |
271 | | * Only do the key check if KMAC is fetched directly. |
272 | | * Other algorithms that embed KMAC such as SSKDF will ignore this check. |
273 | | */ |
274 | | if (!kctx->internal) { |
275 | | int approved = ossl_mac_check_key_size(keylen); |
276 | | |
277 | | if (!approved) { |
278 | | if (!OSSL_FIPS_IND_ON_UNAPPROVED(kctx, OSSL_FIPS_IND_SETTABLE1, |
279 | | PROV_LIBCTX_OF(kctx->provctx), |
280 | | "KMAC", "Key size", |
281 | | ossl_fips_config_kmac_key_check)) { |
282 | | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH); |
283 | | return 0; |
284 | | } |
285 | | } |
286 | | } |
287 | | #endif |
288 | 0 | if (w <= 0) { |
289 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_LENGTH); |
290 | 0 | return 0; |
291 | 0 | } |
292 | 0 | if (!kmac_bytepad_encode_key(kctx->key, sizeof(kctx->key), &kctx->key_len, |
293 | 0 | key, keylen, (size_t)w)) |
294 | 0 | return 0; |
295 | 0 | return 1; |
296 | 0 | } |
297 | | |
298 | | /* |
299 | | * The init() assumes that any ctrl methods are set beforehand for |
300 | | * md, key and custom. Setting the fields afterwards will have no |
301 | | * effect on the output mac. |
302 | | */ |
303 | | static int kmac_init(void *vmacctx, const unsigned char *key, |
304 | | size_t keylen, const OSSL_PARAM params[]) |
305 | 0 | { |
306 | 0 | struct kmac_data_st *kctx = vmacctx; |
307 | 0 | EVP_MD_CTX *ctx = kctx->ctx; |
308 | 0 | unsigned char *out; |
309 | 0 | size_t out_len, block_len; |
310 | 0 | int res, t; |
311 | |
|
312 | 0 | if (!ossl_prov_is_running() || !kmac_set_ctx_params(kctx, params)) |
313 | 0 | return 0; |
314 | | |
315 | 0 | if (key != NULL) { |
316 | 0 | if (!kmac_setkey(kctx, key, keylen)) |
317 | 0 | return 0; |
318 | 0 | } else if (kctx->key_len == 0) { |
319 | | /* Check key has been set */ |
320 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET); |
321 | 0 | return 0; |
322 | 0 | } |
323 | 0 | if (!EVP_DigestInit_ex(kctx->ctx, ossl_prov_digest_md(&kctx->digest), |
324 | 0 | NULL)) |
325 | 0 | return 0; |
326 | | |
327 | 0 | t = EVP_MD_get_block_size(ossl_prov_digest_md(&kctx->digest)); |
328 | 0 | if (t <= 0) { |
329 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_LENGTH); |
330 | 0 | return 0; |
331 | 0 | } |
332 | 0 | block_len = t; |
333 | | |
334 | | /* Set default custom string if it is not already set */ |
335 | 0 | if (kctx->custom_len == 0) { |
336 | 0 | const OSSL_PARAM cparams[] = { |
337 | 0 | OSSL_PARAM_octet_string(OSSL_MAC_PARAM_CUSTOM, "", 0), |
338 | 0 | OSSL_PARAM_END |
339 | 0 | }; |
340 | 0 | (void)kmac_set_ctx_params(kctx, cparams); |
341 | 0 | } |
342 | |
|
343 | 0 | if (!ossl_sp800_185_bytepad(NULL, 0, &out_len, kmac_string, sizeof(kmac_string), |
344 | 0 | kctx->custom, kctx->custom_len, block_len)) { |
345 | 0 | ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR); |
346 | 0 | return 0; |
347 | 0 | } |
348 | 0 | out = OPENSSL_malloc(out_len); |
349 | 0 | if (out == NULL) |
350 | 0 | return 0; |
351 | 0 | res = ossl_sp800_185_bytepad(out, out_len, NULL, kmac_string, sizeof(kmac_string), |
352 | 0 | kctx->custom, kctx->custom_len, block_len) |
353 | 0 | && EVP_DigestUpdate(ctx, out, out_len) |
354 | 0 | && EVP_DigestUpdate(ctx, kctx->key, kctx->key_len); |
355 | 0 | OPENSSL_free(out); |
356 | 0 | return res; |
357 | 0 | } |
358 | | |
359 | | static int kmac_update(void *vmacctx, const unsigned char *data, |
360 | | size_t datalen) |
361 | 0 | { |
362 | 0 | struct kmac_data_st *kctx = vmacctx; |
363 | |
|
364 | 0 | return EVP_DigestUpdate(kctx->ctx, data, datalen); |
365 | 0 | } |
366 | | |
367 | | static int kmac_final(void *vmacctx, unsigned char *out, size_t *outl, |
368 | | size_t outsize) |
369 | 0 | { |
370 | 0 | struct kmac_data_st *kctx = vmacctx; |
371 | 0 | EVP_MD_CTX *ctx = kctx->ctx; |
372 | 0 | size_t lbits, len; |
373 | 0 | unsigned char encoded_outlen[KMAC_MAX_ENCODED_HEADER_LEN]; |
374 | 0 | int ok; |
375 | |
|
376 | 0 | if (!ossl_prov_is_running()) |
377 | 0 | return 0; |
378 | | |
379 | | /* KMAC XOF mode sets the encoded length to 0 */ |
380 | 0 | lbits = (kctx->xof_mode ? 0 : (kctx->out_len * 8)); |
381 | |
|
382 | 0 | ok = ossl_sp800_185_right_encode(encoded_outlen, sizeof(encoded_outlen), &len, lbits) |
383 | 0 | && EVP_DigestUpdate(ctx, encoded_outlen, len) |
384 | 0 | && EVP_DigestFinalXOF(ctx, out, kctx->out_len); |
385 | 0 | *outl = kctx->out_len; |
386 | 0 | return ok; |
387 | 0 | } |
388 | | |
389 | | static const OSSL_PARAM *kmac_gettable_ctx_params(ossl_unused void *ctx, |
390 | | ossl_unused void *provctx) |
391 | 0 | { |
392 | 0 | return kmac_get_ctx_params_list; |
393 | 0 | } |
394 | | |
395 | | static int kmac_get_ctx_params(void *vmacctx, OSSL_PARAM params[]) |
396 | 0 | { |
397 | 0 | struct kmac_data_st *kctx = vmacctx; |
398 | 0 | struct kmac_get_ctx_params_st p; |
399 | 0 | int sz; |
400 | |
|
401 | 0 | if (kctx == NULL || !kmac_get_ctx_params_decoder(params, &p)) |
402 | 0 | return 0; |
403 | | |
404 | 0 | if (p.size != NULL && !OSSL_PARAM_set_size_t(p.size, kctx->out_len)) |
405 | 0 | return 0; |
406 | | |
407 | 0 | if (p.bsize != NULL) { |
408 | 0 | sz = EVP_MD_block_size(ossl_prov_digest_md(&kctx->digest)); |
409 | 0 | if (!OSSL_PARAM_set_int(p.bsize, sz)) |
410 | 0 | return 0; |
411 | 0 | } |
412 | | |
413 | 0 | if (!OSSL_FIPS_IND_GET_CTX_FROM_PARAM(kctx, p.ind)) |
414 | 0 | return 0; |
415 | | |
416 | 0 | return 1; |
417 | 0 | } |
418 | | |
419 | | static const OSSL_PARAM *kmac_settable_ctx_params(ossl_unused void *ctx, |
420 | | ossl_unused void *provctx) |
421 | 0 | { |
422 | 0 | return kmac_set_ctx_params_list; |
423 | 0 | } |
424 | | |
425 | | /* |
426 | | * The following params can be set any time before final(): |
427 | | * - "outlen" or "size": The requested output length. |
428 | | * - "xof": If set, this indicates that right_encoded(0) |
429 | | * is part of the digested data, otherwise it |
430 | | * uses right_encoded(requested output length). |
431 | | * |
432 | | * All other params should be set before init(). |
433 | | */ |
434 | | static int kmac_set_ctx_params(void *vmacctx, const OSSL_PARAM *params) |
435 | 0 | { |
436 | 0 | struct kmac_data_st *kctx = vmacctx; |
437 | 0 | struct kmac_set_ctx_params_st p; |
438 | |
|
439 | 0 | if (kctx == NULL || !kmac_set_ctx_params_decoder(params, &p)) |
440 | 0 | return 0; |
441 | | |
442 | 0 | if (!OSSL_FIPS_IND_SET_CTX_FROM_PARAM(kctx, OSSL_FIPS_IND_SETTABLE0, |
443 | 0 | p.ind_sht)) |
444 | 0 | return 0; |
445 | 0 | if (!OSSL_FIPS_IND_SET_CTX_FROM_PARAM(kctx, OSSL_FIPS_IND_SETTABLE1, p.ind_k)) |
446 | 0 | return 0; |
447 | | |
448 | 0 | if (p.xof != NULL && !OSSL_PARAM_get_int(p.xof, &kctx->xof_mode)) |
449 | 0 | return 0; |
450 | | |
451 | 0 | if (p.size != NULL) { |
452 | 0 | size_t sz = 0; |
453 | |
|
454 | 0 | if (!OSSL_PARAM_get_size_t(p.size, &sz)) |
455 | 0 | return 0; |
456 | 0 | if (sz > KMAC_MAX_OUTPUT_LEN) { |
457 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_OUTPUT_LENGTH); |
458 | 0 | return 0; |
459 | 0 | } |
460 | | #ifdef FIPS_MODULE |
461 | | /* SP 800-185 8.4.2 mandates a minimum of 32 bits of output */ |
462 | | if (sz < 32 / 8) { |
463 | | if (!OSSL_FIPS_IND_ON_UNAPPROVED(kctx, OSSL_FIPS_IND_SETTABLE0, |
464 | | PROV_LIBCTX_OF(kctx->provctx), |
465 | | "KMAC", "length", |
466 | | ossl_fips_config_no_short_mac)) { |
467 | | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_OUTPUT_LENGTH); |
468 | | return 0; |
469 | | } |
470 | | } |
471 | | #endif |
472 | 0 | kctx->out_len = sz; |
473 | 0 | } |
474 | | |
475 | 0 | if (p.key != NULL) |
476 | 0 | if (p.key->data_type != OSSL_PARAM_OCTET_STRING |
477 | 0 | || !kmac_setkey(kctx, p.key->data, p.key->data_size)) |
478 | 0 | return 0; |
479 | | |
480 | 0 | if (p.custom != NULL) { |
481 | 0 | if (p.custom->data_type != OSSL_PARAM_OCTET_STRING) |
482 | 0 | return 0; |
483 | 0 | if (p.custom->data_size > KMAC_MAX_CUSTOM) { |
484 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_CUSTOM_LENGTH); |
485 | 0 | return 0; |
486 | 0 | } |
487 | 0 | if (!ossl_sp800_185_encode_string(kctx->custom, |
488 | 0 | sizeof(kctx->custom), &kctx->custom_len, |
489 | 0 | p.custom->data, p.custom->data_size)) |
490 | 0 | return 0; |
491 | 0 | } |
492 | | |
493 | 0 | return 1; |
494 | 0 | } |
495 | | |
496 | | /* Returns out = bytepad(encode_string(in), w) */ |
497 | | static int kmac_bytepad_encode_key(unsigned char *out, size_t out_max_len, |
498 | | size_t *out_len, const unsigned char *in, size_t in_len, size_t w) |
499 | 0 | { |
500 | 0 | unsigned char tmp[KMAC_MAX_KEY + KMAC_MAX_ENCODED_HEADER_LEN]; |
501 | 0 | size_t tmp_len; |
502 | |
|
503 | 0 | if (!ossl_sp800_185_encode_string(tmp, sizeof(tmp), &tmp_len, in, in_len)) |
504 | 0 | return 0; |
505 | 0 | if (!ossl_sp800_185_bytepad(NULL, out_max_len, out_len, tmp, tmp_len, NULL, 0, w)) |
506 | 0 | return 0; |
507 | 0 | return ossl_sp800_185_bytepad(out, out_max_len, NULL, tmp, tmp_len, NULL, 0, w); |
508 | 0 | } |
509 | | |
510 | | #define IMPLEMENT_KMAC_TABLE(size, funcname, newname) \ |
511 | | const OSSL_DISPATCH ossl_kmac##size##_##funcname[] = { \ |
512 | | { OSSL_FUNC_MAC_NEWCTX, (void (*)(void))kmac##size##_##newname }, \ |
513 | | { OSSL_FUNC_MAC_DUPCTX, (void (*)(void))kmac_dup }, \ |
514 | | { OSSL_FUNC_MAC_FREECTX, (void (*)(void))kmac_free }, \ |
515 | | { OSSL_FUNC_MAC_INIT, (void (*)(void))kmac_init }, \ |
516 | | { OSSL_FUNC_MAC_UPDATE, (void (*)(void))kmac_update }, \ |
517 | | { OSSL_FUNC_MAC_FINAL, (void (*)(void))kmac_final }, \ |
518 | | { OSSL_FUNC_MAC_GETTABLE_CTX_PARAMS, \ |
519 | | (void (*)(void))kmac_gettable_ctx_params }, \ |
520 | | { OSSL_FUNC_MAC_GET_CTX_PARAMS, (void (*)(void))kmac_get_ctx_params }, \ |
521 | | { OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS, \ |
522 | | (void (*)(void))kmac_settable_ctx_params }, \ |
523 | | { OSSL_FUNC_MAC_SET_CTX_PARAMS, (void (*)(void))kmac_set_ctx_params }, \ |
524 | | OSSL_DISPATCH_END \ |
525 | | } |
526 | | |
527 | | #define KMAC_TABLE(size) IMPLEMENT_KMAC_TABLE(size, functions, new) |
528 | | |
529 | | KMAC_TABLE(128); |
530 | | KMAC_TABLE(256); |
531 | | |
532 | | #ifdef FIPS_MODULE |
533 | | #define KMAC_INTERNAL_TABLE(size) \ |
534 | | static OSSL_FUNC_mac_newctx_fn kmac##size##_internal_new; \ |
535 | | static void *kmac##size##_internal_new(void *provctx) \ |
536 | | { \ |
537 | | struct kmac_data_st *macctx = kmac##size##_new(provctx); \ |
538 | | \ |
539 | | if (macctx != NULL) \ |
540 | | macctx->internal = 1; \ |
541 | | return macctx; \ |
542 | | } \ |
543 | | IMPLEMENT_KMAC_TABLE(size, internal_functions, internal_new) |
544 | | |
545 | | KMAC_INTERNAL_TABLE(128); |
546 | | KMAC_INTERNAL_TABLE(256); |
547 | | #endif /* FIPS_MODULE */ |