blob: f4cff43f364f86acf6128cbaf06c04b3ebbe0b06 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002 * Wrapper functions for OpenSSL libcrypto
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003 * Copyright (c) 2004-2017, 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>
19#ifdef CONFIG_OPENSSL_CMAC
20#include <openssl/cmac.h>
21#endif /* CONFIG_OPENSSL_CMAC */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080022#ifdef CONFIG_ECC
23#include <openssl/ec.h>
24#endif /* CONFIG_ECC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070025
26#include "common.h"
27#include "wpabuf.h"
28#include "dh_group5.h"
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080029#include "sha1.h"
30#include "sha256.h"
Dmitry Shmidt807291d2015-01-27 13:40:23 -080031#include "sha384.h"
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -070032#include "md5.h"
33#include "aes_wrap.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070034#include "crypto.h"
35
Roshan Pius3a1667e2018-07-03 15:17:14 -070036#if OPENSSL_VERSION_NUMBER < 0x10100000L || \
37 (defined(LIBRESSL_VERSION_NUMBER) && \
38 LIBRESSL_VERSION_NUMBER < 0x20700000L)
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -080039/* Compatibility wrappers for older versions. */
40
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -080041static HMAC_CTX * HMAC_CTX_new(void)
42{
43 HMAC_CTX *ctx;
44
45 ctx = os_zalloc(sizeof(*ctx));
46 if (ctx)
47 HMAC_CTX_init(ctx);
48 return ctx;
49}
50
51
52static void HMAC_CTX_free(HMAC_CTX *ctx)
53{
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -070054 if (!ctx)
55 return;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -080056 HMAC_CTX_cleanup(ctx);
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -080057 bin_clear_free(ctx, sizeof(*ctx));
58}
59
60
61static EVP_MD_CTX * EVP_MD_CTX_new(void)
62{
63 EVP_MD_CTX *ctx;
64
65 ctx = os_zalloc(sizeof(*ctx));
66 if (ctx)
67 EVP_MD_CTX_init(ctx);
68 return ctx;
69}
70
71
72static void EVP_MD_CTX_free(EVP_MD_CTX *ctx)
73{
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -070074 if (!ctx)
75 return;
76 EVP_MD_CTX_cleanup(ctx);
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -080077 bin_clear_free(ctx, sizeof(*ctx));
78}
79
80#endif /* OpenSSL version < 1.1.0 */
81
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070082static BIGNUM * get_group5_prime(void)
83{
Roshan Pius3a1667e2018-07-03 15:17:14 -070084#if OPENSSL_VERSION_NUMBER >= 0x10100000L && \
85 !(defined(LIBRESSL_VERSION_NUMBER) && \
86 LIBRESSL_VERSION_NUMBER < 0x20700000L)
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -070087 return BN_get_rfc3526_prime_1536(NULL);
88#elif !defined(OPENSSL_IS_BORINGSSL)
89 return get_rfc3526_prime_1536(NULL);
90#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070091 static const unsigned char RFC3526_PRIME_1536[] = {
92 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xC9,0x0F,0xDA,0xA2,
93 0x21,0x68,0xC2,0x34,0xC4,0xC6,0x62,0x8B,0x80,0xDC,0x1C,0xD1,
94 0x29,0x02,0x4E,0x08,0x8A,0x67,0xCC,0x74,0x02,0x0B,0xBE,0xA6,
95 0x3B,0x13,0x9B,0x22,0x51,0x4A,0x08,0x79,0x8E,0x34,0x04,0xDD,
96 0xEF,0x95,0x19,0xB3,0xCD,0x3A,0x43,0x1B,0x30,0x2B,0x0A,0x6D,
97 0xF2,0x5F,0x14,0x37,0x4F,0xE1,0x35,0x6D,0x6D,0x51,0xC2,0x45,
98 0xE4,0x85,0xB5,0x76,0x62,0x5E,0x7E,0xC6,0xF4,0x4C,0x42,0xE9,
99 0xA6,0x37,0xED,0x6B,0x0B,0xFF,0x5C,0xB6,0xF4,0x06,0xB7,0xED,
100 0xEE,0x38,0x6B,0xFB,0x5A,0x89,0x9F,0xA5,0xAE,0x9F,0x24,0x11,
101 0x7C,0x4B,0x1F,0xE6,0x49,0x28,0x66,0x51,0xEC,0xE4,0x5B,0x3D,
102 0xC2,0x00,0x7C,0xB8,0xA1,0x63,0xBF,0x05,0x98,0xDA,0x48,0x36,
103 0x1C,0x55,0xD3,0x9A,0x69,0x16,0x3F,0xA8,0xFD,0x24,0xCF,0x5F,
104 0x83,0x65,0x5D,0x23,0xDC,0xA3,0xAD,0x96,0x1C,0x62,0xF3,0x56,
105 0x20,0x85,0x52,0xBB,0x9E,0xD5,0x29,0x07,0x70,0x96,0x96,0x6D,
106 0x67,0x0C,0x35,0x4E,0x4A,0xBC,0x98,0x04,0xF1,0x74,0x6C,0x08,
107 0xCA,0x23,0x73,0x27,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
108 };
109 return BN_bin2bn(RFC3526_PRIME_1536, sizeof(RFC3526_PRIME_1536), NULL);
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -0700110#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700111}
112
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700113#ifdef OPENSSL_NO_SHA256
114#define NO_SHA256_WRAPPER
115#endif
Paul Stewart092955c2017-02-06 09:13:09 -0800116#ifdef OPENSSL_NO_SHA512
117#define NO_SHA384_WRAPPER
118#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700119
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700120static int openssl_digest_vector(const EVP_MD *type, size_t num_elem,
121 const u8 *addr[], const size_t *len, u8 *mac)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700122{
Dmitry Shmidt55840ad2015-12-14 12:45:46 -0800123 EVP_MD_CTX *ctx;
124 size_t i;
125 unsigned int mac_len;
126
127 if (TEST_FAIL())
128 return -1;
129
130 ctx = EVP_MD_CTX_new();
131 if (!ctx)
132 return -1;
133 if (!EVP_DigestInit_ex(ctx, type, NULL)) {
134 wpa_printf(MSG_ERROR, "OpenSSL: EVP_DigestInit_ex failed: %s",
135 ERR_error_string(ERR_get_error(), NULL));
136 EVP_MD_CTX_free(ctx);
137 return -1;
138 }
139 for (i = 0; i < num_elem; i++) {
140 if (!EVP_DigestUpdate(ctx, addr[i], len[i])) {
141 wpa_printf(MSG_ERROR, "OpenSSL: EVP_DigestUpdate "
142 "failed: %s",
143 ERR_error_string(ERR_get_error(), NULL));
144 EVP_MD_CTX_free(ctx);
145 return -1;
146 }
147 }
148 if (!EVP_DigestFinal(ctx, mac, &mac_len)) {
149 wpa_printf(MSG_ERROR, "OpenSSL: EVP_DigestFinal failed: %s",
150 ERR_error_string(ERR_get_error(), NULL));
151 EVP_MD_CTX_free(ctx);
152 return -1;
153 }
154 EVP_MD_CTX_free(ctx);
155
156 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700157}
158
159
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800160#ifndef CONFIG_FIPS
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700161int md4_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
162{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700163 return openssl_digest_vector(EVP_md4(), num_elem, addr, len, mac);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700164}
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800165#endif /* CONFIG_FIPS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700166
167
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700168int des_encrypt(const u8 *clear, const u8 *key, u8 *cypher)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700169{
170 u8 pkey[8], next, tmp;
171 int i;
172 DES_key_schedule ks;
173
174 /* Add parity bits to the key */
175 next = 0;
176 for (i = 0; i < 7; i++) {
177 tmp = key[i];
178 pkey[i] = (tmp >> i) | next | 1;
179 next = tmp << (7 - i);
180 }
181 pkey[i] = next | 1;
182
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -0700183 DES_set_key((DES_cblock *) &pkey, &ks);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700184 DES_ecb_encrypt((DES_cblock *) clear, (DES_cblock *) cypher, &ks,
185 DES_ENCRYPT);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700186 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700187}
188
189
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800190#ifndef CONFIG_NO_RC4
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700191int rc4_skip(const u8 *key, size_t keylen, size_t skip,
192 u8 *data, size_t data_len)
193{
194#ifdef OPENSSL_NO_RC4
195 return -1;
196#else /* OPENSSL_NO_RC4 */
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800197 EVP_CIPHER_CTX *ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700198 int outl;
199 int res = -1;
200 unsigned char skip_buf[16];
201
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800202 ctx = EVP_CIPHER_CTX_new();
203 if (!ctx ||
204 !EVP_CIPHER_CTX_set_padding(ctx, 0) ||
205 !EVP_CipherInit_ex(ctx, EVP_rc4(), NULL, NULL, NULL, 1) ||
206 !EVP_CIPHER_CTX_set_key_length(ctx, keylen) ||
207 !EVP_CipherInit_ex(ctx, NULL, NULL, key, NULL, 1))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700208 goto out;
209
210 while (skip >= sizeof(skip_buf)) {
211 size_t len = skip;
212 if (len > sizeof(skip_buf))
213 len = sizeof(skip_buf);
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800214 if (!EVP_CipherUpdate(ctx, skip_buf, &outl, skip_buf, len))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700215 goto out;
216 skip -= len;
217 }
218
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800219 if (EVP_CipherUpdate(ctx, data, &outl, data, data_len))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700220 res = 0;
221
222out:
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800223 if (ctx)
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800224 EVP_CIPHER_CTX_free(ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700225 return res;
226#endif /* OPENSSL_NO_RC4 */
227}
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800228#endif /* CONFIG_NO_RC4 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700229
230
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800231#ifndef CONFIG_FIPS
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700232int md5_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
233{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700234 return openssl_digest_vector(EVP_md5(), num_elem, addr, len, mac);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700235}
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800236#endif /* CONFIG_FIPS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700237
238
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700239int sha1_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
240{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700241 return openssl_digest_vector(EVP_sha1(), num_elem, addr, len, mac);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700242}
243
244
245#ifndef NO_SHA256_WRAPPER
246int sha256_vector(size_t num_elem, const u8 *addr[], const size_t *len,
247 u8 *mac)
248{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700249 return openssl_digest_vector(EVP_sha256(), num_elem, addr, len, mac);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700250}
251#endif /* NO_SHA256_WRAPPER */
252
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700253
Paul Stewart092955c2017-02-06 09:13:09 -0800254#ifndef NO_SHA384_WRAPPER
255int sha384_vector(size_t num_elem, const u8 *addr[], const size_t *len,
256 u8 *mac)
257{
258 return openssl_digest_vector(EVP_sha384(), num_elem, addr, len, mac);
259}
260#endif /* NO_SHA384_WRAPPER */
261
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700262
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700263#ifndef NO_SHA512_WRAPPER
264int sha512_vector(size_t num_elem, const u8 *addr[], const size_t *len,
265 u8 *mac)
266{
267 return openssl_digest_vector(EVP_sha512(), num_elem, addr, len, mac);
268}
269#endif /* NO_SHA512_WRAPPER */
270
271
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700272static const EVP_CIPHER * aes_get_evp_cipher(size_t keylen)
273{
274 switch (keylen) {
275 case 16:
276 return EVP_aes_128_ecb();
277 case 24:
278 return EVP_aes_192_ecb();
279 case 32:
280 return EVP_aes_256_ecb();
281 }
282
283 return NULL;
284}
285
286
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700287void * aes_encrypt_init(const u8 *key, size_t len)
288{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700289 EVP_CIPHER_CTX *ctx;
290 const EVP_CIPHER *type;
291
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800292 if (TEST_FAIL())
293 return NULL;
294
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700295 type = aes_get_evp_cipher(len);
Roshan Pius3a1667e2018-07-03 15:17:14 -0700296 if (!type) {
297 wpa_printf(MSG_INFO, "%s: Unsupported len=%u",
298 __func__, (unsigned int) len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700299 return NULL;
Roshan Pius3a1667e2018-07-03 15:17:14 -0700300 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700301
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800302 ctx = EVP_CIPHER_CTX_new();
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700303 if (ctx == NULL)
304 return NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700305 if (EVP_EncryptInit_ex(ctx, type, NULL, key, NULL) != 1) {
306 os_free(ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700307 return NULL;
308 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700309 EVP_CIPHER_CTX_set_padding(ctx, 0);
310 return ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700311}
312
313
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700314int aes_encrypt(void *ctx, const u8 *plain, u8 *crypt)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700315{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700316 EVP_CIPHER_CTX *c = ctx;
317 int clen = 16;
318 if (EVP_EncryptUpdate(c, crypt, &clen, plain, 16) != 1) {
319 wpa_printf(MSG_ERROR, "OpenSSL: EVP_EncryptUpdate failed: %s",
320 ERR_error_string(ERR_get_error(), NULL));
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700321 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700322 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700323 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700324}
325
326
327void aes_encrypt_deinit(void *ctx)
328{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700329 EVP_CIPHER_CTX *c = ctx;
330 u8 buf[16];
331 int len = sizeof(buf);
332 if (EVP_EncryptFinal_ex(c, buf, &len) != 1) {
333 wpa_printf(MSG_ERROR, "OpenSSL: EVP_EncryptFinal_ex failed: "
334 "%s", ERR_error_string(ERR_get_error(), NULL));
335 }
336 if (len != 0) {
337 wpa_printf(MSG_ERROR, "OpenSSL: Unexpected padding length %d "
338 "in AES encrypt", len);
339 }
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800340 EVP_CIPHER_CTX_free(c);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700341}
342
343
344void * aes_decrypt_init(const u8 *key, size_t len)
345{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700346 EVP_CIPHER_CTX *ctx;
347 const EVP_CIPHER *type;
348
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800349 if (TEST_FAIL())
350 return NULL;
351
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700352 type = aes_get_evp_cipher(len);
Roshan Pius3a1667e2018-07-03 15:17:14 -0700353 if (!type) {
354 wpa_printf(MSG_INFO, "%s: Unsupported len=%u",
355 __func__, (unsigned int) len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700356 return NULL;
Roshan Pius3a1667e2018-07-03 15:17:14 -0700357 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700358
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800359 ctx = EVP_CIPHER_CTX_new();
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700360 if (ctx == NULL)
361 return NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700362 if (EVP_DecryptInit_ex(ctx, type, NULL, key, NULL) != 1) {
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800363 EVP_CIPHER_CTX_free(ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700364 return NULL;
365 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700366 EVP_CIPHER_CTX_set_padding(ctx, 0);
367 return ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700368}
369
370
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700371int aes_decrypt(void *ctx, const u8 *crypt, u8 *plain)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700372{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700373 EVP_CIPHER_CTX *c = ctx;
374 int plen = 16;
375 if (EVP_DecryptUpdate(c, plain, &plen, crypt, 16) != 1) {
376 wpa_printf(MSG_ERROR, "OpenSSL: EVP_DecryptUpdate failed: %s",
377 ERR_error_string(ERR_get_error(), NULL));
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700378 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700379 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700380 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700381}
382
383
384void aes_decrypt_deinit(void *ctx)
385{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700386 EVP_CIPHER_CTX *c = ctx;
387 u8 buf[16];
388 int len = sizeof(buf);
389 if (EVP_DecryptFinal_ex(c, buf, &len) != 1) {
390 wpa_printf(MSG_ERROR, "OpenSSL: EVP_DecryptFinal_ex failed: "
391 "%s", ERR_error_string(ERR_get_error(), NULL));
392 }
393 if (len != 0) {
394 wpa_printf(MSG_ERROR, "OpenSSL: Unexpected padding length %d "
395 "in AES decrypt", len);
396 }
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800397 EVP_CIPHER_CTX_free(c);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700398}
399
400
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800401#ifndef CONFIG_FIPS
402#ifndef CONFIG_OPENSSL_INTERNAL_AES_WRAP
403
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800404int aes_wrap(const u8 *kek, size_t kek_len, int n, const u8 *plain, u8 *cipher)
405{
406 AES_KEY actx;
407 int res;
408
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800409 if (TEST_FAIL())
410 return -1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800411 if (AES_set_encrypt_key(kek, kek_len << 3, &actx))
412 return -1;
413 res = AES_wrap_key(&actx, NULL, cipher, plain, n * 8);
414 OPENSSL_cleanse(&actx, sizeof(actx));
415 return res <= 0 ? -1 : 0;
416}
417
418
419int aes_unwrap(const u8 *kek, size_t kek_len, int n, const u8 *cipher,
420 u8 *plain)
421{
422 AES_KEY actx;
423 int res;
424
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800425 if (TEST_FAIL())
426 return -1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800427 if (AES_set_decrypt_key(kek, kek_len << 3, &actx))
428 return -1;
429 res = AES_unwrap_key(&actx, NULL, plain, cipher, (n + 1) * 8);
430 OPENSSL_cleanse(&actx, sizeof(actx));
431 return res <= 0 ? -1 : 0;
432}
433
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800434#endif /* CONFIG_OPENSSL_INTERNAL_AES_WRAP */
435#endif /* CONFIG_FIPS */
436
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800437
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -0700438int aes_128_cbc_encrypt(const u8 *key, const u8 *iv, u8 *data, size_t data_len)
439{
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800440 EVP_CIPHER_CTX *ctx;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -0700441 int clen, len;
442 u8 buf[16];
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800443 int res = -1;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -0700444
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -0800445 if (TEST_FAIL())
446 return -1;
447
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800448 ctx = EVP_CIPHER_CTX_new();
449 if (!ctx)
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -0700450 return -1;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -0700451 clen = data_len;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -0700452 len = sizeof(buf);
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800453 if (EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv) == 1 &&
454 EVP_CIPHER_CTX_set_padding(ctx, 0) == 1 &&
455 EVP_EncryptUpdate(ctx, data, &clen, data, data_len) == 1 &&
456 clen == (int) data_len &&
457 EVP_EncryptFinal_ex(ctx, buf, &len) == 1 && len == 0)
458 res = 0;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800459 EVP_CIPHER_CTX_free(ctx);
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -0700460
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800461 return res;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -0700462}
463
464
465int aes_128_cbc_decrypt(const u8 *key, const u8 *iv, u8 *data, size_t data_len)
466{
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800467 EVP_CIPHER_CTX *ctx;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -0700468 int plen, len;
469 u8 buf[16];
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800470 int res = -1;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -0700471
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -0800472 if (TEST_FAIL())
473 return -1;
474
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800475 ctx = EVP_CIPHER_CTX_new();
476 if (!ctx)
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -0700477 return -1;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -0700478 plen = data_len;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -0700479 len = sizeof(buf);
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800480 if (EVP_DecryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv) == 1 &&
481 EVP_CIPHER_CTX_set_padding(ctx, 0) == 1 &&
482 EVP_DecryptUpdate(ctx, data, &plen, data, data_len) == 1 &&
483 plen == (int) data_len &&
484 EVP_DecryptFinal_ex(ctx, buf, &len) == 1 && len == 0)
485 res = 0;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800486 EVP_CIPHER_CTX_free(ctx);
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -0700487
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800488 return res;
489
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -0700490}
491
492
Roshan Pius3a1667e2018-07-03 15:17:14 -0700493int crypto_dh_init(u8 generator, const u8 *prime, size_t prime_len, u8 *privkey,
494 u8 *pubkey)
495{
496 size_t pubkey_len, pad;
497
498 if (os_get_random(privkey, prime_len) < 0)
499 return -1;
500 if (os_memcmp(privkey, prime, prime_len) > 0) {
501 /* Make sure private value is smaller than prime */
502 privkey[0] = 0;
503 }
504
505 pubkey_len = prime_len;
506 if (crypto_mod_exp(&generator, 1, privkey, prime_len, prime, prime_len,
507 pubkey, &pubkey_len) < 0)
508 return -1;
509 if (pubkey_len < prime_len) {
510 pad = prime_len - pubkey_len;
511 os_memmove(pubkey + pad, pubkey, pubkey_len);
512 os_memset(pubkey, 0, pad);
513 }
514
515 return 0;
516}
517
518
519int crypto_dh_derive_secret(u8 generator, const u8 *prime, size_t prime_len,
520 const u8 *privkey, size_t privkey_len,
521 const u8 *pubkey, size_t pubkey_len,
522 u8 *secret, size_t *len)
523{
524 return crypto_mod_exp(pubkey, pubkey_len, privkey, privkey_len,
525 prime, prime_len, secret, len);
526}
527
528
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700529int crypto_mod_exp(const u8 *base, size_t base_len,
530 const u8 *power, size_t power_len,
531 const u8 *modulus, size_t modulus_len,
532 u8 *result, size_t *result_len)
533{
534 BIGNUM *bn_base, *bn_exp, *bn_modulus, *bn_result;
535 int ret = -1;
536 BN_CTX *ctx;
537
538 ctx = BN_CTX_new();
539 if (ctx == NULL)
540 return -1;
541
542 bn_base = BN_bin2bn(base, base_len, NULL);
543 bn_exp = BN_bin2bn(power, power_len, NULL);
544 bn_modulus = BN_bin2bn(modulus, modulus_len, NULL);
545 bn_result = BN_new();
546
547 if (bn_base == NULL || bn_exp == NULL || bn_modulus == NULL ||
548 bn_result == NULL)
549 goto error;
550
551 if (BN_mod_exp(bn_result, bn_base, bn_exp, bn_modulus, ctx) != 1)
552 goto error;
553
554 *result_len = BN_bn2bin(bn_result, result);
555 ret = 0;
556
557error:
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -0700558 BN_clear_free(bn_base);
559 BN_clear_free(bn_exp);
560 BN_clear_free(bn_modulus);
561 BN_clear_free(bn_result);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700562 BN_CTX_free(ctx);
563 return ret;
564}
565
566
567struct crypto_cipher {
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800568 EVP_CIPHER_CTX *enc;
569 EVP_CIPHER_CTX *dec;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700570};
571
572
573struct crypto_cipher * crypto_cipher_init(enum crypto_cipher_alg alg,
574 const u8 *iv, const u8 *key,
575 size_t key_len)
576{
577 struct crypto_cipher *ctx;
578 const EVP_CIPHER *cipher;
579
580 ctx = os_zalloc(sizeof(*ctx));
581 if (ctx == NULL)
582 return NULL;
583
584 switch (alg) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800585#ifndef CONFIG_NO_RC4
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700586#ifndef OPENSSL_NO_RC4
587 case CRYPTO_CIPHER_ALG_RC4:
588 cipher = EVP_rc4();
589 break;
590#endif /* OPENSSL_NO_RC4 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800591#endif /* CONFIG_NO_RC4 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700592#ifndef OPENSSL_NO_AES
593 case CRYPTO_CIPHER_ALG_AES:
594 switch (key_len) {
595 case 16:
596 cipher = EVP_aes_128_cbc();
597 break;
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -0700598#ifndef OPENSSL_IS_BORINGSSL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700599 case 24:
600 cipher = EVP_aes_192_cbc();
601 break;
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -0700602#endif /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700603 case 32:
604 cipher = EVP_aes_256_cbc();
605 break;
606 default:
607 os_free(ctx);
608 return NULL;
609 }
610 break;
611#endif /* OPENSSL_NO_AES */
612#ifndef OPENSSL_NO_DES
613 case CRYPTO_CIPHER_ALG_3DES:
614 cipher = EVP_des_ede3_cbc();
615 break;
616 case CRYPTO_CIPHER_ALG_DES:
617 cipher = EVP_des_cbc();
618 break;
619#endif /* OPENSSL_NO_DES */
620#ifndef OPENSSL_NO_RC2
621 case CRYPTO_CIPHER_ALG_RC2:
622 cipher = EVP_rc2_ecb();
623 break;
624#endif /* OPENSSL_NO_RC2 */
625 default:
626 os_free(ctx);
627 return NULL;
628 }
629
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800630 if (!(ctx->enc = EVP_CIPHER_CTX_new()) ||
631 !EVP_CIPHER_CTX_set_padding(ctx->enc, 0) ||
632 !EVP_EncryptInit_ex(ctx->enc, cipher, NULL, NULL, NULL) ||
633 !EVP_CIPHER_CTX_set_key_length(ctx->enc, key_len) ||
634 !EVP_EncryptInit_ex(ctx->enc, NULL, NULL, key, iv)) {
635 if (ctx->enc)
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800636 EVP_CIPHER_CTX_free(ctx->enc);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700637 os_free(ctx);
638 return NULL;
639 }
640
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800641 if (!(ctx->dec = EVP_CIPHER_CTX_new()) ||
642 !EVP_CIPHER_CTX_set_padding(ctx->dec, 0) ||
643 !EVP_DecryptInit_ex(ctx->dec, cipher, NULL, NULL, NULL) ||
644 !EVP_CIPHER_CTX_set_key_length(ctx->dec, key_len) ||
645 !EVP_DecryptInit_ex(ctx->dec, NULL, NULL, key, iv)) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800646 EVP_CIPHER_CTX_free(ctx->enc);
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800647 if (ctx->dec)
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800648 EVP_CIPHER_CTX_free(ctx->dec);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700649 os_free(ctx);
650 return NULL;
651 }
652
653 return ctx;
654}
655
656
657int crypto_cipher_encrypt(struct crypto_cipher *ctx, const u8 *plain,
658 u8 *crypt, size_t len)
659{
660 int outl;
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800661 if (!EVP_EncryptUpdate(ctx->enc, crypt, &outl, plain, len))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700662 return -1;
663 return 0;
664}
665
666
667int crypto_cipher_decrypt(struct crypto_cipher *ctx, const u8 *crypt,
668 u8 *plain, size_t len)
669{
670 int outl;
671 outl = len;
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800672 if (!EVP_DecryptUpdate(ctx->dec, plain, &outl, crypt, len))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700673 return -1;
674 return 0;
675}
676
677
678void crypto_cipher_deinit(struct crypto_cipher *ctx)
679{
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800680 EVP_CIPHER_CTX_free(ctx->enc);
681 EVP_CIPHER_CTX_free(ctx->dec);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700682 os_free(ctx);
683}
684
685
686void * dh5_init(struct wpabuf **priv, struct wpabuf **publ)
687{
Roshan Pius3a1667e2018-07-03 15:17:14 -0700688#if OPENSSL_VERSION_NUMBER < 0x10100000L || \
689 (defined(LIBRESSL_VERSION_NUMBER) && \
690 LIBRESSL_VERSION_NUMBER < 0x20700000L)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700691 DH *dh;
692 struct wpabuf *pubkey = NULL, *privkey = NULL;
693 size_t publen, privlen;
694
695 *priv = NULL;
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700696 wpabuf_free(*publ);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700697 *publ = NULL;
698
699 dh = DH_new();
700 if (dh == NULL)
701 return NULL;
702
703 dh->g = BN_new();
704 if (dh->g == NULL || BN_set_word(dh->g, 2) != 1)
705 goto err;
706
707 dh->p = get_group5_prime();
708 if (dh->p == NULL)
709 goto err;
710
711 if (DH_generate_key(dh) != 1)
712 goto err;
713
714 publen = BN_num_bytes(dh->pub_key);
715 pubkey = wpabuf_alloc(publen);
716 if (pubkey == NULL)
717 goto err;
718 privlen = BN_num_bytes(dh->priv_key);
719 privkey = wpabuf_alloc(privlen);
720 if (privkey == NULL)
721 goto err;
722
723 BN_bn2bin(dh->pub_key, wpabuf_put(pubkey, publen));
724 BN_bn2bin(dh->priv_key, wpabuf_put(privkey, privlen));
725
726 *priv = privkey;
727 *publ = pubkey;
728 return dh;
729
730err:
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800731 wpabuf_clear_free(pubkey);
732 wpabuf_clear_free(privkey);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700733 DH_free(dh);
734 return NULL;
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700735#else
736 DH *dh;
737 struct wpabuf *pubkey = NULL, *privkey = NULL;
738 size_t publen, privlen;
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -0700739 BIGNUM *p = NULL, *g;
740 const BIGNUM *priv_key = NULL, *pub_key = NULL;
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700741
742 *priv = NULL;
743 wpabuf_free(*publ);
744 *publ = NULL;
745
746 dh = DH_new();
747 if (dh == NULL)
748 return NULL;
749
750 g = BN_new();
751 p = get_group5_prime();
752 if (!g || BN_set_word(g, 2) != 1 || !p ||
753 DH_set0_pqg(dh, p, NULL, g) != 1)
754 goto err;
755 p = NULL;
756 g = NULL;
757
758 if (DH_generate_key(dh) != 1)
759 goto err;
760
761 DH_get0_key(dh, &pub_key, &priv_key);
762 publen = BN_num_bytes(pub_key);
763 pubkey = wpabuf_alloc(publen);
764 if (!pubkey)
765 goto err;
766 privlen = BN_num_bytes(priv_key);
767 privkey = wpabuf_alloc(privlen);
768 if (!privkey)
769 goto err;
770
771 BN_bn2bin(pub_key, wpabuf_put(pubkey, publen));
772 BN_bn2bin(priv_key, wpabuf_put(privkey, privlen));
773
774 *priv = privkey;
775 *publ = pubkey;
776 return dh;
777
778err:
779 BN_free(p);
780 BN_free(g);
781 wpabuf_clear_free(pubkey);
782 wpabuf_clear_free(privkey);
783 DH_free(dh);
784 return NULL;
785#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700786}
787
788
Dmitry Shmidt04949592012-07-19 12:16:46 -0700789void * dh5_init_fixed(const struct wpabuf *priv, const struct wpabuf *publ)
790{
Roshan Pius3a1667e2018-07-03 15:17:14 -0700791#if OPENSSL_VERSION_NUMBER < 0x10100000L || \
792 (defined(LIBRESSL_VERSION_NUMBER) && \
793 LIBRESSL_VERSION_NUMBER < 0x20700000L)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700794 DH *dh;
795
796 dh = DH_new();
797 if (dh == NULL)
798 return NULL;
799
800 dh->g = BN_new();
801 if (dh->g == NULL || BN_set_word(dh->g, 2) != 1)
802 goto err;
803
804 dh->p = get_group5_prime();
805 if (dh->p == NULL)
806 goto err;
807
808 dh->priv_key = BN_bin2bn(wpabuf_head(priv), wpabuf_len(priv), NULL);
809 if (dh->priv_key == NULL)
810 goto err;
811
812 dh->pub_key = BN_bin2bn(wpabuf_head(publ), wpabuf_len(publ), NULL);
813 if (dh->pub_key == NULL)
814 goto err;
815
816 if (DH_generate_key(dh) != 1)
817 goto err;
818
819 return dh;
820
821err:
822 DH_free(dh);
823 return NULL;
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700824#else
825 DH *dh;
826 BIGNUM *p = NULL, *g, *priv_key = NULL, *pub_key = NULL;
827
828 dh = DH_new();
829 if (dh == NULL)
830 return NULL;
831
832 g = BN_new();
833 p = get_group5_prime();
834 if (!g || BN_set_word(g, 2) != 1 || !p ||
835 DH_set0_pqg(dh, p, NULL, g) != 1)
836 goto err;
837 p = NULL;
838 g = NULL;
839
840 priv_key = BN_bin2bn(wpabuf_head(priv), wpabuf_len(priv), NULL);
841 pub_key = BN_bin2bn(wpabuf_head(publ), wpabuf_len(publ), NULL);
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -0700842 if (!priv_key || !pub_key || DH_set0_key(dh, pub_key, priv_key) != 1)
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700843 goto err;
844 pub_key = NULL;
845 priv_key = NULL;
846
847 if (DH_generate_key(dh) != 1)
848 goto err;
849
850 return dh;
851
852err:
853 BN_free(p);
854 BN_free(g);
855 BN_free(pub_key);
856 BN_clear_free(priv_key);
857 DH_free(dh);
858 return NULL;
859#endif
Dmitry Shmidt04949592012-07-19 12:16:46 -0700860}
861
862
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700863struct wpabuf * dh5_derive_shared(void *ctx, const struct wpabuf *peer_public,
864 const struct wpabuf *own_private)
865{
866 BIGNUM *pub_key;
867 struct wpabuf *res = NULL;
868 size_t rlen;
869 DH *dh = ctx;
870 int keylen;
871
872 if (ctx == NULL)
873 return NULL;
874
875 pub_key = BN_bin2bn(wpabuf_head(peer_public), wpabuf_len(peer_public),
876 NULL);
877 if (pub_key == NULL)
878 return NULL;
879
880 rlen = DH_size(dh);
881 res = wpabuf_alloc(rlen);
882 if (res == NULL)
883 goto err;
884
885 keylen = DH_compute_key(wpabuf_mhead(res), pub_key, dh);
886 if (keylen < 0)
887 goto err;
888 wpabuf_put(res, keylen);
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -0700889 BN_clear_free(pub_key);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700890
891 return res;
892
893err:
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -0700894 BN_clear_free(pub_key);
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800895 wpabuf_clear_free(res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700896 return NULL;
897}
898
899
900void dh5_free(void *ctx)
901{
902 DH *dh;
903 if (ctx == NULL)
904 return;
905 dh = ctx;
906 DH_free(dh);
907}
Dmitry Shmidt04949592012-07-19 12:16:46 -0700908
909
910struct crypto_hash {
Dmitry Shmidt55840ad2015-12-14 12:45:46 -0800911 HMAC_CTX *ctx;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700912};
913
914
915struct crypto_hash * crypto_hash_init(enum crypto_hash_alg alg, const u8 *key,
916 size_t key_len)
917{
918 struct crypto_hash *ctx;
919 const EVP_MD *md;
920
921 switch (alg) {
922#ifndef OPENSSL_NO_MD5
923 case CRYPTO_HASH_ALG_HMAC_MD5:
924 md = EVP_md5();
925 break;
926#endif /* OPENSSL_NO_MD5 */
927#ifndef OPENSSL_NO_SHA
928 case CRYPTO_HASH_ALG_HMAC_SHA1:
929 md = EVP_sha1();
930 break;
931#endif /* OPENSSL_NO_SHA */
932#ifndef OPENSSL_NO_SHA256
933#ifdef CONFIG_SHA256
934 case CRYPTO_HASH_ALG_HMAC_SHA256:
935 md = EVP_sha256();
936 break;
937#endif /* CONFIG_SHA256 */
938#endif /* OPENSSL_NO_SHA256 */
939 default:
940 return NULL;
941 }
942
943 ctx = os_zalloc(sizeof(*ctx));
944 if (ctx == NULL)
945 return NULL;
Dmitry Shmidt55840ad2015-12-14 12:45:46 -0800946 ctx->ctx = HMAC_CTX_new();
947 if (!ctx->ctx) {
948 os_free(ctx);
949 return NULL;
950 }
951
952 if (HMAC_Init_ex(ctx->ctx, key, key_len, md, NULL) != 1) {
953 HMAC_CTX_free(ctx->ctx);
954 bin_clear_free(ctx, sizeof(*ctx));
955 return NULL;
956 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700957
958 return ctx;
959}
960
961
962void crypto_hash_update(struct crypto_hash *ctx, const u8 *data, size_t len)
963{
964 if (ctx == NULL)
965 return;
Dmitry Shmidt55840ad2015-12-14 12:45:46 -0800966 HMAC_Update(ctx->ctx, data, len);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700967}
968
969
970int crypto_hash_finish(struct crypto_hash *ctx, u8 *mac, size_t *len)
971{
972 unsigned int mdlen;
973 int res;
974
975 if (ctx == NULL)
976 return -2;
977
978 if (mac == NULL || len == NULL) {
Dmitry Shmidt55840ad2015-12-14 12:45:46 -0800979 HMAC_CTX_free(ctx->ctx);
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800980 bin_clear_free(ctx, sizeof(*ctx));
Dmitry Shmidt04949592012-07-19 12:16:46 -0700981 return 0;
982 }
983
984 mdlen = *len;
Dmitry Shmidt55840ad2015-12-14 12:45:46 -0800985 res = HMAC_Final(ctx->ctx, mac, &mdlen);
986 HMAC_CTX_free(ctx->ctx);
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800987 bin_clear_free(ctx, sizeof(*ctx));
Dmitry Shmidt04949592012-07-19 12:16:46 -0700988
989 if (res == 1) {
990 *len = mdlen;
991 return 0;
992 }
993
994 return -1;
995}
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700996
997
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800998static int openssl_hmac_vector(const EVP_MD *type, const u8 *key,
999 size_t key_len, size_t num_elem,
1000 const u8 *addr[], const size_t *len, u8 *mac,
1001 unsigned int mdlen)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001002{
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08001003 HMAC_CTX *ctx;
1004 size_t i;
1005 int res;
1006
1007 if (TEST_FAIL())
1008 return -1;
1009
1010 ctx = HMAC_CTX_new();
1011 if (!ctx)
1012 return -1;
1013 res = HMAC_Init_ex(ctx, key, key_len, type, NULL);
1014 if (res != 1)
1015 goto done;
1016
1017 for (i = 0; i < num_elem; i++)
1018 HMAC_Update(ctx, addr[i], len[i]);
1019
1020 res = HMAC_Final(ctx, mac, &mdlen);
1021done:
1022 HMAC_CTX_free(ctx);
1023
1024 return res == 1 ? 0 : -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001025}
1026
1027
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001028#ifndef CONFIG_FIPS
1029
1030int hmac_md5_vector(const u8 *key, size_t key_len, size_t num_elem,
1031 const u8 *addr[], const size_t *len, u8 *mac)
1032{
1033 return openssl_hmac_vector(EVP_md5(), key ,key_len, num_elem, addr, len,
1034 mac, 16);
1035}
1036
1037
1038int hmac_md5(const u8 *key, size_t key_len, const u8 *data, size_t data_len,
1039 u8 *mac)
1040{
1041 return hmac_md5_vector(key, key_len, 1, &data, &data_len, mac);
1042}
1043
1044#endif /* CONFIG_FIPS */
1045
1046
1047int pbkdf2_sha1(const char *passphrase, const u8 *ssid, size_t ssid_len,
1048 int iterations, u8 *buf, size_t buflen)
1049{
1050 if (PKCS5_PBKDF2_HMAC_SHA1(passphrase, os_strlen(passphrase), ssid,
1051 ssid_len, iterations, buflen, buf) != 1)
1052 return -1;
1053 return 0;
1054}
1055
1056
1057int hmac_sha1_vector(const u8 *key, size_t key_len, size_t num_elem,
1058 const u8 *addr[], const size_t *len, u8 *mac)
1059{
1060 return openssl_hmac_vector(EVP_sha1(), key, key_len, num_elem, addr,
1061 len, mac, 20);
1062}
1063
1064
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001065int hmac_sha1(const u8 *key, size_t key_len, const u8 *data, size_t data_len,
1066 u8 *mac)
1067{
1068 return hmac_sha1_vector(key, key_len, 1, &data, &data_len, mac);
1069}
1070
1071
1072#ifdef CONFIG_SHA256
1073
1074int hmac_sha256_vector(const u8 *key, size_t key_len, size_t num_elem,
1075 const u8 *addr[], const size_t *len, u8 *mac)
1076{
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001077 return openssl_hmac_vector(EVP_sha256(), key, key_len, num_elem, addr,
1078 len, mac, 32);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001079}
1080
1081
1082int hmac_sha256(const u8 *key, size_t key_len, const u8 *data,
1083 size_t data_len, u8 *mac)
1084{
1085 return hmac_sha256_vector(key, key_len, 1, &data, &data_len, mac);
1086}
1087
1088#endif /* CONFIG_SHA256 */
1089
1090
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001091#ifdef CONFIG_SHA384
1092
1093int hmac_sha384_vector(const u8 *key, size_t key_len, size_t num_elem,
1094 const u8 *addr[], const size_t *len, u8 *mac)
1095{
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001096 return openssl_hmac_vector(EVP_sha384(), key, key_len, num_elem, addr,
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08001097 len, mac, 48);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001098}
1099
1100
1101int hmac_sha384(const u8 *key, size_t key_len, const u8 *data,
1102 size_t data_len, u8 *mac)
1103{
1104 return hmac_sha384_vector(key, key_len, 1, &data, &data_len, mac);
1105}
1106
1107#endif /* CONFIG_SHA384 */
1108
1109
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001110#ifdef CONFIG_SHA512
1111
1112int hmac_sha512_vector(const u8 *key, size_t key_len, size_t num_elem,
1113 const u8 *addr[], const size_t *len, u8 *mac)
1114{
1115 return openssl_hmac_vector(EVP_sha512(), key, key_len, num_elem, addr,
1116 len, mac, 64);
1117}
1118
1119
1120int hmac_sha512(const u8 *key, size_t key_len, const u8 *data,
1121 size_t data_len, u8 *mac)
1122{
1123 return hmac_sha512_vector(key, key_len, 1, &data, &data_len, mac);
1124}
1125
1126#endif /* CONFIG_SHA512 */
1127
1128
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001129int crypto_get_random(void *buf, size_t len)
1130{
1131 if (RAND_bytes(buf, len) != 1)
1132 return -1;
1133 return 0;
1134}
1135
1136
1137#ifdef CONFIG_OPENSSL_CMAC
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001138int omac1_aes_vector(const u8 *key, size_t key_len, size_t num_elem,
1139 const u8 *addr[], const size_t *len, u8 *mac)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001140{
1141 CMAC_CTX *ctx;
1142 int ret = -1;
1143 size_t outlen, i;
1144
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001145 if (TEST_FAIL())
1146 return -1;
1147
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001148 ctx = CMAC_CTX_new();
1149 if (ctx == NULL)
1150 return -1;
1151
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001152 if (key_len == 32) {
1153 if (!CMAC_Init(ctx, key, 32, EVP_aes_256_cbc(), NULL))
1154 goto fail;
1155 } else if (key_len == 16) {
1156 if (!CMAC_Init(ctx, key, 16, EVP_aes_128_cbc(), NULL))
1157 goto fail;
1158 } else {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001159 goto fail;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001160 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001161 for (i = 0; i < num_elem; i++) {
1162 if (!CMAC_Update(ctx, addr[i], len[i]))
1163 goto fail;
1164 }
1165 if (!CMAC_Final(ctx, mac, &outlen) || outlen != 16)
1166 goto fail;
1167
1168 ret = 0;
1169fail:
1170 CMAC_CTX_free(ctx);
1171 return ret;
1172}
1173
1174
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001175int omac1_aes_128_vector(const u8 *key, size_t num_elem,
1176 const u8 *addr[], const size_t *len, u8 *mac)
1177{
1178 return omac1_aes_vector(key, 16, num_elem, addr, len, mac);
1179}
1180
1181
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001182int omac1_aes_128(const u8 *key, const u8 *data, size_t data_len, u8 *mac)
1183{
1184 return omac1_aes_128_vector(key, 1, &data, &data_len, mac);
1185}
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001186
1187
1188int omac1_aes_256(const u8 *key, const u8 *data, size_t data_len, u8 *mac)
1189{
1190 return omac1_aes_vector(key, 32, 1, &data, &data_len, mac);
1191}
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001192#endif /* CONFIG_OPENSSL_CMAC */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001193
1194
1195struct crypto_bignum * crypto_bignum_init(void)
1196{
Dmitry Shmidte4663042016-04-04 10:07:49 -07001197 if (TEST_FAIL())
1198 return NULL;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001199 return (struct crypto_bignum *) BN_new();
1200}
1201
1202
1203struct crypto_bignum * crypto_bignum_init_set(const u8 *buf, size_t len)
1204{
Dmitry Shmidte4663042016-04-04 10:07:49 -07001205 BIGNUM *bn;
1206
1207 if (TEST_FAIL())
1208 return NULL;
1209
1210 bn = BN_bin2bn(buf, len, NULL);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001211 return (struct crypto_bignum *) bn;
1212}
1213
1214
1215void crypto_bignum_deinit(struct crypto_bignum *n, int clear)
1216{
1217 if (clear)
1218 BN_clear_free((BIGNUM *) n);
1219 else
1220 BN_free((BIGNUM *) n);
1221}
1222
1223
1224int crypto_bignum_to_bin(const struct crypto_bignum *a,
1225 u8 *buf, size_t buflen, size_t padlen)
1226{
1227 int num_bytes, offset;
1228
Dmitry Shmidte4663042016-04-04 10:07:49 -07001229 if (TEST_FAIL())
1230 return -1;
1231
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001232 if (padlen > buflen)
1233 return -1;
1234
1235 num_bytes = BN_num_bytes((const BIGNUM *) a);
1236 if ((size_t) num_bytes > buflen)
1237 return -1;
1238 if (padlen > (size_t) num_bytes)
1239 offset = padlen - num_bytes;
1240 else
1241 offset = 0;
1242
1243 os_memset(buf, 0, offset);
1244 BN_bn2bin((const BIGNUM *) a, buf + offset);
1245
1246 return num_bytes + offset;
1247}
1248
1249
Roshan Pius3a1667e2018-07-03 15:17:14 -07001250int crypto_bignum_rand(struct crypto_bignum *r, const struct crypto_bignum *m)
1251{
1252 return BN_rand_range((BIGNUM *) r, (const BIGNUM *) m) == 1 ? 0 : -1;
1253}
1254
1255
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001256int crypto_bignum_add(const struct crypto_bignum *a,
1257 const struct crypto_bignum *b,
1258 struct crypto_bignum *c)
1259{
1260 return BN_add((BIGNUM *) c, (const BIGNUM *) a, (const BIGNUM *) b) ?
1261 0 : -1;
1262}
1263
1264
1265int crypto_bignum_mod(const struct crypto_bignum *a,
1266 const struct crypto_bignum *b,
1267 struct crypto_bignum *c)
1268{
1269 int res;
1270 BN_CTX *bnctx;
1271
1272 bnctx = BN_CTX_new();
1273 if (bnctx == NULL)
1274 return -1;
1275 res = BN_mod((BIGNUM *) c, (const BIGNUM *) a, (const BIGNUM *) b,
1276 bnctx);
1277 BN_CTX_free(bnctx);
1278
1279 return res ? 0 : -1;
1280}
1281
1282
1283int crypto_bignum_exptmod(const struct crypto_bignum *a,
1284 const struct crypto_bignum *b,
1285 const struct crypto_bignum *c,
1286 struct crypto_bignum *d)
1287{
1288 int res;
1289 BN_CTX *bnctx;
1290
Dmitry Shmidte4663042016-04-04 10:07:49 -07001291 if (TEST_FAIL())
1292 return -1;
1293
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001294 bnctx = BN_CTX_new();
1295 if (bnctx == NULL)
1296 return -1;
1297 res = BN_mod_exp((BIGNUM *) d, (const BIGNUM *) a, (const BIGNUM *) b,
1298 (const BIGNUM *) c, bnctx);
1299 BN_CTX_free(bnctx);
1300
1301 return res ? 0 : -1;
1302}
1303
1304
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001305int crypto_bignum_inverse(const struct crypto_bignum *a,
1306 const struct crypto_bignum *b,
1307 struct crypto_bignum *c)
1308{
1309 BIGNUM *res;
1310 BN_CTX *bnctx;
1311
Dmitry Shmidte4663042016-04-04 10:07:49 -07001312 if (TEST_FAIL())
1313 return -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001314 bnctx = BN_CTX_new();
1315 if (bnctx == NULL)
1316 return -1;
1317 res = BN_mod_inverse((BIGNUM *) c, (const BIGNUM *) a,
1318 (const BIGNUM *) b, bnctx);
1319 BN_CTX_free(bnctx);
1320
1321 return res ? 0 : -1;
1322}
1323
1324
1325int crypto_bignum_sub(const struct crypto_bignum *a,
1326 const struct crypto_bignum *b,
1327 struct crypto_bignum *c)
1328{
Dmitry Shmidte4663042016-04-04 10:07:49 -07001329 if (TEST_FAIL())
1330 return -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001331 return BN_sub((BIGNUM *) c, (const BIGNUM *) a, (const BIGNUM *) b) ?
1332 0 : -1;
1333}
1334
1335
1336int crypto_bignum_div(const struct crypto_bignum *a,
1337 const struct crypto_bignum *b,
1338 struct crypto_bignum *c)
1339{
1340 int res;
1341
1342 BN_CTX *bnctx;
1343
Dmitry Shmidte4663042016-04-04 10:07:49 -07001344 if (TEST_FAIL())
1345 return -1;
1346
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001347 bnctx = BN_CTX_new();
1348 if (bnctx == NULL)
1349 return -1;
1350 res = BN_div((BIGNUM *) c, NULL, (const BIGNUM *) a,
1351 (const BIGNUM *) b, bnctx);
1352 BN_CTX_free(bnctx);
1353
1354 return res ? 0 : -1;
1355}
1356
1357
1358int crypto_bignum_mulmod(const struct crypto_bignum *a,
1359 const struct crypto_bignum *b,
1360 const struct crypto_bignum *c,
1361 struct crypto_bignum *d)
1362{
1363 int res;
1364
1365 BN_CTX *bnctx;
1366
Dmitry Shmidte4663042016-04-04 10:07:49 -07001367 if (TEST_FAIL())
1368 return -1;
1369
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001370 bnctx = BN_CTX_new();
1371 if (bnctx == NULL)
1372 return -1;
1373 res = BN_mod_mul((BIGNUM *) d, (const BIGNUM *) a, (const BIGNUM *) b,
1374 (const BIGNUM *) c, bnctx);
1375 BN_CTX_free(bnctx);
1376
1377 return res ? 0 : -1;
1378}
1379
1380
Roshan Pius3a1667e2018-07-03 15:17:14 -07001381int crypto_bignum_rshift(const struct crypto_bignum *a, int n,
1382 struct crypto_bignum *r)
1383{
1384 /* Note: BN_rshift() does not modify the first argument even though it
1385 * has not been marked const. */
1386 return BN_rshift((BIGNUM *) a, (BIGNUM *) r, n) == 1 ? 0 : -1;
1387}
1388
1389
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001390int crypto_bignum_cmp(const struct crypto_bignum *a,
1391 const struct crypto_bignum *b)
1392{
1393 return BN_cmp((const BIGNUM *) a, (const BIGNUM *) b);
1394}
1395
1396
1397int crypto_bignum_bits(const struct crypto_bignum *a)
1398{
1399 return BN_num_bits((const BIGNUM *) a);
1400}
1401
1402
1403int crypto_bignum_is_zero(const struct crypto_bignum *a)
1404{
1405 return BN_is_zero((const BIGNUM *) a);
1406}
1407
1408
1409int crypto_bignum_is_one(const struct crypto_bignum *a)
1410{
1411 return BN_is_one((const BIGNUM *) a);
1412}
1413
1414
Roshan Pius3a1667e2018-07-03 15:17:14 -07001415int crypto_bignum_is_odd(const struct crypto_bignum *a)
1416{
1417 return BN_is_odd((const BIGNUM *) a);
1418}
1419
1420
Dmitry Shmidt41712582015-06-29 11:02:15 -07001421int crypto_bignum_legendre(const struct crypto_bignum *a,
1422 const struct crypto_bignum *p)
1423{
1424 BN_CTX *bnctx;
1425 BIGNUM *exp = NULL, *tmp = NULL;
1426 int res = -2;
1427
Dmitry Shmidte4663042016-04-04 10:07:49 -07001428 if (TEST_FAIL())
1429 return -2;
1430
Dmitry Shmidt41712582015-06-29 11:02:15 -07001431 bnctx = BN_CTX_new();
1432 if (bnctx == NULL)
1433 return -2;
1434
1435 exp = BN_new();
1436 tmp = BN_new();
1437 if (!exp || !tmp ||
1438 /* exp = (p-1) / 2 */
1439 !BN_sub(exp, (const BIGNUM *) p, BN_value_one()) ||
1440 !BN_rshift1(exp, exp) ||
1441 !BN_mod_exp(tmp, (const BIGNUM *) a, exp, (const BIGNUM *) p,
1442 bnctx))
1443 goto fail;
1444
1445 if (BN_is_word(tmp, 1))
1446 res = 1;
1447 else if (BN_is_zero(tmp))
1448 res = 0;
1449 else
1450 res = -1;
1451
1452fail:
1453 BN_clear_free(tmp);
1454 BN_clear_free(exp);
1455 BN_CTX_free(bnctx);
1456 return res;
1457}
1458
1459
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001460#ifdef CONFIG_ECC
1461
1462struct crypto_ec {
1463 EC_GROUP *group;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001464 int nid;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001465 BN_CTX *bnctx;
1466 BIGNUM *prime;
1467 BIGNUM *order;
Dmitry Shmidt41712582015-06-29 11:02:15 -07001468 BIGNUM *a;
1469 BIGNUM *b;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001470};
1471
1472struct crypto_ec * crypto_ec_init(int group)
1473{
1474 struct crypto_ec *e;
1475 int nid;
1476
1477 /* Map from IANA registry for IKE D-H groups to OpenSSL NID */
1478 switch (group) {
1479 case 19:
1480 nid = NID_X9_62_prime256v1;
1481 break;
1482 case 20:
1483 nid = NID_secp384r1;
1484 break;
1485 case 21:
1486 nid = NID_secp521r1;
1487 break;
1488 case 25:
1489 nid = NID_X9_62_prime192v1;
1490 break;
1491 case 26:
1492 nid = NID_secp224r1;
1493 break;
Dmitry Shmidt41712582015-06-29 11:02:15 -07001494#ifdef NID_brainpoolP224r1
1495 case 27:
1496 nid = NID_brainpoolP224r1;
1497 break;
1498#endif /* NID_brainpoolP224r1 */
1499#ifdef NID_brainpoolP256r1
1500 case 28:
1501 nid = NID_brainpoolP256r1;
1502 break;
1503#endif /* NID_brainpoolP256r1 */
1504#ifdef NID_brainpoolP384r1
1505 case 29:
1506 nid = NID_brainpoolP384r1;
1507 break;
1508#endif /* NID_brainpoolP384r1 */
1509#ifdef NID_brainpoolP512r1
1510 case 30:
1511 nid = NID_brainpoolP512r1;
1512 break;
1513#endif /* NID_brainpoolP512r1 */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001514 default:
1515 return NULL;
1516 }
1517
1518 e = os_zalloc(sizeof(*e));
1519 if (e == NULL)
1520 return NULL;
1521
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001522 e->nid = nid;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001523 e->bnctx = BN_CTX_new();
1524 e->group = EC_GROUP_new_by_curve_name(nid);
1525 e->prime = BN_new();
1526 e->order = BN_new();
Dmitry Shmidt41712582015-06-29 11:02:15 -07001527 e->a = BN_new();
1528 e->b = BN_new();
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001529 if (e->group == NULL || e->bnctx == NULL || e->prime == NULL ||
Dmitry Shmidt41712582015-06-29 11:02:15 -07001530 e->order == NULL || e->a == NULL || e->b == NULL ||
1531 !EC_GROUP_get_curve_GFp(e->group, e->prime, e->a, e->b, e->bnctx) ||
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001532 !EC_GROUP_get_order(e->group, e->order, e->bnctx)) {
1533 crypto_ec_deinit(e);
1534 e = NULL;
1535 }
1536
1537 return e;
1538}
1539
1540
1541void crypto_ec_deinit(struct crypto_ec *e)
1542{
1543 if (e == NULL)
1544 return;
Dmitry Shmidt41712582015-06-29 11:02:15 -07001545 BN_clear_free(e->b);
1546 BN_clear_free(e->a);
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001547 BN_clear_free(e->order);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001548 BN_clear_free(e->prime);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001549 EC_GROUP_free(e->group);
1550 BN_CTX_free(e->bnctx);
1551 os_free(e);
1552}
1553
1554
Roshan Pius3a1667e2018-07-03 15:17:14 -07001555int crypto_ec_cofactor(struct crypto_ec *e, struct crypto_bignum *cofactor)
1556{
1557 return EC_GROUP_get_cofactor(e->group, (BIGNUM *) cofactor,
1558 e->bnctx) == 0 ? -1 : 0;
1559}
1560
1561
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001562struct crypto_ec_point * crypto_ec_point_init(struct crypto_ec *e)
1563{
Dmitry Shmidte4663042016-04-04 10:07:49 -07001564 if (TEST_FAIL())
1565 return NULL;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001566 if (e == NULL)
1567 return NULL;
1568 return (struct crypto_ec_point *) EC_POINT_new(e->group);
1569}
1570
1571
1572size_t crypto_ec_prime_len(struct crypto_ec *e)
1573{
1574 return BN_num_bytes(e->prime);
1575}
1576
1577
1578size_t crypto_ec_prime_len_bits(struct crypto_ec *e)
1579{
1580 return BN_num_bits(e->prime);
1581}
1582
1583
Roshan Pius3a1667e2018-07-03 15:17:14 -07001584size_t crypto_ec_order_len(struct crypto_ec *e)
1585{
1586 return BN_num_bytes(e->order);
1587}
1588
1589
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001590const struct crypto_bignum * crypto_ec_get_prime(struct crypto_ec *e)
1591{
1592 return (const struct crypto_bignum *) e->prime;
1593}
1594
1595
1596const struct crypto_bignum * crypto_ec_get_order(struct crypto_ec *e)
1597{
1598 return (const struct crypto_bignum *) e->order;
1599}
1600
1601
1602void crypto_ec_point_deinit(struct crypto_ec_point *p, int clear)
1603{
1604 if (clear)
1605 EC_POINT_clear_free((EC_POINT *) p);
1606 else
1607 EC_POINT_free((EC_POINT *) p);
1608}
1609
1610
Roshan Pius3a1667e2018-07-03 15:17:14 -07001611int crypto_ec_point_x(struct crypto_ec *e, const struct crypto_ec_point *p,
1612 struct crypto_bignum *x)
1613{
1614 return EC_POINT_get_affine_coordinates_GFp(e->group,
1615 (const EC_POINT *) p,
1616 (BIGNUM *) x, NULL,
1617 e->bnctx) == 1 ? 0 : -1;
1618}
1619
1620
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001621int crypto_ec_point_to_bin(struct crypto_ec *e,
1622 const struct crypto_ec_point *point, u8 *x, u8 *y)
1623{
1624 BIGNUM *x_bn, *y_bn;
1625 int ret = -1;
1626 int len = BN_num_bytes(e->prime);
1627
Dmitry Shmidte4663042016-04-04 10:07:49 -07001628 if (TEST_FAIL())
1629 return -1;
1630
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001631 x_bn = BN_new();
1632 y_bn = BN_new();
1633
1634 if (x_bn && y_bn &&
1635 EC_POINT_get_affine_coordinates_GFp(e->group, (EC_POINT *) point,
1636 x_bn, y_bn, e->bnctx)) {
1637 if (x) {
1638 crypto_bignum_to_bin((struct crypto_bignum *) x_bn,
1639 x, len, len);
1640 }
1641 if (y) {
1642 crypto_bignum_to_bin((struct crypto_bignum *) y_bn,
1643 y, len, len);
1644 }
1645 ret = 0;
1646 }
1647
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001648 BN_clear_free(x_bn);
1649 BN_clear_free(y_bn);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001650 return ret;
1651}
1652
1653
1654struct crypto_ec_point * crypto_ec_point_from_bin(struct crypto_ec *e,
1655 const u8 *val)
1656{
1657 BIGNUM *x, *y;
1658 EC_POINT *elem;
1659 int len = BN_num_bytes(e->prime);
1660
Dmitry Shmidte4663042016-04-04 10:07:49 -07001661 if (TEST_FAIL())
1662 return NULL;
1663
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001664 x = BN_bin2bn(val, len, NULL);
1665 y = BN_bin2bn(val + len, len, NULL);
1666 elem = EC_POINT_new(e->group);
1667 if (x == NULL || y == NULL || elem == NULL) {
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001668 BN_clear_free(x);
1669 BN_clear_free(y);
1670 EC_POINT_clear_free(elem);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001671 return NULL;
1672 }
1673
1674 if (!EC_POINT_set_affine_coordinates_GFp(e->group, elem, x, y,
1675 e->bnctx)) {
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001676 EC_POINT_clear_free(elem);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001677 elem = NULL;
1678 }
1679
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001680 BN_clear_free(x);
1681 BN_clear_free(y);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001682
1683 return (struct crypto_ec_point *) elem;
1684}
1685
1686
1687int crypto_ec_point_add(struct crypto_ec *e, const struct crypto_ec_point *a,
1688 const struct crypto_ec_point *b,
1689 struct crypto_ec_point *c)
1690{
Dmitry Shmidte4663042016-04-04 10:07:49 -07001691 if (TEST_FAIL())
1692 return -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001693 return EC_POINT_add(e->group, (EC_POINT *) c, (const EC_POINT *) a,
1694 (const EC_POINT *) b, e->bnctx) ? 0 : -1;
1695}
1696
1697
1698int crypto_ec_point_mul(struct crypto_ec *e, const struct crypto_ec_point *p,
1699 const struct crypto_bignum *b,
1700 struct crypto_ec_point *res)
1701{
Dmitry Shmidte4663042016-04-04 10:07:49 -07001702 if (TEST_FAIL())
1703 return -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001704 return EC_POINT_mul(e->group, (EC_POINT *) res, NULL,
1705 (const EC_POINT *) p, (const BIGNUM *) b, e->bnctx)
1706 ? 0 : -1;
1707}
1708
1709
1710int crypto_ec_point_invert(struct crypto_ec *e, struct crypto_ec_point *p)
1711{
Dmitry Shmidte4663042016-04-04 10:07:49 -07001712 if (TEST_FAIL())
1713 return -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001714 return EC_POINT_invert(e->group, (EC_POINT *) p, e->bnctx) ? 0 : -1;
1715}
1716
1717
1718int crypto_ec_point_solve_y_coord(struct crypto_ec *e,
1719 struct crypto_ec_point *p,
1720 const struct crypto_bignum *x, int y_bit)
1721{
Dmitry Shmidte4663042016-04-04 10:07:49 -07001722 if (TEST_FAIL())
1723 return -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001724 if (!EC_POINT_set_compressed_coordinates_GFp(e->group, (EC_POINT *) p,
1725 (const BIGNUM *) x, y_bit,
1726 e->bnctx) ||
1727 !EC_POINT_is_on_curve(e->group, (EC_POINT *) p, e->bnctx))
1728 return -1;
1729 return 0;
1730}
1731
1732
Dmitry Shmidt41712582015-06-29 11:02:15 -07001733struct crypto_bignum *
1734crypto_ec_point_compute_y_sqr(struct crypto_ec *e,
1735 const struct crypto_bignum *x)
1736{
1737 BIGNUM *tmp, *tmp2, *y_sqr = NULL;
1738
Dmitry Shmidte4663042016-04-04 10:07:49 -07001739 if (TEST_FAIL())
1740 return NULL;
1741
Dmitry Shmidt41712582015-06-29 11:02:15 -07001742 tmp = BN_new();
1743 tmp2 = BN_new();
1744
1745 /* y^2 = x^3 + ax + b */
1746 if (tmp && tmp2 &&
1747 BN_mod_sqr(tmp, (const BIGNUM *) x, e->prime, e->bnctx) &&
1748 BN_mod_mul(tmp, tmp, (const BIGNUM *) x, e->prime, e->bnctx) &&
1749 BN_mod_mul(tmp2, e->a, (const BIGNUM *) x, e->prime, e->bnctx) &&
1750 BN_mod_add_quick(tmp2, tmp2, tmp, e->prime) &&
1751 BN_mod_add_quick(tmp2, tmp2, e->b, e->prime)) {
1752 y_sqr = tmp2;
1753 tmp2 = NULL;
1754 }
1755
1756 BN_clear_free(tmp);
1757 BN_clear_free(tmp2);
1758
1759 return (struct crypto_bignum *) y_sqr;
1760}
1761
1762
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001763int crypto_ec_point_is_at_infinity(struct crypto_ec *e,
1764 const struct crypto_ec_point *p)
1765{
1766 return EC_POINT_is_at_infinity(e->group, (const EC_POINT *) p);
1767}
1768
1769
1770int crypto_ec_point_is_on_curve(struct crypto_ec *e,
1771 const struct crypto_ec_point *p)
1772{
Dmitry Shmidt41712582015-06-29 11:02:15 -07001773 return EC_POINT_is_on_curve(e->group, (const EC_POINT *) p,
1774 e->bnctx) == 1;
1775}
1776
1777
1778int crypto_ec_point_cmp(const struct crypto_ec *e,
1779 const struct crypto_ec_point *a,
1780 const struct crypto_ec_point *b)
1781{
1782 return EC_POINT_cmp(e->group, (const EC_POINT *) a,
1783 (const EC_POINT *) b, e->bnctx);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001784}
1785
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001786
1787struct crypto_ecdh {
1788 struct crypto_ec *ec;
1789 EVP_PKEY *pkey;
1790};
1791
1792struct crypto_ecdh * crypto_ecdh_init(int group)
1793{
1794 struct crypto_ecdh *ecdh;
1795 EVP_PKEY *params = NULL;
Roshan Pius3a1667e2018-07-03 15:17:14 -07001796 EC_KEY *ec_params;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001797 EVP_PKEY_CTX *kctx = NULL;
1798
1799 ecdh = os_zalloc(sizeof(*ecdh));
1800 if (!ecdh)
1801 goto fail;
1802
1803 ecdh->ec = crypto_ec_init(group);
1804 if (!ecdh->ec)
1805 goto fail;
1806
Roshan Pius3a1667e2018-07-03 15:17:14 -07001807 ec_params = EC_KEY_new_by_curve_name(ecdh->ec->nid);
1808 if (!ec_params) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001809 wpa_printf(MSG_ERROR,
Roshan Pius3a1667e2018-07-03 15:17:14 -07001810 "OpenSSL: Failed to generate EC_KEY parameters");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001811 goto fail;
1812 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07001813 EC_KEY_set_asn1_flag(ec_params, OPENSSL_EC_NAMED_CURVE);
1814 params = EVP_PKEY_new();
1815 if (!params || EVP_PKEY_set1_EC_KEY(params, ec_params) != 1) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001816 wpa_printf(MSG_ERROR,
Roshan Pius3a1667e2018-07-03 15:17:14 -07001817 "OpenSSL: Failed to generate EVP_PKEY parameters");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001818 goto fail;
1819 }
1820
1821 kctx = EVP_PKEY_CTX_new(params, NULL);
1822 if (!kctx)
1823 goto fail;
1824
1825 if (EVP_PKEY_keygen_init(kctx) != 1) {
1826 wpa_printf(MSG_ERROR,
1827 "OpenSSL: EVP_PKEY_keygen_init failed: %s",
1828 ERR_error_string(ERR_get_error(), NULL));
1829 goto fail;
1830 }
1831
1832 if (EVP_PKEY_keygen(kctx, &ecdh->pkey) != 1) {
1833 wpa_printf(MSG_ERROR, "OpenSSL: EVP_PKEY_keygen failed: %s",
1834 ERR_error_string(ERR_get_error(), NULL));
1835 goto fail;
1836 }
1837
1838done:
1839 EVP_PKEY_free(params);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001840 EVP_PKEY_CTX_free(kctx);
1841
1842 return ecdh;
1843fail:
1844 crypto_ecdh_deinit(ecdh);
1845 ecdh = NULL;
1846 goto done;
1847}
1848
1849
1850struct wpabuf * crypto_ecdh_get_pubkey(struct crypto_ecdh *ecdh, int inc_y)
1851{
1852 struct wpabuf *buf = NULL;
1853 EC_KEY *eckey;
1854 const EC_POINT *pubkey;
1855 BIGNUM *x, *y = NULL;
1856 int len = BN_num_bytes(ecdh->ec->prime);
1857 int res;
1858
1859 eckey = EVP_PKEY_get1_EC_KEY(ecdh->pkey);
1860 if (!eckey)
1861 return NULL;
1862
1863 pubkey = EC_KEY_get0_public_key(eckey);
1864 if (!pubkey)
1865 return NULL;
1866
1867 x = BN_new();
1868 if (inc_y) {
1869 y = BN_new();
1870 if (!y)
1871 goto fail;
1872 }
1873 buf = wpabuf_alloc(inc_y ? 2 * len : len);
1874 if (!x || !buf)
1875 goto fail;
1876
1877 if (EC_POINT_get_affine_coordinates_GFp(ecdh->ec->group, pubkey,
1878 x, y, ecdh->ec->bnctx) != 1) {
1879 wpa_printf(MSG_ERROR,
1880 "OpenSSL: EC_POINT_get_affine_coordinates_GFp failed: %s",
1881 ERR_error_string(ERR_get_error(), NULL));
1882 goto fail;
1883 }
1884
1885 res = crypto_bignum_to_bin((struct crypto_bignum *) x,
1886 wpabuf_put(buf, len), len, len);
1887 if (res < 0)
1888 goto fail;
1889
1890 if (inc_y) {
1891 res = crypto_bignum_to_bin((struct crypto_bignum *) y,
1892 wpabuf_put(buf, len), len, len);
1893 if (res < 0)
1894 goto fail;
1895 }
1896
1897done:
1898 BN_clear_free(x);
1899 BN_clear_free(y);
1900 EC_KEY_free(eckey);
1901
1902 return buf;
1903fail:
1904 wpabuf_free(buf);
1905 buf = NULL;
1906 goto done;
1907}
1908
1909
1910struct wpabuf * crypto_ecdh_set_peerkey(struct crypto_ecdh *ecdh, int inc_y,
1911 const u8 *key, size_t len)
1912{
1913 BIGNUM *x, *y = NULL;
1914 EVP_PKEY_CTX *ctx = NULL;
1915 EVP_PKEY *peerkey = NULL;
1916 struct wpabuf *secret = NULL;
1917 size_t secret_len;
1918 EC_POINT *pub;
1919 EC_KEY *eckey = NULL;
1920
1921 x = BN_bin2bn(key, inc_y ? len / 2 : len, NULL);
1922 pub = EC_POINT_new(ecdh->ec->group);
1923 if (!x || !pub)
1924 goto fail;
1925
1926 if (inc_y) {
1927 y = BN_bin2bn(key + len / 2, len / 2, NULL);
1928 if (!y)
1929 goto fail;
1930 if (!EC_POINT_set_affine_coordinates_GFp(ecdh->ec->group, pub,
1931 x, y,
1932 ecdh->ec->bnctx)) {
1933 wpa_printf(MSG_ERROR,
1934 "OpenSSL: EC_POINT_set_affine_coordinates_GFp failed: %s",
1935 ERR_error_string(ERR_get_error(), NULL));
1936 goto fail;
1937 }
1938 } else if (!EC_POINT_set_compressed_coordinates_GFp(ecdh->ec->group,
1939 pub, x, 0,
1940 ecdh->ec->bnctx)) {
1941 wpa_printf(MSG_ERROR,
1942 "OpenSSL: EC_POINT_set_compressed_coordinates_GFp failed: %s",
1943 ERR_error_string(ERR_get_error(), NULL));
1944 goto fail;
1945 }
1946
1947 if (!EC_POINT_is_on_curve(ecdh->ec->group, pub, ecdh->ec->bnctx)) {
1948 wpa_printf(MSG_ERROR,
1949 "OpenSSL: ECDH peer public key is not on curve");
1950 goto fail;
1951 }
1952
1953 eckey = EC_KEY_new_by_curve_name(ecdh->ec->nid);
1954 if (!eckey || EC_KEY_set_public_key(eckey, pub) != 1) {
1955 wpa_printf(MSG_ERROR,
1956 "OpenSSL: EC_KEY_set_public_key failed: %s",
1957 ERR_error_string(ERR_get_error(), NULL));
1958 goto fail;
1959 }
1960
1961 peerkey = EVP_PKEY_new();
1962 if (!peerkey || EVP_PKEY_set1_EC_KEY(peerkey, eckey) != 1)
1963 goto fail;
1964
1965 ctx = EVP_PKEY_CTX_new(ecdh->pkey, NULL);
1966 if (!ctx || EVP_PKEY_derive_init(ctx) != 1 ||
1967 EVP_PKEY_derive_set_peer(ctx, peerkey) != 1 ||
1968 EVP_PKEY_derive(ctx, NULL, &secret_len) != 1) {
1969 wpa_printf(MSG_ERROR,
1970 "OpenSSL: EVP_PKEY_derive(1) failed: %s",
1971 ERR_error_string(ERR_get_error(), NULL));
1972 goto fail;
1973 }
1974
1975 secret = wpabuf_alloc(secret_len);
1976 if (!secret)
1977 goto fail;
1978 if (EVP_PKEY_derive(ctx, wpabuf_put(secret, secret_len),
1979 &secret_len) != 1) {
1980 wpa_printf(MSG_ERROR,
1981 "OpenSSL: EVP_PKEY_derive(2) failed: %s",
1982 ERR_error_string(ERR_get_error(), NULL));
1983 goto fail;
1984 }
1985
1986done:
1987 BN_free(x);
1988 BN_free(y);
1989 EC_KEY_free(eckey);
1990 EC_POINT_free(pub);
1991 EVP_PKEY_CTX_free(ctx);
1992 EVP_PKEY_free(peerkey);
1993 return secret;
1994fail:
1995 wpabuf_free(secret);
1996 secret = NULL;
1997 goto done;
1998}
1999
2000
2001void crypto_ecdh_deinit(struct crypto_ecdh *ecdh)
2002{
2003 if (ecdh) {
2004 crypto_ec_deinit(ecdh->ec);
2005 EVP_PKEY_free(ecdh->pkey);
2006 os_free(ecdh);
2007 }
2008}
2009
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002010#endif /* CONFIG_ECC */