LCOV - code coverage report
Current view: top level - src/crypto - crypto_openssl.c (source / functions) Hit Total Coverage
Test: wpa_supplicant/hostapd combined for hwsim test run 1388338050 Lines: 401 509 78.8 %
Date: 2013-12-29 Functions: 63 65 96.9 %
Branches: 140 260 53.8 %

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