blob: f058e067dfb438d1fc4502b4805804672898d7f5 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002 * Wrapper functions for OpenSSL libcrypto
Sunil Ravia04bd252022-05-02 22:54:18 -07003 * Copyright (c) 2004-2022, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "includes.h"
10#include <openssl/opensslv.h>
11#include <openssl/err.h>
12#include <openssl/des.h>
13#include <openssl/aes.h>
14#include <openssl/bn.h>
15#include <openssl/evp.h>
16#include <openssl/dh.h>
Dmitry Shmidt04949592012-07-19 12:16:46 -070017#include <openssl/hmac.h>
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070018#include <openssl/rand.h>
Sunil Ravi89eba102022-09-13 21:04:37 -070019#include <openssl/rsa.h>
Sunil Ravia04bd252022-05-02 22:54:18 -070020#include <openssl/pem.h>
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080021#ifdef CONFIG_ECC
22#include <openssl/ec.h>
Hai Shalom899fcc72020-10-19 14:38:18 -070023#include <openssl/x509.h>
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080024#endif /* CONFIG_ECC */
Hai Shaloma20dcd72022-02-04 13:43:00 -080025#if OPENSSL_VERSION_NUMBER >= 0x30000000L
26#include <openssl/provider.h>
Sunil Ravia04bd252022-05-02 22:54:18 -070027#include <openssl/core_names.h>
28#include <openssl/param_build.h>
Sunil8cd6f4d2022-06-28 18:40:46 +000029#include <openssl/encoder.h>
30#include <openssl/decoder.h>
Sunil Ravia04bd252022-05-02 22:54:18 -070031#else /* OpenSSL version >= 3.0 */
32#include <openssl/cmac.h>
Hai Shaloma20dcd72022-02-04 13:43:00 -080033#endif /* OpenSSL version >= 3.0 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070034
35#include "common.h"
Hai Shalom021b0b52019-04-10 11:17:58 -070036#include "utils/const_time.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070037#include "wpabuf.h"
38#include "dh_group5.h"
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080039#include "sha1.h"
40#include "sha256.h"
Dmitry Shmidt807291d2015-01-27 13:40:23 -080041#include "sha384.h"
Hai Shalom74f70d42019-02-11 14:42:39 -080042#include "sha512.h"
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -070043#include "md5.h"
44#include "aes_wrap.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070045#include "crypto.h"
46
Sunil Ravia04bd252022-05-02 22:54:18 -070047#if OPENSSL_VERSION_NUMBER < 0x10100000L
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -080048/* Compatibility wrappers for older versions. */
49
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -080050static HMAC_CTX * HMAC_CTX_new(void)
51{
52 HMAC_CTX *ctx;
53
54 ctx = os_zalloc(sizeof(*ctx));
55 if (ctx)
56 HMAC_CTX_init(ctx);
57 return ctx;
58}
59
60
61static void HMAC_CTX_free(HMAC_CTX *ctx)
62{
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -070063 if (!ctx)
64 return;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -080065 HMAC_CTX_cleanup(ctx);
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -080066 bin_clear_free(ctx, sizeof(*ctx));
67}
68
69
70static EVP_MD_CTX * EVP_MD_CTX_new(void)
71{
72 EVP_MD_CTX *ctx;
73
74 ctx = os_zalloc(sizeof(*ctx));
75 if (ctx)
76 EVP_MD_CTX_init(ctx);
77 return ctx;
78}
79
80
81static void EVP_MD_CTX_free(EVP_MD_CTX *ctx)
82{
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -070083 if (!ctx)
84 return;
85 EVP_MD_CTX_cleanup(ctx);
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -080086 bin_clear_free(ctx, sizeof(*ctx));
87}
88
Hai Shalom899fcc72020-10-19 14:38:18 -070089
Hai Shaloma20dcd72022-02-04 13:43:00 -080090#ifdef CONFIG_ECC
91
Hai Shalom899fcc72020-10-19 14:38:18 -070092static EC_KEY * EVP_PKEY_get0_EC_KEY(EVP_PKEY *pkey)
93{
94 if (pkey->type != EVP_PKEY_EC)
95 return NULL;
96 return pkey->pkey.ec;
97}
98
Hai Shaloma20dcd72022-02-04 13:43:00 -080099
100static int ECDSA_SIG_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s)
101{
102 sig->r = r;
103 sig->s = s;
104 return 1;
105}
106
107
108static void ECDSA_SIG_get0(const ECDSA_SIG *sig, const BIGNUM **pr,
109 const BIGNUM **ps)
110{
111 if (pr)
112 *pr = sig->r;
113 if (ps)
114 *ps = sig->s;
115}
116
117#endif /* CONFIG_ECC */
118
119static const unsigned char * ASN1_STRING_get0_data(const ASN1_STRING *x)
120{
121 return ASN1_STRING_data((ASN1_STRING *) x);
122}
Sunil8cd6f4d2022-06-28 18:40:46 +0000123
124
125static const ASN1_TIME * X509_get0_notBefore(const X509 *x)
126{
127 return X509_get_notBefore(x);
128}
129
130
131static const ASN1_TIME * X509_get0_notAfter(const X509 *x)
132{
133 return X509_get_notAfter(x);
134}
135
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800136#endif /* OpenSSL version < 1.1.0 */
137
Hai Shaloma20dcd72022-02-04 13:43:00 -0800138
Sunil Ravia04bd252022-05-02 22:54:18 -0700139#if OPENSSL_VERSION_NUMBER < 0x10101000L || \
140 (defined(LIBRESSL_VERSION_NUMBER) && \
141 LIBRESSL_VERSION_NUMBER < 0x30400000L)
142
143static int EC_POINT_get_affine_coordinates(const EC_GROUP *group,
144 const EC_POINT *point, BIGNUM *x,
145 BIGNUM *y, BN_CTX *ctx)
146{
147 return EC_POINT_get_affine_coordinates_GFp(group, point, x, y, ctx);
148}
149
150
151static int EC_POINT_set_affine_coordinates(const EC_GROUP *group,
152 EC_POINT *point, const BIGNUM *x,
153 const BIGNUM *y, BN_CTX *ctx)
154{
155 return EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx);
156}
157
158#endif /* OpenSSL version < 1.1.1 */
159
160
161#if OPENSSL_VERSION_NUMBER < 0x10101000L || \
162 defined(OPENSSL_IS_BORINGSSL) || \
163 (defined(LIBRESSL_VERSION_NUMBER) && \
164 LIBRESSL_VERSION_NUMBER < 0x30400000L)
165
166static int EC_POINT_set_compressed_coordinates(const EC_GROUP *group,
167 EC_POINT *point, const BIGNUM *x,
168 int y_bit, BN_CTX *ctx)
169{
170 return EC_POINT_set_compressed_coordinates_GFp(group, point, x, y_bit,
171 ctx);
172}
173
174
175static int EC_GROUP_get_curve(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
176 BIGNUM *b, BN_CTX *ctx)
177{
178 return EC_GROUP_get_curve_GFp(group, p, a, b, ctx);
179}
180
181#endif /* OpenSSL version < 1.1.1 */
182
183
184#if OPENSSL_VERSION_NUMBER >= 0x30000000L
185static OSSL_PROVIDER *openssl_default_provider = NULL;
186static OSSL_PROVIDER *openssl_legacy_provider = NULL;
187#endif /* OpenSSL version >= 3.0 */
188
Hai Shaloma20dcd72022-02-04 13:43:00 -0800189void openssl_load_legacy_provider(void)
190{
191#if OPENSSL_VERSION_NUMBER >= 0x30000000L
Sunil Ravia04bd252022-05-02 22:54:18 -0700192 if (openssl_legacy_provider)
Hai Shaloma20dcd72022-02-04 13:43:00 -0800193 return;
194
Sunil Ravia04bd252022-05-02 22:54:18 -0700195 openssl_legacy_provider = OSSL_PROVIDER_load(NULL, "legacy");
196 if (openssl_legacy_provider && !openssl_default_provider)
197 openssl_default_provider = OSSL_PROVIDER_load(NULL, "default");
198#endif /* OpenSSL version >= 3.0 */
199}
Hai Shaloma20dcd72022-02-04 13:43:00 -0800200
Sunil Ravia04bd252022-05-02 22:54:18 -0700201
202static void openssl_unload_legacy_provider(void)
203{
204#if OPENSSL_VERSION_NUMBER >= 0x30000000L
205 if (openssl_legacy_provider) {
206 OSSL_PROVIDER_unload(openssl_legacy_provider);
207 openssl_legacy_provider = NULL;
208 }
209 if (openssl_default_provider) {
210 OSSL_PROVIDER_unload(openssl_default_provider);
211 openssl_default_provider = NULL;
Hai Shaloma20dcd72022-02-04 13:43:00 -0800212 }
213#endif /* OpenSSL version >= 3.0 */
214}
215
216
Sunil Ravia04bd252022-05-02 22:54:18 -0700217#if OPENSSL_VERSION_NUMBER < 0x30000000L
218
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700219static BIGNUM * get_group5_prime(void)
220{
Sunil Ravia04bd252022-05-02 22:54:18 -0700221#if OPENSSL_VERSION_NUMBER >= 0x10100000L
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -0700222 return BN_get_rfc3526_prime_1536(NULL);
223#elif !defined(OPENSSL_IS_BORINGSSL)
224 return get_rfc3526_prime_1536(NULL);
225#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700226 static const unsigned char RFC3526_PRIME_1536[] = {
227 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xC9,0x0F,0xDA,0xA2,
228 0x21,0x68,0xC2,0x34,0xC4,0xC6,0x62,0x8B,0x80,0xDC,0x1C,0xD1,
229 0x29,0x02,0x4E,0x08,0x8A,0x67,0xCC,0x74,0x02,0x0B,0xBE,0xA6,
230 0x3B,0x13,0x9B,0x22,0x51,0x4A,0x08,0x79,0x8E,0x34,0x04,0xDD,
231 0xEF,0x95,0x19,0xB3,0xCD,0x3A,0x43,0x1B,0x30,0x2B,0x0A,0x6D,
232 0xF2,0x5F,0x14,0x37,0x4F,0xE1,0x35,0x6D,0x6D,0x51,0xC2,0x45,
233 0xE4,0x85,0xB5,0x76,0x62,0x5E,0x7E,0xC6,0xF4,0x4C,0x42,0xE9,
234 0xA6,0x37,0xED,0x6B,0x0B,0xFF,0x5C,0xB6,0xF4,0x06,0xB7,0xED,
235 0xEE,0x38,0x6B,0xFB,0x5A,0x89,0x9F,0xA5,0xAE,0x9F,0x24,0x11,
236 0x7C,0x4B,0x1F,0xE6,0x49,0x28,0x66,0x51,0xEC,0xE4,0x5B,0x3D,
237 0xC2,0x00,0x7C,0xB8,0xA1,0x63,0xBF,0x05,0x98,0xDA,0x48,0x36,
238 0x1C,0x55,0xD3,0x9A,0x69,0x16,0x3F,0xA8,0xFD,0x24,0xCF,0x5F,
239 0x83,0x65,0x5D,0x23,0xDC,0xA3,0xAD,0x96,0x1C,0x62,0xF3,0x56,
240 0x20,0x85,0x52,0xBB,0x9E,0xD5,0x29,0x07,0x70,0x96,0x96,0x6D,
241 0x67,0x0C,0x35,0x4E,0x4A,0xBC,0x98,0x04,0xF1,0x74,0x6C,0x08,
242 0xCA,0x23,0x73,0x27,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
243 };
244 return BN_bin2bn(RFC3526_PRIME_1536, sizeof(RFC3526_PRIME_1536), NULL);
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -0700245#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700246}
247
Hai Shalom021b0b52019-04-10 11:17:58 -0700248
249static BIGNUM * get_group5_order(void)
250{
251 static const unsigned char RFC3526_ORDER_1536[] = {
252 0x7F,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xE4,0x87,0xED,0x51,
253 0x10,0xB4,0x61,0x1A,0x62,0x63,0x31,0x45,0xC0,0x6E,0x0E,0x68,
254 0x94,0x81,0x27,0x04,0x45,0x33,0xE6,0x3A,0x01,0x05,0xDF,0x53,
255 0x1D,0x89,0xCD,0x91,0x28,0xA5,0x04,0x3C,0xC7,0x1A,0x02,0x6E,
256 0xF7,0xCA,0x8C,0xD9,0xE6,0x9D,0x21,0x8D,0x98,0x15,0x85,0x36,
257 0xF9,0x2F,0x8A,0x1B,0xA7,0xF0,0x9A,0xB6,0xB6,0xA8,0xE1,0x22,
258 0xF2,0x42,0xDA,0xBB,0x31,0x2F,0x3F,0x63,0x7A,0x26,0x21,0x74,
259 0xD3,0x1B,0xF6,0xB5,0x85,0xFF,0xAE,0x5B,0x7A,0x03,0x5B,0xF6,
260 0xF7,0x1C,0x35,0xFD,0xAD,0x44,0xCF,0xD2,0xD7,0x4F,0x92,0x08,
261 0xBE,0x25,0x8F,0xF3,0x24,0x94,0x33,0x28,0xF6,0x72,0x2D,0x9E,
262 0xE1,0x00,0x3E,0x5C,0x50,0xB1,0xDF,0x82,0xCC,0x6D,0x24,0x1B,
263 0x0E,0x2A,0xE9,0xCD,0x34,0x8B,0x1F,0xD4,0x7E,0x92,0x67,0xAF,
264 0xC1,0xB2,0xAE,0x91,0xEE,0x51,0xD6,0xCB,0x0E,0x31,0x79,0xAB,
265 0x10,0x42,0xA9,0x5D,0xCF,0x6A,0x94,0x83,0xB8,0x4B,0x4B,0x36,
266 0xB3,0x86,0x1A,0xA7,0x25,0x5E,0x4C,0x02,0x78,0xBA,0x36,0x04,
267 0x65,0x11,0xB9,0x93,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
268 };
269 return BN_bin2bn(RFC3526_ORDER_1536, sizeof(RFC3526_ORDER_1536), NULL);
270}
271
Sunil Ravia04bd252022-05-02 22:54:18 -0700272#endif /* OpenSSL version < 3.0 */
273
Hai Shalom021b0b52019-04-10 11:17:58 -0700274
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700275#ifdef OPENSSL_NO_SHA256
276#define NO_SHA256_WRAPPER
277#endif
Paul Stewart092955c2017-02-06 09:13:09 -0800278#ifdef OPENSSL_NO_SHA512
279#define NO_SHA384_WRAPPER
280#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700281
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700282static int openssl_digest_vector(const EVP_MD *type, size_t num_elem,
283 const u8 *addr[], const size_t *len, u8 *mac)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700284{
Dmitry Shmidt55840ad2015-12-14 12:45:46 -0800285 EVP_MD_CTX *ctx;
286 size_t i;
287 unsigned int mac_len;
288
289 if (TEST_FAIL())
290 return -1;
291
292 ctx = EVP_MD_CTX_new();
293 if (!ctx)
294 return -1;
295 if (!EVP_DigestInit_ex(ctx, type, NULL)) {
296 wpa_printf(MSG_ERROR, "OpenSSL: EVP_DigestInit_ex failed: %s",
297 ERR_error_string(ERR_get_error(), NULL));
298 EVP_MD_CTX_free(ctx);
299 return -1;
300 }
301 for (i = 0; i < num_elem; i++) {
302 if (!EVP_DigestUpdate(ctx, addr[i], len[i])) {
303 wpa_printf(MSG_ERROR, "OpenSSL: EVP_DigestUpdate "
304 "failed: %s",
305 ERR_error_string(ERR_get_error(), NULL));
306 EVP_MD_CTX_free(ctx);
307 return -1;
308 }
309 }
310 if (!EVP_DigestFinal(ctx, mac, &mac_len)) {
311 wpa_printf(MSG_ERROR, "OpenSSL: EVP_DigestFinal failed: %s",
312 ERR_error_string(ERR_get_error(), NULL));
313 EVP_MD_CTX_free(ctx);
314 return -1;
315 }
316 EVP_MD_CTX_free(ctx);
317
318 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700319}
320
321
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800322#ifndef CONFIG_FIPS
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700323int md4_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
324{
Hai Shaloma20dcd72022-02-04 13:43:00 -0800325 openssl_load_legacy_provider();
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700326 return openssl_digest_vector(EVP_md4(), num_elem, addr, len, mac);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700327}
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800328#endif /* CONFIG_FIPS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700329
330
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700331int des_encrypt(const u8 *clear, const u8 *key, u8 *cypher)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700332{
333 u8 pkey[8], next, tmp;
Hai Shaloma20dcd72022-02-04 13:43:00 -0800334 int i, plen, ret = -1;
335 EVP_CIPHER_CTX *ctx;
336
337 openssl_load_legacy_provider();
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700338
339 /* Add parity bits to the key */
340 next = 0;
341 for (i = 0; i < 7; i++) {
342 tmp = key[i];
343 pkey[i] = (tmp >> i) | next | 1;
344 next = tmp << (7 - i);
345 }
346 pkey[i] = next | 1;
347
Hai Shaloma20dcd72022-02-04 13:43:00 -0800348 ctx = EVP_CIPHER_CTX_new();
349 if (ctx &&
350 EVP_EncryptInit_ex(ctx, EVP_des_ecb(), NULL, pkey, NULL) == 1 &&
351 EVP_CIPHER_CTX_set_padding(ctx, 0) == 1 &&
352 EVP_EncryptUpdate(ctx, cypher, &plen, clear, 8) == 1 &&
353 EVP_EncryptFinal_ex(ctx, &cypher[plen], &plen) == 1)
354 ret = 0;
355 else
356 wpa_printf(MSG_ERROR, "OpenSSL: DES encrypt failed");
357
358 if (ctx)
359 EVP_CIPHER_CTX_free(ctx);
360 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700361}
362
363
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800364#ifndef CONFIG_NO_RC4
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700365int rc4_skip(const u8 *key, size_t keylen, size_t skip,
366 u8 *data, size_t data_len)
367{
368#ifdef OPENSSL_NO_RC4
369 return -1;
370#else /* OPENSSL_NO_RC4 */
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800371 EVP_CIPHER_CTX *ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700372 int outl;
373 int res = -1;
374 unsigned char skip_buf[16];
375
Hai Shaloma20dcd72022-02-04 13:43:00 -0800376 openssl_load_legacy_provider();
377
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800378 ctx = EVP_CIPHER_CTX_new();
379 if (!ctx ||
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800380 !EVP_CipherInit_ex(ctx, EVP_rc4(), NULL, NULL, NULL, 1) ||
Hai Shaloma20dcd72022-02-04 13:43:00 -0800381 !EVP_CIPHER_CTX_set_padding(ctx, 0) ||
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800382 !EVP_CIPHER_CTX_set_key_length(ctx, keylen) ||
383 !EVP_CipherInit_ex(ctx, NULL, NULL, key, NULL, 1))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700384 goto out;
385
386 while (skip >= sizeof(skip_buf)) {
387 size_t len = skip;
388 if (len > sizeof(skip_buf))
389 len = sizeof(skip_buf);
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800390 if (!EVP_CipherUpdate(ctx, skip_buf, &outl, skip_buf, len))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700391 goto out;
392 skip -= len;
393 }
394
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800395 if (EVP_CipherUpdate(ctx, data, &outl, data, data_len))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700396 res = 0;
397
398out:
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800399 if (ctx)
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800400 EVP_CIPHER_CTX_free(ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700401 return res;
402#endif /* OPENSSL_NO_RC4 */
403}
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800404#endif /* CONFIG_NO_RC4 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700405
406
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800407#ifndef CONFIG_FIPS
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700408int md5_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
409{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700410 return openssl_digest_vector(EVP_md5(), num_elem, addr, len, mac);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700411}
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800412#endif /* CONFIG_FIPS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700413
414
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700415int sha1_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
416{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700417 return openssl_digest_vector(EVP_sha1(), num_elem, addr, len, mac);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700418}
419
420
421#ifndef NO_SHA256_WRAPPER
422int sha256_vector(size_t num_elem, const u8 *addr[], const size_t *len,
423 u8 *mac)
424{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700425 return openssl_digest_vector(EVP_sha256(), num_elem, addr, len, mac);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700426}
427#endif /* NO_SHA256_WRAPPER */
428
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700429
Paul Stewart092955c2017-02-06 09:13:09 -0800430#ifndef NO_SHA384_WRAPPER
431int sha384_vector(size_t num_elem, const u8 *addr[], const size_t *len,
432 u8 *mac)
433{
434 return openssl_digest_vector(EVP_sha384(), num_elem, addr, len, mac);
435}
436#endif /* NO_SHA384_WRAPPER */
437
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700438
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700439#ifndef NO_SHA512_WRAPPER
440int sha512_vector(size_t num_elem, const u8 *addr[], const size_t *len,
441 u8 *mac)
442{
443 return openssl_digest_vector(EVP_sha512(), num_elem, addr, len, mac);
444}
445#endif /* NO_SHA512_WRAPPER */
446
447
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700448static const EVP_CIPHER * aes_get_evp_cipher(size_t keylen)
449{
450 switch (keylen) {
451 case 16:
452 return EVP_aes_128_ecb();
453 case 24:
454 return EVP_aes_192_ecb();
455 case 32:
456 return EVP_aes_256_ecb();
457 }
458
459 return NULL;
460}
461
462
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700463void * aes_encrypt_init(const u8 *key, size_t len)
464{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700465 EVP_CIPHER_CTX *ctx;
466 const EVP_CIPHER *type;
467
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800468 if (TEST_FAIL())
469 return NULL;
470
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700471 type = aes_get_evp_cipher(len);
Roshan Pius3a1667e2018-07-03 15:17:14 -0700472 if (!type) {
473 wpa_printf(MSG_INFO, "%s: Unsupported len=%u",
474 __func__, (unsigned int) len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700475 return NULL;
Roshan Pius3a1667e2018-07-03 15:17:14 -0700476 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700477
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800478 ctx = EVP_CIPHER_CTX_new();
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700479 if (ctx == NULL)
480 return NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700481 if (EVP_EncryptInit_ex(ctx, type, NULL, key, NULL) != 1) {
Sunil Ravia04bd252022-05-02 22:54:18 -0700482 EVP_CIPHER_CTX_free(ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700483 return NULL;
484 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700485 EVP_CIPHER_CTX_set_padding(ctx, 0);
486 return ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700487}
488
489
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700490int aes_encrypt(void *ctx, const u8 *plain, u8 *crypt)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700491{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700492 EVP_CIPHER_CTX *c = ctx;
493 int clen = 16;
494 if (EVP_EncryptUpdate(c, crypt, &clen, plain, 16) != 1) {
495 wpa_printf(MSG_ERROR, "OpenSSL: EVP_EncryptUpdate failed: %s",
496 ERR_error_string(ERR_get_error(), NULL));
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700497 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700498 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700499 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700500}
501
502
503void aes_encrypt_deinit(void *ctx)
504{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700505 EVP_CIPHER_CTX *c = ctx;
506 u8 buf[16];
507 int len = sizeof(buf);
508 if (EVP_EncryptFinal_ex(c, buf, &len) != 1) {
509 wpa_printf(MSG_ERROR, "OpenSSL: EVP_EncryptFinal_ex failed: "
510 "%s", ERR_error_string(ERR_get_error(), NULL));
511 }
512 if (len != 0) {
513 wpa_printf(MSG_ERROR, "OpenSSL: Unexpected padding length %d "
514 "in AES encrypt", len);
515 }
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800516 EVP_CIPHER_CTX_free(c);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700517}
518
519
520void * aes_decrypt_init(const u8 *key, size_t len)
521{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700522 EVP_CIPHER_CTX *ctx;
523 const EVP_CIPHER *type;
524
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800525 if (TEST_FAIL())
526 return NULL;
527
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700528 type = aes_get_evp_cipher(len);
Roshan Pius3a1667e2018-07-03 15:17:14 -0700529 if (!type) {
530 wpa_printf(MSG_INFO, "%s: Unsupported len=%u",
531 __func__, (unsigned int) len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700532 return NULL;
Roshan Pius3a1667e2018-07-03 15:17:14 -0700533 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700534
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800535 ctx = EVP_CIPHER_CTX_new();
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700536 if (ctx == NULL)
537 return NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700538 if (EVP_DecryptInit_ex(ctx, type, NULL, key, NULL) != 1) {
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800539 EVP_CIPHER_CTX_free(ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700540 return NULL;
541 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700542 EVP_CIPHER_CTX_set_padding(ctx, 0);
543 return ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700544}
545
546
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700547int aes_decrypt(void *ctx, const u8 *crypt, u8 *plain)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700548{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700549 EVP_CIPHER_CTX *c = ctx;
550 int plen = 16;
551 if (EVP_DecryptUpdate(c, plain, &plen, crypt, 16) != 1) {
552 wpa_printf(MSG_ERROR, "OpenSSL: EVP_DecryptUpdate failed: %s",
553 ERR_error_string(ERR_get_error(), NULL));
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700554 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700555 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700556 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700557}
558
559
560void aes_decrypt_deinit(void *ctx)
561{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700562 EVP_CIPHER_CTX *c = ctx;
563 u8 buf[16];
564 int len = sizeof(buf);
565 if (EVP_DecryptFinal_ex(c, buf, &len) != 1) {
566 wpa_printf(MSG_ERROR, "OpenSSL: EVP_DecryptFinal_ex failed: "
567 "%s", ERR_error_string(ERR_get_error(), NULL));
568 }
569 if (len != 0) {
570 wpa_printf(MSG_ERROR, "OpenSSL: Unexpected padding length %d "
571 "in AES decrypt", len);
572 }
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800573 EVP_CIPHER_CTX_free(c);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700574}
575
576
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800577#ifndef CONFIG_FIPS
578#ifndef CONFIG_OPENSSL_INTERNAL_AES_WRAP
579
Sunil Ravia04bd252022-05-02 22:54:18 -0700580#if OPENSSL_VERSION_NUMBER >= 0x30000000L
581static const EVP_CIPHER * aes_get_evp_wrap_cipher(size_t keylen)
582{
583 switch (keylen) {
584 case 16:
585 return EVP_aes_128_wrap();
586 case 24:
587 return EVP_aes_192_wrap();
588 case 32:
589 return EVP_aes_256_wrap();
590 default:
591 return NULL;
592 }
593}
594#endif /* OpenSSL version >= 3.0 */
595
596
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800597int aes_wrap(const u8 *kek, size_t kek_len, int n, const u8 *plain, u8 *cipher)
598{
Sunil Ravia04bd252022-05-02 22:54:18 -0700599#if OPENSSL_VERSION_NUMBER >= 0x30000000L
600 EVP_CIPHER_CTX *ctx;
601 const EVP_CIPHER *type;
602 int ret = -1, len;
603 u8 buf[16];
604
605 if (TEST_FAIL())
606 return -1;
607
608 type = aes_get_evp_wrap_cipher(kek_len);
609 if (!type)
610 return -1;
611
612 ctx = EVP_CIPHER_CTX_new();
613 if (!ctx)
614 return -1;
615
616 if (EVP_EncryptInit_ex(ctx, type, NULL, kek, NULL) == 1 &&
617 EVP_CIPHER_CTX_set_padding(ctx, 0) == 1 &&
618 EVP_EncryptUpdate(ctx, cipher, &len, plain, n * 8) == 1 &&
619 len == (n + 1) * 8 &&
620 EVP_EncryptFinal_ex(ctx, buf, &len) == 1)
621 ret = 0;
622
623 EVP_CIPHER_CTX_free(ctx);
624 return ret;
625#else /* OpenSSL version >= 3.0 */
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800626 AES_KEY actx;
627 int res;
628
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800629 if (TEST_FAIL())
630 return -1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800631 if (AES_set_encrypt_key(kek, kek_len << 3, &actx))
632 return -1;
633 res = AES_wrap_key(&actx, NULL, cipher, plain, n * 8);
634 OPENSSL_cleanse(&actx, sizeof(actx));
635 return res <= 0 ? -1 : 0;
Sunil Ravia04bd252022-05-02 22:54:18 -0700636#endif /* OpenSSL version >= 3.0 */
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800637}
638
639
640int aes_unwrap(const u8 *kek, size_t kek_len, int n, const u8 *cipher,
641 u8 *plain)
642{
Sunil Ravia04bd252022-05-02 22:54:18 -0700643#if OPENSSL_VERSION_NUMBER >= 0x30000000L
644 EVP_CIPHER_CTX *ctx;
645 const EVP_CIPHER *type;
646 int ret = -1, len;
647 u8 buf[16];
648
649 if (TEST_FAIL())
650 return -1;
651
652 type = aes_get_evp_wrap_cipher(kek_len);
653 if (!type)
654 return -1;
655
656 ctx = EVP_CIPHER_CTX_new();
657 if (!ctx)
658 return -1;
659
660 if (EVP_DecryptInit_ex(ctx, type, NULL, kek, NULL) == 1 &&
661 EVP_CIPHER_CTX_set_padding(ctx, 0) == 1 &&
662 EVP_DecryptUpdate(ctx, plain, &len, cipher, (n + 1) * 8) == 1 &&
663 len == n * 8 &&
664 EVP_DecryptFinal_ex(ctx, buf, &len) == 1)
665 ret = 0;
666
667 EVP_CIPHER_CTX_free(ctx);
668 return ret;
669#else /* OpenSSL version >= 3.0 */
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800670 AES_KEY actx;
671 int res;
672
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800673 if (TEST_FAIL())
674 return -1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800675 if (AES_set_decrypt_key(kek, kek_len << 3, &actx))
676 return -1;
677 res = AES_unwrap_key(&actx, NULL, plain, cipher, (n + 1) * 8);
678 OPENSSL_cleanse(&actx, sizeof(actx));
679 return res <= 0 ? -1 : 0;
Sunil Ravia04bd252022-05-02 22:54:18 -0700680#endif /* OpenSSL version >= 3.0 */
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800681}
682
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800683#endif /* CONFIG_OPENSSL_INTERNAL_AES_WRAP */
684#endif /* CONFIG_FIPS */
685
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800686
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -0700687int aes_128_cbc_encrypt(const u8 *key, const u8 *iv, u8 *data, size_t data_len)
688{
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800689 EVP_CIPHER_CTX *ctx;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -0700690 int clen, len;
691 u8 buf[16];
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800692 int res = -1;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -0700693
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -0800694 if (TEST_FAIL())
695 return -1;
696
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800697 ctx = EVP_CIPHER_CTX_new();
698 if (!ctx)
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -0700699 return -1;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -0700700 clen = data_len;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -0700701 len = sizeof(buf);
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800702 if (EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv) == 1 &&
703 EVP_CIPHER_CTX_set_padding(ctx, 0) == 1 &&
704 EVP_EncryptUpdate(ctx, data, &clen, data, data_len) == 1 &&
705 clen == (int) data_len &&
706 EVP_EncryptFinal_ex(ctx, buf, &len) == 1 && len == 0)
707 res = 0;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800708 EVP_CIPHER_CTX_free(ctx);
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -0700709
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800710 return res;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -0700711}
712
713
714int aes_128_cbc_decrypt(const u8 *key, const u8 *iv, u8 *data, size_t data_len)
715{
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800716 EVP_CIPHER_CTX *ctx;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -0700717 int plen, len;
718 u8 buf[16];
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800719 int res = -1;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -0700720
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -0800721 if (TEST_FAIL())
722 return -1;
723
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800724 ctx = EVP_CIPHER_CTX_new();
725 if (!ctx)
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -0700726 return -1;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -0700727 plen = data_len;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -0700728 len = sizeof(buf);
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800729 if (EVP_DecryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv) == 1 &&
730 EVP_CIPHER_CTX_set_padding(ctx, 0) == 1 &&
731 EVP_DecryptUpdate(ctx, data, &plen, data, data_len) == 1 &&
732 plen == (int) data_len &&
733 EVP_DecryptFinal_ex(ctx, buf, &len) == 1 && len == 0)
734 res = 0;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800735 EVP_CIPHER_CTX_free(ctx);
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -0700736
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800737 return res;
738
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -0700739}
740
741
Roshan Pius3a1667e2018-07-03 15:17:14 -0700742int crypto_dh_init(u8 generator, const u8 *prime, size_t prime_len, u8 *privkey,
743 u8 *pubkey)
744{
745 size_t pubkey_len, pad;
746
747 if (os_get_random(privkey, prime_len) < 0)
748 return -1;
749 if (os_memcmp(privkey, prime, prime_len) > 0) {
750 /* Make sure private value is smaller than prime */
751 privkey[0] = 0;
752 }
753
754 pubkey_len = prime_len;
755 if (crypto_mod_exp(&generator, 1, privkey, prime_len, prime, prime_len,
756 pubkey, &pubkey_len) < 0)
757 return -1;
758 if (pubkey_len < prime_len) {
759 pad = prime_len - pubkey_len;
760 os_memmove(pubkey + pad, pubkey, pubkey_len);
761 os_memset(pubkey, 0, pad);
762 }
763
764 return 0;
765}
766
767
768int crypto_dh_derive_secret(u8 generator, const u8 *prime, size_t prime_len,
Hai Shalom021b0b52019-04-10 11:17:58 -0700769 const u8 *order, size_t order_len,
Roshan Pius3a1667e2018-07-03 15:17:14 -0700770 const u8 *privkey, size_t privkey_len,
771 const u8 *pubkey, size_t pubkey_len,
772 u8 *secret, size_t *len)
773{
Hai Shalom021b0b52019-04-10 11:17:58 -0700774 BIGNUM *pub, *p;
775 int res = -1;
776
777 pub = BN_bin2bn(pubkey, pubkey_len, NULL);
778 p = BN_bin2bn(prime, prime_len, NULL);
779 if (!pub || !p || BN_is_zero(pub) || BN_is_one(pub) ||
780 BN_cmp(pub, p) >= 0)
781 goto fail;
782
783 if (order) {
784 BN_CTX *ctx;
785 BIGNUM *q, *tmp;
786 int failed;
787
788 /* verify: pubkey^q == 1 mod p */
789 q = BN_bin2bn(order, order_len, NULL);
790 ctx = BN_CTX_new();
791 tmp = BN_new();
792 failed = !q || !ctx || !tmp ||
793 !BN_mod_exp(tmp, pub, q, p, ctx) ||
794 !BN_is_one(tmp);
Hai Shalom81f62d82019-07-22 12:10:00 -0700795 BN_clear_free(q);
796 BN_clear_free(tmp);
Hai Shalom021b0b52019-04-10 11:17:58 -0700797 BN_CTX_free(ctx);
798 if (failed)
799 goto fail;
800 }
801
802 res = crypto_mod_exp(pubkey, pubkey_len, privkey, privkey_len,
803 prime, prime_len, secret, len);
804fail:
Hai Shalom81f62d82019-07-22 12:10:00 -0700805 BN_clear_free(pub);
806 BN_clear_free(p);
Hai Shalom021b0b52019-04-10 11:17:58 -0700807 return res;
Roshan Pius3a1667e2018-07-03 15:17:14 -0700808}
809
810
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700811int crypto_mod_exp(const u8 *base, size_t base_len,
812 const u8 *power, size_t power_len,
813 const u8 *modulus, size_t modulus_len,
814 u8 *result, size_t *result_len)
815{
816 BIGNUM *bn_base, *bn_exp, *bn_modulus, *bn_result;
817 int ret = -1;
818 BN_CTX *ctx;
819
820 ctx = BN_CTX_new();
821 if (ctx == NULL)
822 return -1;
823
824 bn_base = BN_bin2bn(base, base_len, NULL);
825 bn_exp = BN_bin2bn(power, power_len, NULL);
826 bn_modulus = BN_bin2bn(modulus, modulus_len, NULL);
827 bn_result = BN_new();
828
829 if (bn_base == NULL || bn_exp == NULL || bn_modulus == NULL ||
830 bn_result == NULL)
831 goto error;
832
Hai Shalom021b0b52019-04-10 11:17:58 -0700833 if (BN_mod_exp_mont_consttime(bn_result, bn_base, bn_exp, bn_modulus,
834 ctx, NULL) != 1)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700835 goto error;
836
837 *result_len = BN_bn2bin(bn_result, result);
838 ret = 0;
839
840error:
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -0700841 BN_clear_free(bn_base);
842 BN_clear_free(bn_exp);
843 BN_clear_free(bn_modulus);
844 BN_clear_free(bn_result);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700845 BN_CTX_free(ctx);
846 return ret;
847}
848
849
850struct crypto_cipher {
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800851 EVP_CIPHER_CTX *enc;
852 EVP_CIPHER_CTX *dec;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700853};
854
855
856struct crypto_cipher * crypto_cipher_init(enum crypto_cipher_alg alg,
857 const u8 *iv, const u8 *key,
858 size_t key_len)
859{
860 struct crypto_cipher *ctx;
861 const EVP_CIPHER *cipher;
862
863 ctx = os_zalloc(sizeof(*ctx));
864 if (ctx == NULL)
865 return NULL;
866
867 switch (alg) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800868#ifndef CONFIG_NO_RC4
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700869#ifndef OPENSSL_NO_RC4
870 case CRYPTO_CIPHER_ALG_RC4:
871 cipher = EVP_rc4();
872 break;
873#endif /* OPENSSL_NO_RC4 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800874#endif /* CONFIG_NO_RC4 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700875#ifndef OPENSSL_NO_AES
876 case CRYPTO_CIPHER_ALG_AES:
877 switch (key_len) {
878 case 16:
879 cipher = EVP_aes_128_cbc();
880 break;
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -0700881#ifndef OPENSSL_IS_BORINGSSL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700882 case 24:
883 cipher = EVP_aes_192_cbc();
884 break;
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -0700885#endif /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700886 case 32:
887 cipher = EVP_aes_256_cbc();
888 break;
889 default:
890 os_free(ctx);
891 return NULL;
892 }
893 break;
894#endif /* OPENSSL_NO_AES */
895#ifndef OPENSSL_NO_DES
896 case CRYPTO_CIPHER_ALG_3DES:
897 cipher = EVP_des_ede3_cbc();
898 break;
899 case CRYPTO_CIPHER_ALG_DES:
900 cipher = EVP_des_cbc();
901 break;
902#endif /* OPENSSL_NO_DES */
903#ifndef OPENSSL_NO_RC2
904 case CRYPTO_CIPHER_ALG_RC2:
905 cipher = EVP_rc2_ecb();
906 break;
907#endif /* OPENSSL_NO_RC2 */
908 default:
909 os_free(ctx);
910 return NULL;
911 }
912
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800913 if (!(ctx->enc = EVP_CIPHER_CTX_new()) ||
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800914 !EVP_EncryptInit_ex(ctx->enc, cipher, NULL, NULL, NULL) ||
Hai Shaloma20dcd72022-02-04 13:43:00 -0800915 !EVP_CIPHER_CTX_set_padding(ctx->enc, 0) ||
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800916 !EVP_CIPHER_CTX_set_key_length(ctx->enc, key_len) ||
917 !EVP_EncryptInit_ex(ctx->enc, NULL, NULL, key, iv)) {
918 if (ctx->enc)
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800919 EVP_CIPHER_CTX_free(ctx->enc);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700920 os_free(ctx);
921 return NULL;
922 }
923
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800924 if (!(ctx->dec = EVP_CIPHER_CTX_new()) ||
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800925 !EVP_DecryptInit_ex(ctx->dec, cipher, NULL, NULL, NULL) ||
Hai Shaloma20dcd72022-02-04 13:43:00 -0800926 !EVP_CIPHER_CTX_set_padding(ctx->dec, 0) ||
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800927 !EVP_CIPHER_CTX_set_key_length(ctx->dec, key_len) ||
928 !EVP_DecryptInit_ex(ctx->dec, NULL, NULL, key, iv)) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800929 EVP_CIPHER_CTX_free(ctx->enc);
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800930 if (ctx->dec)
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800931 EVP_CIPHER_CTX_free(ctx->dec);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700932 os_free(ctx);
933 return NULL;
934 }
935
936 return ctx;
937}
938
939
940int crypto_cipher_encrypt(struct crypto_cipher *ctx, const u8 *plain,
941 u8 *crypt, size_t len)
942{
943 int outl;
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800944 if (!EVP_EncryptUpdate(ctx->enc, crypt, &outl, plain, len))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700945 return -1;
946 return 0;
947}
948
949
950int crypto_cipher_decrypt(struct crypto_cipher *ctx, const u8 *crypt,
951 u8 *plain, size_t len)
952{
953 int outl;
954 outl = len;
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800955 if (!EVP_DecryptUpdate(ctx->dec, plain, &outl, crypt, len))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700956 return -1;
957 return 0;
958}
959
960
961void crypto_cipher_deinit(struct crypto_cipher *ctx)
962{
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800963 EVP_CIPHER_CTX_free(ctx->enc);
964 EVP_CIPHER_CTX_free(ctx->dec);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700965 os_free(ctx);
966}
967
968
969void * dh5_init(struct wpabuf **priv, struct wpabuf **publ)
970{
Sunil Ravia04bd252022-05-02 22:54:18 -0700971#if OPENSSL_VERSION_NUMBER < 0x10100000L
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700972 DH *dh;
973 struct wpabuf *pubkey = NULL, *privkey = NULL;
974 size_t publen, privlen;
975
976 *priv = NULL;
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700977 wpabuf_free(*publ);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700978 *publ = NULL;
979
980 dh = DH_new();
981 if (dh == NULL)
982 return NULL;
983
984 dh->g = BN_new();
985 if (dh->g == NULL || BN_set_word(dh->g, 2) != 1)
986 goto err;
987
988 dh->p = get_group5_prime();
989 if (dh->p == NULL)
990 goto err;
991
Hai Shalom021b0b52019-04-10 11:17:58 -0700992 dh->q = get_group5_order();
993 if (!dh->q)
994 goto err;
995
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700996 if (DH_generate_key(dh) != 1)
997 goto err;
998
999 publen = BN_num_bytes(dh->pub_key);
1000 pubkey = wpabuf_alloc(publen);
1001 if (pubkey == NULL)
1002 goto err;
1003 privlen = BN_num_bytes(dh->priv_key);
1004 privkey = wpabuf_alloc(privlen);
1005 if (privkey == NULL)
1006 goto err;
1007
1008 BN_bn2bin(dh->pub_key, wpabuf_put(pubkey, publen));
1009 BN_bn2bin(dh->priv_key, wpabuf_put(privkey, privlen));
1010
1011 *priv = privkey;
1012 *publ = pubkey;
1013 return dh;
1014
1015err:
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001016 wpabuf_clear_free(pubkey);
1017 wpabuf_clear_free(privkey);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001018 DH_free(dh);
1019 return NULL;
Sunil Ravia04bd252022-05-02 22:54:18 -07001020#elif OPENSSL_VERSION_NUMBER >= 0x30000000L
1021 EVP_PKEY *pkey = NULL;
1022 OSSL_PARAM params[2];
1023 size_t pub_len = OSSL_PARAM_UNMODIFIED;
1024 size_t priv_len;
1025 struct wpabuf *pubkey = NULL, *privkey = NULL;
1026 BIGNUM *priv_bn = NULL;
1027 EVP_PKEY_CTX *gctx;
1028
1029 *priv = NULL;
1030 wpabuf_free(*publ);
1031 *publ = NULL;
1032
1033 params[0] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME,
1034 "modp_1536", 0);
1035 params[1] = OSSL_PARAM_construct_end();
1036
1037 gctx = EVP_PKEY_CTX_new_from_name(NULL, "DH", NULL);
1038 if (!gctx ||
1039 EVP_PKEY_keygen_init(gctx) != 1 ||
1040 EVP_PKEY_CTX_set_params(gctx, params) != 1 ||
1041 EVP_PKEY_generate(gctx, &pkey) != 1 ||
1042 EVP_PKEY_get_bn_param(pkey, OSSL_PKEY_PARAM_PRIV_KEY,
1043 &priv_bn) != 1 ||
1044 EVP_PKEY_get_octet_string_param(pkey,
1045 OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY,
1046 NULL, 0, &pub_len) < 0 ||
1047 pub_len == OSSL_PARAM_UNMODIFIED ||
1048 (priv_len = BN_num_bytes(priv_bn)) == 0 ||
1049 !(pubkey = wpabuf_alloc(pub_len)) ||
1050 !(privkey = wpabuf_alloc(priv_len)) ||
1051 EVP_PKEY_get_octet_string_param(pkey,
1052 OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY,
1053 wpabuf_put(pubkey, pub_len),
1054 pub_len, NULL) != 1) {
1055 wpa_printf(MSG_INFO, "OpenSSL: failed: %s",
1056 ERR_error_string(ERR_get_error(), NULL));
1057 wpabuf_free(pubkey);
1058 wpabuf_clear_free(privkey);
1059 EVP_PKEY_free(pkey);
1060 pkey = NULL;
1061 } else {
1062 BN_bn2bin(priv_bn, wpabuf_put(privkey, priv_len));
1063
1064 *priv = privkey;
1065 *publ = pubkey;
1066 }
1067
1068 BN_clear_free(priv_bn);
1069 EVP_PKEY_CTX_free(gctx);
1070 return pkey;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001071#else
1072 DH *dh;
1073 struct wpabuf *pubkey = NULL, *privkey = NULL;
1074 size_t publen, privlen;
Hai Shalom021b0b52019-04-10 11:17:58 -07001075 BIGNUM *p, *g, *q;
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -07001076 const BIGNUM *priv_key = NULL, *pub_key = NULL;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001077
1078 *priv = NULL;
1079 wpabuf_free(*publ);
1080 *publ = NULL;
1081
1082 dh = DH_new();
1083 if (dh == NULL)
1084 return NULL;
1085
1086 g = BN_new();
1087 p = get_group5_prime();
Hai Shalom021b0b52019-04-10 11:17:58 -07001088 q = get_group5_order();
1089 if (!g || BN_set_word(g, 2) != 1 || !p || !q ||
1090 DH_set0_pqg(dh, p, q, g) != 1)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001091 goto err;
1092 p = NULL;
Hai Shalom021b0b52019-04-10 11:17:58 -07001093 q = NULL;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001094 g = NULL;
1095
1096 if (DH_generate_key(dh) != 1)
1097 goto err;
1098
1099 DH_get0_key(dh, &pub_key, &priv_key);
1100 publen = BN_num_bytes(pub_key);
1101 pubkey = wpabuf_alloc(publen);
1102 if (!pubkey)
1103 goto err;
1104 privlen = BN_num_bytes(priv_key);
1105 privkey = wpabuf_alloc(privlen);
1106 if (!privkey)
1107 goto err;
1108
1109 BN_bn2bin(pub_key, wpabuf_put(pubkey, publen));
1110 BN_bn2bin(priv_key, wpabuf_put(privkey, privlen));
1111
1112 *priv = privkey;
1113 *publ = pubkey;
1114 return dh;
1115
1116err:
1117 BN_free(p);
Hai Shalom021b0b52019-04-10 11:17:58 -07001118 BN_free(q);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001119 BN_free(g);
1120 wpabuf_clear_free(pubkey);
1121 wpabuf_clear_free(privkey);
1122 DH_free(dh);
1123 return NULL;
1124#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001125}
1126
1127
Dmitry Shmidt04949592012-07-19 12:16:46 -07001128void * dh5_init_fixed(const struct wpabuf *priv, const struct wpabuf *publ)
1129{
Sunil Ravia04bd252022-05-02 22:54:18 -07001130#if OPENSSL_VERSION_NUMBER < 0x10100000L
Dmitry Shmidt04949592012-07-19 12:16:46 -07001131 DH *dh;
1132
1133 dh = DH_new();
1134 if (dh == NULL)
1135 return NULL;
1136
1137 dh->g = BN_new();
1138 if (dh->g == NULL || BN_set_word(dh->g, 2) != 1)
1139 goto err;
1140
1141 dh->p = get_group5_prime();
1142 if (dh->p == NULL)
1143 goto err;
1144
1145 dh->priv_key = BN_bin2bn(wpabuf_head(priv), wpabuf_len(priv), NULL);
1146 if (dh->priv_key == NULL)
1147 goto err;
1148
1149 dh->pub_key = BN_bin2bn(wpabuf_head(publ), wpabuf_len(publ), NULL);
1150 if (dh->pub_key == NULL)
1151 goto err;
1152
1153 if (DH_generate_key(dh) != 1)
1154 goto err;
1155
1156 return dh;
1157
1158err:
1159 DH_free(dh);
1160 return NULL;
Sunil Ravia04bd252022-05-02 22:54:18 -07001161#elif OPENSSL_VERSION_NUMBER >= 0x30000000L
1162 EVP_PKEY *pkey = NULL;
1163 OSSL_PARAM_BLD *bld;
1164 OSSL_PARAM *params = NULL;
1165 BIGNUM *priv_key, *pub_key;
1166 EVP_PKEY_CTX *fctx;
1167
1168 fctx = EVP_PKEY_CTX_new_from_name(NULL, "DH", NULL);
1169 priv_key = BN_bin2bn(wpabuf_head(priv), wpabuf_len(priv), NULL);
1170 pub_key = BN_bin2bn(wpabuf_head(publ), wpabuf_len(publ), NULL);
1171 bld = OSSL_PARAM_BLD_new();
1172 if (!fctx || !priv_key || !pub_key || !bld ||
1173 OSSL_PARAM_BLD_push_utf8_string(bld, OSSL_PKEY_PARAM_GROUP_NAME,
1174 "modp_1536", 0) != 1 ||
1175 OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PRIV_KEY,
1176 priv_key) != 1 ||
1177 OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PUB_KEY,
1178 pub_key) != 1 ||
1179 !(params = OSSL_PARAM_BLD_to_param(bld)) ||
1180 EVP_PKEY_fromdata_init(fctx) != 1 ||
1181 EVP_PKEY_fromdata(fctx, &pkey, EVP_PKEY_KEYPAIR, params) != 1) {
1182 wpa_printf(MSG_INFO, "OpenSSL: EVP_PKEY_fromdata failed: %s",
1183 ERR_error_string(ERR_get_error(), NULL));
1184 EVP_PKEY_free(pkey);
1185 pkey = NULL;
1186 }
1187
1188 BN_clear_free(priv_key);
1189 BN_free(pub_key);
1190 EVP_PKEY_CTX_free(fctx);
1191 OSSL_PARAM_BLD_free(bld);
1192 OSSL_PARAM_free(params);
1193 return pkey;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001194#else
1195 DH *dh;
1196 BIGNUM *p = NULL, *g, *priv_key = NULL, *pub_key = NULL;
1197
1198 dh = DH_new();
1199 if (dh == NULL)
1200 return NULL;
1201
1202 g = BN_new();
1203 p = get_group5_prime();
1204 if (!g || BN_set_word(g, 2) != 1 || !p ||
1205 DH_set0_pqg(dh, p, NULL, g) != 1)
1206 goto err;
1207 p = NULL;
1208 g = NULL;
1209
1210 priv_key = BN_bin2bn(wpabuf_head(priv), wpabuf_len(priv), NULL);
1211 pub_key = BN_bin2bn(wpabuf_head(publ), wpabuf_len(publ), NULL);
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07001212 if (!priv_key || !pub_key || DH_set0_key(dh, pub_key, priv_key) != 1)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001213 goto err;
1214 pub_key = NULL;
1215 priv_key = NULL;
1216
1217 if (DH_generate_key(dh) != 1)
1218 goto err;
1219
1220 return dh;
1221
1222err:
1223 BN_free(p);
1224 BN_free(g);
1225 BN_free(pub_key);
1226 BN_clear_free(priv_key);
1227 DH_free(dh);
1228 return NULL;
1229#endif
Dmitry Shmidt04949592012-07-19 12:16:46 -07001230}
1231
1232
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001233struct wpabuf * dh5_derive_shared(void *ctx, const struct wpabuf *peer_public,
1234 const struct wpabuf *own_private)
1235{
Sunil Ravia04bd252022-05-02 22:54:18 -07001236#if OPENSSL_VERSION_NUMBER >= 0x30000000L
1237 EVP_PKEY *pkey = ctx;
1238 EVP_PKEY *peer_pub;
1239 size_t len;
1240 struct wpabuf *res = NULL;
1241 EVP_PKEY_CTX *dctx = NULL;
1242
1243 peer_pub = EVP_PKEY_new();
1244 if (!pkey || !peer_pub ||
1245 EVP_PKEY_copy_parameters(peer_pub, pkey) != 1 ||
1246 EVP_PKEY_set1_encoded_public_key(peer_pub, wpabuf_head(peer_public),
1247 wpabuf_len(peer_public)) != 1 ||
1248 !(dctx = EVP_PKEY_CTX_new(pkey, NULL)) ||
1249 EVP_PKEY_derive_init(dctx) != 1 ||
1250 EVP_PKEY_derive_set_peer(dctx, peer_pub) != 1 ||
1251 EVP_PKEY_derive(dctx, NULL, &len) != 1 ||
1252 !(res = wpabuf_alloc(len)) ||
1253 EVP_PKEY_derive(dctx, wpabuf_mhead(res), &len) != 1) {
1254 wpa_printf(MSG_INFO, "OpenSSL: EVP_PKEY_derive failed: %s",
1255 ERR_error_string(ERR_get_error(), NULL));
1256 wpabuf_free(res);
1257 res = NULL;
1258 } else {
1259 wpabuf_put(res, len);
1260 }
1261
1262 EVP_PKEY_free(peer_pub);
1263 EVP_PKEY_CTX_free(dctx);
1264 return res;
1265#else /* OpenSSL version >= 3.0 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001266 BIGNUM *pub_key;
1267 struct wpabuf *res = NULL;
1268 size_t rlen;
1269 DH *dh = ctx;
1270 int keylen;
1271
1272 if (ctx == NULL)
1273 return NULL;
1274
1275 pub_key = BN_bin2bn(wpabuf_head(peer_public), wpabuf_len(peer_public),
1276 NULL);
1277 if (pub_key == NULL)
1278 return NULL;
1279
1280 rlen = DH_size(dh);
1281 res = wpabuf_alloc(rlen);
1282 if (res == NULL)
1283 goto err;
1284
1285 keylen = DH_compute_key(wpabuf_mhead(res), pub_key, dh);
1286 if (keylen < 0)
1287 goto err;
1288 wpabuf_put(res, keylen);
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001289 BN_clear_free(pub_key);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001290
1291 return res;
1292
1293err:
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001294 BN_clear_free(pub_key);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001295 wpabuf_clear_free(res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001296 return NULL;
Sunil Ravia04bd252022-05-02 22:54:18 -07001297#endif /* OpenSSL version >= 3.0 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001298}
1299
1300
1301void dh5_free(void *ctx)
1302{
Sunil Ravia04bd252022-05-02 22:54:18 -07001303#if OPENSSL_VERSION_NUMBER >= 0x30000000L
1304 EVP_PKEY *pkey = ctx;
1305
1306 EVP_PKEY_free(pkey);
1307#else /* OpenSSL version >= 3.0 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001308 DH *dh;
1309 if (ctx == NULL)
1310 return;
1311 dh = ctx;
1312 DH_free(dh);
Sunil Ravia04bd252022-05-02 22:54:18 -07001313#endif /* OpenSSL version >= 3.0 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001314}
Dmitry Shmidt04949592012-07-19 12:16:46 -07001315
1316
1317struct crypto_hash {
Sunil Ravia04bd252022-05-02 22:54:18 -07001318#if OPENSSL_VERSION_NUMBER >= 0x30000000L
1319 EVP_MAC_CTX *ctx;
1320#else /* OpenSSL version >= 3.0 */
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08001321 HMAC_CTX *ctx;
Sunil Ravia04bd252022-05-02 22:54:18 -07001322#endif /* OpenSSL version >= 3.0 */
Dmitry Shmidt04949592012-07-19 12:16:46 -07001323};
1324
1325
1326struct crypto_hash * crypto_hash_init(enum crypto_hash_alg alg, const u8 *key,
1327 size_t key_len)
1328{
Sunil Ravia04bd252022-05-02 22:54:18 -07001329#if OPENSSL_VERSION_NUMBER >= 0x30000000L
1330 struct crypto_hash *ctx;
1331 EVP_MAC *mac;
1332 OSSL_PARAM params[2];
1333 char *a = NULL;
1334
1335 switch (alg) {
1336#ifndef OPENSSL_NO_MD5
1337 case CRYPTO_HASH_ALG_HMAC_MD5:
1338 a = "MD5";
1339 break;
1340#endif /* OPENSSL_NO_MD5 */
1341#ifndef OPENSSL_NO_SHA
1342 case CRYPTO_HASH_ALG_HMAC_SHA1:
1343 a = "SHA1";
1344 break;
1345#endif /* OPENSSL_NO_SHA */
1346#ifndef OPENSSL_NO_SHA256
1347#ifdef CONFIG_SHA256
1348 case CRYPTO_HASH_ALG_HMAC_SHA256:
1349 a = "SHA256";
1350 break;
1351#endif /* CONFIG_SHA256 */
1352#endif /* OPENSSL_NO_SHA256 */
1353 default:
1354 return NULL;
1355 }
1356
1357 mac = EVP_MAC_fetch(NULL, "HMAC", NULL);
1358 if (!mac)
1359 return NULL;
1360
1361 params[0] = OSSL_PARAM_construct_utf8_string("digest", a, 0);
1362 params[1] = OSSL_PARAM_construct_end();
1363
1364 ctx = os_zalloc(sizeof(*ctx));
1365 if (!ctx)
Sunil8cd6f4d2022-06-28 18:40:46 +00001366 goto fail;
Sunil Ravia04bd252022-05-02 22:54:18 -07001367 ctx->ctx = EVP_MAC_CTX_new(mac);
1368 if (!ctx->ctx) {
Sunil Ravia04bd252022-05-02 22:54:18 -07001369 os_free(ctx);
Sunil8cd6f4d2022-06-28 18:40:46 +00001370 ctx = NULL;
1371 goto fail;
Sunil Ravia04bd252022-05-02 22:54:18 -07001372 }
1373
1374 if (EVP_MAC_init(ctx->ctx, key, key_len, params) != 1) {
1375 EVP_MAC_CTX_free(ctx->ctx);
1376 bin_clear_free(ctx, sizeof(*ctx));
Sunil8cd6f4d2022-06-28 18:40:46 +00001377 ctx = NULL;
1378 goto fail;
Sunil Ravia04bd252022-05-02 22:54:18 -07001379 }
1380
Sunil8cd6f4d2022-06-28 18:40:46 +00001381fail:
Sunil Ravia04bd252022-05-02 22:54:18 -07001382 EVP_MAC_free(mac);
1383 return ctx;
1384#else /* OpenSSL version >= 3.0 */
Dmitry Shmidt04949592012-07-19 12:16:46 -07001385 struct crypto_hash *ctx;
1386 const EVP_MD *md;
1387
1388 switch (alg) {
1389#ifndef OPENSSL_NO_MD5
1390 case CRYPTO_HASH_ALG_HMAC_MD5:
1391 md = EVP_md5();
1392 break;
1393#endif /* OPENSSL_NO_MD5 */
1394#ifndef OPENSSL_NO_SHA
1395 case CRYPTO_HASH_ALG_HMAC_SHA1:
1396 md = EVP_sha1();
1397 break;
1398#endif /* OPENSSL_NO_SHA */
1399#ifndef OPENSSL_NO_SHA256
1400#ifdef CONFIG_SHA256
1401 case CRYPTO_HASH_ALG_HMAC_SHA256:
1402 md = EVP_sha256();
1403 break;
1404#endif /* CONFIG_SHA256 */
1405#endif /* OPENSSL_NO_SHA256 */
1406 default:
1407 return NULL;
1408 }
1409
1410 ctx = os_zalloc(sizeof(*ctx));
1411 if (ctx == NULL)
1412 return NULL;
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08001413 ctx->ctx = HMAC_CTX_new();
1414 if (!ctx->ctx) {
1415 os_free(ctx);
1416 return NULL;
1417 }
1418
1419 if (HMAC_Init_ex(ctx->ctx, key, key_len, md, NULL) != 1) {
1420 HMAC_CTX_free(ctx->ctx);
1421 bin_clear_free(ctx, sizeof(*ctx));
1422 return NULL;
1423 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001424
1425 return ctx;
Sunil Ravia04bd252022-05-02 22:54:18 -07001426#endif /* OpenSSL version >= 3.0 */
Dmitry Shmidt04949592012-07-19 12:16:46 -07001427}
1428
1429
1430void crypto_hash_update(struct crypto_hash *ctx, const u8 *data, size_t len)
1431{
1432 if (ctx == NULL)
1433 return;
Sunil Ravia04bd252022-05-02 22:54:18 -07001434#if OPENSSL_VERSION_NUMBER >= 0x30000000L
1435 EVP_MAC_update(ctx->ctx, data, len);
1436#else /* OpenSSL version >= 3.0 */
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08001437 HMAC_Update(ctx->ctx, data, len);
Sunil Ravia04bd252022-05-02 22:54:18 -07001438#endif /* OpenSSL version >= 3.0 */
Dmitry Shmidt04949592012-07-19 12:16:46 -07001439}
1440
1441
1442int crypto_hash_finish(struct crypto_hash *ctx, u8 *mac, size_t *len)
1443{
Sunil Ravia04bd252022-05-02 22:54:18 -07001444#if OPENSSL_VERSION_NUMBER >= 0x30000000L
1445 size_t mdlen;
1446 int res;
1447
1448 if (!ctx)
1449 return -2;
1450
1451 if (!mac || !len) {
1452 EVP_MAC_CTX_free(ctx->ctx);
1453 bin_clear_free(ctx, sizeof(*ctx));
1454 return 0;
1455 }
1456
1457 res = EVP_MAC_final(ctx->ctx, NULL, &mdlen, 0);
1458 if (res != 1) {
1459 EVP_MAC_CTX_free(ctx->ctx);
1460 bin_clear_free(ctx, sizeof(*ctx));
1461 return -1;
1462 }
1463 res = EVP_MAC_final(ctx->ctx, mac, &mdlen, mdlen);
1464 EVP_MAC_CTX_free(ctx->ctx);
1465 bin_clear_free(ctx, sizeof(*ctx));
1466
1467 if (TEST_FAIL())
1468 return -1;
1469
1470 if (res == 1) {
1471 *len = mdlen;
1472 return 0;
1473 }
1474
1475 return -1;
1476#else /* OpenSSL version >= 3.0 */
Dmitry Shmidt04949592012-07-19 12:16:46 -07001477 unsigned int mdlen;
1478 int res;
1479
1480 if (ctx == NULL)
1481 return -2;
1482
1483 if (mac == NULL || len == NULL) {
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08001484 HMAC_CTX_free(ctx->ctx);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001485 bin_clear_free(ctx, sizeof(*ctx));
Dmitry Shmidt04949592012-07-19 12:16:46 -07001486 return 0;
1487 }
1488
1489 mdlen = *len;
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08001490 res = HMAC_Final(ctx->ctx, mac, &mdlen);
1491 HMAC_CTX_free(ctx->ctx);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001492 bin_clear_free(ctx, sizeof(*ctx));
Dmitry Shmidt04949592012-07-19 12:16:46 -07001493
Hai Shalom5f92bc92019-04-18 11:54:11 -07001494 if (TEST_FAIL())
1495 return -1;
1496
Dmitry Shmidt04949592012-07-19 12:16:46 -07001497 if (res == 1) {
1498 *len = mdlen;
1499 return 0;
1500 }
1501
1502 return -1;
Sunil Ravia04bd252022-05-02 22:54:18 -07001503#endif /* OpenSSL version >= 3.0 */
Dmitry Shmidt04949592012-07-19 12:16:46 -07001504}
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001505
1506
Sunil Ravia04bd252022-05-02 22:54:18 -07001507#if OPENSSL_VERSION_NUMBER >= 0x30000000L
1508
1509static int openssl_hmac_vector(char *digest, const u8 *key,
1510 size_t key_len, size_t num_elem,
1511 const u8 *addr[], const size_t *len, u8 *mac,
1512 unsigned int mdlen)
1513{
1514 EVP_MAC *hmac;
1515 OSSL_PARAM params[2];
1516 EVP_MAC_CTX *ctx;
1517 size_t i, mlen;
1518 int res;
1519
1520 if (TEST_FAIL())
1521 return -1;
1522
1523 hmac = EVP_MAC_fetch(NULL, "HMAC", NULL);
1524 if (!hmac)
1525 return -1;
1526
1527 params[0] = OSSL_PARAM_construct_utf8_string("digest", digest, 0);
1528 params[1] = OSSL_PARAM_construct_end();
1529
1530 ctx = EVP_MAC_CTX_new(hmac);
1531 EVP_MAC_free(hmac);
1532 if (!ctx)
1533 return -1;
1534
1535 if (EVP_MAC_init(ctx, key, key_len, params) != 1)
1536 goto fail;
1537
1538 for (i = 0; i < num_elem; i++) {
1539 if (EVP_MAC_update(ctx, addr[i], len[i]) != 1)
1540 goto fail;
1541 }
1542
1543 res = EVP_MAC_final(ctx, mac, &mlen, mdlen);
1544 EVP_MAC_CTX_free(ctx);
1545
1546 return res == 1 ? 0 : -1;
1547fail:
1548 EVP_MAC_CTX_free(ctx);
1549 return -1;
1550}
1551
1552
1553#ifndef CONFIG_FIPS
1554
1555int hmac_md5_vector(const u8 *key, size_t key_len, size_t num_elem,
1556 const u8 *addr[], const size_t *len, u8 *mac)
1557{
1558 return openssl_hmac_vector("MD5", key ,key_len, num_elem, addr, len,
1559 mac, 16);
1560}
1561
1562
1563int hmac_md5(const u8 *key, size_t key_len, const u8 *data, size_t data_len,
1564 u8 *mac)
1565{
1566 return hmac_md5_vector(key, key_len, 1, &data, &data_len, mac);
1567}
1568
1569#endif /* CONFIG_FIPS */
1570
1571
1572int hmac_sha1_vector(const u8 *key, size_t key_len, size_t num_elem,
1573 const u8 *addr[], const size_t *len, u8 *mac)
1574{
1575 return openssl_hmac_vector("SHA1", key, key_len, num_elem, addr,
1576 len, mac, 20);
1577}
1578
1579
1580int hmac_sha1(const u8 *key, size_t key_len, const u8 *data, size_t data_len,
1581 u8 *mac)
1582{
1583 return hmac_sha1_vector(key, key_len, 1, &data, &data_len, mac);
1584}
1585
1586
1587#ifdef CONFIG_SHA256
1588
1589int hmac_sha256_vector(const u8 *key, size_t key_len, size_t num_elem,
1590 const u8 *addr[], const size_t *len, u8 *mac)
1591{
1592 return openssl_hmac_vector("SHA256", key, key_len, num_elem, addr,
1593 len, mac, 32);
1594}
1595
1596
1597int hmac_sha256(const u8 *key, size_t key_len, const u8 *data,
1598 size_t data_len, u8 *mac)
1599{
1600 return hmac_sha256_vector(key, key_len, 1, &data, &data_len, mac);
1601}
1602
1603#endif /* CONFIG_SHA256 */
1604
1605
1606#ifdef CONFIG_SHA384
1607
1608int hmac_sha384_vector(const u8 *key, size_t key_len, size_t num_elem,
1609 const u8 *addr[], const size_t *len, u8 *mac)
1610{
1611 return openssl_hmac_vector("SHA384", key, key_len, num_elem, addr,
1612 len, mac, 48);
1613}
1614
1615
1616int hmac_sha384(const u8 *key, size_t key_len, const u8 *data,
1617 size_t data_len, u8 *mac)
1618{
1619 return hmac_sha384_vector(key, key_len, 1, &data, &data_len, mac);
1620}
1621
1622#endif /* CONFIG_SHA384 */
1623
1624
1625#ifdef CONFIG_SHA512
1626
1627int hmac_sha512_vector(const u8 *key, size_t key_len, size_t num_elem,
1628 const u8 *addr[], const size_t *len, u8 *mac)
1629{
1630 return openssl_hmac_vector("SHA512", key, key_len, num_elem, addr,
1631 len, mac, 64);
1632}
1633
1634
1635int hmac_sha512(const u8 *key, size_t key_len, const u8 *data,
1636 size_t data_len, u8 *mac)
1637{
1638 return hmac_sha512_vector(key, key_len, 1, &data, &data_len, mac);
1639}
1640
1641#endif /* CONFIG_SHA512 */
1642
1643#else /* OpenSSL version >= 3.0 */
1644
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001645static int openssl_hmac_vector(const EVP_MD *type, const u8 *key,
1646 size_t key_len, size_t num_elem,
1647 const u8 *addr[], const size_t *len, u8 *mac,
1648 unsigned int mdlen)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001649{
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08001650 HMAC_CTX *ctx;
1651 size_t i;
1652 int res;
1653
1654 if (TEST_FAIL())
1655 return -1;
1656
1657 ctx = HMAC_CTX_new();
1658 if (!ctx)
1659 return -1;
1660 res = HMAC_Init_ex(ctx, key, key_len, type, NULL);
1661 if (res != 1)
1662 goto done;
1663
1664 for (i = 0; i < num_elem; i++)
1665 HMAC_Update(ctx, addr[i], len[i]);
1666
1667 res = HMAC_Final(ctx, mac, &mdlen);
1668done:
1669 HMAC_CTX_free(ctx);
1670
1671 return res == 1 ? 0 : -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001672}
1673
1674
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001675#ifndef CONFIG_FIPS
1676
1677int hmac_md5_vector(const u8 *key, size_t key_len, size_t num_elem,
1678 const u8 *addr[], const size_t *len, u8 *mac)
1679{
1680 return openssl_hmac_vector(EVP_md5(), key ,key_len, num_elem, addr, len,
1681 mac, 16);
1682}
1683
1684
1685int hmac_md5(const u8 *key, size_t key_len, const u8 *data, size_t data_len,
1686 u8 *mac)
1687{
1688 return hmac_md5_vector(key, key_len, 1, &data, &data_len, mac);
1689}
1690
1691#endif /* CONFIG_FIPS */
1692
1693
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001694int hmac_sha1_vector(const u8 *key, size_t key_len, size_t num_elem,
1695 const u8 *addr[], const size_t *len, u8 *mac)
1696{
1697 return openssl_hmac_vector(EVP_sha1(), key, key_len, num_elem, addr,
1698 len, mac, 20);
1699}
1700
1701
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001702int hmac_sha1(const u8 *key, size_t key_len, const u8 *data, size_t data_len,
1703 u8 *mac)
1704{
1705 return hmac_sha1_vector(key, key_len, 1, &data, &data_len, mac);
1706}
1707
1708
1709#ifdef CONFIG_SHA256
1710
1711int hmac_sha256_vector(const u8 *key, size_t key_len, size_t num_elem,
1712 const u8 *addr[], const size_t *len, u8 *mac)
1713{
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001714 return openssl_hmac_vector(EVP_sha256(), key, key_len, num_elem, addr,
1715 len, mac, 32);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001716}
1717
1718
1719int hmac_sha256(const u8 *key, size_t key_len, const u8 *data,
1720 size_t data_len, u8 *mac)
1721{
1722 return hmac_sha256_vector(key, key_len, 1, &data, &data_len, mac);
1723}
1724
1725#endif /* CONFIG_SHA256 */
1726
1727
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001728#ifdef CONFIG_SHA384
1729
1730int hmac_sha384_vector(const u8 *key, size_t key_len, size_t num_elem,
1731 const u8 *addr[], const size_t *len, u8 *mac)
1732{
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001733 return openssl_hmac_vector(EVP_sha384(), key, key_len, num_elem, addr,
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08001734 len, mac, 48);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001735}
1736
1737
1738int hmac_sha384(const u8 *key, size_t key_len, const u8 *data,
1739 size_t data_len, u8 *mac)
1740{
1741 return hmac_sha384_vector(key, key_len, 1, &data, &data_len, mac);
1742}
1743
1744#endif /* CONFIG_SHA384 */
1745
1746
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001747#ifdef CONFIG_SHA512
1748
1749int hmac_sha512_vector(const u8 *key, size_t key_len, size_t num_elem,
1750 const u8 *addr[], const size_t *len, u8 *mac)
1751{
1752 return openssl_hmac_vector(EVP_sha512(), key, key_len, num_elem, addr,
1753 len, mac, 64);
1754}
1755
1756
1757int hmac_sha512(const u8 *key, size_t key_len, const u8 *data,
1758 size_t data_len, u8 *mac)
1759{
1760 return hmac_sha512_vector(key, key_len, 1, &data, &data_len, mac);
1761}
1762
1763#endif /* CONFIG_SHA512 */
1764
Sunil Ravia04bd252022-05-02 22:54:18 -07001765#endif /* OpenSSL version >= 3.0 */
1766
1767
1768int pbkdf2_sha1(const char *passphrase, const u8 *ssid, size_t ssid_len,
1769 int iterations, u8 *buf, size_t buflen)
1770{
1771 if (PKCS5_PBKDF2_HMAC_SHA1(passphrase, os_strlen(passphrase), ssid,
1772 ssid_len, iterations, buflen, buf) != 1)
1773 return -1;
1774 return 0;
1775}
1776
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001777
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001778int crypto_get_random(void *buf, size_t len)
1779{
1780 if (RAND_bytes(buf, len) != 1)
1781 return -1;
1782 return 0;
1783}
1784
1785
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001786int omac1_aes_vector(const u8 *key, size_t key_len, size_t num_elem,
1787 const u8 *addr[], const size_t *len, u8 *mac)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001788{
Sunil Ravia04bd252022-05-02 22:54:18 -07001789#if OPENSSL_VERSION_NUMBER >= 0x30000000L
1790 EVP_MAC_CTX *ctx = NULL;
1791 EVP_MAC *emac;
1792 int ret = -1;
1793 size_t outlen, i;
1794 OSSL_PARAM params[2];
1795 char *cipher = NULL;
1796
1797 if (TEST_FAIL())
1798 return -1;
1799
1800 emac = EVP_MAC_fetch(NULL, "CMAC", NULL);
1801
1802 if (key_len == 32)
1803 cipher = "aes-256-cbc";
1804 else if (key_len == 24)
1805 cipher = "aes-192-cbc";
1806 else if (key_len == 16)
1807 cipher = "aes-128-cbc";
1808
1809 params[0] = OSSL_PARAM_construct_utf8_string("cipher", cipher, 0);
1810 params[1] = OSSL_PARAM_construct_end();
1811
1812 if (!emac || !cipher ||
1813 !(ctx = EVP_MAC_CTX_new(emac)) ||
1814 EVP_MAC_init(ctx, key, key_len, params) != 1)
1815 goto fail;
1816
1817 for (i = 0; i < num_elem; i++) {
1818 if (!EVP_MAC_update(ctx, addr[i], len[i]))
1819 goto fail;
1820 }
1821 if (EVP_MAC_final(ctx, mac, &outlen, 16) != 1 || outlen != 16)
1822 goto fail;
1823
1824 ret = 0;
1825fail:
1826 EVP_MAC_CTX_free(ctx);
1827 return ret;
1828#else /* OpenSSL version >= 3.0 */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001829 CMAC_CTX *ctx;
1830 int ret = -1;
1831 size_t outlen, i;
1832
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001833 if (TEST_FAIL())
1834 return -1;
1835
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001836 ctx = CMAC_CTX_new();
1837 if (ctx == NULL)
1838 return -1;
1839
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001840 if (key_len == 32) {
1841 if (!CMAC_Init(ctx, key, 32, EVP_aes_256_cbc(), NULL))
1842 goto fail;
Sunil Ravia04bd252022-05-02 22:54:18 -07001843 } else if (key_len == 24) {
1844 if (!CMAC_Init(ctx, key, 24, EVP_aes_192_cbc(), NULL))
1845 goto fail;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001846 } else if (key_len == 16) {
1847 if (!CMAC_Init(ctx, key, 16, EVP_aes_128_cbc(), NULL))
1848 goto fail;
1849 } else {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001850 goto fail;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001851 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001852 for (i = 0; i < num_elem; i++) {
1853 if (!CMAC_Update(ctx, addr[i], len[i]))
1854 goto fail;
1855 }
1856 if (!CMAC_Final(ctx, mac, &outlen) || outlen != 16)
1857 goto fail;
1858
1859 ret = 0;
1860fail:
1861 CMAC_CTX_free(ctx);
1862 return ret;
Sunil Ravia04bd252022-05-02 22:54:18 -07001863#endif /* OpenSSL version >= 3.0 */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001864}
1865
1866
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001867int omac1_aes_128_vector(const u8 *key, size_t num_elem,
1868 const u8 *addr[], const size_t *len, u8 *mac)
1869{
1870 return omac1_aes_vector(key, 16, num_elem, addr, len, mac);
1871}
1872
1873
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001874int omac1_aes_128(const u8 *key, const u8 *data, size_t data_len, u8 *mac)
1875{
1876 return omac1_aes_128_vector(key, 1, &data, &data_len, mac);
1877}
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001878
1879
1880int omac1_aes_256(const u8 *key, const u8 *data, size_t data_len, u8 *mac)
1881{
1882 return omac1_aes_vector(key, 32, 1, &data, &data_len, mac);
1883}
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001884
1885
1886struct crypto_bignum * crypto_bignum_init(void)
1887{
Dmitry Shmidte4663042016-04-04 10:07:49 -07001888 if (TEST_FAIL())
1889 return NULL;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001890 return (struct crypto_bignum *) BN_new();
1891}
1892
1893
1894struct crypto_bignum * crypto_bignum_init_set(const u8 *buf, size_t len)
1895{
Dmitry Shmidte4663042016-04-04 10:07:49 -07001896 BIGNUM *bn;
1897
1898 if (TEST_FAIL())
1899 return NULL;
1900
1901 bn = BN_bin2bn(buf, len, NULL);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001902 return (struct crypto_bignum *) bn;
1903}
1904
1905
Hai Shalomc3565922019-10-28 11:58:20 -07001906struct crypto_bignum * crypto_bignum_init_uint(unsigned int val)
1907{
1908 BIGNUM *bn;
1909
1910 if (TEST_FAIL())
1911 return NULL;
1912
1913 bn = BN_new();
1914 if (!bn)
1915 return NULL;
1916 if (BN_set_word(bn, val) != 1) {
1917 BN_free(bn);
1918 return NULL;
1919 }
1920 return (struct crypto_bignum *) bn;
1921}
1922
1923
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001924void crypto_bignum_deinit(struct crypto_bignum *n, int clear)
1925{
1926 if (clear)
1927 BN_clear_free((BIGNUM *) n);
1928 else
1929 BN_free((BIGNUM *) n);
1930}
1931
1932
1933int crypto_bignum_to_bin(const struct crypto_bignum *a,
1934 u8 *buf, size_t buflen, size_t padlen)
1935{
1936 int num_bytes, offset;
1937
Dmitry Shmidte4663042016-04-04 10:07:49 -07001938 if (TEST_FAIL())
1939 return -1;
1940
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001941 if (padlen > buflen)
1942 return -1;
1943
Hai Shalomc3565922019-10-28 11:58:20 -07001944 if (padlen) {
Hai Shalom81f62d82019-07-22 12:10:00 -07001945#ifdef OPENSSL_IS_BORINGSSL
Hai Shalomc3565922019-10-28 11:58:20 -07001946 if (BN_bn2bin_padded(buf, padlen, (const BIGNUM *) a) == 0)
1947 return -1;
1948 return padlen;
Hai Shalom81f62d82019-07-22 12:10:00 -07001949#else /* OPENSSL_IS_BORINGSSL */
1950#if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)
Hai Shalomc3565922019-10-28 11:58:20 -07001951 return BN_bn2binpad((const BIGNUM *) a, buf, padlen);
1952#endif
1953#endif
1954 }
1955
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001956 num_bytes = BN_num_bytes((const BIGNUM *) a);
1957 if ((size_t) num_bytes > buflen)
1958 return -1;
1959 if (padlen > (size_t) num_bytes)
1960 offset = padlen - num_bytes;
1961 else
1962 offset = 0;
1963
1964 os_memset(buf, 0, offset);
1965 BN_bn2bin((const BIGNUM *) a, buf + offset);
1966
1967 return num_bytes + offset;
1968}
1969
1970
Roshan Pius3a1667e2018-07-03 15:17:14 -07001971int crypto_bignum_rand(struct crypto_bignum *r, const struct crypto_bignum *m)
1972{
Hai Shalom5f92bc92019-04-18 11:54:11 -07001973 if (TEST_FAIL())
1974 return -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07001975 return BN_rand_range((BIGNUM *) r, (const BIGNUM *) m) == 1 ? 0 : -1;
1976}
1977
1978
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001979int crypto_bignum_add(const struct crypto_bignum *a,
1980 const struct crypto_bignum *b,
1981 struct crypto_bignum *c)
1982{
1983 return BN_add((BIGNUM *) c, (const BIGNUM *) a, (const BIGNUM *) b) ?
1984 0 : -1;
1985}
1986
1987
1988int crypto_bignum_mod(const struct crypto_bignum *a,
1989 const struct crypto_bignum *b,
1990 struct crypto_bignum *c)
1991{
1992 int res;
1993 BN_CTX *bnctx;
1994
1995 bnctx = BN_CTX_new();
1996 if (bnctx == NULL)
1997 return -1;
1998 res = BN_mod((BIGNUM *) c, (const BIGNUM *) a, (const BIGNUM *) b,
1999 bnctx);
2000 BN_CTX_free(bnctx);
2001
2002 return res ? 0 : -1;
2003}
2004
2005
2006int crypto_bignum_exptmod(const struct crypto_bignum *a,
2007 const struct crypto_bignum *b,
2008 const struct crypto_bignum *c,
2009 struct crypto_bignum *d)
2010{
2011 int res;
2012 BN_CTX *bnctx;
2013
Dmitry Shmidte4663042016-04-04 10:07:49 -07002014 if (TEST_FAIL())
2015 return -1;
2016
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002017 bnctx = BN_CTX_new();
2018 if (bnctx == NULL)
2019 return -1;
Hai Shalom021b0b52019-04-10 11:17:58 -07002020 res = BN_mod_exp_mont_consttime((BIGNUM *) d, (const BIGNUM *) a,
2021 (const BIGNUM *) b, (const BIGNUM *) c,
2022 bnctx, NULL);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002023 BN_CTX_free(bnctx);
2024
2025 return res ? 0 : -1;
2026}
2027
2028
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002029int crypto_bignum_inverse(const struct crypto_bignum *a,
2030 const struct crypto_bignum *b,
2031 struct crypto_bignum *c)
2032{
2033 BIGNUM *res;
2034 BN_CTX *bnctx;
2035
Dmitry Shmidte4663042016-04-04 10:07:49 -07002036 if (TEST_FAIL())
2037 return -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002038 bnctx = BN_CTX_new();
2039 if (bnctx == NULL)
2040 return -1;
Hai Shalom021b0b52019-04-10 11:17:58 -07002041#ifdef OPENSSL_IS_BORINGSSL
2042 /* TODO: use BN_mod_inverse_blinded() ? */
2043#else /* OPENSSL_IS_BORINGSSL */
2044 BN_set_flags((BIGNUM *) a, BN_FLG_CONSTTIME);
2045#endif /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002046 res = BN_mod_inverse((BIGNUM *) c, (const BIGNUM *) a,
2047 (const BIGNUM *) b, bnctx);
2048 BN_CTX_free(bnctx);
2049
2050 return res ? 0 : -1;
2051}
2052
2053
2054int crypto_bignum_sub(const struct crypto_bignum *a,
2055 const struct crypto_bignum *b,
2056 struct crypto_bignum *c)
2057{
Dmitry Shmidte4663042016-04-04 10:07:49 -07002058 if (TEST_FAIL())
2059 return -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002060 return BN_sub((BIGNUM *) c, (const BIGNUM *) a, (const BIGNUM *) b) ?
2061 0 : -1;
2062}
2063
2064
2065int crypto_bignum_div(const struct crypto_bignum *a,
2066 const struct crypto_bignum *b,
2067 struct crypto_bignum *c)
2068{
2069 int res;
2070
2071 BN_CTX *bnctx;
2072
Dmitry Shmidte4663042016-04-04 10:07:49 -07002073 if (TEST_FAIL())
2074 return -1;
2075
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002076 bnctx = BN_CTX_new();
2077 if (bnctx == NULL)
2078 return -1;
Hai Shalom021b0b52019-04-10 11:17:58 -07002079#ifndef OPENSSL_IS_BORINGSSL
2080 BN_set_flags((BIGNUM *) a, BN_FLG_CONSTTIME);
2081#endif /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002082 res = BN_div((BIGNUM *) c, NULL, (const BIGNUM *) a,
2083 (const BIGNUM *) b, bnctx);
2084 BN_CTX_free(bnctx);
2085
2086 return res ? 0 : -1;
2087}
2088
2089
Hai Shalomc3565922019-10-28 11:58:20 -07002090int crypto_bignum_addmod(const struct crypto_bignum *a,
2091 const struct crypto_bignum *b,
2092 const struct crypto_bignum *c,
2093 struct crypto_bignum *d)
2094{
2095 int res;
2096 BN_CTX *bnctx;
2097
2098 if (TEST_FAIL())
2099 return -1;
2100
2101 bnctx = BN_CTX_new();
2102 if (!bnctx)
2103 return -1;
2104 res = BN_mod_add((BIGNUM *) d, (const BIGNUM *) a, (const BIGNUM *) b,
2105 (const BIGNUM *) c, bnctx);
2106 BN_CTX_free(bnctx);
2107
2108 return res ? 0 : -1;
2109}
2110
2111
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002112int crypto_bignum_mulmod(const struct crypto_bignum *a,
2113 const struct crypto_bignum *b,
2114 const struct crypto_bignum *c,
2115 struct crypto_bignum *d)
2116{
2117 int res;
2118
2119 BN_CTX *bnctx;
2120
Dmitry Shmidte4663042016-04-04 10:07:49 -07002121 if (TEST_FAIL())
2122 return -1;
2123
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002124 bnctx = BN_CTX_new();
2125 if (bnctx == NULL)
2126 return -1;
2127 res = BN_mod_mul((BIGNUM *) d, (const BIGNUM *) a, (const BIGNUM *) b,
2128 (const BIGNUM *) c, bnctx);
2129 BN_CTX_free(bnctx);
2130
2131 return res ? 0 : -1;
2132}
2133
2134
Hai Shalomc3565922019-10-28 11:58:20 -07002135int crypto_bignum_sqrmod(const struct crypto_bignum *a,
2136 const struct crypto_bignum *b,
2137 struct crypto_bignum *c)
2138{
2139 int res;
2140 BN_CTX *bnctx;
2141
2142 if (TEST_FAIL())
2143 return -1;
2144
2145 bnctx = BN_CTX_new();
2146 if (!bnctx)
2147 return -1;
2148 res = BN_mod_sqr((BIGNUM *) c, (const BIGNUM *) a, (const BIGNUM *) b,
2149 bnctx);
2150 BN_CTX_free(bnctx);
2151
2152 return res ? 0 : -1;
2153}
2154
2155
Roshan Pius3a1667e2018-07-03 15:17:14 -07002156int crypto_bignum_rshift(const struct crypto_bignum *a, int n,
2157 struct crypto_bignum *r)
2158{
2159 /* Note: BN_rshift() does not modify the first argument even though it
2160 * has not been marked const. */
2161 return BN_rshift((BIGNUM *) a, (BIGNUM *) r, n) == 1 ? 0 : -1;
2162}
2163
2164
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002165int crypto_bignum_cmp(const struct crypto_bignum *a,
2166 const struct crypto_bignum *b)
2167{
2168 return BN_cmp((const BIGNUM *) a, (const BIGNUM *) b);
2169}
2170
2171
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002172int crypto_bignum_is_zero(const struct crypto_bignum *a)
2173{
2174 return BN_is_zero((const BIGNUM *) a);
2175}
2176
2177
2178int crypto_bignum_is_one(const struct crypto_bignum *a)
2179{
2180 return BN_is_one((const BIGNUM *) a);
2181}
2182
2183
Roshan Pius3a1667e2018-07-03 15:17:14 -07002184int crypto_bignum_is_odd(const struct crypto_bignum *a)
2185{
2186 return BN_is_odd((const BIGNUM *) a);
2187}
2188
2189
Dmitry Shmidt41712582015-06-29 11:02:15 -07002190int crypto_bignum_legendre(const struct crypto_bignum *a,
2191 const struct crypto_bignum *p)
2192{
2193 BN_CTX *bnctx;
2194 BIGNUM *exp = NULL, *tmp = NULL;
2195 int res = -2;
Hai Shalom021b0b52019-04-10 11:17:58 -07002196 unsigned int mask;
Dmitry Shmidt41712582015-06-29 11:02:15 -07002197
Dmitry Shmidte4663042016-04-04 10:07:49 -07002198 if (TEST_FAIL())
2199 return -2;
2200
Dmitry Shmidt41712582015-06-29 11:02:15 -07002201 bnctx = BN_CTX_new();
2202 if (bnctx == NULL)
2203 return -2;
2204
2205 exp = BN_new();
2206 tmp = BN_new();
2207 if (!exp || !tmp ||
2208 /* exp = (p-1) / 2 */
2209 !BN_sub(exp, (const BIGNUM *) p, BN_value_one()) ||
2210 !BN_rshift1(exp, exp) ||
Hai Shalom021b0b52019-04-10 11:17:58 -07002211 !BN_mod_exp_mont_consttime(tmp, (const BIGNUM *) a, exp,
2212 (const BIGNUM *) p, bnctx, NULL))
Dmitry Shmidt41712582015-06-29 11:02:15 -07002213 goto fail;
2214
Hai Shalom021b0b52019-04-10 11:17:58 -07002215 /* Return 1 if tmp == 1, 0 if tmp == 0, or -1 otherwise. Need to use
2216 * constant time selection to avoid branches here. */
2217 res = -1;
2218 mask = const_time_eq(BN_is_word(tmp, 1), 1);
2219 res = const_time_select_int(mask, 1, res);
2220 mask = const_time_eq(BN_is_zero(tmp), 1);
2221 res = const_time_select_int(mask, 0, res);
Dmitry Shmidt41712582015-06-29 11:02:15 -07002222
2223fail:
2224 BN_clear_free(tmp);
2225 BN_clear_free(exp);
2226 BN_CTX_free(bnctx);
2227 return res;
2228}
2229
2230
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002231#ifdef CONFIG_ECC
2232
2233struct crypto_ec {
2234 EC_GROUP *group;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002235 int nid;
Sunil8cd6f4d2022-06-28 18:40:46 +00002236 int iana_group;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002237 BN_CTX *bnctx;
2238 BIGNUM *prime;
2239 BIGNUM *order;
Dmitry Shmidt41712582015-06-29 11:02:15 -07002240 BIGNUM *a;
2241 BIGNUM *b;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002242};
2243
Hai Shaloma20dcd72022-02-04 13:43:00 -08002244
2245static int crypto_ec_group_2_nid(int group)
2246{
2247 /* Map from IANA registry for IKE D-H groups to OpenSSL NID */
2248 switch (group) {
2249 case 19:
2250 return NID_X9_62_prime256v1;
2251 case 20:
2252 return NID_secp384r1;
2253 case 21:
2254 return NID_secp521r1;
2255 case 25:
2256 return NID_X9_62_prime192v1;
2257 case 26:
2258 return NID_secp224r1;
2259#ifdef NID_brainpoolP224r1
2260 case 27:
2261 return NID_brainpoolP224r1;
2262#endif /* NID_brainpoolP224r1 */
2263#ifdef NID_brainpoolP256r1
2264 case 28:
2265 return NID_brainpoolP256r1;
2266#endif /* NID_brainpoolP256r1 */
2267#ifdef NID_brainpoolP384r1
2268 case 29:
2269 return NID_brainpoolP384r1;
2270#endif /* NID_brainpoolP384r1 */
2271#ifdef NID_brainpoolP512r1
2272 case 30:
2273 return NID_brainpoolP512r1;
2274#endif /* NID_brainpoolP512r1 */
2275 default:
2276 return -1;
2277 }
2278}
2279
2280
Sunil8cd6f4d2022-06-28 18:40:46 +00002281#if OPENSSL_VERSION_NUMBER >= 0x30000000L
2282static const char * crypto_ec_group_2_name(int group)
2283{
2284 /* Map from IANA registry for IKE D-H groups to OpenSSL group name */
2285 switch (group) {
2286 case 19:
2287 return "prime256v1";
2288 case 20:
2289 return "secp384r1";
2290 case 21:
2291 return "secp521r1";
2292 case 25:
2293 return "prime192v1";
2294 case 26:
2295 return "secp224r1";
2296#ifdef NID_brainpoolP224r1
2297 case 27:
2298 return "brainpoolP224r1";
2299#endif /* NID_brainpoolP224r1 */
2300#ifdef NID_brainpoolP256r1
2301 case 28:
2302 return "brainpoolP256r1";
2303#endif /* NID_brainpoolP256r1 */
2304#ifdef NID_brainpoolP384r1
2305 case 29:
2306 return "brainpoolP384r1";
2307#endif /* NID_brainpoolP384r1 */
2308#ifdef NID_brainpoolP512r1
2309 case 30:
2310 return "brainpoolP512r1";
2311#endif /* NID_brainpoolP512r1 */
2312 default:
2313 return NULL;
2314 }
2315}
2316#endif /* OpenSSL version >= 3.0 */
2317
2318
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002319struct crypto_ec * crypto_ec_init(int group)
2320{
2321 struct crypto_ec *e;
2322 int nid;
2323
Hai Shaloma20dcd72022-02-04 13:43:00 -08002324 nid = crypto_ec_group_2_nid(group);
2325 if (nid < 0)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002326 return NULL;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002327
2328 e = os_zalloc(sizeof(*e));
2329 if (e == NULL)
2330 return NULL;
2331
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002332 e->nid = nid;
Sunil8cd6f4d2022-06-28 18:40:46 +00002333 e->iana_group = group;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002334 e->bnctx = BN_CTX_new();
2335 e->group = EC_GROUP_new_by_curve_name(nid);
2336 e->prime = BN_new();
2337 e->order = BN_new();
Dmitry Shmidt41712582015-06-29 11:02:15 -07002338 e->a = BN_new();
2339 e->b = BN_new();
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002340 if (e->group == NULL || e->bnctx == NULL || e->prime == NULL ||
Dmitry Shmidt41712582015-06-29 11:02:15 -07002341 e->order == NULL || e->a == NULL || e->b == NULL ||
Sunil Ravia04bd252022-05-02 22:54:18 -07002342 !EC_GROUP_get_curve(e->group, e->prime, e->a, e->b, e->bnctx) ||
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002343 !EC_GROUP_get_order(e->group, e->order, e->bnctx)) {
2344 crypto_ec_deinit(e);
2345 e = NULL;
2346 }
2347
2348 return e;
2349}
2350
2351
2352void crypto_ec_deinit(struct crypto_ec *e)
2353{
2354 if (e == NULL)
2355 return;
Dmitry Shmidt41712582015-06-29 11:02:15 -07002356 BN_clear_free(e->b);
2357 BN_clear_free(e->a);
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07002358 BN_clear_free(e->order);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002359 BN_clear_free(e->prime);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002360 EC_GROUP_free(e->group);
2361 BN_CTX_free(e->bnctx);
2362 os_free(e);
2363}
2364
2365
2366struct crypto_ec_point * crypto_ec_point_init(struct crypto_ec *e)
2367{
Dmitry Shmidte4663042016-04-04 10:07:49 -07002368 if (TEST_FAIL())
2369 return NULL;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002370 if (e == NULL)
2371 return NULL;
2372 return (struct crypto_ec_point *) EC_POINT_new(e->group);
2373}
2374
2375
2376size_t crypto_ec_prime_len(struct crypto_ec *e)
2377{
2378 return BN_num_bytes(e->prime);
2379}
2380
2381
2382size_t crypto_ec_prime_len_bits(struct crypto_ec *e)
2383{
2384 return BN_num_bits(e->prime);
2385}
2386
2387
Roshan Pius3a1667e2018-07-03 15:17:14 -07002388size_t crypto_ec_order_len(struct crypto_ec *e)
2389{
2390 return BN_num_bytes(e->order);
2391}
2392
2393
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002394const struct crypto_bignum * crypto_ec_get_prime(struct crypto_ec *e)
2395{
2396 return (const struct crypto_bignum *) e->prime;
2397}
2398
2399
2400const struct crypto_bignum * crypto_ec_get_order(struct crypto_ec *e)
2401{
2402 return (const struct crypto_bignum *) e->order;
2403}
2404
2405
Hai Shalomc3565922019-10-28 11:58:20 -07002406const struct crypto_bignum * crypto_ec_get_a(struct crypto_ec *e)
2407{
2408 return (const struct crypto_bignum *) e->a;
2409}
2410
2411
2412const struct crypto_bignum * crypto_ec_get_b(struct crypto_ec *e)
2413{
2414 return (const struct crypto_bignum *) e->b;
2415}
2416
2417
Hai Shaloma20dcd72022-02-04 13:43:00 -08002418const struct crypto_ec_point * crypto_ec_get_generator(struct crypto_ec *e)
2419{
2420 return (const struct crypto_ec_point *)
2421 EC_GROUP_get0_generator(e->group);
2422}
2423
2424
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002425void crypto_ec_point_deinit(struct crypto_ec_point *p, int clear)
2426{
2427 if (clear)
2428 EC_POINT_clear_free((EC_POINT *) p);
2429 else
2430 EC_POINT_free((EC_POINT *) p);
2431}
2432
2433
Roshan Pius3a1667e2018-07-03 15:17:14 -07002434int crypto_ec_point_x(struct crypto_ec *e, const struct crypto_ec_point *p,
2435 struct crypto_bignum *x)
2436{
Sunil Ravia04bd252022-05-02 22:54:18 -07002437 return EC_POINT_get_affine_coordinates(e->group,
2438 (const EC_POINT *) p,
2439 (BIGNUM *) x, NULL,
2440 e->bnctx) == 1 ? 0 : -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07002441}
2442
2443
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002444int crypto_ec_point_to_bin(struct crypto_ec *e,
2445 const struct crypto_ec_point *point, u8 *x, u8 *y)
2446{
2447 BIGNUM *x_bn, *y_bn;
2448 int ret = -1;
2449 int len = BN_num_bytes(e->prime);
2450
Dmitry Shmidte4663042016-04-04 10:07:49 -07002451 if (TEST_FAIL())
2452 return -1;
2453
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002454 x_bn = BN_new();
2455 y_bn = BN_new();
2456
2457 if (x_bn && y_bn &&
Sunil Ravia04bd252022-05-02 22:54:18 -07002458 EC_POINT_get_affine_coordinates(e->group, (EC_POINT *) point,
2459 x_bn, y_bn, e->bnctx)) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002460 if (x) {
2461 crypto_bignum_to_bin((struct crypto_bignum *) x_bn,
2462 x, len, len);
2463 }
2464 if (y) {
2465 crypto_bignum_to_bin((struct crypto_bignum *) y_bn,
2466 y, len, len);
2467 }
2468 ret = 0;
2469 }
2470
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07002471 BN_clear_free(x_bn);
2472 BN_clear_free(y_bn);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002473 return ret;
2474}
2475
2476
2477struct crypto_ec_point * crypto_ec_point_from_bin(struct crypto_ec *e,
2478 const u8 *val)
2479{
2480 BIGNUM *x, *y;
2481 EC_POINT *elem;
2482 int len = BN_num_bytes(e->prime);
2483
Dmitry Shmidte4663042016-04-04 10:07:49 -07002484 if (TEST_FAIL())
2485 return NULL;
2486
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002487 x = BN_bin2bn(val, len, NULL);
2488 y = BN_bin2bn(val + len, len, NULL);
2489 elem = EC_POINT_new(e->group);
2490 if (x == NULL || y == NULL || elem == NULL) {
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07002491 BN_clear_free(x);
2492 BN_clear_free(y);
2493 EC_POINT_clear_free(elem);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002494 return NULL;
2495 }
2496
Sunil Ravia04bd252022-05-02 22:54:18 -07002497 if (!EC_POINT_set_affine_coordinates(e->group, elem, x, y, e->bnctx)) {
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07002498 EC_POINT_clear_free(elem);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002499 elem = NULL;
2500 }
2501
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07002502 BN_clear_free(x);
2503 BN_clear_free(y);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002504
2505 return (struct crypto_ec_point *) elem;
2506}
2507
2508
2509int crypto_ec_point_add(struct crypto_ec *e, const struct crypto_ec_point *a,
2510 const struct crypto_ec_point *b,
2511 struct crypto_ec_point *c)
2512{
Dmitry Shmidte4663042016-04-04 10:07:49 -07002513 if (TEST_FAIL())
2514 return -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002515 return EC_POINT_add(e->group, (EC_POINT *) c, (const EC_POINT *) a,
2516 (const EC_POINT *) b, e->bnctx) ? 0 : -1;
2517}
2518
2519
2520int crypto_ec_point_mul(struct crypto_ec *e, const struct crypto_ec_point *p,
2521 const struct crypto_bignum *b,
2522 struct crypto_ec_point *res)
2523{
Dmitry Shmidte4663042016-04-04 10:07:49 -07002524 if (TEST_FAIL())
2525 return -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002526 return EC_POINT_mul(e->group, (EC_POINT *) res, NULL,
2527 (const EC_POINT *) p, (const BIGNUM *) b, e->bnctx)
2528 ? 0 : -1;
2529}
2530
2531
2532int crypto_ec_point_invert(struct crypto_ec *e, struct crypto_ec_point *p)
2533{
Dmitry Shmidte4663042016-04-04 10:07:49 -07002534 if (TEST_FAIL())
2535 return -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002536 return EC_POINT_invert(e->group, (EC_POINT *) p, e->bnctx) ? 0 : -1;
2537}
2538
2539
Dmitry Shmidt41712582015-06-29 11:02:15 -07002540struct crypto_bignum *
2541crypto_ec_point_compute_y_sqr(struct crypto_ec *e,
2542 const struct crypto_bignum *x)
2543{
Hai Shaloma20dcd72022-02-04 13:43:00 -08002544 BIGNUM *tmp;
Dmitry Shmidt41712582015-06-29 11:02:15 -07002545
Dmitry Shmidte4663042016-04-04 10:07:49 -07002546 if (TEST_FAIL())
2547 return NULL;
2548
Dmitry Shmidt41712582015-06-29 11:02:15 -07002549 tmp = BN_new();
Dmitry Shmidt41712582015-06-29 11:02:15 -07002550
Hai Shaloma20dcd72022-02-04 13:43:00 -08002551 /* y^2 = x^3 + ax + b = (x^2 + a)x + b */
2552 if (tmp &&
Dmitry Shmidt41712582015-06-29 11:02:15 -07002553 BN_mod_sqr(tmp, (const BIGNUM *) x, e->prime, e->bnctx) &&
Hai Shaloma20dcd72022-02-04 13:43:00 -08002554 BN_mod_add_quick(tmp, e->a, tmp, e->prime) &&
Dmitry Shmidt41712582015-06-29 11:02:15 -07002555 BN_mod_mul(tmp, tmp, (const BIGNUM *) x, e->prime, e->bnctx) &&
Hai Shaloma20dcd72022-02-04 13:43:00 -08002556 BN_mod_add_quick(tmp, tmp, e->b, e->prime))
2557 return (struct crypto_bignum *) tmp;
Dmitry Shmidt41712582015-06-29 11:02:15 -07002558
2559 BN_clear_free(tmp);
Hai Shaloma20dcd72022-02-04 13:43:00 -08002560 return NULL;
Dmitry Shmidt41712582015-06-29 11:02:15 -07002561}
2562
2563
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002564int crypto_ec_point_is_at_infinity(struct crypto_ec *e,
2565 const struct crypto_ec_point *p)
2566{
2567 return EC_POINT_is_at_infinity(e->group, (const EC_POINT *) p);
2568}
2569
2570
2571int crypto_ec_point_is_on_curve(struct crypto_ec *e,
2572 const struct crypto_ec_point *p)
2573{
Dmitry Shmidt41712582015-06-29 11:02:15 -07002574 return EC_POINT_is_on_curve(e->group, (const EC_POINT *) p,
2575 e->bnctx) == 1;
2576}
2577
2578
2579int crypto_ec_point_cmp(const struct crypto_ec *e,
2580 const struct crypto_ec_point *a,
2581 const struct crypto_ec_point *b)
2582{
2583 return EC_POINT_cmp(e->group, (const EC_POINT *) a,
2584 (const EC_POINT *) b, e->bnctx);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002585}
2586
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002587
Hai Shaloma20dcd72022-02-04 13:43:00 -08002588void crypto_ec_point_debug_print(const struct crypto_ec *e,
2589 const struct crypto_ec_point *p,
2590 const char *title)
2591{
2592 BIGNUM *x, *y;
2593 char *x_str = NULL, *y_str = NULL;
2594
2595 x = BN_new();
2596 y = BN_new();
2597 if (!x || !y ||
Sunil Ravia04bd252022-05-02 22:54:18 -07002598 EC_POINT_get_affine_coordinates(e->group, (const EC_POINT *) p,
2599 x, y, e->bnctx) != 1)
Hai Shaloma20dcd72022-02-04 13:43:00 -08002600 goto fail;
2601
2602 x_str = BN_bn2hex(x);
2603 y_str = BN_bn2hex(y);
2604 if (!x_str || !y_str)
2605 goto fail;
2606
2607 wpa_printf(MSG_DEBUG, "%s (%s,%s)", title, x_str, y_str);
2608
2609fail:
2610 OPENSSL_free(x_str);
2611 OPENSSL_free(y_str);
2612 BN_free(x);
2613 BN_free(y);
2614}
2615
2616
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002617struct crypto_ecdh {
2618 struct crypto_ec *ec;
2619 EVP_PKEY *pkey;
2620};
2621
2622struct crypto_ecdh * crypto_ecdh_init(int group)
2623{
Sunil Ravia04bd252022-05-02 22:54:18 -07002624#if OPENSSL_VERSION_NUMBER >= 0x30000000L
2625 struct crypto_ecdh *ecdh;
2626 const char *name;
2627
2628 ecdh = os_zalloc(sizeof(*ecdh));
2629 if (!ecdh)
2630 goto fail;
2631
2632 ecdh->ec = crypto_ec_init(group);
2633 if (!ecdh->ec)
2634 goto fail;
2635
2636 name = OSSL_EC_curve_nid2name(ecdh->ec->nid);
2637 if (!name)
2638 goto fail;
2639
2640 ecdh->pkey = EVP_EC_gen(name);
2641 if (!ecdh->pkey)
2642 goto fail;
2643
2644done:
2645 return ecdh;
2646fail:
2647 crypto_ecdh_deinit(ecdh);
2648 ecdh = NULL;
2649 goto done;
2650#else /* OpenSSL version >= 3.0 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002651 struct crypto_ecdh *ecdh;
2652 EVP_PKEY *params = NULL;
Hai Shalom81f62d82019-07-22 12:10:00 -07002653 EC_KEY *ec_params = NULL;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002654 EVP_PKEY_CTX *kctx = NULL;
2655
2656 ecdh = os_zalloc(sizeof(*ecdh));
2657 if (!ecdh)
2658 goto fail;
2659
2660 ecdh->ec = crypto_ec_init(group);
2661 if (!ecdh->ec)
2662 goto fail;
2663
Roshan Pius3a1667e2018-07-03 15:17:14 -07002664 ec_params = EC_KEY_new_by_curve_name(ecdh->ec->nid);
2665 if (!ec_params) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002666 wpa_printf(MSG_ERROR,
Roshan Pius3a1667e2018-07-03 15:17:14 -07002667 "OpenSSL: Failed to generate EC_KEY parameters");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002668 goto fail;
2669 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07002670 EC_KEY_set_asn1_flag(ec_params, OPENSSL_EC_NAMED_CURVE);
2671 params = EVP_PKEY_new();
2672 if (!params || EVP_PKEY_set1_EC_KEY(params, ec_params) != 1) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002673 wpa_printf(MSG_ERROR,
Roshan Pius3a1667e2018-07-03 15:17:14 -07002674 "OpenSSL: Failed to generate EVP_PKEY parameters");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002675 goto fail;
2676 }
2677
2678 kctx = EVP_PKEY_CTX_new(params, NULL);
2679 if (!kctx)
2680 goto fail;
2681
2682 if (EVP_PKEY_keygen_init(kctx) != 1) {
2683 wpa_printf(MSG_ERROR,
2684 "OpenSSL: EVP_PKEY_keygen_init failed: %s",
2685 ERR_error_string(ERR_get_error(), NULL));
2686 goto fail;
2687 }
2688
2689 if (EVP_PKEY_keygen(kctx, &ecdh->pkey) != 1) {
2690 wpa_printf(MSG_ERROR, "OpenSSL: EVP_PKEY_keygen failed: %s",
2691 ERR_error_string(ERR_get_error(), NULL));
2692 goto fail;
2693 }
2694
2695done:
Hai Shalom81f62d82019-07-22 12:10:00 -07002696 EC_KEY_free(ec_params);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002697 EVP_PKEY_free(params);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002698 EVP_PKEY_CTX_free(kctx);
2699
2700 return ecdh;
2701fail:
2702 crypto_ecdh_deinit(ecdh);
2703 ecdh = NULL;
2704 goto done;
Sunil Ravia04bd252022-05-02 22:54:18 -07002705#endif /* OpenSSL version >= 3.0 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002706}
2707
2708
Hai Shaloma20dcd72022-02-04 13:43:00 -08002709struct crypto_ecdh * crypto_ecdh_init2(int group, struct crypto_ec_key *own_key)
2710{
Sunil Ravia04bd252022-05-02 22:54:18 -07002711#if OPENSSL_VERSION_NUMBER >= 0x30000000L
2712 struct crypto_ecdh *ecdh;
2713
2714 ecdh = os_zalloc(sizeof(*ecdh));
2715 if (!ecdh)
2716 goto fail;
2717
2718 ecdh->ec = crypto_ec_init(group);
2719 if (!ecdh->ec)
2720 goto fail;
2721
2722 ecdh->pkey = EVP_PKEY_dup((EVP_PKEY *) own_key);
2723 if (!ecdh->pkey)
2724 goto fail;
2725
2726 return ecdh;
2727fail:
2728 crypto_ecdh_deinit(ecdh);
2729 return NULL;
2730#else /* OpenSSL version >= 3.0 */
Hai Shaloma20dcd72022-02-04 13:43:00 -08002731 struct crypto_ecdh *ecdh;
2732
2733 ecdh = os_zalloc(sizeof(*ecdh));
2734 if (!ecdh)
2735 goto fail;
2736
2737 ecdh->ec = crypto_ec_init(group);
2738 if (!ecdh->ec)
2739 goto fail;
2740
2741 ecdh->pkey = EVP_PKEY_new();
2742 if (!ecdh->pkey ||
2743 EVP_PKEY_assign_EC_KEY(ecdh->pkey,
2744 EVP_PKEY_get1_EC_KEY((EVP_PKEY *) own_key))
2745 != 1)
2746 goto fail;
2747
2748 return ecdh;
2749fail:
2750 crypto_ecdh_deinit(ecdh);
2751 return NULL;
Sunil Ravia04bd252022-05-02 22:54:18 -07002752#endif /* OpenSSL version >= 3.0 */
Hai Shaloma20dcd72022-02-04 13:43:00 -08002753}
2754
2755
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002756struct wpabuf * crypto_ecdh_get_pubkey(struct crypto_ecdh *ecdh, int inc_y)
2757{
Sunil Ravia04bd252022-05-02 22:54:18 -07002758#if OPENSSL_VERSION_NUMBER >= 0x30000000L
2759 struct wpabuf *buf = NULL;
2760 unsigned char *pub;
2761 size_t len, exp_len;
2762
2763 len = EVP_PKEY_get1_encoded_public_key(ecdh->pkey, &pub);
2764 if (len == 0)
2765 return NULL;
2766
2767 /* Encoded using SECG SEC 1, Sec. 2.3.4 format */
2768 exp_len = 1 + 2 * crypto_ec_prime_len(ecdh->ec);
2769 if (len != exp_len) {
2770 wpa_printf(MSG_ERROR,
2771 "OpenSSL:%s: Unexpected encoded public key length %zu (expected %zu)",
2772 __func__, len, exp_len);
2773 goto fail;
2774 }
2775 buf = wpabuf_alloc_copy(pub + 1, inc_y ? len - 1 : len / 2);
2776fail:
2777 OPENSSL_free(pub);
2778 return buf;
2779#else /* OpenSSL version >= 3.0 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002780 struct wpabuf *buf = NULL;
2781 EC_KEY *eckey;
2782 const EC_POINT *pubkey;
2783 BIGNUM *x, *y = NULL;
2784 int len = BN_num_bytes(ecdh->ec->prime);
2785 int res;
2786
2787 eckey = EVP_PKEY_get1_EC_KEY(ecdh->pkey);
2788 if (!eckey)
2789 return NULL;
2790
2791 pubkey = EC_KEY_get0_public_key(eckey);
2792 if (!pubkey)
2793 return NULL;
2794
2795 x = BN_new();
2796 if (inc_y) {
2797 y = BN_new();
2798 if (!y)
2799 goto fail;
2800 }
2801 buf = wpabuf_alloc(inc_y ? 2 * len : len);
2802 if (!x || !buf)
2803 goto fail;
2804
Sunil Ravia04bd252022-05-02 22:54:18 -07002805 if (EC_POINT_get_affine_coordinates(ecdh->ec->group, pubkey,
2806 x, y, ecdh->ec->bnctx) != 1) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002807 wpa_printf(MSG_ERROR,
Sunil Ravia04bd252022-05-02 22:54:18 -07002808 "OpenSSL: EC_POINT_get_affine_coordinates failed: %s",
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002809 ERR_error_string(ERR_get_error(), NULL));
2810 goto fail;
2811 }
2812
2813 res = crypto_bignum_to_bin((struct crypto_bignum *) x,
2814 wpabuf_put(buf, len), len, len);
2815 if (res < 0)
2816 goto fail;
2817
2818 if (inc_y) {
2819 res = crypto_bignum_to_bin((struct crypto_bignum *) y,
2820 wpabuf_put(buf, len), len, len);
2821 if (res < 0)
2822 goto fail;
2823 }
2824
2825done:
2826 BN_clear_free(x);
2827 BN_clear_free(y);
2828 EC_KEY_free(eckey);
2829
2830 return buf;
2831fail:
2832 wpabuf_free(buf);
2833 buf = NULL;
2834 goto done;
Sunil Ravia04bd252022-05-02 22:54:18 -07002835#endif /* OpenSSL version >= 3.0 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002836}
2837
2838
2839struct wpabuf * crypto_ecdh_set_peerkey(struct crypto_ecdh *ecdh, int inc_y,
2840 const u8 *key, size_t len)
2841{
Sunil Ravia04bd252022-05-02 22:54:18 -07002842#if OPENSSL_VERSION_NUMBER >= 0x30000000L
2843 EVP_PKEY *peerkey = EVP_PKEY_new();
2844 EVP_PKEY_CTX *ctx;
2845 size_t res_len;
2846 struct wpabuf *res = NULL;
2847 u8 *peer;
2848
2849 /* Encode using SECG SEC 1, Sec. 2.3.4 format */
2850 peer = os_malloc(1 + len);
2851 if (!peer)
2852 return NULL;
2853 peer[0] = inc_y ? 0x04 : 0x02;
2854 os_memcpy(peer + 1, key, len);
2855
2856 if (!peerkey ||
2857 EVP_PKEY_copy_parameters(peerkey, ecdh->pkey) != 1 ||
2858 EVP_PKEY_set1_encoded_public_key(peerkey, peer, 1 + len) != 1) {
2859 wpa_printf(MSG_INFO, "OpenSSL: EVP_PKEY_set1_encoded_public_key failed: %s",
2860 ERR_error_string(ERR_get_error(), NULL));
2861 EVP_PKEY_free(peerkey);
2862 os_free(peer);
2863 return NULL;
2864 }
2865 os_free(peer);
2866
2867 ctx = EVP_PKEY_CTX_new(ecdh->pkey, NULL);
2868 if (!ctx ||
2869 EVP_PKEY_derive_init(ctx) != 1 ||
2870 EVP_PKEY_derive_set_peer(ctx, peerkey) != 1 ||
2871 EVP_PKEY_derive(ctx, NULL, &res_len) != 1 ||
2872 !(res = wpabuf_alloc(res_len)) ||
2873 EVP_PKEY_derive(ctx, wpabuf_mhead(res), &res_len) != 1) {
2874 wpa_printf(MSG_INFO, "OpenSSL: EVP_PKEY_derive failed: %s",
2875 ERR_error_string(ERR_get_error(), NULL));
2876 wpabuf_free(res);
2877 res = NULL;
2878 } else {
2879 wpabuf_put(res, res_len);
2880 }
2881
2882 EVP_PKEY_free(peerkey);
2883 EVP_PKEY_CTX_free(ctx);
2884 return res;
2885#else /* OpenSSL version >= 3.0 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002886 BIGNUM *x, *y = NULL;
2887 EVP_PKEY_CTX *ctx = NULL;
2888 EVP_PKEY *peerkey = NULL;
2889 struct wpabuf *secret = NULL;
2890 size_t secret_len;
2891 EC_POINT *pub;
2892 EC_KEY *eckey = NULL;
2893
2894 x = BN_bin2bn(key, inc_y ? len / 2 : len, NULL);
2895 pub = EC_POINT_new(ecdh->ec->group);
2896 if (!x || !pub)
2897 goto fail;
2898
2899 if (inc_y) {
2900 y = BN_bin2bn(key + len / 2, len / 2, NULL);
2901 if (!y)
2902 goto fail;
Sunil Ravia04bd252022-05-02 22:54:18 -07002903 if (!EC_POINT_set_affine_coordinates(ecdh->ec->group, pub,
2904 x, y, ecdh->ec->bnctx)) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002905 wpa_printf(MSG_ERROR,
Sunil Ravia04bd252022-05-02 22:54:18 -07002906 "OpenSSL: EC_POINT_set_affine_coordinates failed: %s",
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002907 ERR_error_string(ERR_get_error(), NULL));
2908 goto fail;
2909 }
Sunil Ravia04bd252022-05-02 22:54:18 -07002910 } else if (!EC_POINT_set_compressed_coordinates(ecdh->ec->group,
2911 pub, x, 0,
2912 ecdh->ec->bnctx)) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002913 wpa_printf(MSG_ERROR,
Sunil Ravia04bd252022-05-02 22:54:18 -07002914 "OpenSSL: EC_POINT_set_compressed_coordinates failed: %s",
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002915 ERR_error_string(ERR_get_error(), NULL));
2916 goto fail;
2917 }
2918
2919 if (!EC_POINT_is_on_curve(ecdh->ec->group, pub, ecdh->ec->bnctx)) {
2920 wpa_printf(MSG_ERROR,
2921 "OpenSSL: ECDH peer public key is not on curve");
2922 goto fail;
2923 }
2924
2925 eckey = EC_KEY_new_by_curve_name(ecdh->ec->nid);
2926 if (!eckey || EC_KEY_set_public_key(eckey, pub) != 1) {
2927 wpa_printf(MSG_ERROR,
2928 "OpenSSL: EC_KEY_set_public_key failed: %s",
2929 ERR_error_string(ERR_get_error(), NULL));
2930 goto fail;
2931 }
2932
2933 peerkey = EVP_PKEY_new();
2934 if (!peerkey || EVP_PKEY_set1_EC_KEY(peerkey, eckey) != 1)
2935 goto fail;
2936
2937 ctx = EVP_PKEY_CTX_new(ecdh->pkey, NULL);
2938 if (!ctx || EVP_PKEY_derive_init(ctx) != 1 ||
2939 EVP_PKEY_derive_set_peer(ctx, peerkey) != 1 ||
2940 EVP_PKEY_derive(ctx, NULL, &secret_len) != 1) {
2941 wpa_printf(MSG_ERROR,
2942 "OpenSSL: EVP_PKEY_derive(1) failed: %s",
2943 ERR_error_string(ERR_get_error(), NULL));
2944 goto fail;
2945 }
2946
2947 secret = wpabuf_alloc(secret_len);
2948 if (!secret)
2949 goto fail;
Hai Shalomc3565922019-10-28 11:58:20 -07002950 if (EVP_PKEY_derive(ctx, wpabuf_put(secret, 0), &secret_len) != 1) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002951 wpa_printf(MSG_ERROR,
2952 "OpenSSL: EVP_PKEY_derive(2) failed: %s",
2953 ERR_error_string(ERR_get_error(), NULL));
2954 goto fail;
2955 }
Hai Shalomc3565922019-10-28 11:58:20 -07002956 if (secret->size != secret_len)
2957 wpa_printf(MSG_DEBUG,
2958 "OpenSSL: EVP_PKEY_derive(2) changed secret_len %d -> %d",
2959 (int) secret->size, (int) secret_len);
2960 wpabuf_put(secret, secret_len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002961
2962done:
2963 BN_free(x);
2964 BN_free(y);
2965 EC_KEY_free(eckey);
2966 EC_POINT_free(pub);
2967 EVP_PKEY_CTX_free(ctx);
2968 EVP_PKEY_free(peerkey);
2969 return secret;
2970fail:
2971 wpabuf_free(secret);
2972 secret = NULL;
2973 goto done;
Sunil Ravia04bd252022-05-02 22:54:18 -07002974#endif /* OpenSSL version >= 3.0 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002975}
2976
2977
2978void crypto_ecdh_deinit(struct crypto_ecdh *ecdh)
2979{
2980 if (ecdh) {
2981 crypto_ec_deinit(ecdh->ec);
2982 EVP_PKEY_free(ecdh->pkey);
2983 os_free(ecdh);
2984 }
2985}
2986
Hai Shalomfdcde762020-04-02 11:19:20 -07002987
2988size_t crypto_ecdh_prime_len(struct crypto_ecdh *ecdh)
2989{
2990 return crypto_ec_prime_len(ecdh->ec);
2991}
2992
Hai Shalom899fcc72020-10-19 14:38:18 -07002993
Hai Shalom899fcc72020-10-19 14:38:18 -07002994struct crypto_ec_key * crypto_ec_key_parse_priv(const u8 *der, size_t der_len)
2995{
Sunil8cd6f4d2022-06-28 18:40:46 +00002996#if OPENSSL_VERSION_NUMBER >= 0x30000000L
2997 EVP_PKEY *pkey = NULL;
2998 OSSL_DECODER_CTX *ctx;
2999
3000 ctx = OSSL_DECODER_CTX_new_for_pkey(
3001 &pkey, "DER", NULL, "EC",
3002 OSSL_KEYMGMT_SELECT_KEYPAIR |
3003 OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS,
3004 NULL, NULL);
3005 if (!ctx ||
3006 OSSL_DECODER_from_data(ctx, &der, &der_len) != 1) {
3007 wpa_printf(MSG_INFO, "OpenSSL: Decoding EC private key (DER) failed: %s",
3008 ERR_error_string(ERR_get_error(), NULL));
3009 goto fail;
3010 }
3011
3012 return (struct crypto_ec_key *) pkey;
3013fail:
3014 crypto_ec_key_deinit((struct crypto_ec_key *) pkey);
3015 return NULL;
3016#else /* OpenSSL version >= 3.0 */
Hai Shaloma20dcd72022-02-04 13:43:00 -08003017 EVP_PKEY *pkey = NULL;
3018 EC_KEY *eckey;
Hai Shalom899fcc72020-10-19 14:38:18 -07003019
Hai Shaloma20dcd72022-02-04 13:43:00 -08003020 eckey = d2i_ECPrivateKey(NULL, &der, der_len);
3021 if (!eckey) {
Hai Shalom899fcc72020-10-19 14:38:18 -07003022 wpa_printf(MSG_INFO, "OpenSSL: d2i_ECPrivateKey() failed: %s",
3023 ERR_error_string(ERR_get_error(), NULL));
3024 goto fail;
3025 }
Hai Shaloma20dcd72022-02-04 13:43:00 -08003026 EC_KEY_set_conv_form(eckey, POINT_CONVERSION_COMPRESSED);
Hai Shalom899fcc72020-10-19 14:38:18 -07003027
Hai Shaloma20dcd72022-02-04 13:43:00 -08003028 pkey = EVP_PKEY_new();
3029 if (!pkey || EVP_PKEY_assign_EC_KEY(pkey, eckey) != 1) {
3030 EC_KEY_free(eckey);
Hai Shalom899fcc72020-10-19 14:38:18 -07003031 goto fail;
3032 }
3033
Hai Shaloma20dcd72022-02-04 13:43:00 -08003034 return (struct crypto_ec_key *) pkey;
Hai Shalom899fcc72020-10-19 14:38:18 -07003035fail:
Hai Shaloma20dcd72022-02-04 13:43:00 -08003036 crypto_ec_key_deinit((struct crypto_ec_key *) pkey);
Hai Shalom899fcc72020-10-19 14:38:18 -07003037 return NULL;
Sunil8cd6f4d2022-06-28 18:40:46 +00003038#endif /* OpenSSL version >= 3.0 */
Hai Shalom899fcc72020-10-19 14:38:18 -07003039}
3040
3041
Sunil Ravi89eba102022-09-13 21:04:37 -07003042struct crypto_ec_key * crypto_ec_key_set_priv(int group,
3043 const u8 *raw, size_t raw_len)
3044{
3045#if OPENSSL_VERSION_NUMBER >= 0x30000000L
3046 const char *group_name;
3047 OSSL_PARAM params[4];
3048 EVP_PKEY_CTX *ctx = NULL;
3049 EVP_PKEY *pkey = NULL;
3050 BIGNUM *priv;
3051 EC_POINT *pub = NULL;
3052 EC_GROUP *ec_group = NULL;
3053 size_t len;
3054 u8 *pub_bin = NULL;
3055 u8 *priv_bin = NULL;
3056 int priv_bin_len;
3057
3058 group_name = crypto_ec_group_2_name(group);
3059 if (!group_name)
3060 return NULL;
3061
3062 priv = BN_bin2bn(raw, raw_len, NULL);
3063 if (!priv)
3064 return NULL;
3065 priv_bin = os_malloc(raw_len);
3066 if (!priv_bin)
3067 goto fail;
3068 priv_bin_len = BN_bn2lebinpad(priv, priv_bin, raw_len);
3069 if (priv_bin_len < 0)
3070 goto fail;
3071
3072 ec_group = EC_GROUP_new_by_curve_name(crypto_ec_group_2_nid(group));
3073 if (!ec_group)
3074 goto fail;
3075 pub = EC_POINT_new(ec_group);
3076 if (!pub ||
3077 EC_POINT_mul(ec_group, pub, priv, NULL, NULL, NULL) != 1)
3078 goto fail;
3079 len = EC_POINT_point2oct(ec_group, pub, POINT_CONVERSION_UNCOMPRESSED,
3080 NULL, 0, NULL);
3081 if (len == 0)
3082 goto fail;
3083 pub_bin = os_malloc(len);
3084 if (!pub_bin)
3085 goto fail;
3086 len = EC_POINT_point2oct(ec_group, pub, POINT_CONVERSION_UNCOMPRESSED,
3087 pub_bin, len, NULL);
3088 if (len == 0)
3089 goto fail;
3090
3091 params[0] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME,
3092 (char *) group_name, 0);
3093 params[1] = OSSL_PARAM_construct_BN(OSSL_PKEY_PARAM_PRIV_KEY,
3094 priv_bin, priv_bin_len);
3095 params[2] = OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_PUB_KEY,
3096 pub_bin, len);
3097 params[3] = OSSL_PARAM_construct_end();
3098
3099 ctx = EVP_PKEY_CTX_new_from_name(NULL, "EC", NULL);
3100 if (!ctx ||
3101 EVP_PKEY_fromdata_init(ctx) <= 0 ||
3102 EVP_PKEY_fromdata(ctx, &pkey, EVP_PKEY_KEYPAIR, params) <= 0)
3103 goto fail;
3104
3105out:
3106 bin_clear_free(priv_bin, raw_len);
3107 os_free(pub_bin);
3108 BN_clear_free(priv);
3109 EVP_PKEY_CTX_free(ctx);
3110 EC_POINT_free(pub);
3111 EC_GROUP_free(ec_group);
3112 return (struct crypto_ec_key *) pkey;
3113
3114fail:
3115 EVP_PKEY_free(pkey);
3116 pkey = NULL;
3117 goto out;
3118#else /* OpenSSL version >= 3.0 */
3119 EC_KEY *eckey = NULL;
3120 EVP_PKEY *pkey = NULL;
3121 BIGNUM *priv = NULL;
3122 int nid;
3123 const EC_GROUP *ec_group;
3124 EC_POINT *pub = NULL;
3125
3126 nid = crypto_ec_group_2_nid(group);
3127 if (nid < 0) {
3128 wpa_printf(MSG_ERROR, "OpenSSL: Unsupported group %d", group);
3129 return NULL;
3130 }
3131
3132 eckey = EC_KEY_new_by_curve_name(nid);
3133 priv = BN_bin2bn(raw, raw_len, NULL);
3134 if (!eckey || !priv ||
3135 EC_KEY_set_private_key(eckey, priv) != 1) {
3136 wpa_printf(MSG_ERROR,
3137 "OpenSSL: Failed to set EC_KEY: %s",
3138 ERR_error_string(ERR_get_error(), NULL));
3139 goto fail;
3140 }
3141
3142 ec_group = EC_KEY_get0_group(eckey);
3143 if (!ec_group)
3144 goto fail;
3145 pub = EC_POINT_new(ec_group);
3146 if (!pub ||
3147 EC_POINT_mul(ec_group, pub, priv, NULL, NULL, NULL) != 1 ||
3148 EC_KEY_set_public_key(eckey, pub) != 1) {
3149 wpa_printf(MSG_ERROR,
3150 "OpenSSL: Failed to set EC_KEY(pub): %s",
3151 ERR_error_string(ERR_get_error(), NULL));
3152 goto fail;
3153 }
3154
3155 EC_KEY_set_asn1_flag(eckey, OPENSSL_EC_NAMED_CURVE);
3156
3157 pkey = EVP_PKEY_new();
3158 if (!pkey || EVP_PKEY_assign_EC_KEY(pkey, eckey) != 1) {
3159 wpa_printf(MSG_ERROR, "OpenSSL: Could not create EVP_PKEY");
3160 goto fail;
3161 }
3162
3163out:
3164 BN_clear_free(priv);
3165 EC_POINT_free(pub);
3166 return (struct crypto_ec_key *) pkey;
3167
3168fail:
3169 EC_KEY_free(eckey);
3170 EVP_PKEY_free(pkey);
3171 pkey = NULL;
3172 goto out;
3173#endif /* OpenSSL version >= 3.0 */
3174}
3175
3176
Hai Shalom899fcc72020-10-19 14:38:18 -07003177struct crypto_ec_key * crypto_ec_key_parse_pub(const u8 *der, size_t der_len)
3178{
Hai Shaloma20dcd72022-02-04 13:43:00 -08003179 EVP_PKEY *pkey;
Hai Shalom899fcc72020-10-19 14:38:18 -07003180
Hai Shaloma20dcd72022-02-04 13:43:00 -08003181 pkey = d2i_PUBKEY(NULL, &der, der_len);
3182 if (!pkey) {
Hai Shalom899fcc72020-10-19 14:38:18 -07003183 wpa_printf(MSG_INFO, "OpenSSL: d2i_PUBKEY() failed: %s",
3184 ERR_error_string(ERR_get_error(), NULL));
3185 goto fail;
3186 }
3187
Hai Shaloma20dcd72022-02-04 13:43:00 -08003188 /* Ensure this is an EC key */
Sunil8cd6f4d2022-06-28 18:40:46 +00003189#if OPENSSL_VERSION_NUMBER >= 0x30000000L
3190 if (!EVP_PKEY_is_a(pkey, "EC"))
3191 goto fail;
3192#else /* OpenSSL version >= 3.0 */
Hai Shaloma20dcd72022-02-04 13:43:00 -08003193 if (!EVP_PKEY_get0_EC_KEY(pkey))
Hai Shalom899fcc72020-10-19 14:38:18 -07003194 goto fail;
Sunil8cd6f4d2022-06-28 18:40:46 +00003195#endif /* OpenSSL version >= 3.0 */
Hai Shaloma20dcd72022-02-04 13:43:00 -08003196 return (struct crypto_ec_key *) pkey;
Hai Shalom899fcc72020-10-19 14:38:18 -07003197fail:
Hai Shaloma20dcd72022-02-04 13:43:00 -08003198 crypto_ec_key_deinit((struct crypto_ec_key *) pkey);
Hai Shalom899fcc72020-10-19 14:38:18 -07003199 return NULL;
3200}
3201
3202
Hai Shaloma20dcd72022-02-04 13:43:00 -08003203struct crypto_ec_key * crypto_ec_key_set_pub(int group, const u8 *buf_x,
3204 const u8 *buf_y, size_t len)
3205{
Sunil8cd6f4d2022-06-28 18:40:46 +00003206#if OPENSSL_VERSION_NUMBER >= 0x30000000L
3207 const char *group_name;
3208 OSSL_PARAM params[3];
3209 u8 *pub;
3210 EVP_PKEY_CTX *ctx;
3211 EVP_PKEY *pkey = NULL;
3212
3213 group_name = crypto_ec_group_2_name(group);
3214 if (!group_name)
3215 return NULL;
3216
3217 pub = os_malloc(1 + len * 2);
3218 if (!pub)
3219 return NULL;
3220 pub[0] = 0x04; /* uncompressed */
3221 os_memcpy(pub + 1, buf_x, len);
3222 os_memcpy(pub + 1 + len, buf_y, len);
3223
3224 params[0] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME,
3225 (char *) group_name, 0);
3226 params[1] = OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_PUB_KEY,
3227 pub, 1 + len * 2);
3228 params[2] = OSSL_PARAM_construct_end();
3229
3230 ctx = EVP_PKEY_CTX_new_from_name(NULL, "EC", NULL);
3231 if (!ctx) {
3232 os_free(pub);
3233 return NULL;
3234 }
3235 if (EVP_PKEY_fromdata_init(ctx) <= 0 ||
3236 EVP_PKEY_fromdata(ctx, &pkey, EVP_PKEY_PUBLIC_KEY, params) <= 0) {
3237 os_free(pub);
3238 EVP_PKEY_CTX_free(ctx);
3239 return NULL;
3240 }
3241
3242 os_free(pub);
3243 EVP_PKEY_CTX_free(ctx);
3244
3245 return (struct crypto_ec_key *) pkey;
3246#else /* OpenSSL version >= 3.0 */
Hai Shaloma20dcd72022-02-04 13:43:00 -08003247 EC_KEY *eckey = NULL;
3248 EVP_PKEY *pkey = NULL;
3249 EC_GROUP *ec_group = NULL;
3250 BN_CTX *ctx;
3251 EC_POINT *point = NULL;
3252 BIGNUM *x = NULL, *y = NULL;
3253 int nid;
3254
3255 if (!buf_x || !buf_y)
3256 return NULL;
3257
3258 nid = crypto_ec_group_2_nid(group);
3259 if (nid < 0) {
3260 wpa_printf(MSG_ERROR, "OpenSSL: Unsupported group %d", group);
3261 return NULL;
3262 }
3263
3264 ctx = BN_CTX_new();
3265 if (!ctx)
3266 goto fail;
3267
3268 ec_group = EC_GROUP_new_by_curve_name(nid);
3269 if (!ec_group)
3270 goto fail;
3271
3272 x = BN_bin2bn(buf_x, len, NULL);
3273 y = BN_bin2bn(buf_y, len, NULL);
3274 point = EC_POINT_new(ec_group);
3275 if (!x || !y || !point)
3276 goto fail;
3277
Sunil Ravia04bd252022-05-02 22:54:18 -07003278 if (!EC_POINT_set_affine_coordinates(ec_group, point, x, y, ctx)) {
Hai Shaloma20dcd72022-02-04 13:43:00 -08003279 wpa_printf(MSG_ERROR,
Sunil Ravia04bd252022-05-02 22:54:18 -07003280 "OpenSSL: EC_POINT_set_affine_coordinates failed: %s",
Hai Shaloma20dcd72022-02-04 13:43:00 -08003281 ERR_error_string(ERR_get_error(), NULL));
3282 goto fail;
3283 }
3284
3285 if (!EC_POINT_is_on_curve(ec_group, point, ctx) ||
3286 EC_POINT_is_at_infinity(ec_group, point)) {
3287 wpa_printf(MSG_ERROR, "OpenSSL: Invalid point");
3288 goto fail;
3289 }
3290
3291 eckey = EC_KEY_new();
3292 if (!eckey ||
3293 EC_KEY_set_group(eckey, ec_group) != 1 ||
3294 EC_KEY_set_public_key(eckey, point) != 1) {
3295 wpa_printf(MSG_ERROR,
3296 "OpenSSL: Failed to set EC_KEY: %s",
3297 ERR_error_string(ERR_get_error(), NULL));
3298 goto fail;
3299 }
3300 EC_KEY_set_asn1_flag(eckey, OPENSSL_EC_NAMED_CURVE);
3301
3302 pkey = EVP_PKEY_new();
3303 if (!pkey || EVP_PKEY_assign_EC_KEY(pkey, eckey) != 1) {
3304 wpa_printf(MSG_ERROR, "OpenSSL: Could not create EVP_PKEY");
3305 goto fail;
3306 }
3307
3308out:
3309 EC_GROUP_free(ec_group);
3310 BN_free(x);
3311 BN_free(y);
3312 EC_POINT_free(point);
3313 BN_CTX_free(ctx);
3314 return (struct crypto_ec_key *) pkey;
3315
3316fail:
3317 EC_KEY_free(eckey);
3318 EVP_PKEY_free(pkey);
3319 pkey = NULL;
3320 goto out;
Sunil8cd6f4d2022-06-28 18:40:46 +00003321#endif /* OpenSSL version >= 3.0 */
Hai Shaloma20dcd72022-02-04 13:43:00 -08003322}
3323
3324
3325struct crypto_ec_key *
3326crypto_ec_key_set_pub_point(struct crypto_ec *ec,
3327 const struct crypto_ec_point *pub)
3328{
Sunil8cd6f4d2022-06-28 18:40:46 +00003329#if OPENSSL_VERSION_NUMBER >= 0x30000000L
3330 int len = BN_num_bytes(ec->prime);
3331 struct crypto_ec_key *key;
3332 u8 *buf;
3333
3334 buf = os_malloc(2 * len);
3335 if (!buf)
3336 return NULL;
3337 if (crypto_ec_point_to_bin(ec, pub, buf, buf + len) < 0) {
3338 os_free(buf);
3339 return NULL;
3340 }
3341
3342 key = crypto_ec_key_set_pub(ec->iana_group, buf, buf + len, len);
3343 os_free(buf);
3344
3345 return key;
3346#else /* OpenSSL version >= 3.0 */
Hai Shaloma20dcd72022-02-04 13:43:00 -08003347 EC_KEY *eckey;
3348 EVP_PKEY *pkey = NULL;
3349
3350 eckey = EC_KEY_new();
3351 if (!eckey ||
3352 EC_KEY_set_group(eckey, ec->group) != 1 ||
3353 EC_KEY_set_public_key(eckey, (const EC_POINT *) pub) != 1) {
3354 wpa_printf(MSG_ERROR,
3355 "OpenSSL: Failed to set EC_KEY: %s",
3356 ERR_error_string(ERR_get_error(), NULL));
3357 goto fail;
3358 }
3359 EC_KEY_set_asn1_flag(eckey, OPENSSL_EC_NAMED_CURVE);
3360
3361 pkey = EVP_PKEY_new();
3362 if (!pkey || EVP_PKEY_assign_EC_KEY(pkey, eckey) != 1) {
3363 wpa_printf(MSG_ERROR, "OpenSSL: Could not create EVP_PKEY");
3364 goto fail;
3365 }
3366
3367out:
3368 return (struct crypto_ec_key *) pkey;
3369
3370fail:
3371 EVP_PKEY_free(pkey);
3372 EC_KEY_free(eckey);
3373 pkey = NULL;
3374 goto out;
Sunil8cd6f4d2022-06-28 18:40:46 +00003375#endif /* OpenSSL version >= 3.0 */
Hai Shaloma20dcd72022-02-04 13:43:00 -08003376}
3377
3378
3379struct crypto_ec_key * crypto_ec_key_gen(int group)
3380{
Sunil8cd6f4d2022-06-28 18:40:46 +00003381#if OPENSSL_VERSION_NUMBER >= 0x30000000L
3382 EVP_PKEY_CTX *ctx;
3383 OSSL_PARAM params[2];
3384 const char *group_name;
3385 EVP_PKEY *pkey = NULL;
3386
3387 group_name = crypto_ec_group_2_name(group);
3388 if (!group_name)
3389 return NULL;
3390
3391 params[0] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME,
3392 (char *) group_name, 0);
3393 params[1] = OSSL_PARAM_construct_end();
3394
3395 ctx = EVP_PKEY_CTX_new_from_name(NULL, "EC", NULL);
3396 if (!ctx ||
3397 EVP_PKEY_keygen_init(ctx) != 1 ||
3398 EVP_PKEY_CTX_set_params(ctx, params) != 1 ||
3399 EVP_PKEY_generate(ctx, &pkey) != 1) {
3400 wpa_printf(MSG_INFO,
3401 "OpenSSL: failed to generate EC keypair: %s",
3402 ERR_error_string(ERR_get_error(), NULL));
3403 pkey = NULL;
3404 }
3405
3406 EVP_PKEY_CTX_free(ctx);
3407
3408 return (struct crypto_ec_key *) pkey;
3409#else /* OpenSSL version >= 3.0 */
Hai Shaloma20dcd72022-02-04 13:43:00 -08003410 EVP_PKEY_CTX *kctx = NULL;
3411 EC_KEY *ec_params = NULL, *eckey;
3412 EVP_PKEY *params = NULL, *key = NULL;
3413 int nid;
3414
3415 nid = crypto_ec_group_2_nid(group);
3416 if (nid < 0) {
3417 wpa_printf(MSG_ERROR, "OpenSSL: Unsupported group %d", group);
3418 return NULL;
3419 }
3420
3421 ec_params = EC_KEY_new_by_curve_name(nid);
3422 if (!ec_params) {
3423 wpa_printf(MSG_ERROR,
3424 "OpenSSL: Failed to generate EC_KEY parameters");
3425 goto fail;
3426 }
3427 EC_KEY_set_asn1_flag(ec_params, OPENSSL_EC_NAMED_CURVE);
3428 params = EVP_PKEY_new();
3429 if (!params || EVP_PKEY_set1_EC_KEY(params, ec_params) != 1) {
3430 wpa_printf(MSG_ERROR,
3431 "OpenSSL: Failed to generate EVP_PKEY parameters");
3432 goto fail;
3433 }
3434
3435 kctx = EVP_PKEY_CTX_new(params, NULL);
3436 if (!kctx ||
3437 EVP_PKEY_keygen_init(kctx) != 1 ||
3438 EVP_PKEY_keygen(kctx, &key) != 1) {
3439 wpa_printf(MSG_ERROR, "OpenSSL: Failed to generate EC key");
3440 key = NULL;
3441 goto fail;
3442 }
3443
3444 eckey = EVP_PKEY_get1_EC_KEY(key);
3445 if (!eckey) {
3446 key = NULL;
3447 goto fail;
3448 }
3449 EC_KEY_set_conv_form(eckey, POINT_CONVERSION_COMPRESSED);
3450 EC_KEY_free(eckey);
3451
3452fail:
3453 EC_KEY_free(ec_params);
3454 EVP_PKEY_free(params);
3455 EVP_PKEY_CTX_free(kctx);
3456 return (struct crypto_ec_key *) key;
Sunil8cd6f4d2022-06-28 18:40:46 +00003457#endif /* OpenSSL version >= 3.0 */
Hai Shaloma20dcd72022-02-04 13:43:00 -08003458}
3459
3460
Hai Shalom899fcc72020-10-19 14:38:18 -07003461void crypto_ec_key_deinit(struct crypto_ec_key *key)
3462{
Hai Shaloma20dcd72022-02-04 13:43:00 -08003463 EVP_PKEY_free((EVP_PKEY *) key);
Hai Shalom899fcc72020-10-19 14:38:18 -07003464}
3465
3466
Hai Shaloma20dcd72022-02-04 13:43:00 -08003467#ifdef OPENSSL_IS_BORINGSSL
3468
3469/* BoringSSL version of i2d_PUBKEY() always outputs public EC key using
3470 * uncompressed form so define a custom function to export EC pubkey using
3471 * the compressed format that is explicitly required for some protocols. */
3472
3473#include <openssl/asn1.h>
3474#include <openssl/asn1t.h>
3475
3476typedef struct {
3477 /* AlgorithmIdentifier ecPublicKey with optional parameters present
3478 * as an OID identifying the curve */
3479 X509_ALGOR *alg;
3480 /* Compressed format public key per ANSI X9.63 */
3481 ASN1_BIT_STRING *pub_key;
3482} EC_COMP_PUBKEY;
3483
3484ASN1_SEQUENCE(EC_COMP_PUBKEY) = {
3485 ASN1_SIMPLE(EC_COMP_PUBKEY, alg, X509_ALGOR),
3486 ASN1_SIMPLE(EC_COMP_PUBKEY, pub_key, ASN1_BIT_STRING)
3487} ASN1_SEQUENCE_END(EC_COMP_PUBKEY);
3488
3489IMPLEMENT_ASN1_FUNCTIONS(EC_COMP_PUBKEY);
3490
3491#endif /* OPENSSL_IS_BORINGSSL */
3492
3493
Hai Shalom899fcc72020-10-19 14:38:18 -07003494struct wpabuf * crypto_ec_key_get_subject_public_key(struct crypto_ec_key *key)
3495{
Sunil8cd6f4d2022-06-28 18:40:46 +00003496 EVP_PKEY *pkey = (EVP_PKEY *) key;
3497#if OPENSSL_VERSION_NUMBER >= 0x30000000L
3498 OSSL_ENCODER_CTX *ctx;
3499 int selection;
3500 unsigned char *pdata = NULL;
3501 size_t pdata_len = 0;
3502 EVP_PKEY *copy = NULL;
3503 struct wpabuf *buf = NULL;
3504
3505 if (EVP_PKEY_get_ec_point_conv_form(pkey) !=
3506 POINT_CONVERSION_COMPRESSED) {
3507 copy = EVP_PKEY_dup(pkey);
3508 if (!copy)
3509 return NULL;
3510 if (EVP_PKEY_set_utf8_string_param(
3511 copy, OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT,
3512 OSSL_PKEY_EC_POINT_CONVERSION_FORMAT_COMPRESSED) !=
3513 1) {
3514 wpa_printf(MSG_INFO,
3515 "OpenSSL: Failed to set compressed format");
3516 EVP_PKEY_free(copy);
3517 return NULL;
3518 }
3519 pkey = copy;
3520 }
3521
3522 selection = OSSL_KEYMGMT_SELECT_ALL_PARAMETERS |
3523 OSSL_KEYMGMT_SELECT_PUBLIC_KEY;
3524
3525 ctx = OSSL_ENCODER_CTX_new_for_pkey(pkey, selection, "DER",
3526 "SubjectPublicKeyInfo",
3527 NULL);
3528 if (!ctx || OSSL_ENCODER_to_data(ctx, &pdata, &pdata_len) != 1) {
3529 wpa_printf(MSG_INFO,
3530 "OpenSSL: Failed to encode SubjectPublicKeyInfo: %s",
3531 ERR_error_string(ERR_get_error(), NULL));
3532 pdata = NULL;
3533 }
3534 OSSL_ENCODER_CTX_free(ctx);
3535 if (pdata) {
3536 buf = wpabuf_alloc_copy(pdata, pdata_len);
3537 OPENSSL_free(pdata);
3538 }
3539
3540 EVP_PKEY_free(copy);
3541
3542 return buf;
3543#else /* OpenSSL version >= 3.0 */
Hai Shaloma20dcd72022-02-04 13:43:00 -08003544#ifdef OPENSSL_IS_BORINGSSL
3545 unsigned char *der = NULL;
3546 int der_len;
3547 const EC_KEY *eckey;
3548 struct wpabuf *ret = NULL;
3549 size_t len;
3550 const EC_GROUP *group;
3551 const EC_POINT *point;
3552 BN_CTX *ctx;
3553 EC_COMP_PUBKEY *pubkey = NULL;
3554 int nid;
3555
3556 ctx = BN_CTX_new();
Sunil8cd6f4d2022-06-28 18:40:46 +00003557 eckey = EVP_PKEY_get0_EC_KEY(pkey);
Hai Shaloma20dcd72022-02-04 13:43:00 -08003558 if (!ctx || !eckey)
3559 goto fail;
3560
3561 group = EC_KEY_get0_group(eckey);
3562 point = EC_KEY_get0_public_key(eckey);
3563 if (!group || !point)
3564 goto fail;
3565 nid = EC_GROUP_get_curve_name(group);
3566
3567 pubkey = EC_COMP_PUBKEY_new();
3568 if (!pubkey ||
3569 X509_ALGOR_set0(pubkey->alg, OBJ_nid2obj(EVP_PKEY_EC),
3570 V_ASN1_OBJECT, (void *) OBJ_nid2obj(nid)) != 1)
3571 goto fail;
3572
3573 len = EC_POINT_point2oct(group, point, POINT_CONVERSION_COMPRESSED,
3574 NULL, 0, ctx);
3575 if (len == 0)
3576 goto fail;
3577
3578 der = OPENSSL_malloc(len);
3579 if (!der)
3580 goto fail;
3581 len = EC_POINT_point2oct(group, point, POINT_CONVERSION_COMPRESSED,
3582 der, len, ctx);
3583
3584 OPENSSL_free(pubkey->pub_key->data);
3585 pubkey->pub_key->data = der;
3586 der = NULL;
3587 pubkey->pub_key->length = len;
3588 /* No unused bits */
3589 pubkey->pub_key->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
3590 pubkey->pub_key->flags |= ASN1_STRING_FLAG_BITS_LEFT;
3591
3592 der_len = i2d_EC_COMP_PUBKEY(pubkey, &der);
3593 if (der_len <= 0) {
3594 wpa_printf(MSG_ERROR,
3595 "BoringSSL: Failed to build DER encoded public key");
3596 goto fail;
3597 }
3598
3599 ret = wpabuf_alloc_copy(der, der_len);
3600fail:
3601 EC_COMP_PUBKEY_free(pubkey);
3602 OPENSSL_free(der);
3603 BN_CTX_free(ctx);
3604 return ret;
3605#else /* OPENSSL_IS_BORINGSSL */
Hai Shalom899fcc72020-10-19 14:38:18 -07003606 unsigned char *der = NULL;
3607 int der_len;
3608 struct wpabuf *buf;
Hai Shaloma20dcd72022-02-04 13:43:00 -08003609 EC_KEY *eckey;
Hai Shalom899fcc72020-10-19 14:38:18 -07003610
Sunil8cd6f4d2022-06-28 18:40:46 +00003611 eckey = EVP_PKEY_get1_EC_KEY(pkey);
Hai Shaloma20dcd72022-02-04 13:43:00 -08003612 if (!eckey)
3613 return NULL;
3614
3615 /* For now, all users expect COMPRESSED form */
3616 EC_KEY_set_conv_form(eckey, POINT_CONVERSION_COMPRESSED);
3617
Hai Shaloma20dcd72022-02-04 13:43:00 -08003618 der_len = i2d_PUBKEY((EVP_PKEY *) key, &der);
3619 EC_KEY_free(eckey);
Hai Shalom899fcc72020-10-19 14:38:18 -07003620 if (der_len <= 0) {
3621 wpa_printf(MSG_INFO, "OpenSSL: i2d_PUBKEY() failed: %s",
3622 ERR_error_string(ERR_get_error(), NULL));
3623 return NULL;
3624 }
3625
3626 buf = wpabuf_alloc_copy(der, der_len);
3627 OPENSSL_free(der);
3628 return buf;
Hai Shaloma20dcd72022-02-04 13:43:00 -08003629#endif /* OPENSSL_IS_BORINGSSL */
Sunil8cd6f4d2022-06-28 18:40:46 +00003630#endif /* OpenSSL version >= 3.0 */
Hai Shaloma20dcd72022-02-04 13:43:00 -08003631}
3632
3633
3634struct wpabuf * crypto_ec_key_get_ecprivate_key(struct crypto_ec_key *key,
3635 bool include_pub)
3636{
Sunil8cd6f4d2022-06-28 18:40:46 +00003637 EVP_PKEY *pkey = (EVP_PKEY *) key;
3638#if OPENSSL_VERSION_NUMBER >= 0x30000000L
3639 OSSL_ENCODER_CTX *ctx;
3640 int selection;
3641 unsigned char *pdata = NULL;
3642 size_t pdata_len = 0;
3643 struct wpabuf *buf;
3644 EVP_PKEY *copy = NULL;
3645
3646 selection = OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS |
3647 OSSL_KEYMGMT_SELECT_PRIVATE_KEY;
3648 if (include_pub) {
3649 selection |= OSSL_KEYMGMT_SELECT_PUBLIC_KEY;
3650 } else {
3651 /* Not including OSSL_KEYMGMT_SELECT_PUBLIC_KEY does not seem
3652 * to really be sufficient, so clone the key and explicitly
3653 * mark it not to include the public key. */
3654 copy = EVP_PKEY_dup(pkey);
3655 if (!copy)
3656 return NULL;
3657 EVP_PKEY_set_int_param(copy, OSSL_PKEY_PARAM_EC_INCLUDE_PUBLIC,
3658 0);
3659 pkey = copy;
3660 }
3661
3662 ctx = OSSL_ENCODER_CTX_new_for_pkey(pkey, selection, "DER",
3663 "type-specific", NULL);
3664 if (!ctx || OSSL_ENCODER_to_data(ctx, &pdata, &pdata_len) != 1) {
3665 OSSL_ENCODER_CTX_free(ctx);
3666 EVP_PKEY_free(copy);
3667 return NULL;
3668 }
3669 OSSL_ENCODER_CTX_free(ctx);
3670 buf = wpabuf_alloc_copy(pdata, pdata_len);
3671 OPENSSL_free(pdata);
3672 EVP_PKEY_free(copy);
3673 return buf;
3674#else /* OpenSSL version >= 3.0 */
Hai Shaloma20dcd72022-02-04 13:43:00 -08003675 EC_KEY *eckey;
3676 unsigned char *der = NULL;
3677 int der_len;
3678 struct wpabuf *buf;
3679 unsigned int key_flags;
3680
Sunil8cd6f4d2022-06-28 18:40:46 +00003681 eckey = EVP_PKEY_get1_EC_KEY(pkey);
Hai Shaloma20dcd72022-02-04 13:43:00 -08003682 if (!eckey)
3683 return NULL;
3684
3685 key_flags = EC_KEY_get_enc_flags(eckey);
3686 if (include_pub)
3687 key_flags &= ~EC_PKEY_NO_PUBKEY;
3688 else
3689 key_flags |= EC_PKEY_NO_PUBKEY;
3690 EC_KEY_set_enc_flags(eckey, key_flags);
3691
3692 EC_KEY_set_conv_form(eckey, POINT_CONVERSION_UNCOMPRESSED);
3693
3694 der_len = i2d_ECPrivateKey(eckey, &der);
3695 EC_KEY_free(eckey);
3696 if (der_len <= 0)
3697 return NULL;
3698 buf = wpabuf_alloc_copy(der, der_len);
3699 OPENSSL_free(der);
3700
3701 return buf;
Sunil8cd6f4d2022-06-28 18:40:46 +00003702#endif /* OpenSSL version >= 3.0 */
Hai Shaloma20dcd72022-02-04 13:43:00 -08003703}
3704
3705
3706struct wpabuf * crypto_ec_key_get_pubkey_point(struct crypto_ec_key *key,
3707 int prefix)
3708{
Sunil8cd6f4d2022-06-28 18:40:46 +00003709 EVP_PKEY *pkey = (EVP_PKEY *) key;
3710#if OPENSSL_VERSION_NUMBER >= 0x30000000L
3711 struct wpabuf *buf;
3712 unsigned char *pos;
3713 size_t pub_len = OSSL_PARAM_UNMODIFIED;
3714
3715 buf = NULL;
3716 if (!EVP_PKEY_is_a(pkey, "EC") ||
3717 EVP_PKEY_get_octet_string_param(pkey,
3718 OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY,
3719 NULL, 0, &pub_len) < 0 ||
3720 pub_len == OSSL_PARAM_UNMODIFIED ||
3721 !(buf = wpabuf_alloc(pub_len)) ||
3722 EVP_PKEY_get_octet_string_param(pkey,
3723 OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY,
3724 wpabuf_put(buf, pub_len),
3725 pub_len, NULL) != 1 ||
3726 wpabuf_head_u8(buf)[0] != 0x04) {
3727 wpa_printf(MSG_INFO,
3728 "OpenSSL: Failed to get encoded public key: %s",
3729 ERR_error_string(ERR_get_error(), NULL));
3730 wpabuf_free(buf);
3731 return NULL;
3732 }
3733
3734 if (!prefix) {
3735 /* Remove 0x04 prefix if requested */
3736 pos = wpabuf_mhead(buf);
3737 os_memmove(pos, pos + 1, pub_len - 1);
3738 buf->used--;
3739 }
3740
3741 return buf;
3742#else /* OpenSSL version >= 3.0 */
Hai Shaloma20dcd72022-02-04 13:43:00 -08003743 int len, res;
3744 EC_KEY *eckey;
3745 struct wpabuf *buf;
3746 unsigned char *pos;
3747
Sunil8cd6f4d2022-06-28 18:40:46 +00003748 eckey = EVP_PKEY_get1_EC_KEY(pkey);
Hai Shaloma20dcd72022-02-04 13:43:00 -08003749 if (!eckey)
3750 return NULL;
3751 EC_KEY_set_conv_form(eckey, POINT_CONVERSION_UNCOMPRESSED);
3752 len = i2o_ECPublicKey(eckey, NULL);
3753 if (len <= 0) {
3754 wpa_printf(MSG_ERROR,
3755 "OpenSSL: Failed to determine public key encoding length");
3756 EC_KEY_free(eckey);
3757 return NULL;
3758 }
3759
3760 buf = wpabuf_alloc(len);
3761 if (!buf) {
3762 EC_KEY_free(eckey);
3763 return NULL;
3764 }
3765
3766 pos = wpabuf_put(buf, len);
3767 res = i2o_ECPublicKey(eckey, &pos);
3768 EC_KEY_free(eckey);
3769 if (res != len) {
3770 wpa_printf(MSG_ERROR,
3771 "OpenSSL: Failed to encode public key (res=%d/%d)",
3772 res, len);
3773 wpabuf_free(buf);
3774 return NULL;
3775 }
3776
3777 if (!prefix) {
3778 /* Remove 0x04 prefix if requested */
3779 pos = wpabuf_mhead(buf);
3780 os_memmove(pos, pos + 1, len - 1);
3781 buf->used--;
3782 }
3783
3784 return buf;
Sunil8cd6f4d2022-06-28 18:40:46 +00003785#endif /* OpenSSL version >= 3.0 */
Hai Shaloma20dcd72022-02-04 13:43:00 -08003786}
3787
3788
Sunil8cd6f4d2022-06-28 18:40:46 +00003789struct crypto_ec_point *
Hai Shaloma20dcd72022-02-04 13:43:00 -08003790crypto_ec_key_get_public_key(struct crypto_ec_key *key)
3791{
Sunil8cd6f4d2022-06-28 18:40:46 +00003792 EVP_PKEY *pkey = (EVP_PKEY *) key;
3793#if OPENSSL_VERSION_NUMBER >= 0x30000000L
3794 char group[64];
3795 unsigned char pub[256];
3796 size_t len;
3797 EC_POINT *point = NULL;
3798 EC_GROUP *grp;
3799 int res = 0;
3800 OSSL_PARAM params[2];
Hai Shaloma20dcd72022-02-04 13:43:00 -08003801
Sunil8cd6f4d2022-06-28 18:40:46 +00003802 if (!EVP_PKEY_is_a(pkey, "EC") ||
3803 EVP_PKEY_get_utf8_string_param(pkey, OSSL_PKEY_PARAM_GROUP_NAME,
3804 group, sizeof(group), &len) != 1 ||
3805 EVP_PKEY_get_octet_string_param(pkey, OSSL_PKEY_PARAM_PUB_KEY,
3806 pub, sizeof(pub), &len) != 1)
3807 return NULL;
3808
3809 params[0] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME,
3810 group, 0);
3811 params[1] = OSSL_PARAM_construct_end();
3812 grp = EC_GROUP_new_from_params(params, NULL, NULL);
3813 if (!grp)
3814 goto fail;
3815 point = EC_POINT_new(grp);
3816 if (!point)
3817 goto fail;
3818 res = EC_POINT_oct2point(grp, point, pub, len, NULL);
3819
3820fail:
3821 if (res != 1) {
3822 EC_POINT_free(point);
3823 point = NULL;
3824 }
3825
3826 EC_GROUP_free(grp);
3827
3828 return (struct crypto_ec_point *) point;
3829#else /* OpenSSL version >= 3.0 */
3830 const EC_KEY *eckey;
3831 const EC_POINT *point;
3832 const EC_GROUP *group;
3833
3834 eckey = EVP_PKEY_get0_EC_KEY(pkey);
Hai Shaloma20dcd72022-02-04 13:43:00 -08003835 if (!eckey)
3836 return NULL;
Sunil8cd6f4d2022-06-28 18:40:46 +00003837 group = EC_KEY_get0_group(eckey);
3838 if (!group)
3839 return NULL;
3840 point = EC_KEY_get0_public_key(eckey);
3841 if (!point)
3842 return NULL;
3843 return (struct crypto_ec_point *) EC_POINT_dup(point, group);
3844#endif /* OpenSSL version >= 3.0 */
Hai Shaloma20dcd72022-02-04 13:43:00 -08003845}
3846
3847
Sunil8cd6f4d2022-06-28 18:40:46 +00003848struct crypto_bignum *
Hai Shaloma20dcd72022-02-04 13:43:00 -08003849crypto_ec_key_get_private_key(struct crypto_ec_key *key)
3850{
Sunil8cd6f4d2022-06-28 18:40:46 +00003851 EVP_PKEY *pkey = (EVP_PKEY *) key;
3852#if OPENSSL_VERSION_NUMBER >= 0x30000000L
3853 BIGNUM *bn = NULL;
Hai Shaloma20dcd72022-02-04 13:43:00 -08003854
Sunil8cd6f4d2022-06-28 18:40:46 +00003855 if (!EVP_PKEY_is_a(pkey, "EC") ||
3856 EVP_PKEY_get_bn_param(pkey, OSSL_PKEY_PARAM_PRIV_KEY, &bn) != 1)
3857 return NULL;
3858 return (struct crypto_bignum *) bn;
3859#else /* OpenSSL version >= 3.0 */
3860 const EC_KEY *eckey;
3861 const BIGNUM *bn;
3862
3863 eckey = EVP_PKEY_get0_EC_KEY(pkey);
Hai Shaloma20dcd72022-02-04 13:43:00 -08003864 if (!eckey)
3865 return NULL;
Sunil8cd6f4d2022-06-28 18:40:46 +00003866 bn = EC_KEY_get0_private_key(eckey);
3867 if (!bn)
3868 return NULL;
3869 return (struct crypto_bignum *) BN_dup(bn);
3870#endif /* OpenSSL version >= 3.0 */
Hai Shalom899fcc72020-10-19 14:38:18 -07003871}
3872
3873
3874struct wpabuf * crypto_ec_key_sign(struct crypto_ec_key *key, const u8 *data,
3875 size_t len)
3876{
3877 EVP_PKEY_CTX *pkctx;
3878 struct wpabuf *sig_der;
3879 size_t sig_len;
3880
Hai Shaloma20dcd72022-02-04 13:43:00 -08003881 sig_len = EVP_PKEY_size((EVP_PKEY *) key);
Hai Shalom899fcc72020-10-19 14:38:18 -07003882 sig_der = wpabuf_alloc(sig_len);
3883 if (!sig_der)
3884 return NULL;
3885
Hai Shaloma20dcd72022-02-04 13:43:00 -08003886 pkctx = EVP_PKEY_CTX_new((EVP_PKEY *) key, NULL);
Hai Shalom899fcc72020-10-19 14:38:18 -07003887 if (!pkctx ||
3888 EVP_PKEY_sign_init(pkctx) <= 0 ||
3889 EVP_PKEY_sign(pkctx, wpabuf_put(sig_der, 0), &sig_len,
3890 data, len) <= 0) {
3891 wpabuf_free(sig_der);
3892 sig_der = NULL;
3893 } else {
3894 wpabuf_put(sig_der, sig_len);
3895 }
3896
3897 EVP_PKEY_CTX_free(pkctx);
3898 return sig_der;
3899}
3900
3901
Sunil Ravia04bd252022-05-02 22:54:18 -07003902static int openssl_evp_pkey_ec_prime_len(struct crypto_ec_key *key)
Hai Shaloma20dcd72022-02-04 13:43:00 -08003903{
Sunil Ravia04bd252022-05-02 22:54:18 -07003904#if OPENSSL_VERSION_NUMBER >= 0x30000000L
3905 char gname[50];
3906 int nid;
3907 EC_GROUP *group;
3908 BIGNUM *prime = NULL;
3909 int prime_len = -1;
3910
3911 if (EVP_PKEY_get_group_name((EVP_PKEY *) key, gname, sizeof(gname),
3912 NULL) != 1)
3913 return -1;
3914 nid = OBJ_txt2nid(gname);
3915 group = EC_GROUP_new_by_curve_name(nid);
3916 prime = BN_new();
3917 if (!group || !prime)
3918 return -1;
3919 if (EC_GROUP_get_curve(group, prime, NULL, NULL, NULL) == 1)
3920 prime_len = BN_num_bytes(prime);
3921 EC_GROUP_free(group);
3922 BN_free(prime);
3923 return prime_len;
3924#else
Hai Shaloma20dcd72022-02-04 13:43:00 -08003925 const EC_GROUP *group;
3926 const EC_KEY *eckey;
3927 BIGNUM *prime = NULL;
Sunil Ravia04bd252022-05-02 22:54:18 -07003928 int prime_len = -1;
3929
3930 eckey = EVP_PKEY_get0_EC_KEY((EVP_PKEY *) key);
3931 if (!eckey)
3932 goto fail;
3933 group = EC_KEY_get0_group(eckey);
3934 prime = BN_new();
3935 if (!prime || !group ||
3936 !EC_GROUP_get_curve(group, prime, NULL, NULL, NULL))
3937 goto fail;
3938 prime_len = BN_num_bytes(prime);
3939fail:
3940 BN_free(prime);
3941 return prime_len;
3942#endif
3943}
3944
3945
3946struct wpabuf * crypto_ec_key_sign_r_s(struct crypto_ec_key *key,
3947 const u8 *data, size_t len)
3948{
Hai Shaloma20dcd72022-02-04 13:43:00 -08003949 ECDSA_SIG *sig = NULL;
3950 const BIGNUM *r, *s;
3951 u8 *r_buf, *s_buf;
3952 struct wpabuf *buf;
3953 const unsigned char *p;
3954 int prime_len;
3955
Sunil Ravia04bd252022-05-02 22:54:18 -07003956 prime_len = openssl_evp_pkey_ec_prime_len(key);
3957 if (prime_len < 0)
3958 return NULL;
3959
Hai Shaloma20dcd72022-02-04 13:43:00 -08003960 buf = crypto_ec_key_sign(key, data, len);
3961 if (!buf)
3962 return NULL;
3963
3964 /* Extract (r,s) from Ecdsa-Sig-Value */
Hai Shaloma20dcd72022-02-04 13:43:00 -08003965
3966 p = wpabuf_head(buf);
3967 sig = d2i_ECDSA_SIG(NULL, &p, wpabuf_len(buf));
3968 if (!sig)
3969 goto fail;
3970 ECDSA_SIG_get0(sig, &r, &s);
3971
3972 /* Re-use wpabuf returned by crypto_ec_key_sign() */
3973 buf->used = 0;
3974 r_buf = wpabuf_put(buf, prime_len);
3975 s_buf = wpabuf_put(buf, prime_len);
3976 if (crypto_bignum_to_bin((const struct crypto_bignum *) r, r_buf,
3977 prime_len, prime_len) < 0 ||
3978 crypto_bignum_to_bin((const struct crypto_bignum *) s, s_buf,
3979 prime_len, prime_len) < 0)
3980 goto fail;
3981
3982out:
Hai Shaloma20dcd72022-02-04 13:43:00 -08003983 ECDSA_SIG_free(sig);
3984 return buf;
3985fail:
3986 wpabuf_clear_free(buf);
3987 buf = NULL;
3988 goto out;
3989}
3990
3991
Hai Shalom899fcc72020-10-19 14:38:18 -07003992int crypto_ec_key_verify_signature(struct crypto_ec_key *key, const u8 *data,
3993 size_t len, const u8 *sig, size_t sig_len)
3994{
3995 EVP_PKEY_CTX *pkctx;
3996 int ret;
3997
Hai Shaloma20dcd72022-02-04 13:43:00 -08003998 pkctx = EVP_PKEY_CTX_new((EVP_PKEY *) key, NULL);
Hai Shalom899fcc72020-10-19 14:38:18 -07003999 if (!pkctx || EVP_PKEY_verify_init(pkctx) <= 0) {
4000 EVP_PKEY_CTX_free(pkctx);
4001 return -1;
4002 }
4003
4004 ret = EVP_PKEY_verify(pkctx, sig, sig_len, data, len);
4005 EVP_PKEY_CTX_free(pkctx);
4006 if (ret == 1)
4007 return 1; /* signature ok */
4008 if (ret == 0)
4009 return 0; /* incorrect signature */
4010 return -1;
4011}
4012
4013
Hai Shaloma20dcd72022-02-04 13:43:00 -08004014int crypto_ec_key_verify_signature_r_s(struct crypto_ec_key *key,
4015 const u8 *data, size_t len,
4016 const u8 *r, size_t r_len,
4017 const u8 *s, size_t s_len)
4018{
4019 ECDSA_SIG *sig;
4020 BIGNUM *r_bn, *s_bn;
4021 unsigned char *der = NULL;
4022 int der_len;
4023 int ret = -1;
4024
4025 r_bn = BN_bin2bn(r, r_len, NULL);
4026 s_bn = BN_bin2bn(s, s_len, NULL);
4027 sig = ECDSA_SIG_new();
4028 if (!r_bn || !s_bn || !sig || ECDSA_SIG_set0(sig, r_bn, s_bn) != 1)
4029 goto fail;
4030 r_bn = NULL;
4031 s_bn = NULL;
4032
4033 der_len = i2d_ECDSA_SIG(sig, &der);
4034 if (der_len <= 0) {
4035 wpa_printf(MSG_DEBUG,
4036 "OpenSSL: Could not DER encode signature");
4037 goto fail;
4038 }
4039
4040 ret = crypto_ec_key_verify_signature(key, data, len, der, der_len);
4041
4042fail:
4043 OPENSSL_free(der);
4044 BN_free(r_bn);
4045 BN_free(s_bn);
4046 ECDSA_SIG_free(sig);
4047 return ret;
4048}
4049
4050
Hai Shalom899fcc72020-10-19 14:38:18 -07004051int crypto_ec_key_group(struct crypto_ec_key *key)
4052{
Sunil Ravia04bd252022-05-02 22:54:18 -07004053#if OPENSSL_VERSION_NUMBER >= 0x30000000L
4054 char gname[50];
4055 int nid;
4056
4057 if (EVP_PKEY_get_group_name((EVP_PKEY *) key, gname, sizeof(gname),
4058 NULL) != 1)
4059 return -1;
4060 nid = OBJ_txt2nid(gname);
4061#else
Hai Shaloma20dcd72022-02-04 13:43:00 -08004062 const EC_KEY *eckey;
Hai Shalom899fcc72020-10-19 14:38:18 -07004063 const EC_GROUP *group;
4064 int nid;
4065
Hai Shaloma20dcd72022-02-04 13:43:00 -08004066 eckey = EVP_PKEY_get0_EC_KEY((EVP_PKEY *) key);
4067 if (!eckey)
4068 return -1;
4069 group = EC_KEY_get0_group(eckey);
Hai Shalom899fcc72020-10-19 14:38:18 -07004070 if (!group)
4071 return -1;
4072 nid = EC_GROUP_get_curve_name(group);
Sunil Ravia04bd252022-05-02 22:54:18 -07004073#endif
Hai Shalom899fcc72020-10-19 14:38:18 -07004074 switch (nid) {
4075 case NID_X9_62_prime256v1:
4076 return 19;
4077 case NID_secp384r1:
4078 return 20;
4079 case NID_secp521r1:
4080 return 21;
Hai Shaloma20dcd72022-02-04 13:43:00 -08004081#ifdef NID_brainpoolP256r1
4082 case NID_brainpoolP256r1:
4083 return 28;
4084#endif /* NID_brainpoolP256r1 */
4085#ifdef NID_brainpoolP384r1
4086 case NID_brainpoolP384r1:
4087 return 29;
4088#endif /* NID_brainpoolP384r1 */
4089#ifdef NID_brainpoolP512r1
4090 case NID_brainpoolP512r1:
4091 return 30;
4092#endif /* NID_brainpoolP512r1 */
Hai Shalom899fcc72020-10-19 14:38:18 -07004093 }
Hai Shaloma20dcd72022-02-04 13:43:00 -08004094 wpa_printf(MSG_ERROR, "OpenSSL: Unsupported curve (nid=%d) in EC key",
4095 nid);
Hai Shalom899fcc72020-10-19 14:38:18 -07004096 return -1;
4097}
4098
Hai Shaloma20dcd72022-02-04 13:43:00 -08004099
4100int crypto_ec_key_cmp(struct crypto_ec_key *key1, struct crypto_ec_key *key2)
4101{
Sunil Ravia04bd252022-05-02 22:54:18 -07004102#if OPENSSL_VERSION_NUMBER >= 0x30000000L
4103 if (EVP_PKEY_eq((EVP_PKEY *) key1, (EVP_PKEY *) key2) != 1)
4104 return -1;
4105#else
Hai Shaloma20dcd72022-02-04 13:43:00 -08004106 if (EVP_PKEY_cmp((EVP_PKEY *) key1, (EVP_PKEY *) key2) != 1)
4107 return -1;
Sunil Ravia04bd252022-05-02 22:54:18 -07004108#endif
Hai Shaloma20dcd72022-02-04 13:43:00 -08004109 return 0;
4110}
4111
4112
4113void crypto_ec_key_debug_print(const struct crypto_ec_key *key,
4114 const char *title)
4115{
4116 BIO *out;
4117 size_t rlen;
4118 char *txt;
4119 int res;
4120
4121 out = BIO_new(BIO_s_mem());
4122 if (!out)
4123 return;
4124
4125 EVP_PKEY_print_private(out, (EVP_PKEY *) key, 0, NULL);
4126 rlen = BIO_ctrl_pending(out);
4127 txt = os_malloc(rlen + 1);
4128 if (txt) {
4129 res = BIO_read(out, txt, rlen);
4130 if (res > 0) {
4131 txt[res] = '\0';
4132 wpa_printf(MSG_DEBUG, "%s: %s", title, txt);
4133 }
4134 os_free(txt);
4135 }
4136 BIO_free(out);
4137}
4138
4139
4140struct wpabuf * crypto_pkcs7_get_certificates(const struct wpabuf *pkcs7)
4141{
4142#ifdef OPENSSL_IS_BORINGSSL
4143 CBS pkcs7_cbs;
4144#else /* OPENSSL_IS_BORINGSSL */
4145 PKCS7 *p7 = NULL;
4146 const unsigned char *p = wpabuf_head(pkcs7);
4147#endif /* OPENSSL_IS_BORINGSSL */
4148 STACK_OF(X509) *certs;
4149 int i, num;
4150 BIO *out = NULL;
4151 size_t rlen;
4152 struct wpabuf *pem = NULL;
4153 int res;
4154
4155#ifdef OPENSSL_IS_BORINGSSL
4156 certs = sk_X509_new_null();
4157 if (!certs)
4158 goto fail;
4159 CBS_init(&pkcs7_cbs, wpabuf_head(pkcs7), wpabuf_len(pkcs7));
4160 if (!PKCS7_get_certificates(certs, &pkcs7_cbs)) {
4161 wpa_printf(MSG_INFO,
4162 "OpenSSL: Could not parse PKCS#7 object: %s",
4163 ERR_error_string(ERR_get_error(), NULL));
4164 goto fail;
4165 }
4166#else /* OPENSSL_IS_BORINGSSL */
4167 p7 = d2i_PKCS7(NULL, &p, wpabuf_len(pkcs7));
4168 if (!p7) {
4169 wpa_printf(MSG_INFO,
4170 "OpenSSL: Could not parse PKCS#7 object: %s",
4171 ERR_error_string(ERR_get_error(), NULL));
4172 goto fail;
4173 }
4174
4175 switch (OBJ_obj2nid(p7->type)) {
4176 case NID_pkcs7_signed:
4177 certs = p7->d.sign->cert;
4178 break;
4179 case NID_pkcs7_signedAndEnveloped:
4180 certs = p7->d.signed_and_enveloped->cert;
4181 break;
4182 default:
4183 certs = NULL;
4184 break;
4185 }
4186#endif /* OPENSSL_IS_BORINGSSL */
4187
4188 if (!certs || ((num = sk_X509_num(certs)) == 0)) {
4189 wpa_printf(MSG_INFO,
4190 "OpenSSL: No certificates found in PKCS#7 object");
4191 goto fail;
4192 }
4193
4194 out = BIO_new(BIO_s_mem());
4195 if (!out)
4196 goto fail;
4197
4198 for (i = 0; i < num; i++) {
4199 X509 *cert = sk_X509_value(certs, i);
4200
4201 PEM_write_bio_X509(out, cert);
4202 }
4203
4204 rlen = BIO_ctrl_pending(out);
4205 pem = wpabuf_alloc(rlen);
4206 if (!pem)
4207 goto fail;
4208 res = BIO_read(out, wpabuf_put(pem, 0), rlen);
4209 if (res <= 0) {
4210 wpabuf_free(pem);
4211 pem = NULL;
4212 goto fail;
4213 }
4214 wpabuf_put(pem, res);
4215
4216fail:
4217#ifdef OPENSSL_IS_BORINGSSL
4218 if (certs)
4219 sk_X509_pop_free(certs, X509_free);
4220#else /* OPENSSL_IS_BORINGSSL */
4221 PKCS7_free(p7);
4222#endif /* OPENSSL_IS_BORINGSSL */
4223 if (out)
4224 BIO_free_all(out);
4225
4226 return pem;
4227}
4228
4229
4230struct crypto_csr * crypto_csr_init()
4231{
4232 return (struct crypto_csr *)X509_REQ_new();
4233}
4234
4235
4236struct crypto_csr * crypto_csr_verify(const struct wpabuf *req)
4237{
4238 X509_REQ *csr;
4239 EVP_PKEY *pkey = NULL;
4240 const u8 *der = wpabuf_head(req);
4241
4242 csr = d2i_X509_REQ(NULL, &der, wpabuf_len(req));
4243 if (!csr)
4244 return NULL;
4245
4246 pkey = X509_REQ_get_pubkey((X509_REQ *)csr);
4247 if (!pkey)
4248 goto fail;
4249
4250 if (X509_REQ_verify((X509_REQ *)csr, pkey) != 1)
4251 goto fail;
4252
4253 return (struct crypto_csr *)csr;
4254fail:
4255 X509_REQ_free(csr);
4256 return NULL;
4257}
4258
4259
4260void crypto_csr_deinit(struct crypto_csr *csr)
4261{
4262 X509_REQ_free((X509_REQ *)csr);
4263}
4264
4265
4266int crypto_csr_set_ec_public_key(struct crypto_csr *csr, struct crypto_ec_key *key)
4267{
4268 if (!X509_REQ_set_pubkey((X509_REQ *)csr, (EVP_PKEY *)key))
4269 return -1;
4270
4271 return 0;
4272}
4273
4274
4275int crypto_csr_set_name(struct crypto_csr *csr, enum crypto_csr_name type,
4276 const char *name)
4277{
4278 X509_NAME *n;
4279 int nid;
4280
4281 switch (type) {
4282 case CSR_NAME_CN:
4283 nid = NID_commonName;
4284 break;
4285 case CSR_NAME_SN:
4286 nid = NID_surname;
4287 break;
4288 case CSR_NAME_C:
4289 nid = NID_countryName;
4290 break;
4291 case CSR_NAME_O:
4292 nid = NID_organizationName;
4293 break;
4294 case CSR_NAME_OU:
4295 nid = NID_organizationalUnitName;
4296 break;
4297 default:
4298 return -1;
4299 }
4300
4301 n = X509_REQ_get_subject_name((X509_REQ *) csr);
4302 if (!n)
4303 return -1;
4304
4305#if OPENSSL_VERSION_NUMBER < 0x10100000L
4306 if (!X509_NAME_add_entry_by_NID(n, nid, MBSTRING_UTF8,
4307 (unsigned char *) name,
4308 os_strlen(name), -1, 0))
4309 return -1;
4310#else
4311 if (!X509_NAME_add_entry_by_NID(n, nid, MBSTRING_UTF8,
4312 (const unsigned char *) name,
4313 os_strlen(name), -1, 0))
4314 return -1;
4315#endif
4316
4317 return 0;
4318}
4319
4320
4321int crypto_csr_set_attribute(struct crypto_csr *csr, enum crypto_csr_attr attr,
4322 int attr_type, const u8 *value, size_t len)
4323{
4324 int nid;
4325
4326 switch (attr) {
4327 case CSR_ATTR_CHALLENGE_PASSWORD:
4328 nid = NID_pkcs9_challengePassword;
4329 break;
4330 default:
4331 return -1;
4332 }
4333
4334 if (!X509_REQ_add1_attr_by_NID((X509_REQ *) csr, nid, attr_type, value,
4335 len))
4336 return -1;
4337
4338 return 0;
4339}
4340
4341
4342const u8 * crypto_csr_get_attribute(struct crypto_csr *csr,
4343 enum crypto_csr_attr attr,
4344 size_t *len, int *type)
4345{
4346 X509_ATTRIBUTE *attrib;
4347 ASN1_TYPE *attrib_type;
4348 ASN1_STRING *data;
4349 int loc;
4350 int nid;
4351
4352 switch (attr) {
4353 case CSR_ATTR_CHALLENGE_PASSWORD:
4354 nid = NID_pkcs9_challengePassword;
4355 break;
4356 default:
4357 return NULL;
4358 }
4359
4360 loc = X509_REQ_get_attr_by_NID((X509_REQ *) csr, nid, -1);
4361 if (loc < 0)
4362 return NULL;
4363
4364 attrib = X509_REQ_get_attr((X509_REQ *) csr, loc);
4365 if (!attrib)
4366 return NULL;
4367
4368 attrib_type = X509_ATTRIBUTE_get0_type(attrib, 0);
4369 if (!attrib_type)
4370 return NULL;
4371 *type = ASN1_TYPE_get(attrib_type);
4372 data = X509_ATTRIBUTE_get0_data(attrib, 0, *type, NULL);
4373 if (!data)
4374 return NULL;
4375 *len = ASN1_STRING_length(data);
4376 return ASN1_STRING_get0_data(data);
4377}
4378
4379
4380struct wpabuf * crypto_csr_sign(struct crypto_csr *csr,
4381 struct crypto_ec_key *key,
4382 enum crypto_hash_alg algo)
4383{
4384 const EVP_MD *sign_md;
4385 struct wpabuf *buf;
4386 unsigned char *der = NULL;
4387 int der_len;
4388
4389 switch (algo) {
4390 case CRYPTO_HASH_ALG_SHA256:
4391 sign_md = EVP_sha256();
4392 break;
4393 case CRYPTO_HASH_ALG_SHA384:
4394 sign_md = EVP_sha384();
4395 break;
4396 case CRYPTO_HASH_ALG_SHA512:
4397 sign_md = EVP_sha512();
4398 break;
4399 default:
4400 return NULL;
4401 }
4402
4403 if (!X509_REQ_sign((X509_REQ *) csr, (EVP_PKEY *) key, sign_md))
4404 return NULL;
4405
4406 der_len = i2d_X509_REQ((X509_REQ *) csr, &der);
4407 if (der_len < 0)
4408 return NULL;
4409
4410 buf = wpabuf_alloc_copy(der, der_len);
4411 OPENSSL_free(der);
4412
4413 return buf;
4414}
4415
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004416#endif /* CONFIG_ECC */
Sunil Ravia04bd252022-05-02 22:54:18 -07004417
4418
4419static EVP_PKEY * crypto_rsa_key_read_public(FILE *f)
4420{
4421 EVP_PKEY *pkey;
4422 X509 *x509;
Sunil8cd6f4d2022-06-28 18:40:46 +00004423 const ASN1_TIME *not_before, *not_after;
4424 int res_before, res_after;
Sunil Ravia04bd252022-05-02 22:54:18 -07004425
4426 pkey = PEM_read_PUBKEY(f, NULL, NULL, NULL);
4427 if (pkey)
4428 return pkey;
4429
4430 rewind(f);
4431 x509 = PEM_read_X509(f, NULL, NULL, NULL);
4432 if (!x509)
4433 return NULL;
4434
Sunil8cd6f4d2022-06-28 18:40:46 +00004435 not_before = X509_get0_notBefore(x509);
4436 not_after = X509_get0_notAfter(x509);
4437 if (!not_before || !not_after)
4438 goto fail;
4439 res_before = X509_cmp_current_time(not_before);
4440 res_after = X509_cmp_current_time(not_after);
4441 if (!res_before || !res_after)
4442 goto fail;
4443 if (res_before > 0 || res_after < 0) {
4444 wpa_printf(MSG_INFO,
4445 "OpenSSL: Certificate for RSA public key is not valid at this time (%d %d)",
4446 res_before, res_after);
4447 goto fail;
4448 }
4449
Sunil Ravia04bd252022-05-02 22:54:18 -07004450 pkey = X509_get_pubkey(x509);
4451 X509_free(x509);
4452
4453 if (!pkey)
4454 return NULL;
4455 if (EVP_PKEY_base_id(pkey) != EVP_PKEY_RSA) {
Sunil8cd6f4d2022-06-28 18:40:46 +00004456 wpa_printf(MSG_INFO, "OpenSSL: No RSA public key found");
Sunil Ravia04bd252022-05-02 22:54:18 -07004457 EVP_PKEY_free(pkey);
4458 return NULL;
4459 }
4460
4461 return pkey;
Sunil8cd6f4d2022-06-28 18:40:46 +00004462fail:
4463 X509_free(x509);
4464 return NULL;
Sunil Ravia04bd252022-05-02 22:54:18 -07004465}
4466
4467
4468struct crypto_rsa_key * crypto_rsa_key_read(const char *file, bool private_key)
4469{
4470 FILE *f;
4471 EVP_PKEY *pkey;
4472
4473 f = fopen(file, "r");
4474 if (!f)
4475 return NULL;
4476 if (private_key)
4477 pkey = PEM_read_PrivateKey(f, NULL, NULL, NULL);
4478 else
4479 pkey = crypto_rsa_key_read_public(f);
4480 fclose(f);
4481 return (struct crypto_rsa_key *) pkey;
4482}
4483
4484
4485#ifndef OPENSSL_NO_SHA256
4486
4487struct wpabuf * crypto_rsa_oaep_sha256_encrypt(struct crypto_rsa_key *key,
4488 const struct wpabuf *in)
4489{
4490#if !defined(LIBRESSL_VERSION_NUMBER) || LIBRESSL_VERSION_NUMBER >= 0x30400000L
4491 EVP_PKEY *pkey = (EVP_PKEY *) key;
4492 EVP_PKEY_CTX *pkctx;
4493 struct wpabuf *res = NULL;
4494 size_t outlen;
4495
4496 pkctx = EVP_PKEY_CTX_new(pkey, NULL);
4497 if (!pkctx)
4498 goto fail;
4499
4500 if (EVP_PKEY_encrypt_init(pkctx) != 1 ||
4501 EVP_PKEY_CTX_set_rsa_padding(pkctx, RSA_PKCS1_OAEP_PADDING) <= 0 ||
4502 EVP_PKEY_CTX_set_rsa_oaep_md(pkctx, EVP_sha256()) <= 0 ||
4503 EVP_PKEY_encrypt(pkctx, NULL, &outlen, wpabuf_head(in),
4504 wpabuf_len(in)) != 1 ||
4505 !(res = wpabuf_alloc(outlen)) ||
4506 EVP_PKEY_encrypt(pkctx, wpabuf_put(res, 0), &outlen,
4507 wpabuf_head(in), wpabuf_len(in)) != 1) {
4508 wpabuf_free(res);
4509 res = NULL;
4510 goto fail;
4511 }
4512 wpabuf_put(res, outlen);
4513
4514fail:
4515 EVP_PKEY_CTX_free(pkctx);
4516 return res;
4517#else
4518 wpa_printf(MSG_ERROR, "%s() not supported", __func__);
4519 return NULL;
4520#endif
4521}
4522
4523
4524struct wpabuf * crypto_rsa_oaep_sha256_decrypt(struct crypto_rsa_key *key,
4525 const struct wpabuf *in)
4526{
4527#if !defined(LIBRESSL_VERSION_NUMBER) || LIBRESSL_VERSION_NUMBER >= 0x30400000L
4528 EVP_PKEY *pkey = (EVP_PKEY *) key;
4529 EVP_PKEY_CTX *pkctx;
4530 struct wpabuf *res = NULL;
4531 size_t outlen;
4532
4533 pkctx = EVP_PKEY_CTX_new(pkey, NULL);
4534 if (!pkctx)
4535 goto fail;
4536
4537 if (EVP_PKEY_decrypt_init(pkctx) != 1 ||
4538 EVP_PKEY_CTX_set_rsa_padding(pkctx, RSA_PKCS1_OAEP_PADDING) <= 0 ||
4539 EVP_PKEY_CTX_set_rsa_oaep_md(pkctx, EVP_sha256()) <= 0 ||
4540 EVP_PKEY_decrypt(pkctx, NULL, &outlen, wpabuf_head(in),
4541 wpabuf_len(in)) != 1 ||
4542 !(res = wpabuf_alloc(outlen)) ||
4543 EVP_PKEY_decrypt(pkctx, wpabuf_put(res, 0), &outlen,
4544 wpabuf_head(in), wpabuf_len(in)) != 1) {
4545 wpabuf_free(res);
4546 res = NULL;
4547 goto fail;
4548 }
4549 wpabuf_put(res, outlen);
4550
4551fail:
4552 EVP_PKEY_CTX_free(pkctx);
4553 return res;
4554#else
4555 wpa_printf(MSG_ERROR, "%s() not supported", __func__);
4556 return NULL;
4557#endif
4558}
4559
4560#endif /* OPENSSL_NO_SHA256 */
4561
4562
4563void crypto_rsa_key_free(struct crypto_rsa_key *key)
4564{
4565 EVP_PKEY_free((EVP_PKEY *) key);
4566}
4567
4568
Sunil Ravi89eba102022-09-13 21:04:37 -07004569#ifdef CONFIG_DPP3
4570
4571#define HPKE_MAX_SHARED_SECRET_LEN 66
4572#define HPKE_MAX_HASH_LEN 64
4573#define HPKE_MAX_KEY_LEN 32
4574#define HPKE_MAX_NONCE_LEN 12
4575#define HPKE_MAX_PUB_LEN (1 + 2 * 66)
4576
4577struct hpke_context {
4578 /* KEM */
4579 enum hpke_kem_id kem_id;
4580 int kem_nid;
4581 int iana_group;
4582 size_t n_pk;
4583 size_t n_secret;
4584 const EVP_MD *kem_h;
4585 size_t kem_n_h;
4586
4587 /* KDF */
4588 enum hpke_kdf_id kdf_id;
4589 const EVP_MD *kdf_h;
4590 size_t n_h;
4591
4592 /* AEAD */
4593 enum hpke_aead_id aead_id;
4594 const EVP_CIPHER *cipher;
4595 size_t n_k;
4596 size_t n_n;
4597 size_t n_t;
4598 u8 key[HPKE_MAX_KEY_LEN];
4599 u8 base_nonce[HPKE_MAX_NONCE_LEN];
4600};
4601
4602
4603static void hpke_free_context(struct hpke_context *ctx)
4604{
4605 bin_clear_free(ctx, sizeof(*ctx));
4606}
4607
4608
4609static struct hpke_context * hpke_get_context(enum hpke_kem_id kem_id,
4610 enum hpke_kdf_id kdf_id,
4611 enum hpke_aead_id aead_id,
4612 struct crypto_ec_key *key)
4613{
4614 struct hpke_context *ctx;
4615 int group;
4616
4617 ctx = os_zalloc(sizeof(*ctx));
4618 if (!ctx)
4619 return NULL;
4620
4621 ctx->kem_id = kem_id;
4622 switch (kem_id) {
4623 case HPKE_DHKEM_P256_HKDF_SHA256:
4624 ctx->kem_nid = NID_X9_62_prime256v1;
4625 ctx->iana_group = 19;
4626 ctx->n_pk = 65;
4627 ctx->n_secret = 32;
4628 ctx->kem_h = EVP_sha256();
4629 ctx->kem_n_h = 32;
4630 break;
4631 case HPKE_DHKEM_P384_HKDF_SHA384:
4632 ctx->kem_nid = NID_secp384r1;
4633 ctx->iana_group = 20;
4634 ctx->n_pk = 97;
4635 ctx->n_secret = 48;
4636 ctx->kem_h = EVP_sha384();
4637 ctx->kem_n_h = 48;
4638 break;
4639 case HPKE_DHKEM_P521_HKDF_SHA512:
4640 ctx->kem_nid = NID_secp521r1;
4641 ctx->iana_group = 21;
4642 ctx->n_pk = 133;
4643 ctx->n_secret = 64;
4644 ctx->kem_h = EVP_sha512();
4645 ctx->kem_n_h = 64;
4646 break;
4647 default:
4648 goto fail;
4649 }
4650
4651 ctx->kdf_id = kdf_id;
4652 switch (kdf_id) {
4653 case HPKE_KDF_HKDF_SHA256:
4654 ctx->kdf_h = EVP_sha256();
4655 ctx->n_h = 32;
4656 break;
4657 case HPKE_KDF_HKDF_SHA384:
4658 ctx->kdf_h = EVP_sha384();
4659 ctx->n_h = 48;
4660 break;
4661 case HPKE_KDF_HKDF_SHA512:
4662 ctx->kdf_h = EVP_sha512();
4663 ctx->n_h = 64;
4664 break;
4665 default:
4666 goto fail;
4667 }
4668
4669 ctx->aead_id = aead_id;
4670 switch (aead_id) {
4671 case HPKE_AEAD_AES_128_GCM:
4672 ctx->cipher = EVP_aes_128_gcm();
4673 ctx->n_k = 16;
4674 ctx->n_n = 12;
4675 ctx->n_t = 16;
4676 break;
4677 case HPKE_AEAD_AES_256_GCM:
4678 ctx->cipher = EVP_aes_256_gcm();
4679 ctx->n_k = 32;
4680 ctx->n_n = 12;
4681 ctx->n_t = 16;
4682 break;
4683 default:
4684 goto fail;
4685 }
4686
4687 /* Convert BP-256/384/512 to P-256/384/521 for DPP */
4688 group = crypto_ec_key_group(key);
4689 if (group == 28 && ctx->iana_group == 19) {
4690 ctx->iana_group = 28;
4691 } else if (group == 29 && ctx->iana_group == 20) {
4692 ctx->iana_group = 29;
4693 } else if (group == 30 && ctx->iana_group == 21) {
4694 ctx->iana_group = 30;
4695 ctx->n_pk = 129;
4696 }
4697 if (group != ctx->iana_group) {
4698 wpa_printf(MSG_INFO, "OpenSSL:%s:group mismatch (%d != %d)",
4699 __func__, group, ctx->iana_group);
4700 goto fail;
4701 }
4702
4703 return ctx;
4704fail:
4705 hpke_free_context(ctx);
4706 return NULL;
4707}
4708
4709
4710static size_t hpke_suite_id(struct hpke_context *ctx, bool kem, u8 *suite_id)
4711{
4712 size_t suite_id_len;
4713
4714 if (kem) {
4715 os_memcpy(suite_id, "KEM", 3);
4716 WPA_PUT_BE16(&suite_id[3], ctx->kem_id);
4717 suite_id_len = 5;
4718 } else {
4719 os_memcpy(suite_id, "HPKE", 4);
4720 WPA_PUT_BE16(&suite_id[4], ctx->kem_id);
4721 WPA_PUT_BE16(&suite_id[6], ctx->kdf_id);
4722 WPA_PUT_BE16(&suite_id[8], ctx->aead_id);
4723 suite_id_len = 10;
4724 }
4725 return suite_id_len;
4726}
4727
4728
4729static int hpke_labeled_extract(struct hpke_context *ctx, bool kem,
4730 const u8 *salt, size_t salt_len,
4731 const char *label,
4732 const u8 *ikm, size_t ikm_len, u8 *prk)
4733{
4734 u8 zero[HPKE_MAX_HASH_LEN];
4735 u8 suite_id[10];
4736 size_t suite_id_len;
4737 unsigned int mdlen = kem ? ctx->kem_n_h : ctx->n_h;
4738#if OPENSSL_VERSION_NUMBER >= 0x30000000L
4739 EVP_MAC *hmac;
4740 OSSL_PARAM params[2];
4741 EVP_MAC_CTX *hctx;
4742 size_t mlen;
4743 int res;
4744#else /* OpenSSL version >= 3.0 */
4745 HMAC_CTX *hctx;
4746 int res;
4747#endif /* OpenSSL version >= 3.0 */
4748
4749 if (!salt || !salt_len) {
4750 salt_len = mdlen;
4751 os_memset(zero, 0, salt_len);
4752 salt = zero;
4753 }
4754
4755 suite_id_len = hpke_suite_id(ctx, kem, suite_id);
4756
4757 /* labeled_ikm = concat("HPKE-v1", suite_id, label, ikm)
4758 * return Extract(salt, labeled_ikm) */
4759
4760#if OPENSSL_VERSION_NUMBER >= 0x30000000L
4761 hmac = EVP_MAC_fetch(NULL, "HMAC", NULL);
4762 if (!hmac)
4763 return -1;
4764
4765 params[0] = OSSL_PARAM_construct_utf8_string(
4766 "digest",
4767 (char *) EVP_MD_get0_name(kem ? ctx->kem_h : ctx->kdf_h), 0);
4768 params[1] = OSSL_PARAM_construct_end();
4769
4770 hctx = EVP_MAC_CTX_new(hmac);
4771 EVP_MAC_free(hmac);
4772 if (!hctx)
4773 return -1;
4774
4775 if (EVP_MAC_init(hctx, salt, salt_len, params) != 1)
4776 goto fail;
4777
4778 if (EVP_MAC_update(hctx, (const unsigned char *) "HPKE-v1", 7) != 1 ||
4779 EVP_MAC_update(hctx, suite_id, suite_id_len) != 1 ||
4780 EVP_MAC_update(hctx, (const unsigned char *) label,
4781 os_strlen(label)) != 1 ||
4782 EVP_MAC_update(hctx, ikm, ikm_len) != 1)
4783 goto fail;
4784
4785 res = EVP_MAC_final(hctx, prk, &mlen, mdlen);
4786 EVP_MAC_CTX_free(hctx);
4787
4788 return res == 1 ? 0 : -1;
4789fail:
4790 EVP_MAC_CTX_free(hctx);
4791 return -1;
4792#else /* OpenSSL version >= 3.0 */
4793 hctx = HMAC_CTX_new();
4794 if (!hctx)
4795 return -1;
4796 res = HMAC_Init_ex(hctx, salt, salt_len, kem ? ctx->kem_h : ctx->kdf_h,
4797 NULL);
4798 if (res != 1)
4799 goto done;
4800
4801 HMAC_Update(hctx, (const unsigned char *) "HPKE-v1", 7);
4802 HMAC_Update(hctx, suite_id, suite_id_len);
4803 HMAC_Update(hctx, (const unsigned char *) label, os_strlen(label));
4804 HMAC_Update(hctx, ikm, ikm_len);
4805
4806 res = HMAC_Final(hctx, prk, &mdlen);
4807done:
4808 HMAC_CTX_free(hctx);
4809
4810 return res == 1 ? 0 : -1;
4811#endif /* OpenSSL version >= 3.0 */
4812}
4813
4814
4815static int
4816hpke_labeled_expand(struct hpke_context *ctx, bool kem, const u8 *prk,
4817 const char *label, const u8 *info, size_t info_len,
4818 u8 *out, size_t out_len)
4819{
4820 u8 suite_id[10];
4821 size_t suite_id_len;
4822 u8 hash[HPKE_MAX_HASH_LEN];
4823 u8 iter = 0;
4824 size_t label_len = os_strlen(label);
4825 u8 *pos;
4826 size_t left = out_len, clen;
4827 int res = -1;
4828 u8 *labeled_info;
4829 size_t labeled_info_len;
4830#if OPENSSL_VERSION_NUMBER >= 0x30000000L
4831 EVP_MAC *hmac;
4832 OSSL_PARAM params[2];
4833 EVP_MAC_CTX *hctx = NULL;
4834 size_t mdlen;
4835#else /* OpenSSL version >= 3.0 */
4836 HMAC_CTX *hctx;
4837 unsigned int mdlen;
4838#endif /* OpenSSL version >= 3.0 */
4839
4840 /* labeled_info = concat(I2OSP(L, 2), "HPKE-v1", suite_id,
4841 * label, info)
4842 * return Expand(prk, labeled_info, L) */
4843 suite_id_len = hpke_suite_id(ctx, kem, suite_id);
4844 labeled_info_len = 2 + 7 + suite_id_len + label_len + info_len;
4845 labeled_info = os_malloc(labeled_info_len);
4846 if (!labeled_info)
4847 return -1;
4848 pos = labeled_info;
4849 WPA_PUT_BE16(pos, out_len);
4850 pos += 2;
4851 os_memcpy(pos, "HPKE-v1", 7);
4852 pos += 7;
4853 os_memcpy(pos, suite_id, suite_id_len);
4854 pos += suite_id_len;
4855 os_memcpy(pos, label, label_len);
4856 pos += label_len;
4857 if (info && info_len) {
4858 os_memcpy(pos, info, info_len);
4859 pos += info_len;
4860 }
4861
4862 pos = out;
4863#if OPENSSL_VERSION_NUMBER >= 0x30000000L
4864 hmac = EVP_MAC_fetch(NULL, "HMAC", NULL);
4865 if (!hmac)
4866 return -1;
4867
4868 params[0] = OSSL_PARAM_construct_utf8_string(
4869 "digest",
4870 (char *) EVP_MD_get0_name(kem ? ctx->kem_h : ctx->kdf_h), 0);
4871 params[1] = OSSL_PARAM_construct_end();
4872#else /* OpenSSL version >= 3.0 */
4873 hctx = HMAC_CTX_new();
4874 if (!hctx)
4875 return -1;
4876#endif /* OpenSSL version >= 3.0 */
4877
4878 while (left > 0) {
4879 mdlen = kem ? ctx->kem_n_h : ctx->n_h;
4880#if OPENSSL_VERSION_NUMBER >= 0x30000000L
4881 EVP_MAC_CTX_free(hctx);
4882 hctx = EVP_MAC_CTX_new(hmac);
4883 if (!hctx)
4884 return -1;
4885
4886 if (EVP_MAC_init(hctx, prk, mdlen, params) != 1)
4887 goto fail;
4888
4889 if (iter > 0 && EVP_MAC_update(hctx, hash, mdlen) != 1)
4890 goto fail;
4891 if (iter == 255)
4892 goto fail;
4893 iter++;
4894
4895 if (EVP_MAC_update(hctx, labeled_info, labeled_info_len) != 1 ||
4896 EVP_MAC_update(hctx, &iter, sizeof(iter)) != 1)
4897 goto fail;
4898
4899 if (EVP_MAC_final(hctx, hash, &mdlen, mdlen) != 1)
4900 goto fail;
4901#else /* OpenSSL version >= 3.0 */
4902 if (HMAC_Init_ex(hctx, prk, mdlen,
4903 kem ? ctx->kem_h : ctx->kdf_h,
4904 NULL) != 1)
4905 goto fail;
4906
4907 if (iter > 0)
4908 HMAC_Update(hctx, hash, mdlen);
4909 if (iter == 255)
4910 goto fail;
4911 iter++;
4912 HMAC_Update(hctx, labeled_info, labeled_info_len);
4913 HMAC_Update(hctx, &iter, sizeof(iter));
4914
4915 if (HMAC_Final(hctx, hash, &mdlen) != 1)
4916 goto fail;
4917 HMAC_CTX_reset(hctx);
4918#endif /* OpenSSL version >= 3.0 */
4919
4920 clen = left > mdlen ? mdlen : left;
4921 os_memcpy(pos, hash, clen);
4922 pos += clen;
4923 left -= clen;
4924 }
4925 res = 0;
4926fail:
4927#if OPENSSL_VERSION_NUMBER >= 0x30000000L
4928 EVP_MAC_free(hmac);
4929 EVP_MAC_CTX_free(hctx);
4930#else /* OpenSSL version >= 3.0 */
4931 HMAC_CTX_free(hctx);
4932#endif /* OpenSSL version >= 3.0 */
4933 os_free(labeled_info);
4934
4935 return res;
4936}
4937
4938
4939static int hpke_extract_and_expand(struct hpke_context *ctx,
4940 const u8 *dhss, size_t dhss_len,
4941 const u8 *enc, size_t enc_len,
4942 const u8 *pk_rm, size_t pk_rm_len,
4943 u8 *shared_secret)
4944{
4945 u8 kem_context[2 * HPKE_MAX_PUB_LEN];
4946 u8 eae_prk[HPKE_MAX_HASH_LEN];
4947
4948 /* eae_prk = LabeledExtract("", "eae_prk", dh) */
4949 if (hpke_labeled_extract(ctx, true, NULL, 0, "eae_prk", dhss, dhss_len,
4950 eae_prk) < 0)
4951 return -1;
4952
4953 if (enc_len > HPKE_MAX_PUB_LEN || pk_rm_len > HPKE_MAX_PUB_LEN)
4954 return -1;
4955 /* kem_context = concat(enc, pkRm) */
4956 os_memcpy(kem_context, enc, enc_len);
4957 os_memcpy(&kem_context[enc_len], pk_rm, pk_rm_len);
4958
4959 /* shared_secret = LabeledExpand(eae_prk, "shared_secret",
4960 * kem_context, Nsecret) */
4961 if (hpke_labeled_expand(ctx, true, eae_prk, "shared_secret",
4962 kem_context, enc_len + pk_rm_len,
4963 shared_secret, ctx->n_secret) < 0)
4964 return -1;
4965
4966 forced_memzero(eae_prk, sizeof(eae_prk));
4967 return 0;
4968}
4969
4970
4971static int hpke_key_schedule(struct hpke_context *ctx, const u8 *shared_secret,
4972 const u8 *info, size_t info_len)
4973{
4974 u8 key_schedule_context[1 + 2 * HPKE_MAX_HASH_LEN];
4975 u8 secret[HPKE_MAX_HASH_LEN];
4976 int res = -1;
4977
4978 /* key_schedule_context = concat(mode, psk_id_hash, info_hash) */
4979 key_schedule_context[0] = HPKE_MODE_BASE;
4980
4981 /* psk_id_hash = LabeledExtract("", "psk_id_hash", psk_id) */
4982 if (hpke_labeled_extract(ctx, false, NULL, 0, "psk_id_hash",
4983 NULL, 0, &key_schedule_context[1]) < 0)
4984 goto fail;
4985
4986 /* info_hash = LabeledExtract("", "info_hash", info) */
4987 if (hpke_labeled_extract(ctx, false, NULL, 0, "info_hash",
4988 info, info_len,
4989 &key_schedule_context[1 + ctx->n_h]) < 0)
4990 goto fail;
4991
4992 /* secret = LabeledExtract(shared_secret, "secret", psk) */
4993 if (hpke_labeled_extract(ctx, false, shared_secret, ctx->n_secret,
4994 "secret", NULL, 0, secret) < 0)
4995 goto fail;
4996
4997 /* key = LabeledExpand(secret, "key", key_schedule_context, Nk) */
4998 if (hpke_labeled_expand(ctx, false, secret, "key",
4999 key_schedule_context, 1 + 2 * ctx->n_h,
5000 ctx->key, ctx->n_k) < 0)
5001 goto fail;
5002
5003 /* base_nonce = LabeledExpand(secret, "base_nonce",
5004 * key_schedule_context, Nn) */
5005 if (hpke_labeled_expand(ctx, false, secret, "base_nonce",
5006 key_schedule_context, 1 + 2 * ctx->n_h,
5007 ctx->base_nonce, ctx->n_n) < 0)
5008 goto fail;
5009 res = 0;
5010fail:
5011 forced_memzero(key_schedule_context, sizeof(key_schedule_context));
5012 forced_memzero(secret, sizeof(secret));
5013 return res;
5014}
5015
5016
5017static int hpke_encap(struct hpke_context *ctx, struct crypto_ec_key *pk_r,
5018 u8 *shared_secret, u8 *enc)
5019{
5020 EVP_PKEY_CTX *pctx = NULL;
5021 struct crypto_ec_key *sk_e;
5022 int res = -1;
5023 u8 dhss[HPKE_MAX_SHARED_SECRET_LEN + 16];
5024 size_t dhss_len;
5025 struct wpabuf *enc_buf = NULL, *pk_rm = NULL;
5026
5027 /* skE, pkE = GenerateKeyPair() */
5028 sk_e = crypto_ec_key_gen(ctx->iana_group);
5029 if (!sk_e) {
5030 wpa_printf(MSG_INFO, "OpenSSL:%s:Could not generate key pair",
5031 __func__);
5032 goto fail;
5033 }
5034
5035 /* dh = DH(skE, pkR) */
5036 dhss_len = sizeof(dhss);
5037 pctx = EVP_PKEY_CTX_new((EVP_PKEY *) sk_e, NULL);
5038 if (!pctx ||
5039 EVP_PKEY_derive_init(pctx) != 1 ||
5040 EVP_PKEY_derive_set_peer(pctx, (EVP_PKEY *) pk_r) != 1 ||
5041 EVP_PKEY_derive(pctx, dhss, &dhss_len) != 1 ||
5042 dhss_len > HPKE_MAX_SHARED_SECRET_LEN) {
5043 wpa_printf(MSG_INFO, "OpenSSL: EVP_PKEY_derive failed: %s",
5044 ERR_error_string(ERR_get_error(), NULL));
5045 goto fail;
5046 }
5047
5048 /* enc = SerializePublicKey(pkE) */
5049 enc_buf = crypto_ec_key_get_pubkey_point(sk_e, 1);
5050 if (!enc_buf)
5051 goto fail;
5052 os_memcpy(enc, wpabuf_head(enc_buf), wpabuf_len(enc_buf));
5053
5054 /* pkRm = SerializePublicKey(pkR) */
5055 pk_rm = crypto_ec_key_get_pubkey_point(pk_r, 1);
5056 if (!pk_rm)
5057 goto fail;
5058
5059 /* kem_context = concat(enc, pkRm) */
5060 /* shared_secret = ExtractAndExpand(dh, kem_context) */
5061 /* return shared_secret, enc */
5062 res = hpke_extract_and_expand(ctx, dhss, dhss_len, enc, ctx->n_pk,
5063 wpabuf_head(pk_rm),
5064 wpabuf_len(pk_rm), shared_secret);
5065fail:
5066 forced_memzero(dhss, sizeof(dhss));
5067 crypto_ec_key_deinit(sk_e);
5068 EVP_PKEY_CTX_free(pctx);
5069 wpabuf_free(enc_buf);
5070 wpabuf_free(pk_rm);
5071 return res;
5072}
5073
5074
5075static struct wpabuf *
5076hpke_aead_seal(struct hpke_context *ctx, const u8 *aad, size_t aad_len,
5077 const u8 *pt, size_t pt_len)
5078{
5079 EVP_CIPHER_CTX *cctx;
5080 int len = 0;
5081 struct wpabuf *ct = NULL;
5082
5083 /* No need to xor in sequence number since we support only the
5084 * single-shot API, i.e., base_nonce can be used as-is. */
5085
5086 cctx = EVP_CIPHER_CTX_new();
5087 if (!cctx ||
5088 EVP_EncryptInit_ex(cctx, ctx->cipher, NULL, ctx->key,
5089 ctx->base_nonce) != 1) {
5090 wpa_printf(MSG_INFO, "OpenSSL:%s:EVP_DecryptInit_ex failed",
5091 __func__);
5092 goto fail;
5093 }
5094 if (aad && aad_len &&
5095 EVP_EncryptUpdate(cctx, NULL, &len, aad, aad_len) != 1) {
5096 wpa_printf(MSG_INFO, "OpenSSL:%s:EVP_EncryptUpdate(AAD) failed",
5097 __func__);
5098 goto fail;
5099 }
5100 ct = wpabuf_alloc(pt_len + AES_BLOCK_SIZE + ctx->n_t);
5101 if (!ct)
5102 goto fail;
5103 if (EVP_EncryptUpdate(cctx, wpabuf_put(ct, 0), &len, pt, pt_len) != 1) {
5104 wpa_printf(MSG_INFO, "OpenSSL:%s:EVP_EncryptUpdate failed",
5105 __func__);
5106 goto fail;
5107 }
5108 wpabuf_put(ct, len);
5109
5110 if (EVP_EncryptFinal(cctx, wpabuf_put(ct, 0), &len) != 1) {
5111 wpa_printf(MSG_INFO, "OpenSSL:%s:EVP_DecryptFinal failed",
5112 __func__);
5113 wpabuf_free(ct);
5114 ct = NULL;
5115 goto fail;
5116 }
5117
5118 if (EVP_CIPHER_CTX_ctrl(cctx, EVP_CTRL_AEAD_GET_TAG, ctx->n_t,
5119 wpabuf_put(ct, ctx->n_t)) != 1) {
5120 wpa_printf(MSG_INFO, "OpenSSL:%s:Could not get tag",
5121 __func__);
5122 wpabuf_free(ct);
5123 ct = NULL;
5124 goto fail;
5125 }
5126fail:
5127 EVP_CIPHER_CTX_free(cctx);
5128 return ct;
5129}
5130
5131
5132struct wpabuf * hpke_base_seal(enum hpke_kem_id kem_id,
5133 enum hpke_kdf_id kdf_id,
5134 enum hpke_aead_id aead_id,
5135 struct crypto_ec_key *peer_pub,
5136 const u8 *info, size_t info_len,
5137 const u8 *aad, size_t aad_len,
5138 const u8 *pt, size_t pt_len)
5139{
5140 struct hpke_context *ctx;
5141 u8 shared_secret[HPKE_MAX_SHARED_SECRET_LEN];
5142 u8 enc[1 + 2 * HPKE_MAX_PUB_LEN];
5143 struct wpabuf *ct = NULL, *enc_ct = NULL;
5144
5145 ctx = hpke_get_context(kem_id, kdf_id, aead_id, peer_pub);
5146 if (!ctx)
5147 return NULL;
5148
5149 /* shared_secret, enc = Encap(pkR) */
5150 if (hpke_encap(ctx, peer_pub, shared_secret, enc) < 0)
5151 goto fail;
5152
5153 /* KeyScheduleS(mode_base, shared_secret, info,
5154 * default_psk, default_psk_id) */
5155 if (hpke_key_schedule(ctx, shared_secret, info, info_len) < 0)
5156 goto fail;
5157
5158 /* ct = ctx.Seal(aad, pt) */
5159 ct = hpke_aead_seal(ctx, aad, aad_len, pt, pt_len);
5160 if (!ct)
5161 goto fail;
5162
5163 /* return enc, ct */
5164 enc_ct = wpabuf_alloc(ctx->n_pk + wpabuf_len(ct));
5165 if (!enc_ct)
5166 goto fail;
5167 wpabuf_put_data(enc_ct, enc, ctx->n_pk);
5168 wpabuf_put_buf(enc_ct, ct);
5169
5170fail:
5171 forced_memzero(shared_secret, sizeof(shared_secret));
5172 hpke_free_context(ctx);
5173 wpabuf_free(ct);
5174 return enc_ct;
5175}
5176
5177
5178static int hpke_decap(struct hpke_context *ctx, const u8 *enc,
5179 size_t enc_ct_len, struct crypto_ec_key *sk_r,
5180 u8 *shared_secret)
5181{
5182 EVP_PKEY_CTX *pctx = NULL;
5183 struct wpabuf *pk_rm = NULL;
5184 size_t len;
5185 int res = -1;
5186 struct crypto_ec_key *pk_e = NULL;
5187 u8 dhss[HPKE_MAX_SHARED_SECRET_LEN + 16];
5188 size_t dhss_len;
5189
5190 /* pkE = DeserializePublicKey(enc) */
5191 if (enc_ct_len < ctx->n_pk)
5192 return -1; /* not enough room for enc */
5193 if (enc[0] != 0x04)
5194 return -1; /* not in uncompressed form */
5195 len = (ctx->n_pk - 1) / 2;
5196 pk_e = crypto_ec_key_set_pub(ctx->iana_group, &enc[1],
5197 &enc[1 + len], len);
5198 if (!pk_e)
5199 return -1; /* invalid public key point */
5200 /* dh = DH(skR, pkE) */
5201 dhss_len = sizeof(dhss);
5202 pctx = EVP_PKEY_CTX_new((EVP_PKEY *) sk_r, NULL);
5203 if (!pctx ||
5204 EVP_PKEY_derive_init(pctx) != 1 ||
5205 EVP_PKEY_derive_set_peer(pctx, (EVP_PKEY *) pk_e) != 1 ||
5206 EVP_PKEY_derive(pctx, dhss, &dhss_len) != 1 ||
5207 dhss_len > HPKE_MAX_SHARED_SECRET_LEN) {
5208 wpa_printf(MSG_INFO, "OpenSSL: EVP_PKEY_derive failed: %s",
5209 ERR_error_string(ERR_get_error(), NULL));
5210 goto fail;
5211 }
5212
5213 /* pkRm = SerializePublicKey(pk(skR)) */
5214 pk_rm = crypto_ec_key_get_pubkey_point(sk_r, 1);
5215 if (!pk_rm)
5216 goto fail;
5217
5218 /* kem_context = concat(enc, pkRm) */
5219 /* shared_secret = ExtractAndExpand(dh, kem_context) */
5220 res = hpke_extract_and_expand(ctx, dhss, dhss_len, enc, ctx->n_pk,
5221 wpabuf_head(pk_rm),
5222 wpabuf_len(pk_rm), shared_secret);
5223fail:
5224 forced_memzero(dhss, sizeof(dhss));
5225 crypto_ec_key_deinit(pk_e);
5226 EVP_PKEY_CTX_free(pctx);
5227 wpabuf_free(pk_rm);
5228 return res;
5229}
5230
5231
5232static struct wpabuf *
5233hpke_aead_open(struct hpke_context *ctx, const u8 *aad, size_t aad_len,
5234 const u8 *ct, size_t ct_len)
5235{
5236 EVP_CIPHER_CTX *cctx;
5237 int len = 0;
5238 const u8 *tag;
5239 struct wpabuf *pt = NULL;
5240
5241 if (ct_len < ctx->n_t)
5242 return NULL;
5243 tag = ct + ct_len - ctx->n_t;
5244 ct_len -= ctx->n_t;
5245
5246 /* No need to xor in sequence number since we support only the
5247 * single-shot API, i.e., base_nonce can be used as-is. */
5248
5249 cctx = EVP_CIPHER_CTX_new();
5250 if (!cctx ||
5251 EVP_DecryptInit_ex(cctx, ctx->cipher, NULL, ctx->key,
5252 ctx->base_nonce) != 1) {
5253 wpa_printf(MSG_INFO, "OpenSSL:%s:EVP_DecryptInit_ex failed",
5254 __func__);
5255 goto fail;
5256 }
5257 if (aad && aad_len &&
5258 EVP_DecryptUpdate(cctx, NULL, &len, aad, aad_len) != 1) {
5259 wpa_printf(MSG_INFO, "OpenSSL:%s:EVP_DecryptUpdate(AAD) failed",
5260 __func__);
5261 goto fail;
5262 }
5263 pt = wpabuf_alloc(ct_len + AES_BLOCK_SIZE);
5264 if (!pt)
5265 goto fail;
5266 if (EVP_DecryptUpdate(cctx, wpabuf_put(pt, 0), &len, ct, ct_len) != 1) {
5267 wpa_printf(MSG_INFO, "OpenSSL:%s:EVP_DecryptUpdate failed",
5268 __func__);
5269 goto fail;
5270 }
5271 wpabuf_put(pt, len);
5272
5273 if (EVP_CIPHER_CTX_ctrl(cctx, EVP_CTRL_AEAD_SET_TAG, ctx->n_t,
5274 (void *) tag) != 1) {
5275 wpa_printf(MSG_INFO, "OpenSSL:%s:Could not set tag",
5276 __func__);
5277 wpabuf_free(pt);
5278 pt = NULL;
5279 goto fail;
5280 }
5281
5282 if (EVP_DecryptFinal(cctx, wpabuf_put(pt, 0), &len) != 1) {
5283 wpa_printf(MSG_INFO, "OpenSSL:%s:EVP_DecryptFinal failed",
5284 __func__);
5285 wpabuf_free(pt);
5286 pt = NULL;
5287 }
5288fail:
5289 EVP_CIPHER_CTX_free(cctx);
5290 return pt;
5291}
5292
5293
5294struct wpabuf * hpke_base_open(enum hpke_kem_id kem_id,
5295 enum hpke_kdf_id kdf_id,
5296 enum hpke_aead_id aead_id,
5297 struct crypto_ec_key *own_priv,
5298 const u8 *info, size_t info_len,
5299 const u8 *aad, size_t aad_len,
5300 const u8 *enc_ct, size_t enc_ct_len)
5301{
5302 struct hpke_context *ctx;
5303 u8 shared_secret[HPKE_MAX_SHARED_SECRET_LEN];
5304 struct wpabuf *pt = NULL;
5305
5306 ctx = hpke_get_context(kem_id, kdf_id, aead_id, own_priv);
5307 if (!ctx)
5308 return NULL;
5309
5310 /* shared_secret = Decap(enc, skR) */
5311 if (hpke_decap(ctx, enc_ct, enc_ct_len, own_priv, shared_secret) < 0)
5312 goto fail;
5313
5314 /* KeyScheduleR(mode_base, shared_secret, info,
5315 * default_psk, default_psk_id) */
5316 if (hpke_key_schedule(ctx, shared_secret, info, info_len) < 0)
5317 goto fail;
5318
5319 /* return ctx.Open(aad, ct) */
5320 pt = hpke_aead_open(ctx, aad, aad_len,
5321 &enc_ct[ctx->n_pk], enc_ct_len - ctx->n_pk);
5322
5323fail:
5324 forced_memzero(shared_secret, sizeof(shared_secret));
5325 hpke_free_context(ctx);
5326 return pt;
5327}
5328
5329#endif /* CONFIG_DPP3 */
5330
5331
Sunil Ravia04bd252022-05-02 22:54:18 -07005332void crypto_unload(void)
5333{
5334 openssl_unload_legacy_provider();
5335}