]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/kdf/hkdf.c
PBKDF2 updates to conform to SP800-132
[thirdparty/openssl.git] / crypto / kdf / hkdf.c
CommitLineData
d2e9e320 1/*
28428130 2 * Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved.
aacfb134 3 *
7bb803e8 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
d2e9e320
RS
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
aacfb134
AG
8 */
9
10#include <stdlib.h>
5a285add 11#include <stdarg.h>
aacfb134
AG
12#include <string.h>
13#include <openssl/hmac.h>
aacfb134 14#include <openssl/evp.h>
5a285add 15#include <openssl/kdf.h>
aacfb134 16#include "internal/cryptlib.h"
cee719c2 17#include "internal/numbers.h"
aacfb134 18#include "internal/evp_int.h"
5a285add 19#include "kdf_local.h"
aacfb134
AG
20
21#define HKDF_MAXBUF 1024
22
5a285add
DM
23static void kdf_hkdf_reset(EVP_KDF_IMPL *impl);
24static int HKDF(const EVP_MD *evp_md,
25 const unsigned char *salt, size_t salt_len,
26 const unsigned char *key, size_t key_len,
27 const unsigned char *info, size_t info_len,
28 unsigned char *okm, size_t okm_len);
29static int HKDF_Extract(const EVP_MD *evp_md,
30 const unsigned char *salt, size_t salt_len,
e7018588 31 const unsigned char *ikm, size_t ikm_len,
5a285add
DM
32 unsigned char *prk, size_t prk_len);
33static int HKDF_Expand(const EVP_MD *evp_md,
34 const unsigned char *prk, size_t prk_len,
35 const unsigned char *info, size_t info_len,
36 unsigned char *okm, size_t okm_len);
37
38struct evp_kdf_impl_st {
d2139cf8 39 int mode;
aacfb134
AG
40 const EVP_MD *md;
41 unsigned char *salt;
42 size_t salt_len;
43 unsigned char *key;
44 size_t key_len;
45 unsigned char info[HKDF_MAXBUF];
46 size_t info_len;
5a285add 47};
aacfb134 48
5a285add 49static EVP_KDF_IMPL *kdf_hkdf_new(void)
aacfb134 50{
5a285add 51 EVP_KDF_IMPL *impl;
aacfb134 52
5a285add
DM
53 if ((impl = OPENSSL_zalloc(sizeof(*impl))) == NULL)
54 KDFerr(KDF_F_KDF_HKDF_NEW, ERR_R_MALLOC_FAILURE);
55 return impl;
56}
aacfb134 57
5a285add
DM
58static void kdf_hkdf_free(EVP_KDF_IMPL *impl)
59{
60 kdf_hkdf_reset(impl);
61 OPENSSL_free(impl);
aacfb134
AG
62}
63
5a285add 64static void kdf_hkdf_reset(EVP_KDF_IMPL *impl)
aacfb134 65{
5a285add
DM
66 OPENSSL_free(impl->salt);
67 OPENSSL_clear_free(impl->key, impl->key_len);
68 OPENSSL_cleanse(impl->info, impl->info_len);
69 memset(impl, 0, sizeof(*impl));
aacfb134
AG
70}
71
5a285add 72static int kdf_hkdf_ctrl(EVP_KDF_IMPL *impl, int cmd, va_list args)
aacfb134 73{
5a285add
DM
74 const unsigned char *p;
75 size_t len;
76 const EVP_MD *md;
aacfb134 77
5a285add
DM
78 switch (cmd) {
79 case EVP_KDF_CTRL_SET_MD:
80 md = va_arg(args, const EVP_MD *);
81 if (md == NULL)
aacfb134
AG
82 return 0;
83
5a285add 84 impl->md = md;
aacfb134
AG
85 return 1;
86
5a285add
DM
87 case EVP_KDF_CTRL_SET_HKDF_MODE:
88 impl->mode = va_arg(args, int);
d2139cf8
MC
89 return 1;
90
5a285add
DM
91 case EVP_KDF_CTRL_SET_SALT:
92 p = va_arg(args, const unsigned char *);
93 len = va_arg(args, size_t);
94 if (len == 0 || p == NULL)
aacfb134
AG
95 return 1;
96
5a285add
DM
97 OPENSSL_free(impl->salt);
98 impl->salt = OPENSSL_memdup(p, len);
99 if (impl->salt == NULL)
aacfb134
AG
100 return 0;
101
5a285add 102 impl->salt_len = len;
aacfb134
AG
103 return 1;
104
5a285add
DM
105 case EVP_KDF_CTRL_SET_KEY:
106 p = va_arg(args, const unsigned char *);
107 len = va_arg(args, size_t);
108 OPENSSL_clear_free(impl->key, impl->key_len);
109 impl->key = OPENSSL_memdup(p, len);
110 if (impl->key == NULL)
aacfb134
AG
111 return 0;
112
5a285add
DM
113 impl->key_len = len;
114 return 1;
aacfb134 115
5a285add
DM
116 case EVP_KDF_CTRL_RESET_HKDF_INFO:
117 OPENSSL_cleanse(impl->info, impl->info_len);
118 impl->info_len = 0;
aacfb134
AG
119 return 1;
120
5a285add
DM
121 case EVP_KDF_CTRL_ADD_HKDF_INFO:
122 p = va_arg(args, const unsigned char *);
123 len = va_arg(args, size_t);
124 if (len == 0 || p == NULL)
aacfb134
AG
125 return 1;
126
5a285add 127 if (len > (HKDF_MAXBUF - impl->info_len))
aacfb134
AG
128 return 0;
129
5a285add
DM
130 memcpy(impl->info + impl->info_len, p, len);
131 impl->info_len += len;
aacfb134
AG
132 return 1;
133
134 default:
135 return -2;
aacfb134
AG
136 }
137}
138
5a285add
DM
139static int kdf_hkdf_ctrl_str(EVP_KDF_IMPL *impl, const char *type,
140 const char *value)
aacfb134 141{
ddd2c389
MC
142 if (strcmp(type, "mode") == 0) {
143 int mode;
144
145 if (strcmp(value, "EXTRACT_AND_EXPAND") == 0)
5a285add 146 mode = EVP_KDF_HKDF_MODE_EXTRACT_AND_EXPAND;
ddd2c389 147 else if (strcmp(value, "EXTRACT_ONLY") == 0)
5a285add 148 mode = EVP_KDF_HKDF_MODE_EXTRACT_ONLY;
ddd2c389 149 else if (strcmp(value, "EXPAND_ONLY") == 0)
5a285add 150 mode = EVP_KDF_HKDF_MODE_EXPAND_ONLY;
ddd2c389
MC
151 else
152 return 0;
153
5a285add 154 return call_ctrl(kdf_hkdf_ctrl, impl, EVP_KDF_CTRL_SET_HKDF_MODE, mode);
ddd2c389
MC
155 }
156
5a285add
DM
157 if (strcmp(type, "digest") == 0)
158 return kdf_md2ctrl(impl, kdf_hkdf_ctrl, EVP_KDF_CTRL_SET_MD, value);
aacfb134
AG
159
160 if (strcmp(type, "salt") == 0)
5a285add 161 return kdf_str2ctrl(impl, kdf_hkdf_ctrl, EVP_KDF_CTRL_SET_SALT, value);
aacfb134
AG
162
163 if (strcmp(type, "hexsalt") == 0)
5a285add 164 return kdf_hex2ctrl(impl, kdf_hkdf_ctrl, EVP_KDF_CTRL_SET_SALT, value);
aacfb134
AG
165
166 if (strcmp(type, "key") == 0)
5a285add 167 return kdf_str2ctrl(impl, kdf_hkdf_ctrl, EVP_KDF_CTRL_SET_KEY, value);
aacfb134
AG
168
169 if (strcmp(type, "hexkey") == 0)
5a285add 170 return kdf_hex2ctrl(impl, kdf_hkdf_ctrl, EVP_KDF_CTRL_SET_KEY, value);
aacfb134
AG
171
172 if (strcmp(type, "info") == 0)
5a285add
DM
173 return kdf_str2ctrl(impl, kdf_hkdf_ctrl, EVP_KDF_CTRL_ADD_HKDF_INFO,
174 value);
aacfb134
AG
175
176 if (strcmp(type, "hexinfo") == 0)
5a285add
DM
177 return kdf_hex2ctrl(impl, kdf_hkdf_ctrl, EVP_KDF_CTRL_ADD_HKDF_INFO,
178 value);
aacfb134
AG
179
180 return -2;
181}
182
5a285add 183static size_t kdf_hkdf_size(EVP_KDF_IMPL *impl)
ca55d70b 184{
97cc9c9b
SL
185 int sz;
186
5a285add
DM
187 if (impl->mode != EVP_KDF_HKDF_MODE_EXTRACT_ONLY)
188 return SIZE_MAX;
ca55d70b 189
5a285add
DM
190 if (impl->md == NULL) {
191 KDFerr(KDF_F_KDF_HKDF_SIZE, KDF_R_MISSING_MESSAGE_DIGEST);
192 return 0;
193 }
97cc9c9b
SL
194 sz = EVP_MD_size(impl->md);
195 if (sz < 0)
196 return 0;
197
198 return sz;
ca55d70b
MC
199}
200
5a285add
DM
201static int kdf_hkdf_derive(EVP_KDF_IMPL *impl, unsigned char *key,
202 size_t keylen)
aacfb134 203{
5a285add
DM
204 if (impl->md == NULL) {
205 KDFerr(KDF_F_KDF_HKDF_DERIVE, KDF_R_MISSING_MESSAGE_DIGEST);
f55129c7
JB
206 return 0;
207 }
5a285add
DM
208 if (impl->key == NULL) {
209 KDFerr(KDF_F_KDF_HKDF_DERIVE, KDF_R_MISSING_KEY);
aacfb134 210 return 0;
e65f6509 211 }
aacfb134 212
5a285add
DM
213 switch (impl->mode) {
214 case EVP_KDF_HKDF_MODE_EXTRACT_AND_EXPAND:
215 return HKDF(impl->md, impl->salt, impl->salt_len, impl->key,
216 impl->key_len, impl->info, impl->info_len, key,
217 keylen);
d2139cf8 218
5a285add
DM
219 case EVP_KDF_HKDF_MODE_EXTRACT_ONLY:
220 return HKDF_Extract(impl->md, impl->salt, impl->salt_len, impl->key,
221 impl->key_len, key, keylen);
d2139cf8 222
5a285add
DM
223 case EVP_KDF_HKDF_MODE_EXPAND_ONLY:
224 return HKDF_Expand(impl->md, impl->key, impl->key_len, impl->info,
225 impl->info_len, key, keylen);
d2139cf8
MC
226
227 default:
aacfb134
AG
228 return 0;
229 }
aacfb134
AG
230}
231
d2ba8123 232const EVP_KDF hkdf_kdf_meth = {
5a285add
DM
233 EVP_KDF_HKDF,
234 kdf_hkdf_new,
235 kdf_hkdf_free,
236 kdf_hkdf_reset,
237 kdf_hkdf_ctrl,
238 kdf_hkdf_ctrl_str,
239 kdf_hkdf_size,
240 kdf_hkdf_derive
aacfb134
AG
241};
242
e7018588
DM
243/*
244 * Refer to "HMAC-based Extract-and-Expand Key Derivation Function (HKDF)"
245 * Section 2 (https://tools.ietf.org/html/rfc5869#section-2) and
246 * "Cryptographic Extraction and Key Derivation: The HKDF Scheme"
247 * Section 4.2 (https://eprint.iacr.org/2010/264.pdf).
248 *
249 * From the paper:
250 * The scheme HKDF is specified as:
251 * HKDF(XTS, SKM, CTXinfo, L) = K(1) | K(2) | ... | K(t)
252 *
253 * where:
254 * SKM is source key material
255 * XTS is extractor salt (which may be null or constant)
256 * CTXinfo is context information (may be null)
257 * L is the number of key bits to be produced by KDF
258 * k is the output length in bits of the hash function used with HMAC
259 * t = ceil(L/k)
260 * the value K(t) is truncated to its first d = L mod k bits.
261 *
262 * From RFC 5869:
263 * 2.2. Step 1: Extract
264 * HKDF-Extract(salt, IKM) -> PRK
265 * 2.3. Step 2: Expand
266 * HKDF-Expand(PRK, info, L) -> OKM
267 */
5a285add
DM
268static int HKDF(const EVP_MD *evp_md,
269 const unsigned char *salt, size_t salt_len,
e7018588 270 const unsigned char *ikm, size_t ikm_len,
5a285add
DM
271 const unsigned char *info, size_t info_len,
272 unsigned char *okm, size_t okm_len)
aacfb134
AG
273{
274 unsigned char prk[EVP_MAX_MD_SIZE];
97cc9c9b
SL
275 int ret, sz;
276 size_t prk_len;
277
278 sz = EVP_MD_size(evp_md);
279 if (sz < 0)
280 return 0;
281 prk_len = (size_t)sz;
aacfb134 282
e7018588
DM
283 /* Step 1: HKDF-Extract(salt, IKM) -> PRK */
284 if (!HKDF_Extract(evp_md, salt, salt_len, ikm, ikm_len, prk, prk_len))
5a285add 285 return 0;
aacfb134 286
e7018588 287 /* Step 2: HKDF-Expand(PRK, info, L) -> OKM */
d2139cf8
MC
288 ret = HKDF_Expand(evp_md, prk, prk_len, info, info_len, okm, okm_len);
289 OPENSSL_cleanse(prk, sizeof(prk));
290
291 return ret;
aacfb134
AG
292}
293
e7018588
DM
294/*
295 * Refer to "HMAC-based Extract-and-Expand Key Derivation Function (HKDF)"
296 * Section 2.2 (https://tools.ietf.org/html/rfc5869#section-2.2).
297 *
298 * 2.2. Step 1: Extract
299 *
300 * HKDF-Extract(salt, IKM) -> PRK
301 *
302 * Options:
303 * Hash a hash function; HashLen denotes the length of the
304 * hash function output in octets
305 *
306 * Inputs:
307 * salt optional salt value (a non-secret random value);
308 * if not provided, it is set to a string of HashLen zeros.
309 * IKM input keying material
310 *
311 * Output:
312 * PRK a pseudorandom key (of HashLen octets)
313 *
314 * The output PRK is calculated as follows:
315 *
316 * PRK = HMAC-Hash(salt, IKM)
317 */
5a285add
DM
318static int HKDF_Extract(const EVP_MD *evp_md,
319 const unsigned char *salt, size_t salt_len,
e7018588 320 const unsigned char *ikm, size_t ikm_len,
5a285add 321 unsigned char *prk, size_t prk_len)
aacfb134 322{
97cc9c9b
SL
323 int sz = EVP_MD_size(evp_md);
324
325 if (sz < 0)
326 return 0;
327 if (prk_len != (size_t)sz) {
5a285add
DM
328 KDFerr(KDF_F_HKDF_EXTRACT, KDF_R_WRONG_OUTPUT_BUFFER_SIZE);
329 return 0;
330 }
e7018588
DM
331 /* calc: PRK = HMAC-Hash(salt, IKM) */
332 return HMAC(evp_md, salt, salt_len, ikm, ikm_len, prk, NULL) != NULL;
aacfb134
AG
333}
334
e7018588
DM
335/*
336 * Refer to "HMAC-based Extract-and-Expand Key Derivation Function (HKDF)"
337 * Section 2.3 (https://tools.ietf.org/html/rfc5869#section-2.3).
338 *
339 * 2.3. Step 2: Expand
340 *
341 * HKDF-Expand(PRK, info, L) -> OKM
342 *
343 * Options:
344 * Hash a hash function; HashLen denotes the length of the
345 * hash function output in octets
346 *
347 * Inputs:
348 * PRK a pseudorandom key of at least HashLen octets
349 * (usually, the output from the extract step)
350 * info optional context and application specific information
351 * (can be a zero-length string)
352 * L length of output keying material in octets
353 * (<= 255*HashLen)
354 *
355 * Output:
356 * OKM output keying material (of L octets)
357 *
358 * The output OKM is calculated as follows:
359 *
360 * N = ceil(L/HashLen)
361 * T = T(1) | T(2) | T(3) | ... | T(N)
362 * OKM = first L octets of T
363 *
364 * where:
365 * T(0) = empty string (zero length)
366 * T(1) = HMAC-Hash(PRK, T(0) | info | 0x01)
367 * T(2) = HMAC-Hash(PRK, T(1) | info | 0x02)
368 * T(3) = HMAC-Hash(PRK, T(2) | info | 0x03)
369 * ...
370 *
371 * (where the constant concatenated to the end of each T(n) is a
372 * single octet.)
373 */
5a285add
DM
374static int HKDF_Expand(const EVP_MD *evp_md,
375 const unsigned char *prk, size_t prk_len,
376 const unsigned char *info, size_t info_len,
377 unsigned char *okm, size_t okm_len)
aacfb134
AG
378{
379 HMAC_CTX *hmac;
97cc9c9b 380 int ret = 0, sz;
aacfb134 381 unsigned int i;
aacfb134 382 unsigned char prev[EVP_MAX_MD_SIZE];
97cc9c9b
SL
383 size_t done_len = 0, dig_len, n;
384
385 sz = EVP_MD_size(evp_md);
386 if (sz <= 0)
387 return 0;
388 dig_len = (size_t)sz;
5a285add 389
e7018588
DM
390 /* calc: N = ceil(L/HashLen) */
391 n = okm_len / dig_len;
aacfb134
AG
392 if (okm_len % dig_len)
393 n++;
394
d2139cf8 395 if (n > 255 || okm == NULL)
5a285add 396 return 0;
aacfb134
AG
397
398 if ((hmac = HMAC_CTX_new()) == NULL)
5a285add 399 return 0;
aacfb134
AG
400
401 if (!HMAC_Init_ex(hmac, prk, prk_len, evp_md, NULL))
402 goto err;
403
404 for (i = 1; i <= n; i++) {
405 size_t copy_len;
406 const unsigned char ctr = i;
407
e7018588 408 /* calc: T(i) = HMAC-Hash(PRK, T(i - 1) | info | i) */
aacfb134
AG
409 if (i > 1) {
410 if (!HMAC_Init_ex(hmac, NULL, 0, NULL, NULL))
411 goto err;
412
413 if (!HMAC_Update(hmac, prev, dig_len))
414 goto err;
415 }
416
417 if (!HMAC_Update(hmac, info, info_len))
418 goto err;
419
420 if (!HMAC_Update(hmac, &ctr, 1))
421 goto err;
422
423 if (!HMAC_Final(hmac, prev, NULL))
424 goto err;
425
426 copy_len = (done_len + dig_len > okm_len) ?
427 okm_len - done_len :
428 dig_len;
429
430 memcpy(okm + done_len, prev, copy_len);
431
432 done_len += copy_len;
433 }
5a285add 434 ret = 1;
aacfb134
AG
435
436 err:
64ed55ab 437 OPENSSL_cleanse(prev, sizeof(prev));
aacfb134 438 HMAC_CTX_free(hmac);
64ed55ab 439 return ret;
aacfb134 440}