Handle evp_tests assumption of EVP_PKEY_FLAG_AUTOARGLEN
authorJack Lloyd <jack.lloyd@ribose.com>
Fri, 9 Feb 2018 17:21:56 +0000 (12:21 -0500)
committerRichard Levitte <levitte@openssl.org>
Mon, 19 Mar 2018 13:33:25 +0000 (14:33 +0100)
Without actually using EVP_PKEY_FLAG_AUTOARGLEN

Reviewed-by: Rich Salz <rsalz@openssl.org>
Reviewed-by: Richard Levitte <levitte@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/4793)

crypto/ec/ec_pmeth.c
crypto/sm2/sm2_crypt.c
include/openssl/sm2.h
test/sm2crypttest.c
util/libcrypto.num

index 5f3f56c961d0a8cacb5824a148e54da87168cce4..08dda12eeb5e26becd748331bd2a2e14a18704f2 100644 (file)
@@ -221,8 +221,14 @@ static int pkey_ecies_encrypt(EVP_PKEY_CTX *ctx,
 # if defined(OPENSSL_NO_SM2)
         ret = -1;
 # else
-        ret = SM2_encrypt(ec, EVP_get_digestbynid(md_type),
-                          in, inlen, out, outlen);
+        if (out == NULL) {
+            *outlen = SM2_ciphertext_size(ec, EVP_get_digestbynid(md_type), inlen);
+            ret = 1;
+        }
+        else {
+            ret = SM2_encrypt(ec, EVP_get_digestbynid(md_type),
+                              in, inlen, out, outlen);
+        }
 # endif
     } else {
         /* standard ECIES not implemented */
@@ -252,8 +258,14 @@ static int pkey_ecies_decrypt(EVP_PKEY_CTX *ctx,
 # if defined(OPENSSL_NO_SM2)
         ret = -1;
 # else
-        ret = SM2_decrypt(ec, EVP_get_digestbynid(md_type),
-                          in, inlen, out, outlen);
+        if (out == NULL) {
+            *outlen = SM2_plaintext_size(ec, EVP_get_digestbynid(md_type), inlen);
+            ret = 1;
+        }
+        else {
+            ret = SM2_decrypt(ec, EVP_get_digestbynid(md_type),
+                              in, inlen, out, outlen);
+        }
 # endif
     } else {
         /* standard ECIES not implemented */
@@ -510,7 +522,7 @@ static int pkey_ec_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
 
 const EVP_PKEY_METHOD ec_pkey_meth = {
     EVP_PKEY_EC,
-    EVP_PKEY_FLAG_AUTOARGLEN,
+    0,
     pkey_ec_init,
     pkey_ec_copy,
     pkey_ec_cleanup,
index 7e7be9d04a5cad37cd76cce664624771102b22e9..b308e5b2e53e5d4f8ac257e1e236ae8bd8fbfb41 100644 (file)
@@ -57,10 +57,23 @@ static size_t EC_field_size(const EC_GROUP *group)
     return field_size;
 }
 
+size_t SM2_plaintext_size(const EC_KEY *key, const EVP_MD *digest, size_t msg_len)
+{
+    const size_t field_size = EC_field_size(EC_KEY_get0_group(key));
+    const size_t md_size = EVP_MD_size(digest);
+
+    const size_t overhead = 10 + 2 * field_size + md_size;
+    if(msg_len <= overhead)
+       return 0;
+
+    return msg_len - overhead;
+}
+
 size_t SM2_ciphertext_size(const EC_KEY *key, const EVP_MD *digest, size_t msg_len)
 {
-    return 10 + 2 * EC_field_size(EC_KEY_get0_group(key)) +
-        EVP_MD_size(digest) + msg_len;
+    const size_t field_size = EC_field_size(EC_KEY_get0_group(key));
+    const size_t md_size = EVP_MD_size(digest);
+    return 10 + 2 * field_size + md_size + msg_len;
 }
 
 int SM2_encrypt(const EC_KEY *key,
index d12dcadef252b560cfcc01e528096400a3417774..892ffb152a428bd77859dba2852183fd0878612a 100644 (file)
@@ -54,6 +54,10 @@ size_t SM2_ciphertext_size(const EC_KEY *key,
                            const EVP_MD *digest,
                            size_t msg_len);
 
+size_t SM2_plaintext_size(const EC_KEY *key,
+                          const EVP_MD *digest,
+                          size_t msg_len);
+
 int SM2_encrypt(const EC_KEY *key,
                 const EVP_MD *digest,
                 const uint8_t *msg,
index e1e00d8aa4a6b80fc5fb1d87866e4c81c4b56716..961256bc9027128cfeaadca9250c0bc7f83663cd 100644 (file)
@@ -139,6 +139,7 @@ static int test_sm2(const EC_GROUP *group,
     unsigned char *expected = OPENSSL_hexstr2buf(ctext_hex, NULL);
 
     size_t ctext_len = 0;
+    size_t ptext_len = 0;
     uint8_t *ctext = NULL;
     uint8_t *recovered = NULL;
     size_t recovered_len = msg_len;
@@ -172,7 +173,11 @@ static int test_sm2(const EC_GROUP *group,
     if (rc == 0)
         goto done;
 
-    recovered = OPENSSL_zalloc(msg_len);
+    ptext_len = SM2_plaintext_size(key, digest, ctext_len);
+
+    TEST_int_eq(ptext_len, msg_len);
+
+    recovered = OPENSSL_zalloc(ptext_len);
     if (recovered == NULL)
         goto done;
     rc = SM2_decrypt(key, digest, ctext, ctext_len, recovered, &recovered_len);
index 0dfb0cd661c0c25a58fe8154515a4c979133fbbd..96cbb2c3df12d2727246cfcd8cc979ff8f98060c 100644 (file)
@@ -4523,3 +4523,4 @@ SM2_verify                              4464      1_1_1   EXIST::FUNCTION:
 SM2_do_verify                           4465   1_1_1   EXIST::FUNCTION:
 SM2_sign                                4466   1_1_1   EXIST::FUNCTION:
 ERR_load_SM2_strings                    4467   1_1_1   EXIST::FUNCTION:
+SM2_plaintext_size                      4468   1_1_1   EXIST::FUNCTION: