资源简介
国密算法的QT实现,SM2,SM3,SM4算法,国家机关佛教国家和分级方法和
代码片段和文件信息
// \file:sm2.c
//SM2 Algorithm
//2011-11-10
//author:goldboar
//email:goldboar@163.com
//depending:opnessl library
//SM2 Standards: http://www.oscca.gov.cn/News/201012/News_1197.htm
#include
#include
#include
#include
#include
#include
#include
#include “kdf.h“
#define NID_X9_62_prime_field 406
static void BNPrintf(BIGNUM* bn)
{
char *p=NULL;
p=BN_bn2hex(bn);
printf(“%s“p);
OPENSSL_free(p);
}
static int sm2_sign_setup(EC_KEY *eckey BN_CTX *ctx_in BIGNUM **kp BIGNUM **rp)
{
BN_CTX *ctx = NULL;
BIGNUM *k = NULL *r = NULL *order = NULL *X = NULL;
EC_POINT *tmp_point=NULL;
const EC_GROUP *group;
int ret = 0;
if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL)
{
ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP ERR_R_PASSED_NULL_PARAMETER);
return 0;
}
if (ctx_in == NULL)
{
if ((ctx = BN_CTX_new()) == NULL)
{
ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUPERR_R_MALLOC_FAILURE);
return 0;
}
}
else
ctx = ctx_in;
k = BN_new(); /* this value is later returned in *kp */
r = BN_new(); /* this value is later returned in *rp */
order = BN_new();
X = BN_new();
if (!k || !r || !order || !X)
{
ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP ERR_R_MALLOC_FAILURE);
goto err;
}
if ((tmp_point = EC_POINT_new(group)) == NULL)
{
ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP ERR_R_EC_LIB);
goto err;
}
if (!EC_GROUP_get_order(group order ctx))
{
ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP ERR_R_EC_LIB);
goto err;
}
do
{
/* get random k */
do
if (!BN_rand_range(k order))
{
ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP ECDSA_R_RANDOM_NUMBER_GENERATION_FAILED);
goto err;
}
while (BN_is_zero(k));
/* compute r the x-coordinate of generator * k */
if (!EC_POINT_mul(group tmp_point k NULL NULL ctx))
{
ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP ERR_R_EC_LIB);
goto err;
}
if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) == NID_X9_62_prime_field)
{
if (!EC_POINT_get_affine_coordinates_GFp(group
tmp_point X NULL ctx))
{
ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUPERR_R_EC_LIB);
goto err;
}
}
else /* NID_X9_62_characteristic_two_field */
{
if (!EC_POINT_get_affine_coordinates_GF2m(group
tmp_point X NULL ctx))
{
ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUPERR_R_EC_LIB);
goto err;
}
}
if (!BN_nnmod(r X order ctx))
{
ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP ERR_R_BN_LIB);
goto err;
}
}
while (BN_is_zero(r));
/* compute the inverse of k */
// if (!BN_mod_inverse(k k order ctx))
// {
// ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP ERR_R_BN_LIB);
// goto err;
// }
/* clear old values if necessary */
if (*rp != NULL)
BN_clear_free(*rp);
if (*kp != NULL)
BN_clear_free(*kp);
/* save the pre-computed values */
评论
共有 条评论