1 /*
2 * Copyright 1999-2021 The OpenSSL Project Authors. All Rights Reserved.
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 <openssl/trace.h>
11 #include <stdlib.h>
12 #include <stdarg.h>
13 #include <string.h>
14 #include <openssl/evp.h>
15 #include <openssl/kdf.h>
16 #include <openssl/core_names.h>
17 #include <openssl/proverr.h>
18 #include "internal/cryptlib.h"
19 #include "internal/numbers.h"
20 #include "crypto/evp.h"
21 #include "prov/provider_ctx.h"
22 #include "prov/providercommon.h"
23 #include "prov/implementations.h"
24 #include "prov/provider_util.h"
25
26 static OSSL_FUNC_kdf_newctx_fn kdf_pbkdf1_new;
27 static OSSL_FUNC_kdf_freectx_fn kdf_pbkdf1_free;
28 static OSSL_FUNC_kdf_reset_fn kdf_pbkdf1_reset;
29 static OSSL_FUNC_kdf_derive_fn kdf_pbkdf1_derive;
30 static OSSL_FUNC_kdf_settable_ctx_params_fn kdf_pbkdf1_settable_ctx_params;
31 static OSSL_FUNC_kdf_set_ctx_params_fn kdf_pbkdf1_set_ctx_params;
32 static OSSL_FUNC_kdf_gettable_ctx_params_fn kdf_pbkdf1_gettable_ctx_params;
33 static OSSL_FUNC_kdf_get_ctx_params_fn kdf_pbkdf1_get_ctx_params;
34
35 typedef struct {
36 void *provctx;
37 PROV_DIGEST digest;
38 unsigned char *pass;
39 size_t pass_len;
40 unsigned char *salt;
41 size_t salt_len;
42 uint64_t iter;
43 } KDF_PBKDF1;
44
45 /*
46 * PKCS5 PBKDF1 compatible key/IV generation as specified in:
47 * https://tools.ietf.org/html/rfc8018#page-10
48 */
49
kdf_pbkdf1_do_derive(const unsigned char * pass,size_t passlen,const unsigned char * salt,size_t saltlen,uint64_t iter,const EVP_MD * md_type,unsigned char * out,size_t n)50 static int kdf_pbkdf1_do_derive(const unsigned char *pass, size_t passlen,
51 const unsigned char *salt, size_t saltlen,
52 uint64_t iter, const EVP_MD *md_type,
53 unsigned char *out, size_t n)
54 {
55 uint64_t i;
56 int mdsize, ret = 0;
57 unsigned char md_tmp[EVP_MAX_MD_SIZE];
58 EVP_MD_CTX *ctx = NULL;
59
60 ctx = EVP_MD_CTX_new();
61 if (ctx == NULL) {
62 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
63 goto err;
64 }
65
66 if (!EVP_DigestInit_ex(ctx, md_type, NULL)
67 || !EVP_DigestUpdate(ctx, pass, passlen)
68 || !EVP_DigestUpdate(ctx, salt, saltlen)
69 || !EVP_DigestFinal_ex(ctx, md_tmp, NULL))
70 goto err;
71 mdsize = EVP_MD_size(md_type);
72 if (mdsize < 0)
73 goto err;
74 for (i = 1; i < iter; i++) {
75 if (!EVP_DigestInit_ex(ctx, md_type, NULL))
76 goto err;
77 if (!EVP_DigestUpdate(ctx, md_tmp, mdsize))
78 goto err;
79 if (!EVP_DigestFinal_ex(ctx, md_tmp, NULL))
80 goto err;
81 }
82
83 memcpy(out, md_tmp, n);
84 ret = 1;
85 err:
86 EVP_MD_CTX_free(ctx);
87 return ret;
88 }
89
kdf_pbkdf1_new(void * provctx)90 static void *kdf_pbkdf1_new(void *provctx)
91 {
92 KDF_PBKDF1 *ctx;
93
94 if (!ossl_prov_is_running())
95 return NULL;
96
97 ctx = OPENSSL_zalloc(sizeof(*ctx));
98 if (ctx == NULL) {
99 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
100 return NULL;
101 }
102 ctx->provctx = provctx;
103 return ctx;
104 }
105
kdf_pbkdf1_cleanup(KDF_PBKDF1 * ctx)106 static void kdf_pbkdf1_cleanup(KDF_PBKDF1 *ctx)
107 {
108 ossl_prov_digest_reset(&ctx->digest);
109 OPENSSL_free(ctx->salt);
110 OPENSSL_clear_free(ctx->pass, ctx->pass_len);
111 memset(ctx, 0, sizeof(*ctx));
112 }
113
kdf_pbkdf1_free(void * vctx)114 static void kdf_pbkdf1_free(void *vctx)
115 {
116 KDF_PBKDF1 *ctx = (KDF_PBKDF1 *)vctx;
117
118 if (ctx != NULL) {
119 kdf_pbkdf1_cleanup(ctx);
120 OPENSSL_free(ctx);
121 }
122 }
123
kdf_pbkdf1_reset(void * vctx)124 static void kdf_pbkdf1_reset(void *vctx)
125 {
126 KDF_PBKDF1 *ctx = (KDF_PBKDF1 *)vctx;
127 void *provctx = ctx->provctx;
128
129 kdf_pbkdf1_cleanup(ctx);
130 ctx->provctx = provctx;
131 }
132
kdf_pbkdf1_set_membuf(unsigned char ** buffer,size_t * buflen,const OSSL_PARAM * p)133 static int kdf_pbkdf1_set_membuf(unsigned char **buffer, size_t *buflen,
134 const OSSL_PARAM *p)
135 {
136 OPENSSL_clear_free(*buffer, *buflen);
137 if (p->data_size == 0) {
138 if ((*buffer = OPENSSL_malloc(1)) == NULL) {
139 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
140 return 0;
141 }
142 } else if (p->data != NULL) {
143 *buffer = NULL;
144 if (!OSSL_PARAM_get_octet_string(p, (void **)buffer, 0, buflen))
145 return 0;
146 }
147 return 1;
148 }
149
kdf_pbkdf1_derive(void * vctx,unsigned char * key,size_t keylen,const OSSL_PARAM params[])150 static int kdf_pbkdf1_derive(void *vctx, unsigned char *key, size_t keylen,
151 const OSSL_PARAM params[])
152 {
153 KDF_PBKDF1 *ctx = (KDF_PBKDF1 *)vctx;
154 const EVP_MD *md;
155
156 if (!ossl_prov_is_running() || !kdf_pbkdf1_set_ctx_params(ctx, params))
157 return 0;
158
159 if (ctx->pass == NULL) {
160 ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_PASS);
161 return 0;
162 }
163
164 if (ctx->salt == NULL) {
165 ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SALT);
166 return 0;
167 }
168
169 md = ossl_prov_digest_md(&ctx->digest);
170 return kdf_pbkdf1_do_derive(ctx->pass, ctx->pass_len, ctx->salt, ctx->salt_len,
171 ctx->iter, md, key, keylen);
172 }
173
kdf_pbkdf1_set_ctx_params(void * vctx,const OSSL_PARAM params[])174 static int kdf_pbkdf1_set_ctx_params(void *vctx, const OSSL_PARAM params[])
175 {
176 const OSSL_PARAM *p;
177 KDF_PBKDF1 *ctx = vctx;
178 OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx);
179
180 if (!ossl_prov_digest_load_from_params(&ctx->digest, params, libctx))
181 return 0;
182
183 if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_PASSWORD)) != NULL)
184 if (!kdf_pbkdf1_set_membuf(&ctx->pass, &ctx->pass_len, p))
185 return 0;
186
187 if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SALT)) != NULL)
188 if (!kdf_pbkdf1_set_membuf(&ctx->salt, &ctx->salt_len, p))
189 return 0;
190
191 if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_ITER)) != NULL)
192 if (!OSSL_PARAM_get_uint64(p, &ctx->iter))
193 return 0;
194 return 1;
195 }
196
kdf_pbkdf1_settable_ctx_params(ossl_unused void * ctx,ossl_unused void * p_ctx)197 static const OSSL_PARAM *kdf_pbkdf1_settable_ctx_params(ossl_unused void *ctx,
198 ossl_unused void *p_ctx)
199 {
200 static const OSSL_PARAM known_settable_ctx_params[] = {
201 OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0),
202 OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, NULL, 0),
203 OSSL_PARAM_octet_string(OSSL_KDF_PARAM_PASSWORD, NULL, 0),
204 OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SALT, NULL, 0),
205 OSSL_PARAM_uint64(OSSL_KDF_PARAM_ITER, NULL),
206 OSSL_PARAM_END
207 };
208 return known_settable_ctx_params;
209 }
210
kdf_pbkdf1_get_ctx_params(void * vctx,OSSL_PARAM params[])211 static int kdf_pbkdf1_get_ctx_params(void *vctx, OSSL_PARAM params[])
212 {
213 OSSL_PARAM *p;
214
215 if ((p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_SIZE)) != NULL)
216 return OSSL_PARAM_set_size_t(p, SIZE_MAX);
217 return -2;
218 }
219
kdf_pbkdf1_gettable_ctx_params(ossl_unused void * ctx,ossl_unused void * p_ctx)220 static const OSSL_PARAM *kdf_pbkdf1_gettable_ctx_params(ossl_unused void *ctx,
221 ossl_unused void *p_ctx)
222 {
223 static const OSSL_PARAM known_gettable_ctx_params[] = {
224 OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL),
225 OSSL_PARAM_END
226 };
227 return known_gettable_ctx_params;
228 }
229
230 const OSSL_DISPATCH ossl_kdf_pbkdf1_functions[] = {
231 { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))kdf_pbkdf1_new },
232 { OSSL_FUNC_KDF_FREECTX, (void(*)(void))kdf_pbkdf1_free },
233 { OSSL_FUNC_KDF_RESET, (void(*)(void))kdf_pbkdf1_reset },
234 { OSSL_FUNC_KDF_DERIVE, (void(*)(void))kdf_pbkdf1_derive },
235 { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,
236 (void(*)(void))kdf_pbkdf1_settable_ctx_params },
237 { OSSL_FUNC_KDF_SET_CTX_PARAMS, (void(*)(void))kdf_pbkdf1_set_ctx_params },
238 { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,
239 (void(*)(void))kdf_pbkdf1_gettable_ctx_params },
240 { OSSL_FUNC_KDF_GET_CTX_PARAMS, (void(*)(void))kdf_pbkdf1_get_ctx_params },
241 { 0, NULL }
242 };
243