Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1 | /* |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2 | * Wrapper functions for OpenSSL libcrypto |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 3 | * Copyright (c) 2004-2017, Jouni Malinen <j@w1.fi> |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4 | * |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 5 | * This software may be distributed under the terms of the BSD license. |
| 6 | * See README for more details. |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 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> |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 17 | #include <openssl/hmac.h> |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 18 | #include <openssl/rand.h> |
| 19 | #ifdef CONFIG_OPENSSL_CMAC |
| 20 | #include <openssl/cmac.h> |
| 21 | #endif /* CONFIG_OPENSSL_CMAC */ |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 22 | #ifdef CONFIG_ECC |
| 23 | #include <openssl/ec.h> |
| 24 | #endif /* CONFIG_ECC */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 25 | |
| 26 | #include "common.h" |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 27 | #include "utils/const_time.h" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 28 | #include "wpabuf.h" |
| 29 | #include "dh_group5.h" |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 30 | #include "sha1.h" |
| 31 | #include "sha256.h" |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 32 | #include "sha384.h" |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 33 | #include "sha512.h" |
Dmitry Shmidt | 4ae50e6 | 2016-06-27 13:48:39 -0700 | [diff] [blame] | 34 | #include "md5.h" |
| 35 | #include "aes_wrap.h" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 36 | #include "crypto.h" |
| 37 | |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 38 | #if OPENSSL_VERSION_NUMBER < 0x10100000L || \ |
| 39 | (defined(LIBRESSL_VERSION_NUMBER) && \ |
| 40 | LIBRESSL_VERSION_NUMBER < 0x20700000L) |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 41 | /* Compatibility wrappers for older versions. */ |
| 42 | |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 43 | static HMAC_CTX * HMAC_CTX_new(void) |
| 44 | { |
| 45 | HMAC_CTX *ctx; |
| 46 | |
| 47 | ctx = os_zalloc(sizeof(*ctx)); |
| 48 | if (ctx) |
| 49 | HMAC_CTX_init(ctx); |
| 50 | return ctx; |
| 51 | } |
| 52 | |
| 53 | |
| 54 | static void HMAC_CTX_free(HMAC_CTX *ctx) |
| 55 | { |
Dmitry Shmidt | 7f2c753 | 2016-08-15 09:48:12 -0700 | [diff] [blame] | 56 | if (!ctx) |
| 57 | return; |
Dmitry Shmidt | 57c2d39 | 2016-02-23 13:40:19 -0800 | [diff] [blame] | 58 | HMAC_CTX_cleanup(ctx); |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 59 | bin_clear_free(ctx, sizeof(*ctx)); |
| 60 | } |
| 61 | |
| 62 | |
| 63 | static EVP_MD_CTX * EVP_MD_CTX_new(void) |
| 64 | { |
| 65 | EVP_MD_CTX *ctx; |
| 66 | |
| 67 | ctx = os_zalloc(sizeof(*ctx)); |
| 68 | if (ctx) |
| 69 | EVP_MD_CTX_init(ctx); |
| 70 | return ctx; |
| 71 | } |
| 72 | |
| 73 | |
| 74 | static void EVP_MD_CTX_free(EVP_MD_CTX *ctx) |
| 75 | { |
Dmitry Shmidt | 7f2c753 | 2016-08-15 09:48:12 -0700 | [diff] [blame] | 76 | if (!ctx) |
| 77 | return; |
| 78 | EVP_MD_CTX_cleanup(ctx); |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 79 | bin_clear_free(ctx, sizeof(*ctx)); |
| 80 | } |
| 81 | |
| 82 | #endif /* OpenSSL version < 1.1.0 */ |
| 83 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 84 | static BIGNUM * get_group5_prime(void) |
| 85 | { |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 86 | #if OPENSSL_VERSION_NUMBER >= 0x10100000L && \ |
| 87 | !(defined(LIBRESSL_VERSION_NUMBER) && \ |
| 88 | LIBRESSL_VERSION_NUMBER < 0x20700000L) |
Dmitry Shmidt | 7f2c753 | 2016-08-15 09:48:12 -0700 | [diff] [blame] | 89 | return BN_get_rfc3526_prime_1536(NULL); |
| 90 | #elif !defined(OPENSSL_IS_BORINGSSL) |
| 91 | return get_rfc3526_prime_1536(NULL); |
| 92 | #else |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 93 | static const unsigned char RFC3526_PRIME_1536[] = { |
| 94 | 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xC9,0x0F,0xDA,0xA2, |
| 95 | 0x21,0x68,0xC2,0x34,0xC4,0xC6,0x62,0x8B,0x80,0xDC,0x1C,0xD1, |
| 96 | 0x29,0x02,0x4E,0x08,0x8A,0x67,0xCC,0x74,0x02,0x0B,0xBE,0xA6, |
| 97 | 0x3B,0x13,0x9B,0x22,0x51,0x4A,0x08,0x79,0x8E,0x34,0x04,0xDD, |
| 98 | 0xEF,0x95,0x19,0xB3,0xCD,0x3A,0x43,0x1B,0x30,0x2B,0x0A,0x6D, |
| 99 | 0xF2,0x5F,0x14,0x37,0x4F,0xE1,0x35,0x6D,0x6D,0x51,0xC2,0x45, |
| 100 | 0xE4,0x85,0xB5,0x76,0x62,0x5E,0x7E,0xC6,0xF4,0x4C,0x42,0xE9, |
| 101 | 0xA6,0x37,0xED,0x6B,0x0B,0xFF,0x5C,0xB6,0xF4,0x06,0xB7,0xED, |
| 102 | 0xEE,0x38,0x6B,0xFB,0x5A,0x89,0x9F,0xA5,0xAE,0x9F,0x24,0x11, |
| 103 | 0x7C,0x4B,0x1F,0xE6,0x49,0x28,0x66,0x51,0xEC,0xE4,0x5B,0x3D, |
| 104 | 0xC2,0x00,0x7C,0xB8,0xA1,0x63,0xBF,0x05,0x98,0xDA,0x48,0x36, |
| 105 | 0x1C,0x55,0xD3,0x9A,0x69,0x16,0x3F,0xA8,0xFD,0x24,0xCF,0x5F, |
| 106 | 0x83,0x65,0x5D,0x23,0xDC,0xA3,0xAD,0x96,0x1C,0x62,0xF3,0x56, |
| 107 | 0x20,0x85,0x52,0xBB,0x9E,0xD5,0x29,0x07,0x70,0x96,0x96,0x6D, |
| 108 | 0x67,0x0C,0x35,0x4E,0x4A,0xBC,0x98,0x04,0xF1,0x74,0x6C,0x08, |
| 109 | 0xCA,0x23,0x73,0x27,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, |
| 110 | }; |
| 111 | return BN_bin2bn(RFC3526_PRIME_1536, sizeof(RFC3526_PRIME_1536), NULL); |
Dmitry Shmidt | 7f2c753 | 2016-08-15 09:48:12 -0700 | [diff] [blame] | 112 | #endif |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 115 | |
| 116 | static BIGNUM * get_group5_order(void) |
| 117 | { |
| 118 | static const unsigned char RFC3526_ORDER_1536[] = { |
| 119 | 0x7F,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xE4,0x87,0xED,0x51, |
| 120 | 0x10,0xB4,0x61,0x1A,0x62,0x63,0x31,0x45,0xC0,0x6E,0x0E,0x68, |
| 121 | 0x94,0x81,0x27,0x04,0x45,0x33,0xE6,0x3A,0x01,0x05,0xDF,0x53, |
| 122 | 0x1D,0x89,0xCD,0x91,0x28,0xA5,0x04,0x3C,0xC7,0x1A,0x02,0x6E, |
| 123 | 0xF7,0xCA,0x8C,0xD9,0xE6,0x9D,0x21,0x8D,0x98,0x15,0x85,0x36, |
| 124 | 0xF9,0x2F,0x8A,0x1B,0xA7,0xF0,0x9A,0xB6,0xB6,0xA8,0xE1,0x22, |
| 125 | 0xF2,0x42,0xDA,0xBB,0x31,0x2F,0x3F,0x63,0x7A,0x26,0x21,0x74, |
| 126 | 0xD3,0x1B,0xF6,0xB5,0x85,0xFF,0xAE,0x5B,0x7A,0x03,0x5B,0xF6, |
| 127 | 0xF7,0x1C,0x35,0xFD,0xAD,0x44,0xCF,0xD2,0xD7,0x4F,0x92,0x08, |
| 128 | 0xBE,0x25,0x8F,0xF3,0x24,0x94,0x33,0x28,0xF6,0x72,0x2D,0x9E, |
| 129 | 0xE1,0x00,0x3E,0x5C,0x50,0xB1,0xDF,0x82,0xCC,0x6D,0x24,0x1B, |
| 130 | 0x0E,0x2A,0xE9,0xCD,0x34,0x8B,0x1F,0xD4,0x7E,0x92,0x67,0xAF, |
| 131 | 0xC1,0xB2,0xAE,0x91,0xEE,0x51,0xD6,0xCB,0x0E,0x31,0x79,0xAB, |
| 132 | 0x10,0x42,0xA9,0x5D,0xCF,0x6A,0x94,0x83,0xB8,0x4B,0x4B,0x36, |
| 133 | 0xB3,0x86,0x1A,0xA7,0x25,0x5E,0x4C,0x02,0x78,0xBA,0x36,0x04, |
| 134 | 0x65,0x11,0xB9,0x93,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF |
| 135 | }; |
| 136 | return BN_bin2bn(RFC3526_ORDER_1536, sizeof(RFC3526_ORDER_1536), NULL); |
| 137 | } |
| 138 | |
| 139 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 140 | #ifdef OPENSSL_NO_SHA256 |
| 141 | #define NO_SHA256_WRAPPER |
| 142 | #endif |
Paul Stewart | 092955c | 2017-02-06 09:13:09 -0800 | [diff] [blame] | 143 | #ifdef OPENSSL_NO_SHA512 |
| 144 | #define NO_SHA384_WRAPPER |
| 145 | #endif |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 146 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 147 | static int openssl_digest_vector(const EVP_MD *type, size_t num_elem, |
| 148 | const u8 *addr[], const size_t *len, u8 *mac) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 149 | { |
Dmitry Shmidt | 55840ad | 2015-12-14 12:45:46 -0800 | [diff] [blame] | 150 | EVP_MD_CTX *ctx; |
| 151 | size_t i; |
| 152 | unsigned int mac_len; |
| 153 | |
| 154 | if (TEST_FAIL()) |
| 155 | return -1; |
| 156 | |
| 157 | ctx = EVP_MD_CTX_new(); |
| 158 | if (!ctx) |
| 159 | return -1; |
| 160 | if (!EVP_DigestInit_ex(ctx, type, NULL)) { |
| 161 | wpa_printf(MSG_ERROR, "OpenSSL: EVP_DigestInit_ex failed: %s", |
| 162 | ERR_error_string(ERR_get_error(), NULL)); |
| 163 | EVP_MD_CTX_free(ctx); |
| 164 | return -1; |
| 165 | } |
| 166 | for (i = 0; i < num_elem; i++) { |
| 167 | if (!EVP_DigestUpdate(ctx, addr[i], len[i])) { |
| 168 | wpa_printf(MSG_ERROR, "OpenSSL: EVP_DigestUpdate " |
| 169 | "failed: %s", |
| 170 | ERR_error_string(ERR_get_error(), NULL)); |
| 171 | EVP_MD_CTX_free(ctx); |
| 172 | return -1; |
| 173 | } |
| 174 | } |
| 175 | if (!EVP_DigestFinal(ctx, mac, &mac_len)) { |
| 176 | wpa_printf(MSG_ERROR, "OpenSSL: EVP_DigestFinal failed: %s", |
| 177 | ERR_error_string(ERR_get_error(), NULL)); |
| 178 | EVP_MD_CTX_free(ctx); |
| 179 | return -1; |
| 180 | } |
| 181 | EVP_MD_CTX_free(ctx); |
| 182 | |
| 183 | return 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 187 | #ifndef CONFIG_FIPS |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 188 | int md4_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac) |
| 189 | { |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 190 | return openssl_digest_vector(EVP_md4(), num_elem, addr, len, mac); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 191 | } |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 192 | #endif /* CONFIG_FIPS */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 193 | |
| 194 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 195 | int des_encrypt(const u8 *clear, const u8 *key, u8 *cypher) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 196 | { |
| 197 | u8 pkey[8], next, tmp; |
| 198 | int i; |
| 199 | DES_key_schedule ks; |
| 200 | |
| 201 | /* Add parity bits to the key */ |
| 202 | next = 0; |
| 203 | for (i = 0; i < 7; i++) { |
| 204 | tmp = key[i]; |
| 205 | pkey[i] = (tmp >> i) | next | 1; |
| 206 | next = tmp << (7 - i); |
| 207 | } |
| 208 | pkey[i] = next | 1; |
| 209 | |
Dmitry Shmidt | 9ead16e | 2014-10-07 13:15:23 -0700 | [diff] [blame] | 210 | DES_set_key((DES_cblock *) &pkey, &ks); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 211 | DES_ecb_encrypt((DES_cblock *) clear, (DES_cblock *) cypher, &ks, |
| 212 | DES_ENCRYPT); |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 213 | return 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 217 | #ifndef CONFIG_NO_RC4 |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 218 | int rc4_skip(const u8 *key, size_t keylen, size_t skip, |
| 219 | u8 *data, size_t data_len) |
| 220 | { |
| 221 | #ifdef OPENSSL_NO_RC4 |
| 222 | return -1; |
| 223 | #else /* OPENSSL_NO_RC4 */ |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 224 | EVP_CIPHER_CTX *ctx; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 225 | int outl; |
| 226 | int res = -1; |
| 227 | unsigned char skip_buf[16]; |
| 228 | |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 229 | ctx = EVP_CIPHER_CTX_new(); |
| 230 | if (!ctx || |
| 231 | !EVP_CIPHER_CTX_set_padding(ctx, 0) || |
| 232 | !EVP_CipherInit_ex(ctx, EVP_rc4(), NULL, NULL, NULL, 1) || |
| 233 | !EVP_CIPHER_CTX_set_key_length(ctx, keylen) || |
| 234 | !EVP_CipherInit_ex(ctx, NULL, NULL, key, NULL, 1)) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 235 | goto out; |
| 236 | |
| 237 | while (skip >= sizeof(skip_buf)) { |
| 238 | size_t len = skip; |
| 239 | if (len > sizeof(skip_buf)) |
| 240 | len = sizeof(skip_buf); |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 241 | if (!EVP_CipherUpdate(ctx, skip_buf, &outl, skip_buf, len)) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 242 | goto out; |
| 243 | skip -= len; |
| 244 | } |
| 245 | |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 246 | if (EVP_CipherUpdate(ctx, data, &outl, data, data_len)) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 247 | res = 0; |
| 248 | |
| 249 | out: |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 250 | if (ctx) |
Dmitry Shmidt | 57c2d39 | 2016-02-23 13:40:19 -0800 | [diff] [blame] | 251 | EVP_CIPHER_CTX_free(ctx); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 252 | return res; |
| 253 | #endif /* OPENSSL_NO_RC4 */ |
| 254 | } |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 255 | #endif /* CONFIG_NO_RC4 */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 256 | |
| 257 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 258 | #ifndef CONFIG_FIPS |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 259 | int md5_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac) |
| 260 | { |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 261 | return openssl_digest_vector(EVP_md5(), num_elem, addr, len, mac); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 262 | } |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 263 | #endif /* CONFIG_FIPS */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 264 | |
| 265 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 266 | int sha1_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac) |
| 267 | { |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 268 | return openssl_digest_vector(EVP_sha1(), num_elem, addr, len, mac); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | |
| 272 | #ifndef NO_SHA256_WRAPPER |
| 273 | int sha256_vector(size_t num_elem, const u8 *addr[], const size_t *len, |
| 274 | u8 *mac) |
| 275 | { |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 276 | return openssl_digest_vector(EVP_sha256(), num_elem, addr, len, mac); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 277 | } |
| 278 | #endif /* NO_SHA256_WRAPPER */ |
| 279 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 280 | |
Paul Stewart | 092955c | 2017-02-06 09:13:09 -0800 | [diff] [blame] | 281 | #ifndef NO_SHA384_WRAPPER |
| 282 | int sha384_vector(size_t num_elem, const u8 *addr[], const size_t *len, |
| 283 | u8 *mac) |
| 284 | { |
| 285 | return openssl_digest_vector(EVP_sha384(), num_elem, addr, len, mac); |
| 286 | } |
| 287 | #endif /* NO_SHA384_WRAPPER */ |
| 288 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 289 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 290 | #ifndef NO_SHA512_WRAPPER |
| 291 | int sha512_vector(size_t num_elem, const u8 *addr[], const size_t *len, |
| 292 | u8 *mac) |
| 293 | { |
| 294 | return openssl_digest_vector(EVP_sha512(), num_elem, addr, len, mac); |
| 295 | } |
| 296 | #endif /* NO_SHA512_WRAPPER */ |
| 297 | |
| 298 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 299 | static const EVP_CIPHER * aes_get_evp_cipher(size_t keylen) |
| 300 | { |
| 301 | switch (keylen) { |
| 302 | case 16: |
| 303 | return EVP_aes_128_ecb(); |
| 304 | case 24: |
| 305 | return EVP_aes_192_ecb(); |
| 306 | case 32: |
| 307 | return EVP_aes_256_ecb(); |
| 308 | } |
| 309 | |
| 310 | return NULL; |
| 311 | } |
| 312 | |
| 313 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 314 | void * aes_encrypt_init(const u8 *key, size_t len) |
| 315 | { |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 316 | EVP_CIPHER_CTX *ctx; |
| 317 | const EVP_CIPHER *type; |
| 318 | |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 319 | if (TEST_FAIL()) |
| 320 | return NULL; |
| 321 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 322 | type = aes_get_evp_cipher(len); |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 323 | if (!type) { |
| 324 | wpa_printf(MSG_INFO, "%s: Unsupported len=%u", |
| 325 | __func__, (unsigned int) len); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 326 | return NULL; |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 327 | } |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 328 | |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 329 | ctx = EVP_CIPHER_CTX_new(); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 330 | if (ctx == NULL) |
| 331 | return NULL; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 332 | if (EVP_EncryptInit_ex(ctx, type, NULL, key, NULL) != 1) { |
| 333 | os_free(ctx); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 334 | return NULL; |
| 335 | } |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 336 | EVP_CIPHER_CTX_set_padding(ctx, 0); |
| 337 | return ctx; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 341 | int aes_encrypt(void *ctx, const u8 *plain, u8 *crypt) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 342 | { |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 343 | EVP_CIPHER_CTX *c = ctx; |
| 344 | int clen = 16; |
| 345 | if (EVP_EncryptUpdate(c, crypt, &clen, plain, 16) != 1) { |
| 346 | wpa_printf(MSG_ERROR, "OpenSSL: EVP_EncryptUpdate failed: %s", |
| 347 | ERR_error_string(ERR_get_error(), NULL)); |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 348 | return -1; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 349 | } |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 350 | return 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | |
| 354 | void aes_encrypt_deinit(void *ctx) |
| 355 | { |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 356 | EVP_CIPHER_CTX *c = ctx; |
| 357 | u8 buf[16]; |
| 358 | int len = sizeof(buf); |
| 359 | if (EVP_EncryptFinal_ex(c, buf, &len) != 1) { |
| 360 | wpa_printf(MSG_ERROR, "OpenSSL: EVP_EncryptFinal_ex failed: " |
| 361 | "%s", ERR_error_string(ERR_get_error(), NULL)); |
| 362 | } |
| 363 | if (len != 0) { |
| 364 | wpa_printf(MSG_ERROR, "OpenSSL: Unexpected padding length %d " |
| 365 | "in AES encrypt", len); |
| 366 | } |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 367 | EVP_CIPHER_CTX_free(c); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | |
| 371 | void * aes_decrypt_init(const u8 *key, size_t len) |
| 372 | { |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 373 | EVP_CIPHER_CTX *ctx; |
| 374 | const EVP_CIPHER *type; |
| 375 | |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 376 | if (TEST_FAIL()) |
| 377 | return NULL; |
| 378 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 379 | type = aes_get_evp_cipher(len); |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 380 | if (!type) { |
| 381 | wpa_printf(MSG_INFO, "%s: Unsupported len=%u", |
| 382 | __func__, (unsigned int) len); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 383 | return NULL; |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 384 | } |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 385 | |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 386 | ctx = EVP_CIPHER_CTX_new(); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 387 | if (ctx == NULL) |
| 388 | return NULL; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 389 | if (EVP_DecryptInit_ex(ctx, type, NULL, key, NULL) != 1) { |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 390 | EVP_CIPHER_CTX_free(ctx); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 391 | return NULL; |
| 392 | } |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 393 | EVP_CIPHER_CTX_set_padding(ctx, 0); |
| 394 | return ctx; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 398 | int aes_decrypt(void *ctx, const u8 *crypt, u8 *plain) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 399 | { |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 400 | EVP_CIPHER_CTX *c = ctx; |
| 401 | int plen = 16; |
| 402 | if (EVP_DecryptUpdate(c, plain, &plen, crypt, 16) != 1) { |
| 403 | wpa_printf(MSG_ERROR, "OpenSSL: EVP_DecryptUpdate failed: %s", |
| 404 | ERR_error_string(ERR_get_error(), NULL)); |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 405 | return -1; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 406 | } |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 407 | return 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | |
| 411 | void aes_decrypt_deinit(void *ctx) |
| 412 | { |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 413 | EVP_CIPHER_CTX *c = ctx; |
| 414 | u8 buf[16]; |
| 415 | int len = sizeof(buf); |
| 416 | if (EVP_DecryptFinal_ex(c, buf, &len) != 1) { |
| 417 | wpa_printf(MSG_ERROR, "OpenSSL: EVP_DecryptFinal_ex failed: " |
| 418 | "%s", ERR_error_string(ERR_get_error(), NULL)); |
| 419 | } |
| 420 | if (len != 0) { |
| 421 | wpa_printf(MSG_ERROR, "OpenSSL: Unexpected padding length %d " |
| 422 | "in AES decrypt", len); |
| 423 | } |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 424 | EVP_CIPHER_CTX_free(c); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 428 | #ifndef CONFIG_FIPS |
| 429 | #ifndef CONFIG_OPENSSL_INTERNAL_AES_WRAP |
| 430 | |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 431 | int aes_wrap(const u8 *kek, size_t kek_len, int n, const u8 *plain, u8 *cipher) |
| 432 | { |
| 433 | AES_KEY actx; |
| 434 | int res; |
| 435 | |
Dmitry Shmidt | ebd93af | 2017-02-21 13:40:44 -0800 | [diff] [blame] | 436 | if (TEST_FAIL()) |
| 437 | return -1; |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 438 | if (AES_set_encrypt_key(kek, kek_len << 3, &actx)) |
| 439 | return -1; |
| 440 | res = AES_wrap_key(&actx, NULL, cipher, plain, n * 8); |
| 441 | OPENSSL_cleanse(&actx, sizeof(actx)); |
| 442 | return res <= 0 ? -1 : 0; |
| 443 | } |
| 444 | |
| 445 | |
| 446 | int aes_unwrap(const u8 *kek, size_t kek_len, int n, const u8 *cipher, |
| 447 | u8 *plain) |
| 448 | { |
| 449 | AES_KEY actx; |
| 450 | int res; |
| 451 | |
Dmitry Shmidt | ebd93af | 2017-02-21 13:40:44 -0800 | [diff] [blame] | 452 | if (TEST_FAIL()) |
| 453 | return -1; |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 454 | if (AES_set_decrypt_key(kek, kek_len << 3, &actx)) |
| 455 | return -1; |
| 456 | res = AES_unwrap_key(&actx, NULL, plain, cipher, (n + 1) * 8); |
| 457 | OPENSSL_cleanse(&actx, sizeof(actx)); |
| 458 | return res <= 0 ? -1 : 0; |
| 459 | } |
| 460 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 461 | #endif /* CONFIG_OPENSSL_INTERNAL_AES_WRAP */ |
| 462 | #endif /* CONFIG_FIPS */ |
| 463 | |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 464 | |
Dmitry Shmidt | 912c6ec | 2015-03-30 13:16:51 -0700 | [diff] [blame] | 465 | int aes_128_cbc_encrypt(const u8 *key, const u8 *iv, u8 *data, size_t data_len) |
| 466 | { |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 467 | EVP_CIPHER_CTX *ctx; |
Dmitry Shmidt | 912c6ec | 2015-03-30 13:16:51 -0700 | [diff] [blame] | 468 | int clen, len; |
| 469 | u8 buf[16]; |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 470 | int res = -1; |
Dmitry Shmidt | 912c6ec | 2015-03-30 13:16:51 -0700 | [diff] [blame] | 471 | |
Dmitry Shmidt | d7ff03d | 2015-12-04 14:49:35 -0800 | [diff] [blame] | 472 | if (TEST_FAIL()) |
| 473 | return -1; |
| 474 | |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 475 | ctx = EVP_CIPHER_CTX_new(); |
| 476 | if (!ctx) |
Dmitry Shmidt | 912c6ec | 2015-03-30 13:16:51 -0700 | [diff] [blame] | 477 | return -1; |
Dmitry Shmidt | 912c6ec | 2015-03-30 13:16:51 -0700 | [diff] [blame] | 478 | clen = data_len; |
Dmitry Shmidt | 912c6ec | 2015-03-30 13:16:51 -0700 | [diff] [blame] | 479 | len = sizeof(buf); |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 480 | if (EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv) == 1 && |
| 481 | EVP_CIPHER_CTX_set_padding(ctx, 0) == 1 && |
| 482 | EVP_EncryptUpdate(ctx, data, &clen, data, data_len) == 1 && |
| 483 | clen == (int) data_len && |
| 484 | EVP_EncryptFinal_ex(ctx, buf, &len) == 1 && len == 0) |
| 485 | res = 0; |
Dmitry Shmidt | 57c2d39 | 2016-02-23 13:40:19 -0800 | [diff] [blame] | 486 | EVP_CIPHER_CTX_free(ctx); |
Dmitry Shmidt | 912c6ec | 2015-03-30 13:16:51 -0700 | [diff] [blame] | 487 | |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 488 | return res; |
Dmitry Shmidt | 912c6ec | 2015-03-30 13:16:51 -0700 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | |
| 492 | int aes_128_cbc_decrypt(const u8 *key, const u8 *iv, u8 *data, size_t data_len) |
| 493 | { |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 494 | EVP_CIPHER_CTX *ctx; |
Dmitry Shmidt | 912c6ec | 2015-03-30 13:16:51 -0700 | [diff] [blame] | 495 | int plen, len; |
| 496 | u8 buf[16]; |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 497 | int res = -1; |
Dmitry Shmidt | 912c6ec | 2015-03-30 13:16:51 -0700 | [diff] [blame] | 498 | |
Dmitry Shmidt | d7ff03d | 2015-12-04 14:49:35 -0800 | [diff] [blame] | 499 | if (TEST_FAIL()) |
| 500 | return -1; |
| 501 | |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 502 | ctx = EVP_CIPHER_CTX_new(); |
| 503 | if (!ctx) |
Dmitry Shmidt | 912c6ec | 2015-03-30 13:16:51 -0700 | [diff] [blame] | 504 | return -1; |
Dmitry Shmidt | 912c6ec | 2015-03-30 13:16:51 -0700 | [diff] [blame] | 505 | plen = data_len; |
Dmitry Shmidt | 912c6ec | 2015-03-30 13:16:51 -0700 | [diff] [blame] | 506 | len = sizeof(buf); |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 507 | if (EVP_DecryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv) == 1 && |
| 508 | EVP_CIPHER_CTX_set_padding(ctx, 0) == 1 && |
| 509 | EVP_DecryptUpdate(ctx, data, &plen, data, data_len) == 1 && |
| 510 | plen == (int) data_len && |
| 511 | EVP_DecryptFinal_ex(ctx, buf, &len) == 1 && len == 0) |
| 512 | res = 0; |
Dmitry Shmidt | 57c2d39 | 2016-02-23 13:40:19 -0800 | [diff] [blame] | 513 | EVP_CIPHER_CTX_free(ctx); |
Dmitry Shmidt | 912c6ec | 2015-03-30 13:16:51 -0700 | [diff] [blame] | 514 | |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 515 | return res; |
| 516 | |
Dmitry Shmidt | 912c6ec | 2015-03-30 13:16:51 -0700 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 520 | int crypto_dh_init(u8 generator, const u8 *prime, size_t prime_len, u8 *privkey, |
| 521 | u8 *pubkey) |
| 522 | { |
| 523 | size_t pubkey_len, pad; |
| 524 | |
| 525 | if (os_get_random(privkey, prime_len) < 0) |
| 526 | return -1; |
| 527 | if (os_memcmp(privkey, prime, prime_len) > 0) { |
| 528 | /* Make sure private value is smaller than prime */ |
| 529 | privkey[0] = 0; |
| 530 | } |
| 531 | |
| 532 | pubkey_len = prime_len; |
| 533 | if (crypto_mod_exp(&generator, 1, privkey, prime_len, prime, prime_len, |
| 534 | pubkey, &pubkey_len) < 0) |
| 535 | return -1; |
| 536 | if (pubkey_len < prime_len) { |
| 537 | pad = prime_len - pubkey_len; |
| 538 | os_memmove(pubkey + pad, pubkey, pubkey_len); |
| 539 | os_memset(pubkey, 0, pad); |
| 540 | } |
| 541 | |
| 542 | return 0; |
| 543 | } |
| 544 | |
| 545 | |
| 546 | int crypto_dh_derive_secret(u8 generator, const u8 *prime, size_t prime_len, |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 547 | const u8 *order, size_t order_len, |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 548 | const u8 *privkey, size_t privkey_len, |
| 549 | const u8 *pubkey, size_t pubkey_len, |
| 550 | u8 *secret, size_t *len) |
| 551 | { |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 552 | BIGNUM *pub, *p; |
| 553 | int res = -1; |
| 554 | |
| 555 | pub = BN_bin2bn(pubkey, pubkey_len, NULL); |
| 556 | p = BN_bin2bn(prime, prime_len, NULL); |
| 557 | if (!pub || !p || BN_is_zero(pub) || BN_is_one(pub) || |
| 558 | BN_cmp(pub, p) >= 0) |
| 559 | goto fail; |
| 560 | |
| 561 | if (order) { |
| 562 | BN_CTX *ctx; |
| 563 | BIGNUM *q, *tmp; |
| 564 | int failed; |
| 565 | |
| 566 | /* verify: pubkey^q == 1 mod p */ |
| 567 | q = BN_bin2bn(order, order_len, NULL); |
| 568 | ctx = BN_CTX_new(); |
| 569 | tmp = BN_new(); |
| 570 | failed = !q || !ctx || !tmp || |
| 571 | !BN_mod_exp(tmp, pub, q, p, ctx) || |
| 572 | !BN_is_one(tmp); |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 573 | BN_clear_free(q); |
| 574 | BN_clear_free(tmp); |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 575 | BN_CTX_free(ctx); |
| 576 | if (failed) |
| 577 | goto fail; |
| 578 | } |
| 579 | |
| 580 | res = crypto_mod_exp(pubkey, pubkey_len, privkey, privkey_len, |
| 581 | prime, prime_len, secret, len); |
| 582 | fail: |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 583 | BN_clear_free(pub); |
| 584 | BN_clear_free(p); |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 585 | return res; |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 589 | int crypto_mod_exp(const u8 *base, size_t base_len, |
| 590 | const u8 *power, size_t power_len, |
| 591 | const u8 *modulus, size_t modulus_len, |
| 592 | u8 *result, size_t *result_len) |
| 593 | { |
| 594 | BIGNUM *bn_base, *bn_exp, *bn_modulus, *bn_result; |
| 595 | int ret = -1; |
| 596 | BN_CTX *ctx; |
| 597 | |
| 598 | ctx = BN_CTX_new(); |
| 599 | if (ctx == NULL) |
| 600 | return -1; |
| 601 | |
| 602 | bn_base = BN_bin2bn(base, base_len, NULL); |
| 603 | bn_exp = BN_bin2bn(power, power_len, NULL); |
| 604 | bn_modulus = BN_bin2bn(modulus, modulus_len, NULL); |
| 605 | bn_result = BN_new(); |
| 606 | |
| 607 | if (bn_base == NULL || bn_exp == NULL || bn_modulus == NULL || |
| 608 | bn_result == NULL) |
| 609 | goto error; |
| 610 | |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 611 | if (BN_mod_exp_mont_consttime(bn_result, bn_base, bn_exp, bn_modulus, |
| 612 | ctx, NULL) != 1) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 613 | goto error; |
| 614 | |
| 615 | *result_len = BN_bn2bin(bn_result, result); |
| 616 | ret = 0; |
| 617 | |
| 618 | error: |
Dmitry Shmidt | 7f0b69e | 2014-07-28 10:35:20 -0700 | [diff] [blame] | 619 | BN_clear_free(bn_base); |
| 620 | BN_clear_free(bn_exp); |
| 621 | BN_clear_free(bn_modulus); |
| 622 | BN_clear_free(bn_result); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 623 | BN_CTX_free(ctx); |
| 624 | return ret; |
| 625 | } |
| 626 | |
| 627 | |
| 628 | struct crypto_cipher { |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 629 | EVP_CIPHER_CTX *enc; |
| 630 | EVP_CIPHER_CTX *dec; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 631 | }; |
| 632 | |
| 633 | |
| 634 | struct crypto_cipher * crypto_cipher_init(enum crypto_cipher_alg alg, |
| 635 | const u8 *iv, const u8 *key, |
| 636 | size_t key_len) |
| 637 | { |
| 638 | struct crypto_cipher *ctx; |
| 639 | const EVP_CIPHER *cipher; |
| 640 | |
| 641 | ctx = os_zalloc(sizeof(*ctx)); |
| 642 | if (ctx == NULL) |
| 643 | return NULL; |
| 644 | |
| 645 | switch (alg) { |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 646 | #ifndef CONFIG_NO_RC4 |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 647 | #ifndef OPENSSL_NO_RC4 |
| 648 | case CRYPTO_CIPHER_ALG_RC4: |
| 649 | cipher = EVP_rc4(); |
| 650 | break; |
| 651 | #endif /* OPENSSL_NO_RC4 */ |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 652 | #endif /* CONFIG_NO_RC4 */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 653 | #ifndef OPENSSL_NO_AES |
| 654 | case CRYPTO_CIPHER_ALG_AES: |
| 655 | switch (key_len) { |
| 656 | case 16: |
| 657 | cipher = EVP_aes_128_cbc(); |
| 658 | break; |
Dmitry Shmidt | 9ead16e | 2014-10-07 13:15:23 -0700 | [diff] [blame] | 659 | #ifndef OPENSSL_IS_BORINGSSL |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 660 | case 24: |
| 661 | cipher = EVP_aes_192_cbc(); |
| 662 | break; |
Dmitry Shmidt | 9ead16e | 2014-10-07 13:15:23 -0700 | [diff] [blame] | 663 | #endif /* OPENSSL_IS_BORINGSSL */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 664 | case 32: |
| 665 | cipher = EVP_aes_256_cbc(); |
| 666 | break; |
| 667 | default: |
| 668 | os_free(ctx); |
| 669 | return NULL; |
| 670 | } |
| 671 | break; |
| 672 | #endif /* OPENSSL_NO_AES */ |
| 673 | #ifndef OPENSSL_NO_DES |
| 674 | case CRYPTO_CIPHER_ALG_3DES: |
| 675 | cipher = EVP_des_ede3_cbc(); |
| 676 | break; |
| 677 | case CRYPTO_CIPHER_ALG_DES: |
| 678 | cipher = EVP_des_cbc(); |
| 679 | break; |
| 680 | #endif /* OPENSSL_NO_DES */ |
| 681 | #ifndef OPENSSL_NO_RC2 |
| 682 | case CRYPTO_CIPHER_ALG_RC2: |
| 683 | cipher = EVP_rc2_ecb(); |
| 684 | break; |
| 685 | #endif /* OPENSSL_NO_RC2 */ |
| 686 | default: |
| 687 | os_free(ctx); |
| 688 | return NULL; |
| 689 | } |
| 690 | |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 691 | if (!(ctx->enc = EVP_CIPHER_CTX_new()) || |
| 692 | !EVP_CIPHER_CTX_set_padding(ctx->enc, 0) || |
| 693 | !EVP_EncryptInit_ex(ctx->enc, cipher, NULL, NULL, NULL) || |
| 694 | !EVP_CIPHER_CTX_set_key_length(ctx->enc, key_len) || |
| 695 | !EVP_EncryptInit_ex(ctx->enc, NULL, NULL, key, iv)) { |
| 696 | if (ctx->enc) |
Dmitry Shmidt | 57c2d39 | 2016-02-23 13:40:19 -0800 | [diff] [blame] | 697 | EVP_CIPHER_CTX_free(ctx->enc); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 698 | os_free(ctx); |
| 699 | return NULL; |
| 700 | } |
| 701 | |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 702 | if (!(ctx->dec = EVP_CIPHER_CTX_new()) || |
| 703 | !EVP_CIPHER_CTX_set_padding(ctx->dec, 0) || |
| 704 | !EVP_DecryptInit_ex(ctx->dec, cipher, NULL, NULL, NULL) || |
| 705 | !EVP_CIPHER_CTX_set_key_length(ctx->dec, key_len) || |
| 706 | !EVP_DecryptInit_ex(ctx->dec, NULL, NULL, key, iv)) { |
Dmitry Shmidt | 57c2d39 | 2016-02-23 13:40:19 -0800 | [diff] [blame] | 707 | EVP_CIPHER_CTX_free(ctx->enc); |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 708 | if (ctx->dec) |
Dmitry Shmidt | 57c2d39 | 2016-02-23 13:40:19 -0800 | [diff] [blame] | 709 | EVP_CIPHER_CTX_free(ctx->dec); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 710 | os_free(ctx); |
| 711 | return NULL; |
| 712 | } |
| 713 | |
| 714 | return ctx; |
| 715 | } |
| 716 | |
| 717 | |
| 718 | int crypto_cipher_encrypt(struct crypto_cipher *ctx, const u8 *plain, |
| 719 | u8 *crypt, size_t len) |
| 720 | { |
| 721 | int outl; |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 722 | if (!EVP_EncryptUpdate(ctx->enc, crypt, &outl, plain, len)) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 723 | return -1; |
| 724 | return 0; |
| 725 | } |
| 726 | |
| 727 | |
| 728 | int crypto_cipher_decrypt(struct crypto_cipher *ctx, const u8 *crypt, |
| 729 | u8 *plain, size_t len) |
| 730 | { |
| 731 | int outl; |
| 732 | outl = len; |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 733 | if (!EVP_DecryptUpdate(ctx->dec, plain, &outl, crypt, len)) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 734 | return -1; |
| 735 | return 0; |
| 736 | } |
| 737 | |
| 738 | |
| 739 | void crypto_cipher_deinit(struct crypto_cipher *ctx) |
| 740 | { |
Dmitry Shmidt | 57c2d39 | 2016-02-23 13:40:19 -0800 | [diff] [blame] | 741 | EVP_CIPHER_CTX_free(ctx->enc); |
| 742 | EVP_CIPHER_CTX_free(ctx->dec); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 743 | os_free(ctx); |
| 744 | } |
| 745 | |
| 746 | |
| 747 | void * dh5_init(struct wpabuf **priv, struct wpabuf **publ) |
| 748 | { |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 749 | #if OPENSSL_VERSION_NUMBER < 0x10100000L || \ |
| 750 | (defined(LIBRESSL_VERSION_NUMBER) && \ |
| 751 | LIBRESSL_VERSION_NUMBER < 0x20700000L) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 752 | DH *dh; |
| 753 | struct wpabuf *pubkey = NULL, *privkey = NULL; |
| 754 | size_t publen, privlen; |
| 755 | |
| 756 | *priv = NULL; |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 757 | wpabuf_free(*publ); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 758 | *publ = NULL; |
| 759 | |
| 760 | dh = DH_new(); |
| 761 | if (dh == NULL) |
| 762 | return NULL; |
| 763 | |
| 764 | dh->g = BN_new(); |
| 765 | if (dh->g == NULL || BN_set_word(dh->g, 2) != 1) |
| 766 | goto err; |
| 767 | |
| 768 | dh->p = get_group5_prime(); |
| 769 | if (dh->p == NULL) |
| 770 | goto err; |
| 771 | |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 772 | dh->q = get_group5_order(); |
| 773 | if (!dh->q) |
| 774 | goto err; |
| 775 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 776 | if (DH_generate_key(dh) != 1) |
| 777 | goto err; |
| 778 | |
| 779 | publen = BN_num_bytes(dh->pub_key); |
| 780 | pubkey = wpabuf_alloc(publen); |
| 781 | if (pubkey == NULL) |
| 782 | goto err; |
| 783 | privlen = BN_num_bytes(dh->priv_key); |
| 784 | privkey = wpabuf_alloc(privlen); |
| 785 | if (privkey == NULL) |
| 786 | goto err; |
| 787 | |
| 788 | BN_bn2bin(dh->pub_key, wpabuf_put(pubkey, publen)); |
| 789 | BN_bn2bin(dh->priv_key, wpabuf_put(privkey, privlen)); |
| 790 | |
| 791 | *priv = privkey; |
| 792 | *publ = pubkey; |
| 793 | return dh; |
| 794 | |
| 795 | err: |
Dmitry Shmidt | ff787d5 | 2015-01-12 13:01:47 -0800 | [diff] [blame] | 796 | wpabuf_clear_free(pubkey); |
| 797 | wpabuf_clear_free(privkey); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 798 | DH_free(dh); |
| 799 | return NULL; |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 800 | #else |
| 801 | DH *dh; |
| 802 | struct wpabuf *pubkey = NULL, *privkey = NULL; |
| 803 | size_t publen, privlen; |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 804 | BIGNUM *p, *g, *q; |
Dmitry Shmidt | 4ae50e6 | 2016-06-27 13:48:39 -0700 | [diff] [blame] | 805 | const BIGNUM *priv_key = NULL, *pub_key = NULL; |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 806 | |
| 807 | *priv = NULL; |
| 808 | wpabuf_free(*publ); |
| 809 | *publ = NULL; |
| 810 | |
| 811 | dh = DH_new(); |
| 812 | if (dh == NULL) |
| 813 | return NULL; |
| 814 | |
| 815 | g = BN_new(); |
| 816 | p = get_group5_prime(); |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 817 | q = get_group5_order(); |
| 818 | if (!g || BN_set_word(g, 2) != 1 || !p || !q || |
| 819 | DH_set0_pqg(dh, p, q, g) != 1) |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 820 | goto err; |
| 821 | p = NULL; |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 822 | q = NULL; |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 823 | g = NULL; |
| 824 | |
| 825 | if (DH_generate_key(dh) != 1) |
| 826 | goto err; |
| 827 | |
| 828 | DH_get0_key(dh, &pub_key, &priv_key); |
| 829 | publen = BN_num_bytes(pub_key); |
| 830 | pubkey = wpabuf_alloc(publen); |
| 831 | if (!pubkey) |
| 832 | goto err; |
| 833 | privlen = BN_num_bytes(priv_key); |
| 834 | privkey = wpabuf_alloc(privlen); |
| 835 | if (!privkey) |
| 836 | goto err; |
| 837 | |
| 838 | BN_bn2bin(pub_key, wpabuf_put(pubkey, publen)); |
| 839 | BN_bn2bin(priv_key, wpabuf_put(privkey, privlen)); |
| 840 | |
| 841 | *priv = privkey; |
| 842 | *publ = pubkey; |
| 843 | return dh; |
| 844 | |
| 845 | err: |
| 846 | BN_free(p); |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 847 | BN_free(q); |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 848 | BN_free(g); |
| 849 | wpabuf_clear_free(pubkey); |
| 850 | wpabuf_clear_free(privkey); |
| 851 | DH_free(dh); |
| 852 | return NULL; |
| 853 | #endif |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 854 | } |
| 855 | |
| 856 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 857 | void * dh5_init_fixed(const struct wpabuf *priv, const struct wpabuf *publ) |
| 858 | { |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 859 | #if OPENSSL_VERSION_NUMBER < 0x10100000L || \ |
| 860 | (defined(LIBRESSL_VERSION_NUMBER) && \ |
| 861 | LIBRESSL_VERSION_NUMBER < 0x20700000L) |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 862 | DH *dh; |
| 863 | |
| 864 | dh = DH_new(); |
| 865 | if (dh == NULL) |
| 866 | return NULL; |
| 867 | |
| 868 | dh->g = BN_new(); |
| 869 | if (dh->g == NULL || BN_set_word(dh->g, 2) != 1) |
| 870 | goto err; |
| 871 | |
| 872 | dh->p = get_group5_prime(); |
| 873 | if (dh->p == NULL) |
| 874 | goto err; |
| 875 | |
| 876 | dh->priv_key = BN_bin2bn(wpabuf_head(priv), wpabuf_len(priv), NULL); |
| 877 | if (dh->priv_key == NULL) |
| 878 | goto err; |
| 879 | |
| 880 | dh->pub_key = BN_bin2bn(wpabuf_head(publ), wpabuf_len(publ), NULL); |
| 881 | if (dh->pub_key == NULL) |
| 882 | goto err; |
| 883 | |
| 884 | if (DH_generate_key(dh) != 1) |
| 885 | goto err; |
| 886 | |
| 887 | return dh; |
| 888 | |
| 889 | err: |
| 890 | DH_free(dh); |
| 891 | return NULL; |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 892 | #else |
| 893 | DH *dh; |
| 894 | BIGNUM *p = NULL, *g, *priv_key = NULL, *pub_key = NULL; |
| 895 | |
| 896 | dh = DH_new(); |
| 897 | if (dh == NULL) |
| 898 | return NULL; |
| 899 | |
| 900 | g = BN_new(); |
| 901 | p = get_group5_prime(); |
| 902 | if (!g || BN_set_word(g, 2) != 1 || !p || |
| 903 | DH_set0_pqg(dh, p, NULL, g) != 1) |
| 904 | goto err; |
| 905 | p = NULL; |
| 906 | g = NULL; |
| 907 | |
| 908 | priv_key = BN_bin2bn(wpabuf_head(priv), wpabuf_len(priv), NULL); |
| 909 | pub_key = BN_bin2bn(wpabuf_head(publ), wpabuf_len(publ), NULL); |
Dmitry Shmidt | 58d12ad | 2016-07-28 10:07:03 -0700 | [diff] [blame] | 910 | if (!priv_key || !pub_key || DH_set0_key(dh, pub_key, priv_key) != 1) |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 911 | goto err; |
| 912 | pub_key = NULL; |
| 913 | priv_key = NULL; |
| 914 | |
| 915 | if (DH_generate_key(dh) != 1) |
| 916 | goto err; |
| 917 | |
| 918 | return dh; |
| 919 | |
| 920 | err: |
| 921 | BN_free(p); |
| 922 | BN_free(g); |
| 923 | BN_free(pub_key); |
| 924 | BN_clear_free(priv_key); |
| 925 | DH_free(dh); |
| 926 | return NULL; |
| 927 | #endif |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 928 | } |
| 929 | |
| 930 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 931 | struct wpabuf * dh5_derive_shared(void *ctx, const struct wpabuf *peer_public, |
| 932 | const struct wpabuf *own_private) |
| 933 | { |
| 934 | BIGNUM *pub_key; |
| 935 | struct wpabuf *res = NULL; |
| 936 | size_t rlen; |
| 937 | DH *dh = ctx; |
| 938 | int keylen; |
| 939 | |
| 940 | if (ctx == NULL) |
| 941 | return NULL; |
| 942 | |
| 943 | pub_key = BN_bin2bn(wpabuf_head(peer_public), wpabuf_len(peer_public), |
| 944 | NULL); |
| 945 | if (pub_key == NULL) |
| 946 | return NULL; |
| 947 | |
| 948 | rlen = DH_size(dh); |
| 949 | res = wpabuf_alloc(rlen); |
| 950 | if (res == NULL) |
| 951 | goto err; |
| 952 | |
| 953 | keylen = DH_compute_key(wpabuf_mhead(res), pub_key, dh); |
| 954 | if (keylen < 0) |
| 955 | goto err; |
| 956 | wpabuf_put(res, keylen); |
Dmitry Shmidt | 7f0b69e | 2014-07-28 10:35:20 -0700 | [diff] [blame] | 957 | BN_clear_free(pub_key); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 958 | |
| 959 | return res; |
| 960 | |
| 961 | err: |
Dmitry Shmidt | 7f0b69e | 2014-07-28 10:35:20 -0700 | [diff] [blame] | 962 | BN_clear_free(pub_key); |
Dmitry Shmidt | ff787d5 | 2015-01-12 13:01:47 -0800 | [diff] [blame] | 963 | wpabuf_clear_free(res); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 964 | return NULL; |
| 965 | } |
| 966 | |
| 967 | |
| 968 | void dh5_free(void *ctx) |
| 969 | { |
| 970 | DH *dh; |
| 971 | if (ctx == NULL) |
| 972 | return; |
| 973 | dh = ctx; |
| 974 | DH_free(dh); |
| 975 | } |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 976 | |
| 977 | |
| 978 | struct crypto_hash { |
Dmitry Shmidt | 55840ad | 2015-12-14 12:45:46 -0800 | [diff] [blame] | 979 | HMAC_CTX *ctx; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 980 | }; |
| 981 | |
| 982 | |
| 983 | struct crypto_hash * crypto_hash_init(enum crypto_hash_alg alg, const u8 *key, |
| 984 | size_t key_len) |
| 985 | { |
| 986 | struct crypto_hash *ctx; |
| 987 | const EVP_MD *md; |
| 988 | |
| 989 | switch (alg) { |
| 990 | #ifndef OPENSSL_NO_MD5 |
| 991 | case CRYPTO_HASH_ALG_HMAC_MD5: |
| 992 | md = EVP_md5(); |
| 993 | break; |
| 994 | #endif /* OPENSSL_NO_MD5 */ |
| 995 | #ifndef OPENSSL_NO_SHA |
| 996 | case CRYPTO_HASH_ALG_HMAC_SHA1: |
| 997 | md = EVP_sha1(); |
| 998 | break; |
| 999 | #endif /* OPENSSL_NO_SHA */ |
| 1000 | #ifndef OPENSSL_NO_SHA256 |
| 1001 | #ifdef CONFIG_SHA256 |
| 1002 | case CRYPTO_HASH_ALG_HMAC_SHA256: |
| 1003 | md = EVP_sha256(); |
| 1004 | break; |
| 1005 | #endif /* CONFIG_SHA256 */ |
| 1006 | #endif /* OPENSSL_NO_SHA256 */ |
| 1007 | default: |
| 1008 | return NULL; |
| 1009 | } |
| 1010 | |
| 1011 | ctx = os_zalloc(sizeof(*ctx)); |
| 1012 | if (ctx == NULL) |
| 1013 | return NULL; |
Dmitry Shmidt | 55840ad | 2015-12-14 12:45:46 -0800 | [diff] [blame] | 1014 | ctx->ctx = HMAC_CTX_new(); |
| 1015 | if (!ctx->ctx) { |
| 1016 | os_free(ctx); |
| 1017 | return NULL; |
| 1018 | } |
| 1019 | |
| 1020 | if (HMAC_Init_ex(ctx->ctx, key, key_len, md, NULL) != 1) { |
| 1021 | HMAC_CTX_free(ctx->ctx); |
| 1022 | bin_clear_free(ctx, sizeof(*ctx)); |
| 1023 | return NULL; |
| 1024 | } |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1025 | |
| 1026 | return ctx; |
| 1027 | } |
| 1028 | |
| 1029 | |
| 1030 | void crypto_hash_update(struct crypto_hash *ctx, const u8 *data, size_t len) |
| 1031 | { |
| 1032 | if (ctx == NULL) |
| 1033 | return; |
Dmitry Shmidt | 55840ad | 2015-12-14 12:45:46 -0800 | [diff] [blame] | 1034 | HMAC_Update(ctx->ctx, data, len); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1035 | } |
| 1036 | |
| 1037 | |
| 1038 | int crypto_hash_finish(struct crypto_hash *ctx, u8 *mac, size_t *len) |
| 1039 | { |
| 1040 | unsigned int mdlen; |
| 1041 | int res; |
| 1042 | |
| 1043 | if (ctx == NULL) |
| 1044 | return -2; |
| 1045 | |
| 1046 | if (mac == NULL || len == NULL) { |
Dmitry Shmidt | 55840ad | 2015-12-14 12:45:46 -0800 | [diff] [blame] | 1047 | HMAC_CTX_free(ctx->ctx); |
Dmitry Shmidt | ff787d5 | 2015-01-12 13:01:47 -0800 | [diff] [blame] | 1048 | bin_clear_free(ctx, sizeof(*ctx)); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1049 | return 0; |
| 1050 | } |
| 1051 | |
| 1052 | mdlen = *len; |
Dmitry Shmidt | 55840ad | 2015-12-14 12:45:46 -0800 | [diff] [blame] | 1053 | res = HMAC_Final(ctx->ctx, mac, &mdlen); |
| 1054 | HMAC_CTX_free(ctx->ctx); |
Dmitry Shmidt | ff787d5 | 2015-01-12 13:01:47 -0800 | [diff] [blame] | 1055 | bin_clear_free(ctx, sizeof(*ctx)); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1056 | |
Hai Shalom | 5f92bc9 | 2019-04-18 11:54:11 -0700 | [diff] [blame] | 1057 | if (TEST_FAIL()) |
| 1058 | return -1; |
| 1059 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1060 | if (res == 1) { |
| 1061 | *len = mdlen; |
| 1062 | return 0; |
| 1063 | } |
| 1064 | |
| 1065 | return -1; |
| 1066 | } |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1067 | |
| 1068 | |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 1069 | static int openssl_hmac_vector(const EVP_MD *type, const u8 *key, |
| 1070 | size_t key_len, size_t num_elem, |
| 1071 | const u8 *addr[], const size_t *len, u8 *mac, |
| 1072 | unsigned int mdlen) |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1073 | { |
Dmitry Shmidt | 55840ad | 2015-12-14 12:45:46 -0800 | [diff] [blame] | 1074 | HMAC_CTX *ctx; |
| 1075 | size_t i; |
| 1076 | int res; |
| 1077 | |
| 1078 | if (TEST_FAIL()) |
| 1079 | return -1; |
| 1080 | |
| 1081 | ctx = HMAC_CTX_new(); |
| 1082 | if (!ctx) |
| 1083 | return -1; |
| 1084 | res = HMAC_Init_ex(ctx, key, key_len, type, NULL); |
| 1085 | if (res != 1) |
| 1086 | goto done; |
| 1087 | |
| 1088 | for (i = 0; i < num_elem; i++) |
| 1089 | HMAC_Update(ctx, addr[i], len[i]); |
| 1090 | |
| 1091 | res = HMAC_Final(ctx, mac, &mdlen); |
| 1092 | done: |
| 1093 | HMAC_CTX_free(ctx); |
| 1094 | |
| 1095 | return res == 1 ? 0 : -1; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1096 | } |
| 1097 | |
| 1098 | |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 1099 | #ifndef CONFIG_FIPS |
| 1100 | |
| 1101 | int hmac_md5_vector(const u8 *key, size_t key_len, size_t num_elem, |
| 1102 | const u8 *addr[], const size_t *len, u8 *mac) |
| 1103 | { |
| 1104 | return openssl_hmac_vector(EVP_md5(), key ,key_len, num_elem, addr, len, |
| 1105 | mac, 16); |
| 1106 | } |
| 1107 | |
| 1108 | |
| 1109 | int hmac_md5(const u8 *key, size_t key_len, const u8 *data, size_t data_len, |
| 1110 | u8 *mac) |
| 1111 | { |
| 1112 | return hmac_md5_vector(key, key_len, 1, &data, &data_len, mac); |
| 1113 | } |
| 1114 | |
| 1115 | #endif /* CONFIG_FIPS */ |
| 1116 | |
| 1117 | |
| 1118 | int pbkdf2_sha1(const char *passphrase, const u8 *ssid, size_t ssid_len, |
| 1119 | int iterations, u8 *buf, size_t buflen) |
| 1120 | { |
| 1121 | if (PKCS5_PBKDF2_HMAC_SHA1(passphrase, os_strlen(passphrase), ssid, |
| 1122 | ssid_len, iterations, buflen, buf) != 1) |
| 1123 | return -1; |
| 1124 | return 0; |
| 1125 | } |
| 1126 | |
| 1127 | |
| 1128 | int hmac_sha1_vector(const u8 *key, size_t key_len, size_t num_elem, |
| 1129 | const u8 *addr[], const size_t *len, u8 *mac) |
| 1130 | { |
| 1131 | return openssl_hmac_vector(EVP_sha1(), key, key_len, num_elem, addr, |
| 1132 | len, mac, 20); |
| 1133 | } |
| 1134 | |
| 1135 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1136 | int hmac_sha1(const u8 *key, size_t key_len, const u8 *data, size_t data_len, |
| 1137 | u8 *mac) |
| 1138 | { |
| 1139 | return hmac_sha1_vector(key, key_len, 1, &data, &data_len, mac); |
| 1140 | } |
| 1141 | |
| 1142 | |
| 1143 | #ifdef CONFIG_SHA256 |
| 1144 | |
| 1145 | int hmac_sha256_vector(const u8 *key, size_t key_len, size_t num_elem, |
| 1146 | const u8 *addr[], const size_t *len, u8 *mac) |
| 1147 | { |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 1148 | return openssl_hmac_vector(EVP_sha256(), key, key_len, num_elem, addr, |
| 1149 | len, mac, 32); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1150 | } |
| 1151 | |
| 1152 | |
| 1153 | int hmac_sha256(const u8 *key, size_t key_len, const u8 *data, |
| 1154 | size_t data_len, u8 *mac) |
| 1155 | { |
| 1156 | return hmac_sha256_vector(key, key_len, 1, &data, &data_len, mac); |
| 1157 | } |
| 1158 | |
| 1159 | #endif /* CONFIG_SHA256 */ |
| 1160 | |
| 1161 | |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 1162 | #ifdef CONFIG_SHA384 |
| 1163 | |
| 1164 | int hmac_sha384_vector(const u8 *key, size_t key_len, size_t num_elem, |
| 1165 | const u8 *addr[], const size_t *len, u8 *mac) |
| 1166 | { |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 1167 | return openssl_hmac_vector(EVP_sha384(), key, key_len, num_elem, addr, |
Dmitry Shmidt | ebd93af | 2017-02-21 13:40:44 -0800 | [diff] [blame] | 1168 | len, mac, 48); |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 1169 | } |
| 1170 | |
| 1171 | |
| 1172 | int hmac_sha384(const u8 *key, size_t key_len, const u8 *data, |
| 1173 | size_t data_len, u8 *mac) |
| 1174 | { |
| 1175 | return hmac_sha384_vector(key, key_len, 1, &data, &data_len, mac); |
| 1176 | } |
| 1177 | |
| 1178 | #endif /* CONFIG_SHA384 */ |
| 1179 | |
| 1180 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 1181 | #ifdef CONFIG_SHA512 |
| 1182 | |
| 1183 | int hmac_sha512_vector(const u8 *key, size_t key_len, size_t num_elem, |
| 1184 | const u8 *addr[], const size_t *len, u8 *mac) |
| 1185 | { |
| 1186 | return openssl_hmac_vector(EVP_sha512(), key, key_len, num_elem, addr, |
| 1187 | len, mac, 64); |
| 1188 | } |
| 1189 | |
| 1190 | |
| 1191 | int hmac_sha512(const u8 *key, size_t key_len, const u8 *data, |
| 1192 | size_t data_len, u8 *mac) |
| 1193 | { |
| 1194 | return hmac_sha512_vector(key, key_len, 1, &data, &data_len, mac); |
| 1195 | } |
| 1196 | |
| 1197 | #endif /* CONFIG_SHA512 */ |
| 1198 | |
| 1199 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1200 | int crypto_get_random(void *buf, size_t len) |
| 1201 | { |
| 1202 | if (RAND_bytes(buf, len) != 1) |
| 1203 | return -1; |
| 1204 | return 0; |
| 1205 | } |
| 1206 | |
| 1207 | |
| 1208 | #ifdef CONFIG_OPENSSL_CMAC |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 1209 | int omac1_aes_vector(const u8 *key, size_t key_len, size_t num_elem, |
| 1210 | const u8 *addr[], const size_t *len, u8 *mac) |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1211 | { |
| 1212 | CMAC_CTX *ctx; |
| 1213 | int ret = -1; |
| 1214 | size_t outlen, i; |
| 1215 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1216 | if (TEST_FAIL()) |
| 1217 | return -1; |
| 1218 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1219 | ctx = CMAC_CTX_new(); |
| 1220 | if (ctx == NULL) |
| 1221 | return -1; |
| 1222 | |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 1223 | if (key_len == 32) { |
| 1224 | if (!CMAC_Init(ctx, key, 32, EVP_aes_256_cbc(), NULL)) |
| 1225 | goto fail; |
| 1226 | } else if (key_len == 16) { |
| 1227 | if (!CMAC_Init(ctx, key, 16, EVP_aes_128_cbc(), NULL)) |
| 1228 | goto fail; |
| 1229 | } else { |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1230 | goto fail; |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 1231 | } |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1232 | for (i = 0; i < num_elem; i++) { |
| 1233 | if (!CMAC_Update(ctx, addr[i], len[i])) |
| 1234 | goto fail; |
| 1235 | } |
| 1236 | if (!CMAC_Final(ctx, mac, &outlen) || outlen != 16) |
| 1237 | goto fail; |
| 1238 | |
| 1239 | ret = 0; |
| 1240 | fail: |
| 1241 | CMAC_CTX_free(ctx); |
| 1242 | return ret; |
| 1243 | } |
| 1244 | |
| 1245 | |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 1246 | int omac1_aes_128_vector(const u8 *key, size_t num_elem, |
| 1247 | const u8 *addr[], const size_t *len, u8 *mac) |
| 1248 | { |
| 1249 | return omac1_aes_vector(key, 16, num_elem, addr, len, mac); |
| 1250 | } |
| 1251 | |
| 1252 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1253 | int omac1_aes_128(const u8 *key, const u8 *data, size_t data_len, u8 *mac) |
| 1254 | { |
| 1255 | return omac1_aes_128_vector(key, 1, &data, &data_len, mac); |
| 1256 | } |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 1257 | |
| 1258 | |
| 1259 | int omac1_aes_256(const u8 *key, const u8 *data, size_t data_len, u8 *mac) |
| 1260 | { |
| 1261 | return omac1_aes_vector(key, 32, 1, &data, &data_len, mac); |
| 1262 | } |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1263 | #endif /* CONFIG_OPENSSL_CMAC */ |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1264 | |
| 1265 | |
| 1266 | struct crypto_bignum * crypto_bignum_init(void) |
| 1267 | { |
Dmitry Shmidt | e466304 | 2016-04-04 10:07:49 -0700 | [diff] [blame] | 1268 | if (TEST_FAIL()) |
| 1269 | return NULL; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1270 | return (struct crypto_bignum *) BN_new(); |
| 1271 | } |
| 1272 | |
| 1273 | |
| 1274 | struct crypto_bignum * crypto_bignum_init_set(const u8 *buf, size_t len) |
| 1275 | { |
Dmitry Shmidt | e466304 | 2016-04-04 10:07:49 -0700 | [diff] [blame] | 1276 | BIGNUM *bn; |
| 1277 | |
| 1278 | if (TEST_FAIL()) |
| 1279 | return NULL; |
| 1280 | |
| 1281 | bn = BN_bin2bn(buf, len, NULL); |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1282 | return (struct crypto_bignum *) bn; |
| 1283 | } |
| 1284 | |
| 1285 | |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame^] | 1286 | struct crypto_bignum * crypto_bignum_init_uint(unsigned int val) |
| 1287 | { |
| 1288 | BIGNUM *bn; |
| 1289 | |
| 1290 | if (TEST_FAIL()) |
| 1291 | return NULL; |
| 1292 | |
| 1293 | bn = BN_new(); |
| 1294 | if (!bn) |
| 1295 | return NULL; |
| 1296 | if (BN_set_word(bn, val) != 1) { |
| 1297 | BN_free(bn); |
| 1298 | return NULL; |
| 1299 | } |
| 1300 | return (struct crypto_bignum *) bn; |
| 1301 | } |
| 1302 | |
| 1303 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1304 | void crypto_bignum_deinit(struct crypto_bignum *n, int clear) |
| 1305 | { |
| 1306 | if (clear) |
| 1307 | BN_clear_free((BIGNUM *) n); |
| 1308 | else |
| 1309 | BN_free((BIGNUM *) n); |
| 1310 | } |
| 1311 | |
| 1312 | |
| 1313 | int crypto_bignum_to_bin(const struct crypto_bignum *a, |
| 1314 | u8 *buf, size_t buflen, size_t padlen) |
| 1315 | { |
| 1316 | int num_bytes, offset; |
| 1317 | |
Dmitry Shmidt | e466304 | 2016-04-04 10:07:49 -0700 | [diff] [blame] | 1318 | if (TEST_FAIL()) |
| 1319 | return -1; |
| 1320 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1321 | if (padlen > buflen) |
| 1322 | return -1; |
| 1323 | |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame^] | 1324 | if (padlen) { |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 1325 | #ifdef OPENSSL_IS_BORINGSSL |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame^] | 1326 | if (BN_bn2bin_padded(buf, padlen, (const BIGNUM *) a) == 0) |
| 1327 | return -1; |
| 1328 | return padlen; |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 1329 | #else /* OPENSSL_IS_BORINGSSL */ |
| 1330 | #if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER) |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame^] | 1331 | return BN_bn2binpad((const BIGNUM *) a, buf, padlen); |
| 1332 | #endif |
| 1333 | #endif |
| 1334 | } |
| 1335 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1336 | num_bytes = BN_num_bytes((const BIGNUM *) a); |
| 1337 | if ((size_t) num_bytes > buflen) |
| 1338 | return -1; |
| 1339 | if (padlen > (size_t) num_bytes) |
| 1340 | offset = padlen - num_bytes; |
| 1341 | else |
| 1342 | offset = 0; |
| 1343 | |
| 1344 | os_memset(buf, 0, offset); |
| 1345 | BN_bn2bin((const BIGNUM *) a, buf + offset); |
| 1346 | |
| 1347 | return num_bytes + offset; |
| 1348 | } |
| 1349 | |
| 1350 | |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 1351 | int crypto_bignum_rand(struct crypto_bignum *r, const struct crypto_bignum *m) |
| 1352 | { |
Hai Shalom | 5f92bc9 | 2019-04-18 11:54:11 -0700 | [diff] [blame] | 1353 | if (TEST_FAIL()) |
| 1354 | return -1; |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 1355 | return BN_rand_range((BIGNUM *) r, (const BIGNUM *) m) == 1 ? 0 : -1; |
| 1356 | } |
| 1357 | |
| 1358 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1359 | int crypto_bignum_add(const struct crypto_bignum *a, |
| 1360 | const struct crypto_bignum *b, |
| 1361 | struct crypto_bignum *c) |
| 1362 | { |
| 1363 | return BN_add((BIGNUM *) c, (const BIGNUM *) a, (const BIGNUM *) b) ? |
| 1364 | 0 : -1; |
| 1365 | } |
| 1366 | |
| 1367 | |
| 1368 | int crypto_bignum_mod(const struct crypto_bignum *a, |
| 1369 | const struct crypto_bignum *b, |
| 1370 | struct crypto_bignum *c) |
| 1371 | { |
| 1372 | int res; |
| 1373 | BN_CTX *bnctx; |
| 1374 | |
| 1375 | bnctx = BN_CTX_new(); |
| 1376 | if (bnctx == NULL) |
| 1377 | return -1; |
| 1378 | res = BN_mod((BIGNUM *) c, (const BIGNUM *) a, (const BIGNUM *) b, |
| 1379 | bnctx); |
| 1380 | BN_CTX_free(bnctx); |
| 1381 | |
| 1382 | return res ? 0 : -1; |
| 1383 | } |
| 1384 | |
| 1385 | |
| 1386 | int crypto_bignum_exptmod(const struct crypto_bignum *a, |
| 1387 | const struct crypto_bignum *b, |
| 1388 | const struct crypto_bignum *c, |
| 1389 | struct crypto_bignum *d) |
| 1390 | { |
| 1391 | int res; |
| 1392 | BN_CTX *bnctx; |
| 1393 | |
Dmitry Shmidt | e466304 | 2016-04-04 10:07:49 -0700 | [diff] [blame] | 1394 | if (TEST_FAIL()) |
| 1395 | return -1; |
| 1396 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1397 | bnctx = BN_CTX_new(); |
| 1398 | if (bnctx == NULL) |
| 1399 | return -1; |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 1400 | res = BN_mod_exp_mont_consttime((BIGNUM *) d, (const BIGNUM *) a, |
| 1401 | (const BIGNUM *) b, (const BIGNUM *) c, |
| 1402 | bnctx, NULL); |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1403 | BN_CTX_free(bnctx); |
| 1404 | |
| 1405 | return res ? 0 : -1; |
| 1406 | } |
| 1407 | |
| 1408 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1409 | int crypto_bignum_inverse(const struct crypto_bignum *a, |
| 1410 | const struct crypto_bignum *b, |
| 1411 | struct crypto_bignum *c) |
| 1412 | { |
| 1413 | BIGNUM *res; |
| 1414 | BN_CTX *bnctx; |
| 1415 | |
Dmitry Shmidt | e466304 | 2016-04-04 10:07:49 -0700 | [diff] [blame] | 1416 | if (TEST_FAIL()) |
| 1417 | return -1; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1418 | bnctx = BN_CTX_new(); |
| 1419 | if (bnctx == NULL) |
| 1420 | return -1; |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 1421 | #ifdef OPENSSL_IS_BORINGSSL |
| 1422 | /* TODO: use BN_mod_inverse_blinded() ? */ |
| 1423 | #else /* OPENSSL_IS_BORINGSSL */ |
| 1424 | BN_set_flags((BIGNUM *) a, BN_FLG_CONSTTIME); |
| 1425 | #endif /* OPENSSL_IS_BORINGSSL */ |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1426 | res = BN_mod_inverse((BIGNUM *) c, (const BIGNUM *) a, |
| 1427 | (const BIGNUM *) b, bnctx); |
| 1428 | BN_CTX_free(bnctx); |
| 1429 | |
| 1430 | return res ? 0 : -1; |
| 1431 | } |
| 1432 | |
| 1433 | |
| 1434 | int crypto_bignum_sub(const struct crypto_bignum *a, |
| 1435 | const struct crypto_bignum *b, |
| 1436 | struct crypto_bignum *c) |
| 1437 | { |
Dmitry Shmidt | e466304 | 2016-04-04 10:07:49 -0700 | [diff] [blame] | 1438 | if (TEST_FAIL()) |
| 1439 | return -1; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1440 | return BN_sub((BIGNUM *) c, (const BIGNUM *) a, (const BIGNUM *) b) ? |
| 1441 | 0 : -1; |
| 1442 | } |
| 1443 | |
| 1444 | |
| 1445 | int crypto_bignum_div(const struct crypto_bignum *a, |
| 1446 | const struct crypto_bignum *b, |
| 1447 | struct crypto_bignum *c) |
| 1448 | { |
| 1449 | int res; |
| 1450 | |
| 1451 | BN_CTX *bnctx; |
| 1452 | |
Dmitry Shmidt | e466304 | 2016-04-04 10:07:49 -0700 | [diff] [blame] | 1453 | if (TEST_FAIL()) |
| 1454 | return -1; |
| 1455 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1456 | bnctx = BN_CTX_new(); |
| 1457 | if (bnctx == NULL) |
| 1458 | return -1; |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 1459 | #ifndef OPENSSL_IS_BORINGSSL |
| 1460 | BN_set_flags((BIGNUM *) a, BN_FLG_CONSTTIME); |
| 1461 | #endif /* OPENSSL_IS_BORINGSSL */ |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1462 | res = BN_div((BIGNUM *) c, NULL, (const BIGNUM *) a, |
| 1463 | (const BIGNUM *) b, bnctx); |
| 1464 | BN_CTX_free(bnctx); |
| 1465 | |
| 1466 | return res ? 0 : -1; |
| 1467 | } |
| 1468 | |
| 1469 | |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame^] | 1470 | int crypto_bignum_addmod(const struct crypto_bignum *a, |
| 1471 | const struct crypto_bignum *b, |
| 1472 | const struct crypto_bignum *c, |
| 1473 | struct crypto_bignum *d) |
| 1474 | { |
| 1475 | int res; |
| 1476 | BN_CTX *bnctx; |
| 1477 | |
| 1478 | if (TEST_FAIL()) |
| 1479 | return -1; |
| 1480 | |
| 1481 | bnctx = BN_CTX_new(); |
| 1482 | if (!bnctx) |
| 1483 | return -1; |
| 1484 | res = BN_mod_add((BIGNUM *) d, (const BIGNUM *) a, (const BIGNUM *) b, |
| 1485 | (const BIGNUM *) c, bnctx); |
| 1486 | BN_CTX_free(bnctx); |
| 1487 | |
| 1488 | return res ? 0 : -1; |
| 1489 | } |
| 1490 | |
| 1491 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1492 | int crypto_bignum_mulmod(const struct crypto_bignum *a, |
| 1493 | const struct crypto_bignum *b, |
| 1494 | const struct crypto_bignum *c, |
| 1495 | struct crypto_bignum *d) |
| 1496 | { |
| 1497 | int res; |
| 1498 | |
| 1499 | BN_CTX *bnctx; |
| 1500 | |
Dmitry Shmidt | e466304 | 2016-04-04 10:07:49 -0700 | [diff] [blame] | 1501 | if (TEST_FAIL()) |
| 1502 | return -1; |
| 1503 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1504 | bnctx = BN_CTX_new(); |
| 1505 | if (bnctx == NULL) |
| 1506 | return -1; |
| 1507 | res = BN_mod_mul((BIGNUM *) d, (const BIGNUM *) a, (const BIGNUM *) b, |
| 1508 | (const BIGNUM *) c, bnctx); |
| 1509 | BN_CTX_free(bnctx); |
| 1510 | |
| 1511 | return res ? 0 : -1; |
| 1512 | } |
| 1513 | |
| 1514 | |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame^] | 1515 | int crypto_bignum_sqrmod(const struct crypto_bignum *a, |
| 1516 | const struct crypto_bignum *b, |
| 1517 | struct crypto_bignum *c) |
| 1518 | { |
| 1519 | int res; |
| 1520 | BN_CTX *bnctx; |
| 1521 | |
| 1522 | if (TEST_FAIL()) |
| 1523 | return -1; |
| 1524 | |
| 1525 | bnctx = BN_CTX_new(); |
| 1526 | if (!bnctx) |
| 1527 | return -1; |
| 1528 | res = BN_mod_sqr((BIGNUM *) c, (const BIGNUM *) a, (const BIGNUM *) b, |
| 1529 | bnctx); |
| 1530 | BN_CTX_free(bnctx); |
| 1531 | |
| 1532 | return res ? 0 : -1; |
| 1533 | } |
| 1534 | |
| 1535 | |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 1536 | int crypto_bignum_rshift(const struct crypto_bignum *a, int n, |
| 1537 | struct crypto_bignum *r) |
| 1538 | { |
| 1539 | /* Note: BN_rshift() does not modify the first argument even though it |
| 1540 | * has not been marked const. */ |
| 1541 | return BN_rshift((BIGNUM *) a, (BIGNUM *) r, n) == 1 ? 0 : -1; |
| 1542 | } |
| 1543 | |
| 1544 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1545 | int crypto_bignum_cmp(const struct crypto_bignum *a, |
| 1546 | const struct crypto_bignum *b) |
| 1547 | { |
| 1548 | return BN_cmp((const BIGNUM *) a, (const BIGNUM *) b); |
| 1549 | } |
| 1550 | |
| 1551 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1552 | int crypto_bignum_is_zero(const struct crypto_bignum *a) |
| 1553 | { |
| 1554 | return BN_is_zero((const BIGNUM *) a); |
| 1555 | } |
| 1556 | |
| 1557 | |
| 1558 | int crypto_bignum_is_one(const struct crypto_bignum *a) |
| 1559 | { |
| 1560 | return BN_is_one((const BIGNUM *) a); |
| 1561 | } |
| 1562 | |
| 1563 | |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 1564 | int crypto_bignum_is_odd(const struct crypto_bignum *a) |
| 1565 | { |
| 1566 | return BN_is_odd((const BIGNUM *) a); |
| 1567 | } |
| 1568 | |
| 1569 | |
Dmitry Shmidt | 4171258 | 2015-06-29 11:02:15 -0700 | [diff] [blame] | 1570 | int crypto_bignum_legendre(const struct crypto_bignum *a, |
| 1571 | const struct crypto_bignum *p) |
| 1572 | { |
| 1573 | BN_CTX *bnctx; |
| 1574 | BIGNUM *exp = NULL, *tmp = NULL; |
| 1575 | int res = -2; |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 1576 | unsigned int mask; |
Dmitry Shmidt | 4171258 | 2015-06-29 11:02:15 -0700 | [diff] [blame] | 1577 | |
Dmitry Shmidt | e466304 | 2016-04-04 10:07:49 -0700 | [diff] [blame] | 1578 | if (TEST_FAIL()) |
| 1579 | return -2; |
| 1580 | |
Dmitry Shmidt | 4171258 | 2015-06-29 11:02:15 -0700 | [diff] [blame] | 1581 | bnctx = BN_CTX_new(); |
| 1582 | if (bnctx == NULL) |
| 1583 | return -2; |
| 1584 | |
| 1585 | exp = BN_new(); |
| 1586 | tmp = BN_new(); |
| 1587 | if (!exp || !tmp || |
| 1588 | /* exp = (p-1) / 2 */ |
| 1589 | !BN_sub(exp, (const BIGNUM *) p, BN_value_one()) || |
| 1590 | !BN_rshift1(exp, exp) || |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 1591 | !BN_mod_exp_mont_consttime(tmp, (const BIGNUM *) a, exp, |
| 1592 | (const BIGNUM *) p, bnctx, NULL)) |
Dmitry Shmidt | 4171258 | 2015-06-29 11:02:15 -0700 | [diff] [blame] | 1593 | goto fail; |
| 1594 | |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 1595 | /* Return 1 if tmp == 1, 0 if tmp == 0, or -1 otherwise. Need to use |
| 1596 | * constant time selection to avoid branches here. */ |
| 1597 | res = -1; |
| 1598 | mask = const_time_eq(BN_is_word(tmp, 1), 1); |
| 1599 | res = const_time_select_int(mask, 1, res); |
| 1600 | mask = const_time_eq(BN_is_zero(tmp), 1); |
| 1601 | res = const_time_select_int(mask, 0, res); |
Dmitry Shmidt | 4171258 | 2015-06-29 11:02:15 -0700 | [diff] [blame] | 1602 | |
| 1603 | fail: |
| 1604 | BN_clear_free(tmp); |
| 1605 | BN_clear_free(exp); |
| 1606 | BN_CTX_free(bnctx); |
| 1607 | return res; |
| 1608 | } |
| 1609 | |
| 1610 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1611 | #ifdef CONFIG_ECC |
| 1612 | |
| 1613 | struct crypto_ec { |
| 1614 | EC_GROUP *group; |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 1615 | int nid; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1616 | BN_CTX *bnctx; |
| 1617 | BIGNUM *prime; |
| 1618 | BIGNUM *order; |
Dmitry Shmidt | 4171258 | 2015-06-29 11:02:15 -0700 | [diff] [blame] | 1619 | BIGNUM *a; |
| 1620 | BIGNUM *b; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1621 | }; |
| 1622 | |
| 1623 | struct crypto_ec * crypto_ec_init(int group) |
| 1624 | { |
| 1625 | struct crypto_ec *e; |
| 1626 | int nid; |
| 1627 | |
| 1628 | /* Map from IANA registry for IKE D-H groups to OpenSSL NID */ |
| 1629 | switch (group) { |
| 1630 | case 19: |
| 1631 | nid = NID_X9_62_prime256v1; |
| 1632 | break; |
| 1633 | case 20: |
| 1634 | nid = NID_secp384r1; |
| 1635 | break; |
| 1636 | case 21: |
| 1637 | nid = NID_secp521r1; |
| 1638 | break; |
| 1639 | case 25: |
| 1640 | nid = NID_X9_62_prime192v1; |
| 1641 | break; |
| 1642 | case 26: |
| 1643 | nid = NID_secp224r1; |
| 1644 | break; |
Dmitry Shmidt | 4171258 | 2015-06-29 11:02:15 -0700 | [diff] [blame] | 1645 | #ifdef NID_brainpoolP224r1 |
| 1646 | case 27: |
| 1647 | nid = NID_brainpoolP224r1; |
| 1648 | break; |
| 1649 | #endif /* NID_brainpoolP224r1 */ |
| 1650 | #ifdef NID_brainpoolP256r1 |
| 1651 | case 28: |
| 1652 | nid = NID_brainpoolP256r1; |
| 1653 | break; |
| 1654 | #endif /* NID_brainpoolP256r1 */ |
| 1655 | #ifdef NID_brainpoolP384r1 |
| 1656 | case 29: |
| 1657 | nid = NID_brainpoolP384r1; |
| 1658 | break; |
| 1659 | #endif /* NID_brainpoolP384r1 */ |
| 1660 | #ifdef NID_brainpoolP512r1 |
| 1661 | case 30: |
| 1662 | nid = NID_brainpoolP512r1; |
| 1663 | break; |
| 1664 | #endif /* NID_brainpoolP512r1 */ |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1665 | default: |
| 1666 | return NULL; |
| 1667 | } |
| 1668 | |
| 1669 | e = os_zalloc(sizeof(*e)); |
| 1670 | if (e == NULL) |
| 1671 | return NULL; |
| 1672 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 1673 | e->nid = nid; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1674 | e->bnctx = BN_CTX_new(); |
| 1675 | e->group = EC_GROUP_new_by_curve_name(nid); |
| 1676 | e->prime = BN_new(); |
| 1677 | e->order = BN_new(); |
Dmitry Shmidt | 4171258 | 2015-06-29 11:02:15 -0700 | [diff] [blame] | 1678 | e->a = BN_new(); |
| 1679 | e->b = BN_new(); |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1680 | if (e->group == NULL || e->bnctx == NULL || e->prime == NULL || |
Dmitry Shmidt | 4171258 | 2015-06-29 11:02:15 -0700 | [diff] [blame] | 1681 | e->order == NULL || e->a == NULL || e->b == NULL || |
| 1682 | !EC_GROUP_get_curve_GFp(e->group, e->prime, e->a, e->b, e->bnctx) || |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1683 | !EC_GROUP_get_order(e->group, e->order, e->bnctx)) { |
| 1684 | crypto_ec_deinit(e); |
| 1685 | e = NULL; |
| 1686 | } |
| 1687 | |
| 1688 | return e; |
| 1689 | } |
| 1690 | |
| 1691 | |
| 1692 | void crypto_ec_deinit(struct crypto_ec *e) |
| 1693 | { |
| 1694 | if (e == NULL) |
| 1695 | return; |
Dmitry Shmidt | 4171258 | 2015-06-29 11:02:15 -0700 | [diff] [blame] | 1696 | BN_clear_free(e->b); |
| 1697 | BN_clear_free(e->a); |
Dmitry Shmidt | 7f0b69e | 2014-07-28 10:35:20 -0700 | [diff] [blame] | 1698 | BN_clear_free(e->order); |
Dmitry Shmidt | 661b4f7 | 2014-09-29 14:58:27 -0700 | [diff] [blame] | 1699 | BN_clear_free(e->prime); |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1700 | EC_GROUP_free(e->group); |
| 1701 | BN_CTX_free(e->bnctx); |
| 1702 | os_free(e); |
| 1703 | } |
| 1704 | |
| 1705 | |
| 1706 | struct crypto_ec_point * crypto_ec_point_init(struct crypto_ec *e) |
| 1707 | { |
Dmitry Shmidt | e466304 | 2016-04-04 10:07:49 -0700 | [diff] [blame] | 1708 | if (TEST_FAIL()) |
| 1709 | return NULL; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1710 | if (e == NULL) |
| 1711 | return NULL; |
| 1712 | return (struct crypto_ec_point *) EC_POINT_new(e->group); |
| 1713 | } |
| 1714 | |
| 1715 | |
| 1716 | size_t crypto_ec_prime_len(struct crypto_ec *e) |
| 1717 | { |
| 1718 | return BN_num_bytes(e->prime); |
| 1719 | } |
| 1720 | |
| 1721 | |
| 1722 | size_t crypto_ec_prime_len_bits(struct crypto_ec *e) |
| 1723 | { |
| 1724 | return BN_num_bits(e->prime); |
| 1725 | } |
| 1726 | |
| 1727 | |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 1728 | size_t crypto_ec_order_len(struct crypto_ec *e) |
| 1729 | { |
| 1730 | return BN_num_bytes(e->order); |
| 1731 | } |
| 1732 | |
| 1733 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1734 | const struct crypto_bignum * crypto_ec_get_prime(struct crypto_ec *e) |
| 1735 | { |
| 1736 | return (const struct crypto_bignum *) e->prime; |
| 1737 | } |
| 1738 | |
| 1739 | |
| 1740 | const struct crypto_bignum * crypto_ec_get_order(struct crypto_ec *e) |
| 1741 | { |
| 1742 | return (const struct crypto_bignum *) e->order; |
| 1743 | } |
| 1744 | |
| 1745 | |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame^] | 1746 | const struct crypto_bignum * crypto_ec_get_a(struct crypto_ec *e) |
| 1747 | { |
| 1748 | return (const struct crypto_bignum *) e->a; |
| 1749 | } |
| 1750 | |
| 1751 | |
| 1752 | const struct crypto_bignum * crypto_ec_get_b(struct crypto_ec *e) |
| 1753 | { |
| 1754 | return (const struct crypto_bignum *) e->b; |
| 1755 | } |
| 1756 | |
| 1757 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1758 | void crypto_ec_point_deinit(struct crypto_ec_point *p, int clear) |
| 1759 | { |
| 1760 | if (clear) |
| 1761 | EC_POINT_clear_free((EC_POINT *) p); |
| 1762 | else |
| 1763 | EC_POINT_free((EC_POINT *) p); |
| 1764 | } |
| 1765 | |
| 1766 | |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 1767 | int crypto_ec_point_x(struct crypto_ec *e, const struct crypto_ec_point *p, |
| 1768 | struct crypto_bignum *x) |
| 1769 | { |
| 1770 | return EC_POINT_get_affine_coordinates_GFp(e->group, |
| 1771 | (const EC_POINT *) p, |
| 1772 | (BIGNUM *) x, NULL, |
| 1773 | e->bnctx) == 1 ? 0 : -1; |
| 1774 | } |
| 1775 | |
| 1776 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1777 | int crypto_ec_point_to_bin(struct crypto_ec *e, |
| 1778 | const struct crypto_ec_point *point, u8 *x, u8 *y) |
| 1779 | { |
| 1780 | BIGNUM *x_bn, *y_bn; |
| 1781 | int ret = -1; |
| 1782 | int len = BN_num_bytes(e->prime); |
| 1783 | |
Dmitry Shmidt | e466304 | 2016-04-04 10:07:49 -0700 | [diff] [blame] | 1784 | if (TEST_FAIL()) |
| 1785 | return -1; |
| 1786 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1787 | x_bn = BN_new(); |
| 1788 | y_bn = BN_new(); |
| 1789 | |
| 1790 | if (x_bn && y_bn && |
| 1791 | EC_POINT_get_affine_coordinates_GFp(e->group, (EC_POINT *) point, |
| 1792 | x_bn, y_bn, e->bnctx)) { |
| 1793 | if (x) { |
| 1794 | crypto_bignum_to_bin((struct crypto_bignum *) x_bn, |
| 1795 | x, len, len); |
| 1796 | } |
| 1797 | if (y) { |
| 1798 | crypto_bignum_to_bin((struct crypto_bignum *) y_bn, |
| 1799 | y, len, len); |
| 1800 | } |
| 1801 | ret = 0; |
| 1802 | } |
| 1803 | |
Dmitry Shmidt | 7f0b69e | 2014-07-28 10:35:20 -0700 | [diff] [blame] | 1804 | BN_clear_free(x_bn); |
| 1805 | BN_clear_free(y_bn); |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1806 | return ret; |
| 1807 | } |
| 1808 | |
| 1809 | |
| 1810 | struct crypto_ec_point * crypto_ec_point_from_bin(struct crypto_ec *e, |
| 1811 | const u8 *val) |
| 1812 | { |
| 1813 | BIGNUM *x, *y; |
| 1814 | EC_POINT *elem; |
| 1815 | int len = BN_num_bytes(e->prime); |
| 1816 | |
Dmitry Shmidt | e466304 | 2016-04-04 10:07:49 -0700 | [diff] [blame] | 1817 | if (TEST_FAIL()) |
| 1818 | return NULL; |
| 1819 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1820 | x = BN_bin2bn(val, len, NULL); |
| 1821 | y = BN_bin2bn(val + len, len, NULL); |
| 1822 | elem = EC_POINT_new(e->group); |
| 1823 | if (x == NULL || y == NULL || elem == NULL) { |
Dmitry Shmidt | 7f0b69e | 2014-07-28 10:35:20 -0700 | [diff] [blame] | 1824 | BN_clear_free(x); |
| 1825 | BN_clear_free(y); |
| 1826 | EC_POINT_clear_free(elem); |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1827 | return NULL; |
| 1828 | } |
| 1829 | |
| 1830 | if (!EC_POINT_set_affine_coordinates_GFp(e->group, elem, x, y, |
| 1831 | e->bnctx)) { |
Dmitry Shmidt | 7f0b69e | 2014-07-28 10:35:20 -0700 | [diff] [blame] | 1832 | EC_POINT_clear_free(elem); |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1833 | elem = NULL; |
| 1834 | } |
| 1835 | |
Dmitry Shmidt | 7f0b69e | 2014-07-28 10:35:20 -0700 | [diff] [blame] | 1836 | BN_clear_free(x); |
| 1837 | BN_clear_free(y); |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1838 | |
| 1839 | return (struct crypto_ec_point *) elem; |
| 1840 | } |
| 1841 | |
| 1842 | |
| 1843 | int crypto_ec_point_add(struct crypto_ec *e, const struct crypto_ec_point *a, |
| 1844 | const struct crypto_ec_point *b, |
| 1845 | struct crypto_ec_point *c) |
| 1846 | { |
Dmitry Shmidt | e466304 | 2016-04-04 10:07:49 -0700 | [diff] [blame] | 1847 | if (TEST_FAIL()) |
| 1848 | return -1; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1849 | return EC_POINT_add(e->group, (EC_POINT *) c, (const EC_POINT *) a, |
| 1850 | (const EC_POINT *) b, e->bnctx) ? 0 : -1; |
| 1851 | } |
| 1852 | |
| 1853 | |
| 1854 | int crypto_ec_point_mul(struct crypto_ec *e, const struct crypto_ec_point *p, |
| 1855 | const struct crypto_bignum *b, |
| 1856 | struct crypto_ec_point *res) |
| 1857 | { |
Dmitry Shmidt | e466304 | 2016-04-04 10:07:49 -0700 | [diff] [blame] | 1858 | if (TEST_FAIL()) |
| 1859 | return -1; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1860 | return EC_POINT_mul(e->group, (EC_POINT *) res, NULL, |
| 1861 | (const EC_POINT *) p, (const BIGNUM *) b, e->bnctx) |
| 1862 | ? 0 : -1; |
| 1863 | } |
| 1864 | |
| 1865 | |
| 1866 | int crypto_ec_point_invert(struct crypto_ec *e, struct crypto_ec_point *p) |
| 1867 | { |
Dmitry Shmidt | e466304 | 2016-04-04 10:07:49 -0700 | [diff] [blame] | 1868 | if (TEST_FAIL()) |
| 1869 | return -1; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1870 | return EC_POINT_invert(e->group, (EC_POINT *) p, e->bnctx) ? 0 : -1; |
| 1871 | } |
| 1872 | |
| 1873 | |
| 1874 | int crypto_ec_point_solve_y_coord(struct crypto_ec *e, |
| 1875 | struct crypto_ec_point *p, |
| 1876 | const struct crypto_bignum *x, int y_bit) |
| 1877 | { |
Dmitry Shmidt | e466304 | 2016-04-04 10:07:49 -0700 | [diff] [blame] | 1878 | if (TEST_FAIL()) |
| 1879 | return -1; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1880 | if (!EC_POINT_set_compressed_coordinates_GFp(e->group, (EC_POINT *) p, |
| 1881 | (const BIGNUM *) x, y_bit, |
| 1882 | e->bnctx) || |
| 1883 | !EC_POINT_is_on_curve(e->group, (EC_POINT *) p, e->bnctx)) |
| 1884 | return -1; |
| 1885 | return 0; |
| 1886 | } |
| 1887 | |
| 1888 | |
Dmitry Shmidt | 4171258 | 2015-06-29 11:02:15 -0700 | [diff] [blame] | 1889 | struct crypto_bignum * |
| 1890 | crypto_ec_point_compute_y_sqr(struct crypto_ec *e, |
| 1891 | const struct crypto_bignum *x) |
| 1892 | { |
| 1893 | BIGNUM *tmp, *tmp2, *y_sqr = NULL; |
| 1894 | |
Dmitry Shmidt | e466304 | 2016-04-04 10:07:49 -0700 | [diff] [blame] | 1895 | if (TEST_FAIL()) |
| 1896 | return NULL; |
| 1897 | |
Dmitry Shmidt | 4171258 | 2015-06-29 11:02:15 -0700 | [diff] [blame] | 1898 | tmp = BN_new(); |
| 1899 | tmp2 = BN_new(); |
| 1900 | |
| 1901 | /* y^2 = x^3 + ax + b */ |
| 1902 | if (tmp && tmp2 && |
| 1903 | BN_mod_sqr(tmp, (const BIGNUM *) x, e->prime, e->bnctx) && |
| 1904 | BN_mod_mul(tmp, tmp, (const BIGNUM *) x, e->prime, e->bnctx) && |
| 1905 | BN_mod_mul(tmp2, e->a, (const BIGNUM *) x, e->prime, e->bnctx) && |
| 1906 | BN_mod_add_quick(tmp2, tmp2, tmp, e->prime) && |
| 1907 | BN_mod_add_quick(tmp2, tmp2, e->b, e->prime)) { |
| 1908 | y_sqr = tmp2; |
| 1909 | tmp2 = NULL; |
| 1910 | } |
| 1911 | |
| 1912 | BN_clear_free(tmp); |
| 1913 | BN_clear_free(tmp2); |
| 1914 | |
| 1915 | return (struct crypto_bignum *) y_sqr; |
| 1916 | } |
| 1917 | |
| 1918 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1919 | int crypto_ec_point_is_at_infinity(struct crypto_ec *e, |
| 1920 | const struct crypto_ec_point *p) |
| 1921 | { |
| 1922 | return EC_POINT_is_at_infinity(e->group, (const EC_POINT *) p); |
| 1923 | } |
| 1924 | |
| 1925 | |
| 1926 | int crypto_ec_point_is_on_curve(struct crypto_ec *e, |
| 1927 | const struct crypto_ec_point *p) |
| 1928 | { |
Dmitry Shmidt | 4171258 | 2015-06-29 11:02:15 -0700 | [diff] [blame] | 1929 | return EC_POINT_is_on_curve(e->group, (const EC_POINT *) p, |
| 1930 | e->bnctx) == 1; |
| 1931 | } |
| 1932 | |
| 1933 | |
| 1934 | int crypto_ec_point_cmp(const struct crypto_ec *e, |
| 1935 | const struct crypto_ec_point *a, |
| 1936 | const struct crypto_ec_point *b) |
| 1937 | { |
| 1938 | return EC_POINT_cmp(e->group, (const EC_POINT *) a, |
| 1939 | (const EC_POINT *) b, e->bnctx); |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1940 | } |
| 1941 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 1942 | |
| 1943 | struct crypto_ecdh { |
| 1944 | struct crypto_ec *ec; |
| 1945 | EVP_PKEY *pkey; |
| 1946 | }; |
| 1947 | |
| 1948 | struct crypto_ecdh * crypto_ecdh_init(int group) |
| 1949 | { |
| 1950 | struct crypto_ecdh *ecdh; |
| 1951 | EVP_PKEY *params = NULL; |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 1952 | EC_KEY *ec_params = NULL; |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 1953 | EVP_PKEY_CTX *kctx = NULL; |
| 1954 | |
| 1955 | ecdh = os_zalloc(sizeof(*ecdh)); |
| 1956 | if (!ecdh) |
| 1957 | goto fail; |
| 1958 | |
| 1959 | ecdh->ec = crypto_ec_init(group); |
| 1960 | if (!ecdh->ec) |
| 1961 | goto fail; |
| 1962 | |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 1963 | ec_params = EC_KEY_new_by_curve_name(ecdh->ec->nid); |
| 1964 | if (!ec_params) { |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 1965 | wpa_printf(MSG_ERROR, |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 1966 | "OpenSSL: Failed to generate EC_KEY parameters"); |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 1967 | goto fail; |
| 1968 | } |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 1969 | EC_KEY_set_asn1_flag(ec_params, OPENSSL_EC_NAMED_CURVE); |
| 1970 | params = EVP_PKEY_new(); |
| 1971 | if (!params || EVP_PKEY_set1_EC_KEY(params, ec_params) != 1) { |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 1972 | wpa_printf(MSG_ERROR, |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 1973 | "OpenSSL: Failed to generate EVP_PKEY parameters"); |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 1974 | goto fail; |
| 1975 | } |
| 1976 | |
| 1977 | kctx = EVP_PKEY_CTX_new(params, NULL); |
| 1978 | if (!kctx) |
| 1979 | goto fail; |
| 1980 | |
| 1981 | if (EVP_PKEY_keygen_init(kctx) != 1) { |
| 1982 | wpa_printf(MSG_ERROR, |
| 1983 | "OpenSSL: EVP_PKEY_keygen_init failed: %s", |
| 1984 | ERR_error_string(ERR_get_error(), NULL)); |
| 1985 | goto fail; |
| 1986 | } |
| 1987 | |
| 1988 | if (EVP_PKEY_keygen(kctx, &ecdh->pkey) != 1) { |
| 1989 | wpa_printf(MSG_ERROR, "OpenSSL: EVP_PKEY_keygen failed: %s", |
| 1990 | ERR_error_string(ERR_get_error(), NULL)); |
| 1991 | goto fail; |
| 1992 | } |
| 1993 | |
| 1994 | done: |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 1995 | EC_KEY_free(ec_params); |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 1996 | EVP_PKEY_free(params); |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 1997 | EVP_PKEY_CTX_free(kctx); |
| 1998 | |
| 1999 | return ecdh; |
| 2000 | fail: |
| 2001 | crypto_ecdh_deinit(ecdh); |
| 2002 | ecdh = NULL; |
| 2003 | goto done; |
| 2004 | } |
| 2005 | |
| 2006 | |
| 2007 | struct wpabuf * crypto_ecdh_get_pubkey(struct crypto_ecdh *ecdh, int inc_y) |
| 2008 | { |
| 2009 | struct wpabuf *buf = NULL; |
| 2010 | EC_KEY *eckey; |
| 2011 | const EC_POINT *pubkey; |
| 2012 | BIGNUM *x, *y = NULL; |
| 2013 | int len = BN_num_bytes(ecdh->ec->prime); |
| 2014 | int res; |
| 2015 | |
| 2016 | eckey = EVP_PKEY_get1_EC_KEY(ecdh->pkey); |
| 2017 | if (!eckey) |
| 2018 | return NULL; |
| 2019 | |
| 2020 | pubkey = EC_KEY_get0_public_key(eckey); |
| 2021 | if (!pubkey) |
| 2022 | return NULL; |
| 2023 | |
| 2024 | x = BN_new(); |
| 2025 | if (inc_y) { |
| 2026 | y = BN_new(); |
| 2027 | if (!y) |
| 2028 | goto fail; |
| 2029 | } |
| 2030 | buf = wpabuf_alloc(inc_y ? 2 * len : len); |
| 2031 | if (!x || !buf) |
| 2032 | goto fail; |
| 2033 | |
| 2034 | if (EC_POINT_get_affine_coordinates_GFp(ecdh->ec->group, pubkey, |
| 2035 | x, y, ecdh->ec->bnctx) != 1) { |
| 2036 | wpa_printf(MSG_ERROR, |
| 2037 | "OpenSSL: EC_POINT_get_affine_coordinates_GFp failed: %s", |
| 2038 | ERR_error_string(ERR_get_error(), NULL)); |
| 2039 | goto fail; |
| 2040 | } |
| 2041 | |
| 2042 | res = crypto_bignum_to_bin((struct crypto_bignum *) x, |
| 2043 | wpabuf_put(buf, len), len, len); |
| 2044 | if (res < 0) |
| 2045 | goto fail; |
| 2046 | |
| 2047 | if (inc_y) { |
| 2048 | res = crypto_bignum_to_bin((struct crypto_bignum *) y, |
| 2049 | wpabuf_put(buf, len), len, len); |
| 2050 | if (res < 0) |
| 2051 | goto fail; |
| 2052 | } |
| 2053 | |
| 2054 | done: |
| 2055 | BN_clear_free(x); |
| 2056 | BN_clear_free(y); |
| 2057 | EC_KEY_free(eckey); |
| 2058 | |
| 2059 | return buf; |
| 2060 | fail: |
| 2061 | wpabuf_free(buf); |
| 2062 | buf = NULL; |
| 2063 | goto done; |
| 2064 | } |
| 2065 | |
| 2066 | |
| 2067 | struct wpabuf * crypto_ecdh_set_peerkey(struct crypto_ecdh *ecdh, int inc_y, |
| 2068 | const u8 *key, size_t len) |
| 2069 | { |
| 2070 | BIGNUM *x, *y = NULL; |
| 2071 | EVP_PKEY_CTX *ctx = NULL; |
| 2072 | EVP_PKEY *peerkey = NULL; |
| 2073 | struct wpabuf *secret = NULL; |
| 2074 | size_t secret_len; |
| 2075 | EC_POINT *pub; |
| 2076 | EC_KEY *eckey = NULL; |
| 2077 | |
| 2078 | x = BN_bin2bn(key, inc_y ? len / 2 : len, NULL); |
| 2079 | pub = EC_POINT_new(ecdh->ec->group); |
| 2080 | if (!x || !pub) |
| 2081 | goto fail; |
| 2082 | |
| 2083 | if (inc_y) { |
| 2084 | y = BN_bin2bn(key + len / 2, len / 2, NULL); |
| 2085 | if (!y) |
| 2086 | goto fail; |
| 2087 | if (!EC_POINT_set_affine_coordinates_GFp(ecdh->ec->group, pub, |
| 2088 | x, y, |
| 2089 | ecdh->ec->bnctx)) { |
| 2090 | wpa_printf(MSG_ERROR, |
| 2091 | "OpenSSL: EC_POINT_set_affine_coordinates_GFp failed: %s", |
| 2092 | ERR_error_string(ERR_get_error(), NULL)); |
| 2093 | goto fail; |
| 2094 | } |
| 2095 | } else if (!EC_POINT_set_compressed_coordinates_GFp(ecdh->ec->group, |
| 2096 | pub, x, 0, |
| 2097 | ecdh->ec->bnctx)) { |
| 2098 | wpa_printf(MSG_ERROR, |
| 2099 | "OpenSSL: EC_POINT_set_compressed_coordinates_GFp failed: %s", |
| 2100 | ERR_error_string(ERR_get_error(), NULL)); |
| 2101 | goto fail; |
| 2102 | } |
| 2103 | |
| 2104 | if (!EC_POINT_is_on_curve(ecdh->ec->group, pub, ecdh->ec->bnctx)) { |
| 2105 | wpa_printf(MSG_ERROR, |
| 2106 | "OpenSSL: ECDH peer public key is not on curve"); |
| 2107 | goto fail; |
| 2108 | } |
| 2109 | |
| 2110 | eckey = EC_KEY_new_by_curve_name(ecdh->ec->nid); |
| 2111 | if (!eckey || EC_KEY_set_public_key(eckey, pub) != 1) { |
| 2112 | wpa_printf(MSG_ERROR, |
| 2113 | "OpenSSL: EC_KEY_set_public_key failed: %s", |
| 2114 | ERR_error_string(ERR_get_error(), NULL)); |
| 2115 | goto fail; |
| 2116 | } |
| 2117 | |
| 2118 | peerkey = EVP_PKEY_new(); |
| 2119 | if (!peerkey || EVP_PKEY_set1_EC_KEY(peerkey, eckey) != 1) |
| 2120 | goto fail; |
| 2121 | |
| 2122 | ctx = EVP_PKEY_CTX_new(ecdh->pkey, NULL); |
| 2123 | if (!ctx || EVP_PKEY_derive_init(ctx) != 1 || |
| 2124 | EVP_PKEY_derive_set_peer(ctx, peerkey) != 1 || |
| 2125 | EVP_PKEY_derive(ctx, NULL, &secret_len) != 1) { |
| 2126 | wpa_printf(MSG_ERROR, |
| 2127 | "OpenSSL: EVP_PKEY_derive(1) failed: %s", |
| 2128 | ERR_error_string(ERR_get_error(), NULL)); |
| 2129 | goto fail; |
| 2130 | } |
| 2131 | |
| 2132 | secret = wpabuf_alloc(secret_len); |
| 2133 | if (!secret) |
| 2134 | goto fail; |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame^] | 2135 | if (EVP_PKEY_derive(ctx, wpabuf_put(secret, 0), &secret_len) != 1) { |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2136 | wpa_printf(MSG_ERROR, |
| 2137 | "OpenSSL: EVP_PKEY_derive(2) failed: %s", |
| 2138 | ERR_error_string(ERR_get_error(), NULL)); |
| 2139 | goto fail; |
| 2140 | } |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame^] | 2141 | if (secret->size != secret_len) |
| 2142 | wpa_printf(MSG_DEBUG, |
| 2143 | "OpenSSL: EVP_PKEY_derive(2) changed secret_len %d -> %d", |
| 2144 | (int) secret->size, (int) secret_len); |
| 2145 | wpabuf_put(secret, secret_len); |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2146 | |
| 2147 | done: |
| 2148 | BN_free(x); |
| 2149 | BN_free(y); |
| 2150 | EC_KEY_free(eckey); |
| 2151 | EC_POINT_free(pub); |
| 2152 | EVP_PKEY_CTX_free(ctx); |
| 2153 | EVP_PKEY_free(peerkey); |
| 2154 | return secret; |
| 2155 | fail: |
| 2156 | wpabuf_free(secret); |
| 2157 | secret = NULL; |
| 2158 | goto done; |
| 2159 | } |
| 2160 | |
| 2161 | |
| 2162 | void crypto_ecdh_deinit(struct crypto_ecdh *ecdh) |
| 2163 | { |
| 2164 | if (ecdh) { |
| 2165 | crypto_ec_deinit(ecdh->ec); |
| 2166 | EVP_PKEY_free(ecdh->pkey); |
| 2167 | os_free(ecdh); |
| 2168 | } |
| 2169 | } |
| 2170 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2171 | #endif /* CONFIG_ECC */ |