SM2一个签名者进行单个验证

sm2_create_key_pair.cpp


#include <string.h>
#include <openssl/obj_mac.h>
#include <openssl/bn.h>
#include <openssl/ec.h>
#include "sm2_cipher_error_codes.h"
#include "sm2_create_key_pair.h"

/*********************************************************/
int sm2_create_key_pair(SM2_KEY_PAIR* key_pair)
{
	int error_code;
	BN_CTX* ctx = NULL;
	BIGNUM* bn_d = NULL, * bn_x = NULL, * bn_y = NULL;
	const BIGNUM* bn_order;
	EC_GROUP* group = NULL;
	EC_POINT* ec_pt = NULL;
	unsigned char pub_key_x[32], pub_key_y[32];

	error_code = ALLOCATION_MEMORY_FAIL;
	if (!(ctx = BN_CTX_secure_new()))
	{
		goto clean_up;
	}
	BN_CTX_start(ctx);
	bn_d = BN_CTX_get(ctx);
	bn_x = BN_CTX_get(ctx);
	bn_y = BN_CTX_get(ctx);
	if (!(bn_y))
	{
		goto clean_up;
	}

	if (!(group = EC_GROUP_new_by_curve_name(NID_sm2)))
	{
		goto clean_up;
	}
	if (!(bn_order = EC_GROUP_get0_order(group)))
	{
		goto clean_up;
	}
	if (!(ec_pt = EC_POINT_new(group)))
	{
		goto clean_up;
	}

	error_code = CREATE_SM2_KEY_PAIR_FAIL;
	do
	{
		if (!(BN_rand_range(bn_d, bn_order)))
		{
			goto clean_up;
		}
	} while (BN_is_zero(bn_d));

	if (!(EC_POINT_mul(group, ec_pt, bn_d, NULL, NULL, ctx)))
	{
		goto clean_up;
	}
	if (!(EC_POINT_get_affine_coordinates_GFp(group,
		ec_pt,
		bn_x,
		bn_y,
		ctx)))
	{
		goto clean_up;
	}

	if (BN_bn2binpad(bn_d,
		key_pair->pri_key,
		sizeof(key_pair->pri_key)) != sizeof(key_pair->pri_key))
	{
		goto clean_up;
	}
	if (BN_bn2binpad(bn_x,
		pub_key_x,
		sizeof(pub_key_x)) != sizeof(pub_key_x))
	{
		goto clean_up;
	}
	if (BN_bn2binpad(bn_y,
		pub_key_y,
		sizeof(pub_key_y)) != sizeof(pub_key_y))
	{
		goto clean_up;
	}

	key_pair->pub_key[0] = 0x4;
	memcpy((key_pair->pub_key + 1), pub_key_x, sizeof(pub_key_x));
	memcpy((key_pair->pub_key + 1 + sizeof(pub_key_x)), pub_key_y, sizeof(pub_key_y));
	error_code = 0;

clean_up:
	if (ctx)
	{
		BN_CTX_end(ctx);
		BN_CTX_free(ctx);
	}

	if (group)
	{
		EC_GROUP_free(group);
	}

	if (ec_pt)
	{
		EC_POINT_free(ec_pt);
	}

	return error_code;
}

sm2_sign_and_verify.cpp



#include <string.h>
#include <openssl/obj_mac.h>
#include <openssl/evp.h>
#include <openssl/bn.h>
#include <openssl/ec.h>
#include "sm2_create_key_pair.h"
#include "sm2_cipher_error_codes.h"
#include "sm3_with_preprocess.h"
#include "sm2_sign_and_verify.h"

