139beb93cSSam Leffler /*
25b9c547cSRui Paulo * Wrapper functions for OpenSSL libcrypto
3*a90b9d01SCy Schubert * Copyright (c) 2004-2024, Jouni Malinen <j@w1.fi>
439beb93cSSam Leffler *
5f05cddf9SRui Paulo * This software may be distributed under the terms of the BSD license.
6f05cddf9SRui Paulo * See README for more details.
739beb93cSSam Leffler */
839beb93cSSam Leffler
939beb93cSSam Leffler #include "includes.h"
1039beb93cSSam Leffler #include <openssl/opensslv.h>
11e28a4053SRui Paulo #include <openssl/err.h>
1239beb93cSSam Leffler #include <openssl/des.h>
1339beb93cSSam Leffler #include <openssl/aes.h>
1439beb93cSSam Leffler #include <openssl/bn.h>
1539beb93cSSam Leffler #include <openssl/evp.h>
16e28a4053SRui Paulo #include <openssl/dh.h>
17f05cddf9SRui Paulo #include <openssl/hmac.h>
18f05cddf9SRui Paulo #include <openssl/rand.h>
19*a90b9d01SCy Schubert #include <openssl/rsa.h>
20*a90b9d01SCy Schubert #include <openssl/pem.h>
215b9c547cSRui Paulo #ifdef CONFIG_ECC
225b9c547cSRui Paulo #include <openssl/ec.h>
23c1d255d3SCy Schubert #include <openssl/x509.h>
245b9c547cSRui Paulo #endif /* CONFIG_ECC */
25ec080394SCy Schubert #if OPENSSL_VERSION_NUMBER >= 0x30000000L
26ec080394SCy Schubert #include <openssl/provider.h>
27*a90b9d01SCy Schubert #include <openssl/core_names.h>
28*a90b9d01SCy Schubert #include <openssl/param_build.h>
29*a90b9d01SCy Schubert #include <openssl/encoder.h>
30*a90b9d01SCy Schubert #include <openssl/decoder.h>
31*a90b9d01SCy Schubert #else /* OpenSSL version >= 3.0 */
32*a90b9d01SCy Schubert #include <openssl/cmac.h>
33ec080394SCy Schubert #endif /* OpenSSL version >= 3.0 */
34*a90b9d01SCy Schubert #ifdef CONFIG_DPP3
35*a90b9d01SCy Schubert #if OPENSSL_VERSION_NUMBER >= 0x30200000L
36*a90b9d01SCy Schubert #include <openssl/hpke.h>
37*a90b9d01SCy Schubert #endif
38*a90b9d01SCy Schubert #endif /* CONFIG_DPP3 */
3939beb93cSSam Leffler
4039beb93cSSam Leffler #include "common.h"
414bc52338SCy Schubert #include "utils/const_time.h"
42e28a4053SRui Paulo #include "wpabuf.h"
43e28a4053SRui Paulo #include "dh_group5.h"
445b9c547cSRui Paulo #include "sha1.h"
455b9c547cSRui Paulo #include "sha256.h"
465b9c547cSRui Paulo #include "sha384.h"
4785732ac8SCy Schubert #include "sha512.h"
48780fb4a2SCy Schubert #include "md5.h"
49780fb4a2SCy Schubert #include "aes_wrap.h"
5039beb93cSSam Leffler #include "crypto.h"
5139beb93cSSam Leffler
52*a90b9d01SCy Schubert #if OPENSSL_VERSION_NUMBER < 0x10100000L
53780fb4a2SCy Schubert /* Compatibility wrappers for older versions. */
54780fb4a2SCy Schubert
HMAC_CTX_new(void)55780fb4a2SCy Schubert static HMAC_CTX * HMAC_CTX_new(void)
56780fb4a2SCy Schubert {
57780fb4a2SCy Schubert HMAC_CTX *ctx;
58780fb4a2SCy Schubert
59780fb4a2SCy Schubert ctx = os_zalloc(sizeof(*ctx));
60780fb4a2SCy Schubert if (ctx)
61780fb4a2SCy Schubert HMAC_CTX_init(ctx);
62780fb4a2SCy Schubert return ctx;
63780fb4a2SCy Schubert }
64780fb4a2SCy Schubert
65780fb4a2SCy Schubert
HMAC_CTX_free(HMAC_CTX * ctx)66780fb4a2SCy Schubert static void HMAC_CTX_free(HMAC_CTX *ctx)
67780fb4a2SCy Schubert {
68780fb4a2SCy Schubert if (!ctx)
69780fb4a2SCy Schubert return;
70780fb4a2SCy Schubert HMAC_CTX_cleanup(ctx);
71780fb4a2SCy Schubert bin_clear_free(ctx, sizeof(*ctx));
72780fb4a2SCy Schubert }
73780fb4a2SCy Schubert
74780fb4a2SCy Schubert
EVP_MD_CTX_new(void)75780fb4a2SCy Schubert static EVP_MD_CTX * EVP_MD_CTX_new(void)
76780fb4a2SCy Schubert {
77780fb4a2SCy Schubert EVP_MD_CTX *ctx;
78780fb4a2SCy Schubert
79780fb4a2SCy Schubert ctx = os_zalloc(sizeof(*ctx));
80780fb4a2SCy Schubert if (ctx)
81780fb4a2SCy Schubert EVP_MD_CTX_init(ctx);
82780fb4a2SCy Schubert return ctx;
83780fb4a2SCy Schubert }
84780fb4a2SCy Schubert
85780fb4a2SCy Schubert
EVP_MD_CTX_free(EVP_MD_CTX * ctx)86780fb4a2SCy Schubert static void EVP_MD_CTX_free(EVP_MD_CTX *ctx)
87780fb4a2SCy Schubert {
88780fb4a2SCy Schubert if (!ctx)
89780fb4a2SCy Schubert return;
90780fb4a2SCy Schubert EVP_MD_CTX_cleanup(ctx);
91780fb4a2SCy Schubert bin_clear_free(ctx, sizeof(*ctx));
92780fb4a2SCy Schubert }
93780fb4a2SCy Schubert
94c1d255d3SCy Schubert
95c1d255d3SCy Schubert #ifdef CONFIG_ECC
964b72b91aSCy Schubert
EVP_PKEY_get0_EC_KEY(EVP_PKEY * pkey)97c1d255d3SCy Schubert static EC_KEY * EVP_PKEY_get0_EC_KEY(EVP_PKEY *pkey)
98c1d255d3SCy Schubert {
99c1d255d3SCy Schubert if (pkey->type != EVP_PKEY_EC)
100c1d255d3SCy Schubert return NULL;
101c1d255d3SCy Schubert return pkey->pkey.ec;
102c1d255d3SCy Schubert }
1034b72b91aSCy Schubert
1044b72b91aSCy Schubert
ECDSA_SIG_set0(ECDSA_SIG * sig,BIGNUM * r,BIGNUM * s)1054b72b91aSCy Schubert static int ECDSA_SIG_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s)
1064b72b91aSCy Schubert {
1074b72b91aSCy Schubert sig->r = r;
1084b72b91aSCy Schubert sig->s = s;
1094b72b91aSCy Schubert return 1;
1104b72b91aSCy Schubert }
1114b72b91aSCy Schubert
1124b72b91aSCy Schubert
ECDSA_SIG_get0(const ECDSA_SIG * sig,const BIGNUM ** pr,const BIGNUM ** ps)1134b72b91aSCy Schubert static void ECDSA_SIG_get0(const ECDSA_SIG *sig, const BIGNUM **pr,
1144b72b91aSCy Schubert const BIGNUM **ps)
1154b72b91aSCy Schubert {
1164b72b91aSCy Schubert if (pr)
1174b72b91aSCy Schubert *pr = sig->r;
1184b72b91aSCy Schubert if (ps)
1194b72b91aSCy Schubert *ps = sig->s;
1204b72b91aSCy Schubert }
1214b72b91aSCy Schubert
122c1d255d3SCy Schubert #endif /* CONFIG_ECC */
123c1d255d3SCy Schubert
ASN1_STRING_get0_data(const ASN1_STRING * x)1244b72b91aSCy Schubert static const unsigned char * ASN1_STRING_get0_data(const ASN1_STRING *x)
1254b72b91aSCy Schubert {
1264b72b91aSCy Schubert return ASN1_STRING_data((ASN1_STRING *) x);
1274b72b91aSCy Schubert }
128*a90b9d01SCy Schubert
129*a90b9d01SCy Schubert
X509_get0_notBefore(const X509 * x)130*a90b9d01SCy Schubert static const ASN1_TIME * X509_get0_notBefore(const X509 *x)
131*a90b9d01SCy Schubert {
132*a90b9d01SCy Schubert return X509_get_notBefore(x);
133*a90b9d01SCy Schubert }
134*a90b9d01SCy Schubert
135*a90b9d01SCy Schubert
X509_get0_notAfter(const X509 * x)136*a90b9d01SCy Schubert static const ASN1_TIME * X509_get0_notAfter(const X509 *x)
137*a90b9d01SCy Schubert {
138*a90b9d01SCy Schubert return X509_get_notAfter(x);
139*a90b9d01SCy Schubert }
140*a90b9d01SCy Schubert
141780fb4a2SCy Schubert #endif /* OpenSSL version < 1.1.0 */
142780fb4a2SCy Schubert
143ec080394SCy Schubert
144*a90b9d01SCy Schubert #if OPENSSL_VERSION_NUMBER < 0x10101000L || \
145*a90b9d01SCy Schubert (defined(LIBRESSL_VERSION_NUMBER) && \
146*a90b9d01SCy Schubert LIBRESSL_VERSION_NUMBER < 0x30400000L)
147*a90b9d01SCy Schubert
EC_POINT_get_affine_coordinates(const EC_GROUP * group,const EC_POINT * point,BIGNUM * x,BIGNUM * y,BN_CTX * ctx)148*a90b9d01SCy Schubert static int EC_POINT_get_affine_coordinates(const EC_GROUP *group,
149*a90b9d01SCy Schubert const EC_POINT *point, BIGNUM *x,
150*a90b9d01SCy Schubert BIGNUM *y, BN_CTX *ctx)
151*a90b9d01SCy Schubert {
152*a90b9d01SCy Schubert return EC_POINT_get_affine_coordinates_GFp(group, point, x, y, ctx);
153*a90b9d01SCy Schubert }
154*a90b9d01SCy Schubert
155*a90b9d01SCy Schubert
EC_POINT_set_affine_coordinates(const EC_GROUP * group,EC_POINT * point,const BIGNUM * x,const BIGNUM * y,BN_CTX * ctx)156*a90b9d01SCy Schubert static int EC_POINT_set_affine_coordinates(const EC_GROUP *group,
157*a90b9d01SCy Schubert EC_POINT *point, const BIGNUM *x,
158*a90b9d01SCy Schubert const BIGNUM *y, BN_CTX *ctx)
159*a90b9d01SCy Schubert {
160*a90b9d01SCy Schubert return EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx);
161*a90b9d01SCy Schubert }
162*a90b9d01SCy Schubert
163*a90b9d01SCy Schubert #endif /* OpenSSL version < 1.1.1 */
164*a90b9d01SCy Schubert
165*a90b9d01SCy Schubert
166*a90b9d01SCy Schubert #if OPENSSL_VERSION_NUMBER < 0x10101000L || \
167*a90b9d01SCy Schubert defined(OPENSSL_IS_BORINGSSL) || \
168*a90b9d01SCy Schubert (defined(LIBRESSL_VERSION_NUMBER) && \
169*a90b9d01SCy Schubert LIBRESSL_VERSION_NUMBER < 0x30400000L)
170*a90b9d01SCy Schubert
EC_POINT_set_compressed_coordinates(const EC_GROUP * group,EC_POINT * point,const BIGNUM * x,int y_bit,BN_CTX * ctx)171*a90b9d01SCy Schubert static int EC_POINT_set_compressed_coordinates(const EC_GROUP *group,
172*a90b9d01SCy Schubert EC_POINT *point, const BIGNUM *x,
173*a90b9d01SCy Schubert int y_bit, BN_CTX *ctx)
174*a90b9d01SCy Schubert {
175*a90b9d01SCy Schubert return EC_POINT_set_compressed_coordinates_GFp(group, point, x, y_bit,
176*a90b9d01SCy Schubert ctx);
177*a90b9d01SCy Schubert }
178*a90b9d01SCy Schubert
179*a90b9d01SCy Schubert
EC_GROUP_get_curve(const EC_GROUP * group,BIGNUM * p,BIGNUM * a,BIGNUM * b,BN_CTX * ctx)180*a90b9d01SCy Schubert static int EC_GROUP_get_curve(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
181*a90b9d01SCy Schubert BIGNUM *b, BN_CTX *ctx)
182*a90b9d01SCy Schubert {
183*a90b9d01SCy Schubert return EC_GROUP_get_curve_GFp(group, p, a, b, ctx);
184*a90b9d01SCy Schubert }
185*a90b9d01SCy Schubert
186*a90b9d01SCy Schubert #endif /* OpenSSL version < 1.1.1 */
187*a90b9d01SCy Schubert
188*a90b9d01SCy Schubert
189*a90b9d01SCy Schubert #if OPENSSL_VERSION_NUMBER >= 0x30000000L
190*a90b9d01SCy Schubert static OSSL_PROVIDER *openssl_legacy_provider = NULL;
191*a90b9d01SCy Schubert #endif /* OpenSSL version >= 3.0 */
192*a90b9d01SCy Schubert
openssl_load_legacy_provider(void)193ec080394SCy Schubert void openssl_load_legacy_provider(void)
194ec080394SCy Schubert {
195ec080394SCy Schubert #if OPENSSL_VERSION_NUMBER >= 0x30000000L
196*a90b9d01SCy Schubert if (openssl_legacy_provider)
197ec080394SCy Schubert return;
198ec080394SCy Schubert
199*a90b9d01SCy Schubert openssl_legacy_provider = OSSL_PROVIDER_try_load(NULL, "legacy", 1);
200*a90b9d01SCy Schubert #endif /* OpenSSL version >= 3.0 */
201*a90b9d01SCy Schubert }
202ec080394SCy Schubert
203*a90b9d01SCy Schubert
openssl_unload_legacy_provider(void)204*a90b9d01SCy Schubert static void openssl_unload_legacy_provider(void)
205*a90b9d01SCy Schubert {
206*a90b9d01SCy Schubert #if OPENSSL_VERSION_NUMBER >= 0x30000000L
207*a90b9d01SCy Schubert if (openssl_legacy_provider) {
208*a90b9d01SCy Schubert OSSL_PROVIDER_unload(openssl_legacy_provider);
209*a90b9d01SCy Schubert openssl_legacy_provider = NULL;
210ec080394SCy Schubert }
211ec080394SCy Schubert #endif /* OpenSSL version >= 3.0 */
212ec080394SCy Schubert }
213ec080394SCy Schubert
214ec080394SCy Schubert
215*a90b9d01SCy Schubert #if OPENSSL_VERSION_NUMBER < 0x30000000L
216*a90b9d01SCy Schubert
get_group5_prime(void)217e28a4053SRui Paulo static BIGNUM * get_group5_prime(void)
21839beb93cSSam Leffler {
219*a90b9d01SCy Schubert #if OPENSSL_VERSION_NUMBER >= 0x10100000L
220780fb4a2SCy Schubert return BN_get_rfc3526_prime_1536(NULL);
221780fb4a2SCy Schubert #elif !defined(OPENSSL_IS_BORINGSSL)
222780fb4a2SCy Schubert return get_rfc3526_prime_1536(NULL);
223780fb4a2SCy Schubert #else
224e28a4053SRui Paulo static const unsigned char RFC3526_PRIME_1536[] = {
225e28a4053SRui Paulo 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xC9,0x0F,0xDA,0xA2,
226e28a4053SRui Paulo 0x21,0x68,0xC2,0x34,0xC4,0xC6,0x62,0x8B,0x80,0xDC,0x1C,0xD1,
227e28a4053SRui Paulo 0x29,0x02,0x4E,0x08,0x8A,0x67,0xCC,0x74,0x02,0x0B,0xBE,0xA6,
228e28a4053SRui Paulo 0x3B,0x13,0x9B,0x22,0x51,0x4A,0x08,0x79,0x8E,0x34,0x04,0xDD,
229e28a4053SRui Paulo 0xEF,0x95,0x19,0xB3,0xCD,0x3A,0x43,0x1B,0x30,0x2B,0x0A,0x6D,
230e28a4053SRui Paulo 0xF2,0x5F,0x14,0x37,0x4F,0xE1,0x35,0x6D,0x6D,0x51,0xC2,0x45,
231e28a4053SRui Paulo 0xE4,0x85,0xB5,0x76,0x62,0x5E,0x7E,0xC6,0xF4,0x4C,0x42,0xE9,
232e28a4053SRui Paulo 0xA6,0x37,0xED,0x6B,0x0B,0xFF,0x5C,0xB6,0xF4,0x06,0xB7,0xED,
233e28a4053SRui Paulo 0xEE,0x38,0x6B,0xFB,0x5A,0x89,0x9F,0xA5,0xAE,0x9F,0x24,0x11,
234e28a4053SRui Paulo 0x7C,0x4B,0x1F,0xE6,0x49,0x28,0x66,0x51,0xEC,0xE4,0x5B,0x3D,
235e28a4053SRui Paulo 0xC2,0x00,0x7C,0xB8,0xA1,0x63,0xBF,0x05,0x98,0xDA,0x48,0x36,
236e28a4053SRui Paulo 0x1C,0x55,0xD3,0x9A,0x69,0x16,0x3F,0xA8,0xFD,0x24,0xCF,0x5F,
237e28a4053SRui Paulo 0x83,0x65,0x5D,0x23,0xDC,0xA3,0xAD,0x96,0x1C,0x62,0xF3,0x56,
238e28a4053SRui Paulo 0x20,0x85,0x52,0xBB,0x9E,0xD5,0x29,0x07,0x70,0x96,0x96,0x6D,
239e28a4053SRui Paulo 0x67,0x0C,0x35,0x4E,0x4A,0xBC,0x98,0x04,0xF1,0x74,0x6C,0x08,
240e28a4053SRui Paulo 0xCA,0x23,0x73,0x27,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
241e28a4053SRui Paulo };
242e28a4053SRui Paulo return BN_bin2bn(RFC3526_PRIME_1536, sizeof(RFC3526_PRIME_1536), NULL);
243780fb4a2SCy Schubert #endif
244e28a4053SRui Paulo }
24539beb93cSSam Leffler
2464bc52338SCy Schubert
get_group5_order(void)2474bc52338SCy Schubert static BIGNUM * get_group5_order(void)
2484bc52338SCy Schubert {
2494bc52338SCy Schubert static const unsigned char RFC3526_ORDER_1536[] = {
2504bc52338SCy Schubert 0x7F,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xE4,0x87,0xED,0x51,
2514bc52338SCy Schubert 0x10,0xB4,0x61,0x1A,0x62,0x63,0x31,0x45,0xC0,0x6E,0x0E,0x68,
2524bc52338SCy Schubert 0x94,0x81,0x27,0x04,0x45,0x33,0xE6,0x3A,0x01,0x05,0xDF,0x53,
2534bc52338SCy Schubert 0x1D,0x89,0xCD,0x91,0x28,0xA5,0x04,0x3C,0xC7,0x1A,0x02,0x6E,
2544bc52338SCy Schubert 0xF7,0xCA,0x8C,0xD9,0xE6,0x9D,0x21,0x8D,0x98,0x15,0x85,0x36,
2554bc52338SCy Schubert 0xF9,0x2F,0x8A,0x1B,0xA7,0xF0,0x9A,0xB6,0xB6,0xA8,0xE1,0x22,
2564bc52338SCy Schubert 0xF2,0x42,0xDA,0xBB,0x31,0x2F,0x3F,0x63,0x7A,0x26,0x21,0x74,
2574bc52338SCy Schubert 0xD3,0x1B,0xF6,0xB5,0x85,0xFF,0xAE,0x5B,0x7A,0x03,0x5B,0xF6,
2584bc52338SCy Schubert 0xF7,0x1C,0x35,0xFD,0xAD,0x44,0xCF,0xD2,0xD7,0x4F,0x92,0x08,
2594bc52338SCy Schubert 0xBE,0x25,0x8F,0xF3,0x24,0x94,0x33,0x28,0xF6,0x72,0x2D,0x9E,
2604bc52338SCy Schubert 0xE1,0x00,0x3E,0x5C,0x50,0xB1,0xDF,0x82,0xCC,0x6D,0x24,0x1B,
2614bc52338SCy Schubert 0x0E,0x2A,0xE9,0xCD,0x34,0x8B,0x1F,0xD4,0x7E,0x92,0x67,0xAF,
2624bc52338SCy Schubert 0xC1,0xB2,0xAE,0x91,0xEE,0x51,0xD6,0xCB,0x0E,0x31,0x79,0xAB,
2634bc52338SCy Schubert 0x10,0x42,0xA9,0x5D,0xCF,0x6A,0x94,0x83,0xB8,0x4B,0x4B,0x36,
2644bc52338SCy Schubert 0xB3,0x86,0x1A,0xA7,0x25,0x5E,0x4C,0x02,0x78,0xBA,0x36,0x04,
2654bc52338SCy Schubert 0x65,0x11,0xB9,0x93,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
2664bc52338SCy Schubert };
2674bc52338SCy Schubert return BN_bin2bn(RFC3526_ORDER_1536, sizeof(RFC3526_ORDER_1536), NULL);
2684bc52338SCy Schubert }
2694bc52338SCy Schubert
270*a90b9d01SCy Schubert #endif /* OpenSSL version < 3.0 */
271*a90b9d01SCy Schubert
2724bc52338SCy Schubert
273e28a4053SRui Paulo #ifdef OPENSSL_NO_SHA256
274e28a4053SRui Paulo #define NO_SHA256_WRAPPER
275e28a4053SRui Paulo #endif
27685732ac8SCy Schubert #ifdef OPENSSL_NO_SHA512
27785732ac8SCy Schubert #define NO_SHA384_WRAPPER
27885732ac8SCy Schubert #endif
279e28a4053SRui Paulo
openssl_digest_vector(const EVP_MD * type,size_t num_elem,const u8 * addr[],const size_t * len,u8 * mac)280f05cddf9SRui Paulo static int openssl_digest_vector(const EVP_MD *type, size_t num_elem,
281f05cddf9SRui Paulo const u8 *addr[], const size_t *len, u8 *mac)
282e28a4053SRui Paulo {
283780fb4a2SCy Schubert EVP_MD_CTX *ctx;
284e28a4053SRui Paulo size_t i;
285e28a4053SRui Paulo unsigned int mac_len;
286e28a4053SRui Paulo
287780fb4a2SCy Schubert if (TEST_FAIL())
288780fb4a2SCy Schubert return -1;
289780fb4a2SCy Schubert
290780fb4a2SCy Schubert ctx = EVP_MD_CTX_new();
291780fb4a2SCy Schubert if (!ctx)
292780fb4a2SCy Schubert return -1;
293780fb4a2SCy Schubert if (!EVP_DigestInit_ex(ctx, type, NULL)) {
294e28a4053SRui Paulo wpa_printf(MSG_ERROR, "OpenSSL: EVP_DigestInit_ex failed: %s",
295e28a4053SRui Paulo ERR_error_string(ERR_get_error(), NULL));
296780fb4a2SCy Schubert EVP_MD_CTX_free(ctx);
297e28a4053SRui Paulo return -1;
298e28a4053SRui Paulo }
299e28a4053SRui Paulo for (i = 0; i < num_elem; i++) {
300780fb4a2SCy Schubert if (!EVP_DigestUpdate(ctx, addr[i], len[i])) {
301e28a4053SRui Paulo wpa_printf(MSG_ERROR, "OpenSSL: EVP_DigestUpdate "
302e28a4053SRui Paulo "failed: %s",
303e28a4053SRui Paulo ERR_error_string(ERR_get_error(), NULL));
304780fb4a2SCy Schubert EVP_MD_CTX_free(ctx);
305e28a4053SRui Paulo return -1;
306e28a4053SRui Paulo }
307e28a4053SRui Paulo }
308780fb4a2SCy Schubert if (!EVP_DigestFinal(ctx, mac, &mac_len)) {
309e28a4053SRui Paulo wpa_printf(MSG_ERROR, "OpenSSL: EVP_DigestFinal failed: %s",
310e28a4053SRui Paulo ERR_error_string(ERR_get_error(), NULL));
311780fb4a2SCy Schubert EVP_MD_CTX_free(ctx);
312e28a4053SRui Paulo return -1;
313e28a4053SRui Paulo }
314780fb4a2SCy Schubert EVP_MD_CTX_free(ctx);
315e28a4053SRui Paulo
316e28a4053SRui Paulo return 0;
317e28a4053SRui Paulo }
318e28a4053SRui Paulo
319e28a4053SRui Paulo
320325151a3SRui Paulo #ifndef CONFIG_FIPS
321*a90b9d01SCy Schubert
md4_vector(size_t num_elem,const u8 * addr[],const size_t * len,u8 * mac)322e28a4053SRui Paulo int md4_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
323e28a4053SRui Paulo {
324ec080394SCy Schubert openssl_load_legacy_provider();
325f05cddf9SRui Paulo return openssl_digest_vector(EVP_md4(), num_elem, addr, len, mac);
32639beb93cSSam Leffler }
32739beb93cSSam Leffler
32839beb93cSSam Leffler
des_encrypt(const u8 * clear,const u8 * key,u8 * cypher)32985732ac8SCy Schubert int des_encrypt(const u8 *clear, const u8 *key, u8 *cypher)
33039beb93cSSam Leffler {
33139beb93cSSam Leffler u8 pkey[8], next, tmp;
3324b72b91aSCy Schubert int i, plen, ret = -1;
3334b72b91aSCy Schubert EVP_CIPHER_CTX *ctx;
33439beb93cSSam Leffler
335ec080394SCy Schubert openssl_load_legacy_provider();
336ec080394SCy Schubert
33739beb93cSSam Leffler /* Add parity bits to the key */
33839beb93cSSam Leffler next = 0;
33939beb93cSSam Leffler for (i = 0; i < 7; i++) {
34039beb93cSSam Leffler tmp = key[i];
34139beb93cSSam Leffler pkey[i] = (tmp >> i) | next | 1;
34239beb93cSSam Leffler next = tmp << (7 - i);
34339beb93cSSam Leffler }
34439beb93cSSam Leffler pkey[i] = next | 1;
34539beb93cSSam Leffler
3464b72b91aSCy Schubert ctx = EVP_CIPHER_CTX_new();
3474b72b91aSCy Schubert if (ctx &&
3484b72b91aSCy Schubert EVP_EncryptInit_ex(ctx, EVP_des_ecb(), NULL, pkey, NULL) == 1 &&
3494b72b91aSCy Schubert EVP_CIPHER_CTX_set_padding(ctx, 0) == 1 &&
3504b72b91aSCy Schubert EVP_EncryptUpdate(ctx, cypher, &plen, clear, 8) == 1 &&
3514b72b91aSCy Schubert EVP_EncryptFinal_ex(ctx, &cypher[plen], &plen) == 1)
3524b72b91aSCy Schubert ret = 0;
3534b72b91aSCy Schubert else
3544b72b91aSCy Schubert wpa_printf(MSG_ERROR, "OpenSSL: DES encrypt failed");
3554b72b91aSCy Schubert
3564b72b91aSCy Schubert if (ctx)
3574b72b91aSCy Schubert EVP_CIPHER_CTX_free(ctx);
3584b72b91aSCy Schubert return ret;
35939beb93cSSam Leffler }
36039beb93cSSam Leffler
36139beb93cSSam Leffler
362325151a3SRui Paulo #ifndef CONFIG_NO_RC4
rc4_skip(const u8 * key,size_t keylen,size_t skip,u8 * data,size_t data_len)363e28a4053SRui Paulo int rc4_skip(const u8 *key, size_t keylen, size_t skip,
364e28a4053SRui Paulo u8 *data, size_t data_len)
36539beb93cSSam Leffler {
366e28a4053SRui Paulo #ifdef OPENSSL_NO_RC4
367e28a4053SRui Paulo return -1;
368e28a4053SRui Paulo #else /* OPENSSL_NO_RC4 */
369780fb4a2SCy Schubert EVP_CIPHER_CTX *ctx;
370e28a4053SRui Paulo int outl;
371e28a4053SRui Paulo int res = -1;
372e28a4053SRui Paulo unsigned char skip_buf[16];
37339beb93cSSam Leffler
374ec080394SCy Schubert openssl_load_legacy_provider();
375ec080394SCy Schubert
376780fb4a2SCy Schubert ctx = EVP_CIPHER_CTX_new();
377780fb4a2SCy Schubert if (!ctx ||
378db0ac6deSCy Schubert !EVP_CipherInit_ex(ctx, EVP_rc4(), NULL, NULL, NULL, 1) ||
3794b72b91aSCy Schubert !EVP_CIPHER_CTX_set_padding(ctx, 0) ||
380780fb4a2SCy Schubert !EVP_CIPHER_CTX_set_key_length(ctx, keylen) ||
381780fb4a2SCy Schubert !EVP_CipherInit_ex(ctx, NULL, NULL, key, NULL, 1))
382e28a4053SRui Paulo goto out;
383e28a4053SRui Paulo
384e28a4053SRui Paulo while (skip >= sizeof(skip_buf)) {
385e28a4053SRui Paulo size_t len = skip;
386e28a4053SRui Paulo if (len > sizeof(skip_buf))
387e28a4053SRui Paulo len = sizeof(skip_buf);
388780fb4a2SCy Schubert if (!EVP_CipherUpdate(ctx, skip_buf, &outl, skip_buf, len))
389e28a4053SRui Paulo goto out;
390e28a4053SRui Paulo skip -= len;
391e28a4053SRui Paulo }
392e28a4053SRui Paulo
393780fb4a2SCy Schubert if (EVP_CipherUpdate(ctx, data, &outl, data, data_len))
394e28a4053SRui Paulo res = 0;
395e28a4053SRui Paulo
396e28a4053SRui Paulo out:
397780fb4a2SCy Schubert if (ctx)
398780fb4a2SCy Schubert EVP_CIPHER_CTX_free(ctx);
399e28a4053SRui Paulo return res;
400e28a4053SRui Paulo #endif /* OPENSSL_NO_RC4 */
40139beb93cSSam Leffler }
402325151a3SRui Paulo #endif /* CONFIG_NO_RC4 */
40339beb93cSSam Leffler
40439beb93cSSam Leffler
md5_vector(size_t num_elem,const u8 * addr[],const size_t * len,u8 * mac)405e28a4053SRui Paulo int md5_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
40639beb93cSSam Leffler {
407f05cddf9SRui Paulo return openssl_digest_vector(EVP_md5(), num_elem, addr, len, mac);
40839beb93cSSam Leffler }
409*a90b9d01SCy Schubert
410325151a3SRui Paulo #endif /* CONFIG_FIPS */
41139beb93cSSam Leffler
41239beb93cSSam Leffler
sha1_vector(size_t num_elem,const u8 * addr[],const size_t * len,u8 * mac)413e28a4053SRui Paulo int sha1_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
41439beb93cSSam Leffler {
415f05cddf9SRui Paulo return openssl_digest_vector(EVP_sha1(), num_elem, addr, len, mac);
41639beb93cSSam Leffler }
41739beb93cSSam Leffler
41839beb93cSSam Leffler
419e28a4053SRui Paulo #ifndef NO_SHA256_WRAPPER
sha256_vector(size_t num_elem,const u8 * addr[],const size_t * len,u8 * mac)420e28a4053SRui Paulo int sha256_vector(size_t num_elem, const u8 *addr[], const size_t *len,
421e28a4053SRui Paulo u8 *mac)
422e28a4053SRui Paulo {
423f05cddf9SRui Paulo return openssl_digest_vector(EVP_sha256(), num_elem, addr, len, mac);
42439beb93cSSam Leffler }
425e28a4053SRui Paulo #endif /* NO_SHA256_WRAPPER */
42639beb93cSSam Leffler
42739beb93cSSam Leffler
42885732ac8SCy Schubert #ifndef NO_SHA384_WRAPPER
sha384_vector(size_t num_elem,const u8 * addr[],const size_t * len,u8 * mac)42985732ac8SCy Schubert int sha384_vector(size_t num_elem, const u8 *addr[], const size_t *len,
43085732ac8SCy Schubert u8 *mac)
43185732ac8SCy Schubert {
43285732ac8SCy Schubert return openssl_digest_vector(EVP_sha384(), num_elem, addr, len, mac);
43385732ac8SCy Schubert }
43485732ac8SCy Schubert #endif /* NO_SHA384_WRAPPER */
43585732ac8SCy Schubert
43685732ac8SCy Schubert
43785732ac8SCy Schubert #ifndef NO_SHA512_WRAPPER
sha512_vector(size_t num_elem,const u8 * addr[],const size_t * len,u8 * mac)43885732ac8SCy Schubert int sha512_vector(size_t num_elem, const u8 *addr[], const size_t *len,
43985732ac8SCy Schubert u8 *mac)
44085732ac8SCy Schubert {
44185732ac8SCy Schubert return openssl_digest_vector(EVP_sha512(), num_elem, addr, len, mac);
44285732ac8SCy Schubert }
44385732ac8SCy Schubert #endif /* NO_SHA512_WRAPPER */
44485732ac8SCy Schubert
44585732ac8SCy Schubert
aes_get_evp_cipher(size_t keylen)446f05cddf9SRui Paulo static const EVP_CIPHER * aes_get_evp_cipher(size_t keylen)
44739beb93cSSam Leffler {
448f05cddf9SRui Paulo switch (keylen) {
449f05cddf9SRui Paulo case 16:
450f05cddf9SRui Paulo return EVP_aes_128_ecb();
451f05cddf9SRui Paulo case 24:
452f05cddf9SRui Paulo return EVP_aes_192_ecb();
453f05cddf9SRui Paulo case 32:
454f05cddf9SRui Paulo return EVP_aes_256_ecb();
455*a90b9d01SCy Schubert default:
45639beb93cSSam Leffler return NULL;
45739beb93cSSam Leffler }
458*a90b9d01SCy Schubert }
459f05cddf9SRui Paulo
460f05cddf9SRui Paulo
aes_encrypt_init(const u8 * key,size_t len)461f05cddf9SRui Paulo void * aes_encrypt_init(const u8 *key, size_t len)
462f05cddf9SRui Paulo {
463f05cddf9SRui Paulo EVP_CIPHER_CTX *ctx;
464f05cddf9SRui Paulo const EVP_CIPHER *type;
465f05cddf9SRui Paulo
466780fb4a2SCy Schubert if (TEST_FAIL())
467780fb4a2SCy Schubert return NULL;
468780fb4a2SCy Schubert
469f05cddf9SRui Paulo type = aes_get_evp_cipher(len);
47085732ac8SCy Schubert if (!type) {
47185732ac8SCy Schubert wpa_printf(MSG_INFO, "%s: Unsupported len=%u",
47285732ac8SCy Schubert __func__, (unsigned int) len);
473f05cddf9SRui Paulo return NULL;
47485732ac8SCy Schubert }
475f05cddf9SRui Paulo
476780fb4a2SCy Schubert ctx = EVP_CIPHER_CTX_new();
477f05cddf9SRui Paulo if (ctx == NULL)
478f05cddf9SRui Paulo return NULL;
479*a90b9d01SCy Schubert if (EVP_EncryptInit_ex(ctx, type, NULL, key, NULL) != 1 ||
480*a90b9d01SCy Schubert EVP_CIPHER_CTX_set_padding(ctx, 0) != 1) {
481*a90b9d01SCy Schubert EVP_CIPHER_CTX_free(ctx);
482f05cddf9SRui Paulo return NULL;
483f05cddf9SRui Paulo }
484f05cddf9SRui Paulo return ctx;
48539beb93cSSam Leffler }
48639beb93cSSam Leffler
48739beb93cSSam Leffler
aes_encrypt(void * ctx,const u8 * plain,u8 * crypt)48885732ac8SCy Schubert int aes_encrypt(void *ctx, const u8 *plain, u8 *crypt)
48939beb93cSSam Leffler {
490f05cddf9SRui Paulo EVP_CIPHER_CTX *c = ctx;
491f05cddf9SRui Paulo int clen = 16;
492f05cddf9SRui Paulo if (EVP_EncryptUpdate(c, crypt, &clen, plain, 16) != 1) {
493f05cddf9SRui Paulo wpa_printf(MSG_ERROR, "OpenSSL: EVP_EncryptUpdate failed: %s",
494f05cddf9SRui Paulo ERR_error_string(ERR_get_error(), NULL));
49585732ac8SCy Schubert return -1;
496f05cddf9SRui Paulo }
49785732ac8SCy Schubert return 0;
49839beb93cSSam Leffler }
49939beb93cSSam Leffler
50039beb93cSSam Leffler
aes_encrypt_deinit(void * ctx)50139beb93cSSam Leffler void aes_encrypt_deinit(void *ctx)
50239beb93cSSam Leffler {
503f05cddf9SRui Paulo EVP_CIPHER_CTX *c = ctx;
504f05cddf9SRui Paulo u8 buf[16];
505f05cddf9SRui Paulo int len = sizeof(buf);
506f05cddf9SRui Paulo if (EVP_EncryptFinal_ex(c, buf, &len) != 1) {
507f05cddf9SRui Paulo wpa_printf(MSG_ERROR, "OpenSSL: EVP_EncryptFinal_ex failed: "
508f05cddf9SRui Paulo "%s", ERR_error_string(ERR_get_error(), NULL));
509f05cddf9SRui Paulo }
510f05cddf9SRui Paulo if (len != 0) {
511f05cddf9SRui Paulo wpa_printf(MSG_ERROR, "OpenSSL: Unexpected padding length %d "
512f05cddf9SRui Paulo "in AES encrypt", len);
513f05cddf9SRui Paulo }
514780fb4a2SCy Schubert EVP_CIPHER_CTX_free(c);
51539beb93cSSam Leffler }
51639beb93cSSam Leffler
51739beb93cSSam Leffler
aes_decrypt_init(const u8 * key,size_t len)51839beb93cSSam Leffler void * aes_decrypt_init(const u8 *key, size_t len)
51939beb93cSSam Leffler {
520f05cddf9SRui Paulo EVP_CIPHER_CTX *ctx;
521f05cddf9SRui Paulo const EVP_CIPHER *type;
522f05cddf9SRui Paulo
523780fb4a2SCy Schubert if (TEST_FAIL())
524780fb4a2SCy Schubert return NULL;
525780fb4a2SCy Schubert
526f05cddf9SRui Paulo type = aes_get_evp_cipher(len);
52785732ac8SCy Schubert if (!type) {
52885732ac8SCy Schubert wpa_printf(MSG_INFO, "%s: Unsupported len=%u",
52985732ac8SCy Schubert __func__, (unsigned int) len);
53039beb93cSSam Leffler return NULL;
53185732ac8SCy Schubert }
532f05cddf9SRui Paulo
533780fb4a2SCy Schubert ctx = EVP_CIPHER_CTX_new();
534f05cddf9SRui Paulo if (ctx == NULL)
535f05cddf9SRui Paulo return NULL;
536*a90b9d01SCy Schubert if (EVP_DecryptInit_ex(ctx, type, NULL, key, NULL) != 1 ||
537*a90b9d01SCy Schubert EVP_CIPHER_CTX_set_padding(ctx, 0) != 1) {
538780fb4a2SCy Schubert EVP_CIPHER_CTX_free(ctx);
53939beb93cSSam Leffler return NULL;
54039beb93cSSam Leffler }
541f05cddf9SRui Paulo return ctx;
54239beb93cSSam Leffler }
54339beb93cSSam Leffler
54439beb93cSSam Leffler
aes_decrypt(void * ctx,const u8 * crypt,u8 * plain)54585732ac8SCy Schubert int aes_decrypt(void *ctx, const u8 *crypt, u8 *plain)
54639beb93cSSam Leffler {
547f05cddf9SRui Paulo EVP_CIPHER_CTX *c = ctx;
548f05cddf9SRui Paulo int plen = 16;
549f05cddf9SRui Paulo if (EVP_DecryptUpdate(c, plain, &plen, crypt, 16) != 1) {
550f05cddf9SRui Paulo wpa_printf(MSG_ERROR, "OpenSSL: EVP_DecryptUpdate failed: %s",
551f05cddf9SRui Paulo ERR_error_string(ERR_get_error(), NULL));
55285732ac8SCy Schubert return -1;
553f05cddf9SRui Paulo }
55485732ac8SCy Schubert return 0;
55539beb93cSSam Leffler }
55639beb93cSSam Leffler
55739beb93cSSam Leffler
aes_decrypt_deinit(void * ctx)55839beb93cSSam Leffler void aes_decrypt_deinit(void *ctx)
55939beb93cSSam Leffler {
560f05cddf9SRui Paulo EVP_CIPHER_CTX *c = ctx;
561f05cddf9SRui Paulo u8 buf[16];
562f05cddf9SRui Paulo int len = sizeof(buf);
563f05cddf9SRui Paulo if (EVP_DecryptFinal_ex(c, buf, &len) != 1) {
564f05cddf9SRui Paulo wpa_printf(MSG_ERROR, "OpenSSL: EVP_DecryptFinal_ex failed: "
565f05cddf9SRui Paulo "%s", ERR_error_string(ERR_get_error(), NULL));
566f05cddf9SRui Paulo }
567f05cddf9SRui Paulo if (len != 0) {
568f05cddf9SRui Paulo wpa_printf(MSG_ERROR, "OpenSSL: Unexpected padding length %d "
569f05cddf9SRui Paulo "in AES decrypt", len);
570f05cddf9SRui Paulo }
571780fb4a2SCy Schubert EVP_CIPHER_CTX_free(c);
5725b9c547cSRui Paulo }
5735b9c547cSRui Paulo
5745b9c547cSRui Paulo
575325151a3SRui Paulo #ifndef CONFIG_FIPS
576325151a3SRui Paulo #ifndef CONFIG_OPENSSL_INTERNAL_AES_WRAP
577325151a3SRui Paulo
578*a90b9d01SCy Schubert #if OPENSSL_VERSION_NUMBER >= 0x30000000L
aes_get_evp_wrap_cipher(size_t keylen)579*a90b9d01SCy Schubert static const EVP_CIPHER * aes_get_evp_wrap_cipher(size_t keylen)
580*a90b9d01SCy Schubert {
581*a90b9d01SCy Schubert switch (keylen) {
582*a90b9d01SCy Schubert case 16:
583*a90b9d01SCy Schubert return EVP_aes_128_wrap();
584*a90b9d01SCy Schubert case 24:
585*a90b9d01SCy Schubert return EVP_aes_192_wrap();
586*a90b9d01SCy Schubert case 32:
587*a90b9d01SCy Schubert return EVP_aes_256_wrap();
588*a90b9d01SCy Schubert default:
589*a90b9d01SCy Schubert return NULL;
590*a90b9d01SCy Schubert }
591*a90b9d01SCy Schubert }
592*a90b9d01SCy Schubert #endif /* OpenSSL version >= 3.0 */
593*a90b9d01SCy Schubert
594*a90b9d01SCy Schubert
aes_wrap(const u8 * kek,size_t kek_len,int n,const u8 * plain,u8 * cipher)5955b9c547cSRui Paulo int aes_wrap(const u8 *kek, size_t kek_len, int n, const u8 *plain, u8 *cipher)
5965b9c547cSRui Paulo {
597*a90b9d01SCy Schubert #if OPENSSL_VERSION_NUMBER >= 0x30000000L
598*a90b9d01SCy Schubert EVP_CIPHER_CTX *ctx;
599*a90b9d01SCy Schubert const EVP_CIPHER *type;
600*a90b9d01SCy Schubert int ret = -1, len;
601*a90b9d01SCy Schubert u8 buf[16];
602*a90b9d01SCy Schubert
603*a90b9d01SCy Schubert if (TEST_FAIL())
604*a90b9d01SCy Schubert return -1;
605*a90b9d01SCy Schubert
606*a90b9d01SCy Schubert type = aes_get_evp_wrap_cipher(kek_len);
607*a90b9d01SCy Schubert if (!type)
608*a90b9d01SCy Schubert return -1;
609*a90b9d01SCy Schubert
610*a90b9d01SCy Schubert ctx = EVP_CIPHER_CTX_new();
611*a90b9d01SCy Schubert if (!ctx)
612*a90b9d01SCy Schubert return -1;
613*a90b9d01SCy Schubert
614*a90b9d01SCy Schubert if (EVP_EncryptInit_ex(ctx, type, NULL, kek, NULL) == 1 &&
615*a90b9d01SCy Schubert EVP_CIPHER_CTX_set_padding(ctx, 0) == 1 &&
616*a90b9d01SCy Schubert EVP_EncryptUpdate(ctx, cipher, &len, plain, n * 8) == 1 &&
617*a90b9d01SCy Schubert len == (n + 1) * 8 &&
618*a90b9d01SCy Schubert EVP_EncryptFinal_ex(ctx, buf, &len) == 1)
619*a90b9d01SCy Schubert ret = 0;
620*a90b9d01SCy Schubert
621*a90b9d01SCy Schubert EVP_CIPHER_CTX_free(ctx);
622*a90b9d01SCy Schubert return ret;
623*a90b9d01SCy Schubert #else /* OpenSSL version >= 3.0 */
6245b9c547cSRui Paulo AES_KEY actx;
6255b9c547cSRui Paulo int res;
6265b9c547cSRui Paulo
62785732ac8SCy Schubert if (TEST_FAIL())
62885732ac8SCy Schubert return -1;
6295b9c547cSRui Paulo if (AES_set_encrypt_key(kek, kek_len << 3, &actx))
6305b9c547cSRui Paulo return -1;
6315b9c547cSRui Paulo res = AES_wrap_key(&actx, NULL, cipher, plain, n * 8);
6325b9c547cSRui Paulo OPENSSL_cleanse(&actx, sizeof(actx));
6335b9c547cSRui Paulo return res <= 0 ? -1 : 0;
634*a90b9d01SCy Schubert #endif /* OpenSSL version >= 3.0 */
6355b9c547cSRui Paulo }
6365b9c547cSRui Paulo
6375b9c547cSRui Paulo
aes_unwrap(const u8 * kek,size_t kek_len,int n,const u8 * cipher,u8 * plain)6385b9c547cSRui Paulo int aes_unwrap(const u8 *kek, size_t kek_len, int n, const u8 *cipher,
6395b9c547cSRui Paulo u8 *plain)
6405b9c547cSRui Paulo {
641*a90b9d01SCy Schubert #if OPENSSL_VERSION_NUMBER >= 0x30000000L
642*a90b9d01SCy Schubert EVP_CIPHER_CTX *ctx;
643*a90b9d01SCy Schubert const EVP_CIPHER *type;
644*a90b9d01SCy Schubert int ret = -1, len;
645*a90b9d01SCy Schubert u8 buf[16];
646*a90b9d01SCy Schubert
647*a90b9d01SCy Schubert if (TEST_FAIL())
648*a90b9d01SCy Schubert return -1;
649*a90b9d01SCy Schubert
650*a90b9d01SCy Schubert type = aes_get_evp_wrap_cipher(kek_len);
651*a90b9d01SCy Schubert if (!type)
652*a90b9d01SCy Schubert return -1;
653*a90b9d01SCy Schubert
654*a90b9d01SCy Schubert ctx = EVP_CIPHER_CTX_new();
655*a90b9d01SCy Schubert if (!ctx)
656*a90b9d01SCy Schubert return -1;
657*a90b9d01SCy Schubert
658*a90b9d01SCy Schubert if (EVP_DecryptInit_ex(ctx, type, NULL, kek, NULL) == 1 &&
659*a90b9d01SCy Schubert EVP_CIPHER_CTX_set_padding(ctx, 0) == 1 &&
660*a90b9d01SCy Schubert EVP_DecryptUpdate(ctx, plain, &len, cipher, (n + 1) * 8) == 1 &&
661*a90b9d01SCy Schubert len == n * 8 &&
662*a90b9d01SCy Schubert EVP_DecryptFinal_ex(ctx, buf, &len) == 1)
663*a90b9d01SCy Schubert ret = 0;
664*a90b9d01SCy Schubert
665*a90b9d01SCy Schubert EVP_CIPHER_CTX_free(ctx);
666*a90b9d01SCy Schubert return ret;
667*a90b9d01SCy Schubert #else /* OpenSSL version >= 3.0 */
6685b9c547cSRui Paulo AES_KEY actx;
6695b9c547cSRui Paulo int res;
6705b9c547cSRui Paulo
67185732ac8SCy Schubert if (TEST_FAIL())
67285732ac8SCy Schubert return -1;
6735b9c547cSRui Paulo if (AES_set_decrypt_key(kek, kek_len << 3, &actx))
6745b9c547cSRui Paulo return -1;
6755b9c547cSRui Paulo res = AES_unwrap_key(&actx, NULL, plain, cipher, (n + 1) * 8);
6765b9c547cSRui Paulo OPENSSL_cleanse(&actx, sizeof(actx));
6775b9c547cSRui Paulo return res <= 0 ? -1 : 0;
678*a90b9d01SCy Schubert #endif /* OpenSSL version >= 3.0 */
67939beb93cSSam Leffler }
68039beb93cSSam Leffler
681325151a3SRui Paulo #endif /* CONFIG_OPENSSL_INTERNAL_AES_WRAP */
682325151a3SRui Paulo #endif /* CONFIG_FIPS */
683325151a3SRui Paulo
684325151a3SRui Paulo
aes_128_cbc_encrypt(const u8 * key,const u8 * iv,u8 * data,size_t data_len)685325151a3SRui Paulo int aes_128_cbc_encrypt(const u8 *key, const u8 *iv, u8 *data, size_t data_len)
686325151a3SRui Paulo {
687780fb4a2SCy Schubert EVP_CIPHER_CTX *ctx;
688325151a3SRui Paulo int clen, len;
689325151a3SRui Paulo u8 buf[16];
690780fb4a2SCy Schubert int res = -1;
691325151a3SRui Paulo
692780fb4a2SCy Schubert if (TEST_FAIL())
693325151a3SRui Paulo return -1;
694325151a3SRui Paulo
695780fb4a2SCy Schubert ctx = EVP_CIPHER_CTX_new();
696780fb4a2SCy Schubert if (!ctx)
697780fb4a2SCy Schubert return -1;
698325151a3SRui Paulo clen = data_len;
699325151a3SRui Paulo len = sizeof(buf);
700780fb4a2SCy Schubert if (EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv) == 1 &&
701780fb4a2SCy Schubert EVP_CIPHER_CTX_set_padding(ctx, 0) == 1 &&
702780fb4a2SCy Schubert EVP_EncryptUpdate(ctx, data, &clen, data, data_len) == 1 &&
703780fb4a2SCy Schubert clen == (int) data_len &&
704780fb4a2SCy Schubert EVP_EncryptFinal_ex(ctx, buf, &len) == 1 && len == 0)
705780fb4a2SCy Schubert res = 0;
706780fb4a2SCy Schubert EVP_CIPHER_CTX_free(ctx);
707325151a3SRui Paulo
708780fb4a2SCy Schubert return res;
709325151a3SRui Paulo }
710325151a3SRui Paulo
711325151a3SRui Paulo
aes_128_cbc_decrypt(const u8 * key,const u8 * iv,u8 * data,size_t data_len)712325151a3SRui Paulo int aes_128_cbc_decrypt(const u8 *key, const u8 *iv, u8 *data, size_t data_len)
713325151a3SRui Paulo {
714780fb4a2SCy Schubert EVP_CIPHER_CTX *ctx;
715325151a3SRui Paulo int plen, len;
716325151a3SRui Paulo u8 buf[16];
717780fb4a2SCy Schubert int res = -1;
718325151a3SRui Paulo
719780fb4a2SCy Schubert if (TEST_FAIL())
720325151a3SRui Paulo return -1;
721325151a3SRui Paulo
722780fb4a2SCy Schubert ctx = EVP_CIPHER_CTX_new();
723780fb4a2SCy Schubert if (!ctx)
724780fb4a2SCy Schubert return -1;
725325151a3SRui Paulo plen = data_len;
726325151a3SRui Paulo len = sizeof(buf);
727780fb4a2SCy Schubert if (EVP_DecryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv) == 1 &&
728780fb4a2SCy Schubert EVP_CIPHER_CTX_set_padding(ctx, 0) == 1 &&
729780fb4a2SCy Schubert EVP_DecryptUpdate(ctx, data, &plen, data, data_len) == 1 &&
730780fb4a2SCy Schubert plen == (int) data_len &&
731780fb4a2SCy Schubert EVP_DecryptFinal_ex(ctx, buf, &len) == 1 && len == 0)
732780fb4a2SCy Schubert res = 0;
733780fb4a2SCy Schubert EVP_CIPHER_CTX_free(ctx);
734325151a3SRui Paulo
735780fb4a2SCy Schubert return res;
736780fb4a2SCy Schubert
737325151a3SRui Paulo }
738325151a3SRui Paulo
73939beb93cSSam Leffler
crypto_dh_init(u8 generator,const u8 * prime,size_t prime_len,u8 * privkey,u8 * pubkey)74085732ac8SCy Schubert int crypto_dh_init(u8 generator, const u8 *prime, size_t prime_len, u8 *privkey,
74185732ac8SCy Schubert u8 *pubkey)
74285732ac8SCy Schubert {
74385732ac8SCy Schubert size_t pubkey_len, pad;
74485732ac8SCy Schubert
74585732ac8SCy Schubert if (os_get_random(privkey, prime_len) < 0)
74685732ac8SCy Schubert return -1;
74785732ac8SCy Schubert if (os_memcmp(privkey, prime, prime_len) > 0) {
74885732ac8SCy Schubert /* Make sure private value is smaller than prime */
74985732ac8SCy Schubert privkey[0] = 0;
75085732ac8SCy Schubert }
75185732ac8SCy Schubert
75285732ac8SCy Schubert pubkey_len = prime_len;
75385732ac8SCy Schubert if (crypto_mod_exp(&generator, 1, privkey, prime_len, prime, prime_len,
75485732ac8SCy Schubert pubkey, &pubkey_len) < 0)
75585732ac8SCy Schubert return -1;
75685732ac8SCy Schubert if (pubkey_len < prime_len) {
75785732ac8SCy Schubert pad = prime_len - pubkey_len;
75885732ac8SCy Schubert os_memmove(pubkey + pad, pubkey, pubkey_len);
75985732ac8SCy Schubert os_memset(pubkey, 0, pad);
76085732ac8SCy Schubert }
76185732ac8SCy Schubert
76285732ac8SCy Schubert return 0;
76385732ac8SCy Schubert }
76485732ac8SCy Schubert
76585732ac8SCy Schubert
crypto_dh_derive_secret(u8 generator,const u8 * prime,size_t prime_len,const u8 * order,size_t order_len,const u8 * privkey,size_t privkey_len,const u8 * pubkey,size_t pubkey_len,u8 * secret,size_t * len)76685732ac8SCy Schubert int crypto_dh_derive_secret(u8 generator, const u8 *prime, size_t prime_len,
7674bc52338SCy Schubert const u8 *order, size_t order_len,
76885732ac8SCy Schubert const u8 *privkey, size_t privkey_len,
76985732ac8SCy Schubert const u8 *pubkey, size_t pubkey_len,
77085732ac8SCy Schubert u8 *secret, size_t *len)
77185732ac8SCy Schubert {
7724bc52338SCy Schubert BIGNUM *pub, *p;
7734bc52338SCy Schubert int res = -1;
7744bc52338SCy Schubert
7754bc52338SCy Schubert pub = BN_bin2bn(pubkey, pubkey_len, NULL);
7764bc52338SCy Schubert p = BN_bin2bn(prime, prime_len, NULL);
7774bc52338SCy Schubert if (!pub || !p || BN_is_zero(pub) || BN_is_one(pub) ||
7784bc52338SCy Schubert BN_cmp(pub, p) >= 0)
7794bc52338SCy Schubert goto fail;
7804bc52338SCy Schubert
7814bc52338SCy Schubert if (order) {
7824bc52338SCy Schubert BN_CTX *ctx;
7834bc52338SCy Schubert BIGNUM *q, *tmp;
7844bc52338SCy Schubert int failed;
7854bc52338SCy Schubert
7864bc52338SCy Schubert /* verify: pubkey^q == 1 mod p */
7874bc52338SCy Schubert q = BN_bin2bn(order, order_len, NULL);
7884bc52338SCy Schubert ctx = BN_CTX_new();
7894bc52338SCy Schubert tmp = BN_new();
7904bc52338SCy Schubert failed = !q || !ctx || !tmp ||
7914bc52338SCy Schubert !BN_mod_exp(tmp, pub, q, p, ctx) ||
7924bc52338SCy Schubert !BN_is_one(tmp);
793206b73d0SCy Schubert BN_clear_free(q);
794206b73d0SCy Schubert BN_clear_free(tmp);
7954bc52338SCy Schubert BN_CTX_free(ctx);
7964bc52338SCy Schubert if (failed)
7974bc52338SCy Schubert goto fail;
7984bc52338SCy Schubert }
7994bc52338SCy Schubert
8004bc52338SCy Schubert res = crypto_mod_exp(pubkey, pubkey_len, privkey, privkey_len,
80185732ac8SCy Schubert prime, prime_len, secret, len);
8024bc52338SCy Schubert fail:
803206b73d0SCy Schubert BN_clear_free(pub);
804206b73d0SCy Schubert BN_clear_free(p);
8054bc52338SCy Schubert return res;
80685732ac8SCy Schubert }
80785732ac8SCy Schubert
80885732ac8SCy Schubert
crypto_mod_exp(const u8 * base,size_t base_len,const u8 * power,size_t power_len,const u8 * modulus,size_t modulus_len,u8 * result,size_t * result_len)80939beb93cSSam Leffler int crypto_mod_exp(const u8 *base, size_t base_len,
81039beb93cSSam Leffler const u8 *power, size_t power_len,
81139beb93cSSam Leffler const u8 *modulus, size_t modulus_len,
81239beb93cSSam Leffler u8 *result, size_t *result_len)
81339beb93cSSam Leffler {
81439beb93cSSam Leffler BIGNUM *bn_base, *bn_exp, *bn_modulus, *bn_result;
81539beb93cSSam Leffler int ret = -1;
81639beb93cSSam Leffler BN_CTX *ctx;
81739beb93cSSam Leffler
81839beb93cSSam Leffler ctx = BN_CTX_new();
81939beb93cSSam Leffler if (ctx == NULL)
82039beb93cSSam Leffler return -1;
82139beb93cSSam Leffler
82239beb93cSSam Leffler bn_base = BN_bin2bn(base, base_len, NULL);
82339beb93cSSam Leffler bn_exp = BN_bin2bn(power, power_len, NULL);
82439beb93cSSam Leffler bn_modulus = BN_bin2bn(modulus, modulus_len, NULL);
82539beb93cSSam Leffler bn_result = BN_new();
82639beb93cSSam Leffler
82739beb93cSSam Leffler if (bn_base == NULL || bn_exp == NULL || bn_modulus == NULL ||
82839beb93cSSam Leffler bn_result == NULL)
82939beb93cSSam Leffler goto error;
83039beb93cSSam Leffler
8314bc52338SCy Schubert if (BN_mod_exp_mont_consttime(bn_result, bn_base, bn_exp, bn_modulus,
8324bc52338SCy Schubert ctx, NULL) != 1)
83339beb93cSSam Leffler goto error;
83439beb93cSSam Leffler
83539beb93cSSam Leffler *result_len = BN_bn2bin(bn_result, result);
83639beb93cSSam Leffler ret = 0;
83739beb93cSSam Leffler
83839beb93cSSam Leffler error:
8395b9c547cSRui Paulo BN_clear_free(bn_base);
8405b9c547cSRui Paulo BN_clear_free(bn_exp);
8415b9c547cSRui Paulo BN_clear_free(bn_modulus);
8425b9c547cSRui Paulo BN_clear_free(bn_result);
84339beb93cSSam Leffler BN_CTX_free(ctx);
84439beb93cSSam Leffler return ret;
84539beb93cSSam Leffler }
84639beb93cSSam Leffler
84739beb93cSSam Leffler
84839beb93cSSam Leffler struct crypto_cipher {
849780fb4a2SCy Schubert EVP_CIPHER_CTX *enc;
850780fb4a2SCy Schubert EVP_CIPHER_CTX *dec;
85139beb93cSSam Leffler };
85239beb93cSSam Leffler
85339beb93cSSam Leffler
crypto_cipher_init(enum crypto_cipher_alg alg,const u8 * iv,const u8 * key,size_t key_len)85439beb93cSSam Leffler struct crypto_cipher * crypto_cipher_init(enum crypto_cipher_alg alg,
85539beb93cSSam Leffler const u8 *iv, const u8 *key,
85639beb93cSSam Leffler size_t key_len)
85739beb93cSSam Leffler {
85839beb93cSSam Leffler struct crypto_cipher *ctx;
85939beb93cSSam Leffler const EVP_CIPHER *cipher;
86039beb93cSSam Leffler
86139beb93cSSam Leffler ctx = os_zalloc(sizeof(*ctx));
86239beb93cSSam Leffler if (ctx == NULL)
86339beb93cSSam Leffler return NULL;
86439beb93cSSam Leffler
86539beb93cSSam Leffler switch (alg) {
866325151a3SRui Paulo #ifndef CONFIG_NO_RC4
86739beb93cSSam Leffler #ifndef OPENSSL_NO_RC4
86839beb93cSSam Leffler case CRYPTO_CIPHER_ALG_RC4:
86939beb93cSSam Leffler cipher = EVP_rc4();
87039beb93cSSam Leffler break;
87139beb93cSSam Leffler #endif /* OPENSSL_NO_RC4 */
872325151a3SRui Paulo #endif /* CONFIG_NO_RC4 */
87339beb93cSSam Leffler #ifndef OPENSSL_NO_AES
87439beb93cSSam Leffler case CRYPTO_CIPHER_ALG_AES:
87539beb93cSSam Leffler switch (key_len) {
87639beb93cSSam Leffler case 16:
87739beb93cSSam Leffler cipher = EVP_aes_128_cbc();
87839beb93cSSam Leffler break;
8795b9c547cSRui Paulo #ifndef OPENSSL_IS_BORINGSSL
88039beb93cSSam Leffler case 24:
88139beb93cSSam Leffler cipher = EVP_aes_192_cbc();
88239beb93cSSam Leffler break;
8835b9c547cSRui Paulo #endif /* OPENSSL_IS_BORINGSSL */
88439beb93cSSam Leffler case 32:
88539beb93cSSam Leffler cipher = EVP_aes_256_cbc();
88639beb93cSSam Leffler break;
88739beb93cSSam Leffler default:
88839beb93cSSam Leffler os_free(ctx);
88939beb93cSSam Leffler return NULL;
89039beb93cSSam Leffler }
89139beb93cSSam Leffler break;
89239beb93cSSam Leffler #endif /* OPENSSL_NO_AES */
89339beb93cSSam Leffler #ifndef OPENSSL_NO_DES
89439beb93cSSam Leffler case CRYPTO_CIPHER_ALG_3DES:
89539beb93cSSam Leffler cipher = EVP_des_ede3_cbc();
89639beb93cSSam Leffler break;
89739beb93cSSam Leffler case CRYPTO_CIPHER_ALG_DES:
89839beb93cSSam Leffler cipher = EVP_des_cbc();
89939beb93cSSam Leffler break;
90039beb93cSSam Leffler #endif /* OPENSSL_NO_DES */
90139beb93cSSam Leffler #ifndef OPENSSL_NO_RC2
90239beb93cSSam Leffler case CRYPTO_CIPHER_ALG_RC2:
90339beb93cSSam Leffler cipher = EVP_rc2_ecb();
90439beb93cSSam Leffler break;
90539beb93cSSam Leffler #endif /* OPENSSL_NO_RC2 */
90639beb93cSSam Leffler default:
90739beb93cSSam Leffler os_free(ctx);
90839beb93cSSam Leffler return NULL;
90939beb93cSSam Leffler }
91039beb93cSSam Leffler
911780fb4a2SCy Schubert if (!(ctx->enc = EVP_CIPHER_CTX_new()) ||
912db0ac6deSCy Schubert !EVP_EncryptInit_ex(ctx->enc, cipher, NULL, NULL, NULL) ||
9134b72b91aSCy Schubert !EVP_CIPHER_CTX_set_padding(ctx->enc, 0) ||
914780fb4a2SCy Schubert !EVP_CIPHER_CTX_set_key_length(ctx->enc, key_len) ||
915780fb4a2SCy Schubert !EVP_EncryptInit_ex(ctx->enc, NULL, NULL, key, iv)) {
916780fb4a2SCy Schubert if (ctx->enc)
917780fb4a2SCy Schubert EVP_CIPHER_CTX_free(ctx->enc);
91839beb93cSSam Leffler os_free(ctx);
91939beb93cSSam Leffler return NULL;
92039beb93cSSam Leffler }
92139beb93cSSam Leffler
922780fb4a2SCy Schubert if (!(ctx->dec = EVP_CIPHER_CTX_new()) ||
923db0ac6deSCy Schubert !EVP_DecryptInit_ex(ctx->dec, cipher, NULL, NULL, NULL) ||
9244b72b91aSCy Schubert !EVP_CIPHER_CTX_set_padding(ctx->dec, 0) ||
925780fb4a2SCy Schubert !EVP_CIPHER_CTX_set_key_length(ctx->dec, key_len) ||
926780fb4a2SCy Schubert !EVP_DecryptInit_ex(ctx->dec, NULL, NULL, key, iv)) {
927780fb4a2SCy Schubert EVP_CIPHER_CTX_free(ctx->enc);
928780fb4a2SCy Schubert if (ctx->dec)
929780fb4a2SCy Schubert EVP_CIPHER_CTX_free(ctx->dec);
93039beb93cSSam Leffler os_free(ctx);
93139beb93cSSam Leffler return NULL;
93239beb93cSSam Leffler }
93339beb93cSSam Leffler
93439beb93cSSam Leffler return ctx;
93539beb93cSSam Leffler }
93639beb93cSSam Leffler
93739beb93cSSam Leffler
crypto_cipher_encrypt(struct crypto_cipher * ctx,const u8 * plain,u8 * crypt,size_t len)93839beb93cSSam Leffler int crypto_cipher_encrypt(struct crypto_cipher *ctx, const u8 *plain,
93939beb93cSSam Leffler u8 *crypt, size_t len)
94039beb93cSSam Leffler {
94139beb93cSSam Leffler int outl;
942780fb4a2SCy Schubert if (!EVP_EncryptUpdate(ctx->enc, crypt, &outl, plain, len))
94339beb93cSSam Leffler return -1;
94439beb93cSSam Leffler return 0;
94539beb93cSSam Leffler }
94639beb93cSSam Leffler
94739beb93cSSam Leffler
crypto_cipher_decrypt(struct crypto_cipher * ctx,const u8 * crypt,u8 * plain,size_t len)94839beb93cSSam Leffler int crypto_cipher_decrypt(struct crypto_cipher *ctx, const u8 *crypt,
94939beb93cSSam Leffler u8 *plain, size_t len)
95039beb93cSSam Leffler {
95139beb93cSSam Leffler int outl;
95239beb93cSSam Leffler outl = len;
953780fb4a2SCy Schubert if (!EVP_DecryptUpdate(ctx->dec, plain, &outl, crypt, len))
95439beb93cSSam Leffler return -1;
95539beb93cSSam Leffler return 0;
95639beb93cSSam Leffler }
95739beb93cSSam Leffler
95839beb93cSSam Leffler
crypto_cipher_deinit(struct crypto_cipher * ctx)95939beb93cSSam Leffler void crypto_cipher_deinit(struct crypto_cipher *ctx)
96039beb93cSSam Leffler {
961780fb4a2SCy Schubert EVP_CIPHER_CTX_free(ctx->enc);
962780fb4a2SCy Schubert EVP_CIPHER_CTX_free(ctx->dec);
96339beb93cSSam Leffler os_free(ctx);
96439beb93cSSam Leffler }
965e28a4053SRui Paulo
966e28a4053SRui Paulo
dh5_init(struct wpabuf ** priv,struct wpabuf ** publ)967e28a4053SRui Paulo void * dh5_init(struct wpabuf **priv, struct wpabuf **publ)
968e28a4053SRui Paulo {
969*a90b9d01SCy Schubert #if OPENSSL_VERSION_NUMBER < 0x10100000L
970e28a4053SRui Paulo DH *dh;
971e28a4053SRui Paulo struct wpabuf *pubkey = NULL, *privkey = NULL;
972e28a4053SRui Paulo size_t publen, privlen;
973e28a4053SRui Paulo
974e28a4053SRui Paulo *priv = NULL;
975780fb4a2SCy Schubert wpabuf_free(*publ);
976e28a4053SRui Paulo *publ = NULL;
977e28a4053SRui Paulo
978e28a4053SRui Paulo dh = DH_new();
979e28a4053SRui Paulo if (dh == NULL)
980e28a4053SRui Paulo return NULL;
981e28a4053SRui Paulo
982e28a4053SRui Paulo dh->g = BN_new();
983e28a4053SRui Paulo if (dh->g == NULL || BN_set_word(dh->g, 2) != 1)
984e28a4053SRui Paulo goto err;
985e28a4053SRui Paulo
986e28a4053SRui Paulo dh->p = get_group5_prime();
987e28a4053SRui Paulo if (dh->p == NULL)
988e28a4053SRui Paulo goto err;
989e28a4053SRui Paulo
9904bc52338SCy Schubert dh->q = get_group5_order();
9914bc52338SCy Schubert if (!dh->q)
9924bc52338SCy Schubert goto err;
9934bc52338SCy Schubert
994e28a4053SRui Paulo if (DH_generate_key(dh) != 1)
995e28a4053SRui Paulo goto err;
996e28a4053SRui Paulo
997e28a4053SRui Paulo publen = BN_num_bytes(dh->pub_key);
998e28a4053SRui Paulo pubkey = wpabuf_alloc(publen);
999e28a4053SRui Paulo if (pubkey == NULL)
1000e28a4053SRui Paulo goto err;
1001e28a4053SRui Paulo privlen = BN_num_bytes(dh->priv_key);
1002e28a4053SRui Paulo privkey = wpabuf_alloc(privlen);
1003e28a4053SRui Paulo if (privkey == NULL)
1004e28a4053SRui Paulo goto err;
1005e28a4053SRui Paulo
1006e28a4053SRui Paulo BN_bn2bin(dh->pub_key, wpabuf_put(pubkey, publen));
1007e28a4053SRui Paulo BN_bn2bin(dh->priv_key, wpabuf_put(privkey, privlen));
1008e28a4053SRui Paulo
1009e28a4053SRui Paulo *priv = privkey;
1010e28a4053SRui Paulo *publ = pubkey;
1011e28a4053SRui Paulo return dh;
1012e28a4053SRui Paulo
1013e28a4053SRui Paulo err:
10145b9c547cSRui Paulo wpabuf_clear_free(pubkey);
10155b9c547cSRui Paulo wpabuf_clear_free(privkey);
1016e28a4053SRui Paulo DH_free(dh);
1017e28a4053SRui Paulo return NULL;
1018*a90b9d01SCy Schubert #elif OPENSSL_VERSION_NUMBER >= 0x30000000L
1019*a90b9d01SCy Schubert EVP_PKEY *pkey = NULL;
1020*a90b9d01SCy Schubert OSSL_PARAM params[2];
1021*a90b9d01SCy Schubert size_t pub_len = OSSL_PARAM_UNMODIFIED;
1022*a90b9d01SCy Schubert size_t priv_len;
1023*a90b9d01SCy Schubert struct wpabuf *pubkey = NULL, *privkey = NULL;
1024*a90b9d01SCy Schubert BIGNUM *priv_bn = NULL;
1025*a90b9d01SCy Schubert EVP_PKEY_CTX *gctx;
1026*a90b9d01SCy Schubert
1027*a90b9d01SCy Schubert *priv = NULL;
1028*a90b9d01SCy Schubert wpabuf_free(*publ);
1029*a90b9d01SCy Schubert *publ = NULL;
1030*a90b9d01SCy Schubert
1031*a90b9d01SCy Schubert params[0] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME,
1032*a90b9d01SCy Schubert "modp_1536", 0);
1033*a90b9d01SCy Schubert params[1] = OSSL_PARAM_construct_end();
1034*a90b9d01SCy Schubert
1035*a90b9d01SCy Schubert gctx = EVP_PKEY_CTX_new_from_name(NULL, "DH", NULL);
1036*a90b9d01SCy Schubert if (!gctx ||
1037*a90b9d01SCy Schubert EVP_PKEY_keygen_init(gctx) != 1 ||
1038*a90b9d01SCy Schubert EVP_PKEY_CTX_set_params(gctx, params) != 1 ||
1039*a90b9d01SCy Schubert EVP_PKEY_generate(gctx, &pkey) != 1 ||
1040*a90b9d01SCy Schubert EVP_PKEY_get_bn_param(pkey, OSSL_PKEY_PARAM_PRIV_KEY,
1041*a90b9d01SCy Schubert &priv_bn) != 1 ||
1042*a90b9d01SCy Schubert EVP_PKEY_get_octet_string_param(pkey,
1043*a90b9d01SCy Schubert OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY,
1044*a90b9d01SCy Schubert NULL, 0, &pub_len) < 0 ||
1045*a90b9d01SCy Schubert pub_len == OSSL_PARAM_UNMODIFIED ||
1046*a90b9d01SCy Schubert (priv_len = BN_num_bytes(priv_bn)) == 0 ||
1047*a90b9d01SCy Schubert !(pubkey = wpabuf_alloc(pub_len)) ||
1048*a90b9d01SCy Schubert !(privkey = wpabuf_alloc(priv_len)) ||
1049*a90b9d01SCy Schubert EVP_PKEY_get_octet_string_param(pkey,
1050*a90b9d01SCy Schubert OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY,
1051*a90b9d01SCy Schubert wpabuf_put(pubkey, pub_len),
1052*a90b9d01SCy Schubert pub_len, NULL) != 1) {
1053*a90b9d01SCy Schubert wpa_printf(MSG_INFO, "OpenSSL: failed: %s",
1054*a90b9d01SCy Schubert ERR_error_string(ERR_get_error(), NULL));
1055*a90b9d01SCy Schubert wpabuf_free(pubkey);
1056*a90b9d01SCy Schubert wpabuf_clear_free(privkey);
1057*a90b9d01SCy Schubert EVP_PKEY_free(pkey);
1058*a90b9d01SCy Schubert pkey = NULL;
1059*a90b9d01SCy Schubert } else {
1060*a90b9d01SCy Schubert BN_bn2bin(priv_bn, wpabuf_put(privkey, priv_len));
1061*a90b9d01SCy Schubert
1062*a90b9d01SCy Schubert *priv = privkey;
1063*a90b9d01SCy Schubert *publ = pubkey;
1064*a90b9d01SCy Schubert }
1065*a90b9d01SCy Schubert
1066*a90b9d01SCy Schubert BN_clear_free(priv_bn);
1067*a90b9d01SCy Schubert EVP_PKEY_CTX_free(gctx);
1068*a90b9d01SCy Schubert return pkey;
1069780fb4a2SCy Schubert #else
1070780fb4a2SCy Schubert DH *dh;
1071780fb4a2SCy Schubert struct wpabuf *pubkey = NULL, *privkey = NULL;
1072780fb4a2SCy Schubert size_t publen, privlen;
10734bc52338SCy Schubert BIGNUM *p, *g, *q;
1074780fb4a2SCy Schubert const BIGNUM *priv_key = NULL, *pub_key = NULL;
1075780fb4a2SCy Schubert
1076780fb4a2SCy Schubert *priv = NULL;
1077780fb4a2SCy Schubert wpabuf_free(*publ);
1078780fb4a2SCy Schubert *publ = NULL;
1079780fb4a2SCy Schubert
1080780fb4a2SCy Schubert dh = DH_new();
1081780fb4a2SCy Schubert if (dh == NULL)
1082780fb4a2SCy Schubert return NULL;
1083780fb4a2SCy Schubert
1084780fb4a2SCy Schubert g = BN_new();
1085780fb4a2SCy Schubert p = get_group5_prime();
10864bc52338SCy Schubert q = get_group5_order();
10874bc52338SCy Schubert if (!g || BN_set_word(g, 2) != 1 || !p || !q ||
10884bc52338SCy Schubert DH_set0_pqg(dh, p, q, g) != 1)
1089780fb4a2SCy Schubert goto err;
1090780fb4a2SCy Schubert p = NULL;
10914bc52338SCy Schubert q = NULL;
1092780fb4a2SCy Schubert g = NULL;
1093780fb4a2SCy Schubert
1094780fb4a2SCy Schubert if (DH_generate_key(dh) != 1)
1095780fb4a2SCy Schubert goto err;
1096780fb4a2SCy Schubert
1097780fb4a2SCy Schubert DH_get0_key(dh, &pub_key, &priv_key);
1098780fb4a2SCy Schubert publen = BN_num_bytes(pub_key);
1099780fb4a2SCy Schubert pubkey = wpabuf_alloc(publen);
1100780fb4a2SCy Schubert if (!pubkey)
1101780fb4a2SCy Schubert goto err;
1102780fb4a2SCy Schubert privlen = BN_num_bytes(priv_key);
1103780fb4a2SCy Schubert privkey = wpabuf_alloc(privlen);
1104780fb4a2SCy Schubert if (!privkey)
1105780fb4a2SCy Schubert goto err;
1106780fb4a2SCy Schubert
1107780fb4a2SCy Schubert BN_bn2bin(pub_key, wpabuf_put(pubkey, publen));
1108780fb4a2SCy Schubert BN_bn2bin(priv_key, wpabuf_put(privkey, privlen));
1109780fb4a2SCy Schubert
1110780fb4a2SCy Schubert *priv = privkey;
1111780fb4a2SCy Schubert *publ = pubkey;
1112780fb4a2SCy Schubert return dh;
1113780fb4a2SCy Schubert
1114780fb4a2SCy Schubert err:
1115780fb4a2SCy Schubert BN_free(p);
11164bc52338SCy Schubert BN_free(q);
1117780fb4a2SCy Schubert BN_free(g);
1118780fb4a2SCy Schubert wpabuf_clear_free(pubkey);
1119780fb4a2SCy Schubert wpabuf_clear_free(privkey);
1120780fb4a2SCy Schubert DH_free(dh);
1121780fb4a2SCy Schubert return NULL;
1122780fb4a2SCy Schubert #endif
1123e28a4053SRui Paulo }
1124e28a4053SRui Paulo
1125e28a4053SRui Paulo
dh5_init_fixed(const struct wpabuf * priv,const struct wpabuf * publ)1126f05cddf9SRui Paulo void * dh5_init_fixed(const struct wpabuf *priv, const struct wpabuf *publ)
1127f05cddf9SRui Paulo {
1128*a90b9d01SCy Schubert #if OPENSSL_VERSION_NUMBER < 0x10100000L
1129f05cddf9SRui Paulo DH *dh;
1130f05cddf9SRui Paulo
1131f05cddf9SRui Paulo dh = DH_new();
1132f05cddf9SRui Paulo if (dh == NULL)
1133f05cddf9SRui Paulo return NULL;
1134f05cddf9SRui Paulo
1135f05cddf9SRui Paulo dh->g = BN_new();
1136f05cddf9SRui Paulo if (dh->g == NULL || BN_set_word(dh->g, 2) != 1)
1137f05cddf9SRui Paulo goto err;
1138f05cddf9SRui Paulo
1139f05cddf9SRui Paulo dh->p = get_group5_prime();
1140f05cddf9SRui Paulo if (dh->p == NULL)
1141f05cddf9SRui Paulo goto err;
1142f05cddf9SRui Paulo
1143f05cddf9SRui Paulo dh->priv_key = BN_bin2bn(wpabuf_head(priv), wpabuf_len(priv), NULL);
1144f05cddf9SRui Paulo if (dh->priv_key == NULL)
1145f05cddf9SRui Paulo goto err;
1146f05cddf9SRui Paulo
1147f05cddf9SRui Paulo dh->pub_key = BN_bin2bn(wpabuf_head(publ), wpabuf_len(publ), NULL);
1148f05cddf9SRui Paulo if (dh->pub_key == NULL)
1149f05cddf9SRui Paulo goto err;
1150f05cddf9SRui Paulo
1151f05cddf9SRui Paulo if (DH_generate_key(dh) != 1)
1152f05cddf9SRui Paulo goto err;
1153f05cddf9SRui Paulo
1154f05cddf9SRui Paulo return dh;
1155f05cddf9SRui Paulo
1156f05cddf9SRui Paulo err:
1157f05cddf9SRui Paulo DH_free(dh);
1158f05cddf9SRui Paulo return NULL;
1159*a90b9d01SCy Schubert #elif OPENSSL_VERSION_NUMBER >= 0x30000000L
1160*a90b9d01SCy Schubert EVP_PKEY *pkey = NULL;
1161*a90b9d01SCy Schubert OSSL_PARAM_BLD *bld;
1162*a90b9d01SCy Schubert OSSL_PARAM *params = NULL;
1163*a90b9d01SCy Schubert BIGNUM *priv_key, *pub_key;
1164*a90b9d01SCy Schubert EVP_PKEY_CTX *fctx;
1165*a90b9d01SCy Schubert
1166*a90b9d01SCy Schubert fctx = EVP_PKEY_CTX_new_from_name(NULL, "DH", NULL);
1167*a90b9d01SCy Schubert priv_key = BN_bin2bn(wpabuf_head(priv), wpabuf_len(priv), NULL);
1168*a90b9d01SCy Schubert pub_key = BN_bin2bn(wpabuf_head(publ), wpabuf_len(publ), NULL);
1169*a90b9d01SCy Schubert bld = OSSL_PARAM_BLD_new();
1170*a90b9d01SCy Schubert if (!fctx || !priv_key || !pub_key || !bld ||
1171*a90b9d01SCy Schubert OSSL_PARAM_BLD_push_utf8_string(bld, OSSL_PKEY_PARAM_GROUP_NAME,
1172*a90b9d01SCy Schubert "modp_1536", 0) != 1 ||
1173*a90b9d01SCy Schubert OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PRIV_KEY,
1174*a90b9d01SCy Schubert priv_key) != 1 ||
1175*a90b9d01SCy Schubert OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PUB_KEY,
1176*a90b9d01SCy Schubert pub_key) != 1 ||
1177*a90b9d01SCy Schubert !(params = OSSL_PARAM_BLD_to_param(bld)) ||
1178*a90b9d01SCy Schubert EVP_PKEY_fromdata_init(fctx) != 1 ||
1179*a90b9d01SCy Schubert EVP_PKEY_fromdata(fctx, &pkey, EVP_PKEY_KEYPAIR, params) != 1) {
1180*a90b9d01SCy Schubert wpa_printf(MSG_INFO, "OpenSSL: EVP_PKEY_fromdata failed: %s",
1181*a90b9d01SCy Schubert ERR_error_string(ERR_get_error(), NULL));
1182*a90b9d01SCy Schubert EVP_PKEY_free(pkey);
1183*a90b9d01SCy Schubert pkey = NULL;
1184*a90b9d01SCy Schubert }
1185*a90b9d01SCy Schubert
1186*a90b9d01SCy Schubert BN_clear_free(priv_key);
1187*a90b9d01SCy Schubert BN_free(pub_key);
1188*a90b9d01SCy Schubert EVP_PKEY_CTX_free(fctx);
1189*a90b9d01SCy Schubert OSSL_PARAM_BLD_free(bld);
1190*a90b9d01SCy Schubert OSSL_PARAM_free(params);
1191*a90b9d01SCy Schubert return pkey;
1192780fb4a2SCy Schubert #else
1193780fb4a2SCy Schubert DH *dh;
1194780fb4a2SCy Schubert BIGNUM *p = NULL, *g, *priv_key = NULL, *pub_key = NULL;
1195780fb4a2SCy Schubert
1196780fb4a2SCy Schubert dh = DH_new();
1197780fb4a2SCy Schubert if (dh == NULL)
1198780fb4a2SCy Schubert return NULL;
1199780fb4a2SCy Schubert
1200780fb4a2SCy Schubert g = BN_new();
1201780fb4a2SCy Schubert p = get_group5_prime();
1202780fb4a2SCy Schubert if (!g || BN_set_word(g, 2) != 1 || !p ||
1203780fb4a2SCy Schubert DH_set0_pqg(dh, p, NULL, g) != 1)
1204780fb4a2SCy Schubert goto err;
1205780fb4a2SCy Schubert p = NULL;
1206780fb4a2SCy Schubert g = NULL;
1207780fb4a2SCy Schubert
1208780fb4a2SCy Schubert priv_key = BN_bin2bn(wpabuf_head(priv), wpabuf_len(priv), NULL);
1209780fb4a2SCy Schubert pub_key = BN_bin2bn(wpabuf_head(publ), wpabuf_len(publ), NULL);
1210780fb4a2SCy Schubert if (!priv_key || !pub_key || DH_set0_key(dh, pub_key, priv_key) != 1)
1211780fb4a2SCy Schubert goto err;
1212780fb4a2SCy Schubert pub_key = NULL;
1213780fb4a2SCy Schubert priv_key = NULL;
1214780fb4a2SCy Schubert
1215780fb4a2SCy Schubert if (DH_generate_key(dh) != 1)
1216780fb4a2SCy Schubert goto err;
1217780fb4a2SCy Schubert
1218780fb4a2SCy Schubert return dh;
1219780fb4a2SCy Schubert
1220780fb4a2SCy Schubert err:
1221780fb4a2SCy Schubert BN_free(p);
1222780fb4a2SCy Schubert BN_free(g);
1223780fb4a2SCy Schubert BN_free(pub_key);
1224780fb4a2SCy Schubert BN_clear_free(priv_key);
1225780fb4a2SCy Schubert DH_free(dh);
1226780fb4a2SCy Schubert return NULL;
1227780fb4a2SCy Schubert #endif
1228f05cddf9SRui Paulo }
1229f05cddf9SRui Paulo
1230f05cddf9SRui Paulo
dh5_derive_shared(void * ctx,const struct wpabuf * peer_public,const struct wpabuf * own_private)1231e28a4053SRui Paulo struct wpabuf * dh5_derive_shared(void *ctx, const struct wpabuf *peer_public,
1232e28a4053SRui Paulo const struct wpabuf *own_private)
1233e28a4053SRui Paulo {
1234*a90b9d01SCy Schubert #if OPENSSL_VERSION_NUMBER >= 0x30000000L
1235*a90b9d01SCy Schubert EVP_PKEY *pkey = ctx;
1236*a90b9d01SCy Schubert EVP_PKEY *peer_pub;
1237*a90b9d01SCy Schubert size_t len;
1238*a90b9d01SCy Schubert struct wpabuf *res = NULL;
1239*a90b9d01SCy Schubert EVP_PKEY_CTX *dctx = NULL;
1240*a90b9d01SCy Schubert
1241*a90b9d01SCy Schubert peer_pub = EVP_PKEY_new();
1242*a90b9d01SCy Schubert if (!pkey || !peer_pub ||
1243*a90b9d01SCy Schubert EVP_PKEY_copy_parameters(peer_pub, pkey) != 1 ||
1244*a90b9d01SCy Schubert EVP_PKEY_set1_encoded_public_key(peer_pub, wpabuf_head(peer_public),
1245*a90b9d01SCy Schubert wpabuf_len(peer_public)) != 1 ||
1246*a90b9d01SCy Schubert !(dctx = EVP_PKEY_CTX_new(pkey, NULL)) ||
1247*a90b9d01SCy Schubert EVP_PKEY_derive_init(dctx) != 1 ||
1248*a90b9d01SCy Schubert EVP_PKEY_derive_set_peer(dctx, peer_pub) != 1 ||
1249*a90b9d01SCy Schubert EVP_PKEY_derive(dctx, NULL, &len) != 1 ||
1250*a90b9d01SCy Schubert !(res = wpabuf_alloc(len)) ||
1251*a90b9d01SCy Schubert EVP_PKEY_derive(dctx, wpabuf_mhead(res), &len) != 1) {
1252*a90b9d01SCy Schubert wpa_printf(MSG_INFO, "OpenSSL: EVP_PKEY_derive failed: %s",
1253*a90b9d01SCy Schubert ERR_error_string(ERR_get_error(), NULL));
1254*a90b9d01SCy Schubert wpabuf_free(res);
1255*a90b9d01SCy Schubert res = NULL;
1256*a90b9d01SCy Schubert } else {
1257*a90b9d01SCy Schubert wpabuf_put(res, len);
1258*a90b9d01SCy Schubert }
1259*a90b9d01SCy Schubert
1260*a90b9d01SCy Schubert EVP_PKEY_free(peer_pub);
1261*a90b9d01SCy Schubert EVP_PKEY_CTX_free(dctx);
1262*a90b9d01SCy Schubert return res;
1263*a90b9d01SCy Schubert #else /* OpenSSL version >= 3.0 */
1264e28a4053SRui Paulo BIGNUM *pub_key;
1265e28a4053SRui Paulo struct wpabuf *res = NULL;
1266e28a4053SRui Paulo size_t rlen;
1267e28a4053SRui Paulo DH *dh = ctx;
1268e28a4053SRui Paulo int keylen;
1269e28a4053SRui Paulo
1270e28a4053SRui Paulo if (ctx == NULL)
1271e28a4053SRui Paulo return NULL;
1272e28a4053SRui Paulo
1273e28a4053SRui Paulo pub_key = BN_bin2bn(wpabuf_head(peer_public), wpabuf_len(peer_public),
1274e28a4053SRui Paulo NULL);
1275e28a4053SRui Paulo if (pub_key == NULL)
1276e28a4053SRui Paulo return NULL;
1277e28a4053SRui Paulo
1278e28a4053SRui Paulo rlen = DH_size(dh);
1279e28a4053SRui Paulo res = wpabuf_alloc(rlen);
1280e28a4053SRui Paulo if (res == NULL)
1281e28a4053SRui Paulo goto err;
1282e28a4053SRui Paulo
1283e28a4053SRui Paulo keylen = DH_compute_key(wpabuf_mhead(res), pub_key, dh);
1284e28a4053SRui Paulo if (keylen < 0)
1285e28a4053SRui Paulo goto err;
1286e28a4053SRui Paulo wpabuf_put(res, keylen);
12875b9c547cSRui Paulo BN_clear_free(pub_key);
1288e28a4053SRui Paulo
1289e28a4053SRui Paulo return res;
1290e28a4053SRui Paulo
1291e28a4053SRui Paulo err:
12925b9c547cSRui Paulo BN_clear_free(pub_key);
12935b9c547cSRui Paulo wpabuf_clear_free(res);
1294e28a4053SRui Paulo return NULL;
1295*a90b9d01SCy Schubert #endif /* OpenSSL version >= 3.0 */
1296e28a4053SRui Paulo }
1297e28a4053SRui Paulo
1298e28a4053SRui Paulo
dh5_free(void * ctx)1299e28a4053SRui Paulo void dh5_free(void *ctx)
1300e28a4053SRui Paulo {
1301*a90b9d01SCy Schubert #if OPENSSL_VERSION_NUMBER >= 0x30000000L
1302*a90b9d01SCy Schubert EVP_PKEY *pkey = ctx;
1303*a90b9d01SCy Schubert
1304*a90b9d01SCy Schubert EVP_PKEY_free(pkey);
1305*a90b9d01SCy Schubert #else /* OpenSSL version >= 3.0 */
1306e28a4053SRui Paulo DH *dh;
1307e28a4053SRui Paulo if (ctx == NULL)
1308e28a4053SRui Paulo return;
1309e28a4053SRui Paulo dh = ctx;
1310e28a4053SRui Paulo DH_free(dh);
1311*a90b9d01SCy Schubert #endif /* OpenSSL version >= 3.0 */
1312e28a4053SRui Paulo }
1313f05cddf9SRui Paulo
1314f05cddf9SRui Paulo
1315f05cddf9SRui Paulo struct crypto_hash {
1316*a90b9d01SCy Schubert #if OPENSSL_VERSION_NUMBER >= 0x30000000L
1317*a90b9d01SCy Schubert EVP_MAC_CTX *ctx;
1318*a90b9d01SCy Schubert #else /* OpenSSL version >= 3.0 */
1319780fb4a2SCy Schubert HMAC_CTX *ctx;
1320*a90b9d01SCy Schubert #endif /* OpenSSL version >= 3.0 */
1321*a90b9d01SCy Schubert bool failed;
1322f05cddf9SRui Paulo };
1323f05cddf9SRui Paulo
1324f05cddf9SRui Paulo
crypto_hash_init(enum crypto_hash_alg alg,const u8 * key,size_t key_len)1325f05cddf9SRui Paulo struct crypto_hash * crypto_hash_init(enum crypto_hash_alg alg, const u8 *key,
1326f05cddf9SRui Paulo size_t key_len)
1327f05cddf9SRui Paulo {
1328*a90b9d01SCy Schubert #if OPENSSL_VERSION_NUMBER >= 0x30000000L
1329*a90b9d01SCy Schubert struct crypto_hash *ctx;
1330*a90b9d01SCy Schubert EVP_MAC *mac;
1331*a90b9d01SCy Schubert OSSL_PARAM params[2];
1332*a90b9d01SCy Schubert char *a = NULL;
1333*a90b9d01SCy Schubert
1334*a90b9d01SCy Schubert switch (alg) {
1335*a90b9d01SCy Schubert #ifndef OPENSSL_NO_MD5
1336*a90b9d01SCy Schubert case CRYPTO_HASH_ALG_HMAC_MD5:
1337*a90b9d01SCy Schubert a = "MD5";
1338*a90b9d01SCy Schubert break;
1339*a90b9d01SCy Schubert #endif /* OPENSSL_NO_MD5 */
1340*a90b9d01SCy Schubert #ifndef OPENSSL_NO_SHA
1341*a90b9d01SCy Schubert case CRYPTO_HASH_ALG_HMAC_SHA1:
1342*a90b9d01SCy Schubert a = "SHA1";
1343*a90b9d01SCy Schubert break;
1344*a90b9d01SCy Schubert #endif /* OPENSSL_NO_SHA */
1345*a90b9d01SCy Schubert #ifndef OPENSSL_NO_SHA256
1346*a90b9d01SCy Schubert #ifdef CONFIG_SHA256
1347*a90b9d01SCy Schubert case CRYPTO_HASH_ALG_HMAC_SHA256:
1348*a90b9d01SCy Schubert a = "SHA256";
1349*a90b9d01SCy Schubert break;
1350*a90b9d01SCy Schubert #endif /* CONFIG_SHA256 */
1351*a90b9d01SCy Schubert #endif /* OPENSSL_NO_SHA256 */
1352*a90b9d01SCy Schubert default:
1353*a90b9d01SCy Schubert return NULL;
1354*a90b9d01SCy Schubert }
1355*a90b9d01SCy Schubert
1356*a90b9d01SCy Schubert mac = EVP_MAC_fetch(NULL, "HMAC", NULL);
1357*a90b9d01SCy Schubert if (!mac)
1358*a90b9d01SCy Schubert return NULL;
1359*a90b9d01SCy Schubert
1360*a90b9d01SCy Schubert params[0] = OSSL_PARAM_construct_utf8_string("digest", a, 0);
1361*a90b9d01SCy Schubert params[1] = OSSL_PARAM_construct_end();
1362*a90b9d01SCy Schubert
1363*a90b9d01SCy Schubert ctx = os_zalloc(sizeof(*ctx));
1364*a90b9d01SCy Schubert if (!ctx)
1365*a90b9d01SCy Schubert goto fail;
1366*a90b9d01SCy Schubert ctx->ctx = EVP_MAC_CTX_new(mac);
1367*a90b9d01SCy Schubert if (!ctx->ctx) {
1368*a90b9d01SCy Schubert os_free(ctx);
1369*a90b9d01SCy Schubert ctx = NULL;
1370*a90b9d01SCy Schubert goto fail;
1371*a90b9d01SCy Schubert }
1372*a90b9d01SCy Schubert
1373*a90b9d01SCy Schubert if (EVP_MAC_init(ctx->ctx, key, key_len, params) != 1) {
1374*a90b9d01SCy Schubert EVP_MAC_CTX_free(ctx->ctx);
1375*a90b9d01SCy Schubert bin_clear_free(ctx, sizeof(*ctx));
1376*a90b9d01SCy Schubert ctx = NULL;
1377*a90b9d01SCy Schubert goto fail;
1378*a90b9d01SCy Schubert }
1379*a90b9d01SCy Schubert
1380*a90b9d01SCy Schubert fail:
1381*a90b9d01SCy Schubert EVP_MAC_free(mac);
1382*a90b9d01SCy Schubert return ctx;
1383*a90b9d01SCy Schubert #else /* OpenSSL version >= 3.0 */
1384f05cddf9SRui Paulo struct crypto_hash *ctx;
1385f05cddf9SRui Paulo const EVP_MD *md;
1386f05cddf9SRui Paulo
1387f05cddf9SRui Paulo switch (alg) {
1388f05cddf9SRui Paulo #ifndef OPENSSL_NO_MD5
1389f05cddf9SRui Paulo case CRYPTO_HASH_ALG_HMAC_MD5:
1390f05cddf9SRui Paulo md = EVP_md5();
1391f05cddf9SRui Paulo break;
1392f05cddf9SRui Paulo #endif /* OPENSSL_NO_MD5 */
1393f05cddf9SRui Paulo #ifndef OPENSSL_NO_SHA
1394f05cddf9SRui Paulo case CRYPTO_HASH_ALG_HMAC_SHA1:
1395f05cddf9SRui Paulo md = EVP_sha1();
1396f05cddf9SRui Paulo break;
1397f05cddf9SRui Paulo #endif /* OPENSSL_NO_SHA */
1398f05cddf9SRui Paulo #ifndef OPENSSL_NO_SHA256
1399f05cddf9SRui Paulo #ifdef CONFIG_SHA256
1400f05cddf9SRui Paulo case CRYPTO_HASH_ALG_HMAC_SHA256:
1401f05cddf9SRui Paulo md = EVP_sha256();
1402f05cddf9SRui Paulo break;
1403f05cddf9SRui Paulo #endif /* CONFIG_SHA256 */
1404f05cddf9SRui Paulo #endif /* OPENSSL_NO_SHA256 */
1405f05cddf9SRui Paulo default:
1406f05cddf9SRui Paulo return NULL;
1407f05cddf9SRui Paulo }
1408f05cddf9SRui Paulo
1409f05cddf9SRui Paulo ctx = os_zalloc(sizeof(*ctx));
1410f05cddf9SRui Paulo if (ctx == NULL)
1411f05cddf9SRui Paulo return NULL;
1412780fb4a2SCy Schubert ctx->ctx = HMAC_CTX_new();
1413780fb4a2SCy Schubert if (!ctx->ctx) {
1414780fb4a2SCy Schubert os_free(ctx);
1415780fb4a2SCy Schubert return NULL;
1416780fb4a2SCy Schubert }
1417f05cddf9SRui Paulo
1418780fb4a2SCy Schubert if (HMAC_Init_ex(ctx->ctx, key, key_len, md, NULL) != 1) {
1419780fb4a2SCy Schubert HMAC_CTX_free(ctx->ctx);
14205b9c547cSRui Paulo bin_clear_free(ctx, sizeof(*ctx));
1421f05cddf9SRui Paulo return NULL;
1422f05cddf9SRui Paulo }
1423f05cddf9SRui Paulo
1424f05cddf9SRui Paulo return ctx;
1425*a90b9d01SCy Schubert #endif /* OpenSSL version >= 3.0 */
1426f05cddf9SRui Paulo }
1427f05cddf9SRui Paulo
1428f05cddf9SRui Paulo
crypto_hash_update(struct crypto_hash * ctx,const u8 * data,size_t len)1429f05cddf9SRui Paulo void crypto_hash_update(struct crypto_hash *ctx, const u8 *data, size_t len)
1430f05cddf9SRui Paulo {
1431f05cddf9SRui Paulo if (ctx == NULL)
1432f05cddf9SRui Paulo return;
1433*a90b9d01SCy Schubert #if OPENSSL_VERSION_NUMBER >= 0x30000000L
1434*a90b9d01SCy Schubert if (!EVP_MAC_update(ctx->ctx, data, len))
1435*a90b9d01SCy Schubert ctx->failed = true;
1436*a90b9d01SCy Schubert #else /* OpenSSL version >= 3.0 */
1437*a90b9d01SCy Schubert if (!HMAC_Update(ctx->ctx, data, len))
1438*a90b9d01SCy Schubert ctx->failed = true;
1439*a90b9d01SCy Schubert #endif /* OpenSSL version >= 3.0 */
1440f05cddf9SRui Paulo }
1441f05cddf9SRui Paulo
1442f05cddf9SRui Paulo
crypto_hash_finish(struct crypto_hash * ctx,u8 * mac,size_t * len)1443f05cddf9SRui Paulo int crypto_hash_finish(struct crypto_hash *ctx, u8 *mac, size_t *len)
1444f05cddf9SRui Paulo {
1445*a90b9d01SCy Schubert #if OPENSSL_VERSION_NUMBER >= 0x30000000L
1446*a90b9d01SCy Schubert size_t mdlen;
1447*a90b9d01SCy Schubert int res;
1448*a90b9d01SCy Schubert bool failed;
1449*a90b9d01SCy Schubert
1450*a90b9d01SCy Schubert if (!ctx)
1451*a90b9d01SCy Schubert return -2;
1452*a90b9d01SCy Schubert
1453*a90b9d01SCy Schubert if (!mac || !len) {
1454*a90b9d01SCy Schubert EVP_MAC_CTX_free(ctx->ctx);
1455*a90b9d01SCy Schubert bin_clear_free(ctx, sizeof(*ctx));
1456*a90b9d01SCy Schubert return 0;
1457*a90b9d01SCy Schubert }
1458*a90b9d01SCy Schubert
1459*a90b9d01SCy Schubert res = EVP_MAC_final(ctx->ctx, NULL, &mdlen, 0);
1460*a90b9d01SCy Schubert if (res != 1) {
1461*a90b9d01SCy Schubert EVP_MAC_CTX_free(ctx->ctx);
1462*a90b9d01SCy Schubert bin_clear_free(ctx, sizeof(*ctx));
1463*a90b9d01SCy Schubert return -1;
1464*a90b9d01SCy Schubert }
1465*a90b9d01SCy Schubert res = EVP_MAC_final(ctx->ctx, mac, &mdlen, mdlen);
1466*a90b9d01SCy Schubert EVP_MAC_CTX_free(ctx->ctx);
1467*a90b9d01SCy Schubert failed = ctx->failed;
1468*a90b9d01SCy Schubert bin_clear_free(ctx, sizeof(*ctx));
1469*a90b9d01SCy Schubert
1470*a90b9d01SCy Schubert if (TEST_FAIL())
1471*a90b9d01SCy Schubert return -1;
1472*a90b9d01SCy Schubert
1473*a90b9d01SCy Schubert if (failed)
1474*a90b9d01SCy Schubert return -2;
1475*a90b9d01SCy Schubert
1476*a90b9d01SCy Schubert if (res == 1) {
1477*a90b9d01SCy Schubert *len = mdlen;
1478*a90b9d01SCy Schubert return 0;
1479*a90b9d01SCy Schubert }
1480*a90b9d01SCy Schubert
1481*a90b9d01SCy Schubert return -1;
1482*a90b9d01SCy Schubert #else /* OpenSSL version >= 3.0 */
1483f05cddf9SRui Paulo unsigned int mdlen;
1484f05cddf9SRui Paulo int res;
1485*a90b9d01SCy Schubert bool failed;
1486f05cddf9SRui Paulo
1487f05cddf9SRui Paulo if (ctx == NULL)
1488f05cddf9SRui Paulo return -2;
1489f05cddf9SRui Paulo
1490f05cddf9SRui Paulo if (mac == NULL || len == NULL) {
1491780fb4a2SCy Schubert HMAC_CTX_free(ctx->ctx);
14925b9c547cSRui Paulo bin_clear_free(ctx, sizeof(*ctx));
1493f05cddf9SRui Paulo return 0;
1494f05cddf9SRui Paulo }
1495f05cddf9SRui Paulo
1496f05cddf9SRui Paulo mdlen = *len;
1497780fb4a2SCy Schubert res = HMAC_Final(ctx->ctx, mac, &mdlen);
1498780fb4a2SCy Schubert HMAC_CTX_free(ctx->ctx);
1499*a90b9d01SCy Schubert failed = ctx->failed;
15005b9c547cSRui Paulo bin_clear_free(ctx, sizeof(*ctx));
1501f05cddf9SRui Paulo
15024bc52338SCy Schubert if (TEST_FAIL())
15034bc52338SCy Schubert return -1;
15044bc52338SCy Schubert
1505*a90b9d01SCy Schubert if (failed)
1506*a90b9d01SCy Schubert return -2;
1507*a90b9d01SCy Schubert
1508f05cddf9SRui Paulo if (res == 1) {
1509f05cddf9SRui Paulo *len = mdlen;
1510f05cddf9SRui Paulo return 0;
1511f05cddf9SRui Paulo }
1512f05cddf9SRui Paulo
1513f05cddf9SRui Paulo return -1;
1514*a90b9d01SCy Schubert #endif /* OpenSSL version >= 3.0 */
1515f05cddf9SRui Paulo }
1516f05cddf9SRui Paulo
1517f05cddf9SRui Paulo
1518*a90b9d01SCy Schubert #if OPENSSL_VERSION_NUMBER >= 0x30000000L
1519*a90b9d01SCy Schubert
openssl_hmac_vector(char * digest,const u8 * key,size_t key_len,size_t num_elem,const u8 * addr[],const size_t * len,u8 * mac,unsigned int mdlen)1520*a90b9d01SCy Schubert static int openssl_hmac_vector(char *digest, const u8 *key,
1521*a90b9d01SCy Schubert size_t key_len, size_t num_elem,
1522*a90b9d01SCy Schubert const u8 *addr[], const size_t *len, u8 *mac,
1523*a90b9d01SCy Schubert unsigned int mdlen)
1524*a90b9d01SCy Schubert {
1525*a90b9d01SCy Schubert EVP_MAC *hmac;
1526*a90b9d01SCy Schubert OSSL_PARAM params[2];
1527*a90b9d01SCy Schubert EVP_MAC_CTX *ctx;
1528*a90b9d01SCy Schubert size_t i, mlen;
1529*a90b9d01SCy Schubert int res;
1530*a90b9d01SCy Schubert
1531*a90b9d01SCy Schubert if (TEST_FAIL())
1532*a90b9d01SCy Schubert return -1;
1533*a90b9d01SCy Schubert
1534*a90b9d01SCy Schubert hmac = EVP_MAC_fetch(NULL, "HMAC", NULL);
1535*a90b9d01SCy Schubert if (!hmac)
1536*a90b9d01SCy Schubert return -1;
1537*a90b9d01SCy Schubert
1538*a90b9d01SCy Schubert params[0] = OSSL_PARAM_construct_utf8_string("digest", digest, 0);
1539*a90b9d01SCy Schubert params[1] = OSSL_PARAM_construct_end();
1540*a90b9d01SCy Schubert
1541*a90b9d01SCy Schubert ctx = EVP_MAC_CTX_new(hmac);
1542*a90b9d01SCy Schubert EVP_MAC_free(hmac);
1543*a90b9d01SCy Schubert if (!ctx)
1544*a90b9d01SCy Schubert return -1;
1545*a90b9d01SCy Schubert
1546*a90b9d01SCy Schubert if (EVP_MAC_init(ctx, key, key_len, params) != 1)
1547*a90b9d01SCy Schubert goto fail;
1548*a90b9d01SCy Schubert
1549*a90b9d01SCy Schubert for (i = 0; i < num_elem; i++) {
1550*a90b9d01SCy Schubert if (EVP_MAC_update(ctx, addr[i], len[i]) != 1)
1551*a90b9d01SCy Schubert goto fail;
1552*a90b9d01SCy Schubert }
1553*a90b9d01SCy Schubert
1554*a90b9d01SCy Schubert res = EVP_MAC_final(ctx, mac, &mlen, mdlen);
1555*a90b9d01SCy Schubert EVP_MAC_CTX_free(ctx);
1556*a90b9d01SCy Schubert
1557*a90b9d01SCy Schubert return res == 1 ? 0 : -1;
1558*a90b9d01SCy Schubert fail:
1559*a90b9d01SCy Schubert EVP_MAC_CTX_free(ctx);
1560*a90b9d01SCy Schubert return -1;
1561*a90b9d01SCy Schubert }
1562*a90b9d01SCy Schubert
1563*a90b9d01SCy Schubert
1564*a90b9d01SCy Schubert #ifndef CONFIG_FIPS
1565*a90b9d01SCy Schubert
hmac_md5_vector(const u8 * key,size_t key_len,size_t num_elem,const u8 * addr[],const size_t * len,u8 * mac)1566*a90b9d01SCy Schubert int hmac_md5_vector(const u8 *key, size_t key_len, size_t num_elem,
1567*a90b9d01SCy Schubert const u8 *addr[], const size_t *len, u8 *mac)
1568*a90b9d01SCy Schubert {
1569*a90b9d01SCy Schubert return openssl_hmac_vector("MD5", key ,key_len, num_elem, addr, len,
1570*a90b9d01SCy Schubert mac, 16);
1571*a90b9d01SCy Schubert }
1572*a90b9d01SCy Schubert
1573*a90b9d01SCy Schubert
hmac_md5(const u8 * key,size_t key_len,const u8 * data,size_t data_len,u8 * mac)1574*a90b9d01SCy Schubert int hmac_md5(const u8 *key, size_t key_len, const u8 *data, size_t data_len,
1575*a90b9d01SCy Schubert u8 *mac)
1576*a90b9d01SCy Schubert {
1577*a90b9d01SCy Schubert return hmac_md5_vector(key, key_len, 1, &data, &data_len, mac);
1578*a90b9d01SCy Schubert }
1579*a90b9d01SCy Schubert
1580*a90b9d01SCy Schubert #endif /* CONFIG_FIPS */
1581*a90b9d01SCy Schubert
1582*a90b9d01SCy Schubert
hmac_sha1_vector(const u8 * key,size_t key_len,size_t num_elem,const u8 * addr[],const size_t * len,u8 * mac)1583*a90b9d01SCy Schubert int hmac_sha1_vector(const u8 *key, size_t key_len, size_t num_elem,
1584*a90b9d01SCy Schubert const u8 *addr[], const size_t *len, u8 *mac)
1585*a90b9d01SCy Schubert {
1586*a90b9d01SCy Schubert return openssl_hmac_vector("SHA1", key, key_len, num_elem, addr,
1587*a90b9d01SCy Schubert len, mac, 20);
1588*a90b9d01SCy Schubert }
1589*a90b9d01SCy Schubert
1590*a90b9d01SCy Schubert
hmac_sha1(const u8 * key,size_t key_len,const u8 * data,size_t data_len,u8 * mac)1591*a90b9d01SCy Schubert int hmac_sha1(const u8 *key, size_t key_len, const u8 *data, size_t data_len,
1592*a90b9d01SCy Schubert u8 *mac)
1593*a90b9d01SCy Schubert {
1594*a90b9d01SCy Schubert return hmac_sha1_vector(key, key_len, 1, &data, &data_len, mac);
1595*a90b9d01SCy Schubert }
1596*a90b9d01SCy Schubert
1597*a90b9d01SCy Schubert
1598*a90b9d01SCy Schubert #ifdef CONFIG_SHA256
1599*a90b9d01SCy Schubert
hmac_sha256_vector(const u8 * key,size_t key_len,size_t num_elem,const u8 * addr[],const size_t * len,u8 * mac)1600*a90b9d01SCy Schubert int hmac_sha256_vector(const u8 *key, size_t key_len, size_t num_elem,
1601*a90b9d01SCy Schubert const u8 *addr[], const size_t *len, u8 *mac)
1602*a90b9d01SCy Schubert {
1603*a90b9d01SCy Schubert return openssl_hmac_vector("SHA256", key, key_len, num_elem, addr,
1604*a90b9d01SCy Schubert len, mac, 32);
1605*a90b9d01SCy Schubert }
1606*a90b9d01SCy Schubert
1607*a90b9d01SCy Schubert
hmac_sha256(const u8 * key,size_t key_len,const u8 * data,size_t data_len,u8 * mac)1608*a90b9d01SCy Schubert int hmac_sha256(const u8 *key, size_t key_len, const u8 *data,
1609*a90b9d01SCy Schubert size_t data_len, u8 *mac)
1610*a90b9d01SCy Schubert {
1611*a90b9d01SCy Schubert return hmac_sha256_vector(key, key_len, 1, &data, &data_len, mac);
1612*a90b9d01SCy Schubert }
1613*a90b9d01SCy Schubert
1614*a90b9d01SCy Schubert #endif /* CONFIG_SHA256 */
1615*a90b9d01SCy Schubert
1616*a90b9d01SCy Schubert
1617*a90b9d01SCy Schubert #ifdef CONFIG_SHA384
1618*a90b9d01SCy Schubert
hmac_sha384_vector(const u8 * key,size_t key_len,size_t num_elem,const u8 * addr[],const size_t * len,u8 * mac)1619*a90b9d01SCy Schubert int hmac_sha384_vector(const u8 *key, size_t key_len, size_t num_elem,
1620*a90b9d01SCy Schubert const u8 *addr[], const size_t *len, u8 *mac)
1621*a90b9d01SCy Schubert {
1622*a90b9d01SCy Schubert return openssl_hmac_vector("SHA384", key, key_len, num_elem, addr,
1623*a90b9d01SCy Schubert len, mac, 48);
1624*a90b9d01SCy Schubert }
1625*a90b9d01SCy Schubert
1626*a90b9d01SCy Schubert
hmac_sha384(const u8 * key,size_t key_len,const u8 * data,size_t data_len,u8 * mac)1627*a90b9d01SCy Schubert int hmac_sha384(const u8 *key, size_t key_len, const u8 *data,
1628*a90b9d01SCy Schubert size_t data_len, u8 *mac)
1629*a90b9d01SCy Schubert {
1630*a90b9d01SCy Schubert return hmac_sha384_vector(key, key_len, 1, &data, &data_len, mac);
1631*a90b9d01SCy Schubert }
1632*a90b9d01SCy Schubert
1633*a90b9d01SCy Schubert #endif /* CONFIG_SHA384 */
1634*a90b9d01SCy Schubert
1635*a90b9d01SCy Schubert
1636*a90b9d01SCy Schubert #ifdef CONFIG_SHA512
1637*a90b9d01SCy Schubert
hmac_sha512_vector(const u8 * key,size_t key_len,size_t num_elem,const u8 * addr[],const size_t * len,u8 * mac)1638*a90b9d01SCy Schubert int hmac_sha512_vector(const u8 *key, size_t key_len, size_t num_elem,
1639*a90b9d01SCy Schubert const u8 *addr[], const size_t *len, u8 *mac)
1640*a90b9d01SCy Schubert {
1641*a90b9d01SCy Schubert return openssl_hmac_vector("SHA512", key, key_len, num_elem, addr,
1642*a90b9d01SCy Schubert len, mac, 64);
1643*a90b9d01SCy Schubert }
1644*a90b9d01SCy Schubert
1645*a90b9d01SCy Schubert
hmac_sha512(const u8 * key,size_t key_len,const u8 * data,size_t data_len,u8 * mac)1646*a90b9d01SCy Schubert int hmac_sha512(const u8 *key, size_t key_len, const u8 *data,
1647*a90b9d01SCy Schubert size_t data_len, u8 *mac)
1648*a90b9d01SCy Schubert {
1649*a90b9d01SCy Schubert return hmac_sha512_vector(key, key_len, 1, &data, &data_len, mac);
1650*a90b9d01SCy Schubert }
1651*a90b9d01SCy Schubert
1652*a90b9d01SCy Schubert #endif /* CONFIG_SHA512 */
1653*a90b9d01SCy Schubert
1654*a90b9d01SCy Schubert #else /* OpenSSL version >= 3.0 */
1655*a90b9d01SCy Schubert
openssl_hmac_vector(const EVP_MD * type,const u8 * key,size_t key_len,size_t num_elem,const u8 * addr[],const size_t * len,u8 * mac,unsigned int mdlen)16565b9c547cSRui Paulo static int openssl_hmac_vector(const EVP_MD *type, const u8 *key,
16575b9c547cSRui Paulo size_t key_len, size_t num_elem,
16585b9c547cSRui Paulo const u8 *addr[], const size_t *len, u8 *mac,
16595b9c547cSRui Paulo unsigned int mdlen)
1660f05cddf9SRui Paulo {
1661780fb4a2SCy Schubert HMAC_CTX *ctx;
1662f05cddf9SRui Paulo size_t i;
1663f05cddf9SRui Paulo int res;
1664f05cddf9SRui Paulo
1665780fb4a2SCy Schubert if (TEST_FAIL())
1666f05cddf9SRui Paulo return -1;
1667780fb4a2SCy Schubert
1668780fb4a2SCy Schubert ctx = HMAC_CTX_new();
1669780fb4a2SCy Schubert if (!ctx)
1670780fb4a2SCy Schubert return -1;
1671780fb4a2SCy Schubert res = HMAC_Init_ex(ctx, key, key_len, type, NULL);
1672780fb4a2SCy Schubert if (res != 1)
1673780fb4a2SCy Schubert goto done;
1674f05cddf9SRui Paulo
1675f05cddf9SRui Paulo for (i = 0; i < num_elem; i++)
1676780fb4a2SCy Schubert HMAC_Update(ctx, addr[i], len[i]);
1677f05cddf9SRui Paulo
1678780fb4a2SCy Schubert res = HMAC_Final(ctx, mac, &mdlen);
1679780fb4a2SCy Schubert done:
1680780fb4a2SCy Schubert HMAC_CTX_free(ctx);
1681f05cddf9SRui Paulo
1682f05cddf9SRui Paulo return res == 1 ? 0 : -1;
1683f05cddf9SRui Paulo }
1684f05cddf9SRui Paulo
1685f05cddf9SRui Paulo
16865b9c547cSRui Paulo #ifndef CONFIG_FIPS
16875b9c547cSRui Paulo
hmac_md5_vector(const u8 * key,size_t key_len,size_t num_elem,const u8 * addr[],const size_t * len,u8 * mac)16885b9c547cSRui Paulo int hmac_md5_vector(const u8 *key, size_t key_len, size_t num_elem,
16895b9c547cSRui Paulo const u8 *addr[], const size_t *len, u8 *mac)
16905b9c547cSRui Paulo {
16915b9c547cSRui Paulo return openssl_hmac_vector(EVP_md5(), key ,key_len, num_elem, addr, len,
16925b9c547cSRui Paulo mac, 16);
16935b9c547cSRui Paulo }
16945b9c547cSRui Paulo
16955b9c547cSRui Paulo
hmac_md5(const u8 * key,size_t key_len,const u8 * data,size_t data_len,u8 * mac)16965b9c547cSRui Paulo int hmac_md5(const u8 *key, size_t key_len, const u8 *data, size_t data_len,
16975b9c547cSRui Paulo u8 *mac)
16985b9c547cSRui Paulo {
16995b9c547cSRui Paulo return hmac_md5_vector(key, key_len, 1, &data, &data_len, mac);
17005b9c547cSRui Paulo }
17015b9c547cSRui Paulo
17025b9c547cSRui Paulo #endif /* CONFIG_FIPS */
17035b9c547cSRui Paulo
17045b9c547cSRui Paulo
hmac_sha1_vector(const u8 * key,size_t key_len,size_t num_elem,const u8 * addr[],const size_t * len,u8 * mac)17055b9c547cSRui Paulo int hmac_sha1_vector(const u8 *key, size_t key_len, size_t num_elem,
17065b9c547cSRui Paulo const u8 *addr[], const size_t *len, u8 *mac)
17075b9c547cSRui Paulo {
17085b9c547cSRui Paulo return openssl_hmac_vector(EVP_sha1(), key, key_len, num_elem, addr,
17095b9c547cSRui Paulo len, mac, 20);
17105b9c547cSRui Paulo }
17115b9c547cSRui Paulo
17125b9c547cSRui Paulo
hmac_sha1(const u8 * key,size_t key_len,const u8 * data,size_t data_len,u8 * mac)1713f05cddf9SRui Paulo int hmac_sha1(const u8 *key, size_t key_len, const u8 *data, size_t data_len,
1714f05cddf9SRui Paulo u8 *mac)
1715f05cddf9SRui Paulo {
1716f05cddf9SRui Paulo return hmac_sha1_vector(key, key_len, 1, &data, &data_len, mac);
1717f05cddf9SRui Paulo }
1718f05cddf9SRui Paulo
1719f05cddf9SRui Paulo
1720f05cddf9SRui Paulo #ifdef CONFIG_SHA256
1721f05cddf9SRui Paulo
hmac_sha256_vector(const u8 * key,size_t key_len,size_t num_elem,const u8 * addr[],const size_t * len,u8 * mac)1722f05cddf9SRui Paulo int hmac_sha256_vector(const u8 *key, size_t key_len, size_t num_elem,
1723f05cddf9SRui Paulo const u8 *addr[], const size_t *len, u8 *mac)
1724f05cddf9SRui Paulo {
17255b9c547cSRui Paulo return openssl_hmac_vector(EVP_sha256(), key, key_len, num_elem, addr,
17265b9c547cSRui Paulo len, mac, 32);
1727f05cddf9SRui Paulo }
1728f05cddf9SRui Paulo
1729f05cddf9SRui Paulo
hmac_sha256(const u8 * key,size_t key_len,const u8 * data,size_t data_len,u8 * mac)1730f05cddf9SRui Paulo int hmac_sha256(const u8 *key, size_t key_len, const u8 *data,
1731f05cddf9SRui Paulo size_t data_len, u8 *mac)
1732f05cddf9SRui Paulo {
1733f05cddf9SRui Paulo return hmac_sha256_vector(key, key_len, 1, &data, &data_len, mac);
1734f05cddf9SRui Paulo }
1735f05cddf9SRui Paulo
1736f05cddf9SRui Paulo #endif /* CONFIG_SHA256 */
1737f05cddf9SRui Paulo
1738f05cddf9SRui Paulo
17395b9c547cSRui Paulo #ifdef CONFIG_SHA384
17405b9c547cSRui Paulo
hmac_sha384_vector(const u8 * key,size_t key_len,size_t num_elem,const u8 * addr[],const size_t * len,u8 * mac)17415b9c547cSRui Paulo int hmac_sha384_vector(const u8 *key, size_t key_len, size_t num_elem,
17425b9c547cSRui Paulo const u8 *addr[], const size_t *len, u8 *mac)
17435b9c547cSRui Paulo {
17445b9c547cSRui Paulo return openssl_hmac_vector(EVP_sha384(), key, key_len, num_elem, addr,
174585732ac8SCy Schubert len, mac, 48);
17465b9c547cSRui Paulo }
17475b9c547cSRui Paulo
17485b9c547cSRui Paulo
hmac_sha384(const u8 * key,size_t key_len,const u8 * data,size_t data_len,u8 * mac)17495b9c547cSRui Paulo int hmac_sha384(const u8 *key, size_t key_len, const u8 *data,
17505b9c547cSRui Paulo size_t data_len, u8 *mac)
17515b9c547cSRui Paulo {
17525b9c547cSRui Paulo return hmac_sha384_vector(key, key_len, 1, &data, &data_len, mac);
17535b9c547cSRui Paulo }
17545b9c547cSRui Paulo
17555b9c547cSRui Paulo #endif /* CONFIG_SHA384 */
17565b9c547cSRui Paulo
17575b9c547cSRui Paulo
175885732ac8SCy Schubert #ifdef CONFIG_SHA512
175985732ac8SCy Schubert
hmac_sha512_vector(const u8 * key,size_t key_len,size_t num_elem,const u8 * addr[],const size_t * len,u8 * mac)176085732ac8SCy Schubert int hmac_sha512_vector(const u8 *key, size_t key_len, size_t num_elem,
176185732ac8SCy Schubert const u8 *addr[], const size_t *len, u8 *mac)
176285732ac8SCy Schubert {
176385732ac8SCy Schubert return openssl_hmac_vector(EVP_sha512(), key, key_len, num_elem, addr,
176485732ac8SCy Schubert len, mac, 64);
176585732ac8SCy Schubert }
176685732ac8SCy Schubert
176785732ac8SCy Schubert
hmac_sha512(const u8 * key,size_t key_len,const u8 * data,size_t data_len,u8 * mac)176885732ac8SCy Schubert int hmac_sha512(const u8 *key, size_t key_len, const u8 *data,
176985732ac8SCy Schubert size_t data_len, u8 *mac)
177085732ac8SCy Schubert {
177185732ac8SCy Schubert return hmac_sha512_vector(key, key_len, 1, &data, &data_len, mac);
177285732ac8SCy Schubert }
177385732ac8SCy Schubert
177485732ac8SCy Schubert #endif /* CONFIG_SHA512 */
177585732ac8SCy Schubert
1776*a90b9d01SCy Schubert #endif /* OpenSSL version >= 3.0 */
1777*a90b9d01SCy Schubert
1778*a90b9d01SCy Schubert
pbkdf2_sha1(const char * passphrase,const u8 * ssid,size_t ssid_len,int iterations,u8 * buf,size_t buflen)1779*a90b9d01SCy Schubert int pbkdf2_sha1(const char *passphrase, const u8 *ssid, size_t ssid_len,
1780*a90b9d01SCy Schubert int iterations, u8 *buf, size_t buflen)
1781*a90b9d01SCy Schubert {
1782*a90b9d01SCy Schubert if (PKCS5_PBKDF2_HMAC_SHA1(passphrase, os_strlen(passphrase), ssid,
1783*a90b9d01SCy Schubert ssid_len, iterations, buflen, buf) != 1)
1784*a90b9d01SCy Schubert return -1;
1785*a90b9d01SCy Schubert return 0;
1786*a90b9d01SCy Schubert }
1787*a90b9d01SCy Schubert
178885732ac8SCy Schubert
crypto_get_random(void * buf,size_t len)1789f05cddf9SRui Paulo int crypto_get_random(void *buf, size_t len)
1790f05cddf9SRui Paulo {
1791f05cddf9SRui Paulo if (RAND_bytes(buf, len) != 1)
1792f05cddf9SRui Paulo return -1;
1793f05cddf9SRui Paulo return 0;
1794f05cddf9SRui Paulo }
1795f05cddf9SRui Paulo
1796f05cddf9SRui Paulo
omac1_aes_vector(const u8 * key,size_t key_len,size_t num_elem,const u8 * addr[],const size_t * len,u8 * mac)17975b9c547cSRui Paulo int omac1_aes_vector(const u8 *key, size_t key_len, size_t num_elem,
1798f05cddf9SRui Paulo const u8 *addr[], const size_t *len, u8 *mac)
1799f05cddf9SRui Paulo {
1800*a90b9d01SCy Schubert #if OPENSSL_VERSION_NUMBER >= 0x30000000L
1801*a90b9d01SCy Schubert EVP_MAC_CTX *ctx = NULL;
1802*a90b9d01SCy Schubert EVP_MAC *emac;
1803*a90b9d01SCy Schubert int ret = -1;
1804*a90b9d01SCy Schubert size_t outlen, i;
1805*a90b9d01SCy Schubert OSSL_PARAM params[2];
1806*a90b9d01SCy Schubert char *cipher = NULL;
1807*a90b9d01SCy Schubert
1808*a90b9d01SCy Schubert if (TEST_FAIL())
1809*a90b9d01SCy Schubert return -1;
1810*a90b9d01SCy Schubert
1811*a90b9d01SCy Schubert emac = EVP_MAC_fetch(NULL, "CMAC", NULL);
1812*a90b9d01SCy Schubert
1813*a90b9d01SCy Schubert if (key_len == 32)
1814*a90b9d01SCy Schubert cipher = "aes-256-cbc";
1815*a90b9d01SCy Schubert else if (key_len == 24)
1816*a90b9d01SCy Schubert cipher = "aes-192-cbc";
1817*a90b9d01SCy Schubert else if (key_len == 16)
1818*a90b9d01SCy Schubert cipher = "aes-128-cbc";
1819*a90b9d01SCy Schubert
1820*a90b9d01SCy Schubert params[0] = OSSL_PARAM_construct_utf8_string("cipher", cipher, 0);
1821*a90b9d01SCy Schubert params[1] = OSSL_PARAM_construct_end();
1822*a90b9d01SCy Schubert
1823*a90b9d01SCy Schubert if (!emac || !cipher ||
1824*a90b9d01SCy Schubert !(ctx = EVP_MAC_CTX_new(emac)) ||
1825*a90b9d01SCy Schubert EVP_MAC_init(ctx, key, key_len, params) != 1)
1826*a90b9d01SCy Schubert goto fail;
1827*a90b9d01SCy Schubert
1828*a90b9d01SCy Schubert for (i = 0; i < num_elem; i++) {
1829*a90b9d01SCy Schubert if (!EVP_MAC_update(ctx, addr[i], len[i]))
1830*a90b9d01SCy Schubert goto fail;
1831*a90b9d01SCy Schubert }
1832*a90b9d01SCy Schubert if (EVP_MAC_final(ctx, mac, &outlen, 16) != 1 || outlen != 16)
1833*a90b9d01SCy Schubert goto fail;
1834*a90b9d01SCy Schubert
1835*a90b9d01SCy Schubert ret = 0;
1836*a90b9d01SCy Schubert fail:
1837*a90b9d01SCy Schubert EVP_MAC_CTX_free(ctx);
1838*a90b9d01SCy Schubert EVP_MAC_free(emac);
1839*a90b9d01SCy Schubert return ret;
1840*a90b9d01SCy Schubert #else /* OpenSSL version >= 3.0 */
1841f05cddf9SRui Paulo CMAC_CTX *ctx;
1842f05cddf9SRui Paulo int ret = -1;
1843f05cddf9SRui Paulo size_t outlen, i;
1844f05cddf9SRui Paulo
1845780fb4a2SCy Schubert if (TEST_FAIL())
1846780fb4a2SCy Schubert return -1;
1847780fb4a2SCy Schubert
1848f05cddf9SRui Paulo ctx = CMAC_CTX_new();
1849f05cddf9SRui Paulo if (ctx == NULL)
1850f05cddf9SRui Paulo return -1;
1851f05cddf9SRui Paulo
18525b9c547cSRui Paulo if (key_len == 32) {
18535b9c547cSRui Paulo if (!CMAC_Init(ctx, key, 32, EVP_aes_256_cbc(), NULL))
18545b9c547cSRui Paulo goto fail;
1855*a90b9d01SCy Schubert } else if (key_len == 24) {
1856*a90b9d01SCy Schubert if (!CMAC_Init(ctx, key, 24, EVP_aes_192_cbc(), NULL))
1857*a90b9d01SCy Schubert goto fail;
18585b9c547cSRui Paulo } else if (key_len == 16) {
1859f05cddf9SRui Paulo if (!CMAC_Init(ctx, key, 16, EVP_aes_128_cbc(), NULL))
1860f05cddf9SRui Paulo goto fail;
18615b9c547cSRui Paulo } else {
18625b9c547cSRui Paulo goto fail;
18635b9c547cSRui Paulo }
1864f05cddf9SRui Paulo for (i = 0; i < num_elem; i++) {
1865f05cddf9SRui Paulo if (!CMAC_Update(ctx, addr[i], len[i]))
1866f05cddf9SRui Paulo goto fail;
1867f05cddf9SRui Paulo }
1868f05cddf9SRui Paulo if (!CMAC_Final(ctx, mac, &outlen) || outlen != 16)
1869f05cddf9SRui Paulo goto fail;
1870f05cddf9SRui Paulo
1871f05cddf9SRui Paulo ret = 0;
1872f05cddf9SRui Paulo fail:
1873f05cddf9SRui Paulo CMAC_CTX_free(ctx);
1874f05cddf9SRui Paulo return ret;
1875*a90b9d01SCy Schubert #endif /* OpenSSL version >= 3.0 */
1876f05cddf9SRui Paulo }
1877f05cddf9SRui Paulo
1878f05cddf9SRui Paulo
omac1_aes_128_vector(const u8 * key,size_t num_elem,const u8 * addr[],const size_t * len,u8 * mac)18795b9c547cSRui Paulo int omac1_aes_128_vector(const u8 *key, size_t num_elem,
18805b9c547cSRui Paulo const u8 *addr[], const size_t *len, u8 *mac)
18815b9c547cSRui Paulo {
18825b9c547cSRui Paulo return omac1_aes_vector(key, 16, num_elem, addr, len, mac);
18835b9c547cSRui Paulo }
18845b9c547cSRui Paulo
18855b9c547cSRui Paulo
omac1_aes_128(const u8 * key,const u8 * data,size_t data_len,u8 * mac)1886f05cddf9SRui Paulo int omac1_aes_128(const u8 *key, const u8 *data, size_t data_len, u8 *mac)
1887f05cddf9SRui Paulo {
1888f05cddf9SRui Paulo return omac1_aes_128_vector(key, 1, &data, &data_len, mac);
1889f05cddf9SRui Paulo }
18905b9c547cSRui Paulo
18915b9c547cSRui Paulo
omac1_aes_256(const u8 * key,const u8 * data,size_t data_len,u8 * mac)18925b9c547cSRui Paulo int omac1_aes_256(const u8 *key, const u8 *data, size_t data_len, u8 *mac)
18935b9c547cSRui Paulo {
18945b9c547cSRui Paulo return omac1_aes_vector(key, 32, 1, &data, &data_len, mac);
18955b9c547cSRui Paulo }
18965b9c547cSRui Paulo
18975b9c547cSRui Paulo
crypto_bignum_init(void)18985b9c547cSRui Paulo struct crypto_bignum * crypto_bignum_init(void)
18995b9c547cSRui Paulo {
1900780fb4a2SCy Schubert if (TEST_FAIL())
1901780fb4a2SCy Schubert return NULL;
19025b9c547cSRui Paulo return (struct crypto_bignum *) BN_new();
19035b9c547cSRui Paulo }
19045b9c547cSRui Paulo
19055b9c547cSRui Paulo
crypto_bignum_init_set(const u8 * buf,size_t len)19065b9c547cSRui Paulo struct crypto_bignum * crypto_bignum_init_set(const u8 *buf, size_t len)
19075b9c547cSRui Paulo {
1908780fb4a2SCy Schubert BIGNUM *bn;
1909780fb4a2SCy Schubert
1910780fb4a2SCy Schubert if (TEST_FAIL())
1911780fb4a2SCy Schubert return NULL;
1912780fb4a2SCy Schubert
1913780fb4a2SCy Schubert bn = BN_bin2bn(buf, len, NULL);
19145b9c547cSRui Paulo return (struct crypto_bignum *) bn;
19155b9c547cSRui Paulo }
19165b9c547cSRui Paulo
19175b9c547cSRui Paulo
crypto_bignum_init_uint(unsigned int val)1918c1d255d3SCy Schubert struct crypto_bignum * crypto_bignum_init_uint(unsigned int val)
1919c1d255d3SCy Schubert {
1920c1d255d3SCy Schubert BIGNUM *bn;
1921c1d255d3SCy Schubert
1922c1d255d3SCy Schubert if (TEST_FAIL())
1923c1d255d3SCy Schubert return NULL;
1924c1d255d3SCy Schubert
1925c1d255d3SCy Schubert bn = BN_new();
1926c1d255d3SCy Schubert if (!bn)
1927c1d255d3SCy Schubert return NULL;
1928c1d255d3SCy Schubert if (BN_set_word(bn, val) != 1) {
1929c1d255d3SCy Schubert BN_free(bn);
1930c1d255d3SCy Schubert return NULL;
1931c1d255d3SCy Schubert }
1932c1d255d3SCy Schubert return (struct crypto_bignum *) bn;
1933c1d255d3SCy Schubert }
1934c1d255d3SCy Schubert
1935c1d255d3SCy Schubert
crypto_bignum_deinit(struct crypto_bignum * n,int clear)19365b9c547cSRui Paulo void crypto_bignum_deinit(struct crypto_bignum *n, int clear)
19375b9c547cSRui Paulo {
19385b9c547cSRui Paulo if (clear)
19395b9c547cSRui Paulo BN_clear_free((BIGNUM *) n);
19405b9c547cSRui Paulo else
19415b9c547cSRui Paulo BN_free((BIGNUM *) n);
19425b9c547cSRui Paulo }
19435b9c547cSRui Paulo
19445b9c547cSRui Paulo
crypto_bignum_to_bin(const struct crypto_bignum * a,u8 * buf,size_t buflen,size_t padlen)19455b9c547cSRui Paulo int crypto_bignum_to_bin(const struct crypto_bignum *a,
19465b9c547cSRui Paulo u8 *buf, size_t buflen, size_t padlen)
19475b9c547cSRui Paulo {
19485b9c547cSRui Paulo int num_bytes, offset;
19495b9c547cSRui Paulo
1950780fb4a2SCy Schubert if (TEST_FAIL())
1951780fb4a2SCy Schubert return -1;
1952780fb4a2SCy Schubert
19535b9c547cSRui Paulo if (padlen > buflen)
19545b9c547cSRui Paulo return -1;
19555b9c547cSRui Paulo
1956206b73d0SCy Schubert if (padlen) {
1957206b73d0SCy Schubert #ifdef OPENSSL_IS_BORINGSSL
1958206b73d0SCy Schubert if (BN_bn2bin_padded(buf, padlen, (const BIGNUM *) a) == 0)
1959206b73d0SCy Schubert return -1;
1960206b73d0SCy Schubert return padlen;
1961206b73d0SCy Schubert #else /* OPENSSL_IS_BORINGSSL */
1962206b73d0SCy Schubert #if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)
1963206b73d0SCy Schubert return BN_bn2binpad((const BIGNUM *) a, buf, padlen);
1964206b73d0SCy Schubert #endif
1965206b73d0SCy Schubert #endif
1966206b73d0SCy Schubert }
1967206b73d0SCy Schubert
19685b9c547cSRui Paulo num_bytes = BN_num_bytes((const BIGNUM *) a);
19695b9c547cSRui Paulo if ((size_t) num_bytes > buflen)
19705b9c547cSRui Paulo return -1;
19715b9c547cSRui Paulo if (padlen > (size_t) num_bytes)
19725b9c547cSRui Paulo offset = padlen - num_bytes;
19735b9c547cSRui Paulo else
19745b9c547cSRui Paulo offset = 0;
19755b9c547cSRui Paulo
19765b9c547cSRui Paulo os_memset(buf, 0, offset);
19775b9c547cSRui Paulo BN_bn2bin((const BIGNUM *) a, buf + offset);
19785b9c547cSRui Paulo
19795b9c547cSRui Paulo return num_bytes + offset;
19805b9c547cSRui Paulo }
19815b9c547cSRui Paulo
19825b9c547cSRui Paulo
crypto_bignum_rand(struct crypto_bignum * r,const struct crypto_bignum * m)198385732ac8SCy Schubert int crypto_bignum_rand(struct crypto_bignum *r, const struct crypto_bignum *m)
198485732ac8SCy Schubert {
19854bc52338SCy Schubert if (TEST_FAIL())
19864bc52338SCy Schubert return -1;
198785732ac8SCy Schubert return BN_rand_range((BIGNUM *) r, (const BIGNUM *) m) == 1 ? 0 : -1;
198885732ac8SCy Schubert }
198985732ac8SCy Schubert
199085732ac8SCy Schubert
crypto_bignum_add(const struct crypto_bignum * a,const struct crypto_bignum * b,struct crypto_bignum * c)19915b9c547cSRui Paulo int crypto_bignum_add(const struct crypto_bignum *a,
19925b9c547cSRui Paulo const struct crypto_bignum *b,
19935b9c547cSRui Paulo struct crypto_bignum *c)
19945b9c547cSRui Paulo {
19955b9c547cSRui Paulo return BN_add((BIGNUM *) c, (const BIGNUM *) a, (const BIGNUM *) b) ?
19965b9c547cSRui Paulo 0 : -1;
19975b9c547cSRui Paulo }
19985b9c547cSRui Paulo
19995b9c547cSRui Paulo
crypto_bignum_mod(const struct crypto_bignum * a,const struct crypto_bignum * b,struct crypto_bignum * c)20005b9c547cSRui Paulo int crypto_bignum_mod(const struct crypto_bignum *a,
20015b9c547cSRui Paulo const struct crypto_bignum *b,
20025b9c547cSRui Paulo struct crypto_bignum *c)
20035b9c547cSRui Paulo {
20045b9c547cSRui Paulo int res;
20055b9c547cSRui Paulo BN_CTX *bnctx;
20065b9c547cSRui Paulo
20075b9c547cSRui Paulo bnctx = BN_CTX_new();
20085b9c547cSRui Paulo if (bnctx == NULL)
20095b9c547cSRui Paulo return -1;
20105b9c547cSRui Paulo res = BN_mod((BIGNUM *) c, (const BIGNUM *) a, (const BIGNUM *) b,
20115b9c547cSRui Paulo bnctx);
20125b9c547cSRui Paulo BN_CTX_free(bnctx);
20135b9c547cSRui Paulo
20145b9c547cSRui Paulo return res ? 0 : -1;
20155b9c547cSRui Paulo }
20165b9c547cSRui Paulo
20175b9c547cSRui Paulo
crypto_bignum_exptmod(const struct crypto_bignum * a,const struct crypto_bignum * b,const struct crypto_bignum * c,struct crypto_bignum * d)20185b9c547cSRui Paulo int crypto_bignum_exptmod(const struct crypto_bignum *a,
20195b9c547cSRui Paulo const struct crypto_bignum *b,
20205b9c547cSRui Paulo const struct crypto_bignum *c,
20215b9c547cSRui Paulo struct crypto_bignum *d)
20225b9c547cSRui Paulo {
20235b9c547cSRui Paulo int res;
20245b9c547cSRui Paulo BN_CTX *bnctx;
20255b9c547cSRui Paulo
2026780fb4a2SCy Schubert if (TEST_FAIL())
2027780fb4a2SCy Schubert return -1;
2028780fb4a2SCy Schubert
20295b9c547cSRui Paulo bnctx = BN_CTX_new();
20305b9c547cSRui Paulo if (bnctx == NULL)
20315b9c547cSRui Paulo return -1;
20324bc52338SCy Schubert res = BN_mod_exp_mont_consttime((BIGNUM *) d, (const BIGNUM *) a,
20334bc52338SCy Schubert (const BIGNUM *) b, (const BIGNUM *) c,
20344bc52338SCy Schubert bnctx, NULL);
20355b9c547cSRui Paulo BN_CTX_free(bnctx);
20365b9c547cSRui Paulo
20375b9c547cSRui Paulo return res ? 0 : -1;
20385b9c547cSRui Paulo }
20395b9c547cSRui Paulo
20405b9c547cSRui Paulo
crypto_bignum_inverse(const struct crypto_bignum * a,const struct crypto_bignum * b,struct crypto_bignum * c)20415b9c547cSRui Paulo int crypto_bignum_inverse(const struct crypto_bignum *a,
20425b9c547cSRui Paulo const struct crypto_bignum *b,
20435b9c547cSRui Paulo struct crypto_bignum *c)
20445b9c547cSRui Paulo {
20455b9c547cSRui Paulo BIGNUM *res;
20465b9c547cSRui Paulo BN_CTX *bnctx;
20475b9c547cSRui Paulo
2048780fb4a2SCy Schubert if (TEST_FAIL())
2049780fb4a2SCy Schubert return -1;
20505b9c547cSRui Paulo bnctx = BN_CTX_new();
20515b9c547cSRui Paulo if (bnctx == NULL)
20525b9c547cSRui Paulo return -1;
20534bc52338SCy Schubert #ifdef OPENSSL_IS_BORINGSSL
20544bc52338SCy Schubert /* TODO: use BN_mod_inverse_blinded() ? */
20554bc52338SCy Schubert #else /* OPENSSL_IS_BORINGSSL */
20564bc52338SCy Schubert BN_set_flags((BIGNUM *) a, BN_FLG_CONSTTIME);
20574bc52338SCy Schubert #endif /* OPENSSL_IS_BORINGSSL */
20585b9c547cSRui Paulo res = BN_mod_inverse((BIGNUM *) c, (const BIGNUM *) a,
20595b9c547cSRui Paulo (const BIGNUM *) b, bnctx);
20605b9c547cSRui Paulo BN_CTX_free(bnctx);
20615b9c547cSRui Paulo
20625b9c547cSRui Paulo return res ? 0 : -1;
20635b9c547cSRui Paulo }
20645b9c547cSRui Paulo
20655b9c547cSRui Paulo
crypto_bignum_sub(const struct crypto_bignum * a,const struct crypto_bignum * b,struct crypto_bignum * c)20665b9c547cSRui Paulo int crypto_bignum_sub(const struct crypto_bignum *a,
20675b9c547cSRui Paulo const struct crypto_bignum *b,
20685b9c547cSRui Paulo struct crypto_bignum *c)
20695b9c547cSRui Paulo {
2070780fb4a2SCy Schubert if (TEST_FAIL())
2071780fb4a2SCy Schubert return -1;
20725b9c547cSRui Paulo return BN_sub((BIGNUM *) c, (const BIGNUM *) a, (const BIGNUM *) b) ?
20735b9c547cSRui Paulo 0 : -1;
20745b9c547cSRui Paulo }
20755b9c547cSRui Paulo
20765b9c547cSRui Paulo
crypto_bignum_div(const struct crypto_bignum * a,const struct crypto_bignum * b,struct crypto_bignum * c)20775b9c547cSRui Paulo int crypto_bignum_div(const struct crypto_bignum *a,
20785b9c547cSRui Paulo const struct crypto_bignum *b,
20795b9c547cSRui Paulo struct crypto_bignum *c)
20805b9c547cSRui Paulo {
20815b9c547cSRui Paulo int res;
20825b9c547cSRui Paulo
20835b9c547cSRui Paulo BN_CTX *bnctx;
20845b9c547cSRui Paulo
2085780fb4a2SCy Schubert if (TEST_FAIL())
2086780fb4a2SCy Schubert return -1;
2087780fb4a2SCy Schubert
20885b9c547cSRui Paulo bnctx = BN_CTX_new();
20895b9c547cSRui Paulo if (bnctx == NULL)
20905b9c547cSRui Paulo return -1;
20914bc52338SCy Schubert #ifndef OPENSSL_IS_BORINGSSL
20924bc52338SCy Schubert BN_set_flags((BIGNUM *) a, BN_FLG_CONSTTIME);
20934bc52338SCy Schubert #endif /* OPENSSL_IS_BORINGSSL */
20945b9c547cSRui Paulo res = BN_div((BIGNUM *) c, NULL, (const BIGNUM *) a,
20955b9c547cSRui Paulo (const BIGNUM *) b, bnctx);
20965b9c547cSRui Paulo BN_CTX_free(bnctx);
20975b9c547cSRui Paulo
20985b9c547cSRui Paulo return res ? 0 : -1;
20995b9c547cSRui Paulo }
21005b9c547cSRui Paulo
21015b9c547cSRui Paulo
crypto_bignum_addmod(const struct crypto_bignum * a,const struct crypto_bignum * b,const struct crypto_bignum * c,struct crypto_bignum * d)2102c1d255d3SCy Schubert int crypto_bignum_addmod(const struct crypto_bignum *a,
2103c1d255d3SCy Schubert const struct crypto_bignum *b,
2104c1d255d3SCy Schubert const struct crypto_bignum *c,
2105c1d255d3SCy Schubert struct crypto_bignum *d)
2106c1d255d3SCy Schubert {
2107c1d255d3SCy Schubert int res;
2108c1d255d3SCy Schubert BN_CTX *bnctx;
2109c1d255d3SCy Schubert
2110c1d255d3SCy Schubert if (TEST_FAIL())
2111c1d255d3SCy Schubert return -1;
2112c1d255d3SCy Schubert
2113c1d255d3SCy Schubert bnctx = BN_CTX_new();
2114c1d255d3SCy Schubert if (!bnctx)
2115c1d255d3SCy Schubert return -1;
2116c1d255d3SCy Schubert res = BN_mod_add((BIGNUM *) d, (const BIGNUM *) a, (const BIGNUM *) b,
2117c1d255d3SCy Schubert (const BIGNUM *) c, bnctx);
2118c1d255d3SCy Schubert BN_CTX_free(bnctx);
2119c1d255d3SCy Schubert
2120c1d255d3SCy Schubert return res ? 0 : -1;
2121c1d255d3SCy Schubert }
2122c1d255d3SCy Schubert
2123c1d255d3SCy Schubert
crypto_bignum_mulmod(const struct crypto_bignum * a,const struct crypto_bignum * b,const struct crypto_bignum * c,struct crypto_bignum * d)21245b9c547cSRui Paulo int crypto_bignum_mulmod(const struct crypto_bignum *a,
21255b9c547cSRui Paulo const struct crypto_bignum *b,
21265b9c547cSRui Paulo const struct crypto_bignum *c,
21275b9c547cSRui Paulo struct crypto_bignum *d)
21285b9c547cSRui Paulo {
21295b9c547cSRui Paulo int res;
21305b9c547cSRui Paulo
21315b9c547cSRui Paulo BN_CTX *bnctx;
21325b9c547cSRui Paulo
2133780fb4a2SCy Schubert if (TEST_FAIL())
2134780fb4a2SCy Schubert return -1;
2135780fb4a2SCy Schubert
21365b9c547cSRui Paulo bnctx = BN_CTX_new();
21375b9c547cSRui Paulo if (bnctx == NULL)
21385b9c547cSRui Paulo return -1;
21395b9c547cSRui Paulo res = BN_mod_mul((BIGNUM *) d, (const BIGNUM *) a, (const BIGNUM *) b,
21405b9c547cSRui Paulo (const BIGNUM *) c, bnctx);
21415b9c547cSRui Paulo BN_CTX_free(bnctx);
21425b9c547cSRui Paulo
21435b9c547cSRui Paulo return res ? 0 : -1;
21445b9c547cSRui Paulo }
21455b9c547cSRui Paulo
21465b9c547cSRui Paulo
crypto_bignum_sqrmod(const struct crypto_bignum * a,const struct crypto_bignum * b,struct crypto_bignum * c)2147c1d255d3SCy Schubert int crypto_bignum_sqrmod(const struct crypto_bignum *a,
2148c1d255d3SCy Schubert const struct crypto_bignum *b,
2149c1d255d3SCy Schubert struct crypto_bignum *c)
2150c1d255d3SCy Schubert {
2151c1d255d3SCy Schubert int res;
2152c1d255d3SCy Schubert BN_CTX *bnctx;
2153c1d255d3SCy Schubert
2154c1d255d3SCy Schubert if (TEST_FAIL())
2155c1d255d3SCy Schubert return -1;
2156c1d255d3SCy Schubert
2157c1d255d3SCy Schubert bnctx = BN_CTX_new();
2158c1d255d3SCy Schubert if (!bnctx)
2159c1d255d3SCy Schubert return -1;
2160c1d255d3SCy Schubert res = BN_mod_sqr((BIGNUM *) c, (const BIGNUM *) a, (const BIGNUM *) b,
2161c1d255d3SCy Schubert bnctx);
2162c1d255d3SCy Schubert BN_CTX_free(bnctx);
2163c1d255d3SCy Schubert
2164c1d255d3SCy Schubert return res ? 0 : -1;
2165c1d255d3SCy Schubert }
2166c1d255d3SCy Schubert
2167c1d255d3SCy Schubert
crypto_bignum_rshift(const struct crypto_bignum * a,int n,struct crypto_bignum * r)216885732ac8SCy Schubert int crypto_bignum_rshift(const struct crypto_bignum *a, int n,
216985732ac8SCy Schubert struct crypto_bignum *r)
217085732ac8SCy Schubert {
2171*a90b9d01SCy Schubert return BN_rshift((BIGNUM *) r, (const BIGNUM *) a, n) == 1 ? 0 : -1;
217285732ac8SCy Schubert }
217385732ac8SCy Schubert
217485732ac8SCy Schubert
crypto_bignum_cmp(const struct crypto_bignum * a,const struct crypto_bignum * b)21755b9c547cSRui Paulo int crypto_bignum_cmp(const struct crypto_bignum *a,
21765b9c547cSRui Paulo const struct crypto_bignum *b)
21775b9c547cSRui Paulo {
21785b9c547cSRui Paulo return BN_cmp((const BIGNUM *) a, (const BIGNUM *) b);
21795b9c547cSRui Paulo }
21805b9c547cSRui Paulo
21815b9c547cSRui Paulo
crypto_bignum_is_zero(const struct crypto_bignum * a)21825b9c547cSRui Paulo int crypto_bignum_is_zero(const struct crypto_bignum *a)
21835b9c547cSRui Paulo {
21845b9c547cSRui Paulo return BN_is_zero((const BIGNUM *) a);
21855b9c547cSRui Paulo }
21865b9c547cSRui Paulo
21875b9c547cSRui Paulo
crypto_bignum_is_one(const struct crypto_bignum * a)21885b9c547cSRui Paulo int crypto_bignum_is_one(const struct crypto_bignum *a)
21895b9c547cSRui Paulo {
21905b9c547cSRui Paulo return BN_is_one((const BIGNUM *) a);
21915b9c547cSRui Paulo }
21925b9c547cSRui Paulo
21935b9c547cSRui Paulo
crypto_bignum_is_odd(const struct crypto_bignum * a)219485732ac8SCy Schubert int crypto_bignum_is_odd(const struct crypto_bignum *a)
219585732ac8SCy Schubert {
219685732ac8SCy Schubert return BN_is_odd((const BIGNUM *) a);
219785732ac8SCy Schubert }
219885732ac8SCy Schubert
219985732ac8SCy Schubert
crypto_bignum_legendre(const struct crypto_bignum * a,const struct crypto_bignum * p)2200325151a3SRui Paulo int crypto_bignum_legendre(const struct crypto_bignum *a,
2201325151a3SRui Paulo const struct crypto_bignum *p)
2202325151a3SRui Paulo {
2203325151a3SRui Paulo BN_CTX *bnctx;
2204325151a3SRui Paulo BIGNUM *exp = NULL, *tmp = NULL;
2205325151a3SRui Paulo int res = -2;
22064bc52338SCy Schubert unsigned int mask;
2207325151a3SRui Paulo
2208780fb4a2SCy Schubert if (TEST_FAIL())
2209780fb4a2SCy Schubert return -2;
2210780fb4a2SCy Schubert
2211325151a3SRui Paulo bnctx = BN_CTX_new();
2212325151a3SRui Paulo if (bnctx == NULL)
2213325151a3SRui Paulo return -2;
2214325151a3SRui Paulo
2215325151a3SRui Paulo exp = BN_new();
2216325151a3SRui Paulo tmp = BN_new();
2217325151a3SRui Paulo if (!exp || !tmp ||
2218325151a3SRui Paulo /* exp = (p-1) / 2 */
2219325151a3SRui Paulo !BN_sub(exp, (const BIGNUM *) p, BN_value_one()) ||
2220325151a3SRui Paulo !BN_rshift1(exp, exp) ||
22214bc52338SCy Schubert !BN_mod_exp_mont_consttime(tmp, (const BIGNUM *) a, exp,
22224bc52338SCy Schubert (const BIGNUM *) p, bnctx, NULL))
2223325151a3SRui Paulo goto fail;
2224325151a3SRui Paulo
22254bc52338SCy Schubert /* Return 1 if tmp == 1, 0 if tmp == 0, or -1 otherwise. Need to use
22264bc52338SCy Schubert * constant time selection to avoid branches here. */
2227325151a3SRui Paulo res = -1;
22284bc52338SCy Schubert mask = const_time_eq(BN_is_word(tmp, 1), 1);
22294bc52338SCy Schubert res = const_time_select_int(mask, 1, res);
22304bc52338SCy Schubert mask = const_time_eq(BN_is_zero(tmp), 1);
22314bc52338SCy Schubert res = const_time_select_int(mask, 0, res);
2232325151a3SRui Paulo
2233325151a3SRui Paulo fail:
2234325151a3SRui Paulo BN_clear_free(tmp);
2235325151a3SRui Paulo BN_clear_free(exp);
2236325151a3SRui Paulo BN_CTX_free(bnctx);
2237325151a3SRui Paulo return res;
2238325151a3SRui Paulo }
2239325151a3SRui Paulo
2240325151a3SRui Paulo
22415b9c547cSRui Paulo #ifdef CONFIG_ECC
22425b9c547cSRui Paulo
22435b9c547cSRui Paulo struct crypto_ec {
22445b9c547cSRui Paulo EC_GROUP *group;
224585732ac8SCy Schubert int nid;
2246*a90b9d01SCy Schubert int iana_group;
22475b9c547cSRui Paulo BN_CTX *bnctx;
22485b9c547cSRui Paulo BIGNUM *prime;
22495b9c547cSRui Paulo BIGNUM *order;
2250325151a3SRui Paulo BIGNUM *a;
2251325151a3SRui Paulo BIGNUM *b;
22525b9c547cSRui Paulo };
22535b9c547cSRui Paulo
22544b72b91aSCy Schubert
crypto_ec_group_2_nid(int group)22554b72b91aSCy Schubert static int crypto_ec_group_2_nid(int group)
22564b72b91aSCy Schubert {
22574b72b91aSCy Schubert /* Map from IANA registry for IKE D-H groups to OpenSSL NID */
22584b72b91aSCy Schubert switch (group) {
22594b72b91aSCy Schubert case 19:
22604b72b91aSCy Schubert return NID_X9_62_prime256v1;
22614b72b91aSCy Schubert case 20:
22624b72b91aSCy Schubert return NID_secp384r1;
22634b72b91aSCy Schubert case 21:
22644b72b91aSCy Schubert return NID_secp521r1;
22654b72b91aSCy Schubert case 25:
22664b72b91aSCy Schubert return NID_X9_62_prime192v1;
22674b72b91aSCy Schubert case 26:
22684b72b91aSCy Schubert return NID_secp224r1;
22694b72b91aSCy Schubert #ifdef NID_brainpoolP224r1
22704b72b91aSCy Schubert case 27:
22714b72b91aSCy Schubert return NID_brainpoolP224r1;
22724b72b91aSCy Schubert #endif /* NID_brainpoolP224r1 */
22734b72b91aSCy Schubert #ifdef NID_brainpoolP256r1
22744b72b91aSCy Schubert case 28:
22754b72b91aSCy Schubert return NID_brainpoolP256r1;
22764b72b91aSCy Schubert #endif /* NID_brainpoolP256r1 */
22774b72b91aSCy Schubert #ifdef NID_brainpoolP384r1
22784b72b91aSCy Schubert case 29:
22794b72b91aSCy Schubert return NID_brainpoolP384r1;
22804b72b91aSCy Schubert #endif /* NID_brainpoolP384r1 */
22814b72b91aSCy Schubert #ifdef NID_brainpoolP512r1
22824b72b91aSCy Schubert case 30:
22834b72b91aSCy Schubert return NID_brainpoolP512r1;
22844b72b91aSCy Schubert #endif /* NID_brainpoolP512r1 */
22854b72b91aSCy Schubert default:
22864b72b91aSCy Schubert return -1;
22874b72b91aSCy Schubert }
22884b72b91aSCy Schubert }
22894b72b91aSCy Schubert
22904b72b91aSCy Schubert
2291*a90b9d01SCy Schubert #if OPENSSL_VERSION_NUMBER >= 0x30000000L
crypto_ec_group_2_name(int group)2292*a90b9d01SCy Schubert static const char * crypto_ec_group_2_name(int group)
2293*a90b9d01SCy Schubert {
2294*a90b9d01SCy Schubert /* Map from IANA registry for IKE D-H groups to OpenSSL group name */
2295*a90b9d01SCy Schubert switch (group) {
2296*a90b9d01SCy Schubert case 19:
2297*a90b9d01SCy Schubert return "prime256v1";
2298*a90b9d01SCy Schubert case 20:
2299*a90b9d01SCy Schubert return "secp384r1";
2300*a90b9d01SCy Schubert case 21:
2301*a90b9d01SCy Schubert return "secp521r1";
2302*a90b9d01SCy Schubert case 25:
2303*a90b9d01SCy Schubert return "prime192v1";
2304*a90b9d01SCy Schubert case 26:
2305*a90b9d01SCy Schubert return "secp224r1";
2306*a90b9d01SCy Schubert #ifdef NID_brainpoolP224r1
2307*a90b9d01SCy Schubert case 27:
2308*a90b9d01SCy Schubert return "brainpoolP224r1";
2309*a90b9d01SCy Schubert #endif /* NID_brainpoolP224r1 */
2310*a90b9d01SCy Schubert #ifdef NID_brainpoolP256r1
2311*a90b9d01SCy Schubert case 28:
2312*a90b9d01SCy Schubert return "brainpoolP256r1";
2313*a90b9d01SCy Schubert #endif /* NID_brainpoolP256r1 */
2314*a90b9d01SCy Schubert #ifdef NID_brainpoolP384r1
2315*a90b9d01SCy Schubert case 29:
2316*a90b9d01SCy Schubert return "brainpoolP384r1";
2317*a90b9d01SCy Schubert #endif /* NID_brainpoolP384r1 */
2318*a90b9d01SCy Schubert #ifdef NID_brainpoolP512r1
2319*a90b9d01SCy Schubert case 30:
2320*a90b9d01SCy Schubert return "brainpoolP512r1";
2321*a90b9d01SCy Schubert #endif /* NID_brainpoolP512r1 */
2322*a90b9d01SCy Schubert default:
2323*a90b9d01SCy Schubert return NULL;
2324*a90b9d01SCy Schubert }
2325*a90b9d01SCy Schubert }
2326*a90b9d01SCy Schubert #endif /* OpenSSL version >= 3.0 */
2327*a90b9d01SCy Schubert
2328*a90b9d01SCy Schubert
crypto_ec_init(int group)23295b9c547cSRui Paulo struct crypto_ec * crypto_ec_init(int group)
23305b9c547cSRui Paulo {
23315b9c547cSRui Paulo struct crypto_ec *e;
23325b9c547cSRui Paulo int nid;
23335b9c547cSRui Paulo
23344b72b91aSCy Schubert nid = crypto_ec_group_2_nid(group);
23354b72b91aSCy Schubert if (nid < 0)
23365b9c547cSRui Paulo return NULL;
23375b9c547cSRui Paulo
23385b9c547cSRui Paulo e = os_zalloc(sizeof(*e));
23395b9c547cSRui Paulo if (e == NULL)
23405b9c547cSRui Paulo return NULL;
23415b9c547cSRui Paulo
234285732ac8SCy Schubert e->nid = nid;
2343*a90b9d01SCy Schubert e->iana_group = group;
23445b9c547cSRui Paulo e->bnctx = BN_CTX_new();
23455b9c547cSRui Paulo e->group = EC_GROUP_new_by_curve_name(nid);
23465b9c547cSRui Paulo e->prime = BN_new();
23475b9c547cSRui Paulo e->order = BN_new();
2348325151a3SRui Paulo e->a = BN_new();
2349325151a3SRui Paulo e->b = BN_new();
23505b9c547cSRui Paulo if (e->group == NULL || e->bnctx == NULL || e->prime == NULL ||
2351325151a3SRui Paulo e->order == NULL || e->a == NULL || e->b == NULL ||
2352*a90b9d01SCy Schubert !EC_GROUP_get_curve(e->group, e->prime, e->a, e->b, e->bnctx) ||
23535b9c547cSRui Paulo !EC_GROUP_get_order(e->group, e->order, e->bnctx)) {
23545b9c547cSRui Paulo crypto_ec_deinit(e);
23555b9c547cSRui Paulo e = NULL;
23565b9c547cSRui Paulo }
23575b9c547cSRui Paulo
23585b9c547cSRui Paulo return e;
23595b9c547cSRui Paulo }
23605b9c547cSRui Paulo
23615b9c547cSRui Paulo
crypto_ec_deinit(struct crypto_ec * e)23625b9c547cSRui Paulo void crypto_ec_deinit(struct crypto_ec *e)
23635b9c547cSRui Paulo {
23645b9c547cSRui Paulo if (e == NULL)
23655b9c547cSRui Paulo return;
2366325151a3SRui Paulo BN_clear_free(e->b);
2367325151a3SRui Paulo BN_clear_free(e->a);
23685b9c547cSRui Paulo BN_clear_free(e->order);
23695b9c547cSRui Paulo BN_clear_free(e->prime);
23705b9c547cSRui Paulo EC_GROUP_free(e->group);
23715b9c547cSRui Paulo BN_CTX_free(e->bnctx);
23725b9c547cSRui Paulo os_free(e);
23735b9c547cSRui Paulo }
23745b9c547cSRui Paulo
23755b9c547cSRui Paulo
crypto_ec_point_init(struct crypto_ec * e)23765b9c547cSRui Paulo struct crypto_ec_point * crypto_ec_point_init(struct crypto_ec *e)
23775b9c547cSRui Paulo {
2378780fb4a2SCy Schubert if (TEST_FAIL())
2379780fb4a2SCy Schubert return NULL;
23805b9c547cSRui Paulo if (e == NULL)
23815b9c547cSRui Paulo return NULL;
23825b9c547cSRui Paulo return (struct crypto_ec_point *) EC_POINT_new(e->group);
23835b9c547cSRui Paulo }
23845b9c547cSRui Paulo
23855b9c547cSRui Paulo
crypto_ec_prime_len(struct crypto_ec * e)23865b9c547cSRui Paulo size_t crypto_ec_prime_len(struct crypto_ec *e)
23875b9c547cSRui Paulo {
23885b9c547cSRui Paulo return BN_num_bytes(e->prime);
23895b9c547cSRui Paulo }
23905b9c547cSRui Paulo
23915b9c547cSRui Paulo
crypto_ec_prime_len_bits(struct crypto_ec * e)23925b9c547cSRui Paulo size_t crypto_ec_prime_len_bits(struct crypto_ec *e)
23935b9c547cSRui Paulo {
23945b9c547cSRui Paulo return BN_num_bits(e->prime);
23955b9c547cSRui Paulo }
23965b9c547cSRui Paulo
23975b9c547cSRui Paulo
crypto_ec_order_len(struct crypto_ec * e)239885732ac8SCy Schubert size_t crypto_ec_order_len(struct crypto_ec *e)
239985732ac8SCy Schubert {
240085732ac8SCy Schubert return BN_num_bytes(e->order);
240185732ac8SCy Schubert }
240285732ac8SCy Schubert
240385732ac8SCy Schubert
crypto_ec_get_prime(struct crypto_ec * e)24045b9c547cSRui Paulo const struct crypto_bignum * crypto_ec_get_prime(struct crypto_ec *e)
24055b9c547cSRui Paulo {
24065b9c547cSRui Paulo return (const struct crypto_bignum *) e->prime;
24075b9c547cSRui Paulo }
24085b9c547cSRui Paulo
24095b9c547cSRui Paulo
crypto_ec_get_order(struct crypto_ec * e)24105b9c547cSRui Paulo const struct crypto_bignum * crypto_ec_get_order(struct crypto_ec *e)
24115b9c547cSRui Paulo {
24125b9c547cSRui Paulo return (const struct crypto_bignum *) e->order;
24135b9c547cSRui Paulo }
24145b9c547cSRui Paulo
24155b9c547cSRui Paulo
crypto_ec_get_a(struct crypto_ec * e)2416c1d255d3SCy Schubert const struct crypto_bignum * crypto_ec_get_a(struct crypto_ec *e)
2417c1d255d3SCy Schubert {
2418c1d255d3SCy Schubert return (const struct crypto_bignum *) e->a;
2419c1d255d3SCy Schubert }
2420c1d255d3SCy Schubert
2421c1d255d3SCy Schubert
crypto_ec_get_b(struct crypto_ec * e)2422c1d255d3SCy Schubert const struct crypto_bignum * crypto_ec_get_b(struct crypto_ec *e)
2423c1d255d3SCy Schubert {
2424c1d255d3SCy Schubert return (const struct crypto_bignum *) e->b;
2425c1d255d3SCy Schubert }
2426c1d255d3SCy Schubert
2427c1d255d3SCy Schubert
crypto_ec_get_generator(struct crypto_ec * e)24284b72b91aSCy Schubert const struct crypto_ec_point * crypto_ec_get_generator(struct crypto_ec *e)
24294b72b91aSCy Schubert {
24304b72b91aSCy Schubert return (const struct crypto_ec_point *)
24314b72b91aSCy Schubert EC_GROUP_get0_generator(e->group);
24324b72b91aSCy Schubert }
24334b72b91aSCy Schubert
24344b72b91aSCy Schubert
crypto_ec_point_deinit(struct crypto_ec_point * p,int clear)24355b9c547cSRui Paulo void crypto_ec_point_deinit(struct crypto_ec_point *p, int clear)
24365b9c547cSRui Paulo {
24375b9c547cSRui Paulo if (clear)
24385b9c547cSRui Paulo EC_POINT_clear_free((EC_POINT *) p);
24395b9c547cSRui Paulo else
24405b9c547cSRui Paulo EC_POINT_free((EC_POINT *) p);
24415b9c547cSRui Paulo }
24425b9c547cSRui Paulo
24435b9c547cSRui Paulo
crypto_ec_point_x(struct crypto_ec * e,const struct crypto_ec_point * p,struct crypto_bignum * x)244485732ac8SCy Schubert int crypto_ec_point_x(struct crypto_ec *e, const struct crypto_ec_point *p,
244585732ac8SCy Schubert struct crypto_bignum *x)
244685732ac8SCy Schubert {
2447*a90b9d01SCy Schubert return EC_POINT_get_affine_coordinates(e->group,
244885732ac8SCy Schubert (const EC_POINT *) p,
244985732ac8SCy Schubert (BIGNUM *) x, NULL,
245085732ac8SCy Schubert e->bnctx) == 1 ? 0 : -1;
245185732ac8SCy Schubert }
245285732ac8SCy Schubert
245385732ac8SCy Schubert
crypto_ec_point_to_bin(struct crypto_ec * e,const struct crypto_ec_point * point,u8 * x,u8 * y)24545b9c547cSRui Paulo int crypto_ec_point_to_bin(struct crypto_ec *e,
24555b9c547cSRui Paulo const struct crypto_ec_point *point, u8 *x, u8 *y)
24565b9c547cSRui Paulo {
24575b9c547cSRui Paulo BIGNUM *x_bn, *y_bn;
24585b9c547cSRui Paulo int ret = -1;
24595b9c547cSRui Paulo int len = BN_num_bytes(e->prime);
24605b9c547cSRui Paulo
2461780fb4a2SCy Schubert if (TEST_FAIL())
2462780fb4a2SCy Schubert return -1;
2463780fb4a2SCy Schubert
24645b9c547cSRui Paulo x_bn = BN_new();
24655b9c547cSRui Paulo y_bn = BN_new();
24665b9c547cSRui Paulo
24675b9c547cSRui Paulo if (x_bn && y_bn &&
2468*a90b9d01SCy Schubert EC_POINT_get_affine_coordinates(e->group, (EC_POINT *) point,
24695b9c547cSRui Paulo x_bn, y_bn, e->bnctx)) {
24705b9c547cSRui Paulo if (x) {
2471*a90b9d01SCy Schubert ret = crypto_bignum_to_bin(
2472*a90b9d01SCy Schubert (struct crypto_bignum *) x_bn, x, len, len);
24735b9c547cSRui Paulo }
2474*a90b9d01SCy Schubert if (ret >= 0 && y) {
2475*a90b9d01SCy Schubert ret = crypto_bignum_to_bin(
2476*a90b9d01SCy Schubert (struct crypto_bignum *) y_bn, y, len, len);
24775b9c547cSRui Paulo }
2478*a90b9d01SCy Schubert
2479*a90b9d01SCy Schubert if (ret > 0)
24805b9c547cSRui Paulo ret = 0;
24815b9c547cSRui Paulo }
24825b9c547cSRui Paulo
24835b9c547cSRui Paulo BN_clear_free(x_bn);
24845b9c547cSRui Paulo BN_clear_free(y_bn);
24855b9c547cSRui Paulo return ret;
24865b9c547cSRui Paulo }
24875b9c547cSRui Paulo
24885b9c547cSRui Paulo
crypto_ec_point_from_bin(struct crypto_ec * e,const u8 * val)24895b9c547cSRui Paulo struct crypto_ec_point * crypto_ec_point_from_bin(struct crypto_ec *e,
24905b9c547cSRui Paulo const u8 *val)
24915b9c547cSRui Paulo {
24925b9c547cSRui Paulo BIGNUM *x, *y;
24935b9c547cSRui Paulo EC_POINT *elem;
24945b9c547cSRui Paulo int len = BN_num_bytes(e->prime);
24955b9c547cSRui Paulo
2496780fb4a2SCy Schubert if (TEST_FAIL())
2497780fb4a2SCy Schubert return NULL;
2498780fb4a2SCy Schubert
24995b9c547cSRui Paulo x = BN_bin2bn(val, len, NULL);
25005b9c547cSRui Paulo y = BN_bin2bn(val + len, len, NULL);
25015b9c547cSRui Paulo elem = EC_POINT_new(e->group);
25025b9c547cSRui Paulo if (x == NULL || y == NULL || elem == NULL) {
25035b9c547cSRui Paulo BN_clear_free(x);
25045b9c547cSRui Paulo BN_clear_free(y);
25055b9c547cSRui Paulo EC_POINT_clear_free(elem);
25065b9c547cSRui Paulo return NULL;
25075b9c547cSRui Paulo }
25085b9c547cSRui Paulo
2509*a90b9d01SCy Schubert if (!EC_POINT_set_affine_coordinates(e->group, elem, x, y, e->bnctx)) {
25105b9c547cSRui Paulo EC_POINT_clear_free(elem);
25115b9c547cSRui Paulo elem = NULL;
25125b9c547cSRui Paulo }
25135b9c547cSRui Paulo
25145b9c547cSRui Paulo BN_clear_free(x);
25155b9c547cSRui Paulo BN_clear_free(y);
25165b9c547cSRui Paulo
25175b9c547cSRui Paulo return (struct crypto_ec_point *) elem;
25185b9c547cSRui Paulo }
25195b9c547cSRui Paulo
25205b9c547cSRui Paulo
crypto_ec_point_add(struct crypto_ec * e,const struct crypto_ec_point * a,const struct crypto_ec_point * b,struct crypto_ec_point * c)25215b9c547cSRui Paulo int crypto_ec_point_add(struct crypto_ec *e, const struct crypto_ec_point *a,
25225b9c547cSRui Paulo const struct crypto_ec_point *b,
25235b9c547cSRui Paulo struct crypto_ec_point *c)
25245b9c547cSRui Paulo {
2525780fb4a2SCy Schubert if (TEST_FAIL())
2526780fb4a2SCy Schubert return -1;
25275b9c547cSRui Paulo return EC_POINT_add(e->group, (EC_POINT *) c, (const EC_POINT *) a,
25285b9c547cSRui Paulo (const EC_POINT *) b, e->bnctx) ? 0 : -1;
25295b9c547cSRui Paulo }
25305b9c547cSRui Paulo
25315b9c547cSRui Paulo
crypto_ec_point_mul(struct crypto_ec * e,const struct crypto_ec_point * p,const struct crypto_bignum * b,struct crypto_ec_point * res)25325b9c547cSRui Paulo int crypto_ec_point_mul(struct crypto_ec *e, const struct crypto_ec_point *p,
25335b9c547cSRui Paulo const struct crypto_bignum *b,
25345b9c547cSRui Paulo struct crypto_ec_point *res)
25355b9c547cSRui Paulo {
2536780fb4a2SCy Schubert if (TEST_FAIL())
2537780fb4a2SCy Schubert return -1;
25385b9c547cSRui Paulo return EC_POINT_mul(e->group, (EC_POINT *) res, NULL,
25395b9c547cSRui Paulo (const EC_POINT *) p, (const BIGNUM *) b, e->bnctx)
25405b9c547cSRui Paulo ? 0 : -1;
25415b9c547cSRui Paulo }
25425b9c547cSRui Paulo
25435b9c547cSRui Paulo
crypto_ec_point_invert(struct crypto_ec * e,struct crypto_ec_point * p)25445b9c547cSRui Paulo int crypto_ec_point_invert(struct crypto_ec *e, struct crypto_ec_point *p)
25455b9c547cSRui Paulo {
2546780fb4a2SCy Schubert if (TEST_FAIL())
2547780fb4a2SCy Schubert return -1;
25485b9c547cSRui Paulo return EC_POINT_invert(e->group, (EC_POINT *) p, e->bnctx) ? 0 : -1;
25495b9c547cSRui Paulo }
25505b9c547cSRui Paulo
25515b9c547cSRui Paulo
2552325151a3SRui Paulo struct crypto_bignum *
crypto_ec_point_compute_y_sqr(struct crypto_ec * e,const struct crypto_bignum * x)2553325151a3SRui Paulo crypto_ec_point_compute_y_sqr(struct crypto_ec *e,
2554325151a3SRui Paulo const struct crypto_bignum *x)
2555325151a3SRui Paulo {
2556ec080394SCy Schubert BIGNUM *tmp;
2557325151a3SRui Paulo
2558780fb4a2SCy Schubert if (TEST_FAIL())
2559780fb4a2SCy Schubert return NULL;
2560780fb4a2SCy Schubert
2561325151a3SRui Paulo tmp = BN_new();
2562325151a3SRui Paulo
2563ec080394SCy Schubert /* y^2 = x^3 + ax + b = (x^2 + a)x + b */
2564ec080394SCy Schubert if (tmp &&
2565325151a3SRui Paulo BN_mod_sqr(tmp, (const BIGNUM *) x, e->prime, e->bnctx) &&
2566ec080394SCy Schubert BN_mod_add_quick(tmp, e->a, tmp, e->prime) &&
2567325151a3SRui Paulo BN_mod_mul(tmp, tmp, (const BIGNUM *) x, e->prime, e->bnctx) &&
2568ec080394SCy Schubert BN_mod_add_quick(tmp, tmp, e->b, e->prime))
2569ec080394SCy Schubert return (struct crypto_bignum *) tmp;
2570325151a3SRui Paulo
2571325151a3SRui Paulo BN_clear_free(tmp);
2572ec080394SCy Schubert return NULL;
2573325151a3SRui Paulo }
2574325151a3SRui Paulo
2575325151a3SRui Paulo
crypto_ec_point_is_at_infinity(struct crypto_ec * e,const struct crypto_ec_point * p)25765b9c547cSRui Paulo int crypto_ec_point_is_at_infinity(struct crypto_ec *e,
25775b9c547cSRui Paulo const struct crypto_ec_point *p)
25785b9c547cSRui Paulo {
25795b9c547cSRui Paulo return EC_POINT_is_at_infinity(e->group, (const EC_POINT *) p);
25805b9c547cSRui Paulo }
25815b9c547cSRui Paulo
25825b9c547cSRui Paulo
crypto_ec_point_is_on_curve(struct crypto_ec * e,const struct crypto_ec_point * p)25835b9c547cSRui Paulo int crypto_ec_point_is_on_curve(struct crypto_ec *e,
25845b9c547cSRui Paulo const struct crypto_ec_point *p)
25855b9c547cSRui Paulo {
2586325151a3SRui Paulo return EC_POINT_is_on_curve(e->group, (const EC_POINT *) p,
2587325151a3SRui Paulo e->bnctx) == 1;
2588325151a3SRui Paulo }
2589325151a3SRui Paulo
2590325151a3SRui Paulo
crypto_ec_point_cmp(const struct crypto_ec * e,const struct crypto_ec_point * a,const struct crypto_ec_point * b)2591325151a3SRui Paulo int crypto_ec_point_cmp(const struct crypto_ec *e,
2592325151a3SRui Paulo const struct crypto_ec_point *a,
2593325151a3SRui Paulo const struct crypto_ec_point *b)
2594325151a3SRui Paulo {
2595325151a3SRui Paulo return EC_POINT_cmp(e->group, (const EC_POINT *) a,
2596325151a3SRui Paulo (const EC_POINT *) b, e->bnctx);
25975b9c547cSRui Paulo }
25985b9c547cSRui Paulo
259985732ac8SCy Schubert
crypto_ec_point_debug_print(const struct crypto_ec * e,const struct crypto_ec_point * p,const char * title)26004b72b91aSCy Schubert void crypto_ec_point_debug_print(const struct crypto_ec *e,
26014b72b91aSCy Schubert const struct crypto_ec_point *p,
26024b72b91aSCy Schubert const char *title)
26034b72b91aSCy Schubert {
26044b72b91aSCy Schubert BIGNUM *x, *y;
26054b72b91aSCy Schubert char *x_str = NULL, *y_str = NULL;
26064b72b91aSCy Schubert
26074b72b91aSCy Schubert x = BN_new();
26084b72b91aSCy Schubert y = BN_new();
26094b72b91aSCy Schubert if (!x || !y ||
2610*a90b9d01SCy Schubert EC_POINT_get_affine_coordinates(e->group, (const EC_POINT *) p,
26114b72b91aSCy Schubert x, y, e->bnctx) != 1)
26124b72b91aSCy Schubert goto fail;
26134b72b91aSCy Schubert
26144b72b91aSCy Schubert x_str = BN_bn2hex(x);
26154b72b91aSCy Schubert y_str = BN_bn2hex(y);
26164b72b91aSCy Schubert if (!x_str || !y_str)
26174b72b91aSCy Schubert goto fail;
26184b72b91aSCy Schubert
26194b72b91aSCy Schubert wpa_printf(MSG_DEBUG, "%s (%s,%s)", title, x_str, y_str);
26204b72b91aSCy Schubert
26214b72b91aSCy Schubert fail:
26224b72b91aSCy Schubert OPENSSL_free(x_str);
26234b72b91aSCy Schubert OPENSSL_free(y_str);
26244b72b91aSCy Schubert BN_free(x);
26254b72b91aSCy Schubert BN_free(y);
26264b72b91aSCy Schubert }
26274b72b91aSCy Schubert
26284b72b91aSCy Schubert
262985732ac8SCy Schubert struct crypto_ecdh {
263085732ac8SCy Schubert struct crypto_ec *ec;
263185732ac8SCy Schubert EVP_PKEY *pkey;
263285732ac8SCy Schubert };
263385732ac8SCy Schubert
crypto_ecdh_init(int group)263485732ac8SCy Schubert struct crypto_ecdh * crypto_ecdh_init(int group)
263585732ac8SCy Schubert {
2636*a90b9d01SCy Schubert #if OPENSSL_VERSION_NUMBER >= 0x30000000L
2637*a90b9d01SCy Schubert struct crypto_ecdh *ecdh;
2638*a90b9d01SCy Schubert const char *name;
2639*a90b9d01SCy Schubert
2640*a90b9d01SCy Schubert ecdh = os_zalloc(sizeof(*ecdh));
2641*a90b9d01SCy Schubert if (!ecdh)
2642*a90b9d01SCy Schubert goto fail;
2643*a90b9d01SCy Schubert
2644*a90b9d01SCy Schubert ecdh->ec = crypto_ec_init(group);
2645*a90b9d01SCy Schubert if (!ecdh->ec)
2646*a90b9d01SCy Schubert goto fail;
2647*a90b9d01SCy Schubert
2648*a90b9d01SCy Schubert name = OSSL_EC_curve_nid2name(ecdh->ec->nid);
2649*a90b9d01SCy Schubert if (!name)
2650*a90b9d01SCy Schubert goto fail;
2651*a90b9d01SCy Schubert
2652*a90b9d01SCy Schubert ecdh->pkey = EVP_EC_gen(name);
2653*a90b9d01SCy Schubert if (!ecdh->pkey)
2654*a90b9d01SCy Schubert goto fail;
2655*a90b9d01SCy Schubert
2656*a90b9d01SCy Schubert done:
2657*a90b9d01SCy Schubert return ecdh;
2658*a90b9d01SCy Schubert fail:
2659*a90b9d01SCy Schubert crypto_ecdh_deinit(ecdh);
2660*a90b9d01SCy Schubert ecdh = NULL;
2661*a90b9d01SCy Schubert goto done;
2662*a90b9d01SCy Schubert #else /* OpenSSL version >= 3.0 */
266385732ac8SCy Schubert struct crypto_ecdh *ecdh;
266485732ac8SCy Schubert EVP_PKEY *params = NULL;
2665206b73d0SCy Schubert EC_KEY *ec_params = NULL;
266685732ac8SCy Schubert EVP_PKEY_CTX *kctx = NULL;
266785732ac8SCy Schubert
266885732ac8SCy Schubert ecdh = os_zalloc(sizeof(*ecdh));
266985732ac8SCy Schubert if (!ecdh)
267085732ac8SCy Schubert goto fail;
267185732ac8SCy Schubert
267285732ac8SCy Schubert ecdh->ec = crypto_ec_init(group);
267385732ac8SCy Schubert if (!ecdh->ec)
267485732ac8SCy Schubert goto fail;
267585732ac8SCy Schubert
267685732ac8SCy Schubert ec_params = EC_KEY_new_by_curve_name(ecdh->ec->nid);
267785732ac8SCy Schubert if (!ec_params) {
267885732ac8SCy Schubert wpa_printf(MSG_ERROR,
267985732ac8SCy Schubert "OpenSSL: Failed to generate EC_KEY parameters");
268085732ac8SCy Schubert goto fail;
268185732ac8SCy Schubert }
268285732ac8SCy Schubert EC_KEY_set_asn1_flag(ec_params, OPENSSL_EC_NAMED_CURVE);
268385732ac8SCy Schubert params = EVP_PKEY_new();
268485732ac8SCy Schubert if (!params || EVP_PKEY_set1_EC_KEY(params, ec_params) != 1) {
268585732ac8SCy Schubert wpa_printf(MSG_ERROR,
268685732ac8SCy Schubert "OpenSSL: Failed to generate EVP_PKEY parameters");
268785732ac8SCy Schubert goto fail;
268885732ac8SCy Schubert }
268985732ac8SCy Schubert
269085732ac8SCy Schubert kctx = EVP_PKEY_CTX_new(params, NULL);
269185732ac8SCy Schubert if (!kctx)
269285732ac8SCy Schubert goto fail;
269385732ac8SCy Schubert
269485732ac8SCy Schubert if (EVP_PKEY_keygen_init(kctx) != 1) {
269585732ac8SCy Schubert wpa_printf(MSG_ERROR,
269685732ac8SCy Schubert "OpenSSL: EVP_PKEY_keygen_init failed: %s",
269785732ac8SCy Schubert ERR_error_string(ERR_get_error(), NULL));
269885732ac8SCy Schubert goto fail;
269985732ac8SCy Schubert }
270085732ac8SCy Schubert
270185732ac8SCy Schubert if (EVP_PKEY_keygen(kctx, &ecdh->pkey) != 1) {
270285732ac8SCy Schubert wpa_printf(MSG_ERROR, "OpenSSL: EVP_PKEY_keygen failed: %s",
270385732ac8SCy Schubert ERR_error_string(ERR_get_error(), NULL));
270485732ac8SCy Schubert goto fail;
270585732ac8SCy Schubert }
270685732ac8SCy Schubert
270785732ac8SCy Schubert done:
2708206b73d0SCy Schubert EC_KEY_free(ec_params);
270985732ac8SCy Schubert EVP_PKEY_free(params);
271085732ac8SCy Schubert EVP_PKEY_CTX_free(kctx);
271185732ac8SCy Schubert
271285732ac8SCy Schubert return ecdh;
271385732ac8SCy Schubert fail:
271485732ac8SCy Schubert crypto_ecdh_deinit(ecdh);
271585732ac8SCy Schubert ecdh = NULL;
271685732ac8SCy Schubert goto done;
2717*a90b9d01SCy Schubert #endif /* OpenSSL version >= 3.0 */
271885732ac8SCy Schubert }
271985732ac8SCy Schubert
272085732ac8SCy Schubert
crypto_ecdh_init2(int group,struct crypto_ec_key * own_key)27214b72b91aSCy Schubert struct crypto_ecdh * crypto_ecdh_init2(int group, struct crypto_ec_key *own_key)
27224b72b91aSCy Schubert {
2723*a90b9d01SCy Schubert #if OPENSSL_VERSION_NUMBER >= 0x30000000L
2724*a90b9d01SCy Schubert struct crypto_ecdh *ecdh;
2725*a90b9d01SCy Schubert
2726*a90b9d01SCy Schubert ecdh = os_zalloc(sizeof(*ecdh));
2727*a90b9d01SCy Schubert if (!ecdh)
2728*a90b9d01SCy Schubert goto fail;
2729*a90b9d01SCy Schubert
2730*a90b9d01SCy Schubert ecdh->ec = crypto_ec_init(group);
2731*a90b9d01SCy Schubert if (!ecdh->ec)
2732*a90b9d01SCy Schubert goto fail;
2733*a90b9d01SCy Schubert
2734*a90b9d01SCy Schubert ecdh->pkey = EVP_PKEY_dup((EVP_PKEY *) own_key);
2735*a90b9d01SCy Schubert if (!ecdh->pkey)
2736*a90b9d01SCy Schubert goto fail;
2737*a90b9d01SCy Schubert
2738*a90b9d01SCy Schubert return ecdh;
2739*a90b9d01SCy Schubert fail:
2740*a90b9d01SCy Schubert crypto_ecdh_deinit(ecdh);
2741*a90b9d01SCy Schubert return NULL;
2742*a90b9d01SCy Schubert #else /* OpenSSL version >= 3.0 */
27434b72b91aSCy Schubert struct crypto_ecdh *ecdh;
27444b72b91aSCy Schubert
27454b72b91aSCy Schubert ecdh = os_zalloc(sizeof(*ecdh));
27464b72b91aSCy Schubert if (!ecdh)
27474b72b91aSCy Schubert goto fail;
27484b72b91aSCy Schubert
27494b72b91aSCy Schubert ecdh->ec = crypto_ec_init(group);
27504b72b91aSCy Schubert if (!ecdh->ec)
27514b72b91aSCy Schubert goto fail;
27524b72b91aSCy Schubert
27534b72b91aSCy Schubert ecdh->pkey = EVP_PKEY_new();
27544b72b91aSCy Schubert if (!ecdh->pkey ||
27554b72b91aSCy Schubert EVP_PKEY_assign_EC_KEY(ecdh->pkey,
27564b72b91aSCy Schubert EVP_PKEY_get1_EC_KEY((EVP_PKEY *) own_key))
27574b72b91aSCy Schubert != 1)
27584b72b91aSCy Schubert goto fail;
27594b72b91aSCy Schubert
27604b72b91aSCy Schubert return ecdh;
27614b72b91aSCy Schubert fail:
27624b72b91aSCy Schubert crypto_ecdh_deinit(ecdh);
27634b72b91aSCy Schubert return NULL;
2764*a90b9d01SCy Schubert #endif /* OpenSSL version >= 3.0 */
27654b72b91aSCy Schubert }
27664b72b91aSCy Schubert
27674b72b91aSCy Schubert
crypto_ecdh_get_pubkey(struct crypto_ecdh * ecdh,int inc_y)276885732ac8SCy Schubert struct wpabuf * crypto_ecdh_get_pubkey(struct crypto_ecdh *ecdh, int inc_y)
276985732ac8SCy Schubert {
2770*a90b9d01SCy Schubert #if OPENSSL_VERSION_NUMBER >= 0x30000000L
2771*a90b9d01SCy Schubert struct wpabuf *buf = NULL;
2772*a90b9d01SCy Schubert unsigned char *pub;
2773*a90b9d01SCy Schubert size_t len, exp_len;
2774*a90b9d01SCy Schubert
2775*a90b9d01SCy Schubert len = EVP_PKEY_get1_encoded_public_key(ecdh->pkey, &pub);
2776*a90b9d01SCy Schubert if (len == 0)
2777*a90b9d01SCy Schubert return NULL;
2778*a90b9d01SCy Schubert
2779*a90b9d01SCy Schubert /* Encoded using SECG SEC 1, Sec. 2.3.4 format */
2780*a90b9d01SCy Schubert exp_len = 1 + 2 * crypto_ec_prime_len(ecdh->ec);
2781*a90b9d01SCy Schubert if (len != exp_len) {
2782*a90b9d01SCy Schubert wpa_printf(MSG_ERROR,
2783*a90b9d01SCy Schubert "OpenSSL:%s: Unexpected encoded public key length %zu (expected %zu)",
2784*a90b9d01SCy Schubert __func__, len, exp_len);
2785*a90b9d01SCy Schubert goto fail;
2786*a90b9d01SCy Schubert }
2787*a90b9d01SCy Schubert buf = wpabuf_alloc_copy(pub + 1, inc_y ? len - 1 : len / 2);
2788*a90b9d01SCy Schubert fail:
2789*a90b9d01SCy Schubert OPENSSL_free(pub);
2790*a90b9d01SCy Schubert return buf;
2791*a90b9d01SCy Schubert #else /* OpenSSL version >= 3.0 */
279285732ac8SCy Schubert struct wpabuf *buf = NULL;
279385732ac8SCy Schubert EC_KEY *eckey;
279485732ac8SCy Schubert const EC_POINT *pubkey;
279585732ac8SCy Schubert BIGNUM *x, *y = NULL;
279685732ac8SCy Schubert int len = BN_num_bytes(ecdh->ec->prime);
279785732ac8SCy Schubert int res;
279885732ac8SCy Schubert
279985732ac8SCy Schubert eckey = EVP_PKEY_get1_EC_KEY(ecdh->pkey);
280085732ac8SCy Schubert if (!eckey)
280185732ac8SCy Schubert return NULL;
280285732ac8SCy Schubert
280385732ac8SCy Schubert pubkey = EC_KEY_get0_public_key(eckey);
280485732ac8SCy Schubert if (!pubkey)
280585732ac8SCy Schubert return NULL;
280685732ac8SCy Schubert
280785732ac8SCy Schubert x = BN_new();
280885732ac8SCy Schubert if (inc_y) {
280985732ac8SCy Schubert y = BN_new();
281085732ac8SCy Schubert if (!y)
281185732ac8SCy Schubert goto fail;
281285732ac8SCy Schubert }
281385732ac8SCy Schubert buf = wpabuf_alloc(inc_y ? 2 * len : len);
281485732ac8SCy Schubert if (!x || !buf)
281585732ac8SCy Schubert goto fail;
281685732ac8SCy Schubert
2817*a90b9d01SCy Schubert if (EC_POINT_get_affine_coordinates(ecdh->ec->group, pubkey,
281885732ac8SCy Schubert x, y, ecdh->ec->bnctx) != 1) {
281985732ac8SCy Schubert wpa_printf(MSG_ERROR,
2820*a90b9d01SCy Schubert "OpenSSL: EC_POINT_get_affine_coordinates failed: %s",
282185732ac8SCy Schubert ERR_error_string(ERR_get_error(), NULL));
282285732ac8SCy Schubert goto fail;
282385732ac8SCy Schubert }
282485732ac8SCy Schubert
282585732ac8SCy Schubert res = crypto_bignum_to_bin((struct crypto_bignum *) x,
282685732ac8SCy Schubert wpabuf_put(buf, len), len, len);
282785732ac8SCy Schubert if (res < 0)
282885732ac8SCy Schubert goto fail;
282985732ac8SCy Schubert
283085732ac8SCy Schubert if (inc_y) {
283185732ac8SCy Schubert res = crypto_bignum_to_bin((struct crypto_bignum *) y,
283285732ac8SCy Schubert wpabuf_put(buf, len), len, len);
283385732ac8SCy Schubert if (res < 0)
283485732ac8SCy Schubert goto fail;
283585732ac8SCy Schubert }
283685732ac8SCy Schubert
283785732ac8SCy Schubert done:
283885732ac8SCy Schubert BN_clear_free(x);
283985732ac8SCy Schubert BN_clear_free(y);
284085732ac8SCy Schubert EC_KEY_free(eckey);
284185732ac8SCy Schubert
284285732ac8SCy Schubert return buf;
284385732ac8SCy Schubert fail:
284485732ac8SCy Schubert wpabuf_free(buf);
284585732ac8SCy Schubert buf = NULL;
284685732ac8SCy Schubert goto done;
2847*a90b9d01SCy Schubert #endif /* OpenSSL version >= 3.0 */
284885732ac8SCy Schubert }
284985732ac8SCy Schubert
285085732ac8SCy Schubert
crypto_ecdh_set_peerkey(struct crypto_ecdh * ecdh,int inc_y,const u8 * key,size_t len)285185732ac8SCy Schubert struct wpabuf * crypto_ecdh_set_peerkey(struct crypto_ecdh *ecdh, int inc_y,
285285732ac8SCy Schubert const u8 *key, size_t len)
285385732ac8SCy Schubert {
2854*a90b9d01SCy Schubert #if OPENSSL_VERSION_NUMBER >= 0x30000000L
2855*a90b9d01SCy Schubert EVP_PKEY *peerkey = EVP_PKEY_new();
2856*a90b9d01SCy Schubert EVP_PKEY_CTX *ctx;
2857*a90b9d01SCy Schubert size_t res_len;
2858*a90b9d01SCy Schubert struct wpabuf *res = NULL;
2859*a90b9d01SCy Schubert u8 *peer;
2860*a90b9d01SCy Schubert
2861*a90b9d01SCy Schubert /* Encode using SECG SEC 1, Sec. 2.3.4 format */
2862*a90b9d01SCy Schubert peer = os_malloc(1 + len);
2863*a90b9d01SCy Schubert if (!peer) {
2864*a90b9d01SCy Schubert EVP_PKEY_free(peerkey);
2865*a90b9d01SCy Schubert return NULL;
2866*a90b9d01SCy Schubert }
2867*a90b9d01SCy Schubert peer[0] = inc_y ? 0x04 : 0x02;
2868*a90b9d01SCy Schubert os_memcpy(peer + 1, key, len);
2869*a90b9d01SCy Schubert
2870*a90b9d01SCy Schubert if (!peerkey ||
2871*a90b9d01SCy Schubert EVP_PKEY_copy_parameters(peerkey, ecdh->pkey) != 1 ||
2872*a90b9d01SCy Schubert EVP_PKEY_set1_encoded_public_key(peerkey, peer, 1 + len) != 1) {
2873*a90b9d01SCy Schubert wpa_printf(MSG_INFO, "OpenSSL: EVP_PKEY_set1_encoded_public_key failed: %s",
2874*a90b9d01SCy Schubert ERR_error_string(ERR_get_error(), NULL));
2875*a90b9d01SCy Schubert EVP_PKEY_free(peerkey);
2876*a90b9d01SCy Schubert os_free(peer);
2877*a90b9d01SCy Schubert return NULL;
2878*a90b9d01SCy Schubert }
2879*a90b9d01SCy Schubert os_free(peer);
2880*a90b9d01SCy Schubert
2881*a90b9d01SCy Schubert ctx = EVP_PKEY_CTX_new(ecdh->pkey, NULL);
2882*a90b9d01SCy Schubert if (!ctx ||
2883*a90b9d01SCy Schubert EVP_PKEY_derive_init(ctx) != 1 ||
2884*a90b9d01SCy Schubert EVP_PKEY_derive_set_peer(ctx, peerkey) != 1 ||
2885*a90b9d01SCy Schubert EVP_PKEY_derive(ctx, NULL, &res_len) != 1 ||
2886*a90b9d01SCy Schubert !(res = wpabuf_alloc(res_len)) ||
2887*a90b9d01SCy Schubert EVP_PKEY_derive(ctx, wpabuf_mhead(res), &res_len) != 1) {
2888*a90b9d01SCy Schubert wpa_printf(MSG_INFO, "OpenSSL: EVP_PKEY_derive failed: %s",
2889*a90b9d01SCy Schubert ERR_error_string(ERR_get_error(), NULL));
2890*a90b9d01SCy Schubert wpabuf_free(res);
2891*a90b9d01SCy Schubert res = NULL;
2892*a90b9d01SCy Schubert } else {
2893*a90b9d01SCy Schubert wpabuf_put(res, res_len);
2894*a90b9d01SCy Schubert }
2895*a90b9d01SCy Schubert
2896*a90b9d01SCy Schubert EVP_PKEY_free(peerkey);
2897*a90b9d01SCy Schubert EVP_PKEY_CTX_free(ctx);
2898*a90b9d01SCy Schubert return res;
2899*a90b9d01SCy Schubert #else /* OpenSSL version >= 3.0 */
290085732ac8SCy Schubert BIGNUM *x, *y = NULL;
290185732ac8SCy Schubert EVP_PKEY_CTX *ctx = NULL;
290285732ac8SCy Schubert EVP_PKEY *peerkey = NULL;
290385732ac8SCy Schubert struct wpabuf *secret = NULL;
290485732ac8SCy Schubert size_t secret_len;
290585732ac8SCy Schubert EC_POINT *pub;
290685732ac8SCy Schubert EC_KEY *eckey = NULL;
290785732ac8SCy Schubert
290885732ac8SCy Schubert x = BN_bin2bn(key, inc_y ? len / 2 : len, NULL);
290985732ac8SCy Schubert pub = EC_POINT_new(ecdh->ec->group);
291085732ac8SCy Schubert if (!x || !pub)
291185732ac8SCy Schubert goto fail;
291285732ac8SCy Schubert
291385732ac8SCy Schubert if (inc_y) {
291485732ac8SCy Schubert y = BN_bin2bn(key + len / 2, len / 2, NULL);
291585732ac8SCy Schubert if (!y)
291685732ac8SCy Schubert goto fail;
2917*a90b9d01SCy Schubert if (!EC_POINT_set_affine_coordinates(ecdh->ec->group, pub,
2918*a90b9d01SCy Schubert x, y, ecdh->ec->bnctx)) {
291985732ac8SCy Schubert wpa_printf(MSG_ERROR,
2920*a90b9d01SCy Schubert "OpenSSL: EC_POINT_set_affine_coordinates failed: %s",
292185732ac8SCy Schubert ERR_error_string(ERR_get_error(), NULL));
292285732ac8SCy Schubert goto fail;
292385732ac8SCy Schubert }
2924*a90b9d01SCy Schubert } else if (!EC_POINT_set_compressed_coordinates(ecdh->ec->group,
292585732ac8SCy Schubert pub, x, 0,
292685732ac8SCy Schubert ecdh->ec->bnctx)) {
292785732ac8SCy Schubert wpa_printf(MSG_ERROR,
2928*a90b9d01SCy Schubert "OpenSSL: EC_POINT_set_compressed_coordinates failed: %s",
292985732ac8SCy Schubert ERR_error_string(ERR_get_error(), NULL));
293085732ac8SCy Schubert goto fail;
293185732ac8SCy Schubert }
293285732ac8SCy Schubert
293385732ac8SCy Schubert if (!EC_POINT_is_on_curve(ecdh->ec->group, pub, ecdh->ec->bnctx)) {
293485732ac8SCy Schubert wpa_printf(MSG_ERROR,
293585732ac8SCy Schubert "OpenSSL: ECDH peer public key is not on curve");
293685732ac8SCy Schubert goto fail;
293785732ac8SCy Schubert }
293885732ac8SCy Schubert
293985732ac8SCy Schubert eckey = EC_KEY_new_by_curve_name(ecdh->ec->nid);
294085732ac8SCy Schubert if (!eckey || EC_KEY_set_public_key(eckey, pub) != 1) {
294185732ac8SCy Schubert wpa_printf(MSG_ERROR,
294285732ac8SCy Schubert "OpenSSL: EC_KEY_set_public_key failed: %s",
294385732ac8SCy Schubert ERR_error_string(ERR_get_error(), NULL));
294485732ac8SCy Schubert goto fail;
294585732ac8SCy Schubert }
294685732ac8SCy Schubert
294785732ac8SCy Schubert peerkey = EVP_PKEY_new();
294885732ac8SCy Schubert if (!peerkey || EVP_PKEY_set1_EC_KEY(peerkey, eckey) != 1)
294985732ac8SCy Schubert goto fail;
295085732ac8SCy Schubert
295185732ac8SCy Schubert ctx = EVP_PKEY_CTX_new(ecdh->pkey, NULL);
295285732ac8SCy Schubert if (!ctx || EVP_PKEY_derive_init(ctx) != 1 ||
295385732ac8SCy Schubert EVP_PKEY_derive_set_peer(ctx, peerkey) != 1 ||
295485732ac8SCy Schubert EVP_PKEY_derive(ctx, NULL, &secret_len) != 1) {
295585732ac8SCy Schubert wpa_printf(MSG_ERROR,
295685732ac8SCy Schubert "OpenSSL: EVP_PKEY_derive(1) failed: %s",
295785732ac8SCy Schubert ERR_error_string(ERR_get_error(), NULL));
295885732ac8SCy Schubert goto fail;
295985732ac8SCy Schubert }
296085732ac8SCy Schubert
296185732ac8SCy Schubert secret = wpabuf_alloc(secret_len);
296285732ac8SCy Schubert if (!secret)
296385732ac8SCy Schubert goto fail;
2964206b73d0SCy Schubert if (EVP_PKEY_derive(ctx, wpabuf_put(secret, 0), &secret_len) != 1) {
296585732ac8SCy Schubert wpa_printf(MSG_ERROR,
296685732ac8SCy Schubert "OpenSSL: EVP_PKEY_derive(2) failed: %s",
296785732ac8SCy Schubert ERR_error_string(ERR_get_error(), NULL));
296885732ac8SCy Schubert goto fail;
296985732ac8SCy Schubert }
2970206b73d0SCy Schubert if (secret->size != secret_len)
2971206b73d0SCy Schubert wpa_printf(MSG_DEBUG,
2972206b73d0SCy Schubert "OpenSSL: EVP_PKEY_derive(2) changed secret_len %d -> %d",
2973206b73d0SCy Schubert (int) secret->size, (int) secret_len);
2974206b73d0SCy Schubert wpabuf_put(secret, secret_len);
297585732ac8SCy Schubert
297685732ac8SCy Schubert done:
297785732ac8SCy Schubert BN_free(x);
297885732ac8SCy Schubert BN_free(y);
297985732ac8SCy Schubert EC_KEY_free(eckey);
298085732ac8SCy Schubert EC_POINT_free(pub);
298185732ac8SCy Schubert EVP_PKEY_CTX_free(ctx);
298285732ac8SCy Schubert EVP_PKEY_free(peerkey);
298385732ac8SCy Schubert return secret;
298485732ac8SCy Schubert fail:
298585732ac8SCy Schubert wpabuf_free(secret);
298685732ac8SCy Schubert secret = NULL;
298785732ac8SCy Schubert goto done;
2988*a90b9d01SCy Schubert #endif /* OpenSSL version >= 3.0 */
298985732ac8SCy Schubert }
299085732ac8SCy Schubert
299185732ac8SCy Schubert
crypto_ecdh_deinit(struct crypto_ecdh * ecdh)299285732ac8SCy Schubert void crypto_ecdh_deinit(struct crypto_ecdh *ecdh)
299385732ac8SCy Schubert {
299485732ac8SCy Schubert if (ecdh) {
299585732ac8SCy Schubert crypto_ec_deinit(ecdh->ec);
299685732ac8SCy Schubert EVP_PKEY_free(ecdh->pkey);
299785732ac8SCy Schubert os_free(ecdh);
299885732ac8SCy Schubert }
299985732ac8SCy Schubert }
300085732ac8SCy Schubert
3001c1d255d3SCy Schubert
crypto_ecdh_prime_len(struct crypto_ecdh * ecdh)3002c1d255d3SCy Schubert size_t crypto_ecdh_prime_len(struct crypto_ecdh *ecdh)
3003c1d255d3SCy Schubert {
3004c1d255d3SCy Schubert return crypto_ec_prime_len(ecdh->ec);
3005c1d255d3SCy Schubert }
3006c1d255d3SCy Schubert
3007c1d255d3SCy Schubert
crypto_ec_key_parse_priv(const u8 * der,size_t der_len)3008c1d255d3SCy Schubert struct crypto_ec_key * crypto_ec_key_parse_priv(const u8 *der, size_t der_len)
3009c1d255d3SCy Schubert {
3010*a90b9d01SCy Schubert #if OPENSSL_VERSION_NUMBER >= 0x30000000L
3011*a90b9d01SCy Schubert EVP_PKEY *pkey = NULL;
3012*a90b9d01SCy Schubert OSSL_DECODER_CTX *ctx;
3013*a90b9d01SCy Schubert
3014*a90b9d01SCy Schubert ctx = OSSL_DECODER_CTX_new_for_pkey(
3015*a90b9d01SCy Schubert &pkey, "DER", NULL, "EC",
3016*a90b9d01SCy Schubert OSSL_KEYMGMT_SELECT_KEYPAIR |
3017*a90b9d01SCy Schubert OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS,
3018*a90b9d01SCy Schubert NULL, NULL);
3019*a90b9d01SCy Schubert if (!ctx ||
3020*a90b9d01SCy Schubert OSSL_DECODER_from_data(ctx, &der, &der_len) != 1) {
3021*a90b9d01SCy Schubert wpa_printf(MSG_INFO,
3022*a90b9d01SCy Schubert "OpenSSL: Decoding EC private key (DER) failed: %s",
3023*a90b9d01SCy Schubert ERR_error_string(ERR_get_error(), NULL));
3024*a90b9d01SCy Schubert if (ctx)
3025*a90b9d01SCy Schubert OSSL_DECODER_CTX_free(ctx);
3026*a90b9d01SCy Schubert goto fail;
3027*a90b9d01SCy Schubert }
3028*a90b9d01SCy Schubert
3029*a90b9d01SCy Schubert OSSL_DECODER_CTX_free(ctx);
3030*a90b9d01SCy Schubert return (struct crypto_ec_key *) pkey;
3031*a90b9d01SCy Schubert fail:
3032*a90b9d01SCy Schubert crypto_ec_key_deinit((struct crypto_ec_key *) pkey);
3033*a90b9d01SCy Schubert return NULL;
3034*a90b9d01SCy Schubert #else /* OpenSSL version >= 3.0 */
30354b72b91aSCy Schubert EVP_PKEY *pkey = NULL;
30364b72b91aSCy Schubert EC_KEY *eckey;
3037c1d255d3SCy Schubert
30384b72b91aSCy Schubert eckey = d2i_ECPrivateKey(NULL, &der, der_len);
30394b72b91aSCy Schubert if (!eckey) {
3040c1d255d3SCy Schubert wpa_printf(MSG_INFO, "OpenSSL: d2i_ECPrivateKey() failed: %s",
3041c1d255d3SCy Schubert ERR_error_string(ERR_get_error(), NULL));
3042c1d255d3SCy Schubert goto fail;
3043c1d255d3SCy Schubert }
30444b72b91aSCy Schubert EC_KEY_set_conv_form(eckey, POINT_CONVERSION_COMPRESSED);
3045c1d255d3SCy Schubert
30464b72b91aSCy Schubert pkey = EVP_PKEY_new();
30474b72b91aSCy Schubert if (!pkey || EVP_PKEY_assign_EC_KEY(pkey, eckey) != 1) {
30484b72b91aSCy Schubert EC_KEY_free(eckey);
3049c1d255d3SCy Schubert goto fail;
3050c1d255d3SCy Schubert }
3051c1d255d3SCy Schubert
30524b72b91aSCy Schubert return (struct crypto_ec_key *) pkey;
3053c1d255d3SCy Schubert fail:
30544b72b91aSCy Schubert crypto_ec_key_deinit((struct crypto_ec_key *) pkey);
3055c1d255d3SCy Schubert return NULL;
3056*a90b9d01SCy Schubert #endif /* OpenSSL version >= 3.0 */
3057*a90b9d01SCy Schubert }
3058*a90b9d01SCy Schubert
3059*a90b9d01SCy Schubert
crypto_ec_key_set_priv(int group,const u8 * raw,size_t raw_len)3060*a90b9d01SCy Schubert struct crypto_ec_key * crypto_ec_key_set_priv(int group,
3061*a90b9d01SCy Schubert const u8 *raw, size_t raw_len)
3062*a90b9d01SCy Schubert {
3063*a90b9d01SCy Schubert #if OPENSSL_VERSION_NUMBER >= 0x30000000L
3064*a90b9d01SCy Schubert const char *group_name;
3065*a90b9d01SCy Schubert OSSL_PARAM params[4];
3066*a90b9d01SCy Schubert EVP_PKEY_CTX *ctx = NULL;
3067*a90b9d01SCy Schubert EVP_PKEY *pkey = NULL;
3068*a90b9d01SCy Schubert BIGNUM *priv;
3069*a90b9d01SCy Schubert EC_POINT *pub = NULL;
3070*a90b9d01SCy Schubert EC_GROUP *ec_group = NULL;
3071*a90b9d01SCy Schubert size_t len;
3072*a90b9d01SCy Schubert u8 *pub_bin = NULL;
3073*a90b9d01SCy Schubert u8 *priv_bin = NULL;
3074*a90b9d01SCy Schubert int priv_bin_len;
3075*a90b9d01SCy Schubert
3076*a90b9d01SCy Schubert group_name = crypto_ec_group_2_name(group);
3077*a90b9d01SCy Schubert if (!group_name)
3078*a90b9d01SCy Schubert return NULL;
3079*a90b9d01SCy Schubert
3080*a90b9d01SCy Schubert priv = BN_bin2bn(raw, raw_len, NULL);
3081*a90b9d01SCy Schubert if (!priv)
3082*a90b9d01SCy Schubert return NULL;
3083*a90b9d01SCy Schubert priv_bin = os_malloc(raw_len);
3084*a90b9d01SCy Schubert if (!priv_bin)
3085*a90b9d01SCy Schubert goto fail;
3086*a90b9d01SCy Schubert priv_bin_len = BN_bn2lebinpad(priv, priv_bin, raw_len);
3087*a90b9d01SCy Schubert if (priv_bin_len < 0)
3088*a90b9d01SCy Schubert goto fail;
3089*a90b9d01SCy Schubert
3090*a90b9d01SCy Schubert ec_group = EC_GROUP_new_by_curve_name(crypto_ec_group_2_nid(group));
3091*a90b9d01SCy Schubert if (!ec_group)
3092*a90b9d01SCy Schubert goto fail;
3093*a90b9d01SCy Schubert pub = EC_POINT_new(ec_group);
3094*a90b9d01SCy Schubert if (!pub ||
3095*a90b9d01SCy Schubert EC_POINT_mul(ec_group, pub, priv, NULL, NULL, NULL) != 1)
3096*a90b9d01SCy Schubert goto fail;
3097*a90b9d01SCy Schubert len = EC_POINT_point2oct(ec_group, pub, POINT_CONVERSION_UNCOMPRESSED,
3098*a90b9d01SCy Schubert NULL, 0, NULL);
3099*a90b9d01SCy Schubert if (len == 0)
3100*a90b9d01SCy Schubert goto fail;
3101*a90b9d01SCy Schubert pub_bin = os_malloc(len);
3102*a90b9d01SCy Schubert if (!pub_bin)
3103*a90b9d01SCy Schubert goto fail;
3104*a90b9d01SCy Schubert len = EC_POINT_point2oct(ec_group, pub, POINT_CONVERSION_UNCOMPRESSED,
3105*a90b9d01SCy Schubert pub_bin, len, NULL);
3106*a90b9d01SCy Schubert if (len == 0)
3107*a90b9d01SCy Schubert goto fail;
3108*a90b9d01SCy Schubert
3109*a90b9d01SCy Schubert params[0] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME,
3110*a90b9d01SCy Schubert (char *) group_name, 0);
3111*a90b9d01SCy Schubert params[1] = OSSL_PARAM_construct_BN(OSSL_PKEY_PARAM_PRIV_KEY,
3112*a90b9d01SCy Schubert priv_bin, priv_bin_len);
3113*a90b9d01SCy Schubert params[2] = OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_PUB_KEY,
3114*a90b9d01SCy Schubert pub_bin, len);
3115*a90b9d01SCy Schubert params[3] = OSSL_PARAM_construct_end();
3116*a90b9d01SCy Schubert
3117*a90b9d01SCy Schubert ctx = EVP_PKEY_CTX_new_from_name(NULL, "EC", NULL);
3118*a90b9d01SCy Schubert if (!ctx ||
3119*a90b9d01SCy Schubert EVP_PKEY_fromdata_init(ctx) <= 0 ||
3120*a90b9d01SCy Schubert EVP_PKEY_fromdata(ctx, &pkey, EVP_PKEY_KEYPAIR, params) <= 0)
3121*a90b9d01SCy Schubert goto fail;
3122*a90b9d01SCy Schubert
3123*a90b9d01SCy Schubert out:
3124*a90b9d01SCy Schubert bin_clear_free(priv_bin, raw_len);
3125*a90b9d01SCy Schubert os_free(pub_bin);
3126*a90b9d01SCy Schubert BN_clear_free(priv);
3127*a90b9d01SCy Schubert EVP_PKEY_CTX_free(ctx);
3128*a90b9d01SCy Schubert EC_POINT_free(pub);
3129*a90b9d01SCy Schubert EC_GROUP_free(ec_group);
3130*a90b9d01SCy Schubert return (struct crypto_ec_key *) pkey;
3131*a90b9d01SCy Schubert
3132*a90b9d01SCy Schubert fail:
3133*a90b9d01SCy Schubert EVP_PKEY_free(pkey);
3134*a90b9d01SCy Schubert pkey = NULL;
3135*a90b9d01SCy Schubert goto out;
3136*a90b9d01SCy Schubert #else /* OpenSSL version >= 3.0 */
3137*a90b9d01SCy Schubert EC_KEY *eckey = NULL;
3138*a90b9d01SCy Schubert EVP_PKEY *pkey = NULL;
3139*a90b9d01SCy Schubert BIGNUM *priv = NULL;
3140*a90b9d01SCy Schubert int nid;
3141*a90b9d01SCy Schubert const EC_GROUP *ec_group;
3142*a90b9d01SCy Schubert EC_POINT *pub = NULL;
3143*a90b9d01SCy Schubert
3144*a90b9d01SCy Schubert nid = crypto_ec_group_2_nid(group);
3145*a90b9d01SCy Schubert if (nid < 0) {
3146*a90b9d01SCy Schubert wpa_printf(MSG_ERROR, "OpenSSL: Unsupported group %d", group);
3147*a90b9d01SCy Schubert return NULL;
3148*a90b9d01SCy Schubert }
3149*a90b9d01SCy Schubert
3150*a90b9d01SCy Schubert eckey = EC_KEY_new_by_curve_name(nid);
3151*a90b9d01SCy Schubert priv = BN_bin2bn(raw, raw_len, NULL);
3152*a90b9d01SCy Schubert if (!eckey || !priv ||
3153*a90b9d01SCy Schubert EC_KEY_set_private_key(eckey, priv) != 1) {
3154*a90b9d01SCy Schubert wpa_printf(MSG_ERROR,
3155*a90b9d01SCy Schubert "OpenSSL: Failed to set EC_KEY: %s",
3156*a90b9d01SCy Schubert ERR_error_string(ERR_get_error(), NULL));
3157*a90b9d01SCy Schubert goto fail;
3158*a90b9d01SCy Schubert }
3159*a90b9d01SCy Schubert
3160*a90b9d01SCy Schubert ec_group = EC_KEY_get0_group(eckey);
3161*a90b9d01SCy Schubert if (!ec_group)
3162*a90b9d01SCy Schubert goto fail;
3163*a90b9d01SCy Schubert pub = EC_POINT_new(ec_group);
3164*a90b9d01SCy Schubert if (!pub ||
3165*a90b9d01SCy Schubert EC_POINT_mul(ec_group, pub, priv, NULL, NULL, NULL) != 1 ||
3166*a90b9d01SCy Schubert EC_KEY_set_public_key(eckey, pub) != 1) {
3167*a90b9d01SCy Schubert wpa_printf(MSG_ERROR,
3168*a90b9d01SCy Schubert "OpenSSL: Failed to set EC_KEY(pub): %s",
3169*a90b9d01SCy Schubert ERR_error_string(ERR_get_error(), NULL));
3170*a90b9d01SCy Schubert goto fail;
3171*a90b9d01SCy Schubert }
3172*a90b9d01SCy Schubert
3173*a90b9d01SCy Schubert EC_KEY_set_asn1_flag(eckey, OPENSSL_EC_NAMED_CURVE);
3174*a90b9d01SCy Schubert
3175*a90b9d01SCy Schubert pkey = EVP_PKEY_new();
3176*a90b9d01SCy Schubert if (!pkey || EVP_PKEY_assign_EC_KEY(pkey, eckey) != 1) {
3177*a90b9d01SCy Schubert wpa_printf(MSG_ERROR, "OpenSSL: Could not create EVP_PKEY");
3178*a90b9d01SCy Schubert goto fail;
3179*a90b9d01SCy Schubert }
3180*a90b9d01SCy Schubert
3181*a90b9d01SCy Schubert out:
3182*a90b9d01SCy Schubert BN_clear_free(priv);
3183*a90b9d01SCy Schubert EC_POINT_free(pub);
3184*a90b9d01SCy Schubert return (struct crypto_ec_key *) pkey;
3185*a90b9d01SCy Schubert
3186*a90b9d01SCy Schubert fail:
3187*a90b9d01SCy Schubert EC_KEY_free(eckey);
3188*a90b9d01SCy Schubert EVP_PKEY_free(pkey);
3189*a90b9d01SCy Schubert pkey = NULL;
3190*a90b9d01SCy Schubert goto out;
3191*a90b9d01SCy Schubert #endif /* OpenSSL version >= 3.0 */
3192c1d255d3SCy Schubert }
3193c1d255d3SCy Schubert
3194c1d255d3SCy Schubert
crypto_ec_key_parse_pub(const u8 * der,size_t der_len)3195c1d255d3SCy Schubert struct crypto_ec_key * crypto_ec_key_parse_pub(const u8 *der, size_t der_len)
3196c1d255d3SCy Schubert {
31974b72b91aSCy Schubert EVP_PKEY *pkey;
3198c1d255d3SCy Schubert
31994b72b91aSCy Schubert pkey = d2i_PUBKEY(NULL, &der, der_len);
32004b72b91aSCy Schubert if (!pkey) {
3201c1d255d3SCy Schubert wpa_printf(MSG_INFO, "OpenSSL: d2i_PUBKEY() failed: %s",
3202c1d255d3SCy Schubert ERR_error_string(ERR_get_error(), NULL));
3203c1d255d3SCy Schubert goto fail;
3204c1d255d3SCy Schubert }
3205c1d255d3SCy Schubert
32064b72b91aSCy Schubert /* Ensure this is an EC key */
3207*a90b9d01SCy Schubert #if OPENSSL_VERSION_NUMBER >= 0x30000000L
3208*a90b9d01SCy Schubert if (!EVP_PKEY_is_a(pkey, "EC"))
3209*a90b9d01SCy Schubert goto fail;
3210*a90b9d01SCy Schubert #else /* OpenSSL version >= 3.0 */
32114b72b91aSCy Schubert if (!EVP_PKEY_get0_EC_KEY(pkey))
3212c1d255d3SCy Schubert goto fail;
3213*a90b9d01SCy Schubert #endif /* OpenSSL version >= 3.0 */
32144b72b91aSCy Schubert return (struct crypto_ec_key *) pkey;
3215c1d255d3SCy Schubert fail:
32164b72b91aSCy Schubert crypto_ec_key_deinit((struct crypto_ec_key *) pkey);
3217c1d255d3SCy Schubert return NULL;
3218c1d255d3SCy Schubert }
3219c1d255d3SCy Schubert
3220c1d255d3SCy Schubert
crypto_ec_key_set_pub(int group,const u8 * buf_x,const u8 * buf_y,size_t len)32214b72b91aSCy Schubert struct crypto_ec_key * crypto_ec_key_set_pub(int group, const u8 *buf_x,
32224b72b91aSCy Schubert const u8 *buf_y, size_t len)
32234b72b91aSCy Schubert {
3224*a90b9d01SCy Schubert #if OPENSSL_VERSION_NUMBER >= 0x30000000L
3225*a90b9d01SCy Schubert const char *group_name;
3226*a90b9d01SCy Schubert OSSL_PARAM params[3];
3227*a90b9d01SCy Schubert u8 *pub;
3228*a90b9d01SCy Schubert EVP_PKEY_CTX *ctx;
3229*a90b9d01SCy Schubert EVP_PKEY *pkey = NULL;
3230*a90b9d01SCy Schubert
3231*a90b9d01SCy Schubert group_name = crypto_ec_group_2_name(group);
3232*a90b9d01SCy Schubert if (!group_name)
3233*a90b9d01SCy Schubert return NULL;
3234*a90b9d01SCy Schubert
3235*a90b9d01SCy Schubert pub = os_malloc(1 + len * 2);
3236*a90b9d01SCy Schubert if (!pub)
3237*a90b9d01SCy Schubert return NULL;
3238*a90b9d01SCy Schubert pub[0] = 0x04; /* uncompressed */
3239*a90b9d01SCy Schubert os_memcpy(pub + 1, buf_x, len);
3240*a90b9d01SCy Schubert os_memcpy(pub + 1 + len, buf_y, len);
3241*a90b9d01SCy Schubert
3242*a90b9d01SCy Schubert params[0] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME,
3243*a90b9d01SCy Schubert (char *) group_name, 0);
3244*a90b9d01SCy Schubert params[1] = OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_PUB_KEY,
3245*a90b9d01SCy Schubert pub, 1 + len * 2);
3246*a90b9d01SCy Schubert params[2] = OSSL_PARAM_construct_end();
3247*a90b9d01SCy Schubert
3248*a90b9d01SCy Schubert ctx = EVP_PKEY_CTX_new_from_name(NULL, "EC", NULL);
3249*a90b9d01SCy Schubert if (!ctx) {
3250*a90b9d01SCy Schubert os_free(pub);
3251*a90b9d01SCy Schubert return NULL;
3252*a90b9d01SCy Schubert }
3253*a90b9d01SCy Schubert if (EVP_PKEY_fromdata_init(ctx) <= 0 ||
3254*a90b9d01SCy Schubert EVP_PKEY_fromdata(ctx, &pkey, EVP_PKEY_PUBLIC_KEY, params) <= 0) {
3255*a90b9d01SCy Schubert os_free(pub);
3256*a90b9d01SCy Schubert EVP_PKEY_CTX_free(ctx);
3257*a90b9d01SCy Schubert return NULL;
3258*a90b9d01SCy Schubert }
3259*a90b9d01SCy Schubert
3260*a90b9d01SCy Schubert os_free(pub);
3261*a90b9d01SCy Schubert EVP_PKEY_CTX_free(ctx);
3262*a90b9d01SCy Schubert
3263*a90b9d01SCy Schubert return (struct crypto_ec_key *) pkey;
3264*a90b9d01SCy Schubert #else /* OpenSSL version >= 3.0 */
32654b72b91aSCy Schubert EC_KEY *eckey = NULL;
32664b72b91aSCy Schubert EVP_PKEY *pkey = NULL;
32674b72b91aSCy Schubert EC_GROUP *ec_group = NULL;
32684b72b91aSCy Schubert BN_CTX *ctx;
32694b72b91aSCy Schubert EC_POINT *point = NULL;
32704b72b91aSCy Schubert BIGNUM *x = NULL, *y = NULL;
32714b72b91aSCy Schubert int nid;
32724b72b91aSCy Schubert
32734b72b91aSCy Schubert if (!buf_x || !buf_y)
32744b72b91aSCy Schubert return NULL;
32754b72b91aSCy Schubert
32764b72b91aSCy Schubert nid = crypto_ec_group_2_nid(group);
32774b72b91aSCy Schubert if (nid < 0) {
32784b72b91aSCy Schubert wpa_printf(MSG_ERROR, "OpenSSL: Unsupported group %d", group);
32794b72b91aSCy Schubert return NULL;
32804b72b91aSCy Schubert }
32814b72b91aSCy Schubert
32824b72b91aSCy Schubert ctx = BN_CTX_new();
32834b72b91aSCy Schubert if (!ctx)
32844b72b91aSCy Schubert goto fail;
32854b72b91aSCy Schubert
32864b72b91aSCy Schubert ec_group = EC_GROUP_new_by_curve_name(nid);
32874b72b91aSCy Schubert if (!ec_group)
32884b72b91aSCy Schubert goto fail;
32894b72b91aSCy Schubert
32904b72b91aSCy Schubert x = BN_bin2bn(buf_x, len, NULL);
32914b72b91aSCy Schubert y = BN_bin2bn(buf_y, len, NULL);
32924b72b91aSCy Schubert point = EC_POINT_new(ec_group);
32934b72b91aSCy Schubert if (!x || !y || !point)
32944b72b91aSCy Schubert goto fail;
32954b72b91aSCy Schubert
3296*a90b9d01SCy Schubert if (!EC_POINT_set_affine_coordinates(ec_group, point, x, y, ctx)) {
32974b72b91aSCy Schubert wpa_printf(MSG_ERROR,
3298*a90b9d01SCy Schubert "OpenSSL: EC_POINT_set_affine_coordinates failed: %s",
32994b72b91aSCy Schubert ERR_error_string(ERR_get_error(), NULL));
33004b72b91aSCy Schubert goto fail;
33014b72b91aSCy Schubert }
33024b72b91aSCy Schubert
33034b72b91aSCy Schubert if (!EC_POINT_is_on_curve(ec_group, point, ctx) ||
33044b72b91aSCy Schubert EC_POINT_is_at_infinity(ec_group, point)) {
33054b72b91aSCy Schubert wpa_printf(MSG_ERROR, "OpenSSL: Invalid point");
33064b72b91aSCy Schubert goto fail;
33074b72b91aSCy Schubert }
33084b72b91aSCy Schubert
33094b72b91aSCy Schubert eckey = EC_KEY_new();
33104b72b91aSCy Schubert if (!eckey ||
33114b72b91aSCy Schubert EC_KEY_set_group(eckey, ec_group) != 1 ||
33124b72b91aSCy Schubert EC_KEY_set_public_key(eckey, point) != 1) {
33134b72b91aSCy Schubert wpa_printf(MSG_ERROR,
33144b72b91aSCy Schubert "OpenSSL: Failed to set EC_KEY: %s",
33154b72b91aSCy Schubert ERR_error_string(ERR_get_error(), NULL));
33164b72b91aSCy Schubert goto fail;
33174b72b91aSCy Schubert }
33184b72b91aSCy Schubert EC_KEY_set_asn1_flag(eckey, OPENSSL_EC_NAMED_CURVE);
33194b72b91aSCy Schubert
33204b72b91aSCy Schubert pkey = EVP_PKEY_new();
33214b72b91aSCy Schubert if (!pkey || EVP_PKEY_assign_EC_KEY(pkey, eckey) != 1) {
33224b72b91aSCy Schubert wpa_printf(MSG_ERROR, "OpenSSL: Could not create EVP_PKEY");
33234b72b91aSCy Schubert goto fail;
33244b72b91aSCy Schubert }
33254b72b91aSCy Schubert
33264b72b91aSCy Schubert out:
33274b72b91aSCy Schubert EC_GROUP_free(ec_group);
33284b72b91aSCy Schubert BN_free(x);
33294b72b91aSCy Schubert BN_free(y);
33304b72b91aSCy Schubert EC_POINT_free(point);
33314b72b91aSCy Schubert BN_CTX_free(ctx);
33324b72b91aSCy Schubert return (struct crypto_ec_key *) pkey;
33334b72b91aSCy Schubert
33344b72b91aSCy Schubert fail:
33354b72b91aSCy Schubert EC_KEY_free(eckey);
33364b72b91aSCy Schubert EVP_PKEY_free(pkey);
33374b72b91aSCy Schubert pkey = NULL;
33384b72b91aSCy Schubert goto out;
3339*a90b9d01SCy Schubert #endif /* OpenSSL version >= 3.0 */
33404b72b91aSCy Schubert }
33414b72b91aSCy Schubert
33424b72b91aSCy Schubert
33434b72b91aSCy Schubert struct crypto_ec_key *
crypto_ec_key_set_pub_point(struct crypto_ec * ec,const struct crypto_ec_point * pub)33444b72b91aSCy Schubert crypto_ec_key_set_pub_point(struct crypto_ec *ec,
33454b72b91aSCy Schubert const struct crypto_ec_point *pub)
33464b72b91aSCy Schubert {
3347*a90b9d01SCy Schubert #if OPENSSL_VERSION_NUMBER >= 0x30000000L
3348*a90b9d01SCy Schubert int len = BN_num_bytes(ec->prime);
3349*a90b9d01SCy Schubert struct crypto_ec_key *key;
3350*a90b9d01SCy Schubert u8 *buf;
3351*a90b9d01SCy Schubert
3352*a90b9d01SCy Schubert buf = os_malloc(2 * len);
3353*a90b9d01SCy Schubert if (!buf)
3354*a90b9d01SCy Schubert return NULL;
3355*a90b9d01SCy Schubert if (crypto_ec_point_to_bin(ec, pub, buf, buf + len) < 0) {
3356*a90b9d01SCy Schubert os_free(buf);
3357*a90b9d01SCy Schubert return NULL;
3358*a90b9d01SCy Schubert }
3359*a90b9d01SCy Schubert
3360*a90b9d01SCy Schubert key = crypto_ec_key_set_pub(ec->iana_group, buf, buf + len, len);
3361*a90b9d01SCy Schubert os_free(buf);
3362*a90b9d01SCy Schubert
3363*a90b9d01SCy Schubert return key;
3364*a90b9d01SCy Schubert #else /* OpenSSL version >= 3.0 */
33654b72b91aSCy Schubert EC_KEY *eckey;
33664b72b91aSCy Schubert EVP_PKEY *pkey = NULL;
33674b72b91aSCy Schubert
33684b72b91aSCy Schubert eckey = EC_KEY_new();
33694b72b91aSCy Schubert if (!eckey ||
33704b72b91aSCy Schubert EC_KEY_set_group(eckey, ec->group) != 1 ||
33714b72b91aSCy Schubert EC_KEY_set_public_key(eckey, (const EC_POINT *) pub) != 1) {
33724b72b91aSCy Schubert wpa_printf(MSG_ERROR,
33734b72b91aSCy Schubert "OpenSSL: Failed to set EC_KEY: %s",
33744b72b91aSCy Schubert ERR_error_string(ERR_get_error(), NULL));
33754b72b91aSCy Schubert goto fail;
33764b72b91aSCy Schubert }
33774b72b91aSCy Schubert EC_KEY_set_asn1_flag(eckey, OPENSSL_EC_NAMED_CURVE);
33784b72b91aSCy Schubert
33794b72b91aSCy Schubert pkey = EVP_PKEY_new();
33804b72b91aSCy Schubert if (!pkey || EVP_PKEY_assign_EC_KEY(pkey, eckey) != 1) {
33814b72b91aSCy Schubert wpa_printf(MSG_ERROR, "OpenSSL: Could not create EVP_PKEY");
33824b72b91aSCy Schubert goto fail;
33834b72b91aSCy Schubert }
33844b72b91aSCy Schubert
33854b72b91aSCy Schubert out:
33864b72b91aSCy Schubert return (struct crypto_ec_key *) pkey;
33874b72b91aSCy Schubert
33884b72b91aSCy Schubert fail:
33894b72b91aSCy Schubert EVP_PKEY_free(pkey);
33904b72b91aSCy Schubert EC_KEY_free(eckey);
33914b72b91aSCy Schubert pkey = NULL;
33924b72b91aSCy Schubert goto out;
3393*a90b9d01SCy Schubert #endif /* OpenSSL version >= 3.0 */
33944b72b91aSCy Schubert }
33954b72b91aSCy Schubert
33964b72b91aSCy Schubert
crypto_ec_key_gen(int group)33974b72b91aSCy Schubert struct crypto_ec_key * crypto_ec_key_gen(int group)
33984b72b91aSCy Schubert {
3399*a90b9d01SCy Schubert #if OPENSSL_VERSION_NUMBER >= 0x30000000L
3400*a90b9d01SCy Schubert EVP_PKEY_CTX *ctx;
3401*a90b9d01SCy Schubert OSSL_PARAM params[2];
3402*a90b9d01SCy Schubert const char *group_name;
3403*a90b9d01SCy Schubert EVP_PKEY *pkey = NULL;
3404*a90b9d01SCy Schubert
3405*a90b9d01SCy Schubert group_name = crypto_ec_group_2_name(group);
3406*a90b9d01SCy Schubert if (!group_name)
3407*a90b9d01SCy Schubert return NULL;
3408*a90b9d01SCy Schubert
3409*a90b9d01SCy Schubert params[0] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME,
3410*a90b9d01SCy Schubert (char *) group_name, 0);
3411*a90b9d01SCy Schubert params[1] = OSSL_PARAM_construct_end();
3412*a90b9d01SCy Schubert
3413*a90b9d01SCy Schubert ctx = EVP_PKEY_CTX_new_from_name(NULL, "EC", NULL);
3414*a90b9d01SCy Schubert if (!ctx ||
3415*a90b9d01SCy Schubert EVP_PKEY_keygen_init(ctx) != 1 ||
3416*a90b9d01SCy Schubert EVP_PKEY_CTX_set_params(ctx, params) != 1 ||
3417*a90b9d01SCy Schubert EVP_PKEY_generate(ctx, &pkey) != 1) {
3418*a90b9d01SCy Schubert wpa_printf(MSG_INFO,
3419*a90b9d01SCy Schubert "OpenSSL: failed to generate EC keypair: %s",
3420*a90b9d01SCy Schubert ERR_error_string(ERR_get_error(), NULL));
3421*a90b9d01SCy Schubert pkey = NULL;
3422*a90b9d01SCy Schubert }
3423*a90b9d01SCy Schubert
3424*a90b9d01SCy Schubert EVP_PKEY_CTX_free(ctx);
3425*a90b9d01SCy Schubert
3426*a90b9d01SCy Schubert return (struct crypto_ec_key *) pkey;
3427*a90b9d01SCy Schubert #else /* OpenSSL version >= 3.0 */
34284b72b91aSCy Schubert EVP_PKEY_CTX *kctx = NULL;
34294b72b91aSCy Schubert EC_KEY *ec_params = NULL, *eckey;
34304b72b91aSCy Schubert EVP_PKEY *params = NULL, *key = NULL;
34314b72b91aSCy Schubert int nid;
34324b72b91aSCy Schubert
34334b72b91aSCy Schubert nid = crypto_ec_group_2_nid(group);
34344b72b91aSCy Schubert if (nid < 0) {
34354b72b91aSCy Schubert wpa_printf(MSG_ERROR, "OpenSSL: Unsupported group %d", group);
34364b72b91aSCy Schubert return NULL;
34374b72b91aSCy Schubert }
34384b72b91aSCy Schubert
34394b72b91aSCy Schubert ec_params = EC_KEY_new_by_curve_name(nid);
34404b72b91aSCy Schubert if (!ec_params) {
34414b72b91aSCy Schubert wpa_printf(MSG_ERROR,
34424b72b91aSCy Schubert "OpenSSL: Failed to generate EC_KEY parameters");
34434b72b91aSCy Schubert goto fail;
34444b72b91aSCy Schubert }
34454b72b91aSCy Schubert EC_KEY_set_asn1_flag(ec_params, OPENSSL_EC_NAMED_CURVE);
34464b72b91aSCy Schubert params = EVP_PKEY_new();
34474b72b91aSCy Schubert if (!params || EVP_PKEY_set1_EC_KEY(params, ec_params) != 1) {
34484b72b91aSCy Schubert wpa_printf(MSG_ERROR,
34494b72b91aSCy Schubert "OpenSSL: Failed to generate EVP_PKEY parameters");
34504b72b91aSCy Schubert goto fail;
34514b72b91aSCy Schubert }
34524b72b91aSCy Schubert
34534b72b91aSCy Schubert kctx = EVP_PKEY_CTX_new(params, NULL);
34544b72b91aSCy Schubert if (!kctx ||
34554b72b91aSCy Schubert EVP_PKEY_keygen_init(kctx) != 1 ||
34564b72b91aSCy Schubert EVP_PKEY_keygen(kctx, &key) != 1) {
34574b72b91aSCy Schubert wpa_printf(MSG_ERROR, "OpenSSL: Failed to generate EC key");
34584b72b91aSCy Schubert key = NULL;
34594b72b91aSCy Schubert goto fail;
34604b72b91aSCy Schubert }
34614b72b91aSCy Schubert
3462ec080394SCy Schubert eckey = EVP_PKEY_get1_EC_KEY(key);
34634b72b91aSCy Schubert if (!eckey) {
34644b72b91aSCy Schubert key = NULL;
34654b72b91aSCy Schubert goto fail;
34664b72b91aSCy Schubert }
34674b72b91aSCy Schubert EC_KEY_set_conv_form(eckey, POINT_CONVERSION_COMPRESSED);
3468ec080394SCy Schubert EC_KEY_free(eckey);
34694b72b91aSCy Schubert
34704b72b91aSCy Schubert fail:
34714b72b91aSCy Schubert EC_KEY_free(ec_params);
34724b72b91aSCy Schubert EVP_PKEY_free(params);
34734b72b91aSCy Schubert EVP_PKEY_CTX_free(kctx);
34744b72b91aSCy Schubert return (struct crypto_ec_key *) key;
3475*a90b9d01SCy Schubert #endif /* OpenSSL version >= 3.0 */
34764b72b91aSCy Schubert }
34774b72b91aSCy Schubert
34784b72b91aSCy Schubert
crypto_ec_key_deinit(struct crypto_ec_key * key)3479c1d255d3SCy Schubert void crypto_ec_key_deinit(struct crypto_ec_key *key)
3480c1d255d3SCy Schubert {
34814b72b91aSCy Schubert EVP_PKEY_free((EVP_PKEY *) key);
3482c1d255d3SCy Schubert }
34834b72b91aSCy Schubert
34844b72b91aSCy Schubert
34854b72b91aSCy Schubert #ifdef OPENSSL_IS_BORINGSSL
34864b72b91aSCy Schubert
34874b72b91aSCy Schubert /* BoringSSL version of i2d_PUBKEY() always outputs public EC key using
34884b72b91aSCy Schubert * uncompressed form so define a custom function to export EC pubkey using
34894b72b91aSCy Schubert * the compressed format that is explicitly required for some protocols. */
34904b72b91aSCy Schubert
34914b72b91aSCy Schubert #include <openssl/asn1.h>
34924b72b91aSCy Schubert #include <openssl/asn1t.h>
34934b72b91aSCy Schubert
34944b72b91aSCy Schubert typedef struct {
34954b72b91aSCy Schubert /* AlgorithmIdentifier ecPublicKey with optional parameters present
34964b72b91aSCy Schubert * as an OID identifying the curve */
34974b72b91aSCy Schubert X509_ALGOR *alg;
34984b72b91aSCy Schubert /* Compressed format public key per ANSI X9.63 */
34994b72b91aSCy Schubert ASN1_BIT_STRING *pub_key;
35004b72b91aSCy Schubert } EC_COMP_PUBKEY;
35014b72b91aSCy Schubert
35024b72b91aSCy Schubert ASN1_SEQUENCE(EC_COMP_PUBKEY) = {
35034b72b91aSCy Schubert ASN1_SIMPLE(EC_COMP_PUBKEY, alg, X509_ALGOR),
35044b72b91aSCy Schubert ASN1_SIMPLE(EC_COMP_PUBKEY, pub_key, ASN1_BIT_STRING)
35054b72b91aSCy Schubert } ASN1_SEQUENCE_END(EC_COMP_PUBKEY);
35064b72b91aSCy Schubert
35074b72b91aSCy Schubert IMPLEMENT_ASN1_FUNCTIONS(EC_COMP_PUBKEY);
35084b72b91aSCy Schubert
35094b72b91aSCy Schubert #endif /* OPENSSL_IS_BORINGSSL */
3510c1d255d3SCy Schubert
3511c1d255d3SCy Schubert
crypto_ec_key_get_subject_public_key(struct crypto_ec_key * key)3512c1d255d3SCy Schubert struct wpabuf * crypto_ec_key_get_subject_public_key(struct crypto_ec_key *key)
3513c1d255d3SCy Schubert {
3514*a90b9d01SCy Schubert EVP_PKEY *pkey = (EVP_PKEY *) key;
3515*a90b9d01SCy Schubert #if OPENSSL_VERSION_NUMBER >= 0x30000000L
3516*a90b9d01SCy Schubert OSSL_ENCODER_CTX *ctx;
3517*a90b9d01SCy Schubert int selection;
3518*a90b9d01SCy Schubert unsigned char *pdata = NULL;
3519*a90b9d01SCy Schubert size_t pdata_len = 0;
3520*a90b9d01SCy Schubert EVP_PKEY *copy = NULL;
3521*a90b9d01SCy Schubert struct wpabuf *buf = NULL;
3522*a90b9d01SCy Schubert
3523*a90b9d01SCy Schubert if (EVP_PKEY_get_ec_point_conv_form(pkey) !=
3524*a90b9d01SCy Schubert POINT_CONVERSION_COMPRESSED) {
3525*a90b9d01SCy Schubert copy = EVP_PKEY_dup(pkey);
3526*a90b9d01SCy Schubert if (!copy)
3527*a90b9d01SCy Schubert return NULL;
3528*a90b9d01SCy Schubert if (EVP_PKEY_set_utf8_string_param(
3529*a90b9d01SCy Schubert copy, OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT,
3530*a90b9d01SCy Schubert OSSL_PKEY_EC_POINT_CONVERSION_FORMAT_COMPRESSED) !=
3531*a90b9d01SCy Schubert 1) {
3532*a90b9d01SCy Schubert wpa_printf(MSG_INFO,
3533*a90b9d01SCy Schubert "OpenSSL: Failed to set compressed format");
3534*a90b9d01SCy Schubert EVP_PKEY_free(copy);
3535*a90b9d01SCy Schubert return NULL;
3536*a90b9d01SCy Schubert }
3537*a90b9d01SCy Schubert pkey = copy;
3538*a90b9d01SCy Schubert }
3539*a90b9d01SCy Schubert
3540*a90b9d01SCy Schubert selection = OSSL_KEYMGMT_SELECT_ALL_PARAMETERS |
3541*a90b9d01SCy Schubert OSSL_KEYMGMT_SELECT_PUBLIC_KEY;
3542*a90b9d01SCy Schubert
3543*a90b9d01SCy Schubert ctx = OSSL_ENCODER_CTX_new_for_pkey(pkey, selection, "DER",
3544*a90b9d01SCy Schubert "SubjectPublicKeyInfo",
3545*a90b9d01SCy Schubert NULL);
3546*a90b9d01SCy Schubert if (!ctx || OSSL_ENCODER_to_data(ctx, &pdata, &pdata_len) != 1) {
3547*a90b9d01SCy Schubert wpa_printf(MSG_INFO,
3548*a90b9d01SCy Schubert "OpenSSL: Failed to encode SubjectPublicKeyInfo: %s",
3549*a90b9d01SCy Schubert ERR_error_string(ERR_get_error(), NULL));
3550*a90b9d01SCy Schubert pdata = NULL;
3551*a90b9d01SCy Schubert }
3552*a90b9d01SCy Schubert OSSL_ENCODER_CTX_free(ctx);
3553*a90b9d01SCy Schubert if (pdata) {
3554*a90b9d01SCy Schubert buf = wpabuf_alloc_copy(pdata, pdata_len);
3555*a90b9d01SCy Schubert OPENSSL_free(pdata);
3556*a90b9d01SCy Schubert }
3557*a90b9d01SCy Schubert
3558*a90b9d01SCy Schubert EVP_PKEY_free(copy);
3559*a90b9d01SCy Schubert
3560*a90b9d01SCy Schubert return buf;
3561*a90b9d01SCy Schubert #else /* OpenSSL version >= 3.0 */
35624b72b91aSCy Schubert #ifdef OPENSSL_IS_BORINGSSL
35634b72b91aSCy Schubert unsigned char *der = NULL;
35644b72b91aSCy Schubert int der_len;
35654b72b91aSCy Schubert const EC_KEY *eckey;
35664b72b91aSCy Schubert struct wpabuf *ret = NULL;
35674b72b91aSCy Schubert size_t len;
35684b72b91aSCy Schubert const EC_GROUP *group;
35694b72b91aSCy Schubert const EC_POINT *point;
35704b72b91aSCy Schubert BN_CTX *ctx;
35714b72b91aSCy Schubert EC_COMP_PUBKEY *pubkey = NULL;
35724b72b91aSCy Schubert int nid;
35734b72b91aSCy Schubert
35744b72b91aSCy Schubert ctx = BN_CTX_new();
3575*a90b9d01SCy Schubert eckey = EVP_PKEY_get0_EC_KEY(pkey);
35764b72b91aSCy Schubert if (!ctx || !eckey)
35774b72b91aSCy Schubert goto fail;
35784b72b91aSCy Schubert
35794b72b91aSCy Schubert group = EC_KEY_get0_group(eckey);
35804b72b91aSCy Schubert point = EC_KEY_get0_public_key(eckey);
35814b72b91aSCy Schubert if (!group || !point)
35824b72b91aSCy Schubert goto fail;
35834b72b91aSCy Schubert nid = EC_GROUP_get_curve_name(group);
35844b72b91aSCy Schubert
35854b72b91aSCy Schubert pubkey = EC_COMP_PUBKEY_new();
35864b72b91aSCy Schubert if (!pubkey ||
35874b72b91aSCy Schubert X509_ALGOR_set0(pubkey->alg, OBJ_nid2obj(EVP_PKEY_EC),
35884b72b91aSCy Schubert V_ASN1_OBJECT, (void *) OBJ_nid2obj(nid)) != 1)
35894b72b91aSCy Schubert goto fail;
35904b72b91aSCy Schubert
35914b72b91aSCy Schubert len = EC_POINT_point2oct(group, point, POINT_CONVERSION_COMPRESSED,
35924b72b91aSCy Schubert NULL, 0, ctx);
35934b72b91aSCy Schubert if (len == 0)
35944b72b91aSCy Schubert goto fail;
35954b72b91aSCy Schubert
35964b72b91aSCy Schubert der = OPENSSL_malloc(len);
35974b72b91aSCy Schubert if (!der)
35984b72b91aSCy Schubert goto fail;
35994b72b91aSCy Schubert len = EC_POINT_point2oct(group, point, POINT_CONVERSION_COMPRESSED,
36004b72b91aSCy Schubert der, len, ctx);
36014b72b91aSCy Schubert
36024b72b91aSCy Schubert OPENSSL_free(pubkey->pub_key->data);
36034b72b91aSCy Schubert pubkey->pub_key->data = der;
36044b72b91aSCy Schubert der = NULL;
36054b72b91aSCy Schubert pubkey->pub_key->length = len;
36064b72b91aSCy Schubert /* No unused bits */
36074b72b91aSCy Schubert pubkey->pub_key->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
36084b72b91aSCy Schubert pubkey->pub_key->flags |= ASN1_STRING_FLAG_BITS_LEFT;
36094b72b91aSCy Schubert
36104b72b91aSCy Schubert der_len = i2d_EC_COMP_PUBKEY(pubkey, &der);
36114b72b91aSCy Schubert if (der_len <= 0) {
36124b72b91aSCy Schubert wpa_printf(MSG_ERROR,
36134b72b91aSCy Schubert "BoringSSL: Failed to build DER encoded public key");
36144b72b91aSCy Schubert goto fail;
36154b72b91aSCy Schubert }
36164b72b91aSCy Schubert
36174b72b91aSCy Schubert ret = wpabuf_alloc_copy(der, der_len);
36184b72b91aSCy Schubert fail:
36194b72b91aSCy Schubert EC_COMP_PUBKEY_free(pubkey);
36204b72b91aSCy Schubert OPENSSL_free(der);
36214b72b91aSCy Schubert BN_CTX_free(ctx);
36224b72b91aSCy Schubert return ret;
36234b72b91aSCy Schubert #else /* OPENSSL_IS_BORINGSSL */
3624c1d255d3SCy Schubert unsigned char *der = NULL;
3625c1d255d3SCy Schubert int der_len;
3626c1d255d3SCy Schubert struct wpabuf *buf;
3627ec080394SCy Schubert EC_KEY *eckey;
3628ec080394SCy Schubert
3629*a90b9d01SCy Schubert eckey = EVP_PKEY_get1_EC_KEY(pkey);
3630ec080394SCy Schubert if (!eckey)
3631ec080394SCy Schubert return NULL;
3632c1d255d3SCy Schubert
36334b72b91aSCy Schubert /* For now, all users expect COMPRESSED form */
3634ec080394SCy Schubert EC_KEY_set_conv_form(eckey, POINT_CONVERSION_COMPRESSED);
3635ec080394SCy Schubert
36364b72b91aSCy Schubert der_len = i2d_PUBKEY((EVP_PKEY *) key, &der);
3637ec080394SCy Schubert EC_KEY_free(eckey);
3638c1d255d3SCy Schubert if (der_len <= 0) {
3639c1d255d3SCy Schubert wpa_printf(MSG_INFO, "OpenSSL: i2d_PUBKEY() failed: %s",
3640c1d255d3SCy Schubert ERR_error_string(ERR_get_error(), NULL));
3641c1d255d3SCy Schubert return NULL;
3642c1d255d3SCy Schubert }
3643c1d255d3SCy Schubert
3644c1d255d3SCy Schubert buf = wpabuf_alloc_copy(der, der_len);
3645c1d255d3SCy Schubert OPENSSL_free(der);
3646c1d255d3SCy Schubert return buf;
36474b72b91aSCy Schubert #endif /* OPENSSL_IS_BORINGSSL */
3648*a90b9d01SCy Schubert #endif /* OpenSSL version >= 3.0 */
36494b72b91aSCy Schubert }
36504b72b91aSCy Schubert
36514b72b91aSCy Schubert
crypto_ec_key_get_ecprivate_key(struct crypto_ec_key * key,bool include_pub)36524b72b91aSCy Schubert struct wpabuf * crypto_ec_key_get_ecprivate_key(struct crypto_ec_key *key,
36534b72b91aSCy Schubert bool include_pub)
36544b72b91aSCy Schubert {
3655*a90b9d01SCy Schubert EVP_PKEY *pkey = (EVP_PKEY *) key;
3656*a90b9d01SCy Schubert #if OPENSSL_VERSION_NUMBER >= 0x30000000L
3657*a90b9d01SCy Schubert OSSL_ENCODER_CTX *ctx;
3658*a90b9d01SCy Schubert int selection;
3659*a90b9d01SCy Schubert unsigned char *pdata = NULL;
3660*a90b9d01SCy Schubert size_t pdata_len = 0;
3661*a90b9d01SCy Schubert struct wpabuf *buf;
3662*a90b9d01SCy Schubert EVP_PKEY *copy = NULL;
3663*a90b9d01SCy Schubert
3664*a90b9d01SCy Schubert selection = OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS |
3665*a90b9d01SCy Schubert OSSL_KEYMGMT_SELECT_PRIVATE_KEY;
3666*a90b9d01SCy Schubert if (include_pub) {
3667*a90b9d01SCy Schubert selection |= OSSL_KEYMGMT_SELECT_PUBLIC_KEY;
3668*a90b9d01SCy Schubert } else {
3669*a90b9d01SCy Schubert /* Not including OSSL_KEYMGMT_SELECT_PUBLIC_KEY does not seem
3670*a90b9d01SCy Schubert * to really be sufficient, so clone the key and explicitly
3671*a90b9d01SCy Schubert * mark it not to include the public key. */
3672*a90b9d01SCy Schubert copy = EVP_PKEY_dup(pkey);
3673*a90b9d01SCy Schubert if (!copy)
3674*a90b9d01SCy Schubert return NULL;
3675*a90b9d01SCy Schubert EVP_PKEY_set_int_param(copy, OSSL_PKEY_PARAM_EC_INCLUDE_PUBLIC,
3676*a90b9d01SCy Schubert 0);
3677*a90b9d01SCy Schubert pkey = copy;
3678*a90b9d01SCy Schubert }
3679*a90b9d01SCy Schubert
3680*a90b9d01SCy Schubert ctx = OSSL_ENCODER_CTX_new_for_pkey(pkey, selection, "DER",
3681*a90b9d01SCy Schubert "type-specific", NULL);
3682*a90b9d01SCy Schubert if (!ctx || OSSL_ENCODER_to_data(ctx, &pdata, &pdata_len) != 1) {
3683*a90b9d01SCy Schubert OSSL_ENCODER_CTX_free(ctx);
3684*a90b9d01SCy Schubert EVP_PKEY_free(copy);
3685*a90b9d01SCy Schubert return NULL;
3686*a90b9d01SCy Schubert }
3687*a90b9d01SCy Schubert OSSL_ENCODER_CTX_free(ctx);
3688*a90b9d01SCy Schubert buf = wpabuf_alloc_copy(pdata, pdata_len);
3689*a90b9d01SCy Schubert OPENSSL_free(pdata);
3690*a90b9d01SCy Schubert EVP_PKEY_free(copy);
3691*a90b9d01SCy Schubert return buf;
3692*a90b9d01SCy Schubert #else /* OpenSSL version >= 3.0 */
36934b72b91aSCy Schubert EC_KEY *eckey;
36944b72b91aSCy Schubert unsigned char *der = NULL;
36954b72b91aSCy Schubert int der_len;
36964b72b91aSCy Schubert struct wpabuf *buf;
36974b72b91aSCy Schubert unsigned int key_flags;
36984b72b91aSCy Schubert
3699*a90b9d01SCy Schubert eckey = EVP_PKEY_get1_EC_KEY(pkey);
37004b72b91aSCy Schubert if (!eckey)
37014b72b91aSCy Schubert return NULL;
37024b72b91aSCy Schubert
37034b72b91aSCy Schubert key_flags = EC_KEY_get_enc_flags(eckey);
37044b72b91aSCy Schubert if (include_pub)
37054b72b91aSCy Schubert key_flags &= ~EC_PKEY_NO_PUBKEY;
37064b72b91aSCy Schubert else
37074b72b91aSCy Schubert key_flags |= EC_PKEY_NO_PUBKEY;
37084b72b91aSCy Schubert EC_KEY_set_enc_flags(eckey, key_flags);
37094b72b91aSCy Schubert
37104b72b91aSCy Schubert EC_KEY_set_conv_form(eckey, POINT_CONVERSION_UNCOMPRESSED);
37114b72b91aSCy Schubert
37124b72b91aSCy Schubert der_len = i2d_ECPrivateKey(eckey, &der);
3713ec080394SCy Schubert EC_KEY_free(eckey);
37144b72b91aSCy Schubert if (der_len <= 0)
37154b72b91aSCy Schubert return NULL;
37164b72b91aSCy Schubert buf = wpabuf_alloc_copy(der, der_len);
37174b72b91aSCy Schubert OPENSSL_free(der);
37184b72b91aSCy Schubert
37194b72b91aSCy Schubert return buf;
3720*a90b9d01SCy Schubert #endif /* OpenSSL version >= 3.0 */
37214b72b91aSCy Schubert }
37224b72b91aSCy Schubert
37234b72b91aSCy Schubert
crypto_ec_key_get_pubkey_point(struct crypto_ec_key * key,int prefix)37244b72b91aSCy Schubert struct wpabuf * crypto_ec_key_get_pubkey_point(struct crypto_ec_key *key,
37254b72b91aSCy Schubert int prefix)
37264b72b91aSCy Schubert {
3727*a90b9d01SCy Schubert EVP_PKEY *pkey = (EVP_PKEY *) key;
3728*a90b9d01SCy Schubert #if OPENSSL_VERSION_NUMBER >= 0x30000000L
3729*a90b9d01SCy Schubert struct wpabuf *buf;
3730*a90b9d01SCy Schubert unsigned char *pos;
3731*a90b9d01SCy Schubert size_t pub_len = OSSL_PARAM_UNMODIFIED;
3732*a90b9d01SCy Schubert
3733*a90b9d01SCy Schubert buf = NULL;
3734*a90b9d01SCy Schubert if (!EVP_PKEY_is_a(pkey, "EC") ||
3735*a90b9d01SCy Schubert EVP_PKEY_get_octet_string_param(pkey,
3736*a90b9d01SCy Schubert OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY,
3737*a90b9d01SCy Schubert NULL, 0, &pub_len) < 0 ||
3738*a90b9d01SCy Schubert pub_len == OSSL_PARAM_UNMODIFIED ||
3739*a90b9d01SCy Schubert !(buf = wpabuf_alloc(pub_len)) ||
3740*a90b9d01SCy Schubert EVP_PKEY_get_octet_string_param(pkey,
3741*a90b9d01SCy Schubert OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY,
3742*a90b9d01SCy Schubert wpabuf_put(buf, pub_len),
3743*a90b9d01SCy Schubert pub_len, NULL) != 1 ||
3744*a90b9d01SCy Schubert wpabuf_head_u8(buf)[0] != 0x04) {
3745*a90b9d01SCy Schubert wpa_printf(MSG_INFO,
3746*a90b9d01SCy Schubert "OpenSSL: Failed to get encoded public key: %s",
3747*a90b9d01SCy Schubert ERR_error_string(ERR_get_error(), NULL));
3748*a90b9d01SCy Schubert wpabuf_free(buf);
3749*a90b9d01SCy Schubert return NULL;
3750*a90b9d01SCy Schubert }
3751*a90b9d01SCy Schubert
3752*a90b9d01SCy Schubert if (!prefix) {
3753*a90b9d01SCy Schubert /* Remove 0x04 prefix if requested */
3754*a90b9d01SCy Schubert pos = wpabuf_mhead(buf);
3755*a90b9d01SCy Schubert os_memmove(pos, pos + 1, pub_len - 1);
3756*a90b9d01SCy Schubert buf->used--;
3757*a90b9d01SCy Schubert }
3758*a90b9d01SCy Schubert
3759*a90b9d01SCy Schubert return buf;
3760*a90b9d01SCy Schubert #else /* OpenSSL version >= 3.0 */
37614b72b91aSCy Schubert int len, res;
37624b72b91aSCy Schubert EC_KEY *eckey;
37634b72b91aSCy Schubert struct wpabuf *buf;
37644b72b91aSCy Schubert unsigned char *pos;
37654b72b91aSCy Schubert
3766*a90b9d01SCy Schubert eckey = EVP_PKEY_get1_EC_KEY(pkey);
37674b72b91aSCy Schubert if (!eckey)
37684b72b91aSCy Schubert return NULL;
37694b72b91aSCy Schubert EC_KEY_set_conv_form(eckey, POINT_CONVERSION_UNCOMPRESSED);
37704b72b91aSCy Schubert len = i2o_ECPublicKey(eckey, NULL);
37714b72b91aSCy Schubert if (len <= 0) {
37724b72b91aSCy Schubert wpa_printf(MSG_ERROR,
37734b72b91aSCy Schubert "OpenSSL: Failed to determine public key encoding length");
37744b72b91aSCy Schubert EC_KEY_free(eckey);
37754b72b91aSCy Schubert return NULL;
37764b72b91aSCy Schubert }
37774b72b91aSCy Schubert
37784b72b91aSCy Schubert buf = wpabuf_alloc(len);
37794b72b91aSCy Schubert if (!buf) {
37804b72b91aSCy Schubert EC_KEY_free(eckey);
37814b72b91aSCy Schubert return NULL;
37824b72b91aSCy Schubert }
37834b72b91aSCy Schubert
37844b72b91aSCy Schubert pos = wpabuf_put(buf, len);
37854b72b91aSCy Schubert res = i2o_ECPublicKey(eckey, &pos);
37864b72b91aSCy Schubert EC_KEY_free(eckey);
37874b72b91aSCy Schubert if (res != len) {
37884b72b91aSCy Schubert wpa_printf(MSG_ERROR,
37894b72b91aSCy Schubert "OpenSSL: Failed to encode public key (res=%d/%d)",
37904b72b91aSCy Schubert res, len);
37914b72b91aSCy Schubert wpabuf_free(buf);
37924b72b91aSCy Schubert return NULL;
37934b72b91aSCy Schubert }
37944b72b91aSCy Schubert
37954b72b91aSCy Schubert if (!prefix) {
37964b72b91aSCy Schubert /* Remove 0x04 prefix if requested */
37974b72b91aSCy Schubert pos = wpabuf_mhead(buf);
37984b72b91aSCy Schubert os_memmove(pos, pos + 1, len - 1);
37994b72b91aSCy Schubert buf->used--;
38004b72b91aSCy Schubert }
38014b72b91aSCy Schubert
38024b72b91aSCy Schubert return buf;
3803*a90b9d01SCy Schubert #endif /* OpenSSL version >= 3.0 */
38044b72b91aSCy Schubert }
38054b72b91aSCy Schubert
38064b72b91aSCy Schubert
3807*a90b9d01SCy Schubert struct crypto_ec_point *
crypto_ec_key_get_public_key(struct crypto_ec_key * key)38084b72b91aSCy Schubert crypto_ec_key_get_public_key(struct crypto_ec_key *key)
38094b72b91aSCy Schubert {
3810*a90b9d01SCy Schubert EVP_PKEY *pkey = (EVP_PKEY *) key;
3811*a90b9d01SCy Schubert #if OPENSSL_VERSION_NUMBER >= 0x30000000L
3812*a90b9d01SCy Schubert char group[64];
3813*a90b9d01SCy Schubert unsigned char pub[256];
3814*a90b9d01SCy Schubert size_t len;
3815*a90b9d01SCy Schubert EC_POINT *point = NULL;
3816*a90b9d01SCy Schubert EC_GROUP *grp;
3817*a90b9d01SCy Schubert int res = 0;
3818*a90b9d01SCy Schubert OSSL_PARAM params[2];
38194b72b91aSCy Schubert
3820*a90b9d01SCy Schubert if (!EVP_PKEY_is_a(pkey, "EC") ||
3821*a90b9d01SCy Schubert EVP_PKEY_get_utf8_string_param(pkey, OSSL_PKEY_PARAM_GROUP_NAME,
3822*a90b9d01SCy Schubert group, sizeof(group), &len) != 1 ||
3823*a90b9d01SCy Schubert EVP_PKEY_get_octet_string_param(pkey, OSSL_PKEY_PARAM_PUB_KEY,
3824*a90b9d01SCy Schubert pub, sizeof(pub), &len) != 1)
3825*a90b9d01SCy Schubert return NULL;
3826*a90b9d01SCy Schubert
3827*a90b9d01SCy Schubert params[0] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME,
3828*a90b9d01SCy Schubert group, 0);
3829*a90b9d01SCy Schubert params[1] = OSSL_PARAM_construct_end();
3830*a90b9d01SCy Schubert grp = EC_GROUP_new_from_params(params, NULL, NULL);
3831*a90b9d01SCy Schubert if (!grp)
3832*a90b9d01SCy Schubert goto fail;
3833*a90b9d01SCy Schubert point = EC_POINT_new(grp);
3834*a90b9d01SCy Schubert if (!point)
3835*a90b9d01SCy Schubert goto fail;
3836*a90b9d01SCy Schubert res = EC_POINT_oct2point(grp, point, pub, len, NULL);
3837*a90b9d01SCy Schubert
3838*a90b9d01SCy Schubert fail:
3839*a90b9d01SCy Schubert if (res != 1) {
3840*a90b9d01SCy Schubert EC_POINT_free(point);
3841*a90b9d01SCy Schubert point = NULL;
3842*a90b9d01SCy Schubert }
3843*a90b9d01SCy Schubert
3844*a90b9d01SCy Schubert EC_GROUP_free(grp);
3845*a90b9d01SCy Schubert
3846*a90b9d01SCy Schubert return (struct crypto_ec_point *) point;
3847*a90b9d01SCy Schubert #else /* OpenSSL version >= 3.0 */
3848*a90b9d01SCy Schubert const EC_KEY *eckey;
3849*a90b9d01SCy Schubert const EC_POINT *point;
3850*a90b9d01SCy Schubert const EC_GROUP *group;
3851*a90b9d01SCy Schubert
3852*a90b9d01SCy Schubert eckey = EVP_PKEY_get0_EC_KEY(pkey);
38534b72b91aSCy Schubert if (!eckey)
38544b72b91aSCy Schubert return NULL;
3855*a90b9d01SCy Schubert group = EC_KEY_get0_group(eckey);
3856*a90b9d01SCy Schubert if (!group)
3857*a90b9d01SCy Schubert return NULL;
3858*a90b9d01SCy Schubert point = EC_KEY_get0_public_key(eckey);
3859*a90b9d01SCy Schubert if (!point)
3860*a90b9d01SCy Schubert return NULL;
3861*a90b9d01SCy Schubert return (struct crypto_ec_point *) EC_POINT_dup(point, group);
3862*a90b9d01SCy Schubert #endif /* OpenSSL version >= 3.0 */
38634b72b91aSCy Schubert }
38644b72b91aSCy Schubert
38654b72b91aSCy Schubert
3866*a90b9d01SCy Schubert struct crypto_bignum *
crypto_ec_key_get_private_key(struct crypto_ec_key * key)38674b72b91aSCy Schubert crypto_ec_key_get_private_key(struct crypto_ec_key *key)
38684b72b91aSCy Schubert {
3869*a90b9d01SCy Schubert EVP_PKEY *pkey = (EVP_PKEY *) key;
3870*a90b9d01SCy Schubert #if OPENSSL_VERSION_NUMBER >= 0x30000000L
3871*a90b9d01SCy Schubert BIGNUM *bn = NULL;
38724b72b91aSCy Schubert
3873*a90b9d01SCy Schubert if (!EVP_PKEY_is_a(pkey, "EC") ||
3874*a90b9d01SCy Schubert EVP_PKEY_get_bn_param(pkey, OSSL_PKEY_PARAM_PRIV_KEY, &bn) != 1)
3875*a90b9d01SCy Schubert return NULL;
3876*a90b9d01SCy Schubert return (struct crypto_bignum *) bn;
3877*a90b9d01SCy Schubert #else /* OpenSSL version >= 3.0 */
3878*a90b9d01SCy Schubert const EC_KEY *eckey;
3879*a90b9d01SCy Schubert const BIGNUM *bn;
3880*a90b9d01SCy Schubert
3881*a90b9d01SCy Schubert eckey = EVP_PKEY_get0_EC_KEY(pkey);
38824b72b91aSCy Schubert if (!eckey)
38834b72b91aSCy Schubert return NULL;
3884*a90b9d01SCy Schubert bn = EC_KEY_get0_private_key(eckey);
3885*a90b9d01SCy Schubert if (!bn)
3886*a90b9d01SCy Schubert return NULL;
3887*a90b9d01SCy Schubert return (struct crypto_bignum *) BN_dup(bn);
3888*a90b9d01SCy Schubert #endif /* OpenSSL version >= 3.0 */
3889c1d255d3SCy Schubert }
3890c1d255d3SCy Schubert
3891c1d255d3SCy Schubert
crypto_ec_key_sign(struct crypto_ec_key * key,const u8 * data,size_t len)3892c1d255d3SCy Schubert struct wpabuf * crypto_ec_key_sign(struct crypto_ec_key *key, const u8 *data,
3893c1d255d3SCy Schubert size_t len)
3894c1d255d3SCy Schubert {
3895c1d255d3SCy Schubert EVP_PKEY_CTX *pkctx;
3896c1d255d3SCy Schubert struct wpabuf *sig_der;
3897c1d255d3SCy Schubert size_t sig_len;
3898c1d255d3SCy Schubert
38994b72b91aSCy Schubert sig_len = EVP_PKEY_size((EVP_PKEY *) key);
3900c1d255d3SCy Schubert sig_der = wpabuf_alloc(sig_len);
3901c1d255d3SCy Schubert if (!sig_der)
3902c1d255d3SCy Schubert return NULL;
3903c1d255d3SCy Schubert
39044b72b91aSCy Schubert pkctx = EVP_PKEY_CTX_new((EVP_PKEY *) key, NULL);
3905c1d255d3SCy Schubert if (!pkctx ||
3906c1d255d3SCy Schubert EVP_PKEY_sign_init(pkctx) <= 0 ||
3907c1d255d3SCy Schubert EVP_PKEY_sign(pkctx, wpabuf_put(sig_der, 0), &sig_len,
3908c1d255d3SCy Schubert data, len) <= 0) {
3909c1d255d3SCy Schubert wpabuf_free(sig_der);
3910c1d255d3SCy Schubert sig_der = NULL;
3911c1d255d3SCy Schubert } else {
3912c1d255d3SCy Schubert wpabuf_put(sig_der, sig_len);
3913c1d255d3SCy Schubert }
3914c1d255d3SCy Schubert
3915c1d255d3SCy Schubert EVP_PKEY_CTX_free(pkctx);
3916c1d255d3SCy Schubert return sig_der;
3917c1d255d3SCy Schubert }
3918c1d255d3SCy Schubert
3919c1d255d3SCy Schubert
openssl_evp_pkey_ec_prime_len(struct crypto_ec_key * key)3920*a90b9d01SCy Schubert static int openssl_evp_pkey_ec_prime_len(struct crypto_ec_key *key)
39214b72b91aSCy Schubert {
3922*a90b9d01SCy Schubert #if OPENSSL_VERSION_NUMBER >= 0x30000000L
3923*a90b9d01SCy Schubert char gname[50];
3924*a90b9d01SCy Schubert int nid;
3925*a90b9d01SCy Schubert EC_GROUP *group;
3926*a90b9d01SCy Schubert BIGNUM *prime = NULL;
3927*a90b9d01SCy Schubert int prime_len = -1;
3928*a90b9d01SCy Schubert
3929*a90b9d01SCy Schubert if (EVP_PKEY_get_group_name((EVP_PKEY *) key, gname, sizeof(gname),
3930*a90b9d01SCy Schubert NULL) != 1)
3931*a90b9d01SCy Schubert return -1;
3932*a90b9d01SCy Schubert nid = OBJ_txt2nid(gname);
3933*a90b9d01SCy Schubert group = EC_GROUP_new_by_curve_name(nid);
3934*a90b9d01SCy Schubert prime = BN_new();
3935*a90b9d01SCy Schubert if (!group || !prime)
3936*a90b9d01SCy Schubert goto fail;
3937*a90b9d01SCy Schubert if (EC_GROUP_get_curve(group, prime, NULL, NULL, NULL) == 1)
3938*a90b9d01SCy Schubert prime_len = BN_num_bytes(prime);
3939*a90b9d01SCy Schubert fail:
3940*a90b9d01SCy Schubert EC_GROUP_free(group);
3941*a90b9d01SCy Schubert BN_free(prime);
3942*a90b9d01SCy Schubert return prime_len;
3943*a90b9d01SCy Schubert #else
39444b72b91aSCy Schubert const EC_GROUP *group;
39454b72b91aSCy Schubert const EC_KEY *eckey;
39464b72b91aSCy Schubert BIGNUM *prime = NULL;
3947*a90b9d01SCy Schubert int prime_len = -1;
3948*a90b9d01SCy Schubert
3949*a90b9d01SCy Schubert eckey = EVP_PKEY_get0_EC_KEY((EVP_PKEY *) key);
3950*a90b9d01SCy Schubert if (!eckey)
3951*a90b9d01SCy Schubert goto fail;
3952*a90b9d01SCy Schubert group = EC_KEY_get0_group(eckey);
3953*a90b9d01SCy Schubert prime = BN_new();
3954*a90b9d01SCy Schubert if (!prime || !group ||
3955*a90b9d01SCy Schubert !EC_GROUP_get_curve(group, prime, NULL, NULL, NULL))
3956*a90b9d01SCy Schubert goto fail;
3957*a90b9d01SCy Schubert prime_len = BN_num_bytes(prime);
3958*a90b9d01SCy Schubert fail:
3959*a90b9d01SCy Schubert BN_free(prime);
3960*a90b9d01SCy Schubert return prime_len;
3961*a90b9d01SCy Schubert #endif
3962*a90b9d01SCy Schubert }
3963*a90b9d01SCy Schubert
3964*a90b9d01SCy Schubert
crypto_ec_key_sign_r_s(struct crypto_ec_key * key,const u8 * data,size_t len)3965*a90b9d01SCy Schubert struct wpabuf * crypto_ec_key_sign_r_s(struct crypto_ec_key *key,
3966*a90b9d01SCy Schubert const u8 *data, size_t len)
3967*a90b9d01SCy Schubert {
39684b72b91aSCy Schubert ECDSA_SIG *sig = NULL;
39694b72b91aSCy Schubert const BIGNUM *r, *s;
39704b72b91aSCy Schubert u8 *r_buf, *s_buf;
39714b72b91aSCy Schubert struct wpabuf *buf;
39724b72b91aSCy Schubert const unsigned char *p;
39734b72b91aSCy Schubert int prime_len;
39744b72b91aSCy Schubert
3975*a90b9d01SCy Schubert prime_len = openssl_evp_pkey_ec_prime_len(key);
3976*a90b9d01SCy Schubert if (prime_len < 0)
3977*a90b9d01SCy Schubert return NULL;
3978*a90b9d01SCy Schubert
39794b72b91aSCy Schubert buf = crypto_ec_key_sign(key, data, len);
39804b72b91aSCy Schubert if (!buf)
39814b72b91aSCy Schubert return NULL;
39824b72b91aSCy Schubert
39834b72b91aSCy Schubert /* Extract (r,s) from Ecdsa-Sig-Value */
39844b72b91aSCy Schubert
39854b72b91aSCy Schubert p = wpabuf_head(buf);
39864b72b91aSCy Schubert sig = d2i_ECDSA_SIG(NULL, &p, wpabuf_len(buf));
39874b72b91aSCy Schubert if (!sig)
39884b72b91aSCy Schubert goto fail;
39894b72b91aSCy Schubert ECDSA_SIG_get0(sig, &r, &s);
39904b72b91aSCy Schubert
39914b72b91aSCy Schubert /* Re-use wpabuf returned by crypto_ec_key_sign() */
39924b72b91aSCy Schubert buf->used = 0;
39934b72b91aSCy Schubert r_buf = wpabuf_put(buf, prime_len);
39944b72b91aSCy Schubert s_buf = wpabuf_put(buf, prime_len);
39954b72b91aSCy Schubert if (crypto_bignum_to_bin((const struct crypto_bignum *) r, r_buf,
39964b72b91aSCy Schubert prime_len, prime_len) < 0 ||
39974b72b91aSCy Schubert crypto_bignum_to_bin((const struct crypto_bignum *) s, s_buf,
39984b72b91aSCy Schubert prime_len, prime_len) < 0)
39994b72b91aSCy Schubert goto fail;
40004b72b91aSCy Schubert
40014b72b91aSCy Schubert out:
40024b72b91aSCy Schubert ECDSA_SIG_free(sig);
40034b72b91aSCy Schubert return buf;
40044b72b91aSCy Schubert fail:
40054b72b91aSCy Schubert wpabuf_clear_free(buf);
40064b72b91aSCy Schubert buf = NULL;
40074b72b91aSCy Schubert goto out;
40084b72b91aSCy Schubert }
40094b72b91aSCy Schubert
40104b72b91aSCy Schubert
crypto_ec_key_verify_signature(struct crypto_ec_key * key,const u8 * data,size_t len,const u8 * sig,size_t sig_len)4011c1d255d3SCy Schubert int crypto_ec_key_verify_signature(struct crypto_ec_key *key, const u8 *data,
4012c1d255d3SCy Schubert size_t len, const u8 *sig, size_t sig_len)
4013c1d255d3SCy Schubert {
4014c1d255d3SCy Schubert EVP_PKEY_CTX *pkctx;
4015c1d255d3SCy Schubert int ret;
4016c1d255d3SCy Schubert
40174b72b91aSCy Schubert pkctx = EVP_PKEY_CTX_new((EVP_PKEY *) key, NULL);
4018c1d255d3SCy Schubert if (!pkctx || EVP_PKEY_verify_init(pkctx) <= 0) {
4019c1d255d3SCy Schubert EVP_PKEY_CTX_free(pkctx);
4020c1d255d3SCy Schubert return -1;
4021c1d255d3SCy Schubert }
4022c1d255d3SCy Schubert
4023c1d255d3SCy Schubert ret = EVP_PKEY_verify(pkctx, sig, sig_len, data, len);
4024c1d255d3SCy Schubert EVP_PKEY_CTX_free(pkctx);
4025c1d255d3SCy Schubert if (ret == 1)
4026c1d255d3SCy Schubert return 1; /* signature ok */
4027c1d255d3SCy Schubert if (ret == 0)
4028c1d255d3SCy Schubert return 0; /* incorrect signature */
4029c1d255d3SCy Schubert return -1;
4030c1d255d3SCy Schubert }
4031c1d255d3SCy Schubert
4032c1d255d3SCy Schubert
crypto_ec_key_verify_signature_r_s(struct crypto_ec_key * key,const u8 * data,size_t len,const u8 * r,size_t r_len,const u8 * s,size_t s_len)40334b72b91aSCy Schubert int crypto_ec_key_verify_signature_r_s(struct crypto_ec_key *key,
40344b72b91aSCy Schubert const u8 *data, size_t len,
40354b72b91aSCy Schubert const u8 *r, size_t r_len,
40364b72b91aSCy Schubert const u8 *s, size_t s_len)
40374b72b91aSCy Schubert {
40384b72b91aSCy Schubert ECDSA_SIG *sig;
40394b72b91aSCy Schubert BIGNUM *r_bn, *s_bn;
40404b72b91aSCy Schubert unsigned char *der = NULL;
40414b72b91aSCy Schubert int der_len;
40424b72b91aSCy Schubert int ret = -1;
40434b72b91aSCy Schubert
40444b72b91aSCy Schubert r_bn = BN_bin2bn(r, r_len, NULL);
40454b72b91aSCy Schubert s_bn = BN_bin2bn(s, s_len, NULL);
40464b72b91aSCy Schubert sig = ECDSA_SIG_new();
40474b72b91aSCy Schubert if (!r_bn || !s_bn || !sig || ECDSA_SIG_set0(sig, r_bn, s_bn) != 1)
40484b72b91aSCy Schubert goto fail;
40494b72b91aSCy Schubert r_bn = NULL;
40504b72b91aSCy Schubert s_bn = NULL;
40514b72b91aSCy Schubert
40524b72b91aSCy Schubert der_len = i2d_ECDSA_SIG(sig, &der);
40534b72b91aSCy Schubert if (der_len <= 0) {
40544b72b91aSCy Schubert wpa_printf(MSG_DEBUG,
40554b72b91aSCy Schubert "OpenSSL: Could not DER encode signature");
40564b72b91aSCy Schubert goto fail;
40574b72b91aSCy Schubert }
40584b72b91aSCy Schubert
40594b72b91aSCy Schubert ret = crypto_ec_key_verify_signature(key, data, len, der, der_len);
40604b72b91aSCy Schubert
40614b72b91aSCy Schubert fail:
40624b72b91aSCy Schubert OPENSSL_free(der);
40634b72b91aSCy Schubert BN_free(r_bn);
40644b72b91aSCy Schubert BN_free(s_bn);
40654b72b91aSCy Schubert ECDSA_SIG_free(sig);
40664b72b91aSCy Schubert return ret;
40674b72b91aSCy Schubert }
40684b72b91aSCy Schubert
40694b72b91aSCy Schubert
crypto_ec_key_group(struct crypto_ec_key * key)4070c1d255d3SCy Schubert int crypto_ec_key_group(struct crypto_ec_key *key)
4071c1d255d3SCy Schubert {
4072*a90b9d01SCy Schubert #if OPENSSL_VERSION_NUMBER >= 0x30000000L
4073*a90b9d01SCy Schubert char gname[50];
4074*a90b9d01SCy Schubert int nid;
4075*a90b9d01SCy Schubert
4076*a90b9d01SCy Schubert if (EVP_PKEY_get_group_name((EVP_PKEY *) key, gname, sizeof(gname),
4077*a90b9d01SCy Schubert NULL) != 1)
4078*a90b9d01SCy Schubert return -1;
4079*a90b9d01SCy Schubert nid = OBJ_txt2nid(gname);
4080*a90b9d01SCy Schubert #else
40814b72b91aSCy Schubert const EC_KEY *eckey;
4082c1d255d3SCy Schubert const EC_GROUP *group;
4083c1d255d3SCy Schubert int nid;
4084c1d255d3SCy Schubert
40854b72b91aSCy Schubert eckey = EVP_PKEY_get0_EC_KEY((EVP_PKEY *) key);
40864b72b91aSCy Schubert if (!eckey)
40874b72b91aSCy Schubert return -1;
40884b72b91aSCy Schubert group = EC_KEY_get0_group(eckey);
4089c1d255d3SCy Schubert if (!group)
4090c1d255d3SCy Schubert return -1;
4091c1d255d3SCy Schubert nid = EC_GROUP_get_curve_name(group);
4092*a90b9d01SCy Schubert #endif
4093c1d255d3SCy Schubert switch (nid) {
4094c1d255d3SCy Schubert case NID_X9_62_prime256v1:
4095c1d255d3SCy Schubert return 19;
4096c1d255d3SCy Schubert case NID_secp384r1:
4097c1d255d3SCy Schubert return 20;
4098c1d255d3SCy Schubert case NID_secp521r1:
4099c1d255d3SCy Schubert return 21;
41004b72b91aSCy Schubert #ifdef NID_brainpoolP256r1
41014b72b91aSCy Schubert case NID_brainpoolP256r1:
41024b72b91aSCy Schubert return 28;
41034b72b91aSCy Schubert #endif /* NID_brainpoolP256r1 */
41044b72b91aSCy Schubert #ifdef NID_brainpoolP384r1
41054b72b91aSCy Schubert case NID_brainpoolP384r1:
41064b72b91aSCy Schubert return 29;
41074b72b91aSCy Schubert #endif /* NID_brainpoolP384r1 */
41084b72b91aSCy Schubert #ifdef NID_brainpoolP512r1
41094b72b91aSCy Schubert case NID_brainpoolP512r1:
41104b72b91aSCy Schubert return 30;
41114b72b91aSCy Schubert #endif /* NID_brainpoolP512r1 */
4112*a90b9d01SCy Schubert default:
4113*a90b9d01SCy Schubert wpa_printf(MSG_ERROR,
4114*a90b9d01SCy Schubert "OpenSSL: Unsupported curve (nid=%d) in EC key",
41154b72b91aSCy Schubert nid);
4116c1d255d3SCy Schubert return -1;
4117c1d255d3SCy Schubert }
4118*a90b9d01SCy Schubert }
4119c1d255d3SCy Schubert
41204b72b91aSCy Schubert
crypto_ec_key_cmp(struct crypto_ec_key * key1,struct crypto_ec_key * key2)41214b72b91aSCy Schubert int crypto_ec_key_cmp(struct crypto_ec_key *key1, struct crypto_ec_key *key2)
41224b72b91aSCy Schubert {
4123*a90b9d01SCy Schubert #if OPENSSL_VERSION_NUMBER >= 0x30000000L
4124*a90b9d01SCy Schubert if (EVP_PKEY_eq((EVP_PKEY *) key1, (EVP_PKEY *) key2) != 1)
4125*a90b9d01SCy Schubert return -1;
4126*a90b9d01SCy Schubert #else
41274b72b91aSCy Schubert if (EVP_PKEY_cmp((EVP_PKEY *) key1, (EVP_PKEY *) key2) != 1)
41284b72b91aSCy Schubert return -1;
4129*a90b9d01SCy Schubert #endif
41304b72b91aSCy Schubert return 0;
41314b72b91aSCy Schubert }
41324b72b91aSCy Schubert
41334b72b91aSCy Schubert
crypto_ec_key_debug_print(const struct crypto_ec_key * key,const char * title)41344b72b91aSCy Schubert void crypto_ec_key_debug_print(const struct crypto_ec_key *key,
41354b72b91aSCy Schubert const char *title)
41364b72b91aSCy Schubert {
41374b72b91aSCy Schubert BIO *out;
41384b72b91aSCy Schubert size_t rlen;
41394b72b91aSCy Schubert char *txt;
41404b72b91aSCy Schubert int res;
41414b72b91aSCy Schubert
41424b72b91aSCy Schubert out = BIO_new(BIO_s_mem());
41434b72b91aSCy Schubert if (!out)
41444b72b91aSCy Schubert return;
41454b72b91aSCy Schubert
41464b72b91aSCy Schubert EVP_PKEY_print_private(out, (EVP_PKEY *) key, 0, NULL);
41474b72b91aSCy Schubert rlen = BIO_ctrl_pending(out);
41484b72b91aSCy Schubert txt = os_malloc(rlen + 1);
41494b72b91aSCy Schubert if (txt) {
41504b72b91aSCy Schubert res = BIO_read(out, txt, rlen);
41514b72b91aSCy Schubert if (res > 0) {
41524b72b91aSCy Schubert txt[res] = '\0';
41534b72b91aSCy Schubert wpa_printf(MSG_DEBUG, "%s: %s", title, txt);
41544b72b91aSCy Schubert }
41554b72b91aSCy Schubert os_free(txt);
41564b72b91aSCy Schubert }
41574b72b91aSCy Schubert BIO_free(out);
41584b72b91aSCy Schubert }
41594b72b91aSCy Schubert
41604b72b91aSCy Schubert
crypto_pkcs7_get_certificates(const struct wpabuf * pkcs7)41614b72b91aSCy Schubert struct wpabuf * crypto_pkcs7_get_certificates(const struct wpabuf *pkcs7)
41624b72b91aSCy Schubert {
41634b72b91aSCy Schubert #ifdef OPENSSL_IS_BORINGSSL
41644b72b91aSCy Schubert CBS pkcs7_cbs;
41654b72b91aSCy Schubert #else /* OPENSSL_IS_BORINGSSL */
41664b72b91aSCy Schubert PKCS7 *p7 = NULL;
41674b72b91aSCy Schubert const unsigned char *p = wpabuf_head(pkcs7);
41684b72b91aSCy Schubert #endif /* OPENSSL_IS_BORINGSSL */
41694b72b91aSCy Schubert STACK_OF(X509) *certs;
41704b72b91aSCy Schubert int i, num;
41714b72b91aSCy Schubert BIO *out = NULL;
41724b72b91aSCy Schubert size_t rlen;
41734b72b91aSCy Schubert struct wpabuf *pem = NULL;
41744b72b91aSCy Schubert int res;
41754b72b91aSCy Schubert
41764b72b91aSCy Schubert #ifdef OPENSSL_IS_BORINGSSL
41774b72b91aSCy Schubert certs = sk_X509_new_null();
41784b72b91aSCy Schubert if (!certs)
41794b72b91aSCy Schubert goto fail;
41804b72b91aSCy Schubert CBS_init(&pkcs7_cbs, wpabuf_head(pkcs7), wpabuf_len(pkcs7));
41814b72b91aSCy Schubert if (!PKCS7_get_certificates(certs, &pkcs7_cbs)) {
41824b72b91aSCy Schubert wpa_printf(MSG_INFO,
41834b72b91aSCy Schubert "OpenSSL: Could not parse PKCS#7 object: %s",
41844b72b91aSCy Schubert ERR_error_string(ERR_get_error(), NULL));
41854b72b91aSCy Schubert goto fail;
41864b72b91aSCy Schubert }
41874b72b91aSCy Schubert #else /* OPENSSL_IS_BORINGSSL */
41884b72b91aSCy Schubert p7 = d2i_PKCS7(NULL, &p, wpabuf_len(pkcs7));
41894b72b91aSCy Schubert if (!p7) {
41904b72b91aSCy Schubert wpa_printf(MSG_INFO,
41914b72b91aSCy Schubert "OpenSSL: Could not parse PKCS#7 object: %s",
41924b72b91aSCy Schubert ERR_error_string(ERR_get_error(), NULL));
41934b72b91aSCy Schubert goto fail;
41944b72b91aSCy Schubert }
41954b72b91aSCy Schubert
41964b72b91aSCy Schubert switch (OBJ_obj2nid(p7->type)) {
41974b72b91aSCy Schubert case NID_pkcs7_signed:
41984b72b91aSCy Schubert certs = p7->d.sign->cert;
41994b72b91aSCy Schubert break;
42004b72b91aSCy Schubert case NID_pkcs7_signedAndEnveloped:
42014b72b91aSCy Schubert certs = p7->d.signed_and_enveloped->cert;
42024b72b91aSCy Schubert break;
42034b72b91aSCy Schubert default:
42044b72b91aSCy Schubert certs = NULL;
42054b72b91aSCy Schubert break;
42064b72b91aSCy Schubert }
42074b72b91aSCy Schubert #endif /* OPENSSL_IS_BORINGSSL */
42084b72b91aSCy Schubert
42094b72b91aSCy Schubert if (!certs || ((num = sk_X509_num(certs)) == 0)) {
42104b72b91aSCy Schubert wpa_printf(MSG_INFO,
42114b72b91aSCy Schubert "OpenSSL: No certificates found in PKCS#7 object");
42124b72b91aSCy Schubert goto fail;
42134b72b91aSCy Schubert }
42144b72b91aSCy Schubert
42154b72b91aSCy Schubert out = BIO_new(BIO_s_mem());
42164b72b91aSCy Schubert if (!out)
42174b72b91aSCy Schubert goto fail;
42184b72b91aSCy Schubert
42194b72b91aSCy Schubert for (i = 0; i < num; i++) {
42204b72b91aSCy Schubert X509 *cert = sk_X509_value(certs, i);
42214b72b91aSCy Schubert
42224b72b91aSCy Schubert PEM_write_bio_X509(out, cert);
42234b72b91aSCy Schubert }
42244b72b91aSCy Schubert
42254b72b91aSCy Schubert rlen = BIO_ctrl_pending(out);
42264b72b91aSCy Schubert pem = wpabuf_alloc(rlen);
42274b72b91aSCy Schubert if (!pem)
42284b72b91aSCy Schubert goto fail;
42294b72b91aSCy Schubert res = BIO_read(out, wpabuf_put(pem, 0), rlen);
42304b72b91aSCy Schubert if (res <= 0) {
42314b72b91aSCy Schubert wpabuf_free(pem);
42324b72b91aSCy Schubert pem = NULL;
42334b72b91aSCy Schubert goto fail;
42344b72b91aSCy Schubert }
42354b72b91aSCy Schubert wpabuf_put(pem, res);
42364b72b91aSCy Schubert
42374b72b91aSCy Schubert fail:
42384b72b91aSCy Schubert #ifdef OPENSSL_IS_BORINGSSL
42394b72b91aSCy Schubert if (certs)
42404b72b91aSCy Schubert sk_X509_pop_free(certs, X509_free);
42414b72b91aSCy Schubert #else /* OPENSSL_IS_BORINGSSL */
42424b72b91aSCy Schubert PKCS7_free(p7);
42434b72b91aSCy Schubert #endif /* OPENSSL_IS_BORINGSSL */
42444b72b91aSCy Schubert if (out)
42454b72b91aSCy Schubert BIO_free_all(out);
42464b72b91aSCy Schubert
42474b72b91aSCy Schubert return pem;
42484b72b91aSCy Schubert }
42494b72b91aSCy Schubert
42504b72b91aSCy Schubert
crypto_csr_init()42514b72b91aSCy Schubert struct crypto_csr * crypto_csr_init()
42524b72b91aSCy Schubert {
42534b72b91aSCy Schubert return (struct crypto_csr *)X509_REQ_new();
42544b72b91aSCy Schubert }
42554b72b91aSCy Schubert
42564b72b91aSCy Schubert
crypto_csr_verify(const struct wpabuf * req)42574b72b91aSCy Schubert struct crypto_csr * crypto_csr_verify(const struct wpabuf *req)
42584b72b91aSCy Schubert {
42594b72b91aSCy Schubert X509_REQ *csr;
42604b72b91aSCy Schubert EVP_PKEY *pkey = NULL;
42614b72b91aSCy Schubert const u8 *der = wpabuf_head(req);
42624b72b91aSCy Schubert
42634b72b91aSCy Schubert csr = d2i_X509_REQ(NULL, &der, wpabuf_len(req));
42644b72b91aSCy Schubert if (!csr)
42654b72b91aSCy Schubert return NULL;
42664b72b91aSCy Schubert
42674b72b91aSCy Schubert pkey = X509_REQ_get_pubkey((X509_REQ *)csr);
42684b72b91aSCy Schubert if (!pkey)
42694b72b91aSCy Schubert goto fail;
42704b72b91aSCy Schubert
42714b72b91aSCy Schubert if (X509_REQ_verify((X509_REQ *)csr, pkey) != 1)
42724b72b91aSCy Schubert goto fail;
42734b72b91aSCy Schubert
42744b72b91aSCy Schubert return (struct crypto_csr *)csr;
42754b72b91aSCy Schubert fail:
42764b72b91aSCy Schubert X509_REQ_free(csr);
42774b72b91aSCy Schubert return NULL;
42784b72b91aSCy Schubert }
42794b72b91aSCy Schubert
42804b72b91aSCy Schubert
crypto_csr_deinit(struct crypto_csr * csr)42814b72b91aSCy Schubert void crypto_csr_deinit(struct crypto_csr *csr)
42824b72b91aSCy Schubert {
42834b72b91aSCy Schubert X509_REQ_free((X509_REQ *)csr);
42844b72b91aSCy Schubert }
42854b72b91aSCy Schubert
42864b72b91aSCy Schubert
crypto_csr_set_ec_public_key(struct crypto_csr * csr,struct crypto_ec_key * key)42874b72b91aSCy Schubert int crypto_csr_set_ec_public_key(struct crypto_csr *csr, struct crypto_ec_key *key)
42884b72b91aSCy Schubert {
42894b72b91aSCy Schubert if (!X509_REQ_set_pubkey((X509_REQ *)csr, (EVP_PKEY *)key))
42904b72b91aSCy Schubert return -1;
42914b72b91aSCy Schubert
42924b72b91aSCy Schubert return 0;
42934b72b91aSCy Schubert }
42944b72b91aSCy Schubert
42954b72b91aSCy Schubert
crypto_csr_set_name(struct crypto_csr * csr,enum crypto_csr_name type,const char * name)42964b72b91aSCy Schubert int crypto_csr_set_name(struct crypto_csr *csr, enum crypto_csr_name type,
42974b72b91aSCy Schubert const char *name)
42984b72b91aSCy Schubert {
42994b72b91aSCy Schubert X509_NAME *n;
43004b72b91aSCy Schubert int nid;
43014b72b91aSCy Schubert
43024b72b91aSCy Schubert switch (type) {
43034b72b91aSCy Schubert case CSR_NAME_CN:
43044b72b91aSCy Schubert nid = NID_commonName;
43054b72b91aSCy Schubert break;
43064b72b91aSCy Schubert case CSR_NAME_SN:
43074b72b91aSCy Schubert nid = NID_surname;
43084b72b91aSCy Schubert break;
43094b72b91aSCy Schubert case CSR_NAME_C:
43104b72b91aSCy Schubert nid = NID_countryName;
43114b72b91aSCy Schubert break;
43124b72b91aSCy Schubert case CSR_NAME_O:
43134b72b91aSCy Schubert nid = NID_organizationName;
43144b72b91aSCy Schubert break;
43154b72b91aSCy Schubert case CSR_NAME_OU:
43164b72b91aSCy Schubert nid = NID_organizationalUnitName;
43174b72b91aSCy Schubert break;
43184b72b91aSCy Schubert default:
43194b72b91aSCy Schubert return -1;
43204b72b91aSCy Schubert }
43214b72b91aSCy Schubert
43224b72b91aSCy Schubert n = X509_REQ_get_subject_name((X509_REQ *) csr);
43234b72b91aSCy Schubert if (!n)
43244b72b91aSCy Schubert return -1;
43254b72b91aSCy Schubert
43264b72b91aSCy Schubert #if OPENSSL_VERSION_NUMBER < 0x10100000L
43274b72b91aSCy Schubert if (!X509_NAME_add_entry_by_NID(n, nid, MBSTRING_UTF8,
43284b72b91aSCy Schubert (unsigned char *) name,
43294b72b91aSCy Schubert os_strlen(name), -1, 0))
43304b72b91aSCy Schubert return -1;
43314b72b91aSCy Schubert #else
43324b72b91aSCy Schubert if (!X509_NAME_add_entry_by_NID(n, nid, MBSTRING_UTF8,
43334b72b91aSCy Schubert (const unsigned char *) name,
43344b72b91aSCy Schubert os_strlen(name), -1, 0))
43354b72b91aSCy Schubert return -1;
43364b72b91aSCy Schubert #endif
43374b72b91aSCy Schubert
43384b72b91aSCy Schubert return 0;
43394b72b91aSCy Schubert }
43404b72b91aSCy Schubert
43414b72b91aSCy Schubert
crypto_csr_set_attribute(struct crypto_csr * csr,enum crypto_csr_attr attr,int attr_type,const u8 * value,size_t len)43424b72b91aSCy Schubert int crypto_csr_set_attribute(struct crypto_csr *csr, enum crypto_csr_attr attr,
43434b72b91aSCy Schubert int attr_type, const u8 *value, size_t len)
43444b72b91aSCy Schubert {
43454b72b91aSCy Schubert int nid;
43464b72b91aSCy Schubert
43474b72b91aSCy Schubert switch (attr) {
43484b72b91aSCy Schubert case CSR_ATTR_CHALLENGE_PASSWORD:
43494b72b91aSCy Schubert nid = NID_pkcs9_challengePassword;
43504b72b91aSCy Schubert break;
43514b72b91aSCy Schubert default:
43524b72b91aSCy Schubert return -1;
43534b72b91aSCy Schubert }
43544b72b91aSCy Schubert
43554b72b91aSCy Schubert if (!X509_REQ_add1_attr_by_NID((X509_REQ *) csr, nid, attr_type, value,
43564b72b91aSCy Schubert len))
43574b72b91aSCy Schubert return -1;
43584b72b91aSCy Schubert
43594b72b91aSCy Schubert return 0;
43604b72b91aSCy Schubert }
43614b72b91aSCy Schubert
43624b72b91aSCy Schubert
crypto_csr_get_attribute(struct crypto_csr * csr,enum crypto_csr_attr attr,size_t * len,int * type)43634b72b91aSCy Schubert const u8 * crypto_csr_get_attribute(struct crypto_csr *csr,
43644b72b91aSCy Schubert enum crypto_csr_attr attr,
43654b72b91aSCy Schubert size_t *len, int *type)
43664b72b91aSCy Schubert {
43674b72b91aSCy Schubert X509_ATTRIBUTE *attrib;
43684b72b91aSCy Schubert ASN1_TYPE *attrib_type;
43694b72b91aSCy Schubert ASN1_STRING *data;
43704b72b91aSCy Schubert int loc;
43714b72b91aSCy Schubert int nid;
43724b72b91aSCy Schubert
43734b72b91aSCy Schubert switch (attr) {
43744b72b91aSCy Schubert case CSR_ATTR_CHALLENGE_PASSWORD:
43754b72b91aSCy Schubert nid = NID_pkcs9_challengePassword;
43764b72b91aSCy Schubert break;
43774b72b91aSCy Schubert default:
43784b72b91aSCy Schubert return NULL;
43794b72b91aSCy Schubert }
43804b72b91aSCy Schubert
43814b72b91aSCy Schubert loc = X509_REQ_get_attr_by_NID((X509_REQ *) csr, nid, -1);
43824b72b91aSCy Schubert if (loc < 0)
43834b72b91aSCy Schubert return NULL;
43844b72b91aSCy Schubert
43854b72b91aSCy Schubert attrib = X509_REQ_get_attr((X509_REQ *) csr, loc);
43864b72b91aSCy Schubert if (!attrib)
43874b72b91aSCy Schubert return NULL;
43884b72b91aSCy Schubert
43894b72b91aSCy Schubert attrib_type = X509_ATTRIBUTE_get0_type(attrib, 0);
43904b72b91aSCy Schubert if (!attrib_type)
43914b72b91aSCy Schubert return NULL;
43924b72b91aSCy Schubert *type = ASN1_TYPE_get(attrib_type);
43934b72b91aSCy Schubert data = X509_ATTRIBUTE_get0_data(attrib, 0, *type, NULL);
43944b72b91aSCy Schubert if (!data)
43954b72b91aSCy Schubert return NULL;
43964b72b91aSCy Schubert *len = ASN1_STRING_length(data);
43974b72b91aSCy Schubert return ASN1_STRING_get0_data(data);
43984b72b91aSCy Schubert }
43994b72b91aSCy Schubert
44004b72b91aSCy Schubert
crypto_csr_sign(struct crypto_csr * csr,struct crypto_ec_key * key,enum crypto_hash_alg algo)44014b72b91aSCy Schubert struct wpabuf * crypto_csr_sign(struct crypto_csr *csr,
44024b72b91aSCy Schubert struct crypto_ec_key *key,
44034b72b91aSCy Schubert enum crypto_hash_alg algo)
44044b72b91aSCy Schubert {
44054b72b91aSCy Schubert const EVP_MD *sign_md;
44064b72b91aSCy Schubert struct wpabuf *buf;
44074b72b91aSCy Schubert unsigned char *der = NULL;
44084b72b91aSCy Schubert int der_len;
44094b72b91aSCy Schubert
44104b72b91aSCy Schubert switch (algo) {
44114b72b91aSCy Schubert case CRYPTO_HASH_ALG_SHA256:
44124b72b91aSCy Schubert sign_md = EVP_sha256();
44134b72b91aSCy Schubert break;
44144b72b91aSCy Schubert case CRYPTO_HASH_ALG_SHA384:
44154b72b91aSCy Schubert sign_md = EVP_sha384();
44164b72b91aSCy Schubert break;
44174b72b91aSCy Schubert case CRYPTO_HASH_ALG_SHA512:
44184b72b91aSCy Schubert sign_md = EVP_sha512();
44194b72b91aSCy Schubert break;
44204b72b91aSCy Schubert default:
44214b72b91aSCy Schubert return NULL;
44224b72b91aSCy Schubert }
44234b72b91aSCy Schubert
44244b72b91aSCy Schubert if (!X509_REQ_sign((X509_REQ *) csr, (EVP_PKEY *) key, sign_md))
44254b72b91aSCy Schubert return NULL;
44264b72b91aSCy Schubert
44274b72b91aSCy Schubert der_len = i2d_X509_REQ((X509_REQ *) csr, &der);
44284b72b91aSCy Schubert if (der_len < 0)
44294b72b91aSCy Schubert return NULL;
44304b72b91aSCy Schubert
44314b72b91aSCy Schubert buf = wpabuf_alloc_copy(der, der_len);
44324b72b91aSCy Schubert OPENSSL_free(der);
44334b72b91aSCy Schubert
44344b72b91aSCy Schubert return buf;
44354b72b91aSCy Schubert }
44364b72b91aSCy Schubert
44375b9c547cSRui Paulo #endif /* CONFIG_ECC */
4438*a90b9d01SCy Schubert
4439*a90b9d01SCy Schubert
crypto_rsa_key_read_public(FILE * f)4440*a90b9d01SCy Schubert static EVP_PKEY * crypto_rsa_key_read_public(FILE *f)
4441*a90b9d01SCy Schubert {
4442*a90b9d01SCy Schubert EVP_PKEY *pkey;
4443*a90b9d01SCy Schubert X509 *x509;
4444*a90b9d01SCy Schubert const ASN1_TIME *not_before, *not_after;
4445*a90b9d01SCy Schubert int res_before, res_after;
4446*a90b9d01SCy Schubert
4447*a90b9d01SCy Schubert pkey = PEM_read_PUBKEY(f, NULL, NULL, NULL);
4448*a90b9d01SCy Schubert if (pkey)
4449*a90b9d01SCy Schubert return pkey;
4450*a90b9d01SCy Schubert
4451*a90b9d01SCy Schubert rewind(f);
4452*a90b9d01SCy Schubert x509 = PEM_read_X509(f, NULL, NULL, NULL);
4453*a90b9d01SCy Schubert if (!x509)
4454*a90b9d01SCy Schubert return NULL;
4455*a90b9d01SCy Schubert
4456*a90b9d01SCy Schubert not_before = X509_get0_notBefore(x509);
4457*a90b9d01SCy Schubert not_after = X509_get0_notAfter(x509);
4458*a90b9d01SCy Schubert if (!not_before || !not_after)
4459*a90b9d01SCy Schubert goto fail;
4460*a90b9d01SCy Schubert res_before = X509_cmp_current_time(not_before);
4461*a90b9d01SCy Schubert res_after = X509_cmp_current_time(not_after);
4462*a90b9d01SCy Schubert if (!res_before || !res_after)
4463*a90b9d01SCy Schubert goto fail;
4464*a90b9d01SCy Schubert if (res_before > 0 || res_after < 0) {
4465*a90b9d01SCy Schubert wpa_printf(MSG_INFO,
4466*a90b9d01SCy Schubert "OpenSSL: Certificate for RSA public key is not valid at this time (%d %d)",
4467*a90b9d01SCy Schubert res_before, res_after);
4468*a90b9d01SCy Schubert goto fail;
4469*a90b9d01SCy Schubert }
4470*a90b9d01SCy Schubert
4471*a90b9d01SCy Schubert pkey = X509_get_pubkey(x509);
4472*a90b9d01SCy Schubert X509_free(x509);
4473*a90b9d01SCy Schubert
4474*a90b9d01SCy Schubert if (!pkey)
4475*a90b9d01SCy Schubert return NULL;
4476*a90b9d01SCy Schubert if (EVP_PKEY_base_id(pkey) != EVP_PKEY_RSA) {
4477*a90b9d01SCy Schubert wpa_printf(MSG_INFO, "OpenSSL: No RSA public key found");
4478*a90b9d01SCy Schubert EVP_PKEY_free(pkey);
4479*a90b9d01SCy Schubert return NULL;
4480*a90b9d01SCy Schubert }
4481*a90b9d01SCy Schubert
4482*a90b9d01SCy Schubert return pkey;
4483*a90b9d01SCy Schubert fail:
4484*a90b9d01SCy Schubert X509_free(x509);
4485*a90b9d01SCy Schubert return NULL;
4486*a90b9d01SCy Schubert }
4487*a90b9d01SCy Schubert
4488*a90b9d01SCy Schubert
crypto_rsa_key_read(const char * file,bool private_key)4489*a90b9d01SCy Schubert struct crypto_rsa_key * crypto_rsa_key_read(const char *file, bool private_key)
4490*a90b9d01SCy Schubert {
4491*a90b9d01SCy Schubert FILE *f;
4492*a90b9d01SCy Schubert EVP_PKEY *pkey;
4493*a90b9d01SCy Schubert
4494*a90b9d01SCy Schubert f = fopen(file, "r");
4495*a90b9d01SCy Schubert if (!f)
4496*a90b9d01SCy Schubert return NULL;
4497*a90b9d01SCy Schubert if (private_key)
4498*a90b9d01SCy Schubert pkey = PEM_read_PrivateKey(f, NULL, NULL, NULL);
4499*a90b9d01SCy Schubert else
4500*a90b9d01SCy Schubert pkey = crypto_rsa_key_read_public(f);
4501*a90b9d01SCy Schubert fclose(f);
4502*a90b9d01SCy Schubert return (struct crypto_rsa_key *) pkey;
4503*a90b9d01SCy Schubert }
4504*a90b9d01SCy Schubert
4505*a90b9d01SCy Schubert
4506*a90b9d01SCy Schubert #ifndef OPENSSL_NO_SHA256
4507*a90b9d01SCy Schubert
crypto_rsa_oaep_sha256_encrypt(struct crypto_rsa_key * key,const struct wpabuf * in)4508*a90b9d01SCy Schubert struct wpabuf * crypto_rsa_oaep_sha256_encrypt(struct crypto_rsa_key *key,
4509*a90b9d01SCy Schubert const struct wpabuf *in)
4510*a90b9d01SCy Schubert {
4511*a90b9d01SCy Schubert #if !defined(LIBRESSL_VERSION_NUMBER) || LIBRESSL_VERSION_NUMBER >= 0x30400000L
4512*a90b9d01SCy Schubert EVP_PKEY *pkey = (EVP_PKEY *) key;
4513*a90b9d01SCy Schubert EVP_PKEY_CTX *pkctx;
4514*a90b9d01SCy Schubert struct wpabuf *res = NULL;
4515*a90b9d01SCy Schubert size_t outlen;
4516*a90b9d01SCy Schubert
4517*a90b9d01SCy Schubert pkctx = EVP_PKEY_CTX_new(pkey, NULL);
4518*a90b9d01SCy Schubert if (!pkctx)
4519*a90b9d01SCy Schubert goto fail;
4520*a90b9d01SCy Schubert
4521*a90b9d01SCy Schubert if (EVP_PKEY_encrypt_init(pkctx) != 1 ||
4522*a90b9d01SCy Schubert EVP_PKEY_CTX_set_rsa_padding(pkctx, RSA_PKCS1_OAEP_PADDING) <= 0 ||
4523*a90b9d01SCy Schubert EVP_PKEY_CTX_set_rsa_oaep_md(pkctx, EVP_sha256()) <= 0 ||
4524*a90b9d01SCy Schubert EVP_PKEY_encrypt(pkctx, NULL, &outlen, wpabuf_head(in),
4525*a90b9d01SCy Schubert wpabuf_len(in)) != 1 ||
4526*a90b9d01SCy Schubert !(res = wpabuf_alloc(outlen)) ||
4527*a90b9d01SCy Schubert EVP_PKEY_encrypt(pkctx, wpabuf_put(res, 0), &outlen,
4528*a90b9d01SCy Schubert wpabuf_head(in), wpabuf_len(in)) != 1) {
4529*a90b9d01SCy Schubert wpabuf_free(res);
4530*a90b9d01SCy Schubert res = NULL;
4531*a90b9d01SCy Schubert goto fail;
4532*a90b9d01SCy Schubert }
4533*a90b9d01SCy Schubert wpabuf_put(res, outlen);
4534*a90b9d01SCy Schubert
4535*a90b9d01SCy Schubert fail:
4536*a90b9d01SCy Schubert EVP_PKEY_CTX_free(pkctx);
4537*a90b9d01SCy Schubert return res;
4538*a90b9d01SCy Schubert #else
4539*a90b9d01SCy Schubert wpa_printf(MSG_ERROR, "%s() not supported", __func__);
4540*a90b9d01SCy Schubert return NULL;
4541*a90b9d01SCy Schubert #endif
4542*a90b9d01SCy Schubert }
4543*a90b9d01SCy Schubert
4544*a90b9d01SCy Schubert
crypto_rsa_oaep_sha256_decrypt(struct crypto_rsa_key * key,const struct wpabuf * in)4545*a90b9d01SCy Schubert struct wpabuf * crypto_rsa_oaep_sha256_decrypt(struct crypto_rsa_key *key,
4546*a90b9d01SCy Schubert const struct wpabuf *in)
4547*a90b9d01SCy Schubert {
4548*a90b9d01SCy Schubert #if !defined(LIBRESSL_VERSION_NUMBER) || LIBRESSL_VERSION_NUMBER >= 0x30400000L
4549*a90b9d01SCy Schubert EVP_PKEY *pkey = (EVP_PKEY *) key;
4550*a90b9d01SCy Schubert EVP_PKEY_CTX *pkctx;
4551*a90b9d01SCy Schubert struct wpabuf *res = NULL;
4552*a90b9d01SCy Schubert size_t outlen;
4553*a90b9d01SCy Schubert
4554*a90b9d01SCy Schubert pkctx = EVP_PKEY_CTX_new(pkey, NULL);
4555*a90b9d01SCy Schubert if (!pkctx)
4556*a90b9d01SCy Schubert goto fail;
4557*a90b9d01SCy Schubert
4558*a90b9d01SCy Schubert if (EVP_PKEY_decrypt_init(pkctx) != 1 ||
4559*a90b9d01SCy Schubert EVP_PKEY_CTX_set_rsa_padding(pkctx, RSA_PKCS1_OAEP_PADDING) <= 0 ||
4560*a90b9d01SCy Schubert EVP_PKEY_CTX_set_rsa_oaep_md(pkctx, EVP_sha256()) <= 0 ||
4561*a90b9d01SCy Schubert EVP_PKEY_decrypt(pkctx, NULL, &outlen, wpabuf_head(in),
4562*a90b9d01SCy Schubert wpabuf_len(in)) != 1 ||
4563*a90b9d01SCy Schubert !(res = wpabuf_alloc(outlen)) ||
4564*a90b9d01SCy Schubert EVP_PKEY_decrypt(pkctx, wpabuf_put(res, 0), &outlen,
4565*a90b9d01SCy Schubert wpabuf_head(in), wpabuf_len(in)) != 1) {
4566*a90b9d01SCy Schubert wpabuf_free(res);
4567*a90b9d01SCy Schubert res = NULL;
4568*a90b9d01SCy Schubert goto fail;
4569*a90b9d01SCy Schubert }
4570*a90b9d01SCy Schubert wpabuf_put(res, outlen);
4571*a90b9d01SCy Schubert
4572*a90b9d01SCy Schubert fail:
4573*a90b9d01SCy Schubert EVP_PKEY_CTX_free(pkctx);
4574*a90b9d01SCy Schubert return res;
4575*a90b9d01SCy Schubert #else
4576*a90b9d01SCy Schubert wpa_printf(MSG_ERROR, "%s() not supported", __func__);
4577*a90b9d01SCy Schubert return NULL;
4578*a90b9d01SCy Schubert #endif
4579*a90b9d01SCy Schubert }
4580*a90b9d01SCy Schubert
4581*a90b9d01SCy Schubert #endif /* OPENSSL_NO_SHA256 */
4582*a90b9d01SCy Schubert
4583*a90b9d01SCy Schubert
crypto_rsa_key_free(struct crypto_rsa_key * key)4584*a90b9d01SCy Schubert void crypto_rsa_key_free(struct crypto_rsa_key *key)
4585*a90b9d01SCy Schubert {
4586*a90b9d01SCy Schubert EVP_PKEY_free((EVP_PKEY *) key);
4587*a90b9d01SCy Schubert }
4588*a90b9d01SCy Schubert
4589*a90b9d01SCy Schubert
4590*a90b9d01SCy Schubert #ifdef CONFIG_DPP3
4591*a90b9d01SCy Schubert
4592*a90b9d01SCy Schubert #define HPKE_MAX_SHARED_SECRET_LEN 66
4593*a90b9d01SCy Schubert #define HPKE_MAX_HASH_LEN 64
4594*a90b9d01SCy Schubert #define HPKE_MAX_KEY_LEN 32
4595*a90b9d01SCy Schubert #define HPKE_MAX_NONCE_LEN 12
4596*a90b9d01SCy Schubert #define HPKE_MAX_PUB_LEN (1 + 2 * 66)
4597*a90b9d01SCy Schubert
4598*a90b9d01SCy Schubert struct hpke_context {
4599*a90b9d01SCy Schubert /* KEM */
4600*a90b9d01SCy Schubert enum hpke_kem_id kem_id;
4601*a90b9d01SCy Schubert int kem_nid;
4602*a90b9d01SCy Schubert int iana_group;
4603*a90b9d01SCy Schubert size_t n_pk;
4604*a90b9d01SCy Schubert size_t n_secret;
4605*a90b9d01SCy Schubert const EVP_MD *kem_h;
4606*a90b9d01SCy Schubert size_t kem_n_h;
4607*a90b9d01SCy Schubert
4608*a90b9d01SCy Schubert /* KDF */
4609*a90b9d01SCy Schubert enum hpke_kdf_id kdf_id;
4610*a90b9d01SCy Schubert const EVP_MD *kdf_h;
4611*a90b9d01SCy Schubert size_t n_h;
4612*a90b9d01SCy Schubert
4613*a90b9d01SCy Schubert /* AEAD */
4614*a90b9d01SCy Schubert enum hpke_aead_id aead_id;
4615*a90b9d01SCy Schubert const EVP_CIPHER *cipher;
4616*a90b9d01SCy Schubert size_t n_k;
4617*a90b9d01SCy Schubert size_t n_n;
4618*a90b9d01SCy Schubert size_t n_t;
4619*a90b9d01SCy Schubert u8 key[HPKE_MAX_KEY_LEN];
4620*a90b9d01SCy Schubert u8 base_nonce[HPKE_MAX_NONCE_LEN];
4621*a90b9d01SCy Schubert };
4622*a90b9d01SCy Schubert
4623*a90b9d01SCy Schubert
hpke_free_context(struct hpke_context * ctx)4624*a90b9d01SCy Schubert static void hpke_free_context(struct hpke_context *ctx)
4625*a90b9d01SCy Schubert {
4626*a90b9d01SCy Schubert bin_clear_free(ctx, sizeof(*ctx));
4627*a90b9d01SCy Schubert }
4628*a90b9d01SCy Schubert
4629*a90b9d01SCy Schubert
hpke_get_context(enum hpke_kem_id kem_id,enum hpke_kdf_id kdf_id,enum hpke_aead_id aead_id,struct crypto_ec_key * key)4630*a90b9d01SCy Schubert static struct hpke_context * hpke_get_context(enum hpke_kem_id kem_id,
4631*a90b9d01SCy Schubert enum hpke_kdf_id kdf_id,
4632*a90b9d01SCy Schubert enum hpke_aead_id aead_id,
4633*a90b9d01SCy Schubert struct crypto_ec_key *key)
4634*a90b9d01SCy Schubert {
4635*a90b9d01SCy Schubert struct hpke_context *ctx;
4636*a90b9d01SCy Schubert int group;
4637*a90b9d01SCy Schubert
4638*a90b9d01SCy Schubert ctx = os_zalloc(sizeof(*ctx));
4639*a90b9d01SCy Schubert if (!ctx)
4640*a90b9d01SCy Schubert return NULL;
4641*a90b9d01SCy Schubert
4642*a90b9d01SCy Schubert ctx->kem_id = kem_id;
4643*a90b9d01SCy Schubert switch (kem_id) {
4644*a90b9d01SCy Schubert case HPKE_DHKEM_P256_HKDF_SHA256:
4645*a90b9d01SCy Schubert ctx->kem_nid = NID_X9_62_prime256v1;
4646*a90b9d01SCy Schubert ctx->iana_group = 19;
4647*a90b9d01SCy Schubert ctx->n_pk = 65;
4648*a90b9d01SCy Schubert ctx->n_secret = 32;
4649*a90b9d01SCy Schubert ctx->kem_h = EVP_sha256();
4650*a90b9d01SCy Schubert ctx->kem_n_h = 32;
4651*a90b9d01SCy Schubert break;
4652*a90b9d01SCy Schubert case HPKE_DHKEM_P384_HKDF_SHA384:
4653*a90b9d01SCy Schubert ctx->kem_nid = NID_secp384r1;
4654*a90b9d01SCy Schubert ctx->iana_group = 20;
4655*a90b9d01SCy Schubert ctx->n_pk = 97;
4656*a90b9d01SCy Schubert ctx->n_secret = 48;
4657*a90b9d01SCy Schubert ctx->kem_h = EVP_sha384();
4658*a90b9d01SCy Schubert ctx->kem_n_h = 48;
4659*a90b9d01SCy Schubert break;
4660*a90b9d01SCy Schubert case HPKE_DHKEM_P521_HKDF_SHA512:
4661*a90b9d01SCy Schubert ctx->kem_nid = NID_secp521r1;
4662*a90b9d01SCy Schubert ctx->iana_group = 21;
4663*a90b9d01SCy Schubert ctx->n_pk = 133;
4664*a90b9d01SCy Schubert ctx->n_secret = 64;
4665*a90b9d01SCy Schubert ctx->kem_h = EVP_sha512();
4666*a90b9d01SCy Schubert ctx->kem_n_h = 64;
4667*a90b9d01SCy Schubert break;
4668*a90b9d01SCy Schubert default:
4669*a90b9d01SCy Schubert goto fail;
4670*a90b9d01SCy Schubert }
4671*a90b9d01SCy Schubert
4672*a90b9d01SCy Schubert ctx->kdf_id = kdf_id;
4673*a90b9d01SCy Schubert switch (kdf_id) {
4674*a90b9d01SCy Schubert case HPKE_KDF_HKDF_SHA256:
4675*a90b9d01SCy Schubert ctx->kdf_h = EVP_sha256();
4676*a90b9d01SCy Schubert ctx->n_h = 32;
4677*a90b9d01SCy Schubert break;
4678*a90b9d01SCy Schubert case HPKE_KDF_HKDF_SHA384:
4679*a90b9d01SCy Schubert ctx->kdf_h = EVP_sha384();
4680*a90b9d01SCy Schubert ctx->n_h = 48;
4681*a90b9d01SCy Schubert break;
4682*a90b9d01SCy Schubert case HPKE_KDF_HKDF_SHA512:
4683*a90b9d01SCy Schubert ctx->kdf_h = EVP_sha512();
4684*a90b9d01SCy Schubert ctx->n_h = 64;
4685*a90b9d01SCy Schubert break;
4686*a90b9d01SCy Schubert default:
4687*a90b9d01SCy Schubert goto fail;
4688*a90b9d01SCy Schubert }
4689*a90b9d01SCy Schubert
4690*a90b9d01SCy Schubert ctx->aead_id = aead_id;
4691*a90b9d01SCy Schubert switch (aead_id) {
4692*a90b9d01SCy Schubert case HPKE_AEAD_AES_128_GCM:
4693*a90b9d01SCy Schubert ctx->cipher = EVP_aes_128_gcm();
4694*a90b9d01SCy Schubert ctx->n_k = 16;
4695*a90b9d01SCy Schubert ctx->n_n = 12;
4696*a90b9d01SCy Schubert ctx->n_t = 16;
4697*a90b9d01SCy Schubert break;
4698*a90b9d01SCy Schubert case HPKE_AEAD_AES_256_GCM:
4699*a90b9d01SCy Schubert ctx->cipher = EVP_aes_256_gcm();
4700*a90b9d01SCy Schubert ctx->n_k = 32;
4701*a90b9d01SCy Schubert ctx->n_n = 12;
4702*a90b9d01SCy Schubert ctx->n_t = 16;
4703*a90b9d01SCy Schubert break;
4704*a90b9d01SCy Schubert default:
4705*a90b9d01SCy Schubert goto fail;
4706*a90b9d01SCy Schubert }
4707*a90b9d01SCy Schubert
4708*a90b9d01SCy Schubert /* Convert BP-256/384/512 to P-256/384/521 for DPP */
4709*a90b9d01SCy Schubert group = crypto_ec_key_group(key);
4710*a90b9d01SCy Schubert if (group == 28 && ctx->iana_group == 19) {
4711*a90b9d01SCy Schubert ctx->iana_group = 28;
4712*a90b9d01SCy Schubert } else if (group == 29 && ctx->iana_group == 20) {
4713*a90b9d01SCy Schubert ctx->iana_group = 29;
4714*a90b9d01SCy Schubert } else if (group == 30 && ctx->iana_group == 21) {
4715*a90b9d01SCy Schubert ctx->iana_group = 30;
4716*a90b9d01SCy Schubert ctx->n_pk = 129;
4717*a90b9d01SCy Schubert }
4718*a90b9d01SCy Schubert if (group != ctx->iana_group) {
4719*a90b9d01SCy Schubert wpa_printf(MSG_INFO, "OpenSSL:%s:group mismatch (%d != %d)",
4720*a90b9d01SCy Schubert __func__, group, ctx->iana_group);
4721*a90b9d01SCy Schubert goto fail;
4722*a90b9d01SCy Schubert }
4723*a90b9d01SCy Schubert
4724*a90b9d01SCy Schubert return ctx;
4725*a90b9d01SCy Schubert fail:
4726*a90b9d01SCy Schubert hpke_free_context(ctx);
4727*a90b9d01SCy Schubert return NULL;
4728*a90b9d01SCy Schubert }
4729*a90b9d01SCy Schubert
4730*a90b9d01SCy Schubert
hpke_suite_id(struct hpke_context * ctx,bool kem,u8 * suite_id)4731*a90b9d01SCy Schubert static size_t hpke_suite_id(struct hpke_context *ctx, bool kem, u8 *suite_id)
4732*a90b9d01SCy Schubert {
4733*a90b9d01SCy Schubert size_t suite_id_len;
4734*a90b9d01SCy Schubert
4735*a90b9d01SCy Schubert if (kem) {
4736*a90b9d01SCy Schubert os_memcpy(suite_id, "KEM", 3);
4737*a90b9d01SCy Schubert WPA_PUT_BE16(&suite_id[3], ctx->kem_id);
4738*a90b9d01SCy Schubert suite_id_len = 5;
4739*a90b9d01SCy Schubert } else {
4740*a90b9d01SCy Schubert os_memcpy(suite_id, "HPKE", 4);
4741*a90b9d01SCy Schubert WPA_PUT_BE16(&suite_id[4], ctx->kem_id);
4742*a90b9d01SCy Schubert WPA_PUT_BE16(&suite_id[6], ctx->kdf_id);
4743*a90b9d01SCy Schubert WPA_PUT_BE16(&suite_id[8], ctx->aead_id);
4744*a90b9d01SCy Schubert suite_id_len = 10;
4745*a90b9d01SCy Schubert }
4746*a90b9d01SCy Schubert return suite_id_len;
4747*a90b9d01SCy Schubert }
4748*a90b9d01SCy Schubert
4749*a90b9d01SCy Schubert
hpke_labeled_extract(struct hpke_context * ctx,bool kem,const u8 * salt,size_t salt_len,const char * label,const u8 * ikm,size_t ikm_len,u8 * prk)4750*a90b9d01SCy Schubert static int hpke_labeled_extract(struct hpke_context *ctx, bool kem,
4751*a90b9d01SCy Schubert const u8 *salt, size_t salt_len,
4752*a90b9d01SCy Schubert const char *label,
4753*a90b9d01SCy Schubert const u8 *ikm, size_t ikm_len, u8 *prk)
4754*a90b9d01SCy Schubert {
4755*a90b9d01SCy Schubert u8 zero[HPKE_MAX_HASH_LEN];
4756*a90b9d01SCy Schubert u8 suite_id[10];
4757*a90b9d01SCy Schubert size_t suite_id_len;
4758*a90b9d01SCy Schubert unsigned int mdlen = kem ? ctx->kem_n_h : ctx->n_h;
4759*a90b9d01SCy Schubert #if OPENSSL_VERSION_NUMBER >= 0x30000000L
4760*a90b9d01SCy Schubert EVP_MAC *hmac;
4761*a90b9d01SCy Schubert OSSL_PARAM params[2];
4762*a90b9d01SCy Schubert EVP_MAC_CTX *hctx;
4763*a90b9d01SCy Schubert size_t mlen;
4764*a90b9d01SCy Schubert int res;
4765*a90b9d01SCy Schubert #else /* OpenSSL version >= 3.0 */
4766*a90b9d01SCy Schubert HMAC_CTX *hctx;
4767*a90b9d01SCy Schubert int res;
4768*a90b9d01SCy Schubert #endif /* OpenSSL version >= 3.0 */
4769*a90b9d01SCy Schubert
4770*a90b9d01SCy Schubert if (!salt || !salt_len) {
4771*a90b9d01SCy Schubert salt_len = mdlen;
4772*a90b9d01SCy Schubert os_memset(zero, 0, salt_len);
4773*a90b9d01SCy Schubert salt = zero;
4774*a90b9d01SCy Schubert }
4775*a90b9d01SCy Schubert
4776*a90b9d01SCy Schubert suite_id_len = hpke_suite_id(ctx, kem, suite_id);
4777*a90b9d01SCy Schubert
4778*a90b9d01SCy Schubert /* labeled_ikm = concat("HPKE-v1", suite_id, label, ikm)
4779*a90b9d01SCy Schubert * return Extract(salt, labeled_ikm) */
4780*a90b9d01SCy Schubert
4781*a90b9d01SCy Schubert #if OPENSSL_VERSION_NUMBER >= 0x30000000L
4782*a90b9d01SCy Schubert hmac = EVP_MAC_fetch(NULL, "HMAC", NULL);
4783*a90b9d01SCy Schubert if (!hmac)
4784*a90b9d01SCy Schubert return -1;
4785*a90b9d01SCy Schubert
4786*a90b9d01SCy Schubert params[0] = OSSL_PARAM_construct_utf8_string(
4787*a90b9d01SCy Schubert "digest",
4788*a90b9d01SCy Schubert (char *) EVP_MD_get0_name(kem ? ctx->kem_h : ctx->kdf_h), 0);
4789*a90b9d01SCy Schubert params[1] = OSSL_PARAM_construct_end();
4790*a90b9d01SCy Schubert
4791*a90b9d01SCy Schubert hctx = EVP_MAC_CTX_new(hmac);
4792*a90b9d01SCy Schubert EVP_MAC_free(hmac);
4793*a90b9d01SCy Schubert if (!hctx)
4794*a90b9d01SCy Schubert return -1;
4795*a90b9d01SCy Schubert
4796*a90b9d01SCy Schubert if (EVP_MAC_init(hctx, salt, salt_len, params) != 1)
4797*a90b9d01SCy Schubert goto fail;
4798*a90b9d01SCy Schubert
4799*a90b9d01SCy Schubert if (EVP_MAC_update(hctx, (const unsigned char *) "HPKE-v1", 7) != 1 ||
4800*a90b9d01SCy Schubert EVP_MAC_update(hctx, suite_id, suite_id_len) != 1 ||
4801*a90b9d01SCy Schubert EVP_MAC_update(hctx, (const unsigned char *) label,
4802*a90b9d01SCy Schubert os_strlen(label)) != 1 ||
4803*a90b9d01SCy Schubert EVP_MAC_update(hctx, ikm, ikm_len) != 1)
4804*a90b9d01SCy Schubert goto fail;
4805*a90b9d01SCy Schubert
4806*a90b9d01SCy Schubert res = EVP_MAC_final(hctx, prk, &mlen, mdlen);
4807*a90b9d01SCy Schubert EVP_MAC_CTX_free(hctx);
4808*a90b9d01SCy Schubert
4809*a90b9d01SCy Schubert return res == 1 ? 0 : -1;
4810*a90b9d01SCy Schubert fail:
4811*a90b9d01SCy Schubert EVP_MAC_CTX_free(hctx);
4812*a90b9d01SCy Schubert return -1;
4813*a90b9d01SCy Schubert #else /* OpenSSL version >= 3.0 */
4814*a90b9d01SCy Schubert hctx = HMAC_CTX_new();
4815*a90b9d01SCy Schubert if (!hctx)
4816*a90b9d01SCy Schubert return -1;
4817*a90b9d01SCy Schubert res = HMAC_Init_ex(hctx, salt, salt_len, kem ? ctx->kem_h : ctx->kdf_h,
4818*a90b9d01SCy Schubert NULL);
4819*a90b9d01SCy Schubert if (res != 1)
4820*a90b9d01SCy Schubert goto done;
4821*a90b9d01SCy Schubert
4822*a90b9d01SCy Schubert HMAC_Update(hctx, (const unsigned char *) "HPKE-v1", 7);
4823*a90b9d01SCy Schubert HMAC_Update(hctx, suite_id, suite_id_len);
4824*a90b9d01SCy Schubert HMAC_Update(hctx, (const unsigned char *) label, os_strlen(label));
4825*a90b9d01SCy Schubert HMAC_Update(hctx, ikm, ikm_len);
4826*a90b9d01SCy Schubert
4827*a90b9d01SCy Schubert res = HMAC_Final(hctx, prk, &mdlen);
4828*a90b9d01SCy Schubert done:
4829*a90b9d01SCy Schubert HMAC_CTX_free(hctx);
4830*a90b9d01SCy Schubert
4831*a90b9d01SCy Schubert return res == 1 ? 0 : -1;
4832*a90b9d01SCy Schubert #endif /* OpenSSL version >= 3.0 */
4833*a90b9d01SCy Schubert }
4834*a90b9d01SCy Schubert
4835*a90b9d01SCy Schubert
4836*a90b9d01SCy Schubert static int
hpke_labeled_expand(struct hpke_context * ctx,bool kem,const u8 * prk,const char * label,const u8 * info,size_t info_len,u8 * out,size_t out_len)4837*a90b9d01SCy Schubert hpke_labeled_expand(struct hpke_context *ctx, bool kem, const u8 *prk,
4838*a90b9d01SCy Schubert const char *label, const u8 *info, size_t info_len,
4839*a90b9d01SCy Schubert u8 *out, size_t out_len)
4840*a90b9d01SCy Schubert {
4841*a90b9d01SCy Schubert u8 suite_id[10];
4842*a90b9d01SCy Schubert size_t suite_id_len;
4843*a90b9d01SCy Schubert u8 hash[HPKE_MAX_HASH_LEN];
4844*a90b9d01SCy Schubert u8 iter = 0;
4845*a90b9d01SCy Schubert size_t label_len = os_strlen(label);
4846*a90b9d01SCy Schubert u8 *pos;
4847*a90b9d01SCy Schubert size_t left = out_len, clen;
4848*a90b9d01SCy Schubert int res = -1;
4849*a90b9d01SCy Schubert u8 *labeled_info;
4850*a90b9d01SCy Schubert size_t labeled_info_len;
4851*a90b9d01SCy Schubert #if OPENSSL_VERSION_NUMBER >= 0x30000000L
4852*a90b9d01SCy Schubert EVP_MAC *hmac;
4853*a90b9d01SCy Schubert OSSL_PARAM params[2];
4854*a90b9d01SCy Schubert EVP_MAC_CTX *hctx = NULL;
4855*a90b9d01SCy Schubert size_t mdlen;
4856*a90b9d01SCy Schubert #else /* OpenSSL version >= 3.0 */
4857*a90b9d01SCy Schubert HMAC_CTX *hctx;
4858*a90b9d01SCy Schubert unsigned int mdlen;
4859*a90b9d01SCy Schubert #endif /* OpenSSL version >= 3.0 */
4860*a90b9d01SCy Schubert
4861*a90b9d01SCy Schubert /* labeled_info = concat(I2OSP(L, 2), "HPKE-v1", suite_id,
4862*a90b9d01SCy Schubert * label, info)
4863*a90b9d01SCy Schubert * return Expand(prk, labeled_info, L) */
4864*a90b9d01SCy Schubert suite_id_len = hpke_suite_id(ctx, kem, suite_id);
4865*a90b9d01SCy Schubert labeled_info_len = 2 + 7 + suite_id_len + label_len + info_len;
4866*a90b9d01SCy Schubert labeled_info = os_malloc(labeled_info_len);
4867*a90b9d01SCy Schubert if (!labeled_info)
4868*a90b9d01SCy Schubert return -1;
4869*a90b9d01SCy Schubert pos = labeled_info;
4870*a90b9d01SCy Schubert WPA_PUT_BE16(pos, out_len);
4871*a90b9d01SCy Schubert pos += 2;
4872*a90b9d01SCy Schubert os_memcpy(pos, "HPKE-v1", 7);
4873*a90b9d01SCy Schubert pos += 7;
4874*a90b9d01SCy Schubert os_memcpy(pos, suite_id, suite_id_len);
4875*a90b9d01SCy Schubert pos += suite_id_len;
4876*a90b9d01SCy Schubert os_memcpy(pos, label, label_len);
4877*a90b9d01SCy Schubert pos += label_len;
4878*a90b9d01SCy Schubert if (info && info_len)
4879*a90b9d01SCy Schubert os_memcpy(pos, info, info_len);
4880*a90b9d01SCy Schubert
4881*a90b9d01SCy Schubert pos = out;
4882*a90b9d01SCy Schubert #if OPENSSL_VERSION_NUMBER >= 0x30000000L
4883*a90b9d01SCy Schubert hmac = EVP_MAC_fetch(NULL, "HMAC", NULL);
4884*a90b9d01SCy Schubert if (!hmac)
4885*a90b9d01SCy Schubert goto fail;
4886*a90b9d01SCy Schubert
4887*a90b9d01SCy Schubert params[0] = OSSL_PARAM_construct_utf8_string(
4888*a90b9d01SCy Schubert "digest",
4889*a90b9d01SCy Schubert (char *) EVP_MD_get0_name(kem ? ctx->kem_h : ctx->kdf_h), 0);
4890*a90b9d01SCy Schubert params[1] = OSSL_PARAM_construct_end();
4891*a90b9d01SCy Schubert #else /* OpenSSL version >= 3.0 */
4892*a90b9d01SCy Schubert hctx = HMAC_CTX_new();
4893*a90b9d01SCy Schubert if (!hctx)
4894*a90b9d01SCy Schubert goto fail;
4895*a90b9d01SCy Schubert #endif /* OpenSSL version >= 3.0 */
4896*a90b9d01SCy Schubert
4897*a90b9d01SCy Schubert while (left > 0) {
4898*a90b9d01SCy Schubert mdlen = kem ? ctx->kem_n_h : ctx->n_h;
4899*a90b9d01SCy Schubert #if OPENSSL_VERSION_NUMBER >= 0x30000000L
4900*a90b9d01SCy Schubert EVP_MAC_CTX_free(hctx);
4901*a90b9d01SCy Schubert hctx = EVP_MAC_CTX_new(hmac);
4902*a90b9d01SCy Schubert if (!hctx)
4903*a90b9d01SCy Schubert goto fail;
4904*a90b9d01SCy Schubert
4905*a90b9d01SCy Schubert if (EVP_MAC_init(hctx, prk, mdlen, params) != 1)
4906*a90b9d01SCy Schubert goto fail;
4907*a90b9d01SCy Schubert
4908*a90b9d01SCy Schubert if (iter > 0 && EVP_MAC_update(hctx, hash, mdlen) != 1)
4909*a90b9d01SCy Schubert goto fail;
4910*a90b9d01SCy Schubert if (iter == 255)
4911*a90b9d01SCy Schubert goto fail;
4912*a90b9d01SCy Schubert iter++;
4913*a90b9d01SCy Schubert
4914*a90b9d01SCy Schubert if (EVP_MAC_update(hctx, labeled_info, labeled_info_len) != 1 ||
4915*a90b9d01SCy Schubert EVP_MAC_update(hctx, &iter, sizeof(iter)) != 1)
4916*a90b9d01SCy Schubert goto fail;
4917*a90b9d01SCy Schubert
4918*a90b9d01SCy Schubert if (EVP_MAC_final(hctx, hash, &mdlen, mdlen) != 1)
4919*a90b9d01SCy Schubert goto fail;
4920*a90b9d01SCy Schubert #else /* OpenSSL version >= 3.0 */
4921*a90b9d01SCy Schubert if (HMAC_Init_ex(hctx, prk, mdlen,
4922*a90b9d01SCy Schubert kem ? ctx->kem_h : ctx->kdf_h,
4923*a90b9d01SCy Schubert NULL) != 1)
4924*a90b9d01SCy Schubert goto fail;
4925*a90b9d01SCy Schubert
4926*a90b9d01SCy Schubert if (iter > 0)
4927*a90b9d01SCy Schubert HMAC_Update(hctx, hash, mdlen);
4928*a90b9d01SCy Schubert if (iter == 255)
4929*a90b9d01SCy Schubert goto fail;
4930*a90b9d01SCy Schubert iter++;
4931*a90b9d01SCy Schubert HMAC_Update(hctx, labeled_info, labeled_info_len);
4932*a90b9d01SCy Schubert HMAC_Update(hctx, &iter, sizeof(iter));
4933*a90b9d01SCy Schubert
4934*a90b9d01SCy Schubert if (HMAC_Final(hctx, hash, &mdlen) != 1)
4935*a90b9d01SCy Schubert goto fail;
4936*a90b9d01SCy Schubert HMAC_CTX_reset(hctx);
4937*a90b9d01SCy Schubert #endif /* OpenSSL version >= 3.0 */
4938*a90b9d01SCy Schubert
4939*a90b9d01SCy Schubert clen = left > mdlen ? mdlen : left;
4940*a90b9d01SCy Schubert os_memcpy(pos, hash, clen);
4941*a90b9d01SCy Schubert pos += clen;
4942*a90b9d01SCy Schubert left -= clen;
4943*a90b9d01SCy Schubert }
4944*a90b9d01SCy Schubert res = 0;
4945*a90b9d01SCy Schubert fail:
4946*a90b9d01SCy Schubert #if OPENSSL_VERSION_NUMBER >= 0x30000000L
4947*a90b9d01SCy Schubert EVP_MAC_free(hmac);
4948*a90b9d01SCy Schubert EVP_MAC_CTX_free(hctx);
4949*a90b9d01SCy Schubert #else /* OpenSSL version >= 3.0 */
4950*a90b9d01SCy Schubert HMAC_CTX_free(hctx);
4951*a90b9d01SCy Schubert #endif /* OpenSSL version >= 3.0 */
4952*a90b9d01SCy Schubert os_free(labeled_info);
4953*a90b9d01SCy Schubert
4954*a90b9d01SCy Schubert return res;
4955*a90b9d01SCy Schubert }
4956*a90b9d01SCy Schubert
4957*a90b9d01SCy Schubert
hpke_extract_and_expand(struct hpke_context * ctx,const u8 * dhss,size_t dhss_len,const u8 * enc,size_t enc_len,const u8 * pk_rm,size_t pk_rm_len,u8 * shared_secret)4958*a90b9d01SCy Schubert static int hpke_extract_and_expand(struct hpke_context *ctx,
4959*a90b9d01SCy Schubert const u8 *dhss, size_t dhss_len,
4960*a90b9d01SCy Schubert const u8 *enc, size_t enc_len,
4961*a90b9d01SCy Schubert const u8 *pk_rm, size_t pk_rm_len,
4962*a90b9d01SCy Schubert u8 *shared_secret)
4963*a90b9d01SCy Schubert {
4964*a90b9d01SCy Schubert u8 kem_context[2 * HPKE_MAX_PUB_LEN];
4965*a90b9d01SCy Schubert u8 eae_prk[HPKE_MAX_HASH_LEN];
4966*a90b9d01SCy Schubert
4967*a90b9d01SCy Schubert /* eae_prk = LabeledExtract("", "eae_prk", dh) */
4968*a90b9d01SCy Schubert if (hpke_labeled_extract(ctx, true, NULL, 0, "eae_prk", dhss, dhss_len,
4969*a90b9d01SCy Schubert eae_prk) < 0)
4970*a90b9d01SCy Schubert return -1;
4971*a90b9d01SCy Schubert
4972*a90b9d01SCy Schubert if (enc_len > HPKE_MAX_PUB_LEN || pk_rm_len > HPKE_MAX_PUB_LEN)
4973*a90b9d01SCy Schubert return -1;
4974*a90b9d01SCy Schubert /* kem_context = concat(enc, pkRm) */
4975*a90b9d01SCy Schubert os_memcpy(kem_context, enc, enc_len);
4976*a90b9d01SCy Schubert os_memcpy(&kem_context[enc_len], pk_rm, pk_rm_len);
4977*a90b9d01SCy Schubert
4978*a90b9d01SCy Schubert /* shared_secret = LabeledExpand(eae_prk, "shared_secret",
4979*a90b9d01SCy Schubert * kem_context, Nsecret) */
4980*a90b9d01SCy Schubert if (hpke_labeled_expand(ctx, true, eae_prk, "shared_secret",
4981*a90b9d01SCy Schubert kem_context, enc_len + pk_rm_len,
4982*a90b9d01SCy Schubert shared_secret, ctx->n_secret) < 0)
4983*a90b9d01SCy Schubert return -1;
4984*a90b9d01SCy Schubert
4985*a90b9d01SCy Schubert forced_memzero(eae_prk, sizeof(eae_prk));
4986*a90b9d01SCy Schubert return 0;
4987*a90b9d01SCy Schubert }
4988*a90b9d01SCy Schubert
4989*a90b9d01SCy Schubert
hpke_key_schedule(struct hpke_context * ctx,const u8 * shared_secret,const u8 * info,size_t info_len)4990*a90b9d01SCy Schubert static int hpke_key_schedule(struct hpke_context *ctx, const u8 *shared_secret,
4991*a90b9d01SCy Schubert const u8 *info, size_t info_len)
4992*a90b9d01SCy Schubert {
4993*a90b9d01SCy Schubert u8 key_schedule_context[1 + 2 * HPKE_MAX_HASH_LEN];
4994*a90b9d01SCy Schubert u8 secret[HPKE_MAX_HASH_LEN];
4995*a90b9d01SCy Schubert int res = -1;
4996*a90b9d01SCy Schubert
4997*a90b9d01SCy Schubert /* key_schedule_context = concat(mode, psk_id_hash, info_hash) */
4998*a90b9d01SCy Schubert key_schedule_context[0] = HPKE_MODE_BASE;
4999*a90b9d01SCy Schubert
5000*a90b9d01SCy Schubert /* psk_id_hash = LabeledExtract("", "psk_id_hash", psk_id) */
5001*a90b9d01SCy Schubert if (hpke_labeled_extract(ctx, false, NULL, 0, "psk_id_hash",
5002*a90b9d01SCy Schubert NULL, 0, &key_schedule_context[1]) < 0)
5003*a90b9d01SCy Schubert goto fail;
5004*a90b9d01SCy Schubert
5005*a90b9d01SCy Schubert /* info_hash = LabeledExtract("", "info_hash", info) */
5006*a90b9d01SCy Schubert if (hpke_labeled_extract(ctx, false, NULL, 0, "info_hash",
5007*a90b9d01SCy Schubert info, info_len,
5008*a90b9d01SCy Schubert &key_schedule_context[1 + ctx->n_h]) < 0)
5009*a90b9d01SCy Schubert goto fail;
5010*a90b9d01SCy Schubert
5011*a90b9d01SCy Schubert /* secret = LabeledExtract(shared_secret, "secret", psk) */
5012*a90b9d01SCy Schubert if (hpke_labeled_extract(ctx, false, shared_secret, ctx->n_secret,
5013*a90b9d01SCy Schubert "secret", NULL, 0, secret) < 0)
5014*a90b9d01SCy Schubert goto fail;
5015*a90b9d01SCy Schubert
5016*a90b9d01SCy Schubert /* key = LabeledExpand(secret, "key", key_schedule_context, Nk) */
5017*a90b9d01SCy Schubert if (hpke_labeled_expand(ctx, false, secret, "key",
5018*a90b9d01SCy Schubert key_schedule_context, 1 + 2 * ctx->n_h,
5019*a90b9d01SCy Schubert ctx->key, ctx->n_k) < 0)
5020*a90b9d01SCy Schubert goto fail;
5021*a90b9d01SCy Schubert
5022*a90b9d01SCy Schubert /* base_nonce = LabeledExpand(secret, "base_nonce",
5023*a90b9d01SCy Schubert * key_schedule_context, Nn) */
5024*a90b9d01SCy Schubert if (hpke_labeled_expand(ctx, false, secret, "base_nonce",
5025*a90b9d01SCy Schubert key_schedule_context, 1 + 2 * ctx->n_h,
5026*a90b9d01SCy Schubert ctx->base_nonce, ctx->n_n) < 0)
5027*a90b9d01SCy Schubert goto fail;
5028*a90b9d01SCy Schubert res = 0;
5029*a90b9d01SCy Schubert fail:
5030*a90b9d01SCy Schubert forced_memzero(key_schedule_context, sizeof(key_schedule_context));
5031*a90b9d01SCy Schubert forced_memzero(secret, sizeof(secret));
5032*a90b9d01SCy Schubert return res;
5033*a90b9d01SCy Schubert }
5034*a90b9d01SCy Schubert
5035*a90b9d01SCy Schubert
hpke_encap(struct hpke_context * ctx,struct crypto_ec_key * pk_r,u8 * shared_secret,u8 * enc)5036*a90b9d01SCy Schubert static int hpke_encap(struct hpke_context *ctx, struct crypto_ec_key *pk_r,
5037*a90b9d01SCy Schubert u8 *shared_secret, u8 *enc)
5038*a90b9d01SCy Schubert {
5039*a90b9d01SCy Schubert EVP_PKEY_CTX *pctx = NULL;
5040*a90b9d01SCy Schubert struct crypto_ec_key *sk_e;
5041*a90b9d01SCy Schubert int res = -1;
5042*a90b9d01SCy Schubert u8 *dhss = NULL;
5043*a90b9d01SCy Schubert size_t dhss_len = 0;
5044*a90b9d01SCy Schubert struct wpabuf *enc_buf = NULL, *pk_rm = NULL;
5045*a90b9d01SCy Schubert
5046*a90b9d01SCy Schubert /* skE, pkE = GenerateKeyPair() */
5047*a90b9d01SCy Schubert sk_e = crypto_ec_key_gen(ctx->iana_group);
5048*a90b9d01SCy Schubert if (!sk_e) {
5049*a90b9d01SCy Schubert wpa_printf(MSG_INFO, "OpenSSL:%s:Could not generate key pair",
5050*a90b9d01SCy Schubert __func__);
5051*a90b9d01SCy Schubert goto fail;
5052*a90b9d01SCy Schubert }
5053*a90b9d01SCy Schubert
5054*a90b9d01SCy Schubert /* dh = DH(skE, pkR) */
5055*a90b9d01SCy Schubert dhss_len = sizeof(dhss);
5056*a90b9d01SCy Schubert pctx = EVP_PKEY_CTX_new((EVP_PKEY *) sk_e, NULL);
5057*a90b9d01SCy Schubert if (!pctx ||
5058*a90b9d01SCy Schubert EVP_PKEY_derive_init(pctx) != 1 ||
5059*a90b9d01SCy Schubert EVP_PKEY_derive_set_peer(pctx, (EVP_PKEY *) pk_r) != 1 ||
5060*a90b9d01SCy Schubert EVP_PKEY_derive(pctx, NULL, &dhss_len) != 1 ||
5061*a90b9d01SCy Schubert !(dhss = os_malloc(dhss_len)) ||
5062*a90b9d01SCy Schubert EVP_PKEY_derive(pctx, dhss, &dhss_len) != 1 ||
5063*a90b9d01SCy Schubert dhss_len > HPKE_MAX_SHARED_SECRET_LEN) {
5064*a90b9d01SCy Schubert wpa_printf(MSG_INFO,
5065*a90b9d01SCy Schubert "OpenSSL: hpke_encap: EVP_PKEY_derive failed (dhss_len=%zu): %s",
5066*a90b9d01SCy Schubert dhss_len, ERR_error_string(ERR_get_error(), NULL));
5067*a90b9d01SCy Schubert goto fail;
5068*a90b9d01SCy Schubert }
5069*a90b9d01SCy Schubert
5070*a90b9d01SCy Schubert /* enc = SerializePublicKey(pkE) */
5071*a90b9d01SCy Schubert enc_buf = crypto_ec_key_get_pubkey_point(sk_e, 1);
5072*a90b9d01SCy Schubert if (!enc_buf)
5073*a90b9d01SCy Schubert goto fail;
5074*a90b9d01SCy Schubert os_memcpy(enc, wpabuf_head(enc_buf), wpabuf_len(enc_buf));
5075*a90b9d01SCy Schubert
5076*a90b9d01SCy Schubert /* pkRm = SerializePublicKey(pkR) */
5077*a90b9d01SCy Schubert pk_rm = crypto_ec_key_get_pubkey_point(pk_r, 1);
5078*a90b9d01SCy Schubert if (!pk_rm)
5079*a90b9d01SCy Schubert goto fail;
5080*a90b9d01SCy Schubert
5081*a90b9d01SCy Schubert /* kem_context = concat(enc, pkRm) */
5082*a90b9d01SCy Schubert /* shared_secret = ExtractAndExpand(dh, kem_context) */
5083*a90b9d01SCy Schubert /* return shared_secret, enc */
5084*a90b9d01SCy Schubert res = hpke_extract_and_expand(ctx, dhss, dhss_len, enc, ctx->n_pk,
5085*a90b9d01SCy Schubert wpabuf_head(pk_rm),
5086*a90b9d01SCy Schubert wpabuf_len(pk_rm), shared_secret);
5087*a90b9d01SCy Schubert fail:
5088*a90b9d01SCy Schubert bin_clear_free(dhss, dhss_len);
5089*a90b9d01SCy Schubert crypto_ec_key_deinit(sk_e);
5090*a90b9d01SCy Schubert EVP_PKEY_CTX_free(pctx);
5091*a90b9d01SCy Schubert wpabuf_free(enc_buf);
5092*a90b9d01SCy Schubert wpabuf_free(pk_rm);
5093*a90b9d01SCy Schubert return res;
5094*a90b9d01SCy Schubert }
5095*a90b9d01SCy Schubert
5096*a90b9d01SCy Schubert
5097*a90b9d01SCy Schubert static struct wpabuf *
hpke_aead_seal(struct hpke_context * ctx,const u8 * aad,size_t aad_len,const u8 * pt,size_t pt_len)5098*a90b9d01SCy Schubert hpke_aead_seal(struct hpke_context *ctx, const u8 *aad, size_t aad_len,
5099*a90b9d01SCy Schubert const u8 *pt, size_t pt_len)
5100*a90b9d01SCy Schubert {
5101*a90b9d01SCy Schubert EVP_CIPHER_CTX *cctx;
5102*a90b9d01SCy Schubert int len = 0;
5103*a90b9d01SCy Schubert struct wpabuf *ct = NULL;
5104*a90b9d01SCy Schubert
5105*a90b9d01SCy Schubert /* No need to xor in sequence number since we support only the
5106*a90b9d01SCy Schubert * single-shot API, i.e., base_nonce can be used as-is. */
5107*a90b9d01SCy Schubert
5108*a90b9d01SCy Schubert cctx = EVP_CIPHER_CTX_new();
5109*a90b9d01SCy Schubert if (!cctx ||
5110*a90b9d01SCy Schubert EVP_EncryptInit_ex(cctx, ctx->cipher, NULL, ctx->key,
5111*a90b9d01SCy Schubert ctx->base_nonce) != 1) {
5112*a90b9d01SCy Schubert wpa_printf(MSG_INFO, "OpenSSL:%s:EVP_DecryptInit_ex failed",
5113*a90b9d01SCy Schubert __func__);
5114*a90b9d01SCy Schubert goto fail;
5115*a90b9d01SCy Schubert }
5116*a90b9d01SCy Schubert if (aad && aad_len &&
5117*a90b9d01SCy Schubert EVP_EncryptUpdate(cctx, NULL, &len, aad, aad_len) != 1) {
5118*a90b9d01SCy Schubert wpa_printf(MSG_INFO, "OpenSSL:%s:EVP_EncryptUpdate(AAD) failed",
5119*a90b9d01SCy Schubert __func__);
5120*a90b9d01SCy Schubert goto fail;
5121*a90b9d01SCy Schubert }
5122*a90b9d01SCy Schubert ct = wpabuf_alloc(pt_len + AES_BLOCK_SIZE + ctx->n_t);
5123*a90b9d01SCy Schubert if (!ct)
5124*a90b9d01SCy Schubert goto fail;
5125*a90b9d01SCy Schubert if (EVP_EncryptUpdate(cctx, wpabuf_put(ct, 0), &len, pt, pt_len) != 1) {
5126*a90b9d01SCy Schubert wpa_printf(MSG_INFO, "OpenSSL:%s:EVP_EncryptUpdate failed",
5127*a90b9d01SCy Schubert __func__);
5128*a90b9d01SCy Schubert goto fail;
5129*a90b9d01SCy Schubert }
5130*a90b9d01SCy Schubert wpabuf_put(ct, len);
5131*a90b9d01SCy Schubert
5132*a90b9d01SCy Schubert if (EVP_EncryptFinal(cctx, wpabuf_put(ct, 0), &len) != 1) {
5133*a90b9d01SCy Schubert wpa_printf(MSG_INFO, "OpenSSL:%s:EVP_DecryptFinal failed",
5134*a90b9d01SCy Schubert __func__);
5135*a90b9d01SCy Schubert wpabuf_free(ct);
5136*a90b9d01SCy Schubert ct = NULL;
5137*a90b9d01SCy Schubert goto fail;
5138*a90b9d01SCy Schubert }
5139*a90b9d01SCy Schubert
5140*a90b9d01SCy Schubert if (EVP_CIPHER_CTX_ctrl(cctx, EVP_CTRL_AEAD_GET_TAG, ctx->n_t,
5141*a90b9d01SCy Schubert wpabuf_put(ct, ctx->n_t)) != 1) {
5142*a90b9d01SCy Schubert wpa_printf(MSG_INFO, "OpenSSL:%s:Could not get tag",
5143*a90b9d01SCy Schubert __func__);
5144*a90b9d01SCy Schubert wpabuf_free(ct);
5145*a90b9d01SCy Schubert ct = NULL;
5146*a90b9d01SCy Schubert goto fail;
5147*a90b9d01SCy Schubert }
5148*a90b9d01SCy Schubert fail:
5149*a90b9d01SCy Schubert EVP_CIPHER_CTX_free(cctx);
5150*a90b9d01SCy Schubert return ct;
5151*a90b9d01SCy Schubert }
5152*a90b9d01SCy Schubert
5153*a90b9d01SCy Schubert
hpke_base_seal_int(enum hpke_kem_id kem_id,enum hpke_kdf_id kdf_id,enum hpke_aead_id aead_id,struct crypto_ec_key * peer_pub,const u8 * info,size_t info_len,const u8 * aad,size_t aad_len,const u8 * pt,size_t pt_len)5154*a90b9d01SCy Schubert static struct wpabuf * hpke_base_seal_int(enum hpke_kem_id kem_id,
5155*a90b9d01SCy Schubert enum hpke_kdf_id kdf_id,
5156*a90b9d01SCy Schubert enum hpke_aead_id aead_id,
5157*a90b9d01SCy Schubert struct crypto_ec_key *peer_pub,
5158*a90b9d01SCy Schubert const u8 *info, size_t info_len,
5159*a90b9d01SCy Schubert const u8 *aad, size_t aad_len,
5160*a90b9d01SCy Schubert const u8 *pt, size_t pt_len)
5161*a90b9d01SCy Schubert {
5162*a90b9d01SCy Schubert struct hpke_context *ctx;
5163*a90b9d01SCy Schubert u8 shared_secret[HPKE_MAX_SHARED_SECRET_LEN];
5164*a90b9d01SCy Schubert u8 enc[1 + 2 * HPKE_MAX_PUB_LEN];
5165*a90b9d01SCy Schubert struct wpabuf *ct = NULL, *enc_ct = NULL;
5166*a90b9d01SCy Schubert
5167*a90b9d01SCy Schubert ctx = hpke_get_context(kem_id, kdf_id, aead_id, peer_pub);
5168*a90b9d01SCy Schubert if (!ctx)
5169*a90b9d01SCy Schubert return NULL;
5170*a90b9d01SCy Schubert
5171*a90b9d01SCy Schubert /* shared_secret, enc = Encap(pkR) */
5172*a90b9d01SCy Schubert if (hpke_encap(ctx, peer_pub, shared_secret, enc) < 0)
5173*a90b9d01SCy Schubert goto fail;
5174*a90b9d01SCy Schubert
5175*a90b9d01SCy Schubert /* KeyScheduleS(mode_base, shared_secret, info,
5176*a90b9d01SCy Schubert * default_psk, default_psk_id) */
5177*a90b9d01SCy Schubert if (hpke_key_schedule(ctx, shared_secret, info, info_len) < 0)
5178*a90b9d01SCy Schubert goto fail;
5179*a90b9d01SCy Schubert
5180*a90b9d01SCy Schubert /* ct = ctx.Seal(aad, pt) */
5181*a90b9d01SCy Schubert ct = hpke_aead_seal(ctx, aad, aad_len, pt, pt_len);
5182*a90b9d01SCy Schubert if (!ct)
5183*a90b9d01SCy Schubert goto fail;
5184*a90b9d01SCy Schubert
5185*a90b9d01SCy Schubert /* return enc, ct */
5186*a90b9d01SCy Schubert enc_ct = wpabuf_alloc(ctx->n_pk + wpabuf_len(ct));
5187*a90b9d01SCy Schubert if (!enc_ct)
5188*a90b9d01SCy Schubert goto fail;
5189*a90b9d01SCy Schubert wpabuf_put_data(enc_ct, enc, ctx->n_pk);
5190*a90b9d01SCy Schubert wpabuf_put_buf(enc_ct, ct);
5191*a90b9d01SCy Schubert
5192*a90b9d01SCy Schubert fail:
5193*a90b9d01SCy Schubert forced_memzero(shared_secret, sizeof(shared_secret));
5194*a90b9d01SCy Schubert hpke_free_context(ctx);
5195*a90b9d01SCy Schubert wpabuf_free(ct);
5196*a90b9d01SCy Schubert return enc_ct;
5197*a90b9d01SCy Schubert }
5198*a90b9d01SCy Schubert
5199*a90b9d01SCy Schubert
hpke_decap(struct hpke_context * ctx,const u8 * enc,size_t enc_ct_len,struct crypto_ec_key * sk_r,u8 * shared_secret)5200*a90b9d01SCy Schubert static int hpke_decap(struct hpke_context *ctx, const u8 *enc,
5201*a90b9d01SCy Schubert size_t enc_ct_len, struct crypto_ec_key *sk_r,
5202*a90b9d01SCy Schubert u8 *shared_secret)
5203*a90b9d01SCy Schubert {
5204*a90b9d01SCy Schubert EVP_PKEY_CTX *pctx = NULL;
5205*a90b9d01SCy Schubert struct wpabuf *pk_rm = NULL;
5206*a90b9d01SCy Schubert size_t len;
5207*a90b9d01SCy Schubert int res = -1;
5208*a90b9d01SCy Schubert struct crypto_ec_key *pk_e = NULL;
5209*a90b9d01SCy Schubert u8 *dhss = NULL;
5210*a90b9d01SCy Schubert size_t dhss_len = 0;
5211*a90b9d01SCy Schubert
5212*a90b9d01SCy Schubert /* pkE = DeserializePublicKey(enc) */
5213*a90b9d01SCy Schubert if (enc_ct_len < ctx->n_pk)
5214*a90b9d01SCy Schubert return -1; /* not enough room for enc */
5215*a90b9d01SCy Schubert if (enc[0] != 0x04)
5216*a90b9d01SCy Schubert return -1; /* not in uncompressed form */
5217*a90b9d01SCy Schubert len = (ctx->n_pk - 1) / 2;
5218*a90b9d01SCy Schubert pk_e = crypto_ec_key_set_pub(ctx->iana_group, &enc[1],
5219*a90b9d01SCy Schubert &enc[1 + len], len);
5220*a90b9d01SCy Schubert if (!pk_e)
5221*a90b9d01SCy Schubert return -1; /* invalid public key point */
5222*a90b9d01SCy Schubert /* dh = DH(skR, pkE) */
5223*a90b9d01SCy Schubert pctx = EVP_PKEY_CTX_new((EVP_PKEY *) sk_r, NULL);
5224*a90b9d01SCy Schubert if (!pctx ||
5225*a90b9d01SCy Schubert EVP_PKEY_derive_init(pctx) != 1 ||
5226*a90b9d01SCy Schubert EVP_PKEY_derive_set_peer(pctx, (EVP_PKEY *) pk_e) != 1 ||
5227*a90b9d01SCy Schubert EVP_PKEY_derive(pctx, NULL, &dhss_len) != 1 ||
5228*a90b9d01SCy Schubert !(dhss = os_malloc(dhss_len)) ||
5229*a90b9d01SCy Schubert EVP_PKEY_derive(pctx, dhss, &dhss_len) != 1 ||
5230*a90b9d01SCy Schubert dhss_len > HPKE_MAX_SHARED_SECRET_LEN) {
5231*a90b9d01SCy Schubert wpa_printf(MSG_INFO,
5232*a90b9d01SCy Schubert "OpenSSL: hpke_decap: EVP_PKEY_derive failed (dhss_len=%zu): %s",
5233*a90b9d01SCy Schubert dhss_len, ERR_error_string(ERR_get_error(), NULL));
5234*a90b9d01SCy Schubert goto fail;
5235*a90b9d01SCy Schubert }
5236*a90b9d01SCy Schubert
5237*a90b9d01SCy Schubert /* pkRm = SerializePublicKey(pk(skR)) */
5238*a90b9d01SCy Schubert pk_rm = crypto_ec_key_get_pubkey_point(sk_r, 1);
5239*a90b9d01SCy Schubert if (!pk_rm)
5240*a90b9d01SCy Schubert goto fail;
5241*a90b9d01SCy Schubert
5242*a90b9d01SCy Schubert /* kem_context = concat(enc, pkRm) */
5243*a90b9d01SCy Schubert /* shared_secret = ExtractAndExpand(dh, kem_context) */
5244*a90b9d01SCy Schubert res = hpke_extract_and_expand(ctx, dhss, dhss_len, enc, ctx->n_pk,
5245*a90b9d01SCy Schubert wpabuf_head(pk_rm),
5246*a90b9d01SCy Schubert wpabuf_len(pk_rm), shared_secret);
5247*a90b9d01SCy Schubert fail:
5248*a90b9d01SCy Schubert bin_clear_free(dhss, dhss_len);
5249*a90b9d01SCy Schubert crypto_ec_key_deinit(pk_e);
5250*a90b9d01SCy Schubert EVP_PKEY_CTX_free(pctx);
5251*a90b9d01SCy Schubert wpabuf_free(pk_rm);
5252*a90b9d01SCy Schubert return res;
5253*a90b9d01SCy Schubert }
5254*a90b9d01SCy Schubert
5255*a90b9d01SCy Schubert
5256*a90b9d01SCy Schubert static struct wpabuf *
hpke_aead_open(struct hpke_context * ctx,const u8 * aad,size_t aad_len,const u8 * ct,size_t ct_len)5257*a90b9d01SCy Schubert hpke_aead_open(struct hpke_context *ctx, const u8 *aad, size_t aad_len,
5258*a90b9d01SCy Schubert const u8 *ct, size_t ct_len)
5259*a90b9d01SCy Schubert {
5260*a90b9d01SCy Schubert EVP_CIPHER_CTX *cctx;
5261*a90b9d01SCy Schubert int len = 0;
5262*a90b9d01SCy Schubert const u8 *tag;
5263*a90b9d01SCy Schubert struct wpabuf *pt = NULL;
5264*a90b9d01SCy Schubert
5265*a90b9d01SCy Schubert if (ct_len < ctx->n_t)
5266*a90b9d01SCy Schubert return NULL;
5267*a90b9d01SCy Schubert tag = ct + ct_len - ctx->n_t;
5268*a90b9d01SCy Schubert ct_len -= ctx->n_t;
5269*a90b9d01SCy Schubert
5270*a90b9d01SCy Schubert /* No need to xor in sequence number since we support only the
5271*a90b9d01SCy Schubert * single-shot API, i.e., base_nonce can be used as-is. */
5272*a90b9d01SCy Schubert
5273*a90b9d01SCy Schubert cctx = EVP_CIPHER_CTX_new();
5274*a90b9d01SCy Schubert if (!cctx ||
5275*a90b9d01SCy Schubert EVP_DecryptInit_ex(cctx, ctx->cipher, NULL, ctx->key,
5276*a90b9d01SCy Schubert ctx->base_nonce) != 1) {
5277*a90b9d01SCy Schubert wpa_printf(MSG_INFO, "OpenSSL:%s:EVP_DecryptInit_ex failed",
5278*a90b9d01SCy Schubert __func__);
5279*a90b9d01SCy Schubert goto fail;
5280*a90b9d01SCy Schubert }
5281*a90b9d01SCy Schubert if (aad && aad_len &&
5282*a90b9d01SCy Schubert EVP_DecryptUpdate(cctx, NULL, &len, aad, aad_len) != 1) {
5283*a90b9d01SCy Schubert wpa_printf(MSG_INFO, "OpenSSL:%s:EVP_DecryptUpdate(AAD) failed",
5284*a90b9d01SCy Schubert __func__);
5285*a90b9d01SCy Schubert goto fail;
5286*a90b9d01SCy Schubert }
5287*a90b9d01SCy Schubert pt = wpabuf_alloc(ct_len + AES_BLOCK_SIZE);
5288*a90b9d01SCy Schubert if (!pt)
5289*a90b9d01SCy Schubert goto fail;
5290*a90b9d01SCy Schubert if (EVP_DecryptUpdate(cctx, wpabuf_put(pt, 0), &len, ct, ct_len) != 1) {
5291*a90b9d01SCy Schubert wpa_printf(MSG_INFO, "OpenSSL:%s:EVP_DecryptUpdate failed",
5292*a90b9d01SCy Schubert __func__);
5293*a90b9d01SCy Schubert goto fail;
5294*a90b9d01SCy Schubert }
5295*a90b9d01SCy Schubert wpabuf_put(pt, len);
5296*a90b9d01SCy Schubert
5297*a90b9d01SCy Schubert if (EVP_CIPHER_CTX_ctrl(cctx, EVP_CTRL_AEAD_SET_TAG, ctx->n_t,
5298*a90b9d01SCy Schubert (void *) tag) != 1) {
5299*a90b9d01SCy Schubert wpa_printf(MSG_INFO, "OpenSSL:%s:Could not set tag",
5300*a90b9d01SCy Schubert __func__);
5301*a90b9d01SCy Schubert wpabuf_free(pt);
5302*a90b9d01SCy Schubert pt = NULL;
5303*a90b9d01SCy Schubert goto fail;
5304*a90b9d01SCy Schubert }
5305*a90b9d01SCy Schubert
5306*a90b9d01SCy Schubert if (EVP_DecryptFinal(cctx, wpabuf_put(pt, 0), &len) != 1) {
5307*a90b9d01SCy Schubert wpa_printf(MSG_INFO, "OpenSSL:%s:EVP_DecryptFinal failed",
5308*a90b9d01SCy Schubert __func__);
5309*a90b9d01SCy Schubert wpabuf_free(pt);
5310*a90b9d01SCy Schubert pt = NULL;
5311*a90b9d01SCy Schubert }
5312*a90b9d01SCy Schubert fail:
5313*a90b9d01SCy Schubert EVP_CIPHER_CTX_free(cctx);
5314*a90b9d01SCy Schubert return pt;
5315*a90b9d01SCy Schubert }
5316*a90b9d01SCy Schubert
5317*a90b9d01SCy Schubert
hpke_base_open_int(enum hpke_kem_id kem_id,enum hpke_kdf_id kdf_id,enum hpke_aead_id aead_id,struct crypto_ec_key * own_priv,const u8 * info,size_t info_len,const u8 * aad,size_t aad_len,const u8 * enc_ct,size_t enc_ct_len)5318*a90b9d01SCy Schubert static struct wpabuf * hpke_base_open_int(enum hpke_kem_id kem_id,
5319*a90b9d01SCy Schubert enum hpke_kdf_id kdf_id,
5320*a90b9d01SCy Schubert enum hpke_aead_id aead_id,
5321*a90b9d01SCy Schubert struct crypto_ec_key *own_priv,
5322*a90b9d01SCy Schubert const u8 *info, size_t info_len,
5323*a90b9d01SCy Schubert const u8 *aad, size_t aad_len,
5324*a90b9d01SCy Schubert const u8 *enc_ct, size_t enc_ct_len)
5325*a90b9d01SCy Schubert {
5326*a90b9d01SCy Schubert struct hpke_context *ctx;
5327*a90b9d01SCy Schubert u8 shared_secret[HPKE_MAX_SHARED_SECRET_LEN];
5328*a90b9d01SCy Schubert struct wpabuf *pt = NULL;
5329*a90b9d01SCy Schubert
5330*a90b9d01SCy Schubert ctx = hpke_get_context(kem_id, kdf_id, aead_id, own_priv);
5331*a90b9d01SCy Schubert if (!ctx)
5332*a90b9d01SCy Schubert return NULL;
5333*a90b9d01SCy Schubert
5334*a90b9d01SCy Schubert /* shared_secret = Decap(enc, skR) */
5335*a90b9d01SCy Schubert if (hpke_decap(ctx, enc_ct, enc_ct_len, own_priv, shared_secret) < 0)
5336*a90b9d01SCy Schubert goto fail;
5337*a90b9d01SCy Schubert
5338*a90b9d01SCy Schubert /* KeyScheduleR(mode_base, shared_secret, info,
5339*a90b9d01SCy Schubert * default_psk, default_psk_id) */
5340*a90b9d01SCy Schubert if (hpke_key_schedule(ctx, shared_secret, info, info_len) < 0)
5341*a90b9d01SCy Schubert goto fail;
5342*a90b9d01SCy Schubert
5343*a90b9d01SCy Schubert /* return ctx.Open(aad, ct) */
5344*a90b9d01SCy Schubert pt = hpke_aead_open(ctx, aad, aad_len,
5345*a90b9d01SCy Schubert &enc_ct[ctx->n_pk], enc_ct_len - ctx->n_pk);
5346*a90b9d01SCy Schubert
5347*a90b9d01SCy Schubert fail:
5348*a90b9d01SCy Schubert forced_memzero(shared_secret, sizeof(shared_secret));
5349*a90b9d01SCy Schubert hpke_free_context(ctx);
5350*a90b9d01SCy Schubert return pt;
5351*a90b9d01SCy Schubert }
5352*a90b9d01SCy Schubert
5353*a90b9d01SCy Schubert
5354*a90b9d01SCy Schubert #if OPENSSL_VERSION_NUMBER >= 0x30200000L
5355*a90b9d01SCy Schubert
hpke_set_suite(OSSL_HPKE_SUITE * suite,enum hpke_kem_id kem_id,enum hpke_kdf_id kdf_id,enum hpke_aead_id aead_id)5356*a90b9d01SCy Schubert static bool hpke_set_suite(OSSL_HPKE_SUITE *suite,
5357*a90b9d01SCy Schubert enum hpke_kem_id kem_id,
5358*a90b9d01SCy Schubert enum hpke_kdf_id kdf_id,
5359*a90b9d01SCy Schubert enum hpke_aead_id aead_id)
5360*a90b9d01SCy Schubert {
5361*a90b9d01SCy Schubert os_memset(suite, 0, sizeof(*suite));
5362*a90b9d01SCy Schubert
5363*a90b9d01SCy Schubert switch (kem_id) {
5364*a90b9d01SCy Schubert case HPKE_DHKEM_P256_HKDF_SHA256:
5365*a90b9d01SCy Schubert suite->kem_id = OSSL_HPKE_KEM_ID_P256;
5366*a90b9d01SCy Schubert break;
5367*a90b9d01SCy Schubert case HPKE_DHKEM_P384_HKDF_SHA384:
5368*a90b9d01SCy Schubert suite->kem_id = OSSL_HPKE_KEM_ID_P384;
5369*a90b9d01SCy Schubert break;
5370*a90b9d01SCy Schubert case HPKE_DHKEM_P521_HKDF_SHA512:
5371*a90b9d01SCy Schubert suite->kem_id = OSSL_HPKE_KEM_ID_P521;
5372*a90b9d01SCy Schubert break;
5373*a90b9d01SCy Schubert default:
5374*a90b9d01SCy Schubert return false;
5375*a90b9d01SCy Schubert }
5376*a90b9d01SCy Schubert
5377*a90b9d01SCy Schubert switch (kdf_id) {
5378*a90b9d01SCy Schubert case HPKE_KDF_HKDF_SHA256:
5379*a90b9d01SCy Schubert suite->kdf_id = OSSL_HPKE_KDF_ID_HKDF_SHA256;
5380*a90b9d01SCy Schubert break;
5381*a90b9d01SCy Schubert case HPKE_KDF_HKDF_SHA384:
5382*a90b9d01SCy Schubert suite->kdf_id = OSSL_HPKE_KDF_ID_HKDF_SHA384;
5383*a90b9d01SCy Schubert break;
5384*a90b9d01SCy Schubert case HPKE_KDF_HKDF_SHA512:
5385*a90b9d01SCy Schubert suite->kdf_id = OSSL_HPKE_KDF_ID_HKDF_SHA512;
5386*a90b9d01SCy Schubert break;
5387*a90b9d01SCy Schubert default:
5388*a90b9d01SCy Schubert return false;
5389*a90b9d01SCy Schubert }
5390*a90b9d01SCy Schubert
5391*a90b9d01SCy Schubert switch (aead_id) {
5392*a90b9d01SCy Schubert case HPKE_AEAD_AES_128_GCM:
5393*a90b9d01SCy Schubert suite->aead_id = OSSL_HPKE_AEAD_ID_AES_GCM_128;
5394*a90b9d01SCy Schubert break;
5395*a90b9d01SCy Schubert case HPKE_AEAD_AES_256_GCM:
5396*a90b9d01SCy Schubert suite->aead_id = OSSL_HPKE_AEAD_ID_AES_GCM_256;
5397*a90b9d01SCy Schubert break;
5398*a90b9d01SCy Schubert default:
5399*a90b9d01SCy Schubert return false;
5400*a90b9d01SCy Schubert }
5401*a90b9d01SCy Schubert
5402*a90b9d01SCy Schubert if (!OSSL_HPKE_suite_check(*suite)) {
5403*a90b9d01SCy Schubert wpa_printf(MSG_INFO,
5404*a90b9d01SCy Schubert "OpenSSL: HPKE suite kem_id=%d kdf_id=%d aead_id=%d not supported",
5405*a90b9d01SCy Schubert kem_id, kdf_id, aead_id);
5406*a90b9d01SCy Schubert return false;
5407*a90b9d01SCy Schubert }
5408*a90b9d01SCy Schubert
5409*a90b9d01SCy Schubert return true;
5410*a90b9d01SCy Schubert }
5411*a90b9d01SCy Schubert
5412*a90b9d01SCy Schubert
hpke_base_seal(enum hpke_kem_id kem_id,enum hpke_kdf_id kdf_id,enum hpke_aead_id aead_id,struct crypto_ec_key * peer_pub,const u8 * info,size_t info_len,const u8 * aad,size_t aad_len,const u8 * pt,size_t pt_len)5413*a90b9d01SCy Schubert struct wpabuf * hpke_base_seal(enum hpke_kem_id kem_id,
5414*a90b9d01SCy Schubert enum hpke_kdf_id kdf_id,
5415*a90b9d01SCy Schubert enum hpke_aead_id aead_id,
5416*a90b9d01SCy Schubert struct crypto_ec_key *peer_pub,
5417*a90b9d01SCy Schubert const u8 *info, size_t info_len,
5418*a90b9d01SCy Schubert const u8 *aad, size_t aad_len,
5419*a90b9d01SCy Schubert const u8 *pt, size_t pt_len)
5420*a90b9d01SCy Schubert {
5421*a90b9d01SCy Schubert OSSL_HPKE_SUITE suite;
5422*a90b9d01SCy Schubert OSSL_HPKE_CTX *ctx = NULL;
5423*a90b9d01SCy Schubert struct wpabuf *res = NULL, *buf, *pub = NULL;
5424*a90b9d01SCy Schubert size_t enc_len, ct_len;
5425*a90b9d01SCy Schubert int group;
5426*a90b9d01SCy Schubert
5427*a90b9d01SCy Schubert group = crypto_ec_key_group(peer_pub);
5428*a90b9d01SCy Schubert if (group == 28 || group == 29 || group == 30) {
5429*a90b9d01SCy Schubert /* Use the internal routines for the special DPP use case with
5430*a90b9d01SCy Schubert * brainpool curves, */
5431*a90b9d01SCy Schubert return hpke_base_seal_int(kem_id, kdf_id, aead_id, peer_pub,
5432*a90b9d01SCy Schubert info, info_len, aad, aad_len,
5433*a90b9d01SCy Schubert pt, pt_len);
5434*a90b9d01SCy Schubert }
5435*a90b9d01SCy Schubert
5436*a90b9d01SCy Schubert
5437*a90b9d01SCy Schubert if (!hpke_set_suite(&suite, kem_id, kdf_id, aead_id))
5438*a90b9d01SCy Schubert return NULL;
5439*a90b9d01SCy Schubert
5440*a90b9d01SCy Schubert enc_len = OSSL_HPKE_get_public_encap_size(suite);
5441*a90b9d01SCy Schubert ct_len = OSSL_HPKE_get_ciphertext_size(suite, pt_len);
5442*a90b9d01SCy Schubert buf = wpabuf_alloc(enc_len + ct_len);
5443*a90b9d01SCy Schubert if (!buf)
5444*a90b9d01SCy Schubert goto out;
5445*a90b9d01SCy Schubert
5446*a90b9d01SCy Schubert pub = crypto_ec_key_get_pubkey_point(peer_pub, 1);
5447*a90b9d01SCy Schubert if (!pub)
5448*a90b9d01SCy Schubert goto out;
5449*a90b9d01SCy Schubert
5450*a90b9d01SCy Schubert ctx = OSSL_HPKE_CTX_new(OSSL_HPKE_MODE_BASE, suite,
5451*a90b9d01SCy Schubert OSSL_HPKE_ROLE_SENDER, NULL, NULL);
5452*a90b9d01SCy Schubert if (!ctx)
5453*a90b9d01SCy Schubert goto out;
5454*a90b9d01SCy Schubert
5455*a90b9d01SCy Schubert if (OSSL_HPKE_encap(ctx, wpabuf_put(buf, 0), &enc_len,
5456*a90b9d01SCy Schubert wpabuf_head(pub), wpabuf_len(pub),
5457*a90b9d01SCy Schubert info, info_len) != 1) {
5458*a90b9d01SCy Schubert wpa_printf(MSG_DEBUG, "OpenSSL: OSSL_HPKE_encap failed: %s",
5459*a90b9d01SCy Schubert ERR_error_string(ERR_get_error(), NULL));
5460*a90b9d01SCy Schubert goto out;
5461*a90b9d01SCy Schubert }
5462*a90b9d01SCy Schubert wpabuf_put(buf, enc_len);
5463*a90b9d01SCy Schubert
5464*a90b9d01SCy Schubert if (OSSL_HPKE_seal(ctx, wpabuf_put(buf, 0), &ct_len, aad, aad_len,
5465*a90b9d01SCy Schubert pt, pt_len) != 1) {
5466*a90b9d01SCy Schubert wpa_printf(MSG_DEBUG, "OpenSSL: OSSL_HPKE_seal failed: %s",
5467*a90b9d01SCy Schubert ERR_error_string(ERR_get_error(), NULL));
5468*a90b9d01SCy Schubert goto out;
5469*a90b9d01SCy Schubert }
5470*a90b9d01SCy Schubert wpabuf_put(buf, ct_len);
5471*a90b9d01SCy Schubert res = buf;
5472*a90b9d01SCy Schubert buf = NULL;
5473*a90b9d01SCy Schubert
5474*a90b9d01SCy Schubert out:
5475*a90b9d01SCy Schubert OSSL_HPKE_CTX_free(ctx);
5476*a90b9d01SCy Schubert wpabuf_free(buf);
5477*a90b9d01SCy Schubert wpabuf_free(pub);
5478*a90b9d01SCy Schubert return res;
5479*a90b9d01SCy Schubert }
5480*a90b9d01SCy Schubert
5481*a90b9d01SCy Schubert
hpke_base_open(enum hpke_kem_id kem_id,enum hpke_kdf_id kdf_id,enum hpke_aead_id aead_id,struct crypto_ec_key * own_priv,const u8 * info,size_t info_len,const u8 * aad,size_t aad_len,const u8 * enc_ct,size_t enc_ct_len)5482*a90b9d01SCy Schubert struct wpabuf * hpke_base_open(enum hpke_kem_id kem_id,
5483*a90b9d01SCy Schubert enum hpke_kdf_id kdf_id,
5484*a90b9d01SCy Schubert enum hpke_aead_id aead_id,
5485*a90b9d01SCy Schubert struct crypto_ec_key *own_priv,
5486*a90b9d01SCy Schubert const u8 *info, size_t info_len,
5487*a90b9d01SCy Schubert const u8 *aad, size_t aad_len,
5488*a90b9d01SCy Schubert const u8 *enc_ct, size_t enc_ct_len)
5489*a90b9d01SCy Schubert {
5490*a90b9d01SCy Schubert OSSL_HPKE_SUITE suite;
5491*a90b9d01SCy Schubert OSSL_HPKE_CTX *ctx;
5492*a90b9d01SCy Schubert struct wpabuf *buf = NULL, *res = NULL;
5493*a90b9d01SCy Schubert size_t len, enc_len;
5494*a90b9d01SCy Schubert int group;
5495*a90b9d01SCy Schubert
5496*a90b9d01SCy Schubert group = crypto_ec_key_group(own_priv);
5497*a90b9d01SCy Schubert if (group == 28 || group == 29 || group == 30) {
5498*a90b9d01SCy Schubert /* Use the internal routines for the special DPP use case with
5499*a90b9d01SCy Schubert * brainpool curves, */
5500*a90b9d01SCy Schubert return hpke_base_open_int(kem_id, kdf_id, aead_id, own_priv,
5501*a90b9d01SCy Schubert info, info_len, aad, aad_len,
5502*a90b9d01SCy Schubert enc_ct, enc_ct_len);
5503*a90b9d01SCy Schubert }
5504*a90b9d01SCy Schubert
5505*a90b9d01SCy Schubert if (!hpke_set_suite(&suite, kem_id, kdf_id, aead_id))
5506*a90b9d01SCy Schubert return NULL;
5507*a90b9d01SCy Schubert
5508*a90b9d01SCy Schubert enc_len = OSSL_HPKE_get_public_encap_size(suite);
5509*a90b9d01SCy Schubert if (enc_ct_len < enc_len) {
5510*a90b9d01SCy Schubert wpa_printf(MSG_DEBUG, "OpenSSL: Too short HPKE enc_ct data");
5511*a90b9d01SCy Schubert return NULL;
5512*a90b9d01SCy Schubert }
5513*a90b9d01SCy Schubert
5514*a90b9d01SCy Schubert ctx = OSSL_HPKE_CTX_new(OSSL_HPKE_MODE_BASE, suite,
5515*a90b9d01SCy Schubert OSSL_HPKE_ROLE_RECEIVER, NULL, NULL);
5516*a90b9d01SCy Schubert if (!ctx)
5517*a90b9d01SCy Schubert goto out;
5518*a90b9d01SCy Schubert
5519*a90b9d01SCy Schubert if (OSSL_HPKE_decap(ctx, enc_ct, enc_len, (EVP_PKEY *) own_priv,
5520*a90b9d01SCy Schubert info, info_len) != 1) {
5521*a90b9d01SCy Schubert wpa_printf(MSG_DEBUG, "OpenSSL: OSSL_HPKE_decap failed: %s",
5522*a90b9d01SCy Schubert ERR_error_string(ERR_get_error(), NULL));
5523*a90b9d01SCy Schubert goto out;
5524*a90b9d01SCy Schubert }
5525*a90b9d01SCy Schubert
5526*a90b9d01SCy Schubert len = enc_ct_len;
5527*a90b9d01SCy Schubert buf = wpabuf_alloc(len);
5528*a90b9d01SCy Schubert if (!buf)
5529*a90b9d01SCy Schubert goto out;
5530*a90b9d01SCy Schubert
5531*a90b9d01SCy Schubert if (OSSL_HPKE_open(ctx, wpabuf_put(buf, 0), &len, aad, aad_len,
5532*a90b9d01SCy Schubert enc_ct + enc_len, enc_ct_len - enc_len) != 1) {
5533*a90b9d01SCy Schubert wpa_printf(MSG_DEBUG, "OpenSSL: OSSL_HPKE_open failed: %s",
5534*a90b9d01SCy Schubert ERR_error_string(ERR_get_error(), NULL));
5535*a90b9d01SCy Schubert goto out;
5536*a90b9d01SCy Schubert }
5537*a90b9d01SCy Schubert
5538*a90b9d01SCy Schubert wpabuf_put(buf, len);
5539*a90b9d01SCy Schubert res = buf;
5540*a90b9d01SCy Schubert buf = NULL;
5541*a90b9d01SCy Schubert
5542*a90b9d01SCy Schubert out:
5543*a90b9d01SCy Schubert OSSL_HPKE_CTX_free(ctx);
5544*a90b9d01SCy Schubert wpabuf_free(buf);
5545*a90b9d01SCy Schubert return res;
5546*a90b9d01SCy Schubert }
5547*a90b9d01SCy Schubert
5548*a90b9d01SCy Schubert #else /* OpenSSL < 3.2 */
5549*a90b9d01SCy Schubert
hpke_base_seal(enum hpke_kem_id kem_id,enum hpke_kdf_id kdf_id,enum hpke_aead_id aead_id,struct crypto_ec_key * peer_pub,const u8 * info,size_t info_len,const u8 * aad,size_t aad_len,const u8 * pt,size_t pt_len)5550*a90b9d01SCy Schubert struct wpabuf * hpke_base_seal(enum hpke_kem_id kem_id,
5551*a90b9d01SCy Schubert enum hpke_kdf_id kdf_id,
5552*a90b9d01SCy Schubert enum hpke_aead_id aead_id,
5553*a90b9d01SCy Schubert struct crypto_ec_key *peer_pub,
5554*a90b9d01SCy Schubert const u8 *info, size_t info_len,
5555*a90b9d01SCy Schubert const u8 *aad, size_t aad_len,
5556*a90b9d01SCy Schubert const u8 *pt, size_t pt_len)
5557*a90b9d01SCy Schubert {
5558*a90b9d01SCy Schubert return hpke_base_seal_int(kem_id, kdf_id, aead_id, peer_pub,
5559*a90b9d01SCy Schubert info, info_len, aad, aad_len, pt, pt_len);
5560*a90b9d01SCy Schubert }
5561*a90b9d01SCy Schubert
5562*a90b9d01SCy Schubert
hpke_base_open(enum hpke_kem_id kem_id,enum hpke_kdf_id kdf_id,enum hpke_aead_id aead_id,struct crypto_ec_key * own_priv,const u8 * info,size_t info_len,const u8 * aad,size_t aad_len,const u8 * enc_ct,size_t enc_ct_len)5563*a90b9d01SCy Schubert struct wpabuf * hpke_base_open(enum hpke_kem_id kem_id,
5564*a90b9d01SCy Schubert enum hpke_kdf_id kdf_id,
5565*a90b9d01SCy Schubert enum hpke_aead_id aead_id,
5566*a90b9d01SCy Schubert struct crypto_ec_key *own_priv,
5567*a90b9d01SCy Schubert const u8 *info, size_t info_len,
5568*a90b9d01SCy Schubert const u8 *aad, size_t aad_len,
5569*a90b9d01SCy Schubert const u8 *enc_ct, size_t enc_ct_len)
5570*a90b9d01SCy Schubert {
5571*a90b9d01SCy Schubert return hpke_base_open_int(kem_id, kdf_id, aead_id, own_priv,
5572*a90b9d01SCy Schubert info, info_len, aad, aad_len,
5573*a90b9d01SCy Schubert enc_ct, enc_ct_len);
5574*a90b9d01SCy Schubert }
5575*a90b9d01SCy Schubert
5576*a90b9d01SCy Schubert #endif /* OpenSSL < 3.2 */
5577*a90b9d01SCy Schubert
5578*a90b9d01SCy Schubert #endif /* CONFIG_DPP3 */
5579*a90b9d01SCy Schubert
5580*a90b9d01SCy Schubert
crypto_unload(void)5581*a90b9d01SCy Schubert void crypto_unload(void)
5582*a90b9d01SCy Schubert {
5583*a90b9d01SCy Schubert openssl_unload_legacy_provider();
5584*a90b9d01SCy Schubert }
5585