More more on SM2 error codes and tidy up
[openssl.git] / crypto / sm2 / sm2_sign.c
index e12eca12fb432944540b80297c5569e910e18917..542c49f044ef9ff59e3235c1881ac5acac7f9807 100644 (file)
@@ -9,44 +9,51 @@
  * https://www.openssl.org/source/license.html
  */
 
-#include <openssl/sm2.h>
+#include "internal/sm2.h"
+#include "internal/sm2err.h"
+#include <openssl/err.h>
 #include <openssl/evp.h>
+#include <openssl/err.h>
 #include <openssl/bn.h>
 #include <string.h>
 
-static BIGNUM *compute_msg_hash(const EVP_MD *digest,
-                                const EC_KEY *key,
-                                const char *user_id,
-                                const uint8_t *msg, size_t msg_len)
+static BIGNUM *SM2_compute_msg_hash(const EVP_MD *digest,
+                                    const EC_KEY *key,
+                                    const char *user_id,
+                                    const uint8_t *msg, size_t msg_len)
 {
     EVP_MD_CTX *hash = EVP_MD_CTX_new();
     const int md_size = EVP_MD_size(digest);
     uint8_t *za = OPENSSL_zalloc(md_size);
     BIGNUM *e = NULL;
 
-    if (za == NULL)
-        goto done;
-
-    if (hash == NULL)
-        goto done;
-
-    if (SM2_compute_userid_digest(za, digest, user_id, key) == 0)
-        goto done;
-
-    if (EVP_DigestInit(hash, digest) == 0)
+    if (hash == NULL || za == NULL) {
+        SM2err(SM2_F_SM2_COMPUTE_MSG_HASH, ERR_R_MALLOC_FAILURE);
         goto done;
+    }
 
-    if (EVP_DigestUpdate(hash, za, md_size) == 0)
+    if (md_size < 0) {
+        SM2err(SM2_F_SM2_COMPUTE_MSG_HASH, SM2_R_INVALID_DIGEST);
         goto done;
+    }
 
-    if (EVP_DigestUpdate(hash, msg, msg_len) == 0)
+    if (!SM2_compute_userid_digest(za, digest, user_id, key)) {
+        /* SM2err already called */
         goto done;
+    }
 
-    /* reuse za buffer to hold H(ZA || M) */
-    if (EVP_DigestFinal(hash, za, NULL) == 0)
+    if (!EVP_DigestInit(hash, digest)
+            || !EVP_DigestUpdate(hash, za, md_size)
+            || !EVP_DigestUpdate(hash, msg, msg_len)
+               /* reuse za buffer to hold H(ZA || M) */
+            || !EVP_DigestFinal(hash, za, NULL)) {
+        SM2err(SM2_F_SM2_COMPUTE_MSG_HASH, ERR_R_EVP_LIB);
         goto done;
+    }
 
     e = BN_bin2bn(za, md_size, NULL);
+    if (e == NULL)
+        SM2err(SM2_F_SM2_COMPUTE_MSG_HASH, ERR_R_INTERNAL_ERROR);
 
  done:
     OPENSSL_free(za);
@@ -54,13 +61,11 @@ static BIGNUM *compute_msg_hash(const EVP_MD *digest,
     return e;
 }
 
-static
-ECDSA_SIG *SM2_sig_gen(const EC_KEY *key, const BIGNUM *e)
+static ECDSA_SIG *SM2_sig_gen(const EC_KEY *key, const BIGNUM *e)
 {
     const BIGNUM *dA = EC_KEY_get0_private_key(key);
     const EC_GROUP *group = EC_KEY_get0_group(key);
     const BIGNUM *order = EC_GROUP_get0_order(group);
-
     ECDSA_SIG *sig = NULL;
     EC_POINT *kG = NULL;
     BN_CTX *ctx = NULL;
@@ -72,63 +77,75 @@ ECDSA_SIG *SM2_sig_gen(const EC_KEY *key, const BIGNUM *e)
     BIGNUM *tmp = NULL;
 
     kG = EC_POINT_new(group);
-    if (kG == NULL)
-        goto done;
-
     ctx = BN_CTX_new();
-    if (ctx == NULL)
+    if (kG == NULL || ctx == NULL) {
+        SM2err(SM2_F_SM2_SIG_GEN, ERR_R_MALLOC_FAILURE);
         goto done;
+    }
 
-    BN_CTX_start(ctx);
 
+    BN_CTX_start(ctx);
     k = BN_CTX_get(ctx);
     rk = BN_CTX_get(ctx);
     x1 = BN_CTX_get(ctx);
     tmp = BN_CTX_get(ctx);
-
-    if (tmp == NULL)
+    if (tmp == NULL) {
+        SM2err(SM2_F_SM2_SIG_GEN, ERR_R_MALLOC_FAILURE);
         goto done;
+    }
 
-    /* These values are returned and so should not be allocated out of the context */
+    /*
+     * These values are returned and so should not be allocated out of the
+     * context
+     */
     r = BN_new();
     s = BN_new();
 
-    if (r == NULL || s == NULL)
+    if (r == NULL || s == NULL) {
+        SM2err(SM2_F_SM2_SIG_GEN, ERR_R_MALLOC_FAILURE);
         goto done;
+    }
 
     for (;;) {
-        BN_priv_rand_range(k, order);
-
-        if (EC_POINT_mul(group, kG, k, NULL, NULL, ctx) == 0)
-            goto done;
-
-        if (EC_POINT_get_affine_coordinates_GFp(group, kG, x1, NULL, ctx) == 0)
-            goto done;
-
-        if (BN_mod_add(r, e, x1, order, ctx) == 0)
+        if (!BN_priv_rand_range(k, order)) {
+            SM2err(SM2_F_SM2_SIG_GEN, ERR_R_INTERNAL_ERROR);
+                goto done;
+        }
+
+        if (!EC_POINT_mul(group, kG, k, NULL, NULL, ctx)
+                || !EC_POINT_get_affine_coordinates_GFp(group, kG, x1, NULL,
+                                                        ctx)
+                || !BN_mod_add(r, e, x1, order, ctx)) {
+            SM2err(SM2_F_SM2_SIG_GEN, ERR_R_INTERNAL_ERROR);
             goto done;
+        }
 
         /* try again if r == 0 or r+k == n */
         if (BN_is_zero(r))
             continue;
 
-        BN_add(rk, r, k);
+        if (!BN_add(rk, r, k)) {
+            SM2err(SM2_F_SM2_SIG_GEN, ERR_R_INTERNAL_ERROR);
+            goto done;
+        }
 
         if (BN_cmp(rk, order) == 0)
             continue;
 
-        BN_add(s, dA, BN_value_one());
-        BN_mod_inverse(s, s, order, ctx);
-
-        BN_mod_mul(tmp, dA, r, order, ctx);
-        BN_sub(tmp, k, tmp);
-
-        BN_mod_mul(s, s, tmp, order, ctx);
+        if (!BN_add(s, dA, BN_value_one())
+                || !BN_mod_inverse(s, s, order, ctx)
+                || !BN_mod_mul(tmp, dA, r, order, ctx)
+                || !BN_sub(tmp, k, tmp)
+                || !BN_mod_mul(s, s, tmp, order, ctx)) {
+            SM2err(SM2_F_SM2_SIG_GEN, ERR_R_BN_LIB);
+            goto done;
+        }
 
         sig = ECDSA_SIG_new();
-
-        if (sig == NULL)
+        if (sig == NULL) {
+            SM2err(SM2_F_SM2_SIG_GEN, ERR_R_MALLOC_FAILURE);
             goto done;
+        }
 
          /* takes ownership of r and s */
         ECDSA_SIG_set0(sig, r, s);
@@ -136,7 +153,6 @@ ECDSA_SIG *SM2_sig_gen(const EC_KEY *key, const BIGNUM *e)
     }
 
  done:
-
     if (sig == NULL) {
         BN_free(r);
         BN_free(s);
@@ -145,74 +161,76 @@ ECDSA_SIG *SM2_sig_gen(const EC_KEY *key, const BIGNUM *e)
     BN_CTX_free(ctx);
     EC_POINT_free(kG);
     return sig;
-
 }
 
-static
-int SM2_sig_verify(const EC_KEY *key, const ECDSA_SIG *sig, const BIGNUM *e)
+static int SM2_sig_verify(const EC_KEY *key, const ECDSA_SIG *sig,
+                          const BIGNUM *e)
 {
     int ret = 0;
     const EC_GROUP *group = EC_KEY_get0_group(key);
     const BIGNUM *order = EC_GROUP_get0_order(group);
     BN_CTX *ctx = NULL;
     EC_POINT *pt = NULL;
-
     BIGNUM *t = NULL;
     BIGNUM *x1 = NULL;
     const BIGNUM *r = NULL;
     const BIGNUM *s = NULL;
 
     ctx = BN_CTX_new();
-    if (ctx == NULL)
-        goto done;
     pt = EC_POINT_new(group);
-    if (pt == NULL)
+    if (ctx == NULL || pt == NULL) {
+        SM2err(SM2_F_SM2_SIG_VERIFY, ERR_R_MALLOC_FAILURE);
         goto done;
+    }
 
     BN_CTX_start(ctx);
-
     t = BN_CTX_get(ctx);
     x1 = BN_CTX_get(ctx);
-
-    if (x1 == NULL)
+    if (x1 == NULL) {
+        SM2err(SM2_F_SM2_SIG_VERIFY, ERR_R_MALLOC_FAILURE);
         goto done;
+    }
 
     /*
-       B1: verify whether r' in [1,n-1], verification failed if not
-       B2: vefify whether s' in [1,n-1], verification failed if not
-       B3: set M'~=ZA || M'
-       B4: calculate e'=Hv(M'~)
-       B5: calculate t = (r' + s') modn, verification failed if t=0
-       B6: calculate the point (x1', y1')=[s']G + [t]PA
-       B7: calculate R=(e'+x1') modn, verfication pass if yes, otherwise failed
+     * B1: verify whether r' in [1,n-1], verification failed if not
+     * B2: vefify whether s' in [1,n-1], verification failed if not
+     * B3: set M'~=ZA || M'
+     * B4: calculate e'=Hv(M'~)
+     * B5: calculate t = (r' + s') modn, verification failed if t=0
+     * B6: calculate the point (x1', y1')=[s']G + [t]PA
+     * B7: calculate R=(e'+x1') modn, verfication pass if yes, otherwise failed
      */
 
     ECDSA_SIG_get0(sig, &r, &s);
 
-    if (BN_cmp(r, BN_value_one()) < 0)
-        goto done;
-    if (BN_cmp(s, BN_value_one()) < 0)
-        goto done;
-
-    if (BN_cmp(order, r) <= 0)
-        goto done;
-    if (BN_cmp(order, s) <= 0)
-        goto done;
-
-    if (BN_mod_add(t, r, s, order, ctx) == 0)
+    if (BN_cmp(r, BN_value_one()) < 0
+            || BN_cmp(s, BN_value_one()) < 0
+            || BN_cmp(order, r) <= 0
+            || BN_cmp(order, s) <= 0) {
+        SM2err(SM2_F_SM2_SIG_VERIFY, SM2_R_BAD_SIGNATURE);
         goto done;
+    }
 
-    if (BN_is_zero(t) == 1)
+    if (!BN_mod_add(t, r, s, order, ctx)) {
+        SM2err(SM2_F_SM2_SIG_VERIFY, ERR_R_BN_LIB);
         goto done;
+    }
 
-    if (EC_POINT_mul(group, pt, s, EC_KEY_get0_public_key(key), t, ctx) == 0)
+    if (BN_is_zero(t)) {
+        SM2err(SM2_F_SM2_SIG_VERIFY, SM2_R_BAD_SIGNATURE);
         goto done;
+    }
 
-    if (EC_POINT_get_affine_coordinates_GFp(group, pt, x1, NULL, ctx) == 0)
+    if (!EC_POINT_mul(group, pt, s, EC_KEY_get0_public_key(key), t, ctx)
+            || !EC_POINT_get_affine_coordinates_GFp(group, pt, x1, NULL, ctx)) {
+        SM2err(SM2_F_SM2_SIG_VERIFY, ERR_R_EC_LIB);
         goto done;
+    }
 
-    if (BN_mod_add(t, e, x1, order, ctx) == 0)
+    if (!BN_mod_add(t, e, x1, order, ctx)) {
+        SM2err(SM2_F_SM2_SIG_VERIFY, ERR_R_BN_LIB);
         goto done;
+    }
 
     if (BN_cmp(r, t) == 0)
         ret = 1;
@@ -230,9 +248,11 @@ ECDSA_SIG *SM2_do_sign(const EC_KEY *key,
     BIGNUM *e = NULL;
     ECDSA_SIG *sig = NULL;
 
-    e = compute_msg_hash(digest, key, user_id, msg, msg_len);
-    if (e == NULL)
+    e = SM2_compute_msg_hash(digest, key, user_id, msg, msg_len);
+    if (e == NULL) {
+        /* SM2err already called */
         goto done;
+    }
 
     sig = SM2_sig_gen(key, e);
 
@@ -247,11 +267,13 @@ int SM2_do_verify(const EC_KEY *key,
                   const char *user_id, const uint8_t *msg, size_t msg_len)
 {
     BIGNUM *e = NULL;
-    int ret = -1;
+    int ret = 0;
 
-    e = compute_msg_hash(digest, key, user_id, msg, msg_len);
-    if (e == NULL)
+    e = SM2_compute_msg_hash(digest, key, user_id, msg, msg_len);
+    if (e == NULL) {
+        /* SM2err already called */
         goto done;
+    }
 
     ret = SM2_sig_verify(key, sig, e);
 
@@ -265,19 +287,28 @@ int SM2_sign(int type, const unsigned char *dgst, int dgstlen,
 {
     BIGNUM *e = NULL;
     ECDSA_SIG *s = NULL;
+    int sigleni;
     int ret = -1;
 
-    if (type != NID_sm3)
-        goto done;
-
-    if (dgstlen != 32)          /* expected length of SM3 hash */
-        goto done;
+    if (type != NID_sm3 || dgstlen != 32) {
+       SM2err(SM2_F_SM2_SIGN, SM2_R_INVALID_DIGEST_TYPE);
+       goto done;
+   }
 
     e = BN_bin2bn(dgst, dgstlen, NULL);
+    if (e == NULL) {
+       SM2err(SM2_F_SM2_SIGN, ERR_R_BN_LIB);
+       goto done;
+    }
 
     s = SM2_sig_gen(eckey, e);
 
-    *siglen = i2d_ECDSA_SIG(s, &sig);
+    sigleni = i2d_ECDSA_SIG(s, &sig);
+    if (sigleni < 0) {
+       SM2err(SM2_F_SM2_SIGN, ERR_R_INTERNAL_ERROR);
+       goto done;
+    }
+    *siglen = (unsigned int)sigleni;
 
     ret = 1;
 
@@ -297,20 +328,32 @@ int SM2_verify(int type, const unsigned char *dgst, int dgstlen,
     int derlen = -1;
     int ret = -1;
 
-    if (type != NID_sm3)
-        goto done;
+    if (type != NID_sm3) {
+       SM2err(SM2_F_SM2_VERIFY, SM2_R_INVALID_DIGEST_TYPE);
+       goto done;
+   }
 
     s = ECDSA_SIG_new();
-    if (s == NULL)
+    if (s == NULL) {
+        SM2err(SM2_F_SM2_VERIFY, ERR_R_MALLOC_FAILURE);
         goto done;
-    if (d2i_ECDSA_SIG(&s, &p, sig_len) == NULL)
+    }
+    if (d2i_ECDSA_SIG(&s, &p, sig_len) == NULL) {
+        SM2err(SM2_F_SM2_VERIFY, SM2_R_INVALID_ENCODING);
         goto done;
+    }
     /* Ensure signature uses DER and doesn't have trailing garbage */
     derlen = i2d_ECDSA_SIG(s, &der);
-    if (derlen != sig_len || memcmp(sig, der, derlen) != 0)
+    if (derlen != sig_len || memcmp(sig, der, derlen) != 0) {
+        SM2err(SM2_F_SM2_VERIFY, SM2_R_INVALID_ENCODING);
         goto done;
+    }
 
     e = BN_bin2bn(dgst, dgstlen, NULL);
+    if (e == NULL) {
+        SM2err(SM2_F_SM2_VERIFY, ERR_R_BN_LIB);
+        goto done;
+    }
 
     ret = SM2_sig_verify(eckey, s, e);