]> git.ipfire.org Git - thirdparty/openssl.git/blob - providers/implementations/kdfs/kbkdf.c
Stop raising ERR_R_MALLOC_FAILURE in most places
[thirdparty/openssl.git] / providers / implementations / kdfs / kbkdf.c
1 /*
2 * Copyright 2019-2022 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright 2019 Red Hat, Inc.
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 * This implements https://csrc.nist.gov/publications/detail/sp/800-108/final
13 * section 5.1 ("counter mode") and section 5.2 ("feedback mode") in both HMAC
14 * and CMAC. That document does not name the KDFs it defines; the name is
15 * derived from
16 * https://csrc.nist.gov/Projects/Cryptographic-Algorithm-Validation-Program/Key-Derivation
17 *
18 * Note that section 5.3 ("double-pipeline mode") is not implemented, though
19 * it would be possible to do so in the future.
20 *
21 * These versions all assume the counter is used. It would be relatively
22 * straightforward to expose a configuration handle should the need arise.
23 *
24 * Variable names attempt to match those of SP800-108.
25 */
26
27 #include <stdarg.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 #include <openssl/core_names.h>
32 #include <openssl/evp.h>
33 #include <openssl/hmac.h>
34 #include <openssl/kdf.h>
35 #include <openssl/params.h>
36 #include <openssl/proverr.h>
37
38 #include "internal/cryptlib.h"
39 #include "crypto/evp.h"
40 #include "internal/numbers.h"
41 #include "internal/endian.h"
42 #include "prov/implementations.h"
43 #include "prov/provider_ctx.h"
44 #include "prov/provider_util.h"
45 #include "prov/providercommon.h"
46
47 #include "internal/e_os.h"
48
49 #define ossl_min(a, b) ((a) < (b)) ? (a) : (b)
50
51 typedef enum {
52 COUNTER = 0,
53 FEEDBACK
54 } kbkdf_mode;
55
56 /* Our context structure. */
57 typedef struct {
58 void *provctx;
59 kbkdf_mode mode;
60 EVP_MAC_CTX *ctx_init;
61
62 /* Names are lowercased versions of those found in SP800-108. */
63 int r;
64 unsigned char *ki;
65 size_t ki_len;
66 unsigned char *label;
67 size_t label_len;
68 unsigned char *context;
69 size_t context_len;
70 unsigned char *iv;
71 size_t iv_len;
72 int use_l;
73 int use_separator;
74 } KBKDF;
75
76 /* Definitions needed for typechecking. */
77 static OSSL_FUNC_kdf_newctx_fn kbkdf_new;
78 static OSSL_FUNC_kdf_newctx_fn kbkdf_dup;
79 static OSSL_FUNC_kdf_freectx_fn kbkdf_free;
80 static OSSL_FUNC_kdf_reset_fn kbkdf_reset;
81 static OSSL_FUNC_kdf_derive_fn kbkdf_derive;
82 static OSSL_FUNC_kdf_settable_ctx_params_fn kbkdf_settable_ctx_params;
83 static OSSL_FUNC_kdf_set_ctx_params_fn kbkdf_set_ctx_params;
84 static OSSL_FUNC_kdf_gettable_ctx_params_fn kbkdf_gettable_ctx_params;
85 static OSSL_FUNC_kdf_get_ctx_params_fn kbkdf_get_ctx_params;
86
87 /* Not all platforms have htobe32(). */
88 static uint32_t be32(uint32_t host)
89 {
90 uint32_t big = 0;
91 DECLARE_IS_ENDIAN;
92
93 if (!IS_LITTLE_ENDIAN)
94 return host;
95
96 big |= (host & 0xff000000) >> 24;
97 big |= (host & 0x00ff0000) >> 8;
98 big |= (host & 0x0000ff00) << 8;
99 big |= (host & 0x000000ff) << 24;
100 return big;
101 }
102
103 static void init(KBKDF *ctx)
104 {
105 ctx->r = 32;
106 ctx->use_l = 1;
107 ctx->use_separator = 1;
108 }
109
110 static void *kbkdf_new(void *provctx)
111 {
112 KBKDF *ctx;
113
114 if (!ossl_prov_is_running())
115 return NULL;
116
117 ctx = OPENSSL_zalloc(sizeof(*ctx));
118 if (ctx == NULL)
119 return NULL;
120
121 ctx->provctx = provctx;
122 init(ctx);
123 return ctx;
124 }
125
126 static void kbkdf_free(void *vctx)
127 {
128 KBKDF *ctx = (KBKDF *)vctx;
129
130 if (ctx != NULL) {
131 kbkdf_reset(ctx);
132 OPENSSL_free(ctx);
133 }
134 }
135
136 static void kbkdf_reset(void *vctx)
137 {
138 KBKDF *ctx = (KBKDF *)vctx;
139 void *provctx = ctx->provctx;
140
141 EVP_MAC_CTX_free(ctx->ctx_init);
142 OPENSSL_clear_free(ctx->context, ctx->context_len);
143 OPENSSL_clear_free(ctx->label, ctx->label_len);
144 OPENSSL_clear_free(ctx->ki, ctx->ki_len);
145 OPENSSL_clear_free(ctx->iv, ctx->iv_len);
146 memset(ctx, 0, sizeof(*ctx));
147 ctx->provctx = provctx;
148 init(ctx);
149 }
150
151 static void *kbkdf_dup(void *vctx)
152 {
153 const KBKDF *src = (const KBKDF *)vctx;
154 KBKDF *dest;
155
156 dest = kbkdf_new(src->provctx);
157 if (dest != NULL) {
158 dest->ctx_init = EVP_MAC_CTX_dup(src->ctx_init);
159 if (dest->ctx_init == NULL
160 || !ossl_prov_memdup(src->ki, src->ki_len,
161 &dest->ki, &dest->ki_len)
162 || !ossl_prov_memdup(src->label, src->label_len,
163 &dest->label, &dest->label_len)
164 || !ossl_prov_memdup(src->context, src->context_len,
165 &dest->context, &dest->context_len)
166 || !ossl_prov_memdup(src->iv, src->iv_len,
167 &dest->iv, &dest->iv_len))
168 goto err;
169 dest->mode = src->mode;
170 dest->r = src->r;
171 dest->use_l = src->use_l;
172 dest->use_separator = src->use_separator;
173 }
174 return dest;
175
176 err:
177 kbkdf_free(dest);
178 return NULL;
179 }
180
181 /* SP800-108 section 5.1 or section 5.2 depending on mode. */
182 static int derive(EVP_MAC_CTX *ctx_init, kbkdf_mode mode, unsigned char *iv,
183 size_t iv_len, unsigned char *label, size_t label_len,
184 unsigned char *context, size_t context_len,
185 unsigned char *k_i, size_t h, uint32_t l, int has_separator,
186 unsigned char *ko, size_t ko_len, int r)
187 {
188 int ret = 0;
189 EVP_MAC_CTX *ctx = NULL;
190 size_t written = 0, to_write, k_i_len = iv_len;
191 const unsigned char zero = 0;
192 uint32_t counter, i;
193 /*
194 * From SP800-108:
195 * The fixed input data is a concatenation of a Label,
196 * a separation indicator 0x00, the Context, and L.
197 * One or more of these fixed input data fields may be omitted.
198 *
199 * has_separator == 0 means that the separator is omitted.
200 * Passing a value of l == 0 means that L is omitted.
201 * The Context and L are omitted automatically if a NULL buffer is passed.
202 */
203 int has_l = (l != 0);
204
205 /* Setup K(0) for feedback mode. */
206 if (iv_len > 0)
207 memcpy(k_i, iv, iv_len);
208
209 for (counter = 1; written < ko_len; counter++) {
210 i = be32(counter);
211
212 ctx = EVP_MAC_CTX_dup(ctx_init);
213 if (ctx == NULL)
214 goto done;
215
216 /* Perform feedback, if appropriate. */
217 if (mode == FEEDBACK && !EVP_MAC_update(ctx, k_i, k_i_len))
218 goto done;
219
220 if (!EVP_MAC_update(ctx, 4 - (r / 8) + (unsigned char *)&i, r / 8)
221 || !EVP_MAC_update(ctx, label, label_len)
222 || (has_separator && !EVP_MAC_update(ctx, &zero, 1))
223 || !EVP_MAC_update(ctx, context, context_len)
224 || (has_l && !EVP_MAC_update(ctx, (unsigned char *)&l, 4))
225 || !EVP_MAC_final(ctx, k_i, NULL, h))
226 goto done;
227
228 to_write = ko_len - written;
229 memcpy(ko + written, k_i, ossl_min(to_write, h));
230 written += h;
231
232 k_i_len = h;
233 EVP_MAC_CTX_free(ctx);
234 ctx = NULL;
235 }
236
237 ret = 1;
238 done:
239 EVP_MAC_CTX_free(ctx);
240 return ret;
241 }
242
243 static int kbkdf_derive(void *vctx, unsigned char *key, size_t keylen,
244 const OSSL_PARAM params[])
245 {
246 KBKDF *ctx = (KBKDF *)vctx;
247 int ret = 0;
248 unsigned char *k_i = NULL;
249 uint32_t l = 0;
250 size_t h = 0;
251 uint64_t counter_max;
252
253 if (!ossl_prov_is_running() || !kbkdf_set_ctx_params(ctx, params))
254 return 0;
255
256 /* label, context, and iv are permitted to be empty. Check everything
257 * else. */
258 if (ctx->ctx_init == NULL) {
259 if (ctx->ki_len == 0 || ctx->ki == NULL) {
260 ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET);
261 return 0;
262 }
263 /* Could either be missing MAC or missing message digest or missing
264 * cipher - arbitrarily, I pick this one. */
265 ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MAC);
266 return 0;
267 }
268
269 /* Fail if the output length is zero */
270 if (keylen == 0) {
271 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
272 return 0;
273 }
274
275 h = EVP_MAC_CTX_get_mac_size(ctx->ctx_init);
276 if (h == 0)
277 goto done;
278 if (ctx->iv_len != 0 && ctx->iv_len != h) {
279 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_SEED_LENGTH);
280 goto done;
281 }
282
283 if (ctx->mode == COUNTER) {
284 /* Fail if keylen is too large for r */
285 counter_max = (uint64_t)1 << (uint64_t)ctx->r;
286 if ((uint64_t)(keylen / h) >= counter_max) {
287 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
288 goto done;
289 }
290 }
291
292 if (ctx->use_l != 0)
293 l = be32(keylen * 8);
294
295 k_i = OPENSSL_zalloc(h);
296 if (k_i == NULL)
297 goto done;
298
299 ret = derive(ctx->ctx_init, ctx->mode, ctx->iv, ctx->iv_len, ctx->label,
300 ctx->label_len, ctx->context, ctx->context_len, k_i, h, l,
301 ctx->use_separator, key, keylen, ctx->r);
302 done:
303 if (ret != 1)
304 OPENSSL_cleanse(key, keylen);
305 OPENSSL_clear_free(k_i, h);
306 return ret;
307 }
308
309 static int kbkdf_set_buffer(unsigned char **out, size_t *out_len,
310 const OSSL_PARAM *p)
311 {
312 if (p->data == NULL || p->data_size == 0)
313 return 1;
314
315 OPENSSL_clear_free(*out, *out_len);
316 *out = NULL;
317 return OSSL_PARAM_get_octet_string(p, (void **)out, 0, out_len);
318 }
319
320 static int kbkdf_set_ctx_params(void *vctx, const OSSL_PARAM params[])
321 {
322 KBKDF *ctx = (KBKDF *)vctx;
323 OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx);
324 const OSSL_PARAM *p;
325
326 if (params == NULL)
327 return 1;
328
329 if (!ossl_prov_macctx_load_from_params(&ctx->ctx_init, params, NULL,
330 NULL, NULL, libctx))
331 return 0;
332 else if (ctx->ctx_init != NULL
333 && !EVP_MAC_is_a(EVP_MAC_CTX_get0_mac(ctx->ctx_init),
334 OSSL_MAC_NAME_HMAC)
335 && !EVP_MAC_is_a(EVP_MAC_CTX_get0_mac(ctx->ctx_init),
336 OSSL_MAC_NAME_CMAC)) {
337 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_MAC);
338 return 0;
339 }
340
341 p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_MODE);
342 if (p != NULL
343 && OPENSSL_strncasecmp("counter", p->data, p->data_size) == 0) {
344 ctx->mode = COUNTER;
345 } else if (p != NULL
346 && OPENSSL_strncasecmp("feedback", p->data, p->data_size) == 0) {
347 ctx->mode = FEEDBACK;
348 } else if (p != NULL) {
349 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_MODE);
350 return 0;
351 }
352
353 p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_KEY);
354 if (p != NULL && !kbkdf_set_buffer(&ctx->ki, &ctx->ki_len, p))
355 return 0;
356
357 p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SALT);
358 if (p != NULL && !kbkdf_set_buffer(&ctx->label, &ctx->label_len, p))
359 return 0;
360
361 p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_INFO);
362 if (p != NULL && !kbkdf_set_buffer(&ctx->context, &ctx->context_len, p))
363 return 0;
364
365 p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SEED);
366 if (p != NULL && !kbkdf_set_buffer(&ctx->iv, &ctx->iv_len, p))
367 return 0;
368
369 p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_KBKDF_USE_L);
370 if (p != NULL && !OSSL_PARAM_get_int(p, &ctx->use_l))
371 return 0;
372
373 p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_KBKDF_R);
374 if (p != NULL) {
375 int new_r = 0;
376
377 if (!OSSL_PARAM_get_int(p, &new_r))
378 return 0;
379 if (new_r != 8 && new_r != 16 && new_r != 24 && new_r != 32)
380 return 0;
381 ctx->r = new_r;
382 }
383
384 p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_KBKDF_USE_SEPARATOR);
385 if (p != NULL && !OSSL_PARAM_get_int(p, &ctx->use_separator))
386 return 0;
387
388 /* Set up digest context, if we can. */
389 if (ctx->ctx_init != NULL && ctx->ki_len != 0
390 && !EVP_MAC_init(ctx->ctx_init, ctx->ki, ctx->ki_len, NULL))
391 return 0;
392 return 1;
393 }
394
395 static const OSSL_PARAM *kbkdf_settable_ctx_params(ossl_unused void *ctx,
396 ossl_unused void *provctx)
397 {
398 static const OSSL_PARAM known_settable_ctx_params[] = {
399 OSSL_PARAM_octet_string(OSSL_KDF_PARAM_INFO, NULL, 0),
400 OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SALT, NULL, 0),
401 OSSL_PARAM_octet_string(OSSL_KDF_PARAM_KEY, NULL, 0),
402 OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SEED, NULL, 0),
403 OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, NULL, 0),
404 OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_CIPHER, NULL, 0),
405 OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_MAC, NULL, 0),
406 OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_MODE, NULL, 0),
407 OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0),
408 OSSL_PARAM_int(OSSL_KDF_PARAM_KBKDF_USE_L, NULL),
409 OSSL_PARAM_int(OSSL_KDF_PARAM_KBKDF_USE_SEPARATOR, NULL),
410 OSSL_PARAM_int(OSSL_KDF_PARAM_KBKDF_R, NULL),
411 OSSL_PARAM_END,
412 };
413 return known_settable_ctx_params;
414 }
415
416 static int kbkdf_get_ctx_params(void *vctx, OSSL_PARAM params[])
417 {
418 OSSL_PARAM *p;
419
420 p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_SIZE);
421 if (p == NULL)
422 return -2;
423
424 /* KBKDF can produce results as large as you like. */
425 return OSSL_PARAM_set_size_t(p, SIZE_MAX);
426 }
427
428 static const OSSL_PARAM *kbkdf_gettable_ctx_params(ossl_unused void *ctx,
429 ossl_unused void *provctx)
430 {
431 static const OSSL_PARAM known_gettable_ctx_params[] =
432 { OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL), OSSL_PARAM_END };
433 return known_gettable_ctx_params;
434 }
435
436 const OSSL_DISPATCH ossl_kdf_kbkdf_functions[] = {
437 { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))kbkdf_new },
438 { OSSL_FUNC_KDF_DUPCTX, (void(*)(void))kbkdf_dup },
439 { OSSL_FUNC_KDF_FREECTX, (void(*)(void))kbkdf_free },
440 { OSSL_FUNC_KDF_RESET, (void(*)(void))kbkdf_reset },
441 { OSSL_FUNC_KDF_DERIVE, (void(*)(void))kbkdf_derive },
442 { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,
443 (void(*)(void))kbkdf_settable_ctx_params },
444 { OSSL_FUNC_KDF_SET_CTX_PARAMS, (void(*)(void))kbkdf_set_ctx_params },
445 { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,
446 (void(*)(void))kbkdf_gettable_ctx_params },
447 { OSSL_FUNC_KDF_GET_CTX_PARAMS, (void(*)(void))kbkdf_get_ctx_params },
448 { 0, NULL },
449 };