Make SM2 functions private
authorJack Lloyd <jack.lloyd@ribose.com>
Fri, 6 Apr 2018 13:45:41 +0000 (09:45 -0400)
committerMatt Caswell <matt@openssl.org>
Mon, 4 Jun 2018 10:59:40 +0000 (11:59 +0100)
Address issue #5670

Reviewed-by: Richard Levitte <levitte@openssl.org>
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/6386)

15 files changed:
crypto/ec/ec_pmeth.c
crypto/err/openssl.ec
crypto/include/internal/sm2.h [new file with mode: 0644]
crypto/include/internal/sm2err.h [new file with mode: 0644]
crypto/sm2/sm2_crypt.c
crypto/sm2/sm2_err.c
crypto/sm2/sm2_sign.c
crypto/sm2/sm2_za.c
include/openssl/sm2.h [deleted file]
include/openssl/sm2err.h [deleted file]
test/build.info
test/sm2_crypt_internal_test.c [new file with mode: 0644]
test/sm2_sign_internal_test.c [new file with mode: 0644]
test/sm2crypttest.c [deleted file]
test/sm2sigtest.c [deleted file]

index b4105c20f2c901579e00e00663cbbd29922285c5..eefe2d0cd5fe5f274ecbedbdd7b506cf7821fe12 100644 (file)
@@ -17,7 +17,7 @@
 #include "internal/evp_int.h"
 
 #if !defined(OPENSSL_NO_SM2)
-# include <openssl/sm2.h>
+# include "internal/sm2.h"
 #endif
 
 /* EC pkey context structure */
index f45e230749989da271f80e3e4a360bb8b66a17fe..7fc5788434c13410f037b3ad125b415c7d98f19c 100644 (file)
@@ -32,7 +32,7 @@ L CMS           include/openssl/cms.h           crypto/cms/cms_err.c
 L CT            include/openssl/ct.h            crypto/ct/ct_err.c
 L ASYNC         include/openssl/async.h         crypto/async/async_err.c
 L KDF           include/openssl/kdf.h           crypto/kdf/kdf_err.c
-L SM2           include/openssl/sm2.h           crypto/sm2/sm2_err.c
+L SM2           crypto/include/internal/sm2.h   crypto/sm2/sm2_err.c
 L OSSL_STORE    include/openssl/store.h         crypto/store/store_err.c
 
 # additional header files to be scanned for function names
diff --git a/crypto/include/internal/sm2.h b/crypto/include/internal/sm2.h
new file mode 100644 (file)
index 0000000..af24c01
--- /dev/null
@@ -0,0 +1,86 @@
+/*
+ * Copyright 2017-2018 The OpenSSL Project Authors. All Rights Reserved.
+ * Copyright 2017 Ribose Inc. All Rights Reserved.
+ * Ported from Ribose contributions from Botan.
+ *
+ * Licensed under the OpenSSL license (the "License").  You may not use
+ * this file except in compliance with the License.  You can obtain a copy
+ * in the file LICENSE in the source distribution or at
+ * https://www.openssl.org/source/license.html
+ */
+
+#ifndef HEADER_SM2_H
+# define HEADER_SM2_H
+# include <openssl/opensslconf.h>
+
+# ifndef OPENSSL_NO_SM2
+
+#  ifdef __cplusplus
+extern "C" {
+#  endif
+
+#  include <openssl/ec.h>
+
+/* The default user id as specified in GM/T 0009-2012 */
+#  define SM2_DEFAULT_USERID "1234567812345678"
+
+int SM2_compute_userid_digest(uint8_t *out,
+                              const EVP_MD *digest,
+                              const char *user_id, const EC_KEY *key);
+
+/*
+ * SM2 signature operation. Computes ZA (user id digest) and then signs
+ * H(ZA || msg) using SM2
+ */
+ECDSA_SIG *SM2_do_sign(const EC_KEY *key,
+                       const EVP_MD *digest,
+                       const char *user_id, const uint8_t *msg, size_t msg_len);
+
+int SM2_do_verify(const EC_KEY *key,
+                  const EVP_MD *digest,
+                  const ECDSA_SIG *signature,
+                  const char *user_id, const uint8_t *msg, size_t msg_len);
+
+/*
+ * SM2 signature generation. Assumes input is an SM3 digest
+ */
+int SM2_sign(int type, const unsigned char *dgst, int dgstlen,
+             unsigned char *sig, unsigned int *siglen, EC_KEY *eckey);
+
+/*
+ * SM2 signature verification. Assumes input is an SM3 digest
+ */
+int SM2_verify(int type, const unsigned char *dgst, int dgstlen,
+               const unsigned char *sig, int siglen, EC_KEY *eckey);
+
+
+/*
+ * SM2 encryption
+ */
+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,
+                size_t msg_len,
+                uint8_t *ciphertext_buf, size_t *ciphertext_len);
+
+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 ERR_load_SM2_strings(void);
+
+#  ifdef __cplusplus
+}
+#  endif
+
+# endif /* OPENSSL_NO_SM2 */
+#endif
diff --git a/crypto/include/internal/sm2err.h b/crypto/include/internal/sm2err.h
new file mode 100644 (file)
index 0000000..3416c3d
--- /dev/null
@@ -0,0 +1,95 @@
+/*
+ * Generated by util/mkerr.pl DO NOT EDIT
+ * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
+ *
+ * Licensed under the OpenSSL license (the "License").  You may not use
+ * this file except in compliance with the License.  You can obtain a copy
+ * in the file LICENSE in the source distribution or at
+ * https://www.openssl.org/source/license.html
+ */
+
+#ifndef HEADER_SM2ERR_H
+# define HEADER_SM2ERR_H
+
+# ifdef  __cplusplus
+extern "C" {
+# endif
+int ERR_load_SM2_strings(void);
+# ifdef  __cplusplus
+}
+# endif
+
+/*
+ * SM2 function codes.
+ */
+# define SM2_F_PKEY_SM2_CTRL                              274
+# define SM2_F_PKEY_SM2_CTRL_STR                          275
+# define SM2_F_PKEY_SM2_KEYGEN                            276
+# define SM2_F_PKEY_SM2_PARAMGEN                          277
+# define SM2_F_PKEY_SM2_SIGN                              278
+
+/*
+ * SM2 reason codes.
+ */
+# define SM2_R_ASN1_ERROR                                 115
+# define SM2_R_ASN5_ERROR                                 1150
+# define SM2_R_BAD_SIGNATURE                              156
+# define SM2_R_BIGNUM_OUT_OF_RANGE                        144
+# define SM2_R_BUFFER_TOO_SMALL                           100
+# define SM2_R_COORDINATES_OUT_OF_RANGE                   146
+# define SM2_R_CURVE_DOES_NOT_SUPPORT_ECDH                160
+# define SM2_R_CURVE_DOES_NOT_SUPPORT_SIGNING             159
+# define SM2_R_D2I_ECPKPARAMETERS_FAILURE                 117
+# define SM2_R_DECODE_ERROR                               142
+# define SM2_R_DISCRIMINANT_IS_ZERO                       118
+# define SM2_R_EC_GROUP_NEW_BY_NAME_FAILURE               119
+# define SM2_R_FIELD_TOO_LARGE                            143
+# define SM2_R_GF2M_NOT_SUPPORTED                         147
+# define SM2_R_GROUP2PKPARAMETERS_FAILURE                 120
+# define SM2_R_I2D_ECPKPARAMETERS_FAILURE                 121
+# define SM2_R_INCOMPATIBLE_OBJECTS                       101
+# define SM2_R_INVALID_ARGUMENT                           112
+# define SM2_R_INVALID_COMPRESSED_POINT                   110
+# define SM2_R_INVALID_COMPRESSION_BIT                    109
+# define SM2_R_INVALID_CURVE                              141
+# define SM2_R_INVALID_DIGEST                             151
+# define SM2_R_INVALID_DIGEST_TYPE                        138
+# define SM2_R_INVALID_ENCODING                           102
+# define SM2_R_INVALID_FIELD                              103
+# define SM2_R_INVALID_FORM                               104
+# define SM2_R_INVALID_GROUP_ORDER                        122
+# define SM2_R_INVALID_KEY                                116
+# define SM2_R_INVALID_OUTPUT_LENGTH                      161
+# define SM2_R_INVALID_PEER_KEY                           133
+# define SM2_R_INVALID_PENTANOMIAL_BASIS                  132
+# define SM2_R_INVALID_PRIVATE_KEY                        123
+# define SM2_R_INVALID_TRINOMIAL_BASIS                    137
+# define SM2_R_KDF_PARAMETER_ERROR                        148
+# define SM2_R_KEYS_NOT_SET                               140
+# define SM2_R_MISSING_PARAMETERS                         124
+# define SM2_R_MISSING_PRIVATE_KEY                        125
+# define SM2_R_NEED_NEW_SETUP_VALUES                      157
+# define SM2_R_NOT_A_NIST_PRIME                           135
+# define SM2_R_NOT_IMPLEMENTED                            126
+# define SM2_R_NOT_INITIALIZED                            111
+# define SM2_R_NO_PARAMETERS_SET                          139
+# define SM2_R_NO_PRIVATE_VALUE                           154
+# define SM2_R_OPERATION_NOT_SUPPORTED                    152
+# define SM2_R_PASSED_NULL_PARAMETER                      134
+# define SM2_R_PEER_KEY_ERROR                             149
+# define SM2_R_PKPARAMETERS2GROUP_FAILURE                 127
+# define SM2_R_POINT_ARITHMETIC_FAILURE                   155
+# define SM2_R_POINT_AT_INFINITY                          106
+# define SM2_R_POINT_IS_NOT_ON_CURVE                      107
+# define SM2_R_RANDOM_NUMBER_GENERATION_FAILED            158
+# define SM2_R_SHARED_INFO_ERROR                          150
+# define SM2_R_SLOT_FULL                                  108
+# define SM2_R_UNDEFINED_GENERATOR                        113
+# define SM2_R_UNDEFINED_ORDER                            128
+# define SM2_R_UNKNOWN_GROUP                              129
+# define SM2_R_UNKNOWN_ORDER                              114
+# define SM2_R_UNSUPPORTED_FIELD                          131
+# define SM2_R_WRONG_CURVE_PARAMETERS                     145
+# define SM2_R_WRONG_ORDER                                130
+
+#endif
index a31c40fc7acbefbb89683249cd46dd820010cb8f..c3abd969eb71b6d78a690e8b08fa4774be7f9d0b 100644 (file)
@@ -9,7 +9,7 @@
  * https://www.openssl.org/source/license.html
  */
 
