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 EVP_MAC_IMPL *kmac_dup(const EVP_MAC_IMPL *gsrc)
154 gdst = kmac_new(gsrc->md);
158 if (!EVP_MD_CTX_copy(gdst->ctx, gsrc->ctx)) {
164 gdst->out_len = gsrc->out_len;
165 gdst->key_len = gsrc->key_len;
166 gdst->custom_len = gsrc->custom_len;
167 gdst->xof_mode = gsrc->xof_mode;
168 memcpy(gdst->key, gsrc->key, gsrc->key_len);
169 memcpy(gdst->custom, gsrc->custom, gdst->custom_len);
175 * The init() assumes that any ctrl methods are set beforehand for
176 * md, key and custom. Setting the fields afterwards will have no
177 * effect on the output mac.
179 static int kmac_init(EVP_MAC_IMPL *kctx)
181 EVP_MD_CTX *ctx = kctx->ctx;
182 unsigned char out[KMAC_MAX_BLOCKSIZE];
183 int out_len, block_len;
185 /* Check key has been set */
186 if (kctx->key_len == 0) {
187 EVPerr(EVP_F_KMAC_INIT, EVP_R_NO_KEY_SET);
190 if (!EVP_DigestInit_ex(kctx->ctx, kctx->md, NULL))
193 block_len = EVP_MD_block_size(kctx->md);
195 /* Set default custom string if it is not already set */
196 if (kctx->custom_len == 0)
197 (void)kmac_ctrl_str(kctx, "custom", "");
199 return bytepad(out, &out_len, kmac_string, sizeof(kmac_string),
200 kctx->custom, kctx->custom_len, block_len)
201 && EVP_DigestUpdate(ctx, out, out_len)
202 && EVP_DigestUpdate(ctx, kctx->key, kctx->key_len);
205 static size_t kmac_size(EVP_MAC_IMPL *kctx)
207 return kctx->out_len;
210 static int kmac_update(EVP_MAC_IMPL *kctx, const unsigned char *data,
213 return EVP_DigestUpdate(kctx->ctx, data, datalen);
216 static int kmac_final(EVP_MAC_IMPL *kctx, unsigned char *out)
218 EVP_MD_CTX *ctx = kctx->ctx;
220 unsigned char encoded_outlen[KMAC_MAX_ENCODED_HEADER_LEN];
222 /* KMAC XOF mode sets the encoded length to 0 */
223 lbits = (kctx->xof_mode ? 0 : (kctx->out_len * 8));
225 return right_encode(encoded_outlen, &len, lbits)
226 && EVP_DigestUpdate(ctx, encoded_outlen, len)
227 && EVP_DigestFinalXOF(ctx, out, kctx->out_len);
231 * The following Ctrl functions can be set any time before final():
232 * - EVP_MAC_CTRL_SET_SIZE: The requested output length.
233 * - EVP_MAC_CTRL_SET_XOF: If set, this indicates that right_encoded(0) is
234 * part of the digested data, otherwise it uses
235 * right_encoded(requested output length).
237 * All other Ctrl functions should be set before init().
239 static int kmac_ctrl(EVP_MAC_IMPL *kctx, int cmd, va_list args)
241 const unsigned char *p;
246 case EVP_MAC_CTRL_SET_XOF:
247 kctx->xof_mode = va_arg(args, int);
250 case EVP_MAC_CTRL_SET_SIZE:
251 size = va_arg(args, size_t);
252 kctx->out_len = size;
255 case EVP_MAC_CTRL_SET_KEY:
256 p = va_arg(args, const unsigned char *);
257 len = va_arg(args, size_t);
258 if (len < 4 || len > KMAC_MAX_KEY) {
259 EVPerr(EVP_F_KMAC_CTRL, EVP_R_INVALID_KEY_LENGTH);
262 return kmac_bytepad_encode_key(kctx->key, &kctx->key_len, p, len,
263 EVP_MD_block_size(kctx->md));
265 case EVP_MAC_CTRL_SET_CUSTOM:
266 p = va_arg(args, const unsigned char *);
267 len = va_arg(args, size_t);
268 if (len > KMAC_MAX_CUSTOM) {
269 EVPerr(EVP_F_KMAC_CTRL, EVP_R_INVALID_CUSTOM_LENGTH);
272 return encode_string(kctx->custom, &kctx->custom_len, p, len);
279 static int kmac_ctrl_int(EVP_MAC_IMPL *kctx, int cmd, ...)
285 rv = kmac_ctrl(kctx, cmd, args);
291 static int kmac_ctrl_str_cb(void *kctx, int cmd, void *buf, size_t buflen)
293 return kmac_ctrl_int(kctx, cmd, buf, buflen);
296 static int kmac_ctrl_str(EVP_MAC_IMPL *kctx, const char *type,
302 if (strcmp(type, "outlen") == 0)
303 return kmac_ctrl_int(kctx, EVP_MAC_CTRL_SET_SIZE, (size_t)atoi(value));
304 if (strcmp(type, "xof") == 0)
305 return kmac_ctrl_int(kctx, EVP_MAC_CTRL_SET_XOF, atoi(value));
306 if (strcmp(type, "key") == 0)
307 return EVP_str2ctrl(kmac_ctrl_str_cb, kctx, EVP_MAC_CTRL_SET_KEY,
309 if (strcmp(type, "hexkey") == 0)
310 return EVP_hex2ctrl(kmac_ctrl_str_cb, kctx, EVP_MAC_CTRL_SET_KEY,
312 if (strcmp(type, "custom") == 0)
313 return EVP_str2ctrl(kmac_ctrl_str_cb, kctx, EVP_MAC_CTRL_SET_CUSTOM,
315 if (strcmp(type, "hexcustom") == 0)
316 return EVP_hex2ctrl(kmac_ctrl_str_cb, kctx, EVP_MAC_CTRL_SET_CUSTOM,
322 * Encoding/Padding Methods.
325 /* Returns the number of bytes required to store 'bits' into a byte array */
326 static unsigned int get_encode_size(size_t bits)
328 unsigned int cnt = 0, sz = sizeof(size_t);
330 while (bits && (cnt < sz)) {
334 /* If bits is zero 1 byte is required */
341 * Convert an integer into bytes . The number of bytes is appended
342 * to the end of the buffer. Returns an array of bytes 'out' of size
345 * e.g if bits = 32, out[2] = { 0x20, 0x01 }
348 static int right_encode(unsigned char *out, int *out_len, size_t bits)
350 unsigned int len = get_encode_size(bits);
353 /* The length is constrained to a single byte: 2040/8 = 255 */
357 /* MSB's are at the start of the bytes array */
358 for (i = len - 1; i >= 0; --i) {
359 out[i] = (unsigned char)(bits & 0xFF);
362 /* Tack the length onto the end */
363 out[len] = (unsigned char)len;
365 /* The Returned length includes the tacked on byte */
371 * Encodes a string with a left encoded length added. Note that the
372 * in_len is converted to bits (*8).
374 * e.g- in="KMAC" gives out[6] = { 0x01, 0x20, 0x4B, 0x4D, 0x41, 0x43 }
377 static int encode_string(unsigned char *out, int *out_len,
378 const unsigned char *in, int in_len)
386 len = get_encode_size(bits);
391 for (i = len; i > 0; --i) {
392 out[i] = (bits & 0xFF);
395 memcpy(out + len + 1, in, in_len);
396 *out_len = (1 + len + in_len);
402 * Returns a zero padded encoding of the inputs in1 and an optional
403 * in2 (can be NULL). The padded output must be a multiple of the blocksize 'w'.
404 * The value of w is in bytes (< 256).
406 * The returned output is:
407 * zero_padded(multiple of w, (left_encode(w) || in1 [|| in2])
409 static int bytepad(unsigned char *out, int *out_len,
410 const unsigned char *in1, int in1_len,
411 const unsigned char *in2, int in2_len, int w)
414 unsigned char *p = out;
421 memcpy(p, in1, in1_len);
424 if (in2 != NULL && in2_len > 0) {
425 memcpy(p, in2, in2_len);
428 /* Figure out the pad size (divisible by w) */
433 /* zero pad the end of the buffer */
434 memset(p, 0, sz - len);
440 * Returns out = bytepad(encode_string(in), w)
442 static int kmac_bytepad_encode_key(unsigned char *out, int *out_len,
443 const unsigned char *in, int in_len,
446 unsigned char tmp[KMAC_MAX_KEY + KMAC_MAX_ENCODED_HEADER_LEN];
449 if (!encode_string(tmp, &tmp_len, in, in_len))
452 return bytepad(out, out_len, tmp, tmp_len, NULL, 0, w);
455 const EVP_MAC kmac128_meth = {
468 const EVP_MAC kmac256_meth = {