blob: c6e065f82b5a48a6af29ee1033d0b023e7000f79 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002 * Wrapper functions for OpenSSL libcrypto
Sunil Ravia04bd252022-05-02 22:54:18 -07003 * Copyright (c) 2004-2022, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "includes.h"
10#include <openssl/opensslv.h>
11#include <openssl/err.h>
12#include <openssl/des.h>
13#include <openssl/aes.h>
14#include <openssl/bn.h>
15#include <openssl/evp.h>
16#include <openssl/dh.h>
Dmitry Shmidt04949592012-07-19 12:16:46 -070017#include <openssl/hmac.h>
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070018#include <openssl/rand.h>
Sunil Ravia04bd252022-05-02 22:54:18 -070019#include <openssl/pem.h>
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080020#ifdef CONFIG_ECC
21#include <openssl/ec.h>
Hai Shalom899fcc72020-10-19 14:38:18 -070022#include <openssl/x509.h>
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080023#endif /* CONFIG_ECC */
Hai Shaloma20dcd72022-02-04 13:43:00 -080024#if OPENSSL_VERSION_NUMBER >= 0x30000000L
25#include <openssl/provider.h>
Sunil Ravia04bd252022-05-02 22:54:18 -070026#include <openssl/core_names.h>
27#include <openssl/param_build.h>
28#else /* OpenSSL version >= 3.0 */
29#include <openssl/cmac.h>
Hai Shaloma20dcd72022-02-04 13:43:00 -080030#endif /* OpenSSL version >= 3.0 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070031
32#include "common.h"
Hai Shalom021b0b52019-04-10 11:17:58 -070033#include "utils/const_time.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070034#include "wpabuf.h"
35#include "dh_group5.h"
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080036#include "sha1.h"
37#include "sha256.h"
Dmitry Shmidt807291d2015-01-27 13:40:23 -080038#include "sha384.h"
Hai Shalom74f70d42019-02-11 14:42:39 -080039#include "sha512.h"
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -070040#include "md5.h"
41#include "aes_wrap.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070042#include "crypto.h"
43
Sunil Ravia04bd252022-05-02 22:54:18 -070044#if OPENSSL_VERSION_NUMBER < 0x10100000L
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -080045/* Compatibility wrappers for older versions. */
46
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -080047static 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
58static void HMAC_CTX_free(HMAC_CTX *ctx)
59{
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -070060 if (!ctx)
61 return;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -080062 HMAC_CTX_cleanup(ctx);
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -080063 bin_clear_free(ctx, sizeof(*ctx));
64}
65
66
67static 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
78static void EVP_MD_CTX_free(EVP_MD_CTX *ctx)
79{
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -070080 if (!ctx)
81 return;
82 EVP_MD_CTX_cleanup(ctx);
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -080083 bin_clear_free(ctx, sizeof(*ctx));
84}
85
Hai Shalom899fcc72020-10-19 14:38:18 -070086
Hai Shaloma20dcd72022-02-04 13:43:00 -080087#ifdef CONFIG_ECC
88
Hai Shalom899fcc72020-10-19 14:38:18 -070089static 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
Hai Shaloma20dcd72022-02-04 13:43:00 -080096
97static 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
105static 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
116static const unsigned char * ASN1_STRING_get0_data(const ASN1_STRING *x)
117{
118 return ASN1_STRING_data((ASN1_STRING *) x);
119}
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800120#endif /* OpenSSL version < 1.1.0 */
121
Hai Shaloma20dcd72022-02-04 13:43:00 -0800122
Sunil Ravia04bd252022-05-02 22:54:18 -0700123#if OPENSSL_VERSION_NUMBER < 0x10101000L || \
124 (defined(LIBRESSL_VERSION_NUMBER) && \
125 LIBRESSL_VERSION_NUMBER < 0x30400000L)
126
127static 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
135static 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
150static 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
159static 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
169static OSSL_PROVIDER *openssl_default_provider = NULL;
170static OSSL_PROVIDER *openssl_legacy_provider = NULL;
171#endif /* OpenSSL version >= 3.0 */
172
Hai Shaloma20dcd72022-02-04 13:43:00 -0800173void openssl_load_legacy_provider(void)
174{
175#if OPENSSL_VERSION_NUMBER >= 0x30000000L
Sunil Ravia04bd252022-05-02 22:54:18 -0700176 if (openssl_legacy_provider)
Hai Shaloma20dcd72022-02-04 13:43:00 -0800177 return;
178
Sunil Ravia04bd252022-05-02 22:54:18 -0700179 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}
Hai Shaloma20dcd72022-02-04 13:43:00 -0800184
Sunil Ravia04bd252022-05-02 22:54:18 -0700185
186static 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;
Hai Shaloma20dcd72022-02-04 13:43:00 -0800196 }
197#endif /* OpenSSL version >= 3.0 */
198}
199
200
Sunil Ravia04bd252022-05-02 22:54:18 -0700201#if OPENSSL_VERSION_NUMBER < 0x30000000L
202
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700203static BIGNUM * get_group5_prime(void)
204{
Sunil Ravia04bd252022-05-02 22:54:18 -0700205#if OPENSSL_VERSION_NUMBER >= 0x10100000L
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -0700206 return BN_get_rfc3526_prime_1536(NULL);
207#elif !defined(OPENSSL_IS_BORINGSSL)
208 return get_rfc3526_prime_1536(NULL);
209#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700210 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);
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -0700229#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700230}
231
Hai Shalom021b0b52019-04-10 11:17:58 -0700232
233static 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
Sunil Ravia04bd252022-05-02 22:54:18 -0700256#endif /* OpenSSL version < 3.0 */
257
Hai Shalom021b0b52019-04-10 11:17:58 -0700258
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700259#ifdef OPENSSL_NO_SHA256
260#define NO_SHA256_WRAPPER
261#endif
Paul Stewart092955c2017-02-06 09:13:09 -0800262#ifdef OPENSSL_NO_SHA512
263#define NO_SHA384_WRAPPER
264#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700265
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700266static int openssl_digest_vector(const EVP_MD *type, size_t num_elem,
267 const u8 *addr[], const size_t *len, u8 *mac)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700268{
Dmitry Shmidt55840ad2015-12-14 12:45:46 -0800269 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;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700303}
304
305
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800306#ifndef CONFIG_FIPS
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700307int md4_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
308{
Hai Shaloma20dcd72022-02-04 13:43:00 -0800309 openssl_load_legacy_provider();
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700310 return openssl_digest_vector(EVP_md4(), num_elem, addr, len, mac);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700311}
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800312#endif /* CONFIG_FIPS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700313
314
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700315int des_encrypt(const u8 *clear, const u8 *key, u8 *cypher)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700316{
317 u8 pkey[8], next, tmp;
Hai Shaloma20dcd72022-02-04 13:43:00 -0800318 int i, plen, ret = -1;
319 EVP_CIPHER_CTX *ctx;
320
321 openssl_load_legacy_provider();
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700322
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
Hai Shaloma20dcd72022-02-04 13:43:00 -0800332 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;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700345}
346
347
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800348#ifndef CONFIG_NO_RC4
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700349int 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 */
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800355 EVP_CIPHER_CTX *ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700356 int outl;
357 int res = -1;
358 unsigned char skip_buf[16];
359
Hai Shaloma20dcd72022-02-04 13:43:00 -0800360 openssl_load_legacy_provider();
361
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800362 ctx = EVP_CIPHER_CTX_new();
363 if (!ctx ||
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800364 !EVP_CipherInit_ex(ctx, EVP_rc4(), NULL, NULL, NULL, 1) ||
Hai Shaloma20dcd72022-02-04 13:43:00 -0800365 !EVP_CIPHER_CTX_set_padding(ctx, 0) ||
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800366 !EVP_CIPHER_CTX_set_key_length(ctx, keylen) ||
367 !EVP_CipherInit_ex(ctx, NULL, NULL, key, NULL, 1))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700368 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);
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800374 if (!EVP_CipherUpdate(ctx, skip_buf, &outl, skip_buf, len))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700375 goto out;
376 skip -= len;
377 }
378
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800379 if (EVP_CipherUpdate(ctx, data, &outl, data, data_len))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700380 res = 0;
381
382out:
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800383 if (ctx)
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800384 EVP_CIPHER_CTX_free(ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700385 return res;
386#endif /* OPENSSL_NO_RC4 */
387}
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800388#endif /* CONFIG_NO_RC4 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700389
390
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800391#ifndef CONFIG_FIPS
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700392int md5_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
393{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700394 return openssl_digest_vector(EVP_md5(), num_elem, addr, len, mac);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700395}
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800396#endif /* CONFIG_FIPS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700397
398
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700399int sha1_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
400{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700401 return openssl_digest_vector(EVP_sha1(), num_elem, addr, len, mac);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700402}
403
404
405#ifndef NO_SHA256_WRAPPER
406int sha256_vector(size_t num_elem, const u8 *addr[], const size_t *len,
407 u8 *mac)
408{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700409 return openssl_digest_vector(EVP_sha256(), num_elem, addr, len, mac);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700410}
411#endif /* NO_SHA256_WRAPPER */
412
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700413
Paul Stewart092955c2017-02-06 09:13:09 -0800414#ifndef NO_SHA384_WRAPPER
415int 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
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700422
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700423#ifndef NO_SHA512_WRAPPER
424int 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
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700432static 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
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700447void * aes_encrypt_init(const u8 *key, size_t len)
448{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700449 EVP_CIPHER_CTX *ctx;
450 const EVP_CIPHER *type;
451
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800452 if (TEST_FAIL())
453 return NULL;
454
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700455 type = aes_get_evp_cipher(len);
Roshan Pius3a1667e2018-07-03 15:17:14 -0700456 if (!type) {
457 wpa_printf(MSG_INFO, "%s: Unsupported len=%u",
458 __func__, (unsigned int) len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700459 return NULL;
Roshan Pius3a1667e2018-07-03 15:17:14 -0700460 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700461
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800462 ctx = EVP_CIPHER_CTX_new();
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700463 if (ctx == NULL)
464 return NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700465 if (EVP_EncryptInit_ex(ctx, type, NULL, key, NULL) != 1) {
Sunil Ravia04bd252022-05-02 22:54:18 -0700466 EVP_CIPHER_CTX_free(ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700467 return NULL;
468 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700469 EVP_CIPHER_CTX_set_padding(ctx, 0);
470 return ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700471}
472
473
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700474int aes_encrypt(void *ctx, const u8 *plain, u8 *crypt)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700475{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700476 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));
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700481 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700482 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700483 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700484}
485
486
487void aes_encrypt_deinit(void *ctx)
488{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700489 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 }
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800500 EVP_CIPHER_CTX_free(c);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700501}
502
503
504void * aes_decrypt_init(const u8 *key, size_t len)
505{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700506 EVP_CIPHER_CTX *ctx;
507 const EVP_CIPHER *type;
508
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800509 if (TEST_FAIL())
510 return NULL;
511
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700512 type = aes_get_evp_cipher(len);
Roshan Pius3a1667e2018-07-03 15:17:14 -0700513 if (!type) {
514 wpa_printf(MSG_INFO, "%s: Unsupported len=%u",
515 __func__, (unsigned int) len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700516 return NULL;
Roshan Pius3a1667e2018-07-03 15:17:14 -0700517 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700518
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800519 ctx = EVP_CIPHER_CTX_new();
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700520 if (ctx == NULL)
521 return NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700522 if (EVP_DecryptInit_ex(ctx, type, NULL, key, NULL) != 1) {
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800523 EVP_CIPHER_CTX_free(ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700524 return NULL;
525 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700526 EVP_CIPHER_CTX_set_padding(ctx, 0);
527 return ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700528}
529
530
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700531int aes_decrypt(void *ctx, const u8 *crypt, u8 *plain)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700532{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700533 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));
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700538 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700539 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700540 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700541}
542
543
544void aes_decrypt_deinit(void *ctx)
545{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700546 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 }
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800557 EVP_CIPHER_CTX_free(c);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700558}
559
560
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800561#ifndef CONFIG_FIPS
562#ifndef CONFIG_OPENSSL_INTERNAL_AES_WRAP
563
Sunil Ravia04bd252022-05-02 22:54:18 -0700564#if OPENSSL_VERSION_NUMBER >= 0x30000000L
565static 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
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800581int aes_wrap(const u8 *kek, size_t kek_len, int n, const u8 *plain, u8 *cipher)
582{
Sunil Ravia04bd252022-05-02 22:54:18 -0700583#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 */
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800610 AES_KEY actx;
611 int res;
612
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800613 if (TEST_FAIL())
614 return -1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800615 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;
Sunil Ravia04bd252022-05-02 22:54:18 -0700620#endif /* OpenSSL version >= 3.0 */
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800621}
622
623
624int aes_unwrap(const u8 *kek, size_t kek_len, int n, const u8 *cipher,
625 u8 *plain)
626{
Sunil Ravia04bd252022-05-02 22:54:18 -0700627#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 */
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800654 AES_KEY actx;
655 int res;
656
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800657 if (TEST_FAIL())
658 return -1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800659 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;
Sunil Ravia04bd252022-05-02 22:54:18 -0700664#endif /* OpenSSL version >= 3.0 */
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800665}
666
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800667#endif /* CONFIG_OPENSSL_INTERNAL_AES_WRAP */
668#endif /* CONFIG_FIPS */
669
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800670
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -0700671int aes_128_cbc_encrypt(const u8 *key, const u8 *iv, u8 *data, size_t data_len)
672{
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800673 EVP_CIPHER_CTX *ctx;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -0700674 int clen, len;
675 u8 buf[16];
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800676 int res = -1;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -0700677
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -0800678 if (TEST_FAIL())
679 return -1;
680
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800681 ctx = EVP_CIPHER_CTX_new();
682 if (!ctx)
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -0700683 return -1;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -0700684 clen = data_len;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -0700685 len = sizeof(buf);
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800686 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;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800692 EVP_CIPHER_CTX_free(ctx);
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -0700693
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800694 return res;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -0700695}
696
697
698int aes_128_cbc_decrypt(const u8 *key, const u8 *iv, u8 *data, size_t data_len)
699{
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800700 EVP_CIPHER_CTX *ctx;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -0700701 int plen, len;
702 u8 buf[16];
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800703 int res = -1;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -0700704
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -0800705 if (TEST_FAIL())
706 return -1;
707
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800708 ctx = EVP_CIPHER_CTX_new();
709 if (!ctx)
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -0700710 return -1;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -0700711 plen = data_len;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -0700712 len = sizeof(buf);
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800713 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;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800719 EVP_CIPHER_CTX_free(ctx);
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -0700720
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800721 return res;
722
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -0700723}
724
725
Roshan Pius3a1667e2018-07-03 15:17:14 -0700726int 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
752int crypto_dh_derive_secret(u8 generator, const u8 *prime, size_t prime_len,
Hai Shalom021b0b52019-04-10 11:17:58 -0700753 const u8 *order, size_t order_len,
Roshan Pius3a1667e2018-07-03 15:17:14 -0700754 const u8 *privkey, size_t privkey_len,
755 const u8 *pubkey, size_t pubkey_len,
756 u8 *secret, size_t *len)
757{
Hai Shalom021b0b52019-04-10 11:17:58 -0700758 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);
Hai Shalom81f62d82019-07-22 12:10:00 -0700779 BN_clear_free(q);
780 BN_clear_free(tmp);
Hai Shalom021b0b52019-04-10 11:17:58 -0700781 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);
788fail:
Hai Shalom81f62d82019-07-22 12:10:00 -0700789 BN_clear_free(pub);
790 BN_clear_free(p);
Hai Shalom021b0b52019-04-10 11:17:58 -0700791 return res;
Roshan Pius3a1667e2018-07-03 15:17:14 -0700792}
793
794
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700795int 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
Hai Shalom021b0b52019-04-10 11:17:58 -0700817 if (BN_mod_exp_mont_consttime(bn_result, bn_base, bn_exp, bn_modulus,
818 ctx, NULL) != 1)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700819 goto error;
820
821 *result_len = BN_bn2bin(bn_result, result);
822 ret = 0;
823
824error:
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -0700825 BN_clear_free(bn_base);
826 BN_clear_free(bn_exp);
827 BN_clear_free(bn_modulus);
828 BN_clear_free(bn_result);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700829 BN_CTX_free(ctx);
830 return ret;
831}
832
833
834struct crypto_cipher {
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800835 EVP_CIPHER_CTX *enc;
836 EVP_CIPHER_CTX *dec;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700837};
838
839
840struct 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) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800852#ifndef CONFIG_NO_RC4
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700853#ifndef OPENSSL_NO_RC4
854 case CRYPTO_CIPHER_ALG_RC4:
855 cipher = EVP_rc4();
856 break;
857#endif /* OPENSSL_NO_RC4 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800858#endif /* CONFIG_NO_RC4 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700859#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;
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -0700865#ifndef OPENSSL_IS_BORINGSSL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700866 case 24:
867 cipher = EVP_aes_192_cbc();
868 break;
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -0700869#endif /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700870 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
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800897 if (!(ctx->enc = EVP_CIPHER_CTX_new()) ||
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800898 !EVP_EncryptInit_ex(ctx->enc, cipher, NULL, NULL, NULL) ||
Hai Shaloma20dcd72022-02-04 13:43:00 -0800899 !EVP_CIPHER_CTX_set_padding(ctx->enc, 0) ||
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800900 !EVP_CIPHER_CTX_set_key_length(ctx->enc, key_len) ||
901 !EVP_EncryptInit_ex(ctx->enc, NULL, NULL, key, iv)) {
902 if (ctx->enc)
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800903 EVP_CIPHER_CTX_free(ctx->enc);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700904 os_free(ctx);
905 return NULL;
906 }
907
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800908 if (!(ctx->dec = EVP_CIPHER_CTX_new()) ||
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800909 !EVP_DecryptInit_ex(ctx->dec, cipher, NULL, NULL, NULL) ||
Hai Shaloma20dcd72022-02-04 13:43:00 -0800910 !EVP_CIPHER_CTX_set_padding(ctx->dec, 0) ||
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800911 !EVP_CIPHER_CTX_set_key_length(ctx->dec, key_len) ||
912 !EVP_DecryptInit_ex(ctx->dec, NULL, NULL, key, iv)) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800913 EVP_CIPHER_CTX_free(ctx->enc);
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800914 if (ctx->dec)
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800915 EVP_CIPHER_CTX_free(ctx->dec);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700916 os_free(ctx);
917 return NULL;
918 }
919
920 return ctx;
921}
922
923
924int crypto_cipher_encrypt(struct crypto_cipher *ctx, const u8 *plain,
925 u8 *crypt, size_t len)
926{
927 int outl;
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800928 if (!EVP_EncryptUpdate(ctx->enc, crypt, &outl, plain, len))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700929 return -1;
930 return 0;
931}
932
933
934int crypto_cipher_decrypt(struct crypto_cipher *ctx, const u8 *crypt,
935 u8 *plain, size_t len)
936{
937 int outl;
938 outl = len;
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800939 if (!EVP_DecryptUpdate(ctx->dec, plain, &outl, crypt, len))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700940 return -1;
941 return 0;
942}
943
944
945void crypto_cipher_deinit(struct crypto_cipher *ctx)
946{
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800947 EVP_CIPHER_CTX_free(ctx->enc);
948 EVP_CIPHER_CTX_free(ctx->dec);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700949 os_free(ctx);
950}
951
952
953void * dh5_init(struct wpabuf **priv, struct wpabuf **publ)
954{
Sunil Ravia04bd252022-05-02 22:54:18 -0700955#if OPENSSL_VERSION_NUMBER < 0x10100000L
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700956 DH *dh;
957 struct wpabuf *pubkey = NULL, *privkey = NULL;
958 size_t publen, privlen;
959
960 *priv = NULL;
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700961 wpabuf_free(*publ);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700962 *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
Hai Shalom021b0b52019-04-10 11:17:58 -0700976 dh->q = get_group5_order();
977 if (!dh->q)
978 goto err;
979
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700980 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
999err:
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001000 wpabuf_clear_free(pubkey);
1001 wpabuf_clear_free(privkey);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001002 DH_free(dh);
1003 return NULL;
Sunil Ravia04bd252022-05-02 22:54:18 -07001004#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;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001055#else
1056 DH *dh;
1057 struct wpabuf *pubkey = NULL, *privkey = NULL;
1058 size_t publen, privlen;
Hai Shalom021b0b52019-04-10 11:17:58 -07001059 BIGNUM *p, *g, *q;
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -07001060 const BIGNUM *priv_key = NULL, *pub_key = NULL;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001061
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();
Hai Shalom021b0b52019-04-10 11:17:58 -07001072 q = get_group5_order();
1073 if (!g || BN_set_word(g, 2) != 1 || !p || !q ||
1074 DH_set0_pqg(dh, p, q, g) != 1)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001075 goto err;
1076 p = NULL;
Hai Shalom021b0b52019-04-10 11:17:58 -07001077 q = NULL;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001078 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
1100err:
1101 BN_free(p);
Hai Shalom021b0b52019-04-10 11:17:58 -07001102 BN_free(q);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001103 BN_free(g);
1104 wpabuf_clear_free(pubkey);
1105 wpabuf_clear_free(privkey);
1106 DH_free(dh);
1107 return NULL;
1108#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001109}
1110
1111
Dmitry Shmidt04949592012-07-19 12:16:46 -07001112void * dh5_init_fixed(const struct wpabuf *priv, const struct wpabuf *publ)
1113{
Sunil Ravia04bd252022-05-02 22:54:18 -07001114#if OPENSSL_VERSION_NUMBER < 0x10100000L
Dmitry Shmidt04949592012-07-19 12:16:46 -07001115 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
1142err:
1143 DH_free(dh);
1144 return NULL;
Sunil Ravia04bd252022-05-02 22:54:18 -07001145#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;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001178#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);
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07001196 if (!priv_key || !pub_key || DH_set0_key(dh, pub_key, priv_key) != 1)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001197 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
1206err:
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
Dmitry Shmidt04949592012-07-19 12:16:46 -07001214}
1215
1216
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001217struct wpabuf * dh5_derive_shared(void *ctx, const struct wpabuf *peer_public,
1218 const struct wpabuf *own_private)
1219{
Sunil Ravia04bd252022-05-02 22:54:18 -07001220#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 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001250 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);
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001273 BN_clear_free(pub_key);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001274
1275 return res;
1276
1277err:
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001278 BN_clear_free(pub_key);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001279 wpabuf_clear_free(res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001280 return NULL;
Sunil Ravia04bd252022-05-02 22:54:18 -07001281#endif /* OpenSSL version >= 3.0 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001282}
1283
1284
1285void dh5_free(void *ctx)
1286{
Sunil Ravia04bd252022-05-02 22:54:18 -07001287#if OPENSSL_VERSION_NUMBER >= 0x30000000L
1288 EVP_PKEY *pkey = ctx;
1289
1290 EVP_PKEY_free(pkey);
1291#else /* OpenSSL version >= 3.0 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001292 DH *dh;
1293 if (ctx == NULL)
1294 return;
1295 dh = ctx;
1296 DH_free(dh);
Sunil Ravia04bd252022-05-02 22:54:18 -07001297#endif /* OpenSSL version >= 3.0 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001298}
Dmitry Shmidt04949592012-07-19 12:16:46 -07001299
1300
1301struct crypto_hash {
Sunil Ravia04bd252022-05-02 22:54:18 -07001302#if OPENSSL_VERSION_NUMBER >= 0x30000000L
1303 EVP_MAC_CTX *ctx;
1304#else /* OpenSSL version >= 3.0 */
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08001305 HMAC_CTX *ctx;
Sunil Ravia04bd252022-05-02 22:54:18 -07001306#endif /* OpenSSL version >= 3.0 */
Dmitry Shmidt04949592012-07-19 12:16:46 -07001307};
1308
1309
1310struct crypto_hash * crypto_hash_init(enum crypto_hash_alg alg, const u8 *key,
1311 size_t key_len)
1312{
Sunil Ravia04bd252022-05-02 22:54:18 -07001313#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 */
Dmitry Shmidt04949592012-07-19 12:16:46 -07001368 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;
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08001396 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 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001407
1408 return ctx;
Sunil Ravia04bd252022-05-02 22:54:18 -07001409#endif /* OpenSSL version >= 3.0 */
Dmitry Shmidt04949592012-07-19 12:16:46 -07001410}
1411
1412
1413void crypto_hash_update(struct crypto_hash *ctx, const u8 *data, size_t len)
1414{
1415 if (ctx == NULL)
1416 return;
Sunil Ravia04bd252022-05-02 22:54:18 -07001417#if OPENSSL_VERSION_NUMBER >= 0x30000000L
1418 EVP_MAC_update(ctx->ctx, data, len);
1419#else /* OpenSSL version >= 3.0 */
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08001420 HMAC_Update(ctx->ctx, data, len);
Sunil Ravia04bd252022-05-02 22:54:18 -07001421#endif /* OpenSSL version >= 3.0 */
Dmitry Shmidt04949592012-07-19 12:16:46 -07001422}
1423
1424
1425int crypto_hash_finish(struct crypto_hash *ctx, u8 *mac, size_t *len)
1426{
Sunil Ravia04bd252022-05-02 22:54:18 -07001427#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 */
Dmitry Shmidt04949592012-07-19 12:16:46 -07001460 unsigned int mdlen;
1461 int res;
1462
1463 if (ctx == NULL)
1464 return -2;
1465
1466 if (mac == NULL || len == NULL) {
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08001467 HMAC_CTX_free(ctx->ctx);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001468 bin_clear_free(ctx, sizeof(*ctx));
Dmitry Shmidt04949592012-07-19 12:16:46 -07001469 return 0;
1470 }
1471
1472 mdlen = *len;
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08001473 res = HMAC_Final(ctx->ctx, mac, &mdlen);
1474 HMAC_CTX_free(ctx->ctx);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001475 bin_clear_free(ctx, sizeof(*ctx));
Dmitry Shmidt04949592012-07-19 12:16:46 -07001476
Hai Shalom5f92bc92019-04-18 11:54:11 -07001477 if (TEST_FAIL())
1478 return -1;
1479
Dmitry Shmidt04949592012-07-19 12:16:46 -07001480 if (res == 1) {
1481 *len = mdlen;
1482 return 0;
1483 }
1484
1485 return -1;
Sunil Ravia04bd252022-05-02 22:54:18 -07001486#endif /* OpenSSL version >= 3.0 */
Dmitry Shmidt04949592012-07-19 12:16:46 -07001487}
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001488
1489
Sunil Ravia04bd252022-05-02 22:54:18 -07001490#if OPENSSL_VERSION_NUMBER >= 0x30000000L
1491
1492static 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;
1530fail:
1531 EVP_MAC_CTX_free(ctx);
1532 return -1;
1533}
1534
1535
1536#ifndef CONFIG_FIPS
1537
1538int 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
1546int 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
1555int 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
1563int 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
1572int 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
1580int 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
1591int 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
1599int 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
1610int 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
1618int 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
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001628static 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)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001632{
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08001633 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);
1651done:
1652 HMAC_CTX_free(ctx);
1653
1654 return res == 1 ? 0 : -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001655}
1656
1657
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001658#ifndef CONFIG_FIPS
1659
1660int 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
1668int 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
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001677int 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
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001685int 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
1694int 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{
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001697 return openssl_hmac_vector(EVP_sha256(), key, key_len, num_elem, addr,
1698 len, mac, 32);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001699}
1700
1701
1702int 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
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001711#ifdef CONFIG_SHA384
1712
1713int 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{
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001716 return openssl_hmac_vector(EVP_sha384(), key, key_len, num_elem, addr,
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08001717 len, mac, 48);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001718}
1719
1720
1721int 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
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001730#ifdef CONFIG_SHA512
1731
1732int 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
1740int 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
Sunil Ravia04bd252022-05-02 22:54:18 -07001748#endif /* OpenSSL version >= 3.0 */
1749
1750
1751int 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
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001760
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001761int 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
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001769int omac1_aes_vector(const u8 *key, size_t key_len, size_t num_elem,
1770 const u8 *addr[], const size_t *len, u8 *mac)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001771{
Sunil Ravia04bd252022-05-02 22:54:18 -07001772#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;
1808fail:
1809 EVP_MAC_CTX_free(ctx);
1810 return ret;
1811#else /* OpenSSL version >= 3.0 */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001812 CMAC_CTX *ctx;
1813 int ret = -1;
1814 size_t outlen, i;
1815
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001816 if (TEST_FAIL())
1817 return -1;
1818
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001819 ctx = CMAC_CTX_new();
1820 if (ctx == NULL)
1821 return -1;
1822
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001823 if (key_len == 32) {
1824 if (!CMAC_Init(ctx, key, 32, EVP_aes_256_cbc(), NULL))
1825 goto fail;
Sunil Ravia04bd252022-05-02 22:54:18 -07001826 } else if (key_len == 24) {
1827 if (!CMAC_Init(ctx, key, 24, EVP_aes_192_cbc(), NULL))
1828 goto fail;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001829 } else if (key_len == 16) {
1830 if (!CMAC_Init(ctx, key, 16, EVP_aes_128_cbc(), NULL))
1831 goto fail;
1832 } else {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001833 goto fail;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001834 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001835 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;
1843fail:
1844 CMAC_CTX_free(ctx);
1845 return ret;
Sunil Ravia04bd252022-05-02 22:54:18 -07001846#endif /* OpenSSL version >= 3.0 */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001847}
1848
1849
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001850int 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
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001857int 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}
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001861
1862
1863int 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}
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001867
1868
1869struct crypto_bignum * crypto_bignum_init(void)
1870{
Dmitry Shmidte4663042016-04-04 10:07:49 -07001871 if (TEST_FAIL())
1872 return NULL;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001873 return (struct crypto_bignum *) BN_new();
1874}
1875
1876
1877struct crypto_bignum * crypto_bignum_init_set(const u8 *buf, size_t len)
1878{
Dmitry Shmidte4663042016-04-04 10:07:49 -07001879 BIGNUM *bn;
1880
1881 if (TEST_FAIL())
1882 return NULL;
1883
1884 bn = BN_bin2bn(buf, len, NULL);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001885 return (struct crypto_bignum *) bn;
1886}
1887
1888
Hai Shalomc3565922019-10-28 11:58:20 -07001889struct 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
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001907void 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
1916int 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
Dmitry Shmidte4663042016-04-04 10:07:49 -07001921 if (TEST_FAIL())
1922 return -1;
1923
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001924 if (padlen > buflen)
1925 return -1;
1926
Hai Shalomc3565922019-10-28 11:58:20 -07001927 if (padlen) {
Hai Shalom81f62d82019-07-22 12:10:00 -07001928#ifdef OPENSSL_IS_BORINGSSL
Hai Shalomc3565922019-10-28 11:58:20 -07001929 if (BN_bn2bin_padded(buf, padlen, (const BIGNUM *) a) == 0)
1930 return -1;
1931 return padlen;
Hai Shalom81f62d82019-07-22 12:10:00 -07001932#else /* OPENSSL_IS_BORINGSSL */
1933#if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)
Hai Shalomc3565922019-10-28 11:58:20 -07001934 return BN_bn2binpad((const BIGNUM *) a, buf, padlen);
1935#endif
1936#endif
1937 }
1938
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001939 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
Roshan Pius3a1667e2018-07-03 15:17:14 -07001954int crypto_bignum_rand(struct crypto_bignum *r, const struct crypto_bignum *m)
1955{
Hai Shalom5f92bc92019-04-18 11:54:11 -07001956 if (TEST_FAIL())
1957 return -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07001958 return BN_rand_range((BIGNUM *) r, (const BIGNUM *) m) == 1 ? 0 : -1;
1959}
1960
1961
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001962int 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
1971int 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
1989int 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
Dmitry Shmidte4663042016-04-04 10:07:49 -07001997 if (TEST_FAIL())
1998 return -1;
1999
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002000 bnctx = BN_CTX_new();
2001 if (bnctx == NULL)
2002 return -1;
Hai Shalom021b0b52019-04-10 11:17:58 -07002003 res = BN_mod_exp_mont_consttime((BIGNUM *) d, (const BIGNUM *) a,
2004 (const BIGNUM *) b, (const BIGNUM *) c,
2005 bnctx, NULL);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002006 BN_CTX_free(bnctx);
2007
2008 return res ? 0 : -1;
2009}
2010
2011
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002012int 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
Dmitry Shmidte4663042016-04-04 10:07:49 -07002019 if (TEST_FAIL())
2020 return -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002021 bnctx = BN_CTX_new();
2022 if (bnctx == NULL)
2023 return -1;
Hai Shalom021b0b52019-04-10 11:17:58 -07002024#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 */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002029 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
2037int crypto_bignum_sub(const struct crypto_bignum *a,
2038 const struct crypto_bignum *b,
2039 struct crypto_bignum *c)
2040{
Dmitry Shmidte4663042016-04-04 10:07:49 -07002041 if (TEST_FAIL())
2042 return -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002043 return BN_sub((BIGNUM *) c, (const BIGNUM *) a, (const BIGNUM *) b) ?
2044 0 : -1;
2045}
2046
2047
2048int 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
Dmitry Shmidte4663042016-04-04 10:07:49 -07002056 if (TEST_FAIL())
2057 return -1;
2058
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002059 bnctx = BN_CTX_new();
2060 if (bnctx == NULL)
2061 return -1;
Hai Shalom021b0b52019-04-10 11:17:58 -07002062#ifndef OPENSSL_IS_BORINGSSL
2063 BN_set_flags((BIGNUM *) a, BN_FLG_CONSTTIME);
2064#endif /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002065 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
Hai Shalomc3565922019-10-28 11:58:20 -07002073int 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
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002095int 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
Dmitry Shmidte4663042016-04-04 10:07:49 -07002104 if (TEST_FAIL())
2105 return -1;
2106
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002107 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
Hai Shalomc3565922019-10-28 11:58:20 -07002118int 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
Roshan Pius3a1667e2018-07-03 15:17:14 -07002139int 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
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002148int 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
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002155int crypto_bignum_is_zero(const struct crypto_bignum *a)
2156{
2157 return BN_is_zero((const BIGNUM *) a);
2158}
2159
2160
2161int crypto_bignum_is_one(const struct crypto_bignum *a)
2162{
2163 return BN_is_one((const BIGNUM *) a);
2164}
2165
2166
Roshan Pius3a1667e2018-07-03 15:17:14 -07002167int crypto_bignum_is_odd(const struct crypto_bignum *a)
2168{
2169 return BN_is_odd((const BIGNUM *) a);
2170}
2171
2172
Dmitry Shmidt41712582015-06-29 11:02:15 -07002173int 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;
Hai Shalom021b0b52019-04-10 11:17:58 -07002179 unsigned int mask;
Dmitry Shmidt41712582015-06-29 11:02:15 -07002180
Dmitry Shmidte4663042016-04-04 10:07:49 -07002181 if (TEST_FAIL())
2182 return -2;
2183
Dmitry Shmidt41712582015-06-29 11:02:15 -07002184 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) ||
Hai Shalom021b0b52019-04-10 11:17:58 -07002194 !BN_mod_exp_mont_consttime(tmp, (const BIGNUM *) a, exp,
2195 (const BIGNUM *) p, bnctx, NULL))
Dmitry Shmidt41712582015-06-29 11:02:15 -07002196 goto fail;
2197
Hai Shalom021b0b52019-04-10 11:17:58 -07002198 /* 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);
Dmitry Shmidt41712582015-06-29 11:02:15 -07002205
2206fail:
2207 BN_clear_free(tmp);
2208 BN_clear_free(exp);
2209 BN_CTX_free(bnctx);
2210 return res;
2211}
2212
2213
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002214#ifdef CONFIG_ECC
2215
2216struct crypto_ec {
2217 EC_GROUP *group;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002218 int nid;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002219 BN_CTX *bnctx;
2220 BIGNUM *prime;
2221 BIGNUM *order;
Dmitry Shmidt41712582015-06-29 11:02:15 -07002222 BIGNUM *a;
2223 BIGNUM *b;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002224};
2225
Hai Shaloma20dcd72022-02-04 13:43:00 -08002226
2227static 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
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002263struct crypto_ec * crypto_ec_init(int group)
2264{
2265 struct crypto_ec *e;
2266 int nid;
2267
Hai Shaloma20dcd72022-02-04 13:43:00 -08002268 nid = crypto_ec_group_2_nid(group);
2269 if (nid < 0)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002270 return NULL;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002271
2272 e = os_zalloc(sizeof(*e));
2273 if (e == NULL)
2274 return NULL;
2275
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002276 e->nid = nid;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002277 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();
Dmitry Shmidt41712582015-06-29 11:02:15 -07002281 e->a = BN_new();
2282 e->b = BN_new();
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002283 if (e->group == NULL || e->bnctx == NULL || e->prime == NULL ||
Dmitry Shmidt41712582015-06-29 11:02:15 -07002284 e->order == NULL || e->a == NULL || e->b == NULL ||
Sunil Ravia04bd252022-05-02 22:54:18 -07002285 !EC_GROUP_get_curve(e->group, e->prime, e->a, e->b, e->bnctx) ||
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002286 !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
2295void crypto_ec_deinit(struct crypto_ec *e)
2296{
2297 if (e == NULL)
2298 return;
Dmitry Shmidt41712582015-06-29 11:02:15 -07002299 BN_clear_free(e->b);
2300 BN_clear_free(e->a);
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07002301 BN_clear_free(e->order);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002302 BN_clear_free(e->prime);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002303 EC_GROUP_free(e->group);
2304 BN_CTX_free(e->bnctx);
2305 os_free(e);
2306}
2307
2308
2309struct crypto_ec_point * crypto_ec_point_init(struct crypto_ec *e)
2310{
Dmitry Shmidte4663042016-04-04 10:07:49 -07002311 if (TEST_FAIL())
2312 return NULL;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002313 if (e == NULL)
2314 return NULL;
2315 return (struct crypto_ec_point *) EC_POINT_new(e->group);
2316}
2317
2318
2319size_t crypto_ec_prime_len(struct crypto_ec *e)
2320{
2321 return BN_num_bytes(e->prime);
2322}
2323
2324
2325size_t crypto_ec_prime_len_bits(struct crypto_ec *e)
2326{
2327 return BN_num_bits(e->prime);
2328}
2329
2330
Roshan Pius3a1667e2018-07-03 15:17:14 -07002331size_t crypto_ec_order_len(struct crypto_ec *e)
2332{
2333 return BN_num_bytes(e->order);
2334}
2335
2336
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002337const struct crypto_bignum * crypto_ec_get_prime(struct crypto_ec *e)
2338{
2339 return (const struct crypto_bignum *) e->prime;
2340}
2341
2342
2343const struct crypto_bignum * crypto_ec_get_order(struct crypto_ec *e)
2344{
2345 return (const struct crypto_bignum *) e->order;
2346}
2347
2348
Hai Shalomc3565922019-10-28 11:58:20 -07002349const struct crypto_bignum * crypto_ec_get_a(struct crypto_ec *e)
2350{
2351 return (const struct crypto_bignum *) e->a;
2352}
2353
2354
2355const struct crypto_bignum * crypto_ec_get_b(struct crypto_ec *e)
2356{
2357 return (const struct crypto_bignum *) e->b;
2358}
2359
2360
Hai Shaloma20dcd72022-02-04 13:43:00 -08002361const 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
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002368void 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
Roshan Pius3a1667e2018-07-03 15:17:14 -07002377int crypto_ec_point_x(struct crypto_ec *e, const struct crypto_ec_point *p,
2378 struct crypto_bignum *x)
2379{
Sunil Ravia04bd252022-05-02 22:54:18 -07002380 return EC_POINT_get_affine_coordinates(e->group,
2381 (const EC_POINT *) p,
2382 (BIGNUM *) x, NULL,
2383 e->bnctx) == 1 ? 0 : -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07002384}
2385
2386
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002387int 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
Dmitry Shmidte4663042016-04-04 10:07:49 -07002394 if (TEST_FAIL())
2395 return -1;
2396
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002397 x_bn = BN_new();
2398 y_bn = BN_new();
2399
2400 if (x_bn && y_bn &&
Sunil Ravia04bd252022-05-02 22:54:18 -07002401 EC_POINT_get_affine_coordinates(e->group, (EC_POINT *) point,
2402 x_bn, y_bn, e->bnctx)) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002403 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
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07002414 BN_clear_free(x_bn);
2415 BN_clear_free(y_bn);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002416 return ret;
2417}
2418
2419
2420struct 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
Dmitry Shmidte4663042016-04-04 10:07:49 -07002427 if (TEST_FAIL())
2428 return NULL;
2429
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002430 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) {
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07002434 BN_clear_free(x);
2435 BN_clear_free(y);
2436 EC_POINT_clear_free(elem);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002437 return NULL;
2438 }
2439
Sunil Ravia04bd252022-05-02 22:54:18 -07002440 if (!EC_POINT_set_affine_coordinates(e->group, elem, x, y, e->bnctx)) {
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07002441 EC_POINT_clear_free(elem);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002442 elem = NULL;
2443 }
2444
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07002445 BN_clear_free(x);
2446 BN_clear_free(y);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002447
2448 return (struct crypto_ec_point *) elem;
2449}
2450
2451
2452int 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{
Dmitry Shmidte4663042016-04-04 10:07:49 -07002456 if (TEST_FAIL())
2457 return -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002458 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
2463int 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{
Dmitry Shmidte4663042016-04-04 10:07:49 -07002467 if (TEST_FAIL())
2468 return -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002469 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
2475int crypto_ec_point_invert(struct crypto_ec *e, struct crypto_ec_point *p)
2476{
Dmitry Shmidte4663042016-04-04 10:07:49 -07002477 if (TEST_FAIL())
2478 return -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002479 return EC_POINT_invert(e->group, (EC_POINT *) p, e->bnctx) ? 0 : -1;
2480}
2481
2482
Dmitry Shmidt41712582015-06-29 11:02:15 -07002483struct crypto_bignum *
2484crypto_ec_point_compute_y_sqr(struct crypto_ec *e,
2485 const struct crypto_bignum *x)
2486{
Hai Shaloma20dcd72022-02-04 13:43:00 -08002487 BIGNUM *tmp;
Dmitry Shmidt41712582015-06-29 11:02:15 -07002488
Dmitry Shmidte4663042016-04-04 10:07:49 -07002489 if (TEST_FAIL())
2490 return NULL;
2491
Dmitry Shmidt41712582015-06-29 11:02:15 -07002492 tmp = BN_new();
Dmitry Shmidt41712582015-06-29 11:02:15 -07002493
Hai Shaloma20dcd72022-02-04 13:43:00 -08002494 /* y^2 = x^3 + ax + b = (x^2 + a)x + b */
2495 if (tmp &&
Dmitry Shmidt41712582015-06-29 11:02:15 -07002496 BN_mod_sqr(tmp, (const BIGNUM *) x, e->prime, e->bnctx) &&
Hai Shaloma20dcd72022-02-04 13:43:00 -08002497 BN_mod_add_quick(tmp, e->a, tmp, e->prime) &&
Dmitry Shmidt41712582015-06-29 11:02:15 -07002498 BN_mod_mul(tmp, tmp, (const BIGNUM *) x, e->prime, e->bnctx) &&
Hai Shaloma20dcd72022-02-04 13:43:00 -08002499 BN_mod_add_quick(tmp, tmp, e->b, e->prime))
2500 return (struct crypto_bignum *) tmp;
Dmitry Shmidt41712582015-06-29 11:02:15 -07002501
2502 BN_clear_free(tmp);
Hai Shaloma20dcd72022-02-04 13:43:00 -08002503 return NULL;
Dmitry Shmidt41712582015-06-29 11:02:15 -07002504}
2505
2506
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002507int 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
2514int crypto_ec_point_is_on_curve(struct crypto_ec *e,
2515 const struct crypto_ec_point *p)
2516{
Dmitry Shmidt41712582015-06-29 11:02:15 -07002517 return EC_POINT_is_on_curve(e->group, (const EC_POINT *) p,
2518 e->bnctx) == 1;
2519}
2520
2521
2522int 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);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002528}
2529
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002530
Hai Shaloma20dcd72022-02-04 13:43:00 -08002531void 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 ||
Sunil Ravia04bd252022-05-02 22:54:18 -07002541 EC_POINT_get_affine_coordinates(e->group, (const EC_POINT *) p,
2542 x, y, e->bnctx) != 1)
Hai Shaloma20dcd72022-02-04 13:43:00 -08002543 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
2552fail:
2553 OPENSSL_free(x_str);
2554 OPENSSL_free(y_str);
2555 BN_free(x);
2556 BN_free(y);
2557}
2558
2559
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002560struct crypto_ecdh {
2561 struct crypto_ec *ec;
2562 EVP_PKEY *pkey;
2563};
2564
2565struct crypto_ecdh * crypto_ecdh_init(int group)
2566{
Sunil Ravia04bd252022-05-02 22:54:18 -07002567#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
2587done:
2588 return ecdh;
2589fail:
2590 crypto_ecdh_deinit(ecdh);
2591 ecdh = NULL;
2592 goto done;
2593#else /* OpenSSL version >= 3.0 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002594 struct crypto_ecdh *ecdh;
2595 EVP_PKEY *params = NULL;
Hai Shalom81f62d82019-07-22 12:10:00 -07002596 EC_KEY *ec_params = NULL;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002597 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
Roshan Pius3a1667e2018-07-03 15:17:14 -07002607 ec_params = EC_KEY_new_by_curve_name(ecdh->ec->nid);
2608 if (!ec_params) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002609 wpa_printf(MSG_ERROR,
Roshan Pius3a1667e2018-07-03 15:17:14 -07002610 "OpenSSL: Failed to generate EC_KEY parameters");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002611 goto fail;
2612 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07002613 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) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002616 wpa_printf(MSG_ERROR,
Roshan Pius3a1667e2018-07-03 15:17:14 -07002617 "OpenSSL: Failed to generate EVP_PKEY parameters");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002618 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
2638done:
Hai Shalom81f62d82019-07-22 12:10:00 -07002639 EC_KEY_free(ec_params);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002640 EVP_PKEY_free(params);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002641 EVP_PKEY_CTX_free(kctx);
2642
2643 return ecdh;
2644fail:
2645 crypto_ecdh_deinit(ecdh);
2646 ecdh = NULL;
2647 goto done;
Sunil Ravia04bd252022-05-02 22:54:18 -07002648#endif /* OpenSSL version >= 3.0 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002649}
2650
2651
Hai Shaloma20dcd72022-02-04 13:43:00 -08002652struct crypto_ecdh * crypto_ecdh_init2(int group, struct crypto_ec_key *own_key)
2653{
Sunil Ravia04bd252022-05-02 22:54:18 -07002654#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;
2670fail:
2671 crypto_ecdh_deinit(ecdh);
2672 return NULL;
2673#else /* OpenSSL version >= 3.0 */
Hai Shaloma20dcd72022-02-04 13:43:00 -08002674 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;
2692fail:
2693 crypto_ecdh_deinit(ecdh);
2694 return NULL;
Sunil Ravia04bd252022-05-02 22:54:18 -07002695#endif /* OpenSSL version >= 3.0 */
Hai Shaloma20dcd72022-02-04 13:43:00 -08002696}
2697
2698
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002699struct wpabuf * crypto_ecdh_get_pubkey(struct crypto_ecdh *ecdh, int inc_y)
2700{
Sunil Ravia04bd252022-05-02 22:54:18 -07002701#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);
2719fail:
2720 OPENSSL_free(pub);
2721 return buf;
2722#else /* OpenSSL version >= 3.0 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002723 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
Sunil Ravia04bd252022-05-02 22:54:18 -07002748 if (EC_POINT_get_affine_coordinates(ecdh->ec->group, pubkey,
2749 x, y, ecdh->ec->bnctx) != 1) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002750 wpa_printf(MSG_ERROR,
Sunil Ravia04bd252022-05-02 22:54:18 -07002751 "OpenSSL: EC_POINT_get_affine_coordinates failed: %s",
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002752 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
2768done:
2769 BN_clear_free(x);
2770 BN_clear_free(y);
2771 EC_KEY_free(eckey);
2772
2773 return buf;
2774fail:
2775 wpabuf_free(buf);
2776 buf = NULL;
2777 goto done;
Sunil Ravia04bd252022-05-02 22:54:18 -07002778#endif /* OpenSSL version >= 3.0 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002779}
2780
2781
2782struct wpabuf * crypto_ecdh_set_peerkey(struct crypto_ecdh *ecdh, int inc_y,
2783 const u8 *key, size_t len)
2784{
Sunil Ravia04bd252022-05-02 22:54:18 -07002785#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 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002829 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;
Sunil Ravia04bd252022-05-02 22:54:18 -07002846 if (!EC_POINT_set_affine_coordinates(ecdh->ec->group, pub,
2847 x, y, ecdh->ec->bnctx)) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002848 wpa_printf(MSG_ERROR,
Sunil Ravia04bd252022-05-02 22:54:18 -07002849 "OpenSSL: EC_POINT_set_affine_coordinates failed: %s",
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002850 ERR_error_string(ERR_get_error(), NULL));
2851 goto fail;
2852 }
Sunil Ravia04bd252022-05-02 22:54:18 -07002853 } else if (!EC_POINT_set_compressed_coordinates(ecdh->ec->group,
2854 pub, x, 0,
2855 ecdh->ec->bnctx)) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002856 wpa_printf(MSG_ERROR,
Sunil Ravia04bd252022-05-02 22:54:18 -07002857 "OpenSSL: EC_POINT_set_compressed_coordinates failed: %s",
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002858 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;
Hai Shalomc3565922019-10-28 11:58:20 -07002893 if (EVP_PKEY_derive(ctx, wpabuf_put(secret, 0), &secret_len) != 1) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002894 wpa_printf(MSG_ERROR,
2895 "OpenSSL: EVP_PKEY_derive(2) failed: %s",
2896 ERR_error_string(ERR_get_error(), NULL));
2897 goto fail;
2898 }
Hai Shalomc3565922019-10-28 11:58:20 -07002899 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);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002904
2905done:
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;
2913fail:
2914 wpabuf_free(secret);
2915 secret = NULL;
2916 goto done;
Sunil Ravia04bd252022-05-02 22:54:18 -07002917#endif /* OpenSSL version >= 3.0 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002918}
2919
2920
2921void 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
Hai Shalomfdcde762020-04-02 11:19:20 -07002930
2931size_t crypto_ecdh_prime_len(struct crypto_ecdh *ecdh)
2932{
2933 return crypto_ec_prime_len(ecdh->ec);
2934}
2935
Hai Shalom899fcc72020-10-19 14:38:18 -07002936
Hai Shalom899fcc72020-10-19 14:38:18 -07002937struct crypto_ec_key * crypto_ec_key_parse_priv(const u8 *der, size_t der_len)
2938{
Hai Shaloma20dcd72022-02-04 13:43:00 -08002939 EVP_PKEY *pkey = NULL;
2940 EC_KEY *eckey;
Hai Shalom899fcc72020-10-19 14:38:18 -07002941
Hai Shaloma20dcd72022-02-04 13:43:00 -08002942 eckey = d2i_ECPrivateKey(NULL, &der, der_len);
2943 if (!eckey) {
Hai Shalom899fcc72020-10-19 14:38:18 -07002944 wpa_printf(MSG_INFO, "OpenSSL: d2i_ECPrivateKey() failed: %s",
2945 ERR_error_string(ERR_get_error(), NULL));
2946 goto fail;
2947 }
Hai Shaloma20dcd72022-02-04 13:43:00 -08002948 EC_KEY_set_conv_form(eckey, POINT_CONVERSION_COMPRESSED);
Hai Shalom899fcc72020-10-19 14:38:18 -07002949
Hai Shaloma20dcd72022-02-04 13:43:00 -08002950 pkey = EVP_PKEY_new();
2951 if (!pkey || EVP_PKEY_assign_EC_KEY(pkey, eckey) != 1) {
2952 EC_KEY_free(eckey);
Hai Shalom899fcc72020-10-19 14:38:18 -07002953 goto fail;
2954 }
2955
Hai Shaloma20dcd72022-02-04 13:43:00 -08002956 return (struct crypto_ec_key *) pkey;
Hai Shalom899fcc72020-10-19 14:38:18 -07002957fail:
Hai Shaloma20dcd72022-02-04 13:43:00 -08002958 crypto_ec_key_deinit((struct crypto_ec_key *) pkey);
Hai Shalom899fcc72020-10-19 14:38:18 -07002959 return NULL;
2960}
2961
2962
2963struct crypto_ec_key * crypto_ec_key_parse_pub(const u8 *der, size_t der_len)
2964{
Hai Shaloma20dcd72022-02-04 13:43:00 -08002965 EVP_PKEY *pkey;
Hai Shalom899fcc72020-10-19 14:38:18 -07002966
Hai Shaloma20dcd72022-02-04 13:43:00 -08002967 pkey = d2i_PUBKEY(NULL, &der, der_len);
2968 if (!pkey) {
Hai Shalom899fcc72020-10-19 14:38:18 -07002969 wpa_printf(MSG_INFO, "OpenSSL: d2i_PUBKEY() failed: %s",
2970 ERR_error_string(ERR_get_error(), NULL));
2971 goto fail;
2972 }
2973
Hai Shaloma20dcd72022-02-04 13:43:00 -08002974 /* Ensure this is an EC key */
2975 if (!EVP_PKEY_get0_EC_KEY(pkey))
Hai Shalom899fcc72020-10-19 14:38:18 -07002976 goto fail;
Hai Shaloma20dcd72022-02-04 13:43:00 -08002977 return (struct crypto_ec_key *) pkey;
Hai Shalom899fcc72020-10-19 14:38:18 -07002978fail:
Hai Shaloma20dcd72022-02-04 13:43:00 -08002979 crypto_ec_key_deinit((struct crypto_ec_key *) pkey);
Hai Shalom899fcc72020-10-19 14:38:18 -07002980 return NULL;
2981}
2982
2983
Hai Shaloma20dcd72022-02-04 13:43:00 -08002984struct 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
Sunil Ravia04bd252022-05-02 22:54:18 -07003018 if (!EC_POINT_set_affine_coordinates(ec_group, point, x, y, ctx)) {
Hai Shaloma20dcd72022-02-04 13:43:00 -08003019 wpa_printf(MSG_ERROR,
Sunil Ravia04bd252022-05-02 22:54:18 -07003020 "OpenSSL: EC_POINT_set_affine_coordinates failed: %s",
Hai Shaloma20dcd72022-02-04 13:43:00 -08003021 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
3048out:
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
3056fail:
3057 EC_KEY_free(eckey);
3058 EVP_PKEY_free(pkey);
3059 pkey = NULL;
3060 goto out;
3061}
3062
3063
3064struct crypto_ec_key *
3065crypto_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
3088out:
3089 return (struct crypto_ec_key *) pkey;
3090
3091fail:
3092 EVP_PKEY_free(pkey);
3093 EC_KEY_free(eckey);
3094 pkey = NULL;
3095 goto out;
3096}
3097
3098
3099struct 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
3143fail:
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
Hai Shalom899fcc72020-10-19 14:38:18 -07003151void crypto_ec_key_deinit(struct crypto_ec_key *key)
3152{
Hai Shaloma20dcd72022-02-04 13:43:00 -08003153 EVP_PKEY_free((EVP_PKEY *) key);
Hai Shalom899fcc72020-10-19 14:38:18 -07003154}
3155
3156
Hai Shaloma20dcd72022-02-04 13:43:00 -08003157#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
3166typedef 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
3174ASN1_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
3179IMPLEMENT_ASN1_FUNCTIONS(EC_COMP_PUBKEY);
3180
3181#endif /* OPENSSL_IS_BORINGSSL */
3182
3183
Hai Shalom899fcc72020-10-19 14:38:18 -07003184struct wpabuf * crypto_ec_key_get_subject_public_key(struct crypto_ec_key *key)
3185{
Hai Shaloma20dcd72022-02-04 13:43:00 -08003186#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);
3242fail:
3243 EC_COMP_PUBKEY_free(pubkey);
3244 OPENSSL_free(der);
3245 BN_CTX_free(ctx);
3246 return ret;
3247#else /* OPENSSL_IS_BORINGSSL */
Hai Shalom899fcc72020-10-19 14:38:18 -07003248 unsigned char *der = NULL;
3249 int der_len;
3250 struct wpabuf *buf;
Hai Shaloma20dcd72022-02-04 13:43:00 -08003251 EC_KEY *eckey;
3252#if OPENSSL_VERSION_NUMBER >= 0x30000000L
3253 EVP_PKEY *tmp;
3254#endif /* OpenSSL version >= 3.0 */
Hai Shalom899fcc72020-10-19 14:38:18 -07003255
Hai Shaloma20dcd72022-02-04 13:43:00 -08003256 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 */
Hai Shalom899fcc72020-10-19 14:38:18 -07003279 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;
Hai Shaloma20dcd72022-02-04 13:43:00 -08003288#endif /* OPENSSL_IS_BORINGSSL */
3289}
3290
3291
3292struct 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
3325struct 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
3373const struct crypto_ec_point *
3374crypto_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
3385const struct crypto_bignum *
3386crypto_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);
Hai Shalom899fcc72020-10-19 14:38:18 -07003394}
3395
3396
3397struct 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
Hai Shaloma20dcd72022-02-04 13:43:00 -08003404 sig_len = EVP_PKEY_size((EVP_PKEY *) key);
Hai Shalom899fcc72020-10-19 14:38:18 -07003405 sig_der = wpabuf_alloc(sig_len);
3406 if (!sig_der)
3407 return NULL;
3408
Hai Shaloma20dcd72022-02-04 13:43:00 -08003409 pkctx = EVP_PKEY_CTX_new((EVP_PKEY *) key, NULL);
Hai Shalom899fcc72020-10-19 14:38:18 -07003410 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
Sunil Ravia04bd252022-05-02 22:54:18 -07003425static int openssl_evp_pkey_ec_prime_len(struct crypto_ec_key *key)
Hai Shaloma20dcd72022-02-04 13:43:00 -08003426{
Sunil Ravia04bd252022-05-02 22:54:18 -07003427#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
Hai Shaloma20dcd72022-02-04 13:43:00 -08003448 const EC_GROUP *group;
3449 const EC_KEY *eckey;
3450 BIGNUM *prime = NULL;
Sunil Ravia04bd252022-05-02 22:54:18 -07003451 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);
3462fail:
3463 BN_free(prime);
3464 return prime_len;
3465#endif
3466}
3467
3468
3469struct wpabuf * crypto_ec_key_sign_r_s(struct crypto_ec_key *key,
3470 const u8 *data, size_t len)
3471{
Hai Shaloma20dcd72022-02-04 13:43:00 -08003472 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
Sunil Ravia04bd252022-05-02 22:54:18 -07003479 prime_len = openssl_evp_pkey_ec_prime_len(key);
3480 if (prime_len < 0)
3481 return NULL;
3482
Hai Shaloma20dcd72022-02-04 13:43:00 -08003483 buf = crypto_ec_key_sign(key, data, len);
3484 if (!buf)
3485 return NULL;
3486
3487 /* Extract (r,s) from Ecdsa-Sig-Value */
Hai Shaloma20dcd72022-02-04 13:43:00 -08003488
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
3505out:
Hai Shaloma20dcd72022-02-04 13:43:00 -08003506 ECDSA_SIG_free(sig);
3507 return buf;
3508fail:
3509 wpabuf_clear_free(buf);
3510 buf = NULL;
3511 goto out;
3512}
3513
3514
Hai Shalom899fcc72020-10-19 14:38:18 -07003515int 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
Hai Shaloma20dcd72022-02-04 13:43:00 -08003521 pkctx = EVP_PKEY_CTX_new((EVP_PKEY *) key, NULL);
Hai Shalom899fcc72020-10-19 14:38:18 -07003522 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
Hai Shaloma20dcd72022-02-04 13:43:00 -08003537int 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
3565fail:
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
Hai Shalom899fcc72020-10-19 14:38:18 -07003574int crypto_ec_key_group(struct crypto_ec_key *key)
3575{
Sunil Ravia04bd252022-05-02 22:54:18 -07003576#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
Hai Shaloma20dcd72022-02-04 13:43:00 -08003585 const EC_KEY *eckey;
Hai Shalom899fcc72020-10-19 14:38:18 -07003586 const EC_GROUP *group;
3587 int nid;
3588
Hai Shaloma20dcd72022-02-04 13:43:00 -08003589 eckey = EVP_PKEY_get0_EC_KEY((EVP_PKEY *) key);
3590 if (!eckey)
3591 return -1;
3592 group = EC_KEY_get0_group(eckey);
Hai Shalom899fcc72020-10-19 14:38:18 -07003593 if (!group)
3594 return -1;
3595 nid = EC_GROUP_get_curve_name(group);
Sunil Ravia04bd252022-05-02 22:54:18 -07003596#endif
Hai Shalom899fcc72020-10-19 14:38:18 -07003597 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;
Hai Shaloma20dcd72022-02-04 13:43:00 -08003604#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 */
Hai Shalom899fcc72020-10-19 14:38:18 -07003616 }
Hai Shaloma20dcd72022-02-04 13:43:00 -08003617 wpa_printf(MSG_ERROR, "OpenSSL: Unsupported curve (nid=%d) in EC key",
3618 nid);
Hai Shalom899fcc72020-10-19 14:38:18 -07003619 return -1;
3620}
3621
Hai Shaloma20dcd72022-02-04 13:43:00 -08003622
3623int crypto_ec_key_cmp(struct crypto_ec_key *key1, struct crypto_ec_key *key2)
3624{
Sunil Ravia04bd252022-05-02 22:54:18 -07003625#if OPENSSL_VERSION_NUMBER >= 0x30000000L
3626 if (EVP_PKEY_eq((EVP_PKEY *) key1, (EVP_PKEY *) key2) != 1)
3627 return -1;
3628#else
Hai Shaloma20dcd72022-02-04 13:43:00 -08003629 if (EVP_PKEY_cmp((EVP_PKEY *) key1, (EVP_PKEY *) key2) != 1)
3630 return -1;
Sunil Ravia04bd252022-05-02 22:54:18 -07003631#endif
Hai Shaloma20dcd72022-02-04 13:43:00 -08003632 return 0;
3633}
3634
3635
3636void 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
3663struct 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
3739fail:
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
3753struct crypto_csr * crypto_csr_init()
3754{
3755 return (struct crypto_csr *)X509_REQ_new();
3756}
3757
3758
3759struct 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;
3777fail:
3778 X509_REQ_free(csr);
3779 return NULL;
3780}
3781
3782
3783void crypto_csr_deinit(struct crypto_csr *csr)
3784{
3785 X509_REQ_free((X509_REQ *)csr);
3786}
3787
3788
3789int 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
3798int 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
3844int 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
3865const 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
3903struct 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
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003939#endif /* CONFIG_ECC */
Sunil Ravia04bd252022-05-02 22:54:18 -07003940
3941
3942static 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
3970struct 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
3989struct 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
4016fail:
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
4026struct 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
4053fail:
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
4065void crypto_rsa_key_free(struct crypto_rsa_key *key)
4066{
4067 EVP_PKEY_free((EVP_PKEY *) key);
4068}
4069
4070
4071void crypto_unload(void)
4072{
4073 openssl_unload_legacy_provider();
4074}