#include<windows.h>
/*********************************************************/
struct SM2_MES_STRUCTURE {
	unsigned char msg[1000];
	unsigned int msg_len;
	unsigned char user_id[1000];
	unsigned int user_id_len;
	unsigned char r_coordinate[32];
	unsigned char s_coordinate[32];
}sm2_mes[10001];
int sm2_sign_data(const unsigned char* message,
	const int message_len,
	const unsigned char* id,
	const int id_len,
	const unsigned char* pub_key,
	const unsigned char* pri_key,
	const int i,
	const unsigned char* r,
	const unsigned char* s
)
{
	int error_code;
	unsigned char digest[32];
	BN_CTX* ctx = NULL;
	BIGNUM* bn_e = NULL, * bn_k = NULL, * bn_x = NULL, * bn_tmp = NULL;
	BIGNUM* bn_r = NULL, * bn_s = NULL, * bn_one = NULL, * bn_d = NULL;
	BIGNUM* bn_sum_inv = NULL, * bn_dif = NULL;
	const BIGNUM* bn_order;
	EC_GROUP* group = NULL;
	const EC_POINT* generator;
	EC_POINT* k_G = NULL;

	if (error_code = sm3_digest_with_preprocess(message,
		message_len,
		id,
		id_len,
		pub_key,
		digest))
	{
		return error_code;
	}

	error_code = ALLOCATION_MEMORY_FAIL;
	if (!(ctx = BN_CTX_secure_new()))
	{
		goto clean_up;
	}
	BN_CTX_start(ctx);
	bn_one = BN_CTX_get(ctx);
	bn_e = BN_CTX_get(ctx);
	bn_k = BN_CTX_get(ctx);
	bn_x = BN_CTX_get(ctx);
	bn_tmp = BN_CTX_get(ctx);
	bn_r = BN_CTX_get(ctx);
	bn_s = BN_CTX_get(ctx);
	bn_d = BN_CTX_get(ctx);
	bn_sum_inv = BN_CTX_get(ctx);
	bn_dif = BN_CTX_get(ctx);
	if (!(bn_dif))
	{
		goto clean_up;
	}
	if (!(group = EC_GROUP_new_by_curve_name(NID_sm2)))
	{
		goto clean_up;
	}

	if (!(k_G = EC_POINT_new(group)))
	{
		goto clean_up;
	}

	error_code = COMPUTE_SM2_SIGNATURE_FAIL;
	if (!(BN_one(bn_one)))
	{
		goto clean_up;
	}

	if (!(BN_bin2bn(pri_key, 32, bn_d)))
	{
		goto clean_up;
	}

	if (!(BN_bin2bn(digest, sizeof(digest), bn_e)))
	{
		goto clean_up;
	}
	if (!(bn_order = EC_GROUP_get0_order(group)))
	{
		goto clean_up;
	}
	if (!(generator = EC_GROUP_get0_generator(group)))
	{
		goto clean_up;
	}

	do
	{
		if (!(BN_rand_range(bn_k, bn_order)))
		{
			goto clean_up;
		}
		if (BN_is_zero(bn_k))
		{
			continue;
		}
		if (!(EC_POINT_mul(group, k_G, bn_k, NULL, NULL, ctx)))
		{
			goto clean_up;
		}
		if (!(EC_POINT_get_affine_coordinates_GFp(group,
			k_G,
			bn_x,
			bn_tmp,
			ctx)))
		{
			goto clean_up;
		}
		if (!(BN_mod_add(bn_r, bn_e, bn_x, bn_order, ctx)))
		{
			goto clean_up;
		}
		if (BN_is_zero(bn_r)) /* check if r==0 ? */
		{
			continue;
		}
		if (!(BN_add(bn_tmp, bn_r, bn_k)))
		{
			goto clean_up;
		}
		if (!(BN_cmp(bn_tmp, bn_order)))  /* check if (r + k) == n ? */
		{
			continue;
		}
		if (!(BN_add(bn_tmp, bn_one, bn_d)))  /* compute (1 + d) */
		{
			goto clean_up;
		}
		if (!(BN_mod_inverse(bn_sum_inv, bn_tmp, bn_order, ctx)))
		{
			goto clean_up;
		}
		if (!(BN_mul(bn_tmp, bn_r, bn_d, ctx)))
		{
			goto clean_up;
		}
		if (!(BN_mod_sub(bn_dif, bn_k, bn_tmp, bn_order, ctx)))
		{
			goto clean_up;
		}
		if (!(BN_mod_mul(bn_s, bn_sum_inv, bn_dif, bn_order, ctx)))
		{
			goto clean_up;
		}
	} while (BN_is_zero(bn_s));  /* check if s == 0 ? */

	if (BN_bn2binpad(bn_r,
		sm2_mes[i].r_coordinate,
		sizeof(sm2_mes[i].r_coordinate)) != sizeof(sm2_mes[i].r_coordinate))
	{
		goto clean_up;
	}
	if (BN_bn2binpad(bn_s,
		sm2_mes[i].s_coordinate,
		sizeof(sm2_mes[i].s_coordinate)) != sizeof(sm2_mes[i].s_coordinate))
	{
		goto clean_up;
	}
	error_code = 0;

clean_up:
	if (ctx)
	{
		BN_CTX_end(ctx);
		BN_CTX_free(ctx);
	}
	if (group)
	{
		EC_GROUP_free(group);
	}
	if (k_G)
	{
		EC_POINT_free(k_G);
	}

	return error_code;
}

