VirtualBox

source: vbox/trunk/src/libs/openssl-3.0.1/providers/implementations/exchange/kdf_exch.c@ 94320

Last change on this file since 94320 was 94320, checked in by vboxsync, 3 years ago

libs/openssl-3.0.1: Export to OSE and fix copyright headers in Makefiles, bugref:10128

File size: 5.6 KB
Line 
1/*
2 * Copyright 2020-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 <openssl/crypto.h>
11#include <openssl/kdf.h>
12#include <openssl/core_dispatch.h>
13#include <openssl/core_names.h>
14#include <openssl/params.h>
15#include "prov/implementations.h"
16#include "prov/provider_ctx.h"
17#include "prov/kdfexchange.h"
18#include "prov/providercommon.h"
19
20static OSSL_FUNC_keyexch_newctx_fn kdf_tls1_prf_newctx;
21static OSSL_FUNC_keyexch_newctx_fn kdf_hkdf_newctx;
22static OSSL_FUNC_keyexch_newctx_fn kdf_scrypt_newctx;
23static OSSL_FUNC_keyexch_init_fn kdf_init;
24static OSSL_FUNC_keyexch_derive_fn kdf_derive;
25static OSSL_FUNC_keyexch_freectx_fn kdf_freectx;
26static OSSL_FUNC_keyexch_dupctx_fn kdf_dupctx;
27static OSSL_FUNC_keyexch_set_ctx_params_fn kdf_set_ctx_params;
28static OSSL_FUNC_keyexch_settable_ctx_params_fn kdf_tls1_prf_settable_ctx_params;
29static OSSL_FUNC_keyexch_settable_ctx_params_fn kdf_hkdf_settable_ctx_params;
30static OSSL_FUNC_keyexch_settable_ctx_params_fn kdf_scrypt_settable_ctx_params;
31
32typedef struct {
33 void *provctx;
34 EVP_KDF_CTX *kdfctx;
35 KDF_DATA *kdfdata;
36} PROV_KDF_CTX;
37
38static void *kdf_newctx(const char *kdfname, void *provctx)
39{
40 PROV_KDF_CTX *kdfctx;
41 EVP_KDF *kdf = NULL;
42
43 if (!ossl_prov_is_running())
44 return NULL;
45
46 kdfctx = OPENSSL_zalloc(sizeof(PROV_KDF_CTX));
47 if (kdfctx == NULL)
48 return NULL;
49
50 kdfctx->provctx = provctx;
51
52 kdf = EVP_KDF_fetch(PROV_LIBCTX_OF(provctx), kdfname, NULL);
53 if (kdf == NULL)
54 goto err;
55 kdfctx->kdfctx = EVP_KDF_CTX_new(kdf);
56 EVP_KDF_free(kdf);
57
58 if (kdfctx->kdfctx == NULL)
59 goto err;
60
61 return kdfctx;
62err:
63 OPENSSL_free(kdfctx);
64 return NULL;
65}
66
67#define KDF_NEWCTX(funcname, kdfname) \
68 static void *kdf_##funcname##_newctx(void *provctx) \
69 { \
70 return kdf_newctx(kdfname, provctx); \
71 }
72
73KDF_NEWCTX(tls1_prf, "TLS1-PRF")
74KDF_NEWCTX(hkdf, "HKDF")
75KDF_NEWCTX(scrypt, "SCRYPT")
76
77static int kdf_init(void *vpkdfctx, void *vkdf, const OSSL_PARAM params[])
78{
79 PROV_KDF_CTX *pkdfctx = (PROV_KDF_CTX *)vpkdfctx;
80
81 if (!ossl_prov_is_running()
82 || pkdfctx == NULL
83 || vkdf == NULL
84 || !ossl_kdf_data_up_ref(vkdf))
85 return 0;
86 pkdfctx->kdfdata = vkdf;
87
88 return kdf_set_ctx_params(pkdfctx, params);
89}
90
91static int kdf_derive(void *vpkdfctx, unsigned char *secret, size_t *secretlen,
92 size_t outlen)
93{
94 PROV_KDF_CTX *pkdfctx = (PROV_KDF_CTX *)vpkdfctx;
95
96 if (!ossl_prov_is_running())
97 return 0;
98
99 if (secret == NULL) {
100 *secretlen = EVP_KDF_CTX_get_kdf_size(pkdfctx->kdfctx);
101 return 1;
102 }
103
104 return EVP_KDF_derive(pkdfctx->kdfctx, secret, outlen, NULL);
105}
106
107static void kdf_freectx(void *vpkdfctx)
108{
109 PROV_KDF_CTX *pkdfctx = (PROV_KDF_CTX *)vpkdfctx;
110
111 EVP_KDF_CTX_free(pkdfctx->kdfctx);
112 ossl_kdf_data_free(pkdfctx->kdfdata);
113
114 OPENSSL_free(pkdfctx);
115}
116
117static void *kdf_dupctx(void *vpkdfctx)
118{
119 PROV_KDF_CTX *srcctx = (PROV_KDF_CTX *)vpkdfctx;
120 PROV_KDF_CTX *dstctx;
121
122 if (!ossl_prov_is_running())
123 return NULL;
124
125 dstctx = OPENSSL_zalloc(sizeof(*srcctx));
126 if (dstctx == NULL)
127 return NULL;
128
129 *dstctx = *srcctx;
130
131 dstctx->kdfctx = EVP_KDF_CTX_dup(srcctx->kdfctx);
132 if (dstctx->kdfctx == NULL) {
133 OPENSSL_free(dstctx);
134 return NULL;
135 }
136 if (!ossl_kdf_data_up_ref(dstctx->kdfdata)) {
137 EVP_KDF_CTX_free(dstctx->kdfctx);
138 OPENSSL_free(dstctx);
139 return NULL;
140 }
141
142 return dstctx;
143}
144
145static int kdf_set_ctx_params(void *vpkdfctx, const OSSL_PARAM params[])
146{
147 PROV_KDF_CTX *pkdfctx = (PROV_KDF_CTX *)vpkdfctx;
148
149 return EVP_KDF_CTX_set_params(pkdfctx->kdfctx, params);
150}
151
152static const OSSL_PARAM *kdf_settable_ctx_params(ossl_unused void *vpkdfctx,
153 void *provctx,
154 const char *kdfname)
155{
156 EVP_KDF *kdf = EVP_KDF_fetch(PROV_LIBCTX_OF(provctx), kdfname,
157 NULL);
158 const OSSL_PARAM *params;
159
160 if (kdf == NULL)
161 return NULL;
162
163 params = EVP_KDF_settable_ctx_params(kdf);
164 EVP_KDF_free(kdf);
165
166 return params;
167}
168
169#define KDF_SETTABLE_CTX_PARAMS(funcname, kdfname) \
170 static const OSSL_PARAM *kdf_##funcname##_settable_ctx_params(void *vpkdfctx, \
171 void *provctx) \
172 { \
173 return kdf_settable_ctx_params(vpkdfctx, provctx, kdfname); \
174 }
175
176KDF_SETTABLE_CTX_PARAMS(tls1_prf, "TLS1-PRF")
177KDF_SETTABLE_CTX_PARAMS(hkdf, "HKDF")
178KDF_SETTABLE_CTX_PARAMS(scrypt, "SCRYPT")
179
180#define KDF_KEYEXCH_FUNCTIONS(funcname) \
181 const OSSL_DISPATCH ossl_kdf_##funcname##_keyexch_functions[] = { \
182 { OSSL_FUNC_KEYEXCH_NEWCTX, (void (*)(void))kdf_##funcname##_newctx }, \
183 { OSSL_FUNC_KEYEXCH_INIT, (void (*)(void))kdf_init }, \
184 { OSSL_FUNC_KEYEXCH_DERIVE, (void (*)(void))kdf_derive }, \
185 { OSSL_FUNC_KEYEXCH_FREECTX, (void (*)(void))kdf_freectx }, \
186 { OSSL_FUNC_KEYEXCH_DUPCTX, (void (*)(void))kdf_dupctx }, \
187 { OSSL_FUNC_KEYEXCH_SET_CTX_PARAMS, (void (*)(void))kdf_set_ctx_params }, \
188 { OSSL_FUNC_KEYEXCH_SETTABLE_CTX_PARAMS, \
189 (void (*)(void))kdf_##funcname##_settable_ctx_params }, \
190 { 0, NULL } \
191 };
192
193KDF_KEYEXCH_FUNCTIONS(tls1_prf)
194KDF_KEYEXCH_FUNCTIONS(hkdf)
195KDF_KEYEXCH_FUNCTIONS(scrypt)
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette