]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/kmac/kmac.c
Change EVP_MAC method from copy to dup
[thirdparty/openssl.git] / crypto / kmac / kmac.c
1 /*
2 * Copyright 2018 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 <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 EVP_MAC_IMPL *kmac_dup(const EVP_MAC_IMPL *gsrc)
151 {
152 EVP_MAC_IMPL *gdst;
153
154 gdst = kmac_new(gsrc->md);
155 if (gdst == NULL)
156 return NULL;
157
158 if (!EVP_MD_CTX_copy(gdst->ctx, gsrc->ctx)) {
159 kmac_free(gdst);
160 return NULL;
161 }
162
163 gdst->md = gsrc->md;
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);
170
171 return gdst;
172 }
173
174 /*
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.
178 */
179 static int kmac_init(EVP_MAC_IMPL *kctx)
180 {
181 EVP_MD_CTX *ctx = kctx->ctx;
182 unsigned char out[KMAC_MAX_BLOCKSIZE];
183 int out_len, block_len;
184
185 /* Check key has been set */
186 if (kctx->key_len == 0) {
187 EVPerr(EVP_F_KMAC_INIT, EVP_R_NO_KEY_SET);
188 return 0;
189 }
190 if (!EVP_DigestInit_ex(kctx->ctx, kctx->md, NULL))
191 return 0;
192
193 block_len = EVP_MD_block_size(kctx->md);
194
195 /* Set default custom string if it is not already set */
196 if (kctx->custom_len == 0)
197 (void)kmac_ctrl_str(kctx, "custom", "");
198
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);
203 }
204
205 static size_t kmac_size(EVP_MAC_IMPL *kctx)
206 {
207 return kctx->out_len;
208 }
209
210 static int kmac_update(EVP_MAC_IMPL *kctx, const unsigned char *data,
211 size_t datalen)
212 {
213 return EVP_DigestUpdate(kctx->ctx, data, datalen);
214 }
215
216 static int kmac_final(EVP_MAC_IMPL *kctx, unsigned char *out)
217 {
218 EVP_MD_CTX *ctx = kctx->ctx;
219 int lbits, len;
220 unsigned char encoded_outlen[KMAC_MAX_ENCODED_HEADER_LEN];
221
222 /* KMAC XOF mode sets the encoded length to 0 */
223 lbits = (kctx->xof_mode ? 0 : (kctx->out_len * 8));
224
225 return right_encode(encoded_outlen, &len, lbits)
226 && EVP_DigestUpdate(ctx, encoded_outlen, len)
227 && EVP_DigestFinalXOF(ctx, out, kctx->out_len);
228 }
229
230 /*
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).
236
237 * All other Ctrl functions should be set before init().
238 */
239 static int kmac_ctrl(EVP_MAC_IMPL *kctx, int cmd, va_list args)
240 {
241 const unsigned char *p;
242 size_t len;
243 size_t size;
244
245 switch (cmd) {
246 case EVP_MAC_CTRL_SET_XOF:
247 kctx->xof_mode = va_arg(args, int);
248 return 1;
249
250 case EVP_MAC_CTRL_SET_SIZE:
251 size = va_arg(args, size_t);
252 kctx->out_len = size;
253 return 1;
254
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);
260 return 0;
261 }
262 return kmac_bytepad_encode_key(kctx->key, &kctx->key_len, p, len,
263 EVP_MD_block_size(kctx->md));
264
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);
270 return 0;
271 }
272 return encode_string(kctx->custom, &kctx->custom_len, p, len);
273
274 default:
275 return -2;
276 }
277 }
278
279 static int kmac_ctrl_int(EVP_MAC_IMPL *kctx, int cmd, ...)
280 {
281 int rv;
282 va_list args;
283
284 va_start(args, cmd);
285 rv = kmac_ctrl(kctx, cmd, args);
286 va_end(args);
287
288 return rv;
289 }
290
291 static int kmac_ctrl_str_cb(void *kctx, int cmd, void *buf, size_t buflen)
292 {
293 return kmac_ctrl_int(kctx, cmd, buf, buflen);
294 }
295
296 static int kmac_ctrl_str(EVP_MAC_IMPL *kctx, const char *type,
297 const char *value)
298 {
299 if (value == NULL)
300 return 0;
301
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,
308 value);
309 if (strcmp(type, "hexkey") == 0)
310 return EVP_hex2ctrl(kmac_ctrl_str_cb, kctx, EVP_MAC_CTRL_SET_KEY,
311 value);
312 if (strcmp(type, "custom") == 0)
313 return EVP_str2ctrl(kmac_ctrl_str_cb, kctx, EVP_MAC_CTRL_SET_CUSTOM,
314 value);
315 if (strcmp(type, "hexcustom") == 0)
316 return EVP_hex2ctrl(kmac_ctrl_str_cb, kctx, EVP_MAC_CTRL_SET_CUSTOM,
317 value);
318 return -2;
319 }
320
321 /*
322 * Encoding/Padding Methods.
323 */
324
325 /* Returns the number of bytes required to store 'bits' into a byte array */
326 static unsigned int get_encode_size(size_t bits)
327 {
328 unsigned int cnt = 0, sz = sizeof(size_t);
329
330 while (bits && (cnt < sz)) {
331 ++cnt;
332 bits >>= 8;
333 }
334 /* If bits is zero 1 byte is required */
335 if (cnt == 0)
336 cnt = 1;
337 return cnt;
338 }
339
340 /*
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
343 * *out_len.
344 *
345 * e.g if bits = 32, out[2] = { 0x20, 0x01 }
346 *
347 */
348 static int right_encode(unsigned char *out, int *out_len, size_t bits)
349 {
350 unsigned int len = get_encode_size(bits);
351 int i;
352
353 /* The length is constrained to a single byte: 2040/8 = 255 */
354 if (len > 0xFF)
355 return 0;
356
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);
360 bits >>= 8;
361 }
362 /* Tack the length onto the end */
363 out[len] = (unsigned char)len;
364
365 /* The Returned length includes the tacked on byte */
366 *out_len = len + 1;
367 return 1;
368 }
369
370 /*
371 * Encodes a string with a left encoded length added. Note that the
372 * in_len is converted to bits (*8).
373 *
374 * e.g- in="KMAC" gives out[6] = { 0x01, 0x20, 0x4B, 0x4D, 0x41, 0x43 }
375 * len bits K M A C
376 */
377 static int encode_string(unsigned char *out, int *out_len,
378 const unsigned char *in, int in_len)
379 {
380 if (in == NULL) {
381 *out_len = 0;
382 } else {
383 int i, bits, len;
384
385 bits = 8 * in_len;
386 len = get_encode_size(bits);
387 if (len > 0xFF)
388 return 0;
389
390 out[0] = len;
391 for (i = len; i > 0; --i) {
392 out[i] = (bits & 0xFF);
393 bits >>= 8;
394 }
395 memcpy(out + len + 1, in, in_len);
396 *out_len = (1 + len + in_len);
397 }
398 return 1;
399 }
400
401 /*
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).
405 *
406 * The returned output is:
407 * zero_padded(multiple of w, (left_encode(w) || in1 [|| in2])
408 */
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)
412 {
413 int len;
414 unsigned char *p = out;
415 int sz = w;
416
417 /* Left encoded w */
418 *p++ = 1;
419 *p++ = w;
420 /* || in1 */
421 memcpy(p, in1, in1_len);
422 p += in1_len;
423 /* [ || in2 ] */
424 if (in2 != NULL && in2_len > 0) {
425 memcpy(p, in2, in2_len);
426 p += in2_len;
427 }
428 /* Figure out the pad size (divisible by w) */
429 len = p - out;
430 while (len > sz) {
431 sz += w;
432 }
433 /* zero pad the end of the buffer */
434 memset(p, 0, sz - len);
435 *out_len = sz;
436 return 1;
437 }
438
439 /*
440 * Returns out = bytepad(encode_string(in), w)
441 */
442 static int kmac_bytepad_encode_key(unsigned char *out, int *out_len,
443 const unsigned char *in, int in_len,
444 int w)
445 {
446 unsigned char tmp[KMAC_MAX_KEY + KMAC_MAX_ENCODED_HEADER_LEN];
447 int tmp_len;
448
449 if (!encode_string(tmp, &tmp_len, in, in_len))
450 return 0;
451
452 return bytepad(out, out_len, tmp, tmp_len, NULL, 0, w);
453 }
454
455 const EVP_MAC kmac128_meth = {
456 EVP_MAC_KMAC128,
457 kmac128_new,
458 kmac_dup,
459 kmac_free,
460 kmac_size,
461 kmac_init,
462 kmac_update,
463 kmac_final,
464 kmac_ctrl,
465 kmac_ctrl_str
466 };
467
468 const EVP_MAC kmac256_meth = {
469 EVP_MAC_KMAC256,
470 kmac256_new,
471 kmac_dup,
472 kmac_free,
473 kmac_size,
474 kmac_init,
475 kmac_update,
476 kmac_final,
477 kmac_ctrl,
478 kmac_ctrl_str
479 };
480