-#include <openssl/sm2.h>
+#include "internal/sm2.h"
 #include <openssl/evp.h>
 #include <openssl/bn.h>
 #include <openssl/asn1.h>
index 0c051f68b6ead011d20771f65b82c75f0349d9db..6f244a5eb055f821161674aeea41a65e39be7c86 100644 (file)
@@ -9,7 +9,7 @@
  */
 
 #include <openssl/err.h>
-#include <openssl/sm2err.h>
+#include "internal/sm2err.h"
 
 #ifndef OPENSSL_NO_ERR
 
index e12eca12fb432944540b80297c5569e910e18917..ddfd318ed904e0c764811d8840996a00a1b1aa89 100644 (file)
@@ -9,7 +9,7 @@
  * https://www.openssl.org/source/license.html
  */
 
-#include <openssl/sm2.h>
+#include "internal/sm2.h"
 #include <openssl/evp.h>
 #include <openssl/bn.h>
 #include <string.h>
index f76fe0fcadcc0c47807bdf492c12c5f5d19b96d7..cf355238b284c9ad8ca6eb37e85b098e61fc7579 100644 (file)
@@ -9,7 +9,7 @@
  * https://www.openssl.org/source/license.html
  */
 
-#include <openssl/sm2.h>
+#include "internal/sm2.h"
 #include <openssl/evp.h>
 #include <openssl/bn.h>
 #include <string.h>
