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