77 lines
1.8 KiB
C
77 lines
1.8 KiB
C
#include <stdio.h>
|
||
#include <time.h>
|
||
#include <string.h>
|
||
#include "utils.h"
|
||
#include "ecurve.h"
|
||
#include "kgc.h"
|
||
|
||
//将big大数类型转为char*类型
|
||
void outbig(big num, char *val_name)
|
||
{
|
||
char out_str[257] = {0};
|
||
cotstr(num, out_str);
|
||
printf("\nchar str_%s[] = \"%s\";", val_name, out_str);
|
||
}
|
||
|
||
//将big大数类型转为char*类型
|
||
void outpoint(epoint *PO, char *val_name)
|
||
{
|
||
char out_str[257] = {0};
|
||
big PO_x = mirvar(0);
|
||
big PO_y = mirvar(0);
|
||
epoint_get(PO, PO_x, PO_y);
|
||
cotstr(PO_x, out_str);
|
||
printf("\nchar str_%s_x[]= \"%s\";", val_name, out_str);
|
||
cotstr(PO_y, out_str);
|
||
printf("\nchar str_%s_y[]= \"%s\";", val_name, out_str);
|
||
}
|
||
|
||
// 设置随机数种子
|
||
void setRandSeed()
|
||
{
|
||
time_t seed;
|
||
time(&seed); // 用系统时间做种子
|
||
irand((long)seed);
|
||
return;
|
||
}
|
||
|
||
//
|
||
void sha256_update_string(sha256 sh, const char *data, long data_len)
|
||
{
|
||
for (long i = 0; i < data_len; i++)
|
||
{
|
||
shs256_process(&sh, data[i]);
|
||
}
|
||
}
|
||
|
||
void sha256_update_point(sha256 sh, epoint *point)
|
||
{
|
||
big point_x = mirvar(0);
|
||
big point_y = mirvar(0);
|
||
char point_x_string[256] = {0};
|
||
char point_y_string[256] = {0};
|
||
epoint_get(point, point_x, point_y);
|
||
cotstr(point_x, point_x_string);
|
||
cotstr(point_y, point_y_string);
|
||
|
||
for (unsigned int i = 0; i < strlen(point_x_string); i++)
|
||
{
|
||
shs256_process(&sh, point_x_string[i]);
|
||
}
|
||
|
||
for (unsigned int i = 0; i < strlen(point_y_string); i++)
|
||
{
|
||
shs256_process(&sh, point_y_string[i]);
|
||
}
|
||
|
||
mirkill(point_x);
|
||
mirkill(point_y);
|
||
}
|
||
|
||
//用户产生秘密值x,以及与基点点乘后的X
|
||
void genSecret(ECC_PARAMS *params, big x, epoint *X)
|
||
{
|
||
bigrand((*params).p, x); //产生小于阶p的big值
|
||
ecurve_mult(x, (*params).P, X);
|
||
}
|