基于mbedTLS算法库实现国密SM2签名和验签算法
网上找到的都是这一篇文章,先放过来,有空的时候来手动验证一下看看。
基于mbedTLS算法库实现国密SM2签名和验签算法
update:2021-9-28
曲线参数:
static const mbedtls_mpi_uint sm2256_p[] = {
BYTES_TO_T_UINT_8(0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF),
BYTES_TO_T_UINT_8(0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF),
BYTES_TO_T_UINT_8(0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF),
BYTES_TO_T_UINT_8(0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0xFF, 0xFF, 0xFF),
};
static const mbedtls_mpi_uint sm2256_a[] = {
BYTES_TO_T_UINT_8(0xFC, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF),
BYTES_TO_T_UINT_8(0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF),
BYTES_TO_T_UINT_8(0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF),
BYTES_TO_T_UINT_8(0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0xFF, 0xFF, 0xFF),
};
static const mbedtls_mpi_uint sm2256_b[] = {
BYTES_TO_T_UINT_8(0x93, 0x0E, 0x94, 0x4D, 0x41, 0xBD, 0xBC, 0xDD),
BYTES_TO_T_UINT_8(0x92, 0x8F, 0xAB, 0x15, 0xF5, 0x89, 0x97, 0xF3),
BYTES_TO_T_UINT_8(0xA7, 0x09, 0x65, 0xCF, 0x4B, 0x9E, 0x5A, 0x4D),
BYTES_TO_T_UINT_8(0x34, 0x5E, 0x9F, 0x9D, 0x9E, 0xFA, 0xE9, 0x28),
};
static const mbedtls_mpi_uint sm2256_gx[] = {
BYTES_TO_T_UINT_8(0xC7, 0x74, 0x4C, 0x33, 0x89, 0x45, 0x5A, 0x71),
BYTES_TO_T_UINT_8(0xE1, 0x0B, 0x66, 0xF2, 0xBF, 0x0B, 0xE3, 0x8F),
BYTES_TO_T_UINT_8(0x94, 0xC9, 0x39, 0x6A, 0x46, 0x04, 0x99, 0x5F),
BYTES_TO_T_UINT_8(0x19, 0x81, 0x19, 0x1F, 0x2C, 0xAE, 0xC4, 0x32),
};
static const mbedtls_mpi_uint sm2256_gy[] = {
BYTES_TO_T_UINT_8(0xA0, 0xF0, 0x39, 0x21, 0xE5, 0x32, 0xDF, 0x02),
BYTES_TO_T_UINT_8(0x40, 0x47, 0x2A, 0xC6, 0x7C, 0x87, 0xA9, 0xD0),
BYTES_TO_T_UINT_8(0x53, 0x21, 0x69, 0x6B, 0xE3, 0xCE, 0xBD, 0x59),
BYTES_TO_T_UINT_8(0x9C, 0x77, 0xF6, 0xF4, 0xA2, 0x36, 0x37, 0xBC),
};
static const mbedtls_mpi_uint sm2256_n[] = {
BYTES_TO_T_UINT_8(0x23, 0x41, 0xD5, 0x39, 0x09, 0xF4, 0xBB, 0x53),
BYTES_TO_T_UINT_8(0x2B, 0x05, 0xC6, 0x21, 0x6B, 0xDF, 0x03, 0x72),
BYTES_TO_T_UINT_8(0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF),
BYTES_TO_T_UINT_8(0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0xFF, 0xFF, 0xFF),
};
签名过程:
1.对待签名数据进行哈希算法(国密规范里还规定了使用用户ID,曲线参数等生成Z的过程,这里不考虑那些过程,直接处理最后哈希后的数据),得到e;
2.先生成一个SM2密钥对,私钥:k,公钥:kG = (x,y);
3.计算r = (e+x) mod n;
4.如果r=0 或者r+k=n返回步骤2;
5.s=((1+d)^-1)(k-rd) mod n ;
6.如果s=0 返回 2;
7.签名结果(r,s).
code:
int mbedtls_ecdsa_sm2_sign(mbedtls_ecp_group grp, mbedtls_mpi r, mbedtls_mpi *s,
const mbedtls_mpi d, const unsigned char buf, size_t blen,
int(f_rng)(void , unsigned char , size_t), void p_rng)
{
int ret, key_tries, sign_tries, blind_tries;
mbedtls_ecp_point R;
mbedtls_mpi k, e, t, l, m;
if (grp->N.p == NULL)
return(MBEDTLS_ERR_ECP_BAD_INPUT_DATA);
mbedtls_ecp_point_init(&R);
mbedtls_mpi_init(&k); mbedtls_mpi_init(&e); mbedtls_mpi_init(&t); mbedtls_mpi_init(&l);
mbedtls_mpi_init(&m);
sign_tries = 0;
do
{
MBEDTLS_MPI_CHK(derive_mpi(grp, &e, buf, blen));
key_tries = 0;
do
{
MBEDTLS_MPI_CHK(mbedtls_ecp_gen_keypair(grp, &k, &R, f_rng, p_rng));
MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&l, &e, &R.X));
MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(r, &l, &grp->N));
if (key_tries++ > 10)
{
ret = MBEDTLS_ERR_ECP_RANDOM_FAILED;
goto cleanup;
}
//r+k != n
MBEDTLS_MPI_CHK((mbedtls_mpi_add_mpi(&m, r, &k)));
} while ((mbedtls_mpi_cmp_int(r, 0) == 0)|| (mbedtls_mpi_cmp_mpi(&m, &grp->N) == 0));
blind_tries = 0;
do
{
size_t n_size = (grp->nbits + 7) / 8;
MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(&t, n_size, f_rng, p_rng));
MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&t, 8 * n_size - grp->nbits));
if (++blind_tries > 30)
return(MBEDTLS_ERR_ECP_RANDOM_FAILED);
} while (mbedtls_mpi_cmp_int(&t, 1) < 0 ||
mbedtls_mpi_cmp_mpi(&t, &grp->N) >= 0);
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(s, r, d)); //s = r*d
MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(s, &k, s)); //s = k - s
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(s, s, &t));//s = s*t
MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&l, d, 1));//l = 1+d
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&l, &l, &t));//l=l*t
MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod(&l, &l, &grp->N));// l = l^-1
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(s, s, &l));//s = s * l
MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(s, s, &grp->N));//s mod n
if (sign_tries++ > 10)
{
ret = MBEDTLS_ERR_ECP_RANDOM_FAILED;
goto cleanup;
}
//
} while (mbedtls_mpi_cmp_int(&t, 1) < 0 ||
mbedtls_mpi_cmp_mpi(&t, &grp->N) >= 0);
cleanup:
mbedtls_ecp_point_free(&R);
mbedtls_mpi_free(&k); mbedtls_mpi_free(&e); mbedtls_mpi_free(&t);
mbedtls_mpi_free(&l); mbedtls_mpi_free(&m);
return (ret);
}
int mbedtls_ecdsa_sm2_sign_det(mbedtls_ecp_group grp, mbedtls_mpi r, mbedtls_mpi *s,
const mbedtls_mpi d, const unsigned char buf, size_t blen,
mbedtls_md_type_t md_alg)
{
int ret;
mbedtls_hmac_drbg_context rng_ctx;
unsigned char data[2 * MBEDTLS_ECP_MAX_BYTES];
size_t grp_len = (grp->nbits + 7) / 8;
const mbedtls_md_info_t *md_info;
mbedtls_mpi h;
if ((md_info = mbedtls_md_info_from_type(md_alg)) == NULL)
return(MBEDTLS_ERR_ECP_BAD_INPUT_DATA);
mbedtls_mpi_init(&h);
mbedtls_hmac_drbg_init(&rng_ctx);
MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(d, data, grp_len));
MBEDTLS_MPI_CHK(derive_mpi(grp, &h, buf, blen));
MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&h, data + grp_len, grp_len));
mbedtls_hmac_drbg_seed_buf(&rng_ctx, md_info, data, 2 * grp_len);
ret = mbedtls_ecdsa_sm2_sign(grp, r, s, d, buf, blen,
mbedtls_hmac_drbg_random, &rng_ctx);
cleanup:
mbedtls_hmac_drbg_free(&rng_ctx);
mbedtls_mpi_free(&h);
return(ret);
}
验证签名:
1.e = hash(m);
2.计算t = (r + s) mod n,如果t=0验签失败;
3.计算椭圆曲线上的点(x,y) = sG + tP
4.计算R = (e + x) mod n 如果R=r那么签名正确,否则签名验证失败.
code:
int mbedtls_ecdsa_sm2_verify(mbedtls_ecp_group *grp,
const unsigned char *buf, size_t blen,
const mbedtls_ecp_point Q, const mbedtls_mpi r, const mbedtls_mpi *s)
{
int ret;
mbedtls_mpi e, s_inv, u1, u2, t, result;
mbedtls_ecp_point R;
mbedtls_ecp_point_init(&R);
mbedtls_mpi_init(&e); mbedtls_mpi_init(&s_inv); mbedtls_mpi_init(&u1); mbedtls_mpi_init(&u2);
mbedtls_mpi_init(&t); mbedtls_mpi_init(&result);
if (grp->N.p == NULL)
return(MBEDTLS_ERR_ECP_BAD_INPUT_DATA);
if (mbedtls_mpi_cmp_int(r, 1) < 0 || mbedtls_mpi_cmp_mpi(r, &grp->N) >= 0 ||
mbedtls_mpi_cmp_int(s, 1) < 0 || mbedtls_mpi_cmp_mpi(s, &grp->N) >= 0)
{
ret = MBEDTLS_ERR_ECP_VERIFY_FAILED;
goto cleanup;
}
MBEDTLS_MPI_CHK(mbedtls_ecp_check_pubkey(grp, Q));
MBEDTLS_MPI_CHK(derive_mpi(grp, &e, buf, blen));
MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&t, r, s));
MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&t, &t, &grp->N));
if (mbedtls_mpi_cmp_int(&t, 0) == 0)
{
ret = MBEDTLS_ERR_ECP_VERIFY_FAILED;
goto cleanup;
}
MBEDTLS_MPI_CHK(mbedtls_ecp_muladd(grp, &R, s, &grp->G, &t, Q));
MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&e, &e, &R.X));
MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&result, &e, &grp->N));
if (mbedtls_mpi_cmp_mpi(&result, r) != 0)
{
ret = MBEDTLS_ERR_ECP_VERIFY_FAILED;
goto cleanup;
}
//
cleanup:
mbedtls_ecp_point_free(&R);
mbedtls_mpi_free(&e); mbedtls_mpi_free(&s_inv); mbedtls_mpi_free(&u1); mbedtls_mpi_free(&u2);
mbedtls_mpi_free(&t); mbedtls_mpi_free(&result);
return(ret);
}
Referenced from:http://blog.sina.com.cn/s/blog_4b9c791e0102xqeq.html
本文链接地址:https://const.net.cn/438.html