/*********************************************************/
int sm2_verify_sig(const unsigned char* message,
	const int message_len,
	const unsigned char* id,
	const int id_len,
	const unsigned char* pub_key,
	const int i,
	const unsigned char* r,
	const unsigned char* s)
{
	int error_code;
	unsigned char digest[32];
	unsigned char pub_key_x[32], pub_key_y[32];
	BN_CTX* ctx = NULL;
	BIGNUM* bn_e = NULL, * bn_r = NULL, * bn_s = NULL, * bn_t = NULL;
	BIGNUM* bn_pub_key_x = NULL, * bn_pub_key_y = NULL;
	BIGNUM* bn_x = NULL, * bn_y = NULL, * bn_R = NULL;
	const BIGNUM* bn_order;
	EC_GROUP* group = NULL;
	const EC_POINT* generator;
	EC_POINT* ec_pub_key_pt = NULL, * ec_pt1 = NULL, * ec_pt2 = NULL;

	if (error_code = sm3_digest_with_preprocess(message,
		message_len,
		id,
		id_len,
		pub_key,
		digest))
	{
		return error_code;
	}

	memcpy(pub_key_x, (pub_key + 1), sizeof(pub_key_x));
	memcpy(pub_key_y, (pub_key + 1 + sizeof(pub_key_x)), sizeof(pub_key_y));

	error_code = ALLOCATION_MEMORY_FAIL;
	if (!(ctx = BN_CTX_new()))
	{
		goto clean_up;
	}
	BN_CTX_start(ctx);
	bn_e = BN_CTX_get(ctx);
	bn_r = BN_CTX_get(ctx);
	bn_s = BN_CTX_get(ctx);
	bn_t = BN_CTX_get(ctx);
	bn_pub_key_x = BN_CTX_get(ctx);
	bn_pub_key_y = BN_CTX_get(ctx);
	bn_x = BN_CTX_get(ctx);
	bn_y = BN_CTX_get(ctx);
	bn_R = BN_CTX_get(ctx);
	if (!(bn_R))
	{
		goto clean_up;
	}
	if (!(group = EC_GROUP_new_by_curve_name(NID_sm2)))
	{
		goto clean_up;
	}

	if (!(ec_pub_key_pt = EC_POINT_new(group)))
	{
		goto clean_up;
	}
	if (!(ec_pt1 = EC_POINT_new(group)))
	{
		goto clean_up;
	}
	if (!(ec_pt2 = EC_POINT_new(group)))
	{
		goto clean_up;
	}

	error_code = VERIFY_SM2_SIGNATURE_FAIL;
	if (!(BN_bin2bn(digest, sizeof(digest), bn_e)))
	{
		goto clean_up;
	}
	if (!(BN_bin2bn(sm2_mes[i].r_coordinate, sizeof(sm2_mes[i].r_coordinate), bn_r)))
	{
		goto clean_up;
	}
	if (!(BN_bin2bn(sm2_mes[i].s_coordinate, sizeof(sm2_mes[i].s_coordinate), bn_s)))
	{
		goto clean_up;
	}
	if (!(BN_bin2bn(pub_key_x, sizeof(pub_key_x), bn_pub_key_x)))
	{
		goto clean_up;
	}
	if (!(BN_bin2bn(pub_key_y, sizeof(pub_key_y), bn_pub_key_y)))
	{
		goto clean_up;
	}

	if (!(bn_order = EC_GROUP_get0_order(group)))
	{
		goto clean_up;
	}
	if (!(generator = EC_GROUP_get0_generator(group)))
	{
		goto clean_up;
	}

	if ((BN_is_zero(bn_r)) || (BN_cmp(bn_r, bn_order) != (-1)))
	{
		error_code = INVALID_SM2_SIGNATURE;
		goto clean_up;
	}
	if ((BN_is_zero(bn_s)) || (BN_cmp(bn_s, bn_order) != (-1)))
	{
		error_code = INVALID_SM2_SIGNATURE;
		goto clean_up;
	}

	if (!(BN_mod_add(bn_t, bn_r, bn_s, bn_order, ctx)))
	{
		goto clean_up;
	}
	if (BN_is_zero(bn_t))
	{
		goto clean_up;
	}

	if (!(EC_POINT_mul(group, ec_pt1, bn_s, NULL, NULL, ctx)))
	{
		goto clean_up;
	}

	if (!(EC_POINT_set_affine_coordinates_GFp(group,
		ec_pub_key_pt,
		bn_pub_key_x,
		bn_pub_key_y,
		ctx)))
	{
		goto clean_up;
	}

	if (!(EC_POINT_mul(group, ec_pt2, NULL, ec_pub_key_pt, bn_t, ctx)))
	{
		goto clean_up;
	}

	if (!(EC_POINT_add(group, ec_pt1, ec_pt1, ec_pt2, ctx)))
	{
		goto clean_up;
	}

	if (!(EC_POINT_get_affine_coordinates_GFp(group,
		ec_pt1,
		bn_x,
		bn_y,
		ctx)))
	{
		goto clean_up;
	}
	if (!(BN_mod_add(bn_R, bn_e, bn_x, bn_order, ctx)))
	{
		goto clean_up;
	}

	if (!(BN_cmp(bn_r, bn_R))) /* verify signature succeed */
	{
		error_code = 0;
	}

clean_up:
	if (ctx)
	{
		BN_CTX_end(ctx);
		BN_CTX_free(ctx);
	}
	if (group)
	{
		EC_GROUP_free(group);
	}

	if (ec_pub_key_pt)
	{
		EC_POINT_free(ec_pub_key_pt);
	}
	if (ec_pt1)
	{
		EC_POINT_free(ec_pt1);
	}
	if (ec_pt2)
	{
		EC_POINT_free(ec_pt2);
	}

	return error_code;
}

