1/*
2 * Copyright 2016-2019 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#include <stdlib.h>
11#include <stdarg.h>
12#include <string.h>
13#include <openssl/hmac.h>
14#include <openssl/evp.h>
15#include <openssl/kdf.h>
16#include <openssl/core_names.h>
17#include "internal/cryptlib.h"
18#include "internal/numbers.h"
19#include "crypto/evp.h"
20#include "prov/provider_ctx.h"
21#include "prov/providercommonerr.h"
22#include "prov/implementations.h"
23#include "prov/provider_util.h"
24#include "e_os.h"
25
26#define HKDF_MAXBUF 1024
27
28static OSSL_OP_kdf_newctx_fn kdf_hkdf_new;
29static OSSL_OP_kdf_freectx_fn kdf_hkdf_free;
30static OSSL_OP_kdf_reset_fn kdf_hkdf_reset;
31static OSSL_OP_kdf_derive_fn kdf_hkdf_derive;
32static OSSL_OP_kdf_settable_ctx_params_fn kdf_hkdf_settable_ctx_params;
33static OSSL_OP_kdf_set_ctx_params_fn kdf_hkdf_set_ctx_params;
34static OSSL_OP_kdf_gettable_ctx_params_fn kdf_hkdf_gettable_ctx_params;
35static OSSL_OP_kdf_get_ctx_params_fn kdf_hkdf_get_ctx_params;
36
37static int HKDF(const EVP_MD *evp_md,
38 const unsigned char *salt, size_t salt_len,
39 const unsigned char *key, size_t key_len,
40 const unsigned char *info, size_t info_len,
41 unsigned char *okm, size_t okm_len);
42static int HKDF_Extract(const EVP_MD *evp_md,
43 const unsigned char *salt, size_t salt_len,
44 const unsigned char *ikm, size_t ikm_len,
45 unsigned char *prk, size_t prk_len);
46static int HKDF_Expand(const EVP_MD *evp_md,
47 const unsigned char *prk, size_t prk_len,
48 const unsigned char *info, size_t info_len,
49 unsigned char *okm, size_t okm_len);
50
51typedef struct {
52 void *provctx;
53 int mode;
54 PROV_DIGEST digest;
55 unsigned char *salt;
56 size_t salt_len;
57 unsigned char *key;
58 size_t key_len;
59 unsigned char info[HKDF_MAXBUF];
60 size_t info_len;
61} KDF_HKDF;
62
63static void *kdf_hkdf_new(void *provctx)
64{
65 KDF_HKDF *ctx;
66
67 if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL)
68 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
69 else
70 ctx->provctx = provctx;
71 return ctx;
72}
73
74static void kdf_hkdf_free(void *vctx)
75{
76 KDF_HKDF *ctx = (KDF_HKDF *)vctx;
77
78 if (ctx != NULL) {
79 kdf_hkdf_reset(ctx);
80 OPENSSL_free(ctx);
81 }
82}
83
84static void kdf_hkdf_reset(void *vctx)
85{
86 KDF_HKDF *ctx = (KDF_HKDF *)vctx;
87
88 ossl_prov_digest_reset(&ctx->digest);
89 OPENSSL_free(ctx->salt);
90 OPENSSL_clear_free(ctx->key, ctx->key_len);
91 OPENSSL_cleanse(ctx->info, ctx->info_len);
92 memset(ctx, 0, sizeof(*ctx));
93}
94
95static size_t kdf_hkdf_size(KDF_HKDF *ctx)
96{
97 int sz;
98 const EVP_MD *md = ossl_prov_digest_md(&ctx->digest);
99
100 if (ctx->mode != EVP_KDF_HKDF_MODE_EXTRACT_ONLY)
101 return SIZE_MAX;
102
103 if (md == NULL) {
104 ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST);
105 return 0;
106 }
107 sz = EVP_MD_size(md);
108 if (sz < 0)
109 return 0;
110
111 return sz;
112}
113
114static int kdf_hkdf_derive(void *vctx, unsigned char *key, size_t keylen)
115{
116 KDF_HKDF *ctx = (KDF_HKDF *)vctx;
117 const EVP_MD *md = ossl_prov_digest_md(&ctx->digest);
118
119 if (md == NULL) {
120 ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST);
121 return 0;
122 }
123 if (ctx->key == NULL) {
124 ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_KEY);
125 return 0;
126 }
127
128 switch (ctx->mode) {
129 case EVP_KDF_HKDF_MODE_EXTRACT_AND_EXPAND:
130 return HKDF(md, ctx->salt, ctx->salt_len, ctx->key,
131 ctx->key_len, ctx->info, ctx->info_len, key,
132 keylen);
133
134 case EVP_KDF_HKDF_MODE_EXTRACT_ONLY:
135 return HKDF_Extract(md, ctx->salt, ctx->salt_len, ctx->key,
136 ctx->key_len, key, keylen);
137
138 case EVP_KDF_HKDF_MODE_EXPAND_ONLY:
139 return HKDF_Expand(md, ctx->key, ctx->key_len, ctx->info,
140 ctx->info_len, key, keylen);
141
142 default:
143 return 0;
144 }
145}
146
147static int kdf_hkdf_set_ctx_params(void *vctx, const OSSL_PARAM params[])
148{
149 const OSSL_PARAM *p;
150 KDF_HKDF *ctx = vctx;
151 OPENSSL_CTX *provctx = PROV_LIBRARY_CONTEXT_OF(ctx->provctx);
152 int n;
153
154 if (!ossl_prov_digest_load_from_params(&ctx->digest, params, provctx))
155 return 0;
156
157 if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_MODE)) != NULL) {
158 if (p->data_type == OSSL_PARAM_UTF8_STRING) {
159 if (strcasecmp(p->data, "EXTRACT_AND_EXPAND") == 0) {
160 ctx->mode = EVP_KDF_HKDF_MODE_EXTRACT_AND_EXPAND;
161 } else if (strcasecmp(p->data, "EXTRACT_ONLY") == 0) {
162 ctx->mode = EVP_KDF_HKDF_MODE_EXTRACT_ONLY;
163 } else if (strcasecmp(p->data, "EXPAND_ONLY") == 0) {
164 ctx->mode = EVP_KDF_HKDF_MODE_EXPAND_ONLY;
165 } else {
166 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_MODE);
167 return 0;
168 }
169 } else if (OSSL_PARAM_get_int(p, &n)) {
170 if (n != EVP_KDF_HKDF_MODE_EXTRACT_AND_EXPAND
171 && n != EVP_KDF_HKDF_MODE_EXTRACT_ONLY
172 && n != EVP_KDF_HKDF_MODE_EXPAND_ONLY) {
173 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_MODE);
174 return 0;
175 }
176 ctx->mode = n;
177 } else {
178 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_MODE);
179 return 0;
180 }
181 }
182
183 if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_KEY)) != NULL) {
184 OPENSSL_clear_free(ctx->key, ctx->key_len);
185 ctx->key = NULL;
186 if (!OSSL_PARAM_get_octet_string(p, (void **)&ctx->key, 0,
187 &ctx->key_len))
188 return 0;
189 }
190
191 if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SALT)) != NULL) {
192 if (p->data_size != 0 && p->data != NULL) {
193 OPENSSL_free(ctx->salt);
194 ctx->salt = NULL;
195 if (!OSSL_PARAM_get_octet_string(p, (void **)&ctx->salt, 0,
196 &ctx->salt_len))
197 return 0;
198 }
199 }
200 /* The info fields concatenate, so process them all */
201 if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_INFO)) != NULL) {
202 ctx->info_len = 0;
203 for (; p != NULL; p = OSSL_PARAM_locate_const(p + 1,
204 OSSL_KDF_PARAM_INFO)) {
205 const void *q = ctx->info + ctx->info_len;
206 size_t sz = 0;
207
208 if (p->data_size != 0
209 && p->data != NULL
210 && !OSSL_PARAM_get_octet_string(p, (void **)&q,
211 HKDF_MAXBUF - ctx->info_len,
212 &sz))
213 return 0;
214 ctx->info_len += sz;
215 }
216 }
217 return 1;
218}
219
220static const OSSL_PARAM *kdf_hkdf_settable_ctx_params(void)
221{
222 static const OSSL_PARAM known_settable_ctx_params[] = {
223 OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_MODE, NULL, 0),
224 OSSL_PARAM_int(OSSL_KDF_PARAM_MODE, NULL),
225 OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0),
226 OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, NULL, 0),
227 OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SALT, NULL, 0),
228 OSSL_PARAM_octet_string(OSSL_KDF_PARAM_KEY, NULL, 0),
229 OSSL_PARAM_octet_string(OSSL_KDF_PARAM_INFO, NULL, 0),
230 OSSL_PARAM_END
231 };
232 return known_settable_ctx_params;
233}
234
235static int kdf_hkdf_get_ctx_params(void *vctx, OSSL_PARAM params[])
236{
237 KDF_HKDF *ctx = (KDF_HKDF *)vctx;
238 OSSL_PARAM *p;
239
240 if ((p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_SIZE)) != NULL)
241 return OSSL_PARAM_set_size_t(p, kdf_hkdf_size(ctx));
242 return -2;
243}
244
245static const OSSL_PARAM *kdf_hkdf_gettable_ctx_params(void)
246{
247 static const OSSL_PARAM known_gettable_ctx_params[] = {
248 OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL),
249 OSSL_PARAM_END
250 };
251 return known_gettable_ctx_params;
252}
253
254const OSSL_DISPATCH kdf_hkdf_functions[] = {
255 { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))kdf_hkdf_new },
256 { OSSL_FUNC_KDF_FREECTX, (void(*)(void))kdf_hkdf_free },
257 { OSSL_FUNC_KDF_RESET, (void(*)(void))kdf_hkdf_reset },
258 { OSSL_FUNC_KDF_DERIVE, (void(*)(void))kdf_hkdf_derive },
259 { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,
260 (void(*)(void))kdf_hkdf_settable_ctx_params },
261 { OSSL_FUNC_KDF_SET_CTX_PARAMS, (void(*)(void))kdf_hkdf_set_ctx_params },
262 { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,
263 (void(*)(void))kdf_hkdf_gettable_ctx_params },
264 { OSSL_FUNC_KDF_GET_CTX_PARAMS, (void(*)(void))kdf_hkdf_get_ctx_params },
265 { 0, NULL }
266};
267
268/*
269 * Refer to "HMAC-based Extract-and-Expand Key Derivation Function (HKDF)"
270 * Section 2 (https://tools.ietf.org/html/rfc5869#section-2) and
271 * "Cryptographic Extraction and Key Derivation: The HKDF Scheme"
272 * Section 4.2 (https://eprint.iacr.org/2010/264.pdf).
273 *
274 * From the paper:
275 * The scheme HKDF is specified as:
276 * HKDF(XTS, SKM, CTXinfo, L) = K(1) | K(2) | ... | K(t)
277 *
278 * where:
279 * SKM is source key material
280 * XTS is extractor salt (which may be null or constant)
281 * CTXinfo is context information (may be null)
282 * L is the number of key bits to be produced by KDF
283 * k is the output length in bits of the hash function used with HMAC
284 * t = ceil(L/k)
285 * the value K(t) is truncated to its first d = L mod k bits.
286 *
287 * From RFC 5869:
288 * 2.2. Step 1: Extract
289 * HKDF-Extract(salt, IKM) -> PRK
290 * 2.3. Step 2: Expand
291 * HKDF-Expand(PRK, info, L) -> OKM
292 */
293static int HKDF(const EVP_MD *evp_md,
294 const unsigned char *salt, size_t salt_len,
295 const unsigned char *ikm, size_t ikm_len,
296 const unsigned char *info, size_t info_len,
297 unsigned char *okm, size_t okm_len)
298{
299 unsigned char prk[EVP_MAX_MD_SIZE];
300 int ret, sz;
301 size_t prk_len;
302
303 sz = EVP_MD_size(evp_md);
304 if (sz < 0)
305 return 0;
306 prk_len = (size_t)sz;
307
308 /* Step 1: HKDF-Extract(salt, IKM) -> PRK */
309 if (!HKDF_Extract(evp_md, salt, salt_len, ikm, ikm_len, prk, prk_len))
310 return 0;
311
312 /* Step 2: HKDF-Expand(PRK, info, L) -> OKM */
313 ret = HKDF_Expand(evp_md, prk, prk_len, info, info_len, okm, okm_len);
314 OPENSSL_cleanse(prk, sizeof(prk));
315
316 return ret;
317}
318
319/*
320 * Refer to "HMAC-based Extract-and-Expand Key Derivation Function (HKDF)"
321 * Section 2.2 (https://tools.ietf.org/html/rfc5869#section-2.2).
322 *
323 * 2.2. Step 1: Extract
324 *
325 * HKDF-Extract(salt, IKM) -> PRK
326 *
327 * Options:
328 * Hash a hash function; HashLen denotes the length of the
329 * hash function output in octets
330 *
331 * Inputs:
332 * salt optional salt value (a non-secret random value);
333 * if not provided, it is set to a string of HashLen zeros.
334 * IKM input keying material
335 *
336 * Output:
337 * PRK a pseudorandom key (of HashLen octets)
338 *
339 * The output PRK is calculated as follows:
340 *
341 * PRK = HMAC-Hash(salt, IKM)
342 */
343static int HKDF_Extract(const EVP_MD *evp_md,
344 const unsigned char *salt, size_t salt_len,
345 const unsigned char *ikm, size_t ikm_len,
346 unsigned char *prk, size_t prk_len)
347{
348 int sz = EVP_MD_size(evp_md);
349
350 if (sz < 0)
351 return 0;
352 if (prk_len != (size_t)sz) {
353 ERR_raise(ERR_LIB_PROV, PROV_R_WRONG_OUTPUT_BUFFER_SIZE);
354 return 0;
355 }
356 /* calc: PRK = HMAC-Hash(salt, IKM) */
357 return HMAC(evp_md, salt, salt_len, ikm, ikm_len, prk, NULL) != NULL;
358}
359
360/*
361 * Refer to "HMAC-based Extract-and-Expand Key Derivation Function (HKDF)"
362 * Section 2.3 (https://tools.ietf.org/html/rfc5869#section-2.3).
363 *
364 * 2.3. Step 2: Expand
365 *
366 * HKDF-Expand(PRK, info, L) -> OKM
367 *
368 * Options:
369 * Hash a hash function; HashLen denotes the length of the
370 * hash function output in octets
371 *
372 * Inputs:
373 * PRK a pseudorandom key of at least HashLen octets
374 * (usually, the output from the extract step)
375 * info optional context and application specific information
376 * (can be a zero-length string)
377 * L length of output keying material in octets
378 * (<= 255*HashLen)
379 *
380 * Output:
381 * OKM output keying material (of L octets)
382 *
383 * The output OKM is calculated as follows:
384 *
385 * N = ceil(L/HashLen)
386 * T = T(1) | T(2) | T(3) | ... | T(N)
387 * OKM = first L octets of T
388 *
389 * where:
390 * T(0) = empty string (zero length)
391 * T(1) = HMAC-Hash(PRK, T(0) | info | 0x01)
392 * T(2) = HMAC-Hash(PRK, T(1) | info | 0x02)
393 * T(3) = HMAC-Hash(PRK, T(2) | info | 0x03)
394 * ...
395 *
396 * (where the constant concatenated to the end of each T(n) is a
397 * single octet.)
398 */
399static int HKDF_Expand(const EVP_MD *evp_md,
400 const unsigned char *prk, size_t prk_len,
401 const unsigned char *info, size_t info_len,
402 unsigned char *okm, size_t okm_len)
403{
404 HMAC_CTX *hmac;
405 int ret = 0, sz;
406 unsigned int i;
407 unsigned char prev[EVP_MAX_MD_SIZE];
408 size_t done_len = 0, dig_len, n;
409
410 sz = EVP_MD_size(evp_md);
411 if (sz <= 0)
412 return 0;
413 dig_len = (size_t)sz;
414
415 /* calc: N = ceil(L/HashLen) */
416 n = okm_len / dig_len;
417 if (okm_len % dig_len)
418 n++;
419
420 if (n > 255 || okm == NULL)
421 return 0;
422
423 if ((hmac = HMAC_CTX_new()) == NULL)
424 return 0;
425
426 if (!HMAC_Init_ex(hmac, prk, prk_len, evp_md, NULL))
427 goto err;
428
429 for (i = 1; i <= n; i++) {
430 size_t copy_len;
431 const unsigned char ctr = i;
432
433 /* calc: T(i) = HMAC-Hash(PRK, T(i - 1) | info | i) */
434 if (i > 1) {
435 if (!HMAC_Init_ex(hmac, NULL, 0, NULL, NULL))
436 goto err;
437
438 if (!HMAC_Update(hmac, prev, dig_len))
439 goto err;
440 }
441
442 if (!HMAC_Update(hmac, info, info_len))
443 goto err;
444
445 if (!HMAC_Update(hmac, &ctr, 1))
446 goto err;
447
448 if (!HMAC_Final(hmac, prev, NULL))
449 goto err;
450
451 copy_len = (done_len + dig_len > okm_len) ?
452 okm_len - done_len :
453 dig_len;
454
455 memcpy(okm + done_len, prev, copy_len);
456
457 done_len += copy_len;
458 }
459 ret = 1;
460
461 err:
462 OPENSSL_cleanse(prev, sizeof(prev));
463 HMAC_CTX_free(hmac);
464 return ret;
465}
466