]>
Commit | Line | Data |
---|---|---|
5a285add | 1 | /* |
f0efeea2 | 2 | * Copyright 2018-2019 The OpenSSL Project Authors. All Rights Reserved. |
5a285add DM |
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> | |
e3405a4a | 16 | #include <openssl/core_names.h> |
5a285add | 17 | #include "internal/cryptlib.h" |
e3405a4a | 18 | #include "internal/numbers.h" |
5a285add | 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" | |
e957226a | 23 | #include "internal/provider_util.h" |
5a285add | 24 | |
f0efeea2 SL |
25 | /* Constants specified in SP800-132 */ |
26 | #define KDF_PBKDF2_MIN_KEY_LEN_BITS 112 | |
27 | #define KDF_PBKDF2_MAX_KEY_LEN_DIGEST_RATIO 0xFFFFFFFF | |
28 | #define KDF_PBKDF2_MIN_ITERATIONS 1000 | |
29 | #define KDF_PBKDF2_MIN_SALT_LEN (128 / 8) | |
30 | /* | |
31 | * For backwards compatibility reasons, | |
32 | * Extra checks are done by default in fips mode only. | |
33 | */ | |
34 | #ifdef FIPS_MODE | |
35 | # define KDF_PBKDF2_DEFAULT_CHECKS 1 | |
36 | #else | |
37 | # define KDF_PBKDF2_DEFAULT_CHECKS 0 | |
38 | #endif /* FIPS_MODE */ | |
39 | ||
e3405a4a P |
40 | static OSSL_OP_kdf_newctx_fn kdf_pbkdf2_new; |
41 | static OSSL_OP_kdf_freectx_fn kdf_pbkdf2_free; | |
42 | static OSSL_OP_kdf_reset_fn kdf_pbkdf2_reset; | |
43 | static OSSL_OP_kdf_derive_fn kdf_pbkdf2_derive; | |
44 | static OSSL_OP_kdf_settable_ctx_params_fn kdf_pbkdf2_settable_ctx_params; | |
45 | static OSSL_OP_kdf_set_ctx_params_fn kdf_pbkdf2_set_ctx_params; | |
46 | ||
f0efeea2 | 47 | static int pbkdf2_derive(const char *pass, size_t passlen, |
e3405a4a | 48 | const unsigned char *salt, int saltlen, uint64_t iter, |
f0efeea2 SL |
49 | const EVP_MD *digest, unsigned char *key, |
50 | size_t keylen, int extra_checks); | |
5a285add | 51 | |
e3405a4a P |
52 | typedef struct { |
53 | void *provctx; | |
5a285add DM |
54 | unsigned char *pass; |
55 | size_t pass_len; | |
56 | unsigned char *salt; | |
57 | size_t salt_len; | |
e3405a4a | 58 | uint64_t iter; |
e957226a | 59 | PROV_DIGEST digest; |
f0efeea2 | 60 | int lower_bound_checks; |
e3405a4a | 61 | } KDF_PBKDF2; |
5a285add | 62 | |
e3405a4a P |
63 | static void kdf_pbkdf2_init(KDF_PBKDF2 *ctx); |
64 | ||
65 | static void *kdf_pbkdf2_new(void *provctx) | |
5a285add | 66 | { |
e3405a4a | 67 | KDF_PBKDF2 *ctx; |
5a285add | 68 | |
e3405a4a P |
69 | ctx = OPENSSL_zalloc(sizeof(*ctx)); |
70 | if (ctx == NULL) { | |
71 | ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE); | |
5a285add DM |
72 | return NULL; |
73 | } | |
e3405a4a P |
74 | ctx->provctx = provctx; |
75 | kdf_pbkdf2_init(ctx); | |
76 | return ctx; | |
5a285add DM |
77 | } |
78 | ||
b1f15129 RL |
79 | static void kdf_pbkdf2_cleanup(KDF_PBKDF2 *ctx) |
80 | { | |
e957226a | 81 | ossl_prov_digest_reset(&ctx->digest); |
b1f15129 RL |
82 | OPENSSL_free(ctx->salt); |
83 | OPENSSL_clear_free(ctx->pass, ctx->pass_len); | |
84 | memset(ctx, 0, sizeof(*ctx)); | |
85 | } | |
86 | ||
e3405a4a | 87 | static void kdf_pbkdf2_free(void *vctx) |
5a285add | 88 | { |
e3405a4a P |
89 | KDF_PBKDF2 *ctx = (KDF_PBKDF2 *)vctx; |
90 | ||
b1f15129 | 91 | kdf_pbkdf2_cleanup(ctx); |
e3405a4a | 92 | OPENSSL_free(ctx); |
5a285add DM |
93 | } |
94 | ||
e3405a4a | 95 | static void kdf_pbkdf2_reset(void *vctx) |
5a285add | 96 | { |
e3405a4a P |
97 | KDF_PBKDF2 *ctx = (KDF_PBKDF2 *)vctx; |
98 | ||
b1f15129 | 99 | kdf_pbkdf2_cleanup(ctx); |
e3405a4a | 100 | kdf_pbkdf2_init(ctx); |
5a285add DM |
101 | } |
102 | ||
e3405a4a | 103 | static void kdf_pbkdf2_init(KDF_PBKDF2 *ctx) |
5a285add | 104 | { |
e957226a P |
105 | OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END }; |
106 | OPENSSL_CTX *provctx = PROV_LIBRARY_CONTEXT_OF(ctx->provctx); | |
107 | ||
108 | params[0] = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST, | |
109 | SN_sha1, 0); | |
110 | ossl_prov_digest_load_from_params(&ctx->digest, params, provctx); | |
e3405a4a | 111 | ctx->iter = PKCS5_DEFAULT_ITER; |
e3405a4a | 112 | ctx->lower_bound_checks = KDF_PBKDF2_DEFAULT_CHECKS; |
5a285add DM |
113 | } |
114 | ||
115 | static int pbkdf2_set_membuf(unsigned char **buffer, size_t *buflen, | |
e3405a4a | 116 | const OSSL_PARAM *p) |
5a285add | 117 | { |
5a285add | 118 | OPENSSL_clear_free(*buffer, *buflen); |
e3405a4a P |
119 | if (p->data_size == 0) { |
120 | if ((*buffer = OPENSSL_malloc(1)) == NULL) { | |
121 | ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE); | |
122 | return 0; | |
123 | } | |
124 | } else if (p->data != NULL) { | |
125 | *buffer = NULL; | |
126 | if (!OSSL_PARAM_get_octet_string(p, (void **)buffer, 0, buflen)) | |
127 | return 0; | |
128 | } | |
129 | return 1; | |
130 | } | |
131 | ||
132 | static int kdf_pbkdf2_derive(void *vctx, unsigned char *key, | |
133 | size_t keylen) | |
134 | { | |
135 | KDF_PBKDF2 *ctx = (KDF_PBKDF2 *)vctx; | |
e957226a | 136 | const EVP_MD *md = ossl_prov_digest_md(&ctx->digest); |
5a285add | 137 | |
e3405a4a P |
138 | if (ctx->pass == NULL) { |
139 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_PASS); | |
140 | return 0; | |
5a285add | 141 | } |
e3405a4a P |
142 | |
143 | if (ctx->salt == NULL) { | |
144 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SALT); | |
5a285add DM |
145 | return 0; |
146 | } | |
147 | ||
e3405a4a P |
148 | return pbkdf2_derive((char *)ctx->pass, ctx->pass_len, |
149 | ctx->salt, ctx->salt_len, ctx->iter, | |
e957226a | 150 | md, key, keylen, ctx->lower_bound_checks); |
5a285add DM |
151 | } |
152 | ||
e3405a4a | 153 | static int kdf_pbkdf2_set_ctx_params(void *vctx, const OSSL_PARAM params[]) |
5a285add | 154 | { |
e3405a4a P |
155 | const OSSL_PARAM *p; |
156 | KDF_PBKDF2 *ctx = vctx; | |
e957226a | 157 | OPENSSL_CTX *provctx = PROV_LIBRARY_CONTEXT_OF(ctx->provctx); |
e3405a4a P |
158 | int pkcs5; |
159 | uint64_t iter, min_iter; | |
e3405a4a | 160 | |
e957226a P |
161 | if (!ossl_prov_digest_load_from_params(&ctx->digest, params, provctx)) |
162 | return 0; | |
5a285add | 163 | |
e3405a4a P |
164 | if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_PKCS5)) != NULL) { |
165 | if (!OSSL_PARAM_get_int(p, &pkcs5)) | |
166 | return 0; | |
167 | ctx->lower_bound_checks = pkcs5 == 0; | |
5a285add DM |
168 | } |
169 | ||
e3405a4a P |
170 | if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_PASSWORD)) != NULL) |
171 | if (!pbkdf2_set_membuf(&ctx->pass, &ctx->pass_len, p)) | |
172 | return 0; | |
5a285add | 173 | |
e3405a4a P |
174 | if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SALT)) != NULL) { |
175 | if (ctx->lower_bound_checks != 0 | |
176 | && p->data_size < KDF_PBKDF2_MIN_SALT_LEN) { | |
177 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_SALT_LENGTH); | |
178 | return 0; | |
179 | } | |
180 | if (!pbkdf2_set_membuf(&ctx->salt, &ctx->salt_len,p)) | |
181 | return 0; | |
182 | } | |
5a285add | 183 | |
e3405a4a P |
184 | if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_ITER)) != NULL) { |
185 | if (!OSSL_PARAM_get_uint64(p, &iter)) | |
186 | return 0; | |
187 | min_iter = ctx->lower_bound_checks != 0 ? KDF_PBKDF2_MIN_ITERATIONS : 1; | |
188 | if (iter < min_iter) { | |
189 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_ITERATION_COUNT); | |
190 | return 0; | |
191 | } | |
192 | ctx->iter = iter; | |
193 | } | |
194 | return 1; | |
195 | } | |
5a285add | 196 | |
e3405a4a P |
197 | static const OSSL_PARAM *kdf_pbkdf2_settable_ctx_params(void) |
198 | { | |
199 | static const OSSL_PARAM known_settable_ctx_params[] = { | |
200 | OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0), | |
201 | OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, NULL, 0), | |
202 | OSSL_PARAM_octet_string(OSSL_KDF_PARAM_PASSWORD, NULL, 0), | |
203 | OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SALT, NULL, 0), | |
204 | OSSL_PARAM_uint64(OSSL_KDF_PARAM_ITER, NULL), | |
205 | OSSL_PARAM_int(OSSL_KDF_PARAM_PKCS5, NULL), | |
206 | OSSL_PARAM_END | |
207 | }; | |
208 | return known_settable_ctx_params; | |
209 | } | |
5a285add | 210 | |
e3405a4a P |
211 | static int kdf_pbkdf2_get_ctx_params(void *vctx, OSSL_PARAM params[]) |
212 | { | |
213 | OSSL_PARAM *p; | |
5a285add | 214 | |
e3405a4a P |
215 | if ((p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_SIZE)) != NULL) |
216 | return OSSL_PARAM_set_size_t(p, SIZE_MAX); | |
5a285add DM |
217 | return -2; |
218 | } | |
219 | ||
e3405a4a | 220 | static const OSSL_PARAM *kdf_pbkdf2_gettable_ctx_params(void) |
5a285add | 221 | { |
e3405a4a P |
222 | static const OSSL_PARAM known_gettable_ctx_params[] = { |
223 | OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL), | |
224 | OSSL_PARAM_END | |
225 | }; | |
226 | return known_gettable_ctx_params; | |
5a285add DM |
227 | } |
228 | ||
e3405a4a P |
229 | const OSSL_DISPATCH kdf_pbkdf2_functions[] = { |
230 | { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))kdf_pbkdf2_new }, | |
231 | { OSSL_FUNC_KDF_FREECTX, (void(*)(void))kdf_pbkdf2_free }, | |
232 | { OSSL_FUNC_KDF_RESET, (void(*)(void))kdf_pbkdf2_reset }, | |
233 | { OSSL_FUNC_KDF_DERIVE, (void(*)(void))kdf_pbkdf2_derive }, | |
234 | { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS, | |
235 | (void(*)(void))kdf_pbkdf2_settable_ctx_params }, | |
236 | { OSSL_FUNC_KDF_SET_CTX_PARAMS, (void(*)(void))kdf_pbkdf2_set_ctx_params }, | |
237 | { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS, | |
238 | (void(*)(void))kdf_pbkdf2_gettable_ctx_params }, | |
239 | { OSSL_FUNC_KDF_GET_CTX_PARAMS, (void(*)(void))kdf_pbkdf2_get_ctx_params }, | |
240 | { 0, NULL } | |
5a285add DM |
241 | }; |
242 | ||
243 | /* | |
244 | * This is an implementation of PKCS#5 v2.0 password based encryption key | |
245 | * derivation function PBKDF2. SHA1 version verified against test vectors | |
246 | * posted by Peter Gutmann to the PKCS-TNG mailing list. | |
f0efeea2 SL |
247 | * |
248 | * The constraints specified by SP800-132 have been added i.e. | |
249 | * - Check the range of the key length. | |
250 | * - Minimum iteration count of 1000. | |
251 | * - Randomly-generated portion of the salt shall be at least 128 bits. | |
5a285add | 252 | */ |
f0efeea2 | 253 | static int pbkdf2_derive(const char *pass, size_t passlen, |
e3405a4a | 254 | const unsigned char *salt, int saltlen, uint64_t iter, |
f0efeea2 SL |
255 | const EVP_MD *digest, unsigned char *key, |
256 | size_t keylen, int lower_bound_checks) | |
5a285add DM |
257 | { |
258 | int ret = 0; | |
259 | unsigned char digtmp[EVP_MAX_MD_SIZE], *p, itmp[4]; | |
e3405a4a P |
260 | int cplen, k, tkeylen, mdlen; |
261 | uint64_t j; | |
5a285add DM |
262 | unsigned long i = 1; |
263 | HMAC_CTX *hctx_tpl = NULL, *hctx = NULL; | |
264 | ||
265 | mdlen = EVP_MD_size(digest); | |
f0efeea2 SL |
266 | if (mdlen <= 0) |
267 | return 0; | |
268 | ||
269 | /* | |
270 | * This check should always be done because keylen / mdlen >= (2^32 - 1) | |
271 | * results in an overflow of the loop counter 'i'. | |
272 | */ | |
273 | if ((keylen / mdlen) >= KDF_PBKDF2_MAX_KEY_LEN_DIGEST_RATIO) { | |
e3405a4a | 274 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LEN); |
5a285add | 275 | return 0; |
f0efeea2 SL |
276 | } |
277 | ||
278 | if (lower_bound_checks) { | |
e3405a4a P |
279 | if ((keylen * 8) < KDF_PBKDF2_MIN_KEY_LEN_BITS) { |
280 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LEN); | |
281 | return 0; | |
282 | } | |
283 | if (saltlen < KDF_PBKDF2_MIN_SALT_LEN) { | |
284 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_SALT_LENGTH); | |
f0efeea2 | 285 | return 0; |
e3405a4a P |
286 | } |
287 | if (iter < KDF_PBKDF2_MIN_ITERATIONS) { | |
288 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_ITERATION_COUNT); | |
289 | return 0; | |
290 | } | |
f0efeea2 | 291 | } |
5a285add DM |
292 | |
293 | hctx_tpl = HMAC_CTX_new(); | |
294 | if (hctx_tpl == NULL) | |
295 | return 0; | |
296 | p = key; | |
297 | tkeylen = keylen; | |
298 | if (!HMAC_Init_ex(hctx_tpl, pass, passlen, digest, NULL)) | |
299 | goto err; | |
300 | hctx = HMAC_CTX_new(); | |
301 | if (hctx == NULL) | |
302 | goto err; | |
303 | while (tkeylen) { | |
304 | if (tkeylen > mdlen) | |
305 | cplen = mdlen; | |
306 | else | |
307 | cplen = tkeylen; | |
308 | /* | |
309 | * We are unlikely to ever use more than 256 blocks (5120 bits!) but | |
310 | * just in case... | |
311 | */ | |
312 | itmp[0] = (unsigned char)((i >> 24) & 0xff); | |
313 | itmp[1] = (unsigned char)((i >> 16) & 0xff); | |
314 | itmp[2] = (unsigned char)((i >> 8) & 0xff); | |
315 | itmp[3] = (unsigned char)(i & 0xff); | |
316 | if (!HMAC_CTX_copy(hctx, hctx_tpl)) | |
317 | goto err; | |
318 | if (!HMAC_Update(hctx, salt, saltlen) | |
319 | || !HMAC_Update(hctx, itmp, 4) | |
320 | || !HMAC_Final(hctx, digtmp, NULL)) | |
321 | goto err; | |
322 | memcpy(p, digtmp, cplen); | |
323 | for (j = 1; j < iter; j++) { | |
324 | if (!HMAC_CTX_copy(hctx, hctx_tpl)) | |
325 | goto err; | |
326 | if (!HMAC_Update(hctx, digtmp, mdlen) | |
327 | || !HMAC_Final(hctx, digtmp, NULL)) | |
328 | goto err; | |
329 | for (k = 0; k < cplen; k++) | |
330 | p[k] ^= digtmp[k]; | |
331 | } | |
332 | tkeylen -= cplen; | |
333 | i++; | |
334 | p += cplen; | |
335 | } | |
336 | ret = 1; | |
337 | ||
338 | err: | |
339 | HMAC_CTX_free(hctx); | |
340 | HMAC_CTX_free(hctx_tpl); | |
341 | return ret; | |
342 | } |