/src/openssl/providers/implementations/kdfs/hkdf.c
Line  | Count  | Source  | 
1  |  | /*  | 
2  |  |  * Copyright 2016-2025 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  |  | /*  | 
11  |  |  * HMAC low level APIs are deprecated for public use, but still ok for internal  | 
12  |  |  * use.  | 
13  |  |  */  | 
14  |  | #include "internal/deprecated.h"  | 
15  |  |  | 
16  |  | #include <stdlib.h>  | 
17  |  | #include <stdarg.h>  | 
18  |  | #include <string.h>  | 
19  |  | #include <openssl/hmac.h>  | 
20  |  | #include <openssl/evp.h>  | 
21  |  | #include <openssl/kdf.h>  | 
22  |  | #include <openssl/core_names.h>  | 
23  |  | #include <openssl/proverr.h>  | 
24  |  | #include "internal/cryptlib.h"  | 
25  |  | #include "internal/numbers.h"  | 
26  |  | #include "internal/packet.h"  | 
27  |  | #include "crypto/evp.h"  | 
28  |  | #include "prov/provider_ctx.h"  | 
29  |  | #include "prov/providercommon.h"  | 
30  |  | #include "prov/implementations.h"  | 
31  |  | #include "prov/provider_util.h"  | 
32  |  | #include "prov/securitycheck.h"  | 
33  |  | #include "internal/e_os.h"  | 
34  |  | #include "internal/params.h"  | 
35  |  | #include "internal/sizes.h"  | 
36  |  |  | 
37  |  | #define HKDF_MAXBUF 2048  | 
38  |  | #define HKDF_MAXINFO (32*1024)  | 
39  | 0  | #define HKDF_MAX_INFOS    5  | 
40  |  |  | 
41  |  | static OSSL_FUNC_kdf_newctx_fn kdf_hkdf_new;  | 
42  |  | static OSSL_FUNC_kdf_dupctx_fn kdf_hkdf_dup;  | 
43  |  | static OSSL_FUNC_kdf_freectx_fn kdf_hkdf_free;  | 
44  |  | static OSSL_FUNC_kdf_reset_fn kdf_hkdf_reset;  | 
45  |  | static OSSL_FUNC_kdf_derive_fn kdf_hkdf_derive;  | 
46  |  | static OSSL_FUNC_kdf_settable_ctx_params_fn kdf_hkdf_settable_ctx_params;  | 
47  |  | static OSSL_FUNC_kdf_set_ctx_params_fn kdf_hkdf_set_ctx_params;  | 
48  |  | static OSSL_FUNC_kdf_gettable_ctx_params_fn hkdf_gettable_ctx_params;  | 
49  |  | static OSSL_FUNC_kdf_get_ctx_params_fn hkdf_common_get_ctx_params;  | 
50  |  | static OSSL_FUNC_kdf_derive_fn kdf_tls1_3_derive;  | 
51  |  | static OSSL_FUNC_kdf_settable_ctx_params_fn kdf_tls1_3_settable_ctx_params;  | 
52  |  | static OSSL_FUNC_kdf_set_ctx_params_fn kdf_tls1_3_set_ctx_params;  | 
53  |  | static OSSL_FUNC_kdf_newctx_fn kdf_hkdf_sha256_new;  | 
54  |  | static OSSL_FUNC_kdf_newctx_fn kdf_hkdf_sha384_new;  | 
55  |  | static OSSL_FUNC_kdf_newctx_fn kdf_hkdf_sha512_new;  | 
56  |  | static OSSL_FUNC_kdf_settable_ctx_params_fn kdf_hkdf_fixed_digest_settable_ctx_params;  | 
57  |  | static OSSL_FUNC_kdf_set_ctx_params_fn kdf_hkdf_fixed_digest_set_ctx_params;  | 
58  |  |  | 
59  |  | static void *kdf_hkdf_fixed_digest_new(void *provctx, const char *digest);  | 
60  |  | static void kdf_hkdf_reset_ex(void *vctx, int on_free);  | 
61  |  |  | 
62  |  | static int HKDF(OSSL_LIB_CTX *libctx, const EVP_MD *evp_md,  | 
63  |  |                 const unsigned char *salt, size_t salt_len,  | 
64  |  |                 const unsigned char *key, size_t key_len,  | 
65  |  |                 const unsigned char *info, size_t info_len,  | 
66  |  |                 unsigned char *okm, size_t okm_len);  | 
67  |  | static int HKDF_Extract(OSSL_LIB_CTX *libctx, const EVP_MD *evp_md,  | 
68  |  |                         const unsigned char *salt, size_t salt_len,  | 
69  |  |                         const unsigned char *ikm, size_t ikm_len,  | 
70  |  |                         unsigned char *prk, size_t prk_len);  | 
71  |  | static int HKDF_Expand(const EVP_MD *evp_md,  | 
72  |  |                        const unsigned char *prk, size_t prk_len,  | 
73  |  |                        const unsigned char *info, size_t info_len,  | 
74  |  |                        unsigned char *okm, size_t okm_len);  | 
75  |  |  | 
76  |  | typedef struct { | 
77  |  |     void *provctx;  | 
78  |  |     int mode;  | 
79  |  |     PROV_DIGEST digest;  | 
80  |  |     unsigned char *salt;  | 
81  |  |     size_t salt_len;  | 
82  |  |     unsigned char *key;  | 
83  |  |     size_t key_len;  | 
84  |  |     unsigned char *prefix;  | 
85  |  |     size_t prefix_len;  | 
86  |  |     unsigned char *label;  | 
87  |  |     size_t label_len;  | 
88  |  |     unsigned char *data;  | 
89  |  |     size_t data_len;  | 
90  |  |     unsigned char *info;  | 
91  |  |     size_t info_len;  | 
92  |  |     int fixed_digest;  | 
93  |  |     OSSL_FIPS_IND_DECLARE  | 
94  |  | } KDF_HKDF;  | 
95  |  |  | 
96  |  | static void *kdf_hkdf_new(void *provctx)  | 
97  | 0  | { | 
98  | 0  |     KDF_HKDF *ctx;  | 
99  |  | 
  | 
100  | 0  |     if (!ossl_prov_is_running())  | 
101  | 0  |         return NULL;  | 
102  |  |  | 
103  | 0  |     if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) != NULL) { | 
104  | 0  |         ctx->provctx = provctx;  | 
105  | 0  |         OSSL_FIPS_IND_INIT(ctx)  | 
106  | 0  |     }  | 
107  | 0  |     return ctx;  | 
108  | 0  | }  | 
109  |  |  | 
110  |  | static void kdf_hkdf_free(void *vctx)  | 
111  | 0  | { | 
112  | 0  |     KDF_HKDF *ctx = (KDF_HKDF *)vctx;  | 
113  |  | 
  | 
114  | 0  |     if (ctx != NULL) { | 
115  | 0  |         kdf_hkdf_reset_ex(vctx, 1);  | 
116  | 0  |         OPENSSL_free(ctx);  | 
117  | 0  |     }  | 
118  | 0  | }  | 
119  |  |  | 
120  |  | static void kdf_hkdf_reset(void *vctx)  | 
121  | 0  | { | 
122  | 0  |     kdf_hkdf_reset_ex(vctx, 0);  | 
123  | 0  | }  | 
124  |  |  | 
125  |  | static void kdf_hkdf_reset_ex(void *vctx, int on_free)  | 
126  | 0  | { | 
127  | 0  |     KDF_HKDF *ctx = (KDF_HKDF *)vctx;  | 
128  | 0  |     void *provctx = ctx->provctx;  | 
129  | 0  |     int preserve_digest = on_free ? 0 : ctx->fixed_digest;  | 
130  | 0  |     PROV_DIGEST save_prov_digest = { 0 }; | 
131  |  |  | 
132  |  |     /* For fixed digests just save and restore the PROV_DIGEST object */  | 
133  | 0  |     if (preserve_digest)  | 
134  | 0  |         save_prov_digest = ctx->digest;  | 
135  | 0  |     else  | 
136  | 0  |         ossl_prov_digest_reset(&ctx->digest);  | 
137  |  | #ifdef OPENSSL_PEDANTIC_ZEROIZATION  | 
138  |  |     OPENSSL_clear_free(ctx->salt, ctx->salt_len);  | 
139  |  | #else  | 
140  | 0  |     OPENSSL_free(ctx->salt);  | 
141  | 0  | #endif  | 
142  | 0  |     OPENSSL_free(ctx->prefix);  | 
143  | 0  |     OPENSSL_free(ctx->label);  | 
144  | 0  |     OPENSSL_clear_free(ctx->data, ctx->data_len);  | 
145  | 0  |     OPENSSL_clear_free(ctx->key, ctx->key_len);  | 
146  | 0  |     OPENSSL_clear_free(ctx->info, ctx->info_len);  | 
147  | 0  |     memset(ctx, 0, sizeof(*ctx));  | 
148  | 0  |     ctx->provctx = provctx;  | 
149  | 0  |     if (preserve_digest) { | 
150  | 0  |         ctx->fixed_digest = preserve_digest;  | 
151  | 0  |         ctx->digest = save_prov_digest;  | 
152  | 0  |     }  | 
153  | 0  | }  | 
154  |  |  | 
155  |  | static void *kdf_hkdf_dup(void *vctx)  | 
156  | 0  | { | 
157  | 0  |     const KDF_HKDF *src = (const KDF_HKDF *)vctx;  | 
158  | 0  |     KDF_HKDF *dest;  | 
159  |  | 
  | 
160  | 0  |     dest = kdf_hkdf_new(src->provctx);  | 
161  | 0  |     if (dest != NULL) { | 
162  | 0  |         if (!ossl_prov_memdup(src->salt, src->salt_len, &dest->salt,  | 
163  | 0  |                               &dest->salt_len)  | 
164  | 0  |                 || !ossl_prov_memdup(src->key, src->key_len,  | 
165  | 0  |                                      &dest->key , &dest->key_len)  | 
166  | 0  |                 || !ossl_prov_memdup(src->prefix, src->prefix_len,  | 
167  | 0  |                                      &dest->prefix, &dest->prefix_len)  | 
168  | 0  |                 || !ossl_prov_memdup(src->label, src->label_len,  | 
169  | 0  |                                      &dest->label, &dest->label_len)  | 
170  | 0  |                 || !ossl_prov_memdup(src->data, src->data_len,  | 
171  | 0  |                                      &dest->data, &dest->data_len)  | 
172  | 0  |                 || !ossl_prov_memdup(src->info, src->info_len,  | 
173  | 0  |                                      &dest->info, &dest->info_len)  | 
174  | 0  |                 || !ossl_prov_digest_copy(&dest->digest, &src->digest))  | 
175  | 0  |             goto err;  | 
176  | 0  |         dest->mode = src->mode;  | 
177  | 0  |         dest->fixed_digest = src->fixed_digest;  | 
178  | 0  |         OSSL_FIPS_IND_COPY(dest, src)  | 
179  | 0  |     }  | 
180  | 0  |     return dest;  | 
181  |  |  | 
182  | 0  |  err:  | 
183  | 0  |     kdf_hkdf_free(dest);  | 
184  | 0  |     return NULL;  | 
185  | 0  | }  | 
186  |  |  | 
187  |  | static size_t kdf_hkdf_size(KDF_HKDF *ctx)  | 
188  | 0  | { | 
189  | 0  |     int sz;  | 
190  | 0  |     const EVP_MD *md = ossl_prov_digest_md(&ctx->digest);  | 
191  |  | 
  | 
192  | 0  |     if (ctx->mode != EVP_KDF_HKDF_MODE_EXTRACT_ONLY)  | 
193  | 0  |         return SIZE_MAX;  | 
194  |  |  | 
195  | 0  |     if (md == NULL) { | 
196  | 0  |         ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST);  | 
197  | 0  |         return 0;  | 
198  | 0  |     }  | 
199  | 0  |     sz = EVP_MD_get_size(md);  | 
200  | 0  |     if (sz <= 0)  | 
201  | 0  |         return 0;  | 
202  |  |  | 
203  | 0  |     return sz;  | 
204  | 0  | }  | 
205  |  |  | 
206  |  | #ifdef FIPS_MODULE  | 
207  |  | static int fips_hkdf_key_check_passed(KDF_HKDF *ctx)  | 
208  |  | { | 
209  |  |     OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx);  | 
210  |  |     int key_approved = ossl_kdf_check_key_size(ctx->key_len);  | 
211  |  |  | 
212  |  |     if (!key_approved) { | 
213  |  |         if (!OSSL_FIPS_IND_ON_UNAPPROVED(ctx, OSSL_FIPS_IND_SETTABLE0,  | 
214  |  |                                          libctx, "HKDF", "Key size",  | 
215  |  |                                          ossl_fips_config_hkdf_key_check)) { | 
216  |  |             ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);  | 
217  |  |             return 0;  | 
218  |  |         }  | 
219  |  |     }  | 
220  |  |     return 1;  | 
221  |  | }  | 
222  |  | #endif  | 
223  |  |  | 
224  |  | static int kdf_hkdf_derive(void *vctx, unsigned char *key, size_t keylen,  | 
225  |  |                            const OSSL_PARAM params[])  | 
226  | 0  | { | 
227  | 0  |     KDF_HKDF *ctx = (KDF_HKDF *)vctx;  | 
228  | 0  |     OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx);  | 
229  | 0  |     const EVP_MD *md;  | 
230  |  | 
  | 
