-
Notifications
You must be signed in to change notification settings - Fork 242
/
Copy pathdssign.c
123 lines (110 loc) · 2.67 KB
/
dssign.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
* Digital Signature Algorithm (DSA)
*
* See Communications ACM July 1992, Vol. 35 No. 7
* This new standard for digital signatures has been proposed by
* the American National Institute of Standards and Technology (NIST)
* under advisement from the National Security Agency (NSA).
*
* This program asks for the name of a <file>, computes its message digest,
* signs it, and outputs the signature to a file <file>.dss. It is assumed
* that the common values p, q and g, as well as the private key of the
* signer have been previously generated by the dssgen program
*
*/
#include <stdio.h>
#include "miracl.h"
#include <stdlib.h>
#include <string.h>
void strip(char *name)
{ /* strip off filename extension */
int i;
for (i=0;name[i]!='\0';i++)
{
if (name[i]!='.') continue;
name[i]='\0';
break;
}
}
static void hashing(FILE *fp,big hash)
{ /* compute hash function */
char h[20];
int i,ch;
sha sh;
shs_init(&sh);
while ((ch=fgetc(fp))!=EOF) shs_process(&sh,ch);
shs_hash(&sh,h);
bytes_to_big(20,h,hash);
}
int main()
{
FILE *fp;
char ifname[50],ofname[50];
big p,q,g,x,r,s,k,hash;
long seed;
int bits;
miracl *mip;
/* get public data */
fp=fopen("common.dss","rt");
if (fp==NULL)
{
printf("file common.dss does not exist\n");
return 0;
}
fscanf(fp,"%d\n",&bits);
mip=mirsys(bits/4,16); /* use Hex internally */
p=mirvar(0);
q=mirvar(0);
g=mirvar(0);
x=mirvar(0);
r=mirvar(0);
s=mirvar(0);
k=mirvar(0);
hash=mirvar(0);
innum(p,fp);
innum(q,fp);
innum(g,fp);
fclose(fp);
/* randomise */
printf("Enter 9 digit random number seed = ");
scanf("%ld",&seed);
getchar();
irand(seed);
/* calculate r - this can be done offline,
and hence amortized to almost nothing */
bigrand(q,k);
powmod(g,k,p,r); /* see brick.c for method to speed this up */
divide(r,q,q);
/* get private key of signer */
fp=fopen("private.dss","rt");
if (fp==NULL)
{
printf("file private.dss does not exist\n");
return 0;
}
innum(x,fp);
fclose(fp);
/* calculate message digest */
printf("file to be signed = ");
gets(ifname);
strcpy(ofname,ifname);
strip(ofname);
strcat(ofname,".dss");
if ((fp=fopen(ifname,"rb"))==NULL)
{
printf("Unable to open file %s\n",ifname);
return 0;
}
hashing(fp,hash);
fclose(fp);
/* calculate s */
xgcd(k,q,k,k,k);
mad(x,r,hash,q,q,s);
mad(s,k,k,q,q,s);
fp=fopen(ofname,"wt");
otnum(r,fp);
otnum(s,fp);
fclose(fp);
mirexit();
return 0;
}