/src/openssl/providers/implementations/kdfs/sshkdf.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2018-2023 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 | | #include "prov/securitycheck.h" |
25 | | #include "prov/fipscommon.h" |
26 | | #include "prov/fipsindicator.h" |
27 | | |
28 | | /* See RFC 4253, Section 7.2 */ |
29 | | static OSSL_FUNC_kdf_newctx_fn kdf_sshkdf_new; |
30 | | static OSSL_FUNC_kdf_dupctx_fn kdf_sshkdf_dup; |
31 | | static OSSL_FUNC_kdf_freectx_fn kdf_sshkdf_free; |
32 | | static OSSL_FUNC_kdf_reset_fn kdf_sshkdf_reset; |
33 | | static OSSL_FUNC_kdf_derive_fn kdf_sshkdf_derive; |
34 | | static OSSL_FUNC_kdf_settable_ctx_params_fn kdf_sshkdf_settable_ctx_params; |
35 | | static OSSL_FUNC_kdf_set_ctx_params_fn kdf_sshkdf_set_ctx_params; |
36 | | static OSSL_FUNC_kdf_gettable_ctx_params_fn kdf_sshkdf_gettable_ctx_params; |
37 | | static OSSL_FUNC_kdf_get_ctx_params_fn kdf_sshkdf_get_ctx_params; |
38 | | |
39 | | static int SSHKDF(const EVP_MD *evp_md, |
40 | | const unsigned char *key, size_t key_len, |
41 | | const unsigned char *xcghash, size_t xcghash_len, |
42 | | const unsigned char *session_id, size_t session_id_len, |
43 | | char type, unsigned char *okey, size_t okey_len); |
44 | | |
45 | | typedef struct { |
46 | | void *provctx; |
47 | | PROV_DIGEST digest; |
48 | | unsigned char *key; /* K */ |
49 | | size_t key_len; |
50 | | unsigned char *xcghash; /* H */ |
51 | | size_t xcghash_len; |
52 | | char type; /* X */ |
53 | | unsigned char *session_id; |
54 | | size_t session_id_len; |
55 | | OSSL_FIPS_IND_DECLARE |
56 | | } KDF_SSHKDF; |
57 | | |
58 | | static void *kdf_sshkdf_new(void *provctx) |
59 | 0 | { |
60 | 0 | KDF_SSHKDF *ctx; |
61 | |
|
62 | 0 | if (!ossl_prov_is_running()) |
63 | 0 | return NULL; |
64 | | |
65 | 0 | if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) != NULL) { |
66 | 0 | ctx->provctx = provctx; |
67 | 0 | OSSL_FIPS_IND_INIT(ctx) |
68 | 0 | } |
69 | 0 | return ctx; |
70 | 0 | } |
71 | | |
72 | | static void kdf_sshkdf_free(void *vctx) |
73 | 0 | { |
74 | 0 | KDF_SSHKDF *ctx = (KDF_SSHKDF *)vctx; |
75 | |
|
76 | 0 | if (ctx != NULL) { |
77 | 0 | kdf_sshkdf_reset(ctx); |
78 | 0 | OPENSSL_free(ctx); |
79 | 0 | } |
80 | 0 | } |
81 | | |
82 | | static void kdf_sshkdf_reset(void *vctx) |
83 | 0 | { |
84 | 0 | KDF_SSHKDF *ctx = (KDF_SSHKDF *)vctx; |
85 | 0 | void *provctx = ctx->provctx; |
86 | |
|
87 | 0 | ossl_prov_digest_reset(&ctx->digest); |
88 | 0 | OPENSSL_clear_free(ctx->key, ctx->key_len); |
89 | 0 | OPENSSL_clear_free(ctx->xcghash, ctx->xcghash_len); |
90 | 0 | OPENSSL_clear_free(ctx->session_id, ctx->session_id_len); |
91 | 0 | memset(ctx, 0, sizeof(*ctx)); |
92 | 0 | ctx->provctx = provctx; |
93 | 0 | } |
94 | | |
95 | | static void *kdf_sshkdf_dup(void *vctx) |
96 | 0 | { |
97 | 0 | const KDF_SSHKDF *src = (const KDF_SSHKDF *)vctx; |
98 | 0 | KDF_SSHKDF *dest; |
99 | |
|
100 | 0 | dest = kdf_sshkdf_new(src->provctx); |
101 | 0 | if (dest != NULL) { |
102 | 0 | if (!ossl_prov_memdup(src->key, src->key_len, |
103 | 0 | &dest->key, &dest->key_len) |
104 | 0 | || !ossl_prov_memdup(src->xcghash, src->xcghash_len, |
105 | 0 | &dest->xcghash , &dest->xcghash_len) |
106 | 0 | || !ossl_prov_memdup(src->session_id, src->session_id_len, |
107 | 0 | &dest->session_id , &dest->session_id_len) |
108 | 0 | || !ossl_prov_digest_copy(&dest->digest, &src->digest)) |
109 | 0 | goto err; |
110 | 0 | dest->type = src->type; |
111 | 0 | OSSL_FIPS_IND_COPY(dest, src) |
112 | 0 | } |
113 | 0 | return dest; |
114 | | |
115 | 0 | err: |
116 | 0 | kdf_sshkdf_free(dest); |
117 | 0 | return NULL; |
118 | 0 | } |
119 | | |
120 | | static int sshkdf_set_membuf(unsigned char **dst, size_t *dst_len, |
121 | | const OSSL_PARAM *p) |
122 | 0 | { |
123 | 0 | OPENSSL_clear_free(*dst, *dst_len); |
124 | 0 | *dst = NULL; |
125 | 0 | *dst_len = 0; |
126 | 0 | return OSSL_PARAM_get_octet_string(p, (void **)dst, 0, dst_len); |
127 | 0 | } |
128 | | |
129 | | #ifdef FIPS_MODULE |
130 | | static int fips_digest_check_passed(KDF_SSHKDF *ctx, const EVP_MD *md) |
131 | | { |
132 | | OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx); |
133 | | /* |
134 | | * Perform digest check |
135 | | * |
136 | | * According to NIST SP 800-135r1 section 5.2, the valid hash functions are |
137 | | * specified in FIPS 180-3. ACVP also only lists the same set of hash |
138 | | * functions. |
139 | | */ |
140 | | int digest_unapproved = !EVP_MD_is_a(md, SN_sha1) |
141 | | && !EVP_MD_is_a(md, SN_sha224) |
142 | | && !EVP_MD_is_a(md, SN_sha256) |
143 | | && !EVP_MD_is_a(md, SN_sha384) |
144 | | && !EVP_MD_is_a(md, SN_sha512); |
145 | | |
146 | | if (digest_unapproved) { |
147 | | if (!OSSL_FIPS_IND_ON_UNAPPROVED(ctx, OSSL_FIPS_IND_SETTABLE0, |
148 | | libctx, "SSHKDF", "Digest", |
149 | | FIPS_sshkdf_digest_check)) { |
150 | | ERR_raise(ERR_LIB_PROV, PROV_R_DIGEST_NOT_ALLOWED); |
151 | | return 0; |
152 | | } |
153 | | } |
154 | | return 1; |
155 | | } |
156 | | #endif |
157 | | |
158 | | static int kdf_sshkdf_derive(void *vctx, unsigned char *key, size_t keylen, |
159 | | const OSSL_PARAM params[]) |
160 | 0 | { |
161 | 0 | KDF_SSHKDF *ctx = (KDF_SSHKDF *)vctx; |
162 | 0 | const EVP_MD *md; |
163 | |
|
164 | 0 | if (!ossl_prov_is_running() || !kdf_sshkdf_set_ctx_params(ctx, params)) |
165 | 0 | return 0; |
166 | | |
167 | 0 | md = ossl_prov_digest_md(&ctx->digest); |
168 | 0 | if (md == NULL) { |
169 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST); |
170 | 0 | return 0; |
171 | 0 | } |
172 | 0 | if (ctx->key == NULL) { |
173 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_KEY); |
174 | 0 | return 0; |
175 | 0 | } |
176 | 0 | if (ctx->xcghash == NULL) { |
177 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_XCGHASH); |
178 | 0 | return 0; |
179 | 0 | } |
180 | 0 | if (ctx->session_id == NULL) { |
181 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SESSION_ID); |
182 | 0 | return 0; |
183 | 0 | } |
184 | 0 | if (ctx->type == 0) { |
185 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_TYPE); |
186 | 0 | return 0; |
187 | 0 | } |
188 | | |
189 | 0 | return SSHKDF(md, ctx->key, ctx->key_len, |
190 | 0 | ctx->xcghash, ctx->xcghash_len, |
191 | 0 | ctx->session_id, ctx->session_id_len, |
192 | 0 | ctx->type, key, keylen); |
193 | 0 | } |
194 | | |
195 | | static int kdf_sshkdf_set_ctx_params(void *vctx, const OSSL_PARAM params[]) |
196 | 0 | { |
197 | 0 | const OSSL_PARAM *p; |
198 | 0 | KDF_SSHKDF *ctx = vctx; |
199 | 0 | OSSL_LIB_CTX *provctx = PROV_LIBCTX_OF(ctx->provctx); |
200 | |
|
201 | 0 | if (params == NULL) |
202 | 0 | return 1; |
203 | | |
204 | 0 | if (!OSSL_FIPS_IND_SET_CTX_PARAM(ctx, OSSL_FIPS_IND_SETTABLE0, params, |
205 | 0 | OSSL_KDF_PARAM_FIPS_DIGEST_CHECK)) |
206 | 0 | return 0; |
207 | | |
208 | 0 | if (OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_DIGEST) != NULL) { |
209 | 0 | const EVP_MD *md = NULL; |
210 | |
|
211 | 0 | if (!ossl_prov_digest_load_from_params(&ctx->digest, params, provctx)) |
212 | 0 | return 0; |
213 | | |
214 | 0 | md = ossl_prov_digest_md(&ctx->digest); |
215 | 0 | if ((EVP_MD_get_flags(md) & EVP_MD_FLAG_XOF) != 0) { |
216 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_XOF_DIGESTS_NOT_ALLOWED); |
217 | 0 | return 0; |
218 | 0 | } |
219 | |
|
220 | | #ifdef FIPS_MODULE |
221 | | if (!fips_digest_check_passed(ctx, md)) |
222 | | return 0; |
223 | | #endif |
224 | 0 | } |
225 | | |
226 | 0 | if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_KEY)) != NULL) |
227 | 0 | if (!sshkdf_set_membuf(&ctx->key, &ctx->key_len, p)) |
228 | 0 | return 0; |
229 | | |
230 | 0 | if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SSHKDF_XCGHASH)) |
231 | 0 | != NULL) |
232 | 0 | if (!sshkdf_set_membuf(&ctx->xcghash, &ctx->xcghash_len, p)) |
233 | 0 | return 0; |
234 | | |
235 | 0 | if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SSHKDF_SESSION_ID)) |
236 | 0 | != NULL) |
237 | 0 | if (!sshkdf_set_membuf(&ctx->session_id, &ctx->session_id_len, p)) |
238 | 0 | return 0; |
239 | | |
240 | 0 | if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SSHKDF_TYPE)) |
241 | 0 | != NULL) { |
242 | 0 | const char *kdftype; |
243 | |
|
244 | 0 | if (!OSSL_PARAM_get_utf8_string_ptr(p, &kdftype)) |
245 | 0 | return 0; |
246 | | /* Expect one character (byte in this case) */ |
247 | 0 | if (kdftype == NULL || p->data_size != 1) |
248 | 0 | return 0; |
249 | 0 | if (kdftype[0] < 65 || kdftype[0] > 70) { |
250 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_VALUE_ERROR); |
251 | 0 | return 0; |
252 | 0 | } |
253 | 0 | ctx->type = kdftype[0]; |
254 | 0 | } |
255 | 0 | return 1; |
256 | 0 | } |
257 | | |
258 | | static const OSSL_PARAM *kdf_sshkdf_settable_ctx_params(ossl_unused void *ctx, |
259 | | ossl_unused void *p_ctx) |
260 | 0 | { |
261 | 0 | static const OSSL_PARAM known_settable_ctx_params[] = { |
262 | 0 | OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0), |
263 | 0 | OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, NULL, 0), |
264 | 0 | OSSL_PARAM_octet_string(OSSL_KDF_PARAM_KEY, NULL, 0), |
265 | 0 | OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SSHKDF_XCGHASH, NULL, 0), |
266 | 0 | OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SSHKDF_SESSION_ID, NULL, 0), |
267 | 0 | OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_SSHKDF_TYPE, NULL, 0), |
268 | 0 | OSSL_FIPS_IND_SETTABLE_CTX_PARAM(OSSL_KDF_PARAM_FIPS_DIGEST_CHECK) |
269 | 0 | OSSL_PARAM_END |
270 | 0 | }; |
271 | 0 | return known_settable_ctx_params; |
272 | 0 | } |
273 | | |
274 | | static int kdf_sshkdf_get_ctx_params(void *vctx, OSSL_PARAM params[]) |
275 | 0 | { |
276 | 0 | OSSL_PARAM *p; |
277 | |
|
278 | 0 | if ((p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_SIZE)) != NULL) { |
279 | 0 | if (!OSSL_PARAM_set_size_t(p, SIZE_MAX)) |
280 | 0 | return 0; |
281 | 0 | } |
282 | 0 | if (!OSSL_FIPS_IND_GET_CTX_PARAM(((KDF_SSHKDF *)vctx), params)) |
283 | 0 | return 0; |
284 | 0 | return 1; |
285 | 0 | } |
286 | | |
287 | | static const OSSL_PARAM *kdf_sshkdf_gettable_ctx_params(ossl_unused void *ctx, |
288 | | ossl_unused void *p_ctx) |
289 | 0 | { |
290 | 0 | static const OSSL_PARAM known_gettable_ctx_params[] = { |
291 | 0 | OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL), |
292 | 0 | OSSL_FIPS_IND_GETTABLE_CTX_PARAM() |
293 | 0 | OSSL_PARAM_END |
294 | 0 | }; |
295 | 0 | return known_gettable_ctx_params; |
296 | 0 | } |
297 | | |
298 | | const OSSL_DISPATCH ossl_kdf_sshkdf_functions[] = { |
299 | | { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))kdf_sshkdf_new }, |
300 | | { OSSL_FUNC_KDF_DUPCTX, (void(*)(void))kdf_sshkdf_dup }, |
301 | | { OSSL_FUNC_KDF_FREECTX, (void(*)(void))kdf_sshkdf_free }, |
302 | | { OSSL_FUNC_KDF_RESET, (void(*)(void))kdf_sshkdf_reset }, |
303 | | { OSSL_FUNC_KDF_DERIVE, (void(*)(void))kdf_sshkdf_derive }, |
304 | | { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS, |
305 | | (void(*)(void))kdf_sshkdf_settable_ctx_params }, |
306 | | { OSSL_FUNC_KDF_SET_CTX_PARAMS, (void(*)(void))kdf_sshkdf_set_ctx_params }, |
307 | | { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS, |
308 | | (void(*)(void))kdf_sshkdf_gettable_ctx_params }, |
309 | | { OSSL_FUNC_KDF_GET_CTX_PARAMS, (void(*)(void))kdf_sshkdf_get_ctx_params }, |
310 | | OSSL_DISPATCH_END |
311 | | }; |
312 | | |
313 | | static int SSHKDF(const EVP_MD *evp_md, |
314 | | const unsigned char *key, size_t key_len, |
315 | | const unsigned char *xcghash, size_t xcghash_len, |
316 | | const unsigned char *session_id, size_t session_id_len, |
317 | | char type, unsigned char *okey, size_t okey_len) |
318 | 0 | { |
319 | 0 | EVP_MD_CTX *md = NULL; |
320 | 0 | unsigned char digest[EVP_MAX_MD_SIZE]; |
321 | 0 | unsigned int dsize = 0; |
322 | 0 | size_t cursize = 0; |
323 | 0 | int ret = 0; |
324 | |
|
325 | 0 | md = EVP_MD_CTX_new(); |
326 | 0 | if (md == NULL) |
327 | 0 | return 0; |
328 | | |
329 | 0 | if (!EVP_DigestInit_ex(md, evp_md, NULL)) |
330 | 0 | goto out; |
331 | | |
332 | 0 | if (!EVP_DigestUpdate(md, key, key_len)) |
333 | 0 | goto out; |
334 | | |
335 | 0 | if (!EVP_DigestUpdate(md, xcghash, xcghash_len)) |
336 | 0 | goto out; |
337 | | |
338 | 0 | if (!EVP_DigestUpdate(md, &type, 1)) |
339 | 0 | goto out; |
340 | | |
341 | 0 | if (!EVP_DigestUpdate(md, session_id, session_id_len)) |
342 | 0 | goto out; |
343 | | |
344 | 0 | if (!EVP_DigestFinal_ex(md, digest, &dsize)) |
345 | 0 | goto out; |
346 | | |
347 | 0 | if (okey_len < dsize) { |
348 | 0 | memcpy(okey, digest, okey_len); |
349 | 0 | ret = 1; |
350 | 0 | goto out; |
351 | 0 | } |
352 | | |
353 | 0 | memcpy(okey, digest, dsize); |
354 | |
|
355 | 0 | for (cursize = dsize; cursize < okey_len; cursize += dsize) { |
356 | |
|
357 | 0 | if (!EVP_DigestInit_ex(md, evp_md, NULL)) |
358 | 0 | goto out; |
359 | | |
360 | 0 | if (!EVP_DigestUpdate(md, key, key_len)) |
361 | 0 | goto out; |
362 | | |
363 | 0 | if (!EVP_DigestUpdate(md, xcghash, xcghash_len)) |
364 | 0 | goto out; |
365 | | |
366 | 0 | if (!EVP_DigestUpdate(md, okey, cursize)) |
367 | 0 | goto out; |
368 | | |
369 | 0 | if (!EVP_DigestFinal_ex(md, digest, &dsize)) |
370 | 0 | goto out; |
371 | | |
372 | 0 | if (okey_len < cursize + dsize) { |
373 | 0 | memcpy(okey + cursize, digest, okey_len - cursize); |
374 | 0 | ret = 1; |
375 | 0 | goto out; |
376 | 0 | } |
377 | | |
378 | 0 | memcpy(okey + cursize, digest, dsize); |
379 | 0 | } |
380 | | |
381 | 0 | ret = 1; |
382 | |
|
383 | 0 | out: |
384 | 0 | EVP_MD_CTX_free(md); |
385 | 0 | OPENSSL_cleanse(digest, EVP_MAX_MD_SIZE); |
386 | 0 | return ret; |
387 | 0 | } |