diff --git a/include/openssl/sm2.h b/include/openssl/sm2.h
deleted file mode 100644 (file)
index af24c01..0000000
+++ /dev/null
@@ -1,86 +0,0 @@
-/*
- * Copyright 2017-2018 The OpenSSL Project Authors. All Rights Reserved.
- * Copyright 2017 Ribose Inc. All Rights Reserved.
- * Ported from Ribose contributions from Botan.
- *
- * Licensed under the OpenSSL license (the "License").  You may not use
- * this file except in compliance with the License.  You can obtain a copy
- * in the file LICENSE in the source distribution or at
- * https://www.openssl.org/source/license.html
- */
-
-#ifndef HEADER_SM2_H
-# define HEADER_SM2_H
-# include <openssl/opensslconf.h>
-
-# ifndef OPENSSL_NO_SM2
-
-#  ifdef __cplusplus
-extern "C" {
-#  endif
-
-#  include <openssl/ec.h>
-
-/* The default user id as specified in GM/T 0009-2012 */
-#  define SM2_DEFAULT_USERID "1234567812345678"
-
-int SM2_compute_userid_digest(uint8_t *out,
-                              const EVP_MD *digest,
-                              const char *user_id, const EC_KEY *key);
-
-/*
- * SM2 signature operation. Computes ZA (user id digest) and then signs
- * H(ZA || msg) using SM2
- */
-ECDSA_SIG *SM2_do_sign(const EC_KEY *key,
-                       const EVP_MD *digest,
-                       const char *user_id, const uint8_t *msg, size_t msg_len);
-
-int SM2_do_verify(const EC_KEY *key,
-                  const EVP_MD *digest,
-                  const ECDSA_SIG *signature,
-                  const char *user_id, const uint8_t *msg, size_t msg_len);
-
-/*
- * SM2 signature generation. Assumes input is an SM3 digest
- */
-int SM2_sign(int type, const unsigned char *dgst, int dgstlen,
-             unsigned char *sig, unsigned int *siglen, EC_KEY *eckey);
-
-/*
- * SM2 signature verification. Assumes input is an SM3 digest
- */
-int SM2_verify(int type, const unsigned char *dgst, int dgstlen,
-               const unsigned char *sig, int siglen, EC_KEY *eckey);
-
-
-/*
- * SM2 encryption
- */
-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,
-                size_t msg_len,
-                uint8_t *ciphertext_buf, size_t *ciphertext_len);
-
-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 ERR_load_SM2_strings(void);
-
-#  ifdef __cplusplus
-}
-#  endif
-
-# endif /* OPENSSL_NO_SM2 */
-#endif
diff --git a/include/openssl/sm2err.h b/include/openssl/sm2err.h
deleted file mode 100644 (file)
index 3416c3d..0000000
+++ /dev/null
@@ -1,95 +0,0 @@
-/*
- * Generated by util/mkerr.pl DO NOT EDIT
- * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
- *
- * Licensed under the OpenSSL license (the "License").  You may not use
- * this file except in compliance with the License.  You can obtain a copy
- * in the file LICENSE in the source distribution or at
- * https://www.openssl.org/source/license.html
- */
-
-#ifndef HEADER_SM2ERR_H
-# define HEADER_SM2ERR_H
-
-# ifdef  __cplusplus
-extern "C" {
-# endif
-int ERR_load_SM2_strings(void);
-# ifdef  __cplusplus
-}
-# endif
-
-/*
- * SM2 function codes.
- */
-# define SM2_F_PKEY_SM2_CTRL                              274
-# define SM2_F_PKEY_SM2_CTRL_STR                          275
-# define SM2_F_PKEY_SM2_KEYGEN                            276
-# define SM2_F_PKEY_SM2_PARAMGEN                          277
-# define SM2_F_PKEY_SM2_SIGN                              278
-
-/*
- * SM2 reason codes.
- */
-# define SM2_R_ASN1_ERROR                                 115
-# define SM2_R_ASN5_ERROR                                 1150
-# define SM2_R_BAD_SIGNATURE                              156
-# define SM2_R_BIGNUM_OUT_OF_RANGE                        144
-# define SM2_R_BUFFER_TOO_SMALL                           100
-# define SM2_R_COORDINATES_OUT_OF_RANGE                   146
-# define SM2_R_CURVE_DOES_NOT_SUPPORT_ECDH                160
-# define SM2_R_CURVE_DOES_NOT_SUPPORT_SIGNING             159
-# define SM2_R_D2I_ECPKPARAMETERS_FAILURE                 117
-# define SM2_R_DECODE_ERROR                               142
-# define SM2_R_DISCRIMINANT_IS_ZERO                       118
-# define SM2_R_EC_GROUP_NEW_BY_NAME_FAILURE               119
-# define SM2_R_FIELD_TOO_LARGE                            143
-# define SM2_R_GF2M_NOT_SUPPORTED                         147
-# define SM2_R_GROUP2PKPARAMETERS_FAILURE                 120
-# define SM2_R_I2D_ECPKPARAMETERS_FAILURE                 121
-# define SM2_R_INCOMPATIBLE_OBJECTS                       101
-# define SM2_R_INVALID_ARGUMENT                           112
-# define SM2_R_INVALID_COMPRESSED_POINT                   110
-# define SM2_R_INVALID_COMPRESSION_BIT                    109
-# define SM2_R_INVALID_CURVE                              141
-# define SM2_R_INVALID_DIGEST                             151
-# define SM2_R_INVALID_DIGEST_TYPE                        138
-# define SM2_R_INVALID_ENCODING                           102
-# define SM2_R_INVALID_FIELD                              103
-# define SM2_R_INVALID_FORM                               104
-# define SM2_R_INVALID_GROUP_ORDER                        122
-# define SM2_R_INVALID_KEY                                116
-# define SM2_R_INVALID_OUTPUT_LENGTH                      161
-# define SM2_R_INVALID_PEER_KEY                           133
-# define SM2_R_INVALID_PENTANOMIAL_BASIS                  132
-# define SM2_R_INVALID_PRIVATE_KEY                        123
-# define SM2_R_INVALID_TRINOMIAL_BASIS                    137
-# define SM2_R_KDF_PARAMETER_ERROR                        148
-# define SM2_R_KEYS_NOT_SET                               140
-# define SM2_R_MISSING_PARAMETERS                         124
-# define SM2_R_MISSING_PRIVATE_KEY                        125
-# define SM2_R_NEED_NEW_SETUP_VALUES                      157
-# define SM2_R_NOT_A_NIST_PRIME                           135
-# define SM2_R_NOT_IMPLEMENTED                            126
-# define SM2_R_NOT_INITIALIZED                            111
-# define SM2_R_NO_PARAMETERS_SET                          139
-# define SM2_R_NO_PRIVATE_VALUE                           154
-# define SM2_R_OPERATION_NOT_SUPPORTED                    152
-# define SM2_R_PASSED_NULL_PARAMETER                      134
-# define SM2_R_PEER_KEY_ERROR                             149
-# define SM2_R_PKPARAMETERS2GROUP_FAILURE                 127
-# define SM2_R_POINT_ARITHMETIC_FAILURE                   155
-# define SM2_R_POINT_AT_INFINITY                          106
-# define SM2_R_POINT_IS_NOT_ON_CURVE                      107
-# define SM2_R_RANDOM_NUMBER_GENERATION_FAILED            158
-# define SM2_R_SHARED_INFO_ERROR                          150
-# define SM2_R_SLOT_FULL                                  108
-# define SM2_R_UNDEFINED_GENERATOR                        113
-# define SM2_R_UNDEFINED_ORDER                            128
-# define SM2_R_UNKNOWN_GROUP                              129
-# define SM2_R_UNKNOWN_ORDER                              114
-# define SM2_R_UNSUPPORTED_FIELD                          131
-# define SM2_R_WRONG_CURVE_PARAMETERS                     145
-# define SM2_R_WRONG_ORDER                                130
-
-#endif
index 000153d510bccc5bd4bd3b44171fc5591a9507b8..ee302e233cf05b4e7a87e5618668eafc71c542b8 100644 (file)
@@ -28,7 +28,6 @@ INCLUDE_MAIN___test_libtestutil_OLB = /INCLUDE=MAIN
           aborttest test_test \
           sanitytest exdatatest bntest \
           ectest ecstresstest ecdsatest gmdifftest pbelutest ideatest \
-          sm2sigtest sm2crypttest \
           md2test \
           hmactest \
           rc2test rc4test rc5test \
@@ -89,14 +88,6 @@ INCLUDE_MAIN___test_libtestutil_OLB = /INCLUDE=MAIN
   INCLUDE[ecdsatest]=../include
   DEPEND[ecdsatest]=../libcrypto libtestutil.a
 
-  SOURCE[sm2sigtest]=sm2sigtest.c
-  INCLUDE[sm2sigtest]=../include
-  DEPEND[sm2sigtest]=../libcrypto libtestutil.a
-
-  SOURCE[sm2crypttest]=sm2crypttest.c
-  INCLUDE[sm2crypttest]=../include
-  DEPEND[sm2crypttest]=../libcrypto libtestutil.a
-
   SOURCE[gmdifftest]=gmdifftest.c
   INCLUDE[gmdifftest]=../include
   DEPEND[gmdifftest]=../libcrypto libtestutil.a
@@ -448,6 +439,9 @@ INCLUDE_MAIN___test_libtestutil_OLB = /INCLUDE=MAIN
     IF[{- !$disabled{siphash} -}]
       PROGRAMS_NO_INST=siphash_internal_test
     ENDIF
