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 |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 3 | * Copyright (c) 2004-2022, 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> |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 19 | #include <openssl/pem.h> |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 20 | #ifdef CONFIG_ECC |
| 21 | #include <openssl/ec.h> |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 22 | #include <openssl/x509.h> |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 23 | #endif /* CONFIG_ECC */ |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 24 | #if OPENSSL_VERSION_NUMBER >= 0x30000000L |
| 25 | #include <openssl/provider.h> |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 26 | #include <openssl/core_names.h> |
| 27 | #include <openssl/param_build.h> |
Sunil | 8cd6f4d | 2022-06-28 18:40:46 +0000 | [diff] [blame] | 28 | #include <openssl/rsa.h> |
| 29 | #include <openssl/encoder.h> |
| 30 | #include <openssl/decoder.h> |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 31 | #else /* OpenSSL version >= 3.0 */ |
| 32 | #include <openssl/cmac.h> |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 33 | #endif /* OpenSSL version >= 3.0 */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 34 | |
| 35 | #include "common.h" |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 36 | #include "utils/const_time.h" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 37 | #include "wpabuf.h" |
| 38 | #include "dh_group5.h" |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 39 | #include "sha1.h" |
| 40 | #include "sha256.h" |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 41 | #include "sha384.h" |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 42 | #include "sha512.h" |
Dmitry Shmidt | 4ae50e6 | 2016-06-27 13:48:39 -0700 | [diff] [blame] | 43 | #include "md5.h" |
| 44 | #include "aes_wrap.h" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 45 | #include "crypto.h" |
| 46 | |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 47 | #if OPENSSL_VERSION_NUMBER < 0x10100000L |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 48 | /* Compatibility wrappers for older versions. */ |
| 49 | |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 50 | static HMAC_CTX * HMAC_CTX_new(void) |
| 51 | { |
| 52 | HMAC_CTX *ctx; |
| 53 | |
| 54 | ctx = os_zalloc(sizeof(*ctx)); |
| 55 | if (ctx) |
| 56 | HMAC_CTX_init(ctx); |
| 57 | return ctx; |
| 58 | } |
| 59 | |
| 60 | |
| 61 | static void HMAC_CTX_free(HMAC_CTX *ctx) |
| 62 | { |
Dmitry Shmidt | 7f2c753 | 2016-08-15 09:48:12 -0700 | [diff] [blame] | 63 | if (!ctx) |
| 64 | return; |
Dmitry Shmidt | 57c2d39 | 2016-02-23 13:40:19 -0800 | [diff] [blame] | 65 | HMAC_CTX_cleanup(ctx); |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 66 | bin_clear_free(ctx, sizeof(*ctx)); |
| 67 | } |
| 68 | |
| 69 | |
| 70 | static EVP_MD_CTX * EVP_MD_CTX_new(void) |
| 71 | { |
| 72 | EVP_MD_CTX *ctx; |
| 73 | |
| 74 | ctx = os_zalloc(sizeof(*ctx)); |
| 75 | if (ctx) |
| 76 | EVP_MD_CTX_init(ctx); |
| 77 | return ctx; |
| 78 | } |
| 79 | |
| 80 | |
| 81 | static void EVP_MD_CTX_free(EVP_MD_CTX *ctx) |
| 82 | { |
Dmitry Shmidt | 7f2c753 | 2016-08-15 09:48:12 -0700 | [diff] [blame] | 83 | if (!ctx) |
| 84 | return; |
| 85 | EVP_MD_CTX_cleanup(ctx); |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 86 | bin_clear_free(ctx, sizeof(*ctx)); |
| 87 | } |
| 88 | |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 89 | |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 90 | #ifdef CONFIG_ECC |
| 91 | |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 92 | static EC_KEY * EVP_PKEY_get0_EC_KEY(EVP_PKEY *pkey) |
| 93 | { |
| 94 | if (pkey->type != EVP_PKEY_EC) |
| 95 | return NULL; |
| 96 | return pkey->pkey.ec; |
| 97 | } |
| 98 | |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 99 | |
| 100 | static int ECDSA_SIG_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s) |
| 101 | { |
| 102 | sig->r = r; |
| 103 | sig->s = s; |
| 104 | return 1; |
| 105 | } |
| 106 | |
| 107 | |
| 108 | static void ECDSA_SIG_get0(const ECDSA_SIG *sig, const BIGNUM **pr, |
| 109 | const BIGNUM **ps) |
| 110 | { |
| 111 | if (pr) |
| 112 | *pr = sig->r; |
| 113 | if (ps) |
| 114 | *ps = sig->s; |
| 115 | } |
| 116 | |
| 117 | #endif /* CONFIG_ECC */ |
| 118 | |
| 119 | static const unsigned char * ASN1_STRING_get0_data(const ASN1_STRING *x) |
| 120 | { |
| 121 | return ASN1_STRING_data((ASN1_STRING *) x); |
| 122 | } |
Sunil | 8cd6f4d | 2022-06-28 18:40:46 +0000 | [diff] [blame] | 123 | |
| 124 | |
| 125 | static const ASN1_TIME * X509_get0_notBefore(const X509 *x) |
| 126 | { |
| 127 | return X509_get_notBefore(x); |
| 128 | } |
| 129 | |
| 130 | |
| 131 | static const ASN1_TIME * X509_get0_notAfter(const X509 *x) |
| 132 | { |
| 133 | return X509_get_notAfter(x); |
| 134 | } |
| 135 | |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 136 | #endif /* OpenSSL version < 1.1.0 */ |
| 137 | |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 138 | |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 139 | #if OPENSSL_VERSION_NUMBER < 0x10101000L || \ |
| 140 | (defined(LIBRESSL_VERSION_NUMBER) && \ |
| 141 | LIBRESSL_VERSION_NUMBER < 0x30400000L) |
| 142 | |
| 143 | static int EC_POINT_get_affine_coordinates(const EC_GROUP *group, |
| 144 | const EC_POINT *point, BIGNUM *x, |
| 145 | BIGNUM *y, BN_CTX *ctx) |
| 146 | { |
| 147 | return EC_POINT_get_affine_coordinates_GFp(group, point, x, y, ctx); |
| 148 | } |
| 149 | |
| 150 | |
| 151 | static int EC_POINT_set_affine_coordinates(const EC_GROUP *group, |
| 152 | EC_POINT *point, const BIGNUM *x, |
| 153 | const BIGNUM *y, BN_CTX *ctx) |
| 154 | { |
| 155 | return EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx); |
| 156 | } |
| 157 | |
| 158 | #endif /* OpenSSL version < 1.1.1 */ |
| 159 | |
| 160 | |
| 161 | #if OPENSSL_VERSION_NUMBER < 0x10101000L || \ |
| 162 | defined(OPENSSL_IS_BORINGSSL) || \ |
| 163 | (defined(LIBRESSL_VERSION_NUMBER) && \ |
| 164 | LIBRESSL_VERSION_NUMBER < 0x30400000L) |
| 165 | |
| 166 | static int EC_POINT_set_compressed_coordinates(const EC_GROUP *group, |
| 167 | EC_POINT *point, const BIGNUM *x, |
| 168 | int y_bit, BN_CTX *ctx) |
| 169 | { |
| 170 | return EC_POINT_set_compressed_coordinates_GFp(group, point, x, y_bit, |
| 171 | ctx); |
| 172 | } |
| 173 | |
| 174 | |
| 175 | static int EC_GROUP_get_curve(const EC_GROUP *group, BIGNUM *p, BIGNUM *a, |
| 176 | BIGNUM *b, BN_CTX *ctx) |
| 177 | { |
| 178 | return EC_GROUP_get_curve_GFp(group, p, a, b, ctx); |
| 179 | } |
| 180 | |
| 181 | #endif /* OpenSSL version < 1.1.1 */ |
| 182 | |
| 183 | |
| 184 | #if OPENSSL_VERSION_NUMBER >= 0x30000000L |
| 185 | static OSSL_PROVIDER *openssl_default_provider = NULL; |
| 186 | static OSSL_PROVIDER *openssl_legacy_provider = NULL; |
| 187 | #endif /* OpenSSL version >= 3.0 */ |
| 188 | |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 189 | void openssl_load_legacy_provider(void) |
| 190 | { |
| 191 | #if OPENSSL_VERSION_NUMBER >= 0x30000000L |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 192 | if (openssl_legacy_provider) |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 193 | return; |
| 194 | |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 195 | openssl_legacy_provider = OSSL_PROVIDER_load(NULL, "legacy"); |
| 196 | if (openssl_legacy_provider && !openssl_default_provider) |
| 197 | openssl_default_provider = OSSL_PROVIDER_load(NULL, "default"); |
| 198 | #endif /* OpenSSL version >= 3.0 */ |
| 199 | } |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 200 | |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 201 | |
| 202 | static void openssl_unload_legacy_provider(void) |
| 203 | { |
| 204 | #if OPENSSL_VERSION_NUMBER >= 0x30000000L |
| 205 | if (openssl_legacy_provider) { |
| 206 | OSSL_PROVIDER_unload(openssl_legacy_provider); |
| 207 | openssl_legacy_provider = NULL; |
| 208 | } |
| 209 | if (openssl_default_provider) { |
| 210 | OSSL_PROVIDER_unload(openssl_default_provider); |
| 211 | openssl_default_provider = NULL; |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 212 | } |
| 213 | #endif /* OpenSSL version >= 3.0 */ |
| 214 | } |
| 215 | |
| 216 | |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 217 | #if OPENSSL_VERSION_NUMBER < 0x30000000L |
| 218 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 219 | static BIGNUM * get_group5_prime(void) |
| 220 | { |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 221 | #if OPENSSL_VERSION_NUMBER >= 0x10100000L |
Dmitry Shmidt | 7f2c753 | 2016-08-15 09:48:12 -0700 | [diff] [blame] | 222 | return BN_get_rfc3526_prime_1536(NULL); |
| 223 | #elif !defined(OPENSSL_IS_BORINGSSL) |
| 224 | return get_rfc3526_prime_1536(NULL); |
| 225 | #else |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 226 | static const unsigned char RFC3526_PRIME_1536[] = { |
| 227 | 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xC9,0x0F,0xDA,0xA2, |
| 228 | 0x21,0x68,0xC2,0x34,0xC4,0xC6,0x62,0x8B,0x80,0xDC,0x1C,0xD1, |
| 229 | 0x29,0x02,0x4E,0x08,0x8A,0x67,0xCC,0x74,0x02,0x0B,0xBE,0xA6, |
| 230 | 0x3B,0x13,0x9B,0x22,0x51,0x4A,0x08,0x79,0x8E,0x34,0x04,0xDD, |
| 231 | 0xEF,0x95,0x19,0xB3,0xCD,0x3A,0x43,0x1B,0x30,0x2B,0x0A,0x6D, |
| 232 | 0xF2,0x5F,0x14,0x37,0x4F,0xE1,0x35,0x6D,0x6D,0x51,0xC2,0x45, |
| 233 | 0xE4,0x85,0xB5,0x76,0x62,0x5E,0x7E,0xC6,0xF4,0x4C,0x42,0xE9, |
| 234 | 0xA6,0x37,0xED,0x6B,0x0B,0xFF,0x5C,0xB6,0xF4,0x06,0xB7,0xED, |
| 235 | 0xEE,0x38,0x6B,0xFB,0x5A,0x89,0x9F,0xA5,0xAE,0x9F,0x24,0x11, |
| 236 | 0x7C,0x4B,0x1F,0xE6,0x49,0x28,0x66,0x51,0xEC,0xE4,0x5B,0x3D, |
| 237 | 0xC2,0x00,0x7C,0xB8,0xA1,0x63,0xBF,0x05,0x98,0xDA,0x48,0x36, |
| 238 | 0x1C,0x55,0xD3,0x9A,0x69,0x16,0x3F,0xA8,0xFD,0x24,0xCF,0x5F, |
| 239 | 0x83,0x65,0x5D,0x23,0xDC,0xA3,0xAD,0x96,0x1C,0x62,0xF3,0x56, |
| 240 | 0x20,0x85,0x52,0xBB,0x9E,0xD5,0x29,0x07,0x70,0x96,0x96,0x6D, |
| 241 | 0x67,0x0C,0x35,0x4E,0x4A,0xBC,0x98,0x04,0xF1,0x74,0x6C,0x08, |
| 242 | 0xCA,0x23,0x73,0x27,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, |
| 243 | }; |
| 244 | return BN_bin2bn(RFC3526_PRIME_1536, sizeof(RFC3526_PRIME_1536), NULL); |
Dmitry Shmidt | 7f2c753 | 2016-08-15 09:48:12 -0700 | [diff] [blame] | 245 | #endif |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 246 | } |
| 247 | |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 248 | |
| 249 | static BIGNUM * get_group5_order(void) |
| 250 | { |
| 251 | static const unsigned char RFC3526_ORDER_1536[] = { |
| 252 | 0x7F,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xE4,0x87,0xED,0x51, |
| 253 | 0x10,0xB4,0x61,0x1A,0x62,0x63,0x31,0x45,0xC0,0x6E,0x0E,0x68, |
| 254 | 0x94,0x81,0x27,0x04,0x45,0x33,0xE6,0x3A,0x01,0x05,0xDF,0x53, |
| 255 | 0x1D,0x89,0xCD,0x91,0x28,0xA5,0x04,0x3C,0xC7,0x1A,0x02,0x6E, |
| 256 | 0xF7,0xCA,0x8C,0xD9,0xE6,0x9D,0x21,0x8D,0x98,0x15,0x85,0x36, |
| 257 | 0xF9,0x2F,0x8A,0x1B,0xA7,0xF0,0x9A,0xB6,0xB6,0xA8,0xE1,0x22, |
| 258 | 0xF2,0x42,0xDA,0xBB,0x31,0x2F,0x3F,0x63,0x7A,0x26,0x21,0x74, |
| 259 | 0xD3,0x1B,0xF6,0xB5,0x85,0xFF,0xAE,0x5B,0x7A,0x03,0x5B,0xF6, |
| 260 | 0xF7,0x1C,0x35,0xFD,0xAD,0x44,0xCF,0xD2,0xD7,0x4F,0x92,0x08, |
| 261 | 0xBE,0x25,0x8F,0xF3,0x24,0x94,0x33,0x28,0xF6,0x72,0x2D,0x9E, |
| 262 | 0xE1,0x00,0x3E,0x5C,0x50,0xB1,0xDF,0x82,0xCC,0x6D,0x24,0x1B, |
| 263 | 0x0E,0x2A,0xE9,0xCD,0x34,0x8B,0x1F,0xD4,0x7E,0x92,0x67,0xAF, |
| 264 | 0xC1,0xB2,0xAE,0x91,0xEE,0x51,0xD6,0xCB,0x0E,0x31,0x79,0xAB, |
| 265 | 0x10,0x42,0xA9,0x5D,0xCF,0x6A,0x94,0x83,0xB8,0x4B,0x4B,0x36, |
| 266 | 0xB3,0x86,0x1A,0xA7,0x25,0x5E,0x4C,0x02,0x78,0xBA,0x36,0x04, |
| 267 | 0x65,0x11,0xB9,0x93,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF |
| 268 | }; |
| 269 | return BN_bin2bn(RFC3526_ORDER_1536, sizeof(RFC3526_ORDER_1536), NULL); |
| 270 | } |
| 271 | |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 272 | #endif /* OpenSSL version < 3.0 */ |
| 273 | |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 274 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 275 | #ifdef OPENSSL_NO_SHA256 |
| 276 | #define NO_SHA256_WRAPPER |
| 277 | #endif |
Paul Stewart | 092955c | 2017-02-06 09:13:09 -0800 | [diff] [blame] | 278 | #ifdef OPENSSL_NO_SHA512 |
| 279 | #define NO_SHA384_WRAPPER |
| 280 | #endif |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 281 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 282 | static int openssl_digest_vector(const EVP_MD *type, size_t num_elem, |
| 283 | const u8 *addr[], const size_t *len, u8 *mac) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 284 | { |
Dmitry Shmidt | 55840ad | 2015-12-14 12:45:46 -0800 | [diff] [blame] | 285 | EVP_MD_CTX *ctx; |
| 286 | size_t i; |
| 287 | unsigned int mac_len; |
| 288 | |
| 289 | if (TEST_FAIL()) |
| 290 | return -1; |
| 291 | |
| 292 | ctx = EVP_MD_CTX_new(); |
| 293 | if (!ctx) |
| 294 | return -1; |
| 295 | if (!EVP_DigestInit_ex(ctx, type, NULL)) { |
| 296 | wpa_printf(MSG_ERROR, "OpenSSL: EVP_DigestInit_ex failed: %s", |
| 297 | ERR_error_string(ERR_get_error(), NULL)); |
| 298 | EVP_MD_CTX_free(ctx); |
| 299 | return -1; |
| 300 | } |
| 301 | for (i = 0; i < num_elem; i++) { |
| 302 | if (!EVP_DigestUpdate(ctx, addr[i], len[i])) { |
| 303 | wpa_printf(MSG_ERROR, "OpenSSL: EVP_DigestUpdate " |
| 304 | "failed: %s", |
| 305 | ERR_error_string(ERR_get_error(), NULL)); |
| 306 | EVP_MD_CTX_free(ctx); |
| 307 | return -1; |
| 308 | } |
| 309 | } |
| 310 | if (!EVP_DigestFinal(ctx, mac, &mac_len)) { |
| 311 | wpa_printf(MSG_ERROR, "OpenSSL: EVP_DigestFinal failed: %s", |
| 312 | ERR_error_string(ERR_get_error(), NULL)); |
| 313 | EVP_MD_CTX_free(ctx); |
| 314 | return -1; |
| 315 | } |
| 316 | EVP_MD_CTX_free(ctx); |
| 317 | |
| 318 | return 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 322 | #ifndef CONFIG_FIPS |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 323 | int md4_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac) |
| 324 | { |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 325 | openssl_load_legacy_provider(); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 326 | return openssl_digest_vector(EVP_md4(), num_elem, addr, len, mac); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 327 | } |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 328 | #endif /* CONFIG_FIPS */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 329 | |
| 330 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 331 | int des_encrypt(const u8 *clear, const u8 *key, u8 *cypher) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 332 | { |
| 333 | u8 pkey[8], next, tmp; |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 334 | int i, plen, ret = -1; |
| 335 | EVP_CIPHER_CTX *ctx; |
| 336 | |
| 337 | openssl_load_legacy_provider(); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 338 | |
| 339 | /* Add parity bits to the key */ |
| 340 | next = 0; |
| 341 | for (i = 0; i < 7; i++) { |
| 342 | tmp = key[i]; |
| 343 | pkey[i] = (tmp >> i) | next | 1; |
| 344 | next = tmp << (7 - i); |
| 345 | } |
| 346 | pkey[i] = next | 1; |
| 347 | |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 348 | ctx = EVP_CIPHER_CTX_new(); |
| 349 | if (ctx && |
| 350 | EVP_EncryptInit_ex(ctx, EVP_des_ecb(), NULL, pkey, NULL) == 1 && |
| 351 | EVP_CIPHER_CTX_set_padding(ctx, 0) == 1 && |
| 352 | EVP_EncryptUpdate(ctx, cypher, &plen, clear, 8) == 1 && |
| 353 | EVP_EncryptFinal_ex(ctx, &cypher[plen], &plen) == 1) |
| 354 | ret = 0; |
| 355 | else |
| 356 | wpa_printf(MSG_ERROR, "OpenSSL: DES encrypt failed"); |
| 357 | |
| 358 | if (ctx) |
| 359 | EVP_CIPHER_CTX_free(ctx); |
| 360 | return ret; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 364 | #ifndef CONFIG_NO_RC4 |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 365 | int rc4_skip(const u8 *key, size_t keylen, size_t skip, |
| 366 | u8 *data, size_t data_len) |
| 367 | { |
| 368 | #ifdef OPENSSL_NO_RC4 |
| 369 | return -1; |
| 370 | #else /* OPENSSL_NO_RC4 */ |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 371 | EVP_CIPHER_CTX *ctx; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 372 | int outl; |
| 373 | int res = -1; |
| 374 | unsigned char skip_buf[16]; |
| 375 | |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 376 | openssl_load_legacy_provider(); |
| 377 | |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 378 | ctx = EVP_CIPHER_CTX_new(); |
| 379 | if (!ctx || |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 380 | !EVP_CipherInit_ex(ctx, EVP_rc4(), NULL, NULL, NULL, 1) || |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 381 | !EVP_CIPHER_CTX_set_padding(ctx, 0) || |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 382 | !EVP_CIPHER_CTX_set_key_length(ctx, keylen) || |
| 383 | !EVP_CipherInit_ex(ctx, NULL, NULL, key, NULL, 1)) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 384 | goto out; |
| 385 | |
| 386 | while (skip >= sizeof(skip_buf)) { |
| 387 | size_t len = skip; |
| 388 | if (len > sizeof(skip_buf)) |
| 389 | len = sizeof(skip_buf); |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 390 | if (!EVP_CipherUpdate(ctx, skip_buf, &outl, skip_buf, len)) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 391 | goto out; |
| 392 | skip -= len; |
| 393 | } |
| 394 | |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 395 | if (EVP_CipherUpdate(ctx, data, &outl, data, data_len)) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 396 | res = 0; |
| 397 | |
| 398 | out: |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 399 | if (ctx) |
Dmitry Shmidt | 57c2d39 | 2016-02-23 13:40:19 -0800 | [diff] [blame] | 400 | EVP_CIPHER_CTX_free(ctx); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 401 | return res; |
| 402 | #endif /* OPENSSL_NO_RC4 */ |
| 403 | } |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 404 | #endif /* CONFIG_NO_RC4 */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 405 | |
| 406 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 407 | #ifndef CONFIG_FIPS |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 408 | int md5_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac) |
| 409 | { |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 410 | return openssl_digest_vector(EVP_md5(), num_elem, addr, len, mac); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 411 | } |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 412 | #endif /* CONFIG_FIPS */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 413 | |
| 414 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 415 | int sha1_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac) |
| 416 | { |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 417 | return openssl_digest_vector(EVP_sha1(), num_elem, addr, len, mac); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | |
| 421 | #ifndef NO_SHA256_WRAPPER |
| 422 | int sha256_vector(size_t num_elem, const u8 *addr[], const size_t *len, |
| 423 | u8 *mac) |
| 424 | { |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 425 | return openssl_digest_vector(EVP_sha256(), num_elem, addr, len, mac); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 426 | } |
| 427 | #endif /* NO_SHA256_WRAPPER */ |
| 428 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 429 | |
Paul Stewart | 092955c | 2017-02-06 09:13:09 -0800 | [diff] [blame] | 430 | #ifndef NO_SHA384_WRAPPER |
| 431 | int sha384_vector(size_t num_elem, const u8 *addr[], const size_t *len, |
| 432 | u8 *mac) |
| 433 | { |
| 434 | return openssl_digest_vector(EVP_sha384(), num_elem, addr, len, mac); |
| 435 | } |
| 436 | #endif /* NO_SHA384_WRAPPER */ |
| 437 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 438 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 439 | #ifndef NO_SHA512_WRAPPER |
| 440 | int sha512_vector(size_t num_elem, const u8 *addr[], const size_t *len, |
| 441 | u8 *mac) |
| 442 | { |
| 443 | return openssl_digest_vector(EVP_sha512(), num_elem, addr, len, mac); |
| 444 | } |
| 445 | #endif /* NO_SHA512_WRAPPER */ |
| 446 | |
| 447 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 448 | static const EVP_CIPHER * aes_get_evp_cipher(size_t keylen) |
| 449 | { |
| 450 | switch (keylen) { |
| 451 | case 16: |
| 452 | return EVP_aes_128_ecb(); |
| 453 | case 24: |
| 454 | return EVP_aes_192_ecb(); |
| 455 | case 32: |
| 456 | return EVP_aes_256_ecb(); |
| 457 | } |
| 458 | |
| 459 | return NULL; |
| 460 | } |
| 461 | |
| 462 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 463 | void * aes_encrypt_init(const u8 *key, size_t len) |
| 464 | { |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 465 | EVP_CIPHER_CTX *ctx; |
| 466 | const EVP_CIPHER *type; |
| 467 | |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 468 | if (TEST_FAIL()) |
| 469 | return NULL; |
| 470 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 471 | type = aes_get_evp_cipher(len); |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 472 | if (!type) { |
| 473 | wpa_printf(MSG_INFO, "%s: Unsupported len=%u", |
| 474 | __func__, (unsigned int) len); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 475 | return NULL; |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 476 | } |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 477 | |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 478 | ctx = EVP_CIPHER_CTX_new(); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 479 | if (ctx == NULL) |
| 480 | return NULL; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 481 | if (EVP_EncryptInit_ex(ctx, type, NULL, key, NULL) != 1) { |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 482 | EVP_CIPHER_CTX_free(ctx); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 483 | return NULL; |
| 484 | } |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 485 | EVP_CIPHER_CTX_set_padding(ctx, 0); |
| 486 | return ctx; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 490 | int aes_encrypt(void *ctx, const u8 *plain, u8 *crypt) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 491 | { |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 492 | EVP_CIPHER_CTX *c = ctx; |
| 493 | int clen = 16; |
| 494 | if (EVP_EncryptUpdate(c, crypt, &clen, plain, 16) != 1) { |
| 495 | wpa_printf(MSG_ERROR, "OpenSSL: EVP_EncryptUpdate failed: %s", |
| 496 | ERR_error_string(ERR_get_error(), NULL)); |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 497 | return -1; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 498 | } |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 499 | return 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | |
| 503 | void aes_encrypt_deinit(void *ctx) |
| 504 | { |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 505 | EVP_CIPHER_CTX *c = ctx; |
| 506 | u8 buf[16]; |
| 507 | int len = sizeof(buf); |
| 508 | if (EVP_EncryptFinal_ex(c, buf, &len) != 1) { |
| 509 | wpa_printf(MSG_ERROR, "OpenSSL: EVP_EncryptFinal_ex failed: " |
| 510 | "%s", ERR_error_string(ERR_get_error(), NULL)); |
| 511 | } |
| 512 | if (len != 0) { |
| 513 | wpa_printf(MSG_ERROR, "OpenSSL: Unexpected padding length %d " |
| 514 | "in AES encrypt", len); |
| 515 | } |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 516 | EVP_CIPHER_CTX_free(c); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | |
| 520 | void * aes_decrypt_init(const u8 *key, size_t len) |
| 521 | { |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 522 | EVP_CIPHER_CTX *ctx; |
| 523 | const EVP_CIPHER *type; |
| 524 | |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 525 | if (TEST_FAIL()) |
| 526 | return NULL; |
| 527 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 528 | type = aes_get_evp_cipher(len); |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 529 | if (!type) { |
| 530 | wpa_printf(MSG_INFO, "%s: Unsupported len=%u", |
| 531 | __func__, (unsigned int) len); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 532 | return NULL; |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 533 | } |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 534 | |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 535 | ctx = EVP_CIPHER_CTX_new(); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 536 | if (ctx == NULL) |
| 537 | return NULL; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 538 | if (EVP_DecryptInit_ex(ctx, type, NULL, key, NULL) != 1) { |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 539 | EVP_CIPHER_CTX_free(ctx); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 540 | return NULL; |
| 541 | } |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 542 | EVP_CIPHER_CTX_set_padding(ctx, 0); |
| 543 | return ctx; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 547 | int aes_decrypt(void *ctx, const u8 *crypt, u8 *plain) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 548 | { |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 549 | EVP_CIPHER_CTX *c = ctx; |
| 550 | int plen = 16; |
| 551 | if (EVP_DecryptUpdate(c, plain, &plen, crypt, 16) != 1) { |
| 552 | wpa_printf(MSG_ERROR, "OpenSSL: EVP_DecryptUpdate failed: %s", |
| 553 | ERR_error_string(ERR_get_error(), NULL)); |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 554 | return -1; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 555 | } |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 556 | return 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | |
| 560 | void aes_decrypt_deinit(void *ctx) |
| 561 | { |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 562 | EVP_CIPHER_CTX *c = ctx; |
| 563 | u8 buf[16]; |
| 564 | int len = sizeof(buf); |
| 565 | if (EVP_DecryptFinal_ex(c, buf, &len) != 1) { |
| 566 | wpa_printf(MSG_ERROR, "OpenSSL: EVP_DecryptFinal_ex failed: " |
| 567 | "%s", ERR_error_string(ERR_get_error(), NULL)); |
| 568 | } |
| 569 | if (len != 0) { |
| 570 | wpa_printf(MSG_ERROR, "OpenSSL: Unexpected padding length %d " |
| 571 | "in AES decrypt", len); |
| 572 | } |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 573 | EVP_CIPHER_CTX_free(c); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 577 | #ifndef CONFIG_FIPS |
| 578 | #ifndef CONFIG_OPENSSL_INTERNAL_AES_WRAP |
| 579 | |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 580 | #if OPENSSL_VERSION_NUMBER >= 0x30000000L |
| 581 | static const EVP_CIPHER * aes_get_evp_wrap_cipher(size_t keylen) |
| 582 | { |
| 583 | switch (keylen) { |
| 584 | case 16: |
| 585 | return EVP_aes_128_wrap(); |
| 586 | case 24: |
| 587 | return EVP_aes_192_wrap(); |
| 588 | case 32: |
| 589 | return EVP_aes_256_wrap(); |
| 590 | default: |
| 591 | return NULL; |
| 592 | } |
| 593 | } |
| 594 | #endif /* OpenSSL version >= 3.0 */ |
| 595 | |
| 596 | |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 597 | int aes_wrap(const u8 *kek, size_t kek_len, int n, const u8 *plain, u8 *cipher) |
| 598 | { |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 599 | #if OPENSSL_VERSION_NUMBER >= 0x30000000L |
| 600 | EVP_CIPHER_CTX *ctx; |
| 601 | const EVP_CIPHER *type; |
| 602 | int ret = -1, len; |
| 603 | u8 buf[16]; |
| 604 | |
| 605 | if (TEST_FAIL()) |
| 606 | return -1; |
| 607 | |
| 608 | type = aes_get_evp_wrap_cipher(kek_len); |
| 609 | if (!type) |
| 610 | return -1; |
| 611 | |
| 612 | ctx = EVP_CIPHER_CTX_new(); |
| 613 | if (!ctx) |
| 614 | return -1; |
| 615 | |
| 616 | if (EVP_EncryptInit_ex(ctx, type, NULL, kek, NULL) == 1 && |
| 617 | EVP_CIPHER_CTX_set_padding(ctx, 0) == 1 && |
| 618 | EVP_EncryptUpdate(ctx, cipher, &len, plain, n * 8) == 1 && |
| 619 | len == (n + 1) * 8 && |
| 620 | EVP_EncryptFinal_ex(ctx, buf, &len) == 1) |
| 621 | ret = 0; |
| 622 | |
| 623 | EVP_CIPHER_CTX_free(ctx); |
| 624 | return ret; |
| 625 | #else /* OpenSSL version >= 3.0 */ |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 626 | AES_KEY actx; |
| 627 | int res; |
| 628 | |
Dmitry Shmidt | ebd93af | 2017-02-21 13:40:44 -0800 | [diff] [blame] | 629 | if (TEST_FAIL()) |
| 630 | return -1; |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 631 | if (AES_set_encrypt_key(kek, kek_len << 3, &actx)) |
| 632 | return -1; |
| 633 | res = AES_wrap_key(&actx, NULL, cipher, plain, n * 8); |
| 634 | OPENSSL_cleanse(&actx, sizeof(actx)); |
| 635 | return res <= 0 ? -1 : 0; |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 636 | #endif /* OpenSSL version >= 3.0 */ |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | |
| 640 | int aes_unwrap(const u8 *kek, size_t kek_len, int n, const u8 *cipher, |
| 641 | u8 *plain) |
| 642 | { |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 643 | #if OPENSSL_VERSION_NUMBER >= 0x30000000L |
| 644 | EVP_CIPHER_CTX *ctx; |
| 645 | const EVP_CIPHER *type; |
| 646 | int ret = -1, len; |
| 647 | u8 buf[16]; |
| 648 | |
| 649 | if (TEST_FAIL()) |
| 650 | return -1; |
| 651 | |
| 652 | type = aes_get_evp_wrap_cipher(kek_len); |
| 653 | if (!type) |
| 654 | return -1; |
| 655 | |
| 656 | ctx = EVP_CIPHER_CTX_new(); |
| 657 | if (!ctx) |
| 658 | return -1; |
| 659 | |
| 660 | if (EVP_DecryptInit_ex(ctx, type, NULL, kek, NULL) == 1 && |
| 661 | EVP_CIPHER_CTX_set_padding(ctx, 0) == 1 && |
| 662 | EVP_DecryptUpdate(ctx, plain, &len, cipher, (n + 1) * 8) == 1 && |
| 663 | len == n * 8 && |
| 664 | EVP_DecryptFinal_ex(ctx, buf, &len) == 1) |
| 665 | ret = 0; |
| 666 | |
| 667 | EVP_CIPHER_CTX_free(ctx); |
| 668 | return ret; |
| 669 | #else /* OpenSSL version >= 3.0 */ |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 670 | AES_KEY actx; |
| 671 | int res; |
| 672 | |
Dmitry Shmidt | ebd93af | 2017-02-21 13:40:44 -0800 | [diff] [blame] | 673 | if (TEST_FAIL()) |
| 674 | return -1; |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 675 | if (AES_set_decrypt_key(kek, kek_len << 3, &actx)) |
| 676 | return -1; |
| 677 | res = AES_unwrap_key(&actx, NULL, plain, cipher, (n + 1) * 8); |
| 678 | OPENSSL_cleanse(&actx, sizeof(actx)); |
| 679 | return res <= 0 ? -1 : 0; |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 680 | #endif /* OpenSSL version >= 3.0 */ |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 681 | } |
| 682 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 683 | #endif /* CONFIG_OPENSSL_INTERNAL_AES_WRAP */ |
| 684 | #endif /* CONFIG_FIPS */ |
| 685 | |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 686 | |
Dmitry Shmidt | 912c6ec | 2015-03-30 13:16:51 -0700 | [diff] [blame] | 687 | int aes_128_cbc_encrypt(const u8 *key, const u8 *iv, u8 *data, size_t data_len) |
| 688 | { |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 689 | EVP_CIPHER_CTX *ctx; |
Dmitry Shmidt | 912c6ec | 2015-03-30 13:16:51 -0700 | [diff] [blame] | 690 | int clen, len; |
| 691 | u8 buf[16]; |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 692 | int res = -1; |
Dmitry Shmidt | 912c6ec | 2015-03-30 13:16:51 -0700 | [diff] [blame] | 693 | |
Dmitry Shmidt | d7ff03d | 2015-12-04 14:49:35 -0800 | [diff] [blame] | 694 | if (TEST_FAIL()) |
| 695 | return -1; |
| 696 | |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 697 | ctx = EVP_CIPHER_CTX_new(); |
| 698 | if (!ctx) |
Dmitry Shmidt | 912c6ec | 2015-03-30 13:16:51 -0700 | [diff] [blame] | 699 | return -1; |
Dmitry Shmidt | 912c6ec | 2015-03-30 13:16:51 -0700 | [diff] [blame] | 700 | clen = data_len; |
Dmitry Shmidt | 912c6ec | 2015-03-30 13:16:51 -0700 | [diff] [blame] | 701 | len = sizeof(buf); |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 702 | if (EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv) == 1 && |
| 703 | EVP_CIPHER_CTX_set_padding(ctx, 0) == 1 && |
| 704 | EVP_EncryptUpdate(ctx, data, &clen, data, data_len) == 1 && |
| 705 | clen == (int) data_len && |
| 706 | EVP_EncryptFinal_ex(ctx, buf, &len) == 1 && len == 0) |
| 707 | res = 0; |
Dmitry Shmidt | 57c2d39 | 2016-02-23 13:40:19 -0800 | [diff] [blame] | 708 | EVP_CIPHER_CTX_free(ctx); |
Dmitry Shmidt | 912c6ec | 2015-03-30 13:16:51 -0700 | [diff] [blame] | 709 | |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 710 | return res; |
Dmitry Shmidt | 912c6ec | 2015-03-30 13:16:51 -0700 | [diff] [blame] | 711 | } |
| 712 | |
| 713 | |
| 714 | int aes_128_cbc_decrypt(const u8 *key, const u8 *iv, u8 *data, size_t data_len) |
| 715 | { |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 716 | EVP_CIPHER_CTX *ctx; |
Dmitry Shmidt | 912c6ec | 2015-03-30 13:16:51 -0700 | [diff] [blame] | 717 | int plen, len; |
| 718 | u8 buf[16]; |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 719 | int res = -1; |
Dmitry Shmidt | 912c6ec | 2015-03-30 13:16:51 -0700 | [diff] [blame] | 720 | |
Dmitry Shmidt | d7ff03d | 2015-12-04 14:49:35 -0800 | [diff] [blame] | 721 | if (TEST_FAIL()) |
| 722 | return -1; |
| 723 | |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 724 | ctx = EVP_CIPHER_CTX_new(); |
| 725 | if (!ctx) |
Dmitry Shmidt | 912c6ec | 2015-03-30 13:16:51 -0700 | [diff] [blame] | 726 | return -1; |
Dmitry Shmidt | 912c6ec | 2015-03-30 13:16:51 -0700 | [diff] [blame] | 727 | plen = data_len; |
Dmitry Shmidt | 912c6ec | 2015-03-30 13:16:51 -0700 | [diff] [blame] | 728 | len = sizeof(buf); |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 729 | if (EVP_DecryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv) == 1 && |
| 730 | EVP_CIPHER_CTX_set_padding(ctx, 0) == 1 && |
| 731 | EVP_DecryptUpdate(ctx, data, &plen, data, data_len) == 1 && |
| 732 | plen == (int) data_len && |
| 733 | EVP_DecryptFinal_ex(ctx, buf, &len) == 1 && len == 0) |
| 734 | res = 0; |
Dmitry Shmidt | 57c2d39 | 2016-02-23 13:40:19 -0800 | [diff] [blame] | 735 | EVP_CIPHER_CTX_free(ctx); |
Dmitry Shmidt | 912c6ec | 2015-03-30 13:16:51 -0700 | [diff] [blame] | 736 | |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 737 | return res; |
| 738 | |
Dmitry Shmidt | 912c6ec | 2015-03-30 13:16:51 -0700 | [diff] [blame] | 739 | } |
| 740 | |
| 741 | |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 742 | int crypto_dh_init(u8 generator, const u8 *prime, size_t prime_len, u8 *privkey, |
| 743 | u8 *pubkey) |
| 744 | { |
| 745 | size_t pubkey_len, pad; |
| 746 | |
| 747 | if (os_get_random(privkey, prime_len) < 0) |
| 748 | return -1; |
| 749 | if (os_memcmp(privkey, prime, prime_len) > 0) { |
| 750 | /* Make sure private value is smaller than prime */ |
| 751 | privkey[0] = 0; |
| 752 | } |
| 753 | |
| 754 | pubkey_len = prime_len; |
| 755 | if (crypto_mod_exp(&generator, 1, privkey, prime_len, prime, prime_len, |
| 756 | pubkey, &pubkey_len) < 0) |
| 757 | return -1; |
| 758 | if (pubkey_len < prime_len) { |
| 759 | pad = prime_len - pubkey_len; |
| 760 | os_memmove(pubkey + pad, pubkey, pubkey_len); |
| 761 | os_memset(pubkey, 0, pad); |
| 762 | } |
| 763 | |
| 764 | return 0; |
| 765 | } |
| 766 | |
| 767 | |
| 768 | 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] | 769 | const u8 *order, size_t order_len, |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 770 | const u8 *privkey, size_t privkey_len, |
| 771 | const u8 *pubkey, size_t pubkey_len, |
| 772 | u8 *secret, size_t *len) |
| 773 | { |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 774 | BIGNUM *pub, *p; |
| 775 | int res = -1; |
| 776 | |
| 777 | pub = BN_bin2bn(pubkey, pubkey_len, NULL); |
| 778 | p = BN_bin2bn(prime, prime_len, NULL); |
| 779 | if (!pub || !p || BN_is_zero(pub) || BN_is_one(pub) || |
| 780 | BN_cmp(pub, p) >= 0) |
| 781 | goto fail; |
| 782 | |
| 783 | if (order) { |
| 784 | BN_CTX *ctx; |
| 785 | BIGNUM *q, *tmp; |
| 786 | int failed; |
| 787 | |
| 788 | /* verify: pubkey^q == 1 mod p */ |
| 789 | q = BN_bin2bn(order, order_len, NULL); |
| 790 | ctx = BN_CTX_new(); |
| 791 | tmp = BN_new(); |
| 792 | failed = !q || !ctx || !tmp || |
| 793 | !BN_mod_exp(tmp, pub, q, p, ctx) || |
| 794 | !BN_is_one(tmp); |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 795 | BN_clear_free(q); |
| 796 | BN_clear_free(tmp); |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 797 | BN_CTX_free(ctx); |
| 798 | if (failed) |
| 799 | goto fail; |
| 800 | } |
| 801 | |
| 802 | res = crypto_mod_exp(pubkey, pubkey_len, privkey, privkey_len, |
| 803 | prime, prime_len, secret, len); |
| 804 | fail: |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 805 | BN_clear_free(pub); |
| 806 | BN_clear_free(p); |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 807 | return res; |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 808 | } |
| 809 | |
| 810 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 811 | int crypto_mod_exp(const u8 *base, size_t base_len, |
| 812 | const u8 *power, size_t power_len, |
| 813 | const u8 *modulus, size_t modulus_len, |
| 814 | u8 *result, size_t *result_len) |
| 815 | { |
| 816 | BIGNUM *bn_base, *bn_exp, *bn_modulus, *bn_result; |
| 817 | int ret = -1; |
| 818 | BN_CTX *ctx; |
| 819 | |
| 820 | ctx = BN_CTX_new(); |
| 821 | if (ctx == NULL) |
| 822 | return -1; |
| 823 | |
| 824 | bn_base = BN_bin2bn(base, base_len, NULL); |
| 825 | bn_exp = BN_bin2bn(power, power_len, NULL); |
| 826 | bn_modulus = BN_bin2bn(modulus, modulus_len, NULL); |
| 827 | bn_result = BN_new(); |
| 828 | |
| 829 | if (bn_base == NULL || bn_exp == NULL || bn_modulus == NULL || |
| 830 | bn_result == NULL) |
| 831 | goto error; |
| 832 | |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 833 | if (BN_mod_exp_mont_consttime(bn_result, bn_base, bn_exp, bn_modulus, |
| 834 | ctx, NULL) != 1) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 835 | goto error; |
| 836 | |
| 837 | *result_len = BN_bn2bin(bn_result, result); |
| 838 | ret = 0; |
| 839 | |
| 840 | error: |
Dmitry Shmidt | 7f0b69e | 2014-07-28 10:35:20 -0700 | [diff] [blame] | 841 | BN_clear_free(bn_base); |
| 842 | BN_clear_free(bn_exp); |
| 843 | BN_clear_free(bn_modulus); |
| 844 | BN_clear_free(bn_result); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 845 | BN_CTX_free(ctx); |
| 846 | return ret; |
| 847 | } |
| 848 | |
| 849 | |
| 850 | struct crypto_cipher { |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 851 | EVP_CIPHER_CTX *enc; |
| 852 | EVP_CIPHER_CTX *dec; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 853 | }; |
| 854 | |
| 855 | |
| 856 | struct crypto_cipher * crypto_cipher_init(enum crypto_cipher_alg alg, |
| 857 | const u8 *iv, const u8 *key, |
| 858 | size_t key_len) |
| 859 | { |
| 860 | struct crypto_cipher *ctx; |
| 861 | const EVP_CIPHER *cipher; |
| 862 | |
| 863 | ctx = os_zalloc(sizeof(*ctx)); |
| 864 | if (ctx == NULL) |
| 865 | return NULL; |
| 866 | |
| 867 | switch (alg) { |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 868 | #ifndef CONFIG_NO_RC4 |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 869 | #ifndef OPENSSL_NO_RC4 |
| 870 | case CRYPTO_CIPHER_ALG_RC4: |
| 871 | cipher = EVP_rc4(); |
| 872 | break; |
| 873 | #endif /* OPENSSL_NO_RC4 */ |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 874 | #endif /* CONFIG_NO_RC4 */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 875 | #ifndef OPENSSL_NO_AES |
| 876 | case CRYPTO_CIPHER_ALG_AES: |
| 877 | switch (key_len) { |
| 878 | case 16: |
| 879 | cipher = EVP_aes_128_cbc(); |
| 880 | break; |
Dmitry Shmidt | 9ead16e | 2014-10-07 13:15:23 -0700 | [diff] [blame] | 881 | #ifndef OPENSSL_IS_BORINGSSL |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 882 | case 24: |
| 883 | cipher = EVP_aes_192_cbc(); |
| 884 | break; |
Dmitry Shmidt | 9ead16e | 2014-10-07 13:15:23 -0700 | [diff] [blame] | 885 | #endif /* OPENSSL_IS_BORINGSSL */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 886 | case 32: |
| 887 | cipher = EVP_aes_256_cbc(); |
| 888 | break; |
| 889 | default: |
| 890 | os_free(ctx); |
| 891 | return NULL; |
| 892 | } |
| 893 | break; |
| 894 | #endif /* OPENSSL_NO_AES */ |
| 895 | #ifndef OPENSSL_NO_DES |
| 896 | case CRYPTO_CIPHER_ALG_3DES: |
| 897 | cipher = EVP_des_ede3_cbc(); |
| 898 | break; |
| 899 | case CRYPTO_CIPHER_ALG_DES: |
| 900 | cipher = EVP_des_cbc(); |
| 901 | break; |
| 902 | #endif /* OPENSSL_NO_DES */ |
| 903 | #ifndef OPENSSL_NO_RC2 |
| 904 | case CRYPTO_CIPHER_ALG_RC2: |
| 905 | cipher = EVP_rc2_ecb(); |
| 906 | break; |
| 907 | #endif /* OPENSSL_NO_RC2 */ |
| 908 | default: |
| 909 | os_free(ctx); |
| 910 | return NULL; |
| 911 | } |
| 912 | |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 913 | if (!(ctx->enc = EVP_CIPHER_CTX_new()) || |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 914 | !EVP_EncryptInit_ex(ctx->enc, cipher, NULL, NULL, NULL) || |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 915 | !EVP_CIPHER_CTX_set_padding(ctx->enc, 0) || |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 916 | !EVP_CIPHER_CTX_set_key_length(ctx->enc, key_len) || |
| 917 | !EVP_EncryptInit_ex(ctx->enc, NULL, NULL, key, iv)) { |
| 918 | if (ctx->enc) |
Dmitry Shmidt | 57c2d39 | 2016-02-23 13:40:19 -0800 | [diff] [blame] | 919 | EVP_CIPHER_CTX_free(ctx->enc); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 920 | os_free(ctx); |
| 921 | return NULL; |
| 922 | } |
| 923 | |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 924 | if (!(ctx->dec = EVP_CIPHER_CTX_new()) || |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 925 | !EVP_DecryptInit_ex(ctx->dec, cipher, NULL, NULL, NULL) || |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 926 | !EVP_CIPHER_CTX_set_padding(ctx->dec, 0) || |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 927 | !EVP_CIPHER_CTX_set_key_length(ctx->dec, key_len) || |
| 928 | !EVP_DecryptInit_ex(ctx->dec, NULL, NULL, key, iv)) { |
Dmitry Shmidt | 57c2d39 | 2016-02-23 13:40:19 -0800 | [diff] [blame] | 929 | EVP_CIPHER_CTX_free(ctx->enc); |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 930 | if (ctx->dec) |
Dmitry Shmidt | 57c2d39 | 2016-02-23 13:40:19 -0800 | [diff] [blame] | 931 | EVP_CIPHER_CTX_free(ctx->dec); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 932 | os_free(ctx); |
| 933 | return NULL; |
| 934 | } |
| 935 | |
| 936 | return ctx; |
| 937 | } |
| 938 | |
| 939 | |
| 940 | int crypto_cipher_encrypt(struct crypto_cipher *ctx, const u8 *plain, |
| 941 | u8 *crypt, size_t len) |
| 942 | { |
| 943 | int outl; |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 944 | if (!EVP_EncryptUpdate(ctx->enc, crypt, &outl, plain, len)) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 945 | return -1; |
| 946 | return 0; |
| 947 | } |
| 948 | |
| 949 | |
| 950 | int crypto_cipher_decrypt(struct crypto_cipher *ctx, const u8 *crypt, |
| 951 | u8 *plain, size_t len) |
| 952 | { |
| 953 | int outl; |
| 954 | outl = len; |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 955 | if (!EVP_DecryptUpdate(ctx->dec, plain, &outl, crypt, len)) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 956 | return -1; |
| 957 | return 0; |
| 958 | } |
| 959 | |
| 960 | |
| 961 | void crypto_cipher_deinit(struct crypto_cipher *ctx) |
| 962 | { |
Dmitry Shmidt | 57c2d39 | 2016-02-23 13:40:19 -0800 | [diff] [blame] | 963 | EVP_CIPHER_CTX_free(ctx->enc); |
| 964 | EVP_CIPHER_CTX_free(ctx->dec); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 965 | os_free(ctx); |
| 966 | } |
| 967 | |
| 968 | |
| 969 | void * dh5_init(struct wpabuf **priv, struct wpabuf **publ) |
| 970 | { |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 971 | #if OPENSSL_VERSION_NUMBER < 0x10100000L |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 972 | DH *dh; |
| 973 | struct wpabuf *pubkey = NULL, *privkey = NULL; |
| 974 | size_t publen, privlen; |
| 975 | |
| 976 | *priv = NULL; |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 977 | wpabuf_free(*publ); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 978 | *publ = NULL; |
| 979 | |
| 980 | dh = DH_new(); |
| 981 | if (dh == NULL) |
| 982 | return NULL; |
| 983 | |
| 984 | dh->g = BN_new(); |
| 985 | if (dh->g == NULL || BN_set_word(dh->g, 2) != 1) |
| 986 | goto err; |
| 987 | |
| 988 | dh->p = get_group5_prime(); |
| 989 | if (dh->p == NULL) |
| 990 | goto err; |
| 991 | |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 992 | dh->q = get_group5_order(); |
| 993 | if (!dh->q) |
| 994 | goto err; |
| 995 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 996 | if (DH_generate_key(dh) != 1) |
| 997 | goto err; |
| 998 | |
| 999 | publen = BN_num_bytes(dh->pub_key); |
| 1000 | pubkey = wpabuf_alloc(publen); |
| 1001 | if (pubkey == NULL) |
| 1002 | goto err; |
| 1003 | privlen = BN_num_bytes(dh->priv_key); |
| 1004 | privkey = wpabuf_alloc(privlen); |
| 1005 | if (privkey == NULL) |
| 1006 | goto err; |
| 1007 | |
| 1008 | BN_bn2bin(dh->pub_key, wpabuf_put(pubkey, publen)); |
| 1009 | BN_bn2bin(dh->priv_key, wpabuf_put(privkey, privlen)); |
| 1010 | |
| 1011 | *priv = privkey; |
| 1012 | *publ = pubkey; |
| 1013 | return dh; |
| 1014 | |
| 1015 | err: |
Dmitry Shmidt | ff787d5 | 2015-01-12 13:01:47 -0800 | [diff] [blame] | 1016 | wpabuf_clear_free(pubkey); |
| 1017 | wpabuf_clear_free(privkey); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1018 | DH_free(dh); |
| 1019 | return NULL; |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 1020 | #elif OPENSSL_VERSION_NUMBER >= 0x30000000L |
| 1021 | EVP_PKEY *pkey = NULL; |
| 1022 | OSSL_PARAM params[2]; |
| 1023 | size_t pub_len = OSSL_PARAM_UNMODIFIED; |
| 1024 | size_t priv_len; |
| 1025 | struct wpabuf *pubkey = NULL, *privkey = NULL; |
| 1026 | BIGNUM *priv_bn = NULL; |
| 1027 | EVP_PKEY_CTX *gctx; |
| 1028 | |
| 1029 | *priv = NULL; |
| 1030 | wpabuf_free(*publ); |
| 1031 | *publ = NULL; |
| 1032 | |
| 1033 | params[0] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME, |
| 1034 | "modp_1536", 0); |
| 1035 | params[1] = OSSL_PARAM_construct_end(); |
| 1036 | |
| 1037 | gctx = EVP_PKEY_CTX_new_from_name(NULL, "DH", NULL); |
| 1038 | if (!gctx || |
| 1039 | EVP_PKEY_keygen_init(gctx) != 1 || |
| 1040 | EVP_PKEY_CTX_set_params(gctx, params) != 1 || |
| 1041 | EVP_PKEY_generate(gctx, &pkey) != 1 || |
| 1042 | EVP_PKEY_get_bn_param(pkey, OSSL_PKEY_PARAM_PRIV_KEY, |
| 1043 | &priv_bn) != 1 || |
| 1044 | EVP_PKEY_get_octet_string_param(pkey, |
| 1045 | OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, |
| 1046 | NULL, 0, &pub_len) < 0 || |
| 1047 | pub_len == OSSL_PARAM_UNMODIFIED || |
| 1048 | (priv_len = BN_num_bytes(priv_bn)) == 0 || |
| 1049 | !(pubkey = wpabuf_alloc(pub_len)) || |
| 1050 | !(privkey = wpabuf_alloc(priv_len)) || |
| 1051 | EVP_PKEY_get_octet_string_param(pkey, |
| 1052 | OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, |
| 1053 | wpabuf_put(pubkey, pub_len), |
| 1054 | pub_len, NULL) != 1) { |
| 1055 | wpa_printf(MSG_INFO, "OpenSSL: failed: %s", |
| 1056 | ERR_error_string(ERR_get_error(), NULL)); |
| 1057 | wpabuf_free(pubkey); |
| 1058 | wpabuf_clear_free(privkey); |
| 1059 | EVP_PKEY_free(pkey); |
| 1060 | pkey = NULL; |
| 1061 | } else { |
| 1062 | BN_bn2bin(priv_bn, wpabuf_put(privkey, priv_len)); |
| 1063 | |
| 1064 | *priv = privkey; |
| 1065 | *publ = pubkey; |
| 1066 | } |
| 1067 | |
| 1068 | BN_clear_free(priv_bn); |
| 1069 | EVP_PKEY_CTX_free(gctx); |
| 1070 | return pkey; |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 1071 | #else |
| 1072 | DH *dh; |
| 1073 | struct wpabuf *pubkey = NULL, *privkey = NULL; |
| 1074 | size_t publen, privlen; |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 1075 | BIGNUM *p, *g, *q; |
Dmitry Shmidt | 4ae50e6 | 2016-06-27 13:48:39 -0700 | [diff] [blame] | 1076 | const BIGNUM *priv_key = NULL, *pub_key = NULL; |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 1077 | |
| 1078 | *priv = NULL; |
| 1079 | wpabuf_free(*publ); |
| 1080 | *publ = NULL; |
| 1081 | |
| 1082 | dh = DH_new(); |
| 1083 | if (dh == NULL) |
| 1084 | return NULL; |
| 1085 | |
| 1086 | g = BN_new(); |
| 1087 | p = get_group5_prime(); |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 1088 | q = get_group5_order(); |
| 1089 | if (!g || BN_set_word(g, 2) != 1 || !p || !q || |
| 1090 | DH_set0_pqg(dh, p, q, g) != 1) |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 1091 | goto err; |
| 1092 | p = NULL; |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 1093 | q = NULL; |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 1094 | g = NULL; |
| 1095 | |
| 1096 | if (DH_generate_key(dh) != 1) |
| 1097 | goto err; |
| 1098 | |
| 1099 | DH_get0_key(dh, &pub_key, &priv_key); |
| 1100 | publen = BN_num_bytes(pub_key); |
| 1101 | pubkey = wpabuf_alloc(publen); |
| 1102 | if (!pubkey) |
| 1103 | goto err; |
| 1104 | privlen = BN_num_bytes(priv_key); |
| 1105 | privkey = wpabuf_alloc(privlen); |
| 1106 | if (!privkey) |
| 1107 | goto err; |
| 1108 | |
| 1109 | BN_bn2bin(pub_key, wpabuf_put(pubkey, publen)); |
| 1110 | BN_bn2bin(priv_key, wpabuf_put(privkey, privlen)); |
| 1111 | |
| 1112 | *priv = privkey; |
| 1113 | *publ = pubkey; |
| 1114 | return dh; |
| 1115 | |
| 1116 | err: |
| 1117 | BN_free(p); |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 1118 | BN_free(q); |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 1119 | BN_free(g); |
| 1120 | wpabuf_clear_free(pubkey); |
| 1121 | wpabuf_clear_free(privkey); |
| 1122 | DH_free(dh); |
| 1123 | return NULL; |
| 1124 | #endif |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1125 | } |
| 1126 | |
| 1127 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1128 | void * dh5_init_fixed(const struct wpabuf *priv, const struct wpabuf *publ) |
| 1129 | { |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 1130 | #if OPENSSL_VERSION_NUMBER < 0x10100000L |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1131 | DH *dh; |
| 1132 | |
| 1133 | dh = DH_new(); |
| 1134 | if (dh == NULL) |
| 1135 | return NULL; |
| 1136 | |
| 1137 | dh->g = BN_new(); |
| 1138 | if (dh->g == NULL || BN_set_word(dh->g, 2) != 1) |
| 1139 | goto err; |
| 1140 | |
| 1141 | dh->p = get_group5_prime(); |
| 1142 | if (dh->p == NULL) |
| 1143 | goto err; |
| 1144 | |
| 1145 | dh->priv_key = BN_bin2bn(wpabuf_head(priv), wpabuf_len(priv), NULL); |
| 1146 | if (dh->priv_key == NULL) |
| 1147 | goto err; |
| 1148 | |
| 1149 | dh->pub_key = BN_bin2bn(wpabuf_head(publ), wpabuf_len(publ), NULL); |
| 1150 | if (dh->pub_key == NULL) |
| 1151 | goto err; |
| 1152 | |
| 1153 | if (DH_generate_key(dh) != 1) |
| 1154 | goto err; |
| 1155 | |
| 1156 | return dh; |
| 1157 | |
| 1158 | err: |
| 1159 | DH_free(dh); |
| 1160 | return NULL; |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 1161 | #elif OPENSSL_VERSION_NUMBER >= 0x30000000L |
| 1162 | EVP_PKEY *pkey = NULL; |
| 1163 | OSSL_PARAM_BLD *bld; |
| 1164 | OSSL_PARAM *params = NULL; |
| 1165 | BIGNUM *priv_key, *pub_key; |
| 1166 | EVP_PKEY_CTX *fctx; |
| 1167 | |
| 1168 | fctx = EVP_PKEY_CTX_new_from_name(NULL, "DH", NULL); |
| 1169 | priv_key = BN_bin2bn(wpabuf_head(priv), wpabuf_len(priv), NULL); |
| 1170 | pub_key = BN_bin2bn(wpabuf_head(publ), wpabuf_len(publ), NULL); |
| 1171 | bld = OSSL_PARAM_BLD_new(); |
| 1172 | if (!fctx || !priv_key || !pub_key || !bld || |
| 1173 | OSSL_PARAM_BLD_push_utf8_string(bld, OSSL_PKEY_PARAM_GROUP_NAME, |
| 1174 | "modp_1536", 0) != 1 || |
| 1175 | OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PRIV_KEY, |
| 1176 | priv_key) != 1 || |
| 1177 | OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PUB_KEY, |
| 1178 | pub_key) != 1 || |
| 1179 | !(params = OSSL_PARAM_BLD_to_param(bld)) || |
| 1180 | EVP_PKEY_fromdata_init(fctx) != 1 || |
| 1181 | EVP_PKEY_fromdata(fctx, &pkey, EVP_PKEY_KEYPAIR, params) != 1) { |
| 1182 | wpa_printf(MSG_INFO, "OpenSSL: EVP_PKEY_fromdata failed: %s", |
| 1183 | ERR_error_string(ERR_get_error(), NULL)); |
| 1184 | EVP_PKEY_free(pkey); |
| 1185 | pkey = NULL; |
| 1186 | } |
| 1187 | |
| 1188 | BN_clear_free(priv_key); |
| 1189 | BN_free(pub_key); |
| 1190 | EVP_PKEY_CTX_free(fctx); |
| 1191 | OSSL_PARAM_BLD_free(bld); |
| 1192 | OSSL_PARAM_free(params); |
| 1193 | return pkey; |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 1194 | #else |
| 1195 | DH *dh; |
| 1196 | BIGNUM *p = NULL, *g, *priv_key = NULL, *pub_key = NULL; |
| 1197 | |
| 1198 | dh = DH_new(); |
| 1199 | if (dh == NULL) |
| 1200 | return NULL; |
| 1201 | |
| 1202 | g = BN_new(); |
| 1203 | p = get_group5_prime(); |
| 1204 | if (!g || BN_set_word(g, 2) != 1 || !p || |
| 1205 | DH_set0_pqg(dh, p, NULL, g) != 1) |
| 1206 | goto err; |
| 1207 | p = NULL; |
| 1208 | g = NULL; |
| 1209 | |
| 1210 | priv_key = BN_bin2bn(wpabuf_head(priv), wpabuf_len(priv), NULL); |
| 1211 | pub_key = BN_bin2bn(wpabuf_head(publ), wpabuf_len(publ), NULL); |
Dmitry Shmidt | 58d12ad | 2016-07-28 10:07:03 -0700 | [diff] [blame] | 1212 | 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] | 1213 | goto err; |
| 1214 | pub_key = NULL; |
| 1215 | priv_key = NULL; |
| 1216 | |
| 1217 | if (DH_generate_key(dh) != 1) |
| 1218 | goto err; |
| 1219 | |
| 1220 | return dh; |
| 1221 | |
| 1222 | err: |
| 1223 | BN_free(p); |
| 1224 | BN_free(g); |
| 1225 | BN_free(pub_key); |
| 1226 | BN_clear_free(priv_key); |
| 1227 | DH_free(dh); |
| 1228 | return NULL; |
| 1229 | #endif |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1230 | } |
| 1231 | |
| 1232 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1233 | struct wpabuf * dh5_derive_shared(void *ctx, const struct wpabuf *peer_public, |
| 1234 | const struct wpabuf *own_private) |
| 1235 | { |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 1236 | #if OPENSSL_VERSION_NUMBER >= 0x30000000L |
| 1237 | EVP_PKEY *pkey = ctx; |
| 1238 | EVP_PKEY *peer_pub; |
| 1239 | size_t len; |
| 1240 | struct wpabuf *res = NULL; |
| 1241 | EVP_PKEY_CTX *dctx = NULL; |
| 1242 | |
| 1243 | peer_pub = EVP_PKEY_new(); |
| 1244 | if (!pkey || !peer_pub || |
| 1245 | EVP_PKEY_copy_parameters(peer_pub, pkey) != 1 || |
| 1246 | EVP_PKEY_set1_encoded_public_key(peer_pub, wpabuf_head(peer_public), |
| 1247 | wpabuf_len(peer_public)) != 1 || |
| 1248 | !(dctx = EVP_PKEY_CTX_new(pkey, NULL)) || |
| 1249 | EVP_PKEY_derive_init(dctx) != 1 || |
| 1250 | EVP_PKEY_derive_set_peer(dctx, peer_pub) != 1 || |
| 1251 | EVP_PKEY_derive(dctx, NULL, &len) != 1 || |
| 1252 | !(res = wpabuf_alloc(len)) || |
| 1253 | EVP_PKEY_derive(dctx, wpabuf_mhead(res), &len) != 1) { |
| 1254 | wpa_printf(MSG_INFO, "OpenSSL: EVP_PKEY_derive failed: %s", |
| 1255 | ERR_error_string(ERR_get_error(), NULL)); |
| 1256 | wpabuf_free(res); |
| 1257 | res = NULL; |
| 1258 | } else { |
| 1259 | wpabuf_put(res, len); |
| 1260 | } |
| 1261 | |
| 1262 | EVP_PKEY_free(peer_pub); |
| 1263 | EVP_PKEY_CTX_free(dctx); |
| 1264 | return res; |
| 1265 | #else /* OpenSSL version >= 3.0 */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1266 | BIGNUM *pub_key; |
| 1267 | struct wpabuf *res = NULL; |
| 1268 | size_t rlen; |
| 1269 | DH *dh = ctx; |
| 1270 | int keylen; |
| 1271 | |
| 1272 | if (ctx == NULL) |
| 1273 | return NULL; |
| 1274 | |
| 1275 | pub_key = BN_bin2bn(wpabuf_head(peer_public), wpabuf_len(peer_public), |
| 1276 | NULL); |
| 1277 | if (pub_key == NULL) |
| 1278 | return NULL; |
| 1279 | |
| 1280 | rlen = DH_size(dh); |
| 1281 | res = wpabuf_alloc(rlen); |
| 1282 | if (res == NULL) |
| 1283 | goto err; |
| 1284 | |
| 1285 | keylen = DH_compute_key(wpabuf_mhead(res), pub_key, dh); |
| 1286 | if (keylen < 0) |
| 1287 | goto err; |
| 1288 | wpabuf_put(res, keylen); |
Dmitry Shmidt | 7f0b69e | 2014-07-28 10:35:20 -0700 | [diff] [blame] | 1289 | BN_clear_free(pub_key); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1290 | |
| 1291 | return res; |
| 1292 | |
| 1293 | err: |
Dmitry Shmidt | 7f0b69e | 2014-07-28 10:35:20 -0700 | [diff] [blame] | 1294 | BN_clear_free(pub_key); |
Dmitry Shmidt | ff787d5 | 2015-01-12 13:01:47 -0800 | [diff] [blame] | 1295 | wpabuf_clear_free(res); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1296 | return NULL; |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 1297 | #endif /* OpenSSL version >= 3.0 */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1298 | } |
| 1299 | |
| 1300 | |
| 1301 | void dh5_free(void *ctx) |
| 1302 | { |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 1303 | #if OPENSSL_VERSION_NUMBER >= 0x30000000L |
| 1304 | EVP_PKEY *pkey = ctx; |
| 1305 | |
| 1306 | EVP_PKEY_free(pkey); |
| 1307 | #else /* OpenSSL version >= 3.0 */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1308 | DH *dh; |
| 1309 | if (ctx == NULL) |
| 1310 | return; |
| 1311 | dh = ctx; |
| 1312 | DH_free(dh); |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 1313 | #endif /* OpenSSL version >= 3.0 */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1314 | } |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1315 | |
| 1316 | |
| 1317 | struct crypto_hash { |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 1318 | #if OPENSSL_VERSION_NUMBER >= 0x30000000L |
| 1319 | EVP_MAC_CTX *ctx; |
| 1320 | #else /* OpenSSL version >= 3.0 */ |
Dmitry Shmidt | 55840ad | 2015-12-14 12:45:46 -0800 | [diff] [blame] | 1321 | HMAC_CTX *ctx; |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 1322 | #endif /* OpenSSL version >= 3.0 */ |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1323 | }; |
| 1324 | |
| 1325 | |
| 1326 | struct crypto_hash * crypto_hash_init(enum crypto_hash_alg alg, const u8 *key, |
| 1327 | size_t key_len) |
| 1328 | { |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 1329 | #if OPENSSL_VERSION_NUMBER >= 0x30000000L |
| 1330 | struct crypto_hash *ctx; |
| 1331 | EVP_MAC *mac; |
| 1332 | OSSL_PARAM params[2]; |
| 1333 | char *a = NULL; |
| 1334 | |
| 1335 | switch (alg) { |
| 1336 | #ifndef OPENSSL_NO_MD5 |
| 1337 | case CRYPTO_HASH_ALG_HMAC_MD5: |
| 1338 | a = "MD5"; |
| 1339 | break; |
| 1340 | #endif /* OPENSSL_NO_MD5 */ |
| 1341 | #ifndef OPENSSL_NO_SHA |
| 1342 | case CRYPTO_HASH_ALG_HMAC_SHA1: |
| 1343 | a = "SHA1"; |
| 1344 | break; |
| 1345 | #endif /* OPENSSL_NO_SHA */ |
| 1346 | #ifndef OPENSSL_NO_SHA256 |
| 1347 | #ifdef CONFIG_SHA256 |
| 1348 | case CRYPTO_HASH_ALG_HMAC_SHA256: |
| 1349 | a = "SHA256"; |
| 1350 | break; |
| 1351 | #endif /* CONFIG_SHA256 */ |
| 1352 | #endif /* OPENSSL_NO_SHA256 */ |
| 1353 | default: |
| 1354 | return NULL; |
| 1355 | } |
| 1356 | |
| 1357 | mac = EVP_MAC_fetch(NULL, "HMAC", NULL); |
| 1358 | if (!mac) |
| 1359 | return NULL; |
| 1360 | |
| 1361 | params[0] = OSSL_PARAM_construct_utf8_string("digest", a, 0); |
| 1362 | params[1] = OSSL_PARAM_construct_end(); |
| 1363 | |
| 1364 | ctx = os_zalloc(sizeof(*ctx)); |
| 1365 | if (!ctx) |
Sunil | 8cd6f4d | 2022-06-28 18:40:46 +0000 | [diff] [blame] | 1366 | goto fail; |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 1367 | ctx->ctx = EVP_MAC_CTX_new(mac); |
| 1368 | if (!ctx->ctx) { |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 1369 | os_free(ctx); |
Sunil | 8cd6f4d | 2022-06-28 18:40:46 +0000 | [diff] [blame] | 1370 | ctx = NULL; |
| 1371 | goto fail; |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 1372 | } |
| 1373 | |
| 1374 | if (EVP_MAC_init(ctx->ctx, key, key_len, params) != 1) { |
| 1375 | EVP_MAC_CTX_free(ctx->ctx); |
| 1376 | bin_clear_free(ctx, sizeof(*ctx)); |
Sunil | 8cd6f4d | 2022-06-28 18:40:46 +0000 | [diff] [blame] | 1377 | ctx = NULL; |
| 1378 | goto fail; |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 1379 | } |
| 1380 | |
Sunil | 8cd6f4d | 2022-06-28 18:40:46 +0000 | [diff] [blame] | 1381 | fail: |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 1382 | EVP_MAC_free(mac); |
| 1383 | return ctx; |
| 1384 | #else /* OpenSSL version >= 3.0 */ |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1385 | struct crypto_hash *ctx; |
| 1386 | const EVP_MD *md; |
| 1387 | |
| 1388 | switch (alg) { |
| 1389 | #ifndef OPENSSL_NO_MD5 |
| 1390 | case CRYPTO_HASH_ALG_HMAC_MD5: |
| 1391 | md = EVP_md5(); |
| 1392 | break; |
| 1393 | #endif /* OPENSSL_NO_MD5 */ |
| 1394 | #ifndef OPENSSL_NO_SHA |
| 1395 | case CRYPTO_HASH_ALG_HMAC_SHA1: |
| 1396 | md = EVP_sha1(); |
| 1397 | break; |
| 1398 | #endif /* OPENSSL_NO_SHA */ |
| 1399 | #ifndef OPENSSL_NO_SHA256 |
| 1400 | #ifdef CONFIG_SHA256 |
| 1401 | case CRYPTO_HASH_ALG_HMAC_SHA256: |
| 1402 | md = EVP_sha256(); |
| 1403 | break; |
| 1404 | #endif /* CONFIG_SHA256 */ |
| 1405 | #endif /* OPENSSL_NO_SHA256 */ |
| 1406 | default: |
| 1407 | return NULL; |
| 1408 | } |
| 1409 | |
| 1410 | ctx = os_zalloc(sizeof(*ctx)); |
| 1411 | if (ctx == NULL) |
| 1412 | return NULL; |
Dmitry Shmidt | 55840ad | 2015-12-14 12:45:46 -0800 | [diff] [blame] | 1413 | ctx->ctx = HMAC_CTX_new(); |
| 1414 | if (!ctx->ctx) { |
| 1415 | os_free(ctx); |
| 1416 | return NULL; |
| 1417 | } |
| 1418 | |
| 1419 | if (HMAC_Init_ex(ctx->ctx, key, key_len, md, NULL) != 1) { |
| 1420 | HMAC_CTX_free(ctx->ctx); |
| 1421 | bin_clear_free(ctx, sizeof(*ctx)); |
| 1422 | return NULL; |
| 1423 | } |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1424 | |
| 1425 | return ctx; |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 1426 | #endif /* OpenSSL version >= 3.0 */ |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1427 | } |
| 1428 | |
| 1429 | |
| 1430 | void crypto_hash_update(struct crypto_hash *ctx, const u8 *data, size_t len) |
| 1431 | { |
| 1432 | if (ctx == NULL) |
| 1433 | return; |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 1434 | #if OPENSSL_VERSION_NUMBER >= 0x30000000L |
| 1435 | EVP_MAC_update(ctx->ctx, data, len); |
| 1436 | #else /* OpenSSL version >= 3.0 */ |
Dmitry Shmidt | 55840ad | 2015-12-14 12:45:46 -0800 | [diff] [blame] | 1437 | HMAC_Update(ctx->ctx, data, len); |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 1438 | #endif /* OpenSSL version >= 3.0 */ |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1439 | } |
| 1440 | |
| 1441 | |
| 1442 | int crypto_hash_finish(struct crypto_hash *ctx, u8 *mac, size_t *len) |
| 1443 | { |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 1444 | #if OPENSSL_VERSION_NUMBER >= 0x30000000L |
| 1445 | size_t mdlen; |
| 1446 | int res; |
| 1447 | |
| 1448 | if (!ctx) |
| 1449 | return -2; |
| 1450 | |
| 1451 | if (!mac || !len) { |
| 1452 | EVP_MAC_CTX_free(ctx->ctx); |
| 1453 | bin_clear_free(ctx, sizeof(*ctx)); |
| 1454 | return 0; |
| 1455 | } |
| 1456 | |
| 1457 | res = EVP_MAC_final(ctx->ctx, NULL, &mdlen, 0); |
| 1458 | if (res != 1) { |
| 1459 | EVP_MAC_CTX_free(ctx->ctx); |
| 1460 | bin_clear_free(ctx, sizeof(*ctx)); |
| 1461 | return -1; |
| 1462 | } |
| 1463 | res = EVP_MAC_final(ctx->ctx, mac, &mdlen, mdlen); |
| 1464 | EVP_MAC_CTX_free(ctx->ctx); |
| 1465 | bin_clear_free(ctx, sizeof(*ctx)); |
| 1466 | |
| 1467 | if (TEST_FAIL()) |
| 1468 | return -1; |
| 1469 | |
| 1470 | if (res == 1) { |
| 1471 | *len = mdlen; |
| 1472 | return 0; |
| 1473 | } |
| 1474 | |
| 1475 | return -1; |
| 1476 | #else /* OpenSSL version >= 3.0 */ |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1477 | unsigned int mdlen; |
| 1478 | int res; |
| 1479 | |
| 1480 | if (ctx == NULL) |
| 1481 | return -2; |
| 1482 | |
| 1483 | if (mac == NULL || len == NULL) { |
Dmitry Shmidt | 55840ad | 2015-12-14 12:45:46 -0800 | [diff] [blame] | 1484 | HMAC_CTX_free(ctx->ctx); |
Dmitry Shmidt | ff787d5 | 2015-01-12 13:01:47 -0800 | [diff] [blame] | 1485 | bin_clear_free(ctx, sizeof(*ctx)); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1486 | return 0; |
| 1487 | } |
| 1488 | |
| 1489 | mdlen = *len; |
Dmitry Shmidt | 55840ad | 2015-12-14 12:45:46 -0800 | [diff] [blame] | 1490 | res = HMAC_Final(ctx->ctx, mac, &mdlen); |
| 1491 | HMAC_CTX_free(ctx->ctx); |
Dmitry Shmidt | ff787d5 | 2015-01-12 13:01:47 -0800 | [diff] [blame] | 1492 | bin_clear_free(ctx, sizeof(*ctx)); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1493 | |
Hai Shalom | 5f92bc9 | 2019-04-18 11:54:11 -0700 | [diff] [blame] | 1494 | if (TEST_FAIL()) |
| 1495 | return -1; |
| 1496 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1497 | if (res == 1) { |
| 1498 | *len = mdlen; |
| 1499 | return 0; |
| 1500 | } |
| 1501 | |
| 1502 | return -1; |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 1503 | #endif /* OpenSSL version >= 3.0 */ |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1504 | } |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1505 | |
| 1506 | |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 1507 | #if OPENSSL_VERSION_NUMBER >= 0x30000000L |
| 1508 | |
| 1509 | static int openssl_hmac_vector(char *digest, const u8 *key, |
| 1510 | size_t key_len, size_t num_elem, |
| 1511 | const u8 *addr[], const size_t *len, u8 *mac, |
| 1512 | unsigned int mdlen) |
| 1513 | { |
| 1514 | EVP_MAC *hmac; |
| 1515 | OSSL_PARAM params[2]; |
| 1516 | EVP_MAC_CTX *ctx; |
| 1517 | size_t i, mlen; |
| 1518 | int res; |
| 1519 | |
| 1520 | if (TEST_FAIL()) |
| 1521 | return -1; |
| 1522 | |
| 1523 | hmac = EVP_MAC_fetch(NULL, "HMAC", NULL); |
| 1524 | if (!hmac) |
| 1525 | return -1; |
| 1526 | |
| 1527 | params[0] = OSSL_PARAM_construct_utf8_string("digest", digest, 0); |
| 1528 | params[1] = OSSL_PARAM_construct_end(); |
| 1529 | |
| 1530 | ctx = EVP_MAC_CTX_new(hmac); |
| 1531 | EVP_MAC_free(hmac); |
| 1532 | if (!ctx) |
| 1533 | return -1; |
| 1534 | |
| 1535 | if (EVP_MAC_init(ctx, key, key_len, params) != 1) |
| 1536 | goto fail; |
| 1537 | |
| 1538 | for (i = 0; i < num_elem; i++) { |
| 1539 | if (EVP_MAC_update(ctx, addr[i], len[i]) != 1) |
| 1540 | goto fail; |
| 1541 | } |
| 1542 | |
| 1543 | res = EVP_MAC_final(ctx, mac, &mlen, mdlen); |
| 1544 | EVP_MAC_CTX_free(ctx); |
| 1545 | |
| 1546 | return res == 1 ? 0 : -1; |
| 1547 | fail: |
| 1548 | EVP_MAC_CTX_free(ctx); |
| 1549 | return -1; |
| 1550 | } |
| 1551 | |
| 1552 | |
| 1553 | #ifndef CONFIG_FIPS |
| 1554 | |
| 1555 | int hmac_md5_vector(const u8 *key, size_t key_len, size_t num_elem, |
| 1556 | const u8 *addr[], const size_t *len, u8 *mac) |
| 1557 | { |
| 1558 | return openssl_hmac_vector("MD5", key ,key_len, num_elem, addr, len, |
| 1559 | mac, 16); |
| 1560 | } |
| 1561 | |
| 1562 | |
| 1563 | int hmac_md5(const u8 *key, size_t key_len, const u8 *data, size_t data_len, |
| 1564 | u8 *mac) |
| 1565 | { |
| 1566 | return hmac_md5_vector(key, key_len, 1, &data, &data_len, mac); |
| 1567 | } |
| 1568 | |
| 1569 | #endif /* CONFIG_FIPS */ |
| 1570 | |
| 1571 | |
| 1572 | int hmac_sha1_vector(const u8 *key, size_t key_len, size_t num_elem, |
| 1573 | const u8 *addr[], const size_t *len, u8 *mac) |
| 1574 | { |
| 1575 | return openssl_hmac_vector("SHA1", key, key_len, num_elem, addr, |
| 1576 | len, mac, 20); |
| 1577 | } |
| 1578 | |
| 1579 | |
| 1580 | int hmac_sha1(const u8 *key, size_t key_len, const u8 *data, size_t data_len, |
| 1581 | u8 *mac) |
| 1582 | { |
| 1583 | return hmac_sha1_vector(key, key_len, 1, &data, &data_len, mac); |
| 1584 | } |
| 1585 | |
| 1586 | |
| 1587 | #ifdef CONFIG_SHA256 |
| 1588 | |
| 1589 | int hmac_sha256_vector(const u8 *key, size_t key_len, size_t num_elem, |
| 1590 | const u8 *addr[], const size_t *len, u8 *mac) |
| 1591 | { |
| 1592 | return openssl_hmac_vector("SHA256", key, key_len, num_elem, addr, |
| 1593 | len, mac, 32); |
| 1594 | } |
| 1595 | |
| 1596 | |
| 1597 | int hmac_sha256(const u8 *key, size_t key_len, const u8 *data, |
| 1598 | size_t data_len, u8 *mac) |
| 1599 | { |
| 1600 | return hmac_sha256_vector(key, key_len, 1, &data, &data_len, mac); |
| 1601 | } |
| 1602 | |
| 1603 | #endif /* CONFIG_SHA256 */ |
| 1604 | |
| 1605 | |
| 1606 | #ifdef CONFIG_SHA384 |
| 1607 | |
| 1608 | int hmac_sha384_vector(const u8 *key, size_t key_len, size_t num_elem, |
| 1609 | const u8 *addr[], const size_t *len, u8 *mac) |
| 1610 | { |
| 1611 | return openssl_hmac_vector("SHA384", key, key_len, num_elem, addr, |
| 1612 | len, mac, 48); |
| 1613 | } |
| 1614 | |
| 1615 | |
| 1616 | int hmac_sha384(const u8 *key, size_t key_len, const u8 *data, |
| 1617 | size_t data_len, u8 *mac) |
| 1618 | { |
| 1619 | return hmac_sha384_vector(key, key_len, 1, &data, &data_len, mac); |
| 1620 | } |
| 1621 | |
| 1622 | #endif /* CONFIG_SHA384 */ |
| 1623 | |
| 1624 | |
| 1625 | #ifdef CONFIG_SHA512 |
| 1626 | |
| 1627 | int hmac_sha512_vector(const u8 *key, size_t key_len, size_t num_elem, |
| 1628 | const u8 *addr[], const size_t *len, u8 *mac) |
| 1629 | { |
| 1630 | return openssl_hmac_vector("SHA512", key, key_len, num_elem, addr, |
| 1631 | len, mac, 64); |
| 1632 | } |
| 1633 | |
| 1634 | |
| 1635 | int hmac_sha512(const u8 *key, size_t key_len, const u8 *data, |
| 1636 | size_t data_len, u8 *mac) |
| 1637 | { |
| 1638 | return hmac_sha512_vector(key, key_len, 1, &data, &data_len, mac); |
| 1639 | } |
| 1640 | |
| 1641 | #endif /* CONFIG_SHA512 */ |
| 1642 | |
| 1643 | #else /* OpenSSL version >= 3.0 */ |
| 1644 | |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 1645 | static int openssl_hmac_vector(const EVP_MD *type, const u8 *key, |
| 1646 | size_t key_len, size_t num_elem, |
| 1647 | const u8 *addr[], const size_t *len, u8 *mac, |
| 1648 | unsigned int mdlen) |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1649 | { |
Dmitry Shmidt | 55840ad | 2015-12-14 12:45:46 -0800 | [diff] [blame] | 1650 | HMAC_CTX *ctx; |
| 1651 | size_t i; |
| 1652 | int res; |
| 1653 | |
| 1654 | if (TEST_FAIL()) |
| 1655 | return -1; |
| 1656 | |
| 1657 | ctx = HMAC_CTX_new(); |
| 1658 | if (!ctx) |
| 1659 | return -1; |
| 1660 | res = HMAC_Init_ex(ctx, key, key_len, type, NULL); |
| 1661 | if (res != 1) |
| 1662 | goto done; |
| 1663 | |
| 1664 | for (i = 0; i < num_elem; i++) |
| 1665 | HMAC_Update(ctx, addr[i], len[i]); |
| 1666 | |
| 1667 | res = HMAC_Final(ctx, mac, &mdlen); |
| 1668 | done: |
| 1669 | HMAC_CTX_free(ctx); |
| 1670 | |
| 1671 | return res == 1 ? 0 : -1; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1672 | } |
| 1673 | |
| 1674 | |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 1675 | #ifndef CONFIG_FIPS |
| 1676 | |
| 1677 | int hmac_md5_vector(const u8 *key, size_t key_len, size_t num_elem, |
| 1678 | const u8 *addr[], const size_t *len, u8 *mac) |
| 1679 | { |
| 1680 | return openssl_hmac_vector(EVP_md5(), key ,key_len, num_elem, addr, len, |
| 1681 | mac, 16); |
| 1682 | } |
| 1683 | |
| 1684 | |
| 1685 | int hmac_md5(const u8 *key, size_t key_len, const u8 *data, size_t data_len, |
| 1686 | u8 *mac) |
| 1687 | { |
| 1688 | return hmac_md5_vector(key, key_len, 1, &data, &data_len, mac); |
| 1689 | } |
| 1690 | |
| 1691 | #endif /* CONFIG_FIPS */ |
| 1692 | |
| 1693 | |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 1694 | int hmac_sha1_vector(const u8 *key, size_t key_len, size_t num_elem, |
| 1695 | const u8 *addr[], const size_t *len, u8 *mac) |
| 1696 | { |
| 1697 | return openssl_hmac_vector(EVP_sha1(), key, key_len, num_elem, addr, |
| 1698 | len, mac, 20); |
| 1699 | } |
| 1700 | |
| 1701 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1702 | int hmac_sha1(const u8 *key, size_t key_len, const u8 *data, size_t data_len, |
| 1703 | u8 *mac) |
| 1704 | { |
| 1705 | return hmac_sha1_vector(key, key_len, 1, &data, &data_len, mac); |
| 1706 | } |
| 1707 | |
| 1708 | |
| 1709 | #ifdef CONFIG_SHA256 |
| 1710 | |
| 1711 | int hmac_sha256_vector(const u8 *key, size_t key_len, size_t num_elem, |
| 1712 | const u8 *addr[], const size_t *len, u8 *mac) |
| 1713 | { |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 1714 | return openssl_hmac_vector(EVP_sha256(), key, key_len, num_elem, addr, |
| 1715 | len, mac, 32); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1716 | } |
| 1717 | |
| 1718 | |
| 1719 | int hmac_sha256(const u8 *key, size_t key_len, const u8 *data, |
| 1720 | size_t data_len, u8 *mac) |
| 1721 | { |
| 1722 | return hmac_sha256_vector(key, key_len, 1, &data, &data_len, mac); |
| 1723 | } |
| 1724 | |
| 1725 | #endif /* CONFIG_SHA256 */ |
| 1726 | |
| 1727 | |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 1728 | #ifdef CONFIG_SHA384 |
| 1729 | |
| 1730 | int hmac_sha384_vector(const u8 *key, size_t key_len, size_t num_elem, |
| 1731 | const u8 *addr[], const size_t *len, u8 *mac) |
| 1732 | { |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 1733 | return openssl_hmac_vector(EVP_sha384(), key, key_len, num_elem, addr, |
Dmitry Shmidt | ebd93af | 2017-02-21 13:40:44 -0800 | [diff] [blame] | 1734 | len, mac, 48); |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 1735 | } |
| 1736 | |
| 1737 | |
| 1738 | int hmac_sha384(const u8 *key, size_t key_len, const u8 *data, |
| 1739 | size_t data_len, u8 *mac) |
| 1740 | { |
| 1741 | return hmac_sha384_vector(key, key_len, 1, &data, &data_len, mac); |
| 1742 | } |
| 1743 | |
| 1744 | #endif /* CONFIG_SHA384 */ |
| 1745 | |
| 1746 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 1747 | #ifdef CONFIG_SHA512 |
| 1748 | |
| 1749 | int hmac_sha512_vector(const u8 *key, size_t key_len, size_t num_elem, |
| 1750 | const u8 *addr[], const size_t *len, u8 *mac) |
| 1751 | { |
| 1752 | return openssl_hmac_vector(EVP_sha512(), key, key_len, num_elem, addr, |
| 1753 | len, mac, 64); |
| 1754 | } |
| 1755 | |
| 1756 | |
| 1757 | int hmac_sha512(const u8 *key, size_t key_len, const u8 *data, |
| 1758 | size_t data_len, u8 *mac) |
| 1759 | { |
| 1760 | return hmac_sha512_vector(key, key_len, 1, &data, &data_len, mac); |
| 1761 | } |
| 1762 | |
| 1763 | #endif /* CONFIG_SHA512 */ |
| 1764 | |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 1765 | #endif /* OpenSSL version >= 3.0 */ |
| 1766 | |
| 1767 | |
| 1768 | int pbkdf2_sha1(const char *passphrase, const u8 *ssid, size_t ssid_len, |
| 1769 | int iterations, u8 *buf, size_t buflen) |
| 1770 | { |
| 1771 | if (PKCS5_PBKDF2_HMAC_SHA1(passphrase, os_strlen(passphrase), ssid, |
| 1772 | ssid_len, iterations, buflen, buf) != 1) |
| 1773 | return -1; |
| 1774 | return 0; |
| 1775 | } |
| 1776 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 1777 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1778 | int crypto_get_random(void *buf, size_t len) |
| 1779 | { |
| 1780 | if (RAND_bytes(buf, len) != 1) |
| 1781 | return -1; |
| 1782 | return 0; |
| 1783 | } |
| 1784 | |
| 1785 | |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 1786 | int omac1_aes_vector(const u8 *key, size_t key_len, size_t num_elem, |
| 1787 | const u8 *addr[], const size_t *len, u8 *mac) |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1788 | { |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 1789 | #if OPENSSL_VERSION_NUMBER >= 0x30000000L |
| 1790 | EVP_MAC_CTX *ctx = NULL; |
| 1791 | EVP_MAC *emac; |
| 1792 | int ret = -1; |
| 1793 | size_t outlen, i; |
| 1794 | OSSL_PARAM params[2]; |
| 1795 | char *cipher = NULL; |
| 1796 | |
| 1797 | if (TEST_FAIL()) |
| 1798 | return -1; |
| 1799 | |
| 1800 | emac = EVP_MAC_fetch(NULL, "CMAC", NULL); |
| 1801 | |
| 1802 | if (key_len == 32) |
| 1803 | cipher = "aes-256-cbc"; |
| 1804 | else if (key_len == 24) |
| 1805 | cipher = "aes-192-cbc"; |
| 1806 | else if (key_len == 16) |
| 1807 | cipher = "aes-128-cbc"; |
| 1808 | |
| 1809 | params[0] = OSSL_PARAM_construct_utf8_string("cipher", cipher, 0); |
| 1810 | params[1] = OSSL_PARAM_construct_end(); |
| 1811 | |
| 1812 | if (!emac || !cipher || |
| 1813 | !(ctx = EVP_MAC_CTX_new(emac)) || |
| 1814 | EVP_MAC_init(ctx, key, key_len, params) != 1) |
| 1815 | goto fail; |
| 1816 | |
| 1817 | for (i = 0; i < num_elem; i++) { |
| 1818 | if (!EVP_MAC_update(ctx, addr[i], len[i])) |
| 1819 | goto fail; |
| 1820 | } |
| 1821 | if (EVP_MAC_final(ctx, mac, &outlen, 16) != 1 || outlen != 16) |
| 1822 | goto fail; |
| 1823 | |
| 1824 | ret = 0; |
| 1825 | fail: |
| 1826 | EVP_MAC_CTX_free(ctx); |
| 1827 | return ret; |
| 1828 | #else /* OpenSSL version >= 3.0 */ |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1829 | CMAC_CTX *ctx; |
| 1830 | int ret = -1; |
| 1831 | size_t outlen, i; |
| 1832 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1833 | if (TEST_FAIL()) |
| 1834 | return -1; |
| 1835 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1836 | ctx = CMAC_CTX_new(); |
| 1837 | if (ctx == NULL) |
| 1838 | return -1; |
| 1839 | |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 1840 | if (key_len == 32) { |
| 1841 | if (!CMAC_Init(ctx, key, 32, EVP_aes_256_cbc(), NULL)) |
| 1842 | goto fail; |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 1843 | } else if (key_len == 24) { |
| 1844 | if (!CMAC_Init(ctx, key, 24, EVP_aes_192_cbc(), NULL)) |
| 1845 | goto fail; |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 1846 | } else if (key_len == 16) { |
| 1847 | if (!CMAC_Init(ctx, key, 16, EVP_aes_128_cbc(), NULL)) |
| 1848 | goto fail; |
| 1849 | } else { |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1850 | goto fail; |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 1851 | } |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1852 | for (i = 0; i < num_elem; i++) { |
| 1853 | if (!CMAC_Update(ctx, addr[i], len[i])) |
| 1854 | goto fail; |
| 1855 | } |
| 1856 | if (!CMAC_Final(ctx, mac, &outlen) || outlen != 16) |
| 1857 | goto fail; |
| 1858 | |
| 1859 | ret = 0; |
| 1860 | fail: |
| 1861 | CMAC_CTX_free(ctx); |
| 1862 | return ret; |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 1863 | #endif /* OpenSSL version >= 3.0 */ |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1864 | } |
| 1865 | |
| 1866 | |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 1867 | int omac1_aes_128_vector(const u8 *key, size_t num_elem, |
| 1868 | const u8 *addr[], const size_t *len, u8 *mac) |
| 1869 | { |
| 1870 | return omac1_aes_vector(key, 16, num_elem, addr, len, mac); |
| 1871 | } |
| 1872 | |
| 1873 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1874 | int omac1_aes_128(const u8 *key, const u8 *data, size_t data_len, u8 *mac) |
| 1875 | { |
| 1876 | return omac1_aes_128_vector(key, 1, &data, &data_len, mac); |
| 1877 | } |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 1878 | |
| 1879 | |
| 1880 | int omac1_aes_256(const u8 *key, const u8 *data, size_t data_len, u8 *mac) |
| 1881 | { |
| 1882 | return omac1_aes_vector(key, 32, 1, &data, &data_len, mac); |
| 1883 | } |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1884 | |
| 1885 | |
| 1886 | struct crypto_bignum * crypto_bignum_init(void) |
| 1887 | { |
Dmitry Shmidt | e466304 | 2016-04-04 10:07:49 -0700 | [diff] [blame] | 1888 | if (TEST_FAIL()) |
| 1889 | return NULL; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1890 | return (struct crypto_bignum *) BN_new(); |
| 1891 | } |
| 1892 | |
| 1893 | |
| 1894 | struct crypto_bignum * crypto_bignum_init_set(const u8 *buf, size_t len) |
| 1895 | { |
Dmitry Shmidt | e466304 | 2016-04-04 10:07:49 -0700 | [diff] [blame] | 1896 | BIGNUM *bn; |
| 1897 | |
| 1898 | if (TEST_FAIL()) |
| 1899 | return NULL; |
| 1900 | |
| 1901 | bn = BN_bin2bn(buf, len, NULL); |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1902 | return (struct crypto_bignum *) bn; |
| 1903 | } |
| 1904 | |
| 1905 | |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 1906 | struct crypto_bignum * crypto_bignum_init_uint(unsigned int val) |
| 1907 | { |
| 1908 | BIGNUM *bn; |
| 1909 | |
| 1910 | if (TEST_FAIL()) |
| 1911 | return NULL; |
| 1912 | |
| 1913 | bn = BN_new(); |
| 1914 | if (!bn) |
| 1915 | return NULL; |
| 1916 | if (BN_set_word(bn, val) != 1) { |
| 1917 | BN_free(bn); |
| 1918 | return NULL; |
| 1919 | } |
| 1920 | return (struct crypto_bignum *) bn; |
| 1921 | } |
| 1922 | |
| 1923 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1924 | void crypto_bignum_deinit(struct crypto_bignum *n, int clear) |
| 1925 | { |
| 1926 | if (clear) |
| 1927 | BN_clear_free((BIGNUM *) n); |
| 1928 | else |
| 1929 | BN_free((BIGNUM *) n); |
| 1930 | } |
| 1931 | |
| 1932 | |
| 1933 | int crypto_bignum_to_bin(const struct crypto_bignum *a, |
| 1934 | u8 *buf, size_t buflen, size_t padlen) |
| 1935 | { |
| 1936 | int num_bytes, offset; |
| 1937 | |
Dmitry Shmidt | e466304 | 2016-04-04 10:07:49 -0700 | [diff] [blame] | 1938 | if (TEST_FAIL()) |
| 1939 | return -1; |
| 1940 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1941 | if (padlen > buflen) |
| 1942 | return -1; |
| 1943 | |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 1944 | if (padlen) { |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 1945 | #ifdef OPENSSL_IS_BORINGSSL |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 1946 | if (BN_bn2bin_padded(buf, padlen, (const BIGNUM *) a) == 0) |
| 1947 | return -1; |
| 1948 | return padlen; |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 1949 | #else /* OPENSSL_IS_BORINGSSL */ |
| 1950 | #if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER) |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 1951 | return BN_bn2binpad((const BIGNUM *) a, buf, padlen); |
| 1952 | #endif |
| 1953 | #endif |
| 1954 | } |
| 1955 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1956 | num_bytes = BN_num_bytes((const BIGNUM *) a); |
| 1957 | if ((size_t) num_bytes > buflen) |
| 1958 | return -1; |
| 1959 | if (padlen > (size_t) num_bytes) |
| 1960 | offset = padlen - num_bytes; |
| 1961 | else |
| 1962 | offset = 0; |
| 1963 | |
| 1964 | os_memset(buf, 0, offset); |
| 1965 | BN_bn2bin((const BIGNUM *) a, buf + offset); |
| 1966 | |
| 1967 | return num_bytes + offset; |
| 1968 | } |
| 1969 | |
| 1970 | |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 1971 | int crypto_bignum_rand(struct crypto_bignum *r, const struct crypto_bignum *m) |
| 1972 | { |
Hai Shalom | 5f92bc9 | 2019-04-18 11:54:11 -0700 | [diff] [blame] | 1973 | if (TEST_FAIL()) |
| 1974 | return -1; |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 1975 | return BN_rand_range((BIGNUM *) r, (const BIGNUM *) m) == 1 ? 0 : -1; |
| 1976 | } |
| 1977 | |
| 1978 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1979 | int crypto_bignum_add(const struct crypto_bignum *a, |
| 1980 | const struct crypto_bignum *b, |
| 1981 | struct crypto_bignum *c) |
| 1982 | { |
| 1983 | return BN_add((BIGNUM *) c, (const BIGNUM *) a, (const BIGNUM *) b) ? |
| 1984 | 0 : -1; |
| 1985 | } |
| 1986 | |
| 1987 | |
| 1988 | int crypto_bignum_mod(const struct crypto_bignum *a, |
| 1989 | const struct crypto_bignum *b, |
| 1990 | struct crypto_bignum *c) |
| 1991 | { |
| 1992 | int res; |
| 1993 | BN_CTX *bnctx; |
| 1994 | |
| 1995 | bnctx = BN_CTX_new(); |
| 1996 | if (bnctx == NULL) |
| 1997 | return -1; |
| 1998 | res = BN_mod((BIGNUM *) c, (const BIGNUM *) a, (const BIGNUM *) b, |
| 1999 | bnctx); |
| 2000 | BN_CTX_free(bnctx); |
| 2001 | |
| 2002 | return res ? 0 : -1; |
| 2003 | } |
| 2004 | |
| 2005 | |
| 2006 | int crypto_bignum_exptmod(const struct crypto_bignum *a, |
| 2007 | const struct crypto_bignum *b, |
| 2008 | const struct crypto_bignum *c, |
| 2009 | struct crypto_bignum *d) |
| 2010 | { |
| 2011 | int res; |
| 2012 | BN_CTX *bnctx; |
| 2013 | |
Dmitry Shmidt | e466304 | 2016-04-04 10:07:49 -0700 | [diff] [blame] | 2014 | if (TEST_FAIL()) |
| 2015 | return -1; |
| 2016 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2017 | bnctx = BN_CTX_new(); |
| 2018 | if (bnctx == NULL) |
| 2019 | return -1; |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 2020 | res = BN_mod_exp_mont_consttime((BIGNUM *) d, (const BIGNUM *) a, |
| 2021 | (const BIGNUM *) b, (const BIGNUM *) c, |
| 2022 | bnctx, NULL); |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2023 | BN_CTX_free(bnctx); |
| 2024 | |
| 2025 | return res ? 0 : -1; |
| 2026 | } |
| 2027 | |
| 2028 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2029 | int crypto_bignum_inverse(const struct crypto_bignum *a, |
| 2030 | const struct crypto_bignum *b, |
| 2031 | struct crypto_bignum *c) |
| 2032 | { |
| 2033 | BIGNUM *res; |
| 2034 | BN_CTX *bnctx; |
| 2035 | |
Dmitry Shmidt | e466304 | 2016-04-04 10:07:49 -0700 | [diff] [blame] | 2036 | if (TEST_FAIL()) |
| 2037 | return -1; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2038 | bnctx = BN_CTX_new(); |
| 2039 | if (bnctx == NULL) |
| 2040 | return -1; |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 2041 | #ifdef OPENSSL_IS_BORINGSSL |
| 2042 | /* TODO: use BN_mod_inverse_blinded() ? */ |
| 2043 | #else /* OPENSSL_IS_BORINGSSL */ |
| 2044 | BN_set_flags((BIGNUM *) a, BN_FLG_CONSTTIME); |
| 2045 | #endif /* OPENSSL_IS_BORINGSSL */ |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2046 | res = BN_mod_inverse((BIGNUM *) c, (const BIGNUM *) a, |
| 2047 | (const BIGNUM *) b, bnctx); |
| 2048 | BN_CTX_free(bnctx); |
| 2049 | |
| 2050 | return res ? 0 : -1; |
| 2051 | } |
| 2052 | |
| 2053 | |
| 2054 | int crypto_bignum_sub(const struct crypto_bignum *a, |
| 2055 | const struct crypto_bignum *b, |
| 2056 | struct crypto_bignum *c) |
| 2057 | { |
Dmitry Shmidt | e466304 | 2016-04-04 10:07:49 -0700 | [diff] [blame] | 2058 | if (TEST_FAIL()) |
| 2059 | return -1; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2060 | return BN_sub((BIGNUM *) c, (const BIGNUM *) a, (const BIGNUM *) b) ? |
| 2061 | 0 : -1; |
| 2062 | } |
| 2063 | |
| 2064 | |
| 2065 | int crypto_bignum_div(const struct crypto_bignum *a, |
| 2066 | const struct crypto_bignum *b, |
| 2067 | struct crypto_bignum *c) |
| 2068 | { |
| 2069 | int res; |
| 2070 | |
| 2071 | BN_CTX *bnctx; |
| 2072 | |
Dmitry Shmidt | e466304 | 2016-04-04 10:07:49 -0700 | [diff] [blame] | 2073 | if (TEST_FAIL()) |
| 2074 | return -1; |
| 2075 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2076 | bnctx = BN_CTX_new(); |
| 2077 | if (bnctx == NULL) |
| 2078 | return -1; |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 2079 | #ifndef OPENSSL_IS_BORINGSSL |
| 2080 | BN_set_flags((BIGNUM *) a, BN_FLG_CONSTTIME); |
| 2081 | #endif /* OPENSSL_IS_BORINGSSL */ |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2082 | res = BN_div((BIGNUM *) c, NULL, (const BIGNUM *) a, |
| 2083 | (const BIGNUM *) b, bnctx); |
| 2084 | BN_CTX_free(bnctx); |
| 2085 | |
| 2086 | return res ? 0 : -1; |
| 2087 | } |
| 2088 | |
| 2089 | |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 2090 | int crypto_bignum_addmod(const struct crypto_bignum *a, |
| 2091 | const struct crypto_bignum *b, |
| 2092 | const struct crypto_bignum *c, |
| 2093 | struct crypto_bignum *d) |
| 2094 | { |
| 2095 | int res; |
| 2096 | BN_CTX *bnctx; |
| 2097 | |
| 2098 | if (TEST_FAIL()) |
| 2099 | return -1; |
| 2100 | |
| 2101 | bnctx = BN_CTX_new(); |
| 2102 | if (!bnctx) |
| 2103 | return -1; |
| 2104 | res = BN_mod_add((BIGNUM *) d, (const BIGNUM *) a, (const BIGNUM *) b, |
| 2105 | (const BIGNUM *) c, bnctx); |
| 2106 | BN_CTX_free(bnctx); |
| 2107 | |
| 2108 | return res ? 0 : -1; |
| 2109 | } |
| 2110 | |
| 2111 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2112 | int crypto_bignum_mulmod(const struct crypto_bignum *a, |
| 2113 | const struct crypto_bignum *b, |
| 2114 | const struct crypto_bignum *c, |
| 2115 | struct crypto_bignum *d) |
| 2116 | { |
| 2117 | int res; |
| 2118 | |
| 2119 | BN_CTX *bnctx; |
| 2120 | |
Dmitry Shmidt | e466304 | 2016-04-04 10:07:49 -0700 | [diff] [blame] | 2121 | if (TEST_FAIL()) |
| 2122 | return -1; |
| 2123 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2124 | bnctx = BN_CTX_new(); |
| 2125 | if (bnctx == NULL) |
| 2126 | return -1; |
| 2127 | res = BN_mod_mul((BIGNUM *) d, (const BIGNUM *) a, (const BIGNUM *) b, |
| 2128 | (const BIGNUM *) c, bnctx); |
| 2129 | BN_CTX_free(bnctx); |
| 2130 | |
| 2131 | return res ? 0 : -1; |
| 2132 | } |
| 2133 | |
| 2134 | |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 2135 | int crypto_bignum_sqrmod(const struct crypto_bignum *a, |
| 2136 | const struct crypto_bignum *b, |
| 2137 | struct crypto_bignum *c) |
| 2138 | { |
| 2139 | int res; |
| 2140 | BN_CTX *bnctx; |
| 2141 | |
| 2142 | if (TEST_FAIL()) |
| 2143 | return -1; |
| 2144 | |
| 2145 | bnctx = BN_CTX_new(); |
| 2146 | if (!bnctx) |
| 2147 | return -1; |
| 2148 | res = BN_mod_sqr((BIGNUM *) c, (const BIGNUM *) a, (const BIGNUM *) b, |
| 2149 | bnctx); |
| 2150 | BN_CTX_free(bnctx); |
| 2151 | |
| 2152 | return res ? 0 : -1; |
| 2153 | } |
| 2154 | |
| 2155 | |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 2156 | int crypto_bignum_rshift(const struct crypto_bignum *a, int n, |
| 2157 | struct crypto_bignum *r) |
| 2158 | { |
| 2159 | /* Note: BN_rshift() does not modify the first argument even though it |
| 2160 | * has not been marked const. */ |
| 2161 | return BN_rshift((BIGNUM *) a, (BIGNUM *) r, n) == 1 ? 0 : -1; |
| 2162 | } |
| 2163 | |
| 2164 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2165 | int crypto_bignum_cmp(const struct crypto_bignum *a, |
| 2166 | const struct crypto_bignum *b) |
| 2167 | { |
| 2168 | return BN_cmp((const BIGNUM *) a, (const BIGNUM *) b); |
| 2169 | } |
| 2170 | |
| 2171 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2172 | int crypto_bignum_is_zero(const struct crypto_bignum *a) |
| 2173 | { |
| 2174 | return BN_is_zero((const BIGNUM *) a); |
| 2175 | } |
| 2176 | |
| 2177 | |
| 2178 | int crypto_bignum_is_one(const struct crypto_bignum *a) |
| 2179 | { |
| 2180 | return BN_is_one((const BIGNUM *) a); |
| 2181 | } |
| 2182 | |
| 2183 | |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 2184 | int crypto_bignum_is_odd(const struct crypto_bignum *a) |
| 2185 | { |
| 2186 | return BN_is_odd((const BIGNUM *) a); |
| 2187 | } |
| 2188 | |
| 2189 | |
Dmitry Shmidt | 4171258 | 2015-06-29 11:02:15 -0700 | [diff] [blame] | 2190 | int crypto_bignum_legendre(const struct crypto_bignum *a, |
| 2191 | const struct crypto_bignum *p) |
| 2192 | { |
| 2193 | BN_CTX *bnctx; |
| 2194 | BIGNUM *exp = NULL, *tmp = NULL; |
| 2195 | int res = -2; |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 2196 | unsigned int mask; |
Dmitry Shmidt | 4171258 | 2015-06-29 11:02:15 -0700 | [diff] [blame] | 2197 | |
Dmitry Shmidt | e466304 | 2016-04-04 10:07:49 -0700 | [diff] [blame] | 2198 | if (TEST_FAIL()) |
| 2199 | return -2; |
| 2200 | |
Dmitry Shmidt | 4171258 | 2015-06-29 11:02:15 -0700 | [diff] [blame] | 2201 | bnctx = BN_CTX_new(); |
| 2202 | if (bnctx == NULL) |
| 2203 | return -2; |
| 2204 | |
| 2205 | exp = BN_new(); |
| 2206 | tmp = BN_new(); |
| 2207 | if (!exp || !tmp || |
| 2208 | /* exp = (p-1) / 2 */ |
| 2209 | !BN_sub(exp, (const BIGNUM *) p, BN_value_one()) || |
| 2210 | !BN_rshift1(exp, exp) || |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 2211 | !BN_mod_exp_mont_consttime(tmp, (const BIGNUM *) a, exp, |
| 2212 | (const BIGNUM *) p, bnctx, NULL)) |
Dmitry Shmidt | 4171258 | 2015-06-29 11:02:15 -0700 | [diff] [blame] | 2213 | goto fail; |
| 2214 | |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 2215 | /* Return 1 if tmp == 1, 0 if tmp == 0, or -1 otherwise. Need to use |
| 2216 | * constant time selection to avoid branches here. */ |
| 2217 | res = -1; |
| 2218 | mask = const_time_eq(BN_is_word(tmp, 1), 1); |
| 2219 | res = const_time_select_int(mask, 1, res); |
| 2220 | mask = const_time_eq(BN_is_zero(tmp), 1); |
| 2221 | res = const_time_select_int(mask, 0, res); |
Dmitry Shmidt | 4171258 | 2015-06-29 11:02:15 -0700 | [diff] [blame] | 2222 | |
| 2223 | fail: |
| 2224 | BN_clear_free(tmp); |
| 2225 | BN_clear_free(exp); |
| 2226 | BN_CTX_free(bnctx); |
| 2227 | return res; |
| 2228 | } |
| 2229 | |
| 2230 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2231 | #ifdef CONFIG_ECC |
| 2232 | |
| 2233 | struct crypto_ec { |
| 2234 | EC_GROUP *group; |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2235 | int nid; |
Sunil | 8cd6f4d | 2022-06-28 18:40:46 +0000 | [diff] [blame] | 2236 | int iana_group; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2237 | BN_CTX *bnctx; |
| 2238 | BIGNUM *prime; |
| 2239 | BIGNUM *order; |
Dmitry Shmidt | 4171258 | 2015-06-29 11:02:15 -0700 | [diff] [blame] | 2240 | BIGNUM *a; |
| 2241 | BIGNUM *b; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2242 | }; |
| 2243 | |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 2244 | |
| 2245 | static int crypto_ec_group_2_nid(int group) |
| 2246 | { |
| 2247 | /* Map from IANA registry for IKE D-H groups to OpenSSL NID */ |
| 2248 | switch (group) { |
| 2249 | case 19: |
| 2250 | return NID_X9_62_prime256v1; |
| 2251 | case 20: |
| 2252 | return NID_secp384r1; |
| 2253 | case 21: |
| 2254 | return NID_secp521r1; |
| 2255 | case 25: |
| 2256 | return NID_X9_62_prime192v1; |
| 2257 | case 26: |
| 2258 | return NID_secp224r1; |
| 2259 | #ifdef NID_brainpoolP224r1 |
| 2260 | case 27: |
| 2261 | return NID_brainpoolP224r1; |
| 2262 | #endif /* NID_brainpoolP224r1 */ |
| 2263 | #ifdef NID_brainpoolP256r1 |
| 2264 | case 28: |
| 2265 | return NID_brainpoolP256r1; |
| 2266 | #endif /* NID_brainpoolP256r1 */ |
| 2267 | #ifdef NID_brainpoolP384r1 |
| 2268 | case 29: |
| 2269 | return NID_brainpoolP384r1; |
| 2270 | #endif /* NID_brainpoolP384r1 */ |
| 2271 | #ifdef NID_brainpoolP512r1 |
| 2272 | case 30: |
| 2273 | return NID_brainpoolP512r1; |
| 2274 | #endif /* NID_brainpoolP512r1 */ |
| 2275 | default: |
| 2276 | return -1; |
| 2277 | } |
| 2278 | } |
| 2279 | |
| 2280 | |
Sunil | 8cd6f4d | 2022-06-28 18:40:46 +0000 | [diff] [blame] | 2281 | #if OPENSSL_VERSION_NUMBER >= 0x30000000L |
| 2282 | static const char * crypto_ec_group_2_name(int group) |
| 2283 | { |
| 2284 | /* Map from IANA registry for IKE D-H groups to OpenSSL group name */ |
| 2285 | switch (group) { |
| 2286 | case 19: |
| 2287 | return "prime256v1"; |
| 2288 | case 20: |
| 2289 | return "secp384r1"; |
| 2290 | case 21: |
| 2291 | return "secp521r1"; |
| 2292 | case 25: |
| 2293 | return "prime192v1"; |
| 2294 | case 26: |
| 2295 | return "secp224r1"; |
| 2296 | #ifdef NID_brainpoolP224r1 |
| 2297 | case 27: |
| 2298 | return "brainpoolP224r1"; |
| 2299 | #endif /* NID_brainpoolP224r1 */ |
| 2300 | #ifdef NID_brainpoolP256r1 |
| 2301 | case 28: |
| 2302 | return "brainpoolP256r1"; |
| 2303 | #endif /* NID_brainpoolP256r1 */ |
| 2304 | #ifdef NID_brainpoolP384r1 |
| 2305 | case 29: |
| 2306 | return "brainpoolP384r1"; |
| 2307 | #endif /* NID_brainpoolP384r1 */ |
| 2308 | #ifdef NID_brainpoolP512r1 |
| 2309 | case 30: |
| 2310 | return "brainpoolP512r1"; |
| 2311 | #endif /* NID_brainpoolP512r1 */ |
| 2312 | default: |
| 2313 | return NULL; |
| 2314 | } |
| 2315 | } |
| 2316 | #endif /* OpenSSL version >= 3.0 */ |
| 2317 | |
| 2318 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2319 | struct crypto_ec * crypto_ec_init(int group) |
| 2320 | { |
| 2321 | struct crypto_ec *e; |
| 2322 | int nid; |
| 2323 | |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 2324 | nid = crypto_ec_group_2_nid(group); |
| 2325 | if (nid < 0) |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2326 | return NULL; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2327 | |
| 2328 | e = os_zalloc(sizeof(*e)); |
| 2329 | if (e == NULL) |
| 2330 | return NULL; |
| 2331 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2332 | e->nid = nid; |
Sunil | 8cd6f4d | 2022-06-28 18:40:46 +0000 | [diff] [blame] | 2333 | e->iana_group = group; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2334 | e->bnctx = BN_CTX_new(); |
| 2335 | e->group = EC_GROUP_new_by_curve_name(nid); |
| 2336 | e->prime = BN_new(); |
| 2337 | e->order = BN_new(); |
Dmitry Shmidt | 4171258 | 2015-06-29 11:02:15 -0700 | [diff] [blame] | 2338 | e->a = BN_new(); |
| 2339 | e->b = BN_new(); |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2340 | if (e->group == NULL || e->bnctx == NULL || e->prime == NULL || |
Dmitry Shmidt | 4171258 | 2015-06-29 11:02:15 -0700 | [diff] [blame] | 2341 | e->order == NULL || e->a == NULL || e->b == NULL || |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 2342 | !EC_GROUP_get_curve(e->group, e->prime, e->a, e->b, e->bnctx) || |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2343 | !EC_GROUP_get_order(e->group, e->order, e->bnctx)) { |
| 2344 | crypto_ec_deinit(e); |
| 2345 | e = NULL; |
| 2346 | } |
| 2347 | |
| 2348 | return e; |
| 2349 | } |
| 2350 | |
| 2351 | |
| 2352 | void crypto_ec_deinit(struct crypto_ec *e) |
| 2353 | { |
| 2354 | if (e == NULL) |
| 2355 | return; |
Dmitry Shmidt | 4171258 | 2015-06-29 11:02:15 -0700 | [diff] [blame] | 2356 | BN_clear_free(e->b); |
| 2357 | BN_clear_free(e->a); |
Dmitry Shmidt | 7f0b69e | 2014-07-28 10:35:20 -0700 | [diff] [blame] | 2358 | BN_clear_free(e->order); |
Dmitry Shmidt | 661b4f7 | 2014-09-29 14:58:27 -0700 | [diff] [blame] | 2359 | BN_clear_free(e->prime); |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2360 | EC_GROUP_free(e->group); |
| 2361 | BN_CTX_free(e->bnctx); |
| 2362 | os_free(e); |
| 2363 | } |
| 2364 | |
| 2365 | |
| 2366 | struct crypto_ec_point * crypto_ec_point_init(struct crypto_ec *e) |
| 2367 | { |
Dmitry Shmidt | e466304 | 2016-04-04 10:07:49 -0700 | [diff] [blame] | 2368 | if (TEST_FAIL()) |
| 2369 | return NULL; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2370 | if (e == NULL) |
| 2371 | return NULL; |
| 2372 | return (struct crypto_ec_point *) EC_POINT_new(e->group); |
| 2373 | } |
| 2374 | |
| 2375 | |
| 2376 | size_t crypto_ec_prime_len(struct crypto_ec *e) |
| 2377 | { |
| 2378 | return BN_num_bytes(e->prime); |
| 2379 | } |
| 2380 | |
| 2381 | |
| 2382 | size_t crypto_ec_prime_len_bits(struct crypto_ec *e) |
| 2383 | { |
| 2384 | return BN_num_bits(e->prime); |
| 2385 | } |
| 2386 | |
| 2387 | |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 2388 | size_t crypto_ec_order_len(struct crypto_ec *e) |
| 2389 | { |
| 2390 | return BN_num_bytes(e->order); |
| 2391 | } |
| 2392 | |
| 2393 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2394 | const struct crypto_bignum * crypto_ec_get_prime(struct crypto_ec *e) |
| 2395 | { |
| 2396 | return (const struct crypto_bignum *) e->prime; |
| 2397 | } |
| 2398 | |
| 2399 | |
| 2400 | const struct crypto_bignum * crypto_ec_get_order(struct crypto_ec *e) |
| 2401 | { |
| 2402 | return (const struct crypto_bignum *) e->order; |
| 2403 | } |
| 2404 | |
| 2405 | |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 2406 | const struct crypto_bignum * crypto_ec_get_a(struct crypto_ec *e) |
| 2407 | { |
| 2408 | return (const struct crypto_bignum *) e->a; |
| 2409 | } |
| 2410 | |
| 2411 | |
| 2412 | const struct crypto_bignum * crypto_ec_get_b(struct crypto_ec *e) |
| 2413 | { |
| 2414 | return (const struct crypto_bignum *) e->b; |
| 2415 | } |
| 2416 | |
| 2417 | |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 2418 | const struct crypto_ec_point * crypto_ec_get_generator(struct crypto_ec *e) |
| 2419 | { |
| 2420 | return (const struct crypto_ec_point *) |
| 2421 | EC_GROUP_get0_generator(e->group); |
| 2422 | } |
| 2423 | |
| 2424 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2425 | void crypto_ec_point_deinit(struct crypto_ec_point *p, int clear) |
| 2426 | { |
| 2427 | if (clear) |
| 2428 | EC_POINT_clear_free((EC_POINT *) p); |
| 2429 | else |
| 2430 | EC_POINT_free((EC_POINT *) p); |
| 2431 | } |
| 2432 | |
| 2433 | |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 2434 | int crypto_ec_point_x(struct crypto_ec *e, const struct crypto_ec_point *p, |
| 2435 | struct crypto_bignum *x) |
| 2436 | { |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 2437 | return EC_POINT_get_affine_coordinates(e->group, |
| 2438 | (const EC_POINT *) p, |
| 2439 | (BIGNUM *) x, NULL, |
| 2440 | e->bnctx) == 1 ? 0 : -1; |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 2441 | } |
| 2442 | |
| 2443 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2444 | int crypto_ec_point_to_bin(struct crypto_ec *e, |
| 2445 | const struct crypto_ec_point *point, u8 *x, u8 *y) |
| 2446 | { |
| 2447 | BIGNUM *x_bn, *y_bn; |
| 2448 | int ret = -1; |
| 2449 | int len = BN_num_bytes(e->prime); |
| 2450 | |
Dmitry Shmidt | e466304 | 2016-04-04 10:07:49 -0700 | [diff] [blame] | 2451 | if (TEST_FAIL()) |
| 2452 | return -1; |
| 2453 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2454 | x_bn = BN_new(); |
| 2455 | y_bn = BN_new(); |
| 2456 | |
| 2457 | if (x_bn && y_bn && |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 2458 | EC_POINT_get_affine_coordinates(e->group, (EC_POINT *) point, |
| 2459 | x_bn, y_bn, e->bnctx)) { |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2460 | if (x) { |
| 2461 | crypto_bignum_to_bin((struct crypto_bignum *) x_bn, |
| 2462 | x, len, len); |
| 2463 | } |
| 2464 | if (y) { |
| 2465 | crypto_bignum_to_bin((struct crypto_bignum *) y_bn, |
| 2466 | y, len, len); |
| 2467 | } |
| 2468 | ret = 0; |
| 2469 | } |
| 2470 | |
Dmitry Shmidt | 7f0b69e | 2014-07-28 10:35:20 -0700 | [diff] [blame] | 2471 | BN_clear_free(x_bn); |
| 2472 | BN_clear_free(y_bn); |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2473 | return ret; |
| 2474 | } |
| 2475 | |
| 2476 | |
| 2477 | struct crypto_ec_point * crypto_ec_point_from_bin(struct crypto_ec *e, |
| 2478 | const u8 *val) |
| 2479 | { |
| 2480 | BIGNUM *x, *y; |
| 2481 | EC_POINT *elem; |
| 2482 | int len = BN_num_bytes(e->prime); |
| 2483 | |
Dmitry Shmidt | e466304 | 2016-04-04 10:07:49 -0700 | [diff] [blame] | 2484 | if (TEST_FAIL()) |
| 2485 | return NULL; |
| 2486 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2487 | x = BN_bin2bn(val, len, NULL); |
| 2488 | y = BN_bin2bn(val + len, len, NULL); |
| 2489 | elem = EC_POINT_new(e->group); |
| 2490 | if (x == NULL || y == NULL || elem == NULL) { |
Dmitry Shmidt | 7f0b69e | 2014-07-28 10:35:20 -0700 | [diff] [blame] | 2491 | BN_clear_free(x); |
| 2492 | BN_clear_free(y); |
| 2493 | EC_POINT_clear_free(elem); |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2494 | return NULL; |
| 2495 | } |
| 2496 | |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 2497 | if (!EC_POINT_set_affine_coordinates(e->group, elem, x, y, e->bnctx)) { |
Dmitry Shmidt | 7f0b69e | 2014-07-28 10:35:20 -0700 | [diff] [blame] | 2498 | EC_POINT_clear_free(elem); |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2499 | elem = NULL; |
| 2500 | } |
| 2501 | |
Dmitry Shmidt | 7f0b69e | 2014-07-28 10:35:20 -0700 | [diff] [blame] | 2502 | BN_clear_free(x); |
| 2503 | BN_clear_free(y); |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2504 | |
| 2505 | return (struct crypto_ec_point *) elem; |
| 2506 | } |
| 2507 | |
| 2508 | |
| 2509 | int crypto_ec_point_add(struct crypto_ec *e, const struct crypto_ec_point *a, |
| 2510 | const struct crypto_ec_point *b, |
| 2511 | struct crypto_ec_point *c) |
| 2512 | { |
Dmitry Shmidt | e466304 | 2016-04-04 10:07:49 -0700 | [diff] [blame] | 2513 | if (TEST_FAIL()) |
| 2514 | return -1; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2515 | return EC_POINT_add(e->group, (EC_POINT *) c, (const EC_POINT *) a, |
| 2516 | (const EC_POINT *) b, e->bnctx) ? 0 : -1; |
| 2517 | } |
| 2518 | |
| 2519 | |
| 2520 | int crypto_ec_point_mul(struct crypto_ec *e, const struct crypto_ec_point *p, |
| 2521 | const struct crypto_bignum *b, |
| 2522 | struct crypto_ec_point *res) |
| 2523 | { |
Dmitry Shmidt | e466304 | 2016-04-04 10:07:49 -0700 | [diff] [blame] | 2524 | if (TEST_FAIL()) |
| 2525 | return -1; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2526 | return EC_POINT_mul(e->group, (EC_POINT *) res, NULL, |
| 2527 | (const EC_POINT *) p, (const BIGNUM *) b, e->bnctx) |
| 2528 | ? 0 : -1; |
| 2529 | } |
| 2530 | |
| 2531 | |
| 2532 | int crypto_ec_point_invert(struct crypto_ec *e, struct crypto_ec_point *p) |
| 2533 | { |
Dmitry Shmidt | e466304 | 2016-04-04 10:07:49 -0700 | [diff] [blame] | 2534 | if (TEST_FAIL()) |
| 2535 | return -1; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2536 | return EC_POINT_invert(e->group, (EC_POINT *) p, e->bnctx) ? 0 : -1; |
| 2537 | } |
| 2538 | |
| 2539 | |
Dmitry Shmidt | 4171258 | 2015-06-29 11:02:15 -0700 | [diff] [blame] | 2540 | struct crypto_bignum * |
| 2541 | crypto_ec_point_compute_y_sqr(struct crypto_ec *e, |
| 2542 | const struct crypto_bignum *x) |
| 2543 | { |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 2544 | BIGNUM *tmp; |
Dmitry Shmidt | 4171258 | 2015-06-29 11:02:15 -0700 | [diff] [blame] | 2545 | |
Dmitry Shmidt | e466304 | 2016-04-04 10:07:49 -0700 | [diff] [blame] | 2546 | if (TEST_FAIL()) |
| 2547 | return NULL; |
| 2548 | |
Dmitry Shmidt | 4171258 | 2015-06-29 11:02:15 -0700 | [diff] [blame] | 2549 | tmp = BN_new(); |
Dmitry Shmidt | 4171258 | 2015-06-29 11:02:15 -0700 | [diff] [blame] | 2550 | |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 2551 | /* y^2 = x^3 + ax + b = (x^2 + a)x + b */ |
| 2552 | if (tmp && |
Dmitry Shmidt | 4171258 | 2015-06-29 11:02:15 -0700 | [diff] [blame] | 2553 | BN_mod_sqr(tmp, (const BIGNUM *) x, e->prime, e->bnctx) && |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 2554 | BN_mod_add_quick(tmp, e->a, tmp, e->prime) && |
Dmitry Shmidt | 4171258 | 2015-06-29 11:02:15 -0700 | [diff] [blame] | 2555 | BN_mod_mul(tmp, tmp, (const BIGNUM *) x, e->prime, e->bnctx) && |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 2556 | BN_mod_add_quick(tmp, tmp, e->b, e->prime)) |
| 2557 | return (struct crypto_bignum *) tmp; |
Dmitry Shmidt | 4171258 | 2015-06-29 11:02:15 -0700 | [diff] [blame] | 2558 | |
| 2559 | BN_clear_free(tmp); |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 2560 | return NULL; |
Dmitry Shmidt | 4171258 | 2015-06-29 11:02:15 -0700 | [diff] [blame] | 2561 | } |
| 2562 | |
| 2563 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2564 | int crypto_ec_point_is_at_infinity(struct crypto_ec *e, |
| 2565 | const struct crypto_ec_point *p) |
| 2566 | { |
| 2567 | return EC_POINT_is_at_infinity(e->group, (const EC_POINT *) p); |
| 2568 | } |
| 2569 | |
| 2570 | |
| 2571 | int crypto_ec_point_is_on_curve(struct crypto_ec *e, |
| 2572 | const struct crypto_ec_point *p) |
| 2573 | { |
Dmitry Shmidt | 4171258 | 2015-06-29 11:02:15 -0700 | [diff] [blame] | 2574 | return EC_POINT_is_on_curve(e->group, (const EC_POINT *) p, |
| 2575 | e->bnctx) == 1; |
| 2576 | } |
| 2577 | |
| 2578 | |
| 2579 | int crypto_ec_point_cmp(const struct crypto_ec *e, |
| 2580 | const struct crypto_ec_point *a, |
| 2581 | const struct crypto_ec_point *b) |
| 2582 | { |
| 2583 | return EC_POINT_cmp(e->group, (const EC_POINT *) a, |
| 2584 | (const EC_POINT *) b, e->bnctx); |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2585 | } |
| 2586 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2587 | |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 2588 | void crypto_ec_point_debug_print(const struct crypto_ec *e, |
| 2589 | const struct crypto_ec_point *p, |
| 2590 | const char *title) |
| 2591 | { |
| 2592 | BIGNUM *x, *y; |
| 2593 | char *x_str = NULL, *y_str = NULL; |
| 2594 | |
| 2595 | x = BN_new(); |
| 2596 | y = BN_new(); |
| 2597 | if (!x || !y || |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 2598 | EC_POINT_get_affine_coordinates(e->group, (const EC_POINT *) p, |
| 2599 | x, y, e->bnctx) != 1) |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 2600 | goto fail; |
| 2601 | |
| 2602 | x_str = BN_bn2hex(x); |
| 2603 | y_str = BN_bn2hex(y); |
| 2604 | if (!x_str || !y_str) |
| 2605 | goto fail; |
| 2606 | |
| 2607 | wpa_printf(MSG_DEBUG, "%s (%s,%s)", title, x_str, y_str); |
| 2608 | |
| 2609 | fail: |
| 2610 | OPENSSL_free(x_str); |
| 2611 | OPENSSL_free(y_str); |
| 2612 | BN_free(x); |
| 2613 | BN_free(y); |
| 2614 | } |
| 2615 | |
| 2616 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2617 | struct crypto_ecdh { |
| 2618 | struct crypto_ec *ec; |
| 2619 | EVP_PKEY *pkey; |
| 2620 | }; |
| 2621 | |
| 2622 | struct crypto_ecdh * crypto_ecdh_init(int group) |
| 2623 | { |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 2624 | #if OPENSSL_VERSION_NUMBER >= 0x30000000L |
| 2625 | struct crypto_ecdh *ecdh; |
| 2626 | const char *name; |
| 2627 | |
| 2628 | ecdh = os_zalloc(sizeof(*ecdh)); |
| 2629 | if (!ecdh) |
| 2630 | goto fail; |
| 2631 | |
| 2632 | ecdh->ec = crypto_ec_init(group); |
| 2633 | if (!ecdh->ec) |
| 2634 | goto fail; |
| 2635 | |
| 2636 | name = OSSL_EC_curve_nid2name(ecdh->ec->nid); |
| 2637 | if (!name) |
| 2638 | goto fail; |
| 2639 | |
| 2640 | ecdh->pkey = EVP_EC_gen(name); |
| 2641 | if (!ecdh->pkey) |
| 2642 | goto fail; |
| 2643 | |
| 2644 | done: |
| 2645 | return ecdh; |
| 2646 | fail: |
| 2647 | crypto_ecdh_deinit(ecdh); |
| 2648 | ecdh = NULL; |
| 2649 | goto done; |
| 2650 | #else /* OpenSSL version >= 3.0 */ |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2651 | struct crypto_ecdh *ecdh; |
| 2652 | EVP_PKEY *params = NULL; |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 2653 | EC_KEY *ec_params = NULL; |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2654 | EVP_PKEY_CTX *kctx = NULL; |
| 2655 | |
| 2656 | ecdh = os_zalloc(sizeof(*ecdh)); |
| 2657 | if (!ecdh) |
| 2658 | goto fail; |
| 2659 | |
| 2660 | ecdh->ec = crypto_ec_init(group); |
| 2661 | if (!ecdh->ec) |
| 2662 | goto fail; |
| 2663 | |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 2664 | ec_params = EC_KEY_new_by_curve_name(ecdh->ec->nid); |
| 2665 | if (!ec_params) { |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2666 | wpa_printf(MSG_ERROR, |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 2667 | "OpenSSL: Failed to generate EC_KEY parameters"); |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2668 | goto fail; |
| 2669 | } |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 2670 | EC_KEY_set_asn1_flag(ec_params, OPENSSL_EC_NAMED_CURVE); |
| 2671 | params = EVP_PKEY_new(); |
| 2672 | if (!params || EVP_PKEY_set1_EC_KEY(params, ec_params) != 1) { |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2673 | wpa_printf(MSG_ERROR, |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 2674 | "OpenSSL: Failed to generate EVP_PKEY parameters"); |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2675 | goto fail; |
| 2676 | } |
| 2677 | |
| 2678 | kctx = EVP_PKEY_CTX_new(params, NULL); |
| 2679 | if (!kctx) |
| 2680 | goto fail; |
| 2681 | |
| 2682 | if (EVP_PKEY_keygen_init(kctx) != 1) { |
| 2683 | wpa_printf(MSG_ERROR, |
| 2684 | "OpenSSL: EVP_PKEY_keygen_init failed: %s", |
| 2685 | ERR_error_string(ERR_get_error(), NULL)); |
| 2686 | goto fail; |
| 2687 | } |
| 2688 | |
| 2689 | if (EVP_PKEY_keygen(kctx, &ecdh->pkey) != 1) { |
| 2690 | wpa_printf(MSG_ERROR, "OpenSSL: EVP_PKEY_keygen failed: %s", |
| 2691 | ERR_error_string(ERR_get_error(), NULL)); |
| 2692 | goto fail; |
| 2693 | } |
| 2694 | |
| 2695 | done: |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 2696 | EC_KEY_free(ec_params); |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2697 | EVP_PKEY_free(params); |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2698 | EVP_PKEY_CTX_free(kctx); |
| 2699 | |
| 2700 | return ecdh; |
| 2701 | fail: |
| 2702 | crypto_ecdh_deinit(ecdh); |
| 2703 | ecdh = NULL; |
| 2704 | goto done; |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 2705 | #endif /* OpenSSL version >= 3.0 */ |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2706 | } |
| 2707 | |
| 2708 | |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 2709 | struct crypto_ecdh * crypto_ecdh_init2(int group, struct crypto_ec_key *own_key) |
| 2710 | { |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 2711 | #if OPENSSL_VERSION_NUMBER >= 0x30000000L |
| 2712 | struct crypto_ecdh *ecdh; |
| 2713 | |
| 2714 | ecdh = os_zalloc(sizeof(*ecdh)); |
| 2715 | if (!ecdh) |
| 2716 | goto fail; |
| 2717 | |
| 2718 | ecdh->ec = crypto_ec_init(group); |
| 2719 | if (!ecdh->ec) |
| 2720 | goto fail; |
| 2721 | |
| 2722 | ecdh->pkey = EVP_PKEY_dup((EVP_PKEY *) own_key); |
| 2723 | if (!ecdh->pkey) |
| 2724 | goto fail; |
| 2725 | |
| 2726 | return ecdh; |
| 2727 | fail: |
| 2728 | crypto_ecdh_deinit(ecdh); |
| 2729 | return NULL; |
| 2730 | #else /* OpenSSL version >= 3.0 */ |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 2731 | struct crypto_ecdh *ecdh; |
| 2732 | |
| 2733 | ecdh = os_zalloc(sizeof(*ecdh)); |
| 2734 | if (!ecdh) |
| 2735 | goto fail; |
| 2736 | |
| 2737 | ecdh->ec = crypto_ec_init(group); |
| 2738 | if (!ecdh->ec) |
| 2739 | goto fail; |
| 2740 | |
| 2741 | ecdh->pkey = EVP_PKEY_new(); |
| 2742 | if (!ecdh->pkey || |
| 2743 | EVP_PKEY_assign_EC_KEY(ecdh->pkey, |
| 2744 | EVP_PKEY_get1_EC_KEY((EVP_PKEY *) own_key)) |
| 2745 | != 1) |
| 2746 | goto fail; |
| 2747 | |
| 2748 | return ecdh; |
| 2749 | fail: |
| 2750 | crypto_ecdh_deinit(ecdh); |
| 2751 | return NULL; |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 2752 | #endif /* OpenSSL version >= 3.0 */ |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 2753 | } |
| 2754 | |
| 2755 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2756 | struct wpabuf * crypto_ecdh_get_pubkey(struct crypto_ecdh *ecdh, int inc_y) |
| 2757 | { |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 2758 | #if OPENSSL_VERSION_NUMBER >= 0x30000000L |
| 2759 | struct wpabuf *buf = NULL; |
| 2760 | unsigned char *pub; |
| 2761 | size_t len, exp_len; |
| 2762 | |
| 2763 | len = EVP_PKEY_get1_encoded_public_key(ecdh->pkey, &pub); |
| 2764 | if (len == 0) |
| 2765 | return NULL; |
| 2766 | |
| 2767 | /* Encoded using SECG SEC 1, Sec. 2.3.4 format */ |
| 2768 | exp_len = 1 + 2 * crypto_ec_prime_len(ecdh->ec); |
| 2769 | if (len != exp_len) { |
| 2770 | wpa_printf(MSG_ERROR, |
| 2771 | "OpenSSL:%s: Unexpected encoded public key length %zu (expected %zu)", |
| 2772 | __func__, len, exp_len); |
| 2773 | goto fail; |
| 2774 | } |
| 2775 | buf = wpabuf_alloc_copy(pub + 1, inc_y ? len - 1 : len / 2); |
| 2776 | fail: |
| 2777 | OPENSSL_free(pub); |
| 2778 | return buf; |
| 2779 | #else /* OpenSSL version >= 3.0 */ |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2780 | struct wpabuf *buf = NULL; |
| 2781 | EC_KEY *eckey; |
| 2782 | const EC_POINT *pubkey; |
| 2783 | BIGNUM *x, *y = NULL; |
| 2784 | int len = BN_num_bytes(ecdh->ec->prime); |
| 2785 | int res; |
| 2786 | |
| 2787 | eckey = EVP_PKEY_get1_EC_KEY(ecdh->pkey); |
| 2788 | if (!eckey) |
| 2789 | return NULL; |
| 2790 | |
| 2791 | pubkey = EC_KEY_get0_public_key(eckey); |
| 2792 | if (!pubkey) |
| 2793 | return NULL; |
| 2794 | |
| 2795 | x = BN_new(); |
| 2796 | if (inc_y) { |
| 2797 | y = BN_new(); |
| 2798 | if (!y) |
| 2799 | goto fail; |
| 2800 | } |
| 2801 | buf = wpabuf_alloc(inc_y ? 2 * len : len); |
| 2802 | if (!x || !buf) |
| 2803 | goto fail; |
| 2804 | |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 2805 | if (EC_POINT_get_affine_coordinates(ecdh->ec->group, pubkey, |
| 2806 | x, y, ecdh->ec->bnctx) != 1) { |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2807 | wpa_printf(MSG_ERROR, |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 2808 | "OpenSSL: EC_POINT_get_affine_coordinates failed: %s", |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2809 | ERR_error_string(ERR_get_error(), NULL)); |
| 2810 | goto fail; |
| 2811 | } |
| 2812 | |
| 2813 | res = crypto_bignum_to_bin((struct crypto_bignum *) x, |
| 2814 | wpabuf_put(buf, len), len, len); |
| 2815 | if (res < 0) |
| 2816 | goto fail; |
| 2817 | |
| 2818 | if (inc_y) { |
| 2819 | res = crypto_bignum_to_bin((struct crypto_bignum *) y, |
| 2820 | wpabuf_put(buf, len), len, len); |
| 2821 | if (res < 0) |
| 2822 | goto fail; |
| 2823 | } |
| 2824 | |
| 2825 | done: |
| 2826 | BN_clear_free(x); |
| 2827 | BN_clear_free(y); |
| 2828 | EC_KEY_free(eckey); |
| 2829 | |
| 2830 | return buf; |
| 2831 | fail: |
| 2832 | wpabuf_free(buf); |
| 2833 | buf = NULL; |
| 2834 | goto done; |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 2835 | #endif /* OpenSSL version >= 3.0 */ |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2836 | } |
| 2837 | |
| 2838 | |
| 2839 | struct wpabuf * crypto_ecdh_set_peerkey(struct crypto_ecdh *ecdh, int inc_y, |
| 2840 | const u8 *key, size_t len) |
| 2841 | { |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 2842 | #if OPENSSL_VERSION_NUMBER >= 0x30000000L |
| 2843 | EVP_PKEY *peerkey = EVP_PKEY_new(); |
| 2844 | EVP_PKEY_CTX *ctx; |
| 2845 | size_t res_len; |
| 2846 | struct wpabuf *res = NULL; |
| 2847 | u8 *peer; |
| 2848 | |
| 2849 | /* Encode using SECG SEC 1, Sec. 2.3.4 format */ |
| 2850 | peer = os_malloc(1 + len); |
| 2851 | if (!peer) |
| 2852 | return NULL; |
| 2853 | peer[0] = inc_y ? 0x04 : 0x02; |
| 2854 | os_memcpy(peer + 1, key, len); |
| 2855 | |
| 2856 | if (!peerkey || |
| 2857 | EVP_PKEY_copy_parameters(peerkey, ecdh->pkey) != 1 || |
| 2858 | EVP_PKEY_set1_encoded_public_key(peerkey, peer, 1 + len) != 1) { |
| 2859 | wpa_printf(MSG_INFO, "OpenSSL: EVP_PKEY_set1_encoded_public_key failed: %s", |
| 2860 | ERR_error_string(ERR_get_error(), NULL)); |
| 2861 | EVP_PKEY_free(peerkey); |
| 2862 | os_free(peer); |
| 2863 | return NULL; |
| 2864 | } |
| 2865 | os_free(peer); |
| 2866 | |
| 2867 | ctx = EVP_PKEY_CTX_new(ecdh->pkey, NULL); |
| 2868 | if (!ctx || |
| 2869 | EVP_PKEY_derive_init(ctx) != 1 || |
| 2870 | EVP_PKEY_derive_set_peer(ctx, peerkey) != 1 || |
| 2871 | EVP_PKEY_derive(ctx, NULL, &res_len) != 1 || |
| 2872 | !(res = wpabuf_alloc(res_len)) || |
| 2873 | EVP_PKEY_derive(ctx, wpabuf_mhead(res), &res_len) != 1) { |
| 2874 | wpa_printf(MSG_INFO, "OpenSSL: EVP_PKEY_derive failed: %s", |
| 2875 | ERR_error_string(ERR_get_error(), NULL)); |
| 2876 | wpabuf_free(res); |
| 2877 | res = NULL; |
| 2878 | } else { |
| 2879 | wpabuf_put(res, res_len); |
| 2880 | } |
| 2881 | |
| 2882 | EVP_PKEY_free(peerkey); |
| 2883 | EVP_PKEY_CTX_free(ctx); |
| 2884 | return res; |
| 2885 | #else /* OpenSSL version >= 3.0 */ |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2886 | BIGNUM *x, *y = NULL; |
| 2887 | EVP_PKEY_CTX *ctx = NULL; |
| 2888 | EVP_PKEY *peerkey = NULL; |
| 2889 | struct wpabuf *secret = NULL; |
| 2890 | size_t secret_len; |
| 2891 | EC_POINT *pub; |
| 2892 | EC_KEY *eckey = NULL; |
| 2893 | |
| 2894 | x = BN_bin2bn(key, inc_y ? len / 2 : len, NULL); |
| 2895 | pub = EC_POINT_new(ecdh->ec->group); |
| 2896 | if (!x || !pub) |
| 2897 | goto fail; |
| 2898 | |
| 2899 | if (inc_y) { |
| 2900 | y = BN_bin2bn(key + len / 2, len / 2, NULL); |
| 2901 | if (!y) |
| 2902 | goto fail; |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 2903 | if (!EC_POINT_set_affine_coordinates(ecdh->ec->group, pub, |
| 2904 | x, y, ecdh->ec->bnctx)) { |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2905 | wpa_printf(MSG_ERROR, |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 2906 | "OpenSSL: EC_POINT_set_affine_coordinates failed: %s", |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2907 | ERR_error_string(ERR_get_error(), NULL)); |
| 2908 | goto fail; |
| 2909 | } |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 2910 | } else if (!EC_POINT_set_compressed_coordinates(ecdh->ec->group, |
| 2911 | pub, x, 0, |
| 2912 | ecdh->ec->bnctx)) { |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2913 | wpa_printf(MSG_ERROR, |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 2914 | "OpenSSL: EC_POINT_set_compressed_coordinates failed: %s", |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2915 | ERR_error_string(ERR_get_error(), NULL)); |
| 2916 | goto fail; |
| 2917 | } |
| 2918 | |
| 2919 | if (!EC_POINT_is_on_curve(ecdh->ec->group, pub, ecdh->ec->bnctx)) { |
| 2920 | wpa_printf(MSG_ERROR, |
| 2921 | "OpenSSL: ECDH peer public key is not on curve"); |
| 2922 | goto fail; |
| 2923 | } |
| 2924 | |
| 2925 | eckey = EC_KEY_new_by_curve_name(ecdh->ec->nid); |
| 2926 | if (!eckey || EC_KEY_set_public_key(eckey, pub) != 1) { |
| 2927 | wpa_printf(MSG_ERROR, |
| 2928 | "OpenSSL: EC_KEY_set_public_key failed: %s", |
| 2929 | ERR_error_string(ERR_get_error(), NULL)); |
| 2930 | goto fail; |
| 2931 | } |
| 2932 | |
| 2933 | peerkey = EVP_PKEY_new(); |
| 2934 | if (!peerkey || EVP_PKEY_set1_EC_KEY(peerkey, eckey) != 1) |
| 2935 | goto fail; |
| 2936 | |
| 2937 | ctx = EVP_PKEY_CTX_new(ecdh->pkey, NULL); |
| 2938 | if (!ctx || EVP_PKEY_derive_init(ctx) != 1 || |
| 2939 | EVP_PKEY_derive_set_peer(ctx, peerkey) != 1 || |
| 2940 | EVP_PKEY_derive(ctx, NULL, &secret_len) != 1) { |
| 2941 | wpa_printf(MSG_ERROR, |
| 2942 | "OpenSSL: EVP_PKEY_derive(1) failed: %s", |
| 2943 | ERR_error_string(ERR_get_error(), NULL)); |
| 2944 | goto fail; |
| 2945 | } |
| 2946 | |
| 2947 | secret = wpabuf_alloc(secret_len); |
| 2948 | if (!secret) |
| 2949 | goto fail; |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 2950 | if (EVP_PKEY_derive(ctx, wpabuf_put(secret, 0), &secret_len) != 1) { |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2951 | wpa_printf(MSG_ERROR, |
| 2952 | "OpenSSL: EVP_PKEY_derive(2) failed: %s", |
| 2953 | ERR_error_string(ERR_get_error(), NULL)); |
| 2954 | goto fail; |
| 2955 | } |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 2956 | if (secret->size != secret_len) |
| 2957 | wpa_printf(MSG_DEBUG, |
| 2958 | "OpenSSL: EVP_PKEY_derive(2) changed secret_len %d -> %d", |
| 2959 | (int) secret->size, (int) secret_len); |
| 2960 | wpabuf_put(secret, secret_len); |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2961 | |
| 2962 | done: |
| 2963 | BN_free(x); |
| 2964 | BN_free(y); |
| 2965 | EC_KEY_free(eckey); |
| 2966 | EC_POINT_free(pub); |
| 2967 | EVP_PKEY_CTX_free(ctx); |
| 2968 | EVP_PKEY_free(peerkey); |
| 2969 | return secret; |
| 2970 | fail: |
| 2971 | wpabuf_free(secret); |
| 2972 | secret = NULL; |
| 2973 | goto done; |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 2974 | #endif /* OpenSSL version >= 3.0 */ |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2975 | } |
| 2976 | |
| 2977 | |
| 2978 | void crypto_ecdh_deinit(struct crypto_ecdh *ecdh) |
| 2979 | { |
| 2980 | if (ecdh) { |
| 2981 | crypto_ec_deinit(ecdh->ec); |
| 2982 | EVP_PKEY_free(ecdh->pkey); |
| 2983 | os_free(ecdh); |
| 2984 | } |
| 2985 | } |
| 2986 | |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 2987 | |
| 2988 | size_t crypto_ecdh_prime_len(struct crypto_ecdh *ecdh) |
| 2989 | { |
| 2990 | return crypto_ec_prime_len(ecdh->ec); |
| 2991 | } |
| 2992 | |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 2993 | |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 2994 | struct crypto_ec_key * crypto_ec_key_parse_priv(const u8 *der, size_t der_len) |
| 2995 | { |
Sunil | 8cd6f4d | 2022-06-28 18:40:46 +0000 | [diff] [blame] | 2996 | #if OPENSSL_VERSION_NUMBER >= 0x30000000L |
| 2997 | EVP_PKEY *pkey = NULL; |
| 2998 | OSSL_DECODER_CTX *ctx; |
| 2999 | |
| 3000 | ctx = OSSL_DECODER_CTX_new_for_pkey( |
| 3001 | &pkey, "DER", NULL, "EC", |
| 3002 | OSSL_KEYMGMT_SELECT_KEYPAIR | |
| 3003 | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS, |
| 3004 | NULL, NULL); |
| 3005 | if (!ctx || |
| 3006 | OSSL_DECODER_from_data(ctx, &der, &der_len) != 1) { |
| 3007 | wpa_printf(MSG_INFO, "OpenSSL: Decoding EC private key (DER) failed: %s", |
| 3008 | ERR_error_string(ERR_get_error(), NULL)); |
| 3009 | goto fail; |
| 3010 | } |
| 3011 | |
| 3012 | return (struct crypto_ec_key *) pkey; |
| 3013 | fail: |
| 3014 | crypto_ec_key_deinit((struct crypto_ec_key *) pkey); |
| 3015 | return NULL; |
| 3016 | #else /* OpenSSL version >= 3.0 */ |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 3017 | EVP_PKEY *pkey = NULL; |
| 3018 | EC_KEY *eckey; |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 3019 | |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 3020 | eckey = d2i_ECPrivateKey(NULL, &der, der_len); |
| 3021 | if (!eckey) { |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 3022 | wpa_printf(MSG_INFO, "OpenSSL: d2i_ECPrivateKey() failed: %s", |
| 3023 | ERR_error_string(ERR_get_error(), NULL)); |
| 3024 | goto fail; |
| 3025 | } |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 3026 | EC_KEY_set_conv_form(eckey, POINT_CONVERSION_COMPRESSED); |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 3027 | |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 3028 | pkey = EVP_PKEY_new(); |
| 3029 | if (!pkey || EVP_PKEY_assign_EC_KEY(pkey, eckey) != 1) { |
| 3030 | EC_KEY_free(eckey); |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 3031 | goto fail; |
| 3032 | } |
| 3033 | |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 3034 | return (struct crypto_ec_key *) pkey; |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 3035 | fail: |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 3036 | crypto_ec_key_deinit((struct crypto_ec_key *) pkey); |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 3037 | return NULL; |
Sunil | 8cd6f4d | 2022-06-28 18:40:46 +0000 | [diff] [blame] | 3038 | #endif /* OpenSSL version >= 3.0 */ |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 3039 | } |
| 3040 | |
| 3041 | |
| 3042 | struct crypto_ec_key * crypto_ec_key_parse_pub(const u8 *der, size_t der_len) |
| 3043 | { |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 3044 | EVP_PKEY *pkey; |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 3045 | |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 3046 | pkey = d2i_PUBKEY(NULL, &der, der_len); |
| 3047 | if (!pkey) { |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 3048 | wpa_printf(MSG_INFO, "OpenSSL: d2i_PUBKEY() failed: %s", |
| 3049 | ERR_error_string(ERR_get_error(), NULL)); |
| 3050 | goto fail; |
| 3051 | } |
| 3052 | |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 3053 | /* Ensure this is an EC key */ |
Sunil | 8cd6f4d | 2022-06-28 18:40:46 +0000 | [diff] [blame] | 3054 | #if OPENSSL_VERSION_NUMBER >= 0x30000000L |
| 3055 | if (!EVP_PKEY_is_a(pkey, "EC")) |
| 3056 | goto fail; |
| 3057 | #else /* OpenSSL version >= 3.0 */ |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 3058 | if (!EVP_PKEY_get0_EC_KEY(pkey)) |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 3059 | goto fail; |
Sunil | 8cd6f4d | 2022-06-28 18:40:46 +0000 | [diff] [blame] | 3060 | #endif /* OpenSSL version >= 3.0 */ |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 3061 | return (struct crypto_ec_key *) pkey; |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 3062 | fail: |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 3063 | crypto_ec_key_deinit((struct crypto_ec_key *) pkey); |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 3064 | return NULL; |
| 3065 | } |
| 3066 | |
| 3067 | |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 3068 | struct crypto_ec_key * crypto_ec_key_set_pub(int group, const u8 *buf_x, |
| 3069 | const u8 *buf_y, size_t len) |
| 3070 | { |
Sunil | 8cd6f4d | 2022-06-28 18:40:46 +0000 | [diff] [blame] | 3071 | #if OPENSSL_VERSION_NUMBER >= 0x30000000L |
| 3072 | const char *group_name; |
| 3073 | OSSL_PARAM params[3]; |
| 3074 | u8 *pub; |
| 3075 | EVP_PKEY_CTX *ctx; |
| 3076 | EVP_PKEY *pkey = NULL; |
| 3077 | |
| 3078 | group_name = crypto_ec_group_2_name(group); |
| 3079 | if (!group_name) |
| 3080 | return NULL; |
| 3081 | |
| 3082 | pub = os_malloc(1 + len * 2); |
| 3083 | if (!pub) |
| 3084 | return NULL; |
| 3085 | pub[0] = 0x04; /* uncompressed */ |
| 3086 | os_memcpy(pub + 1, buf_x, len); |
| 3087 | os_memcpy(pub + 1 + len, buf_y, len); |
| 3088 | |
| 3089 | params[0] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME, |
| 3090 | (char *) group_name, 0); |
| 3091 | params[1] = OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_PUB_KEY, |
| 3092 | pub, 1 + len * 2); |
| 3093 | params[2] = OSSL_PARAM_construct_end(); |
| 3094 | |
| 3095 | ctx = EVP_PKEY_CTX_new_from_name(NULL, "EC", NULL); |
| 3096 | if (!ctx) { |
| 3097 | os_free(pub); |
| 3098 | return NULL; |
| 3099 | } |
| 3100 | if (EVP_PKEY_fromdata_init(ctx) <= 0 || |
| 3101 | EVP_PKEY_fromdata(ctx, &pkey, EVP_PKEY_PUBLIC_KEY, params) <= 0) { |
| 3102 | os_free(pub); |
| 3103 | EVP_PKEY_CTX_free(ctx); |
| 3104 | return NULL; |
| 3105 | } |
| 3106 | |
| 3107 | os_free(pub); |
| 3108 | EVP_PKEY_CTX_free(ctx); |
| 3109 | |
| 3110 | return (struct crypto_ec_key *) pkey; |
| 3111 | #else /* OpenSSL version >= 3.0 */ |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 3112 | EC_KEY *eckey = NULL; |
| 3113 | EVP_PKEY *pkey = NULL; |
| 3114 | EC_GROUP *ec_group = NULL; |
| 3115 | BN_CTX *ctx; |
| 3116 | EC_POINT *point = NULL; |
| 3117 | BIGNUM *x = NULL, *y = NULL; |
| 3118 | int nid; |
| 3119 | |
| 3120 | if (!buf_x || !buf_y) |
| 3121 | return NULL; |
| 3122 | |
| 3123 | nid = crypto_ec_group_2_nid(group); |
| 3124 | if (nid < 0) { |
| 3125 | wpa_printf(MSG_ERROR, "OpenSSL: Unsupported group %d", group); |
| 3126 | return NULL; |
| 3127 | } |
| 3128 | |
| 3129 | ctx = BN_CTX_new(); |
| 3130 | if (!ctx) |
| 3131 | goto fail; |
| 3132 | |
| 3133 | ec_group = EC_GROUP_new_by_curve_name(nid); |
| 3134 | if (!ec_group) |
| 3135 | goto fail; |
| 3136 | |
| 3137 | x = BN_bin2bn(buf_x, len, NULL); |
| 3138 | y = BN_bin2bn(buf_y, len, NULL); |
| 3139 | point = EC_POINT_new(ec_group); |
| 3140 | if (!x || !y || !point) |
| 3141 | goto fail; |
| 3142 | |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 3143 | if (!EC_POINT_set_affine_coordinates(ec_group, point, x, y, ctx)) { |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 3144 | wpa_printf(MSG_ERROR, |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 3145 | "OpenSSL: EC_POINT_set_affine_coordinates failed: %s", |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 3146 | ERR_error_string(ERR_get_error(), NULL)); |
| 3147 | goto fail; |
| 3148 | } |
| 3149 | |
| 3150 | if (!EC_POINT_is_on_curve(ec_group, point, ctx) || |
| 3151 | EC_POINT_is_at_infinity(ec_group, point)) { |
| 3152 | wpa_printf(MSG_ERROR, "OpenSSL: Invalid point"); |
| 3153 | goto fail; |
| 3154 | } |
| 3155 | |
| 3156 | eckey = EC_KEY_new(); |
| 3157 | if (!eckey || |
| 3158 | EC_KEY_set_group(eckey, ec_group) != 1 || |
| 3159 | EC_KEY_set_public_key(eckey, point) != 1) { |
| 3160 | wpa_printf(MSG_ERROR, |
| 3161 | "OpenSSL: Failed to set EC_KEY: %s", |
| 3162 | ERR_error_string(ERR_get_error(), NULL)); |
| 3163 | goto fail; |
| 3164 | } |
| 3165 | EC_KEY_set_asn1_flag(eckey, OPENSSL_EC_NAMED_CURVE); |
| 3166 | |
| 3167 | pkey = EVP_PKEY_new(); |
| 3168 | if (!pkey || EVP_PKEY_assign_EC_KEY(pkey, eckey) != 1) { |
| 3169 | wpa_printf(MSG_ERROR, "OpenSSL: Could not create EVP_PKEY"); |
| 3170 | goto fail; |
| 3171 | } |
| 3172 | |
| 3173 | out: |
| 3174 | EC_GROUP_free(ec_group); |
| 3175 | BN_free(x); |
| 3176 | BN_free(y); |
| 3177 | EC_POINT_free(point); |
| 3178 | BN_CTX_free(ctx); |
| 3179 | return (struct crypto_ec_key *) pkey; |
| 3180 | |
| 3181 | fail: |
| 3182 | EC_KEY_free(eckey); |
| 3183 | EVP_PKEY_free(pkey); |
| 3184 | pkey = NULL; |
| 3185 | goto out; |
Sunil | 8cd6f4d | 2022-06-28 18:40:46 +0000 | [diff] [blame] | 3186 | #endif /* OpenSSL version >= 3.0 */ |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 3187 | } |
| 3188 | |
| 3189 | |
| 3190 | struct crypto_ec_key * |
| 3191 | crypto_ec_key_set_pub_point(struct crypto_ec *ec, |
| 3192 | const struct crypto_ec_point *pub) |
| 3193 | { |
Sunil | 8cd6f4d | 2022-06-28 18:40:46 +0000 | [diff] [blame] | 3194 | #if OPENSSL_VERSION_NUMBER >= 0x30000000L |
| 3195 | int len = BN_num_bytes(ec->prime); |
| 3196 | struct crypto_ec_key *key; |
| 3197 | u8 *buf; |
| 3198 | |
| 3199 | buf = os_malloc(2 * len); |
| 3200 | if (!buf) |
| 3201 | return NULL; |
| 3202 | if (crypto_ec_point_to_bin(ec, pub, buf, buf + len) < 0) { |
| 3203 | os_free(buf); |
| 3204 | return NULL; |
| 3205 | } |
| 3206 | |
| 3207 | key = crypto_ec_key_set_pub(ec->iana_group, buf, buf + len, len); |
| 3208 | os_free(buf); |
| 3209 | |
| 3210 | return key; |
| 3211 | #else /* OpenSSL version >= 3.0 */ |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 3212 | EC_KEY *eckey; |
| 3213 | EVP_PKEY *pkey = NULL; |
| 3214 | |
Sunil | 8cd6f4d | 2022-06-28 18:40:46 +0000 | [diff] [blame] | 3215 | wpa_printf(MSG_INFO, "JKM:%s", __func__); |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 3216 | eckey = EC_KEY_new(); |
| 3217 | if (!eckey || |
| 3218 | EC_KEY_set_group(eckey, ec->group) != 1 || |
| 3219 | EC_KEY_set_public_key(eckey, (const EC_POINT *) pub) != 1) { |
| 3220 | wpa_printf(MSG_ERROR, |
| 3221 | "OpenSSL: Failed to set EC_KEY: %s", |
| 3222 | ERR_error_string(ERR_get_error(), NULL)); |
| 3223 | goto fail; |
| 3224 | } |
| 3225 | EC_KEY_set_asn1_flag(eckey, OPENSSL_EC_NAMED_CURVE); |
| 3226 | |
| 3227 | pkey = EVP_PKEY_new(); |
| 3228 | if (!pkey || EVP_PKEY_assign_EC_KEY(pkey, eckey) != 1) { |
| 3229 | wpa_printf(MSG_ERROR, "OpenSSL: Could not create EVP_PKEY"); |
| 3230 | goto fail; |
| 3231 | } |
| 3232 | |
| 3233 | out: |
| 3234 | return (struct crypto_ec_key *) pkey; |
| 3235 | |
| 3236 | fail: |
| 3237 | EVP_PKEY_free(pkey); |
| 3238 | EC_KEY_free(eckey); |
| 3239 | pkey = NULL; |
| 3240 | goto out; |
Sunil | 8cd6f4d | 2022-06-28 18:40:46 +0000 | [diff] [blame] | 3241 | #endif /* OpenSSL version >= 3.0 */ |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 3242 | } |
| 3243 | |
| 3244 | |
| 3245 | struct crypto_ec_key * crypto_ec_key_gen(int group) |
| 3246 | { |
Sunil | 8cd6f4d | 2022-06-28 18:40:46 +0000 | [diff] [blame] | 3247 | #if OPENSSL_VERSION_NUMBER >= 0x30000000L |
| 3248 | EVP_PKEY_CTX *ctx; |
| 3249 | OSSL_PARAM params[2]; |
| 3250 | const char *group_name; |
| 3251 | EVP_PKEY *pkey = NULL; |
| 3252 | |
| 3253 | group_name = crypto_ec_group_2_name(group); |
| 3254 | if (!group_name) |
| 3255 | return NULL; |
| 3256 | |
| 3257 | params[0] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME, |
| 3258 | (char *) group_name, 0); |
| 3259 | params[1] = OSSL_PARAM_construct_end(); |
| 3260 | |
| 3261 | ctx = EVP_PKEY_CTX_new_from_name(NULL, "EC", NULL); |
| 3262 | if (!ctx || |
| 3263 | EVP_PKEY_keygen_init(ctx) != 1 || |
| 3264 | EVP_PKEY_CTX_set_params(ctx, params) != 1 || |
| 3265 | EVP_PKEY_generate(ctx, &pkey) != 1) { |
| 3266 | wpa_printf(MSG_INFO, |
| 3267 | "OpenSSL: failed to generate EC keypair: %s", |
| 3268 | ERR_error_string(ERR_get_error(), NULL)); |
| 3269 | pkey = NULL; |
| 3270 | } |
| 3271 | |
| 3272 | EVP_PKEY_CTX_free(ctx); |
| 3273 | |
| 3274 | return (struct crypto_ec_key *) pkey; |
| 3275 | #else /* OpenSSL version >= 3.0 */ |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 3276 | EVP_PKEY_CTX *kctx = NULL; |
| 3277 | EC_KEY *ec_params = NULL, *eckey; |
| 3278 | EVP_PKEY *params = NULL, *key = NULL; |
| 3279 | int nid; |
| 3280 | |
| 3281 | nid = crypto_ec_group_2_nid(group); |
| 3282 | if (nid < 0) { |
| 3283 | wpa_printf(MSG_ERROR, "OpenSSL: Unsupported group %d", group); |
| 3284 | return NULL; |
| 3285 | } |
| 3286 | |
| 3287 | ec_params = EC_KEY_new_by_curve_name(nid); |
| 3288 | if (!ec_params) { |
| 3289 | wpa_printf(MSG_ERROR, |
| 3290 | "OpenSSL: Failed to generate EC_KEY parameters"); |
| 3291 | goto fail; |
| 3292 | } |
| 3293 | EC_KEY_set_asn1_flag(ec_params, OPENSSL_EC_NAMED_CURVE); |
| 3294 | params = EVP_PKEY_new(); |
| 3295 | if (!params || EVP_PKEY_set1_EC_KEY(params, ec_params) != 1) { |
| 3296 | wpa_printf(MSG_ERROR, |
| 3297 | "OpenSSL: Failed to generate EVP_PKEY parameters"); |
| 3298 | goto fail; |
| 3299 | } |
| 3300 | |
| 3301 | kctx = EVP_PKEY_CTX_new(params, NULL); |
| 3302 | if (!kctx || |
| 3303 | EVP_PKEY_keygen_init(kctx) != 1 || |
| 3304 | EVP_PKEY_keygen(kctx, &key) != 1) { |
| 3305 | wpa_printf(MSG_ERROR, "OpenSSL: Failed to generate EC key"); |
| 3306 | key = NULL; |
| 3307 | goto fail; |
| 3308 | } |
| 3309 | |
| 3310 | eckey = EVP_PKEY_get1_EC_KEY(key); |
| 3311 | if (!eckey) { |
| 3312 | key = NULL; |
| 3313 | goto fail; |
| 3314 | } |
| 3315 | EC_KEY_set_conv_form(eckey, POINT_CONVERSION_COMPRESSED); |
| 3316 | EC_KEY_free(eckey); |
| 3317 | |
| 3318 | fail: |
| 3319 | EC_KEY_free(ec_params); |
| 3320 | EVP_PKEY_free(params); |
| 3321 | EVP_PKEY_CTX_free(kctx); |
| 3322 | return (struct crypto_ec_key *) key; |
Sunil | 8cd6f4d | 2022-06-28 18:40:46 +0000 | [diff] [blame] | 3323 | #endif /* OpenSSL version >= 3.0 */ |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 3324 | } |
| 3325 | |
| 3326 | |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 3327 | void crypto_ec_key_deinit(struct crypto_ec_key *key) |
| 3328 | { |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 3329 | EVP_PKEY_free((EVP_PKEY *) key); |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 3330 | } |
| 3331 | |
| 3332 | |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 3333 | #ifdef OPENSSL_IS_BORINGSSL |
| 3334 | |
| 3335 | /* BoringSSL version of i2d_PUBKEY() always outputs public EC key using |
| 3336 | * uncompressed form so define a custom function to export EC pubkey using |
| 3337 | * the compressed format that is explicitly required for some protocols. */ |
| 3338 | |
| 3339 | #include <openssl/asn1.h> |
| 3340 | #include <openssl/asn1t.h> |
| 3341 | |
| 3342 | typedef struct { |
| 3343 | /* AlgorithmIdentifier ecPublicKey with optional parameters present |
| 3344 | * as an OID identifying the curve */ |
| 3345 | X509_ALGOR *alg; |
| 3346 | /* Compressed format public key per ANSI X9.63 */ |
| 3347 | ASN1_BIT_STRING *pub_key; |
| 3348 | } EC_COMP_PUBKEY; |
| 3349 | |
| 3350 | ASN1_SEQUENCE(EC_COMP_PUBKEY) = { |
| 3351 | ASN1_SIMPLE(EC_COMP_PUBKEY, alg, X509_ALGOR), |
| 3352 | ASN1_SIMPLE(EC_COMP_PUBKEY, pub_key, ASN1_BIT_STRING) |
| 3353 | } ASN1_SEQUENCE_END(EC_COMP_PUBKEY); |
| 3354 | |
| 3355 | IMPLEMENT_ASN1_FUNCTIONS(EC_COMP_PUBKEY); |
| 3356 | |
| 3357 | #endif /* OPENSSL_IS_BORINGSSL */ |
| 3358 | |
| 3359 | |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 3360 | struct wpabuf * crypto_ec_key_get_subject_public_key(struct crypto_ec_key *key) |
| 3361 | { |
Sunil | 8cd6f4d | 2022-06-28 18:40:46 +0000 | [diff] [blame] | 3362 | EVP_PKEY *pkey = (EVP_PKEY *) key; |
| 3363 | #if OPENSSL_VERSION_NUMBER >= 0x30000000L |
| 3364 | OSSL_ENCODER_CTX *ctx; |
| 3365 | int selection; |
| 3366 | unsigned char *pdata = NULL; |
| 3367 | size_t pdata_len = 0; |
| 3368 | EVP_PKEY *copy = NULL; |
| 3369 | struct wpabuf *buf = NULL; |
| 3370 | |
| 3371 | if (EVP_PKEY_get_ec_point_conv_form(pkey) != |
| 3372 | POINT_CONVERSION_COMPRESSED) { |
| 3373 | copy = EVP_PKEY_dup(pkey); |
| 3374 | if (!copy) |
| 3375 | return NULL; |
| 3376 | if (EVP_PKEY_set_utf8_string_param( |
| 3377 | copy, OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT, |
| 3378 | OSSL_PKEY_EC_POINT_CONVERSION_FORMAT_COMPRESSED) != |
| 3379 | 1) { |
| 3380 | wpa_printf(MSG_INFO, |
| 3381 | "OpenSSL: Failed to set compressed format"); |
| 3382 | EVP_PKEY_free(copy); |
| 3383 | return NULL; |
| 3384 | } |
| 3385 | pkey = copy; |
| 3386 | } |
| 3387 | |
| 3388 | selection = OSSL_KEYMGMT_SELECT_ALL_PARAMETERS | |
| 3389 | OSSL_KEYMGMT_SELECT_PUBLIC_KEY; |
| 3390 | |
| 3391 | ctx = OSSL_ENCODER_CTX_new_for_pkey(pkey, selection, "DER", |
| 3392 | "SubjectPublicKeyInfo", |
| 3393 | NULL); |
| 3394 | if (!ctx || OSSL_ENCODER_to_data(ctx, &pdata, &pdata_len) != 1) { |
| 3395 | wpa_printf(MSG_INFO, |
| 3396 | "OpenSSL: Failed to encode SubjectPublicKeyInfo: %s", |
| 3397 | ERR_error_string(ERR_get_error(), NULL)); |
| 3398 | pdata = NULL; |
| 3399 | } |
| 3400 | OSSL_ENCODER_CTX_free(ctx); |
| 3401 | if (pdata) { |
| 3402 | buf = wpabuf_alloc_copy(pdata, pdata_len); |
| 3403 | OPENSSL_free(pdata); |
| 3404 | } |
| 3405 | |
| 3406 | EVP_PKEY_free(copy); |
| 3407 | |
| 3408 | return buf; |
| 3409 | #else /* OpenSSL version >= 3.0 */ |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 3410 | #ifdef OPENSSL_IS_BORINGSSL |
| 3411 | unsigned char *der = NULL; |
| 3412 | int der_len; |
| 3413 | const EC_KEY *eckey; |
| 3414 | struct wpabuf *ret = NULL; |
| 3415 | size_t len; |
| 3416 | const EC_GROUP *group; |
| 3417 | const EC_POINT *point; |
| 3418 | BN_CTX *ctx; |
| 3419 | EC_COMP_PUBKEY *pubkey = NULL; |
| 3420 | int nid; |
| 3421 | |
| 3422 | ctx = BN_CTX_new(); |
Sunil | 8cd6f4d | 2022-06-28 18:40:46 +0000 | [diff] [blame] | 3423 | eckey = EVP_PKEY_get0_EC_KEY(pkey); |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 3424 | if (!ctx || !eckey) |
| 3425 | goto fail; |
| 3426 | |
| 3427 | group = EC_KEY_get0_group(eckey); |
| 3428 | point = EC_KEY_get0_public_key(eckey); |
| 3429 | if (!group || !point) |
| 3430 | goto fail; |
| 3431 | nid = EC_GROUP_get_curve_name(group); |
| 3432 | |
| 3433 | pubkey = EC_COMP_PUBKEY_new(); |
| 3434 | if (!pubkey || |
| 3435 | X509_ALGOR_set0(pubkey->alg, OBJ_nid2obj(EVP_PKEY_EC), |
| 3436 | V_ASN1_OBJECT, (void *) OBJ_nid2obj(nid)) != 1) |
| 3437 | goto fail; |
| 3438 | |
| 3439 | len = EC_POINT_point2oct(group, point, POINT_CONVERSION_COMPRESSED, |
| 3440 | NULL, 0, ctx); |
| 3441 | if (len == 0) |
| 3442 | goto fail; |
| 3443 | |
| 3444 | der = OPENSSL_malloc(len); |
| 3445 | if (!der) |
| 3446 | goto fail; |
| 3447 | len = EC_POINT_point2oct(group, point, POINT_CONVERSION_COMPRESSED, |
| 3448 | der, len, ctx); |
| 3449 | |
| 3450 | OPENSSL_free(pubkey->pub_key->data); |
| 3451 | pubkey->pub_key->data = der; |
| 3452 | der = NULL; |
| 3453 | pubkey->pub_key->length = len; |
| 3454 | /* No unused bits */ |
| 3455 | pubkey->pub_key->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07); |
| 3456 | pubkey->pub_key->flags |= ASN1_STRING_FLAG_BITS_LEFT; |
| 3457 | |
| 3458 | der_len = i2d_EC_COMP_PUBKEY(pubkey, &der); |
| 3459 | if (der_len <= 0) { |
| 3460 | wpa_printf(MSG_ERROR, |
| 3461 | "BoringSSL: Failed to build DER encoded public key"); |
| 3462 | goto fail; |
| 3463 | } |
| 3464 | |
| 3465 | ret = wpabuf_alloc_copy(der, der_len); |
| 3466 | fail: |
| 3467 | EC_COMP_PUBKEY_free(pubkey); |
| 3468 | OPENSSL_free(der); |
| 3469 | BN_CTX_free(ctx); |
| 3470 | return ret; |
| 3471 | #else /* OPENSSL_IS_BORINGSSL */ |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 3472 | unsigned char *der = NULL; |
| 3473 | int der_len; |
| 3474 | struct wpabuf *buf; |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 3475 | EC_KEY *eckey; |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 3476 | |
Sunil | 8cd6f4d | 2022-06-28 18:40:46 +0000 | [diff] [blame] | 3477 | eckey = EVP_PKEY_get1_EC_KEY(pkey); |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 3478 | if (!eckey) |
| 3479 | return NULL; |
| 3480 | |
| 3481 | /* For now, all users expect COMPRESSED form */ |
| 3482 | EC_KEY_set_conv_form(eckey, POINT_CONVERSION_COMPRESSED); |
| 3483 | |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 3484 | der_len = i2d_PUBKEY((EVP_PKEY *) key, &der); |
| 3485 | EC_KEY_free(eckey); |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 3486 | if (der_len <= 0) { |
| 3487 | wpa_printf(MSG_INFO, "OpenSSL: i2d_PUBKEY() failed: %s", |
| 3488 | ERR_error_string(ERR_get_error(), NULL)); |
| 3489 | return NULL; |
| 3490 | } |
| 3491 | |
| 3492 | buf = wpabuf_alloc_copy(der, der_len); |
| 3493 | OPENSSL_free(der); |
| 3494 | return buf; |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 3495 | #endif /* OPENSSL_IS_BORINGSSL */ |
Sunil | 8cd6f4d | 2022-06-28 18:40:46 +0000 | [diff] [blame] | 3496 | #endif /* OpenSSL version >= 3.0 */ |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 3497 | } |
| 3498 | |
| 3499 | |
| 3500 | struct wpabuf * crypto_ec_key_get_ecprivate_key(struct crypto_ec_key *key, |
| 3501 | bool include_pub) |
| 3502 | { |
Sunil | 8cd6f4d | 2022-06-28 18:40:46 +0000 | [diff] [blame] | 3503 | EVP_PKEY *pkey = (EVP_PKEY *) key; |
| 3504 | #if OPENSSL_VERSION_NUMBER >= 0x30000000L |
| 3505 | OSSL_ENCODER_CTX *ctx; |
| 3506 | int selection; |
| 3507 | unsigned char *pdata = NULL; |
| 3508 | size_t pdata_len = 0; |
| 3509 | struct wpabuf *buf; |
| 3510 | EVP_PKEY *copy = NULL; |
| 3511 | |
| 3512 | selection = OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS | |
| 3513 | OSSL_KEYMGMT_SELECT_PRIVATE_KEY; |
| 3514 | if (include_pub) { |
| 3515 | selection |= OSSL_KEYMGMT_SELECT_PUBLIC_KEY; |
| 3516 | } else { |
| 3517 | /* Not including OSSL_KEYMGMT_SELECT_PUBLIC_KEY does not seem |
| 3518 | * to really be sufficient, so clone the key and explicitly |
| 3519 | * mark it not to include the public key. */ |
| 3520 | copy = EVP_PKEY_dup(pkey); |
| 3521 | if (!copy) |
| 3522 | return NULL; |
| 3523 | EVP_PKEY_set_int_param(copy, OSSL_PKEY_PARAM_EC_INCLUDE_PUBLIC, |
| 3524 | 0); |
| 3525 | pkey = copy; |
| 3526 | } |
| 3527 | |
| 3528 | ctx = OSSL_ENCODER_CTX_new_for_pkey(pkey, selection, "DER", |
| 3529 | "type-specific", NULL); |
| 3530 | if (!ctx || OSSL_ENCODER_to_data(ctx, &pdata, &pdata_len) != 1) { |
| 3531 | OSSL_ENCODER_CTX_free(ctx); |
| 3532 | EVP_PKEY_free(copy); |
| 3533 | return NULL; |
| 3534 | } |
| 3535 | OSSL_ENCODER_CTX_free(ctx); |
| 3536 | buf = wpabuf_alloc_copy(pdata, pdata_len); |
| 3537 | OPENSSL_free(pdata); |
| 3538 | EVP_PKEY_free(copy); |
| 3539 | return buf; |
| 3540 | #else /* OpenSSL version >= 3.0 */ |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 3541 | EC_KEY *eckey; |
| 3542 | unsigned char *der = NULL; |
| 3543 | int der_len; |
| 3544 | struct wpabuf *buf; |
| 3545 | unsigned int key_flags; |
| 3546 | |
Sunil | 8cd6f4d | 2022-06-28 18:40:46 +0000 | [diff] [blame] | 3547 | eckey = EVP_PKEY_get1_EC_KEY(pkey); |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 3548 | if (!eckey) |
| 3549 | return NULL; |
| 3550 | |
| 3551 | key_flags = EC_KEY_get_enc_flags(eckey); |
| 3552 | if (include_pub) |
| 3553 | key_flags &= ~EC_PKEY_NO_PUBKEY; |
| 3554 | else |
| 3555 | key_flags |= EC_PKEY_NO_PUBKEY; |
| 3556 | EC_KEY_set_enc_flags(eckey, key_flags); |
| 3557 | |
| 3558 | EC_KEY_set_conv_form(eckey, POINT_CONVERSION_UNCOMPRESSED); |
| 3559 | |
| 3560 | der_len = i2d_ECPrivateKey(eckey, &der); |
| 3561 | EC_KEY_free(eckey); |
| 3562 | if (der_len <= 0) |
| 3563 | return NULL; |
| 3564 | buf = wpabuf_alloc_copy(der, der_len); |
| 3565 | OPENSSL_free(der); |
| 3566 | |
| 3567 | return buf; |
Sunil | 8cd6f4d | 2022-06-28 18:40:46 +0000 | [diff] [blame] | 3568 | #endif /* OpenSSL version >= 3.0 */ |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 3569 | } |
| 3570 | |
| 3571 | |
| 3572 | struct wpabuf * crypto_ec_key_get_pubkey_point(struct crypto_ec_key *key, |
| 3573 | int prefix) |
| 3574 | { |
Sunil | 8cd6f4d | 2022-06-28 18:40:46 +0000 | [diff] [blame] | 3575 | EVP_PKEY *pkey = (EVP_PKEY *) key; |
| 3576 | #if OPENSSL_VERSION_NUMBER >= 0x30000000L |
| 3577 | struct wpabuf *buf; |
| 3578 | unsigned char *pos; |
| 3579 | size_t pub_len = OSSL_PARAM_UNMODIFIED; |
| 3580 | |
| 3581 | buf = NULL; |
| 3582 | if (!EVP_PKEY_is_a(pkey, "EC") || |
| 3583 | EVP_PKEY_get_octet_string_param(pkey, |
| 3584 | OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, |
| 3585 | NULL, 0, &pub_len) < 0 || |
| 3586 | pub_len == OSSL_PARAM_UNMODIFIED || |
| 3587 | !(buf = wpabuf_alloc(pub_len)) || |
| 3588 | EVP_PKEY_get_octet_string_param(pkey, |
| 3589 | OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, |
| 3590 | wpabuf_put(buf, pub_len), |
| 3591 | pub_len, NULL) != 1 || |
| 3592 | wpabuf_head_u8(buf)[0] != 0x04) { |
| 3593 | wpa_printf(MSG_INFO, |
| 3594 | "OpenSSL: Failed to get encoded public key: %s", |
| 3595 | ERR_error_string(ERR_get_error(), NULL)); |
| 3596 | wpabuf_free(buf); |
| 3597 | return NULL; |
| 3598 | } |
| 3599 | |
| 3600 | if (!prefix) { |
| 3601 | /* Remove 0x04 prefix if requested */ |
| 3602 | pos = wpabuf_mhead(buf); |
| 3603 | os_memmove(pos, pos + 1, pub_len - 1); |
| 3604 | buf->used--; |
| 3605 | } |
| 3606 | |
| 3607 | return buf; |
| 3608 | #else /* OpenSSL version >= 3.0 */ |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 3609 | int len, res; |
| 3610 | EC_KEY *eckey; |
| 3611 | struct wpabuf *buf; |
| 3612 | unsigned char *pos; |
| 3613 | |
Sunil | 8cd6f4d | 2022-06-28 18:40:46 +0000 | [diff] [blame] | 3614 | eckey = EVP_PKEY_get1_EC_KEY(pkey); |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 3615 | if (!eckey) |
| 3616 | return NULL; |
| 3617 | EC_KEY_set_conv_form(eckey, POINT_CONVERSION_UNCOMPRESSED); |
| 3618 | len = i2o_ECPublicKey(eckey, NULL); |
| 3619 | if (len <= 0) { |
| 3620 | wpa_printf(MSG_ERROR, |
| 3621 | "OpenSSL: Failed to determine public key encoding length"); |
| 3622 | EC_KEY_free(eckey); |
| 3623 | return NULL; |
| 3624 | } |
| 3625 | |
| 3626 | buf = wpabuf_alloc(len); |
| 3627 | if (!buf) { |
| 3628 | EC_KEY_free(eckey); |
| 3629 | return NULL; |
| 3630 | } |
| 3631 | |
| 3632 | pos = wpabuf_put(buf, len); |
| 3633 | res = i2o_ECPublicKey(eckey, &pos); |
| 3634 | EC_KEY_free(eckey); |
| 3635 | if (res != len) { |
| 3636 | wpa_printf(MSG_ERROR, |
| 3637 | "OpenSSL: Failed to encode public key (res=%d/%d)", |
| 3638 | res, len); |
| 3639 | wpabuf_free(buf); |
| 3640 | return NULL; |
| 3641 | } |
| 3642 | |
| 3643 | if (!prefix) { |
| 3644 | /* Remove 0x04 prefix if requested */ |
| 3645 | pos = wpabuf_mhead(buf); |
| 3646 | os_memmove(pos, pos + 1, len - 1); |
| 3647 | buf->used--; |
| 3648 | } |
| 3649 | |
| 3650 | return buf; |
Sunil | 8cd6f4d | 2022-06-28 18:40:46 +0000 | [diff] [blame] | 3651 | #endif /* OpenSSL version >= 3.0 */ |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 3652 | } |
| 3653 | |
| 3654 | |
Sunil | 8cd6f4d | 2022-06-28 18:40:46 +0000 | [diff] [blame] | 3655 | struct crypto_ec_point * |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 3656 | crypto_ec_key_get_public_key(struct crypto_ec_key *key) |
| 3657 | { |
Sunil | 8cd6f4d | 2022-06-28 18:40:46 +0000 | [diff] [blame] | 3658 | EVP_PKEY *pkey = (EVP_PKEY *) key; |
| 3659 | #if OPENSSL_VERSION_NUMBER >= 0x30000000L |
| 3660 | char group[64]; |
| 3661 | unsigned char pub[256]; |
| 3662 | size_t len; |
| 3663 | EC_POINT *point = NULL; |
| 3664 | EC_GROUP *grp; |
| 3665 | int res = 0; |
| 3666 | OSSL_PARAM params[2]; |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 3667 | |
Sunil | 8cd6f4d | 2022-06-28 18:40:46 +0000 | [diff] [blame] | 3668 | if (!EVP_PKEY_is_a(pkey, "EC") || |
| 3669 | EVP_PKEY_get_utf8_string_param(pkey, OSSL_PKEY_PARAM_GROUP_NAME, |
| 3670 | group, sizeof(group), &len) != 1 || |
| 3671 | EVP_PKEY_get_octet_string_param(pkey, OSSL_PKEY_PARAM_PUB_KEY, |
| 3672 | pub, sizeof(pub), &len) != 1) |
| 3673 | return NULL; |
| 3674 | |
| 3675 | params[0] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME, |
| 3676 | group, 0); |
| 3677 | params[1] = OSSL_PARAM_construct_end(); |
| 3678 | grp = EC_GROUP_new_from_params(params, NULL, NULL); |
| 3679 | if (!grp) |
| 3680 | goto fail; |
| 3681 | point = EC_POINT_new(grp); |
| 3682 | if (!point) |
| 3683 | goto fail; |
| 3684 | res = EC_POINT_oct2point(grp, point, pub, len, NULL); |
| 3685 | |
| 3686 | fail: |
| 3687 | if (res != 1) { |
| 3688 | EC_POINT_free(point); |
| 3689 | point = NULL; |
| 3690 | } |
| 3691 | |
| 3692 | EC_GROUP_free(grp); |
| 3693 | |
| 3694 | return (struct crypto_ec_point *) point; |
| 3695 | #else /* OpenSSL version >= 3.0 */ |
| 3696 | const EC_KEY *eckey; |
| 3697 | const EC_POINT *point; |
| 3698 | const EC_GROUP *group; |
| 3699 | |
| 3700 | eckey = EVP_PKEY_get0_EC_KEY(pkey); |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 3701 | if (!eckey) |
| 3702 | return NULL; |
Sunil | 8cd6f4d | 2022-06-28 18:40:46 +0000 | [diff] [blame] | 3703 | group = EC_KEY_get0_group(eckey); |
| 3704 | if (!group) |
| 3705 | return NULL; |
| 3706 | point = EC_KEY_get0_public_key(eckey); |
| 3707 | if (!point) |
| 3708 | return NULL; |
| 3709 | return (struct crypto_ec_point *) EC_POINT_dup(point, group); |
| 3710 | #endif /* OpenSSL version >= 3.0 */ |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 3711 | } |
| 3712 | |
| 3713 | |
Sunil | 8cd6f4d | 2022-06-28 18:40:46 +0000 | [diff] [blame] | 3714 | struct crypto_bignum * |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 3715 | crypto_ec_key_get_private_key(struct crypto_ec_key *key) |
| 3716 | { |
Sunil | 8cd6f4d | 2022-06-28 18:40:46 +0000 | [diff] [blame] | 3717 | EVP_PKEY *pkey = (EVP_PKEY *) key; |
| 3718 | #if OPENSSL_VERSION_NUMBER >= 0x30000000L |
| 3719 | BIGNUM *bn = NULL; |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 3720 | |
Sunil | 8cd6f4d | 2022-06-28 18:40:46 +0000 | [diff] [blame] | 3721 | if (!EVP_PKEY_is_a(pkey, "EC") || |
| 3722 | EVP_PKEY_get_bn_param(pkey, OSSL_PKEY_PARAM_PRIV_KEY, &bn) != 1) |
| 3723 | return NULL; |
| 3724 | return (struct crypto_bignum *) bn; |
| 3725 | #else /* OpenSSL version >= 3.0 */ |
| 3726 | const EC_KEY *eckey; |
| 3727 | const BIGNUM *bn; |
| 3728 | |
| 3729 | eckey = EVP_PKEY_get0_EC_KEY(pkey); |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 3730 | if (!eckey) |
| 3731 | return NULL; |
Sunil | 8cd6f4d | 2022-06-28 18:40:46 +0000 | [diff] [blame] | 3732 | bn = EC_KEY_get0_private_key(eckey); |
| 3733 | if (!bn) |
| 3734 | return NULL; |
| 3735 | return (struct crypto_bignum *) BN_dup(bn); |
| 3736 | #endif /* OpenSSL version >= 3.0 */ |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 3737 | } |
| 3738 | |
| 3739 | |
| 3740 | struct wpabuf * crypto_ec_key_sign(struct crypto_ec_key *key, const u8 *data, |
| 3741 | size_t len) |
| 3742 | { |
| 3743 | EVP_PKEY_CTX *pkctx; |
| 3744 | struct wpabuf *sig_der; |
| 3745 | size_t sig_len; |
| 3746 | |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 3747 | sig_len = EVP_PKEY_size((EVP_PKEY *) key); |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 3748 | sig_der = wpabuf_alloc(sig_len); |
| 3749 | if (!sig_der) |
| 3750 | return NULL; |
| 3751 | |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 3752 | pkctx = EVP_PKEY_CTX_new((EVP_PKEY *) key, NULL); |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 3753 | if (!pkctx || |
| 3754 | EVP_PKEY_sign_init(pkctx) <= 0 || |
| 3755 | EVP_PKEY_sign(pkctx, wpabuf_put(sig_der, 0), &sig_len, |
| 3756 | data, len) <= 0) { |
| 3757 | wpabuf_free(sig_der); |
| 3758 | sig_der = NULL; |
| 3759 | } else { |
| 3760 | wpabuf_put(sig_der, sig_len); |
| 3761 | } |
| 3762 | |
| 3763 | EVP_PKEY_CTX_free(pkctx); |
| 3764 | return sig_der; |
| 3765 | } |
| 3766 | |
| 3767 | |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 3768 | static int openssl_evp_pkey_ec_prime_len(struct crypto_ec_key *key) |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 3769 | { |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 3770 | #if OPENSSL_VERSION_NUMBER >= 0x30000000L |
| 3771 | char gname[50]; |
| 3772 | int nid; |
| 3773 | EC_GROUP *group; |
| 3774 | BIGNUM *prime = NULL; |
| 3775 | int prime_len = -1; |
| 3776 | |
| 3777 | if (EVP_PKEY_get_group_name((EVP_PKEY *) key, gname, sizeof(gname), |
| 3778 | NULL) != 1) |
| 3779 | return -1; |
| 3780 | nid = OBJ_txt2nid(gname); |
| 3781 | group = EC_GROUP_new_by_curve_name(nid); |
| 3782 | prime = BN_new(); |
| 3783 | if (!group || !prime) |
| 3784 | return -1; |
| 3785 | if (EC_GROUP_get_curve(group, prime, NULL, NULL, NULL) == 1) |
| 3786 | prime_len = BN_num_bytes(prime); |
| 3787 | EC_GROUP_free(group); |
| 3788 | BN_free(prime); |
| 3789 | return prime_len; |
| 3790 | #else |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 3791 | const EC_GROUP *group; |
| 3792 | const EC_KEY *eckey; |
| 3793 | BIGNUM *prime = NULL; |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 3794 | int prime_len = -1; |
| 3795 | |
| 3796 | eckey = EVP_PKEY_get0_EC_KEY((EVP_PKEY *) key); |
| 3797 | if (!eckey) |
| 3798 | goto fail; |
| 3799 | group = EC_KEY_get0_group(eckey); |
| 3800 | prime = BN_new(); |
| 3801 | if (!prime || !group || |
| 3802 | !EC_GROUP_get_curve(group, prime, NULL, NULL, NULL)) |
| 3803 | goto fail; |
| 3804 | prime_len = BN_num_bytes(prime); |
| 3805 | fail: |
| 3806 | BN_free(prime); |
| 3807 | return prime_len; |
| 3808 | #endif |
| 3809 | } |
| 3810 | |
| 3811 | |
| 3812 | struct wpabuf * crypto_ec_key_sign_r_s(struct crypto_ec_key *key, |
| 3813 | const u8 *data, size_t len) |
| 3814 | { |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 3815 | ECDSA_SIG *sig = NULL; |
| 3816 | const BIGNUM *r, *s; |
| 3817 | u8 *r_buf, *s_buf; |
| 3818 | struct wpabuf *buf; |
| 3819 | const unsigned char *p; |
| 3820 | int prime_len; |
| 3821 | |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 3822 | prime_len = openssl_evp_pkey_ec_prime_len(key); |
| 3823 | if (prime_len < 0) |
| 3824 | return NULL; |
| 3825 | |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 3826 | buf = crypto_ec_key_sign(key, data, len); |
| 3827 | if (!buf) |
| 3828 | return NULL; |
| 3829 | |
| 3830 | /* Extract (r,s) from Ecdsa-Sig-Value */ |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 3831 | |
| 3832 | p = wpabuf_head(buf); |
| 3833 | sig = d2i_ECDSA_SIG(NULL, &p, wpabuf_len(buf)); |
| 3834 | if (!sig) |
| 3835 | goto fail; |
| 3836 | ECDSA_SIG_get0(sig, &r, &s); |
| 3837 | |
| 3838 | /* Re-use wpabuf returned by crypto_ec_key_sign() */ |
| 3839 | buf->used = 0; |
| 3840 | r_buf = wpabuf_put(buf, prime_len); |
| 3841 | s_buf = wpabuf_put(buf, prime_len); |
| 3842 | if (crypto_bignum_to_bin((const struct crypto_bignum *) r, r_buf, |
| 3843 | prime_len, prime_len) < 0 || |
| 3844 | crypto_bignum_to_bin((const struct crypto_bignum *) s, s_buf, |
| 3845 | prime_len, prime_len) < 0) |
| 3846 | goto fail; |
| 3847 | |
| 3848 | out: |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 3849 | ECDSA_SIG_free(sig); |
| 3850 | return buf; |
| 3851 | fail: |
| 3852 | wpabuf_clear_free(buf); |
| 3853 | buf = NULL; |
| 3854 | goto out; |
| 3855 | } |
| 3856 | |
| 3857 | |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 3858 | int crypto_ec_key_verify_signature(struct crypto_ec_key *key, const u8 *data, |
| 3859 | size_t len, const u8 *sig, size_t sig_len) |
| 3860 | { |
| 3861 | EVP_PKEY_CTX *pkctx; |
| 3862 | int ret; |
| 3863 | |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 3864 | pkctx = EVP_PKEY_CTX_new((EVP_PKEY *) key, NULL); |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 3865 | if (!pkctx || EVP_PKEY_verify_init(pkctx) <= 0) { |
| 3866 | EVP_PKEY_CTX_free(pkctx); |
| 3867 | return -1; |
| 3868 | } |
| 3869 | |
| 3870 | ret = EVP_PKEY_verify(pkctx, sig, sig_len, data, len); |
| 3871 | EVP_PKEY_CTX_free(pkctx); |
| 3872 | if (ret == 1) |
| 3873 | return 1; /* signature ok */ |
| 3874 | if (ret == 0) |
| 3875 | return 0; /* incorrect signature */ |
| 3876 | return -1; |
| 3877 | } |
| 3878 | |
| 3879 | |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 3880 | int crypto_ec_key_verify_signature_r_s(struct crypto_ec_key *key, |
| 3881 | const u8 *data, size_t len, |
| 3882 | const u8 *r, size_t r_len, |
| 3883 | const u8 *s, size_t s_len) |
| 3884 | { |
| 3885 | ECDSA_SIG *sig; |
| 3886 | BIGNUM *r_bn, *s_bn; |
| 3887 | unsigned char *der = NULL; |
| 3888 | int der_len; |
| 3889 | int ret = -1; |
| 3890 | |
| 3891 | r_bn = BN_bin2bn(r, r_len, NULL); |
| 3892 | s_bn = BN_bin2bn(s, s_len, NULL); |
| 3893 | sig = ECDSA_SIG_new(); |
| 3894 | if (!r_bn || !s_bn || !sig || ECDSA_SIG_set0(sig, r_bn, s_bn) != 1) |
| 3895 | goto fail; |
| 3896 | r_bn = NULL; |
| 3897 | s_bn = NULL; |
| 3898 | |
| 3899 | der_len = i2d_ECDSA_SIG(sig, &der); |
| 3900 | if (der_len <= 0) { |
| 3901 | wpa_printf(MSG_DEBUG, |
| 3902 | "OpenSSL: Could not DER encode signature"); |
| 3903 | goto fail; |
| 3904 | } |
| 3905 | |
| 3906 | ret = crypto_ec_key_verify_signature(key, data, len, der, der_len); |
| 3907 | |
| 3908 | fail: |
| 3909 | OPENSSL_free(der); |
| 3910 | BN_free(r_bn); |
| 3911 | BN_free(s_bn); |
| 3912 | ECDSA_SIG_free(sig); |
| 3913 | return ret; |
| 3914 | } |
| 3915 | |
| 3916 | |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 3917 | int crypto_ec_key_group(struct crypto_ec_key *key) |
| 3918 | { |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 3919 | #if OPENSSL_VERSION_NUMBER >= 0x30000000L |
| 3920 | char gname[50]; |
| 3921 | int nid; |
| 3922 | |
| 3923 | if (EVP_PKEY_get_group_name((EVP_PKEY *) key, gname, sizeof(gname), |
| 3924 | NULL) != 1) |
| 3925 | return -1; |
| 3926 | nid = OBJ_txt2nid(gname); |
| 3927 | #else |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 3928 | const EC_KEY *eckey; |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 3929 | const EC_GROUP *group; |
| 3930 | int nid; |
| 3931 | |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 3932 | eckey = EVP_PKEY_get0_EC_KEY((EVP_PKEY *) key); |
| 3933 | if (!eckey) |
| 3934 | return -1; |
| 3935 | group = EC_KEY_get0_group(eckey); |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 3936 | if (!group) |
| 3937 | return -1; |
| 3938 | nid = EC_GROUP_get_curve_name(group); |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 3939 | #endif |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 3940 | switch (nid) { |
| 3941 | case NID_X9_62_prime256v1: |
| 3942 | return 19; |
| 3943 | case NID_secp384r1: |
| 3944 | return 20; |
| 3945 | case NID_secp521r1: |
| 3946 | return 21; |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 3947 | #ifdef NID_brainpoolP256r1 |
| 3948 | case NID_brainpoolP256r1: |
| 3949 | return 28; |
| 3950 | #endif /* NID_brainpoolP256r1 */ |
| 3951 | #ifdef NID_brainpoolP384r1 |
| 3952 | case NID_brainpoolP384r1: |
| 3953 | return 29; |
| 3954 | #endif /* NID_brainpoolP384r1 */ |
| 3955 | #ifdef NID_brainpoolP512r1 |
| 3956 | case NID_brainpoolP512r1: |
| 3957 | return 30; |
| 3958 | #endif /* NID_brainpoolP512r1 */ |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 3959 | } |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 3960 | wpa_printf(MSG_ERROR, "OpenSSL: Unsupported curve (nid=%d) in EC key", |
| 3961 | nid); |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 3962 | return -1; |
| 3963 | } |
| 3964 | |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 3965 | |
| 3966 | int crypto_ec_key_cmp(struct crypto_ec_key *key1, struct crypto_ec_key *key2) |
| 3967 | { |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 3968 | #if OPENSSL_VERSION_NUMBER >= 0x30000000L |
| 3969 | if (EVP_PKEY_eq((EVP_PKEY *) key1, (EVP_PKEY *) key2) != 1) |
| 3970 | return -1; |
| 3971 | #else |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 3972 | if (EVP_PKEY_cmp((EVP_PKEY *) key1, (EVP_PKEY *) key2) != 1) |
| 3973 | return -1; |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 3974 | #endif |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 3975 | return 0; |
| 3976 | } |
| 3977 | |
| 3978 | |
| 3979 | void crypto_ec_key_debug_print(const struct crypto_ec_key *key, |
| 3980 | const char *title) |
| 3981 | { |
| 3982 | BIO *out; |
| 3983 | size_t rlen; |
| 3984 | char *txt; |
| 3985 | int res; |
| 3986 | |
| 3987 | out = BIO_new(BIO_s_mem()); |
| 3988 | if (!out) |
| 3989 | return; |
| 3990 | |
| 3991 | EVP_PKEY_print_private(out, (EVP_PKEY *) key, 0, NULL); |
| 3992 | rlen = BIO_ctrl_pending(out); |
| 3993 | txt = os_malloc(rlen + 1); |
| 3994 | if (txt) { |
| 3995 | res = BIO_read(out, txt, rlen); |
| 3996 | if (res > 0) { |
| 3997 | txt[res] = '\0'; |
| 3998 | wpa_printf(MSG_DEBUG, "%s: %s", title, txt); |
| 3999 | } |
| 4000 | os_free(txt); |
| 4001 | } |
| 4002 | BIO_free(out); |
| 4003 | } |
| 4004 | |
| 4005 | |
| 4006 | struct wpabuf * crypto_pkcs7_get_certificates(const struct wpabuf *pkcs7) |
| 4007 | { |
| 4008 | #ifdef OPENSSL_IS_BORINGSSL |
| 4009 | CBS pkcs7_cbs; |
| 4010 | #else /* OPENSSL_IS_BORINGSSL */ |
| 4011 | PKCS7 *p7 = NULL; |
| 4012 | const unsigned char *p = wpabuf_head(pkcs7); |
| 4013 | #endif /* OPENSSL_IS_BORINGSSL */ |
| 4014 | STACK_OF(X509) *certs; |
| 4015 | int i, num; |
| 4016 | BIO *out = NULL; |
| 4017 | size_t rlen; |
| 4018 | struct wpabuf *pem = NULL; |
| 4019 | int res; |
| 4020 | |
| 4021 | #ifdef OPENSSL_IS_BORINGSSL |
| 4022 | certs = sk_X509_new_null(); |
| 4023 | if (!certs) |
| 4024 | goto fail; |
| 4025 | CBS_init(&pkcs7_cbs, wpabuf_head(pkcs7), wpabuf_len(pkcs7)); |
| 4026 | if (!PKCS7_get_certificates(certs, &pkcs7_cbs)) { |
| 4027 | wpa_printf(MSG_INFO, |
| 4028 | "OpenSSL: Could not parse PKCS#7 object: %s", |
| 4029 | ERR_error_string(ERR_get_error(), NULL)); |
| 4030 | goto fail; |
| 4031 | } |
| 4032 | #else /* OPENSSL_IS_BORINGSSL */ |
| 4033 | p7 = d2i_PKCS7(NULL, &p, wpabuf_len(pkcs7)); |
| 4034 | if (!p7) { |
| 4035 | wpa_printf(MSG_INFO, |
| 4036 | "OpenSSL: Could not parse PKCS#7 object: %s", |
| 4037 | ERR_error_string(ERR_get_error(), NULL)); |
| 4038 | goto fail; |
| 4039 | } |
| 4040 | |
| 4041 | switch (OBJ_obj2nid(p7->type)) { |
| 4042 | case NID_pkcs7_signed: |
| 4043 | certs = p7->d.sign->cert; |
| 4044 | break; |
| 4045 | case NID_pkcs7_signedAndEnveloped: |
| 4046 | certs = p7->d.signed_and_enveloped->cert; |
| 4047 | break; |
| 4048 | default: |
| 4049 | certs = NULL; |
| 4050 | break; |
| 4051 | } |
| 4052 | #endif /* OPENSSL_IS_BORINGSSL */ |
| 4053 | |
| 4054 | if (!certs || ((num = sk_X509_num(certs)) == 0)) { |
| 4055 | wpa_printf(MSG_INFO, |
| 4056 | "OpenSSL: No certificates found in PKCS#7 object"); |
| 4057 | goto fail; |
| 4058 | } |
| 4059 | |
| 4060 | out = BIO_new(BIO_s_mem()); |
| 4061 | if (!out) |
| 4062 | goto fail; |
| 4063 | |
| 4064 | for (i = 0; i < num; i++) { |
| 4065 | X509 *cert = sk_X509_value(certs, i); |
| 4066 | |
| 4067 | PEM_write_bio_X509(out, cert); |
| 4068 | } |
| 4069 | |
| 4070 | rlen = BIO_ctrl_pending(out); |
| 4071 | pem = wpabuf_alloc(rlen); |
| 4072 | if (!pem) |
| 4073 | goto fail; |
| 4074 | res = BIO_read(out, wpabuf_put(pem, 0), rlen); |
| 4075 | if (res <= 0) { |
| 4076 | wpabuf_free(pem); |
| 4077 | pem = NULL; |
| 4078 | goto fail; |
| 4079 | } |
| 4080 | wpabuf_put(pem, res); |
| 4081 | |
| 4082 | fail: |
| 4083 | #ifdef OPENSSL_IS_BORINGSSL |
| 4084 | if (certs) |
| 4085 | sk_X509_pop_free(certs, X509_free); |
| 4086 | #else /* OPENSSL_IS_BORINGSSL */ |
| 4087 | PKCS7_free(p7); |
| 4088 | #endif /* OPENSSL_IS_BORINGSSL */ |
| 4089 | if (out) |
| 4090 | BIO_free_all(out); |
| 4091 | |
| 4092 | return pem; |
| 4093 | } |
| 4094 | |
| 4095 | |
| 4096 | struct crypto_csr * crypto_csr_init() |
| 4097 | { |
| 4098 | return (struct crypto_csr *)X509_REQ_new(); |
| 4099 | } |
| 4100 | |
| 4101 | |
| 4102 | struct crypto_csr * crypto_csr_verify(const struct wpabuf *req) |
| 4103 | { |
| 4104 | X509_REQ *csr; |
| 4105 | EVP_PKEY *pkey = NULL; |
| 4106 | const u8 *der = wpabuf_head(req); |
| 4107 | |
| 4108 | csr = d2i_X509_REQ(NULL, &der, wpabuf_len(req)); |
| 4109 | if (!csr) |
| 4110 | return NULL; |
| 4111 | |
| 4112 | pkey = X509_REQ_get_pubkey((X509_REQ *)csr); |
| 4113 | if (!pkey) |
| 4114 | goto fail; |
| 4115 | |
| 4116 | if (X509_REQ_verify((X509_REQ *)csr, pkey) != 1) |
| 4117 | goto fail; |
| 4118 | |
| 4119 | return (struct crypto_csr *)csr; |
| 4120 | fail: |
| 4121 | X509_REQ_free(csr); |
| 4122 | return NULL; |
| 4123 | } |
| 4124 | |
| 4125 | |
| 4126 | void crypto_csr_deinit(struct crypto_csr *csr) |
| 4127 | { |
| 4128 | X509_REQ_free((X509_REQ *)csr); |
| 4129 | } |
| 4130 | |
| 4131 | |
| 4132 | int crypto_csr_set_ec_public_key(struct crypto_csr *csr, struct crypto_ec_key *key) |
| 4133 | { |
| 4134 | if (!X509_REQ_set_pubkey((X509_REQ *)csr, (EVP_PKEY *)key)) |
| 4135 | return -1; |
| 4136 | |
| 4137 | return 0; |
| 4138 | } |
| 4139 | |
| 4140 | |
| 4141 | int crypto_csr_set_name(struct crypto_csr *csr, enum crypto_csr_name type, |
| 4142 | const char *name) |
| 4143 | { |
| 4144 | X509_NAME *n; |
| 4145 | int nid; |
| 4146 | |
| 4147 | switch (type) { |
| 4148 | case CSR_NAME_CN: |
| 4149 | nid = NID_commonName; |
| 4150 | break; |
| 4151 | case CSR_NAME_SN: |
| 4152 | nid = NID_surname; |
| 4153 | break; |
| 4154 | case CSR_NAME_C: |
| 4155 | nid = NID_countryName; |
| 4156 | break; |
| 4157 | case CSR_NAME_O: |
| 4158 | nid = NID_organizationName; |
| 4159 | break; |
| 4160 | case CSR_NAME_OU: |
| 4161 | nid = NID_organizationalUnitName; |
| 4162 | break; |
| 4163 | default: |
| 4164 | return -1; |
| 4165 | } |
| 4166 | |
| 4167 | n = X509_REQ_get_subject_name((X509_REQ *) csr); |
| 4168 | if (!n) |
| 4169 | return -1; |
| 4170 | |
| 4171 | #if OPENSSL_VERSION_NUMBER < 0x10100000L |
| 4172 | if (!X509_NAME_add_entry_by_NID(n, nid, MBSTRING_UTF8, |
| 4173 | (unsigned char *) name, |
| 4174 | os_strlen(name), -1, 0)) |
| 4175 | return -1; |
| 4176 | #else |
| 4177 | if (!X509_NAME_add_entry_by_NID(n, nid, MBSTRING_UTF8, |
| 4178 | (const unsigned char *) name, |
| 4179 | os_strlen(name), -1, 0)) |
| 4180 | return -1; |
| 4181 | #endif |
| 4182 | |
| 4183 | return 0; |
| 4184 | } |
| 4185 | |
| 4186 | |
| 4187 | int crypto_csr_set_attribute(struct crypto_csr *csr, enum crypto_csr_attr attr, |
| 4188 | int attr_type, const u8 *value, size_t len) |
| 4189 | { |
| 4190 | int nid; |
| 4191 | |
| 4192 | switch (attr) { |
| 4193 | case CSR_ATTR_CHALLENGE_PASSWORD: |
| 4194 | nid = NID_pkcs9_challengePassword; |
| 4195 | break; |
| 4196 | default: |
| 4197 | return -1; |
| 4198 | } |
| 4199 | |
| 4200 | if (!X509_REQ_add1_attr_by_NID((X509_REQ *) csr, nid, attr_type, value, |
| 4201 | len)) |
| 4202 | return -1; |
| 4203 | |
| 4204 | return 0; |
| 4205 | } |
| 4206 | |
| 4207 | |
| 4208 | const u8 * crypto_csr_get_attribute(struct crypto_csr *csr, |
| 4209 | enum crypto_csr_attr attr, |
| 4210 | size_t *len, int *type) |
| 4211 | { |
| 4212 | X509_ATTRIBUTE *attrib; |
| 4213 | ASN1_TYPE *attrib_type; |
| 4214 | ASN1_STRING *data; |
| 4215 | int loc; |
| 4216 | int nid; |
| 4217 | |
| 4218 | switch (attr) { |
| 4219 | case CSR_ATTR_CHALLENGE_PASSWORD: |
| 4220 | nid = NID_pkcs9_challengePassword; |
| 4221 | break; |
| 4222 | default: |
| 4223 | return NULL; |
| 4224 | } |
| 4225 | |
| 4226 | loc = X509_REQ_get_attr_by_NID((X509_REQ *) csr, nid, -1); |
| 4227 | if (loc < 0) |
| 4228 | return NULL; |
| 4229 | |
| 4230 | attrib = X509_REQ_get_attr((X509_REQ *) csr, loc); |
| 4231 | if (!attrib) |
| 4232 | return NULL; |
| 4233 | |
| 4234 | attrib_type = X509_ATTRIBUTE_get0_type(attrib, 0); |
| 4235 | if (!attrib_type) |
| 4236 | return NULL; |
| 4237 | *type = ASN1_TYPE_get(attrib_type); |
| 4238 | data = X509_ATTRIBUTE_get0_data(attrib, 0, *type, NULL); |
| 4239 | if (!data) |
| 4240 | return NULL; |
| 4241 | *len = ASN1_STRING_length(data); |
| 4242 | return ASN1_STRING_get0_data(data); |
| 4243 | } |
| 4244 | |
| 4245 | |
| 4246 | struct wpabuf * crypto_csr_sign(struct crypto_csr *csr, |
| 4247 | struct crypto_ec_key *key, |
| 4248 | enum crypto_hash_alg algo) |
| 4249 | { |
| 4250 | const EVP_MD *sign_md; |
| 4251 | struct wpabuf *buf; |
| 4252 | unsigned char *der = NULL; |
| 4253 | int der_len; |
| 4254 | |
| 4255 | switch (algo) { |
| 4256 | case CRYPTO_HASH_ALG_SHA256: |
| 4257 | sign_md = EVP_sha256(); |
| 4258 | break; |
| 4259 | case CRYPTO_HASH_ALG_SHA384: |
| 4260 | sign_md = EVP_sha384(); |
| 4261 | break; |
| 4262 | case CRYPTO_HASH_ALG_SHA512: |
| 4263 | sign_md = EVP_sha512(); |
| 4264 | break; |
| 4265 | default: |
| 4266 | return NULL; |
| 4267 | } |
| 4268 | |
| 4269 | if (!X509_REQ_sign((X509_REQ *) csr, (EVP_PKEY *) key, sign_md)) |
| 4270 | return NULL; |
| 4271 | |
| 4272 | der_len = i2d_X509_REQ((X509_REQ *) csr, &der); |
| 4273 | if (der_len < 0) |
| 4274 | return NULL; |
| 4275 | |
| 4276 | buf = wpabuf_alloc_copy(der, der_len); |
| 4277 | OPENSSL_free(der); |
| 4278 | |
| 4279 | return buf; |
| 4280 | } |
| 4281 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 4282 | #endif /* CONFIG_ECC */ |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 4283 | |
| 4284 | |
| 4285 | static EVP_PKEY * crypto_rsa_key_read_public(FILE *f) |
| 4286 | { |
| 4287 | EVP_PKEY *pkey; |
| 4288 | X509 *x509; |
Sunil | 8cd6f4d | 2022-06-28 18:40:46 +0000 | [diff] [blame] | 4289 | const ASN1_TIME *not_before, *not_after; |
| 4290 | int res_before, res_after; |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 4291 | |
| 4292 | pkey = PEM_read_PUBKEY(f, NULL, NULL, NULL); |
| 4293 | if (pkey) |
| 4294 | return pkey; |
| 4295 | |
| 4296 | rewind(f); |
| 4297 | x509 = PEM_read_X509(f, NULL, NULL, NULL); |
| 4298 | if (!x509) |
| 4299 | return NULL; |
| 4300 | |
Sunil | 8cd6f4d | 2022-06-28 18:40:46 +0000 | [diff] [blame] | 4301 | not_before = X509_get0_notBefore(x509); |
| 4302 | not_after = X509_get0_notAfter(x509); |
| 4303 | if (!not_before || !not_after) |
| 4304 | goto fail; |
| 4305 | res_before = X509_cmp_current_time(not_before); |
| 4306 | res_after = X509_cmp_current_time(not_after); |
| 4307 | if (!res_before || !res_after) |
| 4308 | goto fail; |
| 4309 | if (res_before > 0 || res_after < 0) { |
| 4310 | wpa_printf(MSG_INFO, |
| 4311 | "OpenSSL: Certificate for RSA public key is not valid at this time (%d %d)", |
| 4312 | res_before, res_after); |
| 4313 | goto fail; |
| 4314 | } |
| 4315 | |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 4316 | pkey = X509_get_pubkey(x509); |
| 4317 | X509_free(x509); |
| 4318 | |
| 4319 | if (!pkey) |
| 4320 | return NULL; |
| 4321 | if (EVP_PKEY_base_id(pkey) != EVP_PKEY_RSA) { |
Sunil | 8cd6f4d | 2022-06-28 18:40:46 +0000 | [diff] [blame] | 4322 | wpa_printf(MSG_INFO, "OpenSSL: No RSA public key found"); |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 4323 | EVP_PKEY_free(pkey); |
| 4324 | return NULL; |
| 4325 | } |
| 4326 | |
| 4327 | return pkey; |
Sunil | 8cd6f4d | 2022-06-28 18:40:46 +0000 | [diff] [blame] | 4328 | fail: |
| 4329 | X509_free(x509); |
| 4330 | return NULL; |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 4331 | } |
| 4332 | |
| 4333 | |
| 4334 | struct crypto_rsa_key * crypto_rsa_key_read(const char *file, bool private_key) |
| 4335 | { |
| 4336 | FILE *f; |
| 4337 | EVP_PKEY *pkey; |
| 4338 | |
| 4339 | f = fopen(file, "r"); |
| 4340 | if (!f) |
| 4341 | return NULL; |
| 4342 | if (private_key) |
| 4343 | pkey = PEM_read_PrivateKey(f, NULL, NULL, NULL); |
| 4344 | else |
| 4345 | pkey = crypto_rsa_key_read_public(f); |
| 4346 | fclose(f); |
| 4347 | return (struct crypto_rsa_key *) pkey; |
| 4348 | } |
| 4349 | |
| 4350 | |
| 4351 | #ifndef OPENSSL_NO_SHA256 |
| 4352 | |
| 4353 | struct wpabuf * crypto_rsa_oaep_sha256_encrypt(struct crypto_rsa_key *key, |
| 4354 | const struct wpabuf *in) |
| 4355 | { |
| 4356 | #if !defined(LIBRESSL_VERSION_NUMBER) || LIBRESSL_VERSION_NUMBER >= 0x30400000L |
| 4357 | EVP_PKEY *pkey = (EVP_PKEY *) key; |
| 4358 | EVP_PKEY_CTX *pkctx; |
| 4359 | struct wpabuf *res = NULL; |
| 4360 | size_t outlen; |
| 4361 | |
| 4362 | pkctx = EVP_PKEY_CTX_new(pkey, NULL); |
| 4363 | if (!pkctx) |
| 4364 | goto fail; |
| 4365 | |
| 4366 | if (EVP_PKEY_encrypt_init(pkctx) != 1 || |
| 4367 | EVP_PKEY_CTX_set_rsa_padding(pkctx, RSA_PKCS1_OAEP_PADDING) <= 0 || |
| 4368 | EVP_PKEY_CTX_set_rsa_oaep_md(pkctx, EVP_sha256()) <= 0 || |
| 4369 | EVP_PKEY_encrypt(pkctx, NULL, &outlen, wpabuf_head(in), |
| 4370 | wpabuf_len(in)) != 1 || |
| 4371 | !(res = wpabuf_alloc(outlen)) || |
| 4372 | EVP_PKEY_encrypt(pkctx, wpabuf_put(res, 0), &outlen, |
| 4373 | wpabuf_head(in), wpabuf_len(in)) != 1) { |
| 4374 | wpabuf_free(res); |
| 4375 | res = NULL; |
| 4376 | goto fail; |
| 4377 | } |
| 4378 | wpabuf_put(res, outlen); |
| 4379 | |
| 4380 | fail: |
| 4381 | EVP_PKEY_CTX_free(pkctx); |
| 4382 | return res; |
| 4383 | #else |
| 4384 | wpa_printf(MSG_ERROR, "%s() not supported", __func__); |
| 4385 | return NULL; |
| 4386 | #endif |
| 4387 | } |
| 4388 | |
| 4389 | |
| 4390 | struct wpabuf * crypto_rsa_oaep_sha256_decrypt(struct crypto_rsa_key *key, |
| 4391 | const struct wpabuf *in) |
| 4392 | { |
| 4393 | #if !defined(LIBRESSL_VERSION_NUMBER) || LIBRESSL_VERSION_NUMBER >= 0x30400000L |
| 4394 | EVP_PKEY *pkey = (EVP_PKEY *) key; |
| 4395 | EVP_PKEY_CTX *pkctx; |
| 4396 | struct wpabuf *res = NULL; |
| 4397 | size_t outlen; |
| 4398 | |
| 4399 | pkctx = EVP_PKEY_CTX_new(pkey, NULL); |
| 4400 | if (!pkctx) |
| 4401 | goto fail; |
| 4402 | |
| 4403 | if (EVP_PKEY_decrypt_init(pkctx) != 1 || |
| 4404 | EVP_PKEY_CTX_set_rsa_padding(pkctx, RSA_PKCS1_OAEP_PADDING) <= 0 || |
| 4405 | EVP_PKEY_CTX_set_rsa_oaep_md(pkctx, EVP_sha256()) <= 0 || |
| 4406 | EVP_PKEY_decrypt(pkctx, NULL, &outlen, wpabuf_head(in), |
| 4407 | wpabuf_len(in)) != 1 || |
| 4408 | !(res = wpabuf_alloc(outlen)) || |
| 4409 | EVP_PKEY_decrypt(pkctx, wpabuf_put(res, 0), &outlen, |
| 4410 | wpabuf_head(in), wpabuf_len(in)) != 1) { |
| 4411 | wpabuf_free(res); |
| 4412 | res = NULL; |
| 4413 | goto fail; |
| 4414 | } |
| 4415 | wpabuf_put(res, outlen); |
| 4416 | |
| 4417 | fail: |
| 4418 | EVP_PKEY_CTX_free(pkctx); |
| 4419 | return res; |
| 4420 | #else |
| 4421 | wpa_printf(MSG_ERROR, "%s() not supported", __func__); |
| 4422 | return NULL; |
| 4423 | #endif |
| 4424 | } |
| 4425 | |
| 4426 | #endif /* OPENSSL_NO_SHA256 */ |
| 4427 | |
| 4428 | |
| 4429 | void crypto_rsa_key_free(struct crypto_rsa_key *key) |
| 4430 | { |
| 4431 | EVP_PKEY_free((EVP_PKEY *) key); |
| 4432 | } |
| 4433 | |
| 4434 | |
| 4435 | void crypto_unload(void) |
| 4436 | { |
| 4437 | openssl_unload_legacy_provider(); |
| 4438 | } |