LCOV - code coverage report
Current view: top level - crypto - crypto_openssl.c (source / functions) Hit Total Coverage
Test: hostapd hwsim test run 1412854115 Lines: 397 508 78.1 %
Date: 2014-10-09 Functions: 63 64 98.4 %

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

Generated by: LCOV version 1.10