Add build option to remove all internal RC4 uses
[mech_eap.git] / src / crypto / crypto_openssl.c
index 046b249..a824293 100644 (file)
 #include "sha384.h"
 #include "crypto.h"
 
-#if OPENSSL_VERSION_NUMBER < 0x00907000
-#define DES_key_schedule des_key_schedule
-#define DES_cblock des_cblock
-#define DES_set_key(key, schedule) des_set_key((key), *(schedule))
-#define DES_ecb_encrypt(input, output, ks, enc) \
-       des_ecb_encrypt((input), (output), *(ks), (enc))
-#endif /* openssl < 0.9.7 */
-
 static BIGNUM * get_group5_prime(void)
 {
-#if OPENSSL_VERSION_NUMBER < 0x00908000 || defined(OPENSSL_IS_BORINGSSL)
+#ifdef OPENSSL_IS_BORINGSSL
        static const unsigned char RFC3526_PRIME_1536[] = {
                0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xC9,0x0F,0xDA,0xA2,
                0x21,0x68,0xC2,0x34,0xC4,0xC6,0x62,0x8B,0x80,0xDC,0x1C,0xD1,
@@ -61,20 +53,11 @@ static BIGNUM * get_group5_prime(void)
                0xCA,0x23,0x73,0x27,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
        };
         return BN_bin2bn(RFC3526_PRIME_1536, sizeof(RFC3526_PRIME_1536), NULL);
-#else /* openssl < 0.9.8 */
+#else /* OPENSSL_IS_BORINGSSL */
        return get_rfc3526_prime_1536(NULL);
-#endif /* openssl < 0.9.8 */
+#endif /* OPENSSL_IS_BORINGSSL */
 }
 
-#if OPENSSL_VERSION_NUMBER < 0x00908000
-#ifndef OPENSSL_NO_SHA256
-#ifndef OPENSSL_FIPS
-#define NO_SHA256_WRAPPER
-#endif
-#endif
-
-#endif /* openssl < 0.9.8 */
-
 #ifdef OPENSSL_NO_SHA256
 #define NO_SHA256_WRAPPER
 #endif
@@ -110,10 +93,12 @@ static int openssl_digest_vector(const EVP_MD *type, size_t num_elem,
 }
 
 
+#ifndef CONFIG_FIPS
 int md4_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
 {
        return openssl_digest_vector(EVP_md4(), num_elem, addr, len, mac);
 }
+#endif /* CONFIG_FIPS */
 
 
 void des_encrypt(const u8 *clear, const u8 *key, u8 *cypher)
@@ -137,6 +122,7 @@ void des_encrypt(const u8 *clear, const u8 *key, u8 *cypher)
 }
 
 
+#ifndef CONFIG_NO_RC4
 int rc4_skip(const u8 *key, size_t keylen, size_t skip,
             u8 *data, size_t data_len)
 {
@@ -172,12 +158,15 @@ out:
        return res;
 #endif /* OPENSSL_NO_RC4 */
 }
+#endif /* CONFIG_NO_RC4 */
 
 
+#ifndef CONFIG_FIPS
 int md5_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
 {
        return openssl_digest_vector(EVP_md5(), num_elem, addr, len, mac);
 }
+#endif /* CONFIG_FIPS */
 
 
 int sha1_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
@@ -314,6 +303,87 @@ void aes_decrypt_deinit(void *ctx)
 }
 
 