int test_sm2_sign_and_verify(void)
{
	int error_code;
	SM2_KEY_PAIR key_pair;
	int num;
	scanf_s("%d", &num);
	for (int i = 0; i < num; i++) {
		scanf_s("%s", &sm2_mes[i].msg, sizeof(sm2_mes[i].msg));
		scanf_s("%s", &sm2_mes[i].user_id, sizeof(sm2_mes[i].user_id));
	}

	for (int i = 0; i < num; i++) {
		sm2_mes[i].msg_len = (unsigned int)(strlen((char*)sm2_mes[i].msg));
		sm2_mes[i].user_id_len = (unsigned int)((strlen((char*)sm2_mes[i].user_id)));
	}

	long long t1 = GetTickCount64();
	if (error_code = sm2_create_key_pair(&key_pair))
	{
		printf("Create SM2 key pair failed!\n");
		return (-1);
	}
	printf("Create SM2 key pair succeeded!\n");
	printf("Private key:\n");
	for (int i = 0; i < sizeof(key_pair.pri_key); i++)
	{
		printf("0x%x  ", key_pair.pri_key[i]);
	}
	printf("\n\n");
	printf("Public key:\n");
	for (int i = 0; i < sizeof(key_pair.pub_key); i++)
	{
		printf("0x%x  ", key_pair.pub_key[i]);
	}
	printf("\n\n");
	long long t2 = GetTickCount64();
	printf("/*********************************************************/\n");
	long long t3 = GetTickCount64();
	for (int i = 0; i < num; i++) {
		if (error_code = sm2_sign_data(sm2_mes[i].msg,
			sm2_mes[i].msg_len,
			sm2_mes[i].user_id,
			sm2_mes[i].user_id_len,
			key_pair.pub_key,
			key_pair.pri_key,
			i,
			sm2_mes[i].r_coordinate,
			sm2_mes[i].s_coordinate))
		{
			printf("Create SM2 signature failed!\n");
			return error_code;
		}
		//printf("Create SM2 signature succeeded!\n");
		/*
		printf("SM2 signature:\n");
		printf("r coordinate:\n");
		for (int j = 0; j < sizeof(sm2_mes[i].r_coordinate); j++)
		{
			printf("0x%x  ", sm2_mes[i].r_coordinate[j]);
		}
		printf("\n");
		printf("s coordinate:\n");
		for (int j = 0; j < sizeof(sm2_mes[i].s_coordinate); j++)
		{
			printf("0x%x  ", sm2_mes[i].s_coordinate[j]);
		}

		printf("\n\n");
		*/
	}
	long long t4 = GetTickCount64();
	long long t5 = GetTickCount64();
	for (int i = 0; i < num; i++){

	
		if (error_code = sm2_verify_sig(sm2_mes[i].msg,
			sm2_mes[i].msg_len,
			sm2_mes[i].user_id,
			sm2_mes[i].user_id_len,
			key_pair.pub_key,
			i,
			sm2_mes[i].r_coordinate,
			sm2_mes[i].s_coordinate))
		{
			printf("Verify SM2 signature failed!\n");
			return error_code;
		}
		//printf("Verify SM2 signature succeeded!\n");
	}
	long long t6 = GetTickCount64();
	printf("密钥生成程序执行时间:%lld\n", t2 - t1);
	printf("签名程序执行时间:%lld\n", t4 - t3);
	printf("验签程序执行时间:%lld\n", t6 - t5);
	return 0;
}