231  | 0  |     if (!ossl_prov_is_running() || !kdf_hkdf_set_ctx_params(ctx, params))  | 
232  | 0  |         return 0;  | 
233  |  |  | 
234  | 0  |     md = ossl_prov_digest_md(&ctx->digest);  | 
235  | 0  |     if (md == NULL) { | 
236  | 0  |         ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST);  | 
237  | 0  |         return 0;  | 
238  | 0  |     }  | 
239  | 0  |     if (ctx->key == NULL) { | 
240  | 0  |         ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_KEY);  | 
241  | 0  |         return 0;  | 
242  | 0  |     }  | 
243  | 0  |     if (keylen == 0) { | 
244  | 0  |         ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);  | 
245  | 0  |         return 0;  | 
246  | 0  |     }  | 
247  |  |  | 
248  | 0  |     switch (ctx->mode) { | 
249  | 0  |     case EVP_KDF_HKDF_MODE_EXTRACT_AND_EXPAND:  | 
250  | 0  |     default:  | 
251  | 0  |         return HKDF(libctx, md, ctx->salt, ctx->salt_len,  | 
252  | 0  |                     ctx->key, ctx->key_len, ctx->info, ctx->info_len, key, keylen);  | 
253  |  |  | 
254  | 0  |     case EVP_KDF_HKDF_MODE_EXTRACT_ONLY:  | 
255  | 0  |         return HKDF_Extract(libctx, md, ctx->salt, ctx->salt_len,  | 
256  | 0  |                             ctx->key, ctx->key_len, key, keylen);  | 
257  |  |  | 
258  | 0  |     case EVP_KDF_HKDF_MODE_EXPAND_ONLY:  | 
259  | 0  |         return HKDF_Expand(md, ctx->key, ctx->key_len, ctx->info,  | 
260  | 0  |                            ctx->info_len, key, keylen);  | 
261  | 0  |     }  | 
262  | 0  | }  | 
263  |  |  | 
264  |  | struct hkdf_all_set_ctx_params_st { | 
265  |  |     OSSL_PARAM *mode;  | 
266  |  |     OSSL_PARAM *propq;  | 
267  |  |     OSSL_PARAM *engine;  | 
268  |  |     OSSL_PARAM *digest;  | 
269  |  |     OSSL_PARAM *key;  | 
270  |  |     OSSL_PARAM *salt;  | 
271  |  | #ifdef FIPS_MODULE  | 
272  |  |     OSSL_PARAM *ind_k;  | 
273  |  |     OSSL_PARAM *ind_d;  | 
274  |  | #endif  | 
275  |  |     OSSL_PARAM *prefix;  | 
276  |  |     OSSL_PARAM *label;  | 
277  |  |     OSSL_PARAM *data;  | 
278  |  |     OSSL_PARAM *info[HKDF_MAX_INFOS];  | 
279  |  |     int num_info;  | 
280  |  | };  | 
281  |  |  | 
282  |  | #define hkdf_set_ctx_params_st hkdf_all_set_ctx_params_st  | 
283  |  | #define hkdf_fixed_digest_set_ctx_params_st hkdf_all_set_ctx_params_st  | 
284  |  | #define kdf_tls1_3_set_ctx_params_st hkdf_all_set_ctx_params_st  | 
285  |  |  | 
286  |  | #include "providers/implementations/kdfs/hkdf.inc"  | 
287  |  |  | 
288  |  | static int hkdf_common_set_ctx_params  | 
289  |  |         (KDF_HKDF *ctx, struct hkdf_all_set_ctx_params_st *p)  | 
290  | 0  | { | 
291  | 0  |     OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx);  | 
292  | 0  |     int n;  | 
293  |  | 
  | 