+    IF[{- !$disabled{sm2} -}]
+      PROGRAMS_NO_INST=sm2_crypt_internal_test sm2_sign_internal_test
+    ENDIF
     IF[{- !$disabled{sm4} -}]
       PROGRAMS_NO_INST=sm4_internal_test
     ENDIF
@@ -491,6 +485,14 @@ INCLUDE_MAIN___test_libtestutil_OLB = /INCLUDE=MAIN
     INCLUDE[siphash_internal_test]=.. ../include ../crypto/include
     DEPEND[siphash_internal_test]=../libcrypto.a libtestutil.a
 
+    SOURCE[sm2_sign_internal_test]=sm2_sign_internal_test.c
+    INCLUDE[sm2_sign_internal_test]=../include ../crypto/include
+    DEPEND[sm2_sign_internal_test]=../libcrypto.a libtestutil.a
+
+    SOURCE[sm2_crypt_internal_test]=sm2_crypt_internal_test.c
+    INCLUDE[sm2_crypt_internal_test]=../include ../crypto/include
+    DEPEND[sm2_crypt_internal_test]=../libcrypto.a libtestutil.a
+
     SOURCE[sm4_internal_test]=sm4_internal_test.c
     INCLUDE[sm4_internal_test]=.. ../include ../crypto/include
     DEPEND[sm4_internal_test]=../libcrypto.a libtestutil.a