sm3_with_preprocess.cpp



#include <string.h>
#include <openssl/evp.h>
#include "sm2_cipher_error_codes.h"
#include "sm3_with_preprocess.h"

/*********************************************************/
int sm3_digest_z(const unsigned char* id,
	const int id_len,
	const unsigned char* pub_key,
	unsigned char* z_digest)
{
	int id_bit_len = id_len * 8;
	unsigned char entl[2];
	unsigned char sm2_param_a[32] = { 0xff, 0xff, 0xff, 0xfe, 0xff, 0xff, 0xff, 0xff,
									 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
					 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00,
					 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc };
	unsigned char sm2_param_b[32] = { 0x28, 0xe9, 0xfa, 0x9e, 0x9d, 0x9f, 0x5e, 0x34,
								 0x4d, 0x5a, 0x9e, 0x4b, 0xcf, 0x65, 0x09, 0xa7,
					 0xf3, 0x97, 0x89, 0xf5, 0x15, 0xab, 0x8f, 0x92,
					 0xdd, 0xbc, 0xbd, 0x41, 0x4d, 0x94, 0x0e, 0x93 };
	unsigned char sm2_param_x_G[32] = { 0x32, 0xc4, 0xae, 0x2c, 0x1f, 0x19, 0x81, 0x19,
								   0x5f, 0x99, 0x04, 0x46, 0x6a, 0x39, 0xc9, 0x94,
					   0x8f, 0xe3, 0x0b, 0xbf, 0xf2, 0x66, 0x0b, 0xe1,
					   0x71, 0x5a, 0x45, 0x89, 0x33, 0x4c, 0x74, 0xc7 };
	unsigned char sm2_param_y_G[32] = { 0xbc, 0x37, 0x36, 0xa2, 0xf4, 0xf6, 0x77, 0x9c,
								   0x59, 0xbd, 0xce, 0xe3, 0x6b, 0x69, 0x21, 0x53,
					   0xd0, 0xa9, 0x87, 0x7c, 0xc6, 0x2a, 0x47, 0x40,
					   0x02, 0xdf, 0x32, 0xe5, 0x21, 0x39, 0xf0, 0xa0 };
	unsigned char x_coordinate[32];
	unsigned char y_coordinate[32];
	EVP_MD_CTX* md_ctx;
	const EVP_MD* md;

	if (!(id) || !(pub_key) || !(z_digest))
	{
		return INVALID_NULL_VALUE_INPUT;
	}

	if ((id_bit_len <= 0) || (id_bit_len > 65535))
	{
		return INVALID_INPUT_LENGTH;
	}

	entl[0] = (id_bit_len & 0xff00) >> 8;
	entl[1] = id_bit_len & 0xff;
	memcpy(x_coordinate, (pub_key + 1), sizeof(x_coordinate));
	memcpy(y_coordinate, (pub_key + 1 + sizeof(x_coordinate)), sizeof(y_coordinate));

	md = EVP_sm3();
	if (!(md_ctx = EVP_MD_CTX_new()))
	{
#ifdef _DEBUG
		printf("Allocate a digest context failed at %s, line %d!\n", __FILE__, __LINE__);
#endif
		return COMPUTE_SM3_DIGEST_FAIL;
	}
	EVP_DigestInit_ex(md_ctx, md, NULL);
	EVP_DigestUpdate(md_ctx, entl, sizeof(entl));
	EVP_DigestUpdate(md_ctx, id, id_len);
	EVP_DigestUpdate(md_ctx, sm2_param_a, sizeof(sm2_param_a));
	EVP_DigestUpdate(md_ctx, sm2_param_b, sizeof(sm2_param_b));
	EVP_DigestUpdate(md_ctx, sm2_param_x_G, sizeof(sm2_param_x_G));
	EVP_DigestUpdate(md_ctx, sm2_param_y_G, sizeof(sm2_param_y_G));
	EVP_DigestUpdate(md_ctx, x_coordinate, sizeof(x_coordinate));
	EVP_DigestUpdate(md_ctx, y_coordinate, sizeof(y_coordinate));
	EVP_DigestFinal_ex(md_ctx, z_digest, NULL);
	EVP_MD_CTX_free(md_ctx);
	return 0;
}

