blob: fde154faf1addf0d376e5bb4af4a31caf77b10af [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 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
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -080036#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
37/* Compatibility wrappers for older versions. */
38
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -080039static HMAC_CTX * HMAC_CTX_new(void)
40{
41 HMAC_CTX *ctx;
42
43 ctx = os_zalloc(sizeof(*ctx));
44 if (ctx)
45 HMAC_CTX_init(ctx);
46 return ctx;
47}
48
49
50static void HMAC_CTX_free(HMAC_CTX *ctx)
51{
Dmitry Shmidt57c2d392016-02-23 13:40:19 -080052 HMAC_CTX_cleanup(ctx);
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -080053 bin_clear_free(ctx, sizeof(*ctx));
54}
55
56
57static EVP_MD_CTX * EVP_MD_CTX_new(void)
58{
59 EVP_MD_CTX *ctx;
60
61 ctx = os_zalloc(sizeof(*ctx));
62 if (ctx)
63 EVP_MD_CTX_init(ctx);
64 return ctx;
65}
66
67
68static void EVP_MD_CTX_free(EVP_MD_CTX *ctx)
69{
70 bin_clear_free(ctx, sizeof(*ctx));
71}
72
73#endif /* OpenSSL version < 1.1.0 */
74
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070075static BIGNUM * get_group5_prime(void)
76{
Dmitry Shmidt216983b2015-02-06 10:50:36 -080077#ifdef OPENSSL_IS_BORINGSSL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070078 static const unsigned char RFC3526_PRIME_1536[] = {
79 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xC9,0x0F,0xDA,0xA2,
80 0x21,0x68,0xC2,0x34,0xC4,0xC6,0x62,0x8B,0x80,0xDC,0x1C,0xD1,
81 0x29,0x02,0x4E,0x08,0x8A,0x67,0xCC,0x74,0x02,0x0B,0xBE,0xA6,
82 0x3B,0x13,0x9B,0x22,0x51,0x4A,0x08,0x79,0x8E,0x34,0x04,0xDD,
83 0xEF,0x95,0x19,0xB3,0xCD,0x3A,0x43,0x1B,0x30,0x2B,0x0A,0x6D,
84 0xF2,0x5F,0x14,0x37,0x4F,0xE1,0x35,0x6D,0x6D,0x51,0xC2,0x45,
85 0xE4,0x85,0xB5,0x76,0x62,0x5E,0x7E,0xC6,0xF4,0x4C,0x42,0xE9,
86 0xA6,0x37,0xED,0x6B,0x0B,0xFF,0x5C,0xB6,0xF4,0x06,0xB7,0xED,
87 0xEE,0x38,0x6B,0xFB,0x5A,0x89,0x9F,0xA5,0xAE,0x9F,0x24,0x11,
88 0x7C,0x4B,0x1F,0xE6,0x49,0x28,0x66,0x51,0xEC,0xE4,0x5B,0x3D,
89 0xC2,0x00,0x7C,0xB8,0xA1,0x63,0xBF,0x05,0x98,0xDA,0x48,0x36,
90 0x1C,0x55,0xD3,0x9A,0x69,0x16,0x3F,0xA8,0xFD,0x24,0xCF,0x5F,
91 0x83,0x65,0x5D,0x23,0xDC,0xA3,0xAD,0x96,0x1C,0x62,0xF3,0x56,
92 0x20,0x85,0x52,0xBB,0x9E,0xD5,0x29,0x07,0x70,0x96,0x96,0x6D,
93 0x67,0x0C,0x35,0x4E,0x4A,0xBC,0x98,0x04,0xF1,0x74,0x6C,0x08,
94 0xCA,0x23,0x73,0x27,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
95 };
96 return BN_bin2bn(RFC3526_PRIME_1536, sizeof(RFC3526_PRIME_1536), NULL);
Dmitry Shmidt216983b2015-02-06 10:50:36 -080097#else /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070098 return get_rfc3526_prime_1536(NULL);
Dmitry Shmidt216983b2015-02-06 10:50:36 -080099#endif /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700100}
101
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700102#ifdef OPENSSL_NO_SHA256
103#define NO_SHA256_WRAPPER
104#endif
105
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700106static int openssl_digest_vector(const EVP_MD *type, size_t num_elem,
107 const u8 *addr[], const size_t *len, u8 *mac)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700108{
Dmitry Shmidt55840ad2015-12-14 12:45:46 -0800109 EVP_MD_CTX *ctx;
110 size_t i;
111 unsigned int mac_len;
112
113 if (TEST_FAIL())
114 return -1;
115
116 ctx = EVP_MD_CTX_new();
117 if (!ctx)
118 return -1;
119 if (!EVP_DigestInit_ex(ctx, type, NULL)) {
120 wpa_printf(MSG_ERROR, "OpenSSL: EVP_DigestInit_ex failed: %s",
121 ERR_error_string(ERR_get_error(), NULL));
122 EVP_MD_CTX_free(ctx);
123 return -1;
124 }
125 for (i = 0; i < num_elem; i++) {
126 if (!EVP_DigestUpdate(ctx, addr[i], len[i])) {
127 wpa_printf(MSG_ERROR, "OpenSSL: EVP_DigestUpdate "
128 "failed: %s",
129 ERR_error_string(ERR_get_error(), NULL));
130 EVP_MD_CTX_free(ctx);
131 return -1;
132 }
133 }
134 if (!EVP_DigestFinal(ctx, mac, &mac_len)) {
135 wpa_printf(MSG_ERROR, "OpenSSL: EVP_DigestFinal failed: %s",
136 ERR_error_string(ERR_get_error(), NULL));
137 EVP_MD_CTX_free(ctx);
138 return -1;
139 }
140 EVP_MD_CTX_free(ctx);
141
142 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700143}
144
145
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800146#ifndef CONFIG_FIPS
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700147int md4_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
148{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700149 return openssl_digest_vector(EVP_md4(), num_elem, addr, len, mac);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700150}
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800151#endif /* CONFIG_FIPS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700152
153
154void des_encrypt(const u8 *clear, const u8 *key, u8 *cypher)
155{
156 u8 pkey[8], next, tmp;
157 int i;
158 DES_key_schedule ks;
159
160 /* Add parity bits to the key */
161 next = 0;
162 for (i = 0; i < 7; i++) {
163 tmp = key[i];
164 pkey[i] = (tmp >> i) | next | 1;
165 next = tmp << (7 - i);
166 }
167 pkey[i] = next | 1;
168
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -0700169 DES_set_key((DES_cblock *) &pkey, &ks);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700170 DES_ecb_encrypt((DES_cblock *) clear, (DES_cblock *) cypher, &ks,
171 DES_ENCRYPT);
172}
173
174
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800175#ifndef CONFIG_NO_RC4
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700176int rc4_skip(const u8 *key, size_t keylen, size_t skip,
177 u8 *data, size_t data_len)
178{
179#ifdef OPENSSL_NO_RC4
180 return -1;
181#else /* OPENSSL_NO_RC4 */
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800182 EVP_CIPHER_CTX *ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700183 int outl;
184 int res = -1;
185 unsigned char skip_buf[16];
186
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800187 ctx = EVP_CIPHER_CTX_new();
188 if (!ctx ||
189 !EVP_CIPHER_CTX_set_padding(ctx, 0) ||
190 !EVP_CipherInit_ex(ctx, EVP_rc4(), NULL, NULL, NULL, 1) ||
191 !EVP_CIPHER_CTX_set_key_length(ctx, keylen) ||
192 !EVP_CipherInit_ex(ctx, NULL, NULL, key, NULL, 1))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700193 goto out;
194
195 while (skip >= sizeof(skip_buf)) {
196 size_t len = skip;
197 if (len > sizeof(skip_buf))
198 len = sizeof(skip_buf);
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800199 if (!EVP_CipherUpdate(ctx, skip_buf, &outl, skip_buf, len))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700200 goto out;
201 skip -= len;
202 }
203
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800204 if (EVP_CipherUpdate(ctx, data, &outl, data, data_len))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700205 res = 0;
206
207out:
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800208 if (ctx)
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800209 EVP_CIPHER_CTX_free(ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700210 return res;
211#endif /* OPENSSL_NO_RC4 */
212}
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800213#endif /* CONFIG_NO_RC4 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700214
215
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800216#ifndef CONFIG_FIPS
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700217int md5_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
218{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700219 return openssl_digest_vector(EVP_md5(), num_elem, addr, len, mac);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700220}
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800221#endif /* CONFIG_FIPS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700222
223
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700224int sha1_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
225{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700226 return openssl_digest_vector(EVP_sha1(), num_elem, addr, len, mac);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700227}
228
229
230#ifndef NO_SHA256_WRAPPER
231int sha256_vector(size_t num_elem, const u8 *addr[], const size_t *len,
232 u8 *mac)
233{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700234 return openssl_digest_vector(EVP_sha256(), num_elem, addr, len, mac);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700235}
236#endif /* NO_SHA256_WRAPPER */
237
238
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700239static const EVP_CIPHER * aes_get_evp_cipher(size_t keylen)
240{
241 switch (keylen) {
242 case 16:
243 return EVP_aes_128_ecb();
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -0700244#ifndef OPENSSL_IS_BORINGSSL
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700245 case 24:
246 return EVP_aes_192_ecb();
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -0700247#endif /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700248 case 32:
249 return EVP_aes_256_ecb();
250 }
251
252 return NULL;
253}
254
255
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700256void * aes_encrypt_init(const u8 *key, size_t len)
257{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700258 EVP_CIPHER_CTX *ctx;
259 const EVP_CIPHER *type;
260
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800261 if (TEST_FAIL())
262 return NULL;
263
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700264 type = aes_get_evp_cipher(len);
265 if (type == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700266 return NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700267
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800268 ctx = EVP_CIPHER_CTX_new();
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700269 if (ctx == NULL)
270 return NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700271 if (EVP_EncryptInit_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_encrypt(void *ctx, const u8 *plain, u8 *crypt)
281{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700282 EVP_CIPHER_CTX *c = ctx;
283 int clen = 16;
284 if (EVP_EncryptUpdate(c, crypt, &clen, plain, 16) != 1) {
285 wpa_printf(MSG_ERROR, "OpenSSL: EVP_EncryptUpdate failed: %s",
286 ERR_error_string(ERR_get_error(), NULL));
287 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700288}
289
290
291void aes_encrypt_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_EncryptFinal_ex(c, buf, &len) != 1) {
297 wpa_printf(MSG_ERROR, "OpenSSL: EVP_EncryptFinal_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 encrypt", len);
303 }
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800304 EVP_CIPHER_CTX_free(c);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700305}
306
307
308void * aes_decrypt_init(const u8 *key, size_t len)
309{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700310 EVP_CIPHER_CTX *ctx;
311 const EVP_CIPHER *type;
312
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800313 if (TEST_FAIL())
314 return NULL;
315
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700316 type = aes_get_evp_cipher(len);
317 if (type == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700318 return NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700319
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800320 ctx = EVP_CIPHER_CTX_new();
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700321 if (ctx == NULL)
322 return NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700323 if (EVP_DecryptInit_ex(ctx, type, NULL, key, NULL) != 1) {
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800324 EVP_CIPHER_CTX_free(ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700325 return NULL;
326 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700327 EVP_CIPHER_CTX_set_padding(ctx, 0);
328 return ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700329}
330
331
332void aes_decrypt(void *ctx, const u8 *crypt, u8 *plain)
333{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700334 EVP_CIPHER_CTX *c = ctx;
335 int plen = 16;
336 if (EVP_DecryptUpdate(c, plain, &plen, crypt, 16) != 1) {
337 wpa_printf(MSG_ERROR, "OpenSSL: EVP_DecryptUpdate failed: %s",
338 ERR_error_string(ERR_get_error(), NULL));
339 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700340}
341
342
343void aes_decrypt_deinit(void *ctx)
344{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700345 EVP_CIPHER_CTX *c = ctx;
346 u8 buf[16];
347 int len = sizeof(buf);
348 if (EVP_DecryptFinal_ex(c, buf, &len) != 1) {
349 wpa_printf(MSG_ERROR, "OpenSSL: EVP_DecryptFinal_ex failed: "
350 "%s", ERR_error_string(ERR_get_error(), NULL));
351 }
352 if (len != 0) {
353 wpa_printf(MSG_ERROR, "OpenSSL: Unexpected padding length %d "
354 "in AES decrypt", len);
355 }
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800356 EVP_CIPHER_CTX_free(c);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700357}
358
359
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800360#ifndef CONFIG_FIPS
361#ifndef CONFIG_OPENSSL_INTERNAL_AES_WRAP
362
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800363int aes_wrap(const u8 *kek, size_t kek_len, int n, const u8 *plain, u8 *cipher)
364{
365 AES_KEY actx;
366 int res;
367
368 if (AES_set_encrypt_key(kek, kek_len << 3, &actx))
369 return -1;
370 res = AES_wrap_key(&actx, NULL, cipher, plain, n * 8);
371 OPENSSL_cleanse(&actx, sizeof(actx));
372 return res <= 0 ? -1 : 0;
373}
374
375
376int aes_unwrap(const u8 *kek, size_t kek_len, int n, const u8 *cipher,
377 u8 *plain)
378{
379 AES_KEY actx;
380 int res;
381
382 if (AES_set_decrypt_key(kek, kek_len << 3, &actx))
383 return -1;
384 res = AES_unwrap_key(&actx, NULL, plain, cipher, (n + 1) * 8);
385 OPENSSL_cleanse(&actx, sizeof(actx));
386 return res <= 0 ? -1 : 0;
387}
388
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800389#endif /* CONFIG_OPENSSL_INTERNAL_AES_WRAP */
390#endif /* CONFIG_FIPS */
391
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800392
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -0700393int aes_128_cbc_encrypt(const u8 *key, const u8 *iv, u8 *data, size_t data_len)
394{
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800395 EVP_CIPHER_CTX *ctx;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -0700396 int clen, len;
397 u8 buf[16];
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800398 int res = -1;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -0700399
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -0800400 if (TEST_FAIL())
401 return -1;
402
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800403 ctx = EVP_CIPHER_CTX_new();
404 if (!ctx)
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -0700405 return -1;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -0700406 clen = data_len;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -0700407 len = sizeof(buf);
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800408 if (EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv) == 1 &&
409 EVP_CIPHER_CTX_set_padding(ctx, 0) == 1 &&
410 EVP_EncryptUpdate(ctx, data, &clen, data, data_len) == 1 &&
411 clen == (int) data_len &&
412 EVP_EncryptFinal_ex(ctx, buf, &len) == 1 && len == 0)
413 res = 0;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800414 EVP_CIPHER_CTX_free(ctx);
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -0700415
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800416 return res;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -0700417}
418
419
420int aes_128_cbc_decrypt(const u8 *key, const u8 *iv, u8 *data, size_t data_len)
421{
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800422 EVP_CIPHER_CTX *ctx;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -0700423 int plen, len;
424 u8 buf[16];
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800425 int res = -1;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -0700426
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -0800427 if (TEST_FAIL())
428 return -1;
429
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800430 ctx = EVP_CIPHER_CTX_new();
431 if (!ctx)
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -0700432 return -1;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -0700433 plen = data_len;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -0700434 len = sizeof(buf);
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800435 if (EVP_DecryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv) == 1 &&
436 EVP_CIPHER_CTX_set_padding(ctx, 0) == 1 &&
437 EVP_DecryptUpdate(ctx, data, &plen, data, data_len) == 1 &&
438 plen == (int) data_len &&
439 EVP_DecryptFinal_ex(ctx, buf, &len) == 1 && len == 0)
440 res = 0;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800441 EVP_CIPHER_CTX_free(ctx);
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -0700442
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800443 return res;
444
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -0700445}
446
447
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700448int crypto_mod_exp(const u8 *base, size_t base_len,
449 const u8 *power, size_t power_len,
450 const u8 *modulus, size_t modulus_len,
451 u8 *result, size_t *result_len)
452{
453 BIGNUM *bn_base, *bn_exp, *bn_modulus, *bn_result;
454 int ret = -1;
455 BN_CTX *ctx;
456
457 ctx = BN_CTX_new();
458 if (ctx == NULL)
459 return -1;
460
461 bn_base = BN_bin2bn(base, base_len, NULL);
462 bn_exp = BN_bin2bn(power, power_len, NULL);
463 bn_modulus = BN_bin2bn(modulus, modulus_len, NULL);
464 bn_result = BN_new();
465
466 if (bn_base == NULL || bn_exp == NULL || bn_modulus == NULL ||
467 bn_result == NULL)
468 goto error;
469
470 if (BN_mod_exp(bn_result, bn_base, bn_exp, bn_modulus, ctx) != 1)
471 goto error;
472
473 *result_len = BN_bn2bin(bn_result, result);
474 ret = 0;
475
476error:
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -0700477 BN_clear_free(bn_base);
478 BN_clear_free(bn_exp);
479 BN_clear_free(bn_modulus);
480 BN_clear_free(bn_result);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700481 BN_CTX_free(ctx);
482 return ret;
483}
484
485
486struct crypto_cipher {
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800487 EVP_CIPHER_CTX *enc;
488 EVP_CIPHER_CTX *dec;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700489};
490
491
492struct crypto_cipher * crypto_cipher_init(enum crypto_cipher_alg alg,
493 const u8 *iv, const u8 *key,
494 size_t key_len)
495{
496 struct crypto_cipher *ctx;
497 const EVP_CIPHER *cipher;
498
499 ctx = os_zalloc(sizeof(*ctx));
500 if (ctx == NULL)
501 return NULL;
502
503 switch (alg) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800504#ifndef CONFIG_NO_RC4
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700505#ifndef OPENSSL_NO_RC4
506 case CRYPTO_CIPHER_ALG_RC4:
507 cipher = EVP_rc4();
508 break;
509#endif /* OPENSSL_NO_RC4 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800510#endif /* CONFIG_NO_RC4 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700511#ifndef OPENSSL_NO_AES
512 case CRYPTO_CIPHER_ALG_AES:
513 switch (key_len) {
514 case 16:
515 cipher = EVP_aes_128_cbc();
516 break;
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -0700517#ifndef OPENSSL_IS_BORINGSSL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700518 case 24:
519 cipher = EVP_aes_192_cbc();
520 break;
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -0700521#endif /* OPENSSL_IS_BORINGSSL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700522 case 32:
523 cipher = EVP_aes_256_cbc();
524 break;
525 default:
526 os_free(ctx);
527 return NULL;
528 }
529 break;
530#endif /* OPENSSL_NO_AES */
531#ifndef OPENSSL_NO_DES
532 case CRYPTO_CIPHER_ALG_3DES:
533 cipher = EVP_des_ede3_cbc();
534 break;
535 case CRYPTO_CIPHER_ALG_DES:
536 cipher = EVP_des_cbc();
537 break;
538#endif /* OPENSSL_NO_DES */
539#ifndef OPENSSL_NO_RC2
540 case CRYPTO_CIPHER_ALG_RC2:
541 cipher = EVP_rc2_ecb();
542 break;
543#endif /* OPENSSL_NO_RC2 */
544 default:
545 os_free(ctx);
546 return NULL;
547 }
548
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800549 if (!(ctx->enc = EVP_CIPHER_CTX_new()) ||
550 !EVP_CIPHER_CTX_set_padding(ctx->enc, 0) ||
551 !EVP_EncryptInit_ex(ctx->enc, cipher, NULL, NULL, NULL) ||
552 !EVP_CIPHER_CTX_set_key_length(ctx->enc, key_len) ||
553 !EVP_EncryptInit_ex(ctx->enc, NULL, NULL, key, iv)) {
554 if (ctx->enc)
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800555 EVP_CIPHER_CTX_free(ctx->enc);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700556 os_free(ctx);
557 return NULL;
558 }
559
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800560 if (!(ctx->dec = EVP_CIPHER_CTX_new()) ||
561 !EVP_CIPHER_CTX_set_padding(ctx->dec, 0) ||
562 !EVP_DecryptInit_ex(ctx->dec, cipher, NULL, NULL, NULL) ||
563 !EVP_CIPHER_CTX_set_key_length(ctx->dec, key_len) ||
564 !EVP_DecryptInit_ex(ctx->dec, NULL, NULL, key, iv)) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800565 EVP_CIPHER_CTX_free(ctx->enc);
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800566 if (ctx->dec)
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800567 EVP_CIPHER_CTX_free(ctx->dec);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700568 os_free(ctx);
569 return NULL;
570 }
571
572 return ctx;
573}
574
575
576int crypto_cipher_encrypt(struct crypto_cipher *ctx, const u8 *plain,
577 u8 *crypt, size_t len)
578{
579 int outl;
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800580 if (!EVP_EncryptUpdate(ctx->enc, crypt, &outl, plain, len))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700581 return -1;
582 return 0;
583}
584
585
586int crypto_cipher_decrypt(struct crypto_cipher *ctx, const u8 *crypt,
587 u8 *plain, size_t len)
588{
589 int outl;
590 outl = len;
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800591 if (!EVP_DecryptUpdate(ctx->dec, plain, &outl, crypt, len))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700592 return -1;
593 return 0;
594}
595
596
597void crypto_cipher_deinit(struct crypto_cipher *ctx)
598{
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800599 EVP_CIPHER_CTX_free(ctx->enc);
600 EVP_CIPHER_CTX_free(ctx->dec);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700601 os_free(ctx);
602}
603
604
605void * dh5_init(struct wpabuf **priv, struct wpabuf **publ)
606{
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700607#if OPENSSL_VERSION_NUMBER < 0x10100000L
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700608 DH *dh;
609 struct wpabuf *pubkey = NULL, *privkey = NULL;
610 size_t publen, privlen;
611
612 *priv = NULL;
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700613 wpabuf_free(*publ);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700614 *publ = NULL;
615
616 dh = DH_new();
617 if (dh == NULL)
618 return NULL;
619
620 dh->g = BN_new();
621 if (dh->g == NULL || BN_set_word(dh->g, 2) != 1)
622 goto err;
623
624 dh->p = get_group5_prime();
625 if (dh->p == NULL)
626 goto err;
627
628 if (DH_generate_key(dh) != 1)
629 goto err;
630
631 publen = BN_num_bytes(dh->pub_key);
632 pubkey = wpabuf_alloc(publen);
633 if (pubkey == NULL)
634 goto err;
635 privlen = BN_num_bytes(dh->priv_key);
636 privkey = wpabuf_alloc(privlen);
637 if (privkey == NULL)
638 goto err;
639
640 BN_bn2bin(dh->pub_key, wpabuf_put(pubkey, publen));
641 BN_bn2bin(dh->priv_key, wpabuf_put(privkey, privlen));
642
643 *priv = privkey;
644 *publ = pubkey;
645 return dh;
646
647err:
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800648 wpabuf_clear_free(pubkey);
649 wpabuf_clear_free(privkey);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700650 DH_free(dh);
651 return NULL;
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700652#else
653 DH *dh;
654 struct wpabuf *pubkey = NULL, *privkey = NULL;
655 size_t publen, privlen;
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -0700656 BIGNUM *p = NULL, *g;
657 const BIGNUM *priv_key = NULL, *pub_key = NULL;
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700658
659 *priv = NULL;
660 wpabuf_free(*publ);
661 *publ = NULL;
662
663 dh = DH_new();
664 if (dh == NULL)
665 return NULL;
666
667 g = BN_new();
668 p = get_group5_prime();
669 if (!g || BN_set_word(g, 2) != 1 || !p ||
670 DH_set0_pqg(dh, p, NULL, g) != 1)
671 goto err;
672 p = NULL;
673 g = NULL;
674
675 if (DH_generate_key(dh) != 1)
676 goto err;
677
678 DH_get0_key(dh, &pub_key, &priv_key);
679 publen = BN_num_bytes(pub_key);
680 pubkey = wpabuf_alloc(publen);
681 if (!pubkey)
682 goto err;
683 privlen = BN_num_bytes(priv_key);
684 privkey = wpabuf_alloc(privlen);
685 if (!privkey)
686 goto err;
687
688 BN_bn2bin(pub_key, wpabuf_put(pubkey, publen));
689 BN_bn2bin(priv_key, wpabuf_put(privkey, privlen));
690
691 *priv = privkey;
692 *publ = pubkey;
693 return dh;
694
695err:
696 BN_free(p);
697 BN_free(g);
698 wpabuf_clear_free(pubkey);
699 wpabuf_clear_free(privkey);
700 DH_free(dh);
701 return NULL;
702#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700703}
704
705
Dmitry Shmidt04949592012-07-19 12:16:46 -0700706void * dh5_init_fixed(const struct wpabuf *priv, const struct wpabuf *publ)
707{
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700708#if OPENSSL_VERSION_NUMBER < 0x10100000L
Dmitry Shmidt04949592012-07-19 12:16:46 -0700709 DH *dh;
710
711 dh = DH_new();
712 if (dh == NULL)
713 return NULL;
714
715 dh->g = BN_new();
716 if (dh->g == NULL || BN_set_word(dh->g, 2) != 1)
717 goto err;
718
719 dh->p = get_group5_prime();
720 if (dh->p == NULL)
721 goto err;
722
723 dh->priv_key = BN_bin2bn(wpabuf_head(priv), wpabuf_len(priv), NULL);
724 if (dh->priv_key == NULL)
725 goto err;
726
727 dh->pub_key = BN_bin2bn(wpabuf_head(publ), wpabuf_len(publ), NULL);
728 if (dh->pub_key == NULL)
729 goto err;
730
731 if (DH_generate_key(dh) != 1)
732 goto err;
733
734 return dh;
735
736err:
737 DH_free(dh);
738 return NULL;
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700739#else
740 DH *dh;
741 BIGNUM *p = NULL, *g, *priv_key = NULL, *pub_key = NULL;
742
743 dh = DH_new();
744 if (dh == NULL)
745 return NULL;
746
747 g = BN_new();
748 p = get_group5_prime();
749 if (!g || BN_set_word(g, 2) != 1 || !p ||
750 DH_set0_pqg(dh, p, NULL, g) != 1)
751 goto err;
752 p = NULL;
753 g = NULL;
754
755 priv_key = BN_bin2bn(wpabuf_head(priv), wpabuf_len(priv), NULL);
756 pub_key = BN_bin2bn(wpabuf_head(publ), wpabuf_len(publ), NULL);
757 if (!priv_key || !pub_key || DH_set0_key(dh, pub_key, priv_key) != 0)
758 goto err;
759 pub_key = NULL;
760 priv_key = NULL;
761
762 if (DH_generate_key(dh) != 1)
763 goto err;
764
765 return dh;
766
767err:
768 BN_free(p);
769 BN_free(g);
770 BN_free(pub_key);
771 BN_clear_free(priv_key);
772 DH_free(dh);
773 return NULL;
774#endif
Dmitry Shmidt04949592012-07-19 12:16:46 -0700775}
776
777
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700778struct wpabuf * dh5_derive_shared(void *ctx, const struct wpabuf *peer_public,
779 const struct wpabuf *own_private)
780{
781 BIGNUM *pub_key;
782 struct wpabuf *res = NULL;
783 size_t rlen;
784 DH *dh = ctx;
785 int keylen;
786
787 if (ctx == NULL)
788 return NULL;
789
790 pub_key = BN_bin2bn(wpabuf_head(peer_public), wpabuf_len(peer_public),
791 NULL);
792 if (pub_key == NULL)
793 return NULL;
794
795 rlen = DH_size(dh);
796 res = wpabuf_alloc(rlen);
797 if (res == NULL)
798 goto err;
799
800 keylen = DH_compute_key(wpabuf_mhead(res), pub_key, dh);
801 if (keylen < 0)
802 goto err;
803 wpabuf_put(res, keylen);
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -0700804 BN_clear_free(pub_key);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700805
806 return res;
807
808err:
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -0700809 BN_clear_free(pub_key);
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800810 wpabuf_clear_free(res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700811 return NULL;
812}
813
814
815void dh5_free(void *ctx)
816{
817 DH *dh;
818 if (ctx == NULL)
819 return;
820 dh = ctx;
821 DH_free(dh);
822}
Dmitry Shmidt04949592012-07-19 12:16:46 -0700823
824
825struct crypto_hash {
Dmitry Shmidt55840ad2015-12-14 12:45:46 -0800826 HMAC_CTX *ctx;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700827};
828
829
830struct crypto_hash * crypto_hash_init(enum crypto_hash_alg alg, const u8 *key,
831 size_t key_len)
832{
833 struct crypto_hash *ctx;
834 const EVP_MD *md;
835
836 switch (alg) {
837#ifndef OPENSSL_NO_MD5
838 case CRYPTO_HASH_ALG_HMAC_MD5:
839 md = EVP_md5();
840 break;
841#endif /* OPENSSL_NO_MD5 */
842#ifndef OPENSSL_NO_SHA
843 case CRYPTO_HASH_ALG_HMAC_SHA1:
844 md = EVP_sha1();
845 break;
846#endif /* OPENSSL_NO_SHA */
847#ifndef OPENSSL_NO_SHA256
848#ifdef CONFIG_SHA256
849 case CRYPTO_HASH_ALG_HMAC_SHA256:
850 md = EVP_sha256();
851 break;
852#endif /* CONFIG_SHA256 */
853#endif /* OPENSSL_NO_SHA256 */
854 default:
855 return NULL;
856 }
857
858 ctx = os_zalloc(sizeof(*ctx));
859 if (ctx == NULL)
860 return NULL;
Dmitry Shmidt55840ad2015-12-14 12:45:46 -0800861 ctx->ctx = HMAC_CTX_new();
862 if (!ctx->ctx) {
863 os_free(ctx);
864 return NULL;
865 }
866
867 if (HMAC_Init_ex(ctx->ctx, key, key_len, md, NULL) != 1) {
868 HMAC_CTX_free(ctx->ctx);
869 bin_clear_free(ctx, sizeof(*ctx));
870 return NULL;
871 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700872
873 return ctx;
874}
875
876
877void crypto_hash_update(struct crypto_hash *ctx, const u8 *data, size_t len)
878{
879 if (ctx == NULL)
880 return;
Dmitry Shmidt55840ad2015-12-14 12:45:46 -0800881 HMAC_Update(ctx->ctx, data, len);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700882}
883
884
885int crypto_hash_finish(struct crypto_hash *ctx, u8 *mac, size_t *len)
886{
887 unsigned int mdlen;
888 int res;
889
890 if (ctx == NULL)
891 return -2;
892
893 if (mac == NULL || len == NULL) {
Dmitry Shmidt55840ad2015-12-14 12:45:46 -0800894 HMAC_CTX_free(ctx->ctx);
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800895 bin_clear_free(ctx, sizeof(*ctx));
Dmitry Shmidt04949592012-07-19 12:16:46 -0700896 return 0;
897 }
898
899 mdlen = *len;
Dmitry Shmidt55840ad2015-12-14 12:45:46 -0800900 res = HMAC_Final(ctx->ctx, mac, &mdlen);
901 HMAC_CTX_free(ctx->ctx);
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800902 bin_clear_free(ctx, sizeof(*ctx));
Dmitry Shmidt04949592012-07-19 12:16:46 -0700903
904 if (res == 1) {
905 *len = mdlen;
906 return 0;
907 }
908
909 return -1;
910}
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700911
912
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800913static int openssl_hmac_vector(const EVP_MD *type, const u8 *key,
914 size_t key_len, size_t num_elem,
915 const u8 *addr[], const size_t *len, u8 *mac,
916 unsigned int mdlen)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700917{
Dmitry Shmidt55840ad2015-12-14 12:45:46 -0800918 HMAC_CTX *ctx;
919 size_t i;
920 int res;
921
922 if (TEST_FAIL())
923 return -1;
924
925 ctx = HMAC_CTX_new();
926 if (!ctx)
927 return -1;
928 res = HMAC_Init_ex(ctx, key, key_len, type, NULL);
929 if (res != 1)
930 goto done;
931
932 for (i = 0; i < num_elem; i++)
933 HMAC_Update(ctx, addr[i], len[i]);
934
935 res = HMAC_Final(ctx, mac, &mdlen);
936done:
937 HMAC_CTX_free(ctx);
938
939 return res == 1 ? 0 : -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700940}
941
942
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800943#ifndef CONFIG_FIPS
944
945int hmac_md5_vector(const u8 *key, size_t key_len, size_t num_elem,
946 const u8 *addr[], const size_t *len, u8 *mac)
947{
948 return openssl_hmac_vector(EVP_md5(), key ,key_len, num_elem, addr, len,
949 mac, 16);
950}
951
952
953int hmac_md5(const u8 *key, size_t key_len, const u8 *data, size_t data_len,
954 u8 *mac)
955{
956 return hmac_md5_vector(key, key_len, 1, &data, &data_len, mac);
957}
958
959#endif /* CONFIG_FIPS */
960
961
962int pbkdf2_sha1(const char *passphrase, const u8 *ssid, size_t ssid_len,
963 int iterations, u8 *buf, size_t buflen)
964{
965 if (PKCS5_PBKDF2_HMAC_SHA1(passphrase, os_strlen(passphrase), ssid,
966 ssid_len, iterations, buflen, buf) != 1)
967 return -1;
968 return 0;
969}
970
971
972int hmac_sha1_vector(const u8 *key, size_t key_len, size_t num_elem,
973 const u8 *addr[], const size_t *len, u8 *mac)
974{
975 return openssl_hmac_vector(EVP_sha1(), key, key_len, num_elem, addr,
976 len, mac, 20);
977}
978
979
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700980int hmac_sha1(const u8 *key, size_t key_len, const u8 *data, size_t data_len,
981 u8 *mac)
982{
983 return hmac_sha1_vector(key, key_len, 1, &data, &data_len, mac);
984}
985
986
987#ifdef CONFIG_SHA256
988
989int hmac_sha256_vector(const u8 *key, size_t key_len, size_t num_elem,
990 const u8 *addr[], const size_t *len, u8 *mac)
991{
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800992 return openssl_hmac_vector(EVP_sha256(), key, key_len, num_elem, addr,
993 len, mac, 32);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700994}
995
996
997int hmac_sha256(const u8 *key, size_t key_len, const u8 *data,
998 size_t data_len, u8 *mac)
999{
1000 return hmac_sha256_vector(key, key_len, 1, &data, &data_len, mac);
1001}
1002
1003#endif /* CONFIG_SHA256 */
1004
1005
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001006#ifdef CONFIG_SHA384
1007
1008int hmac_sha384_vector(const u8 *key, size_t key_len, size_t num_elem,
1009 const u8 *addr[], const size_t *len, u8 *mac)
1010{
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001011 return openssl_hmac_vector(EVP_sha384(), key, key_len, num_elem, addr,
1012 len, mac, 32);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001013}
1014
1015
1016int hmac_sha384(const u8 *key, size_t key_len, const u8 *data,
1017 size_t data_len, u8 *mac)
1018{
1019 return hmac_sha384_vector(key, key_len, 1, &data, &data_len, mac);
1020}
1021
1022#endif /* CONFIG_SHA384 */
1023
1024
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001025int crypto_get_random(void *buf, size_t len)
1026{
1027 if (RAND_bytes(buf, len) != 1)
1028 return -1;
1029 return 0;
1030}
1031
1032
1033#ifdef CONFIG_OPENSSL_CMAC
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001034int omac1_aes_vector(const u8 *key, size_t key_len, size_t num_elem,
1035 const u8 *addr[], const size_t *len, u8 *mac)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001036{
1037 CMAC_CTX *ctx;
1038 int ret = -1;
1039 size_t outlen, i;
1040
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001041 if (TEST_FAIL())
1042 return -1;
1043
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001044 ctx = CMAC_CTX_new();
1045 if (ctx == NULL)
1046 return -1;
1047
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001048 if (key_len == 32) {
1049 if (!CMAC_Init(ctx, key, 32, EVP_aes_256_cbc(), NULL))
1050 goto fail;
1051 } else if (key_len == 16) {
1052 if (!CMAC_Init(ctx, key, 16, EVP_aes_128_cbc(), NULL))
1053 goto fail;
1054 } else {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001055 goto fail;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001056 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001057 for (i = 0; i < num_elem; i++) {
1058 if (!CMAC_Update(ctx, addr[i], len[i]))
1059 goto fail;
1060 }
1061 if (!CMAC_Final(ctx, mac, &outlen) || outlen != 16)
1062 goto fail;
1063
1064 ret = 0;
1065fail:
1066 CMAC_CTX_free(ctx);
1067 return ret;
1068}
1069
1070
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001071int omac1_aes_128_vector(const u8 *key, size_t num_elem,
1072 const u8 *addr[], const size_t *len, u8 *mac)
1073{
1074 return omac1_aes_vector(key, 16, num_elem, addr, len, mac);
1075}
1076
1077
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001078int omac1_aes_128(const u8 *key, const u8 *data, size_t data_len, u8 *mac)
1079{
1080 return omac1_aes_128_vector(key, 1, &data, &data_len, mac);
1081}
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001082
1083
1084int omac1_aes_256(const u8 *key, const u8 *data, size_t data_len, u8 *mac)
1085{
1086 return omac1_aes_vector(key, 32, 1, &data, &data_len, mac);
1087}
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001088#endif /* CONFIG_OPENSSL_CMAC */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001089
1090
1091struct crypto_bignum * crypto_bignum_init(void)
1092{
Dmitry Shmidte4663042016-04-04 10:07:49 -07001093 if (TEST_FAIL())
1094 return NULL;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001095 return (struct crypto_bignum *) BN_new();
1096}
1097
1098
1099struct crypto_bignum * crypto_bignum_init_set(const u8 *buf, size_t len)
1100{
Dmitry Shmidte4663042016-04-04 10:07:49 -07001101 BIGNUM *bn;
1102
1103 if (TEST_FAIL())
1104 return NULL;
1105
1106 bn = BN_bin2bn(buf, len, NULL);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001107 return (struct crypto_bignum *) bn;
1108}
1109
1110
1111void crypto_bignum_deinit(struct crypto_bignum *n, int clear)
1112{
1113 if (clear)
1114 BN_clear_free((BIGNUM *) n);
1115 else
1116 BN_free((BIGNUM *) n);
1117}
1118
1119
1120int crypto_bignum_to_bin(const struct crypto_bignum *a,
1121 u8 *buf, size_t buflen, size_t padlen)
1122{
1123 int num_bytes, offset;
1124
Dmitry Shmidte4663042016-04-04 10:07:49 -07001125 if (TEST_FAIL())
1126 return -1;
1127
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001128 if (padlen > buflen)
1129 return -1;
1130
1131 num_bytes = BN_num_bytes((const BIGNUM *) a);
1132 if ((size_t) num_bytes > buflen)
1133 return -1;
1134 if (padlen > (size_t) num_bytes)
1135 offset = padlen - num_bytes;
1136 else
1137 offset = 0;
1138
1139 os_memset(buf, 0, offset);
1140 BN_bn2bin((const BIGNUM *) a, buf + offset);
1141
1142 return num_bytes + offset;
1143}
1144
1145
1146int crypto_bignum_add(const struct crypto_bignum *a,
1147 const struct crypto_bignum *b,
1148 struct crypto_bignum *c)
1149{
1150 return BN_add((BIGNUM *) c, (const BIGNUM *) a, (const BIGNUM *) b) ?
1151 0 : -1;
1152}
1153
1154
1155int crypto_bignum_mod(const struct crypto_bignum *a,
1156 const struct crypto_bignum *b,
1157 struct crypto_bignum *c)
1158{
1159 int res;
1160 BN_CTX *bnctx;
1161
1162 bnctx = BN_CTX_new();
1163 if (bnctx == NULL)
1164 return -1;
1165 res = BN_mod((BIGNUM *) c, (const BIGNUM *) a, (const BIGNUM *) b,
1166 bnctx);
1167 BN_CTX_free(bnctx);
1168
1169 return res ? 0 : -1;
1170}
1171
1172
1173int crypto_bignum_exptmod(const struct crypto_bignum *a,
1174 const struct crypto_bignum *b,
1175 const struct crypto_bignum *c,
1176 struct crypto_bignum *d)
1177{
1178 int res;
1179 BN_CTX *bnctx;
1180
Dmitry Shmidte4663042016-04-04 10:07:49 -07001181 if (TEST_FAIL())
1182 return -1;
1183
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001184 bnctx = BN_CTX_new();
1185 if (bnctx == NULL)
1186 return -1;
1187 res = BN_mod_exp((BIGNUM *) d, (const BIGNUM *) a, (const BIGNUM *) b,
1188 (const BIGNUM *) c, bnctx);
1189 BN_CTX_free(bnctx);
1190
1191 return res ? 0 : -1;
1192}
1193
1194
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001195int crypto_bignum_inverse(const struct crypto_bignum *a,
1196 const struct crypto_bignum *b,
1197 struct crypto_bignum *c)
1198{
1199 BIGNUM *res;
1200 BN_CTX *bnctx;
1201
Dmitry Shmidte4663042016-04-04 10:07:49 -07001202 if (TEST_FAIL())
1203 return -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001204 bnctx = BN_CTX_new();
1205 if (bnctx == NULL)
1206 return -1;
1207 res = BN_mod_inverse((BIGNUM *) c, (const BIGNUM *) a,
1208 (const BIGNUM *) b, bnctx);
1209 BN_CTX_free(bnctx);
1210
1211 return res ? 0 : -1;
1212}
1213
1214
1215int crypto_bignum_sub(const struct crypto_bignum *a,
1216 const struct crypto_bignum *b,
1217 struct crypto_bignum *c)
1218{
Dmitry Shmidte4663042016-04-04 10:07:49 -07001219 if (TEST_FAIL())
1220 return -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001221 return BN_sub((BIGNUM *) c, (const BIGNUM *) a, (const BIGNUM *) b) ?
1222 0 : -1;
1223}
1224
1225
1226int crypto_bignum_div(const struct crypto_bignum *a,
1227 const struct crypto_bignum *b,
1228 struct crypto_bignum *c)
1229{
1230 int res;
1231
1232 BN_CTX *bnctx;
1233
Dmitry Shmidte4663042016-04-04 10:07:49 -07001234 if (TEST_FAIL())
1235 return -1;
1236
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001237 bnctx = BN_CTX_new();
1238 if (bnctx == NULL)
1239 return -1;
1240 res = BN_div((BIGNUM *) c, NULL, (const BIGNUM *) a,
1241 (const BIGNUM *) b, bnctx);
1242 BN_CTX_free(bnctx);
1243
1244 return res ? 0 : -1;
1245}
1246
1247
1248int crypto_bignum_mulmod(const struct crypto_bignum *a,
1249 const struct crypto_bignum *b,
1250 const struct crypto_bignum *c,
1251 struct crypto_bignum *d)
1252{
1253 int res;
1254
1255 BN_CTX *bnctx;
1256
Dmitry Shmidte4663042016-04-04 10:07:49 -07001257 if (TEST_FAIL())
1258 return -1;
1259
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001260 bnctx = BN_CTX_new();
1261 if (bnctx == NULL)
1262 return -1;
1263 res = BN_mod_mul((BIGNUM *) d, (const BIGNUM *) a, (const BIGNUM *) b,
1264 (const BIGNUM *) c, bnctx);
1265 BN_CTX_free(bnctx);
1266
1267 return res ? 0 : -1;
1268}
1269
1270
1271int crypto_bignum_cmp(const struct crypto_bignum *a,
1272 const struct crypto_bignum *b)
1273{
1274 return BN_cmp((const BIGNUM *) a, (const BIGNUM *) b);
1275}
1276
1277
1278int crypto_bignum_bits(const struct crypto_bignum *a)
1279{
1280 return BN_num_bits((const BIGNUM *) a);
1281}
1282
1283
1284int crypto_bignum_is_zero(const struct crypto_bignum *a)
1285{
1286 return BN_is_zero((const BIGNUM *) a);
1287}
1288
1289
1290int crypto_bignum_is_one(const struct crypto_bignum *a)
1291{
1292 return BN_is_one((const BIGNUM *) a);
1293}
1294
1295
Dmitry Shmidt41712582015-06-29 11:02:15 -07001296int crypto_bignum_legendre(const struct crypto_bignum *a,
1297 const struct crypto_bignum *p)
1298{
1299 BN_CTX *bnctx;
1300 BIGNUM *exp = NULL, *tmp = NULL;
1301 int res = -2;
1302
Dmitry Shmidte4663042016-04-04 10:07:49 -07001303 if (TEST_FAIL())
1304 return -2;
1305
Dmitry Shmidt41712582015-06-29 11:02:15 -07001306 bnctx = BN_CTX_new();
1307 if (bnctx == NULL)
1308 return -2;
1309
1310 exp = BN_new();
1311 tmp = BN_new();
1312 if (!exp || !tmp ||
1313 /* exp = (p-1) / 2 */
1314 !BN_sub(exp, (const BIGNUM *) p, BN_value_one()) ||
1315 !BN_rshift1(exp, exp) ||
1316 !BN_mod_exp(tmp, (const BIGNUM *) a, exp, (const BIGNUM *) p,
1317 bnctx))
1318 goto fail;
1319
1320 if (BN_is_word(tmp, 1))
1321 res = 1;
1322 else if (BN_is_zero(tmp))
1323 res = 0;
1324 else
1325 res = -1;
1326
1327fail:
1328 BN_clear_free(tmp);
1329 BN_clear_free(exp);
1330 BN_CTX_free(bnctx);
1331 return res;
1332}
1333
1334
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001335#ifdef CONFIG_ECC
1336
1337struct crypto_ec {
1338 EC_GROUP *group;
1339 BN_CTX *bnctx;
1340 BIGNUM *prime;
1341 BIGNUM *order;
Dmitry Shmidt41712582015-06-29 11:02:15 -07001342 BIGNUM *a;
1343 BIGNUM *b;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001344};
1345
1346struct crypto_ec * crypto_ec_init(int group)
1347{
1348 struct crypto_ec *e;
1349 int nid;
1350
1351 /* Map from IANA registry for IKE D-H groups to OpenSSL NID */
1352 switch (group) {
1353 case 19:
1354 nid = NID_X9_62_prime256v1;
1355 break;
1356 case 20:
1357 nid = NID_secp384r1;
1358 break;
1359 case 21:
1360 nid = NID_secp521r1;
1361 break;
1362 case 25:
1363 nid = NID_X9_62_prime192v1;
1364 break;
1365 case 26:
1366 nid = NID_secp224r1;
1367 break;
Dmitry Shmidt41712582015-06-29 11:02:15 -07001368#ifdef NID_brainpoolP224r1
1369 case 27:
1370 nid = NID_brainpoolP224r1;
1371 break;
1372#endif /* NID_brainpoolP224r1 */
1373#ifdef NID_brainpoolP256r1
1374 case 28:
1375 nid = NID_brainpoolP256r1;
1376 break;
1377#endif /* NID_brainpoolP256r1 */
1378#ifdef NID_brainpoolP384r1
1379 case 29:
1380 nid = NID_brainpoolP384r1;
1381 break;
1382#endif /* NID_brainpoolP384r1 */
1383#ifdef NID_brainpoolP512r1
1384 case 30:
1385 nid = NID_brainpoolP512r1;
1386 break;
1387#endif /* NID_brainpoolP512r1 */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001388 default:
1389 return NULL;
1390 }
1391
1392 e = os_zalloc(sizeof(*e));
1393 if (e == NULL)
1394 return NULL;
1395
1396 e->bnctx = BN_CTX_new();
1397 e->group = EC_GROUP_new_by_curve_name(nid);
1398 e->prime = BN_new();
1399 e->order = BN_new();
Dmitry Shmidt41712582015-06-29 11:02:15 -07001400 e->a = BN_new();
1401 e->b = BN_new();
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001402 if (e->group == NULL || e->bnctx == NULL || e->prime == NULL ||
Dmitry Shmidt41712582015-06-29 11:02:15 -07001403 e->order == NULL || e->a == NULL || e->b == NULL ||
1404 !EC_GROUP_get_curve_GFp(e->group, e->prime, e->a, e->b, e->bnctx) ||
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001405 !EC_GROUP_get_order(e->group, e->order, e->bnctx)) {
1406 crypto_ec_deinit(e);
1407 e = NULL;
1408 }
1409
1410 return e;
1411}
1412
1413
1414void crypto_ec_deinit(struct crypto_ec *e)
1415{
1416 if (e == NULL)
1417 return;
Dmitry Shmidt41712582015-06-29 11:02:15 -07001418 BN_clear_free(e->b);
1419 BN_clear_free(e->a);
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001420 BN_clear_free(e->order);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001421 BN_clear_free(e->prime);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001422 EC_GROUP_free(e->group);
1423 BN_CTX_free(e->bnctx);
1424 os_free(e);
1425}
1426
1427
1428struct crypto_ec_point * crypto_ec_point_init(struct crypto_ec *e)
1429{
Dmitry Shmidte4663042016-04-04 10:07:49 -07001430 if (TEST_FAIL())
1431 return NULL;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001432 if (e == NULL)
1433 return NULL;
1434 return (struct crypto_ec_point *) EC_POINT_new(e->group);
1435}
1436
1437
1438size_t crypto_ec_prime_len(struct crypto_ec *e)
1439{
1440 return BN_num_bytes(e->prime);
1441}
1442
1443
1444size_t crypto_ec_prime_len_bits(struct crypto_ec *e)
1445{
1446 return BN_num_bits(e->prime);
1447}
1448
1449
1450const struct crypto_bignum * crypto_ec_get_prime(struct crypto_ec *e)
1451{
1452 return (const struct crypto_bignum *) e->prime;
1453}
1454
1455
1456const struct crypto_bignum * crypto_ec_get_order(struct crypto_ec *e)
1457{
1458 return (const struct crypto_bignum *) e->order;
1459}
1460
1461
1462void crypto_ec_point_deinit(struct crypto_ec_point *p, int clear)
1463{
1464 if (clear)
1465 EC_POINT_clear_free((EC_POINT *) p);
1466 else
1467 EC_POINT_free((EC_POINT *) p);
1468}
1469
1470
1471int crypto_ec_point_to_bin(struct crypto_ec *e,
1472 const struct crypto_ec_point *point, u8 *x, u8 *y)
1473{
1474 BIGNUM *x_bn, *y_bn;
1475 int ret = -1;
1476 int len = BN_num_bytes(e->prime);
1477
Dmitry Shmidte4663042016-04-04 10:07:49 -07001478 if (TEST_FAIL())
1479 return -1;
1480
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001481 x_bn = BN_new();
1482 y_bn = BN_new();
1483
1484 if (x_bn && y_bn &&
1485 EC_POINT_get_affine_coordinates_GFp(e->group, (EC_POINT *) point,
1486 x_bn, y_bn, e->bnctx)) {
1487 if (x) {
1488 crypto_bignum_to_bin((struct crypto_bignum *) x_bn,
1489 x, len, len);
1490 }
1491 if (y) {
1492 crypto_bignum_to_bin((struct crypto_bignum *) y_bn,
1493 y, len, len);
1494 }
1495 ret = 0;
1496 }
1497
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001498 BN_clear_free(x_bn);
1499 BN_clear_free(y_bn);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001500 return ret;
1501}
1502
1503
1504struct crypto_ec_point * crypto_ec_point_from_bin(struct crypto_ec *e,
1505 const u8 *val)
1506{
1507 BIGNUM *x, *y;
1508 EC_POINT *elem;
1509 int len = BN_num_bytes(e->prime);
1510
Dmitry Shmidte4663042016-04-04 10:07:49 -07001511 if (TEST_FAIL())
1512 return NULL;
1513
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001514 x = BN_bin2bn(val, len, NULL);
1515 y = BN_bin2bn(val + len, len, NULL);
1516 elem = EC_POINT_new(e->group);
1517 if (x == NULL || y == NULL || elem == NULL) {
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001518 BN_clear_free(x);
1519 BN_clear_free(y);
1520 EC_POINT_clear_free(elem);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001521 return NULL;
1522 }
1523
1524 if (!EC_POINT_set_affine_coordinates_GFp(e->group, elem, x, y,
1525 e->bnctx)) {
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001526 EC_POINT_clear_free(elem);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001527 elem = NULL;
1528 }
1529
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001530 BN_clear_free(x);
1531 BN_clear_free(y);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001532
1533 return (struct crypto_ec_point *) elem;
1534}
1535
1536
1537int crypto_ec_point_add(struct crypto_ec *e, const struct crypto_ec_point *a,
1538 const struct crypto_ec_point *b,
1539 struct crypto_ec_point *c)
1540{
Dmitry Shmidte4663042016-04-04 10:07:49 -07001541 if (TEST_FAIL())
1542 return -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001543 return EC_POINT_add(e->group, (EC_POINT *) c, (const EC_POINT *) a,
1544 (const EC_POINT *) b, e->bnctx) ? 0 : -1;
1545}
1546
1547
1548int crypto_ec_point_mul(struct crypto_ec *e, const struct crypto_ec_point *p,
1549 const struct crypto_bignum *b,
1550 struct crypto_ec_point *res)
1551{
Dmitry Shmidte4663042016-04-04 10:07:49 -07001552 if (TEST_FAIL())
1553 return -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001554 return EC_POINT_mul(e->group, (EC_POINT *) res, NULL,
1555 (const EC_POINT *) p, (const BIGNUM *) b, e->bnctx)
1556 ? 0 : -1;
1557}
1558
1559
1560int crypto_ec_point_invert(struct crypto_ec *e, struct crypto_ec_point *p)
1561{
Dmitry Shmidte4663042016-04-04 10:07:49 -07001562 if (TEST_FAIL())
1563 return -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001564 return EC_POINT_invert(e->group, (EC_POINT *) p, e->bnctx) ? 0 : -1;
1565}
1566
1567
1568int crypto_ec_point_solve_y_coord(struct crypto_ec *e,
1569 struct crypto_ec_point *p,
1570 const struct crypto_bignum *x, int y_bit)
1571{
Dmitry Shmidte4663042016-04-04 10:07:49 -07001572 if (TEST_FAIL())
1573 return -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001574 if (!EC_POINT_set_compressed_coordinates_GFp(e->group, (EC_POINT *) p,
1575 (const BIGNUM *) x, y_bit,
1576 e->bnctx) ||
1577 !EC_POINT_is_on_curve(e->group, (EC_POINT *) p, e->bnctx))
1578 return -1;
1579 return 0;
1580}
1581
1582
Dmitry Shmidt41712582015-06-29 11:02:15 -07001583struct crypto_bignum *
1584crypto_ec_point_compute_y_sqr(struct crypto_ec *e,
1585 const struct crypto_bignum *x)
1586{
1587 BIGNUM *tmp, *tmp2, *y_sqr = NULL;
1588
Dmitry Shmidte4663042016-04-04 10:07:49 -07001589 if (TEST_FAIL())
1590 return NULL;
1591
Dmitry Shmidt41712582015-06-29 11:02:15 -07001592 tmp = BN_new();
1593 tmp2 = BN_new();
1594
1595 /* y^2 = x^3 + ax + b */
1596 if (tmp && tmp2 &&
1597 BN_mod_sqr(tmp, (const BIGNUM *) x, e->prime, e->bnctx) &&
1598 BN_mod_mul(tmp, tmp, (const BIGNUM *) x, e->prime, e->bnctx) &&
1599 BN_mod_mul(tmp2, e->a, (const BIGNUM *) x, e->prime, e->bnctx) &&
1600 BN_mod_add_quick(tmp2, tmp2, tmp, e->prime) &&
1601 BN_mod_add_quick(tmp2, tmp2, e->b, e->prime)) {
1602 y_sqr = tmp2;
1603 tmp2 = NULL;
1604 }
1605
1606 BN_clear_free(tmp);
1607 BN_clear_free(tmp2);
1608
1609 return (struct crypto_bignum *) y_sqr;
1610}
1611
1612
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001613int crypto_ec_point_is_at_infinity(struct crypto_ec *e,
1614 const struct crypto_ec_point *p)
1615{
1616 return EC_POINT_is_at_infinity(e->group, (const EC_POINT *) p);
1617}
1618
1619
1620int crypto_ec_point_is_on_curve(struct crypto_ec *e,
1621 const struct crypto_ec_point *p)
1622{
Dmitry Shmidt41712582015-06-29 11:02:15 -07001623 return EC_POINT_is_on_curve(e->group, (const EC_POINT *) p,
1624 e->bnctx) == 1;
1625}
1626
1627
1628int crypto_ec_point_cmp(const struct crypto_ec *e,
1629 const struct crypto_ec_point *a,
1630 const struct crypto_ec_point *b)
1631{
1632 return EC_POINT_cmp(e->group, (const EC_POINT *) a,
1633 (const EC_POINT *) b, e->bnctx);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001634}
1635
1636#endif /* CONFIG_ECC */