Add build option to remove all internal RC4 uses
[mech_eap.git] / src / crypto / crypto_openssl.c
index 1da2b9f..a824293 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Wrapper functions for OpenSSL libcrypto
- * Copyright (c) 2004-2013, Jouni Malinen <j@w1.fi>
+ * Copyright (c) 2004-2015, Jouni Malinen <j@w1.fi>
  *
  * This software may be distributed under the terms of the BSD license.
  * See README for more details.
 #include "dh_group5.h"
 #include "sha1.h"
 #include "sha256.h"
+#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
+#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,
@@ -60,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
@@ -109,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)
@@ -130,12 +116,13 @@ void des_encrypt(const u8 *clear, const u8 *key, u8 *cypher)
        }
        pkey[i] = next | 1;
 
-       DES_set_key(&pkey, &ks);
+       DES_set_key((DES_cblock *) &pkey, &ks);
        DES_ecb_encrypt((DES_cblock *) clear, (DES_cblock *) cypher, &ks,
                        DES_ENCRYPT);
 }
 
 
+#ifndef CONFIG_NO_RC4
 int rc4_skip(const u8 *key, size_t keylen, size_t skip,
             u8 *data, size_t data_len)
 {
@@ -171,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)
@@ -199,8 +189,10 @@ static const EVP_CIPHER * aes_get_evp_cipher(size_t keylen)
        switch (keylen) {
        case 16:
                return EVP_aes_128_ecb();
+#ifndef OPENSSL_IS_BORINGSSL
        case 24:
                return EVP_aes_192_ecb();
+#endif /* OPENSSL_IS_BORINGSSL */
        case 32:
                return EVP_aes_256_ecb();
        }
@@ -256,7 +248,7 @@ void aes_encrypt_deinit(void *ctx)
                           "in AES encrypt", len);
        }
        EVP_CIPHER_CTX_cleanup(c);
-       os_free(c);
+       bin_clear_free(c, sizeof(*c));
 }
 
 
@@ -307,7 +299,88 @@ void aes_decrypt_deinit(void *ctx)
                           "in AES decrypt", len);
        }
        EVP_CIPHER_CTX_cleanup(c);
-       os_free(ctx);
+       bin_clear_free(c, sizeof(*c));
+}
+
+
+#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;
 }
 
 
@@ -340,10 +413,10 @@ int crypto_mod_exp(const u8 *base, size_t base_len,
        ret = 0;
 
 error:
-       BN_free(bn_base);
-       BN_free(bn_exp);
-       BN_free(bn_modulus);
-       BN_free(bn_result);
+       BN_clear_free(bn_base);
+       BN_clear_free(bn_exp);
+       BN_clear_free(bn_modulus);
+       BN_clear_free(bn_result);
        BN_CTX_free(ctx);
        return ret;
 }
@@ -367,20 +440,24 @@ 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) {
                case 16:
                        cipher = EVP_aes_128_cbc();
                        break;
+#ifndef OPENSSL_IS_BORINGSSL
                case 24:
                        cipher = EVP_aes_192_cbc();
                        break;
+#endif /* OPENSSL_IS_BORINGSSL */
                case 32:
                        cipher = EVP_aes_256_cbc();
                        break;
@@ -503,8 +580,8 @@ void * dh5_init(struct wpabuf **priv, struct wpabuf **publ)
        return dh;
 
 err:
-       wpabuf_free(pubkey);
-       wpabuf_free(privkey);
+       wpabuf_clear_free(pubkey);
+       wpabuf_clear_free(privkey);
        DH_free(dh);
        return NULL;
 }
@@ -571,13 +648,13 @@ struct wpabuf * dh5_derive_shared(void *ctx, const struct wpabuf *peer_public,
        if (keylen < 0)
                goto err;
        wpabuf_put(res, keylen);
-       BN_free(pub_key);
+       BN_clear_free(pub_key);
 
        return res;
 
 err:
-       BN_free(pub_key);
-       wpabuf_free(res);
+       BN_clear_free(pub_key);
+       wpabuf_clear_free(res);
        return NULL;
 }
 