/*********************************************************/
int sm3_digest_with_preprocess(const unsigned char* message,
	const int message_len,
	const unsigned char* id,
	const int id_len,
	const unsigned char* pub_key,
	unsigned char* digest)
{
	int error_code;
	unsigned char z_digest[32];
	EVP_MD_CTX* md_ctx;
	const EVP_MD* md;

	if (error_code = sm3_digest_z(id,
		id_len,
		pub_key,
		z_digest))
	{
#ifdef _DEBUG
		printf("Compute SM3 digest of leading data Z failed at %s, line %d!\n", __FILE__, __LINE__);
#endif
		return COMPUTE_SM3_DIGEST_FAIL;
	}

	md = EVP_sm3();
	if (!(md_ctx = EVP_MD_CTX_new()))
	{
#ifdef _DEBUG
		printf("Allocate a digest context failed at %s, line %d!\n", __FILE__, __LINE__);
#endif
		return COMPUTE_SM3_DIGEST_FAIL;
	}
	EVP_DigestInit_ex(md_ctx, md, NULL);
	EVP_DigestUpdate(md_ctx, z_digest, sizeof(z_digest));
	EVP_DigestUpdate(md_ctx, message, message_len);
	EVP_DigestFinal_ex(md_ctx, digest, NULL);
	EVP_MD_CTX_free(md_ctx);
	return 0;
}

test_demo.cpp



#include <stdio.h>
#include <stdlib.h>
#include "sm2_sign_and_verify.h"

