2 * Copyright 2018-2020 The OpenSSL Project Authors. All Rights Reserved.
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
11 * See SP800-185 "Appendix A - KMAC, .... in Terms of Keccak[c]"
14 * K = Key (len(K) < 2^2040 bits)
16 * L = Output length (0 <= L < 2^2040 bits)
17 * S = Customization String Default="" (len(S) < 2^2040 bits)
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).
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).
33 * KMAC128XOF(K, X, L, S)
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).
40 * KMAC256XOF(K, X, L, S)
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).
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>
57 #include "prov/providercommonerr.h"
58 #include "prov/implementations.h"
59 #include "prov/provider_ctx.h"
60 #include "prov/provider_util.h"
63 * Forward declaration of everything implemented here. This is not strictly
64 * necessary for the compiler, but provides an assurance that the signatures
65 * of the functions in the dispatch table are correct.
67 static OSSL_FUNC_mac_newctx_fn kmac128_new
;
68 static OSSL_FUNC_mac_newctx_fn kmac256_new
;
69 static OSSL_FUNC_mac_dupctx_fn kmac_dup
;
70 static OSSL_FUNC_mac_freectx_fn kmac_free
;
71 static OSSL_FUNC_mac_gettable_ctx_params_fn kmac_gettable_ctx_params
;
72 static OSSL_FUNC_mac_get_ctx_params_fn kmac_get_ctx_params
;
73 static OSSL_FUNC_mac_settable_ctx_params_fn kmac_settable_ctx_params
;
74 static OSSL_FUNC_mac_set_ctx_params_fn kmac_set_ctx_params
;
75 static OSSL_FUNC_mac_size_fn kmac_size
;
76 static OSSL_FUNC_mac_init_fn kmac_init
;
77 static OSSL_FUNC_mac_update_fn kmac_update
;
78 static OSSL_FUNC_mac_final_fn kmac_final
;
80 #define KMAC_MAX_BLOCKSIZE ((1600 - 128*2) / 8) /* 168 */
81 #define KMAC_MIN_BLOCKSIZE ((1600 - 256*2) / 8) /* 136 */
83 /* Length encoding will be a 1 byte size + length in bits (2 bytes max) */
84 #define KMAC_MAX_ENCODED_HEADER_LEN 3
87 * Custom string max size is chosen such that:
88 * len(encoded_string(custom) + len(kmac_encoded_string) <= KMAC_MIN_BLOCKSIZE
89 * i.e: (KMAC_MAX_CUSTOM + KMAC_MAX_ENCODED_LEN) + 6 <= 136
91 #define KMAC_MAX_CUSTOM 127
93 /* Maximum size of encoded custom string */
94 #define KMAC_MAX_CUSTOM_ENCODED (KMAC_MAX_CUSTOM + KMAC_MAX_ENCODED_HEADER_LEN)
96 /* Maximum key size in bytes = 2040 / 8 */
97 #define KMAC_MAX_KEY 255
100 * Maximum Encoded Key size will be padded to a multiple of the blocksize
101 * i.e KMAC_MAX_KEY + KMAC_MAX_ENCODED_LEN = 258
102 * Padded to a multiple of KMAC_MAX_BLOCKSIZE
104 #define KMAC_MAX_KEY_ENCODED (KMAC_MAX_BLOCKSIZE * 2)
106 /* Fixed value of encode_string("KMAC") */
107 static const unsigned char kmac_string
[] = {
108 0x01, 0x20, 0x4B, 0x4D, 0x41, 0x43
112 #define KMAC_FLAG_XOF_MODE 1
114 struct kmac_data_st
{
121 /* If xof_mode = 1 then we use right_encode(0) */
123 /* key and custom are stored in encoded form */
124 unsigned char key
[KMAC_MAX_KEY_ENCODED
];
125 unsigned char custom
[KMAC_MAX_CUSTOM_ENCODED
];
128 static int encode_string(unsigned char *out
, int *out_len
,
129 const unsigned char *in
, int in_len
);
130 static int right_encode(unsigned char *out
, int *out_len
, size_t bits
);
131 static int bytepad(unsigned char *out
, int *out_len
,
132 const unsigned char *in1
, int in1_len
,
133 const unsigned char *in2
, int in2_len
,
135 static int kmac_bytepad_encode_key(unsigned char *out
, int *out_len
,
136 const unsigned char *in
, int in_len
,
139 static void kmac_free(void *vmacctx
)
141 struct kmac_data_st
*kctx
= vmacctx
;
144 EVP_MD_CTX_free(kctx
->ctx
);
145 ossl_prov_digest_reset(&kctx
->digest
);
146 OPENSSL_cleanse(kctx
->key
, kctx
->key_len
);
147 OPENSSL_cleanse(kctx
->custom
, kctx
->custom_len
);
153 * We have KMAC implemented as a hash, which we can use instead of
154 * reimplementing the EVP functionality with direct use of
155 * keccak_mac_init() and friends.
157 static struct kmac_data_st
*kmac_new(void *provctx
)
159 struct kmac_data_st
*kctx
;
161 if ((kctx
= OPENSSL_zalloc(sizeof(*kctx
))) == NULL
162 || (kctx
->ctx
= EVP_MD_CTX_new()) == NULL
) {
166 kctx
->provctx
= provctx
;
170 static void *kmac_fetch_new(void *provctx
, const OSSL_PARAM
*params
)
172 struct kmac_data_st
*kctx
= kmac_new(provctx
);
176 if (!ossl_prov_digest_load_from_params(&kctx
->digest
, params
,
177 PROV_LIBRARY_CONTEXT_OF(provctx
))) {
182 kctx
->out_len
= EVP_MD_size(ossl_prov_digest_md(&kctx
->digest
));
186 static void *kmac128_new(void *provctx
)
188 static const OSSL_PARAM kmac128_params
[] = {
189 OSSL_PARAM_utf8_string("digest", OSSL_DIGEST_NAME_KECCAK_KMAC128
,
190 sizeof(OSSL_DIGEST_NAME_KECCAK_KMAC128
)),
193 return kmac_fetch_new(provctx
, kmac128_params
);
196 static void *kmac256_new(void *provctx
)
198 static const OSSL_PARAM kmac256_params
[] = {
199 OSSL_PARAM_utf8_string("digest", OSSL_DIGEST_NAME_KECCAK_KMAC256
,
200 sizeof(OSSL_DIGEST_NAME_KECCAK_KMAC256
)),
203 return kmac_fetch_new(provctx
, kmac256_params
);
206 static void *kmac_dup(void *vsrc
)
208 struct kmac_data_st
*src
= vsrc
;
209 struct kmac_data_st
*dst
= kmac_new(src
->provctx
);
214 if (!EVP_MD_CTX_copy(dst
->ctx
, src
->ctx
)
215 || !ossl_prov_digest_copy(&dst
->digest
, &src
->digest
)) {
220 dst
->out_len
= src
->out_len
;
221 dst
->key_len
= src
->key_len
;
222 dst
->custom_len
= src
->custom_len
;
223 dst
->xof_mode
= src
->xof_mode
;
224 memcpy(dst
->key
, src
->key
, src
->key_len
);
225 memcpy(dst
->custom
, src
->custom
, dst
->custom_len
);
231 * The init() assumes that any ctrl methods are set beforehand for
232 * md, key and custom. Setting the fields afterwards will have no
233 * effect on the output mac.
235 static int kmac_init(void *vmacctx
)
237 struct kmac_data_st
*kctx
= vmacctx
;
238 EVP_MD_CTX
*ctx
= kctx
->ctx
;
239 unsigned char out
[KMAC_MAX_BLOCKSIZE
];
240 int out_len
, block_len
;
243 /* Check key has been set */
244 if (kctx
->key_len
== 0) {
245 EVPerr(EVP_F_KMAC_INIT
, EVP_R_NO_KEY_SET
);
248 if (!EVP_DigestInit_ex(kctx
->ctx
, ossl_prov_digest_md(&kctx
->digest
),
252 block_len
= EVP_MD_block_size(ossl_prov_digest_md(&kctx
->digest
));
256 /* Set default custom string if it is not already set */
257 if (kctx
->custom_len
== 0) {
258 const OSSL_PARAM params
[] = {
259 OSSL_PARAM_octet_string(OSSL_MAC_PARAM_CUSTOM
, "", 0),
262 (void)kmac_set_ctx_params(kctx
, params
);
265 return bytepad(out
, &out_len
, kmac_string
, sizeof(kmac_string
),
266 kctx
->custom
, kctx
->custom_len
, block_len
)
267 && EVP_DigestUpdate(ctx
, out
, out_len
)
268 && EVP_DigestUpdate(ctx
, kctx
->key
, kctx
->key_len
);
271 static size_t kmac_size(void *vmacctx
)
273 struct kmac_data_st
*kctx
= vmacctx
;
275 return kctx
->out_len
;
278 static int kmac_update(void *vmacctx
, const unsigned char *data
,
281 struct kmac_data_st
*kctx
= vmacctx
;
283 return EVP_DigestUpdate(kctx
->ctx
, data
, datalen
);
286 static int kmac_final(void *vmacctx
, unsigned char *out
, size_t *outl
,
289 struct kmac_data_st
*kctx
= vmacctx
;
290 EVP_MD_CTX
*ctx
= kctx
->ctx
;
292 unsigned char encoded_outlen
[KMAC_MAX_ENCODED_HEADER_LEN
];
295 /* KMAC XOF mode sets the encoded length to 0 */
296 lbits
= (kctx
->xof_mode
? 0 : (kctx
->out_len
* 8));
298 ok
= right_encode(encoded_outlen
, &len
, lbits
)
299 && EVP_DigestUpdate(ctx
, encoded_outlen
, len
)
300 && EVP_DigestFinalXOF(ctx
, out
, kctx
->out_len
);
301 *outl
= kctx
->out_len
;
305 static const OSSL_PARAM known_gettable_ctx_params
[] = {
306 OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE
, NULL
),
309 static const OSSL_PARAM
*kmac_gettable_ctx_params(void *provctx
)
311 return known_gettable_ctx_params
;
314 static int kmac_get_ctx_params(void *vmacctx
, OSSL_PARAM params
[])
318 if ((p
= OSSL_PARAM_locate(params
, OSSL_MAC_PARAM_SIZE
)) != NULL
)
319 return OSSL_PARAM_set_size_t(p
, kmac_size(vmacctx
));
324 static const OSSL_PARAM known_settable_ctx_params
[] = {
325 OSSL_PARAM_int(OSSL_MAC_PARAM_XOF
, NULL
),
326 OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE
, NULL
),
327 OSSL_PARAM_octet_string(OSSL_MAC_PARAM_KEY
, NULL
, 0),
328 OSSL_PARAM_octet_string(OSSL_MAC_PARAM_CUSTOM
, NULL
, 0),
331 static const OSSL_PARAM
*kmac_settable_ctx_params(void *provctx
)
333 return known_settable_ctx_params
;
337 * The following params can be set any time before final():
338 * - "outlen" or "size": The requested output length.
339 * - "xof": If set, this indicates that right_encoded(0)
340 * is part of the digested data, otherwise it
341 * uses right_encoded(requested output length).
343 * All other params should be set before init().
345 static int kmac_set_ctx_params(void *vmacctx
, const OSSL_PARAM
*params
)
347 struct kmac_data_st
*kctx
= vmacctx
;
349 const EVP_MD
*digest
= ossl_prov_digest_md(&kctx
->digest
);
351 if ((p
= OSSL_PARAM_locate_const(params
, OSSL_MAC_PARAM_XOF
)) != NULL
352 && !OSSL_PARAM_get_int(p
, &kctx
->xof_mode
))
354 if (((p
= OSSL_PARAM_locate_const(params
, OSSL_MAC_PARAM_SIZE
)) != NULL
)
355 && !OSSL_PARAM_get_size_t(p
, &kctx
->out_len
))
357 if ((p
= OSSL_PARAM_locate_const(params
, OSSL_MAC_PARAM_KEY
)) != NULL
) {
358 if (p
->data_size
< 4 || p
->data_size
> KMAC_MAX_KEY
) {
359 ERR_raise(ERR_LIB_PROV
, PROV_R_INVALID_KEY_LENGTH
);
362 if (!kmac_bytepad_encode_key(kctx
->key
, &kctx
->key_len
,
363 p
->data
, p
->data_size
,
364 EVP_MD_block_size(digest
)))
367 if ((p
= OSSL_PARAM_locate_const(params
, OSSL_MAC_PARAM_CUSTOM
))
369 if (p
->data_size
> KMAC_MAX_CUSTOM
) {
370 ERR_raise(ERR_LIB_PROV
, PROV_R_INVALID_CUSTOM_LENGTH
);
373 if (!encode_string(kctx
->custom
, &kctx
->custom_len
,
374 p
->data
, p
->data_size
))
381 * Encoding/Padding Methods.
384 /* Returns the number of bytes required to store 'bits' into a byte array */
385 static unsigned int get_encode_size(size_t bits
)
387 unsigned int cnt
= 0, sz
= sizeof(size_t);
389 while (bits
&& (cnt
< sz
)) {
393 /* If bits is zero 1 byte is required */
400 * Convert an integer into bytes . The number of bytes is appended
401 * to the end of the buffer. Returns an array of bytes 'out' of size
404 * e.g if bits = 32, out[2] = { 0x20, 0x01 }
407 static int right_encode(unsigned char *out
, int *out_len
, size_t bits
)
409 unsigned int len
= get_encode_size(bits
);
412 /* The length is constrained to a single byte: 2040/8 = 255 */
416 /* MSB's are at the start of the bytes array */
417 for (i
= len
- 1; i
>= 0; --i
) {
418 out
[i
] = (unsigned char)(bits
& 0xFF);
421 /* Tack the length onto the end */
422 out
[len
] = (unsigned char)len
;
424 /* The Returned length includes the tacked on byte */
430 * Encodes a string with a left encoded length added. Note that the
431 * in_len is converted to bits (*8).
433 * e.g- in="KMAC" gives out[6] = { 0x01, 0x20, 0x4B, 0x4D, 0x41, 0x43 }
436 static int encode_string(unsigned char *out
, int *out_len
,
437 const unsigned char *in
, int in_len
)
445 len
= get_encode_size(bits
);
450 for (i
= len
; i
> 0; --i
) {
451 out
[i
] = (bits
& 0xFF);
454 memcpy(out
+ len
+ 1, in
, in_len
);
455 *out_len
= (1 + len
+ in_len
);
461 * Returns a zero padded encoding of the inputs in1 and an optional
462 * in2 (can be NULL). The padded output must be a multiple of the blocksize 'w'.
463 * The value of w is in bytes (< 256).
465 * The returned output is:
466 * zero_padded(multiple of w, (left_encode(w) || in1 [|| in2])
468 static int bytepad(unsigned char *out
, int *out_len
,
469 const unsigned char *in1
, int in1_len
,
470 const unsigned char *in2
, int in2_len
, int w
)
473 unsigned char *p
= out
;
480 memcpy(p
, in1
, in1_len
);
483 if (in2
!= NULL
&& in2_len
> 0) {
484 memcpy(p
, in2
, in2_len
);
487 /* Figure out the pad size (divisible by w) */
492 /* zero pad the end of the buffer */
493 memset(p
, 0, sz
- len
);
499 * Returns out = bytepad(encode_string(in), w)
501 static int kmac_bytepad_encode_key(unsigned char *out
, int *out_len
,
502 const unsigned char *in
, int in_len
,
505 unsigned char tmp
[KMAC_MAX_KEY
+ KMAC_MAX_ENCODED_HEADER_LEN
];
508 if (!encode_string(tmp
, &tmp_len
, in
, in_len
))
511 return bytepad(out
, out_len
, tmp
, tmp_len
, NULL
, 0, w
);
514 const OSSL_DISPATCH kmac128_functions
[] = {
515 { OSSL_FUNC_MAC_NEWCTX
, (void (*)(void))kmac128_new
},
516 { OSSL_FUNC_MAC_DUPCTX
, (void (*)(void))kmac_dup
},
517 { OSSL_FUNC_MAC_FREECTX
, (void (*)(void))kmac_free
},
518 { OSSL_FUNC_MAC_INIT
, (void (*)(void))kmac_init
},
519 { OSSL_FUNC_MAC_UPDATE
, (void (*)(void))kmac_update
},
520 { OSSL_FUNC_MAC_FINAL
, (void (*)(void))kmac_final
},
521 { OSSL_FUNC_MAC_GETTABLE_CTX_PARAMS
,
522 (void (*)(void))kmac_gettable_ctx_params
},
523 { OSSL_FUNC_MAC_GET_CTX_PARAMS
, (void (*)(void))kmac_get_ctx_params
},
524 { OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS
,
525 (void (*)(void))kmac_settable_ctx_params
},
526 { OSSL_FUNC_MAC_SET_CTX_PARAMS
, (void (*)(void))kmac_set_ctx_params
},
530 const OSSL_DISPATCH kmac256_functions
[] = {
531 { OSSL_FUNC_MAC_NEWCTX
, (void (*)(void))kmac256_new
},
532 { OSSL_FUNC_MAC_DUPCTX
, (void (*)(void))kmac_dup
},
533 { OSSL_FUNC_MAC_FREECTX
, (void (*)(void))kmac_free
},
534 { OSSL_FUNC_MAC_INIT
, (void (*)(void))kmac_init
},
535 { OSSL_FUNC_MAC_UPDATE
, (void (*)(void))kmac_update
},
536 { OSSL_FUNC_MAC_FINAL
, (void (*)(void))kmac_final
},
537 { OSSL_FUNC_MAC_GETTABLE_CTX_PARAMS
,
538 (void (*)(void))kmac_gettable_ctx_params
},
539 { OSSL_FUNC_MAC_GET_CTX_PARAMS
, (void (*)(void))kmac_get_ctx_params
},
540 { OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS
,
541 (void (*)(void))kmac_settable_ctx_params
},
542 { OSSL_FUNC_MAC_SET_CTX_PARAMS
, (void (*)(void))kmac_set_ctx_params
},