prov: add extra params argument to KDF implementations
[openssl.git] / providers / implementations / kdfs / sshkdf.c
1 /*
2  * Copyright 2018-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 <stdlib.h>
11 #include <stdarg.h>
12 #include <string.h>
13 #include <openssl/evp.h>
14 #include <openssl/kdf.h>
15 #include <openssl/core_names.h>
16 #include <openssl/proverr.h>
17 #include "internal/cryptlib.h"
18 #include "internal/numbers.h"
19 #include "crypto/evp.h"
20 #include "prov/provider_ctx.h"
21 #include "prov/providercommon.h"
22 #include "prov/implementations.h"
23 #include "prov/provider_util.h"
24
25 /* See RFC 4253, Section 7.2 */
26 static OSSL_FUNC_kdf_newctx_fn kdf_sshkdf_new;
27 static OSSL_FUNC_kdf_freectx_fn kdf_sshkdf_free;
28 static OSSL_FUNC_kdf_reset_fn kdf_sshkdf_reset;
29 static OSSL_FUNC_kdf_derive_fn kdf_sshkdf_derive;
30 static OSSL_FUNC_kdf_settable_ctx_params_fn kdf_sshkdf_settable_ctx_params;
31 static OSSL_FUNC_kdf_set_ctx_params_fn kdf_sshkdf_set_ctx_params;
32 static OSSL_FUNC_kdf_gettable_ctx_params_fn kdf_sshkdf_gettable_ctx_params;
33 static OSSL_FUNC_kdf_get_ctx_params_fn kdf_sshkdf_get_ctx_params;
34
35 static int SSHKDF(const EVP_MD *evp_md,
36                   const unsigned char *key, size_t key_len,
37                   const unsigned char *xcghash, size_t xcghash_len,
38                   const unsigned char *session_id, size_t session_id_len,
39                   char type, unsigned char *okey, size_t okey_len);
40
41 typedef struct {
42     void *provctx;
43     PROV_DIGEST digest;
44     unsigned char *key; /* K */
45     size_t key_len;
46     unsigned char *xcghash; /* H */
47     size_t xcghash_len;
48     char type; /* X */
49     unsigned char *session_id;
50     size_t session_id_len;
51 } KDF_SSHKDF;
52
53 static void *kdf_sshkdf_new(void *provctx)
54 {
55     KDF_SSHKDF *ctx;
56
57     if (!ossl_prov_is_running())
58         return NULL;
59
60     if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL)
61         ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
62     ctx->provctx = provctx;
63     return ctx;
64 }
65
66 static void kdf_sshkdf_free(void *vctx)
67 {
68     KDF_SSHKDF *ctx = (KDF_SSHKDF *)vctx;
69
70     if (ctx != NULL) {
71         kdf_sshkdf_reset(ctx);
72         OPENSSL_free(ctx);
73     }
74 }
75
76 static void kdf_sshkdf_reset(void *vctx)
77 {
78     KDF_SSHKDF *ctx = (KDF_SSHKDF *)vctx;
79     void *provctx = ctx->provctx;
80
81     ossl_prov_digest_reset(&ctx->digest);
82     OPENSSL_clear_free(ctx->key, ctx->key_len);
83     OPENSSL_clear_free(ctx->xcghash, ctx->xcghash_len);
84     OPENSSL_clear_free(ctx->session_id, ctx->session_id_len);
85     memset(ctx, 0, sizeof(*ctx));
86     ctx->provctx = provctx;
87 }
88
89 static int sshkdf_set_membuf(unsigned char **dst, size_t *dst_len,
90                              const OSSL_PARAM *p)
91 {
92     OPENSSL_clear_free(*dst, *dst_len);
93     *dst = NULL;
94     return OSSL_PARAM_get_octet_string(p, (void **)dst, 0, dst_len);
95 }
96
97 static int kdf_sshkdf_derive(void *vctx, unsigned char *key, size_t keylen,
98                              const OSSL_PARAM params[])
99 {
100     KDF_SSHKDF *ctx = (KDF_SSHKDF *)vctx;
101     const EVP_MD *md;
102
103     if (!ossl_prov_is_running() || !kdf_sshkdf_set_ctx_params(ctx, params))
104         return 0;
105
106     md = ossl_prov_digest_md(&ctx->digest);
107     if (md == NULL) {
108         ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST);
109         return 0;
110     }
111     if (ctx->key == NULL) {
112         ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_KEY);
113         return 0;
114     }
115     if (ctx->xcghash == NULL) {
116         ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_XCGHASH);
117         return 0;
118     }
119     if (ctx->session_id == NULL) {
120         ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SESSION_ID);
121         return 0;
122     }
123     if (ctx->type == 0) {
124         ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_TYPE);
125         return 0;
126     }
127     return SSHKDF(md, ctx->key, ctx->key_len,
128                   ctx->xcghash, ctx->xcghash_len,
129                   ctx->session_id, ctx->session_id_len,
130                   ctx->type, key, keylen);
131 }
132
133 static int kdf_sshkdf_set_ctx_params(void *vctx, const OSSL_PARAM params[])
134 {
135     const OSSL_PARAM *p;
136     KDF_SSHKDF *ctx = vctx;
137     OSSL_LIB_CTX *provctx = PROV_LIBCTX_OF(ctx->provctx);
138
139     if (!ossl_prov_digest_load_from_params(&ctx->digest, params, provctx))
140         return 0;
141
142     if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_KEY)) != NULL)
143         if (!sshkdf_set_membuf(&ctx->key, &ctx->key_len, p))
144             return 0;
145
146     if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SSHKDF_XCGHASH))
147         != NULL)
148         if (!sshkdf_set_membuf(&ctx->xcghash, &ctx->xcghash_len, p))
149             return 0;
150
151     if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SSHKDF_SESSION_ID))
152         != NULL)
153         if (!sshkdf_set_membuf(&ctx->session_id, &ctx->session_id_len, p))
154             return 0;
155
156     if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SSHKDF_TYPE))
157         != NULL) {
158         const char *kdftype;
159
160         if (!OSSL_PARAM_get_utf8_string_ptr(p, &kdftype))
161             return 0;
162         /* Expect one character (byte in this case) */
163         if (kdftype == NULL || p->data_size != 1)
164             return 0;
165         if (kdftype[0] < 65 || kdftype[0] > 70) {
166             ERR_raise(ERR_LIB_PROV, PROV_R_VALUE_ERROR);
167             return 0;
168         }
169         ctx->type = kdftype[0];
170     }
171     return 1;
172 }
173
174 static const OSSL_PARAM *kdf_sshkdf_settable_ctx_params(ossl_unused void *ctx,
175                                                         ossl_unused void *p_ctx)
176 {
177     static const OSSL_PARAM known_settable_ctx_params[] = {
178         OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0),
179         OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, NULL, 0),
180         OSSL_PARAM_octet_string(OSSL_KDF_PARAM_KEY, NULL, 0),
181         OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SSHKDF_XCGHASH, NULL, 0),
182         OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SSHKDF_SESSION_ID, NULL, 0),
183         OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_SSHKDF_TYPE, NULL, 0),
184         OSSL_PARAM_END
185     };
186     return known_settable_ctx_params;
187 }
188
189 static int kdf_sshkdf_get_ctx_params(void *vctx, OSSL_PARAM params[])
190 {
191     OSSL_PARAM *p;
192
193     if ((p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_SIZE)) != NULL)
194         return OSSL_PARAM_set_size_t(p, SIZE_MAX);
195     return -2;
196 }
197
198 static const OSSL_PARAM *kdf_sshkdf_gettable_ctx_params(ossl_unused void *ctx,
199                                                         ossl_unused void *p_ctx)
200 {
201     static const OSSL_PARAM known_gettable_ctx_params[] = {
202         OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL),
203         OSSL_PARAM_END
204     };
205     return known_gettable_ctx_params;
206 }
207
208 const OSSL_DISPATCH ossl_kdf_sshkdf_functions[] = {
209     { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))kdf_sshkdf_new },
210     { OSSL_FUNC_KDF_FREECTX, (void(*)(void))kdf_sshkdf_free },
211     { OSSL_FUNC_KDF_RESET, (void(*)(void))kdf_sshkdf_reset },
212     { OSSL_FUNC_KDF_DERIVE, (void(*)(void))kdf_sshkdf_derive },
213     { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,
214       (void(*)(void))kdf_sshkdf_settable_ctx_params },
215     { OSSL_FUNC_KDF_SET_CTX_PARAMS, (void(*)(void))kdf_sshkdf_set_ctx_params },
216     { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,
217       (void(*)(void))kdf_sshkdf_gettable_ctx_params },
218     { OSSL_FUNC_KDF_GET_CTX_PARAMS, (void(*)(void))kdf_sshkdf_get_ctx_params },
219     { 0, NULL }
220 };
221
222 static int SSHKDF(const EVP_MD *evp_md,
223                   const unsigned char *key, size_t key_len,
224                   const unsigned char *xcghash, size_t xcghash_len,
225                   const unsigned char *session_id, size_t session_id_len,
226                   char type, unsigned char *okey, size_t okey_len)
227 {
228     EVP_MD_CTX *md = NULL;
229     unsigned char digest[EVP_MAX_MD_SIZE];
230     unsigned int dsize = 0;
231     size_t cursize = 0;
232     int ret = 0;
233
234     md = EVP_MD_CTX_new();
235     if (md == NULL)
236         return 0;
237
238     if (!EVP_DigestInit_ex(md, evp_md, NULL))
239         goto out;
240
241     if (!EVP_DigestUpdate(md, key, key_len))
242         goto out;
243
244     if (!EVP_DigestUpdate(md, xcghash, xcghash_len))
245         goto out;
246
247     if (!EVP_DigestUpdate(md, &type, 1))
248         goto out;
249
250     if (!EVP_DigestUpdate(md, session_id, session_id_len))
251         goto out;
252
253     if (!EVP_DigestFinal_ex(md, digest, &dsize))
254         goto out;
255
256     if (okey_len < dsize) {
257         memcpy(okey, digest, okey_len);
258         ret = 1;
259         goto out;
260     }
261
262     memcpy(okey, digest, dsize);
263
264     for (cursize = dsize; cursize < okey_len; cursize += dsize) {
265
266         if (!EVP_DigestInit_ex(md, evp_md, NULL))
267             goto out;
268
269         if (!EVP_DigestUpdate(md, key, key_len))
270             goto out;
271
272         if (!EVP_DigestUpdate(md, xcghash, xcghash_len))
273             goto out;
274
275         if (!EVP_DigestUpdate(md, okey, cursize))
276             goto out;
277
278         if (!EVP_DigestFinal_ex(md, digest, &dsize))
279             goto out;
280
281         if (okey_len < cursize + dsize) {
282             memcpy(okey + cursize, digest, okey_len - cursize);
283             ret = 1;
284             goto out;
285         }
286
287         memcpy(okey + cursize, digest, dsize);
288     }
289
290     ret = 1;
291
292 out:
293     EVP_MD_CTX_free(md);
294     OPENSSL_cleanse(digest, EVP_MAX_MD_SIZE);
295     return ret;
296 }
297