]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/kmac/kmac.c
KMAC implementation using EVP_MAC
[thirdparty/openssl.git] / crypto / kmac / kmac.c
1 /*
2 * Copyright 2018 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the OpenSSL license (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 <openssl/evp.h>
51 #include "internal/cryptlib.h"
52 #include "internal/evp_int.h"
53
54 #define KMAC_MAX_BLOCKSIZE ((1600 - 128*2) / 8) /* 168 */
55 #define KMAC_MIN_BLOCKSIZE ((1600 - 256*2) / 8) /* 136 */
56
57 /* Length encoding will be a 1 byte size + length in bits (2 bytes max) */
58 #define KMAC_MAX_ENCODED_HEADER_LEN 3
59
60 /*
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
64 */
65 #define KMAC_MAX_CUSTOM 127
66
67 /* Maximum size of encoded custom string */
68 #define KMAC_MAX_CUSTOM_ENCODED (KMAC_MAX_CUSTOM + KMAC_MAX_ENCODED_HEADER_LEN)
69
70 /* Maximum key size in bytes = 2040 / 8 */
71 #define KMAC_MAX_KEY 255
72
73 /*
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
77 */
78 #define KMAC_MAX_KEY_ENCODED (KMAC_MAX_BLOCKSIZE * 2)
79
80 /* Fixed value of encode_string("KMAC") */
81 static const unsigned char kmac_string[] = {
82 0x01, 0x20, 0x4B, 0x4D, 0x41, 0x43
83 };
84
85
86 #define KMAC_FLAG_XOF_MODE 1
87
88 /* typedef EVP_MAC_IMPL */
89 struct evp_mac_impl_st {
90 EVP_MD_CTX *ctx;
91 const EVP_MD *md;
92 size_t out_len;
93 int key_len;
94 int custom_len;
95 /* If xof_mode = 1 then we use right_encode(0) */
96 int xof_mode;
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];
100 };
101
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,
108 int w);
109 static int kmac_bytepad_encode_key(unsigned char *out, int *out_len,
110 const unsigned char *in, int in_len,
111 int w);
112 static int kmac_ctrl_str(EVP_MAC_IMPL *kctx, const char *type,
113 const char *value);
114
115
116 static void kmac_free(EVP_MAC_IMPL *kctx)
117 {
118 if (kctx != NULL) {
119 EVP_MD_CTX_free(kctx->ctx);
120 OPENSSL_cleanse(kctx->key, kctx->key_len);
121 OPENSSL_cleanse(kctx->custom, kctx->custom_len);
122 OPENSSL_free(kctx);
123 }
124 }
125
126 static EVP_MAC_IMPL *kmac_new(const EVP_MD *md)
127 {
128 EVP_MAC_IMPL *kctx = NULL;
129
130 if ((kctx = OPENSSL_zalloc(sizeof(*kctx))) == NULL
131 || (kctx->ctx = EVP_MD_CTX_new()) == NULL) {
132 kmac_free(kctx);
133 return NULL;
134 }
135 kctx->md = md;
136 kctx->out_len = md->md_size;
137 return kctx;
138 }
139
140 static EVP_MAC_IMPL *kmac128_new(void)
141 {
142 return kmac_new(evp_keccak_kmac128());
143 }
144
145 static EVP_MAC_IMPL *kmac256_new(void)
146 {
147 return kmac_new(evp_keccak_kmac256());
148 }
149
150 static int kmac_copy(EVP_MAC_IMPL *gdst, EVP_MAC_IMPL *gsrc)
151 {
152 gdst->md = gsrc->md;
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);
159
160 return EVP_MD_CTX_copy(gdst->ctx, gsrc->ctx);
161 }
162
163 /*
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.
167 */
168 static int kmac_init(EVP_MAC_IMPL *kctx)
169 {
170 EVP_MD_CTX *ctx = kctx->ctx;
171 unsigned char out[KMAC_MAX_BLOCKSIZE];
172 int out_len, block_len;
173
174 /* Check key has been set */
175 if (kctx->key_len == 0) {
176 EVPerr(EVP_F_KMAC_INIT, EVP_R_NO_KEY_SET);
177 return 0;
178 }
179 if (!EVP_DigestInit_ex(kctx->ctx, kctx->md, NULL))
180 return 0;
181
182 block_len = EVP_MD_block_size(kctx->md);
183
184 /* Set default custom string if it is not already set */
185 if (kctx->custom_len == 0)
186 (void)kmac_ctrl_str(kctx, "custom", "");
187
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);
192 }
193
194 static size_t kmac_size(EVP_MAC_IMPL *kctx)
195 {
196 return kctx->out_len;
197 }
198
199 static int kmac_update(EVP_MAC_IMPL *kctx, const unsigned char *data,
200 size_t datalen)
201 {
202 return EVP_DigestUpdate(kctx->ctx, data, datalen);
203 }
204
205 static int kmac_final(EVP_MAC_IMPL *kctx, unsigned char *out)
206 {
207 EVP_MD_CTX *ctx = kctx->ctx;
208 int lbits, len;
209 unsigned char encoded_outlen[KMAC_MAX_ENCODED_HEADER_LEN];
210
211 /* KMAC XOF mode sets the encoded length to 0 */
212 lbits = (kctx->xof_mode ? 0 : (kctx->out_len * 8));
213
214 return right_encode(encoded_outlen, &len, lbits)
215 && EVP_DigestUpdate(ctx, encoded_outlen, len)
216 && EVP_DigestFinalXOF(ctx, out, kctx->out_len);
217 }
218
219 /*
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).
225
226 * All other Ctrl functions should be set before init().
227 */
228 static int kmac_ctrl(EVP_MAC_IMPL *kctx, int cmd, va_list args)
229 {
230 const unsigned char *p;
231 size_t len;
232 size_t size;
233
234 switch (cmd) {
235 case EVP_MAC_CTRL_SET_XOF:
236 kctx->xof_mode = va_arg(args, int);
237 return 1;
238
239 case EVP_MAC_CTRL_SET_SIZE:
240 size = va_arg(args, size_t);
241 kctx->out_len = size;
242 return 1;
243
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);
249 return 0;
250 }
251 return kmac_bytepad_encode_key(kctx->key, &kctx->key_len, p, len,
252 EVP_MD_block_size(kctx->md));
253
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);
259 return 0;
260 }
261 return encode_string(kctx->custom, &kctx->custom_len, p, len);
262
263 default:
264 return -2;
265 }
266 }
267
268 static int kmac_ctrl_int(EVP_MAC_IMPL *kctx, int cmd, ...)
269 {
270 int rv;
271 va_list args;
272
273 va_start(args, cmd);
274 rv = kmac_ctrl(kctx, cmd, args);
275 va_end(args);
276
277 return rv;
278 }
279
280 static int kmac_ctrl_str_cb(void *kctx, int cmd, void *buf, size_t buflen)
281 {
282 return kmac_ctrl_int(kctx, cmd, buf, buflen);
283 }
284
285 static int kmac_ctrl_str(EVP_MAC_IMPL *kctx, const char *type,
286 const char *value)
287 {
288 if (value == NULL)
289 return 0;
290
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,
297 value);
298 if (strcmp(type, "hexkey") == 0)
299 return EVP_hex2ctrl(kmac_ctrl_str_cb, kctx, EVP_MAC_CTRL_SET_KEY,
300 value);
301 if (strcmp(type, "custom") == 0)
302 return EVP_str2ctrl(kmac_ctrl_str_cb, kctx, EVP_MAC_CTRL_SET_CUSTOM,
303 value);
304 if (strcmp(type, "hexcustom") == 0)
305 return EVP_hex2ctrl(kmac_ctrl_str_cb, kctx, EVP_MAC_CTRL_SET_CUSTOM,
306 value);
307 return -2;
308 }
309
310 /*
311 * Encoding/Padding Methods.
312 */
313
314 /* Returns the number of bytes required to store 'bits' into a byte array */
315 static unsigned int get_encode_size(size_t bits)
316 {
317 unsigned int cnt = 0, sz = sizeof(size_t);
318
319 while (bits && (cnt < sz)) {
320 ++cnt;
321 bits >>= 8;
322 }
323 /* If bits is zero 1 byte is required */
324 if (cnt == 0)
325 cnt = 1;
326 return cnt;
327 }
328
329 /*
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
332 * *out_len.
333 *
334 * e.g if bits = 32, out[2] = { 0x20, 0x01 }
335 *
336 */
337 static int right_encode(unsigned char *out, int *out_len, size_t bits)
338 {
339 unsigned int len = get_encode_size(bits);
340 int i;
341
342 /* The length is constrained to a single byte: 2040/8 = 255 */
343 if (len > 0xFF)
344 return 0;
345
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);
349 bits >>= 8;
350 }
351 /* Tack the length onto the end */
352 out[len] = (unsigned char)len;
353
354 /* The Returned length includes the tacked on byte */
355 *out_len = len + 1;
356 return 1;
357 }
358
359 /*
360 * Encodes a string with a left encoded length added. Note that the
361 * in_len is converted to bits (*8).
362 *
363 * e.g- in="KMAC" gives out[6] = { 0x01, 0x20, 0x4B, 0x4D, 0x41, 0x43 }
364 * len bits K M A C
365 */
366 static int encode_string(unsigned char *out, int *out_len,
367 const unsigned char *in, int in_len)
368 {
369 if (in == NULL) {
370 *out_len = 0;
371 } else {
372 int i, bits, len;
373
374 bits = 8 * in_len;
375 len = get_encode_size(bits);
376 if (len > 0xFF)
377 return 0;
378
379 out[0] = len;
380 for (i = len; i > 0; --i) {
381 out[i] = (bits & 0xFF);
382 bits >>= 8;
383 }
384 memcpy(out + len + 1, in, in_len);
385 *out_len = (1 + len + in_len);
386 }
387 return 1;
388 }
389
390 /*
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).
394 *
395 * The returned output is:
396 * zero_padded(multiple of w, (left_encode(w) || in1 [|| in2])
397 */
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)
401 {
402 int len;
403 unsigned char *p = out;
404 int sz = w;
405
406 /* Left encoded w */
407 *p++ = 1;
408 *p++ = w;
409 /* || in1 */
410 memcpy(p, in1, in1_len);
411 p += in1_len;
412 /* [ || in2 ] */
413 if (in2 != NULL && in2_len > 0) {
414 memcpy(p, in2, in2_len);
415 p += in2_len;
416 }
417 /* Figure out the pad size (divisible by w) */
418 len = p - out;
419 while (len > sz) {
420 sz += w;
421 }
422 /* zero pad the end of the buffer */
423 memset(p, 0, sz - len);
424 *out_len = sz;
425 return 1;
426 }
427
428 /*
429 * Returns out = bytepad(encode_string(in), w)
430 */
431 static int kmac_bytepad_encode_key(unsigned char *out, int *out_len,
432 const unsigned char *in, int in_len,
433 int w)
434 {
435 unsigned char tmp[KMAC_MAX_KEY + KMAC_MAX_ENCODED_HEADER_LEN];
436 int tmp_len;
437
438 if (!encode_string(tmp, &tmp_len, in, in_len))
439 return 0;
440
441 return bytepad(out, out_len, tmp, tmp_len, NULL, 0, w);
442 }
443
444 const EVP_MAC kmac128_meth = {
445 EVP_MAC_KMAC128,
446 kmac128_new,
447 kmac_copy,
448 kmac_free,
449 kmac_size,
450 kmac_init,
451 kmac_update,
452 kmac_final,
453 kmac_ctrl,
454 kmac_ctrl_str
455 };
456
457 const EVP_MAC kmac256_meth = {
458 EVP_MAC_KMAC256,
459 kmac256_new,
460 kmac_copy,
461 kmac_free,
462 kmac_size,
463 kmac_init,
464 kmac_update,
465 kmac_final,
466 kmac_ctrl,
467 kmac_ctrl_str
468 };
469