tests: Skip ms_funcs module tests in CONFIG_FIPS=y builds
[mech_eap.git] / src / crypto / crypto_openssl.c
1 /*
2  * Wrapper functions for OpenSSL libcrypto
3  * Copyright (c) 2004-2015, Jouni Malinen <j@w1.fi>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
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>
17 #include <openssl/hmac.h>
18 #include <openssl/rand.h>
19 #ifdef CONFIG_OPENSSL_CMAC
20 #include <openssl/cmac.h>
21 #endif /* CONFIG_OPENSSL_CMAC */
22 #ifdef CONFIG_ECC
23 #include <openssl/ec.h>
24 #endif /* CONFIG_ECC */
25
26 #include "common.h"
27 #include "wpabuf.h"
28 #include "dh_group5.h"
29 #include "sha1.h"
30 #include "sha256.h"
31 #include "sha384.h"
32 #include "crypto.h"
33
34 static BIGNUM * get_group5_prime(void)
35 {
36 #ifdef OPENSSL_IS_BORINGSSL
37         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);
56 #else /* OPENSSL_IS_BORINGSSL */
57         return get_rfc3526_prime_1536(NULL);
58 #endif /* OPENSSL_IS_BORINGSSL */
59 }
60
61 #ifdef OPENSSL_NO_SHA256
62 #define NO_SHA256_WRAPPER
63 #endif
64
65 static int openssl_digest_vector(const EVP_MD *type, size_t num_elem,
66                                  const u8 *addr[], const size_t *len, u8 *mac)
67 {
68         EVP_MD_CTX ctx;
69         size_t i;
70         unsigned int mac_len;
71
72         EVP_MD_CTX_init(&ctx);
73         if (!EVP_DigestInit_ex(&ctx, type, NULL)) {
74                 wpa_printf(MSG_ERROR, "OpenSSL: EVP_DigestInit_ex failed: %s",
75                            ERR_error_string(ERR_get_error(), NULL));
76                 return -1;
77         }
78         for (i = 0; i < num_elem; i++) {
79                 if (!EVP_DigestUpdate(&ctx, addr[i], len[i])) {
80                         wpa_printf(MSG_ERROR, "OpenSSL: EVP_DigestUpdate "
81                                    "failed: %s",
82                                    ERR_error_string(ERR_get_error(), NULL));
83                         return -1;
84                 }
85         }
86         if (!EVP_DigestFinal(&ctx, mac, &mac_len)) {
87                 wpa_printf(MSG_ERROR, "OpenSSL: EVP_DigestFinal failed: %s",
88                            ERR_error_string(ERR_get_error(), NULL));
89                 return -1;
90         }
91
92         return 0;
93 }
94
95
96 int md4_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
97 {
98         return openssl_digest_vector(EVP_md4(), num_elem, addr, len, mac);
99 }
100
101
102 void des_encrypt(const u8 *clear, const u8 *key, u8 *cypher)
103 {
104         u8 pkey[8], next, tmp;
105         int i;
106         DES_key_schedule ks;
107
108         /* Add parity bits to the key */
109         next = 0;
110         for (i = 0; i < 7; i++) {
111                 tmp = key[i];
112                 pkey[i] = (tmp >> i) | next | 1;
113                 next = tmp << (7 - i);
114         }
115         pkey[i] = next | 1;
116
117         DES_set_key((DES_cblock *) &pkey, &ks);
118         DES_ecb_encrypt((DES_cblock *) clear, (DES_cblock *) cypher, &ks,
119                         DES_ENCRYPT);
120 }
121
122
123 int rc4_skip(const u8 *key, size_t keylen, size_t skip,
124              u8 *data, size_t data_len)
125 {
126 #ifdef OPENSSL_NO_RC4
127         return -1;
128 #else /* OPENSSL_NO_RC4 */
129         EVP_CIPHER_CTX ctx;
130         int outl;
131         int res = -1;
132         unsigned char skip_buf[16];
133
134         EVP_CIPHER_CTX_init(&ctx);
135         if (!EVP_CIPHER_CTX_set_padding(&ctx, 0) ||
136             !EVP_CipherInit_ex(&ctx, EVP_rc4(), NULL, NULL, NULL, 1) ||
137             !EVP_CIPHER_CTX_set_key_length(&ctx, keylen) ||
138             !EVP_CipherInit_ex(&ctx, NULL, NULL, key, NULL, 1))
139                 goto out;
140
141         while (skip >= sizeof(skip_buf)) {
142                 size_t len = skip;
143                 if (len > sizeof(skip_buf))
144                         len = sizeof(skip_buf);
145                 if (!EVP_CipherUpdate(&ctx, skip_buf, &outl, skip_buf, len))
146                         goto out;
147                 skip -= len;
148         }
149
150         if (EVP_CipherUpdate(&ctx, data, &outl, data, data_len))
151                 res = 0;
152
153 out:
154         EVP_CIPHER_CTX_cleanup(&ctx);
155         return res;
156 #endif /* OPENSSL_NO_RC4 */
157 }
158
159
160 #ifndef CONFIG_FIPS
161 int md5_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
162 {
163         return openssl_digest_vector(EVP_md5(), num_elem, addr, len, mac);
164 }
165 #endif /* CONFIG_FIPS */
166
167
168 int sha1_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
169 {
170         return openssl_digest_vector(EVP_sha1(), num_elem, addr, len, mac);
171 }
172
173
174 #ifndef NO_SHA256_WRAPPER
175 int sha256_vector(size_t num_elem, const u8 *addr[], const size_t *len,
176                   u8 *mac)
177 {
178         return openssl_digest_vector(EVP_sha256(), num_elem, addr, len, mac);
179 }
180 #endif /* NO_SHA256_WRAPPER */
181
182
183 static const EVP_CIPHER * aes_get_evp_cipher(size_t keylen)
184 {
185         switch (keylen) {
186         case 16:
187                 return EVP_aes_128_ecb();
188 #ifndef OPENSSL_IS_BORINGSSL
189         case 24:
190                 return EVP_aes_192_ecb();
191 #endif /* OPENSSL_IS_BORINGSSL */
192         case 32:
193                 return EVP_aes_256_ecb();
194         }
195
196         return NULL;
197 }
198
199
200 void * aes_encrypt_init(const u8 *key, size_t len)
201 {
202         EVP_CIPHER_CTX *ctx;
203         const EVP_CIPHER *type;
204
205         type = aes_get_evp_cipher(len);
206         if (type == NULL)
207                 return NULL;
208
209         ctx = os_malloc(sizeof(*ctx));
210         if (ctx == NULL)
211                 return NULL;
212         EVP_CIPHER_CTX_init(ctx);
213         if (EVP_EncryptInit_ex(ctx, type, NULL, key, NULL) != 1) {
214                 os_free(ctx);
215                 return NULL;
216         }
217         EVP_CIPHER_CTX_set_padding(ctx, 0);
218         return ctx;
219 }
220
221
222 void aes_encrypt(void *ctx, const u8 *plain, u8 *crypt)
223 {
224         EVP_CIPHER_CTX *c = ctx;
225         int clen = 16;
226         if (EVP_EncryptUpdate(c, crypt, &clen, plain, 16) != 1) {
227                 wpa_printf(MSG_ERROR, "OpenSSL: EVP_EncryptUpdate failed: %s",
228                            ERR_error_string(ERR_get_error(), NULL));
229         }
230 }
231
232
233 void aes_encrypt_deinit(void *ctx)
234 {
235         EVP_CIPHER_CTX *c = ctx;
236         u8 buf[16];
237         int len = sizeof(buf);
238         if (EVP_EncryptFinal_ex(c, buf, &len) != 1) {
239                 wpa_printf(MSG_ERROR, "OpenSSL: EVP_EncryptFinal_ex failed: "
240                            "%s", ERR_error_string(ERR_get_error(), NULL));
241         }
242         if (len != 0) {
243                 wpa_printf(MSG_ERROR, "OpenSSL: Unexpected padding length %d "
244                            "in AES encrypt", len);
245         }
246         EVP_CIPHER_CTX_cleanup(c);
247         bin_clear_free(c, sizeof(*c));
248 }
249
250
251 void * aes_decrypt_init(const u8 *key, size_t len)
252 {
253         EVP_CIPHER_CTX *ctx;
254         const EVP_CIPHER *type;
255
256         type = aes_get_evp_cipher(len);
257         if (type == NULL)
258                 return NULL;
259
260         ctx = os_malloc(sizeof(*ctx));
261         if (ctx == NULL)
262                 return NULL;
263         EVP_CIPHER_CTX_init(ctx);
264         if (EVP_DecryptInit_ex(ctx, type, NULL, key, NULL) != 1) {
265                 os_free(ctx);
266                 return NULL;
267         }
268         EVP_CIPHER_CTX_set_padding(ctx, 0);
269         return ctx;
270 }
271
272
273 void aes_decrypt(void *ctx, const u8 *crypt, u8 *plain)
274 {
275         EVP_CIPHER_CTX *c = ctx;
276         int plen = 16;
277         if (EVP_DecryptUpdate(c, plain, &plen, crypt, 16) != 1) {
278                 wpa_printf(MSG_ERROR, "OpenSSL: EVP_DecryptUpdate failed: %s",
279                            ERR_error_string(ERR_get_error(), NULL));
280         }
281 }
282
283
284 void aes_decrypt_deinit(void *ctx)
285 {
286         EVP_CIPHER_CTX *c = ctx;
287         u8 buf[16];
288         int len = sizeof(buf);
289         if (EVP_DecryptFinal_ex(c, buf, &len) != 1) {
290                 wpa_printf(MSG_ERROR, "OpenSSL: EVP_DecryptFinal_ex failed: "
291                            "%s", ERR_error_string(ERR_get_error(), NULL));
292         }
293         if (len != 0) {
294                 wpa_printf(MSG_ERROR, "OpenSSL: Unexpected padding length %d "
295                            "in AES decrypt", len);
296         }
297         EVP_CIPHER_CTX_cleanup(c);
298         bin_clear_free(c, sizeof(*c));
299 }
300
301
302 #ifndef CONFIG_FIPS
303
304 int aes_wrap(const u8 *kek, size_t kek_len, int n, const u8 *plain, u8 *cipher)
305 {
306         AES_KEY actx;
307         int res;
308
309         if (AES_set_encrypt_key(kek, kek_len << 3, &actx))
310                 return -1;
311         res = AES_wrap_key(&actx, NULL, cipher, plain, n * 8);
312         OPENSSL_cleanse(&actx, sizeof(actx));
313         return res <= 0 ? -1 : 0;
314 }
315
316
317 int aes_unwrap(const u8 *kek, size_t kek_len, int n, const u8 *cipher,
318                u8 *plain)
319 {
320         AES_KEY actx;
321         int res;
322
323         if (AES_set_decrypt_key(kek, kek_len << 3, &actx))
324                 return -1;
325         res = AES_unwrap_key(&actx, NULL, plain, cipher, (n + 1) * 8);
326         OPENSSL_cleanse(&actx, sizeof(actx));
327         return res <= 0 ? -1 : 0;
328 }
329
330 #endif /* CONFIG_FIPS */
331
332
333 int aes_128_cbc_encrypt(const u8 *key, const u8 *iv, u8 *data, size_t data_len)
334 {
335         EVP_CIPHER_CTX ctx;
336         int clen, len;
337         u8 buf[16];
338
339         EVP_CIPHER_CTX_init(&ctx);
340         if (EVP_EncryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL, key, iv) != 1)
341                 return -1;
342         EVP_CIPHER_CTX_set_padding(&ctx, 0);
343
344         clen = data_len;
345         if (EVP_EncryptUpdate(&ctx, data, &clen, data, data_len) != 1 ||
346             clen != (int) data_len)
347                 return -1;
348
349         len = sizeof(buf);
350         if (EVP_EncryptFinal_ex(&ctx, buf, &len) != 1 || len != 0)
351                 return -1;
352         EVP_CIPHER_CTX_cleanup(&ctx);
353
354         return 0;
355 }
356
357
358 int aes_128_cbc_decrypt(const u8 *key, const u8 *iv, u8 *data, size_t data_len)
359 {
360         EVP_CIPHER_CTX ctx;
361         int plen, len;
362         u8 buf[16];
363
364         EVP_CIPHER_CTX_init(&ctx);
365         if (EVP_DecryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL, key, iv) != 1)
366                 return -1;
367         EVP_CIPHER_CTX_set_padding(&ctx, 0);
368
369         plen = data_len;
370         if (EVP_DecryptUpdate(&ctx, data, &plen, data, data_len) != 1 ||
371             plen != (int) data_len)
372                 return -1;
373
374         len = sizeof(buf);
375         if (EVP_DecryptFinal_ex(&ctx, buf, &len) != 1 || len != 0)
376                 return -1;
377         EVP_CIPHER_CTX_cleanup(&ctx);
378
379         return 0;
380 }
381
382
383 int crypto_mod_exp(const u8 *base, size_t base_len,
384                    const u8 *power, size_t power_len,
385                    const u8 *modulus, size_t modulus_len,
386                    u8 *result, size_t *result_len)
387 {
388         BIGNUM *bn_base, *bn_exp, *bn_modulus, *bn_result;
389         int ret = -1;
390         BN_CTX *ctx;
391
392         ctx = BN_CTX_new();
393         if (ctx == NULL)
394                 return -1;
395
396         bn_base = BN_bin2bn(base, base_len, NULL);
397         bn_exp = BN_bin2bn(power, power_len, NULL);
398         bn_modulus = BN_bin2bn(modulus, modulus_len, NULL);
399         bn_result = BN_new();
400
401         if (bn_base == NULL || bn_exp == NULL || bn_modulus == NULL ||
402             bn_result == NULL)
403                 goto error;
404
405         if (BN_mod_exp(bn_result, bn_base, bn_exp, bn_modulus, ctx) != 1)
406                 goto error;
407
408         *result_len = BN_bn2bin(bn_result, result);
409         ret = 0;
410
411 error:
412         BN_clear_free(bn_base);
413         BN_clear_free(bn_exp);
414         BN_clear_free(bn_modulus);
415         BN_clear_free(bn_result);
416         BN_CTX_free(ctx);
417         return ret;
418 }
419
420
421 struct crypto_cipher {
422         EVP_CIPHER_CTX enc;
423         EVP_CIPHER_CTX dec;
424 };
425
426
427 struct crypto_cipher * crypto_cipher_init(enum crypto_cipher_alg alg,
428                                           const u8 *iv, const u8 *key,
429                                           size_t key_len)
430 {
431         struct crypto_cipher *ctx;
432         const EVP_CIPHER *cipher;
433
434         ctx = os_zalloc(sizeof(*ctx));
435         if (ctx == NULL)
436                 return NULL;
437
438         switch (alg) {
439 #ifndef OPENSSL_NO_RC4
440         case CRYPTO_CIPHER_ALG_RC4:
441                 cipher = EVP_rc4();
442                 break;
443 #endif /* OPENSSL_NO_RC4 */
444 #ifndef OPENSSL_NO_AES
445         case CRYPTO_CIPHER_ALG_AES:
446                 switch (key_len) {
447                 case 16:
448                         cipher = EVP_aes_128_cbc();
449                         break;
450 #ifndef OPENSSL_IS_BORINGSSL
451                 case 24:
452                         cipher = EVP_aes_192_cbc();
453                         break;
454 #endif /* OPENSSL_IS_BORINGSSL */
455                 case 32:
456                         cipher = EVP_aes_256_cbc();
457                         break;
458                 default:
459                         os_free(ctx);
460                         return NULL;
461                 }
462                 break;
463 #endif /* OPENSSL_NO_AES */
464 #ifndef OPENSSL_NO_DES
465         case CRYPTO_CIPHER_ALG_3DES:
466                 cipher = EVP_des_ede3_cbc();
467                 break;
468         case CRYPTO_CIPHER_ALG_DES:
469                 cipher = EVP_des_cbc();
470                 break;
471 #endif /* OPENSSL_NO_DES */
472 #ifndef OPENSSL_NO_RC2
473         case CRYPTO_CIPHER_ALG_RC2:
474                 cipher = EVP_rc2_ecb();
475                 break;
476 #endif /* OPENSSL_NO_RC2 */
477         default:
478                 os_free(ctx);
479                 return NULL;
480         }
481
482         EVP_CIPHER_CTX_init(&ctx->enc);
483         EVP_CIPHER_CTX_set_padding(&ctx->enc, 0);
484         if (!EVP_EncryptInit_ex(&ctx->enc, cipher, NULL, NULL, NULL) ||
485             !EVP_CIPHER_CTX_set_key_length(&ctx->enc, key_len) ||
486             !EVP_EncryptInit_ex(&ctx->enc, NULL, NULL, key, iv)) {
487                 EVP_CIPHER_CTX_cleanup(&ctx->enc);
488                 os_free(ctx);
489                 return NULL;
490         }
491
492         EVP_CIPHER_CTX_init(&ctx->dec);
493         EVP_CIPHER_CTX_set_padding(&ctx->dec, 0);
494         if (!EVP_DecryptInit_ex(&ctx->dec, cipher, NULL, NULL, NULL) ||
495             !EVP_CIPHER_CTX_set_key_length(&ctx->dec, key_len) ||
496             !EVP_DecryptInit_ex(&ctx->dec, NULL, NULL, key, iv)) {
497                 EVP_CIPHER_CTX_cleanup(&ctx->enc);
498                 EVP_CIPHER_CTX_cleanup(&ctx->dec);
499                 os_free(ctx);
500                 return NULL;
501         }
502
503         return ctx;
504 }
505
506
507 int crypto_cipher_encrypt(struct crypto_cipher *ctx, const u8 *plain,
508                           u8 *crypt, size_t len)
509 {
510         int outl;
511         if (!EVP_EncryptUpdate(&ctx->enc, crypt, &outl, plain, len))
512                 return -1;
513         return 0;
514 }
515
516
517 int crypto_cipher_decrypt(struct crypto_cipher *ctx, const u8 *crypt,
518                           u8 *plain, size_t len)
519 {
520         int outl;
521         outl = len;
522         if (!EVP_DecryptUpdate(&ctx->dec, plain, &outl, crypt, len))
523                 return -1;
524         return 0;
525 }
526
527
528 void crypto_cipher_deinit(struct crypto_cipher *ctx)
529 {
530         EVP_CIPHER_CTX_cleanup(&ctx->enc);
531         EVP_CIPHER_CTX_cleanup(&ctx->dec);
532         os_free(ctx);
533 }
534
535
536 void * dh5_init(struct wpabuf **priv, struct wpabuf **publ)
537 {
538         DH *dh;
539         struct wpabuf *pubkey = NULL, *privkey = NULL;
540         size_t publen, privlen;
541
542         *priv = NULL;
543         *publ = NULL;
544
545         dh = DH_new();
546         if (dh == NULL)
547                 return NULL;
548
549         dh->g = BN_new();
550         if (dh->g == NULL || BN_set_word(dh->g, 2) != 1)
551                 goto err;
552
553         dh->p = get_group5_prime();
554         if (dh->p == NULL)
555                 goto err;
556
557         if (DH_generate_key(dh) != 1)
558                 goto err;
559
560         publen = BN_num_bytes(dh->pub_key);
561         pubkey = wpabuf_alloc(publen);
562         if (pubkey == NULL)
563                 goto err;
564         privlen = BN_num_bytes(dh->priv_key);
565         privkey = wpabuf_alloc(privlen);
566         if (privkey == NULL)
567                 goto err;
568
569         BN_bn2bin(dh->pub_key, wpabuf_put(pubkey, publen));
570         BN_bn2bin(dh->priv_key, wpabuf_put(privkey, privlen));
571
572         *priv = privkey;
573         *publ = pubkey;
574         return dh;
575
576 err:
577         wpabuf_clear_free(pubkey);
578         wpabuf_clear_free(privkey);
579         DH_free(dh);
580         return NULL;
581 }
582
583
584 void * dh5_init_fixed(const struct wpabuf *priv, const struct wpabuf *publ)
585 {
586         DH *dh;
587
588         dh = DH_new();
589         if (dh == NULL)
590                 return NULL;
591
592         dh->g = BN_new();
593         if (dh->g == NULL || BN_set_word(dh->g, 2) != 1)
594                 goto err;
595
596         dh->p = get_group5_prime();
597         if (dh->p == NULL)
598                 goto err;
599
600         dh->priv_key = BN_bin2bn(wpabuf_head(priv), wpabuf_len(priv), NULL);
601         if (dh->priv_key == NULL)
602                 goto err;
603
604         dh->pub_key = BN_bin2bn(wpabuf_head(publ), wpabuf_len(publ), NULL);
605         if (dh->pub_key == NULL)
606                 goto err;
607
608         if (DH_generate_key(dh) != 1)
609                 goto err;
610
611         return dh;
612
613 err:
614         DH_free(dh);
615         return NULL;
616 }
617
618
619 struct wpabuf * dh5_derive_shared(void *ctx, const struct wpabuf *peer_public,
620                                   const struct wpabuf *own_private)
621 {
622         BIGNUM *pub_key;
623         struct wpabuf *res = NULL;
624         size_t rlen;
625         DH *dh = ctx;
626         int keylen;
627
628         if (ctx == NULL)
629                 return NULL;
630
631         pub_key = BN_bin2bn(wpabuf_head(peer_public), wpabuf_len(peer_public),
632                             NULL);
633         if (pub_key == NULL)
634                 return NULL;
635
636         rlen = DH_size(dh);
637         res = wpabuf_alloc(rlen);
638         if (res == NULL)
639                 goto err;
640
641         keylen = DH_compute_key(wpabuf_mhead(res), pub_key, dh);
642         if (keylen < 0)
643                 goto err;
644         wpabuf_put(res, keylen);
645         BN_clear_free(pub_key);
646
647         return res;
648
649 err:
650         BN_clear_free(pub_key);
651         wpabuf_clear_free(res);
652         return NULL;
653 }
654
655
656 void dh5_free(void *ctx)
657 {
658         DH *dh;
659         if (ctx == NULL)
660                 return;
661         dh = ctx;
662         DH_free(dh);
663 }
664
665
666 struct crypto_hash {
667         HMAC_CTX ctx;
668 };
669
670
671 struct crypto_hash * crypto_hash_init(enum crypto_hash_alg alg, const u8 *key,
672                                       size_t key_len)
673 {
674         struct crypto_hash *ctx;
675         const EVP_MD *md;
676
677         switch (alg) {
678 #ifndef OPENSSL_NO_MD5
679         case CRYPTO_HASH_ALG_HMAC_MD5:
680                 md = EVP_md5();
681                 break;
682 #endif /* OPENSSL_NO_MD5 */
683 #ifndef OPENSSL_NO_SHA
684         case CRYPTO_HASH_ALG_HMAC_SHA1:
685                 md = EVP_sha1();
686                 break;
687 #endif /* OPENSSL_NO_SHA */
688 #ifndef OPENSSL_NO_SHA256
689 #ifdef CONFIG_SHA256
690         case CRYPTO_HASH_ALG_HMAC_SHA256:
691                 md = EVP_sha256();
692                 break;
693 #endif /* CONFIG_SHA256 */
694 #endif /* OPENSSL_NO_SHA256 */
695         default:
696                 return NULL;
697         }
698
699         ctx = os_zalloc(sizeof(*ctx));
700         if (ctx == NULL)
701                 return NULL;
702         HMAC_CTX_init(&ctx->ctx);
703
704 #if OPENSSL_VERSION_NUMBER < 0x00909000
705         HMAC_Init_ex(&ctx->ctx, key, key_len, md, NULL);
706 #else /* openssl < 0.9.9 */
707         if (HMAC_Init_ex(&ctx->ctx, key, key_len, md, NULL) != 1) {
708                 bin_clear_free(ctx, sizeof(*ctx));
709                 return NULL;
710         }
711 #endif /* openssl < 0.9.9 */
712
713         return ctx;
714 }
715
716
717 void crypto_hash_update(struct crypto_hash *ctx, const u8 *data, size_t len)
718 {
719         if (ctx == NULL)
720                 return;
721         HMAC_Update(&ctx->ctx, data, len);
722 }
723
724
725 int crypto_hash_finish(struct crypto_hash *ctx, u8 *mac, size_t *len)
726 {
727         unsigned int mdlen;
728         int res;
729
730         if (ctx == NULL)
731                 return -2;
732
733         if (mac == NULL || len == NULL) {
734                 bin_clear_free(ctx, sizeof(*ctx));
735                 return 0;
736         }
737
738         mdlen = *len;
739 #if OPENSSL_VERSION_NUMBER < 0x00909000
740         HMAC_Final(&ctx->ctx, mac, &mdlen);
741         res = 1;
742 #else /* openssl < 0.9.9 */
743         res = HMAC_Final(&ctx->ctx, mac, &mdlen);
744 #endif /* openssl < 0.9.9 */
745         HMAC_CTX_cleanup(&ctx->ctx);
746         bin_clear_free(ctx, sizeof(*ctx));
747
748         if (res == 1) {
749                 *len = mdlen;
750                 return 0;
751         }
752
753         return -1;
754 }
755
756
757 static int openssl_hmac_vector(const EVP_MD *type, const u8 *key,
758                                size_t key_len, size_t num_elem,
759                                const u8 *addr[], const size_t *len, u8 *mac,
760                                unsigned int mdlen)
761 {
762         HMAC_CTX ctx;
763         size_t i;
764         int res;
765
766         HMAC_CTX_init(&ctx);
767 #if OPENSSL_VERSION_NUMBER < 0x00909000
768         HMAC_Init_ex(&ctx, key, key_len, type, NULL);
769 #else /* openssl < 0.9.9 */
770         if (HMAC_Init_ex(&ctx, key, key_len, type, NULL) != 1)
771                 return -1;
772 #endif /* openssl < 0.9.9 */
773
774         for (i = 0; i < num_elem; i++)
775                 HMAC_Update(&ctx, addr[i], len[i]);
776
777 #if OPENSSL_VERSION_NUMBER < 0x00909000
778         HMAC_Final(&ctx, mac, &mdlen);
779         res = 1;
780 #else /* openssl < 0.9.9 */
781         res = HMAC_Final(&ctx, mac, &mdlen);
782 #endif /* openssl < 0.9.9 */
783         HMAC_CTX_cleanup(&ctx);
784
785         return res == 1 ? 0 : -1;
786 }
787
788
789 #ifndef CONFIG_FIPS
790
791 int hmac_md5_vector(const u8 *key, size_t key_len, size_t num_elem,
792                     const u8 *addr[], const size_t *len, u8 *mac)
793 {
794         return openssl_hmac_vector(EVP_md5(), key ,key_len, num_elem, addr, len,
795                                    mac, 16);
796 }
797
798
799 int hmac_md5(const u8 *key, size_t key_len, const u8 *data, size_t data_len,
800              u8 *mac)
801 {
802         return hmac_md5_vector(key, key_len, 1, &data, &data_len, mac);
803 }
804
805 #endif /* CONFIG_FIPS */
806
807
808 int pbkdf2_sha1(const char *passphrase, const u8 *ssid, size_t ssid_len,
809                 int iterations, u8 *buf, size_t buflen)
810 {
811         if (PKCS5_PBKDF2_HMAC_SHA1(passphrase, os_strlen(passphrase), ssid,
812                                    ssid_len, iterations, buflen, buf) != 1)
813                 return -1;
814         return 0;
815 }
816
817
818 int hmac_sha1_vector(const u8 *key, size_t key_len, size_t num_elem,
819                      const u8 *addr[], const size_t *len, u8 *mac)
820 {
821         return openssl_hmac_vector(EVP_sha1(), key, key_len, num_elem, addr,
822                                    len, mac, 20);
823 }
824
825
826 int hmac_sha1(const u8 *key, size_t key_len, const u8 *data, size_t data_len,
827                u8 *mac)
828 {
829         return hmac_sha1_vector(key, key_len, 1, &data, &data_len, mac);
830 }
831
832
833 #ifdef CONFIG_SHA256
834
835 int hmac_sha256_vector(const u8 *key, size_t key_len, size_t num_elem,
836                        const u8 *addr[], const size_t *len, u8 *mac)
837 {
838         return openssl_hmac_vector(EVP_sha256(), key, key_len, num_elem, addr,
839                                    len, mac, 32);
840 }
841
842
843 int hmac_sha256(const u8 *key, size_t key_len, const u8 *data,
844                 size_t data_len, u8 *mac)
845 {
846         return hmac_sha256_vector(key, key_len, 1, &data, &data_len, mac);
847 }
848
849 #endif /* CONFIG_SHA256 */
850
851
852 #ifdef CONFIG_SHA384
853
854 int hmac_sha384_vector(const u8 *key, size_t key_len, size_t num_elem,
855                        const u8 *addr[], const size_t *len, u8 *mac)
856 {
857         return openssl_hmac_vector(EVP_sha384(), key, key_len, num_elem, addr,
858                                    len, mac, 32);
859 }
860
861
862 int hmac_sha384(const u8 *key, size_t key_len, const u8 *data,
863                 size_t data_len, u8 *mac)
864 {
865         return hmac_sha384_vector(key, key_len, 1, &data, &data_len, mac);
866 }
867
868 #endif /* CONFIG_SHA384 */
869
870
871 int crypto_get_random(void *buf, size_t len)
872 {
873         if (RAND_bytes(buf, len) != 1)
874                 return -1;
875         return 0;
876 }
877
878
879 #ifdef CONFIG_OPENSSL_CMAC
880 int omac1_aes_vector(const u8 *key, size_t key_len, size_t num_elem,
881                      const u8 *addr[], const size_t *len, u8 *mac)
882 {
883         CMAC_CTX *ctx;
884         int ret = -1;
885         size_t outlen, i;
886
887         ctx = CMAC_CTX_new();
888         if (ctx == NULL)
889                 return -1;
890
891         if (key_len == 32) {
892                 if (!CMAC_Init(ctx, key, 32, EVP_aes_256_cbc(), NULL))
893                         goto fail;
894         } else if (key_len == 16) {
895                 if (!CMAC_Init(ctx, key, 16, EVP_aes_128_cbc(), NULL))
896                         goto fail;
897         } else {
898                 goto fail;
899         }
900         for (i = 0; i < num_elem; i++) {
901                 if (!CMAC_Update(ctx, addr[i], len[i]))
902                         goto fail;
903         }
904         if (!CMAC_Final(ctx, mac, &outlen) || outlen != 16)
905                 goto fail;
906
907         ret = 0;
908 fail:
909         CMAC_CTX_free(ctx);
910         return ret;
911 }
912
913
914 int omac1_aes_128_vector(const u8 *key, size_t num_elem,
915                          const u8 *addr[], const size_t *len, u8 *mac)
916 {
917         return omac1_aes_vector(key, 16, num_elem, addr, len, mac);
918 }
919
920
921 int omac1_aes_128(const u8 *key, const u8 *data, size_t data_len, u8 *mac)
922 {
923         return omac1_aes_128_vector(key, 1, &data, &data_len, mac);
924 }
925
926
927 int omac1_aes_256(const u8 *key, const u8 *data, size_t data_len, u8 *mac)
928 {
929         return omac1_aes_vector(key, 32, 1, &data, &data_len, mac);
930 }
931 #endif /* CONFIG_OPENSSL_CMAC */
932
933
934 struct crypto_bignum * crypto_bignum_init(void)
935 {
936         return (struct crypto_bignum *) BN_new();
937 }
938
939
940 struct crypto_bignum * crypto_bignum_init_set(const u8 *buf, size_t len)
941 {
942         BIGNUM *bn = BN_bin2bn(buf, len, NULL);
943         return (struct crypto_bignum *) bn;
944 }
945
946
947 void crypto_bignum_deinit(struct crypto_bignum *n, int clear)
948 {
949         if (clear)
950                 BN_clear_free((BIGNUM *) n);
951         else
952                 BN_free((BIGNUM *) n);
953 }
954
955
956 int crypto_bignum_to_bin(const struct crypto_bignum *a,
957                          u8 *buf, size_t buflen, size_t padlen)
958 {
959         int num_bytes, offset;
960
961         if (padlen > buflen)
962                 return -1;
963
964         num_bytes = BN_num_bytes((const BIGNUM *) a);
965         if ((size_t) num_bytes > buflen)
966                 return -1;
967         if (padlen > (size_t) num_bytes)
968                 offset = padlen - num_bytes;
969         else
970                 offset = 0;
971
972         os_memset(buf, 0, offset);
973         BN_bn2bin((const BIGNUM *) a, buf + offset);
974
975         return num_bytes + offset;
976 }
977
978
979 int crypto_bignum_add(const struct crypto_bignum *a,
980                       const struct crypto_bignum *b,
981                       struct crypto_bignum *c)
982 {
983         return BN_add((BIGNUM *) c, (const BIGNUM *) a, (const BIGNUM *) b) ?
984                 0 : -1;
985 }
986
987
988 int crypto_bignum_mod(const struct crypto_bignum *a,
989                       const struct crypto_bignum *b,
990                       struct crypto_bignum *c)
991 {
992         int res;
993         BN_CTX *bnctx;
994
995         bnctx = BN_CTX_new();
996         if (bnctx == NULL)
997                 return -1;
998         res = BN_mod((BIGNUM *) c, (const BIGNUM *) a, (const BIGNUM *) b,
999                      bnctx);
1000         BN_CTX_free(bnctx);
1001
1002         return res ? 0 : -1;
1003 }
1004
1005
1006 int crypto_bignum_exptmod(const struct crypto_bignum *a,
1007                           const struct crypto_bignum *b,
1008                           const struct crypto_bignum *c,
1009                           struct crypto_bignum *d)
1010 {
1011         int res;
1012         BN_CTX *bnctx;
1013
1014         bnctx = BN_CTX_new();
1015         if (bnctx == NULL)
1016                 return -1;
1017         res = BN_mod_exp((BIGNUM *) d, (const BIGNUM *) a, (const BIGNUM *) b,
1018                          (const BIGNUM *) c, bnctx);
1019         BN_CTX_free(bnctx);
1020
1021         return res ? 0 : -1;
1022 }
1023
1024
1025 int crypto_bignum_inverse(const struct crypto_bignum *a,
1026                           const struct crypto_bignum *b,
1027                           struct crypto_bignum *c)
1028 {
1029         BIGNUM *res;
1030         BN_CTX *bnctx;
1031
1032         bnctx = BN_CTX_new();
1033         if (bnctx == NULL)
1034                 return -1;
1035         res = BN_mod_inverse((BIGNUM *) c, (const BIGNUM *) a,
1036                              (const BIGNUM *) b, bnctx);
1037         BN_CTX_free(bnctx);
1038
1039         return res ? 0 : -1;
1040 }
1041
1042
1043 int crypto_bignum_sub(const struct crypto_bignum *a,
1044                       const struct crypto_bignum *b,
1045                       struct crypto_bignum *c)
1046 {
1047         return BN_sub((BIGNUM *) c, (const BIGNUM *) a, (const BIGNUM *) b) ?
1048                 0 : -1;
1049 }
1050
1051
1052 int crypto_bignum_div(const struct crypto_bignum *a,
1053                       const struct crypto_bignum *b,
1054                       struct crypto_bignum *c)
1055 {
1056         int res;
1057
1058         BN_CTX *bnctx;
1059
1060         bnctx = BN_CTX_new();
1061         if (bnctx == NULL)
1062                 return -1;
1063         res = BN_div((BIGNUM *) c, NULL, (const BIGNUM *) a,
1064                      (const BIGNUM *) b, bnctx);
1065         BN_CTX_free(bnctx);
1066
1067         return res ? 0 : -1;
1068 }
1069
1070
1071 int crypto_bignum_mulmod(const struct crypto_bignum *a,
1072                          const struct crypto_bignum *b,
1073                          const struct crypto_bignum *c,
1074                          struct crypto_bignum *d)
1075 {
1076         int res;
1077
1078         BN_CTX *bnctx;
1079
1080         bnctx = BN_CTX_new();
1081         if (bnctx == NULL)
1082                 return -1;
1083         res = BN_mod_mul((BIGNUM *) d, (const BIGNUM *) a, (const BIGNUM *) b,
1084                          (const BIGNUM *) c, bnctx);
1085         BN_CTX_free(bnctx);
1086
1087         return res ? 0 : -1;
1088 }
1089
1090
1091 int crypto_bignum_cmp(const struct crypto_bignum *a,
1092                       const struct crypto_bignum *b)
1093 {
1094         return BN_cmp((const BIGNUM *) a, (const BIGNUM *) b);
1095 }
1096
1097
1098 int crypto_bignum_bits(const struct crypto_bignum *a)
1099 {
1100         return BN_num_bits((const BIGNUM *) a);
1101 }
1102
1103
1104 int crypto_bignum_is_zero(const struct crypto_bignum *a)
1105 {
1106         return BN_is_zero((const BIGNUM *) a);
1107 }
1108
1109
1110 int crypto_bignum_is_one(const struct crypto_bignum *a)
1111 {
1112         return BN_is_one((const BIGNUM *) a);
1113 }
1114
1115
1116 int crypto_bignum_legendre(const struct crypto_bignum *a,
1117                            const struct crypto_bignum *p)
1118 {
1119         BN_CTX *bnctx;
1120         BIGNUM *exp = NULL, *tmp = NULL;
1121         int res = -2;
1122
1123         bnctx = BN_CTX_new();
1124         if (bnctx == NULL)
1125                 return -2;
1126
1127         exp = BN_new();
1128         tmp = BN_new();
1129         if (!exp || !tmp ||
1130             /* exp = (p-1) / 2 */
1131             !BN_sub(exp, (const BIGNUM *) p, BN_value_one()) ||
1132             !BN_rshift1(exp, exp) ||
1133             !BN_mod_exp(tmp, (const BIGNUM *) a, exp, (const BIGNUM *) p,
1134                         bnctx))
1135                 goto fail;
1136
1137         if (BN_is_word(tmp, 1))
1138                 res = 1;
1139         else if (BN_is_zero(tmp))
1140                 res = 0;
1141         else
1142                 res = -1;
1143
1144 fail:
1145         BN_clear_free(tmp);
1146         BN_clear_free(exp);
1147         BN_CTX_free(bnctx);
1148         return res;
1149 }
1150
1151
1152 #ifdef CONFIG_ECC
1153
1154 struct crypto_ec {
1155         EC_GROUP *group;
1156         BN_CTX *bnctx;
1157         BIGNUM *prime;
1158         BIGNUM *order;
1159         BIGNUM *a;
1160         BIGNUM *b;
1161 };
1162
1163 struct crypto_ec * crypto_ec_init(int group)
1164 {
1165         struct crypto_ec *e;
1166         int nid;
1167
1168         /* Map from IANA registry for IKE D-H groups to OpenSSL NID */
1169         switch (group) {
1170         case 19:
1171                 nid = NID_X9_62_prime256v1;
1172                 break;
1173         case 20:
1174                 nid = NID_secp384r1;
1175                 break;
1176         case 21:
1177                 nid = NID_secp521r1;
1178                 break;
1179         case 25:
1180                 nid = NID_X9_62_prime192v1;
1181                 break;
1182         case 26:
1183                 nid = NID_secp224r1;
1184                 break;
1185 #ifdef NID_brainpoolP224r1
1186         case 27:
1187                 nid = NID_brainpoolP224r1;
1188                 break;
1189 #endif /* NID_brainpoolP224r1 */
1190 #ifdef NID_brainpoolP256r1
1191         case 28:
1192                 nid = NID_brainpoolP256r1;
1193                 break;
1194 #endif /* NID_brainpoolP256r1 */
1195 #ifdef NID_brainpoolP384r1
1196         case 29:
1197                 nid = NID_brainpoolP384r1;
1198                 break;
1199 #endif /* NID_brainpoolP384r1 */
1200 #ifdef NID_brainpoolP512r1
1201         case 30:
1202                 nid = NID_brainpoolP512r1;
1203                 break;
1204 #endif /* NID_brainpoolP512r1 */
1205         default:
1206                 return NULL;
1207         }
1208
1209         e = os_zalloc(sizeof(*e));
1210         if (e == NULL)
1211                 return NULL;
1212
1213         e->bnctx = BN_CTX_new();
1214         e->group = EC_GROUP_new_by_curve_name(nid);
1215         e->prime = BN_new();
1216         e->order = BN_new();
1217         e->a = BN_new();
1218         e->b = BN_new();
1219         if (e->group == NULL || e->bnctx == NULL || e->prime == NULL ||
1220             e->order == NULL || e->a == NULL || e->b == NULL ||
1221             !EC_GROUP_get_curve_GFp(e->group, e->prime, e->a, e->b, e->bnctx) ||
1222             !EC_GROUP_get_order(e->group, e->order, e->bnctx)) {
1223                 crypto_ec_deinit(e);
1224                 e = NULL;
1225         }
1226
1227         return e;
1228 }
1229
1230
1231 void crypto_ec_deinit(struct crypto_ec *e)
1232 {
1233         if (e == NULL)
1234                 return;
1235         BN_clear_free(e->b);
1236         BN_clear_free(e->a);
1237         BN_clear_free(e->order);
1238         BN_clear_free(e->prime);
1239         EC_GROUP_free(e->group);
1240         BN_CTX_free(e->bnctx);
1241         os_free(e);
1242 }
1243
1244
1245 struct crypto_ec_point * crypto_ec_point_init(struct crypto_ec *e)
1246 {
1247         if (e == NULL)
1248                 return NULL;
1249         return (struct crypto_ec_point *) EC_POINT_new(e->group);
1250 }
1251
1252
1253 size_t crypto_ec_prime_len(struct crypto_ec *e)
1254 {
1255         return BN_num_bytes(e->prime);
1256 }
1257
1258
1259 size_t crypto_ec_prime_len_bits(struct crypto_ec *e)
1260 {
1261         return BN_num_bits(e->prime);
1262 }
1263
1264
1265 const struct crypto_bignum * crypto_ec_get_prime(struct crypto_ec *e)
1266 {
1267         return (const struct crypto_bignum *) e->prime;
1268 }
1269
1270
1271 const struct crypto_bignum * crypto_ec_get_order(struct crypto_ec *e)
1272 {
1273         return (const struct crypto_bignum *) e->order;
1274 }
1275
1276
1277 void crypto_ec_point_deinit(struct crypto_ec_point *p, int clear)
1278 {
1279         if (clear)
1280                 EC_POINT_clear_free((EC_POINT *) p);
1281         else
1282                 EC_POINT_free((EC_POINT *) p);
1283 }
1284
1285
1286 int crypto_ec_point_to_bin(struct crypto_ec *e,
1287                            const struct crypto_ec_point *point, u8 *x, u8 *y)
1288 {
1289         BIGNUM *x_bn, *y_bn;
1290         int ret = -1;
1291         int len = BN_num_bytes(e->prime);
1292
1293         x_bn = BN_new();
1294         y_bn = BN_new();
1295
1296         if (x_bn && y_bn &&
1297             EC_POINT_get_affine_coordinates_GFp(e->group, (EC_POINT *) point,
1298                                                 x_bn, y_bn, e->bnctx)) {
1299                 if (x) {
1300                         crypto_bignum_to_bin((struct crypto_bignum *) x_bn,
1301                                              x, len, len);
1302                 }
1303                 if (y) {
1304                         crypto_bignum_to_bin((struct crypto_bignum *) y_bn,
1305                                              y, len, len);
1306                 }
1307                 ret = 0;
1308         }
1309
1310         BN_clear_free(x_bn);
1311         BN_clear_free(y_bn);
1312         return ret;
1313 }
1314
1315
1316 struct crypto_ec_point * crypto_ec_point_from_bin(struct crypto_ec *e,
1317                                                   const u8 *val)
1318 {
1319         BIGNUM *x, *y;
1320         EC_POINT *elem;
1321         int len = BN_num_bytes(e->prime);
1322
1323         x = BN_bin2bn(val, len, NULL);
1324         y = BN_bin2bn(val + len, len, NULL);
1325         elem = EC_POINT_new(e->group);
1326         if (x == NULL || y == NULL || elem == NULL) {
1327                 BN_clear_free(x);
1328                 BN_clear_free(y);
1329                 EC_POINT_clear_free(elem);
1330                 return NULL;
1331         }
1332
1333         if (!EC_POINT_set_affine_coordinates_GFp(e->group, elem, x, y,
1334                                                  e->bnctx)) {
1335                 EC_POINT_clear_free(elem);
1336                 elem = NULL;
1337         }
1338
1339         BN_clear_free(x);
1340         BN_clear_free(y);
1341
1342         return (struct crypto_ec_point *) elem;
1343 }
1344
1345
1346 int crypto_ec_point_add(struct crypto_ec *e, const struct crypto_ec_point *a,
1347                         const struct crypto_ec_point *b,
1348                         struct crypto_ec_point *c)
1349 {
1350         return EC_POINT_add(e->group, (EC_POINT *) c, (const EC_POINT *) a,
1351                             (const EC_POINT *) b, e->bnctx) ? 0 : -1;
1352 }
1353
1354
1355 int crypto_ec_point_mul(struct crypto_ec *e, const struct crypto_ec_point *p,
1356                         const struct crypto_bignum *b,
1357                         struct crypto_ec_point *res)
1358 {
1359         return EC_POINT_mul(e->group, (EC_POINT *) res, NULL,
1360                             (const EC_POINT *) p, (const BIGNUM *) b, e->bnctx)
1361                 ? 0 : -1;
1362 }
1363
1364
1365 int crypto_ec_point_invert(struct crypto_ec *e, struct crypto_ec_point *p)
1366 {
1367         return EC_POINT_invert(e->group, (EC_POINT *) p, e->bnctx) ? 0 : -1;
1368 }
1369
1370
1371 int crypto_ec_point_solve_y_coord(struct crypto_ec *e,
1372                                   struct crypto_ec_point *p,
1373                                   const struct crypto_bignum *x, int y_bit)
1374 {
1375         if (!EC_POINT_set_compressed_coordinates_GFp(e->group, (EC_POINT *) p,
1376                                                      (const BIGNUM *) x, y_bit,
1377                                                      e->bnctx) ||
1378             !EC_POINT_is_on_curve(e->group, (EC_POINT *) p, e->bnctx))
1379                 return -1;
1380         return 0;
1381 }
1382
1383
1384 struct crypto_bignum *
1385 crypto_ec_point_compute_y_sqr(struct crypto_ec *e,
1386                               const struct crypto_bignum *x)
1387 {
1388         BIGNUM *tmp, *tmp2, *y_sqr = NULL;
1389
1390         tmp = BN_new();
1391         tmp2 = BN_new();
1392
1393         /* y^2 = x^3 + ax + b */
1394         if (tmp && tmp2 &&
1395             BN_mod_sqr(tmp, (const BIGNUM *) x, e->prime, e->bnctx) &&
1396             BN_mod_mul(tmp, tmp, (const BIGNUM *) x, e->prime, e->bnctx) &&
1397             BN_mod_mul(tmp2, e->a, (const BIGNUM *) x, e->prime, e->bnctx) &&
1398             BN_mod_add_quick(tmp2, tmp2, tmp, e->prime) &&
1399             BN_mod_add_quick(tmp2, tmp2, e->b, e->prime)) {
1400                 y_sqr = tmp2;
1401                 tmp2 = NULL;
1402         }
1403
1404         BN_clear_free(tmp);
1405         BN_clear_free(tmp2);
1406
1407         return (struct crypto_bignum *) y_sqr;
1408 }
1409
1410
1411 int crypto_ec_point_is_at_infinity(struct crypto_ec *e,
1412                                    const struct crypto_ec_point *p)
1413 {
1414         return EC_POINT_is_at_infinity(e->group, (const EC_POINT *) p);
1415 }
1416
1417
1418 int crypto_ec_point_is_on_curve(struct crypto_ec *e,
1419                                 const struct crypto_ec_point *p)
1420 {
1421         return EC_POINT_is_on_curve(e->group, (const EC_POINT *) p,
1422                                     e->bnctx) == 1;
1423 }
1424
1425
1426 int crypto_ec_point_cmp(const struct crypto_ec *e,
1427                         const struct crypto_ec_point *a,
1428                         const struct crypto_ec_point *b)
1429 {
1430         return EC_POINT_cmp(e->group, (const EC_POINT *) a,
1431                             (const EC_POINT *) b, e->bnctx);
1432 }
1433
1434 #endif /* CONFIG_ECC */