blob: ad2d2d4a2c78ad31b071d5752e7e8a4eedb17404 [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 Shmidt8d520ff2011-05-09 14:06:53 -070034static BIGNUM * get_group5_prime(void)
35{
Dmitry Shmidt216983b2015-02-06 10:50:36 -080036#ifdef OPENSSL_IS_BORINGSSL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070037 static const unsigned char RFC3526_PRIME_1536[] = {
38 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xC9,0x0F,0xDA,0xA2,
39 0x21,0x68,0xC2,0x34,0xC4,0xC6,0x62,0x8B,0x80,0xDC,0x1C,0xD1,
40 0x29,0x02,0x4E,0x08,0x8A,0x67,0xCC,0x74,0x02,0x0B,0xBE,0xA6,
41 0x3B,0x13,0x9B,0x22,0x51,0x4A,0x08,0x79,0x8E,0x34,0x04,0xDD,
42 0xEF,0x95,0x19,0xB3,0xCD,0x3A,0x43,0x1B,0x30,0x2B,0x0A,0x6D,
43 0xF2,0x5F,0x14,0x37,0x4F,0xE1,0x35,0x6D,0x6D,0x51,0xC2,0x45,
44 0xE4,0x85,0xB5,0x76,0x62,0x5E,0x7E,0xC6,0xF4,0x4C,0x42,0xE9,
45 0xA6,0x37,0xED,0x6B,0x0B,0xFF,0x5C,0xB6,0xF4,0x06,0xB7,0xED,
46 0xEE,0x38,0x6B,0xFB,0x5A,0x89,0x9F,0xA5,0xAE,0x9F,0x24,0x11,
47 0x7C,0x4B,0x1F,0xE6,0x49,0x28,0x66,0x51,0xEC,0xE4,0x5B,0x3D,
48 0xC2,0x00,0x7C,0xB8,0xA1,0x63,0xBF,0x05,0x98,0xDA,0x48,0x36,
49 0x1C,0x55,0xD3,0x9A,0x69,0x16,0x3F,0xA8,0xFD,0x24,0xCF,0x5F,
50 0x83,0x65,0x5D,0x23,0xDC,0xA3,0xAD,0x96,0x1C,0x62,0xF3,0x56,
51 0x20,0x85,0x52,0xBB,0x9E,0xD5,0x29,0x07,0x70,0x96,0x96,0x6D,
52 0x67,0x0C,0x35,0x4E,0x4A,0xBC,0x98,0x04,0xF1,0x74,0x6C,0x08,
53 0xCA,0x23,0x73,0x27,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
54 };
55 return BN_bin2bn(RFC3526_PRIME_1536, sizeof(RFC3526_PRIME_1536), NULL);
Dmitry Shmidt216983b2015-02-06 10:50:36 -080056#else /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070057 return get_rfc3526_prime_1536(NULL);
Dmitry Shmidt216983b2015-02-06 10:50:36 -080058#endif /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070059}
60
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070061#ifdef OPENSSL_NO_SHA256
62#define NO_SHA256_WRAPPER
63#endif
64
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070065static int openssl_digest_vector(const EVP_MD *type, size_t num_elem,
66 const u8 *addr[], const size_t *len, u8 *mac)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070067{
68 EVP_MD_CTX ctx;
69 size_t i;
70 unsigned int mac_len;
71
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080072 if (TEST_FAIL())
73 return -1;
74
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070075 EVP_MD_CTX_init(&ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070076 if (!EVP_DigestInit_ex(&ctx, type, NULL)) {
77 wpa_printf(MSG_ERROR, "OpenSSL: EVP_DigestInit_ex failed: %s",
78 ERR_error_string(ERR_get_error(), NULL));
79 return -1;
80 }
81 for (i = 0; i < num_elem; i++) {
82 if (!EVP_DigestUpdate(&ctx, addr[i], len[i])) {
83 wpa_printf(MSG_ERROR, "OpenSSL: EVP_DigestUpdate "
84 "failed: %s",
85 ERR_error_string(ERR_get_error(), NULL));
86 return -1;
87 }
88 }
89 if (!EVP_DigestFinal(&ctx, mac, &mac_len)) {
90 wpa_printf(MSG_ERROR, "OpenSSL: EVP_DigestFinal failed: %s",
91 ERR_error_string(ERR_get_error(), NULL));
92 return -1;
93 }
94
95 return 0;
96}
97
98
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080099#ifndef CONFIG_FIPS
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700100int md4_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
101{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700102 return openssl_digest_vector(EVP_md4(), num_elem, addr, len, mac);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700103}
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800104#endif /* CONFIG_FIPS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700105
106
107void des_encrypt(const u8 *clear, const u8 *key, u8 *cypher)
108{
109 u8 pkey[8], next, tmp;
110 int i;
111 DES_key_schedule ks;
112
113 /* Add parity bits to the key */
114 next = 0;
115 for (i = 0; i < 7; i++) {
116 tmp = key[i];
117 pkey[i] = (tmp >> i) | next | 1;
118 next = tmp << (7 - i);
119 }
120 pkey[i] = next | 1;
121
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -0700122 DES_set_key((DES_cblock *) &pkey, &ks);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700123 DES_ecb_encrypt((DES_cblock *) clear, (DES_cblock *) cypher, &ks,
124 DES_ENCRYPT);
125}
126
127
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800128#ifndef CONFIG_NO_RC4
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700129int rc4_skip(const u8 *key, size_t keylen, size_t skip,
130 u8 *data, size_t data_len)
131{
132#ifdef OPENSSL_NO_RC4
133 return -1;
134#else /* OPENSSL_NO_RC4 */
135 EVP_CIPHER_CTX ctx;
136 int outl;
137 int res = -1;
138 unsigned char skip_buf[16];
139
140 EVP_CIPHER_CTX_init(&ctx);
141 if (!EVP_CIPHER_CTX_set_padding(&ctx, 0) ||
142 !EVP_CipherInit_ex(&ctx, EVP_rc4(), NULL, NULL, NULL, 1) ||
143 !EVP_CIPHER_CTX_set_key_length(&ctx, keylen) ||
144 !EVP_CipherInit_ex(&ctx, NULL, NULL, key, NULL, 1))
145 goto out;
146
147 while (skip >= sizeof(skip_buf)) {
148 size_t len = skip;
149 if (len > sizeof(skip_buf))
150 len = sizeof(skip_buf);
151 if (!EVP_CipherUpdate(&ctx, skip_buf, &outl, skip_buf, len))
152 goto out;
153 skip -= len;
154 }
155
156 if (EVP_CipherUpdate(&ctx, data, &outl, data, data_len))
157 res = 0;
158
159out:
160 EVP_CIPHER_CTX_cleanup(&ctx);
161 return res;
162#endif /* OPENSSL_NO_RC4 */
163}
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800164#endif /* CONFIG_NO_RC4 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700165
166
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800167#ifndef CONFIG_FIPS
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700168int md5_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
169{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700170 return openssl_digest_vector(EVP_md5(), num_elem, addr, len, mac);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700171}
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800172#endif /* CONFIG_FIPS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700173
174
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700175int sha1_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
176{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700177 return openssl_digest_vector(EVP_sha1(), num_elem, addr, len, mac);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700178}
179
180
181#ifndef NO_SHA256_WRAPPER
182int sha256_vector(size_t num_elem, const u8 *addr[], const size_t *len,
183 u8 *mac)
184{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700185 return openssl_digest_vector(EVP_sha256(), num_elem, addr, len, mac);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700186}
187#endif /* NO_SHA256_WRAPPER */
188
189
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700190static const EVP_CIPHER * aes_get_evp_cipher(size_t keylen)
191{
192 switch (keylen) {
193 case 16:
194 return EVP_aes_128_ecb();
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -0700195#ifndef OPENSSL_IS_BORINGSSL
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700196 case 24:
197 return EVP_aes_192_ecb();
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -0700198#endif /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700199 case 32:
200 return EVP_aes_256_ecb();
201 }
202
203 return NULL;
204}
205
206
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700207void * aes_encrypt_init(const u8 *key, size_t len)
208{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700209 EVP_CIPHER_CTX *ctx;
210 const EVP_CIPHER *type;
211
212 type = aes_get_evp_cipher(len);
213 if (type == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700214 return NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700215
216 ctx = os_malloc(sizeof(*ctx));
217 if (ctx == NULL)
218 return NULL;
219 EVP_CIPHER_CTX_init(ctx);
220 if (EVP_EncryptInit_ex(ctx, type, NULL, key, NULL) != 1) {
221 os_free(ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700222 return NULL;
223 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700224 EVP_CIPHER_CTX_set_padding(ctx, 0);
225 return ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700226}
227
228
229void aes_encrypt(void *ctx, const u8 *plain, u8 *crypt)
230{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700231 EVP_CIPHER_CTX *c = ctx;
232 int clen = 16;
233 if (EVP_EncryptUpdate(c, crypt, &clen, plain, 16) != 1) {
234 wpa_printf(MSG_ERROR, "OpenSSL: EVP_EncryptUpdate failed: %s",
235 ERR_error_string(ERR_get_error(), NULL));
236 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700237}
238
239
240void aes_encrypt_deinit(void *ctx)
241{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700242 EVP_CIPHER_CTX *c = ctx;
243 u8 buf[16];
244 int len = sizeof(buf);
245 if (EVP_EncryptFinal_ex(c, buf, &len) != 1) {
246 wpa_printf(MSG_ERROR, "OpenSSL: EVP_EncryptFinal_ex failed: "
247 "%s", ERR_error_string(ERR_get_error(), NULL));
248 }
249 if (len != 0) {
250 wpa_printf(MSG_ERROR, "OpenSSL: Unexpected padding length %d "
251 "in AES encrypt", len);
252 }
253 EVP_CIPHER_CTX_cleanup(c);
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800254 bin_clear_free(c, sizeof(*c));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700255}
256
257
258void * aes_decrypt_init(const u8 *key, size_t len)
259{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700260 EVP_CIPHER_CTX *ctx;
261 const EVP_CIPHER *type;
262
263 type = aes_get_evp_cipher(len);
264 if (type == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700265 return NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700266
267 ctx = os_malloc(sizeof(*ctx));
268 if (ctx == NULL)
269 return NULL;
270 EVP_CIPHER_CTX_init(ctx);
271 if (EVP_DecryptInit_ex(ctx, type, NULL, key, NULL) != 1) {
272 os_free(ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700273 return NULL;
274 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700275 EVP_CIPHER_CTX_set_padding(ctx, 0);
276 return ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700277}
278
279
280void aes_decrypt(void *ctx, const u8 *crypt, u8 *plain)
281{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700282 EVP_CIPHER_CTX *c = ctx;
283 int plen = 16;
284 if (EVP_DecryptUpdate(c, plain, &plen, crypt, 16) != 1) {
285 wpa_printf(MSG_ERROR, "OpenSSL: EVP_DecryptUpdate failed: %s",
286 ERR_error_string(ERR_get_error(), NULL));
287 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700288}
289
290
291void aes_decrypt_deinit(void *ctx)
292{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700293 EVP_CIPHER_CTX *c = ctx;
294 u8 buf[16];
295 int len = sizeof(buf);
296 if (EVP_DecryptFinal_ex(c, buf, &len) != 1) {
297 wpa_printf(MSG_ERROR, "OpenSSL: EVP_DecryptFinal_ex failed: "
298 "%s", ERR_error_string(ERR_get_error(), NULL));
299 }
300 if (len != 0) {
301 wpa_printf(MSG_ERROR, "OpenSSL: Unexpected padding length %d "
302 "in AES decrypt", len);
303 }
304 EVP_CIPHER_CTX_cleanup(c);
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800305 bin_clear_free(c, sizeof(*c));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700306}
307
308
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800309#ifndef CONFIG_FIPS
310#ifndef CONFIG_OPENSSL_INTERNAL_AES_WRAP
311
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800312int aes_wrap(const u8 *kek, size_t kek_len, int n, const u8 *plain, u8 *cipher)
313{
314 AES_KEY actx;
315 int res;
316
317 if (AES_set_encrypt_key(kek, kek_len << 3, &actx))
318 return -1;
319 res = AES_wrap_key(&actx, NULL, cipher, plain, n * 8);
320 OPENSSL_cleanse(&actx, sizeof(actx));
321 return res <= 0 ? -1 : 0;
322}
323
324
325int aes_unwrap(const u8 *kek, size_t kek_len, int n, const u8 *cipher,
326 u8 *plain)
327{
328 AES_KEY actx;
329 int res;
330
331 if (AES_set_decrypt_key(kek, kek_len << 3, &actx))
332 return -1;
333 res = AES_unwrap_key(&actx, NULL, plain, cipher, (n + 1) * 8);
334 OPENSSL_cleanse(&actx, sizeof(actx));
335 return res <= 0 ? -1 : 0;
336}
337
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800338#endif /* CONFIG_OPENSSL_INTERNAL_AES_WRAP */
339#endif /* CONFIG_FIPS */
340
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800341
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -0700342int aes_128_cbc_encrypt(const u8 *key, const u8 *iv, u8 *data, size_t data_len)
343{
344 EVP_CIPHER_CTX ctx;
345 int clen, len;
346 u8 buf[16];
347
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -0800348 if (TEST_FAIL())
349 return -1;
350
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -0700351 EVP_CIPHER_CTX_init(&ctx);
352 if (EVP_EncryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL, key, iv) != 1)
353 return -1;
354 EVP_CIPHER_CTX_set_padding(&ctx, 0);
355
356 clen = data_len;
357 if (EVP_EncryptUpdate(&ctx, data, &clen, data, data_len) != 1 ||
358 clen != (int) data_len)
359 return -1;
360
361 len = sizeof(buf);
362 if (EVP_EncryptFinal_ex(&ctx, buf, &len) != 1 || len != 0)
363 return -1;
364 EVP_CIPHER_CTX_cleanup(&ctx);
365
366 return 0;
367}
368
369
370int aes_128_cbc_decrypt(const u8 *key, const u8 *iv, u8 *data, size_t data_len)
371{
372 EVP_CIPHER_CTX ctx;
373 int plen, len;
374 u8 buf[16];
375
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -0800376 if (TEST_FAIL())
377 return -1;
378
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -0700379 EVP_CIPHER_CTX_init(&ctx);
380 if (EVP_DecryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL, key, iv) != 1)
381 return -1;
382 EVP_CIPHER_CTX_set_padding(&ctx, 0);
383
384 plen = data_len;
385 if (EVP_DecryptUpdate(&ctx, data, &plen, data, data_len) != 1 ||
386 plen != (int) data_len)
387 return -1;
388
389 len = sizeof(buf);
390 if (EVP_DecryptFinal_ex(&ctx, buf, &len) != 1 || len != 0)
391 return -1;
392 EVP_CIPHER_CTX_cleanup(&ctx);
393
394 return 0;
395}
396
397
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700398int crypto_mod_exp(const u8 *base, size_t base_len,
399 const u8 *power, size_t power_len,
400 const u8 *modulus, size_t modulus_len,
401 u8 *result, size_t *result_len)
402{
403 BIGNUM *bn_base, *bn_exp, *bn_modulus, *bn_result;
404 int ret = -1;
405 BN_CTX *ctx;
406
407 ctx = BN_CTX_new();
408 if (ctx == NULL)
409 return -1;
410
411 bn_base = BN_bin2bn(base, base_len, NULL);
412 bn_exp = BN_bin2bn(power, power_len, NULL);
413 bn_modulus = BN_bin2bn(modulus, modulus_len, NULL);
414 bn_result = BN_new();
415
416 if (bn_base == NULL || bn_exp == NULL || bn_modulus == NULL ||
417 bn_result == NULL)
418 goto error;
419
420 if (BN_mod_exp(bn_result, bn_base, bn_exp, bn_modulus, ctx) != 1)
421 goto error;
422
423 *result_len = BN_bn2bin(bn_result, result);
424 ret = 0;
425
426error:
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -0700427 BN_clear_free(bn_base);
428 BN_clear_free(bn_exp);
429 BN_clear_free(bn_modulus);
430 BN_clear_free(bn_result);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700431 BN_CTX_free(ctx);
432 return ret;
433}
434
435
436struct crypto_cipher {
437 EVP_CIPHER_CTX enc;
438 EVP_CIPHER_CTX dec;
439};
440
441
442struct crypto_cipher * crypto_cipher_init(enum crypto_cipher_alg alg,
443 const u8 *iv, const u8 *key,
444 size_t key_len)
445{
446 struct crypto_cipher *ctx;
447 const EVP_CIPHER *cipher;
448
449 ctx = os_zalloc(sizeof(*ctx));
450 if (ctx == NULL)
451 return NULL;
452
453 switch (alg) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800454#ifndef CONFIG_NO_RC4
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700455#ifndef OPENSSL_NO_RC4
456 case CRYPTO_CIPHER_ALG_RC4:
457 cipher = EVP_rc4();
458 break;
459#endif /* OPENSSL_NO_RC4 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800460#endif /* CONFIG_NO_RC4 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700461#ifndef OPENSSL_NO_AES
462 case CRYPTO_CIPHER_ALG_AES:
463 switch (key_len) {
464 case 16:
465 cipher = EVP_aes_128_cbc();
466 break;
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -0700467#ifndef OPENSSL_IS_BORINGSSL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700468 case 24:
469 cipher = EVP_aes_192_cbc();
470 break;
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -0700471#endif /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700472 case 32:
473 cipher = EVP_aes_256_cbc();
474 break;
475 default:
476 os_free(ctx);
477 return NULL;
478 }
479 break;
480#endif /* OPENSSL_NO_AES */
481#ifndef OPENSSL_NO_DES
482 case CRYPTO_CIPHER_ALG_3DES:
483 cipher = EVP_des_ede3_cbc();
484 break;
485 case CRYPTO_CIPHER_ALG_DES:
486 cipher = EVP_des_cbc();
487 break;
488#endif /* OPENSSL_NO_DES */
489#ifndef OPENSSL_NO_RC2
490 case CRYPTO_CIPHER_ALG_RC2:
491 cipher = EVP_rc2_ecb();
492 break;
493#endif /* OPENSSL_NO_RC2 */
494 default:
495 os_free(ctx);
496 return NULL;
497 }
498
499 EVP_CIPHER_CTX_init(&ctx->enc);
500 EVP_CIPHER_CTX_set_padding(&ctx->enc, 0);
501 if (!EVP_EncryptInit_ex(&ctx->enc, cipher, NULL, NULL, NULL) ||
502 !EVP_CIPHER_CTX_set_key_length(&ctx->enc, key_len) ||
503 !EVP_EncryptInit_ex(&ctx->enc, NULL, NULL, key, iv)) {
504 EVP_CIPHER_CTX_cleanup(&ctx->enc);
505 os_free(ctx);
506 return NULL;
507 }
508
509 EVP_CIPHER_CTX_init(&ctx->dec);
510 EVP_CIPHER_CTX_set_padding(&ctx->dec, 0);
511 if (!EVP_DecryptInit_ex(&ctx->dec, cipher, NULL, NULL, NULL) ||
512 !EVP_CIPHER_CTX_set_key_length(&ctx->dec, key_len) ||
513 !EVP_DecryptInit_ex(&ctx->dec, NULL, NULL, key, iv)) {
514 EVP_CIPHER_CTX_cleanup(&ctx->enc);
515 EVP_CIPHER_CTX_cleanup(&ctx->dec);
516 os_free(ctx);
517 return NULL;
518 }
519
520 return ctx;
521}
522
523
524int crypto_cipher_encrypt(struct crypto_cipher *ctx, const u8 *plain,
525 u8 *crypt, size_t len)
526{
527 int outl;
528 if (!EVP_EncryptUpdate(&ctx->enc, crypt, &outl, plain, len))
529 return -1;
530 return 0;
531}
532
533
534int crypto_cipher_decrypt(struct crypto_cipher *ctx, const u8 *crypt,
535 u8 *plain, size_t len)
536{
537 int outl;
538 outl = len;
539 if (!EVP_DecryptUpdate(&ctx->dec, plain, &outl, crypt, len))
540 return -1;
541 return 0;
542}
543
544
545void crypto_cipher_deinit(struct crypto_cipher *ctx)
546{
547 EVP_CIPHER_CTX_cleanup(&ctx->enc);
548 EVP_CIPHER_CTX_cleanup(&ctx->dec);
549 os_free(ctx);
550}
551
552
553void * dh5_init(struct wpabuf **priv, struct wpabuf **publ)
554{
555 DH *dh;
556 struct wpabuf *pubkey = NULL, *privkey = NULL;
557 size_t publen, privlen;
558
559 *priv = NULL;
560 *publ = NULL;
561
562 dh = DH_new();
563 if (dh == NULL)
564 return NULL;
565
566 dh->g = BN_new();
567 if (dh->g == NULL || BN_set_word(dh->g, 2) != 1)
568 goto err;
569
570 dh->p = get_group5_prime();
571 if (dh->p == NULL)
572 goto err;
573
574 if (DH_generate_key(dh) != 1)
575 goto err;
576
577 publen = BN_num_bytes(dh->pub_key);
578 pubkey = wpabuf_alloc(publen);
579 if (pubkey == NULL)
580 goto err;
581 privlen = BN_num_bytes(dh->priv_key);
582 privkey = wpabuf_alloc(privlen);
583 if (privkey == NULL)
584 goto err;
585
586 BN_bn2bin(dh->pub_key, wpabuf_put(pubkey, publen));
587 BN_bn2bin(dh->priv_key, wpabuf_put(privkey, privlen));
588
589 *priv = privkey;
590 *publ = pubkey;
591 return dh;
592
593err:
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800594 wpabuf_clear_free(pubkey);
595 wpabuf_clear_free(privkey);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700596 DH_free(dh);
597 return NULL;
598}
599
600
Dmitry Shmidt04949592012-07-19 12:16:46 -0700601void * dh5_init_fixed(const struct wpabuf *priv, const struct wpabuf *publ)
602{
603 DH *dh;
604
605 dh = DH_new();
606 if (dh == NULL)
607 return NULL;
608
609 dh->g = BN_new();
610 if (dh->g == NULL || BN_set_word(dh->g, 2) != 1)
611 goto err;
612
613 dh->p = get_group5_prime();
614 if (dh->p == NULL)
615 goto err;
616
617 dh->priv_key = BN_bin2bn(wpabuf_head(priv), wpabuf_len(priv), NULL);
618 if (dh->priv_key == NULL)
619 goto err;
620
621 dh->pub_key = BN_bin2bn(wpabuf_head(publ), wpabuf_len(publ), NULL);
622 if (dh->pub_key == NULL)
623 goto err;
624
625 if (DH_generate_key(dh) != 1)
626 goto err;
627
628 return dh;
629
630err:
631 DH_free(dh);
632 return NULL;
633}
634
635
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700636struct wpabuf * dh5_derive_shared(void *ctx, const struct wpabuf *peer_public,
637 const struct wpabuf *own_private)
638{
639 BIGNUM *pub_key;
640 struct wpabuf *res = NULL;
641 size_t rlen;
642 DH *dh = ctx;
643 int keylen;
644
645 if (ctx == NULL)
646 return NULL;
647
648 pub_key = BN_bin2bn(wpabuf_head(peer_public), wpabuf_len(peer_public),
649 NULL);
650 if (pub_key == NULL)
651 return NULL;
652
653 rlen = DH_size(dh);
654 res = wpabuf_alloc(rlen);
655 if (res == NULL)
656 goto err;
657
658 keylen = DH_compute_key(wpabuf_mhead(res), pub_key, dh);
659 if (keylen < 0)
660 goto err;
661 wpabuf_put(res, keylen);
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -0700662 BN_clear_free(pub_key);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700663
664 return res;
665
666err:
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -0700667 BN_clear_free(pub_key);
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800668 wpabuf_clear_free(res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700669 return NULL;
670}
671
672
673void dh5_free(void *ctx)
674{
675 DH *dh;
676 if (ctx == NULL)
677 return;
678 dh = ctx;
679 DH_free(dh);
680}
Dmitry Shmidt04949592012-07-19 12:16:46 -0700681
682
683struct crypto_hash {
684 HMAC_CTX ctx;
685};
686
687
688struct crypto_hash * crypto_hash_init(enum crypto_hash_alg alg, const u8 *key,
689 size_t key_len)
690{
691 struct crypto_hash *ctx;
692 const EVP_MD *md;
693
694 switch (alg) {
695#ifndef OPENSSL_NO_MD5
696 case CRYPTO_HASH_ALG_HMAC_MD5:
697 md = EVP_md5();
698 break;
699#endif /* OPENSSL_NO_MD5 */
700#ifndef OPENSSL_NO_SHA
701 case CRYPTO_HASH_ALG_HMAC_SHA1:
702 md = EVP_sha1();
703 break;
704#endif /* OPENSSL_NO_SHA */
705#ifndef OPENSSL_NO_SHA256
706#ifdef CONFIG_SHA256
707 case CRYPTO_HASH_ALG_HMAC_SHA256:
708 md = EVP_sha256();
709 break;
710#endif /* CONFIG_SHA256 */
711#endif /* OPENSSL_NO_SHA256 */
712 default:
713 return NULL;
714 }
715
716 ctx = os_zalloc(sizeof(*ctx));
717 if (ctx == NULL)
718 return NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700719 HMAC_CTX_init(&ctx->ctx);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700720
721#if OPENSSL_VERSION_NUMBER < 0x00909000
722 HMAC_Init_ex(&ctx->ctx, key, key_len, md, NULL);
723#else /* openssl < 0.9.9 */
724 if (HMAC_Init_ex(&ctx->ctx, key, key_len, md, NULL) != 1) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800725 bin_clear_free(ctx, sizeof(*ctx));
Dmitry Shmidt04949592012-07-19 12:16:46 -0700726 return NULL;
727 }
728#endif /* openssl < 0.9.9 */
729
730 return ctx;
731}
732
733
734void crypto_hash_update(struct crypto_hash *ctx, const u8 *data, size_t len)
735{
736 if (ctx == NULL)
737 return;
738 HMAC_Update(&ctx->ctx, data, len);
739}
740
741
742int crypto_hash_finish(struct crypto_hash *ctx, u8 *mac, size_t *len)
743{
744 unsigned int mdlen;
745 int res;
746
747 if (ctx == NULL)
748 return -2;
749
750 if (mac == NULL || len == NULL) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800751 bin_clear_free(ctx, sizeof(*ctx));
Dmitry Shmidt04949592012-07-19 12:16:46 -0700752 return 0;
753 }
754
755 mdlen = *len;
756#if OPENSSL_VERSION_NUMBER < 0x00909000
757 HMAC_Final(&ctx->ctx, mac, &mdlen);
758 res = 1;
759#else /* openssl < 0.9.9 */
760 res = HMAC_Final(&ctx->ctx, mac, &mdlen);
761#endif /* openssl < 0.9.9 */
762 HMAC_CTX_cleanup(&ctx->ctx);
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800763 bin_clear_free(ctx, sizeof(*ctx));
Dmitry Shmidt04949592012-07-19 12:16:46 -0700764
765 if (res == 1) {
766 *len = mdlen;
767 return 0;
768 }
769
770 return -1;
771}
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700772
773
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800774static int openssl_hmac_vector(const EVP_MD *type, const u8 *key,
775 size_t key_len, size_t num_elem,
776 const u8 *addr[], const size_t *len, u8 *mac,
777 unsigned int mdlen)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700778{
779 HMAC_CTX ctx;
780 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700781 int res;
782
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800783 if (TEST_FAIL())
784 return -1;
785
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700786 HMAC_CTX_init(&ctx);
787#if OPENSSL_VERSION_NUMBER < 0x00909000
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800788 HMAC_Init_ex(&ctx, key, key_len, type, NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700789#else /* openssl < 0.9.9 */
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800790 if (HMAC_Init_ex(&ctx, key, key_len, type, NULL) != 1)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700791 return -1;
792#endif /* openssl < 0.9.9 */
793
794 for (i = 0; i < num_elem; i++)
795 HMAC_Update(&ctx, addr[i], len[i]);
796
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700797#if OPENSSL_VERSION_NUMBER < 0x00909000
798 HMAC_Final(&ctx, mac, &mdlen);
799 res = 1;
800#else /* openssl < 0.9.9 */
801 res = HMAC_Final(&ctx, mac, &mdlen);
802#endif /* openssl < 0.9.9 */
803 HMAC_CTX_cleanup(&ctx);
804
805 return res == 1 ? 0 : -1;
806}
807
808
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800809#ifndef CONFIG_FIPS
810
811int hmac_md5_vector(const u8 *key, size_t key_len, size_t num_elem,
812 const u8 *addr[], const size_t *len, u8 *mac)
813{
814 return openssl_hmac_vector(EVP_md5(), key ,key_len, num_elem, addr, len,
815 mac, 16);
816}
817
818
819int hmac_md5(const u8 *key, size_t key_len, const u8 *data, size_t data_len,
820 u8 *mac)
821{
822 return hmac_md5_vector(key, key_len, 1, &data, &data_len, mac);
823}
824
825#endif /* CONFIG_FIPS */
826
827
828int pbkdf2_sha1(const char *passphrase, const u8 *ssid, size_t ssid_len,
829 int iterations, u8 *buf, size_t buflen)
830{
831 if (PKCS5_PBKDF2_HMAC_SHA1(passphrase, os_strlen(passphrase), ssid,
832 ssid_len, iterations, buflen, buf) != 1)
833 return -1;
834 return 0;
835}
836
837
838int hmac_sha1_vector(const u8 *key, size_t key_len, size_t num_elem,
839 const u8 *addr[], const size_t *len, u8 *mac)
840{
841 return openssl_hmac_vector(EVP_sha1(), key, key_len, num_elem, addr,
842 len, mac, 20);
843}
844
845
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700846int hmac_sha1(const u8 *key, size_t key_len, const u8 *data, size_t data_len,
847 u8 *mac)
848{
849 return hmac_sha1_vector(key, key_len, 1, &data, &data_len, mac);
850}
851
852
853#ifdef CONFIG_SHA256
854
855int hmac_sha256_vector(const u8 *key, size_t key_len, size_t num_elem,
856 const u8 *addr[], const size_t *len, u8 *mac)
857{
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800858 return openssl_hmac_vector(EVP_sha256(), key, key_len, num_elem, addr,
859 len, mac, 32);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700860}
861
862
863int hmac_sha256(const u8 *key, size_t key_len, const u8 *data,
864 size_t data_len, u8 *mac)
865{
866 return hmac_sha256_vector(key, key_len, 1, &data, &data_len, mac);
867}
868
869#endif /* CONFIG_SHA256 */
870
871
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800872#ifdef CONFIG_SHA384
873
874int hmac_sha384_vector(const u8 *key, size_t key_len, size_t num_elem,
875 const u8 *addr[], const size_t *len, u8 *mac)
876{
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800877 return openssl_hmac_vector(EVP_sha384(), key, key_len, num_elem, addr,
878 len, mac, 32);
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800879}
880
881
882int hmac_sha384(const u8 *key, size_t key_len, const u8 *data,
883 size_t data_len, u8 *mac)
884{
885 return hmac_sha384_vector(key, key_len, 1, &data, &data_len, mac);
886}
887
888#endif /* CONFIG_SHA384 */
889
890
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700891int crypto_get_random(void *buf, size_t len)
892{
893 if (RAND_bytes(buf, len) != 1)
894 return -1;
895 return 0;
896}
897
898
899#ifdef CONFIG_OPENSSL_CMAC
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800900int omac1_aes_vector(const u8 *key, size_t key_len, size_t num_elem,
901 const u8 *addr[], const size_t *len, u8 *mac)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700902{
903 CMAC_CTX *ctx;
904 int ret = -1;
905 size_t outlen, i;
906
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800907 if (TEST_FAIL())
908 return -1;
909
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700910 ctx = CMAC_CTX_new();
911 if (ctx == NULL)
912 return -1;
913
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800914 if (key_len == 32) {
915 if (!CMAC_Init(ctx, key, 32, EVP_aes_256_cbc(), NULL))
916 goto fail;
917 } else if (key_len == 16) {
918 if (!CMAC_Init(ctx, key, 16, EVP_aes_128_cbc(), NULL))
919 goto fail;
920 } else {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700921 goto fail;
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800922 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700923 for (i = 0; i < num_elem; i++) {
924 if (!CMAC_Update(ctx, addr[i], len[i]))
925 goto fail;
926 }
927 if (!CMAC_Final(ctx, mac, &outlen) || outlen != 16)
928 goto fail;
929
930 ret = 0;
931fail:
932 CMAC_CTX_free(ctx);
933 return ret;
934}
935
936
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800937int omac1_aes_128_vector(const u8 *key, size_t num_elem,
938 const u8 *addr[], const size_t *len, u8 *mac)
939{
940 return omac1_aes_vector(key, 16, num_elem, addr, len, mac);
941}
942
943
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700944int omac1_aes_128(const u8 *key, const u8 *data, size_t data_len, u8 *mac)
945{
946 return omac1_aes_128_vector(key, 1, &data, &data_len, mac);
947}
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800948
949
950int omac1_aes_256(const u8 *key, const u8 *data, size_t data_len, u8 *mac)
951{
952 return omac1_aes_vector(key, 32, 1, &data, &data_len, mac);
953}
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700954#endif /* CONFIG_OPENSSL_CMAC */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800955
956
957struct crypto_bignum * crypto_bignum_init(void)
958{
959 return (struct crypto_bignum *) BN_new();
960}
961
962
963struct crypto_bignum * crypto_bignum_init_set(const u8 *buf, size_t len)
964{
965 BIGNUM *bn = BN_bin2bn(buf, len, NULL);
966 return (struct crypto_bignum *) bn;
967}
968
969
970void crypto_bignum_deinit(struct crypto_bignum *n, int clear)
971{
972 if (clear)
973 BN_clear_free((BIGNUM *) n);
974 else
975 BN_free((BIGNUM *) n);
976}
977
978
979int crypto_bignum_to_bin(const struct crypto_bignum *a,
980 u8 *buf, size_t buflen, size_t padlen)
981{
982 int num_bytes, offset;
983
984 if (padlen > buflen)
985 return -1;
986
987 num_bytes = BN_num_bytes((const BIGNUM *) a);
988 if ((size_t) num_bytes > buflen)
989 return -1;
990 if (padlen > (size_t) num_bytes)
991 offset = padlen - num_bytes;
992 else
993 offset = 0;
994
995 os_memset(buf, 0, offset);
996 BN_bn2bin((const BIGNUM *) a, buf + offset);
997
998 return num_bytes + offset;
999}
1000
1001
1002int crypto_bignum_add(const struct crypto_bignum *a,
1003 const struct crypto_bignum *b,
1004 struct crypto_bignum *c)
1005{
1006 return BN_add((BIGNUM *) c, (const BIGNUM *) a, (const BIGNUM *) b) ?
1007 0 : -1;
1008}
1009
1010
1011int crypto_bignum_mod(const struct crypto_bignum *a,
1012 const struct crypto_bignum *b,
1013 struct crypto_bignum *c)
1014{
1015 int res;
1016 BN_CTX *bnctx;
1017
1018 bnctx = BN_CTX_new();
1019 if (bnctx == NULL)
1020 return -1;
1021 res = BN_mod((BIGNUM *) c, (const BIGNUM *) a, (const BIGNUM *) b,
1022 bnctx);
1023 BN_CTX_free(bnctx);
1024
1025 return res ? 0 : -1;
1026}
1027
1028
1029int crypto_bignum_exptmod(const struct crypto_bignum *a,
1030 const struct crypto_bignum *b,
1031 const struct crypto_bignum *c,
1032 struct crypto_bignum *d)
1033{
1034 int res;
1035 BN_CTX *bnctx;
1036
1037 bnctx = BN_CTX_new();
1038 if (bnctx == NULL)
1039 return -1;
1040 res = BN_mod_exp((BIGNUM *) d, (const BIGNUM *) a, (const BIGNUM *) b,
1041 (const BIGNUM *) c, bnctx);
1042 BN_CTX_free(bnctx);
1043
1044 return res ? 0 : -1;
1045}
1046
1047
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001048int crypto_bignum_inverse(const struct crypto_bignum *a,
1049 const struct crypto_bignum *b,
1050 struct crypto_bignum *c)
1051{
1052 BIGNUM *res;
1053 BN_CTX *bnctx;
1054
1055 bnctx = BN_CTX_new();
1056 if (bnctx == NULL)
1057 return -1;
1058 res = BN_mod_inverse((BIGNUM *) c, (const BIGNUM *) a,
1059 (const BIGNUM *) b, bnctx);
1060 BN_CTX_free(bnctx);
1061
1062 return res ? 0 : -1;
1063}
1064
1065
1066int crypto_bignum_sub(const struct crypto_bignum *a,
1067 const struct crypto_bignum *b,
1068 struct crypto_bignum *c)
1069{
1070 return BN_sub((BIGNUM *) c, (const BIGNUM *) a, (const BIGNUM *) b) ?
1071 0 : -1;
1072}
1073
1074
1075int crypto_bignum_div(const struct crypto_bignum *a,
1076 const struct crypto_bignum *b,
1077 struct crypto_bignum *c)
1078{
1079 int res;
1080
1081 BN_CTX *bnctx;
1082
1083 bnctx = BN_CTX_new();
1084 if (bnctx == NULL)
1085 return -1;
1086 res = BN_div((BIGNUM *) c, NULL, (const BIGNUM *) a,
1087 (const BIGNUM *) b, bnctx);
1088 BN_CTX_free(bnctx);
1089
1090 return res ? 0 : -1;
1091}
1092
1093
1094int crypto_bignum_mulmod(const struct crypto_bignum *a,
1095 const struct crypto_bignum *b,
1096 const struct crypto_bignum *c,
1097 struct crypto_bignum *d)
1098{
1099 int res;
1100
1101 BN_CTX *bnctx;
1102
1103 bnctx = BN_CTX_new();
1104 if (bnctx == NULL)
1105 return -1;
1106 res = BN_mod_mul((BIGNUM *) d, (const BIGNUM *) a, (const BIGNUM *) b,
1107 (const BIGNUM *) c, bnctx);
1108 BN_CTX_free(bnctx);
1109
1110 return res ? 0 : -1;
1111}
1112
1113
1114int crypto_bignum_cmp(const struct crypto_bignum *a,
1115 const struct crypto_bignum *b)
1116{
1117 return BN_cmp((const BIGNUM *) a, (const BIGNUM *) b);
1118}
1119
1120
1121int crypto_bignum_bits(const struct crypto_bignum *a)
1122{
1123 return BN_num_bits((const BIGNUM *) a);
1124}
1125
1126
1127int crypto_bignum_is_zero(const struct crypto_bignum *a)
1128{
1129 return BN_is_zero((const BIGNUM *) a);
1130}
1131
1132
1133int crypto_bignum_is_one(const struct crypto_bignum *a)
1134{
1135 return BN_is_one((const BIGNUM *) a);
1136}
1137
1138
Dmitry Shmidt41712582015-06-29 11:02:15 -07001139int crypto_bignum_legendre(const struct crypto_bignum *a,
1140 const struct crypto_bignum *p)
1141{
1142 BN_CTX *bnctx;
1143 BIGNUM *exp = NULL, *tmp = NULL;
1144 int res = -2;
1145
1146 bnctx = BN_CTX_new();
1147 if (bnctx == NULL)
1148 return -2;
1149
1150 exp = BN_new();
1151 tmp = BN_new();
1152 if (!exp || !tmp ||
1153 /* exp = (p-1) / 2 */
1154 !BN_sub(exp, (const BIGNUM *) p, BN_value_one()) ||
1155 !BN_rshift1(exp, exp) ||
1156 !BN_mod_exp(tmp, (const BIGNUM *) a, exp, (const BIGNUM *) p,
1157 bnctx))
1158 goto fail;
1159
1160 if (BN_is_word(tmp, 1))
1161 res = 1;
1162 else if (BN_is_zero(tmp))
1163 res = 0;
1164 else
1165 res = -1;
1166
1167fail:
1168 BN_clear_free(tmp);
1169 BN_clear_free(exp);
1170 BN_CTX_free(bnctx);
1171 return res;
1172}
1173
1174
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001175#ifdef CONFIG_ECC
1176
1177struct crypto_ec {
1178 EC_GROUP *group;
1179 BN_CTX *bnctx;
1180 BIGNUM *prime;
1181 BIGNUM *order;
Dmitry Shmidt41712582015-06-29 11:02:15 -07001182 BIGNUM *a;
1183 BIGNUM *b;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001184};
1185
1186struct crypto_ec * crypto_ec_init(int group)
1187{
1188 struct crypto_ec *e;
1189 int nid;
1190
1191 /* Map from IANA registry for IKE D-H groups to OpenSSL NID */
1192 switch (group) {
1193 case 19:
1194 nid = NID_X9_62_prime256v1;
1195 break;
1196 case 20:
1197 nid = NID_secp384r1;
1198 break;
1199 case 21:
1200 nid = NID_secp521r1;
1201 break;
1202 case 25:
1203 nid = NID_X9_62_prime192v1;
1204 break;
1205 case 26:
1206 nid = NID_secp224r1;
1207 break;
Dmitry Shmidt41712582015-06-29 11:02:15 -07001208#ifdef NID_brainpoolP224r1
1209 case 27:
1210 nid = NID_brainpoolP224r1;
1211 break;
1212#endif /* NID_brainpoolP224r1 */
1213#ifdef NID_brainpoolP256r1
1214 case 28:
1215 nid = NID_brainpoolP256r1;
1216 break;
1217#endif /* NID_brainpoolP256r1 */
1218#ifdef NID_brainpoolP384r1
1219 case 29:
1220 nid = NID_brainpoolP384r1;
1221 break;
1222#endif /* NID_brainpoolP384r1 */
1223#ifdef NID_brainpoolP512r1
1224 case 30:
1225 nid = NID_brainpoolP512r1;
1226 break;
1227#endif /* NID_brainpoolP512r1 */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001228 default:
1229 return NULL;
1230 }
1231
1232 e = os_zalloc(sizeof(*e));
1233 if (e == NULL)
1234 return NULL;
1235
1236 e->bnctx = BN_CTX_new();
1237 e->group = EC_GROUP_new_by_curve_name(nid);
1238 e->prime = BN_new();
1239 e->order = BN_new();
Dmitry Shmidt41712582015-06-29 11:02:15 -07001240 e->a = BN_new();
1241 e->b = BN_new();
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001242 if (e->group == NULL || e->bnctx == NULL || e->prime == NULL ||
Dmitry Shmidt41712582015-06-29 11:02:15 -07001243 e->order == NULL || e->a == NULL || e->b == NULL ||
1244 !EC_GROUP_get_curve_GFp(e->group, e->prime, e->a, e->b, e->bnctx) ||
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001245 !EC_GROUP_get_order(e->group, e->order, e->bnctx)) {
1246 crypto_ec_deinit(e);
1247 e = NULL;
1248 }
1249
1250 return e;
1251}
1252
1253
1254void crypto_ec_deinit(struct crypto_ec *e)
1255{
1256 if (e == NULL)
1257 return;
Dmitry Shmidt41712582015-06-29 11:02:15 -07001258 BN_clear_free(e->b);
1259 BN_clear_free(e->a);
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001260 BN_clear_free(e->order);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001261 BN_clear_free(e->prime);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001262 EC_GROUP_free(e->group);
1263 BN_CTX_free(e->bnctx);
1264 os_free(e);
1265}
1266
1267
1268struct crypto_ec_point * crypto_ec_point_init(struct crypto_ec *e)
1269{
1270 if (e == NULL)
1271 return NULL;
1272 return (struct crypto_ec_point *) EC_POINT_new(e->group);
1273}
1274
1275
1276size_t crypto_ec_prime_len(struct crypto_ec *e)
1277{
1278 return BN_num_bytes(e->prime);
1279}
1280
1281
1282size_t crypto_ec_prime_len_bits(struct crypto_ec *e)
1283{
1284 return BN_num_bits(e->prime);
1285}
1286
1287
1288const struct crypto_bignum * crypto_ec_get_prime(struct crypto_ec *e)
1289{
1290 return (const struct crypto_bignum *) e->prime;
1291}
1292
1293
1294const struct crypto_bignum * crypto_ec_get_order(struct crypto_ec *e)
1295{
1296 return (const struct crypto_bignum *) e->order;
1297}
1298
1299
1300void crypto_ec_point_deinit(struct crypto_ec_point *p, int clear)
1301{
1302 if (clear)
1303 EC_POINT_clear_free((EC_POINT *) p);
1304 else
1305 EC_POINT_free((EC_POINT *) p);
1306}
1307
1308
1309int crypto_ec_point_to_bin(struct crypto_ec *e,
1310 const struct crypto_ec_point *point, u8 *x, u8 *y)
1311{
1312 BIGNUM *x_bn, *y_bn;
1313 int ret = -1;
1314 int len = BN_num_bytes(e->prime);
1315
1316 x_bn = BN_new();
1317 y_bn = BN_new();
1318
1319 if (x_bn && y_bn &&
1320 EC_POINT_get_affine_coordinates_GFp(e->group, (EC_POINT *) point,
1321 x_bn, y_bn, e->bnctx)) {
1322 if (x) {
1323 crypto_bignum_to_bin((struct crypto_bignum *) x_bn,
1324 x, len, len);
1325 }
1326 if (y) {
1327 crypto_bignum_to_bin((struct crypto_bignum *) y_bn,
1328 y, len, len);
1329 }
1330 ret = 0;
1331 }
1332
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001333 BN_clear_free(x_bn);
1334 BN_clear_free(y_bn);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001335 return ret;
1336}
1337
1338
1339struct crypto_ec_point * crypto_ec_point_from_bin(struct crypto_ec *e,
1340 const u8 *val)
1341{
1342 BIGNUM *x, *y;
1343 EC_POINT *elem;
1344 int len = BN_num_bytes(e->prime);
1345
1346 x = BN_bin2bn(val, len, NULL);
1347 y = BN_bin2bn(val + len, len, NULL);
1348 elem = EC_POINT_new(e->group);
1349 if (x == NULL || y == NULL || elem == NULL) {
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001350 BN_clear_free(x);
1351 BN_clear_free(y);
1352 EC_POINT_clear_free(elem);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001353 return NULL;
1354 }
1355
1356 if (!EC_POINT_set_affine_coordinates_GFp(e->group, elem, x, y,
1357 e->bnctx)) {
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001358 EC_POINT_clear_free(elem);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001359 elem = NULL;
1360 }
1361
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001362 BN_clear_free(x);
1363 BN_clear_free(y);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001364
1365 return (struct crypto_ec_point *) elem;
1366}
1367
1368
1369int crypto_ec_point_add(struct crypto_ec *e, const struct crypto_ec_point *a,
1370 const struct crypto_ec_point *b,
1371 struct crypto_ec_point *c)
1372{
1373 return EC_POINT_add(e->group, (EC_POINT *) c, (const EC_POINT *) a,
1374 (const EC_POINT *) b, e->bnctx) ? 0 : -1;
1375}
1376
1377
1378int crypto_ec_point_mul(struct crypto_ec *e, const struct crypto_ec_point *p,
1379 const struct crypto_bignum *b,
1380 struct crypto_ec_point *res)
1381{
1382 return EC_POINT_mul(e->group, (EC_POINT *) res, NULL,
1383 (const EC_POINT *) p, (const BIGNUM *) b, e->bnctx)
1384 ? 0 : -1;
1385}
1386
1387
1388int crypto_ec_point_invert(struct crypto_ec *e, struct crypto_ec_point *p)
1389{
1390 return EC_POINT_invert(e->group, (EC_POINT *) p, e->bnctx) ? 0 : -1;
1391}
1392
1393
1394int crypto_ec_point_solve_y_coord(struct crypto_ec *e,
1395 struct crypto_ec_point *p,
1396 const struct crypto_bignum *x, int y_bit)
1397{
1398 if (!EC_POINT_set_compressed_coordinates_GFp(e->group, (EC_POINT *) p,
1399 (const BIGNUM *) x, y_bit,
1400 e->bnctx) ||
1401 !EC_POINT_is_on_curve(e->group, (EC_POINT *) p, e->bnctx))
1402 return -1;
1403 return 0;
1404}
1405
1406
Dmitry Shmidt41712582015-06-29 11:02:15 -07001407struct crypto_bignum *
1408crypto_ec_point_compute_y_sqr(struct crypto_ec *e,
1409 const struct crypto_bignum *x)
1410{
1411 BIGNUM *tmp, *tmp2, *y_sqr = NULL;
1412
1413 tmp = BN_new();
1414 tmp2 = BN_new();
1415
1416 /* y^2 = x^3 + ax + b */
1417 if (tmp && tmp2 &&
1418 BN_mod_sqr(tmp, (const BIGNUM *) x, e->prime, e->bnctx) &&
1419 BN_mod_mul(tmp, tmp, (const BIGNUM *) x, e->prime, e->bnctx) &&
1420 BN_mod_mul(tmp2, e->a, (const BIGNUM *) x, e->prime, e->bnctx) &&
1421 BN_mod_add_quick(tmp2, tmp2, tmp, e->prime) &&
1422 BN_mod_add_quick(tmp2, tmp2, e->b, e->prime)) {
1423 y_sqr = tmp2;
1424 tmp2 = NULL;
1425 }
1426
1427 BN_clear_free(tmp);
1428 BN_clear_free(tmp2);
1429
1430 return (struct crypto_bignum *) y_sqr;
1431}
1432
1433
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001434int crypto_ec_point_is_at_infinity(struct crypto_ec *e,
1435 const struct crypto_ec_point *p)
1436{
1437 return EC_POINT_is_at_infinity(e->group, (const EC_POINT *) p);
1438}
1439
1440
1441int crypto_ec_point_is_on_curve(struct crypto_ec *e,
1442 const struct crypto_ec_point *p)
1443{
Dmitry Shmidt41712582015-06-29 11:02:15 -07001444 return EC_POINT_is_on_curve(e->group, (const EC_POINT *) p,
1445 e->bnctx) == 1;
1446}
1447
1448
1449int crypto_ec_point_cmp(const struct crypto_ec *e,
1450 const struct crypto_ec_point *a,
1451 const struct crypto_ec_point *b)
1452{
1453 return EC_POINT_cmp(e->group, (const EC_POINT *) a,
1454 (const EC_POINT *) b, e->bnctx);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001455}
1456
1457#endif /* CONFIG_ECC */