資源簡介
在goldboar寫的SM2簽名及驗簽函數( http://download.csdn.net/detail/goldboar/3833072)的基礎上,改寫的一個純粹用來做SM2簽名驗證的函數,編譯時需要用到OpenSSL的頭文件和庫文件(libeay32.lib或libeay32.dll),與goldboar的程序區別如下:
1.僅用于做驗簽,不能簽名;
2.驗簽使用外部傳入的SM2公鑰,SM2公鑰以(x,y)坐標形式傳入;
3.簽名也是以(r,s)坐標形式傳入;
4.增加了一些內存清理語句,內存泄漏有改善;
5.goldboar的程序中使用的ECC參數是示例參數,不是GM/T 0003.5-2012規范中定義的參數,這里的驗簽函數中采用的是規范中定義的參數。
6.將一些對橢圓曲線參數的驗證操作放入 _DEBUG 宏限制的范圍內。因為參數是規范推薦的,已經過驗證,所以在程序中無需再驗證。將這些驗證語句放入 _DEBUG 宏限制的范圍內以后,如果編譯 release 版本時就不會包含這些驗證語句,效率可以有一點提升。

代碼片段和文件信息
//?derived?from?goldboar‘s?program:?http://download.csdn.net/detail/goldboar/3833072
#include?“sm2_custom.h“
#include?
#include?
#include?
/*********************************************************/
int?BNPrintf(BIGNUM*?bn)
{
??char?*p=NULL;
??p=BN_bn2hex(bn);
??printf(“%s“p);
??OPENSSL_free(p);
??return?0;
}
/*********************************************************/
int?sm2_do_verify(const?unsigned?char?*dgst
??????????????int?dgst_len
??const?ECDSA_SIG?*sig
??EC_KEY?*eckey)
{
??int?ret=(-1)?i;
??BN_CTX?*ctx;
??BIGNUM?*order?*R?*m?*X?*t;
??EC_POINT?*point=NULL;
??const?EC_GROUP?*group=NULL;
??const?EC_POINT?*pub_key=NULL;
/*?check?input?values?*/
??if?(?(!eckey)?||?(?!(group?=?EC_KEY_get0_group(eckey))?)?||
????????(?!(pub_key?=?EC_KEY_get0_public_key(eckey))?)?||?(!sig)?)
return?ret;
??if?(?!(ctx?=?BN_CTX_new())?)
return?ret;
??BN_CTX_start(ctx);
??order?=?BN_CTX_get(ctx);
??R?=?BN_CTX_get(ctx);
??t?=?BN_CTX_get(ctx);
??m?=?BN_CTX_get(ctx);
??X?=?BN_CTX_get(ctx);
??if?(?(!order)?||?(!R)?||?(!t)?||?(!m)?||?(!X)?)
goto?clean_memory;
??if?(?!(EC_GROUP_get_order(group?order?ctx))?)
goto?clean_memory;
??if?(?BN_is_zero(sig->r)????????????||?BN_is_negative(sig->r)?||?
???(BN_ucmp(sig->r?order)?>=?0)?||?BN_is_zero(sig->s)?????||
???BN_is_negative(sig->s)????????||?(BN_ucmp(sig->s?order)?>=?0)?)
??{
ret=0;??/*?signature?is?invalid?*/
goto?clean_memory;
??}
//t?=(r+s)?mod?n
??if?(?!(BN_mod_add_quick(t?sig->s?sig->rorder))?)
goto?clean_memory;
??if?(?BN_is_zero(t)?)
??{
ret=0;??/*?signature?is?invalid?*/
goto?clean_memory;
??}
#ifdef?_DEBUG
??printf(“\nsig->r?=?0x“);
??BNPrintf(sig->r);
??printf(“\n“);
??printf(“sig->s?=?0x“);
??BNPrintf(sig->s);
??printf(“\n“);
??printf(“\nt?=?0x“);
??BNPrintf(t);
??printf(“\n“);
#endif
//point?=?s*G+t*PA
??if?(?!(point?=?EC_POINT_new(group))?)
goto?clean_memory;
??if?(?!(EC_POINT_mul(group?point?sig->s?pub_key?t?ctx))?)
goto?clean_memory;
??if?(?EC_METHOD_get_field_type(EC_GROUP_method_of(group))?==?NID_X9_62_prime_field?)
??{
if?(?!(EC_POINT_get_affine_coordinates_GFp(group?point?X?NULL?ctx))?)
??goto?clean_memory;
??}
??else?/*?NID_X9_62_characteristic_two_field?*/
??{
if?(?!(EC_POINT_get_affine_coordinates_GF2m(group?point?X?NULL?ctx))?)
??goto?clean_memory;
??}
??i?=?BN_num_bits(order);
#ifdef?_DEBUG
??printf(“EC?order?=?%d?bits\n“?i);
#endif
/*?Need?to?truncate?digest?if?it?is?too?long:?first?truncate?whole?bytes. */
??if?(?(8?*?dgst_len)?>?i?)
????dgst_len?=?(i?+?7)/8;
??if?(?!(BN_bin2bn(dgst?dgst_len?m))?)
goto?clean_memory;
/*?If?still?too?long?truncate?remaining?bits?with?a?shift?*/
??if?(?((8?*?dgst_len)?>?i)?&&?(!(BN_rshift(m?m?8?-?(i?&?0x7))))?)
goto?clean_memory;
/*?R?=?m?+?X?mod?order?*/
??if?(?!(BN_mod_add_quick(R?m?X?order))?)
goto?clean_memory;
#ifdef?_DEBUG
??printf(
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件????????8408??2014-07-15?14:21??sm2_custom.c
?????文件????????2540??2014-07-15?11:30??sm2_custom.h
?????文件????????3095??2014-07-15?14:18??test_main.c
評論
共有 條評論