mirror of
https://github.com/guanzhi/GmSSL.git
synced 2026-05-06 16:36:16 +08:00
Update sm2 sign/enc with z256 implementation
This commit is contained in:
341
tests/sm2_enctest.c
Normal file
341
tests/sm2_enctest.c
Normal file
@@ -0,0 +1,341 @@
|
||||
/*
|
||||
* Copyright 2014-2024 The GmSSL Project. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the License); you may
|
||||
* not use this file except in compliance with the License.
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <gmssl/rand.h>
|
||||
#include <gmssl/asn1.h>
|
||||
#include <gmssl/error.h>
|
||||
#include <gmssl/sm2.h>
|
||||
#include <gmssl/pkcs8.h>
|
||||
|
||||
|
||||
|
||||
// 由于当前Ciphertext中椭圆曲线点数据不正确,因此无法通过测试
|
||||
static int test_sm2_ciphertext(void)
|
||||
{
|
||||
SM2_CIPHERTEXT C;
|
||||
uint8_t buf[1024];
|
||||
uint8_t *p = buf;
|
||||
const uint8_t *cp = buf;
|
||||
size_t len = 0;
|
||||
|
||||
memset(&C, 0, sizeof(SM2_CIPHERTEXT));
|
||||
|
||||
cp = p = buf; len = 0;
|
||||
if (sm2_ciphertext_to_der(&C, &p, &len) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
format_print(stderr, 0, 4, "SM2_NULL_CIPHERTEXT_SIZE: %zu\n", len);
|
||||
format_bytes(stderr, 0, 4, "", buf, len);
|
||||
|
||||
|
||||
if (sm2_ciphertext_from_der(&C, &cp, &len) != 1
|
||||
|| asn1_length_is_zero(len) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
// {0, 0, Hash, MinLen}
|
||||
C.ciphertext_size = SM2_MIN_PLAINTEXT_SIZE;
|
||||
cp = p = buf; len = 0;
|
||||
if (sm2_ciphertext_to_der(&C, &p, &len) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
format_print(stderr, 0, 4, "SM2_MIN_PLAINTEXT_SIZE: %zu\n", SM2_MIN_PLAINTEXT_SIZE);
|
||||
format_print(stderr, 0, 4, "SM2_MIN_CIPHERTEXT_SIZE: %zu\n", len);
|
||||
format_bytes(stderr, 0, 4, "", buf, len);
|
||||
if (len != SM2_MIN_CIPHERTEXT_SIZE) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (sm2_ciphertext_from_der(&C, &cp, &len) != 1
|
||||
|| asn1_length_is_zero(len) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
// { 33, 33, Hash, NULL }
|
||||
memset(&C, 0x80, sizeof(SM2_POINT));
|
||||
cp = p = buf; len = 0;
|
||||
if (sm2_ciphertext_to_der(&C, &p, &len) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
format_print(stderr, 0, 4, "ciphertext len: %zu\n", len);
|
||||
format_bytes(stderr, 0, 4, "", buf, len);
|
||||
if (sm2_ciphertext_from_der(&C, &cp, &len) != 1
|
||||
|| asn1_length_is_zero(len) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
// { 33, 33, Hash, MaxLen }
|
||||
C.ciphertext_size = SM2_MAX_PLAINTEXT_SIZE;//SM2_MAX_PLAINTEXT_SIZE;
|
||||
cp = p = buf; len = 0;
|
||||
if (sm2_ciphertext_to_der(&C, &p, &len) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
format_print(stderr, 0, 4, "SM2_MAX_PLAINTEXT_SIZE: %zu\n", SM2_MAX_PLAINTEXT_SIZE);
|
||||
format_print(stderr, 0, 4, "SM2_MAX_CIPHERTEXT_SIZE: %zu\n", len);
|
||||
format_bytes(stderr, 0, 4, "", buf, len);
|
||||
if (len != SM2_MAX_CIPHERTEXT_SIZE) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (sm2_ciphertext_from_der(&C, &cp, &len) != 1
|
||||
|| asn1_length_is_zero(len) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
printf("%s() ok\n", __FUNCTION__);
|
||||
return 1;
|
||||
}
|
||||
|
||||
#define TEST_COUNT 20
|
||||
|
||||
static int test_sm2_do_encrypt(void)
|
||||
{
|
||||
SM2_KEY sm2_key;
|
||||
uint8_t plaintext[] = "Hello World!";
|
||||
SM2_CIPHERTEXT ciphertext;
|
||||
|
||||
uint8_t plainbuf[SM2_MAX_PLAINTEXT_SIZE] = {0};
|
||||
size_t plainlen = 0;
|
||||
int r = 0;
|
||||
|
||||
size_t i = 0;
|
||||
|
||||
for (i = 0; i < TEST_COUNT; i++) {
|
||||
|
||||
if (sm2_key_generate(&sm2_key) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (sm2_do_encrypt(&sm2_key, plaintext, sizeof(plaintext), &ciphertext) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (sm2_do_decrypt(&sm2_key, &ciphertext, plainbuf, &plainlen) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (plainlen != sizeof(plaintext)
|
||||
|| memcmp(plainbuf, plaintext, sizeof(plaintext)) != 0) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
printf("%s() ok\n", __FUNCTION__);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int test_sm2_do_encrypt_fixlen(void)
|
||||
{
|
||||
struct {
|
||||
int point_size;
|
||||
size_t plaintext_len;
|
||||
} tests[] = {
|
||||
{ SM2_ciphertext_compact_point_size, 10 },
|
||||
{ SM2_ciphertext_typical_point_size, 10 },
|
||||
{ SM2_ciphertext_max_point_size, 10 },
|
||||
};
|
||||
|
||||
SM2_KEY sm2_key;
|
||||
uint8_t plaintext[SM2_MAX_PLAINTEXT_SIZE];
|
||||
SM2_CIPHERTEXT ciphertext;
|
||||
uint8_t decrypted[SM2_MAX_PLAINTEXT_SIZE];
|
||||
size_t decrypted_len;
|
||||
|
||||
size_t i;
|
||||
|
||||
if (sm2_key_generate(&sm2_key) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
rand_bytes(plaintext, sizeof(plaintext));
|
||||
|
||||
for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
|
||||
|
||||
if (sm2_do_encrypt_fixlen(&sm2_key, plaintext, tests[i].plaintext_len, tests[i].point_size, &ciphertext) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (sm2_do_decrypt(&sm2_key, &ciphertext, decrypted, &decrypted_len) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (decrypted_len != tests[i].plaintext_len) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (memcmp(decrypted, plaintext, decrypted_len) != 0) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
printf("%s() ok\n", __FUNCTION__);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int test_sm2_encrypt_fixlen(void)
|
||||
{
|
||||
struct {
|
||||
int point_size;
|
||||
size_t plaintext_len;
|
||||
} tests[] = {
|
||||
{ SM2_ciphertext_compact_point_size, 1 },
|
||||
{ SM2_ciphertext_typical_point_size, 64 },
|
||||
{ SM2_ciphertext_max_point_size, SM2_MAX_PLAINTEXT_SIZE },
|
||||
};
|
||||
|
||||
SM2_KEY sm2_key;
|
||||
size_t point_size;
|
||||
uint8_t plaintext[SM2_MAX_PLAINTEXT_SIZE];
|
||||
uint8_t encrypted[SM2_MAX_CIPHERTEXT_SIZE];
|
||||
uint8_t decrypted[SM2_MAX_PLAINTEXT_SIZE];
|
||||
size_t encrypted_len, encrypted_fixlen, decrypted_len;
|
||||
size_t i, j;
|
||||
|
||||
if (sm2_key_generate(&sm2_key) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
rand_bytes(plaintext, sizeof(plaintext));
|
||||
|
||||
for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
|
||||
|
||||
if (sm2_encrypt_fixlen(&sm2_key, plaintext, tests[i].plaintext_len, tests[i].point_size,
|
||||
encrypted, &encrypted_len) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (sm2_decrypt(&sm2_key, encrypted, encrypted_len, decrypted, &decrypted_len) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (decrypted_len != tests[i].plaintext_len) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (memcmp(decrypted, plaintext, tests[i].plaintext_len) != 0) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
// check if sm2_encrypt_fixlen always output fixed length ciphertext
|
||||
encrypted_fixlen = encrypted_len;
|
||||
for (j = 0; j < 10; j++) {
|
||||
if (sm2_encrypt_fixlen(&sm2_key, plaintext, tests[i].plaintext_len, tests[i].point_size,
|
||||
encrypted, &encrypted_len) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
printf("plaintext len = %zu, ciphertext len = %zu\n", tests[i].plaintext_len, encrypted_len);
|
||||
if (encrypted_len != encrypted_fixlen) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
printf("%s() ok\n", __FUNCTION__);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// 应该生成不同情况下的密文!
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
static int test_sm2_encrypt(void)
|
||||
{
|
||||
SM2_KEY sm2_key;
|
||||
uint8_t msg[SM2_MAX_PLAINTEXT_SIZE];
|
||||
uint8_t cbuf[SM2_MAX_CIPHERTEXT_SIZE+100];
|
||||
uint8_t mbuf[SM2_MAX_CIPHERTEXT_SIZE];
|
||||
size_t lens[] = {
|
||||
// 0,
|
||||
1,
|
||||
16,
|
||||
SM2_MAX_PLAINTEXT_SIZE,
|
||||
};
|
||||
size_t clen, mlen;
|
||||
int i;
|
||||
|
||||
if (sm2_key_generate(&sm2_key) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (i = 0; i < sizeof(msg); i++) {
|
||||
msg[i] = (uint8_t)i;
|
||||
}
|
||||
|
||||
for (i = 0; i < sizeof(lens)/sizeof(lens[0]); i++) {
|
||||
format_print(stderr, 0, 0, "test %d\n", i + 1);
|
||||
format_bytes(stderr, 0, 4, "plaintext", msg, lens[i]);
|
||||
if (sm2_encrypt(&sm2_key, msg, lens[i], cbuf, &clen) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
format_bytes(stderr, 0, 4, "ciphertext", cbuf, clen);
|
||||
sm2_ciphertext_print(stderr, 0, 4, "Ciphertext", cbuf, clen);
|
||||
format_print(stderr, 0, 0, "\n");
|
||||
|
||||
if (sm2_decrypt(&sm2_key, cbuf, clen, mbuf, &mlen) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (mlen != lens[i]
|
||||
|| memcmp(mbuf, msg, lens[i]) != 0) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
printf("%s() ok\n", __FUNCTION__);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int main(void)
|
||||
{
|
||||
//if (test_sm2_ciphertext() != 1) goto err; // 需要正确的Ciphertext数据
|
||||
if (test_sm2_do_encrypt() != 1) goto err;
|
||||
if (test_sm2_do_encrypt_fixlen() != 1) goto err;
|
||||
if (test_sm2_encrypt() != 1) goto err;
|
||||
if (test_sm2_encrypt_fixlen() != 1) goto err;
|
||||
printf("%s all tests passed\n", __FILE__);
|
||||
return 0;
|
||||
err:
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
224
tests/sm2_signtest.c
Normal file
224
tests/sm2_signtest.c
Normal file
@@ -0,0 +1,224 @@
|
||||
/*
|
||||
* Copyright 2014-2024 The GmSSL Project. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the License); you may
|
||||
* not use this file except in compliance with the License.
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <gmssl/asn1.h>
|
||||
#include <gmssl/rand.h>
|
||||
#include <gmssl/error.h>
|
||||
#include <gmssl/sm2.h>
|
||||
#include <gmssl/sm2_z256.h>
|
||||
#include <gmssl/pkcs8.h>
|
||||
|
||||
|
||||
static int test_sm2_signature(void)
|
||||
{
|
||||
SM2_SIGNATURE sig;
|
||||
uint8_t buf[512];
|
||||
uint8_t *p = buf;
|
||||
const uint8_t *cp = buf;
|
||||
size_t len = 0;
|
||||
|
||||
// MinLen
|
||||
memset(&sig, 0x00, sizeof(sig));
|
||||
cp = p = buf; len = 0;
|
||||
if (sm2_signature_to_der(&sig, &p, &len) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
format_print(stderr, 0, 4, "SM2_MIN_SIGNATURE_SIZE: %zu\n", len);
|
||||
format_bytes(stderr, 0, 4, "", buf, len);
|
||||
sm2_signature_print(stderr, 0, 4, "signature", buf, len);
|
||||
if (len != SM2_MIN_SIGNATURE_SIZE) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (sm2_signature_from_der(&sig, &cp, &len) != 1
|
||||
|| asn1_length_is_zero(len) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
// MaxLen
|
||||
memset(&sig, 0x80, sizeof(sig));
|
||||
cp = p = buf; len = 0;
|
||||
if (sm2_signature_to_der(&sig, &p, &len) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
format_print(stderr, 0, 4, "SM2_MAX_SIGNATURE_SIZE: %zu\n", len);
|
||||
format_bytes(stderr, 0, 4, "", buf, len);
|
||||
sm2_signature_print(stderr, 0, 4, "signature", buf, len);
|
||||
if (len != SM2_MAX_SIGNATURE_SIZE) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (sm2_signature_from_der(&sig, &cp, &len) != 1
|
||||
|| asn1_length_is_zero(len) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
printf("%s() ok\n", __FUNCTION__);
|
||||
return 1;
|
||||
}
|
||||
|
||||
#define TEST_COUNT 20
|
||||
|
||||
static int test_sm2_do_sign(void)
|
||||
{
|
||||
SM2_KEY sm2_key;
|
||||
uint8_t dgst[32];
|
||||
SM2_SIGNATURE sig;
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < TEST_COUNT; i++) {
|
||||
|
||||
if (sm2_key_generate(&sm2_key) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
rand_bytes(dgst, 32);
|
||||
|
||||
if (sm2_do_sign(&sm2_key, dgst, &sig) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (sm2_do_verify(&sm2_key, dgst, &sig) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
printf("%s() ok\n", __FUNCTION__);
|
||||
return 1;
|
||||
}
|
||||
|
||||
#define SM2_U256 SM2_Z256
|
||||
#define sm2_u256_one sm2_z256_one
|
||||
#define sm2_u256_is_zero sm2_z256_is_zero
|
||||
#define sm2_u256_from_bytes sm2_z256_from_bytes
|
||||
#define sm2_u256_modn_add sm2_z256_modn_add
|
||||
#define sm2_u256_modn_inv sm2_z256_modn_inv
|
||||
|
||||
static int test_sm2_do_sign_fast(void)
|
||||
{
|
||||
SM2_KEY sm2_key;
|
||||
SM2_U256 d;
|
||||
uint8_t dgst[32];
|
||||
SM2_SIGNATURE sig;
|
||||
size_t i;
|
||||
|
||||
// d' = (d + 1)^-1 (mod n)
|
||||
const uint64_t *one = sm2_u256_one();
|
||||
do {
|
||||
sm2_key_generate(&sm2_key);
|
||||
sm2_u256_from_bytes(d, sm2_key.private_key);
|
||||
sm2_u256_modn_add(d, d, one);
|
||||
sm2_u256_modn_inv(d, d);
|
||||
} while (sm2_u256_is_zero(d));
|
||||
|
||||
for (i = 0; i < TEST_COUNT; i++) {
|
||||
if (sm2_do_sign_fast(d, dgst, &sig) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (sm2_do_verify(&sm2_key, dgst, &sig) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
printf("%s() ok\n", __FUNCTION__);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int test_sm2_sign(void)
|
||||
{
|
||||
SM2_KEY sm2_key;
|
||||
uint8_t dgst[32];
|
||||
uint8_t sig[SM2_MAX_SIGNATURE_SIZE];
|
||||
size_t siglen;
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < TEST_COUNT; i++) {
|
||||
|
||||
if (sm2_key_generate(&sm2_key) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
rand_bytes(dgst, 32);
|
||||
|
||||
if (sm2_sign(&sm2_key, dgst, sig, &siglen) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (sm2_verify(&sm2_key, dgst, sig, siglen) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
printf("%s() ok\n", __FUNCTION__);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int test_sm2_sign_ctx(void)
|
||||
{
|
||||
int ret;
|
||||
SM2_KEY sm2_key;
|
||||
SM2_SIGN_CTX sign_ctx;
|
||||
uint8_t msg[] = "Hello World!";
|
||||
uint8_t sig[SM2_MAX_SIGNATURE_SIZE] = {0};
|
||||
size_t siglen;
|
||||
|
||||
if (sm2_key_generate(&sm2_key) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
sm2_key_print(stderr, 0, 4, "SM2_KEY", &sm2_key);
|
||||
|
||||
if (sm2_sign_init(&sign_ctx, &sm2_key, SM2_DEFAULT_ID, SM2_DEFAULT_ID_LENGTH) != 1
|
||||
|| sm2_sign_update(&sign_ctx, msg, sizeof(msg)) != 1
|
||||
|| sm2_sign_finish(&sign_ctx, sig, &siglen) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
format_bytes(stderr, 0, 4, "signature", sig, siglen);
|
||||
sm2_signature_print(stderr, 0, 4, "signature", sig, siglen);
|
||||
|
||||
if (sm2_verify_init(&sign_ctx, &sm2_key, SM2_DEFAULT_ID, SM2_DEFAULT_ID_LENGTH) != 1
|
||||
|| sm2_verify_update(&sign_ctx, msg, sizeof(msg)) != 1
|
||||
|| (ret = sm2_verify_finish(&sign_ctx, sig, siglen)) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
format_print(stderr, 0, 4, "verification: %s\n", ret ? "success" : "failed");
|
||||
|
||||
// FIXME: 还应该增加验证不通过的测试
|
||||
// 还应该增加底层的参数
|
||||
printf("%s() ok\n", __FUNCTION__);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
if (test_sm2_signature() != 1) goto err;
|
||||
if (test_sm2_do_sign() != 1) goto err;
|
||||
if (test_sm2_sign() != 1) goto err;
|
||||
if (test_sm2_sign_ctx() != 1) goto err;
|
||||
printf("%s all tests passed\n", __FILE__);
|
||||
return 0;
|
||||
err:
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
|
||||
enum {
|
||||
OP_ADD,
|
||||
OP_DBL,
|
||||
OP_SUB,
|
||||
OP_NEG,
|
||||
OP_MUL,
|
||||
@@ -240,25 +241,16 @@ static int test_sm2_z256_modn(void)
|
||||
sm2_z256_modn_neg(c, a);
|
||||
break;
|
||||
case OP_MUL:
|
||||
sm2_z256_modn_to_mont(a, a);
|
||||
sm2_z256_modn_to_mont(b, b);
|
||||
sm2_z256_modn_mont_mul(c, a, b);
|
||||
sm2_z256_modn_from_mont(c, c);
|
||||
sm2_z256_modn_mul(c, a, b);
|
||||
break;
|
||||
case OP_SQR:
|
||||
sm2_z256_modn_to_mont(a, a);
|
||||
sm2_z256_modn_mont_sqr(c, a);
|
||||
sm2_z256_modn_from_mont(c, c);
|
||||
sm2_z256_modn_sqr(c, a);
|
||||
break;
|
||||
case OP_EXP:
|
||||
sm2_z256_modn_to_mont(a, a);
|
||||
sm2_z256_modn_mont_exp(c, a, b);
|
||||
sm2_z256_modn_from_mont(c, c);
|
||||
sm2_z256_modn_exp(c, a, b);
|
||||
break;
|
||||
case OP_INV:
|
||||
sm2_z256_modn_to_mont(a, a);
|
||||
sm2_z256_modn_mont_inv(c, a);
|
||||
sm2_z256_modn_from_mont(c, c);
|
||||
sm2_z256_modn_inv(c, a);
|
||||
break;
|
||||
default:
|
||||
error_print();
|
||||
@@ -285,6 +277,219 @@ static int test_sm2_z256_modn(void)
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int test_sm2_z256_point_is_on_curve(void)
|
||||
{
|
||||
|
||||
struct {
|
||||
char *label;
|
||||
char *mont_X;
|
||||
char *mont_Y;
|
||||
char *mont_Z;
|
||||
} tests[] = {
|
||||
{
|
||||
"Point at Infinity (1:1:0)",
|
||||
"0000000100000000000000000000000000000000ffffffff0000000000000001", // mont(1)
|
||||
"0000000100000000000000000000000000000000ffffffff0000000000000001", // mont(1)
|
||||
"0000000000000000000000000000000000000000000000000000000000000000", // 0
|
||||
},
|
||||
{
|
||||
"Affine Point [1]G with Montgomery Coordinates",
|
||||
"91167a5ee1c13b05d6a1ed99ac24c3c33e7981eddca6c05061328990f418029e", // mont(x)
|
||||
"63cd65d481d735bd8d4cfb066e2a48f8c1f5e5788d3295fac1354e593c2d0ddd", // mont(y)
|
||||
"0000000100000000000000000000000000000000ffffffff0000000000000001", // mont(1)
|
||||
},
|
||||
{
|
||||
"Jacobian Point [2]G with Montgomery Coordinates",
|
||||
"398874c476a3b1f77aef3e862601440903243d78d5b614a62eda8381e63c48d6",
|
||||
"1fbbdfdddaf4fd475a86a7ae64921d4829f04a88f6cf4dc128385681c1a73e40",
|
||||
"c79acba903ae6b7b1a99f60cdc5491f183ebcaf11a652bf5826a9cb2785a1bba",
|
||||
},
|
||||
};
|
||||
|
||||
SM2_Z256_POINT P;
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
|
||||
|
||||
sm2_z256_from_hex(P.X, tests[i].mont_X);
|
||||
sm2_z256_from_hex(P.Y, tests[i].mont_Y);
|
||||
sm2_z256_from_hex(P.Z, tests[i].mont_Z);
|
||||
|
||||
if (sm2_z256_point_is_on_curve(&P) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
printf("%s() ok\n", __FUNCTION__);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int test_sm2_z256_point_get_xy(void)
|
||||
{
|
||||
struct {
|
||||
char *label;
|
||||
char *mont_X;
|
||||
char *mont_Y;
|
||||
char *mont_Z;
|
||||
char *x;
|
||||
char *y;
|
||||
} tests[] = {
|
||||
{
|
||||
"Point at Infinity (1:1:0)",
|
||||
"0000000100000000000000000000000000000000ffffffff0000000000000001", // mont(1)
|
||||
"0000000100000000000000000000000000000000ffffffff0000000000000001", // mont(1)
|
||||
"0000000000000000000000000000000000000000000000000000000000000000", // 0
|
||||
"0000000000000000000000000000000000000000000000000000000000000000", // 0
|
||||
"0000000000000000000000000000000000000000000000000000000000000000", // 0
|
||||
},
|
||||
{
|
||||
"Affine Point [1]G with Montgomery Coordinates",
|
||||
"91167a5ee1c13b05d6a1ed99ac24c3c33e7981eddca6c05061328990f418029e", // mont(x)
|
||||
"63cd65d481d735bd8d4cfb066e2a48f8c1f5e5788d3295fac1354e593c2d0ddd", // mont(y)
|
||||
"0000000100000000000000000000000000000000ffffffff0000000000000001", // mont(1)
|
||||
"32c4ae2c1f1981195f9904466a39c9948fe30bbff2660be1715a4589334c74c7", // x
|
||||
"bc3736a2f4f6779c59bdcee36b692153d0a9877cc62a474002df32e52139f0a0", // y
|
||||
},
|
||||
{
|
||||
"Jacobian Point [2]G with Montgomery Coordinates",
|
||||
"398874c476a3b1f77aef3e862601440903243d78d5b614a62eda8381e63c48d6",
|
||||
"1fbbdfdddaf4fd475a86a7ae64921d4829f04a88f6cf4dc128385681c1a73e40",
|
||||
"c79acba903ae6b7b1a99f60cdc5491f183ebcaf11a652bf5826a9cb2785a1bba",
|
||||
"56cefd60d7c87c000d58ef57fa73ba4d9c0dfa08c08a7331495c2e1da3f2bd52",
|
||||
"31b7e7e6cc8189f668535ce0f8eaf1bd6de84c182f6c8e716f780d3a970a23c3",
|
||||
},
|
||||
};
|
||||
|
||||
SM2_Z256_POINT P;
|
||||
uint64_t x[4];
|
||||
uint64_t y[4];
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
|
||||
|
||||
sm2_z256_from_hex(P.X, tests[i].mont_X);
|
||||
sm2_z256_from_hex(P.Y, tests[i].mont_Y);
|
||||
sm2_z256_from_hex(P.Z, tests[i].mont_Z);
|
||||
|
||||
sm2_z256_point_get_xy(&P, x, NULL);
|
||||
if (sm2_z256_equ_hex(x, tests[i].x) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
sm2_z256_point_get_xy(&P, x, y);
|
||||
if (sm2_z256_equ_hex(y, tests[i].y) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
};
|
||||
|
||||
printf("%s() ok\n", __FUNCTION__);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int test_sm2_z256_point_ops(void)
|
||||
{
|
||||
char *hex_G =
|
||||
"32c4ae2c1f1981195f9904466a39c9948fe30bbff2660be1715a4589334c74c7"
|
||||
"bc3736a2f4f6779c59bdcee36b692153d0a9877cc62a474002df32e52139f0a0";
|
||||
char *hex_2G =
|
||||
"56cefd60d7c87c000d58ef57fa73ba4d9c0dfa08c08a7331495c2e1da3f2bd52"
|
||||
"31b7e7e6cc8189f668535ce0f8eaf1bd6de84c182f6c8e716f780d3a970a23c3";
|
||||
char *hex_3G =
|
||||
"a97f7cd4b3c993b4be2daa8cdb41e24ca13f6bd945302244e26918f1d0509ebf"
|
||||
"530b5dd88c688ef5ccc5cec08a72150f7c400ee5cd045292aaacdd037458f6e6";
|
||||
char *hex_negG =
|
||||
"32c4ae2c1f1981195f9904466a39c9948fe30bbff2660be1715a4589334c74c7"
|
||||
"43c8c95c0b098863a642311c9496deac2f56788239d5b8c0fd20cd1adec60f5f";
|
||||
char *hex_10G =
|
||||
"d3f94862519621c121666061f65c3e32b2d0d065cd219e3284a04814db522756"
|
||||
"4b9030cf676f6a742ebd57d146dca428f6b743f64d1482d147d46fb2bab82a14";
|
||||
char *hex_bG =
|
||||
"528470bc74a6ebc663c06fc4cfa1b630d1e9d4a80c0a127b47f73c324c46c0ba"
|
||||
"832cf9c5a15b997e60962b4cf6e2c9cee488faaec98d20599d323d4cabfc1bf4";
|
||||
char *hex_10 =
|
||||
"000000000000000000000000000000000000000000000000000000000000000A";
|
||||
char *hex_b =
|
||||
"28e9fa9e9d9f5e344d5a9e4bcf6509a7f39789f515ab8f92ddbcbd414d940e93";
|
||||
|
||||
struct {
|
||||
char *label;
|
||||
int op;
|
||||
char *R;
|
||||
char *k;
|
||||
char *A;
|
||||
char *B;
|
||||
} tests[] = {
|
||||
{"[2]G", OP_DBL, hex_2G, NULL, hex_G, NULL,},
|
||||
{"[2]G + G", OP_ADD, hex_3G, NULL, hex_2G, hex_G,},
|
||||
{"[3]G - G", OP_SUB, hex_2G, NULL, hex_3G, hex_G,},
|
||||
{"-G", OP_NEG, hex_negG, NULL, hex_G, NULL,},
|
||||
{"[10]G", OP_MUL, hex_10G, hex_10, hex_G, NULL,},
|
||||
{"[b]G", OP_MUL, hex_bG, hex_b, hex_G, NULL,},
|
||||
};
|
||||
|
||||
size_t i;
|
||||
|
||||
SM2_Z256_POINT P;
|
||||
SM2_Z256_POINT R;
|
||||
uint64_t k[4];
|
||||
SM2_Z256_POINT A;
|
||||
SM2_Z256_POINT B;
|
||||
|
||||
|
||||
for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
|
||||
|
||||
sm2_z256_point_from_hex(&R, tests[i].R);
|
||||
if (tests[i].k) {
|
||||
sm2_z256_from_hex(k, tests[i].k);
|
||||
}
|
||||
|
||||
sm2_z256_point_from_hex(&A, tests[i].A);
|
||||
if (tests[i].B) {
|
||||
sm2_z256_point_from_hex(&B, tests[i].B);
|
||||
}
|
||||
|
||||
switch (tests[i].op) {
|
||||
case OP_ADD:
|
||||
sm2_z256_point_add(&P, &A, &B);
|
||||
break;
|
||||
case OP_DBL:
|
||||
sm2_z256_point_dbl(&P, &A);
|
||||
break;
|
||||
case OP_SUB:
|
||||
sm2_z256_point_sub(&P, &A, &B);
|
||||
break;
|
||||
case OP_NEG:
|
||||
sm2_z256_point_neg(&P, &A);
|
||||
break;
|
||||
case OP_MUL:
|
||||
sm2_z256_point_mul(&P, k, &A);
|
||||
break;
|
||||
default:
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
fprintf(stderr, "%s\n", tests[i].label);
|
||||
sm2_z256_point_print(stderr, 0, 4, "R", &P);
|
||||
fprintf(stderr, " R: %s\n", tests[i].R);
|
||||
fprintf(stderr, " k: %s\n", tests[i].k);
|
||||
fprintf(stderr, " A: %s\n", tests[i].A);
|
||||
fprintf(stderr, " B: %s\n", tests[i].B);
|
||||
|
||||
if (sm2_z256_point_equ_hex(&P, tests[i].R) != 1) {
|
||||
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
printf("%s() ok\n", __FUNCTION__);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int test_sm2_z256_point_mul_generator(void)
|
||||
{
|
||||
struct {
|
||||
@@ -340,7 +545,6 @@ static int test_sm2_z256_point_mul_generator(void)
|
||||
"DDF092555409C19DFDBE86A75C139906A80198337744EE78CD27E384D9FCAF15"
|
||||
"847D18FFB38E87065CD6B6E9C12D2922037937707D6A49A2223B949657E52BC1",
|
||||
},
|
||||
// k = G.x
|
||||
{
|
||||
"[x]G",
|
||||
"32C4AE2C1F1981195F9904466A39C9948FE30BBFF2660BE1715A4589334C74C7",
|
||||
@@ -414,6 +618,9 @@ int main(void)
|
||||
{
|
||||
if (test_sm2_z256_modp() != 1) goto err;
|
||||
if (test_sm2_z256_modn() != 1) goto err;
|
||||
if (test_sm2_z256_point_is_on_curve() != 1) goto err;
|
||||
if (test_sm2_z256_point_get_xy() != 1) goto err;
|
||||
if (test_sm2_z256_point_ops() != 1) goto err;
|
||||
if (test_sm2_z256_point_mul_generator() != 1) goto err;
|
||||
printf("%s all tests passed\n", __FILE__);
|
||||
return 0;
|
||||
|
||||
266
tests/sm2test.c
266
tests/sm2test.c
@@ -1,4 +1,4 @@
|
||||
/*
|
||||
/*
|
||||
* Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the License); you may
|
||||
@@ -444,267 +444,8 @@ static int test_sm2_point_from_x(void)
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int test_sm2_signature(void)
|
||||
{
|
||||
SM2_SIGNATURE sig;
|
||||
uint8_t buf[512];
|
||||
uint8_t *p = buf;
|
||||
const uint8_t *cp = buf;
|
||||
size_t len = 0;
|
||||
|
||||
// MinLen
|
||||
memset(&sig, 0x00, sizeof(sig));
|
||||
cp = p = buf; len = 0;
|
||||
if (sm2_signature_to_der(&sig, &p, &len) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
format_print(stderr, 0, 4, "SM2_MIN_SIGNATURE_SIZE: %zu\n", len);
|
||||
format_bytes(stderr, 0, 4, "", buf, len);
|
||||
sm2_signature_print(stderr, 0, 4, "signature", buf, len);
|
||||
if (len != SM2_MIN_SIGNATURE_SIZE) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (sm2_signature_from_der(&sig, &cp, &len) != 1
|
||||
|| asn1_length_is_zero(len) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
// MaxLen
|
||||
memset(&sig, 0x80, sizeof(sig));
|
||||
cp = p = buf; len = 0;
|
||||
if (sm2_signature_to_der(&sig, &p, &len) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
format_print(stderr, 0, 4, "SM2_MAX_SIGNATURE_SIZE: %zu\n", len);
|
||||
format_bytes(stderr, 0, 4, "", buf, len);
|
||||
sm2_signature_print(stderr, 0, 4, "signature", buf, len);
|
||||
if (len != SM2_MAX_SIGNATURE_SIZE) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (sm2_signature_from_der(&sig, &cp, &len) != 1
|
||||
|| asn1_length_is_zero(len) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
printf("%s() ok\n", __FUNCTION__);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int test_sm2_sign(void)
|
||||
{
|
||||
int ret;
|
||||
SM2_KEY sm2_key;
|
||||
SM2_SIGN_CTX sign_ctx;
|
||||
uint8_t msg[] = "Hello World!";
|
||||
uint8_t sig[SM2_MAX_SIGNATURE_SIZE] = {0};
|
||||
size_t siglen;
|
||||
|
||||
if (sm2_key_generate(&sm2_key) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
sm2_key_print(stderr, 0, 4, "SM2_KEY", &sm2_key);
|
||||
|
||||
if (sm2_sign_init(&sign_ctx, &sm2_key, SM2_DEFAULT_ID, SM2_DEFAULT_ID_LENGTH) != 1
|
||||
|| sm2_sign_update(&sign_ctx, msg, sizeof(msg)) != 1
|
||||
|| sm2_sign_finish(&sign_ctx, sig, &siglen) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
format_bytes(stderr, 0, 4, "signature", sig, siglen);
|
||||
sm2_signature_print(stderr, 0, 4, "signature", sig, siglen);
|
||||
|
||||
if (sm2_verify_init(&sign_ctx, &sm2_key, SM2_DEFAULT_ID, SM2_DEFAULT_ID_LENGTH) != 1
|
||||
|| sm2_verify_update(&sign_ctx, msg, sizeof(msg)) != 1
|
||||
|| (ret = sm2_verify_finish(&sign_ctx, sig, siglen)) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
format_print(stderr, 0, 4, "verification: %s\n", ret ? "success" : "failed");
|
||||
|
||||
|
||||
// FIXME: 还应该增加验证不通过的测试
|
||||
// 还应该增加底层的参数
|
||||
printf("%s() ok\n", __FUNCTION__);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// 由于当前Ciphertext中椭圆曲线点数据不正确,因此无法通过测试
|
||||
static int test_sm2_ciphertext(void)
|
||||
{
|
||||
SM2_CIPHERTEXT C;
|
||||
uint8_t buf[1024];
|
||||
uint8_t *p = buf;
|
||||
const uint8_t *cp = buf;
|
||||
size_t len = 0;
|
||||
|
||||
memset(&C, 0, sizeof(SM2_CIPHERTEXT));
|
||||
|
||||
cp = p = buf; len = 0;
|
||||
if (sm2_ciphertext_to_der(&C, &p, &len) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
format_print(stderr, 0, 4, "SM2_NULL_CIPHERTEXT_SIZE: %zu\n", len);
|
||||
format_bytes(stderr, 0, 4, "", buf, len);
|
||||
|
||||
|
||||
if (sm2_ciphertext_from_der(&C, &cp, &len) != 1
|
||||
|| asn1_length_is_zero(len) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
// {0, 0, Hash, MinLen}
|
||||
C.ciphertext_size = SM2_MIN_PLAINTEXT_SIZE;
|
||||
cp = p = buf; len = 0;
|
||||
if (sm2_ciphertext_to_der(&C, &p, &len) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
format_print(stderr, 0, 4, "SM2_MIN_PLAINTEXT_SIZE: %zu\n", SM2_MIN_PLAINTEXT_SIZE);
|
||||
format_print(stderr, 0, 4, "SM2_MIN_CIPHERTEXT_SIZE: %zu\n", len);
|
||||
format_bytes(stderr, 0, 4, "", buf, len);
|
||||
if (len != SM2_MIN_CIPHERTEXT_SIZE) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (sm2_ciphertext_from_der(&C, &cp, &len) != 1
|
||||
|| asn1_length_is_zero(len) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
// { 33, 33, Hash, NULL }
|
||||
memset(&C, 0x80, sizeof(SM2_POINT));
|
||||
cp = p = buf; len = 0;
|
||||
if (sm2_ciphertext_to_der(&C, &p, &len) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
format_print(stderr, 0, 4, "ciphertext len: %zu\n", len);
|
||||
format_bytes(stderr, 0, 4, "", buf, len);
|
||||
if (sm2_ciphertext_from_der(&C, &cp, &len) != 1
|
||||
|| asn1_length_is_zero(len) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
// { 33, 33, Hash, MaxLen }
|
||||
C.ciphertext_size = SM2_MAX_PLAINTEXT_SIZE;//SM2_MAX_PLAINTEXT_SIZE;
|
||||
cp = p = buf; len = 0;
|
||||
if (sm2_ciphertext_to_der(&C, &p, &len) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
format_print(stderr, 0, 4, "SM2_MAX_PLAINTEXT_SIZE: %zu\n", SM2_MAX_PLAINTEXT_SIZE);
|
||||
format_print(stderr, 0, 4, "SM2_MAX_CIPHERTEXT_SIZE: %zu\n", len);
|
||||
format_bytes(stderr, 0, 4, "", buf, len);
|
||||
if (len != SM2_MAX_CIPHERTEXT_SIZE) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (sm2_ciphertext_from_der(&C, &cp, &len) != 1
|
||||
|| asn1_length_is_zero(len) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
printf("%s() ok\n", __FUNCTION__);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int test_sm2_do_encrypt(void)
|
||||
{
|
||||
SM2_KEY sm2_key;
|
||||
uint8_t plaintext[] = "Hello World!";
|
||||
SM2_CIPHERTEXT ciphertext;
|
||||
|
||||
uint8_t plainbuf[SM2_MAX_PLAINTEXT_SIZE] = {0};
|
||||
size_t plainlen = 0;
|
||||
int r = 0;
|
||||
|
||||
if (sm2_key_generate(&sm2_key) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (sm2_do_encrypt(&sm2_key, plaintext, sizeof(plaintext), &ciphertext) != 1
|
||||
|| sm2_do_decrypt(&sm2_key, &ciphertext, plainbuf, &plainlen) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (plainlen != sizeof(plaintext)
|
||||
|| memcmp(plainbuf, plaintext, sizeof(plaintext)) != 0) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
printf("%s() ok\n", __FUNCTION__);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int test_sm2_encrypt(void)
|
||||
{
|
||||
SM2_KEY sm2_key;
|
||||
uint8_t msg[SM2_MAX_PLAINTEXT_SIZE];
|
||||
uint8_t cbuf[SM2_MAX_CIPHERTEXT_SIZE+100];
|
||||
uint8_t mbuf[SM2_MAX_CIPHERTEXT_SIZE];
|
||||
size_t lens[] = {
|
||||
// 0,
|
||||
1,
|
||||
16,
|
||||
SM2_MAX_PLAINTEXT_SIZE,
|
||||
};
|
||||
size_t clen, mlen;
|
||||
int i;
|
||||
|
||||
if (sm2_key_generate(&sm2_key) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (i = 0; i < sizeof(msg); i++) {
|
||||
msg[i] = (uint8_t)i;
|
||||
}
|
||||
|
||||
for (i = 0; i < sizeof(lens)/sizeof(lens[0]); i++) {
|
||||
format_print(stderr, 0, 0, "test %d\n", i + 1);
|
||||
format_bytes(stderr, 0, 4, "plaintext", msg, lens[i]);
|
||||
if (sm2_encrypt(&sm2_key, msg, lens[i], cbuf, &clen) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
format_bytes(stderr, 0, 4, "ciphertext", cbuf, clen);
|
||||
sm2_ciphertext_print(stderr, 0, 4, "Ciphertext", cbuf, clen);
|
||||
format_print(stderr, 0, 0, "\n");
|
||||
|
||||
if (sm2_decrypt(&sm2_key, cbuf, clen, mbuf, &mlen) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (mlen != lens[i]
|
||||
|| memcmp(mbuf, msg, lens[i]) != 0) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
printf("%s() ok\n", __FUNCTION__);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -873,11 +614,6 @@ int main(void)
|
||||
if (test_sm2_private_key() != 1) goto err;
|
||||
if (test_sm2_private_key_info() != 1) goto err;
|
||||
if (test_sm2_enced_private_key_info() != 1) goto err;
|
||||
if (test_sm2_signature() != 1) goto err;
|
||||
if (test_sm2_sign() != 1) goto err;
|
||||
//if (test_sm2_ciphertext() != 1) goto err; // 需要正确的Ciphertext数据
|
||||
if (test_sm2_do_encrypt() != 1) goto err;
|
||||
if (test_sm2_encrypt() != 1) goto err;
|
||||
printf("%s all tests passed\n", __FILE__);
|
||||
return 0;
|
||||
err:
|
||||
|
||||
Reference in New Issue
Block a user