/*********************************************************/
int main(void)
{
	int error_code;

	printf("/*********************************************************/\n");

	if (error_code = test_sm2_sign_and_verify())
	{
		printf("Test create SM2 key pair, sign data and verify signature failed!\n");
		return error_code;
	}
	else
	{
		printf("Test create SM2 key pair, sign data and verify signature succeeded!\n");
	}


	return 0;
}

所有的头文件
sm2_cipher_error_codes.h

#pragma once


#ifndef HEADER_ERROR_CODES_LIST_OF_SM2_CIPHER_H
#define HEADER_ERROR_CODES_LIST_OF_SM2_CIPHER_H

#define INVALID_NULL_VALUE_INPUT    0x1000
#define INVALID_INPUT_LENGTH        0x1001
#define CREATE_SM2_KEY_PAIR_FAIL    0x1002
#define COMPUTE_SM3_DIGEST_FAIL     0x1003
#define ALLOCATION_MEMORY_FAIL      0x1004
#define COMPUTE_SM2_SIGNATURE_FAIL  0x1005
#define INVALID_SM2_SIGNATURE       0x1006
#define VERIFY_SM2_SIGNATURE_FAIL   0x1007

#endif  /* end of HEADER_ERROR_CODES_LIST_OF_SM2_CIPHER_H */

sm2_create_key_pair.h

#pragma once


#ifndef HEADER_SM2_CREATE_KEY_PAIR_H
#define HEADER_SM2_CREATE_KEY_PAIR_H

typedef struct sm2_key_pair_structure {
    /* Private key is a octet string of 32-byte length. */
    unsigned char pri_key[32];
    /* Public key is a octet string of 65 byte length. It is a
       concatenation of 04 || X || Y. X and Y both are SM2 public
       key coordinates of 32-byte length. */
    unsigned char pub_key[65];
} SM2_KEY_PAIR;

#ifdef  __cplusplus
extern "C" {
#endif

    /**************************************************
    * Name: sm2_create_key_pair
    * Function: create SM2 key pair, including private key
        and public key
    * Parameters:
        key_pair[in]  SM2 key pair
    * Return value:
        0:                function executes successfully
        any other value:  an error occurs
    **************************************************/
    int sm2_create_key_pair(SM2_KEY_PAIR* key_pair);

#ifdef  __cplusplus
}
#endif

#endif  /* end of HEADER_SM2_CREATE_KEY_PAIR_H */

sm2_sign_and_verify.h

#pragma once


#ifndef HEADER_SM2_SIGN_AND_VERIFY_COMPUTATION_H
#define HEADER_SM2_SIGN_AND_VERIFY_COMPUTATION_H



#ifdef  __cplusplus
extern "C" {
#endif
	int test_sm2_sign_and_verify(void);

	int sm2_sign_data(const unsigned char* message,
		const int message_len,
		const unsigned char* id,
		const int id_len,
		const unsigned char* pub_key,
		const unsigned char* pri_key,
		const int i,
		const unsigned char* r,
		const unsigned char* s);


	int sm2_verify_sig(const unsigned char* message,
		const int message_len,
		const unsigned char* id,
		const int id_len,
		const unsigned char* pub_key,
		const int i,
		const unsigned char* r,
		const unsigned char* s);

#ifdef  __cplusplus
}
#endif

#endif  /* end of HEADER_SM2_SIGN_AND_VERIFY_COMPUTATION_H */


sm3_with_preprocess.h

#pragma once


#ifndef HEADER_SM3_DIGEST_WTIH_PREPROCESS_COMPUTATION_H
#define HEADER_SM3_DIGEST_WTIH_PREPROCESS_COMPUTATION_H

#ifdef  __cplusplus
extern "C" {
#endif


    int sm3_digest_z(const unsigned char* id,
        const int id_len,
        const unsigned char* pub_key,
        unsigned char* z_digest);

    int sm3_digest_with_preprocess(const unsigned char* message,
        const int message_len,
        const unsigned char* id,
        const int id_len,
        const unsigned char* pub_key,
        unsigned char* digest);

#ifdef  __cplusplus
}
#endif

#endif  /* end of HEADER_SM3_DIGEST_WTIH_PREPROCESS_COMPUTATION_H */

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值