#if !defined(NO_SM2)
#include <stdio.h>
#include <time.h>
#include "alg/sm2.h"
#include "alg/sm3.h"
#include "../flkoutside.h"
#include "utils/flkutils.h"
#if defined(_WIN32) || defined(_WIN64)
#pragma comment(lib,"ws2_32.lib")
#include <WinSock.h>
#else
//#include <arpa/inet.h>
#endif
#define FLK_BN_BITS 64
#define FLK_BN_BYTES 4
#define FLK_BN_BITS2 32
#define FLK_BN_BITS4 16
#define FLK_BN_BITS8 8
#define FLK_BN_MASK2 (0xffffffffL)
#define FLK_BN_MASK2l (0xffff)
#define FLK_BN_MASK2h (0xffff0000L)
#define FLK_BN_TBIT (0x80000000L)
/**************************bn.h*******************/
/**************************ec_bn.h*******************/
#define FLK_BIGNUM_SIZE sizeof(FLKBIGNUM)
//ECC芯片参数结构
typedef struct ECCParameter_st
{
unsigned char p[ECC_BLOCK_LEN]; //模数p
unsigned char a[ECC_BLOCK_LEN]; //参数a
unsigned char b[ECC_BLOCK_LEN]; //参数b
unsigned char Gx[ECC_BLOCK_LEN]; //G点的x坐标
unsigned char Gy[ECC_BLOCK_LEN]; //G点的y坐标
unsigned char Gn[ECC_BLOCK_LEN]; //G点的阶
} ECCParameter;
//ECC公钥结构
typedef struct
{
unsigned char Qx[ECC_BLOCK_LEN]; //Q点的x坐标
unsigned char Qy[ECC_BLOCK_LEN]; //Q点的y坐标
} ECC_PUBLIC_KEY;
//ECC私钥结构
typedef struct
{
unsigned char Ka[ECC_BLOCK_LEN]; //私钥Ka
} ECC_PRIVATE_KEY;
//ECC签名值结构
typedef struct
{
unsigned char r[ECC_BLOCK_LEN];
unsigned char s[ECC_BLOCK_LEN];
} ECC_SIGNATURE;
//ECC加密值结构
typedef struct
{
unsigned char C1[2*ECC_BLOCK_LEN];
//unsigned char C2[ECC_BLOCK_LEN]; //和明文等长,最大是 ECC_BLOCK_LEN
unsigned int len;
unsigned char C2[ECC_MAX_ENCRYPT_LENGTH];
unsigned char C3[ECC_BLOCK_LEN];
} ECC_ENCRYPTION;
/**************************ec_bn.h*******************/
/**************************bn_lcl.h**********************/
#define Lw(t) (((FLK_BN_ULONG)(t))&FLK_BN_MASK2)
#define Hw(t) (((FLK_BN_ULONG)((t)>>FLK_BN_BITS2))&FLK_BN_MASK2)
#define LBITS(a) ((a)&FLK_BN_MASK2l)
#define HBITS(a) (((a)>>FLK_BN_BITS4)&FLK_BN_MASK2l)
#define L2HBITS(a) ((FLK_BN_ULONG)((a)&FLK_BN_MASK2l)<<FLK_BN_BITS4)
#define LLBITS(a) ((a)&BN_MASKl)
#define LHBITS(a) (((a)>>FLK_BN_BITS2)&BN_MASKl)
#define LL2HBITS(a) ((BN_ULLONG)((a)&BN_MASKl)<<FLK_BN_BITS2)
#define mul64(l,h,bl,bh) \
{ \
FLK_BN_ULONG m,m1,lt,ht; \
\
lt=l; \
ht=h; \
m =(bh)*(lt); \
lt=(bl)*(lt); \
m1=(bl)*(ht); \
ht =(bh)*(ht); \
m=(m+m1)&FLK_BN_MASK2; if (m < m1) ht+=L2HBITS(1L); \
ht+=HBITS(m); \
m1=L2HBITS(m); \
lt=(lt+m1)&FLK_BN_MASK2; if (lt < m1) ht++; \
(l)=lt; \
(h)=ht; \
}
#define mul_add(r,a,bl,bh,c) { \
FLK_BN_ULONG l,h; \
\
h= (a); \
l=LBITS(h); \
h=HBITS(h); \
mul64(l,h,(bl),(bh)); \
\
/* non-multiply part */ \
l=(l+(c))&FLK_BN_MASK2; if (l < (c)) h++; \
(c)=(r); \
l=(l+(c))&FLK_BN_MASK2; if (l < (c)) h++; \
(c)=h&FLK_BN_MASK2; \
(r)=l; \
}
#define mul(r,a,bl,bh,c) { \
FLK_BN_ULONG l,h; \
\
h= (a); \
l=LBITS(h); \
h=HBITS(h); \
mul64(l,h,(bl),(bh)); \
\
/* non-multiply part */ \
l+=(c); if ((l&FLK_BN_MASK2) < (c)) h++; \
(c)=h&FLK_BN_MASK2; \
(r)=l&FLK_BN_MASK2; \
}
/**************************bn_lcl.h**********************/
/**************************bn_lib.h**********************/
int BN_is_zero_sm2_ex(FLK_BN_ULONG *a, FLK_BN_ULONG al);
int BN_is_one_sm2_ex(FLK_BN_ULONG *a, FLK_BN_ULONG al);
void bn_fix_top_sm2_ex(FLK_BN_ULONG *a, int *al);
int BN_num_bits_word_sm2_ex(FLK_BN_ULONG l);
int BN_num_bits_sm2_ex(FLK_BN_ULONG *a, int al);
int BN_ucmp_sm2_ex(FLK_BN_ULONG *a, int al, FLK_BN_ULONG *b, int bl);
/**************************bn_lib.h**********************/
/**************************bn_asm.h**********************/
FLK_BN_ULONG bn_mul_add_words_sm2_ex(FLK_BN_ULONG *rp, const FLK_BN_ULONG *ap, int num, FLK_BN_ULONG w);
FLK_BN_ULONG bn_mul_words_sm2_ex(FLK_BN_ULONG *rp, const FLK_BN_ULONG *ap, int num, FLK_BN_ULONG w);
FLK_BN_ULONG bn_div_words_sm2_ex(FLK_BN_ULONG h, FLK_BN_ULONG l, FLK_BN_ULONG d);
FLK_BN_ULONG bn_add_words_sm2_ex(FLK_BN_ULONG *r, const FLK_BN_ULONG *a, const FLK_BN_ULONG *b, int n);
FLK_BN_ULONG bn_sub_words_sm2_ex(FLK_BN_ULONG *r, const FLK_BN_ULONG *a, const FLK_BN_ULONG *b, int n);
/**************************bn_asm.h**********************/
/**************************bn_add.h**********************/
int BN_uadd_sm2_ex(FLK_BN_ULONG *r, int *rl, FLK_BN_ULONG *a, int al, FLK_BN_ULONG *b, int bl);
int BN_usub_sm2_ex(FLK_BN_ULONG *r, int *rl, FLK_BN_ULONG *a, int al, FLK_BN_ULONG *b, int bl);
/**************************bn_add.h**********************/
/**************************bn_div.h**********************/
void BN_div_sm2_ex(FLK_BN_ULONG *dv, int *dv_len, FLK_BN_ULONG *rm, int *rm_len, FLK_BN_ULONG *num, int num_len, FLK_BN_ULONG *divisor, int divisor_len);
/**************************bn_div.h**********************/
/**************************bn_gcd.h**********************/
void BN_mod_inverse_sm2_ex(FLK_BN_ULONG *in, int *in_len, FLK_BN_ULONG *a, int a_len, FLK_BN_ULONG *n, int n_len);
/**************************bn_gcd.h**********************/
/**************************bn_mod.h**********************/
void BN_mod_add_sm2_ex(FLK_BN_ULONG *r, FLK_BN_ULONG *a, FLK_BN_ULONG *b, FLK_BN_ULONG *m, FLK_BN_ULONG mLen);
void BN_mod_sub_sm2_ex(FLK_BN_ULONG *r, int *rl, FLK_BN_ULONG *a, FLK_BN_ULONG *b, FLK_BN_ULONG *m, FLK_BN_ULONG mLen);
void BN_mod_lshift1_sm2_ex(FLK_BN_ULONG *r, FLK_BN_ULONG *a, FLK_BN_ULONG *m, FLK_BN_ULONG mLen);
/**************************bn_mod.h**********************/
/**************************bn_mount.h**********************/
void BN_MONT_CTX_set_sm2_ex(FLK_BN_ULONG *Mod, int ModLen, FLK_BN_ULONG *n0, FLK_BN_ULONG *RR);
void BN_mod_mul_montgomery_sm2_ex(FLK_BN_ULONG *r, FLK_BN_ULONG *a, FLK_BN_ULONG *b, FLK_BN_ULONG *M, int M_Len, FLK_BN_ULONG n0);
void BN_mod_mul_montgomery_one_sm2_ex(FLK_BN_ULONG *r, FLK_BN_ULONG *a, FLK_BN_ULONG *M, int M_Len, FLK_BN_ULONG n0);
/**************************bn_mount.h**********************/
/**************************bn_mul.h**********************/
void BN_mul_nomal_sm2_ex(FLK_BN_ULONG *r, FLK_BN_ULONG *a, int na, FLK_BN_ULONG *b, int nb);
void BN_mul_sm2_ex(FLK_BN_ULONG *r, int *rl, FLK_BN_ULONG *a, int al, FLK_BN_ULONG *b, int bl);
/**************************bn_mul.h**********************/
/**************************bn_shift.h**********************/
void BN_rshift1_sm2_ex(FLKBIGNUM *r, int *r_top, FLKBIGNUM *a, int a_top);
int BN_lshift_sm2_ex(FLK_BN_ULONG *r, int *rl, FLK_BN_ULONG *a, int al, int n);
int BN_rshift_sm2_ex(FLK_BN_ULONG *r, int *rl, FLK_BN_ULONG *a, int al, int n);
int two_number_same_ex(FLK_BN_ULONG *a, int len, FLK_BN_ULONG *b);
/**************************bn_shift.h**********************/
#ifndef SM2_DEBUG
//void myPrintBIGNUM(const char *p, FLKBIGNUM data);
void myPrintHex(const char *p, const unsigned char *data, int len);
//void myprintPoint(const char *p,EC_POINT *point);
/*void myPrintBIGNUM(const char *p, FLKBIGNUM data)
{
unsigned int i;
char *buf;
buf = (char *)malloc(strlen(p) + sizeof(data)*2 + 1024);
if(buf == NULL)
{
return ;
}
memset(buf, 0, strlen(p) + sizeof(data)*2 + 1024);
sprintf(buf, "%s\n", p);
for(i = 0; i < sizeof(data)/sizeof(FLK_BN_ULONG); i++)
{
sprintf(buf + strlen(buf), "%x ", data.d[i]);
}
sprintf(buf + strlen(buf), "\n");
flk_printf("%s\n", buf);
}
void myprintPoint(const char *p,EC_POINT *point)
{
flk_printf("%s:\n", p);
myPrintBIGNUM("point X", point->X);
myPrintBIGNUM("point Y", point->Y);
myPrintBIGNUM("point Z", point->Z);
}
*/
void myPrintHex(const char *p, const unsigned char *data, int len)
{
int i;
unsigned char *buf;
show("%s\n", p);
for (i = 0; i < len; i++)
{
show("%02x ", data[i]);
}
show("\n");
}
#define PrintBIGNUM
#define PrintHex myPrintHex
#define printPoint
#else
#define PrintBIGNUM
#define PrintHex
#define printPoint
#endif
struct ec_group_st {
FLKBIGNUM field;
/* Field specification.
* For curves over GF(p), this is the modulus. */
FLK_BN_ULONG field_top; /* Field length */
FLKBIGNUM a,b;
/* Curve coefficients.
* (Here the assum
评论2