Update copyright year
[openssl.git] / providers / implementations / kdfs / pvkkdf.c
1 /*
2  * Copyright 2018-2022 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/evp.h>
11 #include <openssl/core_names.h>
12 #include <openssl/proverr.h>
13 #include <openssl/err.h>
14 #include "prov/provider_ctx.h"
15 #include "prov/providercommon.h"
16 #include "prov/implementations.h"
17 #include "prov/provider_util.h"
18
19 static OSSL_FUNC_kdf_newctx_fn kdf_pvk_new;
20 static OSSL_FUNC_kdf_dupctx_fn kdf_pvk_dup;
21 static OSSL_FUNC_kdf_freectx_fn kdf_pvk_free;
22 static OSSL_FUNC_kdf_reset_fn kdf_pvk_reset;
23 static OSSL_FUNC_kdf_derive_fn kdf_pvk_derive;
24 static OSSL_FUNC_kdf_settable_ctx_params_fn kdf_pvk_settable_ctx_params;
25 static OSSL_FUNC_kdf_set_ctx_params_fn kdf_pvk_set_ctx_params;
26 static OSSL_FUNC_kdf_gettable_ctx_params_fn kdf_pvk_gettable_ctx_params;
27 static OSSL_FUNC_kdf_get_ctx_params_fn kdf_pvk_get_ctx_params;
28
29 typedef struct {
30     void *provctx;
31     unsigned char *pass;
32     size_t pass_len;
33     unsigned char *salt;
34     size_t salt_len;
35     PROV_DIGEST digest;
36 } KDF_PVK;
37
38 static void kdf_pvk_init(KDF_PVK *ctx);
39
40 static void *kdf_pvk_new(void *provctx)
41 {
42     KDF_PVK *ctx;
43
44     if (!ossl_prov_is_running())
45         return NULL;
46
47     ctx = OPENSSL_zalloc(sizeof(*ctx));
48     if (ctx == NULL) {
49         ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
50         return NULL;
51     }
52     ctx->provctx = provctx;
53     kdf_pvk_init(ctx);
54     return ctx;
55 }
56
57 static void kdf_pvk_cleanup(KDF_PVK *ctx)
58 {
59     ossl_prov_digest_reset(&ctx->digest);
60     OPENSSL_free(ctx->salt);
61     OPENSSL_clear_free(ctx->pass, ctx->pass_len);
62     OPENSSL_cleanse(ctx, sizeof(*ctx));
63 }
64
65 static void kdf_pvk_free(void *vctx)
66 {
67     KDF_PVK *ctx = (KDF_PVK *)vctx;
68
69     if (ctx != NULL) {
70         kdf_pvk_cleanup(ctx);
71         OPENSSL_free(ctx);
72     }
73 }
74
75 static void *kdf_pvk_dup(void *vctx)
76 {
77     const KDF_PVK *src = (const KDF_PVK *)vctx;
78     KDF_PVK *dest;
79
80     dest = kdf_pvk_new(src->provctx);
81     if (dest != NULL)
82         if (!ossl_prov_memdup(src->salt, src->salt_len,
83                               &dest->salt, &dest->salt_len)
84                 || !ossl_prov_memdup(src->pass, src->pass_len,
85                                      &dest->pass , &dest->pass_len)
86                 || !ossl_prov_digest_copy(&dest->digest, &src->digest))
87             goto err;
88     return dest;
89
90  err:
91     kdf_pvk_free(dest);
92     return NULL;
93 }
94
95 static void kdf_pvk_reset(void *vctx)
96 {
97     KDF_PVK *ctx = (KDF_PVK *)vctx;
98     void *provctx = ctx->provctx;
99
100     kdf_pvk_cleanup(ctx);
101     ctx->provctx = provctx;
102     kdf_pvk_init(ctx);
103 }
104
105 static void kdf_pvk_init(KDF_PVK *ctx)
106 {
107     OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
108     OSSL_LIB_CTX *provctx = PROV_LIBCTX_OF(ctx->provctx);
109
110     params[0] = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
111                                                  SN_sha1, 0);
112     if (!ossl_prov_digest_load_from_params(&ctx->digest, params, provctx))
113         /* This is an error, but there is no way to indicate such directly */
114         ossl_prov_digest_reset(&ctx->digest);
115 }
116
117 static int pvk_set_membuf(unsigned char **buffer, size_t *buflen,
118                              const OSSL_PARAM *p)
119 {
120     OPENSSL_clear_free(*buffer, *buflen);
121     *buffer = NULL;
122     *buflen = 0;
123
124     if (p->data_size == 0) {
125         if ((*buffer = OPENSSL_malloc(1)) == NULL) {
126             ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
127             return 0;
128         }
129     } else if (p->data != NULL) {
130         if (!OSSL_PARAM_get_octet_string(p, (void **)buffer, 0, buflen))
131             return 0;
132     }
133     return 1;
134 }
135
136 static int kdf_pvk_derive(void *vctx, unsigned char *key, size_t keylen,
137                              const OSSL_PARAM params[])
138 {
139     KDF_PVK *ctx = (KDF_PVK *)vctx;
140     const EVP_MD *md;
141     EVP_MD_CTX *mctx;
142     int res;
143
144     if (!ossl_prov_is_running() || !kdf_pvk_set_ctx_params(ctx, params))
145         return 0;
146
147     if (ctx->pass == NULL) {
148         ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_PASS);
149         return 0;
150     }
151
152     if (ctx->salt == NULL) {
153         ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SALT);
154         return 0;
155     }
156
157     md = ossl_prov_digest_md(&ctx->digest);
158     if (md == NULL) {
159         ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST);
160         return 0;
161     }
162     res = EVP_MD_get_size(md);
163     if (res <= 0) {
164         ERR_raise(ERR_LIB_PROV, PROV_R_BAD_LENGTH);
165         return 0;
166     }
167     if ((size_t)res > keylen) {
168         ERR_raise(ERR_LIB_PROV, PROV_R_LENGTH_TOO_LARGE);
169         return 0;
170     }
171
172     mctx = EVP_MD_CTX_new();
173     res = mctx != NULL
174           && EVP_DigestInit_ex(mctx, md, NULL)
175           && EVP_DigestUpdate(mctx, ctx->salt, ctx->salt_len)
176           && EVP_DigestUpdate(mctx, ctx->pass, ctx->pass_len)
177           && EVP_DigestFinal_ex(mctx, key, NULL);
178     EVP_MD_CTX_free(mctx);
179     return res;
180 }
181
182 static int kdf_pvk_set_ctx_params(void *vctx, const OSSL_PARAM params[])
183 {
184     const OSSL_PARAM *p;
185     KDF_PVK *ctx = vctx;
186     OSSL_LIB_CTX *provctx = PROV_LIBCTX_OF(ctx->provctx);
187
188     if (params == NULL)
189         return 1;
190
191     if (!ossl_prov_digest_load_from_params(&ctx->digest, params, provctx))
192         return 0;
193
194     if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_PASSWORD)) != NULL)
195         if (!pvk_set_membuf(&ctx->pass, &ctx->pass_len, p))
196             return 0;
197
198     if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SALT)) != NULL) {
199         if (!pvk_set_membuf(&ctx->salt, &ctx->salt_len, p))
200             return 0;
201     }
202
203     return 1;
204 }
205
206 static const OSSL_PARAM *kdf_pvk_settable_ctx_params(ossl_unused void *ctx,
207                                                         ossl_unused void *p_ctx)
208 {
209     static const OSSL_PARAM known_settable_ctx_params[] = {
210         OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0),
211         OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, NULL, 0),
212         OSSL_PARAM_octet_string(OSSL_KDF_PARAM_PASSWORD, NULL, 0),
213         OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SALT, NULL, 0),
214         OSSL_PARAM_END
215     };
216     return known_settable_ctx_params;
217 }
218
219 static int kdf_pvk_get_ctx_params(void *vctx, OSSL_PARAM params[])
220 {
221     OSSL_PARAM *p;
222
223     if ((p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_SIZE)) != NULL)
224         return OSSL_PARAM_set_size_t(p, SIZE_MAX);
225     return -2;
226 }
227
228 static const OSSL_PARAM *kdf_pvk_gettable_ctx_params(ossl_unused void *ctx,
229                                                         ossl_unused void *p_ctx)
230 {
231     static const OSSL_PARAM known_gettable_ctx_params[] = {
232         OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL),
233         OSSL_PARAM_END
234     };
235     return known_gettable_ctx_params;
236 }
237
238 const OSSL_DISPATCH ossl_kdf_pvk_functions[] = {
239     { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))kdf_pvk_new },
240     { OSSL_FUNC_KDF_DUPCTX, (void(*)(void))kdf_pvk_dup },
241     { OSSL_FUNC_KDF_FREECTX, (void(*)(void))kdf_pvk_free },
242     { OSSL_FUNC_KDF_RESET, (void(*)(void))kdf_pvk_reset },
243     { OSSL_FUNC_KDF_DERIVE, (void(*)(void))kdf_pvk_derive },
244     { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,
245       (void(*)(void))kdf_pvk_settable_ctx_params },
246     { OSSL_FUNC_KDF_SET_CTX_PARAMS, (void(*)(void))kdf_pvk_set_ctx_params },
247     { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,
248       (void(*)(void))kdf_pvk_gettable_ctx_params },
249     { OSSL_FUNC_KDF_GET_CTX_PARAMS, (void(*)(void))kdf_pvk_get_ctx_params },
250     { 0, NULL }
251 };