2 * Wrapper functions for OpenSSL libcrypto
3 * Copyright (c) 2004-2013, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
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 */
23 #include <openssl/ec.h>
24 #endif /* CONFIG_ECC */
28 #include "dh_group5.h"
31 #if OPENSSL_VERSION_NUMBER < 0x00907000
32 #define DES_key_schedule des_key_schedule
33 #define DES_cblock des_cblock
34 #define DES_set_key(key, schedule) des_set_key((key), *(schedule))
35 #define DES_ecb_encrypt(input, output, ks, enc) \
36 des_ecb_encrypt((input), (output), *(ks), (enc))
37 #endif /* openssl < 0.9.7 */
39 static BIGNUM * get_group5_prime(void)
41 #if OPENSSL_VERSION_NUMBER < 0x00908000
42 static const unsigned char RFC3526_PRIME_1536[] = {
43 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xC9,0x0F,0xDA,0xA2,
44 0x21,0x68,0xC2,0x34,0xC4,0xC6,0x62,0x8B,0x80,0xDC,0x1C,0xD1,
45 0x29,0x02,0x4E,0x08,0x8A,0x67,0xCC,0x74,0x02,0x0B,0xBE,0xA6,
46 0x3B,0x13,0x9B,0x22,0x51,0x4A,0x08,0x79,0x8E,0x34,0x04,0xDD,
47 0xEF,0x95,0x19,0xB3,0xCD,0x3A,0x43,0x1B,0x30,0x2B,0x0A,0x6D,
48 0xF2,0x5F,0x14,0x37,0x4F,0xE1,0x35,0x6D,0x6D,0x51,0xC2,0x45,
49 0xE4,0x85,0xB5,0x76,0x62,0x5E,0x7E,0xC6,0xF4,0x4C,0x42,0xE9,
50 0xA6,0x37,0xED,0x6B,0x0B,0xFF,0x5C,0xB6,0xF4,0x06,0xB7,0xED,
51 0xEE,0x38,0x6B,0xFB,0x5A,0x89,0x9F,0xA5,0xAE,0x9F,0x24,0x11,
52 0x7C,0x4B,0x1F,0xE6,0x49,0x28,0x66,0x51,0xEC,0xE4,0x5B,0x3D,
53 0xC2,0x00,0x7C,0xB8,0xA1,0x63,0xBF,0x05,0x98,0xDA,0x48,0x36,
54 0x1C,0x55,0xD3,0x9A,0x69,0x16,0x3F,0xA8,0xFD,0x24,0xCF,0x5F,
55 0x83,0x65,0x5D,0x23,0xDC,0xA3,0xAD,0x96,0x1C,0x62,0xF3,0x56,
56 0x20,0x85,0x52,0xBB,0x9E,0xD5,0x29,0x07,0x70,0x96,0x96,0x6D,
57 0x67,0x0C,0x35,0x4E,0x4A,0xBC,0x98,0x04,0xF1,0x74,0x6C,0x08,
58 0xCA,0x23,0x73,0x27,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
60 return BN_bin2bn(RFC3526_PRIME_1536, sizeof(RFC3526_PRIME_1536), NULL);
61 #else /* openssl < 0.9.8 */
62 return get_rfc3526_prime_1536(NULL);
63 #endif /* openssl < 0.9.8 */
66 #if OPENSSL_VERSION_NUMBER < 0x00908000
67 #ifndef OPENSSL_NO_SHA256
69 #define NO_SHA256_WRAPPER
73 #endif /* openssl < 0.9.8 */
75 #ifdef OPENSSL_NO_SHA256
76 #define NO_SHA256_WRAPPER
79 static int openssl_digest_vector(const EVP_MD *type, size_t num_elem,
80 const u8 *addr[], const size_t *len, u8 *mac)
86 EVP_MD_CTX_init(&ctx);
87 if (!EVP_DigestInit_ex(&ctx, type, NULL)) {
88 wpa_printf(MSG_ERROR, "OpenSSL: EVP_DigestInit_ex failed: %s",
89 ERR_error_string(ERR_get_error(), NULL));
92 for (i = 0; i < num_elem; i++) {
93 if (!EVP_DigestUpdate(&ctx, addr[i], len[i])) {
94 wpa_printf(MSG_ERROR, "OpenSSL: EVP_DigestUpdate "
96 ERR_error_string(ERR_get_error(), NULL));
100 if (!EVP_DigestFinal(&ctx, mac, &mac_len)) {
101 wpa_printf(MSG_ERROR, "OpenSSL: EVP_DigestFinal failed: %s",
102 ERR_error_string(ERR_get_error(), NULL));
110 int md4_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
112 return openssl_digest_vector(EVP_md4(), num_elem, addr, len, mac);
116 void des_encrypt(const u8 *clear, const u8 *key, u8 *cypher)
118 u8 pkey[8], next, tmp;
122 /* Add parity bits to the key */
124 for (i = 0; i < 7; i++) {
126 pkey[i] = (tmp >> i) | next | 1;
127 next = tmp << (7 - i);
131 DES_set_key(&pkey, &ks);
132 DES_ecb_encrypt((DES_cblock *) clear, (DES_cblock *) cypher, &ks,
137 int rc4_skip(const u8 *key, size_t keylen, size_t skip,
138 u8 *data, size_t data_len)
140 #ifdef OPENSSL_NO_RC4
142 #else /* OPENSSL_NO_RC4 */
146 unsigned char skip_buf[16];
148 EVP_CIPHER_CTX_init(&ctx);
149 if (!EVP_CIPHER_CTX_set_padding(&ctx, 0) ||
150 !EVP_CipherInit_ex(&ctx, EVP_rc4(), NULL, NULL, NULL, 1) ||
151 !EVP_CIPHER_CTX_set_key_length(&ctx, keylen) ||
152 !EVP_CipherInit_ex(&ctx, NULL, NULL, key, NULL, 1))
155 while (skip >= sizeof(skip_buf)) {
157 if (len > sizeof(skip_buf))
158 len = sizeof(skip_buf);
159 if (!EVP_CipherUpdate(&ctx, skip_buf, &outl, skip_buf, len))
164 if (EVP_CipherUpdate(&ctx, data, &outl, data, data_len))
168 EVP_CIPHER_CTX_cleanup(&ctx);
170 #endif /* OPENSSL_NO_RC4 */
174 int md5_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
176 return openssl_digest_vector(EVP_md5(), num_elem, addr, len, mac);
180 int sha1_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
182 return openssl_digest_vector(EVP_sha1(), num_elem, addr, len, mac);
186 #ifndef NO_SHA256_WRAPPER
187 int sha256_vector(size_t num_elem, const u8 *addr[], const size_t *len,
190 return openssl_digest_vector(EVP_sha256(), num_elem, addr, len, mac);
192 #endif /* NO_SHA256_WRAPPER */
195 static const EVP_CIPHER * aes_get_evp_cipher(size_t keylen)
199 return EVP_aes_128_ecb();
201 return EVP_aes_192_ecb();
203 return EVP_aes_256_ecb();
210 void * aes_encrypt_init(const u8 *key, size_t len)
213 const EVP_CIPHER *type;
215 type = aes_get_evp_cipher(len);
219 ctx = os_malloc(sizeof(*ctx));
222 EVP_CIPHER_CTX_init(ctx);
223 if (EVP_EncryptInit_ex(ctx, type, NULL, key, NULL) != 1) {
227 EVP_CIPHER_CTX_set_padding(ctx, 0);
232 void aes_encrypt(void *ctx, const u8 *plain, u8 *crypt)
234 EVP_CIPHER_CTX *c = ctx;
236 if (EVP_EncryptUpdate(c, crypt, &clen, plain, 16) != 1) {
237 wpa_printf(MSG_ERROR, "OpenSSL: EVP_EncryptUpdate failed: %s",
238 ERR_error_string(ERR_get_error(), NULL));
243 void aes_encrypt_deinit(void *ctx)
245 EVP_CIPHER_CTX *c = ctx;
247 int len = sizeof(buf);
248 if (EVP_EncryptFinal_ex(c, buf, &len) != 1) {
249 wpa_printf(MSG_ERROR, "OpenSSL: EVP_EncryptFinal_ex failed: "
250 "%s", ERR_error_string(ERR_get_error(), NULL));
253 wpa_printf(MSG_ERROR, "OpenSSL: Unexpected padding length %d "
254 "in AES encrypt", len);
256 EVP_CIPHER_CTX_cleanup(c);
261 void * aes_decrypt_init(const u8 *key, size_t len)
264 const EVP_CIPHER *type;
266 type = aes_get_evp_cipher(len);
270 ctx = os_malloc(sizeof(*ctx));
273 EVP_CIPHER_CTX_init(ctx);
274 if (EVP_DecryptInit_ex(ctx, type, NULL, key, NULL) != 1) {
278 EVP_CIPHER_CTX_set_padding(ctx, 0);
283 void aes_decrypt(void *ctx, const u8 *crypt, u8 *plain)
285 EVP_CIPHER_CTX *c = ctx;
287 if (EVP_DecryptUpdate(c, plain, &plen, crypt, 16) != 1) {
288 wpa_printf(MSG_ERROR, "OpenSSL: EVP_DecryptUpdate failed: %s",
289 ERR_error_string(ERR_get_error(), NULL));
294 void aes_decrypt_deinit(void *ctx)
296 EVP_CIPHER_CTX *c = ctx;
298 int len = sizeof(buf);
299 if (EVP_DecryptFinal_ex(c, buf, &len) != 1) {
300 wpa_printf(MSG_ERROR, "OpenSSL: EVP_DecryptFinal_ex failed: "
301 "%s", ERR_error_string(ERR_get_error(), NULL));
304 wpa_printf(MSG_ERROR, "OpenSSL: Unexpected padding length %d "
305 "in AES decrypt", len);
307 EVP_CIPHER_CTX_cleanup(c);
312 int crypto_mod_exp(const u8 *base, size_t base_len,
313 const u8 *power, size_t power_len,
314 const u8 *modulus, size_t modulus_len,
315 u8 *result, size_t *result_len)
317 BIGNUM *bn_base, *bn_exp, *bn_modulus, *bn_result;
325 bn_base = BN_bin2bn(base, base_len, NULL);
326 bn_exp = BN_bin2bn(power, power_len, NULL);
327 bn_modulus = BN_bin2bn(modulus, modulus_len, NULL);
328 bn_result = BN_new();
330 if (bn_base == NULL || bn_exp == NULL || bn_modulus == NULL ||
334 if (BN_mod_exp(bn_result, bn_base, bn_exp, bn_modulus, ctx) != 1)
337 *result_len = BN_bn2bin(bn_result, result);
350 struct crypto_cipher {
356 struct crypto_cipher * crypto_cipher_init(enum crypto_cipher_alg alg,
357 const u8 *iv, const u8 *key,
360 struct crypto_cipher *ctx;
361 const EVP_CIPHER *cipher;
363 ctx = os_zalloc(sizeof(*ctx));
368 #ifndef OPENSSL_NO_RC4
369 case CRYPTO_CIPHER_ALG_RC4:
372 #endif /* OPENSSL_NO_RC4 */
373 #ifndef OPENSSL_NO_AES
374 case CRYPTO_CIPHER_ALG_AES:
377 cipher = EVP_aes_128_cbc();
380 cipher = EVP_aes_192_cbc();
383 cipher = EVP_aes_256_cbc();
390 #endif /* OPENSSL_NO_AES */
391 #ifndef OPENSSL_NO_DES
392 case CRYPTO_CIPHER_ALG_3DES:
393 cipher = EVP_des_ede3_cbc();
395 case CRYPTO_CIPHER_ALG_DES:
396 cipher = EVP_des_cbc();
398 #endif /* OPENSSL_NO_DES */
399 #ifndef OPENSSL_NO_RC2
400 case CRYPTO_CIPHER_ALG_RC2:
401 cipher = EVP_rc2_ecb();
403 #endif /* OPENSSL_NO_RC2 */
409 EVP_CIPHER_CTX_init(&ctx->enc);
410 EVP_CIPHER_CTX_set_padding(&ctx->enc, 0);
411 if (!EVP_EncryptInit_ex(&ctx->enc, cipher, NULL, NULL, NULL) ||
412 !EVP_CIPHER_CTX_set_key_length(&ctx->enc, key_len) ||
413 !EVP_EncryptInit_ex(&ctx->enc, NULL, NULL, key, iv)) {
414 EVP_CIPHER_CTX_cleanup(&ctx->enc);
419 EVP_CIPHER_CTX_init(&ctx->dec);
420 EVP_CIPHER_CTX_set_padding(&ctx->dec, 0);
421 if (!EVP_DecryptInit_ex(&ctx->dec, cipher, NULL, NULL, NULL) ||
422 !EVP_CIPHER_CTX_set_key_length(&ctx->dec, key_len) ||
423 !EVP_DecryptInit_ex(&ctx->dec, NULL, NULL, key, iv)) {
424 EVP_CIPHER_CTX_cleanup(&ctx->enc);
425 EVP_CIPHER_CTX_cleanup(&ctx->dec);
434 int crypto_cipher_encrypt(struct crypto_cipher *ctx, const u8 *plain,
435 u8 *crypt, size_t len)
438 if (!EVP_EncryptUpdate(&ctx->enc, crypt, &outl, plain, len))
444 int crypto_cipher_decrypt(struct crypto_cipher *ctx, const u8 *crypt,
445 u8 *plain, size_t len)
449 if (!EVP_DecryptUpdate(&ctx->dec, plain, &outl, crypt, len))
455 void crypto_cipher_deinit(struct crypto_cipher *ctx)
457 EVP_CIPHER_CTX_cleanup(&ctx->enc);
458 EVP_CIPHER_CTX_cleanup(&ctx->dec);
463 void * dh5_init(struct wpabuf **priv, struct wpabuf **publ)
466 struct wpabuf *pubkey = NULL, *privkey = NULL;
467 size_t publen, privlen;
477 if (dh->g == NULL || BN_set_word(dh->g, 2) != 1)
480 dh->p = get_group5_prime();
484 if (DH_generate_key(dh) != 1)
487 publen = BN_num_bytes(dh->pub_key);
488 pubkey = wpabuf_alloc(publen);
491 privlen = BN_num_bytes(dh->priv_key);
492 privkey = wpabuf_alloc(privlen);
496 BN_bn2bin(dh->pub_key, wpabuf_put(pubkey, publen));
497 BN_bn2bin(dh->priv_key, wpabuf_put(privkey, privlen));
505 wpabuf_free(privkey);
511 void * dh5_init_fixed(const struct wpabuf *priv, const struct wpabuf *publ)
520 if (dh->g == NULL || BN_set_word(dh->g, 2) != 1)
523 dh->p = get_group5_prime();
527 dh->priv_key = BN_bin2bn(wpabuf_head(priv), wpabuf_len(priv), NULL);
528 if (dh->priv_key == NULL)
531 dh->pub_key = BN_bin2bn(wpabuf_head(publ), wpabuf_len(publ), NULL);
532 if (dh->pub_key == NULL)
535 if (DH_generate_key(dh) != 1)
546 struct wpabuf * dh5_derive_shared(void *ctx, const struct wpabuf *peer_public,
547 const struct wpabuf *own_private)
550 struct wpabuf *res = NULL;
558 pub_key = BN_bin2bn(wpabuf_head(peer_public), wpabuf_len(peer_public),
564 res = wpabuf_alloc(rlen);
568 keylen = DH_compute_key(wpabuf_mhead(res), pub_key, dh);
571 wpabuf_put(res, keylen);
583 void dh5_free(void *ctx)
598 struct crypto_hash * crypto_hash_init(enum crypto_hash_alg alg, const u8 *key,
601 struct crypto_hash *ctx;
605 #ifndef OPENSSL_NO_MD5
606 case CRYPTO_HASH_ALG_HMAC_MD5:
609 #endif /* OPENSSL_NO_MD5 */
610 #ifndef OPENSSL_NO_SHA
611 case CRYPTO_HASH_ALG_HMAC_SHA1:
614 #endif /* OPENSSL_NO_SHA */
615 #ifndef OPENSSL_NO_SHA256
617 case CRYPTO_HASH_ALG_HMAC_SHA256:
620 #endif /* CONFIG_SHA256 */
621 #endif /* OPENSSL_NO_SHA256 */
626 ctx = os_zalloc(sizeof(*ctx));
629 HMAC_CTX_init(&ctx->ctx);
631 #if OPENSSL_VERSION_NUMBER < 0x00909000
632 HMAC_Init_ex(&ctx->ctx, key, key_len, md, NULL);
633 #else /* openssl < 0.9.9 */
634 if (HMAC_Init_ex(&ctx->ctx, key, key_len, md, NULL) != 1) {
638 #endif /* openssl < 0.9.9 */
644 void crypto_hash_update(struct crypto_hash *ctx, const u8 *data, size_t len)
648 HMAC_Update(&ctx->ctx, data, len);
652 int crypto_hash_finish(struct crypto_hash *ctx, u8 *mac, size_t *len)
660 if (mac == NULL || len == NULL) {
666 #if OPENSSL_VERSION_NUMBER < 0x00909000
667 HMAC_Final(&ctx->ctx, mac, &mdlen);
669 #else /* openssl < 0.9.9 */
670 res = HMAC_Final(&ctx->ctx, mac, &mdlen);
671 #endif /* openssl < 0.9.9 */
672 HMAC_CTX_cleanup(&ctx->ctx);
684 int pbkdf2_sha1(const char *passphrase, const u8 *ssid, size_t ssid_len,
685 int iterations, u8 *buf, size_t buflen)
687 #if OPENSSL_VERSION_NUMBER < 0x00908000
688 if (PKCS5_PBKDF2_HMAC_SHA1(passphrase, os_strlen(passphrase),
689 (unsigned char *) ssid,
690 ssid_len, 4096, buflen, buf) != 1)
692 #else /* openssl < 0.9.8 */
693 if (PKCS5_PBKDF2_HMAC_SHA1(passphrase, os_strlen(passphrase), ssid,
694 ssid_len, 4096, buflen, buf) != 1)
696 #endif /* openssl < 0.9.8 */
701 int hmac_sha1_vector(const u8 *key, size_t key_len, size_t num_elem,
702 const u8 *addr[], const size_t *len, u8 *mac)
710 #if OPENSSL_VERSION_NUMBER < 0x00909000
711 HMAC_Init_ex(&ctx, key, key_len, EVP_sha1(), NULL);
712 #else /* openssl < 0.9.9 */
713 if (HMAC_Init_ex(&ctx, key, key_len, EVP_sha1(), NULL) != 1)
715 #endif /* openssl < 0.9.9 */
717 for (i = 0; i < num_elem; i++)
718 HMAC_Update(&ctx, addr[i], len[i]);
721 #if OPENSSL_VERSION_NUMBER < 0x00909000
722 HMAC_Final(&ctx, mac, &mdlen);
724 #else /* openssl < 0.9.9 */
725 res = HMAC_Final(&ctx, mac, &mdlen);
726 #endif /* openssl < 0.9.9 */
727 HMAC_CTX_cleanup(&ctx);
729 return res == 1 ? 0 : -1;
733 int hmac_sha1(const u8 *key, size_t key_len, const u8 *data, size_t data_len,
736 return hmac_sha1_vector(key, key_len, 1, &data, &data_len, mac);
742 int hmac_sha256_vector(const u8 *key, size_t key_len, size_t num_elem,
743 const u8 *addr[], const size_t *len, u8 *mac)
751 #if OPENSSL_VERSION_NUMBER < 0x00909000
752 HMAC_Init_ex(&ctx, key, key_len, EVP_sha256(), NULL);
753 #else /* openssl < 0.9.9 */
754 if (HMAC_Init_ex(&ctx, key, key_len, EVP_sha256(), NULL) != 1)
756 #endif /* openssl < 0.9.9 */
758 for (i = 0; i < num_elem; i++)
759 HMAC_Update(&ctx, addr[i], len[i]);
762 #if OPENSSL_VERSION_NUMBER < 0x00909000
763 HMAC_Final(&ctx, mac, &mdlen);
765 #else /* openssl < 0.9.9 */
766 res = HMAC_Final(&ctx, mac, &mdlen);
767 #endif /* openssl < 0.9.9 */
768 HMAC_CTX_cleanup(&ctx);
770 return res == 1 ? 0 : -1;
774 int hmac_sha256(const u8 *key, size_t key_len, const u8 *data,
775 size_t data_len, u8 *mac)
777 return hmac_sha256_vector(key, key_len, 1, &data, &data_len, mac);
780 #endif /* CONFIG_SHA256 */
783 int crypto_get_random(void *buf, size_t len)
785 if (RAND_bytes(buf, len) != 1)
791 #ifdef CONFIG_OPENSSL_CMAC
792 int omac1_aes_128_vector(const u8 *key, size_t num_elem,
793 const u8 *addr[], const size_t *len, u8 *mac)
799 ctx = CMAC_CTX_new();
803 if (!CMAC_Init(ctx, key, 16, EVP_aes_128_cbc(), NULL))
805 for (i = 0; i < num_elem; i++) {
806 if (!CMAC_Update(ctx, addr[i], len[i]))
809 if (!CMAC_Final(ctx, mac, &outlen) || outlen != 16)
819 int omac1_aes_128(const u8 *key, const u8 *data, size_t data_len, u8 *mac)
821 return omac1_aes_128_vector(key, 1, &data, &data_len, mac);
823 #endif /* CONFIG_OPENSSL_CMAC */
826 struct crypto_bignum * crypto_bignum_init(void)
828 return (struct crypto_bignum *) BN_new();
832 struct crypto_bignum * crypto_bignum_init_set(const u8 *buf, size_t len)
834 BIGNUM *bn = BN_bin2bn(buf, len, NULL);
835 return (struct crypto_bignum *) bn;
839 void crypto_bignum_deinit(struct crypto_bignum *n, int clear)
842 BN_clear_free((BIGNUM *) n);
844 BN_free((BIGNUM *) n);
848 int crypto_bignum_to_bin(const struct crypto_bignum *a,
849 u8 *buf, size_t buflen, size_t padlen)
851 int num_bytes, offset;
856 num_bytes = BN_num_bytes((const BIGNUM *) a);
857 if ((size_t) num_bytes > buflen)
859 if (padlen > (size_t) num_bytes)
860 offset = padlen - num_bytes;
864 os_memset(buf, 0, offset);
865 BN_bn2bin((const BIGNUM *) a, buf + offset);
867 return num_bytes + offset;
871 int crypto_bignum_add(const struct crypto_bignum *a,
872 const struct crypto_bignum *b,
873 struct crypto_bignum *c)
875 return BN_add((BIGNUM *) c, (const BIGNUM *) a, (const BIGNUM *) b) ?
880 int crypto_bignum_mod(const struct crypto_bignum *a,
881 const struct crypto_bignum *b,
882 struct crypto_bignum *c)
887 bnctx = BN_CTX_new();
890 res = BN_mod((BIGNUM *) c, (const BIGNUM *) a, (const BIGNUM *) b,
898 int crypto_bignum_exptmod(const struct crypto_bignum *a,
899 const struct crypto_bignum *b,
900 const struct crypto_bignum *c,
901 struct crypto_bignum *d)
906 bnctx = BN_CTX_new();
909 res = BN_mod_exp((BIGNUM *) d, (const BIGNUM *) a, (const BIGNUM *) b,
910 (const BIGNUM *) c, bnctx);
917 int crypto_bignum_rshift(const struct crypto_bignum *a, int n,
918 struct crypto_bignum *b)
920 return BN_rshift((BIGNUM *) b, (const BIGNUM *) a, n) ? 0 : -1;
924 int crypto_bignum_inverse(const struct crypto_bignum *a,
925 const struct crypto_bignum *b,
926 struct crypto_bignum *c)
931 bnctx = BN_CTX_new();
934 res = BN_mod_inverse((BIGNUM *) c, (const BIGNUM *) a,
935 (const BIGNUM *) b, bnctx);
942 int crypto_bignum_sub(const struct crypto_bignum *a,
943 const struct crypto_bignum *b,
944 struct crypto_bignum *c)
946 return BN_sub((BIGNUM *) c, (const BIGNUM *) a, (const BIGNUM *) b) ?
951 int crypto_bignum_div(const struct crypto_bignum *a,
952 const struct crypto_bignum *b,
953 struct crypto_bignum *c)
959 bnctx = BN_CTX_new();
962 res = BN_div((BIGNUM *) c, NULL, (const BIGNUM *) a,
963 (const BIGNUM *) b, bnctx);
970 int crypto_bignum_mulmod(const struct crypto_bignum *a,
971 const struct crypto_bignum *b,
972 const struct crypto_bignum *c,
973 struct crypto_bignum *d)
979 bnctx = BN_CTX_new();
982 res = BN_mod_mul((BIGNUM *) d, (const BIGNUM *) a, (const BIGNUM *) b,
983 (const BIGNUM *) c, bnctx);
999 struct crypto_ec * crypto_ec_init(int group)
1001 struct crypto_ec *e;
1004 /* Map from IANA registry for IKE D-H groups to OpenSSL NID */
1007 nid = NID_X9_62_prime256v1;
1010 nid = NID_secp384r1;
1013 nid = NID_secp521r1;
1016 nid = NID_X9_62_prime192v1;
1019 nid = NID_secp224r1;
1025 e = os_zalloc(sizeof(*e));
1029 e->bnctx = BN_CTX_new();
1030 e->group = EC_GROUP_new_by_curve_name(nid);
1031 e->prime = BN_new();
1032 e->order = BN_new();
1033 if (e->group == NULL || e->bnctx == NULL || e->prime == NULL ||
1035 !EC_GROUP_get_curve_GFp(e->group, e->prime, NULL, NULL, e->bnctx) ||
1036 !EC_GROUP_get_order(e->group, e->order, e->bnctx)) {
1037 crypto_ec_deinit(e);
1045 void crypto_ec_deinit(struct crypto_ec *e)
1050 EC_GROUP_free(e->group);
1051 BN_CTX_free(e->bnctx);
1056 struct crypto_ec_point * crypto_ec_point_init(struct crypto_ec *e)
1060 return (struct crypto_ec_point *) EC_POINT_new(e->group);
1064 size_t crypto_ec_prime_len(struct crypto_ec *e)
1066 return BN_num_bytes(e->prime);
1070 size_t crypto_ec_prime_len_bits(struct crypto_ec *e)
1072 return BN_num_bits(e->prime);
1076 const struct crypto_bignum * crypto_ec_get_prime(struct crypto_ec *e)
1078 return (const struct crypto_bignum *) e->prime;
1082 const struct crypto_bignum * crypto_ec_get_order(struct crypto_ec *e)
1084 return (const struct crypto_bignum *) e->order;
1088 void crypto_ec_point_deinit(struct crypto_ec_point *p, int clear)
1091 EC_POINT_clear_free((EC_POINT *) p);
1093 EC_POINT_free((EC_POINT *) p);
1097 int crypto_ec_point_to_bin(struct crypto_ec *e,
1098 const struct crypto_ec_point *point, u8 *x, u8 *y)
1100 BIGNUM *x_bn, *y_bn;
1102 int len = BN_num_bytes(e->prime);
1108 EC_POINT_get_affine_coordinates_GFp(e->group, (EC_POINT *) point,
1109 x_bn, y_bn, e->bnctx)) {
1111 crypto_bignum_to_bin((struct crypto_bignum *) x_bn,
1115 crypto_bignum_to_bin((struct crypto_bignum *) y_bn,
1127 struct crypto_ec_point * crypto_ec_point_from_bin(struct crypto_ec *e,
1132 int len = BN_num_bytes(e->prime);
1134 x = BN_bin2bn(val, len, NULL);
1135 y = BN_bin2bn(val + len, len, NULL);
1136 elem = EC_POINT_new(e->group);
1137 if (x == NULL || y == NULL || elem == NULL) {
1140 EC_POINT_free(elem);
1144 if (!EC_POINT_set_affine_coordinates_GFp(e->group, elem, x, y,
1146 EC_POINT_free(elem);
1153 return (struct crypto_ec_point *) elem;
1157 int crypto_ec_point_add(struct crypto_ec *e, const struct crypto_ec_point *a,
1158 const struct crypto_ec_point *b,
1159 struct crypto_ec_point *c)
1161 return EC_POINT_add(e->group, (EC_POINT *) c, (const EC_POINT *) a,
1162 (const EC_POINT *) b, e->bnctx) ? 0 : -1;
1166 int crypto_ec_point_mul(struct crypto_ec *e, const struct crypto_ec_point *p,
1167 const struct crypto_bignum *b,
1168 struct crypto_ec_point *res)
1170 return EC_POINT_mul(e->group, (EC_POINT *) res, NULL,
1171 (const EC_POINT *) p, (const BIGNUM *) b, e->bnctx)
1176 int crypto_ec_point_invert(struct crypto_ec *e, struct crypto_ec_point *p)
1178 return EC_POINT_invert(e->group, (EC_POINT *) p, e->bnctx) ? 0 : -1;
1182 int crypto_ec_point_solve_y_coord(struct crypto_ec *e,
1183 struct crypto_ec_point *p,
1184 const struct crypto_bignum *x, int y_bit)
1186 if (!EC_POINT_set_compressed_coordinates_GFp(e->group, (EC_POINT *) p,
1187 (const BIGNUM *) x, y_bit,
1189 !EC_POINT_is_on_curve(e->group, (EC_POINT *) p, e->bnctx))
1195 int crypto_ec_point_is_at_infinity(struct crypto_ec *e,
1196 const struct crypto_ec_point *p)
1198 return EC_POINT_is_at_infinity(e->group, (const EC_POINT *) p);
1202 int crypto_ec_point_is_on_curve(struct crypto_ec *e,
1203 const struct crypto_ec_point *p)
1205 return EC_POINT_is_on_curve(e->group, (const EC_POINT *) p, e->bnctx);
1208 #endif /* CONFIG_ECC */