]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/kdf/sskdf.c
Adapt apps/mac.c to use provider based MACs
[thirdparty/openssl.git] / crypto / kdf / sskdf.c
CommitLineData
9537fe57
SL
1/*
2 * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
4 *
5 * Licensed under the Apache License 2.0 (the "License"). You may not use
6 * this file except in compliance with the License. You can obtain a copy
7 * in the file LICENSE in the source distribution or at
8 * https://www.openssl.org/source/license.html
9 */
10
11/*
12 * Refer to https://csrc.nist.gov/publications/detail/sp/800-56c/rev-1/final
13 * Section 4.1.
14 *
15 * The Single Step KDF algorithm is given by:
16 *
17 * Result(0) = empty bit string (i.e., the null string).
18 * For i = 1 to reps, do the following:
19 * Increment counter by 1.
7a228c39 20 * Result(i) = Result(i - 1) || H(counter || Z || FixedInfo).
9537fe57
SL
21 * DKM = LeftmostBits(Result(reps), L))
22 *
23 * NOTES:
24 * Z is a shared secret required to produce the derived key material.
25 * counter is a 4 byte buffer.
26 * FixedInfo is a bit string containing context specific data.
27 * DKM is the output derived key material.
28 * L is the required size of the DKM.
29 * reps = [L / H_outputBits]
30 * H(x) is the auxiliary function that can be either a hash, HMAC or KMAC.
31 * H_outputBits is the length of the output of the auxiliary function H(x).
32 *
33 * Currently there is not a comprehensive list of test vectors for this
34 * algorithm, especially for H(x) = HMAC and H(x) = KMAC.
35 * Test vectors for H(x) = Hash are indirectly used by CAVS KAS tests.
36 */
37#include <stdlib.h>
38#include <stdarg.h>
39#include <string.h>
40#include <openssl/hmac.h>
41#include <openssl/evp.h>
42#include <openssl/kdf.h>
43#include "internal/cryptlib.h"
44#include "internal/evp_int.h"
45#include "kdf_local.h"
46
47struct evp_kdf_impl_st {
48 const EVP_MAC *mac; /* H(x) = HMAC_hash OR H(x) = KMAC */
49 const EVP_MD *md; /* H(x) = hash OR when H(x) = HMAC_hash */
50 unsigned char *secret;
51 size_t secret_len;
52 unsigned char *info;
53 size_t info_len;
54 unsigned char *salt;
55 size_t salt_len;
56 size_t out_len; /* optional KMAC parameter */
57};
58
59#define SSKDF_MAX_INLEN (1<<30)
60#define SSKDF_KMAC128_DEFAULT_SALT_SIZE (168 - 4)
61#define SSKDF_KMAC256_DEFAULT_SALT_SIZE (136 - 4)
62
63/* KMAC uses a Customisation string of 'KDF' */
64static const unsigned char kmac_custom_str[] = { 0x4B, 0x44, 0x46 };
65
66/*
67 * Refer to https://csrc.nist.gov/publications/detail/sp/800-56c/rev-1/final
68 * Section 4. One-Step Key Derivation using H(x) = hash(x)
8bbeaaa4
SL
69 * Note: X9.63 also uses this code with the only difference being that the
70 * counter is appended to the secret 'z'.
71 * i.e.
72 * result[i] = Hash(counter || z || info) for One Step OR
73 * result[i] = Hash(z || counter || info) for X9.63.
9537fe57
SL
74 */
75static int SSKDF_hash_kdm(const EVP_MD *kdf_md,
76 const unsigned char *z, size_t z_len,
77 const unsigned char *info, size_t info_len,
8bbeaaa4 78 unsigned int append_ctr,
9537fe57
SL
79 unsigned char *derived_key, size_t derived_key_len)
80{
81 int ret = 0, hlen;
82 size_t counter, out_len, len = derived_key_len;
83 unsigned char c[4];
84 unsigned char mac[EVP_MAX_MD_SIZE];
85 unsigned char *out = derived_key;
86 EVP_MD_CTX *ctx = NULL, *ctx_init = NULL;
87
88 if (z_len > SSKDF_MAX_INLEN || info_len > SSKDF_MAX_INLEN
89 || derived_key_len > SSKDF_MAX_INLEN
90 || derived_key_len == 0)
91 return 0;
92
93 hlen = EVP_MD_size(kdf_md);
94 if (hlen <= 0)
95 return 0;
96 out_len = (size_t)hlen;
97
98 ctx = EVP_MD_CTX_create();
99 ctx_init = EVP_MD_CTX_create();
100 if (ctx == NULL || ctx_init == NULL)
101 goto end;
102
103 if (!EVP_DigestInit(ctx_init, kdf_md))
104 goto end;
105
106 for (counter = 1;; counter++) {
107 c[0] = (unsigned char)((counter >> 24) & 0xff);
108 c[1] = (unsigned char)((counter >> 16) & 0xff);
109 c[2] = (unsigned char)((counter >> 8) & 0xff);
110 c[3] = (unsigned char)(counter & 0xff);
111
112 if (!(EVP_MD_CTX_copy_ex(ctx, ctx_init)
8bbeaaa4 113 && (append_ctr || EVP_DigestUpdate(ctx, c, sizeof(c)))
9537fe57 114 && EVP_DigestUpdate(ctx, z, z_len)
8bbeaaa4 115 && (!append_ctr || EVP_DigestUpdate(ctx, c, sizeof(c)))
9537fe57
SL
116 && EVP_DigestUpdate(ctx, info, info_len)))
117 goto end;
118 if (len >= out_len) {
119 if (!EVP_DigestFinal_ex(ctx, out, NULL))
120 goto end;
121 out += out_len;
122 len -= out_len;
123 if (len == 0)
124 break;
125 } else {
126 if (!EVP_DigestFinal_ex(ctx, mac, NULL))
127 goto end;
128 memcpy(out, mac, len);
129 break;
130 }
131 }
132 ret = 1;
133end:
134 EVP_MD_CTX_destroy(ctx);
135 EVP_MD_CTX_destroy(ctx_init);
136 OPENSSL_cleanse(mac, sizeof(mac));
137 return ret;
138}
139
140static int kmac_init(EVP_MAC_CTX *ctx, const unsigned char *custom,
141 size_t custom_len, size_t kmac_out_len,
142 size_t derived_key_len, unsigned char **out)
143{
144 /* Only KMAC has custom data - so return if not KMAC */
145 if (custom == NULL)
146 return 1;
147
17838470 148 if (EVP_MAC_ctrl(ctx, EVP_MAC_CTRL_SET_CUSTOM, custom, custom_len) <= 0)
9537fe57
SL
149 return 0;
150
151 /* By default only do one iteration if kmac_out_len is not specified */
152 if (kmac_out_len == 0)
153 kmac_out_len = derived_key_len;
154 /* otherwise check the size is valid */
155 else if (!(kmac_out_len == derived_key_len
156 || kmac_out_len == 20
157 || kmac_out_len == 28
158 || kmac_out_len == 32
159 || kmac_out_len == 48
160 || kmac_out_len == 64))
161 return 0;
162
17838470 163 if (EVP_MAC_ctrl(ctx, EVP_MAC_CTRL_SET_SIZE, kmac_out_len) <= 0)
9537fe57
SL
164 return 0;
165
166 /*
167 * For kmac the output buffer can be larger than EVP_MAX_MD_SIZE: so
168 * alloc a buffer for this case.
169 */
170 if (kmac_out_len > EVP_MAX_MD_SIZE) {
171 *out = OPENSSL_zalloc(kmac_out_len);
172 if (*out == NULL)
173 return 0;
174 }
175 return 1;
176}
177
178/*
179 * Refer to https://csrc.nist.gov/publications/detail/sp/800-56c/rev-1/final
180 * Section 4. One-Step Key Derivation using MAC: i.e either
181 * H(x) = HMAC-hash(salt, x) OR
182 * H(x) = KMAC#(salt, x, outbits, CustomString='KDF')
183 */
184static int SSKDF_mac_kdm(const EVP_MAC *kdf_mac, const EVP_MD *hmac_md,
185 const unsigned char *kmac_custom,
186 size_t kmac_custom_len, size_t kmac_out_len,
187 const unsigned char *salt, size_t salt_len,
188 const unsigned char *z, size_t z_len,
189 const unsigned char *info, size_t info_len,
190 unsigned char *derived_key, size_t derived_key_len)
191{
192 int ret = 0;
193 size_t counter, out_len, len;
194 unsigned char c[4];
195 unsigned char mac_buf[EVP_MAX_MD_SIZE];
196 unsigned char *out = derived_key;
197 EVP_MAC_CTX *ctx = NULL, *ctx_init = NULL;
198 unsigned char *mac = mac_buf, *kmac_buffer = NULL;
199
200 if (z_len > SSKDF_MAX_INLEN || info_len > SSKDF_MAX_INLEN
201 || derived_key_len > SSKDF_MAX_INLEN
202 || derived_key_len == 0)
203 return 0;
204
9537fe57 205 ctx_init = EVP_MAC_CTX_new(kdf_mac);
be5fc053 206 if (ctx_init == NULL)
9537fe57
SL
207 goto end;
208 if (hmac_md != NULL &&
17838470 209 EVP_MAC_ctrl(ctx_init, EVP_MAC_CTRL_SET_MD, hmac_md) <= 0)
9537fe57
SL
210 goto end;
211
17838470 212 if (EVP_MAC_ctrl(ctx_init, EVP_MAC_CTRL_SET_KEY, salt, salt_len) <= 0)
9537fe57
SL
213 goto end;
214
215 if (!kmac_init(ctx_init, kmac_custom, kmac_custom_len, kmac_out_len,
216 derived_key_len, &kmac_buffer))
217 goto end;
218 if (kmac_buffer != NULL)
219 mac = kmac_buffer;
220
221 if (!EVP_MAC_init(ctx_init))
222 goto end;
223
224 out_len = EVP_MAC_size(ctx_init); /* output size */
225 if (out_len <= 0)
226 goto end;
227 len = derived_key_len;
228
229 for (counter = 1;; counter++) {
230 c[0] = (unsigned char)((counter >> 24) & 0xff);
231 c[1] = (unsigned char)((counter >> 16) & 0xff);
232 c[2] = (unsigned char)((counter >> 8) & 0xff);
233 c[3] = (unsigned char)(counter & 0xff);
234
be5fc053
KR
235 ctx = EVP_MAC_CTX_dup(ctx_init);
236 if (!(ctx != NULL
9537fe57
SL
237 && EVP_MAC_update(ctx, c, sizeof(c))
238 && EVP_MAC_update(ctx, z, z_len)
239 && EVP_MAC_update(ctx, info, info_len)))
240 goto end;
241 if (len >= out_len) {
242 if (!EVP_MAC_final(ctx, out, NULL))
243 goto end;
244 out += out_len;
245 len -= out_len;
246 if (len == 0)
247 break;
248 } else {
249 if (!EVP_MAC_final(ctx, mac, NULL))
250 goto end;
251 memcpy(out, mac, len);
252 break;
253 }
be5fc053
KR
254 EVP_MAC_CTX_free(ctx);
255 ctx = NULL;
9537fe57
SL
256 }
257 ret = 1;
258end:
a3c62426
SL
259 if (kmac_buffer != NULL)
260 OPENSSL_clear_free(kmac_buffer, kmac_out_len);
261 else
262 OPENSSL_cleanse(mac_buf, sizeof(mac_buf));
263
9537fe57
SL
264 EVP_MAC_CTX_free(ctx);
265 EVP_MAC_CTX_free(ctx_init);
9537fe57
SL
266 return ret;
267}
268
269static EVP_KDF_IMPL *sskdf_new(void)
270{
271 EVP_KDF_IMPL *impl;
272
273 if ((impl = OPENSSL_zalloc(sizeof(*impl))) == NULL)
274 KDFerr(KDF_F_SSKDF_NEW, ERR_R_MALLOC_FAILURE);
275 return impl;
276}
277
278static void sskdf_reset(EVP_KDF_IMPL *impl)
279{
280 OPENSSL_clear_free(impl->secret, impl->secret_len);
281 OPENSSL_clear_free(impl->info, impl->info_len);
282 OPENSSL_clear_free(impl->salt, impl->salt_len);
283 memset(impl, 0, sizeof(*impl));
284}
285
286static void sskdf_free(EVP_KDF_IMPL *impl)
287{
288 sskdf_reset(impl);
289 OPENSSL_free(impl);
290}
291
292static int sskdf_set_buffer(va_list args, unsigned char **out, size_t *out_len)
293{
294 const unsigned char *p;
295 size_t len;
296
297 p = va_arg(args, const unsigned char *);
298 len = va_arg(args, size_t);
299 if (len == 0 || p == NULL)
300 return 1;
301
302 OPENSSL_free(*out);
303 *out = OPENSSL_memdup(p, len);
304 if (*out == NULL)
305 return 0;
306
307 *out_len = len;
308 return 1;
309}
310
311static int sskdf_ctrl(EVP_KDF_IMPL *impl, int cmd, va_list args)
312{
313 const EVP_MD *md;
314 const EVP_MAC *mac;
315
316 switch (cmd) {
317 case EVP_KDF_CTRL_SET_KEY:
318 return sskdf_set_buffer(args, &impl->secret, &impl->secret_len);
319
320 case EVP_KDF_CTRL_SET_SSKDF_INFO:
321 return sskdf_set_buffer(args, &impl->info, &impl->info_len);
322
323 case EVP_KDF_CTRL_SET_MD:
324 md = va_arg(args, const EVP_MD *);
325 if (md == NULL)
326 return 0;
327
328 impl->md = md;
329 return 1;
330
331 case EVP_KDF_CTRL_SET_MAC:
332 mac = va_arg(args, const EVP_MAC *);
333 if (mac == NULL)
334 return 0;
335
336 impl->mac = mac;
337 return 1;
338
339 case EVP_KDF_CTRL_SET_SALT:
340 return sskdf_set_buffer(args, &impl->salt, &impl->salt_len);
341
342 case EVP_KDF_CTRL_SET_MAC_SIZE:
343 impl->out_len = va_arg(args, size_t);
344 return 1;
345
346 default:
347 return -2;
348 }
349}
350
351/* Pass a mac to a ctrl */
352static int sskdf_mac2ctrl(EVP_KDF_IMPL *impl,
353 int (*ctrl)(EVP_KDF_IMPL *impl, int cmd, va_list args),
354 int cmd, const char *mac_name)
355{
356 const EVP_MAC *mac;
357
358 if (mac_name == NULL || (mac = EVP_get_macbyname(mac_name)) == NULL) {
359 KDFerr(KDF_F_SSKDF_MAC2CTRL, KDF_R_INVALID_MAC_TYPE);
360 return 0;
361 }
362 return call_ctrl(ctrl, impl, cmd, mac);
363}
364
365static int sskdf_ctrl_str(EVP_KDF_IMPL *impl, const char *type,
366 const char *value)
367{
368 if (strcmp(type, "secret") == 0 || strcmp(type, "key") == 0)
369 return kdf_str2ctrl(impl, sskdf_ctrl, EVP_KDF_CTRL_SET_KEY,
370 value);
371
372 if (strcmp(type, "hexsecret") == 0 || strcmp(type, "hexkey") == 0)
373 return kdf_hex2ctrl(impl, sskdf_ctrl, EVP_KDF_CTRL_SET_KEY,
374 value);
375
376 if (strcmp(type, "info") == 0)
377 return kdf_str2ctrl(impl, sskdf_ctrl, EVP_KDF_CTRL_SET_SSKDF_INFO,
378 value);
379
380 if (strcmp(type, "hexinfo") == 0)
381 return kdf_hex2ctrl(impl, sskdf_ctrl, EVP_KDF_CTRL_SET_SSKDF_INFO,
382 value);
383
384 if (strcmp(type, "digest") == 0)
385 return kdf_md2ctrl(impl, sskdf_ctrl, EVP_KDF_CTRL_SET_MD, value);
386
387 if (strcmp(type, "mac") == 0)
388 return sskdf_mac2ctrl(impl, sskdf_ctrl, EVP_KDF_CTRL_SET_MAC, value);
389
390 if (strcmp(type, "salt") == 0)
391 return kdf_str2ctrl(impl, sskdf_ctrl, EVP_KDF_CTRL_SET_SALT, value);
392
393 if (strcmp(type, "hexsalt") == 0)
394 return kdf_hex2ctrl(impl, sskdf_ctrl, EVP_KDF_CTRL_SET_SALT, value);
395
396
397 if (strcmp(type, "maclen") == 0) {
398 int val = atoi(value);
399 if (val < 0) {
400 KDFerr(KDF_F_SSKDF_CTRL_STR, KDF_R_VALUE_ERROR);
401 return 0;
402 }
403 return call_ctrl(sskdf_ctrl, impl, EVP_KDF_CTRL_SET_MAC_SIZE,
404 (size_t)val);
405 }
406 return -2;
407}
408
409static size_t sskdf_size(EVP_KDF_IMPL *impl)
410{
411 int len;
412
413 if (impl->md == NULL) {
414 KDFerr(KDF_F_SSKDF_SIZE, KDF_R_MISSING_MESSAGE_DIGEST);
415 return 0;
416 }
417 len = EVP_MD_size(impl->md);
418 return (len <= 0) ? 0 : (size_t)len;
419}
420
421static int sskdf_derive(EVP_KDF_IMPL *impl, unsigned char *key, size_t keylen)
422{
423 if (impl->secret == NULL) {
424 KDFerr(KDF_F_SSKDF_DERIVE, KDF_R_MISSING_SECRET);
425 return 0;
426 }
427
428 if (impl->mac != NULL) {
429 /* H(x) = KMAC or H(x) = HMAC */
430 int ret;
431 const unsigned char *custom = NULL;
432 size_t custom_len = 0;
433 int nid;
434 int default_salt_len;
435
436 nid = EVP_MAC_nid(impl->mac);
437 if (nid == EVP_MAC_HMAC) {
438 /* H(x) = HMAC(x, salt, hash) */
439 if (impl->md == NULL) {
440 KDFerr(KDF_F_SSKDF_DERIVE, KDF_R_MISSING_MESSAGE_DIGEST);
441 return 0;
442 }
443 default_salt_len = EVP_MD_block_size(impl->md);
444 if (default_salt_len <= 0)
445 return 0;
446 } else if (nid == EVP_MAC_KMAC128 || nid == EVP_MAC_KMAC256) {
447 /* H(x) = KMACzzz(x, salt, custom) */
448 custom = kmac_custom_str;
449 custom_len = sizeof(kmac_custom_str);
450 if (nid == EVP_MAC_KMAC128)
451 default_salt_len = SSKDF_KMAC128_DEFAULT_SALT_SIZE;
452 else
453 default_salt_len = SSKDF_KMAC256_DEFAULT_SALT_SIZE;
454 } else {
455 KDFerr(KDF_F_SSKDF_DERIVE, KDF_R_UNSUPPORTED_MAC_TYPE);
456 return 0;
457 }
458 /* If no salt is set then use a default_salt of zeros */
459 if (impl->salt == NULL || impl->salt_len <= 0) {
460 impl->salt = OPENSSL_zalloc(default_salt_len);
461 if (impl->salt == NULL) {
462 KDFerr(KDF_F_SSKDF_DERIVE, ERR_R_MALLOC_FAILURE);
463 return 0;
464 }
465 impl->salt_len = default_salt_len;
466 }
467 ret = SSKDF_mac_kdm(impl->mac, impl->md,
468 custom, custom_len, impl->out_len,
469 impl->salt, impl->salt_len,
470 impl->secret, impl->secret_len,
471 impl->info, impl->info_len, key, keylen);
472 return ret;
473 } else {
474 /* H(x) = hash */
475 if (impl->md == NULL) {
476 KDFerr(KDF_F_SSKDF_DERIVE, KDF_R_MISSING_MESSAGE_DIGEST);
477 return 0;
478 }
479 return SSKDF_hash_kdm(impl->md, impl->secret, impl->secret_len,
8bbeaaa4
SL
480 impl->info, impl->info_len, 0, key, keylen);
481 }
482}
483
484static int x963kdf_derive(EVP_KDF_IMPL *impl, unsigned char *key, size_t keylen)
485{
486 if (impl->secret == NULL) {
487 KDFerr(KDF_F_X963KDF_DERIVE, KDF_R_MISSING_SECRET);
488 return 0;
489 }
490
491 if (impl->mac != NULL) {
492 KDFerr(KDF_F_X963KDF_DERIVE, KDF_R_NOT_SUPPORTED);
493 return 0;
494 } else {
495 /* H(x) = hash */
496 if (impl->md == NULL) {
497 KDFerr(KDF_F_X963KDF_DERIVE, KDF_R_MISSING_MESSAGE_DIGEST);
498 return 0;
499 }
500 return SSKDF_hash_kdm(impl->md, impl->secret, impl->secret_len,
501 impl->info, impl->info_len, 1, key, keylen);
9537fe57
SL
502 }
503}
504
d2ba8123 505const EVP_KDF ss_kdf_meth = {
9537fe57
SL
506 EVP_KDF_SS,
507 sskdf_new,
508 sskdf_free,
509 sskdf_reset,
510 sskdf_ctrl,
511 sskdf_ctrl_str,
512 sskdf_size,
513 sskdf_derive
514};
8bbeaaa4
SL
515
516const EVP_KDF x963_kdf_meth = {
517 EVP_KDF_X963,
518 sskdf_new,
519 sskdf_free,
520 sskdf_reset,
521 sskdf_ctrl,
522 sskdf_ctrl_str,
523 sskdf_size,
524 x963kdf_derive
525};