xref: /freebsd/crypto/openssl/providers/implementations/kdfs/sshkdf.c (revision ba3c1f5972d7b90feb6e6da47905ff2757e0fe57)
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 <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     else
63         ctx->provctx = provctx;
64     return ctx;
65 }
66 
67 static void kdf_sshkdf_free(void *vctx)
68 {
69     KDF_SSHKDF *ctx = (KDF_SSHKDF *)vctx;
70 
71     if (ctx != NULL) {
72         kdf_sshkdf_reset(ctx);
73         OPENSSL_free(ctx);
74     }
75 }
76 
77 static void kdf_sshkdf_reset(void *vctx)
78 {
79     KDF_SSHKDF *ctx = (KDF_SSHKDF *)vctx;
80     void *provctx = ctx->provctx;
81 
82     ossl_prov_digest_reset(&ctx->digest);
83     OPENSSL_clear_free(ctx->key, ctx->key_len);
84     OPENSSL_clear_free(ctx->xcghash, ctx->xcghash_len);
85     OPENSSL_clear_free(ctx->session_id, ctx->session_id_len);
86     memset(ctx, 0, sizeof(*ctx));
87     ctx->provctx = provctx;
88 }
89 
90 static int sshkdf_set_membuf(unsigned char **dst, size_t *dst_len,
91                              const OSSL_PARAM *p)
92 {
93     OPENSSL_clear_free(*dst, *dst_len);
94     *dst = NULL;
95     *dst_len = 0;
96     return OSSL_PARAM_get_octet_string(p, (void **)dst, 0, dst_len);
97 }
98 
99 static int kdf_sshkdf_derive(void *vctx, unsigned char *key, size_t keylen,
100                              const OSSL_PARAM params[])
101 {
102     KDF_SSHKDF *ctx = (KDF_SSHKDF *)vctx;
103     const EVP_MD *md;
104 
105     if (!ossl_prov_is_running() || !kdf_sshkdf_set_ctx_params(ctx, params))
106         return 0;
107 
108     md = ossl_prov_digest_md(&ctx->digest);
109     if (md == NULL) {
110         ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST);
111         return 0;
112     }
113     if (ctx->key == NULL) {
114         ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_KEY);
115         return 0;
116     }
117     if (ctx->xcghash == NULL) {
118         ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_XCGHASH);
119         return 0;
120     }
121     if (ctx->session_id == NULL) {
122         ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SESSION_ID);
123         return 0;
124     }
125     if (ctx->type == 0) {
126         ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_TYPE);
127         return 0;
128     }
129     return SSHKDF(md, ctx->key, ctx->key_len,
130                   ctx->xcghash, ctx->xcghash_len,
131                   ctx->session_id, ctx->session_id_len,
132                   ctx->type, key, keylen);
133 }
134 
135 static int kdf_sshkdf_set_ctx_params(void *vctx, const OSSL_PARAM params[])
136 {
137     const OSSL_PARAM *p;
138     KDF_SSHKDF *ctx = vctx;
139     OSSL_LIB_CTX *provctx = PROV_LIBCTX_OF(ctx->provctx);
140 
141     if (params == NULL)
142         return 1;
143 
144     if (!ossl_prov_digest_load_from_params(&ctx->digest, params, provctx))
145         return 0;
146 
147     if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_KEY)) != NULL)
148         if (!sshkdf_set_membuf(&ctx->key, &ctx->key_len, p))
149             return 0;
150 
151     if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SSHKDF_XCGHASH))
152         != NULL)
153         if (!sshkdf_set_membuf(&ctx->xcghash, &ctx->xcghash_len, p))
154             return 0;
155 
156     if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SSHKDF_SESSION_ID))
157         != NULL)
158         if (!sshkdf_set_membuf(&ctx->session_id, &ctx->session_id_len, p))
159             return 0;
160 
161     if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SSHKDF_TYPE))
162         != NULL) {
163         const char *kdftype;
164 
165         if (!OSSL_PARAM_get_utf8_string_ptr(p, &kdftype))
166             return 0;
167         /* Expect one character (byte in this case) */
168         if (kdftype == NULL || p->data_size != 1)
169             return 0;
170         if (kdftype[0] < 65 || kdftype[0] > 70) {
171             ERR_raise(ERR_LIB_PROV, PROV_R_VALUE_ERROR);
172             return 0;
173         }
174         ctx->type = kdftype[0];
175     }
176     return 1;
177 }
178 
179 static const OSSL_PARAM *kdf_sshkdf_settable_ctx_params(ossl_unused void *ctx,
180                                                         ossl_unused void *p_ctx)
181 {
182     static const OSSL_PARAM known_settable_ctx_params[] = {
183         OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0),
184         OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, NULL, 0),
185         OSSL_PARAM_octet_string(OSSL_KDF_PARAM_KEY, NULL, 0),
186         OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SSHKDF_XCGHASH, NULL, 0),
187         OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SSHKDF_SESSION_ID, NULL, 0),
188         OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_SSHKDF_TYPE, NULL, 0),
189         OSSL_PARAM_END
190     };
191     return known_settable_ctx_params;
192 }
193 
194 static int kdf_sshkdf_get_ctx_params(void *vctx, OSSL_PARAM params[])
195 {
196     OSSL_PARAM *p;
197 
198     if ((p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_SIZE)) != NULL)
199         return OSSL_PARAM_set_size_t(p, SIZE_MAX);
200     return -2;
201 }
202 
203 static const OSSL_PARAM *kdf_sshkdf_gettable_ctx_params(ossl_unused void *ctx,
204                                                         ossl_unused void *p_ctx)
205 {
206     static const OSSL_PARAM known_gettable_ctx_params[] = {
207         OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL),
208         OSSL_PARAM_END
209     };
210     return known_gettable_ctx_params;
211 }
212 
213 const OSSL_DISPATCH ossl_kdf_sshkdf_functions[] = {
214     { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))kdf_sshkdf_new },
215     { OSSL_FUNC_KDF_FREECTX, (void(*)(void))kdf_sshkdf_free },
216     { OSSL_FUNC_KDF_RESET, (void(*)(void))kdf_sshkdf_reset },
217     { OSSL_FUNC_KDF_DERIVE, (void(*)(void))kdf_sshkdf_derive },
218     { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,
219       (void(*)(void))kdf_sshkdf_settable_ctx_params },
220     { OSSL_FUNC_KDF_SET_CTX_PARAMS, (void(*)(void))kdf_sshkdf_set_ctx_params },
221     { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,
222       (void(*)(void))kdf_sshkdf_gettable_ctx_params },
223     { OSSL_FUNC_KDF_GET_CTX_PARAMS, (void(*)(void))kdf_sshkdf_get_ctx_params },
224     { 0, NULL }
225 };
226 
227 static int SSHKDF(const EVP_MD *evp_md,
228                   const unsigned char *key, size_t key_len,
229                   const unsigned char *xcghash, size_t xcghash_len,
230                   const unsigned char *session_id, size_t session_id_len,
231                   char type, unsigned char *okey, size_t okey_len)
232 {
233     EVP_MD_CTX *md = NULL;
234     unsigned char digest[EVP_MAX_MD_SIZE];
235     unsigned int dsize = 0;
236     size_t cursize = 0;
237     int ret = 0;
238 
239     md = EVP_MD_CTX_new();
240     if (md == NULL)
241         return 0;
242 
243     if (!EVP_DigestInit_ex(md, evp_md, NULL))
244         goto out;
245 
246     if (!EVP_DigestUpdate(md, key, key_len))
247         goto out;
248 
249     if (!EVP_DigestUpdate(md, xcghash, xcghash_len))
250         goto out;
251 
252     if (!EVP_DigestUpdate(md, &type, 1))
253         goto out;
254 
255     if (!EVP_DigestUpdate(md, session_id, session_id_len))
256         goto out;
257 
258     if (!EVP_DigestFinal_ex(md, digest, &dsize))
259         goto out;
260 
261     if (okey_len < dsize) {
262         memcpy(okey, digest, okey_len);
263         ret = 1;
264         goto out;
265     }
266 
267     memcpy(okey, digest, dsize);
268 
269     for (cursize = dsize; cursize < okey_len; cursize += dsize) {
270 
271         if (!EVP_DigestInit_ex(md, evp_md, NULL))
272             goto out;
273 
274         if (!EVP_DigestUpdate(md, key, key_len))
275             goto out;
276 
277         if (!EVP_DigestUpdate(md, xcghash, xcghash_len))
278             goto out;
279 
280         if (!EVP_DigestUpdate(md, okey, cursize))
281             goto out;
282 
283         if (!EVP_DigestFinal_ex(md, digest, &dsize))
284             goto out;
285 
286         if (okey_len < cursize + dsize) {
287             memcpy(okey + cursize, digest, okey_len - cursize);
288             ret = 1;
289             goto out;
290         }
291 
292         memcpy(okey + cursize, digest, dsize);
293     }
294 
295     ret = 1;
296 
297 out:
298     EVP_MD_CTX_free(md);
299     OPENSSL_cleanse(digest, EVP_MAX_MD_SIZE);
300     return ret;
301 }
302 
303