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"
33 #if OPENSSL_VERSION_NUMBER < 0x00907000
34 #define DES_key_schedule des_key_schedule
35 #define DES_cblock des_cblock
36 #define DES_set_key(key, schedule) des_set_key((key), *(schedule))
37 #define DES_ecb_encrypt(input, output, ks, enc) \
38 des_ecb_encrypt((input), (output), *(ks), (enc))
39 #endif /* openssl < 0.9.7 */
41 static BIGNUM * get_group5_prime(void)
43 #if OPENSSL_VERSION_NUMBER < 0x00908000 || defined(OPENSSL_IS_BORINGSSL)
44 static const unsigned char RFC3526_PRIME_1536[] = {
45 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xC9,0x0F,0xDA,0xA2,
46 0x21,0x68,0xC2,0x34,0xC4,0xC6,0x62,0x8B,0x80,0xDC,0x1C,0xD1,
47 0x29,0x02,0x4E,0x08,0x8A,0x67,0xCC,0x74,0x02,0x0B,0xBE,0xA6,
48 0x3B,0x13,0x9B,0x22,0x51,0x4A,0x08,0x79,0x8E,0x34,0x04,0xDD,
49 0xEF,0x95,0x19,0xB3,0xCD,0x3A,0x43,0x1B,0x30,0x2B,0x0A,0x6D,
50 0xF2,0x5F,0x14,0x37,0x4F,0xE1,0x35,0x6D,0x6D,0x51,0xC2,0x45,
51 0xE4,0x85,0xB5,0x76,0x62,0x5E,0x7E,0xC6,0xF4,0x4C,0x42,0xE9,
52 0xA6,0x37,0xED,0x6B,0x0B,0xFF,0x5C,0xB6,0xF4,0x06,0xB7,0xED,
53 0xEE,0x38,0x6B,0xFB,0x5A,0x89,0x9F,0xA5,0xAE,0x9F,0x24,0x11,
54 0x7C,0x4B,0x1F,0xE6,0x49,0x28,0x66,0x51,0xEC,0xE4,0x5B,0x3D,
55 0xC2,0x00,0x7C,0xB8,0xA1,0x63,0xBF,0x05,0x98,0xDA,0x48,0x36,
56 0x1C,0x55,0xD3,0x9A,0x69,0x16,0x3F,0xA8,0xFD,0x24,0xCF,0x5F,
57 0x83,0x65,0x5D,0x23,0xDC,0xA3,0xAD,0x96,0x1C,0x62,0xF3,0x56,
58 0x20,0x85,0x52,0xBB,0x9E,0xD5,0x29,0x07,0x70,0x96,0x96,0x6D,
59 0x67,0x0C,0x35,0x4E,0x4A,0xBC,0x98,0x04,0xF1,0x74,0x6C,0x08,
60 0xCA,0x23,0x73,0x27,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
62 return BN_bin2bn(RFC3526_PRIME_1536, sizeof(RFC3526_PRIME_1536), NULL);
63 #else /* openssl < 0.9.8 */
64 return get_rfc3526_prime_1536(NULL);
65 #endif /* openssl < 0.9.8 */
68 #if OPENSSL_VERSION_NUMBER < 0x00908000
69 #ifndef OPENSSL_NO_SHA256
71 #define NO_SHA256_WRAPPER
75 #endif /* openssl < 0.9.8 */
77 #ifdef OPENSSL_NO_SHA256
78 #define NO_SHA256_WRAPPER
81 static int openssl_digest_vector(const EVP_MD *type, size_t num_elem,
82 const u8 *addr[], const size_t *len, u8 *mac)
88 EVP_MD_CTX_init(&ctx);
89 if (!EVP_DigestInit_ex(&ctx, type, NULL)) {
90 wpa_printf(MSG_ERROR, "OpenSSL: EVP_DigestInit_ex failed: %s",
91 ERR_error_string(ERR_get_error(), NULL));
94 for (i = 0; i < num_elem; i++) {
95 if (!EVP_DigestUpdate(&ctx, addr[i], len[i])) {
96 wpa_printf(MSG_ERROR, "OpenSSL: EVP_DigestUpdate "
98 ERR_error_string(ERR_get_error(), NULL));
102 if (!EVP_DigestFinal(&ctx, mac, &mac_len)) {
103 wpa_printf(MSG_ERROR, "OpenSSL: EVP_DigestFinal failed: %s",
104 ERR_error_string(ERR_get_error(), NULL));
112 int md4_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
114 return openssl_digest_vector(EVP_md4(), num_elem, addr, len, mac);
118 void des_encrypt(const u8 *clear, const u8 *key, u8 *cypher)
120 u8 pkey[8], next, tmp;
124 /* Add parity bits to the key */
126 for (i = 0; i < 7; i++) {
128 pkey[i] = (tmp >> i) | next | 1;
129 next = tmp << (7 - i);
133 DES_set_key((DES_cblock *) &pkey, &ks);
134 DES_ecb_encrypt((DES_cblock *) clear, (DES_cblock *) cypher, &ks,
139 int rc4_skip(const u8 *key, size_t keylen, size_t skip,
140 u8 *data, size_t data_len)
142 #ifdef OPENSSL_NO_RC4
144 #else /* OPENSSL_NO_RC4 */
148 unsigned char skip_buf[16];
150 EVP_CIPHER_CTX_init(&ctx);
151 if (!EVP_CIPHER_CTX_set_padding(&ctx, 0) ||
152 !EVP_CipherInit_ex(&ctx, EVP_rc4(), NULL, NULL, NULL, 1) ||
153 !EVP_CIPHER_CTX_set_key_length(&ctx, keylen) ||
154 !EVP_CipherInit_ex(&ctx, NULL, NULL, key, NULL, 1))
157 while (skip >= sizeof(skip_buf)) {
159 if (len > sizeof(skip_buf))
160 len = sizeof(skip_buf);
161 if (!EVP_CipherUpdate(&ctx, skip_buf, &outl, skip_buf, len))
166 if (EVP_CipherUpdate(&ctx, data, &outl, data, data_len))
170 EVP_CIPHER_CTX_cleanup(&ctx);
172 #endif /* OPENSSL_NO_RC4 */
176 int md5_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
178 return openssl_digest_vector(EVP_md5(), num_elem, addr, len, mac);
182 int sha1_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
184 return openssl_digest_vector(EVP_sha1(), num_elem, addr, len, mac);
188 #ifndef NO_SHA256_WRAPPER
189 int sha256_vector(size_t num_elem, const u8 *addr[], const size_t *len,
192 return openssl_digest_vector(EVP_sha256(), num_elem, addr, len, mac);
194 #endif /* NO_SHA256_WRAPPER */
197 static const EVP_CIPHER * aes_get_evp_cipher(size_t keylen)
201 return EVP_aes_128_ecb();
202 #ifndef OPENSSL_IS_BORINGSSL
204 return EVP_aes_192_ecb();
205 #endif /* OPENSSL_IS_BORINGSSL */
207 return EVP_aes_256_ecb();
214 void * aes_encrypt_init(const u8 *key, size_t len)
217 const EVP_CIPHER *type;
219 type = aes_get_evp_cipher(len);
223 ctx = os_malloc(sizeof(*ctx));
226 EVP_CIPHER_CTX_init(ctx);
227 if (EVP_EncryptInit_ex(ctx, type, NULL, key, NULL) != 1) {
231 EVP_CIPHER_CTX_set_padding(ctx, 0);
236 void aes_encrypt(void *ctx, const u8 *plain, u8 *crypt)
238 EVP_CIPHER_CTX *c = ctx;
240 if (EVP_EncryptUpdate(c, crypt, &clen, plain, 16) != 1) {
241 wpa_printf(MSG_ERROR, "OpenSSL: EVP_EncryptUpdate failed: %s",
242 ERR_error_string(ERR_get_error(), NULL));
247 void aes_encrypt_deinit(void *ctx)
249 EVP_CIPHER_CTX *c = ctx;
251 int len = sizeof(buf);
252 if (EVP_EncryptFinal_ex(c, buf, &len) != 1) {
253 wpa_printf(MSG_ERROR, "OpenSSL: EVP_EncryptFinal_ex failed: "
254 "%s", ERR_error_string(ERR_get_error(), NULL));
257 wpa_printf(MSG_ERROR, "OpenSSL: Unexpected padding length %d "
258 "in AES encrypt", len);
260 EVP_CIPHER_CTX_cleanup(c);
261 bin_clear_free(c, sizeof(*c));
265 void * aes_decrypt_init(const u8 *key, size_t len)
268 const EVP_CIPHER *type;
270 type = aes_get_evp_cipher(len);
274 ctx = os_malloc(sizeof(*ctx));
277 EVP_CIPHER_CTX_init(ctx);
278 if (EVP_DecryptInit_ex(ctx, type, NULL, key, NULL) != 1) {
282 EVP_CIPHER_CTX_set_padding(ctx, 0);
287 void aes_decrypt(void *ctx, const u8 *crypt, u8 *plain)
289 EVP_CIPHER_CTX *c = ctx;
291 if (EVP_DecryptUpdate(c, plain, &plen, crypt, 16) != 1) {
292 wpa_printf(MSG_ERROR, "OpenSSL: EVP_DecryptUpdate failed: %s",
293 ERR_error_string(ERR_get_error(), NULL));
298 void aes_decrypt_deinit(void *ctx)
300 EVP_CIPHER_CTX *c = ctx;
302 int len = sizeof(buf);
303 if (EVP_DecryptFinal_ex(c, buf, &len) != 1) {
304 wpa_printf(MSG_ERROR, "OpenSSL: EVP_DecryptFinal_ex failed: "
305 "%s", ERR_error_string(ERR_get_error(), NULL));
308 wpa_printf(MSG_ERROR, "OpenSSL: Unexpected padding length %d "
309 "in AES decrypt", len);
311 EVP_CIPHER_CTX_cleanup(c);
312 bin_clear_free(c, sizeof(*c));
316 int crypto_mod_exp(const u8 *base, size_t base_len,
317 const u8 *power, size_t power_len,
318 const u8 *modulus, size_t modulus_len,
319 u8 *result, size_t *result_len)
321 BIGNUM *bn_base, *bn_exp, *bn_modulus, *bn_result;
329 bn_base = BN_bin2bn(base, base_len, NULL);
330 bn_exp = BN_bin2bn(power, power_len, NULL);
331 bn_modulus = BN_bin2bn(modulus, modulus_len, NULL);
332 bn_result = BN_new();
334 if (bn_base == NULL || bn_exp == NULL || bn_modulus == NULL ||
338 if (BN_mod_exp(bn_result, bn_base, bn_exp, bn_modulus, ctx) != 1)
341 *result_len = BN_bn2bin(bn_result, result);
345 BN_clear_free(bn_base);
346 BN_clear_free(bn_exp);
347 BN_clear_free(bn_modulus);
348 BN_clear_free(bn_result);
354 struct crypto_cipher {
360 struct crypto_cipher * crypto_cipher_init(enum crypto_cipher_alg alg,
361 const u8 *iv, const u8 *key,
364 struct crypto_cipher *ctx;
365 const EVP_CIPHER *cipher;
367 ctx = os_zalloc(sizeof(*ctx));
372 #ifndef OPENSSL_NO_RC4
373 case CRYPTO_CIPHER_ALG_RC4:
376 #endif /* OPENSSL_NO_RC4 */
377 #ifndef OPENSSL_NO_AES
378 case CRYPTO_CIPHER_ALG_AES:
381 cipher = EVP_aes_128_cbc();
383 #ifndef OPENSSL_IS_BORINGSSL
385 cipher = EVP_aes_192_cbc();
387 #endif /* OPENSSL_IS_BORINGSSL */
389 cipher = EVP_aes_256_cbc();
396 #endif /* OPENSSL_NO_AES */
397 #ifndef OPENSSL_NO_DES
398 case CRYPTO_CIPHER_ALG_3DES:
399 cipher = EVP_des_ede3_cbc();
401 case CRYPTO_CIPHER_ALG_DES:
402 cipher = EVP_des_cbc();
404 #endif /* OPENSSL_NO_DES */
405 #ifndef OPENSSL_NO_RC2
406 case CRYPTO_CIPHER_ALG_RC2:
407 cipher = EVP_rc2_ecb();
409 #endif /* OPENSSL_NO_RC2 */
415 EVP_CIPHER_CTX_init(&ctx->enc);
416 EVP_CIPHER_CTX_set_padding(&ctx->enc, 0);
417 if (!EVP_EncryptInit_ex(&ctx->enc, cipher, NULL, NULL, NULL) ||
418 !EVP_CIPHER_CTX_set_key_length(&ctx->enc, key_len) ||
419 !EVP_EncryptInit_ex(&ctx->enc, NULL, NULL, key, iv)) {
420 EVP_CIPHER_CTX_cleanup(&ctx->enc);
425 EVP_CIPHER_CTX_init(&ctx->dec);
426 EVP_CIPHER_CTX_set_padding(&ctx->dec, 0);
427 if (!EVP_DecryptInit_ex(&ctx->dec, cipher, NULL, NULL, NULL) ||
428 !EVP_CIPHER_CTX_set_key_length(&ctx->dec, key_len) ||
429 !EVP_DecryptInit_ex(&ctx->dec, NULL, NULL, key, iv)) {
430 EVP_CIPHER_CTX_cleanup(&ctx->enc);
431 EVP_CIPHER_CTX_cleanup(&ctx->dec);
440 int crypto_cipher_encrypt(struct crypto_cipher *ctx, const u8 *plain,
441 u8 *crypt, size_t len)
444 if (!EVP_EncryptUpdate(&ctx->enc, crypt, &outl, plain, len))
450 int crypto_cipher_decrypt(struct crypto_cipher *ctx, const u8 *crypt,
451 u8 *plain, size_t len)
455 if (!EVP_DecryptUpdate(&ctx->dec, plain, &outl, crypt, len))
461 void crypto_cipher_deinit(struct crypto_cipher *ctx)
463 EVP_CIPHER_CTX_cleanup(&ctx->enc);
464 EVP_CIPHER_CTX_cleanup(&ctx->dec);
469 void * dh5_init(struct wpabuf **priv, struct wpabuf **publ)
472 struct wpabuf *pubkey = NULL, *privkey = NULL;
473 size_t publen, privlen;
483 if (dh->g == NULL || BN_set_word(dh->g, 2) != 1)
486 dh->p = get_group5_prime();
490 if (DH_generate_key(dh) != 1)
493 publen = BN_num_bytes(dh->pub_key);
494 pubkey = wpabuf_alloc(publen);
497 privlen = BN_num_bytes(dh->priv_key);
498 privkey = wpabuf_alloc(privlen);
502 BN_bn2bin(dh->pub_key, wpabuf_put(pubkey, publen));
503 BN_bn2bin(dh->priv_key, wpabuf_put(privkey, privlen));
510 wpabuf_clear_free(pubkey);
511 wpabuf_clear_free(privkey);
517 void * dh5_init_fixed(const struct wpabuf *priv, const struct wpabuf *publ)
526 if (dh->g == NULL || BN_set_word(dh->g, 2) != 1)
529 dh->p = get_group5_prime();
533 dh->priv_key = BN_bin2bn(wpabuf_head(priv), wpabuf_len(priv), NULL);
534 if (dh->priv_key == NULL)
537 dh->pub_key = BN_bin2bn(wpabuf_head(publ), wpabuf_len(publ), NULL);
538 if (dh->pub_key == NULL)
541 if (DH_generate_key(dh) != 1)
552 struct wpabuf * dh5_derive_shared(void *ctx, const struct wpabuf *peer_public,
553 const struct wpabuf *own_private)
556 struct wpabuf *res = NULL;
564 pub_key = BN_bin2bn(wpabuf_head(peer_public), wpabuf_len(peer_public),
570 res = wpabuf_alloc(rlen);
574 keylen = DH_compute_key(wpabuf_mhead(res), pub_key, dh);
577 wpabuf_put(res, keylen);
578 BN_clear_free(pub_key);
583 BN_clear_free(pub_key);
584 wpabuf_clear_free(res);
589 void dh5_free(void *ctx)
604 struct crypto_hash * crypto_hash_init(enum crypto_hash_alg alg, const u8 *key,
607 struct crypto_hash *ctx;
611 #ifndef OPENSSL_NO_MD5
612 case CRYPTO_HASH_ALG_HMAC_MD5:
615 #endif /* OPENSSL_NO_MD5 */
616 #ifndef OPENSSL_NO_SHA
617 case CRYPTO_HASH_ALG_HMAC_SHA1:
620 #endif /* OPENSSL_NO_SHA */
621 #ifndef OPENSSL_NO_SHA256
623 case CRYPTO_HASH_ALG_HMAC_SHA256:
626 #endif /* CONFIG_SHA256 */
627 #endif /* OPENSSL_NO_SHA256 */
632 ctx = os_zalloc(sizeof(*ctx));
635 HMAC_CTX_init(&ctx->ctx);
637 #if OPENSSL_VERSION_NUMBER < 0x00909000
638 HMAC_Init_ex(&ctx->ctx, key, key_len, md, NULL);
639 #else /* openssl < 0.9.9 */
640 if (HMAC_Init_ex(&ctx->ctx, key, key_len, md, NULL) != 1) {
641 bin_clear_free(ctx, sizeof(*ctx));
644 #endif /* openssl < 0.9.9 */
650 void crypto_hash_update(struct crypto_hash *ctx, const u8 *data, size_t len)
654 HMAC_Update(&ctx->ctx, data, len);
658 int crypto_hash_finish(struct crypto_hash *ctx, u8 *mac, size_t *len)
666 if (mac == NULL || len == NULL) {
667 bin_clear_free(ctx, sizeof(*ctx));
672 #if OPENSSL_VERSION_NUMBER < 0x00909000
673 HMAC_Final(&ctx->ctx, mac, &mdlen);
675 #else /* openssl < 0.9.9 */
676 res = HMAC_Final(&ctx->ctx, mac, &mdlen);
677 #endif /* openssl < 0.9.9 */
678 HMAC_CTX_cleanup(&ctx->ctx);
679 bin_clear_free(ctx, sizeof(*ctx));
690 int pbkdf2_sha1(const char *passphrase, const u8 *ssid, size_t ssid_len,
691 int iterations, u8 *buf, size_t buflen)
693 #if OPENSSL_VERSION_NUMBER < 0x00908000
694 if (PKCS5_PBKDF2_HMAC_SHA1(passphrase, os_strlen(passphrase),
695 (unsigned char *) ssid,
696 ssid_len, iterations, buflen, buf) != 1)
698 #else /* openssl < 0.9.8 */
699 if (PKCS5_PBKDF2_HMAC_SHA1(passphrase, os_strlen(passphrase), ssid,
700 ssid_len, iterations, buflen, buf) != 1)
702 #endif /* openssl < 0.9.8 */
707 int hmac_sha1_vector(const u8 *key, size_t key_len, size_t num_elem,
708 const u8 *addr[], const size_t *len, u8 *mac)
716 #if OPENSSL_VERSION_NUMBER < 0x00909000
717 HMAC_Init_ex(&ctx, key, key_len, EVP_sha1(), NULL);
718 #else /* openssl < 0.9.9 */
719 if (HMAC_Init_ex(&ctx, key, key_len, EVP_sha1(), NULL) != 1)
721 #endif /* openssl < 0.9.9 */
723 for (i = 0; i < num_elem; i++)
724 HMAC_Update(&ctx, addr[i], len[i]);
727 #if OPENSSL_VERSION_NUMBER < 0x00909000
728 HMAC_Final(&ctx, mac, &mdlen);
730 #else /* openssl < 0.9.9 */
731 res = HMAC_Final(&ctx, mac, &mdlen);
732 #endif /* openssl < 0.9.9 */
733 HMAC_CTX_cleanup(&ctx);
735 return res == 1 ? 0 : -1;
739 int hmac_sha1(const u8 *key, size_t key_len, const u8 *data, size_t data_len,
742 return hmac_sha1_vector(key, key_len, 1, &data, &data_len, mac);
748 int hmac_sha256_vector(const u8 *key, size_t key_len, size_t num_elem,
749 const u8 *addr[], const size_t *len, u8 *mac)
757 #if OPENSSL_VERSION_NUMBER < 0x00909000
758 HMAC_Init_ex(&ctx, key, key_len, EVP_sha256(), NULL);
759 #else /* openssl < 0.9.9 */
760 if (HMAC_Init_ex(&ctx, key, key_len, EVP_sha256(), NULL) != 1)
762 #endif /* openssl < 0.9.9 */
764 for (i = 0; i < num_elem; i++)
765 HMAC_Update(&ctx, addr[i], len[i]);
768 #if OPENSSL_VERSION_NUMBER < 0x00909000
769 HMAC_Final(&ctx, mac, &mdlen);
771 #else /* openssl < 0.9.9 */
772 res = HMAC_Final(&ctx, mac, &mdlen);
773 #endif /* openssl < 0.9.9 */
774 HMAC_CTX_cleanup(&ctx);
776 return res == 1 ? 0 : -1;
780 int hmac_sha256(const u8 *key, size_t key_len, const u8 *data,
781 size_t data_len, u8 *mac)
783 return hmac_sha256_vector(key, key_len, 1, &data, &data_len, mac);
786 #endif /* CONFIG_SHA256 */
789 int crypto_get_random(void *buf, size_t len)
791 if (RAND_bytes(buf, len) != 1)
797 #ifdef CONFIG_OPENSSL_CMAC
798 int omac1_aes_vector(const u8 *key, size_t key_len, size_t num_elem,
799 const u8 *addr[], const size_t *len, u8 *mac)
805 ctx = CMAC_CTX_new();
810 if (!CMAC_Init(ctx, key, 32, EVP_aes_256_cbc(), NULL))
812 } else if (key_len == 16) {
813 if (!CMAC_Init(ctx, key, 16, EVP_aes_128_cbc(), NULL))
818 for (i = 0; i < num_elem; i++) {
819 if (!CMAC_Update(ctx, addr[i], len[i]))
822 if (!CMAC_Final(ctx, mac, &outlen) || outlen != 16)
832 int omac1_aes_128_vector(const u8 *key, size_t num_elem,
833 const u8 *addr[], const size_t *len, u8 *mac)
835 return omac1_aes_vector(key, 16, num_elem, addr, len, mac);
839 int omac1_aes_128(const u8 *key, const u8 *data, size_t data_len, u8 *mac)
841 return omac1_aes_128_vector(key, 1, &data, &data_len, mac);
845 int omac1_aes_256(const u8 *key, const u8 *data, size_t data_len, u8 *mac)
847 return omac1_aes_vector(key, 32, 1, &data, &data_len, mac);
849 #endif /* CONFIG_OPENSSL_CMAC */
852 struct crypto_bignum * crypto_bignum_init(void)
854 return (struct crypto_bignum *) BN_new();
858 struct crypto_bignum * crypto_bignum_init_set(const u8 *buf, size_t len)
860 BIGNUM *bn = BN_bin2bn(buf, len, NULL);
861 return (struct crypto_bignum *) bn;
865 void crypto_bignum_deinit(struct crypto_bignum *n, int clear)
868 BN_clear_free((BIGNUM *) n);
870 BN_free((BIGNUM *) n);
874 int crypto_bignum_to_bin(const struct crypto_bignum *a,
875 u8 *buf, size_t buflen, size_t padlen)
877 int num_bytes, offset;
882 num_bytes = BN_num_bytes((const BIGNUM *) a);
883 if ((size_t) num_bytes > buflen)
885 if (padlen > (size_t) num_bytes)
886 offset = padlen - num_bytes;
890 os_memset(buf, 0, offset);
891 BN_bn2bin((const BIGNUM *) a, buf + offset);
893 return num_bytes + offset;
897 int crypto_bignum_add(const struct crypto_bignum *a,
898 const struct crypto_bignum *b,
899 struct crypto_bignum *c)
901 return BN_add((BIGNUM *) c, (const BIGNUM *) a, (const BIGNUM *) b) ?
906 int crypto_bignum_mod(const struct crypto_bignum *a,
907 const struct crypto_bignum *b,
908 struct crypto_bignum *c)
913 bnctx = BN_CTX_new();
916 res = BN_mod((BIGNUM *) c, (const BIGNUM *) a, (const BIGNUM *) b,
924 int crypto_bignum_exptmod(const struct crypto_bignum *a,
925 const struct crypto_bignum *b,
926 const struct crypto_bignum *c,
927 struct crypto_bignum *d)
932 bnctx = BN_CTX_new();
935 res = BN_mod_exp((BIGNUM *) d, (const BIGNUM *) a, (const BIGNUM *) b,
936 (const BIGNUM *) c, bnctx);
943 int crypto_bignum_inverse(const struct crypto_bignum *a,
944 const struct crypto_bignum *b,
945 struct crypto_bignum *c)
950 bnctx = BN_CTX_new();
953 res = BN_mod_inverse((BIGNUM *) c, (const BIGNUM *) a,
954 (const BIGNUM *) b, bnctx);
961 int crypto_bignum_sub(const struct crypto_bignum *a,
962 const struct crypto_bignum *b,
963 struct crypto_bignum *c)
965 return BN_sub((BIGNUM *) c, (const BIGNUM *) a, (const BIGNUM *) b) ?
970 int crypto_bignum_div(const struct crypto_bignum *a,
971 const struct crypto_bignum *b,
972 struct crypto_bignum *c)
978 bnctx = BN_CTX_new();
981 res = BN_div((BIGNUM *) c, NULL, (const BIGNUM *) a,
982 (const BIGNUM *) b, bnctx);
989 int crypto_bignum_mulmod(const struct crypto_bignum *a,
990 const struct crypto_bignum *b,
991 const struct crypto_bignum *c,
992 struct crypto_bignum *d)
998 bnctx = BN_CTX_new();
1001 res = BN_mod_mul((BIGNUM *) d, (const BIGNUM *) a, (const BIGNUM *) b,
1002 (const BIGNUM *) c, bnctx);
1005 return res ? 0 : -1;
1009 int crypto_bignum_cmp(const struct crypto_bignum *a,
1010 const struct crypto_bignum *b)
1012 return BN_cmp((const BIGNUM *) a, (const BIGNUM *) b);
1016 int crypto_bignum_bits(const struct crypto_bignum *a)
1018 return BN_num_bits((const BIGNUM *) a);
1022 int crypto_bignum_is_zero(const struct crypto_bignum *a)
1024 return BN_is_zero((const BIGNUM *) a);
1028 int crypto_bignum_is_one(const struct crypto_bignum *a)
1030 return BN_is_one((const BIGNUM *) a);
1043 struct crypto_ec * crypto_ec_init(int group)
1045 struct crypto_ec *e;
1048 /* Map from IANA registry for IKE D-H groups to OpenSSL NID */
1051 nid = NID_X9_62_prime256v1;
1054 nid = NID_secp384r1;
1057 nid = NID_secp521r1;
1060 nid = NID_X9_62_prime192v1;
1063 nid = NID_secp224r1;
1069 e = os_zalloc(sizeof(*e));
1073 e->bnctx = BN_CTX_new();
1074 e->group = EC_GROUP_new_by_curve_name(nid);
1075 e->prime = BN_new();
1076 e->order = BN_new();
1077 if (e->group == NULL || e->bnctx == NULL || e->prime == NULL ||
1079 !EC_GROUP_get_curve_GFp(e->group, e->prime, NULL, NULL, e->bnctx) ||
1080 !EC_GROUP_get_order(e->group, e->order, e->bnctx)) {
1081 crypto_ec_deinit(e);
1089 void crypto_ec_deinit(struct crypto_ec *e)
1093 BN_clear_free(e->order);
1094 BN_clear_free(e->prime);
1095 EC_GROUP_free(e->group);
1096 BN_CTX_free(e->bnctx);
1101 struct crypto_ec_point * crypto_ec_point_init(struct crypto_ec *e)
1105 return (struct crypto_ec_point *) EC_POINT_new(e->group);
1109 size_t crypto_ec_prime_len(struct crypto_ec *e)
1111 return BN_num_bytes(e->prime);
1115 size_t crypto_ec_prime_len_bits(struct crypto_ec *e)
1117 return BN_num_bits(e->prime);
1121 const struct crypto_bignum * crypto_ec_get_prime(struct crypto_ec *e)
1123 return (const struct crypto_bignum *) e->prime;
1127 const struct crypto_bignum * crypto_ec_get_order(struct crypto_ec *e)
1129 return (const struct crypto_bignum *) e->order;
1133 void crypto_ec_point_deinit(struct crypto_ec_point *p, int clear)
1136 EC_POINT_clear_free((EC_POINT *) p);
1138 EC_POINT_free((EC_POINT *) p);
1142 int crypto_ec_point_to_bin(struct crypto_ec *e,
1143 const struct crypto_ec_point *point, u8 *x, u8 *y)
1145 BIGNUM *x_bn, *y_bn;
1147 int len = BN_num_bytes(e->prime);
1153 EC_POINT_get_affine_coordinates_GFp(e->group, (EC_POINT *) point,
1154 x_bn, y_bn, e->bnctx)) {
1156 crypto_bignum_to_bin((struct crypto_bignum *) x_bn,
1160 crypto_bignum_to_bin((struct crypto_bignum *) y_bn,
1166 BN_clear_free(x_bn);
1167 BN_clear_free(y_bn);
1172 struct crypto_ec_point * crypto_ec_point_from_bin(struct crypto_ec *e,
1177 int len = BN_num_bytes(e->prime);
1179 x = BN_bin2bn(val, len, NULL);
1180 y = BN_bin2bn(val + len, len, NULL);
1181 elem = EC_POINT_new(e->group);
1182 if (x == NULL || y == NULL || elem == NULL) {
1185 EC_POINT_clear_free(elem);
1189 if (!EC_POINT_set_affine_coordinates_GFp(e->group, elem, x, y,
1191 EC_POINT_clear_free(elem);
1198 return (struct crypto_ec_point *) elem;
1202 int crypto_ec_point_add(struct crypto_ec *e, const struct crypto_ec_point *a,
1203 const struct crypto_ec_point *b,
1204 struct crypto_ec_point *c)
1206 return EC_POINT_add(e->group, (EC_POINT *) c, (const EC_POINT *) a,
1207 (const EC_POINT *) b, e->bnctx) ? 0 : -1;
1211 int crypto_ec_point_mul(struct crypto_ec *e, const struct crypto_ec_point *p,
1212 const struct crypto_bignum *b,
1213 struct crypto_ec_point *res)
1215 return EC_POINT_mul(e->group, (EC_POINT *) res, NULL,
1216 (const EC_POINT *) p, (const BIGNUM *) b, e->bnctx)
1221 int crypto_ec_point_invert(struct crypto_ec *e, struct crypto_ec_point *p)
1223 return EC_POINT_invert(e->group, (EC_POINT *) p, e->bnctx) ? 0 : -1;
1227 int crypto_ec_point_solve_y_coord(struct crypto_ec *e,
1228 struct crypto_ec_point *p,
1229 const struct crypto_bignum *x, int y_bit)
1231 if (!EC_POINT_set_compressed_coordinates_GFp(e->group, (EC_POINT *) p,
1232 (const BIGNUM *) x, y_bit,
1234 !EC_POINT_is_on_curve(e->group, (EC_POINT *) p, e->bnctx))
1240 int crypto_ec_point_is_at_infinity(struct crypto_ec *e,
1241 const struct crypto_ec_point *p)
1243 return EC_POINT_is_at_infinity(e->group, (const EC_POINT *) p);
1247 int crypto_ec_point_is_on_curve(struct crypto_ec *e,
1248 const struct crypto_ec_point *p)
1250 return EC_POINT_is_on_curve(e->group, (const EC_POINT *) p, e->bnctx);
1253 #endif /* CONFIG_ECC */