@@ -634,7 +711,7 @@ struct crypto_hash * crypto_hash_init(enum crypto_hash_alg alg, const u8 *key,
        HMAC_Init_ex(&ctx->ctx, key, key_len, md, NULL);
 #else /* openssl < 0.9.9 */
        if (HMAC_Init_ex(&ctx->ctx, key, key_len, md, NULL) != 1) {
-               os_free(ctx);
+               bin_clear_free(ctx, sizeof(*ctx));
                return NULL;
        }
 #endif /* openssl < 0.9.9 */
@@ -660,7 +737,7 @@ int crypto_hash_finish(struct crypto_hash *ctx, u8 *mac, size_t *len)
                return -2;
 
        if (mac == NULL || len == NULL) {
-               os_free(ctx);
+               bin_clear_free(ctx, sizeof(*ctx));
                return 0;
        }
 
@@ -672,7 +749,7 @@ int crypto_hash_finish(struct crypto_hash *ctx, u8 *mac, size_t *len)
        res = HMAC_Final(&ctx->ctx, mac, &mdlen);
 #endif /* openssl < 0.9.9 */
        HMAC_CTX_cleanup(&ctx->ctx);
-       os_free(ctx);
+       bin_clear_free(ctx, sizeof(*ctx));
 
        if (res == 1) {
                *len = mdlen;
@@ -683,43 +760,26 @@ int crypto_hash_finish(struct crypto_hash *ctx, u8 *mac, size_t *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, 4096, buflen, buf) != 1)
-               return -1;
-#else /* openssl < 0.9.8 */
-       if (PKCS5_PBKDF2_HMAC_SHA1(passphrase, os_strlen(passphrase), ssid,
-                                  ssid_len, 4096, buflen, buf) != 1)
-               return -1;
-#endif /* openssl < 0.9.8 */
-       return 0;
-}
-
-
-int hmac_sha1_vector(const u8 *key, size_t key_len, size_t num_elem,
-                    const u8 *addr[], const size_t *len, u8 *mac)
+static int openssl_hmac_vector(const EVP_MD *type, const u8 *key,
+                              size_t key_len, size_t num_elem,
+                              const u8 *addr[], const size_t *len, u8 *mac,
+                              unsigned int mdlen)
 {
        HMAC_CTX ctx;
        size_t i;
-       unsigned int mdlen;
        int res;
 
        HMAC_CTX_init(&ctx);
 #if OPENSSL_VERSION_NUMBER < 0x00909000
-       HMAC_Init_ex(&ctx, key, key_len, EVP_sha1(), NULL);
+       HMAC_Init_ex(&ctx, key, key_len, type, NULL);
 #else /* openssl < 0.9.9 */
-       if (HMAC_Init_ex(&ctx, key, key_len, EVP_sha1(), NULL) != 1)
+       if (HMAC_Init_ex(&ctx, key, key_len, type, NULL) != 1)
                return -1;
 #endif /* openssl < 0.9.9 */
 
        for (i = 0; i < num_elem; i++)
                HMAC_Update(&ctx, addr[i], len[i]);
 
-       mdlen = 20;
 #if OPENSSL_VERSION_NUMBER < 0x00909000
        HMAC_Final(&ctx, mac, &mdlen);
        res = 1;
@@ -732,6 +792,43 @@ int hmac_sha1_vector(const u8 *key, size_t key_len, size_t num_elem,
 }
 
 
+#ifndef CONFIG_FIPS
+
+int hmac_md5_vector(const u8 *key, size_t key_len, size_t num_elem,
+                   const u8 *addr[], const size_t *len, u8 *mac)
+{
+       return openssl_hmac_vector(EVP_md5(), key ,key_len, num_elem, addr, len,
+                                  mac, 16);
+}
+
+
+int hmac_md5(const u8 *key, size_t key_len, const u8 *data, size_t data_len,
+            u8 *mac)
+{
+       return hmac_md5_vector(key, key_len, 1, &data, &data_len, mac);
+}
+
+#endif /* CONFIG_FIPS */
+
+
+int pbkdf2_sha1(const char *passphrase, const u8 *ssid, size_t ssid_len,
+               int iterations, u8 *buf, size_t buflen)
+{
+       if (PKCS5_PBKDF2_HMAC_SHA1(passphrase, os_strlen(passphrase), ssid,
+                                  ssid_len, iterations, buflen, buf) != 1)
+               return -1;
+       return 0;
+}
+
+
+int hmac_sha1_vector(const u8 *key, size_t key_len, size_t num_elem,
+                    const u8 *addr[], const size_t *len, u8 *mac)
+{
+       return openssl_hmac_vector(EVP_sha1(), key, key_len, num_elem, addr,
+                                  len, mac, 20);
+}
+
+
 int hmac_sha1(const u8 *key, size_t key_len, const u8 *data, size_t data_len,
               u8 *mac)
 {
@@ -744,42 +841,37 @@ int hmac_sha1(const u8 *key, size_t key_len, const u8 *data, size_t data_len,
 int hmac_sha256_vector(const u8 *key, size_t key_len, size_t num_elem,
                       const u8 *addr[], const size_t *len, u8 *mac)
 {
-       HMAC_CTX ctx;
-       size_t i;
-       unsigned int mdlen;
-       int res;
+       return openssl_hmac_vector(EVP_sha256(), key, key_len, num_elem, addr,
+                                  len, mac, 32);
+}
 
-       HMAC_CTX_init(&ctx);
-#if OPENSSL_VERSION_NUMBER < 0x00909000
-       HMAC_Init_ex(&ctx, key, key_len, EVP_sha256(), NULL);
-#else /* openssl < 0.9.9 */
-       if (HMAC_Init_ex(&ctx, key, key_len, EVP_sha256(), NULL) != 1)
-               return -1;
-#endif /* openssl < 0.9.9 */
 
-       for (i = 0; i < num_elem; i++)
-               HMAC_Update(&ctx, addr[i], len[i]);
+int hmac_sha256(const u8 *key, size_t key_len, const u8 *data,
+               size_t data_len, u8 *mac)
+{
+       return hmac_sha256_vector(key, key_len, 1, &data, &data_len, mac);
+}
 
-       mdlen = 32;
-#if OPENSSL_VERSION_NUMBER < 0x00909000
-       HMAC_Final(&ctx, mac, &mdlen);
-       res = 1;
-#else /* openssl < 0.9.9 */
-       res = HMAC_Final(&ctx, mac, &mdlen);
-#endif /* openssl < 0.9.9 */
-       HMAC_CTX_cleanup(&ctx);
+#endif /* CONFIG_SHA256 */
 
-       return res == 1 ? 0 : -1;
+
+#ifdef CONFIG_SHA384
+
+int hmac_sha384_vector(const u8 *key, size_t key_len, size_t num_elem,
+                      const u8 *addr[], const size_t *len, u8 *mac)
+{
+       return openssl_hmac_vector(EVP_sha384(), key, key_len, num_elem, addr,
+                                  len, mac, 32);
 }
 
 
-int hmac_sha256(const u8 *key, size_t key_len, const u8 *data,
+int hmac_sha384(const u8 *key, size_t key_len, const u8 *data,
                size_t data_len, u8 *mac)
 {
-       return hmac_sha256_vector(key, key_len, 1, &data, &data_len, mac);
+       return hmac_sha384_vector(key, key_len, 1, &data, &data_len, mac);
 }
 
-#endif /* CONFIG_SHA256 */
+#endif /* CONFIG_SHA384 */
 
 
 int crypto_get_random(void *buf, size_t len)
@@ -791,8 +883,8 @@ int crypto_get_random(void *buf, size_t len)
 
 
 #ifdef CONFIG_OPENSSL_CMAC
-int omac1_aes_128_vector(const u8 *key, size_t num_elem,
-                        const u8 *addr[], const size_t *len, u8 *mac)
+int omac1_aes_vector(const u8 *key, size_t key_len, size_t num_elem,
+                    const u8 *addr[], const size_t *len, u8 *mac)
 {
        CMAC_CTX *ctx;
        int ret = -1;
@@ -802,8 +894,15 @@ int omac1_aes_128_vector(const u8 *key, size_t num_elem,
        if (ctx == NULL)
                return -1;
 
-       if (!CMAC_Init(ctx, key, 16, EVP_aes_128_cbc(), NULL))
+       if (key_len == 32) {
+               if (!CMAC_Init(ctx, key, 32, EVP_aes_256_cbc(), NULL))
+                       goto fail;
+       } else if (key_len == 16) {
+               if (!CMAC_Init(ctx, key, 16, EVP_aes_128_cbc(), NULL))
+                       goto fail;
+       } else {
                goto fail;
+       }
        for (i = 0; i < num_elem; i++) {
                if (!CMAC_Update(ctx, addr[i], len[i]))
                        goto fail;
@@ -818,10 +917,23 @@ fail:
 }
 
 
+int omac1_aes_128_vector(const u8 *key, size_t num_elem,
+                        const u8 *addr[], const size_t *len, u8 *mac)
+{
+       return omac1_aes_vector(key, 16, num_elem, addr, len, mac);
+}
+
+
 int omac1_aes_128(const u8 *key, const u8 *data, size_t data_len, u8 *mac)
 {
        return omac1_aes_128_vector(key, 1, &data, &data_len, mac);
 }
+
+
+int omac1_aes_256(const u8 *key, const u8 *data, size_t data_len, u8 *mac)
+{
+       return omac1_aes_vector(key, 32, 1, &data, &data_len, mac);
+}
 #endif /* CONFIG_OPENSSL_CMAC */
 
 
@@ -916,13 +1028,6 @@ int crypto_bignum_exptmod(const struct crypto_bignum *a,
 }
 
 
-int crypto_bignum_rshift(const struct crypto_bignum *a, int n,
-                        struct crypto_bignum *b)
-{
-       return BN_rshift((BIGNUM *) b, (const BIGNUM *) a, n) ? 0 : -1;
-}
-
-
 int crypto_bignum_inverse(const struct crypto_bignum *a,
                          const struct crypto_bignum *b,
                          struct crypto_bignum *c)
@@ -1014,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 {
@@ -1021,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)
@@ -1045,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;
        }
@@ -1057,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;
@@ -1073,7 +1238,10 @@ void crypto_ec_deinit(struct crypto_ec *e)
 {
        if (e == NULL)
                return;
-       BN_free(e->order);
+       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);
        BN_CTX_free(e->bnctx);
        os_free(e);
@@ -1145,8 +1313,8 @@ int crypto_ec_point_to_bin(struct crypto_ec *e,
                ret = 0;
        }
 
-       BN_free(x_bn);
-       BN_free(y_bn);
+       BN_clear_free(x_bn);
+       BN_clear_free(y_bn);
        return ret;
 }
 
@@ -1162,20 +1330,20 @@ struct crypto_ec_point * crypto_ec_point_from_bin(struct crypto_ec *e,
        y = BN_bin2bn(val + len, len, NULL);
        elem = EC_POINT_new(e->group);
        if (x == NULL || y == NULL || elem == NULL) {
-               BN_free(x);
-               BN_free(y);
-               EC_POINT_free(elem);
+               BN_clear_free(x);
+               BN_clear_free(y);
+               EC_POINT_clear_free(elem);
                return NULL;
        }
 
        if (!EC_POINT_set_affine_coordinates_GFp(e->group, elem, x, y,
                                                 e->bnctx)) {
-               EC_POINT_free(elem);
+               EC_POINT_clear_free(elem);
                elem = NULL;
        }
 
-       BN_free(x);
-       BN_free(y);
+       BN_clear_free(x);
+       BN_clear_free(y);
 
        return (struct crypto_ec_point *) elem;
 }
@@ -1219,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)
 {
@@ -1229,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 */