294  | 0  |     if (p->digest != NULL) { | 
295  | 0  |         const EVP_MD *md = NULL;  | 
296  |  | 
  | 
297  | 0  |         if (!ossl_prov_digest_load(&ctx->digest, p->digest,  | 
298  | 0  |                                    p->propq, p->engine, libctx))  | 
299  | 0  |             return 0;  | 
300  |  |  | 
301  | 0  |         md = ossl_prov_digest_md(&ctx->digest);  | 
302  | 0  |         if (EVP_MD_xof(md)) { | 
303  | 0  |             ERR_raise(ERR_LIB_PROV, PROV_R_XOF_DIGESTS_NOT_ALLOWED);  | 
304  | 0  |             return 0;  | 
305  | 0  |         }  | 
306  | 0  |     }  | 
307  |  |  | 
308  | 0  |     if (p->mode != NULL) { | 
309  | 0  |         if (p->mode->data_type == OSSL_PARAM_UTF8_STRING) { | 
310  | 0  |             if (OPENSSL_strcasecmp(p->mode->data, "EXTRACT_AND_EXPAND") == 0) { | 
311  | 0  |                 ctx->mode = EVP_KDF_HKDF_MODE_EXTRACT_AND_EXPAND;  | 
312  | 0  |             } else if (OPENSSL_strcasecmp(p->mode->data, "EXTRACT_ONLY") == 0) { | 
313  | 0  |                 ctx->mode = EVP_KDF_HKDF_MODE_EXTRACT_ONLY;  | 
314  | 0  |             } else if (OPENSSL_strcasecmp(p->mode->data, "EXPAND_ONLY") == 0) { | 
315  | 0  |                 ctx->mode = EVP_KDF_HKDF_MODE_EXPAND_ONLY;  | 
316  | 0  |             } else { | 
317  | 0  |                 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_MODE);  | 
318  | 0  |                 return 0;  | 
319  | 0  |             }  | 
320  | 0  |         } else if (OSSL_PARAM_get_int(p->mode, &n)) { | 
321  | 0  |             if (n != EVP_KDF_HKDF_MODE_EXTRACT_AND_EXPAND  | 
322  | 0  |                 && n != EVP_KDF_HKDF_MODE_EXTRACT_ONLY  | 
323  | 0  |                 && n != EVP_KDF_HKDF_MODE_EXPAND_ONLY) { | 
324  | 0  |                 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_MODE);  | 
325  | 0  |                 return 0;  | 
326  | 0  |             }  | 
327  | 0  |             ctx->mode = n;  | 
328  | 0  |         } else { | 
329  | 0  |             ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_MODE);  | 
330  | 0  |             return 0;  | 
331  | 0  |         }  | 
332  | 0  |     }  | 
333  |  |  | 
334  | 0  |     if (p->key != NULL) { | 
335  | 0  |         OPENSSL_clear_free(ctx->key, ctx->key_len);  | 
336  | 0  |         ctx->key = NULL;  | 
337  | 0  |         if (!OSSL_PARAM_get_octet_string(p->key, (void **)&ctx->key, 0,  | 
338  | 0  |                                          &ctx->key_len))  | 
339  | 0  |             return 0;  | 
340  | 0  |     }  | 
341  |  |  | 
342  | 0  |     if (p->salt != NULL) { | 
343  | 0  |         OPENSSL_free(ctx->salt);  | 
344  | 0  |         ctx->salt = NULL;  | 
345  | 0  |         if (!OSSL_PARAM_get_octet_string(p->salt, (void **)&ctx->salt, 0,  | 
346  | 0  |                                          &ctx->salt_len))  | 
347  | 0  |             return 0;  | 
348  | 0  |     }  | 
349  |  |  | 
350  |  |     /* Only relevant for HKDF not to the TLS 1.3 KDF */  | 
351  | 0  |     if (ossl_param_get1_concat_octet_string(p->num_info, p->info,  | 
352  | 0  |                                         &ctx->info, &ctx->info_len) == 0)  | 
353  | 0  |         return 0;  | 
354  |  |  | 
355  | 0  |     return 1;  | 
356  | 0  | }  | 
357  |  |  | 
358  |  | static int kdf_hkdf_set_ctx_params(void *vctx, const OSSL_PARAM params[])  | 
359  | 0  | { | 
360  | 0  |     struct hkdf_all_set_ctx_params_st p;  | 
361  | 0  |     KDF_HKDF *ctx = vctx;  | 
362  |  | 
  | 
363  | 0  |     if (ctx == NULL || !hkdf_set_ctx_params_decoder(params, &p))  | 
364  | 0  |         return 0;  | 
365  |  |  | 
366  | 0  |     if (!OSSL_FIPS_IND_SET_CTX_FROM_PARAM(ctx, OSSL_FIPS_IND_SETTABLE0, p.ind_k))  | 
367  | 0  |         return 0;  | 
368  |  |  | 
369  | 0  |     if (!hkdf_common_set_ctx_params(ctx, &p))  | 
370  | 0  |         return 0;  | 
371  |  |  | 
372  |  | #ifdef FIPS_MODULE  | 
373  |  |     if (p.key != NULL)  | 
374  |  |         if (!fips_hkdf_key_check_passed(ctx))  | 
375  |  |             return 0;  | 
376  |  | #endif  | 
377  |  |  | 
378  | 0  |     return 1;  | 
379  | 0  | }  | 
380  |  |  | 
381  |  | static const OSSL_PARAM *kdf_hkdf_settable_ctx_params(ossl_unused void *ctx,  | 
382  |  |                                                       ossl_unused void *provctx)  | 
383  | 0  | { | 
384  | 0  |     return hkdf_set_ctx_params_list;  | 
385  | 0  | }  | 
386  |  |  | 
387  |  | static const OSSL_PARAM *hkdf_gettable_ctx_params(ossl_unused void *ctx,  | 
388  |  |                                                   ossl_unused void *provctx)  | 
389  | 0  | { | 
390  | 0  |     return hkdf_get_ctx_params_list;  | 
391  | 0  | }  | 
392  |  |  | 
393  |  | static int hkdf_common_get_ctx_params(void *vctx, OSSL_PARAM params[])  | 
394  | 0  | { | 
395  | 0  |     KDF_HKDF *ctx = (KDF_HKDF *)vctx;  | 
396  | 0  |     struct hkdf_get_ctx_params_st p;  | 
397  |  | 
  | 
398  | 0  |     if (ctx == NULL || !hkdf_get_ctx_params_decoder(params, &p))  | 
399  | 0  |         return 0;  | 
400  |  |  | 
401  | 0  |     if (p.size != NULL) { | 
402  | 0  |         size_t sz = kdf_hkdf_size(ctx);  | 
403  |  | 
  | 
404  | 0  |         if (sz == 0)  | 
405  | 0  |             return 0;  | 
406  | 0  |         if (!OSSL_PARAM_set_size_t(p.size, sz))  | 
407  | 0  |             return 0;  | 
408  | 0  |     }  | 
409  |  |  | 
410  | 0  |     if (p.digest != NULL) { | 
411  | 0  |         const EVP_MD *md = ossl_prov_digest_md(&ctx->digest);  | 
412  |  | 
  | 
413  | 0  |         if (md == NULL)  | 
414  | 0  |             return 0;  | 
415  | 0  |         else if (!OSSL_PARAM_set_utf8_string(p.digest, EVP_MD_get0_name(md)))  | 
416  | 0  |             return 0;  | 
417  | 0  |     }  | 
418  |  |  | 
419  |  |     /* OSSL_KDF_PARAM_MODE has multiple parameter types, so look for all instances */  | 
420  | 0  |     if (p.mode != NULL) { | 
421  | 0  |         if (p.mode->data_type == OSSL_PARAM_UTF8_STRING) { | 
422  | 0  |             switch (ctx->mode) { | 
423  | 0  |             case EVP_KDF_HKDF_MODE_EXTRACT_AND_EXPAND:  | 
424  | 0  |                 if (!OSSL_PARAM_set_utf8_string(p.mode, "EXTRACT_AND_EXPAND"))  | 
425  | 0  |                     return 0;  | 
426  | 0  |                 break;  | 
427  | 0  |             case EVP_KDF_HKDF_MODE_EXTRACT_ONLY:  | 
428  | 0  |                 if (!OSSL_PARAM_set_utf8_string(p.mode, "EXTRACT_ONLY"))  | 
429  | 0  |                     return 0;  | 
430  | 0  |                 break;  | 
431  | 0  |             case EVP_KDF_HKDF_MODE_EXPAND_ONLY:  | 
432  | 0  |                 if (!OSSL_PARAM_set_utf8_string(p.mode, "EXPAND_ONLY"))  | 
433  | 0  |                     return 0;  | 
434  | 0  |                 break;  | 
435  | 0  |             default:  | 
436  | 0  |                 return 0;  | 
437  | 0  |             }  | 
438  | 0  |         } else { | 
439  | 0  |             if (!OSSL_PARAM_set_int(p.mode, ctx->mode))  | 
440  | 0  |                 return 0;  | 
441  | 0  |         }  | 
442  | 0  |     }  | 
443  |  |  | 
444  | 0  |     if (p.salt != NULL) { | 
445  | 0  |         if (ctx->salt == NULL || ctx->salt_len == 0)  | 
446  | 0  |             p.salt->return_size = 0;  | 
447  | 0  |         else if (!OSSL_PARAM_set_octet_string(p.salt, ctx->salt, ctx->salt_len))  | 
448  | 0  |             return 0;  | 
449  | 0  |     }  | 
450  |  |  | 
451  | 0  |     if (p.info != NULL) { | 
452  | 0  |         if (ctx->info == NULL || ctx->info_len == 0)  | 
453  | 0  |             p.info->return_size = 0;  | 
454  | 0  |         else if (!OSSL_PARAM_set_octet_string(p.info, ctx->info, ctx->info_len))  | 
455  | 0  |             return 0;  | 
456  | 0  |     }  | 
457  |  |  | 
458  | 0  |     if (!OSSL_FIPS_IND_GET_CTX_FROM_PARAM(ctx, p.ind))  | 
459  | 0  |         return 0;  | 
460  |  |  | 
461  | 0  |     return 1;  | 
462  | 0  | }  | 
463  |  |  | 
464  |  | const OSSL_DISPATCH ossl_kdf_hkdf_functions[] = { | 
465  |  |     { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))kdf_hkdf_new }, | 
466  |  |     { OSSL_FUNC_KDF_DUPCTX, (void(*)(void))kdf_hkdf_dup }, | 
467  |  |     { OSSL_FUNC_KDF_FREECTX, (void(*)(void))kdf_hkdf_free }, | 
468  |  |     { OSSL_FUNC_KDF_RESET, (void(*)(void))kdf_hkdf_reset }, | 
469  |  |     { OSSL_FUNC_KDF_DERIVE, (void(*)(void))kdf_hkdf_derive }, | 
470  |  |     { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS, | 
471  |  |       (void(*)(void))kdf_hkdf_settable_ctx_params },  | 
472  |  |     { OSSL_FUNC_KDF_SET_CTX_PARAMS, (void(*)(void))kdf_hkdf_set_ctx_params }, | 
473  |  |     { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS, | 
474  |  |       (void(*)(void))hkdf_gettable_ctx_params },  | 
475  |  |     { OSSL_FUNC_KDF_GET_CTX_PARAMS, (void(*)(void))hkdf_common_get_ctx_params }, | 
476  |  |     OSSL_DISPATCH_END  | 
477  |  | };  | 
478  |  |  | 
479  |  | static void *kdf_hkdf_fixed_digest_new(void *provctx, const char *digest)  | 
480  | 0  | { | 
481  | 0  |     OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(provctx);  | 
482  | 0  |     KDF_HKDF *ctx;  | 
483  | 0  |     OSSL_PARAM param;  | 
484  |  | 
  | 
485  | 0  |     ctx = kdf_hkdf_new(provctx);  | 
486  | 0  |     if (ctx == NULL)  | 
487  | 0  |         return NULL;  | 
488  |  |  | 
489  | 0  |     param = OSSL_PARAM_construct_utf8_string(OSSL_ALG_PARAM_DIGEST,  | 
490  | 0  |                                              (char *)digest, 0);  | 
491  | 0  |     if (!ossl_prov_digest_load(&ctx->digest, ¶m, NULL, NULL, libctx)) { | 
492  | 0  |         kdf_hkdf_free(ctx);  | 
493  | 0  |         return NULL;  | 
494  | 0  |     }  | 
495  |  |  | 
496  |  |     /* Now the digest can no longer be changed */  | 
497  | 0  |     ctx->fixed_digest = 1;  | 
498  |  | 
  | 
499  | 0  |     return ctx;  | 
500  | 0  | }  | 
501  |  |  | 
502  |  | static int kdf_hkdf_fixed_digest_set_ctx_params(void *vctx, const OSSL_PARAM params[])  | 
503  | 0  | { | 
504  | 0  |     struct hkdf_all_set_ctx_params_st p;  | 
505  | 0  |     KDF_HKDF *ctx = vctx;  | 
506  |  | 
  | 
507  | 0  |     if (ctx == NULL || !hkdf_fixed_digest_set_ctx_params_decoder(params, &p))  | 
508  | 0  |         return 0;  | 
509  |  |  | 
510  | 0  |     if (p.digest != NULL) { | 
511  | 0  |         ERR_raise_data(ERR_LIB_PROV, PROV_R_DIGEST_NOT_ALLOWED,  | 
512  | 0  |                        "Setting the digest is not supported for fixed-digest HKDFs");  | 
513  | 0  |         return 0;  | 
514  | 0  |     }  | 
515  |  |  | 
516  | 0  |     if (!OSSL_FIPS_IND_SET_CTX_FROM_PARAM(ctx, OSSL_FIPS_IND_SETTABLE0, p.ind_k))  | 
517  | 0  |         return 0;  | 
518  |  |  | 
519  | 0  |     if (!hkdf_common_set_ctx_params(ctx, &p))  | 
520  | 0  |         return 0;  | 
521  |  |  | 
522  |  | #ifdef FIPS_MODULE  | 
523  |  |     if (p.key != NULL)  | 
524  |  |         if (!fips_hkdf_key_check_passed(ctx))  | 
525  |  |             return 0;  | 
526  |  | #endif  | 
527  |  |  | 
528  | 0  |     return 1;  | 
529  | 0  | }  | 
530  |  |  | 
531  |  | static const OSSL_PARAM *kdf_hkdf_fixed_digest_settable_ctx_params  | 
532  |  |         (ossl_unused void *ctx, ossl_unused void *provctx)  | 
533  | 0  | { | 
534  | 0  |     return hkdf_fixed_digest_set_ctx_params_list;  | 
535  | 0  | }  | 
536  |  |  | 
537  |  | #define KDF_HKDF_FIXED_DIGEST_NEW(hashname, hashstring) \  | 
538  |  |     static void *kdf_hkdf_##hashname##_new(void *provctx) \  | 
539  | 0  |     { \ | 
540  | 0  |         return kdf_hkdf_fixed_digest_new(provctx, hashstring); \  | 
541  | 0  |     } Unexecuted instantiation: hkdf.c:kdf_hkdf_sha256_new Unexecuted instantiation: hkdf.c:kdf_hkdf_sha384_new Unexecuted instantiation: hkdf.c:kdf_hkdf_sha512_new  | 
542  |  |  | 
543  |  | KDF_HKDF_FIXED_DIGEST_NEW(sha256, "SHA256")  | 
544  |  | KDF_HKDF_FIXED_DIGEST_NEW(sha384, "SHA384")  | 
545  |  | KDF_HKDF_FIXED_DIGEST_NEW(sha512, "SHA512")  | 
546  |  |  | 
547  |  | #define MAKE_KDF_HKDF_FIXED_DIGEST_FUNCTIONS(hashname) \  | 
548  |  |     const OSSL_DISPATCH ossl_kdf_hkdf_##hashname##_functions[] = { \ | 
549  |  |         { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))kdf_hkdf_##hashname##_new }, \ | 
550  |  |         { OSSL_FUNC_KDF_DUPCTX, (void(*)(void))kdf_hkdf_dup }, \ | 
551  |  |         { OSSL_FUNC_KDF_FREECTX, (void(*)(void))kdf_hkdf_free }, \ | 
552  |  |         { OSSL_FUNC_KDF_RESET, (void(*)(void))kdf_hkdf_reset }, \ | 
553  |  |         { OSSL_FUNC_KDF_DERIVE, (void(*)(void))kdf_hkdf_derive }, \ | 
554  |  |         { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS, (void(*)(void))kdf_hkdf_fixed_digest_settable_ctx_params }, \ | 
555  |  |         { OSSL_FUNC_KDF_SET_CTX_PARAMS, (void(*)(void))kdf_hkdf_fixed_digest_set_ctx_params }, \ | 
556  |  |         { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS, (void(*)(void))hkdf_gettable_ctx_params }, \ | 
557  |  |         { OSSL_FUNC_KDF_GET_CTX_PARAMS, (void(*)(void))hkdf_common_get_ctx_params }, \ | 
558  |  |         OSSL_DISPATCH_END \  | 
559  |  |     };  | 
560  |  |  | 
561  |  | MAKE_KDF_HKDF_FIXED_DIGEST_FUNCTIONS(sha256)  | 
562  |  | MAKE_KDF_HKDF_FIXED_DIGEST_FUNCTIONS(sha384)  | 
563  |  | MAKE_KDF_HKDF_FIXED_DIGEST_FUNCTIONS(sha512)  | 
564  |  |  | 
565  |  | /*  | 
566  |  |  * Refer to "HMAC-based Extract-and-Expand Key Derivation Function (HKDF)"  | 
567  |  |  * Section 2 (https://tools.ietf.org/html/rfc5869#section-2) and  | 
568  |  |  * "Cryptographic Extraction and Key Derivation: The HKDF Scheme"  | 
569  |  |  * Section 4.2 (https://eprint.iacr.org/2010/264.pdf).  | 
570  |  |  *  | 
571  |  |  * From the paper:  | 
572  |  |  *   The scheme HKDF is specified as:  | 
573  |  |  *     HKDF(XTS, SKM, CTXinfo, L) = K(1) | K(2) | ... | K(t)  | 
574  |  |  *  | 
575  |  |  *     where:  | 
576  |  |  *       SKM is source key material  | 
577  |  |  *       XTS is extractor salt (which may be null or constant)  | 
578  |  |  *       CTXinfo is context information (may be null)  | 
579  |  |  *       L is the number of key bits to be produced by KDF  | 
580  |  |  *       k is the output length in bits of the hash function used with HMAC  | 
581  |  |  *       t = ceil(L/k)  | 
582  |  |  *       the value K(t) is truncated to its first d = L mod k bits.  | 
583  |  |  *  | 
584  |  |  * From RFC 5869:  | 
585  |  |  *   2.2.  Step 1: Extract  | 
586  |  |  *     HKDF-Extract(salt, IKM) -> PRK  | 
587  |  |  *   2.3.  Step 2: Expand  | 
588  |  |  *     HKDF-Expand(PRK, info, L) -> OKM  | 
589  |  |  */  | 
590  |  | static int HKDF(OSSL_LIB_CTX *libctx, const EVP_MD *evp_md,  | 
591  |  |                 const unsigned char *salt, size_t salt_len,  | 
592  |  |                 const unsigned char *ikm, size_t ikm_len,  | 
593  |  |                 const unsigned char *info, size_t info_len,  | 
594  |  |                 unsigned char *okm, size_t okm_len)  | 
595  | 0  | { | 
596  | 0  |     unsigned char prk[EVP_MAX_MD_SIZE];  | 
597  | 0  |     int ret, sz;  | 
598  | 0  |     size_t prk_len;  | 
599  |  | 
  | 
600  | 0  |     sz = EVP_MD_get_size(evp_md);  | 
601  | 0  |     if (sz <= 0)  | 
602  | 0  |         return 0;  | 
603  | 0  |     prk_len = (size_t)sz;  | 
604  |  |  | 
605  |  |     /* Step 1: HKDF-Extract(salt, IKM) -> PRK */  | 
606  | 0  |     if (!HKDF_Extract(libctx, evp_md,  | 
607  | 0  |                       salt, salt_len, ikm, ikm_len, prk, prk_len))  | 
608  | 0  |         return 0;  | 
609  |  |  | 
610  |  |     /* Step 2: HKDF-Expand(PRK, info, L) -> OKM */  | 
611  | 0  |     ret = HKDF_Expand(evp_md, prk, prk_len, info, info_len, okm, okm_len);  | 
612  | 0  |     OPENSSL_cleanse(prk, sizeof(prk));  | 
613  |  | 
  | 
614  | 0  |     return ret;  | 
615  | 0  | }  | 
616  |  |  | 
617  |  | /*  | 
618  |  |  * Refer to "HMAC-based Extract-and-Expand Key Derivation Function (HKDF)"  | 
619  |  |  * Section 2.2 (https://tools.ietf.org/html/rfc5869#section-2.2).  | 
620  |  |  *  | 
621  |  |  * 2.2.  Step 1: Extract  | 
622  |  |  *  | 
623  |  |  *   HKDF-Extract(salt, IKM) -> PRK  | 
624  |  |  *  | 
625  |  |  *   Options:  | 
626  |  |  *      Hash     a hash function; HashLen denotes the length of the  | 
627  |  |  *               hash function output in octets  | 
628  |  |  *  | 
629  |  |  *   Inputs:  | 
630  |  |  *      salt     optional salt value (a non-secret random value);  | 
631  |  |  *               if not provided, it is set to a string of HashLen zeros.  | 
632  |  |  *      IKM      input keying material  | 
633  |  |  *  | 
634  |  |  *   Output:  | 
635  |  |  *      PRK      a pseudorandom key (of HashLen octets)  | 
636  |  |  *  | 
637  |  |  *   The output PRK is calculated as follows:  | 
638  |  |  *  | 
639  |  |  *   PRK = HMAC-Hash(salt, IKM)  | 
640  |  |  */  | 
641  |  | static int HKDF_Extract(OSSL_LIB_CTX *libctx, const EVP_MD *evp_md,  | 
642  |  |                         const unsigned char *salt, size_t salt_len,  | 
643  |  |                         const unsigned char *ikm, size_t ikm_len,  | 
644  |  |                         unsigned char *prk, size_t prk_len)  | 
645  | 0  | { | 
646  | 0  |     int sz = EVP_MD_get_size(evp_md);  | 
647  |  | 
  | 
648  | 0  |     if (sz <= 0)  | 
649  | 0  |         return 0;  | 
650  | 0  |     if (prk_len != (size_t)sz) { | 
651  | 0  |         ERR_raise(ERR_LIB_PROV, PROV_R_WRONG_OUTPUT_BUFFER_SIZE);  | 
652  | 0  |         return 0;  | 
653  | 0  |     }  | 
654  |  |     /* calc: PRK = HMAC-Hash(salt, IKM) */  | 
655  | 0  |     return  | 
656  | 0  |         EVP_Q_mac(libctx, "HMAC", NULL, EVP_MD_get0_name(evp_md), NULL, salt,  | 
657  | 0  |                   salt_len, ikm, ikm_len, prk, EVP_MD_get_size(evp_md), NULL)  | 
658  | 0  |         != NULL;  | 
659  | 0  | }  | 
660  |  |  | 
661  |  | /*  | 
662  |  |  * Refer to "HMAC-based Extract-and-Expand Key Derivation Function (HKDF)"  | 
663  |  |  * Section 2.3 (https://tools.ietf.org/html/rfc5869#section-2.3).  | 
664  |  |  *  | 
665  |  |  * 2.3.  Step 2: Expand  | 
666  |  |  *  | 
667  |  |  *   HKDF-Expand(PRK, info, L) -> OKM  | 
668  |  |  *  | 
669  |  |  *   Options:  | 
670  |  |  *      Hash     a hash function; HashLen denotes the length of the  | 
671  |  |  *               hash function output in octets  | 
672  |  |  *  | 
673  |  |  *   Inputs:  | 
674  |  |  *      PRK      a pseudorandom key of at least HashLen octets  | 
675  |  |  *               (usually, the output from the extract step)  | 
676  |  |  *      info     optional context and application specific information  | 
677  |  |  *               (can be a zero-length string)  | 
678  |  |  *      L        length of output keying material in octets  | 
679  |  |  *               (<= 255*HashLen)  | 
680  |  |  *  | 
681  |  |  *   Output:  | 
682  |  |  *      OKM      output keying material (of L octets)  | 
683  |  |  *  | 
684  |  |  *   The output OKM is calculated as follows:  | 
685  |  |  *  | 
686  |  |  *   N = ceil(L/HashLen)  | 
687  |  |  *   T = T(1) | T(2) | T(3) | ... | T(N)  | 
688  |  |  *   OKM = first L octets of T  | 
689  |  |  *  | 
690  |  |  *   where:  | 
691  |  |  *   T(0) = empty string (zero length)  | 
692  |  |  *   T(1) = HMAC-Hash(PRK, T(0) | info | 0x01)  | 
693  |  |  *   T(2) = HMAC-Hash(PRK, T(1) | info | 0x02)  | 
694  |  |  *   T(3) = HMAC-Hash(PRK, T(2) | info | 0x03)  | 
695  |  |  *   ...  | 
696  |  |  *  | 
697  |  |  *   (where the constant concatenated to the end of each T(n) is a  | 
698  |  |  *   single octet.)  | 
699  |  |  */  | 
700  |  | static int HKDF_Expand(const EVP_MD *evp_md,  | 
701  |  |                        const unsigned char *prk, size_t prk_len,  | 
702  |  |                        const unsigned char *info, size_t info_len,  | 
703  |  |                        unsigned char *okm, size_t okm_len)  | 
704  | 0  | { | 
705  | 0  |     HMAC_CTX *hmac;  | 
706  | 0  |     int ret = 0, sz;  | 
707  | 0  |     unsigned int i;  | 
708  | 0  |     unsigned char prev[EVP_MAX_MD_SIZE];  | 
709  | 0  |     size_t done_len = 0, dig_len, n;  | 
710  |  | 
  | 
711  | 0  |     sz = EVP_MD_get_size(evp_md);  | 
712  | 0  |     if (sz <= 0)  | 
713  | 0  |         return 0;  | 
714  | 0  |     dig_len = (size_t)sz;  | 
715  |  |  | 
716  |  |     /* calc: N = ceil(L/HashLen) */  | 
717  | 0  |     n = okm_len / dig_len;  | 
718  | 0  |     if (okm_len % dig_len)  | 
719  | 0  |         n++;  | 
720  |  | 
  | 
721  | 0  |     if (n > 255 || okm == NULL)  | 
722  | 0  |         return 0;  | 
723  |  |  | 
724  | 0  |     if ((hmac = HMAC_CTX_new()) == NULL)  | 
725  | 0  |         return 0;  | 
726  |  |  | 
727  | 0  |     if (!HMAC_Init_ex(hmac, prk, (int)prk_len, evp_md, NULL))  | 
728  | 0  |         goto err;  | 
729  |  |  | 
730  | 0  |     for (i = 1; i <= n; i++) { | 
731  | 0  |         size_t copy_len;  | 
732  | 0  |         const unsigned char ctr = i;  | 
733  |  |  | 
734  |  |         /* calc: T(i) = HMAC-Hash(PRK, T(i - 1) | info | i) */  | 
735  | 0  |         if (i > 1) { | 
736  | 0  |             if (!HMAC_Init_ex(hmac, NULL, 0, NULL, NULL))  | 
737  | 0  |                 goto err;  | 
738  |  |  | 
739  | 0  |             if (!HMAC_Update(hmac, prev, dig_len))  | 
740  | 0  |                 goto err;  | 
741  | 0  |         }  | 
742  |  |  | 
743  | 0  |         if (!HMAC_Update(hmac, info, info_len))  | 
744  | 0  |             goto err;  | 
745  |  |  | 
746  | 0  |         if (!HMAC_Update(hmac, &ctr, 1))  | 
747  | 0  |             goto err;  | 
748  |  |  | 
749  | 0  |         if (!HMAC_Final(hmac, prev, NULL))  | 
750  | 0  |             goto err;  | 
751  |  |  | 
752  | 0  |         copy_len = (dig_len > okm_len - done_len) ?  | 
753  | 0  |                        okm_len - done_len :  | 
754  | 0  |                        dig_len;  | 
755  |  | 
  | 
756  | 0  |         memcpy(okm + done_len, prev, copy_len);  | 
757  |  | 
  | 
758  | 0  |         done_len += copy_len;  | 
759  | 0  |     }  | 
760  | 0  |     ret = 1;  | 
761  |  | 
  | 
762  | 0  |  err:  | 
763  | 0  |     OPENSSL_cleanse(prev, sizeof(prev));  | 
764  | 0  |     HMAC_CTX_free(hmac);  | 
765  | 0  |     return ret;  | 
766  | 0  | }  | 
767  |  |  | 
768  |  | /*  | 
769  |  |  * TLS uses slight variations of the above and for FIPS validation purposes,  | 
770  |  |  * they need to be present here.  | 
771  |  |  * Refer to RFC 8446 section 7 for specific details.  | 
772  |  |  */  | 
773  |  |  | 
774  |  | /*  | 
775  |  |  * Given a |secret|; a |label| of length |labellen|; and |data| of length  | 
776  |  |  * |datalen| (e.g. typically a hash of the handshake messages), derive a new  | 
777  |  |  * secret |outlen| bytes long and store it in the location pointed to be |out|.  | 
778  |  |  * The |data| value may be zero length. Returns 1 on success and 0 on failure.  | 
779  |  |  */  | 
780  |  | static int prov_tls13_hkdf_expand(const EVP_MD *md,  | 
781  |  |                                   const unsigned char *key, size_t keylen,  | 
782  |  |                                   const unsigned char *prefix, size_t prefixlen,  | 
783  |  |                                   const unsigned char *label, size_t labellen,  | 
784  |  |                                   const unsigned char *data, size_t datalen,  | 
785  |  |                                   unsigned char *out, size_t outlen)  | 
786  | 0  | { | 
787  | 0  |     size_t hkdflabellen;  | 
788  | 0  |     unsigned char hkdflabel[HKDF_MAXBUF];  | 
789  | 0  |     WPACKET pkt;  | 
790  |  |  | 
791  |  |     /*  | 
792  |  |      * 2 bytes for length of derived secret + 1 byte for length of combined  | 
793  |  |      * prefix and label + bytes for the label itself + 1 byte length of hash  | 
794  |  |      * + bytes for the hash itself.  We've got the maximum the KDF can handle  | 
795  |  |      * which should always be sufficient.  | 
796  |  |      */  | 
797  | 0  |     if (!WPACKET_init_static_len(&pkt, hkdflabel, sizeof(hkdflabel), 0)  | 
798  | 0  |             || !WPACKET_put_bytes_u16(&pkt, outlen)  | 
799  | 0  |             || !WPACKET_start_sub_packet_u8(&pkt)  | 
800  | 0  |             || !WPACKET_memcpy(&pkt, prefix, prefixlen)  | 
801  | 0  |             || !WPACKET_memcpy(&pkt, label, labellen)  | 
802  | 0  |             || !WPACKET_close(&pkt)  | 
803  | 0  |             || !WPACKET_sub_memcpy_u8(&pkt, data, (data == NULL) ? 0 : datalen)  | 
804  | 0  |             || !WPACKET_get_total_written(&pkt, &hkdflabellen)  | 
805  | 0  |             || !WPACKET_finish(&pkt)) { | 
806  | 0  |         WPACKET_cleanup(&pkt);  | 
807  | 0  |         return 0;  | 
808  | 0  |     }  | 
809  |  |  | 
810  | 0  |     return HKDF_Expand(md, key, keylen, hkdflabel, hkdflabellen,  | 
811  | 0  |                        out, outlen);  | 
812  | 0  | }  | 
813  |  |  | 
814  |  | static int prov_tls13_hkdf_generate_secret(OSSL_LIB_CTX *libctx,  | 
815  |  |                                            const EVP_MD *md,  | 
816  |  |                                            const unsigned char *prevsecret,  | 
817  |  |                                            size_t prevsecretlen,  | 
818  |  |                                            const unsigned char *insecret,  | 
819  |  |                                            size_t insecretlen,  | 
820  |  |                                            const unsigned char *prefix,  | 
821  |  |                                            size_t prefixlen,  | 
822  |  |                                            const unsigned char *label,  | 
823  |  |                                            size_t labellen,  | 
824  |  |                                            unsigned char *out, size_t outlen)  | 
825  | 0  | { | 
826  | 0  |     size_t mdlen;  | 
827  | 0  |     int ret;  | 
828  | 0  |     unsigned char preextractsec[EVP_MAX_MD_SIZE];  | 
829  |  |     /* Always filled with zeros */  | 
830  | 0  |     static const unsigned char default_zeros[EVP_MAX_MD_SIZE];  | 
831  |  | 
  | 
832  | 0  |     ret = EVP_MD_get_size(md);  | 
833  |  |     /* Ensure cast to size_t is safe */  | 
834  | 0  |     if (ret <= 0)  | 
835  | 0  |         return 0;  | 
836  | 0  |     mdlen = (size_t)ret;  | 
837  |  | 
  | 
838  | 0  |     if (insecret == NULL) { | 
839  | 0  |         insecret = default_zeros;  | 
840  | 0  |         insecretlen = mdlen;  | 
841  | 0  |     }  | 
842  | 0  |     if (prevsecret == NULL) { | 
843  | 0  |         prevsecret = default_zeros;  | 
844  | 0  |         prevsecretlen = mdlen;  | 
845  | 0  |     } else { | 
846  | 0  |         EVP_MD_CTX *mctx = EVP_MD_CTX_new();  | 
847  | 0  |         unsigned char hash[EVP_MAX_MD_SIZE];  | 
848  |  |  | 
849  |  |         /* The pre-extract derive step uses a hash of no messages */  | 
850  | 0  |         if (mctx == NULL  | 
851  | 0  |                 || EVP_DigestInit_ex(mctx, md, NULL) <= 0  | 
852  | 0  |                 || EVP_DigestFinal_ex(mctx, hash, NULL) <= 0) { | 
853  | 0  |             EVP_MD_CTX_free(mctx);  | 
854  | 0  |             return 0;  | 
855  | 0  |         }  | 
856  | 0  |         EVP_MD_CTX_free(mctx);  | 
857  |  |  | 
858  |  |         /* Generate the pre-extract secret */  | 
859  | 0  |         if (!prov_tls13_hkdf_expand(md, prevsecret, prevsecretlen,  | 
860  | 0  |                                     prefix, prefixlen, label, labellen,  | 
861  | 0  |                                     hash, mdlen, preextractsec, mdlen))  | 
862  | 0  |             return 0;  | 
863  | 0  |         prevsecret = preextractsec;  | 
864  | 0  |         prevsecretlen = mdlen;  | 
865  | 0  |     }  | 
866  |  |  | 
867  | 0  |     ret = HKDF_Extract(libctx, md, prevsecret, prevsecretlen,  | 
868  | 0  |                        insecret, insecretlen, out, outlen);  | 
869  |  | 
  | 
870  | 0  |     if (prevsecret == preextractsec)  | 
871  | 0  |         OPENSSL_cleanse(preextractsec, mdlen);  | 
872  | 0  |     return ret;  | 
873  | 0  | }  | 
874  |  |  | 
875  |  | #ifdef FIPS_MODULE  | 
876  |  | static int fips_tls1_3_digest_check_passed(KDF_HKDF *ctx, const EVP_MD *md)  | 
877  |  | { | 
878  |  |     OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx);  | 
879  |  |     /*  | 
880  |  |      * Perform digest check  | 
881  |  |      *  | 
882  |  |      * According to RFC 8446 appendix B.4, the valid hash functions are  | 
883  |  |      * specified in FIPS 180-4. However, it only lists SHA2-256 and SHA2-384 in  | 
884  |  |      * the table. ACVP also only lists the same set of hash functions.  | 
885  |  |      */  | 
886  |  |     int digest_unapproved = !EVP_MD_is_a(md, SN_sha256)  | 
887  |  |         && !EVP_MD_is_a(md, SN_sha384);  | 
888  |  |  | 
889  |  |     if (digest_unapproved) { | 
890  |  |         if (!OSSL_FIPS_IND_ON_UNAPPROVED(ctx, OSSL_FIPS_IND_SETTABLE0,  | 
891  |  |                                          libctx, "TLS13 KDF", "Digest",  | 
892  |  |                                          ossl_fips_config_tls13_kdf_digest_check)) { | 
893  |  |             ERR_raise(ERR_LIB_PROV, PROV_R_DIGEST_NOT_ALLOWED);  | 
894  |  |             return 0;  | 
895  |  |         }  | 
896  |  |     }  | 
897  |  |     return 1;  | 
898  |  | }  | 
899  |  |  | 
900  |  | /*  | 
901  |  |  * Calculate the correct length of the secret key.  | 
902  |  |  *  | 
903  |  |  * RFC 8446:  | 
904  |  |  *   If a given secret is not available, then the 0-value consisting of a  | 
905  |  |  *   string of Hash.length bytes set to zeros is used.  | 
906  |  |  */  | 
907  |  | static size_t fips_tls1_3_key_size(KDF_HKDF *ctx)  | 
908  |  | { | 
909  |  |     const EVP_MD *md = ossl_prov_digest_md(&ctx->digest);  | 
910  |  |     size_t key_size = 0;  | 
911  |  |  | 
912  |  |     if (ctx->key != NULL)  | 
913  |  |         key_size = ctx->key_len;  | 
914  |  |     else if (md != NULL)  | 
915  |  |         key_size = EVP_MD_size(md);  | 
916  |  |  | 
917  |  |     return key_size;  | 
918  |  | }  | 
919  |  |  | 
920  |  | static int fips_tls1_3_key_check_passed(KDF_HKDF *ctx)  | 
921  |  | { | 
922  |  |     OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx);  | 
923  |  |     int key_approved = ossl_kdf_check_key_size(fips_tls1_3_key_size(ctx));  | 
924  |  |  | 
925  |  |     if (!key_approved) { | 
926  |  |         if (!OSSL_FIPS_IND_ON_UNAPPROVED(ctx, OSSL_FIPS_IND_SETTABLE1,  | 
927  |  |                                          libctx, "TLS13 KDF", "Key size",  | 
928  |  |                                          ossl_fips_config_tls13_kdf_key_check)) { | 
929  |  |             ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);  | 
930  |  |             return 0;  | 
931  |  |         }  | 
932  |  |     }  | 
933  |  |     return 1;  | 
934  |  | }  | 
935  |  | #endif  | 
936  |  |  | 
937  |  | static int kdf_tls1_3_derive(void *vctx, unsigned char *key, size_t keylen,  | 
938  |  |                              const OSSL_PARAM params[])  | 
939  | 0  | { | 
940  | 0  |     KDF_HKDF *ctx = (KDF_HKDF *)vctx;  | 
941  | 0  |     const EVP_MD *md;  | 
942  |  | 
  | 
943  | 0  |     if (!ossl_prov_is_running() || !kdf_tls1_3_set_ctx_params(ctx, params))  | 
944  | 0  |         return 0;  | 
945  |  |  | 
946  | 0  |     md = ossl_prov_digest_md(&ctx->digest);  | 
947  | 0  |     if (md == NULL) { | 
948  | 0  |         ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST);  | 
949  | 0  |         return 0;  | 
950  | 0  |     }  | 
951  |  |  | 
952  | 0  |     switch (ctx->mode) { | 
953  | 0  |     default:  | 
954  | 0  |         return 0;  | 
955  |  |  | 
956  | 0  |     case EVP_KDF_HKDF_MODE_EXTRACT_ONLY:  | 
957  | 0  |         return prov_tls13_hkdf_generate_secret(PROV_LIBCTX_OF(ctx->provctx),  | 
958  | 0  |                                                md,  | 
959  | 0  |                                                ctx->salt, ctx->salt_len,  | 
960  | 0  |                                                ctx->key, ctx->key_len,  | 
961  | 0  |                                                ctx->prefix, ctx->prefix_len,  | 
962  | 0  |                                                ctx->label, ctx->label_len,  | 
963  | 0  |                                                key, keylen);  | 
964  |  |  | 
965  | 0  |     case EVP_KDF_HKDF_MODE_EXPAND_ONLY:  | 
966  | 0  |         return prov_tls13_hkdf_expand(md, ctx->key, ctx->key_len,  | 
967  | 0  |                                       ctx->prefix, ctx->prefix_len,  | 
968  | 0  |                                       ctx->label, ctx->label_len,  | 
969  | 0  |                                       ctx->data, ctx->data_len,  | 
970  | 0  |                                       key, keylen);  | 
971  | 0  |     }  | 
972  | 0  | }  | 
973  |  |  | 
974  |  | static int kdf_tls1_3_set_ctx_params(void *vctx, const OSSL_PARAM params[])  | 
975  | 0  | { | 
976  | 0  |     struct hkdf_all_set_ctx_params_st p;  | 
977  | 0  |     KDF_HKDF *ctx = vctx;  | 
978  |  | 
  | 
979  | 0  |     if (ctx == NULL || !kdf_tls1_3_set_ctx_params_decoder(params, &p))  | 
980  | 0  |         return 0;  | 
981  |  |  | 
982  | 0  |     if (!OSSL_FIPS_IND_SET_CTX_FROM_PARAM(ctx, OSSL_FIPS_IND_SETTABLE0, p.ind_d))  | 
983  | 0  |         return 0;  | 
984  | 0  |     if (!OSSL_FIPS_IND_SET_CTX_FROM_PARAM(ctx, OSSL_FIPS_IND_SETTABLE1, p.ind_k))  | 
985  | 0  |         return 0;  | 
986  |  |  | 
987  | 0  |     if (!hkdf_common_set_ctx_params(ctx, &p))  | 
988  | 0  |         return 0;  | 
989  |  |  | 
990  | 0  |     if (ctx->mode == EVP_KDF_HKDF_MODE_EXTRACT_AND_EXPAND) { | 
991  | 0  |         ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_MODE);  | 
992  | 0  |         return 0;  | 
993  | 0  |     }  | 
994  |  |  | 
995  | 0  |     if (p.prefix != NULL) { | 
996  | 0  |         OPENSSL_free(ctx->prefix);  | 
997  | 0  |         ctx->prefix = NULL;  | 
998  | 0  |         if (!OSSL_PARAM_get_octet_string(p.prefix, (void **)&ctx->prefix, 0,  | 
999  | 0  |                                          &ctx->prefix_len))  | 
1000  | 0  |             return 0;  | 
1001  | 0  |     }  | 
1002  |  |  | 
1003  | 0  |     if (p.label != NULL) { | 
1004  | 0  |         OPENSSL_free(ctx->label);  | 
1005  | 0  |         ctx->label = NULL;  | 
1006  | 0  |         if (!OSSL_PARAM_get_octet_string(p.label, (void **)&ctx->label, 0,  | 
1007  | 0  |                                          &ctx->label_len))  | 
1008  | 0  |             return 0;  | 
1009  | 0  |     }  | 
1010  |  |  | 
1011  | 0  |     if (p.data != NULL) { | 
1012  | 0  |         OPENSSL_clear_free(ctx->data, ctx->data_len);  | 
1013  | 0  |         ctx->data = NULL;  | 
1014  | 0  |         if (!OSSL_PARAM_get_octet_string(p.data, (void **)&ctx->data, 0,  | 
1015  | 0  |                                          &ctx->data_len))  | 
1016  | 0  |             return 0;  | 
1017  | 0  |     }  | 
1018  |  |  | 
1019  |  | #ifdef FIPS_MODULE  | 
1020  |  |     if (p.digest != NULL) { | 
1021  |  |         const EVP_MD *md = ossl_prov_digest_md(&ctx->digest);  | 
1022  |  |  | 
1023  |  |         if (!fips_tls1_3_digest_check_passed(ctx, md))  | 
1024  |  |             return 0;  | 
1025  |  |     }  | 
1026  |  |  | 
1027  |  |     if (p.key != NULL)  | 
1028  |  |         if (!fips_tls1_3_key_check_passed(ctx))  | 
1029  |  |             return 0;  | 
1030  |  | #endif  | 
1031  |  |  | 
1032  | 0  |     return 1;  | 
1033  | 0  | }  | 
1034  |  |  | 
1035  |  | static const OSSL_PARAM *kdf_tls1_3_settable_ctx_params(ossl_unused void *ctx,  | 
1036  |  |                                                         ossl_unused void *provctx)  | 
1037  | 0  | { | 
1038  | 0  |     return kdf_tls1_3_set_ctx_params_list;  | 
1039  | 0  | }  | 
1040  |  |  | 
1041  |  | const OSSL_DISPATCH ossl_kdf_tls1_3_kdf_functions[] = { | 
1042  |  |     { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))kdf_hkdf_new }, | 
1043  |  |     { OSSL_FUNC_KDF_DUPCTX, (void(*)(void))kdf_hkdf_dup }, | 
1044  |  |     { OSSL_FUNC_KDF_FREECTX, (void(*)(void))kdf_hkdf_free }, | 
1045  |  |     { OSSL_FUNC_KDF_RESET, (void(*)(void))kdf_hkdf_reset }, | 
1046  |  |     { OSSL_FUNC_KDF_DERIVE, (void(*)(void))kdf_tls1_3_derive }, | 
1047  |  |     { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS, | 
1048  |  |       (void(*)(void))kdf_tls1_3_settable_ctx_params },  | 
1049  |  |     { OSSL_FUNC_KDF_SET_CTX_PARAMS, (void(*)(void))kdf_tls1_3_set_ctx_params }, | 
1050  |  |     { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS, | 
1051  |  |       (void(*)(void))hkdf_gettable_ctx_params },  | 
1052  |  |     { OSSL_FUNC_KDF_GET_CTX_PARAMS, (void(*)(void))hkdf_common_get_ctx_params }, | 
1053  |  |     OSSL_DISPATCH_END  | 
1054  |  | };  |