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