+#ifndef CONFIG_FIPS
+
+int aes_wrap(const u8 *kek, size_t kek_len, int n, const u8 *plain, u8 *cipher)
+{
+       AES_KEY actx;
+       int res;
+
+       if (AES_set_encrypt_key(kek, kek_len << 3, &actx))
+               return -1;
+       res = AES_wrap_key(&actx, NULL, cipher, plain, n * 8);
+       OPENSSL_cleanse(&actx, sizeof(actx));
+       return res <= 0 ? -1 : 0;
+}
+
+
+int aes_unwrap(const u8 *kek, size_t kek_len, int n, const u8 *cipher,
+              u8 *plain)
+{
+       AES_KEY actx;
+       int res;
+
+       if (AES_set_decrypt_key(kek, kek_len << 3, &actx))
+               return -1;
+       res = AES_unwrap_key(&actx, NULL, plain, cipher, (n + 1) * 8);
+       OPENSSL_cleanse(&actx, sizeof(actx));
+       return res <= 0 ? -1 : 0;
+}
+
+#endif /* CONFIG_FIPS */
+
+
+int aes_128_cbc_encrypt(const u8 *key, const u8 *iv, u8 *data, size_t data_len)
+{
+       EVP_CIPHER_CTX ctx;
+       int clen, len;
+       u8 buf[16];
+
+       EVP_CIPHER_CTX_init(&ctx);
+       if (EVP_EncryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL, key, iv) != 1)
+               return -1;
+       EVP_CIPHER_CTX_set_padding(&ctx, 0);
+
+       clen = data_len;
+       if (EVP_EncryptUpdate(&ctx, data, &clen, data, data_len) != 1 ||
+           clen != (int) data_len)
+               return -1;
+
+       len = sizeof(buf);
+       if (EVP_EncryptFinal_ex(&ctx, buf, &len) != 1 || len != 0)
+               return -1;
+       EVP_CIPHER_CTX_cleanup(&ctx);
+
+       return 0;
+}
+
+
+int aes_128_cbc_decrypt(const u8 *key, const u8 *iv, u8 *data, size_t data_len)
+{
+       EVP_CIPHER_CTX ctx;
+       int plen, len;
+       u8 buf[16];
+
+       EVP_CIPHER_CTX_init(&ctx);
+       if (EVP_DecryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL, key, iv) != 1)
+               return -1;
+       EVP_CIPHER_CTX_set_padding(&ctx, 0);
+
+       plen = data_len;
+       if (EVP_DecryptUpdate(&ctx, data, &plen, data, data_len) != 1 ||
+           plen != (int) data_len)
+               return -1;
+
+       len = sizeof(buf);
+       if (EVP_DecryptFinal_ex(&ctx, buf, &len) != 1 || len != 0)
+               return -1;
+       EVP_CIPHER_CTX_cleanup(&ctx);
+
+       return 0;
+}
+
+
 int crypto_mod_exp(const u8 *base, size_t base_len,
                   const u8 *power, size_t power_len,
                   const u8 *modulus, size_t modulus_len,
@@ -370,11 +440,13 @@ struct crypto_cipher * crypto_cipher_init(enum crypto_cipher_alg alg,
                return NULL;
 
        switch (alg) {
+#ifndef CONFIG_NO_RC4
 #ifndef OPENSSL_NO_RC4
        case CRYPTO_CIPHER_ALG_RC4:
                cipher = EVP_rc4();
                break;
 #endif /* OPENSSL_NO_RC4 */
+#endif /* CONFIG_NO_RC4 */
 #ifndef OPENSSL_NO_AES
        case CRYPTO_CIPHER_ALG_AES:
                switch (key_len) {
@@ -742,16 +814,9 @@ int hmac_md5(const u8 *key, size_t key_len, const u8 *data, size_t data_len,
 int pbkdf2_sha1(const char *passphrase, const u8 *ssid, size_t ssid_len,
                int iterations, u8 *buf, size_t buflen)
 {
-#if OPENSSL_VERSION_NUMBER < 0x00908000
-       if (PKCS5_PBKDF2_HMAC_SHA1(passphrase, os_strlen(passphrase),
-                                  (unsigned char *) ssid,
-                                  ssid_len, iterations, buflen, buf) != 1)
-               return -1;
-#else /* openssl < 0.9.8 */
        if (PKCS5_PBKDF2_HMAC_SHA1(passphrase, os_strlen(passphrase), ssid,
                                   ssid_len, iterations, buflen, buf) != 1)
                return -1;
-#endif /* openssl < 0.9.8 */
        return 0;
 }
 
@@ -1054,6 +1119,42 @@ int crypto_bignum_is_one(const struct crypto_bignum *a)
 }
 
 
+int crypto_bignum_legendre(const struct crypto_bignum *a,
+                          const struct crypto_bignum *p)
+{
+       BN_CTX *bnctx;
+       BIGNUM *exp = NULL, *tmp = NULL;
+       int res = -2;
+
+       bnctx = BN_CTX_new();
+       if (bnctx == NULL)
+               return -2;
+
+       exp = BN_new();
+       tmp = BN_new();
+       if (!exp || !tmp ||
+           /* exp = (p-1) / 2 */
+           !BN_sub(exp, (const BIGNUM *) p, BN_value_one()) ||
+           !BN_rshift1(exp, exp) ||
+           !BN_mod_exp(tmp, (const BIGNUM *) a, exp, (const BIGNUM *) p,
+                       bnctx))
+               goto fail;
+
+       if (BN_is_word(tmp, 1))
+               res = 1;
+       else if (BN_is_zero(tmp))
+               res = 0;
+       else
+               res = -1;
+
+fail:
+       BN_clear_free(tmp);
+       BN_clear_free(exp);
+       BN_CTX_free(bnctx);
+       return res;
+}
+
+
 #ifdef CONFIG_ECC
 
 struct crypto_ec {
@@ -1061,6 +1162,8 @@ struct crypto_ec {
        BN_CTX *bnctx;
        BIGNUM *prime;
        BIGNUM *order;
+       BIGNUM *a;
+       BIGNUM *b;
 };
 
 struct crypto_ec * crypto_ec_init(int group)
@@ -1085,6 +1188,26 @@ struct crypto_ec * crypto_ec_init(int group)
        case 26:
                nid = NID_secp224r1;
                break;
+#ifdef NID_brainpoolP224r1
+       case 27:
+               nid = NID_brainpoolP224r1;
+               break;
+#endif /* NID_brainpoolP224r1 */
+#ifdef NID_brainpoolP256r1
+       case 28:
+               nid = NID_brainpoolP256r1;
+               break;
+#endif /* NID_brainpoolP256r1 */
+#ifdef NID_brainpoolP384r1
+       case 29:
+               nid = NID_brainpoolP384r1;
+               break;
+#endif /* NID_brainpoolP384r1 */
+#ifdef NID_brainpoolP512r1
+       case 30:
+               nid = NID_brainpoolP512r1;
+               break;
+#endif /* NID_brainpoolP512r1 */
        default:
                return NULL;
        }
@@ -1097,9 +1220,11 @@ struct crypto_ec * crypto_ec_init(int group)
        e->group = EC_GROUP_new_by_curve_name(nid);
        e->prime = BN_new();
        e->order = BN_new();
+       e->a = BN_new();
+       e->b = BN_new();
        if (e->group == NULL || e->bnctx == NULL || e->prime == NULL ||
-           e->order == NULL ||
-           !EC_GROUP_get_curve_GFp(e->group, e->prime, NULL, NULL, e->bnctx) ||
+           e->order == NULL || e->a == NULL || e->b == NULL ||
+           !EC_GROUP_get_curve_GFp(e->group, e->prime, e->a, e->b, e->bnctx) ||
            !EC_GROUP_get_order(e->group, e->order, e->bnctx)) {
                crypto_ec_deinit(e);
                e = NULL;
@@ -1113,6 +1238,8 @@ void crypto_ec_deinit(struct crypto_ec *e)
 {
        if (e == NULL)
                return;
+       BN_clear_free(e->b);
+       BN_clear_free(e->a);
        BN_clear_free(e->order);
        BN_clear_free(e->prime);
        EC_GROUP_free(e->group);
@@ -1260,6 +1387,33 @@ int crypto_ec_point_solve_y_coord(struct crypto_ec *e,
 }
 
 
+struct crypto_bignum *
+crypto_ec_point_compute_y_sqr(struct crypto_ec *e,
+                             const struct crypto_bignum *x)
+{
+       BIGNUM *tmp, *tmp2, *y_sqr = NULL;
+
+       tmp = BN_new();
+       tmp2 = BN_new();
+
+       /* y^2 = x^3 + ax + b */
+       if (tmp && tmp2 &&
+           BN_mod_sqr(tmp, (const BIGNUM *) x, e->prime, e->bnctx) &&
+           BN_mod_mul(tmp, tmp, (const BIGNUM *) x, e->prime, e->bnctx) &&
+           BN_mod_mul(tmp2, e->a, (const BIGNUM *) x, e->prime, e->bnctx) &&
+           BN_mod_add_quick(tmp2, tmp2, tmp, e->prime) &&
+           BN_mod_add_quick(tmp2, tmp2, e->b, e->prime)) {
+               y_sqr = tmp2;
+               tmp2 = NULL;
+       }
+
+       BN_clear_free(tmp);
+       BN_clear_free(tmp2);
+
+       return (struct crypto_bignum *) y_sqr;
+}
+
+
 int crypto_ec_point_is_at_infinity(struct crypto_ec *e,
                                   const struct crypto_ec_point *p)
 {
@@ -1270,7 +1424,17 @@ int crypto_ec_point_is_at_infinity(struct crypto_ec *e,
 int crypto_ec_point_is_on_curve(struct crypto_ec *e,
                                const struct crypto_ec_point *p)
 {
-       return EC_POINT_is_on_curve(e->group, (const EC_POINT *) p, e->bnctx);
+       return EC_POINT_is_on_curve(e->group, (const EC_POINT *) p,
+                                   e->bnctx) == 1;
+}
+
+
+int crypto_ec_point_cmp(const struct crypto_ec *e,
+                       const struct crypto_ec_point *a,
+                       const struct crypto_ec_point *b)
+{
+       return EC_POINT_cmp(e->group, (const EC_POINT *) a,
+                           (const EC_POINT *) b, e->bnctx);
 }
 
 #endif /* CONFIG_ECC */