资源简介
利用miracl.lib 大数库实现SM2加解密算法。SM2椭圆曲线加密解密,在博客里有实现的效果
代码片段和文件信息
#include
#include
#include
#include “sm2.h“
#include
#define SM2_PAD_ZERO TRUE
#define SM2_DEBUG 0
struct FPECC {
char *p;
char *a;
char *b;
char *n;
char *x;
char *y;
};
///*SM2*/
struct FPECC Ecc256 = {
“FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFF“//p
“FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFC“//a
“28E9FA9E9D9F5E344D5A9E4BCF6509A7F39789F515AB8F92DDBCBD414D940E93“//b
“FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFF7203DF6B21C6052B53BBF40939D54123“//n
“32C4AE2C1F1981195F9904466A39C9948FE30BBFF2660BE1715A4589334C74C7“//Gx
“BC3736A2F4F6779C59BDCEE36B692153D0A9877CC62A474002DF32E52139F0A0“//Gy
};
#define SEED_CONST 0x1BD8C95A
//秘钥生成函数
void sm2_keygen(unsigned char *wx int *wxlen unsigned char *wy int *wylen unsigned char *privkey int *privkeylen)
{
struct FPECC *cfig = &Ecc256;
epoint *g;
big a b p n x y key1;
miracl *mip = mirsys(20 0);
mip->IObase = 16;
p = mirvar(0);
a = mirvar(0);
b = mirvar(0);
n = mirvar(0);
x = mirvar(0);
y = mirvar(0);
key1 = mirvar(0);
//将给定数据赋值给对应变量
cinstr(p cfig->p);
cinstr(a cfig->a);
cinstr(b cfig->b);
cinstr(n cfig->n);
cinstr(x cfig->x);
cinstr(y cfig->y);
//初始化椭圆曲线
ecurve_init(a b p MR_PROJECTIVE);
g = epoint_init();
epoint_set(x y 0 g);
//初始化随机种子
irand(time(NULL) + SEED_CONST);
bigrand(n key1); //随机数dB
ecurve_mult(key1 g g);//倍点运算后的g点即为PB
epoint_get(g x y);//得到PB的x和y坐标
*wxlen = big_to_bytes(32 x (char *)wx TRUE);
*wylen = big_to_bytes(32 y (char *)wy TRUE);
*privkeylen = big_to_bytes(32 key1 (char *)privkey TRUE);
mirkill(key1);
mirkill(p);
mirkill(a);
mirkill(b);
mirkill(n);
mirkill(x);
mirkill(y);
epoint_free(g);
mirexit();
}
//秘钥派生函数
int kdf(unsigned char *zl unsigned char *zr int klen unsigned char *kbuf)
{
unsigned char buf[70];
unsigned char digest[32];
unsigned int ct = 0x00000001;
int i m n;
unsigned char *p;
memcpy(buf zl 32);
memcpy(buf + 32 zr 32);
m = klen / 32;
n = klen % 32;
p = kbuf;
for (i = 0; i < m; i++)
{
buf[64] = (ct >> 24) & 0xFF;
buf[65] = (ct >> 16) & 0xFF;
buf[66] = (ct >> 8) & 0xFF;
buf[67] = ct & 0xFF;
sm3(buf 68 p);
p += 32;
ct++;
}
if (n != 0)
{
buf[64] = (ct >> 24) & 0xFF;
buf[65] = (ct >> 16) & 0xFF;
buf[66] = (ct >> 8) & 0xFF;
buf[67] = ct & 0xFF;
sm3(buf 68 digest);
}
memcpy(p digest n);
for (i = 0; i < klen; i++)
{
if (kbuf[i] != 0)
break;
}
if (i < klen)
return 1;
else
return 0;
}
int sm2_encrypt(unsigned char *msg int msglen unsigned char *wx int wxlen unsigned char *wy int wylen unsigned char *outmsg)
{
struct FPECC *cfig = &Ecc256;
big x2 y2 c1 c2 k;
big a b p n x y;
epoint *g *w;
int ret = -1;
int i;
unsigned char zl[32] zr[32];
unsigned char *tmp;
miracl *mip;
tmp = malloc(msglen +
相关资源
- SM2椭圆曲线算法软件.rar
- SM2_SM3_SM4计算常用工具
- SM2258东芝颗粒
- SM2椭圆曲线门限方案
- UBNT NSM2 官方5.3.5固件
- 密码函数库miracle的visual studio 2017可用
- miracl库-32位-64位
- 商密SM4-SM3-SM2FPGA实现源码.rar
- 国密SM2 / SM3 / SM4 / SM9 / ZUC算法的官方
- 国密算法SM2 SM3 SM4相关算法及推荐参数
- SM2椭圆曲线公钥密码算法
- iOS国密demoSM2 SM3 SM4 ZUC EEA3 EIA3
- GM T 0009-2012 SM2密码算法使用规范
- 国密算法SM3验证与SM4文件加密工具C
- miracl库及使用手册和头文件miracl.lb及
- 密码学库 miracle库
- 基于MIRACL库下的SM2密钥交换的实现
- miracl_5.5.4_包含vs工程
- 大数库miracl 7.0.1 附带vs2010编译工程及
- 慧荣SM2256主控开卡工具包
- 慧荣SMI SM2246ENAA_2246AB芯片量产工具V
- miracl库以及编译的会用到两个lib库
- SM2椭圆曲线公钥密码算法(完整)
- AB modbus通讯模块手册
- Miracle.rar
- 基于SM2密码算法的证书认证系统密码
- 国密算法文档和代码。基于openssl实现
- openssl1.1.1RSA、ECC、SM2身份认证Demo及文
- sm2_ssl.rar
- 慧荣SM2246EN SSD固态硬盘开卡工具(东
评论
共有 条评论