1.头文件部分
#include
#include
#include
2.判断大数是不是0或1
参见《C语言实现RSA算法》
3.大数加减乘除幂模
参见《C语言实现RSA算法》
4.先乘积后取模的函数
int big_num_mul_mod(unsigned long *a, unsigned long *b, unsigned long *c, unsigned long *s, int len)
{
unsigned long d[2*len];
unsigned long e[2*len];
unsigned long f[2*len];
memset(d, 0x00, sizeof(d));
memset(e, 0x00, sizeof(e));
memset(f, 0x00, sizeof(f));
memcpy(e+len, c, 4*len);
big_num_mul(a, b, d, len);
big_num_mod(d, e, f, 2*len);
memcpy(s, f+len, 4*len);
return 0;
}
5.根据私钥求公钥
int dsa_get_key(unsigned long *p, unsigned long *q, unsigned long *h, unsigned long *g, unsigned long *x, unsigned long *y, int len)
{
unsigned long a[len];
unsigned long b[len];
memset(a, 0x00, sizeof(a));
memset(b, 0x00, sizeof(b));
a[len-1] = 1;
printf("key p: ");
print_num(p, len);
printf("key a: ");
print_num(a, len);
big_num_sub(p, a, b, len);
a[len-1] = 0;
big_num_div(b, q, a, len);
big_num_pow_mod(h, a, p, g, len);
big_num_pow_mod(g, x, p, y, len);
printf("key g: ");
print_num(g, len);
printf("key y: ");
print_num(y, len);
return 0;
}
6.签名函数
int dsa_set_sig(unsigned long *p, unsigned long *q, unsigned long *g, unsigned long *x, unsigned long *k, unsigned long *m, unsigned long *r, unsigned long *s, int len)
{
unsigned long a[len];
unsigned long b[len];
unsigned long c[len];
unsigned long d[len];
unsigned long e[len];
unsigned long f[len];
memset(a, 0x00, sizeof(a));
memset(b, 0x00, sizeof(b));
memset(c, 0x00, sizeof(c));
memset(d, 0x00, sizeof(d));
memset(e, 0x00, sizeof(e));
memset(f, 0x00, sizeof(f));
big_num_pow_mod(g, k, p, a, len);
printf("step 1 end\n");
big_num_mod(a, q, r, len);
printf("step 2 end\n");
big_num_mod_inv(k, q, b, len);
printf("step 3 end\n");
big_num_mul_mod(x, r, q, c, len);
printf("step 4 end\n");
big_num_mod(m, q, d, len);
printf("step 5 end\n");
big_num_add(c, d, e, len);
printf("step 6 end\n");
big_num_mod(e, q, f, len);
printf("step 7 end\n");
big_num_mul_mod(b, f, q, s, len);
printf("step 8 end\n");
printf("key m: ");
print_num(m, len);
printf("key r: ");
print_num(r, len);
printf("key s: ");
print_num(s, len);
return 0;
}
7.签名验证函数
int dsa_verify_sig(unsigned long *p, unsigned long *q, unsigned long *g, unsigned long *y, unsigned long *m, unsigned long *r, unsigned long *s, unsigned long *v, int len)
{
unsigned long a[len];
unsigned long b[len];
unsigned long c[len];
unsigned long d[len];
unsigned long e[len];
unsigned long f[len];
memset(a, 0x00, sizeof(a));
memset(b, 0x00, sizeof(b));
memset(c, 0x00, sizeof(c));
memset(d, 0x00, sizeof(d));
memset(e, 0x00, sizeof(e));
memset(f, 0x00, sizeof(f));
big_num_mod_inv(s, q, a, len);
printf("verify step 3 end\n");
big_num_mul_mod(d, e, p, f, len);
printf("verify step 6 end\n");
big_num_mod(f, q, v, len);
printf("verify step 7 end\n");
return 0;
}
8.主函数部分
int main()
{
unsigned long p[16] = {0x41C416C1, 0x59C0E918, 0xB16668C4, 0x93481D71, 0x1EF64AF5, 0xC9BC2A23, 0x671494D1, 0x9512D773, 0x103C7497, 0xB53ABB51, 0x7CF0EAD2, 0x86F8A8F3, 0x5862D58A, 0x82095489, 0x0A64A7DA, 0xFD3CD307};
unsigned long q[16] = {0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0xE342DAC7, 0x8043EFD8, 0x93AA3A8C, 0x43CB20CE, 0x4FFD13FF};
unsigned long h[16] = {0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x1f25dbcb, 0x8cc6d056, 0x9fff2a9c, 0xc4cc8be6, 0xefdba4d1};
//unsigned long g[16] = {0};
unsigned long x[16] = {0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x90f755d3, 0xb2719345, 0x15098119, 0x6ef74fe8, 0x0104f1f2};
//unsigned long y[16] = {0};
unsigned long g[16] = {0x18C0EB49, 0xA18E9A74, 0x4D119F13, 0x08A5AFCF, 0x5152747A, 0x2F9C9144, 0x1ED48DC3, 0xEFB05F2B, 0x194063CE, 0x77602605, 0x88C098CB, 0x13F80BA5, 0x3C3BBB93, 0xC9BA32FA, 0x4C14A3D2, 0xAB1A8623};
unsigned long y[16] = {0x288FA8BB, 0x4F783D45, 0x514E0C7C, 0x13629F03, 0xDDD82F3A, 0xECBE5337, 0xBFF9FF4A, 0x55945ADA, 0xFBF36083, 0xC68436BA, 0x48649C41, 0x31B647FC, 0x815019DB, 0x105A9B85, 0xCB7C670E, 0xA6A0A615};
unsigned long k[16] = {0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x7ce0b2c4, 0x7ec535c0, 0x40a64a18, 0xc5f6f450, 0xf0868e30};
unsigned long u[16] = {0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0xac01065b, 0xcf974ba9, 0x86c84f0f, 0x335180e0, 0x9a65e139};
unsigned long v[16] = {0};
unsigned long r[16] = {0};
unsigned long s[16] = {0};
int len = 16;
printf("key p: ");
print_num(p, len);
//dsa_get_key(p, q, h, g, x, y, len);
dsa_set_sig(p, q, g, x, k, u, r, s, len);
dsa_verify_sig(p, q, g, y, u, r, s, v, len);
printf("key m: ");
print_num(u, len);
printf("key r: ");
print_num(r, len);
printf("key s: ");
print_num(s, len);
printf("key v: ");
print_num(v, len);
return 0;
}