2 * Copyright 2018 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).
50 #include <openssl/evp.h>
51 #include "internal/cryptlib.h"
52 #include "internal/evp_int.h"
54 #define KMAC_MAX_BLOCKSIZE ((1600 - 128*2) / 8) /* 168 */
55 #define KMAC_MIN_BLOCKSIZE ((1600 - 256*2) / 8) /* 136 */
57 /* Length encoding will be a 1 byte size + length in bits (2 bytes max) */
58 #define KMAC_MAX_ENCODED_HEADER_LEN 3
61 * Custom string max size is chosen such that:
62 * len(encoded_string(custom) + len(kmac_encoded_string) <= KMAC_MIN_BLOCKSIZE
63 * i.e: (KMAC_MAX_CUSTOM + KMAC_MAX_ENCODED_LEN) + 6 <= 136
65 #define KMAC_MAX_CUSTOM 127
67 /* Maximum size of encoded custom string */
68 #define KMAC_MAX_CUSTOM_ENCODED (KMAC_MAX_CUSTOM + KMAC_MAX_ENCODED_HEADER_LEN)
70 /* Maximum key size in bytes = 2040 / 8 */
71 #define KMAC_MAX_KEY 255
74 * Maximum Encoded Key size will be padded to a multiple of the blocksize
75 * i.e KMAC_MAX_KEY + KMAC_MAX_ENCODED_LEN = 258
76 * Padded to a multiple of KMAC_MAX_BLOCKSIZE
78 #define KMAC_MAX_KEY_ENCODED (KMAC_MAX_BLOCKSIZE * 2)
80 /* Fixed value of encode_string("KMAC") */
81 static const unsigned char kmac_string
[] = {
82 0x01, 0x20, 0x4B, 0x4D, 0x41, 0x43
86 #define KMAC_FLAG_XOF_MODE 1
88 /* typedef EVP_MAC_IMPL */
89 struct evp_mac_impl_st
{
95 /* If xof_mode = 1 then we use right_encode(0) */
97 /* key and custom are stored in encoded form */
98 unsigned char key
[KMAC_MAX_KEY_ENCODED
];
99 unsigned char custom
[KMAC_MAX_CUSTOM_ENCODED
];
102 static int encode_string(unsigned char *out
, int *out_len
,
103 const unsigned char *in
, int in_len
);
104 static int right_encode(unsigned char *out
, int *out_len
, size_t bits
);
105 static int bytepad(unsigned char *out
, int *out_len
,
106 const unsigned char *in1
, int in1_len
,
107 const unsigned char *in2
, int in2_len
,
109 static int kmac_bytepad_encode_key(unsigned char *out
, int *out_len
,
110 const unsigned char *in
, int in_len
,
112 static int kmac_ctrl_str(EVP_MAC_IMPL
*kctx
, const char *type
,
116 static void kmac_free(EVP_MAC_IMPL
*kctx
)
119 EVP_MD_CTX_free(kctx
->ctx
);
120 OPENSSL_cleanse(kctx
->key
, kctx
->key_len
);
121 OPENSSL_cleanse(kctx
->custom
, kctx
->custom_len
);
126 static EVP_MAC_IMPL
*kmac_new(const EVP_MD
*md
)
128 EVP_MAC_IMPL
*kctx
= NULL
;
130 if ((kctx
= OPENSSL_zalloc(sizeof(*kctx
))) == NULL
131 || (kctx
->ctx
= EVP_MD_CTX_new()) == NULL
) {
136 kctx
->out_len
= md
->md_size
;
140 static EVP_MAC_IMPL
*kmac128_new(void)
142 return kmac_new(evp_keccak_kmac128());
145 static EVP_MAC_IMPL
*kmac256_new(void)
147 return kmac_new(evp_keccak_kmac256());
150 static int kmac_copy(EVP_MAC_IMPL
*gdst
, EVP_MAC_IMPL
*gsrc
)
153 gdst
->out_len
= gsrc
->out_len
;
154 gdst
->key_len
= gsrc
->key_len
;
155 gdst
->custom_len
= gsrc
->custom_len
;
156 gdst
->xof_mode
= gsrc
->xof_mode
;
157 memcpy(gdst
->key
, gsrc
->key
, gsrc
->key_len
);
158 memcpy(gdst
->custom
, gsrc
->custom
, gdst
->custom_len
);
160 return EVP_MD_CTX_copy(gdst
->ctx
, gsrc
->ctx
);
164 * The init() assumes that any ctrl methods are set beforehand for
165 * md, key and custom. Setting the fields afterwards will have no
166 * effect on the output mac.
168 static int kmac_init(EVP_MAC_IMPL
*kctx
)
170 EVP_MD_CTX
*ctx
= kctx
->ctx
;
171 unsigned char out
[KMAC_MAX_BLOCKSIZE
];
172 int out_len
, block_len
;
174 /* Check key has been set */
175 if (kctx
->key_len
== 0) {
176 EVPerr(EVP_F_KMAC_INIT
, EVP_R_NO_KEY_SET
);
179 if (!EVP_DigestInit_ex(kctx
->ctx
, kctx
->md
, NULL
))
182 block_len
= EVP_MD_block_size(kctx
->md
);
184 /* Set default custom string if it is not already set */
185 if (kctx
->custom_len
== 0)
186 (void)kmac_ctrl_str(kctx
, "custom", "");
188 return bytepad(out
, &out_len
, kmac_string
, sizeof(kmac_string
),
189 kctx
->custom
, kctx
->custom_len
, block_len
)
190 && EVP_DigestUpdate(ctx
, out
, out_len
)
191 && EVP_DigestUpdate(ctx
, kctx
->key
, kctx
->key_len
);
194 static size_t kmac_size(EVP_MAC_IMPL
*kctx
)
196 return kctx
->out_len
;
199 static int kmac_update(EVP_MAC_IMPL
*kctx
, const unsigned char *data
,
202 return EVP_DigestUpdate(kctx
->ctx
, data
, datalen
);
205 static int kmac_final(EVP_MAC_IMPL
*kctx
, unsigned char *out
)
207 EVP_MD_CTX
*ctx
= kctx
->ctx
;
209 unsigned char encoded_outlen
[KMAC_MAX_ENCODED_HEADER_LEN
];
211 /* KMAC XOF mode sets the encoded length to 0 */
212 lbits
= (kctx
->xof_mode
? 0 : (kctx
->out_len
* 8));
214 return right_encode(encoded_outlen
, &len
, lbits
)
215 && EVP_DigestUpdate(ctx
, encoded_outlen
, len
)
216 && EVP_DigestFinalXOF(ctx
, out
, kctx
->out_len
);
220 * The following Ctrl functions can be set any time before final():
221 * - EVP_MAC_CTRL_SET_SIZE: The requested output length.
222 * - EVP_MAC_CTRL_SET_XOF: If set, this indicates that right_encoded(0) is
223 * part of the digested data, otherwise it uses
224 * right_encoded(requested output length).
226 * All other Ctrl functions should be set before init().
228 static int kmac_ctrl(EVP_MAC_IMPL
*kctx
, int cmd
, va_list args
)
230 const unsigned char *p
;
235 case EVP_MAC_CTRL_SET_XOF
:
236 kctx
->xof_mode
= va_arg(args
, int);
239 case EVP_MAC_CTRL_SET_SIZE
:
240 size
= va_arg(args
, size_t);
241 kctx
->out_len
= size
;
244 case EVP_MAC_CTRL_SET_KEY
:
245 p
= va_arg(args
, const unsigned char *);
246 len
= va_arg(args
, size_t);
247 if (len
< 4 || len
> KMAC_MAX_KEY
) {
248 EVPerr(EVP_F_KMAC_CTRL
, EVP_R_INVALID_KEY_LENGTH
);
251 return kmac_bytepad_encode_key(kctx
->key
, &kctx
->key_len
, p
, len
,
252 EVP_MD_block_size(kctx
->md
));
254 case EVP_MAC_CTRL_SET_CUSTOM
:
255 p
= va_arg(args
, const unsigned char *);
256 len
= va_arg(args
, size_t);
257 if (len
> KMAC_MAX_CUSTOM
) {
258 EVPerr(EVP_F_KMAC_CTRL
, EVP_R_INVALID_CUSTOM_LENGTH
);
261 return encode_string(kctx
->custom
, &kctx
->custom_len
, p
, len
);
268 static int kmac_ctrl_int(EVP_MAC_IMPL
*kctx
, int cmd
, ...)
274 rv
= kmac_ctrl(kctx
, cmd
, args
);
280 static int kmac_ctrl_str_cb(void *kctx
, int cmd
, void *buf
, size_t buflen
)
282 return kmac_ctrl_int(kctx
, cmd
, buf
, buflen
);
285 static int kmac_ctrl_str(EVP_MAC_IMPL
*kctx
, const char *type
,
291 if (strcmp(type
, "outlen") == 0)
292 return kmac_ctrl_int(kctx
, EVP_MAC_CTRL_SET_SIZE
, (size_t)atoi(value
));
293 if (strcmp(type
, "xof") == 0)
294 return kmac_ctrl_int(kctx
, EVP_MAC_CTRL_SET_XOF
, atoi(value
));
295 if (strcmp(type
, "key") == 0)
296 return EVP_str2ctrl(kmac_ctrl_str_cb
, kctx
, EVP_MAC_CTRL_SET_KEY
,
298 if (strcmp(type
, "hexkey") == 0)
299 return EVP_hex2ctrl(kmac_ctrl_str_cb
, kctx
, EVP_MAC_CTRL_SET_KEY
,
301 if (strcmp(type
, "custom") == 0)
302 return EVP_str2ctrl(kmac_ctrl_str_cb
, kctx
, EVP_MAC_CTRL_SET_CUSTOM
,
304 if (strcmp(type
, "hexcustom") == 0)
305 return EVP_hex2ctrl(kmac_ctrl_str_cb
, kctx
, EVP_MAC_CTRL_SET_CUSTOM
,
311 * Encoding/Padding Methods.
314 /* Returns the number of bytes required to store 'bits' into a byte array */
315 static unsigned int get_encode_size(size_t bits
)
317 unsigned int cnt
= 0, sz
= sizeof(size_t);
319 while (bits
&& (cnt
< sz
)) {
323 /* If bits is zero 1 byte is required */
330 * Convert an integer into bytes . The number of bytes is appended
331 * to the end of the buffer. Returns an array of bytes 'out' of size
334 * e.g if bits = 32, out[2] = { 0x20, 0x01 }
337 static int right_encode(unsigned char *out
, int *out_len
, size_t bits
)
339 unsigned int len
= get_encode_size(bits
);
342 /* The length is constrained to a single byte: 2040/8 = 255 */
346 /* MSB's are at the start of the bytes array */
347 for (i
= len
- 1; i
>= 0; --i
) {
348 out
[i
] = (unsigned char)(bits
& 0xFF);
351 /* Tack the length onto the end */
352 out
[len
] = (unsigned char)len
;
354 /* The Returned length includes the tacked on byte */
360 * Encodes a string with a left encoded length added. Note that the
361 * in_len is converted to bits (*8).
363 * e.g- in="KMAC" gives out[6] = { 0x01, 0x20, 0x4B, 0x4D, 0x41, 0x43 }
366 static int encode_string(unsigned char *out
, int *out_len
,
367 const unsigned char *in
, int in_len
)
375 len
= get_encode_size(bits
);
380 for (i
= len
; i
> 0; --i
) {
381 out
[i
] = (bits
& 0xFF);
384 memcpy(out
+ len
+ 1, in
, in_len
);
385 *out_len
= (1 + len
+ in_len
);
391 * Returns a zero padded encoding of the inputs in1 and an optional
392 * in2 (can be NULL). The padded output must be a multiple of the blocksize 'w'.
393 * The value of w is in bytes (< 256).
395 * The returned output is:
396 * zero_padded(multiple of w, (left_encode(w) || in1 [|| in2])
398 static int bytepad(unsigned char *out
, int *out_len
,
399 const unsigned char *in1
, int in1_len
,
400 const unsigned char *in2
, int in2_len
, int w
)
403 unsigned char *p
= out
;
410 memcpy(p
, in1
, in1_len
);
413 if (in2
!= NULL
&& in2_len
> 0) {
414 memcpy(p
, in2
, in2_len
);
417 /* Figure out the pad size (divisible by w) */
422 /* zero pad the end of the buffer */
423 memset(p
, 0, sz
- len
);
429 * Returns out = bytepad(encode_string(in), w)
431 static int kmac_bytepad_encode_key(unsigned char *out
, int *out_len
,
432 const unsigned char *in
, int in_len
,
435 unsigned char tmp
[KMAC_MAX_KEY
+ KMAC_MAX_ENCODED_HEADER_LEN
];
438 if (!encode_string(tmp
, &tmp_len
, in
, in_len
))
441 return bytepad(out
, out_len
, tmp
, tmp_len
, NULL
, 0, w
);
444 const EVP_MAC kmac128_meth
= {
457 const EVP_MAC kmac256_meth
= {