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