/src/openssl/providers/implementations/macs/kmac_prov.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2018-2023 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 | | |
59 | | #include "prov/implementations.h" |
60 | | #include "prov/provider_ctx.h" |
61 | | #include "prov/provider_util.h" |
62 | | #include "prov/providercommon.h" |
63 | | #include "prov/fipscommon.h" |
64 | | #include "prov/fipsindicator.h" |
65 | | #include "internal/cryptlib.h" /* ossl_assert */ |
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 | | OSSL_FIPS_IND_DECLARE |
133 | | }; |
134 | | |
135 | | static int encode_string(unsigned char *out, size_t out_max_len, size_t *out_len, |
136 | | const unsigned char *in, size_t in_len); |
137 | | static int right_encode(unsigned char *out, size_t out_max_len, size_t *out_len, |
138 | | size_t bits); |
139 | | static int bytepad(unsigned char *out, size_t *out_len, |
140 | | const unsigned char *in1, size_t in1_len, |
141 | | const unsigned char *in2, size_t in2_len, |
142 | | size_t w); |
143 | | static int kmac_bytepad_encode_key(unsigned char *out, size_t out_max_len, |
144 | | size_t *out_len, |
145 | | const unsigned char *in, size_t in_len, |
146 | | size_t w); |
147 | | |
148 | | static void kmac_free(void *vmacctx) |
149 | 0 | { |
150 | 0 | struct kmac_data_st *kctx = vmacctx; |
151 | |
|
152 | 0 | if (kctx != NULL) { |
153 | 0 | EVP_MD_CTX_free(kctx->ctx); |
154 | 0 | ossl_prov_digest_reset(&kctx->digest); |
155 | 0 | OPENSSL_cleanse(kctx->key, kctx->key_len); |
156 | 0 | OPENSSL_cleanse(kctx->custom, kctx->custom_len); |
157 | 0 | OPENSSL_free(kctx); |
158 | 0 | } |
159 | 0 | } |
160 | | |
161 | | /* |
162 | | * We have KMAC implemented as a hash, which we can use instead of |
163 | | * reimplementing the EVP functionality with direct use of |
164 | | * keccak_mac_init() and friends. |
165 | | */ |
166 | | static struct kmac_data_st *kmac_new(void *provctx) |
167 | 0 | { |
168 | 0 | struct kmac_data_st *kctx; |
169 | |
|
170 | 0 | if (!ossl_prov_is_running()) |
171 | 0 | return NULL; |
172 | | |
173 | 0 | if ((kctx = OPENSSL_zalloc(sizeof(*kctx))) == NULL |
174 | 0 | || (kctx->ctx = EVP_MD_CTX_new()) == NULL) { |
175 | 0 | kmac_free(kctx); |
176 | 0 | return NULL; |
177 | 0 | } |
178 | 0 | kctx->provctx = provctx; |
179 | 0 | OSSL_FIPS_IND_INIT(kctx) |
180 | 0 | return kctx; |
181 | 0 | } |
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 | int md_size; |
187 | |
|
188 | 0 | if (kctx == NULL) |
189 | 0 | return 0; |
190 | 0 | if (!ossl_prov_digest_load_from_params(&kctx->digest, params, |
191 | 0 | PROV_LIBCTX_OF(provctx))) { |
192 | 0 | kmac_free(kctx); |
193 | 0 | return 0; |
194 | 0 | } |
195 | | |
196 | 0 | md_size = EVP_MD_get_size(ossl_prov_digest_md(&kctx->digest)); |
197 | 0 | if (md_size <= 0) { |
198 | 0 | kmac_free(kctx); |
199 | 0 | return 0; |
200 | 0 | } |
201 | 0 | kctx->out_len = (size_t)md_size; |
202 | 0 | return kctx; |
203 | 0 | } |
204 | | |
205 | | static void *kmac128_new(void *provctx) |
206 | 0 | { |
207 | 0 | static const OSSL_PARAM kmac128_params[] = { |
208 | 0 | OSSL_PARAM_utf8_string("digest", OSSL_DIGEST_NAME_KECCAK_KMAC128, |
209 | 0 | sizeof(OSSL_DIGEST_NAME_KECCAK_KMAC128)), |
210 | 0 | OSSL_PARAM_END |
211 | 0 | }; |
212 | 0 | return kmac_fetch_new(provctx, kmac128_params); |
213 | 0 | } |
214 | | |
215 | | static void *kmac256_new(void *provctx) |
216 | 0 | { |
217 | 0 | static const OSSL_PARAM kmac256_params[] = { |
218 | 0 | OSSL_PARAM_utf8_string("digest", OSSL_DIGEST_NAME_KECCAK_KMAC256, |
219 | 0 | sizeof(OSSL_DIGEST_NAME_KECCAK_KMAC256)), |
220 | 0 | OSSL_PARAM_END |
221 | 0 | }; |
222 | 0 | return kmac_fetch_new(provctx, kmac256_params); |
223 | 0 | } |
224 | | |
225 | | static void *kmac_dup(void *vsrc) |
226 | 0 | { |
227 | 0 | struct kmac_data_st *src = vsrc; |
228 | 0 | struct kmac_data_st *dst; |
229 | |
|
230 | 0 | if (!ossl_prov_is_running()) |
231 | 0 | return NULL; |
232 | | |
233 | 0 | dst = kmac_new(src->provctx); |
234 | 0 | if (dst == NULL) |
235 | 0 | return NULL; |
236 | | |
237 | 0 | if (!EVP_MD_CTX_copy(dst->ctx, src->ctx) |
238 | 0 | || !ossl_prov_digest_copy(&dst->digest, &src->digest)) { |
239 | 0 | kmac_free(dst); |
240 | 0 | return NULL; |
241 | 0 | } |
242 | | |
243 | 0 | dst->out_len = src->out_len; |
244 | 0 | dst->key_len = src->key_len; |
245 | 0 | dst->custom_len = src->custom_len; |
246 | 0 | dst->xof_mode = src->xof_mode; |
247 | 0 | memcpy(dst->key, src->key, src->key_len); |
248 | 0 | memcpy(dst->custom, src->custom, dst->custom_len); |
249 | 0 | OSSL_FIPS_IND_COPY(dst, src) |
250 | |
|
251 | 0 | return dst; |
252 | 0 | } |
253 | | |
254 | | static int kmac_setkey(struct kmac_data_st *kctx, const unsigned char *key, |
255 | | size_t keylen) |
256 | 0 | { |
257 | 0 | const EVP_MD *digest = ossl_prov_digest_md(&kctx->digest); |
258 | 0 | int w = EVP_MD_get_block_size(digest); |
259 | |
|
260 | 0 | if (keylen < KMAC_MIN_KEY || keylen > KMAC_MAX_KEY) { |
261 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH); |
262 | 0 | return 0; |
263 | 0 | } |
264 | 0 | if (w <= 0) { |
265 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_LENGTH); |
266 | 0 | return 0; |
267 | 0 | } |
268 | 0 | if (!kmac_bytepad_encode_key(kctx->key, sizeof(kctx->key), &kctx->key_len, |
269 | 0 | key, keylen, (size_t)w)) |
270 | 0 | return 0; |
271 | 0 | return 1; |
272 | 0 | } |
273 | | |
274 | | /* |
275 | | * The init() assumes that any ctrl methods are set beforehand for |
276 | | * md, key and custom. Setting the fields afterwards will have no |
277 | | * effect on the output mac. |
278 | | */ |
279 | | static int kmac_init(void *vmacctx, const unsigned char *key, |
280 | | size_t keylen, const OSSL_PARAM params[]) |
281 | 0 | { |
282 | 0 | struct kmac_data_st *kctx = vmacctx; |
283 | 0 | EVP_MD_CTX *ctx = kctx->ctx; |
284 | 0 | unsigned char *out; |
285 | 0 | size_t out_len, block_len; |
286 | 0 | int res, t; |
287 | |
|
288 | 0 | if (!ossl_prov_is_running() || !kmac_set_ctx_params(kctx, params)) |
289 | 0 | return 0; |
290 | | |
291 | 0 | if (key != NULL) { |
292 | 0 | if (!kmac_setkey(kctx, key, keylen)) |
293 | 0 | return 0; |
294 | 0 | } else if (kctx->key_len == 0) { |
295 | | /* Check key has been set */ |
296 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET); |
297 | 0 | return 0; |
298 | 0 | } |
299 | 0 | if (!EVP_DigestInit_ex(kctx->ctx, ossl_prov_digest_md(&kctx->digest), |
300 | 0 | NULL)) |
301 | 0 | return 0; |
302 | | |
303 | 0 | t = EVP_MD_get_block_size(ossl_prov_digest_md(&kctx->digest)); |
304 | 0 | if (t <= 0) { |
305 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_LENGTH); |
306 | 0 | return 0; |
307 | 0 | } |
308 | 0 | block_len = t; |
309 | | |
310 | | /* Set default custom string if it is not already set */ |
311 | 0 | if (kctx->custom_len == 0) { |
312 | 0 | const OSSL_PARAM cparams[] = { |
313 | 0 | OSSL_PARAM_octet_string(OSSL_MAC_PARAM_CUSTOM, "", 0), |
314 | 0 | OSSL_PARAM_END |
315 | 0 | }; |
316 | 0 | (void)kmac_set_ctx_params(kctx, cparams); |
317 | 0 | } |
318 | |
|
319 | 0 | if (!bytepad(NULL, &out_len, kmac_string, sizeof(kmac_string), |
320 | 0 | kctx->custom, kctx->custom_len, block_len)) { |
321 | 0 | ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR); |
322 | 0 | return 0; |
323 | 0 | } |
324 | 0 | out = OPENSSL_malloc(out_len); |
325 | 0 | if (out == NULL) |
326 | 0 | return 0; |
327 | 0 | res = bytepad(out, NULL, kmac_string, sizeof(kmac_string), |
328 | 0 | kctx->custom, kctx->custom_len, block_len) |
329 | 0 | && EVP_DigestUpdate(ctx, out, out_len) |
330 | 0 | && EVP_DigestUpdate(ctx, kctx->key, kctx->key_len); |
331 | 0 | OPENSSL_free(out); |
332 | 0 | return res; |
333 | 0 | } |
334 | | |
335 | | static int kmac_update(void *vmacctx, const unsigned char *data, |
336 | | size_t datalen) |
337 | 0 | { |
338 | 0 | struct kmac_data_st *kctx = vmacctx; |
339 | |
|
340 | 0 | return EVP_DigestUpdate(kctx->ctx, data, datalen); |
341 | 0 | } |
342 | | |
343 | | static int kmac_final(void *vmacctx, unsigned char *out, size_t *outl, |
344 | | size_t outsize) |
345 | 0 | { |
346 | 0 | struct kmac_data_st *kctx = vmacctx; |
347 | 0 | EVP_MD_CTX *ctx = kctx->ctx; |
348 | 0 | size_t lbits, len; |
349 | 0 | unsigned char encoded_outlen[KMAC_MAX_ENCODED_HEADER_LEN]; |
350 | 0 | int ok; |
351 | |
|
352 | 0 | if (!ossl_prov_is_running()) |
353 | 0 | return 0; |
354 | | |
355 | | /* KMAC XOF mode sets the encoded length to 0 */ |
356 | 0 | lbits = (kctx->xof_mode ? 0 : (kctx->out_len * 8)); |
357 | |
|
358 | 0 | ok = right_encode(encoded_outlen, sizeof(encoded_outlen), &len, lbits) |
359 | 0 | && EVP_DigestUpdate(ctx, encoded_outlen, len) |
360 | 0 | && EVP_DigestFinalXOF(ctx, out, kctx->out_len); |
361 | 0 | *outl = kctx->out_len; |
362 | 0 | return ok; |
363 | 0 | } |
364 | | |
365 | | static const OSSL_PARAM known_gettable_ctx_params[] = { |
366 | | OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL), |
367 | | OSSL_PARAM_size_t(OSSL_MAC_PARAM_BLOCK_SIZE, NULL), |
368 | | OSSL_FIPS_IND_GETTABLE_CTX_PARAM() |
369 | | OSSL_PARAM_END |
370 | | }; |
371 | | static const OSSL_PARAM *kmac_gettable_ctx_params(ossl_unused void *ctx, |
372 | | ossl_unused void *provctx) |
373 | 0 | { |
374 | 0 | return known_gettable_ctx_params; |
375 | 0 | } |
376 | | |
377 | | static int kmac_get_ctx_params(void *vmacctx, OSSL_PARAM params[]) |
378 | 0 | { |
379 | 0 | struct kmac_data_st *kctx = vmacctx; |
380 | 0 | OSSL_PARAM *p; |
381 | 0 | int sz; |
382 | |
|
383 | 0 | if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_SIZE)) != NULL |
384 | 0 | && !OSSL_PARAM_set_size_t(p, kctx->out_len)) |
385 | 0 | return 0; |
386 | | |
387 | 0 | if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_BLOCK_SIZE)) != NULL) { |
388 | 0 | sz = EVP_MD_block_size(ossl_prov_digest_md(&kctx->digest)); |
389 | 0 | if (!OSSL_PARAM_set_int(p, sz)) |
390 | 0 | return 0; |
391 | 0 | } |
392 | | |
393 | 0 | if (!OSSL_FIPS_IND_GET_CTX_PARAM(kctx, params)) |
394 | 0 | return 0; |
395 | | |
396 | 0 | return 1; |
397 | 0 | } |
398 | | |
399 | | static const OSSL_PARAM known_settable_ctx_params[] = { |
400 | | OSSL_PARAM_int(OSSL_MAC_PARAM_XOF, NULL), |
401 | | OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL), |
402 | | OSSL_PARAM_octet_string(OSSL_MAC_PARAM_KEY, NULL, 0), |
403 | | OSSL_PARAM_octet_string(OSSL_MAC_PARAM_CUSTOM, NULL, 0), |
404 | | OSSL_FIPS_IND_SETTABLE_CTX_PARAM(OSSL_PROV_FIPS_PARAM_NO_SHORT_MAC) |
405 | | OSSL_PARAM_END |
406 | | }; |
407 | | static const OSSL_PARAM *kmac_settable_ctx_params(ossl_unused void *ctx, |
408 | | ossl_unused void *provctx) |
409 | 0 | { |
410 | 0 | return known_settable_ctx_params; |
411 | 0 | } |
412 | | |
413 | | /* |
414 | | * The following params can be set any time before final(): |
415 | | * - "outlen" or "size": The requested output length. |
416 | | * - "xof": If set, this indicates that right_encoded(0) |
417 | | * is part of the digested data, otherwise it |
418 | | * uses right_encoded(requested output length). |
419 | | * |
420 | | * All other params should be set before init(). |
421 | | */ |
422 | | static int kmac_set_ctx_params(void *vmacctx, const OSSL_PARAM *params) |
423 | 0 | { |
424 | 0 | struct kmac_data_st *kctx = vmacctx; |
425 | 0 | const OSSL_PARAM *p; |
426 | |
|
427 | 0 | if (params == NULL) |
428 | 0 | return 1; |
429 | | |
430 | 0 | if (!OSSL_FIPS_IND_SET_CTX_PARAM(kctx, OSSL_FIPS_IND_SETTABLE0, params, |
431 | 0 | OSSL_PROV_PARAM_NO_SHORT_MAC)) |
432 | 0 | return 0; |
433 | | |
434 | 0 | if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_XOF)) != NULL |
435 | 0 | && !OSSL_PARAM_get_int(p, &kctx->xof_mode)) |
436 | 0 | return 0; |
437 | 0 | if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_SIZE)) != NULL) { |
438 | 0 | size_t sz = 0; |
439 | |
|
440 | 0 | if (!OSSL_PARAM_get_size_t(p, &sz)) |
441 | 0 | return 0; |
442 | 0 | if (sz > KMAC_MAX_OUTPUT_LEN) { |
443 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_OUTPUT_LENGTH); |
444 | 0 | return 0; |
445 | 0 | } |
446 | | #ifdef FIPS_MODULE |
447 | | /* SP 800-185 8.4.2 mandates a minimum of 32 bits of output */ |
448 | | if (sz < 32 / 8) { |
449 | | if (!OSSL_FIPS_IND_ON_UNAPPROVED(kctx, OSSL_FIPS_IND_SETTABLE0, |
450 | | PROV_LIBCTX_OF(kctx->provctx), |
451 | | "KMAC", "length", |
452 | | &FIPS_no_short_mac)) { |
453 | | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_OUTPUT_LENGTH); |
454 | | return 0; |
455 | | } |
456 | | } |
457 | | #endif |
458 | 0 | kctx->out_len = sz; |
459 | 0 | } |
460 | 0 | if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_KEY)) != NULL |
461 | 0 | && !kmac_setkey(kctx, p->data, p->data_size)) |
462 | 0 | return 0; |
463 | 0 | if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_CUSTOM)) |
464 | 0 | != NULL) { |
465 | 0 | if (p->data_size > KMAC_MAX_CUSTOM) { |
466 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_CUSTOM_LENGTH); |
467 | 0 | return 0; |
468 | 0 | } |
469 | 0 | if (!encode_string(kctx->custom, sizeof(kctx->custom), &kctx->custom_len, |
470 | 0 | p->data, p->data_size)) |
471 | 0 | return 0; |
472 | 0 | } |
473 | 0 | return 1; |
474 | 0 | } |
475 | | |
476 | | /* Encoding/Padding Methods. */ |
477 | | |
478 | | /* Returns the number of bytes required to store 'bits' into a byte array */ |
479 | | static unsigned int get_encode_size(size_t bits) |
480 | 0 | { |
481 | 0 | unsigned int cnt = 0, sz = sizeof(size_t); |
482 | |
|
483 | 0 | while (bits && (cnt < sz)) { |
484 | 0 | ++cnt; |
485 | 0 | bits >>= 8; |
486 | 0 | } |
487 | | /* If bits is zero 1 byte is required */ |
488 | 0 | if (cnt == 0) |
489 | 0 | cnt = 1; |
490 | 0 | return cnt; |
491 | 0 | } |
492 | | |
493 | | /* |
494 | | * Convert an integer into bytes . The number of bytes is appended |
495 | | * to the end of the buffer. Returns an array of bytes 'out' of size |
496 | | * *out_len. |
497 | | * |
498 | | * e.g if bits = 32, out[2] = { 0x20, 0x01 } |
499 | | */ |
500 | | static int right_encode(unsigned char *out, size_t out_max_len, size_t *out_len, |
501 | | size_t bits) |
502 | 0 | { |
503 | 0 | unsigned int len = get_encode_size(bits); |
504 | 0 | int i; |
505 | |
|
506 | 0 | if (len >= out_max_len) { |
507 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_LENGTH_TOO_LARGE); |
508 | 0 | return 0; |
509 | 0 | } |
510 | | |
511 | | /* MSB's are at the start of the bytes array */ |
512 | 0 | for (i = len - 1; i >= 0; --i) { |
513 | 0 | out[i] = (unsigned char)(bits & 0xFF); |
514 | 0 | bits >>= 8; |
515 | 0 | } |
516 | | /* Tack the length onto the end */ |
517 | 0 | out[len] = (unsigned char)len; |
518 | | |
519 | | /* The Returned length includes the tacked on byte */ |
520 | 0 | *out_len = len + 1; |
521 | 0 | return 1; |
522 | 0 | } |
523 | | |
524 | | /* |
525 | | * Encodes a string with a left encoded length added. Note that the |
526 | | * in_len is converted to bits (*8). |
527 | | * |
528 | | * e.g- in="KMAC" gives out[6] = { 0x01, 0x20, 0x4B, 0x4D, 0x41, 0x43 } |
529 | | * len bits K M A C |
530 | | */ |
531 | | static int encode_string(unsigned char *out, size_t out_max_len, size_t *out_len, |
532 | | const unsigned char *in, size_t in_len) |
533 | 0 | { |
534 | 0 | if (in == NULL) { |
535 | 0 | *out_len = 0; |
536 | 0 | } else { |
537 | 0 | size_t i, bits, len, sz; |
538 | |
|
539 | 0 | bits = 8 * in_len; |
540 | 0 | len = get_encode_size(bits); |
541 | 0 | sz = 1 + len + in_len; |
542 | |
|
543 | 0 | if (sz > out_max_len) { |
544 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_LENGTH_TOO_LARGE); |
545 | 0 | return 0; |
546 | 0 | } |
547 | | |
548 | 0 | out[0] = (unsigned char)len; |
549 | 0 | for (i = len; i > 0; --i) { |
550 | 0 | out[i] = (bits & 0xFF); |
551 | 0 | bits >>= 8; |
552 | 0 | } |
553 | 0 | memcpy(out + len + 1, in, in_len); |
554 | 0 | *out_len = sz; |
555 | 0 | } |
556 | 0 | return 1; |
557 | 0 | } |
558 | | |
559 | | /* |
560 | | * Returns a zero padded encoding of the inputs in1 and an optional |
561 | | * in2 (can be NULL). The padded output must be a multiple of the blocksize 'w'. |
562 | | * The value of w is in bytes (< 256). |
563 | | * |
564 | | * The returned output is: |
565 | | * zero_padded(multiple of w, (left_encode(w) || in1 [|| in2]) |
566 | | */ |
567 | | static int bytepad(unsigned char *out, size_t *out_len, |
568 | | const unsigned char *in1, size_t in1_len, |
569 | | const unsigned char *in2, size_t in2_len, size_t w) |
570 | 0 | { |
571 | 0 | int len; |
572 | 0 | unsigned char *p = out; |
573 | 0 | int sz = w; |
574 | |
|
575 | 0 | if (out == NULL) { |
576 | 0 | if (out_len == NULL) { |
577 | 0 | ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER); |
578 | 0 | return 0; |
579 | 0 | } |
580 | 0 | sz = 2 + in1_len + (in2 != NULL ? in2_len : 0); |
581 | 0 | *out_len = (sz + w - 1) / w * w; |
582 | 0 | return 1; |
583 | 0 | } |
584 | | |
585 | 0 | if (!ossl_assert(w <= 255)) |
586 | 0 | return 0; |
587 | | |
588 | | /* Left encoded w */ |
589 | 0 | *p++ = 1; |
590 | 0 | *p++ = (unsigned char)w; |
591 | | /* || in1 */ |
592 | 0 | memcpy(p, in1, in1_len); |
593 | 0 | p += in1_len; |
594 | | /* [ || in2 ] */ |
595 | 0 | if (in2 != NULL && in2_len > 0) { |
596 | 0 | memcpy(p, in2, in2_len); |
597 | 0 | p += in2_len; |
598 | 0 | } |
599 | | /* Figure out the pad size (divisible by w) */ |
600 | 0 | len = p - out; |
601 | 0 | sz = (len + w - 1) / w * w; |
602 | | /* zero pad the end of the buffer */ |
603 | 0 | if (sz != len) |
604 | 0 | memset(p, 0, sz - len); |
605 | 0 | if (out_len != NULL) |
606 | 0 | *out_len = sz; |
607 | 0 | return 1; |
608 | 0 | } |
609 | | |
610 | | /* Returns out = bytepad(encode_string(in), w) */ |
611 | | static int kmac_bytepad_encode_key(unsigned char *out, size_t out_max_len, |
612 | | size_t *out_len, |
613 | | const unsigned char *in, size_t in_len, |
614 | | size_t w) |
615 | 0 | { |
616 | 0 | unsigned char tmp[KMAC_MAX_KEY + KMAC_MAX_ENCODED_HEADER_LEN]; |
617 | 0 | size_t tmp_len; |
618 | |
|
619 | 0 | if (!encode_string(tmp, sizeof(tmp), &tmp_len, in, in_len)) |
620 | 0 | return 0; |
621 | 0 | if (!bytepad(NULL, out_len, tmp, tmp_len, NULL, 0, w)) |
622 | 0 | return 0; |
623 | 0 | if (!ossl_assert(*out_len <= out_max_len)) |
624 | 0 | return 0; |
625 | 0 | return bytepad(out, NULL, tmp, tmp_len, NULL, 0, w); |
626 | 0 | } |
627 | | |
628 | | const OSSL_DISPATCH ossl_kmac128_functions[] = { |
629 | | { OSSL_FUNC_MAC_NEWCTX, (void (*)(void))kmac128_new }, |
630 | | { OSSL_FUNC_MAC_DUPCTX, (void (*)(void))kmac_dup }, |
631 | | { OSSL_FUNC_MAC_FREECTX, (void (*)(void))kmac_free }, |
632 | | { OSSL_FUNC_MAC_INIT, (void (*)(void))kmac_init }, |
633 | | { OSSL_FUNC_MAC_UPDATE, (void (*)(void))kmac_update }, |
634 | | { OSSL_FUNC_MAC_FINAL, (void (*)(void))kmac_final }, |
635 | | { OSSL_FUNC_MAC_GETTABLE_CTX_PARAMS, |
636 | | (void (*)(void))kmac_gettable_ctx_params }, |
637 | | { OSSL_FUNC_MAC_GET_CTX_PARAMS, (void (*)(void))kmac_get_ctx_params }, |
638 | | { OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS, |
639 | | (void (*)(void))kmac_settable_ctx_params }, |
640 | | { OSSL_FUNC_MAC_SET_CTX_PARAMS, (void (*)(void))kmac_set_ctx_params }, |
641 | | OSSL_DISPATCH_END |
642 | | }; |
643 | | |
644 | | const OSSL_DISPATCH ossl_kmac256_functions[] = { |
645 | | { OSSL_FUNC_MAC_NEWCTX, (void (*)(void))kmac256_new }, |
646 | | { OSSL_FUNC_MAC_DUPCTX, (void (*)(void))kmac_dup }, |
647 | | { OSSL_FUNC_MAC_FREECTX, (void (*)(void))kmac_free }, |
648 | | { OSSL_FUNC_MAC_INIT, (void (*)(void))kmac_init }, |
649 | | { OSSL_FUNC_MAC_UPDATE, (void (*)(void))kmac_update }, |
650 | | { OSSL_FUNC_MAC_FINAL, (void (*)(void))kmac_final }, |
651 | | { OSSL_FUNC_MAC_GETTABLE_CTX_PARAMS, |
652 | | (void (*)(void))kmac_gettable_ctx_params }, |
653 | | { OSSL_FUNC_MAC_GET_CTX_PARAMS, (void (*)(void))kmac_get_ctx_params }, |
654 | | { OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS, |
655 | | (void (*)(void))kmac_settable_ctx_params }, |
656 | | { OSSL_FUNC_MAC_SET_CTX_PARAMS, (void (*)(void))kmac_set_ctx_params }, |
657 | | OSSL_DISPATCH_END |
658 | | }; |