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