diff --git a/test/sm2_crypt_internal_test.c b/test/sm2_crypt_internal_test.c
new file mode 100644 (file)
index 0000000..5325fa3
--- /dev/null
@@ -0,0 +1,254 @@
+/*
+ * Copyright 2017-2018 The OpenSSL Project Authors. All Rights Reserved.
+ *
+ * Licensed under the OpenSSL license (the "License").  You may not use
+ * this file except in compliance with the License.  You can obtain a copy
+ * in the file LICENSE in the source distribution or at
+ * https://www.openssl.org/source/license.html
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <openssl/bio.h>
+#include <openssl/evp.h>
+#include <openssl/bn.h>
+#include <openssl/crypto.h>
+#include <openssl/err.h>
+#include <openssl/rand.h>
+#include "testutil.h"
+
+#ifndef OPENSSL_NO_SM2
+
+# include "internal/sm2.h"
+
+static RAND_METHOD fake_rand;
+static const RAND_METHOD *saved_rand;
+
+static uint8_t *fake_rand_bytes = NULL;
+static size_t fake_rand_bytes_offset = 0;
+
+static int get_faked_bytes(unsigned char *buf, int num)
+{
+    int i;
+
+    if (fake_rand_bytes == NULL)
+        return saved_rand->bytes(buf, num);
+
+    for (i = 0; i != num; ++i)
+        buf[i] = fake_rand_bytes[fake_rand_bytes_offset + i];
+    fake_rand_bytes_offset += num;
+    return 1;
+}
+
+static int start_fake_rand(const char *hex_bytes)
+{
+    /* save old rand method */
+    if (!TEST_ptr(saved_rand = RAND_get_rand_method()))
+        return 0;
+
+    fake_rand = *saved_rand;
+    /* use own random function */
+    fake_rand.bytes = get_faked_bytes;
+
+    fake_rand_bytes = OPENSSL_hexstr2buf(hex_bytes, NULL);
+    fake_rand_bytes_offset = 0;
+
+    /* set new RAND_METHOD */
+    if (!TEST_true(RAND_set_rand_method(&fake_rand)))
+        return 0;
+    return 1;
+}
+
+static int restore_rand(void)
+{
+    OPENSSL_free(fake_rand_bytes);
+    fake_rand_bytes = NULL;
+    fake_rand_bytes_offset = 0;
+    if (!TEST_true(RAND_set_rand_method(saved_rand)))
+        return 0;
+    return 1;
+}
+
+static EC_GROUP *create_EC_group(const char *p_hex, const char *a_hex,
+                                 const char *b_hex, const char *x_hex,
+                                 const char *y_hex, const char *order_hex,
+                                 const char *cof_hex)
+{
+    BIGNUM *p = NULL;
+    BIGNUM *a = NULL;
+    BIGNUM *b = NULL;
+    BIGNUM *g_x = NULL;
+    BIGNUM *g_y = NULL;
+    BIGNUM *order = NULL;
+    BIGNUM *cof = NULL;
+    EC_POINT *generator = NULL;
+    EC_GROUP *group = NULL;
+
+    BN_hex2bn(&p, p_hex);
+    BN_hex2bn(&a, a_hex);
+    BN_hex2bn(&b, b_hex);
+
+    group = EC_GROUP_new_curve_GFp(p, a, b, NULL);
+    BN_free(p);
+    BN_free(a);
+    BN_free(b);
+
+    if (group == NULL)
+        return NULL;
+
+    generator = EC_POINT_new(group);
+    if (generator == NULL)
+        return NULL;
+
+    BN_hex2bn(&g_x, x_hex);
+    BN_hex2bn(&g_y, y_hex);
+
+    if (EC_POINT_set_affine_coordinates_GFp(group, generator, g_x, g_y, NULL) ==
+        0)
+        return NULL;
+
+    BN_free(g_x);
+    BN_free(g_y);
+
+    BN_hex2bn(&order, order_hex);
+    BN_hex2bn(&cof, cof_hex);
+
+    if (EC_GROUP_set_generator(group, generator, order, cof) == 0)
+        return NULL;
+
+    EC_POINT_free(generator);
+    BN_free(order);
+    BN_free(cof);
+
+    return group;
+}
+
+static int test_sm2(const EC_GROUP *group,
+                    const EVP_MD *digest,
+                    const char *privkey_hex,
+                    const char *message,
+                    const char *k_hex, const char *ctext_hex)
+{
+    const size_t msg_len = strlen(message);
+
+    BIGNUM *priv = NULL;
+    EC_KEY *key = NULL;
+    EC_POINT *pt = NULL;
+    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;
+
+    int rc = 0;
+
+    BN_hex2bn(&priv, privkey_hex);
+
+    key = EC_KEY_new();
+    EC_KEY_set_group(key, group);
+    EC_KEY_set_private_key(key, priv);
+
+    pt = EC_POINT_new(group);
+    EC_POINT_mul(group, pt, priv, NULL, NULL, NULL);
+
+    EC_KEY_set_public_key(key, pt);
+    BN_free(priv);
+    EC_POINT_free(pt);
+
+    ctext_len = SM2_ciphertext_size(key, digest, msg_len);
+    ctext = OPENSSL_zalloc(ctext_len);
+    if (ctext == NULL)
+        goto done;
+
+    start_fake_rand(k_hex);
+    rc = SM2_encrypt(key, digest,
+                     (const uint8_t *)message, msg_len, ctext, &ctext_len);
+    restore_rand();
+
+    TEST_mem_eq(ctext, ctext_len, expected, ctext_len);
+    if (rc == 0)
+        goto done;
+
+    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);
+
+    TEST_int_eq(recovered_len, msg_len);
+    TEST_mem_eq(recovered, recovered_len, message, msg_len);
+    if (rc == 0)
+        return 0;
+
+    rc = 1;
+ done:
+
+    OPENSSL_free(ctext);
+    OPENSSL_free(recovered);
+    OPENSSL_free(expected);
+    EC_KEY_free(key);
+    return rc;
+}
+
+static int sm2_crypt_test(void)
+{
+    int rc;
+    EC_GROUP *test_group =
+        create_EC_group
+        ("8542D69E4C044F18E8B92435BF6FF7DE457283915C45517D722EDB8B08F1DFC3",
+         "787968B4FA32C3FD2417842E73BBFEFF2F3C848B6831D7E0EC65228B3937E498",
+         "63E4C6D3B23B0C849CF84241484BFE48F61D59A5B16BA06E6E12D1DA27C5249A",
+         "421DEBD61B62EAB6746434EBC3CC315E32220B3BADD50BDC4C4E6C147FEDD43D",
+         "0680512BCBB42C07D47349D2153B70C4E5D7FDFCBFA36EA1A85841B9E46E09A2",
+         "8542D69E4C044F18E8B92435BF6FF7DD297720630485628D5AE74EE7C32E79B7",
+         "1");
+
+    if (test_group == NULL)
+        return 0;
+
+    rc = test_sm2(test_group,
+                  EVP_sm3(),
+                  "1649AB77A00637BD5E2EFE283FBF353534AA7F7CB89463F208DDBC2920BB0DA0",
+                  "encryption standard",
+                  "004C62EEFD6ECFC2B95B92FD6C3D9575148AFA17425546D49018E5388D49DD7B4F",
+                  "307B0220245C26FB68B1DDDDB12C4B6BF9F2B6D5FE60A383B0D18D1C4144ABF1"
+                  "7F6252E7022076CB9264C2A7E88E52B19903FDC47378F605E36811F5C07423A2"
+                  "4B84400F01B804209C3D7360C30156FAB7C80A0276712DA9D8094A634B766D3A"
+                  "285E07480653426D0413650053A89B41C418B0C3AAD00D886C00286467");
+
+    if (rc == 0)
+        return 0;
+
+    /* Same test as above except using SHA-256 instead of SM3 */
+    rc = test_sm2(test_group,
+                  EVP_sha256(),
+                  "1649AB77A00637BD5E2EFE283FBF353534AA7F7CB89463F208DDBC2920BB0DA0",
+                  "encryption standard",
+                  "004C62EEFD6ECFC2B95B92FD6C3D9575148AFA17425546D49018E5388D49DD7B4F",
+                  "307B0220245C26FB68B1DDDDB12C4B6BF9F2B6D5FE60A383B0D18D1C4144ABF17F6252E7022076CB9264C2A7E88E52B19903FDC47378F605E36811F5C07423A24B84400F01B80420BE89139D07853100EFA763F60CBE30099EA3DF7F8F364F9D10A5E988E3C5AAFC0413229E6C9AEE2BB92CAD649FE2C035689785DA33");
+    if (rc == 0)
+        return 0;
+
+    EC_GROUP_free(test_group);
+
+    return 1;
+}
+
+#endif
+
+int setup_tests(void)
+{
+#ifdef OPENSSL_NO_SM2
+    TEST_note("SM2 is disabled.");
+#else
+    ADD_TEST(sm2_crypt_test);
+#endif
+    return 1;
+}
diff --git a/test/sm2_sign_internal_test.c b/test/sm2_sign_internal_test.c
new file mode 100644 (file)
index 0000000..87f0d73
--- /dev/null
@@ -0,0 +1,238 @@
+/*
+ * Copyright 2017-2018 The OpenSSL Project Authors. All Rights Reserved.
+ * Copyright 2017 Ribose Inc. All Rights Reserved.
+ *
+ * Licensed under the OpenSSL license (the "License").  You may not use
+ * this file except in compliance with the License.  You can obtain a copy
+ * in the file LICENSE in the source distribution or at
+ * https://www.openssl.org/source/license.html
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <openssl/bio.h>
+#include <openssl/evp.h>
+#include <openssl/bn.h>
+#include <openssl/crypto.h>
+#include <openssl/err.h>
+#include <openssl/rand.h>
+#include "testutil.h"
+
+#ifndef OPENSSL_NO_SM2
+
+# include "internal/sm2.h"
+
+static RAND_METHOD fake_rand;
+static const RAND_METHOD *saved_rand;
+
+static uint8_t *fake_rand_bytes = NULL;
+static size_t fake_rand_bytes_offset = 0;
+
+static int get_faked_bytes(unsigned char *buf, int num)
+{
+    int i;
+
+    if (fake_rand_bytes == NULL)
+        return saved_rand->bytes(buf, num);
+
+    for (i = 0; i != num; ++i)
+        buf[i] = fake_rand_bytes[fake_rand_bytes_offset + i];
+    fake_rand_bytes_offset += num;
+    return 1;
+}
+
+static int start_fake_rand(const char *hex_bytes)
+{
+    /* save old rand method */
+    if (!TEST_ptr(saved_rand = RAND_get_rand_method()))
+        return 0;
+
+    fake_rand = *saved_rand;
+    /* use own random function */
+    fake_rand.bytes = get_faked_bytes;
+
+    fake_rand_bytes = OPENSSL_hexstr2buf(hex_bytes, NULL);
+    fake_rand_bytes_offset = 0;
+
+    /* set new RAND_METHOD */
+    if (!TEST_true(RAND_set_rand_method(&fake_rand)))
+        return 0;
+    return 1;
+}
+
+static int restore_rand(void)
+{
+    OPENSSL_free(fake_rand_bytes);
+    fake_rand_bytes = NULL;
+    fake_rand_bytes_offset = 0;
+    if (!TEST_true(RAND_set_rand_method(saved_rand)))
+        return 0;
+    return 1;
+}
+
+static EC_GROUP *create_EC_group(const char *p_hex, const char *a_hex,
+                                 const char *b_hex, const char *x_hex,
+                                 const char *y_hex, const char *order_hex,
+                                 const char *cof_hex)
+{
+    BIGNUM *p = NULL;
+    BIGNUM *a = NULL;
+    BIGNUM *b = NULL;
+    BIGNUM *g_x = NULL;
+    BIGNUM *g_y = NULL;
+    BIGNUM *order = NULL;
+    BIGNUM *cof = NULL;
+    EC_POINT *generator = NULL;
+    EC_GROUP *group = NULL;
+
+    BN_hex2bn(&p, p_hex);
+    BN_hex2bn(&a, a_hex);
+    BN_hex2bn(&b, b_hex);
+
+    group = EC_GROUP_new_curve_GFp(p, a, b, NULL);
+    BN_free(p);
+    BN_free(a);
+    BN_free(b);
+
+    if (group == NULL)
+        return NULL;
+
+    generator = EC_POINT_new(group);
+    if (generator == NULL)
+        return NULL;
+
+    BN_hex2bn(&g_x, x_hex);
+    BN_hex2bn(&g_y, y_hex);
+
+    if (EC_POINT_set_affine_coordinates_GFp(group, generator, g_x, g_y, NULL) ==
+        0)
+        return NULL;
+
+    BN_free(g_x);
+    BN_free(g_y);
+
+    BN_hex2bn(&order, order_hex);
+    BN_hex2bn(&cof, cof_hex);
+
+    if (EC_GROUP_set_generator(group, generator, order, cof) == 0)
+        return NULL;
+
+    EC_POINT_free(generator);
+    BN_free(order);
+    BN_free(cof);
+
+    return group;
+}
+
+
+static int test_sm2(const EC_GROUP *group,
+                    const char *userid,
+                    const char *privkey_hex,
+                    const char *message,
+                    const char *k_hex, const char *r_hex, const char *s_hex)
+{
+    const size_t msg_len = strlen(message);
+    int ok = -1;
+    BIGNUM *priv = NULL;
+    EC_POINT *pt = NULL;
+    EC_KEY *key = NULL;
+    ECDSA_SIG *sig = NULL;
+    const BIGNUM *sig_r = NULL;
+    const BIGNUM *sig_s = NULL;
+    BIGNUM *r = NULL;
+    BIGNUM *s = NULL;
+
+    BN_hex2bn(&priv, privkey_hex);
+
+    key = EC_KEY_new();
+    EC_KEY_set_group(key, group);
+    EC_KEY_set_private_key(key, priv);
+
+    pt = EC_POINT_new(group);
+    EC_POINT_mul(group, pt, priv, NULL, NULL, NULL);
+    EC_KEY_set_public_key(key, pt);
+
+    start_fake_rand(k_hex);
+    sig = SM2_do_sign(key, EVP_sm3(), userid, (const uint8_t *)message, msg_len);
+    restore_rand();
+
+    if (sig == NULL)
+        return 0;
+
+    ECDSA_SIG_get0(sig, &sig_r, &sig_s);
+
+    BN_hex2bn(&r, r_hex);
+    BN_hex2bn(&s, s_hex);
+
+    if (BN_cmp(r, sig_r) != 0) {
+        printf("Signature R mismatch: ");
+        BN_print_fp(stdout, r);
+        printf(" != ");
+        BN_print_fp(stdout, sig_r);
+        printf("\n");
+        ok = 0;
+    }
+    if (BN_cmp(s, sig_s) != 0) {
+        printf("Signature S mismatch: ");
+        BN_print_fp(stdout, s);
+        printf(" != ");
+        BN_print_fp(stdout, sig_s);
+        printf("\n");
+        ok = 0;
+    }
+
+    ok = SM2_do_verify(key, EVP_sm3(), sig, userid, (const uint8_t *)message, msg_len);
+
+    ECDSA_SIG_free(sig);
+    EC_POINT_free(pt);
+    EC_KEY_free(key);
+    BN_free(priv);
+    BN_free(r);
+    BN_free(s);
+
+    return ok;
+}
+
+static int sm2_sig_test(void)
+{
+    int rc = 0;
+    /* From draft-shen-sm2-ecdsa-02 */
+    EC_GROUP *test_group =
+        create_EC_group
+        ("8542D69E4C044F18E8B92435BF6FF7DE457283915C45517D722EDB8B08F1DFC3",
+         "787968B4FA32C3FD2417842E73BBFEFF2F3C848B6831D7E0EC65228B3937E498",
+         "63E4C6D3B23B0C849CF84241484BFE48F61D59A5B16BA06E6E12D1DA27C5249A",
+         "421DEBD61B62EAB6746434EBC3CC315E32220B3BADD50BDC4C4E6C147FEDD43D",
+         "0680512BCBB42C07D47349D2153B70C4E5D7FDFCBFA36EA1A85841B9E46E09A2",
+         "8542D69E4C044F18E8B92435BF6FF7DD297720630485628D5AE74EE7C32E79B7",
+         "1");
+
+    if (test_group == NULL)
+        return 0;
+
+    rc = test_sm2(test_group,
+                    "ALICE123@YAHOO.COM",
+                    "128B2FA8BD433C6C068C8D803DFF79792A519A55171B1B650C23661D15897263",
+                    "message digest",
+                    "006CB28D99385C175C94F94E934817663FC176D925DD72B727260DBAAE1FB2F96F",
+                    "40F1EC59F793D9F49E09DCEF49130D4194F79FB1EED2CAA55BACDB49C4E755D1",
+                    "6FC6DAC32C5D5CF10C77DFB20F7C2EB667A457872FB09EC56327A67EC7DEEBE7");
+
+    EC_GROUP_free(test_group);
+
+    return rc;
+}
+
+#endif
+
+int setup_tests(void)
+{
+#ifdef OPENSSL_NO_SM2
+    TEST_note("SM2 is disabled.");
+#else
+    ADD_TEST(sm2_sig_test);
+#endif
+    return 1;
+}
diff --git a/test/sm2crypttest.c b/test/sm2crypttest.c
deleted file mode 100644 (file)
index cd72cb0..0000000
+++ /dev/null
@@ -1,254 +0,0 @@
-/*
- * Copyright 2017-2018 The OpenSSL Project Authors. All Rights Reserved.
- *
- * Licensed under the OpenSSL license (the "License").  You may not use
- * this file except in compliance with the License.  You can obtain a copy
- * in the file LICENSE in the source distribution or at
- * https://www.openssl.org/source/license.html
- */
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-#include <openssl/bio.h>
-#include <openssl/evp.h>
-#include <openssl/bn.h>
-#include <openssl/crypto.h>
-#include <openssl/err.h>
-#include <openssl/rand.h>
-#include "testutil.h"
-
-#ifndef OPENSSL_NO_SM2
-
-# include <openssl/sm2.h>
-
-static RAND_METHOD fake_rand;
-static const RAND_METHOD *saved_rand;
-
-static uint8_t *fake_rand_bytes = NULL;
-static size_t fake_rand_bytes_offset = 0;
-
-static int get_faked_bytes(unsigned char *buf, int num)
-{
-    int i;
-
-    if (fake_rand_bytes == NULL)
-        return saved_rand->bytes(buf, num);
-
-    for (i = 0; i != num; ++i)
-        buf[i] = fake_rand_bytes[fake_rand_bytes_offset + i];
-    fake_rand_bytes_offset += num;
-    return 1;
-}
-
-static int start_fake_rand(const char *hex_bytes)
-{
-    /* save old rand method */
-    if (!TEST_ptr(saved_rand = RAND_get_rand_method()))
-        return 0;
-
-    fake_rand = *saved_rand;
-    /* use own random function */
-    fake_rand.bytes = get_faked_bytes;
-
-    fake_rand_bytes = OPENSSL_hexstr2buf(hex_bytes, NULL);
-    fake_rand_bytes_offset = 0;
-
-    /* set new RAND_METHOD */
-    if (!TEST_true(RAND_set_rand_method(&fake_rand)))
-        return 0;
-    return 1;
-}
-
-static int restore_rand(void)
-{
-    OPENSSL_free(fake_rand_bytes);
-    fake_rand_bytes = NULL;
-    fake_rand_bytes_offset = 0;
-    if (!TEST_true(RAND_set_rand_method(saved_rand)))
-        return 0;
-    return 1;
-}
-
-static EC_GROUP *create_EC_group(const char *p_hex, const char *a_hex,
-                                 const char *b_hex, const char *x_hex,
-                                 const char *y_hex, const char *order_hex,
-                                 const char *cof_hex)
-{
-    BIGNUM *p = NULL;
-    BIGNUM *a = NULL;
-    BIGNUM *b = NULL;
-    BIGNUM *g_x = NULL;
-    BIGNUM *g_y = NULL;
-    BIGNUM *order = NULL;
-    BIGNUM *cof = NULL;
-    EC_POINT *generator = NULL;
-    EC_GROUP *group = NULL;
-
-    BN_hex2bn(&p, p_hex);
-    BN_hex2bn(&a, a_hex);
-    BN_hex2bn(&b, b_hex);
-
-    group = EC_GROUP_new_curve_GFp(p, a, b, NULL);
-    BN_free(p);
-    BN_free(a);
-    BN_free(b);
-
-    if (group == NULL)
-        return NULL;
-
-    generator = EC_POINT_new(group);
-    if (generator == NULL)
-        return NULL;
-
-    BN_hex2bn(&g_x, x_hex);
-    BN_hex2bn(&g_y, y_hex);
-
-    if (EC_POINT_set_affine_coordinates_GFp(group, generator, g_x, g_y, NULL) ==
-        0)
-        return NULL;
-
-    BN_free(g_x);
-    BN_free(g_y);
-
-    BN_hex2bn(&order, order_hex);
-    BN_hex2bn(&cof, cof_hex);
-
-    if (EC_GROUP_set_generator(group, generator, order, cof) == 0)
-        return NULL;
-
-    EC_POINT_free(generator);
-    BN_free(order);
-    BN_free(cof);
-
-    return group;
-}
-
-static int test_sm2(const EC_GROUP *group,
-                    const EVP_MD *digest,
-                    const char *privkey_hex,
-                    const char *message,
-                    const char *k_hex, const char *ctext_hex)
-{
-    const size_t msg_len = strlen(message);
-
-    BIGNUM *priv = NULL;
-    EC_KEY *key = NULL;
-    EC_POINT *pt = NULL;
-    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;
-
-    int rc = 0;
-
-    BN_hex2bn(&priv, privkey_hex);
-
-    key = EC_KEY_new();
-    EC_KEY_set_group(key, group);
-    EC_KEY_set_private_key(key, priv);
-
-    pt = EC_POINT_new(group);
-    EC_POINT_mul(group, pt, priv, NULL, NULL, NULL);
-
-    EC_KEY_set_public_key(key, pt);
-    BN_free(priv);
-    EC_POINT_free(pt);
-
-    ctext_len = SM2_ciphertext_size(key, digest, msg_len);
-    ctext = OPENSSL_zalloc(ctext_len);
-    if (ctext == NULL)
-        goto done;
-
-    start_fake_rand(k_hex);
-    rc = SM2_encrypt(key, digest,
-                     (const uint8_t *)message, msg_len, ctext, &ctext_len);
-    restore_rand();
-
-    TEST_mem_eq(ctext, ctext_len, expected, ctext_len);
-    if (rc == 0)
-        goto done;
-
-    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);
-
-    TEST_int_eq(recovered_len, msg_len);
-    TEST_mem_eq(recovered, recovered_len, message, msg_len);
-    if (rc == 0)
-        return 0;
-
-    rc = 1;
- done:
-
-    OPENSSL_free(ctext);
-    OPENSSL_free(recovered);
-    OPENSSL_free(expected);
-    EC_KEY_free(key);
-    return rc;
-}
-
-static int sm2_crypt_test(void)
-{
-    int rc;
-    EC_GROUP *test_group =
-        create_EC_group
-        ("8542D69E4C044F18E8B92435BF6FF7DE457283915C45517D722EDB8B08F1DFC3",
-         "787968B4FA32C3FD2417842E73BBFEFF2F3C848B6831D7E0EC65228B3937E498",
-         "63E4C6D3B23B0C849CF84241484BFE48F61D59A5B16BA06E6E12D1DA27C5249A",
-         "421DEBD61B62EAB6746434EBC3CC315E32220B3BADD50BDC4C4E6C147FEDD43D",
-         "0680512BCBB42C07D47349D2153B70C4E5D7FDFCBFA36EA1A85841B9E46E09A2",
-         "8542D69E4C044F18E8B92435BF6FF7DD297720630485628D5AE74EE7C32E79B7",
-         "1");
-
-    if (test_group == NULL)
-        return 0;
-
-    rc = test_sm2(test_group,
-                  EVP_sm3(),
-                  "1649AB77A00637BD5E2EFE283FBF353534AA7F7CB89463F208DDBC2920BB0DA0",
-                  "encryption standard",
-                  "004C62EEFD6ECFC2B95B92FD6C3D9575148AFA17425546D49018E5388D49DD7B4F",
-                  "307B0220245C26FB68B1DDDDB12C4B6BF9F2B6D5FE60A383B0D18D1C4144ABF1"
-                  "7F6252E7022076CB9264C2A7E88E52B19903FDC47378F605E36811F5C07423A2"
-                  "4B84400F01B804209C3D7360C30156FAB7C80A0276712DA9D8094A634B766D3A"
-                  "285E07480653426D0413650053A89B41C418B0C3AAD00D886C00286467");
-
-    if (rc == 0)
-        return 0;
-
-    /* Same test as above except using SHA-256 instead of SM3 */
-    rc = test_sm2(test_group,
-                  EVP_sha256(),
-                  "1649AB77A00637BD5E2EFE283FBF353534AA7F7CB89463F208DDBC2920BB0DA0",
-                  "encryption standard",
-                  "004C62EEFD6ECFC2B95B92FD6C3D9575148AFA17425546D49018E5388D49DD7B4F",
-                  "307B0220245C26FB68B1DDDDB12C4B6BF9F2B6D5FE60A383B0D18D1C4144ABF17F6252E7022076CB9264C2A7E88E52B19903FDC47378F605E36811F5C07423A24B84400F01B80420BE89139D07853100EFA763F60CBE30099EA3DF7F8F364F9D10A5E988E3C5AAFC0413229E6C9AEE2BB92CAD649FE2C035689785DA33");
-    if (rc == 0)
-        return 0;
-
-    EC_GROUP_free(test_group);
-
-    return 1;
-}
-
-#endif
-
-int setup_tests(void)
-{
-#ifdef OPENSSL_NO_SM2
-    TEST_note("SM2 is disabled.");
-#else
-    ADD_TEST(sm2_crypt_test);
-#endif
-    return 1;
-}
diff --git a/test/sm2sigtest.c b/test/sm2sigtest.c
deleted file mode 100644 (file)
index 33be783..0000000
+++ /dev/null
@@ -1,238 +0,0 @@
-/*
- * Copyright 2017-2018 The OpenSSL Project Authors. All Rights Reserved.
- * Copyright 2017 Ribose Inc. All Rights Reserved.
- *
- * Licensed under the OpenSSL license (the "License").  You may not use
- * this file except in compliance with the License.  You can obtain a copy
- * in the file LICENSE in the source distribution or at
- * https://www.openssl.org/source/license.html
- */
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-#include <openssl/bio.h>
-#include <openssl/evp.h>
-#include <openssl/bn.h>
-#include <openssl/crypto.h>
-#include <openssl/err.h>
-#include <openssl/rand.h>
-#include "testutil.h"
-
-#ifndef OPENSSL_NO_SM2
-
-# include <openssl/sm2.h>
-
-static RAND_METHOD fake_rand;
-static const RAND_METHOD *saved_rand;
-
-static uint8_t *fake_rand_bytes = NULL;
-static size_t fake_rand_bytes_offset = 0;
-
-static int get_faked_bytes(unsigned char *buf, int num)
-{
-    int i;
-
-    if (fake_rand_bytes == NULL)
-        return saved_rand->bytes(buf, num);
-
-    for (i = 0; i != num; ++i)
-        buf[i] = fake_rand_bytes[fake_rand_bytes_offset + i];
-    fake_rand_bytes_offset += num;
-    return 1;
-}
-
-static int start_fake_rand(const char *hex_bytes)
-{
-    /* save old rand method */
-    if (!TEST_ptr(saved_rand = RAND_get_rand_method()))
-        return 0;
-
-    fake_rand = *saved_rand;
-    /* use own random function */
-    fake_rand.bytes = get_faked_bytes;
-
-    fake_rand_bytes = OPENSSL_hexstr2buf(hex_bytes, NULL);
-    fake_rand_bytes_offset = 0;
-
-    /* set new RAND_METHOD */
-    if (!TEST_true(RAND_set_rand_method(&fake_rand)))
-        return 0;
-    return 1;
-}
-
-static int restore_rand(void)
-{
-    OPENSSL_free(fake_rand_bytes);
-    fake_rand_bytes = NULL;
-    fake_rand_bytes_offset = 0;
-    if (!TEST_true(RAND_set_rand_method(saved_rand)))
-        return 0;
-    return 1;
-}
-
-static EC_GROUP *create_EC_group(const char *p_hex, const char *a_hex,
-                                 const char *b_hex, const char *x_hex,
-                                 const char *y_hex, const char *order_hex,
-                                 const char *cof_hex)
-{
-    BIGNUM *p = NULL;
-    BIGNUM *a = NULL;
-    BIGNUM *b = NULL;
-    BIGNUM *g_x = NULL;
-    BIGNUM *g_y = NULL;
-    BIGNUM *order = NULL;
-    BIGNUM *cof = NULL;
-    EC_POINT *generator = NULL;
-    EC_GROUP *group = NULL;
-
-    BN_hex2bn(&p, p_hex);
-    BN_hex2bn(&a, a_hex);
-    BN_hex2bn(&b, b_hex);
-
-    group = EC_GROUP_new_curve_GFp(p, a, b, NULL);
-    BN_free(p);
-    BN_free(a);
-    BN_free(b);
-
-    if (group == NULL)
-        return NULL;
-
-    generator = EC_POINT_new(group);
-    if (generator == NULL)
-        return NULL;
-
-    BN_hex2bn(&g_x, x_hex);
-    BN_hex2bn(&g_y, y_hex);
-
-    if (EC_POINT_set_affine_coordinates_GFp(group, generator, g_x, g_y, NULL) ==
-        0)
-        return NULL;
-
-    BN_free(g_x);
-    BN_free(g_y);
-
-    BN_hex2bn(&order, order_hex);
-    BN_hex2bn(&cof, cof_hex);
-
-    if (EC_GROUP_set_generator(group, generator, order, cof) == 0)
-        return NULL;
-
-    EC_POINT_free(generator);
-    BN_free(order);
-    BN_free(cof);
-
-    return group;
-}
-
-
-static int test_sm2(const EC_GROUP *group,
-                    const char *userid,
-                    const char *privkey_hex,
-                    const char *message,
-                    const char *k_hex, const char *r_hex, const char *s_hex)
-{
-    const size_t msg_len = strlen(message);
-    int ok = -1;
-    BIGNUM *priv = NULL;
-    EC_POINT *pt = NULL;
-    EC_KEY *key = NULL;
-    ECDSA_SIG *sig = NULL;
-    const BIGNUM *sig_r = NULL;
-    const BIGNUM *sig_s = NULL;
-    BIGNUM *r = NULL;
-    BIGNUM *s = NULL;
-
-    BN_hex2bn(&priv, privkey_hex);
-
-    key = EC_KEY_new();
-    EC_KEY_set_group(key, group);
-    EC_KEY_set_private_key(key, priv);
-
-    pt = EC_POINT_new(group);
-    EC_POINT_mul(group, pt, priv, NULL, NULL, NULL);
-    EC_KEY_set_public_key(key, pt);
-
-    start_fake_rand(k_hex);
-    sig = SM2_do_sign(key, EVP_sm3(), userid, (const uint8_t *)message, msg_len);
-    restore_rand();
-
-    if (sig == NULL)
-        return 0;
-
-    ECDSA_SIG_get0(sig, &sig_r, &sig_s);
-
-    BN_hex2bn(&r, r_hex);
-    BN_hex2bn(&s, s_hex);
-
-    if (BN_cmp(r, sig_r) != 0) {
-        printf("Signature R mismatch: ");
-        BN_print_fp(stdout, r);
-        printf(" != ");
-        BN_print_fp(stdout, sig_r);
-        printf("\n");
-        ok = 0;
-    }
-    if (BN_cmp(s, sig_s) != 0) {
-        printf("Signature S mismatch: ");
-        BN_print_fp(stdout, s);
-        printf(" != ");
-        BN_print_fp(stdout, sig_s);
-        printf("\n");
-        ok = 0;
-    }
-
-    ok = SM2_do_verify(key, EVP_sm3(), sig, userid, (const uint8_t *)message, msg_len);
-
-    ECDSA_SIG_free(sig);
-    EC_POINT_free(pt);
-    EC_KEY_free(key);
-    BN_free(priv);
-    BN_free(r);
-    BN_free(s);
-
-    return ok;
-}
-
-static int sm2_sig_test(void)
-{
-    int rc = 0;
-    /* From draft-shen-sm2-ecdsa-02 */
-    EC_GROUP *test_group =
-        create_EC_group
-        ("8542D69E4C044F18E8B92435BF6FF7DE457283915C45517D722EDB8B08F1DFC3",
-         "787968B4FA32C3FD2417842E73BBFEFF2F3C848B6831D7E0EC65228B3937E498",
-         "63E4C6D3B23B0C849CF84241484BFE48F61D59A5B16BA06E6E12D1DA27C5249A",
-         "421DEBD61B62EAB6746434EBC3CC315E32220B3BADD50BDC4C4E6C147FEDD43D",
-         "0680512BCBB42C07D47349D2153B70C4E5D7FDFCBFA36EA1A85841B9E46E09A2",
-         "8542D69E4C044F18E8B92435BF6FF7DD297720630485628D5AE74EE7C32E79B7",
-         "1");
-
-    if (test_group == NULL)
-        return 0;
-
-    rc = test_sm2(test_group,
-                    "ALICE123@YAHOO.COM",
-                    "128B2FA8BD433C6C068C8D803DFF79792A519A55171B1B650C23661D15897263",
-                    "message digest",
-                    "006CB28D99385C175C94F94E934817663FC176D925DD72B727260DBAAE1FB2F96F",
-                    "40F1EC59F793D9F49E09DCEF49130D4194F79FB1EED2CAA55BACDB49C4E755D1",
-                    "6FC6DAC32C5D5CF10C77DFB20F7C2EB667A457872FB09EC56327A67EC7DEEBE7");
-
-    EC_GROUP_free(test_group);
-
-    return rc;
-}
-
-#endif
-
-int setup_tests(void)
-{
-#ifdef OPENSSL_NO_SM2
-    TEST_note("SM2 is disabled.");
-#else
-    ADD_TEST(sm2_sig_test);
-#endif
-    return 1;
-}