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 | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 3 | * Copyright (c) 2004-2015, 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" |
| 27 | #include "wpabuf.h" |
| 28 | #include "dh_group5.h" |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 29 | #include "sha1.h" |
| 30 | #include "sha256.h" |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 31 | #include "sha384.h" |
Dmitry Shmidt | 4ae50e6 | 2016-06-27 13:48:39 -0700 | [diff] [blame^] | 32 | #include "md5.h" |
| 33 | #include "aes_wrap.h" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 34 | #include "crypto.h" |
| 35 | |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 36 | #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER) |
| 37 | /* Compatibility wrappers for older versions. */ |
| 38 | |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 39 | static HMAC_CTX * HMAC_CTX_new(void) |
| 40 | { |
| 41 | HMAC_CTX *ctx; |
| 42 | |
| 43 | ctx = os_zalloc(sizeof(*ctx)); |
| 44 | if (ctx) |
| 45 | HMAC_CTX_init(ctx); |
| 46 | return ctx; |
| 47 | } |
| 48 | |
| 49 | |
| 50 | static void HMAC_CTX_free(HMAC_CTX *ctx) |
| 51 | { |
Dmitry Shmidt | 57c2d39 | 2016-02-23 13:40:19 -0800 | [diff] [blame] | 52 | HMAC_CTX_cleanup(ctx); |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 53 | bin_clear_free(ctx, sizeof(*ctx)); |
| 54 | } |
| 55 | |
| 56 | |
| 57 | static EVP_MD_CTX * EVP_MD_CTX_new(void) |
| 58 | { |
| 59 | EVP_MD_CTX *ctx; |
| 60 | |
| 61 | ctx = os_zalloc(sizeof(*ctx)); |
| 62 | if (ctx) |
| 63 | EVP_MD_CTX_init(ctx); |
| 64 | return ctx; |
| 65 | } |
| 66 | |
| 67 | |
| 68 | static void EVP_MD_CTX_free(EVP_MD_CTX *ctx) |
| 69 | { |
| 70 | bin_clear_free(ctx, sizeof(*ctx)); |
| 71 | } |
| 72 | |
| 73 | #endif /* OpenSSL version < 1.1.0 */ |
| 74 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 75 | static BIGNUM * get_group5_prime(void) |
| 76 | { |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 77 | #ifdef OPENSSL_IS_BORINGSSL |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 78 | static const unsigned char RFC3526_PRIME_1536[] = { |
| 79 | 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xC9,0x0F,0xDA,0xA2, |
| 80 | 0x21,0x68,0xC2,0x34,0xC4,0xC6,0x62,0x8B,0x80,0xDC,0x1C,0xD1, |
| 81 | 0x29,0x02,0x4E,0x08,0x8A,0x67,0xCC,0x74,0x02,0x0B,0xBE,0xA6, |
| 82 | 0x3B,0x13,0x9B,0x22,0x51,0x4A,0x08,0x79,0x8E,0x34,0x04,0xDD, |
| 83 | 0xEF,0x95,0x19,0xB3,0xCD,0x3A,0x43,0x1B,0x30,0x2B,0x0A,0x6D, |
| 84 | 0xF2,0x5F,0x14,0x37,0x4F,0xE1,0x35,0x6D,0x6D,0x51,0xC2,0x45, |
| 85 | 0xE4,0x85,0xB5,0x76,0x62,0x5E,0x7E,0xC6,0xF4,0x4C,0x42,0xE9, |
| 86 | 0xA6,0x37,0xED,0x6B,0x0B,0xFF,0x5C,0xB6,0xF4,0x06,0xB7,0xED, |
| 87 | 0xEE,0x38,0x6B,0xFB,0x5A,0x89,0x9F,0xA5,0xAE,0x9F,0x24,0x11, |
| 88 | 0x7C,0x4B,0x1F,0xE6,0x49,0x28,0x66,0x51,0xEC,0xE4,0x5B,0x3D, |
| 89 | 0xC2,0x00,0x7C,0xB8,0xA1,0x63,0xBF,0x05,0x98,0xDA,0x48,0x36, |
| 90 | 0x1C,0x55,0xD3,0x9A,0x69,0x16,0x3F,0xA8,0xFD,0x24,0xCF,0x5F, |
| 91 | 0x83,0x65,0x5D,0x23,0xDC,0xA3,0xAD,0x96,0x1C,0x62,0xF3,0x56, |
| 92 | 0x20,0x85,0x52,0xBB,0x9E,0xD5,0x29,0x07,0x70,0x96,0x96,0x6D, |
| 93 | 0x67,0x0C,0x35,0x4E,0x4A,0xBC,0x98,0x04,0xF1,0x74,0x6C,0x08, |
| 94 | 0xCA,0x23,0x73,0x27,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, |
| 95 | }; |
| 96 | return BN_bin2bn(RFC3526_PRIME_1536, sizeof(RFC3526_PRIME_1536), NULL); |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 97 | #else /* OPENSSL_IS_BORINGSSL */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 98 | return get_rfc3526_prime_1536(NULL); |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 99 | #endif /* OPENSSL_IS_BORINGSSL */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 102 | #ifdef OPENSSL_NO_SHA256 |
| 103 | #define NO_SHA256_WRAPPER |
| 104 | #endif |
| 105 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 106 | static int openssl_digest_vector(const EVP_MD *type, size_t num_elem, |
| 107 | const u8 *addr[], const size_t *len, u8 *mac) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 108 | { |
Dmitry Shmidt | 55840ad | 2015-12-14 12:45:46 -0800 | [diff] [blame] | 109 | EVP_MD_CTX *ctx; |
| 110 | size_t i; |
| 111 | unsigned int mac_len; |
| 112 | |
| 113 | if (TEST_FAIL()) |
| 114 | return -1; |
| 115 | |
| 116 | ctx = EVP_MD_CTX_new(); |
| 117 | if (!ctx) |
| 118 | return -1; |
| 119 | if (!EVP_DigestInit_ex(ctx, type, NULL)) { |
| 120 | wpa_printf(MSG_ERROR, "OpenSSL: EVP_DigestInit_ex failed: %s", |
| 121 | ERR_error_string(ERR_get_error(), NULL)); |
| 122 | EVP_MD_CTX_free(ctx); |
| 123 | return -1; |
| 124 | } |
| 125 | for (i = 0; i < num_elem; i++) { |
| 126 | if (!EVP_DigestUpdate(ctx, addr[i], len[i])) { |
| 127 | wpa_printf(MSG_ERROR, "OpenSSL: EVP_DigestUpdate " |
| 128 | "failed: %s", |
| 129 | ERR_error_string(ERR_get_error(), NULL)); |
| 130 | EVP_MD_CTX_free(ctx); |
| 131 | return -1; |
| 132 | } |
| 133 | } |
| 134 | if (!EVP_DigestFinal(ctx, mac, &mac_len)) { |
| 135 | wpa_printf(MSG_ERROR, "OpenSSL: EVP_DigestFinal failed: %s", |
| 136 | ERR_error_string(ERR_get_error(), NULL)); |
| 137 | EVP_MD_CTX_free(ctx); |
| 138 | return -1; |
| 139 | } |
| 140 | EVP_MD_CTX_free(ctx); |
| 141 | |
| 142 | return 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 146 | #ifndef CONFIG_FIPS |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 147 | int md4_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac) |
| 148 | { |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 149 | return openssl_digest_vector(EVP_md4(), num_elem, addr, len, mac); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 150 | } |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 151 | #endif /* CONFIG_FIPS */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 152 | |
| 153 | |
| 154 | void des_encrypt(const u8 *clear, const u8 *key, u8 *cypher) |
| 155 | { |
| 156 | u8 pkey[8], next, tmp; |
| 157 | int i; |
| 158 | DES_key_schedule ks; |
| 159 | |
| 160 | /* Add parity bits to the key */ |
| 161 | next = 0; |
| 162 | for (i = 0; i < 7; i++) { |
| 163 | tmp = key[i]; |
| 164 | pkey[i] = (tmp >> i) | next | 1; |
| 165 | next = tmp << (7 - i); |
| 166 | } |
| 167 | pkey[i] = next | 1; |
| 168 | |
Dmitry Shmidt | 9ead16e | 2014-10-07 13:15:23 -0700 | [diff] [blame] | 169 | DES_set_key((DES_cblock *) &pkey, &ks); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 170 | DES_ecb_encrypt((DES_cblock *) clear, (DES_cblock *) cypher, &ks, |
| 171 | DES_ENCRYPT); |
| 172 | } |
| 173 | |
| 174 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 175 | #ifndef CONFIG_NO_RC4 |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 176 | int rc4_skip(const u8 *key, size_t keylen, size_t skip, |
| 177 | u8 *data, size_t data_len) |
| 178 | { |
| 179 | #ifdef OPENSSL_NO_RC4 |
| 180 | return -1; |
| 181 | #else /* OPENSSL_NO_RC4 */ |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 182 | EVP_CIPHER_CTX *ctx; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 183 | int outl; |
| 184 | int res = -1; |
| 185 | unsigned char skip_buf[16]; |
| 186 | |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 187 | ctx = EVP_CIPHER_CTX_new(); |
| 188 | if (!ctx || |
| 189 | !EVP_CIPHER_CTX_set_padding(ctx, 0) || |
| 190 | !EVP_CipherInit_ex(ctx, EVP_rc4(), NULL, NULL, NULL, 1) || |
| 191 | !EVP_CIPHER_CTX_set_key_length(ctx, keylen) || |
| 192 | !EVP_CipherInit_ex(ctx, NULL, NULL, key, NULL, 1)) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 193 | goto out; |
| 194 | |
| 195 | while (skip >= sizeof(skip_buf)) { |
| 196 | size_t len = skip; |
| 197 | if (len > sizeof(skip_buf)) |
| 198 | len = sizeof(skip_buf); |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 199 | if (!EVP_CipherUpdate(ctx, skip_buf, &outl, skip_buf, len)) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 200 | goto out; |
| 201 | skip -= len; |
| 202 | } |
| 203 | |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 204 | if (EVP_CipherUpdate(ctx, data, &outl, data, data_len)) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 205 | res = 0; |
| 206 | |
| 207 | out: |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 208 | if (ctx) |
Dmitry Shmidt | 57c2d39 | 2016-02-23 13:40:19 -0800 | [diff] [blame] | 209 | EVP_CIPHER_CTX_free(ctx); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 210 | return res; |
| 211 | #endif /* OPENSSL_NO_RC4 */ |
| 212 | } |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 213 | #endif /* CONFIG_NO_RC4 */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 214 | |
| 215 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 216 | #ifndef CONFIG_FIPS |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 217 | int md5_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac) |
| 218 | { |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 219 | return openssl_digest_vector(EVP_md5(), num_elem, addr, len, mac); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 220 | } |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 221 | #endif /* CONFIG_FIPS */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 222 | |
| 223 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 224 | int sha1_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac) |
| 225 | { |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 226 | return openssl_digest_vector(EVP_sha1(), num_elem, addr, len, mac); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | |
| 230 | #ifndef NO_SHA256_WRAPPER |
| 231 | int sha256_vector(size_t num_elem, const u8 *addr[], const size_t *len, |
| 232 | u8 *mac) |
| 233 | { |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 234 | return openssl_digest_vector(EVP_sha256(), num_elem, addr, len, mac); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 235 | } |
| 236 | #endif /* NO_SHA256_WRAPPER */ |
| 237 | |
| 238 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 239 | static const EVP_CIPHER * aes_get_evp_cipher(size_t keylen) |
| 240 | { |
| 241 | switch (keylen) { |
| 242 | case 16: |
| 243 | return EVP_aes_128_ecb(); |
Dmitry Shmidt | 9ead16e | 2014-10-07 13:15:23 -0700 | [diff] [blame] | 244 | #ifndef OPENSSL_IS_BORINGSSL |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 245 | case 24: |
| 246 | return EVP_aes_192_ecb(); |
Dmitry Shmidt | 9ead16e | 2014-10-07 13:15:23 -0700 | [diff] [blame] | 247 | #endif /* OPENSSL_IS_BORINGSSL */ |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 248 | case 32: |
| 249 | return EVP_aes_256_ecb(); |
| 250 | } |
| 251 | |
| 252 | return NULL; |
| 253 | } |
| 254 | |
| 255 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 256 | void * aes_encrypt_init(const u8 *key, size_t len) |
| 257 | { |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 258 | EVP_CIPHER_CTX *ctx; |
| 259 | const EVP_CIPHER *type; |
| 260 | |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 261 | if (TEST_FAIL()) |
| 262 | return NULL; |
| 263 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 264 | type = aes_get_evp_cipher(len); |
| 265 | if (type == NULL) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 266 | return NULL; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 267 | |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 268 | ctx = EVP_CIPHER_CTX_new(); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 269 | if (ctx == NULL) |
| 270 | return NULL; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 271 | if (EVP_EncryptInit_ex(ctx, type, NULL, key, NULL) != 1) { |
| 272 | os_free(ctx); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 273 | return NULL; |
| 274 | } |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 275 | EVP_CIPHER_CTX_set_padding(ctx, 0); |
| 276 | return ctx; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | |
| 280 | void aes_encrypt(void *ctx, const u8 *plain, u8 *crypt) |
| 281 | { |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 282 | EVP_CIPHER_CTX *c = ctx; |
| 283 | int clen = 16; |
| 284 | if (EVP_EncryptUpdate(c, crypt, &clen, plain, 16) != 1) { |
| 285 | wpa_printf(MSG_ERROR, "OpenSSL: EVP_EncryptUpdate failed: %s", |
| 286 | ERR_error_string(ERR_get_error(), NULL)); |
| 287 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | |
| 291 | void aes_encrypt_deinit(void *ctx) |
| 292 | { |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 293 | EVP_CIPHER_CTX *c = ctx; |
| 294 | u8 buf[16]; |
| 295 | int len = sizeof(buf); |
| 296 | if (EVP_EncryptFinal_ex(c, buf, &len) != 1) { |
| 297 | wpa_printf(MSG_ERROR, "OpenSSL: EVP_EncryptFinal_ex failed: " |
| 298 | "%s", ERR_error_string(ERR_get_error(), NULL)); |
| 299 | } |
| 300 | if (len != 0) { |
| 301 | wpa_printf(MSG_ERROR, "OpenSSL: Unexpected padding length %d " |
| 302 | "in AES encrypt", len); |
| 303 | } |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 304 | EVP_CIPHER_CTX_free(c); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | |
| 308 | void * aes_decrypt_init(const u8 *key, size_t len) |
| 309 | { |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 310 | EVP_CIPHER_CTX *ctx; |
| 311 | const EVP_CIPHER *type; |
| 312 | |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 313 | if (TEST_FAIL()) |
| 314 | return NULL; |
| 315 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 316 | type = aes_get_evp_cipher(len); |
| 317 | if (type == NULL) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 318 | return NULL; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 319 | |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 320 | ctx = EVP_CIPHER_CTX_new(); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 321 | if (ctx == NULL) |
| 322 | return NULL; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 323 | if (EVP_DecryptInit_ex(ctx, type, NULL, key, NULL) != 1) { |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 324 | EVP_CIPHER_CTX_free(ctx); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 325 | return NULL; |
| 326 | } |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 327 | EVP_CIPHER_CTX_set_padding(ctx, 0); |
| 328 | return ctx; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | |
| 332 | void aes_decrypt(void *ctx, const u8 *crypt, u8 *plain) |
| 333 | { |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 334 | EVP_CIPHER_CTX *c = ctx; |
| 335 | int plen = 16; |
| 336 | if (EVP_DecryptUpdate(c, plain, &plen, crypt, 16) != 1) { |
| 337 | wpa_printf(MSG_ERROR, "OpenSSL: EVP_DecryptUpdate failed: %s", |
| 338 | ERR_error_string(ERR_get_error(), NULL)); |
| 339 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | |
| 343 | void aes_decrypt_deinit(void *ctx) |
| 344 | { |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 345 | EVP_CIPHER_CTX *c = ctx; |
| 346 | u8 buf[16]; |
| 347 | int len = sizeof(buf); |
| 348 | if (EVP_DecryptFinal_ex(c, buf, &len) != 1) { |
| 349 | wpa_printf(MSG_ERROR, "OpenSSL: EVP_DecryptFinal_ex failed: " |
| 350 | "%s", ERR_error_string(ERR_get_error(), NULL)); |
| 351 | } |
| 352 | if (len != 0) { |
| 353 | wpa_printf(MSG_ERROR, "OpenSSL: Unexpected padding length %d " |
| 354 | "in AES decrypt", len); |
| 355 | } |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 356 | EVP_CIPHER_CTX_free(c); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 360 | #ifndef CONFIG_FIPS |
| 361 | #ifndef CONFIG_OPENSSL_INTERNAL_AES_WRAP |
| 362 | |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 363 | int aes_wrap(const u8 *kek, size_t kek_len, int n, const u8 *plain, u8 *cipher) |
| 364 | { |
| 365 | AES_KEY actx; |
| 366 | int res; |
| 367 | |
| 368 | if (AES_set_encrypt_key(kek, kek_len << 3, &actx)) |
| 369 | return -1; |
| 370 | res = AES_wrap_key(&actx, NULL, cipher, plain, n * 8); |
| 371 | OPENSSL_cleanse(&actx, sizeof(actx)); |
| 372 | return res <= 0 ? -1 : 0; |
| 373 | } |
| 374 | |
| 375 | |
| 376 | int aes_unwrap(const u8 *kek, size_t kek_len, int n, const u8 *cipher, |
| 377 | u8 *plain) |
| 378 | { |
| 379 | AES_KEY actx; |
| 380 | int res; |
| 381 | |
| 382 | if (AES_set_decrypt_key(kek, kek_len << 3, &actx)) |
| 383 | return -1; |
| 384 | res = AES_unwrap_key(&actx, NULL, plain, cipher, (n + 1) * 8); |
| 385 | OPENSSL_cleanse(&actx, sizeof(actx)); |
| 386 | return res <= 0 ? -1 : 0; |
| 387 | } |
| 388 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 389 | #endif /* CONFIG_OPENSSL_INTERNAL_AES_WRAP */ |
| 390 | #endif /* CONFIG_FIPS */ |
| 391 | |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 392 | |
Dmitry Shmidt | 912c6ec | 2015-03-30 13:16:51 -0700 | [diff] [blame] | 393 | int aes_128_cbc_encrypt(const u8 *key, const u8 *iv, u8 *data, size_t data_len) |
| 394 | { |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 395 | EVP_CIPHER_CTX *ctx; |
Dmitry Shmidt | 912c6ec | 2015-03-30 13:16:51 -0700 | [diff] [blame] | 396 | int clen, len; |
| 397 | u8 buf[16]; |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 398 | int res = -1; |
Dmitry Shmidt | 912c6ec | 2015-03-30 13:16:51 -0700 | [diff] [blame] | 399 | |
Dmitry Shmidt | d7ff03d | 2015-12-04 14:49:35 -0800 | [diff] [blame] | 400 | if (TEST_FAIL()) |
| 401 | return -1; |
| 402 | |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 403 | ctx = EVP_CIPHER_CTX_new(); |
| 404 | if (!ctx) |
Dmitry Shmidt | 912c6ec | 2015-03-30 13:16:51 -0700 | [diff] [blame] | 405 | return -1; |
Dmitry Shmidt | 912c6ec | 2015-03-30 13:16:51 -0700 | [diff] [blame] | 406 | clen = data_len; |
Dmitry Shmidt | 912c6ec | 2015-03-30 13:16:51 -0700 | [diff] [blame] | 407 | len = sizeof(buf); |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 408 | if (EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv) == 1 && |
| 409 | EVP_CIPHER_CTX_set_padding(ctx, 0) == 1 && |
| 410 | EVP_EncryptUpdate(ctx, data, &clen, data, data_len) == 1 && |
| 411 | clen == (int) data_len && |
| 412 | EVP_EncryptFinal_ex(ctx, buf, &len) == 1 && len == 0) |
| 413 | res = 0; |
Dmitry Shmidt | 57c2d39 | 2016-02-23 13:40:19 -0800 | [diff] [blame] | 414 | EVP_CIPHER_CTX_free(ctx); |
Dmitry Shmidt | 912c6ec | 2015-03-30 13:16:51 -0700 | [diff] [blame] | 415 | |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 416 | return res; |
Dmitry Shmidt | 912c6ec | 2015-03-30 13:16:51 -0700 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | |
| 420 | int aes_128_cbc_decrypt(const u8 *key, const u8 *iv, u8 *data, size_t data_len) |
| 421 | { |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 422 | EVP_CIPHER_CTX *ctx; |
Dmitry Shmidt | 912c6ec | 2015-03-30 13:16:51 -0700 | [diff] [blame] | 423 | int plen, len; |
| 424 | u8 buf[16]; |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 425 | int res = -1; |
Dmitry Shmidt | 912c6ec | 2015-03-30 13:16:51 -0700 | [diff] [blame] | 426 | |
Dmitry Shmidt | d7ff03d | 2015-12-04 14:49:35 -0800 | [diff] [blame] | 427 | if (TEST_FAIL()) |
| 428 | return -1; |
| 429 | |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 430 | ctx = EVP_CIPHER_CTX_new(); |
| 431 | if (!ctx) |
Dmitry Shmidt | 912c6ec | 2015-03-30 13:16:51 -0700 | [diff] [blame] | 432 | return -1; |
Dmitry Shmidt | 912c6ec | 2015-03-30 13:16:51 -0700 | [diff] [blame] | 433 | plen = data_len; |
Dmitry Shmidt | 912c6ec | 2015-03-30 13:16:51 -0700 | [diff] [blame] | 434 | len = sizeof(buf); |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 435 | if (EVP_DecryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv) == 1 && |
| 436 | EVP_CIPHER_CTX_set_padding(ctx, 0) == 1 && |
| 437 | EVP_DecryptUpdate(ctx, data, &plen, data, data_len) == 1 && |
| 438 | plen == (int) data_len && |
| 439 | EVP_DecryptFinal_ex(ctx, buf, &len) == 1 && len == 0) |
| 440 | res = 0; |
Dmitry Shmidt | 57c2d39 | 2016-02-23 13:40:19 -0800 | [diff] [blame] | 441 | EVP_CIPHER_CTX_free(ctx); |
Dmitry Shmidt | 912c6ec | 2015-03-30 13:16:51 -0700 | [diff] [blame] | 442 | |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 443 | return res; |
| 444 | |
Dmitry Shmidt | 912c6ec | 2015-03-30 13:16:51 -0700 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 448 | int crypto_mod_exp(const u8 *base, size_t base_len, |
| 449 | const u8 *power, size_t power_len, |
| 450 | const u8 *modulus, size_t modulus_len, |
| 451 | u8 *result, size_t *result_len) |
| 452 | { |
| 453 | BIGNUM *bn_base, *bn_exp, *bn_modulus, *bn_result; |
| 454 | int ret = -1; |
| 455 | BN_CTX *ctx; |
| 456 | |
| 457 | ctx = BN_CTX_new(); |
| 458 | if (ctx == NULL) |
| 459 | return -1; |
| 460 | |
| 461 | bn_base = BN_bin2bn(base, base_len, NULL); |
| 462 | bn_exp = BN_bin2bn(power, power_len, NULL); |
| 463 | bn_modulus = BN_bin2bn(modulus, modulus_len, NULL); |
| 464 | bn_result = BN_new(); |
| 465 | |
| 466 | if (bn_base == NULL || bn_exp == NULL || bn_modulus == NULL || |
| 467 | bn_result == NULL) |
| 468 | goto error; |
| 469 | |
| 470 | if (BN_mod_exp(bn_result, bn_base, bn_exp, bn_modulus, ctx) != 1) |
| 471 | goto error; |
| 472 | |
| 473 | *result_len = BN_bn2bin(bn_result, result); |
| 474 | ret = 0; |
| 475 | |
| 476 | error: |
Dmitry Shmidt | 7f0b69e | 2014-07-28 10:35:20 -0700 | [diff] [blame] | 477 | BN_clear_free(bn_base); |
| 478 | BN_clear_free(bn_exp); |
| 479 | BN_clear_free(bn_modulus); |
| 480 | BN_clear_free(bn_result); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 481 | BN_CTX_free(ctx); |
| 482 | return ret; |
| 483 | } |
| 484 | |
| 485 | |
| 486 | struct crypto_cipher { |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 487 | EVP_CIPHER_CTX *enc; |
| 488 | EVP_CIPHER_CTX *dec; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 489 | }; |
| 490 | |
| 491 | |
| 492 | struct crypto_cipher * crypto_cipher_init(enum crypto_cipher_alg alg, |
| 493 | const u8 *iv, const u8 *key, |
| 494 | size_t key_len) |
| 495 | { |
| 496 | struct crypto_cipher *ctx; |
| 497 | const EVP_CIPHER *cipher; |
| 498 | |
| 499 | ctx = os_zalloc(sizeof(*ctx)); |
| 500 | if (ctx == NULL) |
| 501 | return NULL; |
| 502 | |
| 503 | switch (alg) { |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 504 | #ifndef CONFIG_NO_RC4 |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 505 | #ifndef OPENSSL_NO_RC4 |
| 506 | case CRYPTO_CIPHER_ALG_RC4: |
| 507 | cipher = EVP_rc4(); |
| 508 | break; |
| 509 | #endif /* OPENSSL_NO_RC4 */ |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 510 | #endif /* CONFIG_NO_RC4 */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 511 | #ifndef OPENSSL_NO_AES |
| 512 | case CRYPTO_CIPHER_ALG_AES: |
| 513 | switch (key_len) { |
| 514 | case 16: |
| 515 | cipher = EVP_aes_128_cbc(); |
| 516 | break; |
Dmitry Shmidt | 9ead16e | 2014-10-07 13:15:23 -0700 | [diff] [blame] | 517 | #ifndef OPENSSL_IS_BORINGSSL |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 518 | case 24: |
| 519 | cipher = EVP_aes_192_cbc(); |
| 520 | break; |
Dmitry Shmidt | 9ead16e | 2014-10-07 13:15:23 -0700 | [diff] [blame] | 521 | #endif /* OPENSSL_IS_BORINGSSL */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 522 | case 32: |
| 523 | cipher = EVP_aes_256_cbc(); |
| 524 | break; |
| 525 | default: |
| 526 | os_free(ctx); |
| 527 | return NULL; |
| 528 | } |
| 529 | break; |
| 530 | #endif /* OPENSSL_NO_AES */ |
| 531 | #ifndef OPENSSL_NO_DES |
| 532 | case CRYPTO_CIPHER_ALG_3DES: |
| 533 | cipher = EVP_des_ede3_cbc(); |
| 534 | break; |
| 535 | case CRYPTO_CIPHER_ALG_DES: |
| 536 | cipher = EVP_des_cbc(); |
| 537 | break; |
| 538 | #endif /* OPENSSL_NO_DES */ |
| 539 | #ifndef OPENSSL_NO_RC2 |
| 540 | case CRYPTO_CIPHER_ALG_RC2: |
| 541 | cipher = EVP_rc2_ecb(); |
| 542 | break; |
| 543 | #endif /* OPENSSL_NO_RC2 */ |
| 544 | default: |
| 545 | os_free(ctx); |
| 546 | return NULL; |
| 547 | } |
| 548 | |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 549 | if (!(ctx->enc = EVP_CIPHER_CTX_new()) || |
| 550 | !EVP_CIPHER_CTX_set_padding(ctx->enc, 0) || |
| 551 | !EVP_EncryptInit_ex(ctx->enc, cipher, NULL, NULL, NULL) || |
| 552 | !EVP_CIPHER_CTX_set_key_length(ctx->enc, key_len) || |
| 553 | !EVP_EncryptInit_ex(ctx->enc, NULL, NULL, key, iv)) { |
| 554 | if (ctx->enc) |
Dmitry Shmidt | 57c2d39 | 2016-02-23 13:40:19 -0800 | [diff] [blame] | 555 | EVP_CIPHER_CTX_free(ctx->enc); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 556 | os_free(ctx); |
| 557 | return NULL; |
| 558 | } |
| 559 | |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 560 | if (!(ctx->dec = EVP_CIPHER_CTX_new()) || |
| 561 | !EVP_CIPHER_CTX_set_padding(ctx->dec, 0) || |
| 562 | !EVP_DecryptInit_ex(ctx->dec, cipher, NULL, NULL, NULL) || |
| 563 | !EVP_CIPHER_CTX_set_key_length(ctx->dec, key_len) || |
| 564 | !EVP_DecryptInit_ex(ctx->dec, NULL, NULL, key, iv)) { |
Dmitry Shmidt | 57c2d39 | 2016-02-23 13:40:19 -0800 | [diff] [blame] | 565 | EVP_CIPHER_CTX_free(ctx->enc); |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 566 | if (ctx->dec) |
Dmitry Shmidt | 57c2d39 | 2016-02-23 13:40:19 -0800 | [diff] [blame] | 567 | EVP_CIPHER_CTX_free(ctx->dec); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 568 | os_free(ctx); |
| 569 | return NULL; |
| 570 | } |
| 571 | |
| 572 | return ctx; |
| 573 | } |
| 574 | |
| 575 | |
| 576 | int crypto_cipher_encrypt(struct crypto_cipher *ctx, const u8 *plain, |
| 577 | u8 *crypt, size_t len) |
| 578 | { |
| 579 | int outl; |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 580 | if (!EVP_EncryptUpdate(ctx->enc, crypt, &outl, plain, len)) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 581 | return -1; |
| 582 | return 0; |
| 583 | } |
| 584 | |
| 585 | |
| 586 | int crypto_cipher_decrypt(struct crypto_cipher *ctx, const u8 *crypt, |
| 587 | u8 *plain, size_t len) |
| 588 | { |
| 589 | int outl; |
| 590 | outl = len; |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 591 | if (!EVP_DecryptUpdate(ctx->dec, plain, &outl, crypt, len)) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 592 | return -1; |
| 593 | return 0; |
| 594 | } |
| 595 | |
| 596 | |
| 597 | void crypto_cipher_deinit(struct crypto_cipher *ctx) |
| 598 | { |
Dmitry Shmidt | 57c2d39 | 2016-02-23 13:40:19 -0800 | [diff] [blame] | 599 | EVP_CIPHER_CTX_free(ctx->enc); |
| 600 | EVP_CIPHER_CTX_free(ctx->dec); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 601 | os_free(ctx); |
| 602 | } |
| 603 | |
| 604 | |
| 605 | void * dh5_init(struct wpabuf **priv, struct wpabuf **publ) |
| 606 | { |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 607 | #if OPENSSL_VERSION_NUMBER < 0x10100000L |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 608 | DH *dh; |
| 609 | struct wpabuf *pubkey = NULL, *privkey = NULL; |
| 610 | size_t publen, privlen; |
| 611 | |
| 612 | *priv = NULL; |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 613 | wpabuf_free(*publ); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 614 | *publ = NULL; |
| 615 | |
| 616 | dh = DH_new(); |
| 617 | if (dh == NULL) |
| 618 | return NULL; |
| 619 | |
| 620 | dh->g = BN_new(); |
| 621 | if (dh->g == NULL || BN_set_word(dh->g, 2) != 1) |
| 622 | goto err; |
| 623 | |
| 624 | dh->p = get_group5_prime(); |
| 625 | if (dh->p == NULL) |
| 626 | goto err; |
| 627 | |
| 628 | if (DH_generate_key(dh) != 1) |
| 629 | goto err; |
| 630 | |
| 631 | publen = BN_num_bytes(dh->pub_key); |
| 632 | pubkey = wpabuf_alloc(publen); |
| 633 | if (pubkey == NULL) |
| 634 | goto err; |
| 635 | privlen = BN_num_bytes(dh->priv_key); |
| 636 | privkey = wpabuf_alloc(privlen); |
| 637 | if (privkey == NULL) |
| 638 | goto err; |
| 639 | |
| 640 | BN_bn2bin(dh->pub_key, wpabuf_put(pubkey, publen)); |
| 641 | BN_bn2bin(dh->priv_key, wpabuf_put(privkey, privlen)); |
| 642 | |
| 643 | *priv = privkey; |
| 644 | *publ = pubkey; |
| 645 | return dh; |
| 646 | |
| 647 | err: |
Dmitry Shmidt | ff787d5 | 2015-01-12 13:01:47 -0800 | [diff] [blame] | 648 | wpabuf_clear_free(pubkey); |
| 649 | wpabuf_clear_free(privkey); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 650 | DH_free(dh); |
| 651 | return NULL; |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 652 | #else |
| 653 | DH *dh; |
| 654 | struct wpabuf *pubkey = NULL, *privkey = NULL; |
| 655 | size_t publen, privlen; |
Dmitry Shmidt | 4ae50e6 | 2016-06-27 13:48:39 -0700 | [diff] [blame^] | 656 | BIGNUM *p = NULL, *g; |
| 657 | const BIGNUM *priv_key = NULL, *pub_key = NULL; |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 658 | |
| 659 | *priv = NULL; |
| 660 | wpabuf_free(*publ); |
| 661 | *publ = NULL; |
| 662 | |
| 663 | dh = DH_new(); |
| 664 | if (dh == NULL) |
| 665 | return NULL; |
| 666 | |
| 667 | g = BN_new(); |
| 668 | p = get_group5_prime(); |
| 669 | if (!g || BN_set_word(g, 2) != 1 || !p || |
| 670 | DH_set0_pqg(dh, p, NULL, g) != 1) |
| 671 | goto err; |
| 672 | p = NULL; |
| 673 | g = NULL; |
| 674 | |
| 675 | if (DH_generate_key(dh) != 1) |
| 676 | goto err; |
| 677 | |
| 678 | DH_get0_key(dh, &pub_key, &priv_key); |
| 679 | publen = BN_num_bytes(pub_key); |
| 680 | pubkey = wpabuf_alloc(publen); |
| 681 | if (!pubkey) |
| 682 | goto err; |
| 683 | privlen = BN_num_bytes(priv_key); |
| 684 | privkey = wpabuf_alloc(privlen); |
| 685 | if (!privkey) |
| 686 | goto err; |
| 687 | |
| 688 | BN_bn2bin(pub_key, wpabuf_put(pubkey, publen)); |
| 689 | BN_bn2bin(priv_key, wpabuf_put(privkey, privlen)); |
| 690 | |
| 691 | *priv = privkey; |
| 692 | *publ = pubkey; |
| 693 | return dh; |
| 694 | |
| 695 | err: |
| 696 | BN_free(p); |
| 697 | BN_free(g); |
| 698 | wpabuf_clear_free(pubkey); |
| 699 | wpabuf_clear_free(privkey); |
| 700 | DH_free(dh); |
| 701 | return NULL; |
| 702 | #endif |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 706 | void * dh5_init_fixed(const struct wpabuf *priv, const struct wpabuf *publ) |
| 707 | { |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 708 | #if OPENSSL_VERSION_NUMBER < 0x10100000L |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 709 | DH *dh; |
| 710 | |
| 711 | dh = DH_new(); |
| 712 | if (dh == NULL) |
| 713 | return NULL; |
| 714 | |
| 715 | dh->g = BN_new(); |
| 716 | if (dh->g == NULL || BN_set_word(dh->g, 2) != 1) |
| 717 | goto err; |
| 718 | |
| 719 | dh->p = get_group5_prime(); |
| 720 | if (dh->p == NULL) |
| 721 | goto err; |
| 722 | |
| 723 | dh->priv_key = BN_bin2bn(wpabuf_head(priv), wpabuf_len(priv), NULL); |
| 724 | if (dh->priv_key == NULL) |
| 725 | goto err; |
| 726 | |
| 727 | dh->pub_key = BN_bin2bn(wpabuf_head(publ), wpabuf_len(publ), NULL); |
| 728 | if (dh->pub_key == NULL) |
| 729 | goto err; |
| 730 | |
| 731 | if (DH_generate_key(dh) != 1) |
| 732 | goto err; |
| 733 | |
| 734 | return dh; |
| 735 | |
| 736 | err: |
| 737 | DH_free(dh); |
| 738 | return NULL; |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 739 | #else |
| 740 | DH *dh; |
| 741 | BIGNUM *p = NULL, *g, *priv_key = NULL, *pub_key = NULL; |
| 742 | |
| 743 | dh = DH_new(); |
| 744 | if (dh == NULL) |
| 745 | return NULL; |
| 746 | |
| 747 | g = BN_new(); |
| 748 | p = get_group5_prime(); |
| 749 | if (!g || BN_set_word(g, 2) != 1 || !p || |
| 750 | DH_set0_pqg(dh, p, NULL, g) != 1) |
| 751 | goto err; |
| 752 | p = NULL; |
| 753 | g = NULL; |
| 754 | |
| 755 | priv_key = BN_bin2bn(wpabuf_head(priv), wpabuf_len(priv), NULL); |
| 756 | pub_key = BN_bin2bn(wpabuf_head(publ), wpabuf_len(publ), NULL); |
| 757 | if (!priv_key || !pub_key || DH_set0_key(dh, pub_key, priv_key) != 0) |
| 758 | goto err; |
| 759 | pub_key = NULL; |
| 760 | priv_key = NULL; |
| 761 | |
| 762 | if (DH_generate_key(dh) != 1) |
| 763 | goto err; |
| 764 | |
| 765 | return dh; |
| 766 | |
| 767 | err: |
| 768 | BN_free(p); |
| 769 | BN_free(g); |
| 770 | BN_free(pub_key); |
| 771 | BN_clear_free(priv_key); |
| 772 | DH_free(dh); |
| 773 | return NULL; |
| 774 | #endif |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 775 | } |
| 776 | |
| 777 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 778 | struct wpabuf * dh5_derive_shared(void *ctx, const struct wpabuf *peer_public, |
| 779 | const struct wpabuf *own_private) |
| 780 | { |
| 781 | BIGNUM *pub_key; |
| 782 | struct wpabuf *res = NULL; |
| 783 | size_t rlen; |
| 784 | DH *dh = ctx; |
| 785 | int keylen; |
| 786 | |
| 787 | if (ctx == NULL) |
| 788 | return NULL; |
| 789 | |
| 790 | pub_key = BN_bin2bn(wpabuf_head(peer_public), wpabuf_len(peer_public), |
| 791 | NULL); |
| 792 | if (pub_key == NULL) |
| 793 | return NULL; |
| 794 | |
| 795 | rlen = DH_size(dh); |
| 796 | res = wpabuf_alloc(rlen); |
| 797 | if (res == NULL) |
| 798 | goto err; |
| 799 | |
| 800 | keylen = DH_compute_key(wpabuf_mhead(res), pub_key, dh); |
| 801 | if (keylen < 0) |
| 802 | goto err; |
| 803 | wpabuf_put(res, keylen); |
Dmitry Shmidt | 7f0b69e | 2014-07-28 10:35:20 -0700 | [diff] [blame] | 804 | BN_clear_free(pub_key); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 805 | |
| 806 | return res; |
| 807 | |
| 808 | err: |
Dmitry Shmidt | 7f0b69e | 2014-07-28 10:35:20 -0700 | [diff] [blame] | 809 | BN_clear_free(pub_key); |
Dmitry Shmidt | ff787d5 | 2015-01-12 13:01:47 -0800 | [diff] [blame] | 810 | wpabuf_clear_free(res); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 811 | return NULL; |
| 812 | } |
| 813 | |
| 814 | |
| 815 | void dh5_free(void *ctx) |
| 816 | { |
| 817 | DH *dh; |
| 818 | if (ctx == NULL) |
| 819 | return; |
| 820 | dh = ctx; |
| 821 | DH_free(dh); |
| 822 | } |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 823 | |
| 824 | |
| 825 | struct crypto_hash { |
Dmitry Shmidt | 55840ad | 2015-12-14 12:45:46 -0800 | [diff] [blame] | 826 | HMAC_CTX *ctx; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 827 | }; |
| 828 | |
| 829 | |
| 830 | struct crypto_hash * crypto_hash_init(enum crypto_hash_alg alg, const u8 *key, |
| 831 | size_t key_len) |
| 832 | { |
| 833 | struct crypto_hash *ctx; |
| 834 | const EVP_MD *md; |
| 835 | |
| 836 | switch (alg) { |
| 837 | #ifndef OPENSSL_NO_MD5 |
| 838 | case CRYPTO_HASH_ALG_HMAC_MD5: |
| 839 | md = EVP_md5(); |
| 840 | break; |
| 841 | #endif /* OPENSSL_NO_MD5 */ |
| 842 | #ifndef OPENSSL_NO_SHA |
| 843 | case CRYPTO_HASH_ALG_HMAC_SHA1: |
| 844 | md = EVP_sha1(); |
| 845 | break; |
| 846 | #endif /* OPENSSL_NO_SHA */ |
| 847 | #ifndef OPENSSL_NO_SHA256 |
| 848 | #ifdef CONFIG_SHA256 |
| 849 | case CRYPTO_HASH_ALG_HMAC_SHA256: |
| 850 | md = EVP_sha256(); |
| 851 | break; |
| 852 | #endif /* CONFIG_SHA256 */ |
| 853 | #endif /* OPENSSL_NO_SHA256 */ |
| 854 | default: |
| 855 | return NULL; |
| 856 | } |
| 857 | |
| 858 | ctx = os_zalloc(sizeof(*ctx)); |
| 859 | if (ctx == NULL) |
| 860 | return NULL; |
Dmitry Shmidt | 55840ad | 2015-12-14 12:45:46 -0800 | [diff] [blame] | 861 | ctx->ctx = HMAC_CTX_new(); |
| 862 | if (!ctx->ctx) { |
| 863 | os_free(ctx); |
| 864 | return NULL; |
| 865 | } |
| 866 | |
| 867 | if (HMAC_Init_ex(ctx->ctx, key, key_len, md, NULL) != 1) { |
| 868 | HMAC_CTX_free(ctx->ctx); |
| 869 | bin_clear_free(ctx, sizeof(*ctx)); |
| 870 | return NULL; |
| 871 | } |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 872 | |
| 873 | return ctx; |
| 874 | } |
| 875 | |
| 876 | |
| 877 | void crypto_hash_update(struct crypto_hash *ctx, const u8 *data, size_t len) |
| 878 | { |
| 879 | if (ctx == NULL) |
| 880 | return; |
Dmitry Shmidt | 55840ad | 2015-12-14 12:45:46 -0800 | [diff] [blame] | 881 | HMAC_Update(ctx->ctx, data, len); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 882 | } |
| 883 | |
| 884 | |
| 885 | int crypto_hash_finish(struct crypto_hash *ctx, u8 *mac, size_t *len) |
| 886 | { |
| 887 | unsigned int mdlen; |
| 888 | int res; |
| 889 | |
| 890 | if (ctx == NULL) |
| 891 | return -2; |
| 892 | |
| 893 | if (mac == NULL || len == NULL) { |
Dmitry Shmidt | 55840ad | 2015-12-14 12:45:46 -0800 | [diff] [blame] | 894 | HMAC_CTX_free(ctx->ctx); |
Dmitry Shmidt | ff787d5 | 2015-01-12 13:01:47 -0800 | [diff] [blame] | 895 | bin_clear_free(ctx, sizeof(*ctx)); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 896 | return 0; |
| 897 | } |
| 898 | |
| 899 | mdlen = *len; |
Dmitry Shmidt | 55840ad | 2015-12-14 12:45:46 -0800 | [diff] [blame] | 900 | res = HMAC_Final(ctx->ctx, mac, &mdlen); |
| 901 | HMAC_CTX_free(ctx->ctx); |
Dmitry Shmidt | ff787d5 | 2015-01-12 13:01:47 -0800 | [diff] [blame] | 902 | bin_clear_free(ctx, sizeof(*ctx)); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 903 | |
| 904 | if (res == 1) { |
| 905 | *len = mdlen; |
| 906 | return 0; |
| 907 | } |
| 908 | |
| 909 | return -1; |
| 910 | } |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 911 | |
| 912 | |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 913 | static int openssl_hmac_vector(const EVP_MD *type, const u8 *key, |
| 914 | size_t key_len, size_t num_elem, |
| 915 | const u8 *addr[], const size_t *len, u8 *mac, |
| 916 | unsigned int mdlen) |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 917 | { |
Dmitry Shmidt | 55840ad | 2015-12-14 12:45:46 -0800 | [diff] [blame] | 918 | HMAC_CTX *ctx; |
| 919 | size_t i; |
| 920 | int res; |
| 921 | |
| 922 | if (TEST_FAIL()) |
| 923 | return -1; |
| 924 | |
| 925 | ctx = HMAC_CTX_new(); |
| 926 | if (!ctx) |
| 927 | return -1; |
| 928 | res = HMAC_Init_ex(ctx, key, key_len, type, NULL); |
| 929 | if (res != 1) |
| 930 | goto done; |
| 931 | |
| 932 | for (i = 0; i < num_elem; i++) |
| 933 | HMAC_Update(ctx, addr[i], len[i]); |
| 934 | |
| 935 | res = HMAC_Final(ctx, mac, &mdlen); |
| 936 | done: |
| 937 | HMAC_CTX_free(ctx); |
| 938 | |
| 939 | return res == 1 ? 0 : -1; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 940 | } |
| 941 | |
| 942 | |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 943 | #ifndef CONFIG_FIPS |
| 944 | |
| 945 | int hmac_md5_vector(const u8 *key, size_t key_len, size_t num_elem, |
| 946 | const u8 *addr[], const size_t *len, u8 *mac) |
| 947 | { |
| 948 | return openssl_hmac_vector(EVP_md5(), key ,key_len, num_elem, addr, len, |
| 949 | mac, 16); |
| 950 | } |
| 951 | |
| 952 | |
| 953 | int hmac_md5(const u8 *key, size_t key_len, const u8 *data, size_t data_len, |
| 954 | u8 *mac) |
| 955 | { |
| 956 | return hmac_md5_vector(key, key_len, 1, &data, &data_len, mac); |
| 957 | } |
| 958 | |
| 959 | #endif /* CONFIG_FIPS */ |
| 960 | |
| 961 | |
| 962 | int pbkdf2_sha1(const char *passphrase, const u8 *ssid, size_t ssid_len, |
| 963 | int iterations, u8 *buf, size_t buflen) |
| 964 | { |
| 965 | if (PKCS5_PBKDF2_HMAC_SHA1(passphrase, os_strlen(passphrase), ssid, |
| 966 | ssid_len, iterations, buflen, buf) != 1) |
| 967 | return -1; |
| 968 | return 0; |
| 969 | } |
| 970 | |
| 971 | |
| 972 | int hmac_sha1_vector(const u8 *key, size_t key_len, size_t num_elem, |
| 973 | const u8 *addr[], const size_t *len, u8 *mac) |
| 974 | { |
| 975 | return openssl_hmac_vector(EVP_sha1(), key, key_len, num_elem, addr, |
| 976 | len, mac, 20); |
| 977 | } |
| 978 | |
| 979 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 980 | int hmac_sha1(const u8 *key, size_t key_len, const u8 *data, size_t data_len, |
| 981 | u8 *mac) |
| 982 | { |
| 983 | return hmac_sha1_vector(key, key_len, 1, &data, &data_len, mac); |
| 984 | } |
| 985 | |
| 986 | |
| 987 | #ifdef CONFIG_SHA256 |
| 988 | |
| 989 | int hmac_sha256_vector(const u8 *key, size_t key_len, size_t num_elem, |
| 990 | const u8 *addr[], const size_t *len, u8 *mac) |
| 991 | { |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 992 | return openssl_hmac_vector(EVP_sha256(), key, key_len, num_elem, addr, |
| 993 | len, mac, 32); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 994 | } |
| 995 | |
| 996 | |
| 997 | int hmac_sha256(const u8 *key, size_t key_len, const u8 *data, |
| 998 | size_t data_len, u8 *mac) |
| 999 | { |
| 1000 | return hmac_sha256_vector(key, key_len, 1, &data, &data_len, mac); |
| 1001 | } |
| 1002 | |
| 1003 | #endif /* CONFIG_SHA256 */ |
| 1004 | |
| 1005 | |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 1006 | #ifdef CONFIG_SHA384 |
| 1007 | |
| 1008 | int hmac_sha384_vector(const u8 *key, size_t key_len, size_t num_elem, |
| 1009 | const u8 *addr[], const size_t *len, u8 *mac) |
| 1010 | { |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 1011 | return openssl_hmac_vector(EVP_sha384(), key, key_len, num_elem, addr, |
| 1012 | len, mac, 32); |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 1013 | } |
| 1014 | |
| 1015 | |
| 1016 | int hmac_sha384(const u8 *key, size_t key_len, const u8 *data, |
| 1017 | size_t data_len, u8 *mac) |
| 1018 | { |
| 1019 | return hmac_sha384_vector(key, key_len, 1, &data, &data_len, mac); |
| 1020 | } |
| 1021 | |
| 1022 | #endif /* CONFIG_SHA384 */ |
| 1023 | |
| 1024 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1025 | int crypto_get_random(void *buf, size_t len) |
| 1026 | { |
| 1027 | if (RAND_bytes(buf, len) != 1) |
| 1028 | return -1; |
| 1029 | return 0; |
| 1030 | } |
| 1031 | |
| 1032 | |
| 1033 | #ifdef CONFIG_OPENSSL_CMAC |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 1034 | int omac1_aes_vector(const u8 *key, size_t key_len, size_t num_elem, |
| 1035 | const u8 *addr[], const size_t *len, u8 *mac) |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1036 | { |
| 1037 | CMAC_CTX *ctx; |
| 1038 | int ret = -1; |
| 1039 | size_t outlen, i; |
| 1040 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1041 | if (TEST_FAIL()) |
| 1042 | return -1; |
| 1043 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1044 | ctx = CMAC_CTX_new(); |
| 1045 | if (ctx == NULL) |
| 1046 | return -1; |
| 1047 | |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 1048 | if (key_len == 32) { |
| 1049 | if (!CMAC_Init(ctx, key, 32, EVP_aes_256_cbc(), NULL)) |
| 1050 | goto fail; |
| 1051 | } else if (key_len == 16) { |
| 1052 | if (!CMAC_Init(ctx, key, 16, EVP_aes_128_cbc(), NULL)) |
| 1053 | goto fail; |
| 1054 | } else { |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1055 | goto fail; |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 1056 | } |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1057 | for (i = 0; i < num_elem; i++) { |
| 1058 | if (!CMAC_Update(ctx, addr[i], len[i])) |
| 1059 | goto fail; |
| 1060 | } |
| 1061 | if (!CMAC_Final(ctx, mac, &outlen) || outlen != 16) |
| 1062 | goto fail; |
| 1063 | |
| 1064 | ret = 0; |
| 1065 | fail: |
| 1066 | CMAC_CTX_free(ctx); |
| 1067 | return ret; |
| 1068 | } |
| 1069 | |
| 1070 | |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 1071 | int omac1_aes_128_vector(const u8 *key, size_t num_elem, |
| 1072 | const u8 *addr[], const size_t *len, u8 *mac) |
| 1073 | { |
| 1074 | return omac1_aes_vector(key, 16, num_elem, addr, len, mac); |
| 1075 | } |
| 1076 | |
| 1077 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1078 | int omac1_aes_128(const u8 *key, const u8 *data, size_t data_len, u8 *mac) |
| 1079 | { |
| 1080 | return omac1_aes_128_vector(key, 1, &data, &data_len, mac); |
| 1081 | } |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 1082 | |
| 1083 | |
| 1084 | int omac1_aes_256(const u8 *key, const u8 *data, size_t data_len, u8 *mac) |
| 1085 | { |
| 1086 | return omac1_aes_vector(key, 32, 1, &data, &data_len, mac); |
| 1087 | } |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1088 | #endif /* CONFIG_OPENSSL_CMAC */ |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1089 | |
| 1090 | |
| 1091 | struct crypto_bignum * crypto_bignum_init(void) |
| 1092 | { |
Dmitry Shmidt | e466304 | 2016-04-04 10:07:49 -0700 | [diff] [blame] | 1093 | if (TEST_FAIL()) |
| 1094 | return NULL; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1095 | return (struct crypto_bignum *) BN_new(); |
| 1096 | } |
| 1097 | |
| 1098 | |
| 1099 | struct crypto_bignum * crypto_bignum_init_set(const u8 *buf, size_t len) |
| 1100 | { |
Dmitry Shmidt | e466304 | 2016-04-04 10:07:49 -0700 | [diff] [blame] | 1101 | BIGNUM *bn; |
| 1102 | |
| 1103 | if (TEST_FAIL()) |
| 1104 | return NULL; |
| 1105 | |
| 1106 | bn = BN_bin2bn(buf, len, NULL); |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1107 | return (struct crypto_bignum *) bn; |
| 1108 | } |
| 1109 | |
| 1110 | |
| 1111 | void crypto_bignum_deinit(struct crypto_bignum *n, int clear) |
| 1112 | { |
| 1113 | if (clear) |
| 1114 | BN_clear_free((BIGNUM *) n); |
| 1115 | else |
| 1116 | BN_free((BIGNUM *) n); |
| 1117 | } |
| 1118 | |
| 1119 | |
| 1120 | int crypto_bignum_to_bin(const struct crypto_bignum *a, |
| 1121 | u8 *buf, size_t buflen, size_t padlen) |
| 1122 | { |
| 1123 | int num_bytes, offset; |
| 1124 | |
Dmitry Shmidt | e466304 | 2016-04-04 10:07:49 -0700 | [diff] [blame] | 1125 | if (TEST_FAIL()) |
| 1126 | return -1; |
| 1127 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1128 | if (padlen > buflen) |
| 1129 | return -1; |
| 1130 | |
| 1131 | num_bytes = BN_num_bytes((const BIGNUM *) a); |
| 1132 | if ((size_t) num_bytes > buflen) |
| 1133 | return -1; |
| 1134 | if (padlen > (size_t) num_bytes) |
| 1135 | offset = padlen - num_bytes; |
| 1136 | else |
| 1137 | offset = 0; |
| 1138 | |
| 1139 | os_memset(buf, 0, offset); |
| 1140 | BN_bn2bin((const BIGNUM *) a, buf + offset); |
| 1141 | |
| 1142 | return num_bytes + offset; |
| 1143 | } |
| 1144 | |
| 1145 | |
| 1146 | int crypto_bignum_add(const struct crypto_bignum *a, |
| 1147 | const struct crypto_bignum *b, |
| 1148 | struct crypto_bignum *c) |
| 1149 | { |
| 1150 | return BN_add((BIGNUM *) c, (const BIGNUM *) a, (const BIGNUM *) b) ? |
| 1151 | 0 : -1; |
| 1152 | } |
| 1153 | |
| 1154 | |
| 1155 | int crypto_bignum_mod(const struct crypto_bignum *a, |
| 1156 | const struct crypto_bignum *b, |
| 1157 | struct crypto_bignum *c) |
| 1158 | { |
| 1159 | int res; |
| 1160 | BN_CTX *bnctx; |
| 1161 | |
| 1162 | bnctx = BN_CTX_new(); |
| 1163 | if (bnctx == NULL) |
| 1164 | return -1; |
| 1165 | res = BN_mod((BIGNUM *) c, (const BIGNUM *) a, (const BIGNUM *) b, |
| 1166 | bnctx); |
| 1167 | BN_CTX_free(bnctx); |
| 1168 | |
| 1169 | return res ? 0 : -1; |
| 1170 | } |
| 1171 | |
| 1172 | |
| 1173 | int crypto_bignum_exptmod(const struct crypto_bignum *a, |
| 1174 | const struct crypto_bignum *b, |
| 1175 | const struct crypto_bignum *c, |
| 1176 | struct crypto_bignum *d) |
| 1177 | { |
| 1178 | int res; |
| 1179 | BN_CTX *bnctx; |
| 1180 | |
Dmitry Shmidt | e466304 | 2016-04-04 10:07:49 -0700 | [diff] [blame] | 1181 | if (TEST_FAIL()) |
| 1182 | return -1; |
| 1183 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1184 | bnctx = BN_CTX_new(); |
| 1185 | if (bnctx == NULL) |
| 1186 | return -1; |
| 1187 | res = BN_mod_exp((BIGNUM *) d, (const BIGNUM *) a, (const BIGNUM *) b, |
| 1188 | (const BIGNUM *) c, bnctx); |
| 1189 | BN_CTX_free(bnctx); |
| 1190 | |
| 1191 | return res ? 0 : -1; |
| 1192 | } |
| 1193 | |
| 1194 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1195 | int crypto_bignum_inverse(const struct crypto_bignum *a, |
| 1196 | const struct crypto_bignum *b, |
| 1197 | struct crypto_bignum *c) |
| 1198 | { |
| 1199 | BIGNUM *res; |
| 1200 | BN_CTX *bnctx; |
| 1201 | |
Dmitry Shmidt | e466304 | 2016-04-04 10:07:49 -0700 | [diff] [blame] | 1202 | if (TEST_FAIL()) |
| 1203 | return -1; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1204 | bnctx = BN_CTX_new(); |
| 1205 | if (bnctx == NULL) |
| 1206 | return -1; |
| 1207 | res = BN_mod_inverse((BIGNUM *) c, (const BIGNUM *) a, |
| 1208 | (const BIGNUM *) b, bnctx); |
| 1209 | BN_CTX_free(bnctx); |
| 1210 | |
| 1211 | return res ? 0 : -1; |
| 1212 | } |
| 1213 | |
| 1214 | |
| 1215 | int crypto_bignum_sub(const struct crypto_bignum *a, |
| 1216 | const struct crypto_bignum *b, |
| 1217 | struct crypto_bignum *c) |
| 1218 | { |
Dmitry Shmidt | e466304 | 2016-04-04 10:07:49 -0700 | [diff] [blame] | 1219 | if (TEST_FAIL()) |
| 1220 | return -1; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1221 | return BN_sub((BIGNUM *) c, (const BIGNUM *) a, (const BIGNUM *) b) ? |
| 1222 | 0 : -1; |
| 1223 | } |
| 1224 | |
| 1225 | |
| 1226 | int crypto_bignum_div(const struct crypto_bignum *a, |
| 1227 | const struct crypto_bignum *b, |
| 1228 | struct crypto_bignum *c) |
| 1229 | { |
| 1230 | int res; |
| 1231 | |
| 1232 | BN_CTX *bnctx; |
| 1233 | |
Dmitry Shmidt | e466304 | 2016-04-04 10:07:49 -0700 | [diff] [blame] | 1234 | if (TEST_FAIL()) |
| 1235 | return -1; |
| 1236 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1237 | bnctx = BN_CTX_new(); |
| 1238 | if (bnctx == NULL) |
| 1239 | return -1; |
| 1240 | res = BN_div((BIGNUM *) c, NULL, (const BIGNUM *) a, |
| 1241 | (const BIGNUM *) b, bnctx); |
| 1242 | BN_CTX_free(bnctx); |
| 1243 | |
| 1244 | return res ? 0 : -1; |
| 1245 | } |
| 1246 | |
| 1247 | |
| 1248 | int crypto_bignum_mulmod(const struct crypto_bignum *a, |
| 1249 | const struct crypto_bignum *b, |
| 1250 | const struct crypto_bignum *c, |
| 1251 | struct crypto_bignum *d) |
| 1252 | { |
| 1253 | int res; |
| 1254 | |
| 1255 | BN_CTX *bnctx; |
| 1256 | |
Dmitry Shmidt | e466304 | 2016-04-04 10:07:49 -0700 | [diff] [blame] | 1257 | if (TEST_FAIL()) |
| 1258 | return -1; |
| 1259 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1260 | bnctx = BN_CTX_new(); |
| 1261 | if (bnctx == NULL) |
| 1262 | return -1; |
| 1263 | res = BN_mod_mul((BIGNUM *) d, (const BIGNUM *) a, (const BIGNUM *) b, |
| 1264 | (const BIGNUM *) c, bnctx); |
| 1265 | BN_CTX_free(bnctx); |
| 1266 | |
| 1267 | return res ? 0 : -1; |
| 1268 | } |
| 1269 | |
| 1270 | |
| 1271 | int crypto_bignum_cmp(const struct crypto_bignum *a, |
| 1272 | const struct crypto_bignum *b) |
| 1273 | { |
| 1274 | return BN_cmp((const BIGNUM *) a, (const BIGNUM *) b); |
| 1275 | } |
| 1276 | |
| 1277 | |
| 1278 | int crypto_bignum_bits(const struct crypto_bignum *a) |
| 1279 | { |
| 1280 | return BN_num_bits((const BIGNUM *) a); |
| 1281 | } |
| 1282 | |
| 1283 | |
| 1284 | int crypto_bignum_is_zero(const struct crypto_bignum *a) |
| 1285 | { |
| 1286 | return BN_is_zero((const BIGNUM *) a); |
| 1287 | } |
| 1288 | |
| 1289 | |
| 1290 | int crypto_bignum_is_one(const struct crypto_bignum *a) |
| 1291 | { |
| 1292 | return BN_is_one((const BIGNUM *) a); |
| 1293 | } |
| 1294 | |
| 1295 | |
Dmitry Shmidt | 4171258 | 2015-06-29 11:02:15 -0700 | [diff] [blame] | 1296 | int crypto_bignum_legendre(const struct crypto_bignum *a, |
| 1297 | const struct crypto_bignum *p) |
| 1298 | { |
| 1299 | BN_CTX *bnctx; |
| 1300 | BIGNUM *exp = NULL, *tmp = NULL; |
| 1301 | int res = -2; |
| 1302 | |
Dmitry Shmidt | e466304 | 2016-04-04 10:07:49 -0700 | [diff] [blame] | 1303 | if (TEST_FAIL()) |
| 1304 | return -2; |
| 1305 | |
Dmitry Shmidt | 4171258 | 2015-06-29 11:02:15 -0700 | [diff] [blame] | 1306 | bnctx = BN_CTX_new(); |
| 1307 | if (bnctx == NULL) |
| 1308 | return -2; |
| 1309 | |
| 1310 | exp = BN_new(); |
| 1311 | tmp = BN_new(); |
| 1312 | if (!exp || !tmp || |
| 1313 | /* exp = (p-1) / 2 */ |
| 1314 | !BN_sub(exp, (const BIGNUM *) p, BN_value_one()) || |
| 1315 | !BN_rshift1(exp, exp) || |
| 1316 | !BN_mod_exp(tmp, (const BIGNUM *) a, exp, (const BIGNUM *) p, |
| 1317 | bnctx)) |
| 1318 | goto fail; |
| 1319 | |
| 1320 | if (BN_is_word(tmp, 1)) |
| 1321 | res = 1; |
| 1322 | else if (BN_is_zero(tmp)) |
| 1323 | res = 0; |
| 1324 | else |
| 1325 | res = -1; |
| 1326 | |
| 1327 | fail: |
| 1328 | BN_clear_free(tmp); |
| 1329 | BN_clear_free(exp); |
| 1330 | BN_CTX_free(bnctx); |
| 1331 | return res; |
| 1332 | } |
| 1333 | |
| 1334 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1335 | #ifdef CONFIG_ECC |
| 1336 | |
| 1337 | struct crypto_ec { |
| 1338 | EC_GROUP *group; |
| 1339 | BN_CTX *bnctx; |
| 1340 | BIGNUM *prime; |
| 1341 | BIGNUM *order; |
Dmitry Shmidt | 4171258 | 2015-06-29 11:02:15 -0700 | [diff] [blame] | 1342 | BIGNUM *a; |
| 1343 | BIGNUM *b; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1344 | }; |
| 1345 | |
| 1346 | struct crypto_ec * crypto_ec_init(int group) |
| 1347 | { |
| 1348 | struct crypto_ec *e; |
| 1349 | int nid; |
| 1350 | |
| 1351 | /* Map from IANA registry for IKE D-H groups to OpenSSL NID */ |
| 1352 | switch (group) { |
| 1353 | case 19: |
| 1354 | nid = NID_X9_62_prime256v1; |
| 1355 | break; |
| 1356 | case 20: |
| 1357 | nid = NID_secp384r1; |
| 1358 | break; |
| 1359 | case 21: |
| 1360 | nid = NID_secp521r1; |
| 1361 | break; |
| 1362 | case 25: |
| 1363 | nid = NID_X9_62_prime192v1; |
| 1364 | break; |
| 1365 | case 26: |
| 1366 | nid = NID_secp224r1; |
| 1367 | break; |
Dmitry Shmidt | 4171258 | 2015-06-29 11:02:15 -0700 | [diff] [blame] | 1368 | #ifdef NID_brainpoolP224r1 |
| 1369 | case 27: |
| 1370 | nid = NID_brainpoolP224r1; |
| 1371 | break; |
| 1372 | #endif /* NID_brainpoolP224r1 */ |
| 1373 | #ifdef NID_brainpoolP256r1 |
| 1374 | case 28: |
| 1375 | nid = NID_brainpoolP256r1; |
| 1376 | break; |
| 1377 | #endif /* NID_brainpoolP256r1 */ |
| 1378 | #ifdef NID_brainpoolP384r1 |
| 1379 | case 29: |
| 1380 | nid = NID_brainpoolP384r1; |
| 1381 | break; |
| 1382 | #endif /* NID_brainpoolP384r1 */ |
| 1383 | #ifdef NID_brainpoolP512r1 |
| 1384 | case 30: |
| 1385 | nid = NID_brainpoolP512r1; |
| 1386 | break; |
| 1387 | #endif /* NID_brainpoolP512r1 */ |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1388 | default: |
| 1389 | return NULL; |
| 1390 | } |
| 1391 | |
| 1392 | e = os_zalloc(sizeof(*e)); |
| 1393 | if (e == NULL) |
| 1394 | return NULL; |
| 1395 | |
| 1396 | e->bnctx = BN_CTX_new(); |
| 1397 | e->group = EC_GROUP_new_by_curve_name(nid); |
| 1398 | e->prime = BN_new(); |
| 1399 | e->order = BN_new(); |
Dmitry Shmidt | 4171258 | 2015-06-29 11:02:15 -0700 | [diff] [blame] | 1400 | e->a = BN_new(); |
| 1401 | e->b = BN_new(); |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1402 | if (e->group == NULL || e->bnctx == NULL || e->prime == NULL || |
Dmitry Shmidt | 4171258 | 2015-06-29 11:02:15 -0700 | [diff] [blame] | 1403 | e->order == NULL || e->a == NULL || e->b == NULL || |
| 1404 | !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] | 1405 | !EC_GROUP_get_order(e->group, e->order, e->bnctx)) { |
| 1406 | crypto_ec_deinit(e); |
| 1407 | e = NULL; |
| 1408 | } |
| 1409 | |
| 1410 | return e; |
| 1411 | } |
| 1412 | |
| 1413 | |
| 1414 | void crypto_ec_deinit(struct crypto_ec *e) |
| 1415 | { |
| 1416 | if (e == NULL) |
| 1417 | return; |
Dmitry Shmidt | 4171258 | 2015-06-29 11:02:15 -0700 | [diff] [blame] | 1418 | BN_clear_free(e->b); |
| 1419 | BN_clear_free(e->a); |
Dmitry Shmidt | 7f0b69e | 2014-07-28 10:35:20 -0700 | [diff] [blame] | 1420 | BN_clear_free(e->order); |
Dmitry Shmidt | 661b4f7 | 2014-09-29 14:58:27 -0700 | [diff] [blame] | 1421 | BN_clear_free(e->prime); |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1422 | EC_GROUP_free(e->group); |
| 1423 | BN_CTX_free(e->bnctx); |
| 1424 | os_free(e); |
| 1425 | } |
| 1426 | |
| 1427 | |
| 1428 | struct crypto_ec_point * crypto_ec_point_init(struct crypto_ec *e) |
| 1429 | { |
Dmitry Shmidt | e466304 | 2016-04-04 10:07:49 -0700 | [diff] [blame] | 1430 | if (TEST_FAIL()) |
| 1431 | return NULL; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1432 | if (e == NULL) |
| 1433 | return NULL; |
| 1434 | return (struct crypto_ec_point *) EC_POINT_new(e->group); |
| 1435 | } |
| 1436 | |
| 1437 | |
| 1438 | size_t crypto_ec_prime_len(struct crypto_ec *e) |
| 1439 | { |
| 1440 | return BN_num_bytes(e->prime); |
| 1441 | } |
| 1442 | |
| 1443 | |
| 1444 | size_t crypto_ec_prime_len_bits(struct crypto_ec *e) |
| 1445 | { |
| 1446 | return BN_num_bits(e->prime); |
| 1447 | } |
| 1448 | |
| 1449 | |
| 1450 | const struct crypto_bignum * crypto_ec_get_prime(struct crypto_ec *e) |
| 1451 | { |
| 1452 | return (const struct crypto_bignum *) e->prime; |
| 1453 | } |
| 1454 | |
| 1455 | |
| 1456 | const struct crypto_bignum * crypto_ec_get_order(struct crypto_ec *e) |
| 1457 | { |
| 1458 | return (const struct crypto_bignum *) e->order; |
| 1459 | } |
| 1460 | |
| 1461 | |
| 1462 | void crypto_ec_point_deinit(struct crypto_ec_point *p, int clear) |
| 1463 | { |
| 1464 | if (clear) |
| 1465 | EC_POINT_clear_free((EC_POINT *) p); |
| 1466 | else |
| 1467 | EC_POINT_free((EC_POINT *) p); |
| 1468 | } |
| 1469 | |
| 1470 | |
| 1471 | int crypto_ec_point_to_bin(struct crypto_ec *e, |
| 1472 | const struct crypto_ec_point *point, u8 *x, u8 *y) |
| 1473 | { |
| 1474 | BIGNUM *x_bn, *y_bn; |
| 1475 | int ret = -1; |
| 1476 | int len = BN_num_bytes(e->prime); |
| 1477 | |
Dmitry Shmidt | e466304 | 2016-04-04 10:07:49 -0700 | [diff] [blame] | 1478 | if (TEST_FAIL()) |
| 1479 | return -1; |
| 1480 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1481 | x_bn = BN_new(); |
| 1482 | y_bn = BN_new(); |
| 1483 | |
| 1484 | if (x_bn && y_bn && |
| 1485 | EC_POINT_get_affine_coordinates_GFp(e->group, (EC_POINT *) point, |
| 1486 | x_bn, y_bn, e->bnctx)) { |
| 1487 | if (x) { |
| 1488 | crypto_bignum_to_bin((struct crypto_bignum *) x_bn, |
| 1489 | x, len, len); |
| 1490 | } |
| 1491 | if (y) { |
| 1492 | crypto_bignum_to_bin((struct crypto_bignum *) y_bn, |
| 1493 | y, len, len); |
| 1494 | } |
| 1495 | ret = 0; |
| 1496 | } |
| 1497 | |
Dmitry Shmidt | 7f0b69e | 2014-07-28 10:35:20 -0700 | [diff] [blame] | 1498 | BN_clear_free(x_bn); |
| 1499 | BN_clear_free(y_bn); |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1500 | return ret; |
| 1501 | } |
| 1502 | |
| 1503 | |
| 1504 | struct crypto_ec_point * crypto_ec_point_from_bin(struct crypto_ec *e, |
| 1505 | const u8 *val) |
| 1506 | { |
| 1507 | BIGNUM *x, *y; |
| 1508 | EC_POINT *elem; |
| 1509 | int len = BN_num_bytes(e->prime); |
| 1510 | |
Dmitry Shmidt | e466304 | 2016-04-04 10:07:49 -0700 | [diff] [blame] | 1511 | if (TEST_FAIL()) |
| 1512 | return NULL; |
| 1513 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1514 | x = BN_bin2bn(val, len, NULL); |
| 1515 | y = BN_bin2bn(val + len, len, NULL); |
| 1516 | elem = EC_POINT_new(e->group); |
| 1517 | if (x == NULL || y == NULL || elem == NULL) { |
Dmitry Shmidt | 7f0b69e | 2014-07-28 10:35:20 -0700 | [diff] [blame] | 1518 | BN_clear_free(x); |
| 1519 | BN_clear_free(y); |
| 1520 | EC_POINT_clear_free(elem); |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1521 | return NULL; |
| 1522 | } |
| 1523 | |
| 1524 | if (!EC_POINT_set_affine_coordinates_GFp(e->group, elem, x, y, |
| 1525 | e->bnctx)) { |
Dmitry Shmidt | 7f0b69e | 2014-07-28 10:35:20 -0700 | [diff] [blame] | 1526 | EC_POINT_clear_free(elem); |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1527 | elem = NULL; |
| 1528 | } |
| 1529 | |
Dmitry Shmidt | 7f0b69e | 2014-07-28 10:35:20 -0700 | [diff] [blame] | 1530 | BN_clear_free(x); |
| 1531 | BN_clear_free(y); |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1532 | |
| 1533 | return (struct crypto_ec_point *) elem; |
| 1534 | } |
| 1535 | |
| 1536 | |
| 1537 | int crypto_ec_point_add(struct crypto_ec *e, const struct crypto_ec_point *a, |
| 1538 | const struct crypto_ec_point *b, |
| 1539 | struct crypto_ec_point *c) |
| 1540 | { |
Dmitry Shmidt | e466304 | 2016-04-04 10:07:49 -0700 | [diff] [blame] | 1541 | if (TEST_FAIL()) |
| 1542 | return -1; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1543 | return EC_POINT_add(e->group, (EC_POINT *) c, (const EC_POINT *) a, |
| 1544 | (const EC_POINT *) b, e->bnctx) ? 0 : -1; |
| 1545 | } |
| 1546 | |
| 1547 | |
| 1548 | int crypto_ec_point_mul(struct crypto_ec *e, const struct crypto_ec_point *p, |
| 1549 | const struct crypto_bignum *b, |
| 1550 | struct crypto_ec_point *res) |
| 1551 | { |
Dmitry Shmidt | e466304 | 2016-04-04 10:07:49 -0700 | [diff] [blame] | 1552 | if (TEST_FAIL()) |
| 1553 | return -1; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1554 | return EC_POINT_mul(e->group, (EC_POINT *) res, NULL, |
| 1555 | (const EC_POINT *) p, (const BIGNUM *) b, e->bnctx) |
| 1556 | ? 0 : -1; |
| 1557 | } |
| 1558 | |
| 1559 | |
| 1560 | int crypto_ec_point_invert(struct crypto_ec *e, struct crypto_ec_point *p) |
| 1561 | { |
Dmitry Shmidt | e466304 | 2016-04-04 10:07:49 -0700 | [diff] [blame] | 1562 | if (TEST_FAIL()) |
| 1563 | return -1; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1564 | return EC_POINT_invert(e->group, (EC_POINT *) p, e->bnctx) ? 0 : -1; |
| 1565 | } |
| 1566 | |
| 1567 | |
| 1568 | int crypto_ec_point_solve_y_coord(struct crypto_ec *e, |
| 1569 | struct crypto_ec_point *p, |
| 1570 | const struct crypto_bignum *x, int y_bit) |
| 1571 | { |
Dmitry Shmidt | e466304 | 2016-04-04 10:07:49 -0700 | [diff] [blame] | 1572 | if (TEST_FAIL()) |
| 1573 | return -1; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1574 | if (!EC_POINT_set_compressed_coordinates_GFp(e->group, (EC_POINT *) p, |
| 1575 | (const BIGNUM *) x, y_bit, |
| 1576 | e->bnctx) || |
| 1577 | !EC_POINT_is_on_curve(e->group, (EC_POINT *) p, e->bnctx)) |
| 1578 | return -1; |
| 1579 | return 0; |
| 1580 | } |
| 1581 | |
| 1582 | |
Dmitry Shmidt | 4171258 | 2015-06-29 11:02:15 -0700 | [diff] [blame] | 1583 | struct crypto_bignum * |
| 1584 | crypto_ec_point_compute_y_sqr(struct crypto_ec *e, |
| 1585 | const struct crypto_bignum *x) |
| 1586 | { |
| 1587 | BIGNUM *tmp, *tmp2, *y_sqr = NULL; |
| 1588 | |
Dmitry Shmidt | e466304 | 2016-04-04 10:07:49 -0700 | [diff] [blame] | 1589 | if (TEST_FAIL()) |
| 1590 | return NULL; |
| 1591 | |
Dmitry Shmidt | 4171258 | 2015-06-29 11:02:15 -0700 | [diff] [blame] | 1592 | tmp = BN_new(); |
| 1593 | tmp2 = BN_new(); |
| 1594 | |
| 1595 | /* y^2 = x^3 + ax + b */ |
| 1596 | if (tmp && tmp2 && |
| 1597 | BN_mod_sqr(tmp, (const BIGNUM *) x, e->prime, e->bnctx) && |
| 1598 | BN_mod_mul(tmp, tmp, (const BIGNUM *) x, e->prime, e->bnctx) && |
| 1599 | BN_mod_mul(tmp2, e->a, (const BIGNUM *) x, e->prime, e->bnctx) && |
| 1600 | BN_mod_add_quick(tmp2, tmp2, tmp, e->prime) && |
| 1601 | BN_mod_add_quick(tmp2, tmp2, e->b, e->prime)) { |
| 1602 | y_sqr = tmp2; |
| 1603 | tmp2 = NULL; |
| 1604 | } |
| 1605 | |
| 1606 | BN_clear_free(tmp); |
| 1607 | BN_clear_free(tmp2); |
| 1608 | |
| 1609 | return (struct crypto_bignum *) y_sqr; |
| 1610 | } |
| 1611 | |
| 1612 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1613 | int crypto_ec_point_is_at_infinity(struct crypto_ec *e, |
| 1614 | const struct crypto_ec_point *p) |
| 1615 | { |
| 1616 | return EC_POINT_is_at_infinity(e->group, (const EC_POINT *) p); |
| 1617 | } |
| 1618 | |
| 1619 | |
| 1620 | int crypto_ec_point_is_on_curve(struct crypto_ec *e, |
| 1621 | const struct crypto_ec_point *p) |
| 1622 | { |
Dmitry Shmidt | 4171258 | 2015-06-29 11:02:15 -0700 | [diff] [blame] | 1623 | return EC_POINT_is_on_curve(e->group, (const EC_POINT *) p, |
| 1624 | e->bnctx) == 1; |
| 1625 | } |
| 1626 | |
| 1627 | |
| 1628 | int crypto_ec_point_cmp(const struct crypto_ec *e, |
| 1629 | const struct crypto_ec_point *a, |
| 1630 | const struct crypto_ec_point *b) |
| 1631 | { |
| 1632 | return EC_POINT_cmp(e->group, (const EC_POINT *) a, |
| 1633 | (const EC_POINT *) b, e->bnctx); |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1634 | } |
| 1635 | |
| 1636 | #endif /* CONFIG_ECC */ |