2 * Wrapper functions for OpenSSL libcrypto
3 * Copyright (c) 2004-2017, 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 */
27 #include "utils/const_time.h"
29 #include "dh_group5.h"
38 #if OPENSSL_VERSION_NUMBER < 0x10100000L || \
39 (defined(LIBRESSL_VERSION_NUMBER) && \
40 LIBRESSL_VERSION_NUMBER < 0x20700000L)
41 /* Compatibility wrappers for older versions. */
43 static HMAC_CTX * HMAC_CTX_new(void)
47 ctx = os_zalloc(sizeof(*ctx));
54 static void HMAC_CTX_free(HMAC_CTX *ctx)
58 HMAC_CTX_cleanup(ctx);
59 bin_clear_free(ctx, sizeof(*ctx));
63 static EVP_MD_CTX * EVP_MD_CTX_new(void)
67 ctx = os_zalloc(sizeof(*ctx));
74 static void EVP_MD_CTX_free(EVP_MD_CTX *ctx)
78 EVP_MD_CTX_cleanup(ctx);
79 bin_clear_free(ctx, sizeof(*ctx));
82 #endif /* OpenSSL version < 1.1.0 */
84 static BIGNUM * get_group5_prime(void)
86 #if OPENSSL_VERSION_NUMBER >= 0x10100000L && \
87 !(defined(LIBRESSL_VERSION_NUMBER) && \
88 LIBRESSL_VERSION_NUMBER < 0x20700000L)
89 return BN_get_rfc3526_prime_1536(NULL);
90 #elif !defined(OPENSSL_IS_BORINGSSL)
91 return get_rfc3526_prime_1536(NULL);
93 static const unsigned char RFC3526_PRIME_1536[] = {
94 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xC9,0x0F,0xDA,0xA2,
95 0x21,0x68,0xC2,0x34,0xC4,0xC6,0x62,0x8B,0x80,0xDC,0x1C,0xD1,
96 0x29,0x02,0x4E,0x08,0x8A,0x67,0xCC,0x74,0x02,0x0B,0xBE,0xA6,
97 0x3B,0x13,0x9B,0x22,0x51,0x4A,0x08,0x79,0x8E,0x34,0x04,0xDD,
98 0xEF,0x95,0x19,0xB3,0xCD,0x3A,0x43,0x1B,0x30,0x2B,0x0A,0x6D,
99 0xF2,0x5F,0x14,0x37,0x4F,0xE1,0x35,0x6D,0x6D,0x51,0xC2,0x45,
100 0xE4,0x85,0xB5,0x76,0x62,0x5E,0x7E,0xC6,0xF4,0x4C,0x42,0xE9,
101 0xA6,0x37,0xED,0x6B,0x0B,0xFF,0x5C,0xB6,0xF4,0x06,0xB7,0xED,
102 0xEE,0x38,0x6B,0xFB,0x5A,0x89,0x9F,0xA5,0xAE,0x9F,0x24,0x11,
103 0x7C,0x4B,0x1F,0xE6,0x49,0x28,0x66,0x51,0xEC,0xE4,0x5B,0x3D,
104 0xC2,0x00,0x7C,0xB8,0xA1,0x63,0xBF,0x05,0x98,0xDA,0x48,0x36,
105 0x1C,0x55,0xD3,0x9A,0x69,0x16,0x3F,0xA8,0xFD,0x24,0xCF,0x5F,
106 0x83,0x65,0x5D,0x23,0xDC,0xA3,0xAD,0x96,0x1C,0x62,0xF3,0x56,
107 0x20,0x85,0x52,0xBB,0x9E,0xD5,0x29,0x07,0x70,0x96,0x96,0x6D,
108 0x67,0x0C,0x35,0x4E,0x4A,0xBC,0x98,0x04,0xF1,0x74,0x6C,0x08,
109 0xCA,0x23,0x73,0x27,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
111 return BN_bin2bn(RFC3526_PRIME_1536, sizeof(RFC3526_PRIME_1536), NULL);
116 static BIGNUM * get_group5_order(void)
118 static const unsigned char RFC3526_ORDER_1536[] = {
119 0x7F,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xE4,0x87,0xED,0x51,
120 0x10,0xB4,0x61,0x1A,0x62,0x63,0x31,0x45,0xC0,0x6E,0x0E,0x68,
121 0x94,0x81,0x27,0x04,0x45,0x33,0xE6,0x3A,0x01,0x05,0xDF,0x53,
122 0x1D,0x89,0xCD,0x91,0x28,0xA5,0x04,0x3C,0xC7,0x1A,0x02,0x6E,
123 0xF7,0xCA,0x8C,0xD9,0xE6,0x9D,0x21,0x8D,0x98,0x15,0x85,0x36,
124 0xF9,0x2F,0x8A,0x1B,0xA7,0xF0,0x9A,0xB6,0xB6,0xA8,0xE1,0x22,
125 0xF2,0x42,0xDA,0xBB,0x31,0x2F,0x3F,0x63,0x7A,0x26,0x21,0x74,
126 0xD3,0x1B,0xF6,0xB5,0x85,0xFF,0xAE,0x5B,0x7A,0x03,0x5B,0xF6,
127 0xF7,0x1C,0x35,0xFD,0xAD,0x44,0xCF,0xD2,0xD7,0x4F,0x92,0x08,
128 0xBE,0x25,0x8F,0xF3,0x24,0x94,0x33,0x28,0xF6,0x72,0x2D,0x9E,
129 0xE1,0x00,0x3E,0x5C,0x50,0xB1,0xDF,0x82,0xCC,0x6D,0x24,0x1B,
130 0x0E,0x2A,0xE9,0xCD,0x34,0x8B,0x1F,0xD4,0x7E,0x92,0x67,0xAF,
131 0xC1,0xB2,0xAE,0x91,0xEE,0x51,0xD6,0xCB,0x0E,0x31,0x79,0xAB,
132 0x10,0x42,0xA9,0x5D,0xCF,0x6A,0x94,0x83,0xB8,0x4B,0x4B,0x36,
133 0xB3,0x86,0x1A,0xA7,0x25,0x5E,0x4C,0x02,0x78,0xBA,0x36,0x04,
134 0x65,0x11,0xB9,0x93,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
136 return BN_bin2bn(RFC3526_ORDER_1536, sizeof(RFC3526_ORDER_1536), NULL);
140 #ifdef OPENSSL_NO_SHA256
141 #define NO_SHA256_WRAPPER
143 #ifdef OPENSSL_NO_SHA512
144 #define NO_SHA384_WRAPPER
147 static int openssl_digest_vector(const EVP_MD *type, size_t num_elem,
148 const u8 *addr[], const size_t *len, u8 *mac)
152 unsigned int mac_len;
157 ctx = EVP_MD_CTX_new();
160 if (!EVP_DigestInit_ex(ctx, type, NULL)) {
161 wpa_printf(MSG_ERROR, "OpenSSL: EVP_DigestInit_ex failed: %s",
162 ERR_error_string(ERR_get_error(), NULL));
163 EVP_MD_CTX_free(ctx);
166 for (i = 0; i < num_elem; i++) {
167 if (!EVP_DigestUpdate(ctx, addr[i], len[i])) {
168 wpa_printf(MSG_ERROR, "OpenSSL: EVP_DigestUpdate "
170 ERR_error_string(ERR_get_error(), NULL));
171 EVP_MD_CTX_free(ctx);
175 if (!EVP_DigestFinal(ctx, mac, &mac_len)) {
176 wpa_printf(MSG_ERROR, "OpenSSL: EVP_DigestFinal failed: %s",
177 ERR_error_string(ERR_get_error(), NULL));
178 EVP_MD_CTX_free(ctx);
181 EVP_MD_CTX_free(ctx);
188 int md4_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
190 return openssl_digest_vector(EVP_md4(), num_elem, addr, len, mac);
192 #endif /* CONFIG_FIPS */
195 int des_encrypt(const u8 *clear, const u8 *key, u8 *cypher)
197 u8 pkey[8], next, tmp;
201 /* Add parity bits to the key */
203 for (i = 0; i < 7; i++) {
205 pkey[i] = (tmp >> i) | next | 1;
206 next = tmp << (7 - i);
210 DES_set_key((DES_cblock *) &pkey, &ks);
211 DES_ecb_encrypt((DES_cblock *) clear, (DES_cblock *) cypher, &ks,
217 #ifndef CONFIG_NO_RC4
218 int rc4_skip(const u8 *key, size_t keylen, size_t skip,
219 u8 *data, size_t data_len)
221 #ifdef OPENSSL_NO_RC4
223 #else /* OPENSSL_NO_RC4 */
227 unsigned char skip_buf[16];
229 ctx = EVP_CIPHER_CTX_new();
231 !EVP_CIPHER_CTX_set_padding(ctx, 0) ||
232 !EVP_CipherInit_ex(ctx, EVP_rc4(), NULL, NULL, NULL, 1) ||
233 !EVP_CIPHER_CTX_set_key_length(ctx, keylen) ||
234 !EVP_CipherInit_ex(ctx, NULL, NULL, key, NULL, 1))
237 while (skip >= sizeof(skip_buf)) {
239 if (len > sizeof(skip_buf))
240 len = sizeof(skip_buf);
241 if (!EVP_CipherUpdate(ctx, skip_buf, &outl, skip_buf, len))
246 if (EVP_CipherUpdate(ctx, data, &outl, data, data_len))
251 EVP_CIPHER_CTX_free(ctx);
253 #endif /* OPENSSL_NO_RC4 */
255 #endif /* CONFIG_NO_RC4 */
259 int md5_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
261 return openssl_digest_vector(EVP_md5(), num_elem, addr, len, mac);
263 #endif /* CONFIG_FIPS */
266 int sha1_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
268 return openssl_digest_vector(EVP_sha1(), num_elem, addr, len, mac);
272 #ifndef NO_SHA256_WRAPPER
273 int sha256_vector(size_t num_elem, const u8 *addr[], const size_t *len,
276 return openssl_digest_vector(EVP_sha256(), num_elem, addr, len, mac);
278 #endif /* NO_SHA256_WRAPPER */
281 #ifndef NO_SHA384_WRAPPER
282 int sha384_vector(size_t num_elem, const u8 *addr[], const size_t *len,
285 return openssl_digest_vector(EVP_sha384(), num_elem, addr, len, mac);
287 #endif /* NO_SHA384_WRAPPER */
290 #ifndef NO_SHA512_WRAPPER
291 int sha512_vector(size_t num_elem, const u8 *addr[], const size_t *len,
294 return openssl_digest_vector(EVP_sha512(), num_elem, addr, len, mac);
296 #endif /* NO_SHA512_WRAPPER */
299 static const EVP_CIPHER * aes_get_evp_cipher(size_t keylen)
303 return EVP_aes_128_ecb();
305 return EVP_aes_192_ecb();
307 return EVP_aes_256_ecb();
314 void * aes_encrypt_init(const u8 *key, size_t len)
317 const EVP_CIPHER *type;
322 type = aes_get_evp_cipher(len);
324 wpa_printf(MSG_INFO, "%s: Unsupported len=%u",
325 __func__, (unsigned int) len);
329 ctx = EVP_CIPHER_CTX_new();
332 if (EVP_EncryptInit_ex(ctx, type, NULL, key, NULL) != 1) {
336 EVP_CIPHER_CTX_set_padding(ctx, 0);
341 int aes_encrypt(void *ctx, const u8 *plain, u8 *crypt)
343 EVP_CIPHER_CTX *c = ctx;
345 if (EVP_EncryptUpdate(c, crypt, &clen, plain, 16) != 1) {
346 wpa_printf(MSG_ERROR, "OpenSSL: EVP_EncryptUpdate failed: %s",
347 ERR_error_string(ERR_get_error(), NULL));
354 void aes_encrypt_deinit(void *ctx)
356 EVP_CIPHER_CTX *c = ctx;
358 int len = sizeof(buf);
359 if (EVP_EncryptFinal_ex(c, buf, &len) != 1) {
360 wpa_printf(MSG_ERROR, "OpenSSL: EVP_EncryptFinal_ex failed: "
361 "%s", ERR_error_string(ERR_get_error(), NULL));
364 wpa_printf(MSG_ERROR, "OpenSSL: Unexpected padding length %d "
365 "in AES encrypt", len);
367 EVP_CIPHER_CTX_free(c);
371 void * aes_decrypt_init(const u8 *key, size_t len)
374 const EVP_CIPHER *type;
379 type = aes_get_evp_cipher(len);
381 wpa_printf(MSG_INFO, "%s: Unsupported len=%u",
382 __func__, (unsigned int) len);
386 ctx = EVP_CIPHER_CTX_new();
389 if (EVP_DecryptInit_ex(ctx, type, NULL, key, NULL) != 1) {
390 EVP_CIPHER_CTX_free(ctx);
393 EVP_CIPHER_CTX_set_padding(ctx, 0);
398 int aes_decrypt(void *ctx, const u8 *crypt, u8 *plain)
400 EVP_CIPHER_CTX *c = ctx;
402 if (EVP_DecryptUpdate(c, plain, &plen, crypt, 16) != 1) {
403 wpa_printf(MSG_ERROR, "OpenSSL: EVP_DecryptUpdate failed: %s",
404 ERR_error_string(ERR_get_error(), NULL));
411 void aes_decrypt_deinit(void *ctx)
413 EVP_CIPHER_CTX *c = ctx;
415 int len = sizeof(buf);
416 if (EVP_DecryptFinal_ex(c, buf, &len) != 1) {
417 wpa_printf(MSG_ERROR, "OpenSSL: EVP_DecryptFinal_ex failed: "
418 "%s", ERR_error_string(ERR_get_error(), NULL));
421 wpa_printf(MSG_ERROR, "OpenSSL: Unexpected padding length %d "
422 "in AES decrypt", len);
424 EVP_CIPHER_CTX_free(c);
429 #ifndef CONFIG_OPENSSL_INTERNAL_AES_WRAP
431 int aes_wrap(const u8 *kek, size_t kek_len, int n, const u8 *plain, u8 *cipher)
438 if (AES_set_encrypt_key(kek, kek_len << 3, &actx))
440 res = AES_wrap_key(&actx, NULL, cipher, plain, n * 8);
441 OPENSSL_cleanse(&actx, sizeof(actx));
442 return res <= 0 ? -1 : 0;
446 int aes_unwrap(const u8 *kek, size_t kek_len, int n, const u8 *cipher,
454 if (AES_set_decrypt_key(kek, kek_len << 3, &actx))
456 res = AES_unwrap_key(&actx, NULL, plain, cipher, (n + 1) * 8);
457 OPENSSL_cleanse(&actx, sizeof(actx));
458 return res <= 0 ? -1 : 0;
461 #endif /* CONFIG_OPENSSL_INTERNAL_AES_WRAP */
462 #endif /* CONFIG_FIPS */
465 int aes_128_cbc_encrypt(const u8 *key, const u8 *iv, u8 *data, size_t data_len)
475 ctx = EVP_CIPHER_CTX_new();
480 if (EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv) == 1 &&
481 EVP_CIPHER_CTX_set_padding(ctx, 0) == 1 &&
482 EVP_EncryptUpdate(ctx, data, &clen, data, data_len) == 1 &&
483 clen == (int) data_len &&
484 EVP_EncryptFinal_ex(ctx, buf, &len) == 1 && len == 0)
486 EVP_CIPHER_CTX_free(ctx);
492 int aes_128_cbc_decrypt(const u8 *key, const u8 *iv, u8 *data, size_t data_len)
502 ctx = EVP_CIPHER_CTX_new();
507 if (EVP_DecryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv) == 1 &&
508 EVP_CIPHER_CTX_set_padding(ctx, 0) == 1 &&
509 EVP_DecryptUpdate(ctx, data, &plen, data, data_len) == 1 &&
510 plen == (int) data_len &&
511 EVP_DecryptFinal_ex(ctx, buf, &len) == 1 && len == 0)
513 EVP_CIPHER_CTX_free(ctx);
520 int crypto_dh_init(u8 generator, const u8 *prime, size_t prime_len, u8 *privkey,
523 size_t pubkey_len, pad;
525 if (os_get_random(privkey, prime_len) < 0)
527 if (os_memcmp(privkey, prime, prime_len) > 0) {
528 /* Make sure private value is smaller than prime */
532 pubkey_len = prime_len;
533 if (crypto_mod_exp(&generator, 1, privkey, prime_len, prime, prime_len,
534 pubkey, &pubkey_len) < 0)
536 if (pubkey_len < prime_len) {
537 pad = prime_len - pubkey_len;
538 os_memmove(pubkey + pad, pubkey, pubkey_len);
539 os_memset(pubkey, 0, pad);
546 int crypto_dh_derive_secret(u8 generator, const u8 *prime, size_t prime_len,
547 const u8 *order, size_t order_len,
548 const u8 *privkey, size_t privkey_len,
549 const u8 *pubkey, size_t pubkey_len,
550 u8 *secret, size_t *len)
555 pub = BN_bin2bn(pubkey, pubkey_len, NULL);
556 p = BN_bin2bn(prime, prime_len, NULL);
557 if (!pub || !p || BN_is_zero(pub) || BN_is_one(pub) ||
566 /* verify: pubkey^q == 1 mod p */
567 q = BN_bin2bn(order, order_len, NULL);
570 failed = !q || !ctx || !tmp ||
571 !BN_mod_exp(tmp, pub, q, p, ctx) ||
580 res = crypto_mod_exp(pubkey, pubkey_len, privkey, privkey_len,
581 prime, prime_len, secret, len);
589 int crypto_mod_exp(const u8 *base, size_t base_len,
590 const u8 *power, size_t power_len,
591 const u8 *modulus, size_t modulus_len,
592 u8 *result, size_t *result_len)
594 BIGNUM *bn_base, *bn_exp, *bn_modulus, *bn_result;
602 bn_base = BN_bin2bn(base, base_len, NULL);
603 bn_exp = BN_bin2bn(power, power_len, NULL);
604 bn_modulus = BN_bin2bn(modulus, modulus_len, NULL);
605 bn_result = BN_new();
607 if (bn_base == NULL || bn_exp == NULL || bn_modulus == NULL ||
611 if (BN_mod_exp_mont_consttime(bn_result, bn_base, bn_exp, bn_modulus,
615 *result_len = BN_bn2bin(bn_result, result);
619 BN_clear_free(bn_base);
620 BN_clear_free(bn_exp);
621 BN_clear_free(bn_modulus);
622 BN_clear_free(bn_result);
628 struct crypto_cipher {
634 struct crypto_cipher * crypto_cipher_init(enum crypto_cipher_alg alg,
635 const u8 *iv, const u8 *key,
638 struct crypto_cipher *ctx;
639 const EVP_CIPHER *cipher;
641 ctx = os_zalloc(sizeof(*ctx));
646 #ifndef CONFIG_NO_RC4
647 #ifndef OPENSSL_NO_RC4
648 case CRYPTO_CIPHER_ALG_RC4:
651 #endif /* OPENSSL_NO_RC4 */
652 #endif /* CONFIG_NO_RC4 */
653 #ifndef OPENSSL_NO_AES
654 case CRYPTO_CIPHER_ALG_AES:
657 cipher = EVP_aes_128_cbc();
659 #ifndef OPENSSL_IS_BORINGSSL
661 cipher = EVP_aes_192_cbc();
663 #endif /* OPENSSL_IS_BORINGSSL */
665 cipher = EVP_aes_256_cbc();
672 #endif /* OPENSSL_NO_AES */
673 #ifndef OPENSSL_NO_DES
674 case CRYPTO_CIPHER_ALG_3DES:
675 cipher = EVP_des_ede3_cbc();
677 case CRYPTO_CIPHER_ALG_DES:
678 cipher = EVP_des_cbc();
680 #endif /* OPENSSL_NO_DES */
681 #ifndef OPENSSL_NO_RC2
682 case CRYPTO_CIPHER_ALG_RC2:
683 cipher = EVP_rc2_ecb();
685 #endif /* OPENSSL_NO_RC2 */
691 if (!(ctx->enc = EVP_CIPHER_CTX_new()) ||
692 !EVP_CIPHER_CTX_set_padding(ctx->enc, 0) ||
693 !EVP_EncryptInit_ex(ctx->enc, cipher, NULL, NULL, NULL) ||
694 !EVP_CIPHER_CTX_set_key_length(ctx->enc, key_len) ||
695 !EVP_EncryptInit_ex(ctx->enc, NULL, NULL, key, iv)) {
697 EVP_CIPHER_CTX_free(ctx->enc);
702 if (!(ctx->dec = EVP_CIPHER_CTX_new()) ||
703 !EVP_CIPHER_CTX_set_padding(ctx->dec, 0) ||
704 !EVP_DecryptInit_ex(ctx->dec, cipher, NULL, NULL, NULL) ||
705 !EVP_CIPHER_CTX_set_key_length(ctx->dec, key_len) ||
706 !EVP_DecryptInit_ex(ctx->dec, NULL, NULL, key, iv)) {
707 EVP_CIPHER_CTX_free(ctx->enc);
709 EVP_CIPHER_CTX_free(ctx->dec);
718 int crypto_cipher_encrypt(struct crypto_cipher *ctx, const u8 *plain,
719 u8 *crypt, size_t len)
722 if (!EVP_EncryptUpdate(ctx->enc, crypt, &outl, plain, len))
728 int crypto_cipher_decrypt(struct crypto_cipher *ctx, const u8 *crypt,
729 u8 *plain, size_t len)
733 if (!EVP_DecryptUpdate(ctx->dec, plain, &outl, crypt, len))
739 void crypto_cipher_deinit(struct crypto_cipher *ctx)
741 EVP_CIPHER_CTX_free(ctx->enc);
742 EVP_CIPHER_CTX_free(ctx->dec);
747 void * dh5_init(struct wpabuf **priv, struct wpabuf **publ)
749 #if OPENSSL_VERSION_NUMBER < 0x10100000L || \
750 (defined(LIBRESSL_VERSION_NUMBER) && \
751 LIBRESSL_VERSION_NUMBER < 0x20700000L)
753 struct wpabuf *pubkey = NULL, *privkey = NULL;
754 size_t publen, privlen;
765 if (dh->g == NULL || BN_set_word(dh->g, 2) != 1)
768 dh->p = get_group5_prime();
772 dh->q = get_group5_order();
776 if (DH_generate_key(dh) != 1)
779 publen = BN_num_bytes(dh->pub_key);
780 pubkey = wpabuf_alloc(publen);
783 privlen = BN_num_bytes(dh->priv_key);
784 privkey = wpabuf_alloc(privlen);
788 BN_bn2bin(dh->pub_key, wpabuf_put(pubkey, publen));
789 BN_bn2bin(dh->priv_key, wpabuf_put(privkey, privlen));
796 wpabuf_clear_free(pubkey);
797 wpabuf_clear_free(privkey);
802 struct wpabuf *pubkey = NULL, *privkey = NULL;
803 size_t publen, privlen;
805 const BIGNUM *priv_key = NULL, *pub_key = NULL;
816 p = get_group5_prime();
817 q = get_group5_order();
818 if (!g || BN_set_word(g, 2) != 1 || !p || !q ||
819 DH_set0_pqg(dh, p, q, g) != 1)
825 if (DH_generate_key(dh) != 1)
828 DH_get0_key(dh, &pub_key, &priv_key);
829 publen = BN_num_bytes(pub_key);
830 pubkey = wpabuf_alloc(publen);
833 privlen = BN_num_bytes(priv_key);
834 privkey = wpabuf_alloc(privlen);
838 BN_bn2bin(pub_key, wpabuf_put(pubkey, publen));
839 BN_bn2bin(priv_key, wpabuf_put(privkey, privlen));
849 wpabuf_clear_free(pubkey);
850 wpabuf_clear_free(privkey);
857 void * dh5_init_fixed(const struct wpabuf *priv, const struct wpabuf *publ)
859 #if OPENSSL_VERSION_NUMBER < 0x10100000L || \
860 (defined(LIBRESSL_VERSION_NUMBER) && \
861 LIBRESSL_VERSION_NUMBER < 0x20700000L)
869 if (dh->g == NULL || BN_set_word(dh->g, 2) != 1)
872 dh->p = get_group5_prime();
876 dh->priv_key = BN_bin2bn(wpabuf_head(priv), wpabuf_len(priv), NULL);
877 if (dh->priv_key == NULL)
880 dh->pub_key = BN_bin2bn(wpabuf_head(publ), wpabuf_len(publ), NULL);
881 if (dh->pub_key == NULL)
884 if (DH_generate_key(dh) != 1)
894 BIGNUM *p = NULL, *g, *priv_key = NULL, *pub_key = NULL;
901 p = get_group5_prime();
902 if (!g || BN_set_word(g, 2) != 1 || !p ||
903 DH_set0_pqg(dh, p, NULL, g) != 1)
908 priv_key = BN_bin2bn(wpabuf_head(priv), wpabuf_len(priv), NULL);
909 pub_key = BN_bin2bn(wpabuf_head(publ), wpabuf_len(publ), NULL);
910 if (!priv_key || !pub_key || DH_set0_key(dh, pub_key, priv_key) != 1)
915 if (DH_generate_key(dh) != 1)
924 BN_clear_free(priv_key);
931 struct wpabuf * dh5_derive_shared(void *ctx, const struct wpabuf *peer_public,
932 const struct wpabuf *own_private)
935 struct wpabuf *res = NULL;
943 pub_key = BN_bin2bn(wpabuf_head(peer_public), wpabuf_len(peer_public),
949 res = wpabuf_alloc(rlen);
953 keylen = DH_compute_key(wpabuf_mhead(res), pub_key, dh);
956 wpabuf_put(res, keylen);
957 BN_clear_free(pub_key);
962 BN_clear_free(pub_key);
963 wpabuf_clear_free(res);
968 void dh5_free(void *ctx)
983 struct crypto_hash * crypto_hash_init(enum crypto_hash_alg alg, const u8 *key,
986 struct crypto_hash *ctx;
990 #ifndef OPENSSL_NO_MD5
991 case CRYPTO_HASH_ALG_HMAC_MD5:
994 #endif /* OPENSSL_NO_MD5 */
995 #ifndef OPENSSL_NO_SHA
996 case CRYPTO_HASH_ALG_HMAC_SHA1:
999 #endif /* OPENSSL_NO_SHA */
1000 #ifndef OPENSSL_NO_SHA256
1001 #ifdef CONFIG_SHA256
1002 case CRYPTO_HASH_ALG_HMAC_SHA256:
1005 #endif /* CONFIG_SHA256 */
1006 #endif /* OPENSSL_NO_SHA256 */
1011 ctx = os_zalloc(sizeof(*ctx));
1014 ctx->ctx = HMAC_CTX_new();
1020 if (HMAC_Init_ex(ctx->ctx, key, key_len, md, NULL) != 1) {
1021 HMAC_CTX_free(ctx->ctx);
1022 bin_clear_free(ctx, sizeof(*ctx));
1030 void crypto_hash_update(struct crypto_hash *ctx, const u8 *data, size_t len)
1034 HMAC_Update(ctx->ctx, data, len);
1038 int crypto_hash_finish(struct crypto_hash *ctx, u8 *mac, size_t *len)
1046 if (mac == NULL || len == NULL) {
1047 HMAC_CTX_free(ctx->ctx);
1048 bin_clear_free(ctx, sizeof(*ctx));
1053 res = HMAC_Final(ctx->ctx, mac, &mdlen);
1054 HMAC_CTX_free(ctx->ctx);
1055 bin_clear_free(ctx, sizeof(*ctx));
1069 static int openssl_hmac_vector(const EVP_MD *type, const u8 *key,
1070 size_t key_len, size_t num_elem,
1071 const u8 *addr[], const size_t *len, u8 *mac,
1081 ctx = HMAC_CTX_new();
1084 res = HMAC_Init_ex(ctx, key, key_len, type, NULL);
1088 for (i = 0; i < num_elem; i++)
1089 HMAC_Update(ctx, addr[i], len[i]);
1091 res = HMAC_Final(ctx, mac, &mdlen);
1095 return res == 1 ? 0 : -1;
1101 int hmac_md5_vector(const u8 *key, size_t key_len, size_t num_elem,
1102 const u8 *addr[], const size_t *len, u8 *mac)
1104 return openssl_hmac_vector(EVP_md5(), key ,key_len, num_elem, addr, len,
1109 int hmac_md5(const u8 *key, size_t key_len, const u8 *data, size_t data_len,
1112 return hmac_md5_vector(key, key_len, 1, &data, &data_len, mac);
1115 #endif /* CONFIG_FIPS */
1118 int pbkdf2_sha1(const char *passphrase, const u8 *ssid, size_t ssid_len,
1119 int iterations, u8 *buf, size_t buflen)
1121 if (PKCS5_PBKDF2_HMAC_SHA1(passphrase, os_strlen(passphrase), ssid,
1122 ssid_len, iterations, buflen, buf) != 1)
1128 int hmac_sha1_vector(const u8 *key, size_t key_len, size_t num_elem,
1129 const u8 *addr[], const size_t *len, u8 *mac)
1131 return openssl_hmac_vector(EVP_sha1(), key, key_len, num_elem, addr,
1136 int hmac_sha1(const u8 *key, size_t key_len, const u8 *data, size_t data_len,
1139 return hmac_sha1_vector(key, key_len, 1, &data, &data_len, mac);
1143 #ifdef CONFIG_SHA256
1145 int hmac_sha256_vector(const u8 *key, size_t key_len, size_t num_elem,
1146 const u8 *addr[], const size_t *len, u8 *mac)
1148 return openssl_hmac_vector(EVP_sha256(), key, key_len, num_elem, addr,
1153 int hmac_sha256(const u8 *key, size_t key_len, const u8 *data,
1154 size_t data_len, u8 *mac)
1156 return hmac_sha256_vector(key, key_len, 1, &data, &data_len, mac);
1159 #endif /* CONFIG_SHA256 */
1162 #ifdef CONFIG_SHA384
1164 int hmac_sha384_vector(const u8 *key, size_t key_len, size_t num_elem,
1165 const u8 *addr[], const size_t *len, u8 *mac)
1167 return openssl_hmac_vector(EVP_sha384(), key, key_len, num_elem, addr,
1172 int hmac_sha384(const u8 *key, size_t key_len, const u8 *data,
1173 size_t data_len, u8 *mac)
1175 return hmac_sha384_vector(key, key_len, 1, &data, &data_len, mac);
1178 #endif /* CONFIG_SHA384 */
1181 #ifdef CONFIG_SHA512
1183 int hmac_sha512_vector(const u8 *key, size_t key_len, size_t num_elem,
1184 const u8 *addr[], const size_t *len, u8 *mac)
1186 return openssl_hmac_vector(EVP_sha512(), key, key_len, num_elem, addr,
1191 int hmac_sha512(const u8 *key, size_t key_len, const u8 *data,
1192 size_t data_len, u8 *mac)
1194 return hmac_sha512_vector(key, key_len, 1, &data, &data_len, mac);
1197 #endif /* CONFIG_SHA512 */
1200 int crypto_get_random(void *buf, size_t len)
1202 if (RAND_bytes(buf, len) != 1)
1208 #ifdef CONFIG_OPENSSL_CMAC
1209 int omac1_aes_vector(const u8 *key, size_t key_len, size_t num_elem,
1210 const u8 *addr[], const size_t *len, u8 *mac)
1219 ctx = CMAC_CTX_new();
1223 if (key_len == 32) {
1224 if (!CMAC_Init(ctx, key, 32, EVP_aes_256_cbc(), NULL))
1226 } else if (key_len == 16) {
1227 if (!CMAC_Init(ctx, key, 16, EVP_aes_128_cbc(), NULL))
1232 for (i = 0; i < num_elem; i++) {
1233 if (!CMAC_Update(ctx, addr[i], len[i]))
1236 if (!CMAC_Final(ctx, mac, &outlen) || outlen != 16)
1246 int omac1_aes_128_vector(const u8 *key, size_t num_elem,
1247 const u8 *addr[], const size_t *len, u8 *mac)
1249 return omac1_aes_vector(key, 16, num_elem, addr, len, mac);
1253 int omac1_aes_128(const u8 *key, const u8 *data, size_t data_len, u8 *mac)
1255 return omac1_aes_128_vector(key, 1, &data, &data_len, mac);
1259 int omac1_aes_256(const u8 *key, const u8 *data, size_t data_len, u8 *mac)
1261 return omac1_aes_vector(key, 32, 1, &data, &data_len, mac);
1263 #endif /* CONFIG_OPENSSL_CMAC */
1266 struct crypto_bignum * crypto_bignum_init(void)
1270 return (struct crypto_bignum *) BN_new();
1274 struct crypto_bignum * crypto_bignum_init_set(const u8 *buf, size_t len)
1281 bn = BN_bin2bn(buf, len, NULL);
1282 return (struct crypto_bignum *) bn;
1286 void crypto_bignum_deinit(struct crypto_bignum *n, int clear)
1289 BN_clear_free((BIGNUM *) n);
1291 BN_free((BIGNUM *) n);
1295 int crypto_bignum_to_bin(const struct crypto_bignum *a,
1296 u8 *buf, size_t buflen, size_t padlen)
1298 int num_bytes, offset;
1303 if (padlen > buflen)
1307 #ifdef OPENSSL_IS_BORINGSSL
1308 if (BN_bn2bin_padded(buf, padlen, (const BIGNUM *) a) == 0)
1311 #else /* OPENSSL_IS_BORINGSSL */
1312 #if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)
1313 return BN_bn2binpad((const BIGNUM *) a, buf, padlen);
1318 num_bytes = BN_num_bytes((const BIGNUM *) a);
1319 if ((size_t) num_bytes > buflen)
1321 if (padlen > (size_t) num_bytes)
1322 offset = padlen - num_bytes;
1326 os_memset(buf, 0, offset);
1327 BN_bn2bin((const BIGNUM *) a, buf + offset);
1329 return num_bytes + offset;
1333 int crypto_bignum_rand(struct crypto_bignum *r, const struct crypto_bignum *m)
1337 return BN_rand_range((BIGNUM *) r, (const BIGNUM *) m) == 1 ? 0 : -1;
1341 int crypto_bignum_add(const struct crypto_bignum *a,
1342 const struct crypto_bignum *b,
1343 struct crypto_bignum *c)
1345 return BN_add((BIGNUM *) c, (const BIGNUM *) a, (const BIGNUM *) b) ?
1350 int crypto_bignum_mod(const struct crypto_bignum *a,
1351 const struct crypto_bignum *b,
1352 struct crypto_bignum *c)
1357 bnctx = BN_CTX_new();
1360 res = BN_mod((BIGNUM *) c, (const BIGNUM *) a, (const BIGNUM *) b,
1364 return res ? 0 : -1;
1368 int crypto_bignum_exptmod(const struct crypto_bignum *a,
1369 const struct crypto_bignum *b,
1370 const struct crypto_bignum *c,
1371 struct crypto_bignum *d)
1379 bnctx = BN_CTX_new();
1382 res = BN_mod_exp_mont_consttime((BIGNUM *) d, (const BIGNUM *) a,
1383 (const BIGNUM *) b, (const BIGNUM *) c,
1387 return res ? 0 : -1;
1391 int crypto_bignum_inverse(const struct crypto_bignum *a,
1392 const struct crypto_bignum *b,
1393 struct crypto_bignum *c)
1400 bnctx = BN_CTX_new();
1403 #ifdef OPENSSL_IS_BORINGSSL
1404 /* TODO: use BN_mod_inverse_blinded() ? */
1405 #else /* OPENSSL_IS_BORINGSSL */
1406 BN_set_flags((BIGNUM *) a, BN_FLG_CONSTTIME);
1407 #endif /* OPENSSL_IS_BORINGSSL */
1408 res = BN_mod_inverse((BIGNUM *) c, (const BIGNUM *) a,
1409 (const BIGNUM *) b, bnctx);
1412 return res ? 0 : -1;
1416 int crypto_bignum_sub(const struct crypto_bignum *a,
1417 const struct crypto_bignum *b,
1418 struct crypto_bignum *c)
1422 return BN_sub((BIGNUM *) c, (const BIGNUM *) a, (const BIGNUM *) b) ?
1427 int crypto_bignum_div(const struct crypto_bignum *a,
1428 const struct crypto_bignum *b,
1429 struct crypto_bignum *c)
1438 bnctx = BN_CTX_new();
1441 #ifndef OPENSSL_IS_BORINGSSL
1442 BN_set_flags((BIGNUM *) a, BN_FLG_CONSTTIME);
1443 #endif /* OPENSSL_IS_BORINGSSL */
1444 res = BN_div((BIGNUM *) c, NULL, (const BIGNUM *) a,
1445 (const BIGNUM *) b, bnctx);
1448 return res ? 0 : -1;
1452 int crypto_bignum_mulmod(const struct crypto_bignum *a,
1453 const struct crypto_bignum *b,
1454 const struct crypto_bignum *c,
1455 struct crypto_bignum *d)
1464 bnctx = BN_CTX_new();
1467 res = BN_mod_mul((BIGNUM *) d, (const BIGNUM *) a, (const BIGNUM *) b,
1468 (const BIGNUM *) c, bnctx);
1471 return res ? 0 : -1;
1475 int crypto_bignum_rshift(const struct crypto_bignum *a, int n,
1476 struct crypto_bignum *r)
1478 /* Note: BN_rshift() does not modify the first argument even though it
1479 * has not been marked const. */
1480 return BN_rshift((BIGNUM *) a, (BIGNUM *) r, n) == 1 ? 0 : -1;
1484 int crypto_bignum_cmp(const struct crypto_bignum *a,
1485 const struct crypto_bignum *b)
1487 return BN_cmp((const BIGNUM *) a, (const BIGNUM *) b);
1491 int crypto_bignum_is_zero(const struct crypto_bignum *a)
1493 return BN_is_zero((const BIGNUM *) a);
1497 int crypto_bignum_is_one(const struct crypto_bignum *a)
1499 return BN_is_one((const BIGNUM *) a);
1503 int crypto_bignum_is_odd(const struct crypto_bignum *a)
1505 return BN_is_odd((const BIGNUM *) a);
1509 int crypto_bignum_legendre(const struct crypto_bignum *a,
1510 const struct crypto_bignum *p)
1513 BIGNUM *exp = NULL, *tmp = NULL;
1520 bnctx = BN_CTX_new();
1527 /* exp = (p-1) / 2 */
1528 !BN_sub(exp, (const BIGNUM *) p, BN_value_one()) ||
1529 !BN_rshift1(exp, exp) ||
1530 !BN_mod_exp_mont_consttime(tmp, (const BIGNUM *) a, exp,
1531 (const BIGNUM *) p, bnctx, NULL))
1534 /* Return 1 if tmp == 1, 0 if tmp == 0, or -1 otherwise. Need to use
1535 * constant time selection to avoid branches here. */
1537 mask = const_time_eq(BN_is_word(tmp, 1), 1);
1538 res = const_time_select_int(mask, 1, res);
1539 mask = const_time_eq(BN_is_zero(tmp), 1);
1540 res = const_time_select_int(mask, 0, res);
1562 struct crypto_ec * crypto_ec_init(int group)
1564 struct crypto_ec *e;
1567 /* Map from IANA registry for IKE D-H groups to OpenSSL NID */
1570 nid = NID_X9_62_prime256v1;
1573 nid = NID_secp384r1;
1576 nid = NID_secp521r1;
1579 nid = NID_X9_62_prime192v1;
1582 nid = NID_secp224r1;
1584 #ifdef NID_brainpoolP224r1
1586 nid = NID_brainpoolP224r1;
1588 #endif /* NID_brainpoolP224r1 */
1589 #ifdef NID_brainpoolP256r1
1591 nid = NID_brainpoolP256r1;
1593 #endif /* NID_brainpoolP256r1 */
1594 #ifdef NID_brainpoolP384r1
1596 nid = NID_brainpoolP384r1;
1598 #endif /* NID_brainpoolP384r1 */
1599 #ifdef NID_brainpoolP512r1
1601 nid = NID_brainpoolP512r1;
1603 #endif /* NID_brainpoolP512r1 */
1608 e = os_zalloc(sizeof(*e));
1613 e->bnctx = BN_CTX_new();
1614 e->group = EC_GROUP_new_by_curve_name(nid);
1615 e->prime = BN_new();
1616 e->order = BN_new();
1619 if (e->group == NULL || e->bnctx == NULL || e->prime == NULL ||
1620 e->order == NULL || e->a == NULL || e->b == NULL ||
1621 !EC_GROUP_get_curve_GFp(e->group, e->prime, e->a, e->b, e->bnctx) ||
1622 !EC_GROUP_get_order(e->group, e->order, e->bnctx)) {
1623 crypto_ec_deinit(e);
1631 void crypto_ec_deinit(struct crypto_ec *e)
1635 BN_clear_free(e->b);
1636 BN_clear_free(e->a);
1637 BN_clear_free(e->order);
1638 BN_clear_free(e->prime);
1639 EC_GROUP_free(e->group);
1640 BN_CTX_free(e->bnctx);
1645 struct crypto_ec_point * crypto_ec_point_init(struct crypto_ec *e)
1651 return (struct crypto_ec_point *) EC_POINT_new(e->group);
1655 size_t crypto_ec_prime_len(struct crypto_ec *e)
1657 return BN_num_bytes(e->prime);
1661 size_t crypto_ec_prime_len_bits(struct crypto_ec *e)
1663 return BN_num_bits(e->prime);
1667 size_t crypto_ec_order_len(struct crypto_ec *e)
1669 return BN_num_bytes(e->order);
1673 const struct crypto_bignum * crypto_ec_get_prime(struct crypto_ec *e)
1675 return (const struct crypto_bignum *) e->prime;
1679 const struct crypto_bignum * crypto_ec_get_order(struct crypto_ec *e)
1681 return (const struct crypto_bignum *) e->order;
1685 void crypto_ec_point_deinit(struct crypto_ec_point *p, int clear)
1688 EC_POINT_clear_free((EC_POINT *) p);
1690 EC_POINT_free((EC_POINT *) p);
1694 int crypto_ec_point_x(struct crypto_ec *e, const struct crypto_ec_point *p,
1695 struct crypto_bignum *x)
1697 return EC_POINT_get_affine_coordinates_GFp(e->group,
1698 (const EC_POINT *) p,
1700 e->bnctx) == 1 ? 0 : -1;
1704 int crypto_ec_point_to_bin(struct crypto_ec *e,
1705 const struct crypto_ec_point *point, u8 *x, u8 *y)
1707 BIGNUM *x_bn, *y_bn;
1709 int len = BN_num_bytes(e->prime);
1718 EC_POINT_get_affine_coordinates_GFp(e->group, (EC_POINT *) point,
1719 x_bn, y_bn, e->bnctx)) {
1721 crypto_bignum_to_bin((struct crypto_bignum *) x_bn,
1725 crypto_bignum_to_bin((struct crypto_bignum *) y_bn,
1731 BN_clear_free(x_bn);
1732 BN_clear_free(y_bn);
1737 struct crypto_ec_point * crypto_ec_point_from_bin(struct crypto_ec *e,
1742 int len = BN_num_bytes(e->prime);
1747 x = BN_bin2bn(val, len, NULL);
1748 y = BN_bin2bn(val + len, len, NULL);
1749 elem = EC_POINT_new(e->group);
1750 if (x == NULL || y == NULL || elem == NULL) {
1753 EC_POINT_clear_free(elem);
1757 if (!EC_POINT_set_affine_coordinates_GFp(e->group, elem, x, y,
1759 EC_POINT_clear_free(elem);
1766 return (struct crypto_ec_point *) elem;
1770 int crypto_ec_point_add(struct crypto_ec *e, const struct crypto_ec_point *a,
1771 const struct crypto_ec_point *b,
1772 struct crypto_ec_point *c)
1776 return EC_POINT_add(e->group, (EC_POINT *) c, (const EC_POINT *) a,
1777 (const EC_POINT *) b, e->bnctx) ? 0 : -1;
1781 int crypto_ec_point_mul(struct crypto_ec *e, const struct crypto_ec_point *p,
1782 const struct crypto_bignum *b,
1783 struct crypto_ec_point *res)
1787 return EC_POINT_mul(e->group, (EC_POINT *) res, NULL,
1788 (const EC_POINT *) p, (const BIGNUM *) b, e->bnctx)
1793 int crypto_ec_point_invert(struct crypto_ec *e, struct crypto_ec_point *p)
1797 return EC_POINT_invert(e->group, (EC_POINT *) p, e->bnctx) ? 0 : -1;
1801 int crypto_ec_point_solve_y_coord(struct crypto_ec *e,
1802 struct crypto_ec_point *p,
1803 const struct crypto_bignum *x, int y_bit)
1807 if (!EC_POINT_set_compressed_coordinates_GFp(e->group, (EC_POINT *) p,
1808 (const BIGNUM *) x, y_bit,
1810 !EC_POINT_is_on_curve(e->group, (EC_POINT *) p, e->bnctx))
1816 struct crypto_bignum *
1817 crypto_ec_point_compute_y_sqr(struct crypto_ec *e,
1818 const struct crypto_bignum *x)
1820 BIGNUM *tmp, *tmp2, *y_sqr = NULL;
1828 /* y^2 = x^3 + ax + b */
1830 BN_mod_sqr(tmp, (const BIGNUM *) x, e->prime, e->bnctx) &&
1831 BN_mod_mul(tmp, tmp, (const BIGNUM *) x, e->prime, e->bnctx) &&
1832 BN_mod_mul(tmp2, e->a, (const BIGNUM *) x, e->prime, e->bnctx) &&
1833 BN_mod_add_quick(tmp2, tmp2, tmp, e->prime) &&
1834 BN_mod_add_quick(tmp2, tmp2, e->b, e->prime)) {
1840 BN_clear_free(tmp2);
1842 return (struct crypto_bignum *) y_sqr;
1846 int crypto_ec_point_is_at_infinity(struct crypto_ec *e,
1847 const struct crypto_ec_point *p)
1849 return EC_POINT_is_at_infinity(e->group, (const EC_POINT *) p);
1853 int crypto_ec_point_is_on_curve(struct crypto_ec *e,
1854 const struct crypto_ec_point *p)
1856 return EC_POINT_is_on_curve(e->group, (const EC_POINT *) p,
1861 int crypto_ec_point_cmp(const struct crypto_ec *e,
1862 const struct crypto_ec_point *a,
1863 const struct crypto_ec_point *b)
1865 return EC_POINT_cmp(e->group, (const EC_POINT *) a,
1866 (const EC_POINT *) b, e->bnctx);
1870 struct crypto_ecdh {
1871 struct crypto_ec *ec;
1875 struct crypto_ecdh * crypto_ecdh_init(int group)
1877 struct crypto_ecdh *ecdh;
1878 EVP_PKEY *params = NULL;
1879 EC_KEY *ec_params = NULL;
1880 EVP_PKEY_CTX *kctx = NULL;
1882 ecdh = os_zalloc(sizeof(*ecdh));
1886 ecdh->ec = crypto_ec_init(group);
1890 ec_params = EC_KEY_new_by_curve_name(ecdh->ec->nid);
1892 wpa_printf(MSG_ERROR,
1893 "OpenSSL: Failed to generate EC_KEY parameters");
1896 EC_KEY_set_asn1_flag(ec_params, OPENSSL_EC_NAMED_CURVE);
1897 params = EVP_PKEY_new();
1898 if (!params || EVP_PKEY_set1_EC_KEY(params, ec_params) != 1) {
1899 wpa_printf(MSG_ERROR,
1900 "OpenSSL: Failed to generate EVP_PKEY parameters");
1904 kctx = EVP_PKEY_CTX_new(params, NULL);
1908 if (EVP_PKEY_keygen_init(kctx) != 1) {
1909 wpa_printf(MSG_ERROR,
1910 "OpenSSL: EVP_PKEY_keygen_init failed: %s",
1911 ERR_error_string(ERR_get_error(), NULL));
1915 if (EVP_PKEY_keygen(kctx, &ecdh->pkey) != 1) {
1916 wpa_printf(MSG_ERROR, "OpenSSL: EVP_PKEY_keygen failed: %s",
1917 ERR_error_string(ERR_get_error(), NULL));
1922 EC_KEY_free(ec_params);
1923 EVP_PKEY_free(params);
1924 EVP_PKEY_CTX_free(kctx);
1928 crypto_ecdh_deinit(ecdh);
1934 struct wpabuf * crypto_ecdh_get_pubkey(struct crypto_ecdh *ecdh, int inc_y)
1936 struct wpabuf *buf = NULL;
1938 const EC_POINT *pubkey;
1939 BIGNUM *x, *y = NULL;
1940 int len = BN_num_bytes(ecdh->ec->prime);
1943 eckey = EVP_PKEY_get1_EC_KEY(ecdh->pkey);
1947 pubkey = EC_KEY_get0_public_key(eckey);
1957 buf = wpabuf_alloc(inc_y ? 2 * len : len);
1961 if (EC_POINT_get_affine_coordinates_GFp(ecdh->ec->group, pubkey,
1962 x, y, ecdh->ec->bnctx) != 1) {
1963 wpa_printf(MSG_ERROR,
1964 "OpenSSL: EC_POINT_get_affine_coordinates_GFp failed: %s",
1965 ERR_error_string(ERR_get_error(), NULL));
1969 res = crypto_bignum_to_bin((struct crypto_bignum *) x,
1970 wpabuf_put(buf, len), len, len);
1975 res = crypto_bignum_to_bin((struct crypto_bignum *) y,
1976 wpabuf_put(buf, len), len, len);
1994 struct wpabuf * crypto_ecdh_set_peerkey(struct crypto_ecdh *ecdh, int inc_y,
1995 const u8 *key, size_t len)
1997 BIGNUM *x, *y = NULL;
1998 EVP_PKEY_CTX *ctx = NULL;
1999 EVP_PKEY *peerkey = NULL;
2000 struct wpabuf *secret = NULL;
2003 EC_KEY *eckey = NULL;
2005 x = BN_bin2bn(key, inc_y ? len / 2 : len, NULL);
2006 pub = EC_POINT_new(ecdh->ec->group);
2011 y = BN_bin2bn(key + len / 2, len / 2, NULL);
2014 if (!EC_POINT_set_affine_coordinates_GFp(ecdh->ec->group, pub,
2017 wpa_printf(MSG_ERROR,
2018 "OpenSSL: EC_POINT_set_affine_coordinates_GFp failed: %s",
2019 ERR_error_string(ERR_get_error(), NULL));
2022 } else if (!EC_POINT_set_compressed_coordinates_GFp(ecdh->ec->group,
2025 wpa_printf(MSG_ERROR,
2026 "OpenSSL: EC_POINT_set_compressed_coordinates_GFp failed: %s",
2027 ERR_error_string(ERR_get_error(), NULL));
2031 if (!EC_POINT_is_on_curve(ecdh->ec->group, pub, ecdh->ec->bnctx)) {
2032 wpa_printf(MSG_ERROR,
2033 "OpenSSL: ECDH peer public key is not on curve");
2037 eckey = EC_KEY_new_by_curve_name(ecdh->ec->nid);
2038 if (!eckey || EC_KEY_set_public_key(eckey, pub) != 1) {
2039 wpa_printf(MSG_ERROR,
2040 "OpenSSL: EC_KEY_set_public_key failed: %s",
2041 ERR_error_string(ERR_get_error(), NULL));
2045 peerkey = EVP_PKEY_new();
2046 if (!peerkey || EVP_PKEY_set1_EC_KEY(peerkey, eckey) != 1)
2049 ctx = EVP_PKEY_CTX_new(ecdh->pkey, NULL);
2050 if (!ctx || EVP_PKEY_derive_init(ctx) != 1 ||
2051 EVP_PKEY_derive_set_peer(ctx, peerkey) != 1 ||
2052 EVP_PKEY_derive(ctx, NULL, &secret_len) != 1) {
2053 wpa_printf(MSG_ERROR,
2054 "OpenSSL: EVP_PKEY_derive(1) failed: %s",
2055 ERR_error_string(ERR_get_error(), NULL));
2059 secret = wpabuf_alloc(secret_len);
2062 if (EVP_PKEY_derive(ctx, wpabuf_put(secret, 0), &secret_len) != 1) {
2063 wpa_printf(MSG_ERROR,
2064 "OpenSSL: EVP_PKEY_derive(2) failed: %s",
2065 ERR_error_string(ERR_get_error(), NULL));
2068 if (secret->size != secret_len)
2069 wpa_printf(MSG_DEBUG,
2070 "OpenSSL: EVP_PKEY_derive(2) changed secret_len %d -> %d",
2071 (int) secret->size, (int) secret_len);
2072 wpabuf_put(secret, secret_len);
2079 EVP_PKEY_CTX_free(ctx);
2080 EVP_PKEY_free(peerkey);
2083 wpabuf_free(secret);
2089 void crypto_ecdh_deinit(struct crypto_ecdh *ecdh)
2092 crypto_ec_deinit(ecdh->ec);
2093 EVP_PKEY_free(ecdh->pkey);
2098 #endif /* CONFIG_ECC */