Home | History | Annotate | Line # | Download | only in test
      1      1.1  christos /*
      2      1.1  christos  * Copyright 2017-2024 The OpenSSL Project Authors. All Rights Reserved.
      3      1.1  christos  *
      4      1.1  christos  * Licensed under the Apache License 2.0 (the "License").  You may not use
      5      1.1  christos  * this file except in compliance with the License.  You can obtain a copy
      6      1.1  christos  * in the file LICENSE in the source distribution or at
      7      1.1  christos  * https://www.openssl.org/source/license.html
      8      1.1  christos  */
      9      1.1  christos 
     10      1.1  christos /*
     11      1.1  christos  * Low level APIs are deprecated for public use, but still ok for internal use.
     12      1.1  christos  */
     13      1.1  christos #include "internal/deprecated.h"
     14      1.1  christos 
     15      1.1  christos #include <stdio.h>
     16      1.1  christos #include <stdlib.h>
     17      1.1  christos #include <string.h>
     18      1.1  christos 
     19      1.1  christos #include <openssl/bio.h>
     20      1.1  christos #include <openssl/evp.h>
     21      1.1  christos #include <openssl/bn.h>
     22      1.1  christos #include <openssl/crypto.h>
     23      1.1  christos #include <openssl/err.h>
     24      1.1  christos #include <openssl/rand.h>
     25      1.1  christos #include "testutil.h"
     26      1.1  christos 
     27      1.1  christos #ifndef OPENSSL_NO_SM2
     28      1.1  christos 
     29  1.1.1.2  christos #include "crypto/sm2.h"
     30      1.1  christos 
     31      1.1  christos static fake_random_generate_cb get_faked_bytes;
     32      1.1  christos 
     33      1.1  christos static OSSL_PROVIDER *fake_rand = NULL;
     34      1.1  christos static uint8_t *fake_rand_bytes = NULL;
     35      1.1  christos static size_t fake_rand_bytes_offset = 0;
     36      1.1  christos static size_t fake_rand_size = 0;
     37      1.1  christos 
     38      1.1  christos static int get_faked_bytes(unsigned char *buf, size_t num,
     39  1.1.1.2  christos     ossl_unused const char *name,
     40  1.1.1.2  christos     ossl_unused EVP_RAND_CTX *ctx)
     41      1.1  christos {
     42      1.1  christos     if (!TEST_ptr(fake_rand_bytes) || !TEST_size_t_gt(fake_rand_size, 0))
     43      1.1  christos         return 0;
     44      1.1  christos 
     45      1.1  christos     while (num-- > 0) {
     46      1.1  christos         if (fake_rand_bytes_offset >= fake_rand_size)
     47      1.1  christos             fake_rand_bytes_offset = 0;
     48      1.1  christos         *buf++ = fake_rand_bytes[fake_rand_bytes_offset++];
     49      1.1  christos     }
     50      1.1  christos 
     51      1.1  christos     return 1;
     52      1.1  christos }
     53      1.1  christos 
     54      1.1  christos static int start_fake_rand(const char *hex_bytes)
     55      1.1  christos {
     56      1.1  christos     OPENSSL_free(fake_rand_bytes);
     57      1.1  christos     fake_rand_bytes_offset = 0;
     58      1.1  christos     fake_rand_size = strlen(hex_bytes) / 2;
     59      1.1  christos     if (!TEST_ptr(fake_rand_bytes = OPENSSL_hexstr2buf(hex_bytes, NULL)))
     60      1.1  christos         return 0;
     61      1.1  christos 
     62      1.1  christos     /* use own random function */
     63      1.1  christos     fake_rand_set_public_private_callbacks(NULL, get_faked_bytes);
     64      1.1  christos     return 1;
     65      1.1  christos }
     66      1.1  christos 
     67      1.1  christos static void restore_rand(void)
     68      1.1  christos {
     69      1.1  christos     fake_rand_set_public_private_callbacks(NULL, NULL);
     70      1.1  christos     OPENSSL_free(fake_rand_bytes);
     71      1.1  christos     fake_rand_bytes = NULL;
     72      1.1  christos     fake_rand_bytes_offset = 0;
     73      1.1  christos }
     74      1.1  christos 
     75      1.1  christos static EC_GROUP *create_EC_group(const char *p_hex, const char *a_hex,
     76  1.1.1.2  christos     const char *b_hex, const char *x_hex,
     77  1.1.1.2  christos     const char *y_hex, const char *order_hex,
     78  1.1.1.2  christos     const char *cof_hex)
     79      1.1  christos {
     80      1.1  christos     BIGNUM *p = NULL;
     81      1.1  christos     BIGNUM *a = NULL;
     82      1.1  christos     BIGNUM *b = NULL;
     83      1.1  christos     BIGNUM *g_x = NULL;
     84      1.1  christos     BIGNUM *g_y = NULL;
     85      1.1  christos     BIGNUM *order = NULL;
     86      1.1  christos     BIGNUM *cof = NULL;
     87      1.1  christos     EC_POINT *generator = NULL;
     88      1.1  christos     EC_GROUP *group = NULL;
     89      1.1  christos     int ok = 0;
     90      1.1  christos 
     91      1.1  christos     if (!TEST_true(BN_hex2bn(&p, p_hex))
     92  1.1.1.2  christos         || !TEST_true(BN_hex2bn(&a, a_hex))
     93  1.1.1.2  christos         || !TEST_true(BN_hex2bn(&b, b_hex)))
     94      1.1  christos         goto done;
     95      1.1  christos 
     96      1.1  christos     group = EC_GROUP_new_curve_GFp(p, a, b, NULL);
     97      1.1  christos     if (!TEST_ptr(group))
     98      1.1  christos         goto done;
     99      1.1  christos 
    100      1.1  christos     generator = EC_POINT_new(group);
    101      1.1  christos     if (!TEST_ptr(generator))
    102      1.1  christos         goto done;
    103      1.1  christos 
    104      1.1  christos     if (!TEST_true(BN_hex2bn(&g_x, x_hex))
    105  1.1.1.2  christos         || !TEST_true(BN_hex2bn(&g_y, y_hex))
    106  1.1.1.2  christos         || !TEST_true(EC_POINT_set_affine_coordinates(group, generator, g_x,
    107  1.1.1.2  christos             g_y, NULL)))
    108      1.1  christos         goto done;
    109      1.1  christos 
    110      1.1  christos     if (!TEST_true(BN_hex2bn(&order, order_hex))
    111  1.1.1.2  christos         || !TEST_true(BN_hex2bn(&cof, cof_hex))
    112  1.1.1.2  christos         || !TEST_true(EC_GROUP_set_generator(group, generator, order, cof)))
    113      1.1  christos         goto done;
    114      1.1  christos 
    115      1.1  christos     ok = 1;
    116      1.1  christos done:
    117      1.1  christos     BN_free(p);
    118      1.1  christos     BN_free(a);
    119      1.1  christos     BN_free(b);
    120      1.1  christos     BN_free(g_x);
    121      1.1  christos     BN_free(g_y);
    122      1.1  christos     EC_POINT_free(generator);
    123      1.1  christos     BN_free(order);
    124      1.1  christos     BN_free(cof);
    125      1.1  christos     if (!ok) {
    126      1.1  christos         EC_GROUP_free(group);
    127      1.1  christos         group = NULL;
    128      1.1  christos     }
    129      1.1  christos 
    130      1.1  christos     return group;
    131      1.1  christos }
    132      1.1  christos 
    133      1.1  christos static int test_sm2_crypt(const EC_GROUP *group,
    134  1.1.1.2  christos     const EVP_MD *digest,
    135  1.1.1.2  christos     const char *privkey_hex,
    136  1.1.1.2  christos     const char *message,
    137  1.1.1.2  christos     const char *k_hex, const char *ctext_hex)
    138      1.1  christos {
    139      1.1  christos     const size_t msg_len = strlen(message);
    140      1.1  christos     BIGNUM *priv = NULL;
    141      1.1  christos     EC_KEY *key = NULL;
    142      1.1  christos     EC_POINT *pt = NULL;
    143      1.1  christos     unsigned char *expected = OPENSSL_hexstr2buf(ctext_hex, NULL);
    144      1.1  christos     size_t ctext_len = 0;
    145      1.1  christos     size_t ptext_len = 0;
    146      1.1  christos     uint8_t *ctext = NULL;
    147      1.1  christos     uint8_t *recovered = NULL;
    148      1.1  christos     size_t recovered_len = msg_len;
    149      1.1  christos     int rc = 0;
    150      1.1  christos 
    151      1.1  christos     if (!TEST_ptr(expected)
    152  1.1.1.2  christos         || !TEST_true(BN_hex2bn(&priv, privkey_hex)))
    153      1.1  christos         goto done;
    154      1.1  christos 
    155      1.1  christos     key = EC_KEY_new();
    156      1.1  christos     if (!TEST_ptr(key)
    157  1.1.1.2  christos         || !TEST_true(EC_KEY_set_group(key, group))
    158  1.1.1.2  christos         || !TEST_true(EC_KEY_set_private_key(key, priv)))
    159      1.1  christos         goto done;
    160      1.1  christos 
    161      1.1  christos     pt = EC_POINT_new(group);
    162      1.1  christos     if (!TEST_ptr(pt)
    163  1.1.1.2  christos         || !TEST_true(EC_POINT_mul(group, pt, priv, NULL, NULL, NULL))
    164  1.1.1.2  christos         || !TEST_true(EC_KEY_set_public_key(key, pt))
    165  1.1.1.2  christos         || !TEST_true(ossl_sm2_ciphertext_size(key, digest, msg_len,
    166  1.1.1.2  christos             &ctext_len)))
    167      1.1  christos         goto done;
    168      1.1  christos 
    169      1.1  christos     ctext = OPENSSL_zalloc(ctext_len);
    170      1.1  christos     if (!TEST_ptr(ctext))
    171      1.1  christos         goto done;
    172      1.1  christos 
    173      1.1  christos     start_fake_rand(k_hex);
    174      1.1  christos     if (!TEST_true(ossl_sm2_encrypt(key, digest,
    175  1.1.1.2  christos             (const uint8_t *)message, msg_len,
    176  1.1.1.2  christos             ctext, &ctext_len))) {
    177      1.1  christos         restore_rand();
    178      1.1  christos         goto done;
    179      1.1  christos     }
    180      1.1  christos     restore_rand();
    181      1.1  christos 
    182      1.1  christos     if (!TEST_mem_eq(ctext, ctext_len, expected, ctext_len))
    183      1.1  christos         goto done;
    184      1.1  christos 
    185      1.1  christos     if (!TEST_true(ossl_sm2_plaintext_size(ctext, ctext_len, &ptext_len))
    186  1.1.1.2  christos         || !TEST_int_eq(ptext_len, msg_len))
    187      1.1  christos         goto done;
    188      1.1  christos 
    189      1.1  christos     recovered = OPENSSL_zalloc(ptext_len);
    190      1.1  christos     if (!TEST_ptr(recovered)
    191  1.1.1.2  christos         || !TEST_true(ossl_sm2_decrypt(key, digest, ctext, ctext_len,
    192  1.1.1.2  christos             recovered, &recovered_len))
    193  1.1.1.2  christos         || !TEST_int_eq(recovered_len, msg_len)
    194  1.1.1.2  christos         || !TEST_mem_eq(recovered, recovered_len, message, msg_len))
    195      1.1  christos         goto done;
    196      1.1  christos 
    197      1.1  christos     rc = 1;
    198  1.1.1.2  christos done:
    199      1.1  christos     BN_free(priv);
    200      1.1  christos     EC_POINT_free(pt);
    201      1.1  christos     OPENSSL_free(ctext);
    202      1.1  christos     OPENSSL_free(recovered);
    203      1.1  christos     OPENSSL_free(expected);
    204      1.1  christos     EC_KEY_free(key);
    205      1.1  christos     return rc;
    206      1.1  christos }
    207      1.1  christos 
    208      1.1  christos static int sm2_crypt_test(void)
    209      1.1  christos {
    210      1.1  christos     int testresult = 0;
    211      1.1  christos     EC_GROUP *gm_group = NULL;
    212  1.1.1.2  christos     EC_GROUP *test_group = create_EC_group("8542D69E4C044F18E8B92435BF6FF7DE457283915C45517D722EDB8B08F1DFC3",
    213  1.1.1.2  christos         "787968B4FA32C3FD2417842E73BBFEFF2F3C848B6831D7E0EC65228B3937E498",
    214  1.1.1.2  christos         "63E4C6D3B23B0C849CF84241484BFE48F61D59A5B16BA06E6E12D1DA27C5249A",
    215  1.1.1.2  christos         "421DEBD61B62EAB6746434EBC3CC315E32220B3BADD50BDC4C4E6C147FEDD43D",
    216  1.1.1.2  christos         "0680512BCBB42C07D47349D2153B70C4E5D7FDFCBFA36EA1A85841B9E46E09A2",
    217  1.1.1.2  christos         "8542D69E4C044F18E8B92435BF6FF7DD297720630485628D5AE74EE7C32E79B7",
    218  1.1.1.2  christos         "1");
    219      1.1  christos 
    220      1.1  christos     if (!TEST_ptr(test_group))
    221      1.1  christos         goto done;
    222      1.1  christos 
    223      1.1  christos     if (!test_sm2_crypt(
    224      1.1  christos             test_group,
    225      1.1  christos             EVP_sm3(),
    226      1.1  christos             "1649AB77A00637BD5E2EFE283FBF353534AA7F7CB89463F208DDBC2920BB0DA0",
    227      1.1  christos             "encryption standard",
    228      1.1  christos             "004C62EEFD6ECFC2B95B92FD6C3D9575148AFA17425546D49018E5388D49DD7B4F"
    229      1.1  christos             "0092e8ff62146873c258557548500ab2df2a365e0609ab67640a1f6d57d7b17820"
    230      1.1  christos             "008349312695a3e1d2f46905f39a766487f2432e95d6be0cb009fe8c69fd8825a7",
    231      1.1  christos             "307B0220245C26FB68B1DDDDB12C4B6BF9F2B6D5FE60A383B0D18D1C4144ABF1"
    232      1.1  christos             "7F6252E7022076CB9264C2A7E88E52B19903FDC47378F605E36811F5C07423A2"
    233      1.1  christos             "4B84400F01B804209C3D7360C30156FAB7C80A0276712DA9D8094A634B766D3A"
    234      1.1  christos             "285E07480653426D0413650053A89B41C418B0C3AAD00D886C00286467"))
    235      1.1  christos         goto done;
    236      1.1  christos 
    237      1.1  christos     /* Same test as above except using SHA-256 instead of SM3 */
    238      1.1  christos     if (!test_sm2_crypt(
    239      1.1  christos             test_group,
    240      1.1  christos             EVP_sha256(),
    241      1.1  christos             "1649AB77A00637BD5E2EFE283FBF353534AA7F7CB89463F208DDBC2920BB0DA0",
    242      1.1  christos             "encryption standard",
    243      1.1  christos             "004C62EEFD6ECFC2B95B92FD6C3D9575148AFA17425546D49018E5388D49DD7B4F"
    244      1.1  christos             "003da18008784352192d70f22c26c243174a447ba272fec64163dd4742bae8bc98"
    245      1.1  christos             "00df17605cf304e9dd1dfeb90c015e93b393a6f046792f790a6fa4228af67d9588",
    246      1.1  christos             "307B0220245C26FB68B1DDDDB12C4B6BF9F2B6D5FE60A383B0D18D1C4144ABF17F"
    247      1.1  christos             "6252E7022076CB9264C2A7E88E52B19903FDC47378F605E36811F5C07423A24B84"
    248      1.1  christos             "400F01B80420BE89139D07853100EFA763F60CBE30099EA3DF7F8F364F9D10A5E9"
    249      1.1  christos             "88E3C5AAFC0413229E6C9AEE2BB92CAD649FE2C035689785DA33"))
    250      1.1  christos         goto done;
    251      1.1  christos 
    252      1.1  christos     /* From Annex C in both GM/T0003.5-2012 and GB/T 32918.5-2016.*/
    253      1.1  christos     gm_group = create_EC_group(
    254  1.1.1.2  christos         "fffffffeffffffffffffffffffffffffffffffff00000000ffffffffffffffff",
    255  1.1.1.2  christos         "fffffffeffffffffffffffffffffffffffffffff00000000fffffffffffffffc",
    256  1.1.1.2  christos         "28e9fa9e9d9f5e344d5a9e4bcf6509a7f39789f515ab8f92ddbcbd414d940e93",
    257  1.1.1.2  christos         "32c4ae2c1f1981195f9904466a39c9948fe30bbff2660be1715a4589334c74c7",
    258  1.1.1.2  christos         "bc3736a2f4f6779c59bdcee36b692153d0a9877cc62a474002df32e52139f0a0",
    259  1.1.1.2  christos         "fffffffeffffffffffffffffffffffff7203df6b21c6052b53bbf40939d54123",
    260  1.1.1.2  christos         "1");
    261      1.1  christos 
    262      1.1  christos     if (!TEST_ptr(gm_group))
    263      1.1  christos         goto done;
    264      1.1  christos 
    265      1.1  christos     if (!test_sm2_crypt(
    266      1.1  christos             gm_group,
    267      1.1  christos             EVP_sm3(),
    268      1.1  christos             /* privkey (from which the encrypting public key is derived) */
    269      1.1  christos             "3945208F7B2144B13F36E38AC6D39F95889393692860B51A42FB81EF4DF7C5B8",
    270      1.1  christos             /* plaintext message */
    271      1.1  christos             "encryption standard",
    272      1.1  christos             /* ephemeral nonce k */
    273      1.1  christos             "59276E27D506861A16680F3AD9C02DCCEF3CC1FA3CDBE4CE6D54B80DEAC1BC21",
    274      1.1  christos             /*
    275      1.1  christos              * expected ciphertext, the field values are from GM/T 0003.5-2012
    276      1.1  christos              * (Annex C), but serialized following the ASN.1 format specified
    277      1.1  christos              * in GM/T 0009-2012 (Sec. 7.2).
    278      1.1  christos              */
    279      1.1  christos             "307C" /* SEQUENCE, 0x7c bytes */
    280  1.1.1.2  christos             "0220" /* INTEGER, 0x20 bytes */
    281  1.1.1.2  christos             "04EBFC718E8D1798620432268E77FEB6415E2EDE0E073C0F4F640ECD2E149A73"
    282  1.1.1.2  christos             "0221" /* INTEGER, 0x21 bytes */
    283  1.1.1.2  christos             "00" /* leading 00 due to DER for pos. int with topmost bit set */
    284  1.1.1.2  christos             "E858F9D81E5430A57B36DAAB8F950A3C64E6EE6A63094D99283AFF767E124DF0"
    285  1.1.1.2  christos             "0420" /* OCTET STRING, 0x20 bytes */
    286  1.1.1.2  christos             "59983C18F809E262923C53AEC295D30383B54E39D609D160AFCB1908D0BD8766"
    287  1.1.1.2  christos             "0413" /* OCTET STRING, 0x13 bytes */
    288  1.1.1.2  christos             "21886CA989CA9C7D58087307CA93092D651EFA"))
    289      1.1  christos         goto done;
    290      1.1  christos 
    291      1.1  christos     testresult = 1;
    292  1.1.1.2  christos done:
    293      1.1  christos     EC_GROUP_free(test_group);
    294      1.1  christos     EC_GROUP_free(gm_group);
    295      1.1  christos 
    296      1.1  christos     return testresult;
    297      1.1  christos }
    298      1.1  christos 
    299      1.1  christos static int test_sm2_sign(const EC_GROUP *group,
    300  1.1.1.2  christos     const char *userid,
    301  1.1.1.2  christos     const char *privkey_hex,
    302  1.1.1.2  christos     const char *message,
    303  1.1.1.2  christos     const char *k_hex,
    304  1.1.1.2  christos     const char *r_hex,
    305  1.1.1.2  christos     const char *s_hex,
    306  1.1.1.2  christos     int omit_pubkey)
    307      1.1  christos {
    308      1.1  christos     const size_t msg_len = strlen(message);
    309      1.1  christos     int ok = 0;
    310      1.1  christos     BIGNUM *priv = NULL;
    311      1.1  christos     EC_POINT *pt = NULL;
    312      1.1  christos     EC_KEY *key = NULL;
    313      1.1  christos     ECDSA_SIG *sig = NULL;
    314      1.1  christos     const BIGNUM *sig_r = NULL;
    315      1.1  christos     const BIGNUM *sig_s = NULL;
    316      1.1  christos     BIGNUM *r = NULL;
    317      1.1  christos     BIGNUM *s = NULL;
    318      1.1  christos 
    319      1.1  christos     if (!TEST_true(BN_hex2bn(&priv, privkey_hex)))
    320      1.1  christos         goto done;
    321      1.1  christos 
    322      1.1  christos     key = EC_KEY_new();
    323      1.1  christos     if (!TEST_ptr(key)
    324  1.1.1.2  christos         || !TEST_true(EC_KEY_set_group(key, group))
    325  1.1.1.2  christos         || !TEST_true(EC_KEY_set_private_key(key, priv)))
    326      1.1  christos         goto done;
    327      1.1  christos 
    328      1.1  christos     if (omit_pubkey == 0) {
    329      1.1  christos         pt = EC_POINT_new(group);
    330      1.1  christos         if (!TEST_ptr(pt)
    331  1.1.1.2  christos             || !TEST_true(EC_POINT_mul(group, pt, priv, NULL, NULL, NULL))
    332  1.1.1.2  christos             || !TEST_true(EC_KEY_set_public_key(key, pt)))
    333      1.1  christos             goto done;
    334      1.1  christos     }
    335      1.1  christos 
    336      1.1  christos     start_fake_rand(k_hex);
    337      1.1  christos     sig = ossl_sm2_do_sign(key, EVP_sm3(), (const uint8_t *)userid,
    338  1.1.1.2  christos         strlen(userid), (const uint8_t *)message, msg_len);
    339      1.1  christos     if (!TEST_ptr(sig)) {
    340      1.1  christos         restore_rand();
    341      1.1  christos         goto done;
    342      1.1  christos     }
    343      1.1  christos     restore_rand();
    344      1.1  christos 
    345      1.1  christos     ECDSA_SIG_get0(sig, &sig_r, &sig_s);
    346      1.1  christos 
    347      1.1  christos     if (!TEST_true(BN_hex2bn(&r, r_hex))
    348  1.1.1.2  christos         || !TEST_true(BN_hex2bn(&s, s_hex))
    349  1.1.1.2  christos         || !TEST_BN_eq(r, sig_r)
    350  1.1.1.2  christos         || !TEST_BN_eq(s, sig_s))
    351      1.1  christos         goto done;
    352      1.1  christos 
    353      1.1  christos     ok = ossl_sm2_do_verify(key, EVP_sm3(), sig, (const uint8_t *)userid,
    354  1.1.1.2  christos         strlen(userid), (const uint8_t *)message, msg_len);
    355      1.1  christos 
    356      1.1  christos     /* We goto done whether this passes or fails */
    357      1.1  christos     TEST_true(ok);
    358      1.1  christos 
    359  1.1.1.2  christos done:
    360      1.1  christos     ECDSA_SIG_free(sig);
    361      1.1  christos     EC_POINT_free(pt);
    362      1.1  christos     EC_KEY_free(key);
    363      1.1  christos     BN_free(priv);
    364      1.1  christos     BN_free(r);
    365      1.1  christos     BN_free(s);
    366      1.1  christos 
    367      1.1  christos     return ok;
    368      1.1  christos }
    369      1.1  christos 
    370      1.1  christos static int sm2_sig_test(void)
    371      1.1  christos {
    372      1.1  christos     int testresult = 0;
    373      1.1  christos     EC_GROUP *gm_group = NULL;
    374      1.1  christos     /* From draft-shen-sm2-ecdsa-02 */
    375  1.1.1.2  christos     EC_GROUP *test_group = create_EC_group("8542D69E4C044F18E8B92435BF6FF7DE457283915C45517D722EDB8B08F1DFC3",
    376  1.1.1.2  christos         "787968B4FA32C3FD2417842E73BBFEFF2F3C848B6831D7E0EC65228B3937E498",
    377  1.1.1.2  christos         "63E4C6D3B23B0C849CF84241484BFE48F61D59A5B16BA06E6E12D1DA27C5249A",
    378  1.1.1.2  christos         "421DEBD61B62EAB6746434EBC3CC315E32220B3BADD50BDC4C4E6C147FEDD43D",
    379  1.1.1.2  christos         "0680512BCBB42C07D47349D2153B70C4E5D7FDFCBFA36EA1A85841B9E46E09A2",
    380  1.1.1.2  christos         "8542D69E4C044F18E8B92435BF6FF7DD297720630485628D5AE74EE7C32E79B7",
    381  1.1.1.2  christos         "1");
    382      1.1  christos 
    383      1.1  christos     if (!TEST_ptr(test_group))
    384      1.1  christos         goto done;
    385      1.1  christos 
    386      1.1  christos     if (!TEST_true(test_sm2_sign(
    387  1.1.1.2  christos             test_group,
    388  1.1.1.2  christos             "ALICE123 (at) YAHOO.COM",
    389  1.1.1.2  christos             "128B2FA8BD433C6C068C8D803DFF79792A519A55171B1B650C23661D15897263",
    390  1.1.1.2  christos             "message digest",
    391  1.1.1.2  christos             "006CB28D99385C175C94F94E934817663FC176D925DD72B727260DBAAE1FB2F96F"
    392  1.1.1.2  christos             "007c47811054c6f99613a578eb8453706ccb96384fe7df5c171671e760bfa8be3a",
    393  1.1.1.2  christos             "40F1EC59F793D9F49E09DCEF49130D4194F79FB1EED2CAA55BACDB49C4E755D1",
    394  1.1.1.2  christos             "6FC6DAC32C5D5CF10C77DFB20F7C2EB667A457872FB09EC56327A67EC7DEEBE7", 0)))
    395      1.1  christos         goto done;
    396      1.1  christos 
    397      1.1  christos     /* From Annex A in both GM/T0003.5-2012 and GB/T 32918.5-2016.*/
    398      1.1  christos     gm_group = create_EC_group(
    399      1.1  christos         "fffffffeffffffffffffffffffffffffffffffff00000000ffffffffffffffff",
    400      1.1  christos         "fffffffeffffffffffffffffffffffffffffffff00000000fffffffffffffffc",
    401      1.1  christos         "28e9fa9e9d9f5e344d5a9e4bcf6509a7f39789f515ab8f92ddbcbd414d940e93",
    402      1.1  christos         "32c4ae2c1f1981195f9904466a39c9948fe30bbff2660be1715a4589334c74c7",
    403      1.1  christos         "bc3736a2f4f6779c59bdcee36b692153d0a9877cc62a474002df32e52139f0a0",
    404      1.1  christos         "fffffffeffffffffffffffffffffffff7203df6b21c6052b53bbf40939d54123",
    405      1.1  christos         "1");
    406      1.1  christos 
    407      1.1  christos     if (!TEST_ptr(gm_group))
    408      1.1  christos         goto done;
    409      1.1  christos 
    410      1.1  christos     if (!TEST_true(test_sm2_sign(
    411  1.1.1.2  christos             gm_group,
    412  1.1.1.2  christos             /* the default ID specified in GM/T 0009-2012 (Sec. 10).*/
    413  1.1.1.2  christos             SM2_DEFAULT_USERID,
    414  1.1.1.2  christos             /* privkey */
    415  1.1.1.2  christos             "3945208F7B2144B13F36E38AC6D39F95889393692860B51A42FB81EF4DF7C5B8",
    416  1.1.1.2  christos             /* plaintext message */
    417  1.1.1.2  christos             "message digest",
    418  1.1.1.2  christos             /* ephemeral nonce k */
    419  1.1.1.2  christos             "59276E27D506861A16680F3AD9C02DCCEF3CC1FA3CDBE4CE6D54B80DEAC1BC21",
    420  1.1.1.2  christos             /* expected signature, the field values are from GM/T 0003.5-2012,
    421  1.1.1.2  christos                Annex A. */
    422  1.1.1.2  christos             /* signature R, 0x20 bytes */
    423  1.1.1.2  christos             "F5A03B0648D2C4630EEAC513E1BB81A15944DA3827D5B74143AC7EACEEE720B3",
    424  1.1.1.2  christos             /* signature S, 0x20 bytes */
    425  1.1.1.2  christos             "B1B6AA29DF212FD8763182BC0D421CA1BB9038FD1F7F42D4840B69C485BBC1AA", 0)))
    426      1.1  christos         goto done;
    427      1.1  christos 
    428      1.1  christos     /* Make sure we fail if we omit the public portion of the key */
    429      1.1  christos     if (!TEST_false(test_sm2_sign(
    430  1.1.1.2  christos             gm_group,
    431  1.1.1.2  christos             /* the default ID specified in GM/T 0009-2012 (Sec. 10).*/
    432  1.1.1.2  christos             SM2_DEFAULT_USERID,
    433  1.1.1.2  christos             /* privkey */
    434  1.1.1.2  christos             "3945208F7B2144B13F36E38AC6D39F95889393692860B51A42FB81EF4DF7C5B8",
    435  1.1.1.2  christos             /* plaintext message */
    436  1.1.1.2  christos             "message digest",
    437  1.1.1.2  christos             /* ephemeral nonce k */
    438  1.1.1.2  christos             "59276E27D506861A16680F3AD9C02DCCEF3CC1FA3CDBE4CE6D54B80DEAC1BC21",
    439  1.1.1.2  christos             /* expected signature, the field values are from GM/T 0003.5-2012,
    440  1.1.1.2  christos                Annex A. */
    441  1.1.1.2  christos             /* signature R, 0x20 bytes */
    442  1.1.1.2  christos             "F5A03B0648D2C4630EEAC513E1BB81A15944DA3827D5B74143AC7EACEEE720B3",
    443  1.1.1.2  christos             /* signature S, 0x20 bytes */
    444  1.1.1.2  christos             "B1B6AA29DF212FD8763182BC0D421CA1BB9038FD1F7F42D4840B69C485BBC1AA", 1)))
    445      1.1  christos         goto done;
    446      1.1  christos 
    447      1.1  christos     testresult = 1;
    448      1.1  christos 
    449  1.1.1.2  christos done:
    450      1.1  christos     EC_GROUP_free(test_group);
    451      1.1  christos     EC_GROUP_free(gm_group);
    452      1.1  christos 
    453      1.1  christos     return testresult;
    454      1.1  christos }
    455      1.1  christos 
    456      1.1  christos #endif
    457      1.1  christos 
    458      1.1  christos int setup_tests(void)
    459      1.1  christos {
    460      1.1  christos #ifdef OPENSSL_NO_SM2
    461      1.1  christos     TEST_note("SM2 is disabled.");
    462      1.1  christos #else
    463      1.1  christos     fake_rand = fake_rand_start(NULL);
    464      1.1  christos     if (fake_rand == NULL)
    465      1.1  christos         return 0;
    466      1.1  christos 
    467      1.1  christos     ADD_TEST(sm2_crypt_test);
    468      1.1  christos     ADD_TEST(sm2_sig_test);
    469      1.1  christos #endif
    470      1.1  christos     return 1;
    471      1.1  christos }
    472      1.1  christos 
    473      1.1  christos void cleanup_tests(void)
    474      1.1  christos {
    475      1.1  christos #ifndef OPENSSL_NO_SM2
    476      1.1  christos     fake_rand_finish(fake_rand);
    477      1.1  christos #endif
    478      1.1  christos }
    479