blob: 071a4dcc92262fcdb0b839bd28e6da86a1eff090 [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 Shmidt807291d2015-01-27 13:40:23 -08003 * Copyright (c) 2004-2015, 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 Shmidt8d520ff2011-05-09 14:06:53 -070032#include "crypto.h"
33
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -080034#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
35/* Compatibility wrappers for older versions. */
36
37static int EVP_CIPHER_CTX_reset(EVP_CIPHER_CTX *ctx)
38{
39 return EVP_CIPHER_CTX_cleanup(ctx);
40}
41
42
43static HMAC_CTX * HMAC_CTX_new(void)
44{
45 HMAC_CTX *ctx;
46
47 ctx = os_zalloc(sizeof(*ctx));
48 if (ctx)
49 HMAC_CTX_init(ctx);
50 return ctx;
51}
52
53
54static void HMAC_CTX_free(HMAC_CTX *ctx)
55{
56 bin_clear_free(ctx, sizeof(*ctx));
57}
58
59
60static EVP_MD_CTX * EVP_MD_CTX_new(void)
61{
62 EVP_MD_CTX *ctx;
63
64 ctx = os_zalloc(sizeof(*ctx));
65 if (ctx)
66 EVP_MD_CTX_init(ctx);
67 return ctx;
68}
69
70
71static void EVP_MD_CTX_free(EVP_MD_CTX *ctx)
72{
73 bin_clear_free(ctx, sizeof(*ctx));
74}
75
76#endif /* OpenSSL version < 1.1.0 */
77
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070078static BIGNUM * get_group5_prime(void)
79{
Dmitry Shmidt216983b2015-02-06 10:50:36 -080080#ifdef OPENSSL_IS_BORINGSSL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070081 static const unsigned char RFC3526_PRIME_1536[] = {
82 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xC9,0x0F,0xDA,0xA2,
83 0x21,0x68,0xC2,0x34,0xC4,0xC6,0x62,0x8B,0x80,0xDC,0x1C,0xD1,
84 0x29,0x02,0x4E,0x08,0x8A,0x67,0xCC,0x74,0x02,0x0B,0xBE,0xA6,
85 0x3B,0x13,0x9B,0x22,0x51,0x4A,0x08,0x79,0x8E,0x34,0x04,0xDD,
86 0xEF,0x95,0x19,0xB3,0xCD,0x3A,0x43,0x1B,0x30,0x2B,0x0A,0x6D,
87 0xF2,0x5F,0x14,0x37,0x4F,0xE1,0x35,0x6D,0x6D,0x51,0xC2,0x45,
88 0xE4,0x85,0xB5,0x76,0x62,0x5E,0x7E,0xC6,0xF4,0x4C,0x42,0xE9,
89 0xA6,0x37,0xED,0x6B,0x0B,0xFF,0x5C,0xB6,0xF4,0x06,0xB7,0xED,
90 0xEE,0x38,0x6B,0xFB,0x5A,0x89,0x9F,0xA5,0xAE,0x9F,0x24,0x11,
91 0x7C,0x4B,0x1F,0xE6,0x49,0x28,0x66,0x51,0xEC,0xE4,0x5B,0x3D,
92 0xC2,0x00,0x7C,0xB8,0xA1,0x63,0xBF,0x05,0x98,0xDA,0x48,0x36,
93 0x1C,0x55,0xD3,0x9A,0x69,0x16,0x3F,0xA8,0xFD,0x24,0xCF,0x5F,
94 0x83,0x65,0x5D,0x23,0xDC,0xA3,0xAD,0x96,0x1C,0x62,0xF3,0x56,
95 0x20,0x85,0x52,0xBB,0x9E,0xD5,0x29,0x07,0x70,0x96,0x96,0x6D,
96 0x67,0x0C,0x35,0x4E,0x4A,0xBC,0x98,0x04,0xF1,0x74,0x6C,0x08,
97 0xCA,0x23,0x73,0x27,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
98 };
99 return BN_bin2bn(RFC3526_PRIME_1536, sizeof(RFC3526_PRIME_1536), NULL);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800100#else /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700101 return get_rfc3526_prime_1536(NULL);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800102#endif /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700103}
104
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700105#ifdef OPENSSL_NO_SHA256
106#define NO_SHA256_WRAPPER
107#endif
108
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700109static int openssl_digest_vector(const EVP_MD *type, size_t num_elem,
110 const u8 *addr[], const size_t *len, u8 *mac)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700111{
Dmitry Shmidt55840ad2015-12-14 12:45:46 -0800112 EVP_MD_CTX *ctx;
113 size_t i;
114 unsigned int mac_len;
115
116 if (TEST_FAIL())
117 return -1;
118
119 ctx = EVP_MD_CTX_new();
120 if (!ctx)
121 return -1;
122 if (!EVP_DigestInit_ex(ctx, type, NULL)) {
123 wpa_printf(MSG_ERROR, "OpenSSL: EVP_DigestInit_ex failed: %s",
124 ERR_error_string(ERR_get_error(), NULL));
125 EVP_MD_CTX_free(ctx);
126 return -1;
127 }
128 for (i = 0; i < num_elem; i++) {
129 if (!EVP_DigestUpdate(ctx, addr[i], len[i])) {
130 wpa_printf(MSG_ERROR, "OpenSSL: EVP_DigestUpdate "
131 "failed: %s",
132 ERR_error_string(ERR_get_error(), NULL));
133 EVP_MD_CTX_free(ctx);
134 return -1;
135 }
136 }
137 if (!EVP_DigestFinal(ctx, mac, &mac_len)) {
138 wpa_printf(MSG_ERROR, "OpenSSL: EVP_DigestFinal failed: %s",
139 ERR_error_string(ERR_get_error(), NULL));
140 EVP_MD_CTX_free(ctx);
141 return -1;
142 }
143 EVP_MD_CTX_free(ctx);
144
145 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700146}
147
148
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800149#ifndef CONFIG_FIPS
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700150int md4_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
151{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700152 return openssl_digest_vector(EVP_md4(), num_elem, addr, len, mac);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700153}
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800154#endif /* CONFIG_FIPS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700155
156
157void des_encrypt(const u8 *clear, const u8 *key, u8 *cypher)
158{
159 u8 pkey[8], next, tmp;
160 int i;
161 DES_key_schedule ks;
162
163 /* Add parity bits to the key */
164 next = 0;
165 for (i = 0; i < 7; i++) {
166 tmp = key[i];
167 pkey[i] = (tmp >> i) | next | 1;
168 next = tmp << (7 - i);
169 }
170 pkey[i] = next | 1;
171
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -0700172 DES_set_key((DES_cblock *) &pkey, &ks);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700173 DES_ecb_encrypt((DES_cblock *) clear, (DES_cblock *) cypher, &ks,
174 DES_ENCRYPT);
175}
176
177
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800178#ifndef CONFIG_NO_RC4
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700179int rc4_skip(const u8 *key, size_t keylen, size_t skip,
180 u8 *data, size_t data_len)
181{
182#ifdef OPENSSL_NO_RC4
183 return -1;
184#else /* OPENSSL_NO_RC4 */
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800185 EVP_CIPHER_CTX *ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700186 int outl;
187 int res = -1;
188 unsigned char skip_buf[16];
189
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800190 ctx = EVP_CIPHER_CTX_new();
191 if (!ctx ||
192 !EVP_CIPHER_CTX_set_padding(ctx, 0) ||
193 !EVP_CipherInit_ex(ctx, EVP_rc4(), NULL, NULL, NULL, 1) ||
194 !EVP_CIPHER_CTX_set_key_length(ctx, keylen) ||
195 !EVP_CipherInit_ex(ctx, NULL, NULL, key, NULL, 1))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700196 goto out;
197
198 while (skip >= sizeof(skip_buf)) {
199 size_t len = skip;
200 if (len > sizeof(skip_buf))
201 len = sizeof(skip_buf);
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800202 if (!EVP_CipherUpdate(ctx, skip_buf, &outl, skip_buf, len))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700203 goto out;
204 skip -= len;
205 }
206
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800207 if (EVP_CipherUpdate(ctx, data, &outl, data, data_len))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700208 res = 0;
209
210out:
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800211 if (ctx)
212 EVP_CIPHER_CTX_reset(ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700213 return res;
214#endif /* OPENSSL_NO_RC4 */
215}
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800216#endif /* CONFIG_NO_RC4 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700217
218
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800219#ifndef CONFIG_FIPS
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700220int md5_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
221{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700222 return openssl_digest_vector(EVP_md5(), num_elem, addr, len, mac);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700223}
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800224#endif /* CONFIG_FIPS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700225
226
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700227int sha1_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
228{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700229 return openssl_digest_vector(EVP_sha1(), num_elem, addr, len, mac);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700230}
231
232
233#ifndef NO_SHA256_WRAPPER
234int sha256_vector(size_t num_elem, const u8 *addr[], const size_t *len,
235 u8 *mac)
236{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700237 return openssl_digest_vector(EVP_sha256(), num_elem, addr, len, mac);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700238}
239#endif /* NO_SHA256_WRAPPER */
240
241
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700242static const EVP_CIPHER * aes_get_evp_cipher(size_t keylen)
243{
244 switch (keylen) {
245 case 16:
246 return EVP_aes_128_ecb();
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -0700247#ifndef OPENSSL_IS_BORINGSSL
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700248 case 24:
249 return EVP_aes_192_ecb();
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -0700250#endif /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700251 case 32:
252 return EVP_aes_256_ecb();
253 }
254
255 return NULL;
256}
257
258
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700259void * aes_encrypt_init(const u8 *key, size_t len)
260{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700261 EVP_CIPHER_CTX *ctx;
262 const EVP_CIPHER *type;
263
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800264 if (TEST_FAIL())
265 return NULL;
266
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700267 type = aes_get_evp_cipher(len);
268 if (type == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700269 return NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700270
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800271 ctx = EVP_CIPHER_CTX_new();
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700272 if (ctx == NULL)
273 return NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700274 if (EVP_EncryptInit_ex(ctx, type, NULL, key, NULL) != 1) {
275 os_free(ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700276 return NULL;
277 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700278 EVP_CIPHER_CTX_set_padding(ctx, 0);
279 return ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700280}
281
282
283void aes_encrypt(void *ctx, const u8 *plain, u8 *crypt)
284{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700285 EVP_CIPHER_CTX *c = ctx;
286 int clen = 16;
287 if (EVP_EncryptUpdate(c, crypt, &clen, plain, 16) != 1) {
288 wpa_printf(MSG_ERROR, "OpenSSL: EVP_EncryptUpdate failed: %s",
289 ERR_error_string(ERR_get_error(), NULL));
290 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700291}
292
293
294void aes_encrypt_deinit(void *ctx)
295{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700296 EVP_CIPHER_CTX *c = ctx;
297 u8 buf[16];
298 int len = sizeof(buf);
299 if (EVP_EncryptFinal_ex(c, buf, &len) != 1) {
300 wpa_printf(MSG_ERROR, "OpenSSL: EVP_EncryptFinal_ex failed: "
301 "%s", ERR_error_string(ERR_get_error(), NULL));
302 }
303 if (len != 0) {
304 wpa_printf(MSG_ERROR, "OpenSSL: Unexpected padding length %d "
305 "in AES encrypt", len);
306 }
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800307 EVP_CIPHER_CTX_reset(c);
308 EVP_CIPHER_CTX_free(c);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700309}
310
311
312void * aes_decrypt_init(const u8 *key, size_t len)
313{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700314 EVP_CIPHER_CTX *ctx;
315 const EVP_CIPHER *type;
316
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800317 if (TEST_FAIL())
318 return NULL;
319
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700320 type = aes_get_evp_cipher(len);
321 if (type == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700322 return NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700323
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800324 ctx = EVP_CIPHER_CTX_new();
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700325 if (ctx == NULL)
326 return NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700327 if (EVP_DecryptInit_ex(ctx, type, NULL, key, NULL) != 1) {
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800328 EVP_CIPHER_CTX_free(ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700329 return NULL;
330 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700331 EVP_CIPHER_CTX_set_padding(ctx, 0);
332 return ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700333}
334
335
336void aes_decrypt(void *ctx, const u8 *crypt, u8 *plain)
337{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700338 EVP_CIPHER_CTX *c = ctx;
339 int plen = 16;
340 if (EVP_DecryptUpdate(c, plain, &plen, crypt, 16) != 1) {
341 wpa_printf(MSG_ERROR, "OpenSSL: EVP_DecryptUpdate failed: %s",
342 ERR_error_string(ERR_get_error(), NULL));
343 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700344}
345
346
347void aes_decrypt_deinit(void *ctx)
348{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700349 EVP_CIPHER_CTX *c = ctx;
350 u8 buf[16];
351 int len = sizeof(buf);
352 if (EVP_DecryptFinal_ex(c, buf, &len) != 1) {
353 wpa_printf(MSG_ERROR, "OpenSSL: EVP_DecryptFinal_ex failed: "
354 "%s", ERR_error_string(ERR_get_error(), NULL));
355 }
356 if (len != 0) {
357 wpa_printf(MSG_ERROR, "OpenSSL: Unexpected padding length %d "
358 "in AES decrypt", len);
359 }
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800360 EVP_CIPHER_CTX_reset(c);
361 EVP_CIPHER_CTX_free(c);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700362}
363
364
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800365#ifndef CONFIG_FIPS
366#ifndef CONFIG_OPENSSL_INTERNAL_AES_WRAP
367
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800368int aes_wrap(const u8 *kek, size_t kek_len, int n, const u8 *plain, u8 *cipher)
369{
370 AES_KEY actx;
371 int res;
372
373 if (AES_set_encrypt_key(kek, kek_len << 3, &actx))
374 return -1;
375 res = AES_wrap_key(&actx, NULL, cipher, plain, n * 8);
376 OPENSSL_cleanse(&actx, sizeof(actx));
377 return res <= 0 ? -1 : 0;
378}
379
380
381int aes_unwrap(const u8 *kek, size_t kek_len, int n, const u8 *cipher,
382 u8 *plain)
383{
384 AES_KEY actx;
385 int res;
386
387 if (AES_set_decrypt_key(kek, kek_len << 3, &actx))
388 return -1;
389 res = AES_unwrap_key(&actx, NULL, plain, cipher, (n + 1) * 8);
390 OPENSSL_cleanse(&actx, sizeof(actx));
391 return res <= 0 ? -1 : 0;
392}
393
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800394#endif /* CONFIG_OPENSSL_INTERNAL_AES_WRAP */
395#endif /* CONFIG_FIPS */
396
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800397
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -0700398int aes_128_cbc_encrypt(const u8 *key, const u8 *iv, u8 *data, size_t data_len)
399{
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800400 EVP_CIPHER_CTX *ctx;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -0700401 int clen, len;
402 u8 buf[16];
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800403 int res = -1;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -0700404
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -0800405 if (TEST_FAIL())
406 return -1;
407
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800408 ctx = EVP_CIPHER_CTX_new();
409 if (!ctx)
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -0700410 return -1;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -0700411 clen = data_len;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -0700412 len = sizeof(buf);
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800413 if (EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv) == 1 &&
414 EVP_CIPHER_CTX_set_padding(ctx, 0) == 1 &&
415 EVP_EncryptUpdate(ctx, data, &clen, data, data_len) == 1 &&
416 clen == (int) data_len &&
417 EVP_EncryptFinal_ex(ctx, buf, &len) == 1 && len == 0)
418 res = 0;
419 EVP_CIPHER_CTX_reset(ctx);
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -0700420
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800421 return res;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -0700422}
423
424
425int aes_128_cbc_decrypt(const u8 *key, const u8 *iv, u8 *data, size_t data_len)
426{
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800427 EVP_CIPHER_CTX *ctx;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -0700428 int plen, len;
429 u8 buf[16];
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800430 int res = -1;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -0700431
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -0800432 if (TEST_FAIL())
433 return -1;
434
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800435 ctx = EVP_CIPHER_CTX_new();
436 if (!ctx)
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -0700437 return -1;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -0700438 plen = data_len;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -0700439 len = sizeof(buf);
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800440 if (EVP_DecryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv) == 1 &&
441 EVP_CIPHER_CTX_set_padding(ctx, 0) == 1 &&
442 EVP_DecryptUpdate(ctx, data, &plen, data, data_len) == 1 &&
443 plen == (int) data_len &&
444 EVP_DecryptFinal_ex(ctx, buf, &len) == 1 && len == 0)
445 res = 0;
446 EVP_CIPHER_CTX_reset(ctx);
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -0700447
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800448 return res;
449
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -0700450}
451
452
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700453int crypto_mod_exp(const u8 *base, size_t base_len,
454 const u8 *power, size_t power_len,
455 const u8 *modulus, size_t modulus_len,
456 u8 *result, size_t *result_len)
457{
458 BIGNUM *bn_base, *bn_exp, *bn_modulus, *bn_result;
459 int ret = -1;
460 BN_CTX *ctx;
461
462 ctx = BN_CTX_new();
463 if (ctx == NULL)
464 return -1;
465
466 bn_base = BN_bin2bn(base, base_len, NULL);
467 bn_exp = BN_bin2bn(power, power_len, NULL);
468 bn_modulus = BN_bin2bn(modulus, modulus_len, NULL);
469 bn_result = BN_new();
470
471 if (bn_base == NULL || bn_exp == NULL || bn_modulus == NULL ||
472 bn_result == NULL)
473 goto error;
474
475 if (BN_mod_exp(bn_result, bn_base, bn_exp, bn_modulus, ctx) != 1)
476 goto error;
477
478 *result_len = BN_bn2bin(bn_result, result);
479 ret = 0;
480
481error:
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -0700482 BN_clear_free(bn_base);
483 BN_clear_free(bn_exp);
484 BN_clear_free(bn_modulus);
485 BN_clear_free(bn_result);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700486 BN_CTX_free(ctx);
487 return ret;
488}
489
490
491struct crypto_cipher {
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800492 EVP_CIPHER_CTX *enc;
493 EVP_CIPHER_CTX *dec;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700494};
495
496
497struct crypto_cipher * crypto_cipher_init(enum crypto_cipher_alg alg,
498 const u8 *iv, const u8 *key,
499 size_t key_len)
500{
501 struct crypto_cipher *ctx;
502 const EVP_CIPHER *cipher;
503
504 ctx = os_zalloc(sizeof(*ctx));
505 if (ctx == NULL)
506 return NULL;
507
508 switch (alg) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800509#ifndef CONFIG_NO_RC4
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700510#ifndef OPENSSL_NO_RC4
511 case CRYPTO_CIPHER_ALG_RC4:
512 cipher = EVP_rc4();
513 break;
514#endif /* OPENSSL_NO_RC4 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800515#endif /* CONFIG_NO_RC4 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700516#ifndef OPENSSL_NO_AES
517 case CRYPTO_CIPHER_ALG_AES:
518 switch (key_len) {
519 case 16:
520 cipher = EVP_aes_128_cbc();
521 break;
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -0700522#ifndef OPENSSL_IS_BORINGSSL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700523 case 24:
524 cipher = EVP_aes_192_cbc();
525 break;
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -0700526#endif /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700527 case 32:
528 cipher = EVP_aes_256_cbc();
529 break;
530 default:
531 os_free(ctx);
532 return NULL;
533 }
534 break;
535#endif /* OPENSSL_NO_AES */
536#ifndef OPENSSL_NO_DES
537 case CRYPTO_CIPHER_ALG_3DES:
538 cipher = EVP_des_ede3_cbc();
539 break;
540 case CRYPTO_CIPHER_ALG_DES:
541 cipher = EVP_des_cbc();
542 break;
543#endif /* OPENSSL_NO_DES */
544#ifndef OPENSSL_NO_RC2
545 case CRYPTO_CIPHER_ALG_RC2:
546 cipher = EVP_rc2_ecb();
547 break;
548#endif /* OPENSSL_NO_RC2 */
549 default:
550 os_free(ctx);
551 return NULL;
552 }
553
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800554 if (!(ctx->enc = EVP_CIPHER_CTX_new()) ||
555 !EVP_CIPHER_CTX_set_padding(ctx->enc, 0) ||
556 !EVP_EncryptInit_ex(ctx->enc, cipher, NULL, NULL, NULL) ||
557 !EVP_CIPHER_CTX_set_key_length(ctx->enc, key_len) ||
558 !EVP_EncryptInit_ex(ctx->enc, NULL, NULL, key, iv)) {
559 if (ctx->enc)
560 EVP_CIPHER_CTX_reset(ctx->enc);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700561 os_free(ctx);
562 return NULL;
563 }
564
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800565 if (!(ctx->dec = EVP_CIPHER_CTX_new()) ||
566 !EVP_CIPHER_CTX_set_padding(ctx->dec, 0) ||
567 !EVP_DecryptInit_ex(ctx->dec, cipher, NULL, NULL, NULL) ||
568 !EVP_CIPHER_CTX_set_key_length(ctx->dec, key_len) ||
569 !EVP_DecryptInit_ex(ctx->dec, NULL, NULL, key, iv)) {
570 EVP_CIPHER_CTX_reset(ctx->enc);
571 if (ctx->dec)
572 EVP_CIPHER_CTX_reset(ctx->dec);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700573 os_free(ctx);
574 return NULL;
575 }
576
577 return ctx;
578}
579
580
581int crypto_cipher_encrypt(struct crypto_cipher *ctx, const u8 *plain,
582 u8 *crypt, size_t len)
583{
584 int outl;
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800585 if (!EVP_EncryptUpdate(ctx->enc, crypt, &outl, plain, len))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700586 return -1;
587 return 0;
588}
589
590
591int crypto_cipher_decrypt(struct crypto_cipher *ctx, const u8 *crypt,
592 u8 *plain, size_t len)
593{
594 int outl;
595 outl = len;
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800596 if (!EVP_DecryptUpdate(ctx->dec, plain, &outl, crypt, len))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700597 return -1;
598 return 0;
599}
600
601
602void crypto_cipher_deinit(struct crypto_cipher *ctx)
603{
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800604 EVP_CIPHER_CTX_reset(ctx->enc);
605 EVP_CIPHER_CTX_reset(ctx->dec);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700606 os_free(ctx);
607}
608
609
610void * dh5_init(struct wpabuf **priv, struct wpabuf **publ)
611{
612 DH *dh;
613 struct wpabuf *pubkey = NULL, *privkey = NULL;
614 size_t publen, privlen;
615
616 *priv = NULL;
617 *publ = NULL;
618
619 dh = DH_new();
620 if (dh == NULL)
621 return NULL;
622
623 dh->g = BN_new();
624 if (dh->g == NULL || BN_set_word(dh->g, 2) != 1)
625 goto err;
626
627 dh->p = get_group5_prime();
628 if (dh->p == NULL)
629 goto err;
630
631 if (DH_generate_key(dh) != 1)
632 goto err;
633
634 publen = BN_num_bytes(dh->pub_key);
635 pubkey = wpabuf_alloc(publen);
636 if (pubkey == NULL)
637 goto err;
638 privlen = BN_num_bytes(dh->priv_key);
639 privkey = wpabuf_alloc(privlen);
640 if (privkey == NULL)
641 goto err;
642
643 BN_bn2bin(dh->pub_key, wpabuf_put(pubkey, publen));
644 BN_bn2bin(dh->priv_key, wpabuf_put(privkey, privlen));
645
646 *priv = privkey;
647 *publ = pubkey;
648 return dh;
649
650err:
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800651 wpabuf_clear_free(pubkey);
652 wpabuf_clear_free(privkey);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700653 DH_free(dh);
654 return NULL;
655}
656
657
Dmitry Shmidt04949592012-07-19 12:16:46 -0700658void * dh5_init_fixed(const struct wpabuf *priv, const struct wpabuf *publ)
659{
660 DH *dh;
661
662 dh = DH_new();
663 if (dh == NULL)
664 return NULL;
665
666 dh->g = BN_new();
667 if (dh->g == NULL || BN_set_word(dh->g, 2) != 1)
668 goto err;
669
670 dh->p = get_group5_prime();
671 if (dh->p == NULL)
672 goto err;
673
674 dh->priv_key = BN_bin2bn(wpabuf_head(priv), wpabuf_len(priv), NULL);
675 if (dh->priv_key == NULL)
676 goto err;
677
678 dh->pub_key = BN_bin2bn(wpabuf_head(publ), wpabuf_len(publ), NULL);
679 if (dh->pub_key == NULL)
680 goto err;
681
682 if (DH_generate_key(dh) != 1)
683 goto err;
684
685 return dh;
686
687err:
688 DH_free(dh);
689 return NULL;
690}
691
692
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700693struct wpabuf * dh5_derive_shared(void *ctx, const struct wpabuf *peer_public,
694 const struct wpabuf *own_private)
695{
696 BIGNUM *pub_key;
697 struct wpabuf *res = NULL;
698 size_t rlen;
699 DH *dh = ctx;
700 int keylen;
701
702 if (ctx == NULL)
703 return NULL;
704
705 pub_key = BN_bin2bn(wpabuf_head(peer_public), wpabuf_len(peer_public),
706 NULL);
707 if (pub_key == NULL)
708 return NULL;
709
710 rlen = DH_size(dh);
711 res = wpabuf_alloc(rlen);
712 if (res == NULL)
713 goto err;
714
715 keylen = DH_compute_key(wpabuf_mhead(res), pub_key, dh);
716 if (keylen < 0)
717 goto err;
718 wpabuf_put(res, keylen);
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -0700719 BN_clear_free(pub_key);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700720
721 return res;
722
723err:
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -0700724 BN_clear_free(pub_key);
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800725 wpabuf_clear_free(res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700726 return NULL;
727}
728
729
730void dh5_free(void *ctx)
731{
732 DH *dh;
733 if (ctx == NULL)
734 return;
735 dh = ctx;
736 DH_free(dh);
737}
Dmitry Shmidt04949592012-07-19 12:16:46 -0700738
739
740struct crypto_hash {
Dmitry Shmidt55840ad2015-12-14 12:45:46 -0800741 HMAC_CTX *ctx;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700742};
743
744
745struct crypto_hash * crypto_hash_init(enum crypto_hash_alg alg, const u8 *key,
746 size_t key_len)
747{
748 struct crypto_hash *ctx;
749 const EVP_MD *md;
750
751 switch (alg) {
752#ifndef OPENSSL_NO_MD5
753 case CRYPTO_HASH_ALG_HMAC_MD5:
754 md = EVP_md5();
755 break;
756#endif /* OPENSSL_NO_MD5 */
757#ifndef OPENSSL_NO_SHA
758 case CRYPTO_HASH_ALG_HMAC_SHA1:
759 md = EVP_sha1();
760 break;
761#endif /* OPENSSL_NO_SHA */
762#ifndef OPENSSL_NO_SHA256
763#ifdef CONFIG_SHA256
764 case CRYPTO_HASH_ALG_HMAC_SHA256:
765 md = EVP_sha256();
766 break;
767#endif /* CONFIG_SHA256 */
768#endif /* OPENSSL_NO_SHA256 */
769 default:
770 return NULL;
771 }
772
773 ctx = os_zalloc(sizeof(*ctx));
774 if (ctx == NULL)
775 return NULL;
Dmitry Shmidt55840ad2015-12-14 12:45:46 -0800776 ctx->ctx = HMAC_CTX_new();
777 if (!ctx->ctx) {
778 os_free(ctx);
779 return NULL;
780 }
781
782 if (HMAC_Init_ex(ctx->ctx, key, key_len, md, NULL) != 1) {
783 HMAC_CTX_free(ctx->ctx);
784 bin_clear_free(ctx, sizeof(*ctx));
785 return NULL;
786 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700787
788 return ctx;
789}
790
791
792void crypto_hash_update(struct crypto_hash *ctx, const u8 *data, size_t len)
793{
794 if (ctx == NULL)
795 return;
Dmitry Shmidt55840ad2015-12-14 12:45:46 -0800796 HMAC_Update(ctx->ctx, data, len);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700797}
798
799
800int crypto_hash_finish(struct crypto_hash *ctx, u8 *mac, size_t *len)
801{
802 unsigned int mdlen;
803 int res;
804
805 if (ctx == NULL)
806 return -2;
807
808 if (mac == NULL || len == NULL) {
Dmitry Shmidt55840ad2015-12-14 12:45:46 -0800809 HMAC_CTX_free(ctx->ctx);
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800810 bin_clear_free(ctx, sizeof(*ctx));
Dmitry Shmidt04949592012-07-19 12:16:46 -0700811 return 0;
812 }
813
814 mdlen = *len;
Dmitry Shmidt55840ad2015-12-14 12:45:46 -0800815 res = HMAC_Final(ctx->ctx, mac, &mdlen);
816 HMAC_CTX_free(ctx->ctx);
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800817 bin_clear_free(ctx, sizeof(*ctx));
Dmitry Shmidt04949592012-07-19 12:16:46 -0700818
819 if (res == 1) {
820 *len = mdlen;
821 return 0;
822 }
823
824 return -1;
825}
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700826
827
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800828static int openssl_hmac_vector(const EVP_MD *type, const u8 *key,
829 size_t key_len, size_t num_elem,
830 const u8 *addr[], const size_t *len, u8 *mac,
831 unsigned int mdlen)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700832{
Dmitry Shmidt55840ad2015-12-14 12:45:46 -0800833 HMAC_CTX *ctx;
834 size_t i;
835 int res;
836
837 if (TEST_FAIL())
838 return -1;
839
840 ctx = HMAC_CTX_new();
841 if (!ctx)
842 return -1;
843 res = HMAC_Init_ex(ctx, key, key_len, type, NULL);
844 if (res != 1)
845 goto done;
846
847 for (i = 0; i < num_elem; i++)
848 HMAC_Update(ctx, addr[i], len[i]);
849
850 res = HMAC_Final(ctx, mac, &mdlen);
851done:
852 HMAC_CTX_free(ctx);
853
854 return res == 1 ? 0 : -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700855}
856
857
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800858#ifndef CONFIG_FIPS
859
860int hmac_md5_vector(const u8 *key, size_t key_len, size_t num_elem,
861 const u8 *addr[], const size_t *len, u8 *mac)
862{
863 return openssl_hmac_vector(EVP_md5(), key ,key_len, num_elem, addr, len,
864 mac, 16);
865}
866
867
868int hmac_md5(const u8 *key, size_t key_len, const u8 *data, size_t data_len,
869 u8 *mac)
870{
871 return hmac_md5_vector(key, key_len, 1, &data, &data_len, mac);
872}
873
874#endif /* CONFIG_FIPS */
875
876
877int pbkdf2_sha1(const char *passphrase, const u8 *ssid, size_t ssid_len,
878 int iterations, u8 *buf, size_t buflen)
879{
880 if (PKCS5_PBKDF2_HMAC_SHA1(passphrase, os_strlen(passphrase), ssid,
881 ssid_len, iterations, buflen, buf) != 1)
882 return -1;
883 return 0;
884}
885
886
887int hmac_sha1_vector(const u8 *key, size_t key_len, size_t num_elem,
888 const u8 *addr[], const size_t *len, u8 *mac)
889{
890 return openssl_hmac_vector(EVP_sha1(), key, key_len, num_elem, addr,
891 len, mac, 20);
892}
893
894
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700895int hmac_sha1(const u8 *key, size_t key_len, const u8 *data, size_t data_len,
896 u8 *mac)
897{
898 return hmac_sha1_vector(key, key_len, 1, &data, &data_len, mac);
899}
900
901
902#ifdef CONFIG_SHA256
903
904int hmac_sha256_vector(const u8 *key, size_t key_len, size_t num_elem,
905 const u8 *addr[], const size_t *len, u8 *mac)
906{
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800907 return openssl_hmac_vector(EVP_sha256(), key, key_len, num_elem, addr,
908 len, mac, 32);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700909}
910
911
912int hmac_sha256(const u8 *key, size_t key_len, const u8 *data,
913 size_t data_len, u8 *mac)
914{
915 return hmac_sha256_vector(key, key_len, 1, &data, &data_len, mac);
916}
917
918#endif /* CONFIG_SHA256 */
919
920
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800921#ifdef CONFIG_SHA384
922
923int hmac_sha384_vector(const u8 *key, size_t key_len, size_t num_elem,
924 const u8 *addr[], const size_t *len, u8 *mac)
925{
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800926 return openssl_hmac_vector(EVP_sha384(), key, key_len, num_elem, addr,
927 len, mac, 32);
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800928}
929
930
931int hmac_sha384(const u8 *key, size_t key_len, const u8 *data,
932 size_t data_len, u8 *mac)
933{
934 return hmac_sha384_vector(key, key_len, 1, &data, &data_len, mac);
935}
936
937#endif /* CONFIG_SHA384 */
938
939
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700940int crypto_get_random(void *buf, size_t len)
941{
942 if (RAND_bytes(buf, len) != 1)
943 return -1;
944 return 0;
945}
946
947
948#ifdef CONFIG_OPENSSL_CMAC
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800949int omac1_aes_vector(const u8 *key, size_t key_len, size_t num_elem,
950 const u8 *addr[], const size_t *len, u8 *mac)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700951{
952 CMAC_CTX *ctx;
953 int ret = -1;
954 size_t outlen, i;
955
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800956 if (TEST_FAIL())
957 return -1;
958
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700959 ctx = CMAC_CTX_new();
960 if (ctx == NULL)
961 return -1;
962
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800963 if (key_len == 32) {
964 if (!CMAC_Init(ctx, key, 32, EVP_aes_256_cbc(), NULL))
965 goto fail;
966 } else if (key_len == 16) {
967 if (!CMAC_Init(ctx, key, 16, EVP_aes_128_cbc(), NULL))
968 goto fail;
969 } else {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700970 goto fail;
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800971 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700972 for (i = 0; i < num_elem; i++) {
973 if (!CMAC_Update(ctx, addr[i], len[i]))
974 goto fail;
975 }
976 if (!CMAC_Final(ctx, mac, &outlen) || outlen != 16)
977 goto fail;
978
979 ret = 0;
980fail:
981 CMAC_CTX_free(ctx);
982 return ret;
983}
984
985
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800986int omac1_aes_128_vector(const u8 *key, size_t num_elem,
987 const u8 *addr[], const size_t *len, u8 *mac)
988{
989 return omac1_aes_vector(key, 16, num_elem, addr, len, mac);
990}
991
992
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700993int omac1_aes_128(const u8 *key, const u8 *data, size_t data_len, u8 *mac)
994{
995 return omac1_aes_128_vector(key, 1, &data, &data_len, mac);
996}
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800997
998
999int omac1_aes_256(const u8 *key, const u8 *data, size_t data_len, u8 *mac)
1000{
1001 return omac1_aes_vector(key, 32, 1, &data, &data_len, mac);
1002}
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001003#endif /* CONFIG_OPENSSL_CMAC */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001004
1005
1006struct crypto_bignum * crypto_bignum_init(void)
1007{
1008 return (struct crypto_bignum *) BN_new();
1009}
1010
1011
1012struct crypto_bignum * crypto_bignum_init_set(const u8 *buf, size_t len)
1013{
1014 BIGNUM *bn = BN_bin2bn(buf, len, NULL);
1015 return (struct crypto_bignum *) bn;
1016}
1017
1018
1019void crypto_bignum_deinit(struct crypto_bignum *n, int clear)
1020{
1021 if (clear)
1022 BN_clear_free((BIGNUM *) n);
1023 else
1024 BN_free((BIGNUM *) n);
1025}
1026
1027
1028int crypto_bignum_to_bin(const struct crypto_bignum *a,
1029 u8 *buf, size_t buflen, size_t padlen)
1030{
1031 int num_bytes, offset;
1032
1033 if (padlen > buflen)
1034 return -1;
1035
1036 num_bytes = BN_num_bytes((const BIGNUM *) a);
1037 if ((size_t) num_bytes > buflen)
1038 return -1;
1039 if (padlen > (size_t) num_bytes)
1040 offset = padlen - num_bytes;
1041 else
1042 offset = 0;
1043
1044 os_memset(buf, 0, offset);
1045 BN_bn2bin((const BIGNUM *) a, buf + offset);
1046
1047 return num_bytes + offset;
1048}
1049
1050
1051int crypto_bignum_add(const struct crypto_bignum *a,
1052 const struct crypto_bignum *b,
1053 struct crypto_bignum *c)
1054{
1055 return BN_add((BIGNUM *) c, (const BIGNUM *) a, (const BIGNUM *) b) ?
1056 0 : -1;
1057}
1058
1059
1060int crypto_bignum_mod(const struct crypto_bignum *a,
1061 const struct crypto_bignum *b,
1062 struct crypto_bignum *c)
1063{
1064 int res;
1065 BN_CTX *bnctx;
1066
1067 bnctx = BN_CTX_new();
1068 if (bnctx == NULL)
1069 return -1;
1070 res = BN_mod((BIGNUM *) c, (const BIGNUM *) a, (const BIGNUM *) b,
1071 bnctx);
1072 BN_CTX_free(bnctx);
1073
1074 return res ? 0 : -1;
1075}
1076
1077
1078int crypto_bignum_exptmod(const struct crypto_bignum *a,
1079 const struct crypto_bignum *b,
1080 const struct crypto_bignum *c,
1081 struct crypto_bignum *d)
1082{
1083 int res;
1084 BN_CTX *bnctx;
1085
1086 bnctx = BN_CTX_new();
1087 if (bnctx == NULL)
1088 return -1;
1089 res = BN_mod_exp((BIGNUM *) d, (const BIGNUM *) a, (const BIGNUM *) b,
1090 (const BIGNUM *) c, bnctx);
1091 BN_CTX_free(bnctx);
1092
1093 return res ? 0 : -1;
1094}
1095
1096
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001097int crypto_bignum_inverse(const struct crypto_bignum *a,
1098 const struct crypto_bignum *b,
1099 struct crypto_bignum *c)
1100{
1101 BIGNUM *res;
1102 BN_CTX *bnctx;
1103
1104 bnctx = BN_CTX_new();
1105 if (bnctx == NULL)
1106 return -1;
1107 res = BN_mod_inverse((BIGNUM *) c, (const BIGNUM *) a,
1108 (const BIGNUM *) b, bnctx);
1109 BN_CTX_free(bnctx);
1110
1111 return res ? 0 : -1;
1112}
1113
1114
1115int crypto_bignum_sub(const struct crypto_bignum *a,
1116 const struct crypto_bignum *b,
1117 struct crypto_bignum *c)
1118{
1119 return BN_sub((BIGNUM *) c, (const BIGNUM *) a, (const BIGNUM *) b) ?
1120 0 : -1;
1121}
1122
1123
1124int crypto_bignum_div(const struct crypto_bignum *a,
1125 const struct crypto_bignum *b,
1126 struct crypto_bignum *c)
1127{
1128 int res;
1129
1130 BN_CTX *bnctx;
1131
1132 bnctx = BN_CTX_new();
1133 if (bnctx == NULL)
1134 return -1;
1135 res = BN_div((BIGNUM *) c, NULL, (const BIGNUM *) a,
1136 (const BIGNUM *) b, bnctx);
1137 BN_CTX_free(bnctx);
1138
1139 return res ? 0 : -1;
1140}
1141
1142
1143int crypto_bignum_mulmod(const struct crypto_bignum *a,
1144 const struct crypto_bignum *b,
1145 const struct crypto_bignum *c,
1146 struct crypto_bignum *d)
1147{
1148 int res;
1149
1150 BN_CTX *bnctx;
1151
1152 bnctx = BN_CTX_new();
1153 if (bnctx == NULL)
1154 return -1;
1155 res = BN_mod_mul((BIGNUM *) d, (const BIGNUM *) a, (const BIGNUM *) b,
1156 (const BIGNUM *) c, bnctx);
1157 BN_CTX_free(bnctx);
1158
1159 return res ? 0 : -1;
1160}
1161
1162
1163int crypto_bignum_cmp(const struct crypto_bignum *a,
1164 const struct crypto_bignum *b)
1165{
1166 return BN_cmp((const BIGNUM *) a, (const BIGNUM *) b);
1167}
1168
1169
1170int crypto_bignum_bits(const struct crypto_bignum *a)
1171{
1172 return BN_num_bits((const BIGNUM *) a);
1173}
1174
1175
1176int crypto_bignum_is_zero(const struct crypto_bignum *a)
1177{
1178 return BN_is_zero((const BIGNUM *) a);
1179}
1180
1181
1182int crypto_bignum_is_one(const struct crypto_bignum *a)
1183{
1184 return BN_is_one((const BIGNUM *) a);
1185}
1186
1187
Dmitry Shmidt41712582015-06-29 11:02:15 -07001188int crypto_bignum_legendre(const struct crypto_bignum *a,
1189 const struct crypto_bignum *p)
1190{
1191 BN_CTX *bnctx;
1192 BIGNUM *exp = NULL, *tmp = NULL;
1193 int res = -2;
1194
1195 bnctx = BN_CTX_new();
1196 if (bnctx == NULL)
1197 return -2;
1198
1199 exp = BN_new();
1200 tmp = BN_new();
1201 if (!exp || !tmp ||
1202 /* exp = (p-1) / 2 */
1203 !BN_sub(exp, (const BIGNUM *) p, BN_value_one()) ||
1204 !BN_rshift1(exp, exp) ||
1205 !BN_mod_exp(tmp, (const BIGNUM *) a, exp, (const BIGNUM *) p,
1206 bnctx))
1207 goto fail;
1208
1209 if (BN_is_word(tmp, 1))
1210 res = 1;
1211 else if (BN_is_zero(tmp))
1212 res = 0;
1213 else
1214 res = -1;
1215
1216fail:
1217 BN_clear_free(tmp);
1218 BN_clear_free(exp);
1219 BN_CTX_free(bnctx);
1220 return res;
1221}
1222
1223
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001224#ifdef CONFIG_ECC
1225
1226struct crypto_ec {
1227 EC_GROUP *group;
1228 BN_CTX *bnctx;
1229 BIGNUM *prime;
1230 BIGNUM *order;
Dmitry Shmidt41712582015-06-29 11:02:15 -07001231 BIGNUM *a;
1232 BIGNUM *b;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001233};
1234
1235struct crypto_ec * crypto_ec_init(int group)
1236{
1237 struct crypto_ec *e;
1238 int nid;
1239
1240 /* Map from IANA registry for IKE D-H groups to OpenSSL NID */
1241 switch (group) {
1242 case 19:
1243 nid = NID_X9_62_prime256v1;
1244 break;
1245 case 20:
1246 nid = NID_secp384r1;
1247 break;
1248 case 21:
1249 nid = NID_secp521r1;
1250 break;
1251 case 25:
1252 nid = NID_X9_62_prime192v1;
1253 break;
1254 case 26:
1255 nid = NID_secp224r1;
1256 break;
Dmitry Shmidt41712582015-06-29 11:02:15 -07001257#ifdef NID_brainpoolP224r1
1258 case 27:
1259 nid = NID_brainpoolP224r1;
1260 break;
1261#endif /* NID_brainpoolP224r1 */
1262#ifdef NID_brainpoolP256r1
1263 case 28:
1264 nid = NID_brainpoolP256r1;
1265 break;
1266#endif /* NID_brainpoolP256r1 */
1267#ifdef NID_brainpoolP384r1
1268 case 29:
1269 nid = NID_brainpoolP384r1;
1270 break;
1271#endif /* NID_brainpoolP384r1 */
1272#ifdef NID_brainpoolP512r1
1273 case 30:
1274 nid = NID_brainpoolP512r1;
1275 break;
1276#endif /* NID_brainpoolP512r1 */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001277 default:
1278 return NULL;
1279 }
1280
1281 e = os_zalloc(sizeof(*e));
1282 if (e == NULL)
1283 return NULL;
1284
1285 e->bnctx = BN_CTX_new();
1286 e->group = EC_GROUP_new_by_curve_name(nid);
1287 e->prime = BN_new();
1288 e->order = BN_new();
Dmitry Shmidt41712582015-06-29 11:02:15 -07001289 e->a = BN_new();
1290 e->b = BN_new();
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001291 if (e->group == NULL || e->bnctx == NULL || e->prime == NULL ||
Dmitry Shmidt41712582015-06-29 11:02:15 -07001292 e->order == NULL || e->a == NULL || e->b == NULL ||
1293 !EC_GROUP_get_curve_GFp(e->group, e->prime, e->a, e->b, e->bnctx) ||
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001294 !EC_GROUP_get_order(e->group, e->order, e->bnctx)) {
1295 crypto_ec_deinit(e);
1296 e = NULL;
1297 }
1298
1299 return e;
1300}
1301
1302
1303void crypto_ec_deinit(struct crypto_ec *e)
1304{
1305 if (e == NULL)
1306 return;
Dmitry Shmidt41712582015-06-29 11:02:15 -07001307 BN_clear_free(e->b);
1308 BN_clear_free(e->a);
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001309 BN_clear_free(e->order);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001310 BN_clear_free(e->prime);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001311 EC_GROUP_free(e->group);
1312 BN_CTX_free(e->bnctx);
1313 os_free(e);
1314}
1315
1316
1317struct crypto_ec_point * crypto_ec_point_init(struct crypto_ec *e)
1318{
1319 if (e == NULL)
1320 return NULL;
1321 return (struct crypto_ec_point *) EC_POINT_new(e->group);
1322}
1323
1324
1325size_t crypto_ec_prime_len(struct crypto_ec *e)
1326{
1327 return BN_num_bytes(e->prime);
1328}
1329
1330
1331size_t crypto_ec_prime_len_bits(struct crypto_ec *e)
1332{
1333 return BN_num_bits(e->prime);
1334}
1335
1336
1337const struct crypto_bignum * crypto_ec_get_prime(struct crypto_ec *e)
1338{
1339 return (const struct crypto_bignum *) e->prime;
1340}
1341
1342
1343const struct crypto_bignum * crypto_ec_get_order(struct crypto_ec *e)
1344{
1345 return (const struct crypto_bignum *) e->order;
1346}
1347
1348
1349void crypto_ec_point_deinit(struct crypto_ec_point *p, int clear)
1350{
1351 if (clear)
1352 EC_POINT_clear_free((EC_POINT *) p);
1353 else
1354 EC_POINT_free((EC_POINT *) p);
1355}
1356
1357
1358int crypto_ec_point_to_bin(struct crypto_ec *e,
1359 const struct crypto_ec_point *point, u8 *x, u8 *y)
1360{
1361 BIGNUM *x_bn, *y_bn;
1362 int ret = -1;
1363 int len = BN_num_bytes(e->prime);
1364
1365 x_bn = BN_new();
1366 y_bn = BN_new();
1367
1368 if (x_bn && y_bn &&
1369 EC_POINT_get_affine_coordinates_GFp(e->group, (EC_POINT *) point,
1370 x_bn, y_bn, e->bnctx)) {
1371 if (x) {
1372 crypto_bignum_to_bin((struct crypto_bignum *) x_bn,
1373 x, len, len);
1374 }
1375 if (y) {
1376 crypto_bignum_to_bin((struct crypto_bignum *) y_bn,
1377 y, len, len);
1378 }
1379 ret = 0;
1380 }
1381
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001382 BN_clear_free(x_bn);
1383 BN_clear_free(y_bn);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001384 return ret;
1385}
1386
1387
1388struct crypto_ec_point * crypto_ec_point_from_bin(struct crypto_ec *e,
1389 const u8 *val)
1390{
1391 BIGNUM *x, *y;
1392 EC_POINT *elem;
1393 int len = BN_num_bytes(e->prime);
1394
1395 x = BN_bin2bn(val, len, NULL);
1396 y = BN_bin2bn(val + len, len, NULL);
1397 elem = EC_POINT_new(e->group);
1398 if (x == NULL || y == NULL || elem == NULL) {
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001399 BN_clear_free(x);
1400 BN_clear_free(y);
1401 EC_POINT_clear_free(elem);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001402 return NULL;
1403 }
1404
1405 if (!EC_POINT_set_affine_coordinates_GFp(e->group, elem, x, y,
1406 e->bnctx)) {
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001407 EC_POINT_clear_free(elem);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001408 elem = NULL;
1409 }
1410
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001411 BN_clear_free(x);
1412 BN_clear_free(y);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001413
1414 return (struct crypto_ec_point *) elem;
1415}
1416
1417
1418int crypto_ec_point_add(struct crypto_ec *e, const struct crypto_ec_point *a,
1419 const struct crypto_ec_point *b,
1420 struct crypto_ec_point *c)
1421{
1422 return EC_POINT_add(e->group, (EC_POINT *) c, (const EC_POINT *) a,
1423 (const EC_POINT *) b, e->bnctx) ? 0 : -1;
1424}
1425
1426
1427int crypto_ec_point_mul(struct crypto_ec *e, const struct crypto_ec_point *p,
1428 const struct crypto_bignum *b,
1429 struct crypto_ec_point *res)
1430{
1431 return EC_POINT_mul(e->group, (EC_POINT *) res, NULL,
1432 (const EC_POINT *) p, (const BIGNUM *) b, e->bnctx)
1433 ? 0 : -1;
1434}
1435
1436
1437int crypto_ec_point_invert(struct crypto_ec *e, struct crypto_ec_point *p)
1438{
1439 return EC_POINT_invert(e->group, (EC_POINT *) p, e->bnctx) ? 0 : -1;
1440}
1441
1442
1443int crypto_ec_point_solve_y_coord(struct crypto_ec *e,
1444 struct crypto_ec_point *p,
1445 const struct crypto_bignum *x, int y_bit)
1446{
1447 if (!EC_POINT_set_compressed_coordinates_GFp(e->group, (EC_POINT *) p,
1448 (const BIGNUM *) x, y_bit,
1449 e->bnctx) ||
1450 !EC_POINT_is_on_curve(e->group, (EC_POINT *) p, e->bnctx))
1451 return -1;
1452 return 0;
1453}
1454
1455
Dmitry Shmidt41712582015-06-29 11:02:15 -07001456struct crypto_bignum *
1457crypto_ec_point_compute_y_sqr(struct crypto_ec *e,
1458 const struct crypto_bignum *x)
1459{
1460 BIGNUM *tmp, *tmp2, *y_sqr = NULL;
1461
1462 tmp = BN_new();
1463 tmp2 = BN_new();
1464
1465 /* y^2 = x^3 + ax + b */
1466 if (tmp && tmp2 &&
1467 BN_mod_sqr(tmp, (const BIGNUM *) x, e->prime, e->bnctx) &&
1468 BN_mod_mul(tmp, tmp, (const BIGNUM *) x, e->prime, e->bnctx) &&
1469 BN_mod_mul(tmp2, e->a, (const BIGNUM *) x, e->prime, e->bnctx) &&
1470 BN_mod_add_quick(tmp2, tmp2, tmp, e->prime) &&
1471 BN_mod_add_quick(tmp2, tmp2, e->b, e->prime)) {
1472 y_sqr = tmp2;
1473 tmp2 = NULL;
1474 }
1475
1476 BN_clear_free(tmp);
1477 BN_clear_free(tmp2);
1478
1479 return (struct crypto_bignum *) y_sqr;
1480}
1481
1482
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001483int crypto_ec_point_is_at_infinity(struct crypto_ec *e,
1484 const struct crypto_ec_point *p)
1485{
1486 return EC_POINT_is_at_infinity(e->group, (const EC_POINT *) p);
1487}
1488
1489
1490int crypto_ec_point_is_on_curve(struct crypto_ec *e,
1491 const struct crypto_ec_point *p)
1492{
Dmitry Shmidt41712582015-06-29 11:02:15 -07001493 return EC_POINT_is_on_curve(e->group, (const EC_POINT *) p,
1494 e->bnctx) == 1;
1495}
1496
1497
1498int crypto_ec_point_cmp(const struct crypto_ec *e,
1499 const struct crypto_ec_point *a,
1500 const struct crypto_ec_point *b)
1501{
1502 return EC_POINT_cmp(e->group, (const EC_POINT *) a,
1503 (const EC_POINT *) b, e->bnctx);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001504}
1505
1506#endif /* CONFIG_ECC */