mirror of
https://github.com/guanzhi/GmSSL.git
synced 2026-05-20 23:36:29 +08:00
Update SM2 Extensions
Arrange source files
This commit is contained in:
@@ -18,6 +18,9 @@
|
||||
#include <gmssl/sm2_blind.h>
|
||||
|
||||
|
||||
extern SM2_BN SM2_N;
|
||||
extern SM2_BN SM2_ONE;
|
||||
|
||||
int sm2_blind_sign_commit(SM2_Fn k, uint8_t *commit, size_t *commitlen)
|
||||
{
|
||||
SM2_POINT K;
|
||||
@@ -169,80 +172,3 @@ int sm2_blind_sign_unblind(SM2_BLIND_SIGN_CTX *ctx, const uint8_t blinded_sig_s[
|
||||
gmssl_secure_clear(ctx, sizeof(*ctx));
|
||||
return 1;
|
||||
}
|
||||
|
||||
int test_sm2_blind_sign(void)
|
||||
{
|
||||
int r = -1;
|
||||
|
||||
// signer
|
||||
SM2_KEY key;
|
||||
SM2_Fn k;
|
||||
uint8_t commit[65];
|
||||
size_t commitlen;
|
||||
|
||||
// caller
|
||||
SM2_KEY public_key;
|
||||
SM2_BLIND_SIGN_CTX sign_ctx;
|
||||
uint8_t msg[128] = {0};
|
||||
uint8_t blinded_sig_s[32];
|
||||
uint8_t blinded_sig_r[32];
|
||||
uint8_t sig[128];
|
||||
size_t siglen;
|
||||
|
||||
// verifier
|
||||
SM2_SIGN_CTX verify_ctx;
|
||||
|
||||
|
||||
// signer
|
||||
if (sm2_key_generate(&key) != 1
|
||||
|| sm2_key_set_public_key(&public_key, &key.public_key) != 1) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
if (sm2_blind_sign_commit(k, commit, &commitlen) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
format_bytes(stderr, 0, 0, "signer: commitment", commit, commitlen);
|
||||
|
||||
// caller
|
||||
if (sm2_blind_sign_init(&sign_ctx, &public_key,
|
||||
SM2_DEFAULT_ID, strlen(SM2_DEFAULT_ID)) != 1
|
||||
|| sm2_blind_sign_update(&sign_ctx, msg, 32) != 1
|
||||
|| sm2_blind_sign_update(&sign_ctx, msg + 32, 32) != 1
|
||||
|| sm2_blind_sign_update(&sign_ctx, msg + 64, 64) != 1
|
||||
|| sm2_blind_sign_finish(&sign_ctx, commit, commitlen, blinded_sig_r) != 1) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
format_bytes(stderr, 0, 0, "caller: blinded_sig_r", blinded_sig_r, sizeof(blinded_sig_r));
|
||||
|
||||
// signer
|
||||
if (sm2_blind_sign(&key, k, blinded_sig_r, blinded_sig_s) != 1) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
format_bytes(stderr, 0, 0, "signer: blinded_sig_s", blinded_sig_s, sizeof(blinded_sig_s));
|
||||
|
||||
// caller
|
||||
if (sm2_blind_sign_unblind(&sign_ctx, blinded_sig_s, sig, &siglen) != 1) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
format_bytes(stderr, 0, 0, "caller: unblinded_sig", sig, siglen);
|
||||
|
||||
// verifier
|
||||
if (sm2_verify_init(&verify_ctx, &public_key,
|
||||
SM2_DEFAULT_ID, strlen(SM2_DEFAULT_ID)) != 1
|
||||
|| sm2_verify_update(&verify_ctx, msg, sizeof(msg)) != 1
|
||||
|| (r = sm2_verify_finish(&verify_ctx, sig, siglen)) < 0) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
format_print(stderr, 0, 0, "verifier: %s\n", r == 1 ? "success" : "failure");
|
||||
|
||||
end:
|
||||
gmssl_secure_clear(&key, sizeof(key));
|
||||
gmssl_secure_clear(&sign_ctx, sizeof(sign_ctx));
|
||||
return r;
|
||||
}
|
||||
|
||||
@@ -168,43 +168,5 @@ int sm2_commit_vector_open(const sm2_bn_t *x, size_t count, const uint8_t r[32],
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int test_sm2_commit(void)
|
||||
{
|
||||
uint8_t x[32];
|
||||
uint8_t xvec[8][32];
|
||||
uint8_t r[32];
|
||||
uint8_t commit[65];
|
||||
size_t commitlen;
|
||||
int ret;
|
||||
|
||||
rand_bytes(x, sizeof(x));
|
||||
format_bytes(stderr, 0, 0, "secret", x, sizeof(x));
|
||||
|
||||
sm2_commit_generate(x, r, commit, &commitlen);
|
||||
format_bytes(stderr, 0, 0, "random", r, sizeof(r));
|
||||
format_bytes(stderr, 0, 0, "commitment", commit, commitlen);
|
||||
|
||||
ret = sm2_commit_open(x, r, commit, commitlen);
|
||||
printf("open commitment: %s\n", ret == 1 ? "success" : "failure");
|
||||
|
||||
|
||||
sm2_commit_vector_generate(&x, 1, r, commit, &commitlen);
|
||||
format_bytes(stderr, 0, 0, "random", r, sizeof(r));
|
||||
format_bytes(stderr, 0, 0, "commitment", commit, commitlen);
|
||||
|
||||
ret = sm2_commit_vector_open(&x, 1, r, commit, commitlen);
|
||||
printf("open commitment: %s\n", ret == 1 ? "success" : "failure");
|
||||
|
||||
|
||||
rand_bytes(xvec[0], sizeof(xvec));
|
||||
sm2_commit_vector_generate(xvec, 8, r, commit, &commitlen);
|
||||
ret = sm2_commit_vector_open(xvec, 8, r, commit, commitlen);
|
||||
printf("open commitment: %s\n", ret == 1 ? "success" : "failure");
|
||||
|
||||
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -14,18 +14,14 @@
|
||||
#include <stdint.h>
|
||||
#include <assert.h>
|
||||
#include <gmssl/mem.h>
|
||||
#include <gmssl/sm2.h>
|
||||
#include <gmssl/sm2_elgamal.h>
|
||||
#include <gmssl/asn1.h>
|
||||
#include <gmssl/error.h>
|
||||
|
||||
|
||||
#define SM2_PRE_COMPUTE_MAX_OFFSETS 6
|
||||
extern const SM2_JACOBIAN_POINT *SM2_G;
|
||||
|
||||
|
||||
typedef struct {
|
||||
uint16_t offset[SM2_PRE_COMPUTE_MAX_OFFSETS];
|
||||
uint8_t offset_count;
|
||||
uint8_t x_coordinate[32];
|
||||
} SM2_PRE_COMPUTE;
|
||||
|
||||
// generate baby-step table
|
||||
int sm2_elgamal_decrypt_pre_compute(SM2_PRE_COMPUTE table[1<<16])
|
||||
@@ -117,11 +113,6 @@ ok:
|
||||
return ret;
|
||||
}
|
||||
|
||||
typedef struct {
|
||||
SM2_POINT C1;
|
||||
SM2_POINT C2;
|
||||
} SM2_ELGAMAL_CIPHERTEXT;
|
||||
|
||||
int sm2_elgamal_do_encrypt(const SM2_KEY *pub_key, uint32_t in, SM2_ELGAMAL_CIPHERTEXT *out)
|
||||
{
|
||||
int ret = -1;
|
||||
@@ -419,14 +410,3 @@ int sm2_elgamal_decrypt(SM2_KEY *key, const uint8_t *in, size_t inlen, uint32_t
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int test_sm2_elgamal_do_encrypt(void)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
int test_sm2_elgamal_encrypt(void)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
@@ -11,10 +11,11 @@
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <gmssl/sm2.h>
|
||||
#include <gmssl/sm2_key_share.h>
|
||||
#include <gmssl/mem.h>
|
||||
#include <gmssl/error.h>
|
||||
|
||||
extern SM2_BN SM2_N;
|
||||
|
||||
int sm2_key_share_print(FILE *fp, int fmt, int ind, const char *label, const SM2_KEY_SHARE *share)
|
||||
{
|
||||
@@ -36,8 +37,6 @@ static void eval_univariate_poly(SM2_Fn y, const SM2_Fn *coeffs, size_t coeffs_c
|
||||
}
|
||||
}
|
||||
|
||||
#define SM2_KEY_MAX_SHARES 12 // 12! = 479001600 < 2^31 = 2147483648
|
||||
|
||||
int sm2_key_split(const SM2_KEY *key, size_t recover_cnt, size_t total_cnt, SM2_KEY_SHARE *shares)
|
||||
{
|
||||
SM2_Fn coeffs[SM2_KEY_MAX_SHARES];
|
||||
@@ -214,77 +213,3 @@ int sm2_key_share_decrypt_from_file(SM2_KEY_SHARE *share, const char *pass, cons
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
int test_sm2_key_share_args(size_t k, size_t n)
|
||||
{
|
||||
SM2_KEY key;
|
||||
SM2_KEY key_;
|
||||
SM2_KEY_SHARE shares[SM2_KEY_MAX_SHARES];
|
||||
|
||||
if (sm2_key_generate(&key) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (sm2_key_split(&key, k, n, shares) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
// recover from 0 .. k
|
||||
if (sm2_key_recover(&key_, shares, k) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (memcmp(&key_, &key, sizeof(SM2_KEY)) != 0) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
// recover from n-k .. n
|
||||
memset(&key_, 0, sizeof(key_));
|
||||
if (sm2_key_recover(&key_, shares + n - k, k) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (memcmp(&key_, &key, sizeof(SM2_KEY)) != 0) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int test_sm2_key_share(void)
|
||||
{
|
||||
if (test_sm2_key_share_args(1, 1) != 1) { error_print(); return -1; }
|
||||
if (test_sm2_key_share_args(1, 3) != 1) { error_print(); return -1; }
|
||||
if (test_sm2_key_share_args(2, 3) != 1) { error_print(); return -1; }
|
||||
if (test_sm2_key_share_args(3, 5) != 1) { error_print(); return -1; }
|
||||
if (test_sm2_key_share_args(4, 5) != 1) { error_print(); return -1; }
|
||||
if (test_sm2_key_share_args(5, 5) != 1) { error_print(); return -1; }
|
||||
if (test_sm2_key_share_args(11, 12) != 1) { error_print(); return -1; }
|
||||
if (test_sm2_key_share_args(12, 12) != 1) { error_print(); return -1; }
|
||||
return 1;
|
||||
}
|
||||
|
||||
int test_sm2_key_share_file(void)
|
||||
{
|
||||
SM2_KEY key;
|
||||
SM2_KEY_SHARE shares[SM2_KEY_MAX_SHARES];
|
||||
|
||||
if (sm2_key_generate(&key) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (sm2_key_split(&key, 2, 3, shares) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (sm2_key_share_encrypt_to_file(&shares[0], "123456", "sm2key") != 1
|
||||
|| sm2_key_share_encrypt_to_file(&shares[1], "123456", "sm2key") != 1
|
||||
|| sm2_key_share_encrypt_to_file(&shares[2], "123456", "sm2key") != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
@@ -15,6 +15,11 @@
|
||||
#include <gmssl/error.h>
|
||||
|
||||
|
||||
extern SM2_BN SM2_P;
|
||||
extern SM2_BN SM2_B;
|
||||
extern SM2_BN SM2_N;
|
||||
extern SM2_BN SM2_THREE;
|
||||
|
||||
// r = H(Z||M) + x1 (mod n)
|
||||
// x1 = r - H(Z||M) (mod n) or (r - H(Z||M) (mod n)) + n
|
||||
// y1 = sqrt(x1^3 + a*x1 + b)
|
||||
@@ -107,25 +112,3 @@ int sm2_signature_to_public_key_points(const SM2_SIGNATURE *sig, const uint8_t d
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int test_sm2_signature_to_public_key_points(void)
|
||||
{
|
||||
SM2_KEY key;
|
||||
uint8_t dgst[32] = {1,2,3,4};
|
||||
SM2_SIGNATURE sig;
|
||||
SM2_POINT points[4];
|
||||
size_t points_cnt, i;
|
||||
|
||||
sm2_key_generate(&key);
|
||||
sm2_do_sign(&key, dgst, &sig);
|
||||
sm2_signature_to_public_key_points(&sig, dgst, points, &points_cnt);
|
||||
|
||||
for (i = 0; i < points_cnt; i++) {
|
||||
int vr;
|
||||
sm2_point_print(stderr, 0, 0, "point", &points[i]);
|
||||
vr = sm2_do_verify((SM2_KEY *)&points[1], dgst, &sig);
|
||||
printf("verify = %d\n", vr);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
@@ -11,11 +11,15 @@
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
#include <gmssl/sm2.h>
|
||||
#include <gmssl/sm2_ring.h>
|
||||
#include <gmssl/asn1.h>
|
||||
#include <gmssl/error.h>
|
||||
|
||||
|
||||
extern SM2_BN SM2_N;
|
||||
extern SM2_BN SM2_ONE;
|
||||
|
||||
|
||||
static int compare_point(const void *P, const void *Q)
|
||||
{
|
||||
const uint8_t *p = (uint8_t *)P;
|
||||
|
||||
Reference in New Issue
Block a user