• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Wrapper functions for OpenSSL libcrypto
3  * Copyright (c) 2004-2022, Jouni Malinen <j@w1.fi>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
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>
17 #include <openssl/hmac.h>
18 #include <openssl/rand.h>
19 #include <openssl/pem.h>
20 #ifdef CONFIG_ECC
21 #include <openssl/ec.h>
22 #include <openssl/x509.h>
23 #endif /* CONFIG_ECC */
24 #if OPENSSL_VERSION_NUMBER >= 0x30000000L
25 #include <openssl/provider.h>
26 #include <openssl/core_names.h>
27 #include <openssl/param_build.h>
28 #else /* OpenSSL version >= 3.0 */
29 #include <openssl/cmac.h>
30 #endif /* OpenSSL version >= 3.0 */
31 
32 #include "common.h"
33 #include "utils/const_time.h"
34 #include "wpabuf.h"
35 #include "dh_group5.h"
36 #include "sha1.h"
37 #include "sha256.h"
38 #include "sha384.h"
39 #include "sha512.h"
40 #include "md5.h"
41 #include "aes_wrap.h"
42 #include "crypto.h"
43 
44 #if OPENSSL_VERSION_NUMBER < 0x10100000L
45 /* Compatibility wrappers for older versions. */
46 
HMAC_CTX_new(void)47 static HMAC_CTX * HMAC_CTX_new(void)
48 {
49 	HMAC_CTX *ctx;
50 
51 	ctx = os_zalloc(sizeof(*ctx));
52 	if (ctx)
53 		HMAC_CTX_init(ctx);
54 	return ctx;
55 }
56 
57 
HMAC_CTX_free(HMAC_CTX * ctx)58 static void HMAC_CTX_free(HMAC_CTX *ctx)
59 {
60 	if (!ctx)
61 		return;
62 	HMAC_CTX_cleanup(ctx);
63 	bin_clear_free(ctx, sizeof(*ctx));
64 }
65 
66 
EVP_MD_CTX_new(void)67 static EVP_MD_CTX * EVP_MD_CTX_new(void)
68 {
69 	EVP_MD_CTX *ctx;
70 
71 	ctx = os_zalloc(sizeof(*ctx));
72 	if (ctx)
73 		EVP_MD_CTX_init(ctx);
74 	return ctx;
75 }
76 
77 
EVP_MD_CTX_free(EVP_MD_CTX * ctx)78 static void EVP_MD_CTX_free(EVP_MD_CTX *ctx)
79 {
80 	if (!ctx)
81 		return;
82 	EVP_MD_CTX_cleanup(ctx);
83 	bin_clear_free(ctx, sizeof(*ctx));
84 }
85 
86 
87 #ifdef CONFIG_ECC
88 
EVP_PKEY_get0_EC_KEY(EVP_PKEY * pkey)89 static EC_KEY * EVP_PKEY_get0_EC_KEY(EVP_PKEY *pkey)
90 {
91 	if (pkey->type != EVP_PKEY_EC)
92 		return NULL;
93 	return pkey->pkey.ec;
94 }
95 
96 
ECDSA_SIG_set0(ECDSA_SIG * sig,BIGNUM * r,BIGNUM * s)97 static int ECDSA_SIG_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s)
98 {
99 	sig->r = r;
100 	sig->s = s;
101 	return 1;
102 }
103 
104 
ECDSA_SIG_get0(const ECDSA_SIG * sig,const BIGNUM ** pr,const BIGNUM ** ps)105 static void ECDSA_SIG_get0(const ECDSA_SIG *sig, const BIGNUM **pr,
106 			   const BIGNUM **ps)
107 {
108 	if (pr)
109 		*pr = sig->r;
110 	if (ps)
111 		*ps = sig->s;
112 }
113 
114 #endif /* CONFIG_ECC */
115 
ASN1_STRING_get0_data(const ASN1_STRING * x)116 static const unsigned char * ASN1_STRING_get0_data(const ASN1_STRING *x)
117 {
118 	return ASN1_STRING_data((ASN1_STRING *) x);
119 }
120 #endif /* OpenSSL version < 1.1.0 */
121 
122 
123 #if OPENSSL_VERSION_NUMBER < 0x10101000L || \
124 	(defined(LIBRESSL_VERSION_NUMBER) && \
125 	 LIBRESSL_VERSION_NUMBER < 0x30400000L)
126 
EC_POINT_get_affine_coordinates(const EC_GROUP * group,const EC_POINT * point,BIGNUM * x,BIGNUM * y,BN_CTX * ctx)127 static int EC_POINT_get_affine_coordinates(const EC_GROUP *group,
128 					   const EC_POINT *point, BIGNUM *x,
129 					   BIGNUM *y, BN_CTX *ctx)
130 {
131 	return EC_POINT_get_affine_coordinates_GFp(group, point, x, y, ctx);
132 }
133 
134 
EC_POINT_set_affine_coordinates(const EC_GROUP * group,EC_POINT * point,const BIGNUM * x,const BIGNUM * y,BN_CTX * ctx)135 static int EC_POINT_set_affine_coordinates(const EC_GROUP *group,
136 					   EC_POINT *point, const BIGNUM *x,
137 					   const BIGNUM *y, BN_CTX *ctx)
138 {
139 	return EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx);
140 }
141 
142 #endif /* OpenSSL version < 1.1.1 */
143 
144 
145 #if OPENSSL_VERSION_NUMBER < 0x10101000L || \
146 	defined(OPENSSL_IS_BORINGSSL) || \
147 	(defined(LIBRESSL_VERSION_NUMBER) && \
148 	 LIBRESSL_VERSION_NUMBER < 0x30400000L)
149 
EC_POINT_set_compressed_coordinates(const EC_GROUP * group,EC_POINT * point,const BIGNUM * x,int y_bit,BN_CTX * ctx)150 static int EC_POINT_set_compressed_coordinates(const EC_GROUP *group,
151 					       EC_POINT *point, const BIGNUM *x,
152 					       int y_bit, BN_CTX *ctx)
153 {
154 	return EC_POINT_set_compressed_coordinates_GFp(group, point, x, y_bit,
155 						       ctx);
156 }
157 
158 
EC_GROUP_get_curve(const EC_GROUP * group,BIGNUM * p,BIGNUM * a,BIGNUM * b,BN_CTX * ctx)159 static int EC_GROUP_get_curve(const EC_GROUP *group, BIGNUM *p, BIGNUM *a,
160 			      BIGNUM *b, BN_CTX *ctx)
161 {
162 	return EC_GROUP_get_curve_GFp(group, p, a, b, ctx);
163 }
164 
165 #endif /* OpenSSL version < 1.1.1 */
166 
167 
168 #if OPENSSL_VERSION_NUMBER >= 0x30000000L
169 static OSSL_PROVIDER *openssl_default_provider = NULL;
170 static OSSL_PROVIDER *openssl_legacy_provider = NULL;
171 #endif /* OpenSSL version >= 3.0 */
172 
openssl_load_legacy_provider(void)173 void openssl_load_legacy_provider(void)
174 {
175 #if OPENSSL_VERSION_NUMBER >= 0x30000000L
176 	if (openssl_legacy_provider)
177 		return;
178 
179 	openssl_legacy_provider = OSSL_PROVIDER_load(NULL, "legacy");
180 	if (openssl_legacy_provider && !openssl_default_provider)
181 		openssl_default_provider = OSSL_PROVIDER_load(NULL, "default");
182 #endif /* OpenSSL version >= 3.0 */
183 }
184 
185 
openssl_unload_legacy_provider(void)186 static void openssl_unload_legacy_provider(void)
187 {
188 #if OPENSSL_VERSION_NUMBER >= 0x30000000L
189 	if (openssl_legacy_provider) {
190 		OSSL_PROVIDER_unload(openssl_legacy_provider);
191 		openssl_legacy_provider = NULL;
192 	}
193 	if (openssl_default_provider) {
194 		OSSL_PROVIDER_unload(openssl_default_provider);
195 		openssl_default_provider = NULL;
196 	}
197 #endif /* OpenSSL version >= 3.0 */
198 }
199 
200 
201 #if OPENSSL_VERSION_NUMBER < 0x30000000L
202 
get_group5_prime(void)203 static BIGNUM * get_group5_prime(void)
204 {
205 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
206 	return BN_get_rfc3526_prime_1536(NULL);
207 #elif !defined(OPENSSL_IS_BORINGSSL)
208 	return get_rfc3526_prime_1536(NULL);
209 #else
210 	static const unsigned char RFC3526_PRIME_1536[] = {
211 		0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xC9,0x0F,0xDA,0xA2,
212 		0x21,0x68,0xC2,0x34,0xC4,0xC6,0x62,0x8B,0x80,0xDC,0x1C,0xD1,
213 		0x29,0x02,0x4E,0x08,0x8A,0x67,0xCC,0x74,0x02,0x0B,0xBE,0xA6,
214 		0x3B,0x13,0x9B,0x22,0x51,0x4A,0x08,0x79,0x8E,0x34,0x04,0xDD,
215 		0xEF,0x95,0x19,0xB3,0xCD,0x3A,0x43,0x1B,0x30,0x2B,0x0A,0x6D,
216 		0xF2,0x5F,0x14,0x37,0x4F,0xE1,0x35,0x6D,0x6D,0x51,0xC2,0x45,
217 		0xE4,0x85,0xB5,0x76,0x62,0x5E,0x7E,0xC6,0xF4,0x4C,0x42,0xE9,
218 		0xA6,0x37,0xED,0x6B,0x0B,0xFF,0x5C,0xB6,0xF4,0x06,0xB7,0xED,
219 		0xEE,0x38,0x6B,0xFB,0x5A,0x89,0x9F,0xA5,0xAE,0x9F,0x24,0x11,
220 		0x7C,0x4B,0x1F,0xE6,0x49,0x28,0x66,0x51,0xEC,0xE4,0x5B,0x3D,
221 		0xC2,0x00,0x7C,0xB8,0xA1,0x63,0xBF,0x05,0x98,0xDA,0x48,0x36,
222 		0x1C,0x55,0xD3,0x9A,0x69,0x16,0x3F,0xA8,0xFD,0x24,0xCF,0x5F,
223 		0x83,0x65,0x5D,0x23,0xDC,0xA3,0xAD,0x96,0x1C,0x62,0xF3,0x56,
224 		0x20,0x85,0x52,0xBB,0x9E,0xD5,0x29,0x07,0x70,0x96,0x96,0x6D,
225 		0x67,0x0C,0x35,0x4E,0x4A,0xBC,0x98,0x04,0xF1,0x74,0x6C,0x08,
226 		0xCA,0x23,0x73,0x27,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
227 	};
228         return BN_bin2bn(RFC3526_PRIME_1536, sizeof(RFC3526_PRIME_1536), NULL);
229 #endif
230 }
231 
232 
get_group5_order(void)233 static BIGNUM * get_group5_order(void)
234 {
235 	static const unsigned char RFC3526_ORDER_1536[] = {
236 		0x7F,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xE4,0x87,0xED,0x51,
237 		0x10,0xB4,0x61,0x1A,0x62,0x63,0x31,0x45,0xC0,0x6E,0x0E,0x68,
238 		0x94,0x81,0x27,0x04,0x45,0x33,0xE6,0x3A,0x01,0x05,0xDF,0x53,
239 		0x1D,0x89,0xCD,0x91,0x28,0xA5,0x04,0x3C,0xC7,0x1A,0x02,0x6E,
240 		0xF7,0xCA,0x8C,0xD9,0xE6,0x9D,0x21,0x8D,0x98,0x15,0x85,0x36,
241 		0xF9,0x2F,0x8A,0x1B,0xA7,0xF0,0x9A,0xB6,0xB6,0xA8,0xE1,0x22,
242 		0xF2,0x42,0xDA,0xBB,0x31,0x2F,0x3F,0x63,0x7A,0x26,0x21,0x74,
243 		0xD3,0x1B,0xF6,0xB5,0x85,0xFF,0xAE,0x5B,0x7A,0x03,0x5B,0xF6,
244 		0xF7,0x1C,0x35,0xFD,0xAD,0x44,0xCF,0xD2,0xD7,0x4F,0x92,0x08,
245 		0xBE,0x25,0x8F,0xF3,0x24,0x94,0x33,0x28,0xF6,0x72,0x2D,0x9E,
246 		0xE1,0x00,0x3E,0x5C,0x50,0xB1,0xDF,0x82,0xCC,0x6D,0x24,0x1B,
247 		0x0E,0x2A,0xE9,0xCD,0x34,0x8B,0x1F,0xD4,0x7E,0x92,0x67,0xAF,
248 		0xC1,0xB2,0xAE,0x91,0xEE,0x51,0xD6,0xCB,0x0E,0x31,0x79,0xAB,
249 		0x10,0x42,0xA9,0x5D,0xCF,0x6A,0x94,0x83,0xB8,0x4B,0x4B,0x36,
250 		0xB3,0x86,0x1A,0xA7,0x25,0x5E,0x4C,0x02,0x78,0xBA,0x36,0x04,
251 		0x65,0x11,0xB9,0x93,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
252 	};
253 	return BN_bin2bn(RFC3526_ORDER_1536, sizeof(RFC3526_ORDER_1536), NULL);
254 }
255 
256 #endif /* OpenSSL version < 3.0 */
257 
258 
259 #ifdef OPENSSL_NO_SHA256
260 #define NO_SHA256_WRAPPER
261 #endif
262 #ifdef OPENSSL_NO_SHA512
263 #define NO_SHA384_WRAPPER
264 #endif
265 
openssl_digest_vector(const EVP_MD * type,size_t num_elem,const u8 * addr[],const size_t * len,u8 * mac)266 static int openssl_digest_vector(const EVP_MD *type, size_t num_elem,
267 				 const u8 *addr[], const size_t *len, u8 *mac)
268 {
269 	EVP_MD_CTX *ctx;
270 	size_t i;
271 	unsigned int mac_len;
272 
273 	if (TEST_FAIL())
274 		return -1;
275 
276 	ctx = EVP_MD_CTX_new();
277 	if (!ctx)
278 		return -1;
279 	if (!EVP_DigestInit_ex(ctx, type, NULL)) {
280 		wpa_printf(MSG_ERROR, "OpenSSL: EVP_DigestInit_ex failed: %s",
281 			   ERR_error_string(ERR_get_error(), NULL));
282 		EVP_MD_CTX_free(ctx);
283 		return -1;
284 	}
285 	for (i = 0; i < num_elem; i++) {
286 		if (!EVP_DigestUpdate(ctx, addr[i], len[i])) {
287 			wpa_printf(MSG_ERROR, "OpenSSL: EVP_DigestUpdate "
288 				   "failed: %s",
289 				   ERR_error_string(ERR_get_error(), NULL));
290 			EVP_MD_CTX_free(ctx);
291 			return -1;
292 		}
293 	}
294 	if (!EVP_DigestFinal(ctx, mac, &mac_len)) {
295 		wpa_printf(MSG_ERROR, "OpenSSL: EVP_DigestFinal failed: %s",
296 			   ERR_error_string(ERR_get_error(), NULL));
297 		EVP_MD_CTX_free(ctx);
298 		return -1;
299 	}
300 	EVP_MD_CTX_free(ctx);
301 
302 	return 0;
303 }
304 
305 
306 #ifndef CONFIG_FIPS
md4_vector(size_t num_elem,const u8 * addr[],const size_t * len,u8 * mac)307 int md4_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
308 {
309 	openssl_load_legacy_provider();
310 	return openssl_digest_vector(EVP_md4(), num_elem, addr, len, mac);
311 }
312 #endif /* CONFIG_FIPS */
313 
314 
des_encrypt(const u8 * clear,const u8 * key,u8 * cypher)315 int des_encrypt(const u8 *clear, const u8 *key, u8 *cypher)
316 {
317 	u8 pkey[8], next, tmp;
318 	int i, plen, ret = -1;
319 	EVP_CIPHER_CTX *ctx;
320 
321 	openssl_load_legacy_provider();
322 
323 	/* Add parity bits to the key */
324 	next = 0;
325 	for (i = 0; i < 7; i++) {
326 		tmp = key[i];
327 		pkey[i] = (tmp >> i) | next | 1;
328 		next = tmp << (7 - i);
329 	}
330 	pkey[i] = next | 1;
331 
332 	ctx = EVP_CIPHER_CTX_new();
333 	if (ctx &&
334 	    EVP_EncryptInit_ex(ctx, EVP_des_ecb(), NULL, pkey, NULL) == 1 &&
335 	    EVP_CIPHER_CTX_set_padding(ctx, 0) == 1 &&
336 	    EVP_EncryptUpdate(ctx, cypher, &plen, clear, 8) == 1 &&
337 	    EVP_EncryptFinal_ex(ctx, &cypher[plen], &plen) == 1)
338 		ret = 0;
339 	else
340 		wpa_printf(MSG_ERROR, "OpenSSL: DES encrypt failed");
341 
342 	if (ctx)
343 		EVP_CIPHER_CTX_free(ctx);
344 	return ret;
345 }
346 
347 
348 #ifndef CONFIG_NO_RC4
rc4_skip(const u8 * key,size_t keylen,size_t skip,u8 * data,size_t data_len)349 int rc4_skip(const u8 *key, size_t keylen, size_t skip,
350 	     u8 *data, size_t data_len)
351 {
352 #ifdef OPENSSL_NO_RC4
353 	return -1;
354 #else /* OPENSSL_NO_RC4 */
355 	EVP_CIPHER_CTX *ctx;
356 	int outl;
357 	int res = -1;
358 	unsigned char skip_buf[16];
359 
360 	openssl_load_legacy_provider();
361 
362 	ctx = EVP_CIPHER_CTX_new();
363 	if (!ctx ||
364 	    !EVP_CipherInit_ex(ctx, EVP_rc4(), NULL, NULL, NULL, 1) ||
365 	    !EVP_CIPHER_CTX_set_padding(ctx, 0) ||
366 	    !EVP_CIPHER_CTX_set_key_length(ctx, keylen) ||
367 	    !EVP_CipherInit_ex(ctx, NULL, NULL, key, NULL, 1))
368 		goto out;
369 
370 	while (skip >= sizeof(skip_buf)) {
371 		size_t len = skip;
372 		if (len > sizeof(skip_buf))
373 			len = sizeof(skip_buf);
374 		if (!EVP_CipherUpdate(ctx, skip_buf, &outl, skip_buf, len))
375 			goto out;
376 		skip -= len;
377 	}
378 
379 	if (EVP_CipherUpdate(ctx, data, &outl, data, data_len))
380 		res = 0;
381 
382 out:
383 	if (ctx)
384 		EVP_CIPHER_CTX_free(ctx);
385 	return res;
386 #endif /* OPENSSL_NO_RC4 */
387 }
388 #endif /* CONFIG_NO_RC4 */
389 
390 
391 #ifndef CONFIG_FIPS
md5_vector(size_t num_elem,const u8 * addr[],const size_t * len,u8 * mac)392 int md5_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
393 {
394 	return openssl_digest_vector(EVP_md5(), num_elem, addr, len, mac);
395 }
396 #endif /* CONFIG_FIPS */
397 
398 
sha1_vector(size_t num_elem,const u8 * addr[],const size_t * len,u8 * mac)399 int sha1_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
400 {
401 	return openssl_digest_vector(EVP_sha1(), num_elem, addr, len, mac);
402 }
403 
404 
405 #ifndef NO_SHA256_WRAPPER
sha256_vector(size_t num_elem,const u8 * addr[],const size_t * len,u8 * mac)406 int sha256_vector(size_t num_elem, const u8 *addr[], const size_t *len,
407 		  u8 *mac)
408 {
409 	return openssl_digest_vector(EVP_sha256(), num_elem, addr, len, mac);
410 }
411 #endif /* NO_SHA256_WRAPPER */
412 
413 
414 #ifndef NO_SHA384_WRAPPER
sha384_vector(size_t num_elem,const u8 * addr[],const size_t * len,u8 * mac)415 int sha384_vector(size_t num_elem, const u8 *addr[], const size_t *len,
416 		  u8 *mac)
417 {
418 	return openssl_digest_vector(EVP_sha384(), num_elem, addr, len, mac);
419 }
420 #endif /* NO_SHA384_WRAPPER */
421 
422 
423 #ifndef NO_SHA512_WRAPPER
sha512_vector(size_t num_elem,const u8 * addr[],const size_t * len,u8 * mac)424 int sha512_vector(size_t num_elem, const u8 *addr[], const size_t *len,
425 		  u8 *mac)
426 {
427 	return openssl_digest_vector(EVP_sha512(), num_elem, addr, len, mac);
428 }
429 #endif /* NO_SHA512_WRAPPER */
430 
431 
aes_get_evp_cipher(size_t keylen)432 static const EVP_CIPHER * aes_get_evp_cipher(size_t keylen)
433 {
434 	switch (keylen) {
435 	case 16:
436 		return EVP_aes_128_ecb();
437 	case 24:
438 		return EVP_aes_192_ecb();
439 	case 32:
440 		return EVP_aes_256_ecb();
441 	}
442 
443 	return NULL;
444 }
445 
446 
aes_encrypt_init(const u8 * key,size_t len)447 void * aes_encrypt_init(const u8 *key, size_t len)
448 {
449 	EVP_CIPHER_CTX *ctx;
450 	const EVP_CIPHER *type;
451 
452 	if (TEST_FAIL())
453 		return NULL;
454 
455 	type = aes_get_evp_cipher(len);
456 	if (!type) {
457 		wpa_printf(MSG_INFO, "%s: Unsupported len=%u",
458 			   __func__, (unsigned int) len);
459 		return NULL;
460 	}
461 
462 	ctx = EVP_CIPHER_CTX_new();
463 	if (ctx == NULL)
464 		return NULL;
465 	if (EVP_EncryptInit_ex(ctx, type, NULL, key, NULL) != 1) {
466 		EVP_CIPHER_CTX_free(ctx);
467 		return NULL;
468 	}
469 	EVP_CIPHER_CTX_set_padding(ctx, 0);
470 	return ctx;
471 }
472 
473 
aes_encrypt(void * ctx,const u8 * plain,u8 * crypt)474 int aes_encrypt(void *ctx, const u8 *plain, u8 *crypt)
475 {
476 	EVP_CIPHER_CTX *c = ctx;
477 	int clen = 16;
478 	if (EVP_EncryptUpdate(c, crypt, &clen, plain, 16) != 1) {
479 		wpa_printf(MSG_ERROR, "OpenSSL: EVP_EncryptUpdate failed: %s",
480 			   ERR_error_string(ERR_get_error(), NULL));
481 		return -1;
482 	}
483 	return 0;
484 }
485 
486 
aes_encrypt_deinit(void * ctx)487 void aes_encrypt_deinit(void *ctx)
488 {
489 	EVP_CIPHER_CTX *c = ctx;
490 	u8 buf[16];
491 	int len = sizeof(buf);
492 	if (EVP_EncryptFinal_ex(c, buf, &len) != 1) {
493 		wpa_printf(MSG_ERROR, "OpenSSL: EVP_EncryptFinal_ex failed: "
494 			   "%s", ERR_error_string(ERR_get_error(), NULL));
495 	}
496 	if (len != 0) {
497 		wpa_printf(MSG_ERROR, "OpenSSL: Unexpected padding length %d "
498 			   "in AES encrypt", len);
499 	}
500 	EVP_CIPHER_CTX_free(c);
501 }
502 
503 
aes_decrypt_init(const u8 * key,size_t len)504 void * aes_decrypt_init(const u8 *key, size_t len)
505 {
506 	EVP_CIPHER_CTX *ctx;
507 	const EVP_CIPHER *type;
508 
509 	if (TEST_FAIL())
510 		return NULL;
511 
512 	type = aes_get_evp_cipher(len);
513 	if (!type) {
514 		wpa_printf(MSG_INFO, "%s: Unsupported len=%u",
515 			   __func__, (unsigned int) len);
516 		return NULL;
517 	}
518 
519 	ctx = EVP_CIPHER_CTX_new();
520 	if (ctx == NULL)
521 		return NULL;
522 	if (EVP_DecryptInit_ex(ctx, type, NULL, key, NULL) != 1) {
523 		EVP_CIPHER_CTX_free(ctx);
524 		return NULL;
525 	}
526 	EVP_CIPHER_CTX_set_padding(ctx, 0);
527 	return ctx;
528 }
529 
530 
aes_decrypt(void * ctx,const u8 * crypt,u8 * plain)531 int aes_decrypt(void *ctx, const u8 *crypt, u8 *plain)
532 {
533 	EVP_CIPHER_CTX *c = ctx;
534 	int plen = 16;
535 	if (EVP_DecryptUpdate(c, plain, &plen, crypt, 16) != 1) {
536 		wpa_printf(MSG_ERROR, "OpenSSL: EVP_DecryptUpdate failed: %s",
537 			   ERR_error_string(ERR_get_error(), NULL));
538 		return -1;
539 	}
540 	return 0;
541 }
542 
543 
aes_decrypt_deinit(void * ctx)544 void aes_decrypt_deinit(void *ctx)
545 {
546 	EVP_CIPHER_CTX *c = ctx;
547 	u8 buf[16];
548 	int len = sizeof(buf);
549 	if (EVP_DecryptFinal_ex(c, buf, &len) != 1) {
550 		wpa_printf(MSG_ERROR, "OpenSSL: EVP_DecryptFinal_ex failed: "
551 			   "%s", ERR_error_string(ERR_get_error(), NULL));
552 	}
553 	if (len != 0) {
554 		wpa_printf(MSG_ERROR, "OpenSSL: Unexpected padding length %d "
555 			   "in AES decrypt", len);
556 	}
557 	EVP_CIPHER_CTX_free(c);
558 }
559 
560 
561 #ifndef CONFIG_FIPS
562 #ifndef CONFIG_OPENSSL_INTERNAL_AES_WRAP
563 
564 #if OPENSSL_VERSION_NUMBER >= 0x30000000L
aes_get_evp_wrap_cipher(size_t keylen)565 static const EVP_CIPHER * aes_get_evp_wrap_cipher(size_t keylen)
566 {
567 	switch (keylen) {
568 	case 16:
569 		return EVP_aes_128_wrap();
570 	case 24:
571 		return EVP_aes_192_wrap();
572 	case 32:
573 		return EVP_aes_256_wrap();
574 	default:
575 		return NULL;
576 	}
577 }
578 #endif /* OpenSSL version >= 3.0 */
579 
580 
aes_wrap(const u8 * kek,size_t kek_len,int n,const u8 * plain,u8 * cipher)581 int aes_wrap(const u8 *kek, size_t kek_len, int n, const u8 *plain, u8 *cipher)
582 {
583 #if OPENSSL_VERSION_NUMBER >= 0x30000000L
584 	EVP_CIPHER_CTX *ctx;
585 	const EVP_CIPHER *type;
586 	int ret = -1, len;
587 	u8 buf[16];
588 
589 	if (TEST_FAIL())
590 		return -1;
591 
592 	type = aes_get_evp_wrap_cipher(kek_len);
593 	if (!type)
594 		return -1;
595 
596 	ctx = EVP_CIPHER_CTX_new();
597 	if (!ctx)
598 		return -1;
599 
600 	if (EVP_EncryptInit_ex(ctx, type, NULL, kek, NULL) == 1 &&
601 	    EVP_CIPHER_CTX_set_padding(ctx, 0) == 1 &&
602 	    EVP_EncryptUpdate(ctx, cipher, &len, plain, n * 8) == 1 &&
603 	    len == (n + 1) * 8 &&
604 	    EVP_EncryptFinal_ex(ctx, buf, &len) == 1)
605 		ret = 0;
606 
607 	EVP_CIPHER_CTX_free(ctx);
608 	return ret;
609 #else /* OpenSSL version >= 3.0 */
610 	AES_KEY actx;
611 	int res;
612 
613 	if (TEST_FAIL())
614 		return -1;
615 	if (AES_set_encrypt_key(kek, kek_len << 3, &actx))
616 		return -1;
617 	res = AES_wrap_key(&actx, NULL, cipher, plain, n * 8);
618 	OPENSSL_cleanse(&actx, sizeof(actx));
619 	return res <= 0 ? -1 : 0;
620 #endif /* OpenSSL version >= 3.0 */
621 }
622 
623 
aes_unwrap(const u8 * kek,size_t kek_len,int n,const u8 * cipher,u8 * plain)624 int aes_unwrap(const u8 *kek, size_t kek_len, int n, const u8 *cipher,
625 	       u8 *plain)
626 {
627 #if OPENSSL_VERSION_NUMBER >= 0x30000000L
628 	EVP_CIPHER_CTX *ctx;
629 	const EVP_CIPHER *type;
630 	int ret = -1, len;
631 	u8 buf[16];
632 
633 	if (TEST_FAIL())
634 		return -1;
635 
636 	type = aes_get_evp_wrap_cipher(kek_len);
637 	if (!type)
638 		return -1;
639 
640 	ctx = EVP_CIPHER_CTX_new();
641 	if (!ctx)
642 		return -1;
643 
644 	if (EVP_DecryptInit_ex(ctx, type, NULL, kek, NULL) == 1 &&
645 	    EVP_CIPHER_CTX_set_padding(ctx, 0) == 1 &&
646 	    EVP_DecryptUpdate(ctx, plain, &len, cipher, (n + 1) * 8) == 1 &&
647 	    len == n * 8 &&
648 	    EVP_DecryptFinal_ex(ctx, buf, &len) == 1)
649 		ret = 0;
650 
651 	EVP_CIPHER_CTX_free(ctx);
652 	return ret;
653 #else /* OpenSSL version >= 3.0 */
654 	AES_KEY actx;
655 	int res;
656 
657 	if (TEST_FAIL())
658 		return -1;
659 	if (AES_set_decrypt_key(kek, kek_len << 3, &actx))
660 		return -1;
661 	res = AES_unwrap_key(&actx, NULL, plain, cipher, (n + 1) * 8);
662 	OPENSSL_cleanse(&actx, sizeof(actx));
663 	return res <= 0 ? -1 : 0;
664 #endif /* OpenSSL version >= 3.0 */
665 }
666 
667 #endif /* CONFIG_OPENSSL_INTERNAL_AES_WRAP */
668 #endif /* CONFIG_FIPS */
669 
670 
aes_128_cbc_encrypt(const u8 * key,const u8 * iv,u8 * data,size_t data_len)671 int aes_128_cbc_encrypt(const u8 *key, const u8 *iv, u8 *data, size_t data_len)
672 {
673 	EVP_CIPHER_CTX *ctx;
674 	int clen, len;
675 	u8 buf[16];
676 	int res = -1;
677 
678 	if (TEST_FAIL())
679 		return -1;
680 
681 	ctx = EVP_CIPHER_CTX_new();
682 	if (!ctx)
683 		return -1;
684 	clen = data_len;
685 	len = sizeof(buf);
686 	if (EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv) == 1 &&
687 	    EVP_CIPHER_CTX_set_padding(ctx, 0) == 1 &&
688 	    EVP_EncryptUpdate(ctx, data, &clen, data, data_len) == 1 &&
689 	    clen == (int) data_len &&
690 	    EVP_EncryptFinal_ex(ctx, buf, &len) == 1 && len == 0)
691 		res = 0;
692 	EVP_CIPHER_CTX_free(ctx);
693 
694 	return res;
695 }
696 
697 
aes_128_cbc_decrypt(const u8 * key,const u8 * iv,u8 * data,size_t data_len)698 int aes_128_cbc_decrypt(const u8 *key, const u8 *iv, u8 *data, size_t data_len)
699 {
700 	EVP_CIPHER_CTX *ctx;
701 	int plen, len;
702 	u8 buf[16];
703 	int res = -1;
704 
705 	if (TEST_FAIL())
706 		return -1;
707 
708 	ctx = EVP_CIPHER_CTX_new();
709 	if (!ctx)
710 		return -1;
711 	plen = data_len;
712 	len = sizeof(buf);
713 	if (EVP_DecryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv) == 1 &&
714 	    EVP_CIPHER_CTX_set_padding(ctx, 0) == 1 &&
715 	    EVP_DecryptUpdate(ctx, data, &plen, data, data_len) == 1 &&
716 	    plen == (int) data_len &&
717 	    EVP_DecryptFinal_ex(ctx, buf, &len) == 1 && len == 0)
718 		res = 0;
719 	EVP_CIPHER_CTX_free(ctx);
720 
721 	return res;
722 
723 }
724 
725 
crypto_dh_init(u8 generator,const u8 * prime,size_t prime_len,u8 * privkey,u8 * pubkey)726 int crypto_dh_init(u8 generator, const u8 *prime, size_t prime_len, u8 *privkey,
727 		   u8 *pubkey)
728 {
729 	size_t pubkey_len, pad;
730 
731 	if (os_get_random(privkey, prime_len) < 0)
732 		return -1;
733 	if (os_memcmp(privkey, prime, prime_len) > 0) {
734 		/* Make sure private value is smaller than prime */
735 		privkey[0] = 0;
736 	}
737 
738 	pubkey_len = prime_len;
739 	if (crypto_mod_exp(&generator, 1, privkey, prime_len, prime, prime_len,
740 			   pubkey, &pubkey_len) < 0)
741 		return -1;
742 	if (pubkey_len < prime_len) {
743 		pad = prime_len - pubkey_len;
744 		os_memmove(pubkey + pad, pubkey, pubkey_len);
745 		os_memset(pubkey, 0, pad);
746 	}
747 
748 	return 0;
749 }
750 
751 
crypto_dh_derive_secret(u8 generator,const u8 * prime,size_t prime_len,const u8 * order,size_t order_len,const u8 * privkey,size_t privkey_len,const u8 * pubkey,size_t pubkey_len,u8 * secret,size_t * len)752 int crypto_dh_derive_secret(u8 generator, const u8 *prime, size_t prime_len,
753 			    const u8 *order, size_t order_len,
754 			    const u8 *privkey, size_t privkey_len,
755 			    const u8 *pubkey, size_t pubkey_len,
756 			    u8 *secret, size_t *len)
757 {
758 	BIGNUM *pub, *p;
759 	int res = -1;
760 
761 	pub = BN_bin2bn(pubkey, pubkey_len, NULL);
762 	p = BN_bin2bn(prime, prime_len, NULL);
763 	if (!pub || !p || BN_is_zero(pub) || BN_is_one(pub) ||
764 	    BN_cmp(pub, p) >= 0)
765 		goto fail;
766 
767 	if (order) {
768 		BN_CTX *ctx;
769 		BIGNUM *q, *tmp;
770 		int failed;
771 
772 		/* verify: pubkey^q == 1 mod p */
773 		q = BN_bin2bn(order, order_len, NULL);
774 		ctx = BN_CTX_new();
775 		tmp = BN_new();
776 		failed = !q || !ctx || !tmp ||
777 			!BN_mod_exp(tmp, pub, q, p, ctx) ||
778 			!BN_is_one(tmp);
779 		BN_clear_free(q);
780 		BN_clear_free(tmp);
781 		BN_CTX_free(ctx);
782 		if (failed)
783 			goto fail;
784 	}
785 
786 	res = crypto_mod_exp(pubkey, pubkey_len, privkey, privkey_len,
787 			     prime, prime_len, secret, len);
788 fail:
789 	BN_clear_free(pub);
790 	BN_clear_free(p);
791 	return res;
792 }
793 
794 
crypto_mod_exp(const u8 * base,size_t base_len,const u8 * power,size_t power_len,const u8 * modulus,size_t modulus_len,u8 * result,size_t * result_len)795 int crypto_mod_exp(const u8 *base, size_t base_len,
796 		   const u8 *power, size_t power_len,
797 		   const u8 *modulus, size_t modulus_len,
798 		   u8 *result, size_t *result_len)
799 {
800 	BIGNUM *bn_base, *bn_exp, *bn_modulus, *bn_result;
801 	int ret = -1;
802 	BN_CTX *ctx;
803 
804 	ctx = BN_CTX_new();
805 	if (ctx == NULL)
806 		return -1;
807 
808 	bn_base = BN_bin2bn(base, base_len, NULL);
809 	bn_exp = BN_bin2bn(power, power_len, NULL);
810 	bn_modulus = BN_bin2bn(modulus, modulus_len, NULL);
811 	bn_result = BN_new();
812 
813 	if (bn_base == NULL || bn_exp == NULL || bn_modulus == NULL ||
814 	    bn_result == NULL)
815 		goto error;
816 
817 	if (BN_mod_exp_mont_consttime(bn_result, bn_base, bn_exp, bn_modulus,
818 				      ctx, NULL) != 1)
819 		goto error;
820 
821 	*result_len = BN_bn2bin(bn_result, result);
822 	ret = 0;
823 
824 error:
825 	BN_clear_free(bn_base);
826 	BN_clear_free(bn_exp);
827 	BN_clear_free(bn_modulus);
828 	BN_clear_free(bn_result);
829 	BN_CTX_free(ctx);
830 	return ret;
831 }
832 
833 
834 struct crypto_cipher {
835 	EVP_CIPHER_CTX *enc;
836 	EVP_CIPHER_CTX *dec;
837 };
838 
839 
crypto_cipher_init(enum crypto_cipher_alg alg,const u8 * iv,const u8 * key,size_t key_len)840 struct crypto_cipher * crypto_cipher_init(enum crypto_cipher_alg alg,
841 					  const u8 *iv, const u8 *key,
842 					  size_t key_len)
843 {
844 	struct crypto_cipher *ctx;
845 	const EVP_CIPHER *cipher;
846 
847 	ctx = os_zalloc(sizeof(*ctx));
848 	if (ctx == NULL)
849 		return NULL;
850 
851 	switch (alg) {
852 #ifndef CONFIG_NO_RC4
853 #ifndef OPENSSL_NO_RC4
854 	case CRYPTO_CIPHER_ALG_RC4:
855 		cipher = EVP_rc4();
856 		break;
857 #endif /* OPENSSL_NO_RC4 */
858 #endif /* CONFIG_NO_RC4 */
859 #ifndef OPENSSL_NO_AES
860 	case CRYPTO_CIPHER_ALG_AES:
861 		switch (key_len) {
862 		case 16:
863 			cipher = EVP_aes_128_cbc();
864 			break;
865 #ifndef OPENSSL_IS_BORINGSSL
866 		case 24:
867 			cipher = EVP_aes_192_cbc();
868 			break;
869 #endif /* OPENSSL_IS_BORINGSSL */
870 		case 32:
871 			cipher = EVP_aes_256_cbc();
872 			break;
873 		default:
874 			os_free(ctx);
875 			return NULL;
876 		}
877 		break;
878 #endif /* OPENSSL_NO_AES */
879 #ifndef OPENSSL_NO_DES
880 	case CRYPTO_CIPHER_ALG_3DES:
881 		cipher = EVP_des_ede3_cbc();
882 		break;
883 	case CRYPTO_CIPHER_ALG_DES:
884 		cipher = EVP_des_cbc();
885 		break;
886 #endif /* OPENSSL_NO_DES */
887 #ifndef OPENSSL_NO_RC2
888 	case CRYPTO_CIPHER_ALG_RC2:
889 		cipher = EVP_rc2_ecb();
890 		break;
891 #endif /* OPENSSL_NO_RC2 */
892 	default:
893 		os_free(ctx);
894 		return NULL;
895 	}
896 
897 	if (!(ctx->enc = EVP_CIPHER_CTX_new()) ||
898 	    !EVP_EncryptInit_ex(ctx->enc, cipher, NULL, NULL, NULL) ||
899 	    !EVP_CIPHER_CTX_set_padding(ctx->enc, 0) ||
900 	    !EVP_CIPHER_CTX_set_key_length(ctx->enc, key_len) ||
901 	    !EVP_EncryptInit_ex(ctx->enc, NULL, NULL, key, iv)) {
902 		if (ctx->enc)
903 			EVP_CIPHER_CTX_free(ctx->enc);
904 		os_free(ctx);
905 		return NULL;
906 	}
907 
908 	if (!(ctx->dec = EVP_CIPHER_CTX_new()) ||
909 	    !EVP_DecryptInit_ex(ctx->dec, cipher, NULL, NULL, NULL) ||
910 	    !EVP_CIPHER_CTX_set_padding(ctx->dec, 0) ||
911 	    !EVP_CIPHER_CTX_set_key_length(ctx->dec, key_len) ||
912 	    !EVP_DecryptInit_ex(ctx->dec, NULL, NULL, key, iv)) {
913 		EVP_CIPHER_CTX_free(ctx->enc);
914 		if (ctx->dec)
915 			EVP_CIPHER_CTX_free(ctx->dec);
916 		os_free(ctx);
917 		return NULL;
918 	}
919 
920 	return ctx;
921 }
922 
923 
crypto_cipher_encrypt(struct crypto_cipher * ctx,const u8 * plain,u8 * crypt,size_t len)924 int crypto_cipher_encrypt(struct crypto_cipher *ctx, const u8 *plain,
925 			  u8 *crypt, size_t len)
926 {
927 	int outl;
928 	if (!EVP_EncryptUpdate(ctx->enc, crypt, &outl, plain, len))
929 		return -1;
930 	return 0;
931 }
932 
933 
crypto_cipher_decrypt(struct crypto_cipher * ctx,const u8 * crypt,u8 * plain,size_t len)934 int crypto_cipher_decrypt(struct crypto_cipher *ctx, const u8 *crypt,
935 			  u8 *plain, size_t len)
936 {
937 	int outl;
938 	outl = len;
939 	if (!EVP_DecryptUpdate(ctx->dec, plain, &outl, crypt, len))
940 		return -1;
941 	return 0;
942 }
943 
944 
crypto_cipher_deinit(struct crypto_cipher * ctx)945 void crypto_cipher_deinit(struct crypto_cipher *ctx)
946 {
947 	EVP_CIPHER_CTX_free(ctx->enc);
948 	EVP_CIPHER_CTX_free(ctx->dec);
949 	os_free(ctx);
950 }
951 
952 
dh5_init(struct wpabuf ** priv,struct wpabuf ** publ)953 void * dh5_init(struct wpabuf **priv, struct wpabuf **publ)
954 {
955 #if OPENSSL_VERSION_NUMBER < 0x10100000L
956 	DH *dh;
957 	struct wpabuf *pubkey = NULL, *privkey = NULL;
958 	size_t publen, privlen;
959 
960 	*priv = NULL;
961 	wpabuf_free(*publ);
962 	*publ = NULL;
963 
964 	dh = DH_new();
965 	if (dh == NULL)
966 		return NULL;
967 
968 	dh->g = BN_new();
969 	if (dh->g == NULL || BN_set_word(dh->g, 2) != 1)
970 		goto err;
971 
972 	dh->p = get_group5_prime();
973 	if (dh->p == NULL)
974 		goto err;
975 
976 	dh->q = get_group5_order();
977 	if (!dh->q)
978 		goto err;
979 
980 	if (DH_generate_key(dh) != 1)
981 		goto err;
982 
983 	publen = BN_num_bytes(dh->pub_key);
984 	pubkey = wpabuf_alloc(publen);
985 	if (pubkey == NULL)
986 		goto err;
987 	privlen = BN_num_bytes(dh->priv_key);
988 	privkey = wpabuf_alloc(privlen);
989 	if (privkey == NULL)
990 		goto err;
991 
992 	BN_bn2bin(dh->pub_key, wpabuf_put(pubkey, publen));
993 	BN_bn2bin(dh->priv_key, wpabuf_put(privkey, privlen));
994 
995 	*priv = privkey;
996 	*publ = pubkey;
997 	return dh;
998 
999 err:
1000 	wpabuf_clear_free(pubkey);
1001 	wpabuf_clear_free(privkey);
1002 	DH_free(dh);
1003 	return NULL;
1004 #elif OPENSSL_VERSION_NUMBER >= 0x30000000L
1005 	EVP_PKEY *pkey = NULL;
1006 	OSSL_PARAM params[2];
1007 	size_t pub_len = OSSL_PARAM_UNMODIFIED;
1008 	size_t priv_len;
1009 	struct wpabuf *pubkey = NULL, *privkey = NULL;
1010 	BIGNUM *priv_bn = NULL;
1011 	EVP_PKEY_CTX *gctx;
1012 
1013 	*priv = NULL;
1014 	wpabuf_free(*publ);
1015 	*publ = NULL;
1016 
1017 	params[0] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME,
1018 						     "modp_1536", 0);
1019 	params[1] = OSSL_PARAM_construct_end();
1020 
1021 	gctx = EVP_PKEY_CTX_new_from_name(NULL, "DH", NULL);
1022 	if (!gctx ||
1023 	    EVP_PKEY_keygen_init(gctx) != 1 ||
1024 	    EVP_PKEY_CTX_set_params(gctx, params) != 1 ||
1025 	    EVP_PKEY_generate(gctx, &pkey) != 1 ||
1026 	    EVP_PKEY_get_bn_param(pkey, OSSL_PKEY_PARAM_PRIV_KEY,
1027 				  &priv_bn) != 1 ||
1028 	    EVP_PKEY_get_octet_string_param(pkey,
1029 					    OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY,
1030 					    NULL, 0, &pub_len) < 0 ||
1031 	    pub_len == OSSL_PARAM_UNMODIFIED ||
1032 	    (priv_len = BN_num_bytes(priv_bn)) == 0 ||
1033 	    !(pubkey = wpabuf_alloc(pub_len)) ||
1034 	    !(privkey = wpabuf_alloc(priv_len)) ||
1035 	    EVP_PKEY_get_octet_string_param(pkey,
1036 					    OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY,
1037 					    wpabuf_put(pubkey, pub_len),
1038 					    pub_len, NULL) != 1) {
1039 		wpa_printf(MSG_INFO, "OpenSSL: failed: %s",
1040 			   ERR_error_string(ERR_get_error(), NULL));
1041 		wpabuf_free(pubkey);
1042 		wpabuf_clear_free(privkey);
1043 		EVP_PKEY_free(pkey);
1044 		pkey = NULL;
1045 	} else {
1046 		BN_bn2bin(priv_bn, wpabuf_put(privkey, priv_len));
1047 
1048 		*priv = privkey;
1049 		*publ = pubkey;
1050 	}
1051 
1052 	BN_clear_free(priv_bn);
1053 	EVP_PKEY_CTX_free(gctx);
1054 	return pkey;
1055 #else
1056 	DH *dh;
1057 	struct wpabuf *pubkey = NULL, *privkey = NULL;
1058 	size_t publen, privlen;
1059 	BIGNUM *p, *g, *q;
1060 	const BIGNUM *priv_key = NULL, *pub_key = NULL;
1061 
1062 	*priv = NULL;
1063 	wpabuf_free(*publ);
1064 	*publ = NULL;
1065 
1066 	dh = DH_new();
1067 	if (dh == NULL)
1068 		return NULL;
1069 
1070 	g = BN_new();
1071 	p = get_group5_prime();
1072 	q = get_group5_order();
1073 	if (!g || BN_set_word(g, 2) != 1 || !p || !q ||
1074 	    DH_set0_pqg(dh, p, q, g) != 1)
1075 		goto err;
1076 	p = NULL;
1077 	q = NULL;
1078 	g = NULL;
1079 
1080 	if (DH_generate_key(dh) != 1)
1081 		goto err;
1082 
1083 	DH_get0_key(dh, &pub_key, &priv_key);
1084 	publen = BN_num_bytes(pub_key);
1085 	pubkey = wpabuf_alloc(publen);
1086 	if (!pubkey)
1087 		goto err;
1088 	privlen = BN_num_bytes(priv_key);
1089 	privkey = wpabuf_alloc(privlen);
1090 	if (!privkey)
1091 		goto err;
1092 
1093 	BN_bn2bin(pub_key, wpabuf_put(pubkey, publen));
1094 	BN_bn2bin(priv_key, wpabuf_put(privkey, privlen));
1095 
1096 	*priv = privkey;
1097 	*publ = pubkey;
1098 	return dh;
1099 
1100 err:
1101 	BN_free(p);
1102 	BN_free(q);
1103 	BN_free(g);
1104 	wpabuf_clear_free(pubkey);
1105 	wpabuf_clear_free(privkey);
1106 	DH_free(dh);
1107 	return NULL;
1108 #endif
1109 }
1110 
1111 
dh5_init_fixed(const struct wpabuf * priv,const struct wpabuf * publ)1112 void * dh5_init_fixed(const struct wpabuf *priv, const struct wpabuf *publ)
1113 {
1114 #if OPENSSL_VERSION_NUMBER < 0x10100000L
1115 	DH *dh;
1116 
1117 	dh = DH_new();
1118 	if (dh == NULL)
1119 		return NULL;
1120 
1121 	dh->g = BN_new();
1122 	if (dh->g == NULL || BN_set_word(dh->g, 2) != 1)
1123 		goto err;
1124 
1125 	dh->p = get_group5_prime();
1126 	if (dh->p == NULL)
1127 		goto err;
1128 
1129 	dh->priv_key = BN_bin2bn(wpabuf_head(priv), wpabuf_len(priv), NULL);
1130 	if (dh->priv_key == NULL)
1131 		goto err;
1132 
1133 	dh->pub_key = BN_bin2bn(wpabuf_head(publ), wpabuf_len(publ), NULL);
1134 	if (dh->pub_key == NULL)
1135 		goto err;
1136 
1137 	if (DH_generate_key(dh) != 1)
1138 		goto err;
1139 
1140 	return dh;
1141 
1142 err:
1143 	DH_free(dh);
1144 	return NULL;
1145 #elif OPENSSL_VERSION_NUMBER >= 0x30000000L
1146 	EVP_PKEY *pkey = NULL;
1147 	OSSL_PARAM_BLD *bld;
1148 	OSSL_PARAM *params = NULL;
1149 	BIGNUM *priv_key, *pub_key;
1150 	EVP_PKEY_CTX *fctx;
1151 
1152 	fctx = EVP_PKEY_CTX_new_from_name(NULL, "DH", NULL);
1153 	priv_key = BN_bin2bn(wpabuf_head(priv), wpabuf_len(priv), NULL);
1154 	pub_key = BN_bin2bn(wpabuf_head(publ), wpabuf_len(publ), NULL);
1155 	bld = OSSL_PARAM_BLD_new();
1156 	if (!fctx || !priv_key || !pub_key || !bld ||
1157 	    OSSL_PARAM_BLD_push_utf8_string(bld, OSSL_PKEY_PARAM_GROUP_NAME,
1158 					    "modp_1536", 0) != 1 ||
1159 	    OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PRIV_KEY,
1160 				   priv_key) != 1 ||
1161 	    OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PUB_KEY,
1162 				   pub_key) != 1 ||
1163 	    !(params = OSSL_PARAM_BLD_to_param(bld)) ||
1164 	    EVP_PKEY_fromdata_init(fctx) != 1 ||
1165 	    EVP_PKEY_fromdata(fctx, &pkey, EVP_PKEY_KEYPAIR, params) != 1) {
1166 		wpa_printf(MSG_INFO, "OpenSSL: EVP_PKEY_fromdata failed: %s",
1167 			   ERR_error_string(ERR_get_error(), NULL));
1168 		EVP_PKEY_free(pkey);
1169 		pkey = NULL;
1170 	}
1171 
1172 	BN_clear_free(priv_key);
1173 	BN_free(pub_key);
1174 	EVP_PKEY_CTX_free(fctx);
1175 	OSSL_PARAM_BLD_free(bld);
1176 	OSSL_PARAM_free(params);
1177 	return pkey;
1178 #else
1179 	DH *dh;
1180 	BIGNUM *p = NULL, *g, *priv_key = NULL, *pub_key = NULL;
1181 
1182 	dh = DH_new();
1183 	if (dh == NULL)
1184 		return NULL;
1185 
1186 	g = BN_new();
1187 	p = get_group5_prime();
1188 	if (!g || BN_set_word(g, 2) != 1 || !p ||
1189 	    DH_set0_pqg(dh, p, NULL, g) != 1)
1190 		goto err;
1191 	p = NULL;
1192 	g = NULL;
1193 
1194 	priv_key = BN_bin2bn(wpabuf_head(priv), wpabuf_len(priv), NULL);
1195 	pub_key = BN_bin2bn(wpabuf_head(publ), wpabuf_len(publ), NULL);
1196 	if (!priv_key || !pub_key || DH_set0_key(dh, pub_key, priv_key) != 1)
1197 		goto err;
1198 	pub_key = NULL;
1199 	priv_key = NULL;
1200 
1201 	if (DH_generate_key(dh) != 1)
1202 		goto err;
1203 
1204 	return dh;
1205 
1206 err:
1207 	BN_free(p);
1208 	BN_free(g);
1209 	BN_free(pub_key);
1210 	BN_clear_free(priv_key);
1211 	DH_free(dh);
1212 	return NULL;
1213 #endif
1214 }
1215 
1216 
dh5_derive_shared(void * ctx,const struct wpabuf * peer_public,const struct wpabuf * own_private)1217 struct wpabuf * dh5_derive_shared(void *ctx, const struct wpabuf *peer_public,
1218 				  const struct wpabuf *own_private)
1219 {
1220 #if OPENSSL_VERSION_NUMBER >= 0x30000000L
1221 	EVP_PKEY *pkey = ctx;
1222 	EVP_PKEY *peer_pub;
1223 	size_t len;
1224 	struct wpabuf *res = NULL;
1225 	EVP_PKEY_CTX *dctx = NULL;
1226 
1227 	peer_pub = EVP_PKEY_new();
1228 	if (!pkey || !peer_pub ||
1229 	    EVP_PKEY_copy_parameters(peer_pub, pkey) != 1 ||
1230 	    EVP_PKEY_set1_encoded_public_key(peer_pub, wpabuf_head(peer_public),
1231 					     wpabuf_len(peer_public)) != 1 ||
1232 	    !(dctx = EVP_PKEY_CTX_new(pkey, NULL)) ||
1233 	    EVP_PKEY_derive_init(dctx) != 1 ||
1234 	    EVP_PKEY_derive_set_peer(dctx, peer_pub) != 1 ||
1235 	    EVP_PKEY_derive(dctx, NULL, &len) != 1 ||
1236 	    !(res = wpabuf_alloc(len)) ||
1237 	    EVP_PKEY_derive(dctx, wpabuf_mhead(res), &len) != 1) {
1238 		wpa_printf(MSG_INFO, "OpenSSL: EVP_PKEY_derive failed: %s",
1239 			   ERR_error_string(ERR_get_error(), NULL));
1240 		wpabuf_free(res);
1241 		res = NULL;
1242 	} else {
1243 		wpabuf_put(res, len);
1244 	}
1245 
1246 	EVP_PKEY_free(peer_pub);
1247 	EVP_PKEY_CTX_free(dctx);
1248 	return res;
1249 #else /* OpenSSL version >= 3.0 */
1250 	BIGNUM *pub_key;
1251 	struct wpabuf *res = NULL;
1252 	size_t rlen;
1253 	DH *dh = ctx;
1254 	int keylen;
1255 
1256 	if (ctx == NULL)
1257 		return NULL;
1258 
1259 	pub_key = BN_bin2bn(wpabuf_head(peer_public), wpabuf_len(peer_public),
1260 			    NULL);
1261 	if (pub_key == NULL)
1262 		return NULL;
1263 
1264 	rlen = DH_size(dh);
1265 	res = wpabuf_alloc(rlen);
1266 	if (res == NULL)
1267 		goto err;
1268 
1269 	keylen = DH_compute_key(wpabuf_mhead(res), pub_key, dh);
1270 	if (keylen < 0)
1271 		goto err;
1272 	wpabuf_put(res, keylen);
1273 	BN_clear_free(pub_key);
1274 
1275 	return res;
1276 
1277 err:
1278 	BN_clear_free(pub_key);
1279 	wpabuf_clear_free(res);
1280 	return NULL;
1281 #endif /* OpenSSL version >= 3.0 */
1282 }
1283 
1284 
dh5_free(void * ctx)1285 void dh5_free(void *ctx)
1286 {
1287 #if OPENSSL_VERSION_NUMBER >= 0x30000000L
1288 	EVP_PKEY *pkey = ctx;
1289 
1290 	EVP_PKEY_free(pkey);
1291 #else /* OpenSSL version >= 3.0 */
1292 	DH *dh;
1293 	if (ctx == NULL)
1294 		return;
1295 	dh = ctx;
1296 	DH_free(dh);
1297 #endif /* OpenSSL version >= 3.0 */
1298 }
1299 
1300 
1301 struct crypto_hash {
1302 #if OPENSSL_VERSION_NUMBER >= 0x30000000L
1303 	EVP_MAC_CTX *ctx;
1304 #else /* OpenSSL version >= 3.0 */
1305 	HMAC_CTX *ctx;
1306 #endif /* OpenSSL version >= 3.0 */
1307 };
1308 
1309 
crypto_hash_init(enum crypto_hash_alg alg,const u8 * key,size_t key_len)1310 struct crypto_hash * crypto_hash_init(enum crypto_hash_alg alg, const u8 *key,
1311 				      size_t key_len)
1312 {
1313 #if OPENSSL_VERSION_NUMBER >= 0x30000000L
1314 	struct crypto_hash *ctx;
1315 	EVP_MAC *mac;
1316 	OSSL_PARAM params[2];
1317 	char *a = NULL;
1318 
1319 	switch (alg) {
1320 #ifndef OPENSSL_NO_MD5
1321 	case CRYPTO_HASH_ALG_HMAC_MD5:
1322 		a = "MD5";
1323 		break;
1324 #endif /* OPENSSL_NO_MD5 */
1325 #ifndef OPENSSL_NO_SHA
1326 	case CRYPTO_HASH_ALG_HMAC_SHA1:
1327 		a = "SHA1";
1328 		break;
1329 #endif /* OPENSSL_NO_SHA */
1330 #ifndef OPENSSL_NO_SHA256
1331 #ifdef CONFIG_SHA256
1332 	case CRYPTO_HASH_ALG_HMAC_SHA256:
1333 		a = "SHA256";
1334 		break;
1335 #endif /* CONFIG_SHA256 */
1336 #endif /* OPENSSL_NO_SHA256 */
1337 	default:
1338 		return NULL;
1339 	}
1340 
1341 	mac = EVP_MAC_fetch(NULL, "HMAC", NULL);
1342 	if (!mac)
1343 		return NULL;
1344 
1345 	params[0] = OSSL_PARAM_construct_utf8_string("digest", a, 0);
1346 	params[1] = OSSL_PARAM_construct_end();
1347 
1348 	ctx = os_zalloc(sizeof(*ctx));
1349 	if (!ctx)
1350 		return NULL;
1351 	ctx->ctx = EVP_MAC_CTX_new(mac);
1352 	if (!ctx->ctx) {
1353 		EVP_MAC_free(mac);
1354 		os_free(ctx);
1355 		return NULL;
1356 	}
1357 
1358 	if (EVP_MAC_init(ctx->ctx, key, key_len, params) != 1) {
1359 		EVP_MAC_CTX_free(ctx->ctx);
1360 		bin_clear_free(ctx, sizeof(*ctx));
1361 		EVP_MAC_free(mac);
1362 		return NULL;
1363 	}
1364 
1365 	EVP_MAC_free(mac);
1366 	return ctx;
1367 #else /* OpenSSL version >= 3.0 */
1368 	struct crypto_hash *ctx;
1369 	const EVP_MD *md;
1370 
1371 	switch (alg) {
1372 #ifndef OPENSSL_NO_MD5
1373 	case CRYPTO_HASH_ALG_HMAC_MD5:
1374 		md = EVP_md5();
1375 		break;
1376 #endif /* OPENSSL_NO_MD5 */
1377 #ifndef OPENSSL_NO_SHA
1378 	case CRYPTO_HASH_ALG_HMAC_SHA1:
1379 		md = EVP_sha1();
1380 		break;
1381 #endif /* OPENSSL_NO_SHA */
1382 #ifndef OPENSSL_NO_SHA256
1383 #ifdef CONFIG_SHA256
1384 	case CRYPTO_HASH_ALG_HMAC_SHA256:
1385 		md = EVP_sha256();
1386 		break;
1387 #endif /* CONFIG_SHA256 */
1388 #endif /* OPENSSL_NO_SHA256 */
1389 	default:
1390 		return NULL;
1391 	}
1392 
1393 	ctx = os_zalloc(sizeof(*ctx));
1394 	if (ctx == NULL)
1395 		return NULL;
1396 	ctx->ctx = HMAC_CTX_new();
1397 	if (!ctx->ctx) {
1398 		os_free(ctx);
1399 		return NULL;
1400 	}
1401 
1402 	if (HMAC_Init_ex(ctx->ctx, key, key_len, md, NULL) != 1) {
1403 		HMAC_CTX_free(ctx->ctx);
1404 		bin_clear_free(ctx, sizeof(*ctx));
1405 		return NULL;
1406 	}
1407 
1408 	return ctx;
1409 #endif /* OpenSSL version >= 3.0 */
1410 }
1411 
1412 
crypto_hash_update(struct crypto_hash * ctx,const u8 * data,size_t len)1413 void crypto_hash_update(struct crypto_hash *ctx, const u8 *data, size_t len)
1414 {
1415 	if (ctx == NULL)
1416 		return;
1417 #if OPENSSL_VERSION_NUMBER >= 0x30000000L
1418 	EVP_MAC_update(ctx->ctx, data, len);
1419 #else /* OpenSSL version >= 3.0 */
1420 	HMAC_Update(ctx->ctx, data, len);
1421 #endif /* OpenSSL version >= 3.0 */
1422 }
1423 
1424 
crypto_hash_finish(struct crypto_hash * ctx,u8 * mac,size_t * len)1425 int crypto_hash_finish(struct crypto_hash *ctx, u8 *mac, size_t *len)
1426 {
1427 #if OPENSSL_VERSION_NUMBER >= 0x30000000L
1428 	size_t mdlen;
1429 	int res;
1430 
1431 	if (!ctx)
1432 		return -2;
1433 
1434 	if (!mac || !len) {
1435 		EVP_MAC_CTX_free(ctx->ctx);
1436 		bin_clear_free(ctx, sizeof(*ctx));
1437 		return 0;
1438 	}
1439 
1440 	res = EVP_MAC_final(ctx->ctx, NULL, &mdlen, 0);
1441 	if (res != 1) {
1442 		EVP_MAC_CTX_free(ctx->ctx);
1443 		bin_clear_free(ctx, sizeof(*ctx));
1444 		return -1;
1445 	}
1446 	res = EVP_MAC_final(ctx->ctx, mac, &mdlen, mdlen);
1447 	EVP_MAC_CTX_free(ctx->ctx);
1448 	bin_clear_free(ctx, sizeof(*ctx));
1449 
1450 	if (TEST_FAIL())
1451 		return -1;
1452 
1453 	if (res == 1) {
1454 		*len = mdlen;
1455 		return 0;
1456 	}
1457 
1458 	return -1;
1459 #else /* OpenSSL version >= 3.0 */
1460 	unsigned int mdlen;
1461 	int res;
1462 
1463 	if (ctx == NULL)
1464 		return -2;
1465 
1466 	if (mac == NULL || len == NULL) {
1467 		HMAC_CTX_free(ctx->ctx);
1468 		bin_clear_free(ctx, sizeof(*ctx));
1469 		return 0;
1470 	}
1471 
1472 	mdlen = *len;
1473 	res = HMAC_Final(ctx->ctx, mac, &mdlen);
1474 	HMAC_CTX_free(ctx->ctx);
1475 	bin_clear_free(ctx, sizeof(*ctx));
1476 
1477 	if (TEST_FAIL())
1478 		return -1;
1479 
1480 	if (res == 1) {
1481 		*len = mdlen;
1482 		return 0;
1483 	}
1484 
1485 	return -1;
1486 #endif /* OpenSSL version >= 3.0 */
1487 }
1488 
1489 
1490 #if OPENSSL_VERSION_NUMBER >= 0x30000000L
1491 
openssl_hmac_vector(char * digest,const u8 * key,size_t key_len,size_t num_elem,const u8 * addr[],const size_t * len,u8 * mac,unsigned int mdlen)1492 static int openssl_hmac_vector(char *digest, const u8 *key,
1493 			       size_t key_len, size_t num_elem,
1494 			       const u8 *addr[], const size_t *len, u8 *mac,
1495 			       unsigned int mdlen)
1496 {
1497 	EVP_MAC *hmac;
1498 	OSSL_PARAM params[2];
1499 	EVP_MAC_CTX *ctx;
1500 	size_t i, mlen;
1501 	int res;
1502 
1503 	if (TEST_FAIL())
1504 		return -1;
1505 
1506 	hmac = EVP_MAC_fetch(NULL, "HMAC", NULL);
1507 	if (!hmac)
1508 		return -1;
1509 
1510 	params[0] = OSSL_PARAM_construct_utf8_string("digest", digest, 0);
1511 	params[1] = OSSL_PARAM_construct_end();
1512 
1513 	ctx = EVP_MAC_CTX_new(hmac);
1514 	EVP_MAC_free(hmac);
1515 	if (!ctx)
1516 		return -1;
1517 
1518 	if (EVP_MAC_init(ctx, key, key_len, params) != 1)
1519 		goto fail;
1520 
1521 	for (i = 0; i < num_elem; i++) {
1522 		if (EVP_MAC_update(ctx, addr[i], len[i]) != 1)
1523 			goto fail;
1524 	}
1525 
1526 	res = EVP_MAC_final(ctx, mac, &mlen, mdlen);
1527 	EVP_MAC_CTX_free(ctx);
1528 
1529 	return res == 1 ? 0 : -1;
1530 fail:
1531 	EVP_MAC_CTX_free(ctx);
1532 	return -1;
1533 }
1534 
1535 
1536 #ifndef CONFIG_FIPS
1537 
hmac_md5_vector(const u8 * key,size_t key_len,size_t num_elem,const u8 * addr[],const size_t * len,u8 * mac)1538 int hmac_md5_vector(const u8 *key, size_t key_len, size_t num_elem,
1539 		    const u8 *addr[], const size_t *len, u8 *mac)
1540 {
1541 	return openssl_hmac_vector("MD5", key ,key_len, num_elem, addr, len,
1542 				   mac, 16);
1543 }
1544 
1545 
hmac_md5(const u8 * key,size_t key_len,const u8 * data,size_t data_len,u8 * mac)1546 int hmac_md5(const u8 *key, size_t key_len, const u8 *data, size_t data_len,
1547 	     u8 *mac)
1548 {
1549 	return hmac_md5_vector(key, key_len, 1, &data, &data_len, mac);
1550 }
1551 
1552 #endif /* CONFIG_FIPS */
1553 
1554 
hmac_sha1_vector(const u8 * key,size_t key_len,size_t num_elem,const u8 * addr[],const size_t * len,u8 * mac)1555 int hmac_sha1_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("SHA1", key, key_len, num_elem, addr,
1559 				   len, mac, 20);
1560 }
1561 
1562 
hmac_sha1(const u8 * key,size_t key_len,const u8 * data,size_t data_len,u8 * mac)1563 int hmac_sha1(const u8 *key, size_t key_len, const u8 *data, size_t data_len,
1564 	       u8 *mac)
1565 {
1566 	return hmac_sha1_vector(key, key_len, 1, &data, &data_len, mac);
1567 }
1568 
1569 
1570 #ifdef CONFIG_SHA256
1571 
hmac_sha256_vector(const u8 * key,size_t key_len,size_t num_elem,const u8 * addr[],const size_t * len,u8 * mac)1572 int hmac_sha256_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("SHA256", key, key_len, num_elem, addr,
1576 				   len, mac, 32);
1577 }
1578 
1579 
hmac_sha256(const u8 * key,size_t key_len,const u8 * data,size_t data_len,u8 * mac)1580 int hmac_sha256(const u8 *key, size_t key_len, const u8 *data,
1581 		size_t data_len, u8 *mac)
1582 {
1583 	return hmac_sha256_vector(key, key_len, 1, &data, &data_len, mac);
1584 }
1585 
1586 #endif /* CONFIG_SHA256 */
1587 
1588 
1589 #ifdef CONFIG_SHA384
1590 
hmac_sha384_vector(const u8 * key,size_t key_len,size_t num_elem,const u8 * addr[],const size_t * len,u8 * mac)1591 int hmac_sha384_vector(const u8 *key, size_t key_len, size_t num_elem,
1592 		       const u8 *addr[], const size_t *len, u8 *mac)
1593 {
1594 	return openssl_hmac_vector("SHA384", key, key_len, num_elem, addr,
1595 				   len, mac, 48);
1596 }
1597 
1598 
hmac_sha384(const u8 * key,size_t key_len,const u8 * data,size_t data_len,u8 * mac)1599 int hmac_sha384(const u8 *key, size_t key_len, const u8 *data,
1600 		size_t data_len, u8 *mac)
1601 {
1602 	return hmac_sha384_vector(key, key_len, 1, &data, &data_len, mac);
1603 }
1604 
1605 #endif /* CONFIG_SHA384 */
1606 
1607 
1608 #ifdef CONFIG_SHA512
1609 
hmac_sha512_vector(const u8 * key,size_t key_len,size_t num_elem,const u8 * addr[],const size_t * len,u8 * mac)1610 int hmac_sha512_vector(const u8 *key, size_t key_len, size_t num_elem,
1611 		       const u8 *addr[], const size_t *len, u8 *mac)
1612 {
1613 	return openssl_hmac_vector("SHA512", key, key_len, num_elem, addr,
1614 				   len, mac, 64);
1615 }
1616 
1617 
hmac_sha512(const u8 * key,size_t key_len,const u8 * data,size_t data_len,u8 * mac)1618 int hmac_sha512(const u8 *key, size_t key_len, const u8 *data,
1619 		size_t data_len, u8 *mac)
1620 {
1621 	return hmac_sha512_vector(key, key_len, 1, &data, &data_len, mac);
1622 }
1623 
1624 #endif /* CONFIG_SHA512 */
1625 
1626 #else /* OpenSSL version >= 3.0 */
1627 
openssl_hmac_vector(const EVP_MD * type,const u8 * key,size_t key_len,size_t num_elem,const u8 * addr[],const size_t * len,u8 * mac,unsigned int mdlen)1628 static int openssl_hmac_vector(const EVP_MD *type, const u8 *key,
1629 			       size_t key_len, size_t num_elem,
1630 			       const u8 *addr[], const size_t *len, u8 *mac,
1631 			       unsigned int mdlen)
1632 {
1633 	HMAC_CTX *ctx;
1634 	size_t i;
1635 	int res;
1636 
1637 	if (TEST_FAIL())
1638 		return -1;
1639 
1640 	ctx = HMAC_CTX_new();
1641 	if (!ctx)
1642 		return -1;
1643 	res = HMAC_Init_ex(ctx, key, key_len, type, NULL);
1644 	if (res != 1)
1645 		goto done;
1646 
1647 	for (i = 0; i < num_elem; i++)
1648 		HMAC_Update(ctx, addr[i], len[i]);
1649 
1650 	res = HMAC_Final(ctx, mac, &mdlen);
1651 done:
1652 	HMAC_CTX_free(ctx);
1653 
1654 	return res == 1 ? 0 : -1;
1655 }
1656 
1657 
1658 #ifndef CONFIG_FIPS
1659 
hmac_md5_vector(const u8 * key,size_t key_len,size_t num_elem,const u8 * addr[],const size_t * len,u8 * mac)1660 int hmac_md5_vector(const u8 *key, size_t key_len, size_t num_elem,
1661 		    const u8 *addr[], const size_t *len, u8 *mac)
1662 {
1663 	return openssl_hmac_vector(EVP_md5(), key ,key_len, num_elem, addr, len,
1664 				   mac, 16);
1665 }
1666 
1667 
hmac_md5(const u8 * key,size_t key_len,const u8 * data,size_t data_len,u8 * mac)1668 int hmac_md5(const u8 *key, size_t key_len, const u8 *data, size_t data_len,
1669 	     u8 *mac)
1670 {
1671 	return hmac_md5_vector(key, key_len, 1, &data, &data_len, mac);
1672 }
1673 
1674 #endif /* CONFIG_FIPS */
1675 
1676 
hmac_sha1_vector(const u8 * key,size_t key_len,size_t num_elem,const u8 * addr[],const size_t * len,u8 * mac)1677 int hmac_sha1_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_sha1(), key, key_len, num_elem, addr,
1681 				   len, mac, 20);
1682 }
1683 
1684 
hmac_sha1(const u8 * key,size_t key_len,const u8 * data,size_t data_len,u8 * mac)1685 int hmac_sha1(const u8 *key, size_t key_len, const u8 *data, size_t data_len,
1686 	       u8 *mac)
1687 {
1688 	return hmac_sha1_vector(key, key_len, 1, &data, &data_len, mac);
1689 }
1690 
1691 
1692 #ifdef CONFIG_SHA256
1693 
hmac_sha256_vector(const u8 * key,size_t key_len,size_t num_elem,const u8 * addr[],const size_t * len,u8 * mac)1694 int hmac_sha256_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_sha256(), key, key_len, num_elem, addr,
1698 				   len, mac, 32);
1699 }
1700 
1701 
hmac_sha256(const u8 * key,size_t key_len,const u8 * data,size_t data_len,u8 * mac)1702 int hmac_sha256(const u8 *key, size_t key_len, const u8 *data,
1703 		size_t data_len, u8 *mac)
1704 {
1705 	return hmac_sha256_vector(key, key_len, 1, &data, &data_len, mac);
1706 }
1707 
1708 #endif /* CONFIG_SHA256 */
1709 
1710 
1711 #ifdef CONFIG_SHA384
1712 
hmac_sha384_vector(const u8 * key,size_t key_len,size_t num_elem,const u8 * addr[],const size_t * len,u8 * mac)1713 int hmac_sha384_vector(const u8 *key, size_t key_len, size_t num_elem,
1714 		       const u8 *addr[], const size_t *len, u8 *mac)
1715 {
1716 	return openssl_hmac_vector(EVP_sha384(), key, key_len, num_elem, addr,
1717 				   len, mac, 48);
1718 }
1719 
1720 
hmac_sha384(const u8 * key,size_t key_len,const u8 * data,size_t data_len,u8 * mac)1721 int hmac_sha384(const u8 *key, size_t key_len, const u8 *data,
1722 		size_t data_len, u8 *mac)
1723 {
1724 	return hmac_sha384_vector(key, key_len, 1, &data, &data_len, mac);
1725 }
1726 
1727 #endif /* CONFIG_SHA384 */
1728 
1729 
1730 #ifdef CONFIG_SHA512
1731 
hmac_sha512_vector(const u8 * key,size_t key_len,size_t num_elem,const u8 * addr[],const size_t * len,u8 * mac)1732 int hmac_sha512_vector(const u8 *key, size_t key_len, size_t num_elem,
1733 		       const u8 *addr[], const size_t *len, u8 *mac)
1734 {
1735 	return openssl_hmac_vector(EVP_sha512(), key, key_len, num_elem, addr,
1736 				   len, mac, 64);
1737 }
1738 
1739 
hmac_sha512(const u8 * key,size_t key_len,const u8 * data,size_t data_len,u8 * mac)1740 int hmac_sha512(const u8 *key, size_t key_len, const u8 *data,
1741 		size_t data_len, u8 *mac)
1742 {
1743 	return hmac_sha512_vector(key, key_len, 1, &data, &data_len, mac);
1744 }
1745 
1746 #endif /* CONFIG_SHA512 */
1747 
1748 #endif /* OpenSSL version >= 3.0 */
1749 
1750 
pbkdf2_sha1(const char * passphrase,const u8 * ssid,size_t ssid_len,int iterations,u8 * buf,size_t buflen)1751 int pbkdf2_sha1(const char *passphrase, const u8 *ssid, size_t ssid_len,
1752 		int iterations, u8 *buf, size_t buflen)
1753 {
1754 	if (PKCS5_PBKDF2_HMAC_SHA1(passphrase, os_strlen(passphrase), ssid,
1755 				   ssid_len, iterations, buflen, buf) != 1)
1756 		return -1;
1757 	return 0;
1758 }
1759 
1760 
crypto_get_random(void * buf,size_t len)1761 int crypto_get_random(void *buf, size_t len)
1762 {
1763 	if (RAND_bytes(buf, len) != 1)
1764 		return -1;
1765 	return 0;
1766 }
1767 
1768 
omac1_aes_vector(const u8 * key,size_t key_len,size_t num_elem,const u8 * addr[],const size_t * len,u8 * mac)1769 int omac1_aes_vector(const u8 *key, size_t key_len, size_t num_elem,
1770 		     const u8 *addr[], const size_t *len, u8 *mac)
1771 {
1772 #if OPENSSL_VERSION_NUMBER >= 0x30000000L
1773 	EVP_MAC_CTX *ctx = NULL;
1774 	EVP_MAC *emac;
1775 	int ret = -1;
1776 	size_t outlen, i;
1777 	OSSL_PARAM params[2];
1778 	char *cipher = NULL;
1779 
1780 	if (TEST_FAIL())
1781 		return -1;
1782 
1783 	emac = EVP_MAC_fetch(NULL, "CMAC", NULL);
1784 
1785 	if (key_len == 32)
1786 		cipher = "aes-256-cbc";
1787 	else if (key_len == 24)
1788 		cipher = "aes-192-cbc";
1789 	else if (key_len == 16)
1790 		cipher = "aes-128-cbc";
1791 
1792 	params[0] = OSSL_PARAM_construct_utf8_string("cipher", cipher, 0);
1793 	params[1] = OSSL_PARAM_construct_end();
1794 
1795 	if (!emac || !cipher ||
1796 	    !(ctx = EVP_MAC_CTX_new(emac)) ||
1797 	    EVP_MAC_init(ctx, key, key_len, params) != 1)
1798 		goto fail;
1799 
1800 	for (i = 0; i < num_elem; i++) {
1801 		if (!EVP_MAC_update(ctx, addr[i], len[i]))
1802 			goto fail;
1803 	}
1804 	if (EVP_MAC_final(ctx, mac, &outlen, 16) != 1 || outlen != 16)
1805 		goto fail;
1806 
1807 	ret = 0;
1808 fail:
1809 	EVP_MAC_CTX_free(ctx);
1810 	return ret;
1811 #else /* OpenSSL version >= 3.0 */
1812 	CMAC_CTX *ctx;
1813 	int ret = -1;
1814 	size_t outlen, i;
1815 
1816 	if (TEST_FAIL())
1817 		return -1;
1818 
1819 	ctx = CMAC_CTX_new();
1820 	if (ctx == NULL)
1821 		return -1;
1822 
1823 	if (key_len == 32) {
1824 		if (!CMAC_Init(ctx, key, 32, EVP_aes_256_cbc(), NULL))
1825 			goto fail;
1826 	} else if (key_len == 24) {
1827 		if (!CMAC_Init(ctx, key, 24, EVP_aes_192_cbc(), NULL))
1828 			goto fail;
1829 	} else if (key_len == 16) {
1830 		if (!CMAC_Init(ctx, key, 16, EVP_aes_128_cbc(), NULL))
1831 			goto fail;
1832 	} else {
1833 		goto fail;
1834 	}
1835 	for (i = 0; i < num_elem; i++) {
1836 		if (!CMAC_Update(ctx, addr[i], len[i]))
1837 			goto fail;
1838 	}
1839 	if (!CMAC_Final(ctx, mac, &outlen) || outlen != 16)
1840 		goto fail;
1841 
1842 	ret = 0;
1843 fail:
1844 	CMAC_CTX_free(ctx);
1845 	return ret;
1846 #endif /* OpenSSL version >= 3.0 */
1847 }
1848 
1849 
omac1_aes_128_vector(const u8 * key,size_t num_elem,const u8 * addr[],const size_t * len,u8 * mac)1850 int omac1_aes_128_vector(const u8 *key, size_t num_elem,
1851 			 const u8 *addr[], const size_t *len, u8 *mac)
1852 {
1853 	return omac1_aes_vector(key, 16, num_elem, addr, len, mac);
1854 }
1855 
1856 
omac1_aes_128(const u8 * key,const u8 * data,size_t data_len,u8 * mac)1857 int omac1_aes_128(const u8 *key, const u8 *data, size_t data_len, u8 *mac)
1858 {
1859 	return omac1_aes_128_vector(key, 1, &data, &data_len, mac);
1860 }
1861 
1862 
omac1_aes_256(const u8 * key,const u8 * data,size_t data_len,u8 * mac)1863 int omac1_aes_256(const u8 *key, const u8 *data, size_t data_len, u8 *mac)
1864 {
1865 	return omac1_aes_vector(key, 32, 1, &data, &data_len, mac);
1866 }
1867 
1868 
crypto_bignum_init(void)1869 struct crypto_bignum * crypto_bignum_init(void)
1870 {
1871 	if (TEST_FAIL())
1872 		return NULL;
1873 	return (struct crypto_bignum *) BN_new();
1874 }
1875 
1876 
crypto_bignum_init_set(const u8 * buf,size_t len)1877 struct crypto_bignum * crypto_bignum_init_set(const u8 *buf, size_t len)
1878 {
1879 	BIGNUM *bn;
1880 
1881 	if (TEST_FAIL())
1882 		return NULL;
1883 
1884 	bn = BN_bin2bn(buf, len, NULL);
1885 	return (struct crypto_bignum *) bn;
1886 }
1887 
1888 
crypto_bignum_init_uint(unsigned int val)1889 struct crypto_bignum * crypto_bignum_init_uint(unsigned int val)
1890 {
1891 	BIGNUM *bn;
1892 
1893 	if (TEST_FAIL())
1894 		return NULL;
1895 
1896 	bn = BN_new();
1897 	if (!bn)
1898 		return NULL;
1899 	if (BN_set_word(bn, val) != 1) {
1900 		BN_free(bn);
1901 		return NULL;
1902 	}
1903 	return (struct crypto_bignum *) bn;
1904 }
1905 
1906 
crypto_bignum_deinit(struct crypto_bignum * n,int clear)1907 void crypto_bignum_deinit(struct crypto_bignum *n, int clear)
1908 {
1909 	if (clear)
1910 		BN_clear_free((BIGNUM *) n);
1911 	else
1912 		BN_free((BIGNUM *) n);
1913 }
1914 
1915 
crypto_bignum_to_bin(const struct crypto_bignum * a,u8 * buf,size_t buflen,size_t padlen)1916 int crypto_bignum_to_bin(const struct crypto_bignum *a,
1917 			 u8 *buf, size_t buflen, size_t padlen)
1918 {
1919 	int num_bytes, offset;
1920 
1921 	if (TEST_FAIL())
1922 		return -1;
1923 
1924 	if (padlen > buflen)
1925 		return -1;
1926 
1927 	if (padlen) {
1928 #ifdef OPENSSL_IS_BORINGSSL
1929 		if (BN_bn2bin_padded(buf, padlen, (const BIGNUM *) a) == 0)
1930 			return -1;
1931 		return padlen;
1932 #else /* OPENSSL_IS_BORINGSSL */
1933 #if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)
1934 		return BN_bn2binpad((const BIGNUM *) a, buf, padlen);
1935 #endif
1936 #endif
1937 	}
1938 
1939 	num_bytes = BN_num_bytes((const BIGNUM *) a);
1940 	if ((size_t) num_bytes > buflen)
1941 		return -1;
1942 	if (padlen > (size_t) num_bytes)
1943 		offset = padlen - num_bytes;
1944 	else
1945 		offset = 0;
1946 
1947 	os_memset(buf, 0, offset);
1948 	BN_bn2bin((const BIGNUM *) a, buf + offset);
1949 
1950 	return num_bytes + offset;
1951 }
1952 
1953 
crypto_bignum_rand(struct crypto_bignum * r,const struct crypto_bignum * m)1954 int crypto_bignum_rand(struct crypto_bignum *r, const struct crypto_bignum *m)
1955 {
1956 	if (TEST_FAIL())
1957 		return -1;
1958 	return BN_rand_range((BIGNUM *) r, (const BIGNUM *) m) == 1 ? 0 : -1;
1959 }
1960 
1961 
crypto_bignum_add(const struct crypto_bignum * a,const struct crypto_bignum * b,struct crypto_bignum * c)1962 int crypto_bignum_add(const struct crypto_bignum *a,
1963 		      const struct crypto_bignum *b,
1964 		      struct crypto_bignum *c)
1965 {
1966 	return BN_add((BIGNUM *) c, (const BIGNUM *) a, (const BIGNUM *) b) ?
1967 		0 : -1;
1968 }
1969 
1970 
crypto_bignum_mod(const struct crypto_bignum * a,const struct crypto_bignum * b,struct crypto_bignum * c)1971 int crypto_bignum_mod(const struct crypto_bignum *a,
1972 		      const struct crypto_bignum *b,
1973 		      struct crypto_bignum *c)
1974 {
1975 	int res;
1976 	BN_CTX *bnctx;
1977 
1978 	bnctx = BN_CTX_new();
1979 	if (bnctx == NULL)
1980 		return -1;
1981 	res = BN_mod((BIGNUM *) c, (const BIGNUM *) a, (const BIGNUM *) b,
1982 		     bnctx);
1983 	BN_CTX_free(bnctx);
1984 
1985 	return res ? 0 : -1;
1986 }
1987 
1988 
crypto_bignum_exptmod(const struct crypto_bignum * a,const struct crypto_bignum * b,const struct crypto_bignum * c,struct crypto_bignum * d)1989 int crypto_bignum_exptmod(const struct crypto_bignum *a,
1990 			  const struct crypto_bignum *b,
1991 			  const struct crypto_bignum *c,
1992 			  struct crypto_bignum *d)
1993 {
1994 	int res;
1995 	BN_CTX *bnctx;
1996 
1997 	if (TEST_FAIL())
1998 		return -1;
1999 
2000 	bnctx = BN_CTX_new();
2001 	if (bnctx == NULL)
2002 		return -1;
2003 	res = BN_mod_exp_mont_consttime((BIGNUM *) d, (const BIGNUM *) a,
2004 					(const BIGNUM *) b, (const BIGNUM *) c,
2005 					bnctx, NULL);
2006 	BN_CTX_free(bnctx);
2007 
2008 	return res ? 0 : -1;
2009 }
2010 
2011 
crypto_bignum_inverse(const struct crypto_bignum * a,const struct crypto_bignum * b,struct crypto_bignum * c)2012 int crypto_bignum_inverse(const struct crypto_bignum *a,
2013 			  const struct crypto_bignum *b,
2014 			  struct crypto_bignum *c)
2015 {
2016 	BIGNUM *res;
2017 	BN_CTX *bnctx;
2018 
2019 	if (TEST_FAIL())
2020 		return -1;
2021 	bnctx = BN_CTX_new();
2022 	if (bnctx == NULL)
2023 		return -1;
2024 #ifdef OPENSSL_IS_BORINGSSL
2025 	/* TODO: use BN_mod_inverse_blinded() ? */
2026 #else /* OPENSSL_IS_BORINGSSL */
2027 	BN_set_flags((BIGNUM *) a, BN_FLG_CONSTTIME);
2028 #endif /* OPENSSL_IS_BORINGSSL */
2029 	res = BN_mod_inverse((BIGNUM *) c, (const BIGNUM *) a,
2030 			     (const BIGNUM *) b, bnctx);
2031 	BN_CTX_free(bnctx);
2032 
2033 	return res ? 0 : -1;
2034 }
2035 
2036 
crypto_bignum_sub(const struct crypto_bignum * a,const struct crypto_bignum * b,struct crypto_bignum * c)2037 int crypto_bignum_sub(const struct crypto_bignum *a,
2038 		      const struct crypto_bignum *b,
2039 		      struct crypto_bignum *c)
2040 {
2041 	if (TEST_FAIL())
2042 		return -1;
2043 	return BN_sub((BIGNUM *) c, (const BIGNUM *) a, (const BIGNUM *) b) ?
2044 		0 : -1;
2045 }
2046 
2047 
crypto_bignum_div(const struct crypto_bignum * a,const struct crypto_bignum * b,struct crypto_bignum * c)2048 int crypto_bignum_div(const struct crypto_bignum *a,
2049 		      const struct crypto_bignum *b,
2050 		      struct crypto_bignum *c)
2051 {
2052 	int res;
2053 
2054 	BN_CTX *bnctx;
2055 
2056 	if (TEST_FAIL())
2057 		return -1;
2058 
2059 	bnctx = BN_CTX_new();
2060 	if (bnctx == NULL)
2061 		return -1;
2062 #ifndef OPENSSL_IS_BORINGSSL
2063 	BN_set_flags((BIGNUM *) a, BN_FLG_CONSTTIME);
2064 #endif /* OPENSSL_IS_BORINGSSL */
2065 	res = BN_div((BIGNUM *) c, NULL, (const BIGNUM *) a,
2066 		     (const BIGNUM *) b, bnctx);
2067 	BN_CTX_free(bnctx);
2068 
2069 	return res ? 0 : -1;
2070 }
2071 
2072 
crypto_bignum_addmod(const struct crypto_bignum * a,const struct crypto_bignum * b,const struct crypto_bignum * c,struct crypto_bignum * d)2073 int crypto_bignum_addmod(const struct crypto_bignum *a,
2074 			 const struct crypto_bignum *b,
2075 			 const struct crypto_bignum *c,
2076 			 struct crypto_bignum *d)
2077 {
2078 	int res;
2079 	BN_CTX *bnctx;
2080 
2081 	if (TEST_FAIL())
2082 		return -1;
2083 
2084 	bnctx = BN_CTX_new();
2085 	if (!bnctx)
2086 		return -1;
2087 	res = BN_mod_add((BIGNUM *) d, (const BIGNUM *) a, (const BIGNUM *) b,
2088 			 (const BIGNUM *) c, bnctx);
2089 	BN_CTX_free(bnctx);
2090 
2091 	return res ? 0 : -1;
2092 }
2093 
2094 
crypto_bignum_mulmod(const struct crypto_bignum * a,const struct crypto_bignum * b,const struct crypto_bignum * c,struct crypto_bignum * d)2095 int crypto_bignum_mulmod(const struct crypto_bignum *a,
2096 			 const struct crypto_bignum *b,
2097 			 const struct crypto_bignum *c,
2098 			 struct crypto_bignum *d)
2099 {
2100 	int res;
2101 
2102 	BN_CTX *bnctx;
2103 
2104 	if (TEST_FAIL())
2105 		return -1;
2106 
2107 	bnctx = BN_CTX_new();
2108 	if (bnctx == NULL)
2109 		return -1;
2110 	res = BN_mod_mul((BIGNUM *) d, (const BIGNUM *) a, (const BIGNUM *) b,
2111 			 (const BIGNUM *) c, bnctx);
2112 	BN_CTX_free(bnctx);
2113 
2114 	return res ? 0 : -1;
2115 }
2116 
2117 
crypto_bignum_sqrmod(const struct crypto_bignum * a,const struct crypto_bignum * b,struct crypto_bignum * c)2118 int crypto_bignum_sqrmod(const struct crypto_bignum *a,
2119 			 const struct crypto_bignum *b,
2120 			 struct crypto_bignum *c)
2121 {
2122 	int res;
2123 	BN_CTX *bnctx;
2124 
2125 	if (TEST_FAIL())
2126 		return -1;
2127 
2128 	bnctx = BN_CTX_new();
2129 	if (!bnctx)
2130 		return -1;
2131 	res = BN_mod_sqr((BIGNUM *) c, (const BIGNUM *) a, (const BIGNUM *) b,
2132 			 bnctx);
2133 	BN_CTX_free(bnctx);
2134 
2135 	return res ? 0 : -1;
2136 }
2137 
2138 
crypto_bignum_rshift(const struct crypto_bignum * a,int n,struct crypto_bignum * r)2139 int crypto_bignum_rshift(const struct crypto_bignum *a, int n,
2140 			 struct crypto_bignum *r)
2141 {
2142 	/* Note: BN_rshift() does not modify the first argument even though it
2143 	 * has not been marked const. */
2144 	return BN_rshift((BIGNUM *) a, (BIGNUM *) r, n) == 1 ? 0 : -1;
2145 }
2146 
2147 
crypto_bignum_cmp(const struct crypto_bignum * a,const struct crypto_bignum * b)2148 int crypto_bignum_cmp(const struct crypto_bignum *a,
2149 		      const struct crypto_bignum *b)
2150 {
2151 	return BN_cmp((const BIGNUM *) a, (const BIGNUM *) b);
2152 }
2153 
2154 
crypto_bignum_is_zero(const struct crypto_bignum * a)2155 int crypto_bignum_is_zero(const struct crypto_bignum *a)
2156 {
2157 	return BN_is_zero((const BIGNUM *) a);
2158 }
2159 
2160 
crypto_bignum_is_one(const struct crypto_bignum * a)2161 int crypto_bignum_is_one(const struct crypto_bignum *a)
2162 {
2163 	return BN_is_one((const BIGNUM *) a);
2164 }
2165 
2166 
crypto_bignum_is_odd(const struct crypto_bignum * a)2167 int crypto_bignum_is_odd(const struct crypto_bignum *a)
2168 {
2169 	return BN_is_odd((const BIGNUM *) a);
2170 }
2171 
2172 
crypto_bignum_legendre(const struct crypto_bignum * a,const struct crypto_bignum * p)2173 int crypto_bignum_legendre(const struct crypto_bignum *a,
2174 			   const struct crypto_bignum *p)
2175 {
2176 	BN_CTX *bnctx;
2177 	BIGNUM *exp = NULL, *tmp = NULL;
2178 	int res = -2;
2179 	unsigned int mask;
2180 
2181 	if (TEST_FAIL())
2182 		return -2;
2183 
2184 	bnctx = BN_CTX_new();
2185 	if (bnctx == NULL)
2186 		return -2;
2187 
2188 	exp = BN_new();
2189 	tmp = BN_new();
2190 	if (!exp || !tmp ||
2191 	    /* exp = (p-1) / 2 */
2192 	    !BN_sub(exp, (const BIGNUM *) p, BN_value_one()) ||
2193 	    !BN_rshift1(exp, exp) ||
2194 	    !BN_mod_exp_mont_consttime(tmp, (const BIGNUM *) a, exp,
2195 				       (const BIGNUM *) p, bnctx, NULL))
2196 		goto fail;
2197 
2198 	/* Return 1 if tmp == 1, 0 if tmp == 0, or -1 otherwise. Need to use
2199 	 * constant time selection to avoid branches here. */
2200 	res = -1;
2201 	mask = const_time_eq(BN_is_word(tmp, 1), 1);
2202 	res = const_time_select_int(mask, 1, res);
2203 	mask = const_time_eq(BN_is_zero(tmp), 1);
2204 	res = const_time_select_int(mask, 0, res);
2205 
2206 fail:
2207 	BN_clear_free(tmp);
2208 	BN_clear_free(exp);
2209 	BN_CTX_free(bnctx);
2210 	return res;
2211 }
2212 
2213 
2214 #ifdef CONFIG_ECC
2215 
2216 struct crypto_ec {
2217 	EC_GROUP *group;
2218 	int nid;
2219 	BN_CTX *bnctx;
2220 	BIGNUM *prime;
2221 	BIGNUM *order;
2222 	BIGNUM *a;
2223 	BIGNUM *b;
2224 };
2225 
2226 
crypto_ec_group_2_nid(int group)2227 static int crypto_ec_group_2_nid(int group)
2228 {
2229 	/* Map from IANA registry for IKE D-H groups to OpenSSL NID */
2230 	switch (group) {
2231 	case 19:
2232 		return NID_X9_62_prime256v1;
2233 	case 20:
2234 		return NID_secp384r1;
2235 	case 21:
2236 		return NID_secp521r1;
2237 	case 25:
2238 		return NID_X9_62_prime192v1;
2239 	case 26:
2240 		return NID_secp224r1;
2241 #ifdef NID_brainpoolP224r1
2242 	case 27:
2243 		return NID_brainpoolP224r1;
2244 #endif /* NID_brainpoolP224r1 */
2245 #ifdef NID_brainpoolP256r1
2246 	case 28:
2247 		return NID_brainpoolP256r1;
2248 #endif /* NID_brainpoolP256r1 */
2249 #ifdef NID_brainpoolP384r1
2250 	case 29:
2251 		return NID_brainpoolP384r1;
2252 #endif /* NID_brainpoolP384r1 */
2253 #ifdef NID_brainpoolP512r1
2254 	case 30:
2255 		return NID_brainpoolP512r1;
2256 #endif /* NID_brainpoolP512r1 */
2257 	default:
2258 		return -1;
2259 	}
2260 }
2261 
2262 
crypto_ec_init(int group)2263 struct crypto_ec * crypto_ec_init(int group)
2264 {
2265 	struct crypto_ec *e;
2266 	int nid;
2267 
2268 	nid = crypto_ec_group_2_nid(group);
2269 	if (nid < 0)
2270 		return NULL;
2271 
2272 	e = os_zalloc(sizeof(*e));
2273 	if (e == NULL)
2274 		return NULL;
2275 
2276 	e->nid = nid;
2277 	e->bnctx = BN_CTX_new();
2278 	e->group = EC_GROUP_new_by_curve_name(nid);
2279 	e->prime = BN_new();
2280 	e->order = BN_new();
2281 	e->a = BN_new();
2282 	e->b = BN_new();
2283 	if (e->group == NULL || e->bnctx == NULL || e->prime == NULL ||
2284 	    e->order == NULL || e->a == NULL || e->b == NULL ||
2285 	    !EC_GROUP_get_curve(e->group, e->prime, e->a, e->b, e->bnctx) ||
2286 	    !EC_GROUP_get_order(e->group, e->order, e->bnctx)) {
2287 		crypto_ec_deinit(e);
2288 		e = NULL;
2289 	}
2290 
2291 	return e;
2292 }
2293 
2294 
crypto_ec_deinit(struct crypto_ec * e)2295 void crypto_ec_deinit(struct crypto_ec *e)
2296 {
2297 	if (e == NULL)
2298 		return;
2299 	BN_clear_free(e->b);
2300 	BN_clear_free(e->a);
2301 	BN_clear_free(e->order);
2302 	BN_clear_free(e->prime);
2303 	EC_GROUP_free(e->group);
2304 	BN_CTX_free(e->bnctx);
2305 	os_free(e);
2306 }
2307 
2308 
crypto_ec_point_init(struct crypto_ec * e)2309 struct crypto_ec_point * crypto_ec_point_init(struct crypto_ec *e)
2310 {
2311 	if (TEST_FAIL())
2312 		return NULL;
2313 	if (e == NULL)
2314 		return NULL;
2315 	return (struct crypto_ec_point *) EC_POINT_new(e->group);
2316 }
2317 
2318 
crypto_ec_prime_len(struct crypto_ec * e)2319 size_t crypto_ec_prime_len(struct crypto_ec *e)
2320 {
2321 	return BN_num_bytes(e->prime);
2322 }
2323 
2324 
crypto_ec_prime_len_bits(struct crypto_ec * e)2325 size_t crypto_ec_prime_len_bits(struct crypto_ec *e)
2326 {
2327 	return BN_num_bits(e->prime);
2328 }
2329 
2330 
crypto_ec_order_len(struct crypto_ec * e)2331 size_t crypto_ec_order_len(struct crypto_ec *e)
2332 {
2333 	return BN_num_bytes(e->order);
2334 }
2335 
2336 
crypto_ec_get_prime(struct crypto_ec * e)2337 const struct crypto_bignum * crypto_ec_get_prime(struct crypto_ec *e)
2338 {
2339 	return (const struct crypto_bignum *) e->prime;
2340 }
2341 
2342 
crypto_ec_get_order(struct crypto_ec * e)2343 const struct crypto_bignum * crypto_ec_get_order(struct crypto_ec *e)
2344 {
2345 	return (const struct crypto_bignum *) e->order;
2346 }
2347 
2348 
crypto_ec_get_a(struct crypto_ec * e)2349 const struct crypto_bignum * crypto_ec_get_a(struct crypto_ec *e)
2350 {
2351 	return (const struct crypto_bignum *) e->a;
2352 }
2353 
2354 
crypto_ec_get_b(struct crypto_ec * e)2355 const struct crypto_bignum * crypto_ec_get_b(struct crypto_ec *e)
2356 {
2357 	return (const struct crypto_bignum *) e->b;
2358 }
2359 
2360 
crypto_ec_get_generator(struct crypto_ec * e)2361 const struct crypto_ec_point * crypto_ec_get_generator(struct crypto_ec *e)
2362 {
2363 	return (const struct crypto_ec_point *)
2364 		EC_GROUP_get0_generator(e->group);
2365 }
2366 
2367 
crypto_ec_point_deinit(struct crypto_ec_point * p,int clear)2368 void crypto_ec_point_deinit(struct crypto_ec_point *p, int clear)
2369 {
2370 	if (clear)
2371 		EC_POINT_clear_free((EC_POINT *) p);
2372 	else
2373 		EC_POINT_free((EC_POINT *) p);
2374 }
2375 
2376 
crypto_ec_point_x(struct crypto_ec * e,const struct crypto_ec_point * p,struct crypto_bignum * x)2377 int crypto_ec_point_x(struct crypto_ec *e, const struct crypto_ec_point *p,
2378 		      struct crypto_bignum *x)
2379 {
2380 	return EC_POINT_get_affine_coordinates(e->group,
2381 					       (const EC_POINT *) p,
2382 					       (BIGNUM *) x, NULL,
2383 					       e->bnctx) == 1 ? 0 : -1;
2384 }
2385 
2386 
crypto_ec_point_to_bin(struct crypto_ec * e,const struct crypto_ec_point * point,u8 * x,u8 * y)2387 int crypto_ec_point_to_bin(struct crypto_ec *e,
2388 			   const struct crypto_ec_point *point, u8 *x, u8 *y)
2389 {
2390 	BIGNUM *x_bn, *y_bn;
2391 	int ret = -1;
2392 	int len = BN_num_bytes(e->prime);
2393 
2394 	if (TEST_FAIL())
2395 		return -1;
2396 
2397 	x_bn = BN_new();
2398 	y_bn = BN_new();
2399 
2400 	if (x_bn && y_bn &&
2401 	    EC_POINT_get_affine_coordinates(e->group, (EC_POINT *) point,
2402 					    x_bn, y_bn, e->bnctx)) {
2403 		if (x) {
2404 			crypto_bignum_to_bin((struct crypto_bignum *) x_bn,
2405 					     x, len, len);
2406 		}
2407 		if (y) {
2408 			crypto_bignum_to_bin((struct crypto_bignum *) y_bn,
2409 					     y, len, len);
2410 		}
2411 		ret = 0;
2412 	}
2413 
2414 	BN_clear_free(x_bn);
2415 	BN_clear_free(y_bn);
2416 	return ret;
2417 }
2418 
2419 
crypto_ec_point_from_bin(struct crypto_ec * e,const u8 * val)2420 struct crypto_ec_point * crypto_ec_point_from_bin(struct crypto_ec *e,
2421 						  const u8 *val)
2422 {
2423 	BIGNUM *x, *y;
2424 	EC_POINT *elem;
2425 	int len = BN_num_bytes(e->prime);
2426 
2427 	if (TEST_FAIL())
2428 		return NULL;
2429 
2430 	x = BN_bin2bn(val, len, NULL);
2431 	y = BN_bin2bn(val + len, len, NULL);
2432 	elem = EC_POINT_new(e->group);
2433 	if (x == NULL || y == NULL || elem == NULL) {
2434 		BN_clear_free(x);
2435 		BN_clear_free(y);
2436 		EC_POINT_clear_free(elem);
2437 		return NULL;
2438 	}
2439 
2440 	if (!EC_POINT_set_affine_coordinates(e->group, elem, x, y, e->bnctx)) {
2441 		EC_POINT_clear_free(elem);
2442 		elem = NULL;
2443 	}
2444 
2445 	BN_clear_free(x);
2446 	BN_clear_free(y);
2447 
2448 	return (struct crypto_ec_point *) elem;
2449 }
2450 
2451 
crypto_ec_point_add(struct crypto_ec * e,const struct crypto_ec_point * a,const struct crypto_ec_point * b,struct crypto_ec_point * c)2452 int crypto_ec_point_add(struct crypto_ec *e, const struct crypto_ec_point *a,
2453 			const struct crypto_ec_point *b,
2454 			struct crypto_ec_point *c)
2455 {
2456 	if (TEST_FAIL())
2457 		return -1;
2458 	return EC_POINT_add(e->group, (EC_POINT *) c, (const EC_POINT *) a,
2459 			    (const EC_POINT *) b, e->bnctx) ? 0 : -1;
2460 }
2461 
2462 
crypto_ec_point_mul(struct crypto_ec * e,const struct crypto_ec_point * p,const struct crypto_bignum * b,struct crypto_ec_point * res)2463 int crypto_ec_point_mul(struct crypto_ec *e, const struct crypto_ec_point *p,
2464 			const struct crypto_bignum *b,
2465 			struct crypto_ec_point *res)
2466 {
2467 	if (TEST_FAIL())
2468 		return -1;
2469 	return EC_POINT_mul(e->group, (EC_POINT *) res, NULL,
2470 			    (const EC_POINT *) p, (const BIGNUM *) b, e->bnctx)
2471 		? 0 : -1;
2472 }
2473 
2474 
crypto_ec_point_invert(struct crypto_ec * e,struct crypto_ec_point * p)2475 int crypto_ec_point_invert(struct crypto_ec *e, struct crypto_ec_point *p)
2476 {
2477 	if (TEST_FAIL())
2478 		return -1;
2479 	return EC_POINT_invert(e->group, (EC_POINT *) p, e->bnctx) ? 0 : -1;
2480 }
2481 
2482 
2483 struct crypto_bignum *
crypto_ec_point_compute_y_sqr(struct crypto_ec * e,const struct crypto_bignum * x)2484 crypto_ec_point_compute_y_sqr(struct crypto_ec *e,
2485 			      const struct crypto_bignum *x)
2486 {
2487 	BIGNUM *tmp;
2488 
2489 	if (TEST_FAIL())
2490 		return NULL;
2491 
2492 	tmp = BN_new();
2493 
2494 	/* y^2 = x^3 + ax + b = (x^2 + a)x + b */
2495 	if (tmp &&
2496 	    BN_mod_sqr(tmp, (const BIGNUM *) x, e->prime, e->bnctx) &&
2497 	    BN_mod_add_quick(tmp, e->a, tmp, e->prime) &&
2498 	    BN_mod_mul(tmp, tmp, (const BIGNUM *) x, e->prime, e->bnctx) &&
2499 	    BN_mod_add_quick(tmp, tmp, e->b, e->prime))
2500 		return (struct crypto_bignum *) tmp;
2501 
2502 	BN_clear_free(tmp);
2503 	return NULL;
2504 }
2505 
2506 
crypto_ec_point_is_at_infinity(struct crypto_ec * e,const struct crypto_ec_point * p)2507 int crypto_ec_point_is_at_infinity(struct crypto_ec *e,
2508 				   const struct crypto_ec_point *p)
2509 {
2510 	return EC_POINT_is_at_infinity(e->group, (const EC_POINT *) p);
2511 }
2512 
2513 
crypto_ec_point_is_on_curve(struct crypto_ec * e,const struct crypto_ec_point * p)2514 int crypto_ec_point_is_on_curve(struct crypto_ec *e,
2515 				const struct crypto_ec_point *p)
2516 {
2517 	return EC_POINT_is_on_curve(e->group, (const EC_POINT *) p,
2518 				    e->bnctx) == 1;
2519 }
2520 
2521 
crypto_ec_point_cmp(const struct crypto_ec * e,const struct crypto_ec_point * a,const struct crypto_ec_point * b)2522 int crypto_ec_point_cmp(const struct crypto_ec *e,
2523 			const struct crypto_ec_point *a,
2524 			const struct crypto_ec_point *b)
2525 {
2526 	return EC_POINT_cmp(e->group, (const EC_POINT *) a,
2527 			    (const EC_POINT *) b, e->bnctx);
2528 }
2529 
2530 
crypto_ec_point_debug_print(const struct crypto_ec * e,const struct crypto_ec_point * p,const char * title)2531 void crypto_ec_point_debug_print(const struct crypto_ec *e,
2532 				 const struct crypto_ec_point *p,
2533 				 const char *title)
2534 {
2535 	BIGNUM *x, *y;
2536 	char *x_str = NULL, *y_str = NULL;
2537 
2538 	x = BN_new();
2539 	y = BN_new();
2540 	if (!x || !y ||
2541 	    EC_POINT_get_affine_coordinates(e->group, (const EC_POINT *) p,
2542 					    x, y, e->bnctx) != 1)
2543 		goto fail;
2544 
2545 	x_str = BN_bn2hex(x);
2546 	y_str = BN_bn2hex(y);
2547 	if (!x_str || !y_str)
2548 		goto fail;
2549 
2550 	wpa_printf(MSG_DEBUG, "%s (%s,%s)", title, x_str, y_str);
2551 
2552 fail:
2553 	OPENSSL_free(x_str);
2554 	OPENSSL_free(y_str);
2555 	BN_free(x);
2556 	BN_free(y);
2557 }
2558 
2559 
2560 struct crypto_ecdh {
2561 	struct crypto_ec *ec;
2562 	EVP_PKEY *pkey;
2563 };
2564 
crypto_ecdh_init(int group)2565 struct crypto_ecdh * crypto_ecdh_init(int group)
2566 {
2567 #if OPENSSL_VERSION_NUMBER >= 0x30000000L
2568 	struct crypto_ecdh *ecdh;
2569 	const char *name;
2570 
2571 	ecdh = os_zalloc(sizeof(*ecdh));
2572 	if (!ecdh)
2573 		goto fail;
2574 
2575 	ecdh->ec = crypto_ec_init(group);
2576 	if (!ecdh->ec)
2577 		goto fail;
2578 
2579 	name = OSSL_EC_curve_nid2name(ecdh->ec->nid);
2580 	if (!name)
2581 		goto fail;
2582 
2583 	ecdh->pkey = EVP_EC_gen(name);
2584 	if (!ecdh->pkey)
2585 		goto fail;
2586 
2587 done:
2588 	return ecdh;
2589 fail:
2590 	crypto_ecdh_deinit(ecdh);
2591 	ecdh = NULL;
2592 	goto done;
2593 #else /* OpenSSL version >= 3.0 */
2594 	struct crypto_ecdh *ecdh;
2595 	EVP_PKEY *params = NULL;
2596 	EC_KEY *ec_params = NULL;
2597 	EVP_PKEY_CTX *kctx = NULL;
2598 
2599 	ecdh = os_zalloc(sizeof(*ecdh));
2600 	if (!ecdh)
2601 		goto fail;
2602 
2603 	ecdh->ec = crypto_ec_init(group);
2604 	if (!ecdh->ec)
2605 		goto fail;
2606 
2607 	ec_params = EC_KEY_new_by_curve_name(ecdh->ec->nid);
2608 	if (!ec_params) {
2609 		wpa_printf(MSG_ERROR,
2610 			   "OpenSSL: Failed to generate EC_KEY parameters");
2611 		goto fail;
2612 	}
2613 	EC_KEY_set_asn1_flag(ec_params, OPENSSL_EC_NAMED_CURVE);
2614 	params = EVP_PKEY_new();
2615 	if (!params || EVP_PKEY_set1_EC_KEY(params, ec_params) != 1) {
2616 		wpa_printf(MSG_ERROR,
2617 			   "OpenSSL: Failed to generate EVP_PKEY parameters");
2618 		goto fail;
2619 	}
2620 
2621 	kctx = EVP_PKEY_CTX_new(params, NULL);
2622 	if (!kctx)
2623 		goto fail;
2624 
2625 	if (EVP_PKEY_keygen_init(kctx) != 1) {
2626 		wpa_printf(MSG_ERROR,
2627 			   "OpenSSL: EVP_PKEY_keygen_init failed: %s",
2628 			   ERR_error_string(ERR_get_error(), NULL));
2629 		goto fail;
2630 	}
2631 
2632 	if (EVP_PKEY_keygen(kctx, &ecdh->pkey) != 1) {
2633 		wpa_printf(MSG_ERROR, "OpenSSL: EVP_PKEY_keygen failed: %s",
2634 			   ERR_error_string(ERR_get_error(), NULL));
2635 		goto fail;
2636 	}
2637 
2638 done:
2639 	EC_KEY_free(ec_params);
2640 	EVP_PKEY_free(params);
2641 	EVP_PKEY_CTX_free(kctx);
2642 
2643 	return ecdh;
2644 fail:
2645 	crypto_ecdh_deinit(ecdh);
2646 	ecdh = NULL;
2647 	goto done;
2648 #endif /* OpenSSL version >= 3.0 */
2649 }
2650 
2651 
crypto_ecdh_init2(int group,struct crypto_ec_key * own_key)2652 struct crypto_ecdh * crypto_ecdh_init2(int group, struct crypto_ec_key *own_key)
2653 {
2654 #if OPENSSL_VERSION_NUMBER >= 0x30000000L
2655 	struct crypto_ecdh *ecdh;
2656 
2657 	ecdh = os_zalloc(sizeof(*ecdh));
2658 	if (!ecdh)
2659 		goto fail;
2660 
2661 	ecdh->ec = crypto_ec_init(group);
2662 	if (!ecdh->ec)
2663 		goto fail;
2664 
2665 	ecdh->pkey = EVP_PKEY_dup((EVP_PKEY *) own_key);
2666 	if (!ecdh->pkey)
2667 		goto fail;
2668 
2669 	return ecdh;
2670 fail:
2671 	crypto_ecdh_deinit(ecdh);
2672 	return NULL;
2673 #else /* OpenSSL version >= 3.0 */
2674 	struct crypto_ecdh *ecdh;
2675 
2676 	ecdh = os_zalloc(sizeof(*ecdh));
2677 	if (!ecdh)
2678 		goto fail;
2679 
2680 	ecdh->ec = crypto_ec_init(group);
2681 	if (!ecdh->ec)
2682 		goto fail;
2683 
2684 	ecdh->pkey = EVP_PKEY_new();
2685 	if (!ecdh->pkey ||
2686 	    EVP_PKEY_assign_EC_KEY(ecdh->pkey,
2687 				   EVP_PKEY_get1_EC_KEY((EVP_PKEY *) own_key))
2688 	    != 1)
2689 		goto fail;
2690 
2691 	return ecdh;
2692 fail:
2693 	crypto_ecdh_deinit(ecdh);
2694 	return NULL;
2695 #endif /* OpenSSL version >= 3.0 */
2696 }
2697 
2698 
crypto_ecdh_get_pubkey(struct crypto_ecdh * ecdh,int inc_y)2699 struct wpabuf * crypto_ecdh_get_pubkey(struct crypto_ecdh *ecdh, int inc_y)
2700 {
2701 #if OPENSSL_VERSION_NUMBER >= 0x30000000L
2702 	struct wpabuf *buf = NULL;
2703 	unsigned char *pub;
2704 	size_t len, exp_len;
2705 
2706 	len = EVP_PKEY_get1_encoded_public_key(ecdh->pkey, &pub);
2707 	if (len == 0)
2708 		return NULL;
2709 
2710 	/* Encoded using SECG SEC 1, Sec. 2.3.4 format */
2711 	exp_len = 1 + 2 * crypto_ec_prime_len(ecdh->ec);
2712 	if (len != exp_len) {
2713 		wpa_printf(MSG_ERROR,
2714 			   "OpenSSL:%s: Unexpected encoded public key length %zu (expected %zu)",
2715 			   __func__, len, exp_len);
2716 		goto fail;
2717 	}
2718 	buf = wpabuf_alloc_copy(pub + 1, inc_y ? len - 1 : len / 2);
2719 fail:
2720 	OPENSSL_free(pub);
2721 	return buf;
2722 #else /* OpenSSL version >= 3.0 */
2723 	struct wpabuf *buf = NULL;
2724 	EC_KEY *eckey;
2725 	const EC_POINT *pubkey;
2726 	BIGNUM *x, *y = NULL;
2727 	int len = BN_num_bytes(ecdh->ec->prime);
2728 	int res;
2729 
2730 	eckey = EVP_PKEY_get1_EC_KEY(ecdh->pkey);
2731 	if (!eckey)
2732 		return NULL;
2733 
2734 	pubkey = EC_KEY_get0_public_key(eckey);
2735 	if (!pubkey)
2736 		return NULL;
2737 
2738 	x = BN_new();
2739 	if (inc_y) {
2740 		y = BN_new();
2741 		if (!y)
2742 			goto fail;
2743 	}
2744 	buf = wpabuf_alloc(inc_y ? 2 * len : len);
2745 	if (!x || !buf)
2746 		goto fail;
2747 
2748 	if (EC_POINT_get_affine_coordinates(ecdh->ec->group, pubkey,
2749 					    x, y, ecdh->ec->bnctx) != 1) {
2750 		wpa_printf(MSG_ERROR,
2751 			   "OpenSSL: EC_POINT_get_affine_coordinates failed: %s",
2752 			   ERR_error_string(ERR_get_error(), NULL));
2753 		goto fail;
2754 	}
2755 
2756 	res = crypto_bignum_to_bin((struct crypto_bignum *) x,
2757 				   wpabuf_put(buf, len), len, len);
2758 	if (res < 0)
2759 		goto fail;
2760 
2761 	if (inc_y) {
2762 		res = crypto_bignum_to_bin((struct crypto_bignum *) y,
2763 					   wpabuf_put(buf, len), len, len);
2764 		if (res < 0)
2765 			goto fail;
2766 	}
2767 
2768 done:
2769 	BN_clear_free(x);
2770 	BN_clear_free(y);
2771 	EC_KEY_free(eckey);
2772 
2773 	return buf;
2774 fail:
2775 	wpabuf_free(buf);
2776 	buf = NULL;
2777 	goto done;
2778 #endif /* OpenSSL version >= 3.0 */
2779 }
2780 
2781 
crypto_ecdh_set_peerkey(struct crypto_ecdh * ecdh,int inc_y,const u8 * key,size_t len)2782 struct wpabuf * crypto_ecdh_set_peerkey(struct crypto_ecdh *ecdh, int inc_y,
2783 					const u8 *key, size_t len)
2784 {
2785 #if OPENSSL_VERSION_NUMBER >= 0x30000000L
2786 	EVP_PKEY *peerkey = EVP_PKEY_new();
2787 	EVP_PKEY_CTX *ctx;
2788 	size_t res_len;
2789 	struct wpabuf *res = NULL;
2790 	u8 *peer;
2791 
2792 	/* Encode using SECG SEC 1, Sec. 2.3.4 format */
2793 	peer = os_malloc(1 + len);
2794 	if (!peer)
2795 		return NULL;
2796 	peer[0] = inc_y ? 0x04 : 0x02;
2797 	os_memcpy(peer + 1, key, len);
2798 
2799 	if (!peerkey ||
2800 	    EVP_PKEY_copy_parameters(peerkey, ecdh->pkey) != 1 ||
2801 	    EVP_PKEY_set1_encoded_public_key(peerkey, peer, 1 + len) != 1) {
2802 		wpa_printf(MSG_INFO, "OpenSSL: EVP_PKEY_set1_encoded_public_key failed: %s",
2803 			   ERR_error_string(ERR_get_error(), NULL));
2804 		EVP_PKEY_free(peerkey);
2805 		os_free(peer);
2806 		return NULL;
2807 	}
2808 	os_free(peer);
2809 
2810 	ctx = EVP_PKEY_CTX_new(ecdh->pkey, NULL);
2811 	if (!ctx ||
2812 	    EVP_PKEY_derive_init(ctx) != 1 ||
2813 	    EVP_PKEY_derive_set_peer(ctx, peerkey) != 1 ||
2814 	    EVP_PKEY_derive(ctx, NULL, &res_len) != 1 ||
2815 	    !(res = wpabuf_alloc(res_len)) ||
2816 	    EVP_PKEY_derive(ctx, wpabuf_mhead(res), &res_len) != 1) {
2817 		wpa_printf(MSG_INFO, "OpenSSL: EVP_PKEY_derive failed: %s",
2818 			   ERR_error_string(ERR_get_error(), NULL));
2819 		wpabuf_free(res);
2820 		res = NULL;
2821 	} else {
2822 		wpabuf_put(res, res_len);
2823 	}
2824 
2825 	EVP_PKEY_free(peerkey);
2826 	EVP_PKEY_CTX_free(ctx);
2827 	return res;
2828 #else /* OpenSSL version >= 3.0 */
2829 	BIGNUM *x, *y = NULL;
2830 	EVP_PKEY_CTX *ctx = NULL;
2831 	EVP_PKEY *peerkey = NULL;
2832 	struct wpabuf *secret = NULL;
2833 	size_t secret_len;
2834 	EC_POINT *pub;
2835 	EC_KEY *eckey = NULL;
2836 
2837 	x = BN_bin2bn(key, inc_y ? len / 2 : len, NULL);
2838 	pub = EC_POINT_new(ecdh->ec->group);
2839 	if (!x || !pub)
2840 		goto fail;
2841 
2842 	if (inc_y) {
2843 		y = BN_bin2bn(key + len / 2, len / 2, NULL);
2844 		if (!y)
2845 			goto fail;
2846 		if (!EC_POINT_set_affine_coordinates(ecdh->ec->group, pub,
2847 						     x, y, ecdh->ec->bnctx)) {
2848 			wpa_printf(MSG_ERROR,
2849 				   "OpenSSL: EC_POINT_set_affine_coordinates failed: %s",
2850 				   ERR_error_string(ERR_get_error(), NULL));
2851 			goto fail;
2852 		}
2853 	} else if (!EC_POINT_set_compressed_coordinates(ecdh->ec->group,
2854 							pub, x, 0,
2855 							ecdh->ec->bnctx)) {
2856 		wpa_printf(MSG_ERROR,
2857 			   "OpenSSL: EC_POINT_set_compressed_coordinates failed: %s",
2858 			   ERR_error_string(ERR_get_error(), NULL));
2859 		goto fail;
2860 	}
2861 
2862 	if (!EC_POINT_is_on_curve(ecdh->ec->group, pub, ecdh->ec->bnctx)) {
2863 		wpa_printf(MSG_ERROR,
2864 			   "OpenSSL: ECDH peer public key is not on curve");
2865 		goto fail;
2866 	}
2867 
2868 	eckey = EC_KEY_new_by_curve_name(ecdh->ec->nid);
2869 	if (!eckey || EC_KEY_set_public_key(eckey, pub) != 1) {
2870 		wpa_printf(MSG_ERROR,
2871 			   "OpenSSL: EC_KEY_set_public_key failed: %s",
2872 			   ERR_error_string(ERR_get_error(), NULL));
2873 		goto fail;
2874 	}
2875 
2876 	peerkey = EVP_PKEY_new();
2877 	if (!peerkey || EVP_PKEY_set1_EC_KEY(peerkey, eckey) != 1)
2878 		goto fail;
2879 
2880 	ctx = EVP_PKEY_CTX_new(ecdh->pkey, NULL);
2881 	if (!ctx || EVP_PKEY_derive_init(ctx) != 1 ||
2882 	    EVP_PKEY_derive_set_peer(ctx, peerkey) != 1 ||
2883 	    EVP_PKEY_derive(ctx, NULL, &secret_len) != 1) {
2884 		wpa_printf(MSG_ERROR,
2885 			   "OpenSSL: EVP_PKEY_derive(1) failed: %s",
2886 			   ERR_error_string(ERR_get_error(), NULL));
2887 		goto fail;
2888 	}
2889 
2890 	secret = wpabuf_alloc(secret_len);
2891 	if (!secret)
2892 		goto fail;
2893 	if (EVP_PKEY_derive(ctx, wpabuf_put(secret, 0), &secret_len) != 1) {
2894 		wpa_printf(MSG_ERROR,
2895 			   "OpenSSL: EVP_PKEY_derive(2) failed: %s",
2896 			   ERR_error_string(ERR_get_error(), NULL));
2897 		goto fail;
2898 	}
2899 	if (secret->size != secret_len)
2900 		wpa_printf(MSG_DEBUG,
2901 			   "OpenSSL: EVP_PKEY_derive(2) changed secret_len %d -> %d",
2902 			   (int) secret->size, (int) secret_len);
2903 	wpabuf_put(secret, secret_len);
2904 
2905 done:
2906 	BN_free(x);
2907 	BN_free(y);
2908 	EC_KEY_free(eckey);
2909 	EC_POINT_free(pub);
2910 	EVP_PKEY_CTX_free(ctx);
2911 	EVP_PKEY_free(peerkey);
2912 	return secret;
2913 fail:
2914 	wpabuf_free(secret);
2915 	secret = NULL;
2916 	goto done;
2917 #endif /* OpenSSL version >= 3.0 */
2918 }
2919 
2920 
crypto_ecdh_deinit(struct crypto_ecdh * ecdh)2921 void crypto_ecdh_deinit(struct crypto_ecdh *ecdh)
2922 {
2923 	if (ecdh) {
2924 		crypto_ec_deinit(ecdh->ec);
2925 		EVP_PKEY_free(ecdh->pkey);
2926 		os_free(ecdh);
2927 	}
2928 }
2929 
2930 
crypto_ecdh_prime_len(struct crypto_ecdh * ecdh)2931 size_t crypto_ecdh_prime_len(struct crypto_ecdh *ecdh)
2932 {
2933 	return crypto_ec_prime_len(ecdh->ec);
2934 }
2935 
2936 
crypto_ec_key_parse_priv(const u8 * der,size_t der_len)2937 struct crypto_ec_key * crypto_ec_key_parse_priv(const u8 *der, size_t der_len)
2938 {
2939 	EVP_PKEY *pkey = NULL;
2940 	EC_KEY *eckey;
2941 
2942 	eckey = d2i_ECPrivateKey(NULL, &der, der_len);
2943 	if (!eckey) {
2944 		wpa_printf(MSG_INFO, "OpenSSL: d2i_ECPrivateKey() failed: %s",
2945 			   ERR_error_string(ERR_get_error(), NULL));
2946 		goto fail;
2947 	}
2948 	EC_KEY_set_conv_form(eckey, POINT_CONVERSION_COMPRESSED);
2949 
2950 	pkey = EVP_PKEY_new();
2951 	if (!pkey || EVP_PKEY_assign_EC_KEY(pkey, eckey) != 1) {
2952 		EC_KEY_free(eckey);
2953 		goto fail;
2954 	}
2955 
2956 	return (struct crypto_ec_key *) pkey;
2957 fail:
2958 	crypto_ec_key_deinit((struct crypto_ec_key *) pkey);
2959 	return NULL;
2960 }
2961 
2962 
crypto_ec_key_parse_pub(const u8 * der,size_t der_len)2963 struct crypto_ec_key * crypto_ec_key_parse_pub(const u8 *der, size_t der_len)
2964 {
2965 	EVP_PKEY *pkey;
2966 
2967 	pkey = d2i_PUBKEY(NULL, &der, der_len);
2968 	if (!pkey) {
2969 		wpa_printf(MSG_INFO, "OpenSSL: d2i_PUBKEY() failed: %s",
2970 			   ERR_error_string(ERR_get_error(), NULL));
2971 		goto fail;
2972 	}
2973 
2974 	/* Ensure this is an EC key */
2975 	if (!EVP_PKEY_get0_EC_KEY(pkey))
2976 		goto fail;
2977 	return (struct crypto_ec_key *) pkey;
2978 fail:
2979 	crypto_ec_key_deinit((struct crypto_ec_key *) pkey);
2980 	return NULL;
2981 }
2982 
2983 
crypto_ec_key_set_pub(int group,const u8 * buf_x,const u8 * buf_y,size_t len)2984 struct crypto_ec_key * crypto_ec_key_set_pub(int group, const u8 *buf_x,
2985 					     const u8 *buf_y, size_t len)
2986 {
2987 	EC_KEY *eckey = NULL;
2988 	EVP_PKEY *pkey = NULL;
2989 	EC_GROUP *ec_group = NULL;
2990 	BN_CTX *ctx;
2991 	EC_POINT *point = NULL;
2992 	BIGNUM *x = NULL, *y = NULL;
2993 	int nid;
2994 
2995 	if (!buf_x || !buf_y)
2996 		return NULL;
2997 
2998 	nid = crypto_ec_group_2_nid(group);
2999 	if (nid < 0) {
3000 		wpa_printf(MSG_ERROR, "OpenSSL: Unsupported group %d", group);
3001 		return NULL;
3002 	}
3003 
3004 	ctx = BN_CTX_new();
3005 	if (!ctx)
3006 		goto fail;
3007 
3008 	ec_group = EC_GROUP_new_by_curve_name(nid);
3009 	if (!ec_group)
3010 		goto fail;
3011 
3012 	x = BN_bin2bn(buf_x, len, NULL);
3013 	y = BN_bin2bn(buf_y, len, NULL);
3014 	point = EC_POINT_new(ec_group);
3015 	if (!x || !y || !point)
3016 		goto fail;
3017 
3018 	if (!EC_POINT_set_affine_coordinates(ec_group, point, x, y, ctx)) {
3019 		wpa_printf(MSG_ERROR,
3020 			   "OpenSSL: EC_POINT_set_affine_coordinates failed: %s",
3021 			   ERR_error_string(ERR_get_error(), NULL));
3022 		goto fail;
3023 	}
3024 
3025 	if (!EC_POINT_is_on_curve(ec_group, point, ctx) ||
3026 	    EC_POINT_is_at_infinity(ec_group, point)) {
3027 		wpa_printf(MSG_ERROR, "OpenSSL: Invalid point");
3028 		goto fail;
3029 	}
3030 
3031 	eckey = EC_KEY_new();
3032 	if (!eckey ||
3033 	    EC_KEY_set_group(eckey, ec_group) != 1 ||
3034 	    EC_KEY_set_public_key(eckey, point) != 1) {
3035 		wpa_printf(MSG_ERROR,
3036 			   "OpenSSL: Failed to set EC_KEY: %s",
3037 			   ERR_error_string(ERR_get_error(), NULL));
3038 		goto fail;
3039 	}
3040 	EC_KEY_set_asn1_flag(eckey, OPENSSL_EC_NAMED_CURVE);
3041 
3042 	pkey = EVP_PKEY_new();
3043 	if (!pkey || EVP_PKEY_assign_EC_KEY(pkey, eckey) != 1) {
3044 		wpa_printf(MSG_ERROR, "OpenSSL: Could not create EVP_PKEY");
3045 		goto fail;
3046 	}
3047 
3048 out:
3049 	EC_GROUP_free(ec_group);
3050 	BN_free(x);
3051 	BN_free(y);
3052 	EC_POINT_free(point);
3053 	BN_CTX_free(ctx);
3054 	return (struct crypto_ec_key *) pkey;
3055 
3056 fail:
3057 	EC_KEY_free(eckey);
3058 	EVP_PKEY_free(pkey);
3059 	pkey = NULL;
3060 	goto out;
3061 }
3062 
3063 
3064 struct crypto_ec_key *
crypto_ec_key_set_pub_point(struct crypto_ec * ec,const struct crypto_ec_point * pub)3065 crypto_ec_key_set_pub_point(struct crypto_ec *ec,
3066 			    const struct crypto_ec_point *pub)
3067 {
3068 	EC_KEY *eckey;
3069 	EVP_PKEY *pkey = NULL;
3070 
3071 	eckey = EC_KEY_new();
3072 	if (!eckey ||
3073 	    EC_KEY_set_group(eckey, ec->group) != 1 ||
3074 	    EC_KEY_set_public_key(eckey, (const EC_POINT *) pub) != 1) {
3075 		wpa_printf(MSG_ERROR,
3076 			   "OpenSSL: Failed to set EC_KEY: %s",
3077 			   ERR_error_string(ERR_get_error(), NULL));
3078 		goto fail;
3079 	}
3080 	EC_KEY_set_asn1_flag(eckey, OPENSSL_EC_NAMED_CURVE);
3081 
3082 	pkey = EVP_PKEY_new();
3083 	if (!pkey || EVP_PKEY_assign_EC_KEY(pkey, eckey) != 1) {
3084 		wpa_printf(MSG_ERROR, "OpenSSL: Could not create EVP_PKEY");
3085 		goto fail;
3086 	}
3087 
3088 out:
3089 	return (struct crypto_ec_key *) pkey;
3090 
3091 fail:
3092 	EVP_PKEY_free(pkey);
3093 	EC_KEY_free(eckey);
3094 	pkey = NULL;
3095 	goto out;
3096 }
3097 
3098 
crypto_ec_key_gen(int group)3099 struct crypto_ec_key * crypto_ec_key_gen(int group)
3100 {
3101 	EVP_PKEY_CTX *kctx = NULL;
3102 	EC_KEY *ec_params = NULL, *eckey;
3103 	EVP_PKEY *params = NULL, *key = NULL;
3104 	int nid;
3105 
3106 	nid = crypto_ec_group_2_nid(group);
3107 	if (nid < 0) {
3108 		wpa_printf(MSG_ERROR, "OpenSSL: Unsupported group %d", group);
3109 		return NULL;
3110 	}
3111 
3112 	ec_params = EC_KEY_new_by_curve_name(nid);
3113 	if (!ec_params) {
3114 		wpa_printf(MSG_ERROR,
3115 			   "OpenSSL: Failed to generate EC_KEY parameters");
3116 		goto fail;
3117 	}
3118 	EC_KEY_set_asn1_flag(ec_params, OPENSSL_EC_NAMED_CURVE);
3119 	params = EVP_PKEY_new();
3120 	if (!params || EVP_PKEY_set1_EC_KEY(params, ec_params) != 1) {
3121 		wpa_printf(MSG_ERROR,
3122 			   "OpenSSL: Failed to generate EVP_PKEY parameters");
3123 		goto fail;
3124 	}
3125 
3126 	kctx = EVP_PKEY_CTX_new(params, NULL);
3127 	if (!kctx ||
3128 	    EVP_PKEY_keygen_init(kctx) != 1 ||
3129 	    EVP_PKEY_keygen(kctx, &key) != 1) {
3130 		wpa_printf(MSG_ERROR, "OpenSSL: Failed to generate EC key");
3131 		key = NULL;
3132 		goto fail;
3133 	}
3134 
3135 	eckey = EVP_PKEY_get1_EC_KEY(key);
3136 	if (!eckey) {
3137 		key = NULL;
3138 		goto fail;
3139 	}
3140 	EC_KEY_set_conv_form(eckey, POINT_CONVERSION_COMPRESSED);
3141 	EC_KEY_free(eckey);
3142 
3143 fail:
3144 	EC_KEY_free(ec_params);
3145 	EVP_PKEY_free(params);
3146 	EVP_PKEY_CTX_free(kctx);
3147 	return (struct crypto_ec_key *) key;
3148 }
3149 
3150 
crypto_ec_key_deinit(struct crypto_ec_key * key)3151 void crypto_ec_key_deinit(struct crypto_ec_key *key)
3152 {
3153 	EVP_PKEY_free((EVP_PKEY *) key);
3154 }
3155 
3156 
3157 #ifdef OPENSSL_IS_BORINGSSL
3158 
3159 /* BoringSSL version of i2d_PUBKEY() always outputs public EC key using
3160  * uncompressed form so define a custom function to export EC pubkey using
3161  * the compressed format that is explicitly required for some protocols. */
3162 
3163 #include <openssl/asn1.h>
3164 #include <openssl/asn1t.h>
3165 
3166 typedef struct {
3167 	/* AlgorithmIdentifier ecPublicKey with optional parameters present
3168 	 * as an OID identifying the curve */
3169 	X509_ALGOR *alg;
3170 	/* Compressed format public key per ANSI X9.63 */
3171 	ASN1_BIT_STRING *pub_key;
3172 } EC_COMP_PUBKEY;
3173 
3174 ASN1_SEQUENCE(EC_COMP_PUBKEY) = {
3175 	ASN1_SIMPLE(EC_COMP_PUBKEY, alg, X509_ALGOR),
3176 	ASN1_SIMPLE(EC_COMP_PUBKEY, pub_key, ASN1_BIT_STRING)
3177 } ASN1_SEQUENCE_END(EC_COMP_PUBKEY);
3178 
3179 IMPLEMENT_ASN1_FUNCTIONS(EC_COMP_PUBKEY);
3180 
3181 #endif /* OPENSSL_IS_BORINGSSL */
3182 
3183 
crypto_ec_key_get_subject_public_key(struct crypto_ec_key * key)3184 struct wpabuf * crypto_ec_key_get_subject_public_key(struct crypto_ec_key *key)
3185 {
3186 #ifdef OPENSSL_IS_BORINGSSL
3187 	unsigned char *der = NULL;
3188 	int der_len;
3189 	const EC_KEY *eckey;
3190 	struct wpabuf *ret = NULL;
3191 	size_t len;
3192 	const EC_GROUP *group;
3193 	const EC_POINT *point;
3194 	BN_CTX *ctx;
3195 	EC_COMP_PUBKEY *pubkey = NULL;
3196 	int nid;
3197 
3198 	ctx = BN_CTX_new();
3199 	eckey = EVP_PKEY_get0_EC_KEY((EVP_PKEY *) key);
3200 	if (!ctx || !eckey)
3201 		goto fail;
3202 
3203 	group = EC_KEY_get0_group(eckey);
3204 	point = EC_KEY_get0_public_key(eckey);
3205 	if (!group || !point)
3206 		goto fail;
3207 	nid = EC_GROUP_get_curve_name(group);
3208 
3209 	pubkey = EC_COMP_PUBKEY_new();
3210 	if (!pubkey ||
3211 	    X509_ALGOR_set0(pubkey->alg, OBJ_nid2obj(EVP_PKEY_EC),
3212 			    V_ASN1_OBJECT, (void *) OBJ_nid2obj(nid)) != 1)
3213 		goto fail;
3214 
3215 	len = EC_POINT_point2oct(group, point, POINT_CONVERSION_COMPRESSED,
3216 				 NULL, 0, ctx);
3217 	if (len == 0)
3218 		goto fail;
3219 
3220 	der = OPENSSL_malloc(len);
3221 	if (!der)
3222 		goto fail;
3223 	len = EC_POINT_point2oct(group, point, POINT_CONVERSION_COMPRESSED,
3224 				 der, len, ctx);
3225 
3226 	OPENSSL_free(pubkey->pub_key->data);
3227 	pubkey->pub_key->data = der;
3228 	der = NULL;
3229 	pubkey->pub_key->length = len;
3230 	/* No unused bits */
3231 	pubkey->pub_key->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
3232 	pubkey->pub_key->flags |= ASN1_STRING_FLAG_BITS_LEFT;
3233 
3234 	der_len = i2d_EC_COMP_PUBKEY(pubkey, &der);
3235 	if (der_len <= 0) {
3236 		wpa_printf(MSG_ERROR,
3237 			   "BoringSSL: Failed to build DER encoded public key");
3238 		goto fail;
3239 	}
3240 
3241 	ret = wpabuf_alloc_copy(der, der_len);
3242 fail:
3243 	EC_COMP_PUBKEY_free(pubkey);
3244 	OPENSSL_free(der);
3245 	BN_CTX_free(ctx);
3246 	return ret;
3247 #else /* OPENSSL_IS_BORINGSSL */
3248 	unsigned char *der = NULL;
3249 	int der_len;
3250 	struct wpabuf *buf;
3251 	EC_KEY *eckey;
3252 #if OPENSSL_VERSION_NUMBER >= 0x30000000L
3253 	EVP_PKEY *tmp;
3254 #endif /* OpenSSL version >= 3.0 */
3255 
3256 	eckey = EVP_PKEY_get1_EC_KEY((EVP_PKEY *) key);
3257 	if (!eckey)
3258 		return NULL;
3259 
3260 	/* For now, all users expect COMPRESSED form */
3261 	EC_KEY_set_conv_form(eckey, POINT_CONVERSION_COMPRESSED);
3262 
3263 #if OPENSSL_VERSION_NUMBER >= 0x30000000L
3264 	tmp = EVP_PKEY_new();
3265 	if (!tmp)
3266 		return NULL;
3267 	if (EVP_PKEY_set1_EC_KEY(tmp, eckey) != 1) {
3268 		EVP_PKEY_free(tmp);
3269 		return NULL;
3270 	}
3271 	key = (struct crypto_ec_key *) tmp;
3272 #endif /* OpenSSL version >= 3.0 */
3273 
3274 	der_len = i2d_PUBKEY((EVP_PKEY *) key, &der);
3275 	EC_KEY_free(eckey);
3276 #if OPENSSL_VERSION_NUMBER >= 0x30000000L
3277 	EVP_PKEY_free(tmp);
3278 #endif /* OpenSSL version >= 3.0 */
3279 	if (der_len <= 0) {
3280 		wpa_printf(MSG_INFO, "OpenSSL: i2d_PUBKEY() failed: %s",
3281 			   ERR_error_string(ERR_get_error(), NULL));
3282 		return NULL;
3283 	}
3284 
3285 	buf = wpabuf_alloc_copy(der, der_len);
3286 	OPENSSL_free(der);
3287 	return buf;
3288 #endif /* OPENSSL_IS_BORINGSSL */
3289 }
3290 
3291 
crypto_ec_key_get_ecprivate_key(struct crypto_ec_key * key,bool include_pub)3292 struct wpabuf * crypto_ec_key_get_ecprivate_key(struct crypto_ec_key *key,
3293 						bool include_pub)
3294 {
3295 	EC_KEY *eckey;
3296 	unsigned char *der = NULL;
3297 	int der_len;
3298 	struct wpabuf *buf;
3299 	unsigned int key_flags;
3300 
3301 	eckey = EVP_PKEY_get1_EC_KEY((EVP_PKEY *) key);
3302 	if (!eckey)
3303 		return NULL;
3304 
3305 	key_flags = EC_KEY_get_enc_flags(eckey);
3306 	if (include_pub)
3307 		key_flags &= ~EC_PKEY_NO_PUBKEY;
3308 	else
3309 		key_flags |= EC_PKEY_NO_PUBKEY;
3310 	EC_KEY_set_enc_flags(eckey, key_flags);
3311 
3312 	EC_KEY_set_conv_form(eckey, POINT_CONVERSION_UNCOMPRESSED);
3313 
3314 	der_len = i2d_ECPrivateKey(eckey, &der);
3315 	EC_KEY_free(eckey);
3316 	if (der_len <= 0)
3317 		return NULL;
3318 	buf = wpabuf_alloc_copy(der, der_len);
3319 	OPENSSL_free(der);
3320 
3321 	return buf;
3322 }
3323 
3324 
crypto_ec_key_get_pubkey_point(struct crypto_ec_key * key,int prefix)3325 struct wpabuf * crypto_ec_key_get_pubkey_point(struct crypto_ec_key *key,
3326 					       int prefix)
3327 {
3328 	int len, res;
3329 	EC_KEY *eckey;
3330 	struct wpabuf *buf;
3331 	unsigned char *pos;
3332 
3333 	eckey = EVP_PKEY_get1_EC_KEY((EVP_PKEY *) key);
3334 	if (!eckey)
3335 		return NULL;
3336 	EC_KEY_set_conv_form(eckey, POINT_CONVERSION_UNCOMPRESSED);
3337 	len = i2o_ECPublicKey(eckey, NULL);
3338 	if (len <= 0) {
3339 		wpa_printf(MSG_ERROR,
3340 			   "OpenSSL: Failed to determine public key encoding length");
3341 		EC_KEY_free(eckey);
3342 		return NULL;
3343 	}
3344 
3345 	buf = wpabuf_alloc(len);
3346 	if (!buf) {
3347 		EC_KEY_free(eckey);
3348 		return NULL;
3349 	}
3350 
3351 	pos = wpabuf_put(buf, len);
3352 	res = i2o_ECPublicKey(eckey, &pos);
3353 	EC_KEY_free(eckey);
3354 	if (res != len) {
3355 		wpa_printf(MSG_ERROR,
3356 			   "OpenSSL: Failed to encode public key (res=%d/%d)",
3357 			   res, len);
3358 		wpabuf_free(buf);
3359 		return NULL;
3360 	}
3361 
3362 	if (!prefix) {
3363 		/* Remove 0x04 prefix if requested */
3364 		pos = wpabuf_mhead(buf);
3365 		os_memmove(pos, pos + 1, len - 1);
3366 		buf->used--;
3367 	}
3368 
3369 	return buf;
3370 }
3371 
3372 
3373 const struct crypto_ec_point *
crypto_ec_key_get_public_key(struct crypto_ec_key * key)3374 crypto_ec_key_get_public_key(struct crypto_ec_key *key)
3375 {
3376 	const EC_KEY *eckey;
3377 
3378 	eckey = EVP_PKEY_get0_EC_KEY((EVP_PKEY *) key);
3379 	if (!eckey)
3380 		return NULL;
3381 	return (const struct crypto_ec_point *) EC_KEY_get0_public_key(eckey);
3382 }
3383 
3384 
3385 const struct crypto_bignum *
crypto_ec_key_get_private_key(struct crypto_ec_key * key)3386 crypto_ec_key_get_private_key(struct crypto_ec_key *key)
3387 {
3388 	const EC_KEY *eckey;
3389 
3390 	eckey = EVP_PKEY_get0_EC_KEY((EVP_PKEY *) key);
3391 	if (!eckey)
3392 		return NULL;
3393 	return (const struct crypto_bignum *) EC_KEY_get0_private_key(eckey);
3394 }
3395 
3396 
crypto_ec_key_sign(struct crypto_ec_key * key,const u8 * data,size_t len)3397 struct wpabuf * crypto_ec_key_sign(struct crypto_ec_key *key, const u8 *data,
3398 				   size_t len)
3399 {
3400 	EVP_PKEY_CTX *pkctx;
3401 	struct wpabuf *sig_der;
3402 	size_t sig_len;
3403 
3404 	sig_len = EVP_PKEY_size((EVP_PKEY *) key);
3405 	sig_der = wpabuf_alloc(sig_len);
3406 	if (!sig_der)
3407 		return NULL;
3408 
3409 	pkctx = EVP_PKEY_CTX_new((EVP_PKEY *) key, NULL);
3410 	if (!pkctx ||
3411 	    EVP_PKEY_sign_init(pkctx) <= 0 ||
3412 	    EVP_PKEY_sign(pkctx, wpabuf_put(sig_der, 0), &sig_len,
3413 			  data, len) <= 0) {
3414 		wpabuf_free(sig_der);
3415 		sig_der = NULL;
3416 	} else {
3417 		wpabuf_put(sig_der, sig_len);
3418 	}
3419 
3420 	EVP_PKEY_CTX_free(pkctx);
3421 	return sig_der;
3422 }
3423 
3424 
openssl_evp_pkey_ec_prime_len(struct crypto_ec_key * key)3425 static int openssl_evp_pkey_ec_prime_len(struct crypto_ec_key *key)
3426 {
3427 #if OPENSSL_VERSION_NUMBER >= 0x30000000L
3428 	char gname[50];
3429 	int nid;
3430 	EC_GROUP *group;
3431 	BIGNUM *prime = NULL;
3432 	int prime_len = -1;
3433 
3434 	if (EVP_PKEY_get_group_name((EVP_PKEY *) key, gname, sizeof(gname),
3435 				    NULL) != 1)
3436 		return -1;
3437 	nid = OBJ_txt2nid(gname);
3438 	group = EC_GROUP_new_by_curve_name(nid);
3439 	prime = BN_new();
3440 	if (!group || !prime)
3441 		return -1;
3442 	if (EC_GROUP_get_curve(group, prime, NULL, NULL, NULL) == 1)
3443 		prime_len = BN_num_bytes(prime);
3444 	EC_GROUP_free(group);
3445 	BN_free(prime);
3446 	return prime_len;
3447 #else
3448 	const EC_GROUP *group;
3449 	const EC_KEY *eckey;
3450 	BIGNUM *prime = NULL;
3451 	int prime_len = -1;
3452 
3453 	eckey = EVP_PKEY_get0_EC_KEY((EVP_PKEY *) key);
3454 	if (!eckey)
3455 		goto fail;
3456 	group = EC_KEY_get0_group(eckey);
3457 	prime = BN_new();
3458 	if (!prime || !group ||
3459 	    !EC_GROUP_get_curve(group, prime, NULL, NULL, NULL))
3460 		goto fail;
3461 	prime_len = BN_num_bytes(prime);
3462 fail:
3463 	BN_free(prime);
3464 	return prime_len;
3465 #endif
3466 }
3467 
3468 
crypto_ec_key_sign_r_s(struct crypto_ec_key * key,const u8 * data,size_t len)3469 struct wpabuf * crypto_ec_key_sign_r_s(struct crypto_ec_key *key,
3470 				       const u8 *data, size_t len)
3471 {
3472 	ECDSA_SIG *sig = NULL;
3473 	const BIGNUM *r, *s;
3474 	u8 *r_buf, *s_buf;
3475 	struct wpabuf *buf;
3476 	const unsigned char *p;
3477 	int prime_len;
3478 
3479 	prime_len = openssl_evp_pkey_ec_prime_len(key);
3480 	if (prime_len < 0)
3481 		return NULL;
3482 
3483 	buf = crypto_ec_key_sign(key, data, len);
3484 	if (!buf)
3485 		return NULL;
3486 
3487 	/* Extract (r,s) from Ecdsa-Sig-Value */
3488 
3489 	p = wpabuf_head(buf);
3490 	sig = d2i_ECDSA_SIG(NULL, &p, wpabuf_len(buf));
3491 	if (!sig)
3492 		goto fail;
3493 	ECDSA_SIG_get0(sig, &r, &s);
3494 
3495 	/* Re-use wpabuf returned by crypto_ec_key_sign() */
3496 	buf->used = 0;
3497 	r_buf = wpabuf_put(buf, prime_len);
3498 	s_buf = wpabuf_put(buf, prime_len);
3499 	if (crypto_bignum_to_bin((const struct crypto_bignum *) r, r_buf,
3500 				 prime_len, prime_len) < 0 ||
3501 	    crypto_bignum_to_bin((const struct crypto_bignum *) s, s_buf,
3502 				 prime_len, prime_len) < 0)
3503 		goto fail;
3504 
3505 out:
3506 	ECDSA_SIG_free(sig);
3507 	return buf;
3508 fail:
3509 	wpabuf_clear_free(buf);
3510 	buf = NULL;
3511 	goto out;
3512 }
3513 
3514 
crypto_ec_key_verify_signature(struct crypto_ec_key * key,const u8 * data,size_t len,const u8 * sig,size_t sig_len)3515 int crypto_ec_key_verify_signature(struct crypto_ec_key *key, const u8 *data,
3516 				   size_t len, const u8 *sig, size_t sig_len)
3517 {
3518 	EVP_PKEY_CTX *pkctx;
3519 	int ret;
3520 
3521 	pkctx = EVP_PKEY_CTX_new((EVP_PKEY *) key, NULL);
3522 	if (!pkctx || EVP_PKEY_verify_init(pkctx) <= 0) {
3523 		EVP_PKEY_CTX_free(pkctx);
3524 		return -1;
3525 	}
3526 
3527 	ret = EVP_PKEY_verify(pkctx, sig, sig_len, data, len);
3528 	EVP_PKEY_CTX_free(pkctx);
3529 	if (ret == 1)
3530 		return 1; /* signature ok */
3531 	if (ret == 0)
3532 		return 0; /* incorrect signature */
3533 	return -1;
3534 }
3535 
3536 
crypto_ec_key_verify_signature_r_s(struct crypto_ec_key * key,const u8 * data,size_t len,const u8 * r,size_t r_len,const u8 * s,size_t s_len)3537 int crypto_ec_key_verify_signature_r_s(struct crypto_ec_key *key,
3538 				       const u8 *data, size_t len,
3539 				       const u8 *r, size_t r_len,
3540 				       const u8 *s, size_t s_len)
3541 {
3542 	ECDSA_SIG *sig;
3543 	BIGNUM *r_bn, *s_bn;
3544 	unsigned char *der = NULL;
3545 	int der_len;
3546 	int ret = -1;
3547 
3548 	r_bn = BN_bin2bn(r, r_len, NULL);
3549 	s_bn = BN_bin2bn(s, s_len, NULL);
3550 	sig = ECDSA_SIG_new();
3551 	if (!r_bn || !s_bn || !sig || ECDSA_SIG_set0(sig, r_bn, s_bn) != 1)
3552 		goto fail;
3553 	r_bn = NULL;
3554 	s_bn = NULL;
3555 
3556 	der_len = i2d_ECDSA_SIG(sig, &der);
3557 	if (der_len <= 0) {
3558 		wpa_printf(MSG_DEBUG,
3559 			   "OpenSSL: Could not DER encode signature");
3560 		goto fail;
3561 	}
3562 
3563 	ret = crypto_ec_key_verify_signature(key, data, len, der, der_len);
3564 
3565 fail:
3566 	OPENSSL_free(der);
3567 	BN_free(r_bn);
3568 	BN_free(s_bn);
3569 	ECDSA_SIG_free(sig);
3570 	return ret;
3571 }
3572 
3573 
crypto_ec_key_group(struct crypto_ec_key * key)3574 int crypto_ec_key_group(struct crypto_ec_key *key)
3575 {
3576 #if OPENSSL_VERSION_NUMBER >= 0x30000000L
3577 	char gname[50];
3578 	int nid;
3579 
3580 	if (EVP_PKEY_get_group_name((EVP_PKEY *) key, gname, sizeof(gname),
3581 				    NULL) != 1)
3582 		return -1;
3583 	nid = OBJ_txt2nid(gname);
3584 #else
3585 	const EC_KEY *eckey;
3586 	const EC_GROUP *group;
3587 	int nid;
3588 
3589 	eckey = EVP_PKEY_get0_EC_KEY((EVP_PKEY *) key);
3590 	if (!eckey)
3591 		return -1;
3592 	group = EC_KEY_get0_group(eckey);
3593 	if (!group)
3594 		return -1;
3595 	nid = EC_GROUP_get_curve_name(group);
3596 #endif
3597 	switch (nid) {
3598 	case NID_X9_62_prime256v1:
3599 		return 19;
3600 	case NID_secp384r1:
3601 		return 20;
3602 	case NID_secp521r1:
3603 		return 21;
3604 #ifdef NID_brainpoolP256r1
3605 	case NID_brainpoolP256r1:
3606 		return 28;
3607 #endif /* NID_brainpoolP256r1 */
3608 #ifdef NID_brainpoolP384r1
3609 	case NID_brainpoolP384r1:
3610 		return 29;
3611 #endif /* NID_brainpoolP384r1 */
3612 #ifdef NID_brainpoolP512r1
3613 	case NID_brainpoolP512r1:
3614 		return 30;
3615 #endif /* NID_brainpoolP512r1 */
3616 	}
3617 	wpa_printf(MSG_ERROR, "OpenSSL: Unsupported curve (nid=%d) in EC key",
3618 		   nid);
3619 	return -1;
3620 }
3621 
3622 
crypto_ec_key_cmp(struct crypto_ec_key * key1,struct crypto_ec_key * key2)3623 int crypto_ec_key_cmp(struct crypto_ec_key *key1, struct crypto_ec_key *key2)
3624 {
3625 #if OPENSSL_VERSION_NUMBER >= 0x30000000L
3626 	if (EVP_PKEY_eq((EVP_PKEY *) key1, (EVP_PKEY *) key2) != 1)
3627 		return -1;
3628 #else
3629 	if (EVP_PKEY_cmp((EVP_PKEY *) key1, (EVP_PKEY *) key2) != 1)
3630 		return -1;
3631 #endif
3632 	return 0;
3633 }
3634 
3635 
crypto_ec_key_debug_print(const struct crypto_ec_key * key,const char * title)3636 void crypto_ec_key_debug_print(const struct crypto_ec_key *key,
3637 			       const char *title)
3638 {
3639 	BIO *out;
3640 	size_t rlen;
3641 	char *txt;
3642 	int res;
3643 
3644 	out = BIO_new(BIO_s_mem());
3645 	if (!out)
3646 		return;
3647 
3648 	EVP_PKEY_print_private(out, (EVP_PKEY *) key, 0, NULL);
3649 	rlen = BIO_ctrl_pending(out);
3650 	txt = os_malloc(rlen + 1);
3651 	if (txt) {
3652 		res = BIO_read(out, txt, rlen);
3653 		if (res > 0) {
3654 			txt[res] = '\0';
3655 			wpa_printf(MSG_DEBUG, "%s: %s", title, txt);
3656 		}
3657 		os_free(txt);
3658 	}
3659 	BIO_free(out);
3660 }
3661 
3662 
crypto_pkcs7_get_certificates(const struct wpabuf * pkcs7)3663 struct wpabuf * crypto_pkcs7_get_certificates(const struct wpabuf *pkcs7)
3664 {
3665 #ifdef OPENSSL_IS_BORINGSSL
3666 	CBS pkcs7_cbs;
3667 #else /* OPENSSL_IS_BORINGSSL */
3668 	PKCS7 *p7 = NULL;
3669 	const unsigned char *p = wpabuf_head(pkcs7);
3670 #endif /* OPENSSL_IS_BORINGSSL */
3671 	STACK_OF(X509) *certs;
3672 	int i, num;
3673 	BIO *out = NULL;
3674 	size_t rlen;
3675 	struct wpabuf *pem = NULL;
3676 	int res;
3677 
3678 #ifdef OPENSSL_IS_BORINGSSL
3679 	certs = sk_X509_new_null();
3680 	if (!certs)
3681 		goto fail;
3682 	CBS_init(&pkcs7_cbs, wpabuf_head(pkcs7), wpabuf_len(pkcs7));
3683 	if (!PKCS7_get_certificates(certs, &pkcs7_cbs)) {
3684 		wpa_printf(MSG_INFO,
3685 			   "OpenSSL: Could not parse PKCS#7 object: %s",
3686 			   ERR_error_string(ERR_get_error(), NULL));
3687 		goto fail;
3688 	}
3689 #else /* OPENSSL_IS_BORINGSSL */
3690 	p7 = d2i_PKCS7(NULL, &p, wpabuf_len(pkcs7));
3691 	if (!p7) {
3692 		wpa_printf(MSG_INFO,
3693 			   "OpenSSL: Could not parse PKCS#7 object: %s",
3694 			   ERR_error_string(ERR_get_error(), NULL));
3695 		goto fail;
3696 	}
3697 
3698 	switch (OBJ_obj2nid(p7->type)) {
3699 	case NID_pkcs7_signed:
3700 		certs = p7->d.sign->cert;
3701 		break;
3702 	case NID_pkcs7_signedAndEnveloped:
3703 		certs = p7->d.signed_and_enveloped->cert;
3704 		break;
3705 	default:
3706 		certs = NULL;
3707 		break;
3708 	}
3709 #endif /* OPENSSL_IS_BORINGSSL */
3710 
3711 	if (!certs || ((num = sk_X509_num(certs)) == 0)) {
3712 		wpa_printf(MSG_INFO,
3713 			   "OpenSSL: No certificates found in PKCS#7 object");
3714 		goto fail;
3715 	}
3716 
3717 	out = BIO_new(BIO_s_mem());
3718 	if (!out)
3719 		goto fail;
3720 
3721 	for (i = 0; i < num; i++) {
3722 		X509 *cert = sk_X509_value(certs, i);
3723 
3724 		PEM_write_bio_X509(out, cert);
3725 	}
3726 
3727 	rlen = BIO_ctrl_pending(out);
3728 	pem = wpabuf_alloc(rlen);
3729 	if (!pem)
3730 		goto fail;
3731 	res = BIO_read(out, wpabuf_put(pem, 0), rlen);
3732 	if (res <= 0) {
3733 		wpabuf_free(pem);
3734 		pem = NULL;
3735 		goto fail;
3736 	}
3737 	wpabuf_put(pem, res);
3738 
3739 fail:
3740 #ifdef OPENSSL_IS_BORINGSSL
3741 	if (certs)
3742 		sk_X509_pop_free(certs, X509_free);
3743 #else /* OPENSSL_IS_BORINGSSL */
3744 	PKCS7_free(p7);
3745 #endif /* OPENSSL_IS_BORINGSSL */
3746 	if (out)
3747 		BIO_free_all(out);
3748 
3749 	return pem;
3750 }
3751 
3752 
crypto_csr_init()3753 struct crypto_csr * crypto_csr_init()
3754 {
3755 	return (struct crypto_csr *)X509_REQ_new();
3756 }
3757 
3758 
crypto_csr_verify(const struct wpabuf * req)3759 struct crypto_csr * crypto_csr_verify(const struct wpabuf *req)
3760 {
3761 	X509_REQ *csr;
3762 	EVP_PKEY *pkey = NULL;
3763 	const u8 *der = wpabuf_head(req);
3764 
3765 	csr = d2i_X509_REQ(NULL, &der, wpabuf_len(req));
3766 	if (!csr)
3767 		return NULL;
3768 
3769 	pkey = X509_REQ_get_pubkey((X509_REQ *)csr);
3770 	if (!pkey)
3771 		goto fail;
3772 
3773 	if (X509_REQ_verify((X509_REQ *)csr, pkey) != 1)
3774 		goto fail;
3775 
3776 	return (struct crypto_csr *)csr;
3777 fail:
3778 	X509_REQ_free(csr);
3779 	return NULL;
3780 }
3781 
3782 
crypto_csr_deinit(struct crypto_csr * csr)3783 void crypto_csr_deinit(struct crypto_csr *csr)
3784 {
3785 	X509_REQ_free((X509_REQ *)csr);
3786 }
3787 
3788 
crypto_csr_set_ec_public_key(struct crypto_csr * csr,struct crypto_ec_key * key)3789 int crypto_csr_set_ec_public_key(struct crypto_csr *csr, struct crypto_ec_key *key)
3790 {
3791 	if (!X509_REQ_set_pubkey((X509_REQ *)csr, (EVP_PKEY *)key))
3792 		return -1;
3793 
3794 	return 0;
3795 }
3796 
3797 
crypto_csr_set_name(struct crypto_csr * csr,enum crypto_csr_name type,const char * name)3798 int crypto_csr_set_name(struct crypto_csr *csr, enum crypto_csr_name type,
3799 			const char *name)
3800 {
3801 	X509_NAME *n;
3802 	int nid;
3803 
3804 	switch (type) {
3805 	case CSR_NAME_CN:
3806 		nid = NID_commonName;
3807 		break;
3808 	case CSR_NAME_SN:
3809 		nid = NID_surname;
3810 		break;
3811 	case CSR_NAME_C:
3812 		nid = NID_countryName;
3813 		break;
3814 	case CSR_NAME_O:
3815 		nid = NID_organizationName;
3816 		break;
3817 	case CSR_NAME_OU:
3818 		nid = NID_organizationalUnitName;
3819 		break;
3820 	default:
3821 		return -1;
3822 	}
3823 
3824 	n = X509_REQ_get_subject_name((X509_REQ *) csr);
3825 	if (!n)
3826 		return -1;
3827 
3828 #if OPENSSL_VERSION_NUMBER < 0x10100000L
3829 	if (!X509_NAME_add_entry_by_NID(n, nid, MBSTRING_UTF8,
3830 					(unsigned char *) name,
3831 					os_strlen(name), -1, 0))
3832 		return -1;
3833 #else
3834 	if (!X509_NAME_add_entry_by_NID(n, nid, MBSTRING_UTF8,
3835 					(const unsigned char *) name,
3836 					os_strlen(name), -1, 0))
3837 		return -1;
3838 #endif
3839 
3840 	return 0;
3841 }
3842 
3843 
crypto_csr_set_attribute(struct crypto_csr * csr,enum crypto_csr_attr attr,int attr_type,const u8 * value,size_t len)3844 int crypto_csr_set_attribute(struct crypto_csr *csr, enum crypto_csr_attr attr,
3845 			     int attr_type, const u8 *value, size_t len)
3846 {
3847 	int nid;
3848 
3849 	switch (attr) {
3850 	case CSR_ATTR_CHALLENGE_PASSWORD:
3851 		nid = NID_pkcs9_challengePassword;
3852 		break;
3853 	default:
3854 		return -1;
3855 	}
3856 
3857 	if (!X509_REQ_add1_attr_by_NID((X509_REQ *) csr, nid, attr_type, value,
3858 				       len))
3859 		return -1;
3860 
3861 	return 0;
3862 }
3863 
3864 
crypto_csr_get_attribute(struct crypto_csr * csr,enum crypto_csr_attr attr,size_t * len,int * type)3865 const u8 * crypto_csr_get_attribute(struct crypto_csr *csr,
3866 				    enum crypto_csr_attr attr,
3867 				    size_t *len, int *type)
3868 {
3869 	X509_ATTRIBUTE *attrib;
3870 	ASN1_TYPE *attrib_type;
3871 	ASN1_STRING *data;
3872 	int loc;
3873 	int nid;
3874 
3875 	switch (attr) {
3876 	case CSR_ATTR_CHALLENGE_PASSWORD:
3877 		nid = NID_pkcs9_challengePassword;
3878 		break;
3879 	default:
3880 		return NULL;
3881 	}
3882 
3883 	loc = X509_REQ_get_attr_by_NID((X509_REQ *) csr, nid, -1);
3884 	if (loc < 0)
3885 		return NULL;
3886 
3887 	attrib = X509_REQ_get_attr((X509_REQ *) csr, loc);
3888 	if (!attrib)
3889 		return NULL;
3890 
3891 	attrib_type = X509_ATTRIBUTE_get0_type(attrib, 0);
3892 	if (!attrib_type)
3893 		return NULL;
3894 	*type = ASN1_TYPE_get(attrib_type);
3895 	data = X509_ATTRIBUTE_get0_data(attrib, 0, *type, NULL);
3896 	if (!data)
3897 		return NULL;
3898 	*len = ASN1_STRING_length(data);
3899 	return ASN1_STRING_get0_data(data);
3900 }
3901 
3902 
crypto_csr_sign(struct crypto_csr * csr,struct crypto_ec_key * key,enum crypto_hash_alg algo)3903 struct wpabuf * crypto_csr_sign(struct crypto_csr *csr,
3904 				struct crypto_ec_key *key,
3905 				enum crypto_hash_alg algo)
3906 {
3907 	const EVP_MD *sign_md;
3908 	struct wpabuf *buf;
3909 	unsigned char *der = NULL;
3910 	int der_len;
3911 
3912 	switch (algo) {
3913 	case CRYPTO_HASH_ALG_SHA256:
3914 		sign_md = EVP_sha256();
3915 		break;
3916 	case CRYPTO_HASH_ALG_SHA384:
3917 		sign_md = EVP_sha384();
3918 		break;
3919 	case CRYPTO_HASH_ALG_SHA512:
3920 		sign_md = EVP_sha512();
3921 		break;
3922 	default:
3923 		return NULL;
3924 	}
3925 
3926 	if (!X509_REQ_sign((X509_REQ *) csr, (EVP_PKEY *) key, sign_md))
3927 		return NULL;
3928 
3929 	der_len = i2d_X509_REQ((X509_REQ *) csr, &der);
3930 	if (der_len < 0)
3931 		return NULL;
3932 
3933 	buf = wpabuf_alloc_copy(der, der_len);
3934 	OPENSSL_free(der);
3935 
3936 	return buf;
3937 }
3938 
3939 #endif /* CONFIG_ECC */
3940 
3941 
crypto_rsa_key_read_public(FILE * f)3942 static EVP_PKEY * crypto_rsa_key_read_public(FILE *f)
3943 {
3944 	EVP_PKEY *pkey;
3945 	X509 *x509;
3946 
3947 	pkey = PEM_read_PUBKEY(f, NULL, NULL, NULL);
3948 	if (pkey)
3949 		return pkey;
3950 
3951 	rewind(f);
3952 	x509 = PEM_read_X509(f, NULL, NULL, NULL);
3953 	if (!x509)
3954 		return NULL;
3955 
3956 	pkey = X509_get_pubkey(x509);
3957 	X509_free(x509);
3958 
3959 	if (!pkey)
3960 		return NULL;
3961 	if (EVP_PKEY_base_id(pkey) != EVP_PKEY_RSA) {
3962 		EVP_PKEY_free(pkey);
3963 		return NULL;
3964 	}
3965 
3966 	return pkey;
3967 }
3968 
3969 
crypto_rsa_key_read(const char * file,bool private_key)3970 struct crypto_rsa_key * crypto_rsa_key_read(const char *file, bool private_key)
3971 {
3972 	FILE *f;
3973 	EVP_PKEY *pkey;
3974 
3975 	f = fopen(file, "r");
3976 	if (!f)
3977 		return NULL;
3978 	if (private_key)
3979 		pkey = PEM_read_PrivateKey(f, NULL, NULL, NULL);
3980 	else
3981 		pkey = crypto_rsa_key_read_public(f);
3982 	fclose(f);
3983 	return (struct crypto_rsa_key *) pkey;
3984 }
3985 
3986 
3987 #ifndef OPENSSL_NO_SHA256
3988 
crypto_rsa_oaep_sha256_encrypt(struct crypto_rsa_key * key,const struct wpabuf * in)3989 struct wpabuf * crypto_rsa_oaep_sha256_encrypt(struct crypto_rsa_key *key,
3990 					       const struct wpabuf *in)
3991 {
3992 #if !defined(LIBRESSL_VERSION_NUMBER) || LIBRESSL_VERSION_NUMBER >= 0x30400000L
3993 	EVP_PKEY *pkey = (EVP_PKEY *) key;
3994 	EVP_PKEY_CTX *pkctx;
3995 	struct wpabuf *res = NULL;
3996 	size_t outlen;
3997 
3998 	pkctx = EVP_PKEY_CTX_new(pkey, NULL);
3999 	if (!pkctx)
4000 		goto fail;
4001 
4002 	if (EVP_PKEY_encrypt_init(pkctx) != 1 ||
4003 	    EVP_PKEY_CTX_set_rsa_padding(pkctx, RSA_PKCS1_OAEP_PADDING) <= 0 ||
4004 	    EVP_PKEY_CTX_set_rsa_oaep_md(pkctx, EVP_sha256()) <= 0 ||
4005 	    EVP_PKEY_encrypt(pkctx, NULL, &outlen, wpabuf_head(in),
4006 			     wpabuf_len(in)) != 1 ||
4007 	    !(res = wpabuf_alloc(outlen)) ||
4008 	    EVP_PKEY_encrypt(pkctx, wpabuf_put(res, 0), &outlen,
4009 			     wpabuf_head(in), wpabuf_len(in)) != 1) {
4010 		wpabuf_free(res);
4011 		res = NULL;
4012 		goto fail;
4013 	}
4014 	wpabuf_put(res, outlen);
4015 
4016 fail:
4017 	EVP_PKEY_CTX_free(pkctx);
4018 	return res;
4019 #else
4020 	wpa_printf(MSG_ERROR, "%s() not supported", __func__);
4021 	return NULL;
4022 #endif
4023 }
4024 
4025 
crypto_rsa_oaep_sha256_decrypt(struct crypto_rsa_key * key,const struct wpabuf * in)4026 struct wpabuf * crypto_rsa_oaep_sha256_decrypt(struct crypto_rsa_key *key,
4027 					       const struct wpabuf *in)
4028 {
4029 #if !defined(LIBRESSL_VERSION_NUMBER) || LIBRESSL_VERSION_NUMBER >= 0x30400000L
4030 	EVP_PKEY *pkey = (EVP_PKEY *) key;
4031 	EVP_PKEY_CTX *pkctx;
4032 	struct wpabuf *res = NULL;
4033 	size_t outlen;
4034 
4035 	pkctx = EVP_PKEY_CTX_new(pkey, NULL);
4036 	if (!pkctx)
4037 		goto fail;
4038 
4039 	if (EVP_PKEY_decrypt_init(pkctx) != 1 ||
4040 	    EVP_PKEY_CTX_set_rsa_padding(pkctx, RSA_PKCS1_OAEP_PADDING) <= 0 ||
4041 	    EVP_PKEY_CTX_set_rsa_oaep_md(pkctx, EVP_sha256()) <= 0 ||
4042 	    EVP_PKEY_decrypt(pkctx, NULL, &outlen, wpabuf_head(in),
4043 			     wpabuf_len(in)) != 1 ||
4044 	    !(res = wpabuf_alloc(outlen)) ||
4045 	    EVP_PKEY_decrypt(pkctx, wpabuf_put(res, 0), &outlen,
4046 			     wpabuf_head(in), wpabuf_len(in)) != 1) {
4047 		wpabuf_free(res);
4048 		res = NULL;
4049 		goto fail;
4050 	}
4051 	wpabuf_put(res, outlen);
4052 
4053 fail:
4054 	EVP_PKEY_CTX_free(pkctx);
4055 	return res;
4056 #else
4057 	wpa_printf(MSG_ERROR, "%s() not supported", __func__);
4058 	return NULL;
4059 #endif
4060 }
4061 
4062 #endif /* OPENSSL_NO_SHA256 */
4063 
4064 
crypto_rsa_key_free(struct crypto_rsa_key * key)4065 void crypto_rsa_key_free(struct crypto_rsa_key *key)
4066 {
4067 	EVP_PKEY_free((EVP_PKEY *) key);
4068 }
4069 
4070 
crypto_unload(void)4071 void crypto_unload(void)
4072 {
4073 	openssl_unload_legacy_provider();
4074 }
4075