Fix error handling at openssl_strerror_r
[oweals/openssl.git] / crypto / sm2 / sm2_crypt.c
index 2a0fdceea785f42eadeb93a448ce1f6a35d6a5d2..4389fc731edd8917ec1ef61194fe7cdde394860a 100644 (file)
@@ -11,6 +11,7 @@
 
 #include "internal/sm2.h"
 #include "internal/sm2err.h"
+#include "internal/ec_int.h" /* ecdh_KDF_X9_63() */
 #include <openssl/err.h>
 #include <openssl/evp.h>
 #include <openssl/bn.h>
@@ -37,7 +38,7 @@ ASN1_SEQUENCE(SM2_Ciphertext) = {
 
 IMPLEMENT_ASN1_FUNCTIONS(SM2_Ciphertext)
 
-static size_t EC_field_size(const EC_GROUP *group)
+static size_t ec_field_size(const EC_GROUP *group)
 {
     /* Is there some simpler way to do this? */
     BIGNUM *p = BN_new();
@@ -48,7 +49,8 @@ static size_t EC_field_size(const EC_GROUP *group)
     if (p == NULL || a == NULL || b == NULL)
        goto done;
 
-    EC_GROUP_get_curve_GFp(group, p, a, b, NULL);
+    if (!EC_GROUP_get_curve(group, p, a, b, NULL))
+        goto done;
     field_size = (BN_num_bits(p) + 7) / 8;
 
  done:
@@ -59,25 +61,25 @@ static size_t EC_field_size(const EC_GROUP *group)
     return field_size;
 }
 
-int SM2_plaintext_size(const EC_KEY *key, const EVP_MD *digest, size_t msg_len,
+int sm2_plaintext_size(const EC_KEY *key, const EVP_MD *digest, size_t msg_len,
                        size_t *pt_size)
 {
-    const size_t field_size = EC_field_size(EC_KEY_get0_group(key));
+    const size_t field_size = ec_field_size(EC_KEY_get0_group(key));
     const int md_size = EVP_MD_size(digest);
     size_t overhead;
 
     if (md_size < 0) {
-        SM2err(SM2_F_SM2_ENCRYPT, SM2_R_INVALID_DIGEST);
+        SM2err(SM2_F_SM2_PLAINTEXT_SIZE, SM2_R_INVALID_DIGEST);
         return 0;
     }
     if (field_size == 0) {
-        SM2err(SM2_F_SM2_ENCRYPT, SM2_R_INVALID_FIELD);
+        SM2err(SM2_F_SM2_PLAINTEXT_SIZE, SM2_R_INVALID_FIELD);
         return 0;
     }
 
     overhead = 10 + 2 * field_size + (size_t)md_size;
-    if(msg_len <= overhead) {
-        SM2err(SM2_F_SM2_ENCRYPT, SM2_R_INVALID_ENCODING);
+    if (msg_len <= overhead) {
+        SM2err(SM2_F_SM2_PLAINTEXT_SIZE, SM2_R_INVALID_ENCODING);
         return 0;
     }
 
@@ -85,20 +87,27 @@ int SM2_plaintext_size(const EC_KEY *key, const EVP_MD *digest, size_t msg_len,
     return 1;
 }
 
-int SM2_ciphertext_size(const EC_KEY *key, const EVP_MD *digest, size_t msg_len,
+int sm2_ciphertext_size(const EC_KEY *key, const EVP_MD *digest, size_t msg_len,
                         size_t *ct_size)
 {
-    const size_t field_size = EC_field_size(EC_KEY_get0_group(key));
+    const size_t field_size = ec_field_size(EC_KEY_get0_group(key));
     const int md_size = EVP_MD_size(digest);
+    size_t sz;
 
     if (field_size == 0 || md_size < 0)
         return 0;
 
-    *ct_size = 10 + 2 * field_size + (size_t)md_size + msg_len;
+    /* Integer and string are simple type; set constructed = 0, means primitive and definite length encoding. */
+    sz = 2 * ASN1_object_size(0, field_size + 1, V_ASN1_INTEGER)
+         + ASN1_object_size(0, md_size, V_ASN1_OCTET_STRING)
+         + ASN1_object_size(0, msg_len, V_ASN1_OCTET_STRING);
+    /* Sequence is structured type; set constructed = 1, means constructed and definite length encoding. */
+    *ct_size = ASN1_object_size(1, sz, V_ASN1_SEQUENCE);
+
     return 1;
 }
 
-int SM2_encrypt(const EC_KEY *key,
+int sm2_encrypt(const EC_KEY *key,
                 const EVP_MD *digest,
                 const uint8_t *msg,
                 size_t msg_len, uint8_t *ciphertext_buf, size_t *ciphertext_len)
@@ -121,19 +130,20 @@ int SM2_encrypt(const EC_KEY *key,
     uint8_t *msg_mask = NULL;
     uint8_t *x2y2 = NULL;
     uint8_t *C3 = NULL;
-    const size_t field_size = EC_field_size(group);
-    const size_t C3_size = EVP_MD_size(digest);
+    size_t field_size;
+    const int C3_size = EVP_MD_size(digest);
 
     /* NULL these before any "goto done" */
     ctext_struct.C2 = NULL;
     ctext_struct.C3 = NULL;
 
-    if (hash == NULL
-            || group == NULL
-            || order == NULL
-            || P == NULL
-            || field_size == 0
-            || C3_size == 0) {
+    if (hash == NULL || C3_size <= 0) {
+        SM2err(SM2_F_SM2_ENCRYPT, ERR_R_INTERNAL_ERROR);
+        goto done;
+    }
+
+    field_size = ec_field_size(group);
+    if (field_size == 0) {
         SM2err(SM2_F_SM2_ENCRYPT, ERR_R_INTERNAL_ERROR);
         goto done;
     }
@@ -174,9 +184,9 @@ int SM2_encrypt(const EC_KEY *key,
     }
 
     if (!EC_POINT_mul(group, kG, k, NULL, NULL, ctx)
-            || !EC_POINT_get_affine_coordinates_GFp(group, kG, x1, y1, ctx)
+            || !EC_POINT_get_affine_coordinates(group, kG, x1, y1, ctx)
             || !EC_POINT_mul(group, kP, NULL, P, k, ctx)
-            || !EC_POINT_get_affine_coordinates_GFp(group, kP, x2, y2, ctx)) {
+            || !EC_POINT_get_affine_coordinates(group, kP, x2, y2, ctx)) {
         SM2err(SM2_F_SM2_ENCRYPT, ERR_R_EC_LIB);
         goto done;
     }
@@ -194,7 +204,7 @@ int SM2_encrypt(const EC_KEY *key,
    }
 
     /* X9.63 with no salt happens to match the KDF used in SM2 */
-    if (!ECDH_KDF_X9_62(msg_mask, msg_len, x2y2, 2 * field_size, NULL, 0,
+    if (!ecdh_KDF_X9_63(msg_mask, msg_len, x2y2, 2 * field_size, NULL, 0,
                         digest)) {
         SM2err(SM2_F_SM2_ENCRYPT, ERR_R_EVP_LIB);
         goto done;
@@ -250,166 +260,124 @@ int SM2_encrypt(const EC_KEY *key,
     return rc;
 }
 
-int SM2_decrypt(const EC_KEY *key,
+int sm2_decrypt(const EC_KEY *key,
                 const EVP_MD *digest,
                 const uint8_t *ciphertext,
                 size_t ciphertext_len, uint8_t *ptext_buf, size_t *ptext_len)
 {
     int rc = 0;
     int i;
-
     BN_CTX *ctx = NULL;
     const EC_GROUP *group = EC_KEY_get0_group(key);
     EC_POINT *C1 = NULL;
     struct SM2_Ciphertext_st *sm2_ctext = NULL;
     BIGNUM *x2 = NULL;
     BIGNUM *y2 = NULL;
-
     uint8_t *x2y2 = NULL;
     uint8_t *computed_C3 = NULL;
-
-    const size_t field_size = EC_field_size(group);
+    const size_t field_size = ec_field_size(group);
     const int hash_size = EVP_MD_size(digest);
-
     uint8_t *msg_mask = NULL;
     const uint8_t *C2 = NULL;
     const uint8_t *C3 = NULL;
     int msg_len = 0;
     EVP_MD_CTX *hash = NULL;
 
-    if (field_size == 0 || hash_size == 0)
+    if (field_size == 0 || hash_size <= 0)
        goto done;
 
     memset(ptext_buf, 0xFF, *ptext_len);
 
     sm2_ctext = d2i_SM2_Ciphertext(NULL, &ciphertext, ciphertext_len);
 
-    if (sm2_ctext == NULL)
-        {
+    if (sm2_ctext == NULL) {
         SM2err(SM2_F_SM2_DECRYPT, SM2_R_ASN1_ERROR);
         goto done;
-        }
+    }
 
-    if (sm2_ctext->C3->length != hash_size)
-        {
+    if (sm2_ctext->C3->length != hash_size) {
         SM2err(SM2_F_SM2_DECRYPT, SM2_R_INVALID_ENCODING);
         goto done;
-        }
+    }
 
     C2 = sm2_ctext->C2->data;
     C3 = sm2_ctext->C3->data;
     msg_len = sm2_ctext->C2->length;
 
     ctx = BN_CTX_new();
-    if (ctx == NULL)
-        {
+    if (ctx == NULL) {
         SM2err(SM2_F_SM2_DECRYPT, ERR_R_MALLOC_FAILURE);
         goto done;
-        }
+    }
 
     BN_CTX_start(ctx);
     x2 = BN_CTX_get(ctx);
     y2 = BN_CTX_get(ctx);
 
-    if(y2 == NULL)
-        {
+    if (y2 == NULL) {
         SM2err(SM2_F_SM2_DECRYPT, ERR_R_BN_LIB);
         goto done;
-        }
+    }
 
     msg_mask = OPENSSL_zalloc(msg_len);
     x2y2 = OPENSSL_zalloc(2 * field_size);
     computed_C3 = OPENSSL_zalloc(hash_size);
 
-    if(msg_mask == NULL || x2y2 == NULL || computed_C3 == NULL)
-        {
+    if (msg_mask == NULL || x2y2 == NULL || computed_C3 == NULL) {
         SM2err(SM2_F_SM2_DECRYPT, ERR_R_MALLOC_FAILURE);
         goto done;
-        }
+    }
 
     C1 = EC_POINT_new(group);
-    if (C1 == NULL)
-        {
+    if (C1 == NULL) {
         SM2err(SM2_F_SM2_DECRYPT, ERR_R_MALLOC_FAILURE);
         goto done;
-        }
-
-    if (EC_POINT_set_affine_coordinates_GFp
-        (group, C1, sm2_ctext->C1x, sm2_ctext->C1y, ctx) == 0)
-        {
-        SM2err(SM2_F_SM2_DECRYPT, ERR_R_EC_LIB);
-        goto done;
-        }
-
-    if (EC_POINT_mul(group, C1, NULL, C1, EC_KEY_get0_private_key(key), ctx) ==
-        0)
-        {
-        SM2err(SM2_F_SM2_DECRYPT, ERR_R_EC_LIB);
-        goto done;
-        }
+    }
 
-    if (EC_POINT_get_affine_coordinates_GFp(group, C1, x2, y2, ctx) == 0)
-        {
+    if (!EC_POINT_set_affine_coordinates(group, C1, sm2_ctext->C1x,
+                                         sm2_ctext->C1y, ctx)
+            || !EC_POINT_mul(group, C1, NULL, C1, EC_KEY_get0_private_key(key),
+                             ctx)
+            || !EC_POINT_get_affine_coordinates(group, C1, x2, y2, ctx)) {
         SM2err(SM2_F_SM2_DECRYPT, ERR_R_EC_LIB);
         goto done;
-        }
-
-    BN_bn2binpad(x2, x2y2, field_size);
-    BN_bn2binpad(y2, x2y2 + field_size, field_size);
+    }
 
-    if (ECDH_KDF_X9_62(msg_mask, msg_len, x2y2, 2 * field_size, NULL, 0, digest)
-        == 0)
+    if (BN_bn2binpad(x2, x2y2, field_size) < 0
+            || BN_bn2binpad(y2, x2y2 + field_size, field_size) < 0
+            || !ecdh_KDF_X9_63(msg_mask, msg_len, x2y2, 2 * field_size, NULL, 0,
+                               digest)) {
+        SM2err(SM2_F_SM2_DECRYPT, ERR_R_INTERNAL_ERROR);
         goto done;
+    }
 
     for (i = 0; i != msg_len; ++i)
         ptext_buf[i] = C2[i] ^ msg_mask[i];
 
     hash = EVP_MD_CTX_new();
-
-    if (hash == NULL)
-        {
+    if (hash == NULL) {
         SM2err(SM2_F_SM2_DECRYPT, ERR_R_MALLOC_FAILURE);
         goto done;
-        }
-
-    if (EVP_DigestInit(hash, digest) == 0)
-        {
-        SM2err(SM2_F_SM2_DECRYPT, ERR_R_EVP_LIB);
-        goto done;
-        }
-
-    if (EVP_DigestUpdate(hash, x2y2, field_size) == 0)
-        {
-        SM2err(SM2_F_SM2_DECRYPT, ERR_R_EVP_LIB);
-        goto done;
-        }
-
-    if (EVP_DigestUpdate(hash, ptext_buf, msg_len) == 0)
-        {
-        SM2err(SM2_F_SM2_DECRYPT, ERR_R_EVP_LIB);
-        goto done;
-        }
-
-    if (EVP_DigestUpdate(hash, x2y2 + field_size, field_size) == 0)
-        {
-        SM2err(SM2_F_SM2_DECRYPT, ERR_R_EVP_LIB);
-        goto done;
-        }
+    }
 
-    if (EVP_DigestFinal(hash, computed_C3, NULL) == 0)
-        {
+    if (!EVP_DigestInit(hash, digest)
+            || !EVP_DigestUpdate(hash, x2y2, field_size)
+            || !EVP_DigestUpdate(hash, ptext_buf, msg_len)
+            || !EVP_DigestUpdate(hash, x2y2 + field_size, field_size)
+            || !EVP_DigestFinal(hash, computed_C3, NULL)) {
         SM2err(SM2_F_SM2_DECRYPT, ERR_R_EVP_LIB);
         goto done;
-        }
+    }
 
-    if (memcmp(computed_C3, C3, hash_size) != 0)
+    if (CRYPTO_memcmp(computed_C3, C3, hash_size) != 0) {
+        SM2err(SM2_F_SM2_DECRYPT, SM2_R_INVALID_DIGEST);
         goto done;
+    }
 
     rc = 1;
     *ptext_len = msg_len;
 
  done:
-
     if (rc == 0)
         memset(ptext_buf, 0, *ptext_len);