Add our readmes.
[dragonfly.git] / crypto / openssl-0.9.7e / fips / dsa / fips_dssvs.c
1 #include <openssl/bn.h>
2 #include <openssl/dsa.h>
3 #include <openssl/fips.h>
4 #include <openssl/err.h>
5 #include <openssl/sha.h>
6 #include <string.h>
7
8 int hex2bin(const char *in, unsigned char *out)
9     {
10     int n1, n2;
11     unsigned char ch;
12
13     for (n1=0,n2=0 ; in[n1] && in[n1] != '\n' ; )
14         { /* first byte */
15         if ((in[n1] >= '0') && (in[n1] <= '9'))
16             ch = in[n1++] - '0';
17         else if ((in[n1] >= 'A') && (in[n1] <= 'F'))
18             ch = in[n1++] - 'A' + 10;
19         else if ((in[n1] >= 'a') && (in[n1] <= 'f'))
20             ch = in[n1++] - 'a' + 10;
21         else
22             return -1;
23         if(!in[n1])
24             {
25             out[n2++]=ch;
26             break;
27             }
28         out[n2] = ch << 4;
29         /* second byte */
30         if ((in[n1] >= '0') && (in[n1] <= '9'))
31             ch = in[n1++] - '0';
32         else if ((in[n1] >= 'A') && (in[n1] <= 'F'))
33             ch = in[n1++] - 'A' + 10;
34         else if ((in[n1] >= 'a') && (in[n1] <= 'f'))
35             ch = in[n1++] - 'a' + 10;
36         else
37             return -1;
38         out[n2++] |= ch;
39         }
40     return n2;
41     }
42
43 BIGNUM *hex2bn(const char *in)
44     {
45     BIGNUM *p=BN_new();
46
47     BN_hex2bn(&p,in);
48
49     return p;
50     }
51
52 int bin2hex(const unsigned char *in,int len,char *out)
53     {
54     int n1, n2;
55     unsigned char ch;
56
57     for (n1=0,n2=0 ; n1 < len ; ++n1)
58         {
59         ch=in[n1] >> 4;
60         if (ch <= 0x09)
61             out[n2++]=ch+'0';
62         else
63             out[n2++]=ch-10+'a';
64         ch=in[n1] & 0x0f;
65         if(ch <= 0x09)
66             out[n2++]=ch+'0';
67         else
68             out[n2++]=ch-10+'a';
69         }
70     out[n2]='\0';
71     return n2;
72     }
73
74 void pv(const char *tag,const unsigned char *val,int len)
75     {
76     char obuf[2048];
77
78     bin2hex(val,len,obuf);
79     printf("%s = %s\n",tag,obuf);
80     }
81
82 void pbn(const char *tag,const BIGNUM *val)
83     {
84     printf("%s = %s\n",tag,BN_bn2hex(val));
85     }
86
87 void primes()
88     {
89     char buf[10240];
90
91     while(fgets(buf,sizeof buf,stdin) != NULL)
92         {
93         fputs(buf,stdout);
94         if(!strncmp(buf,"Prime= ",7))
95             {
96             BIGNUM *pp;
97
98             pp=BN_new();
99             BN_hex2bn(&pp,buf+7);
100             printf("result= %c\n",
101                    BN_is_prime(pp,20,NULL,NULL,NULL) ? 'P' : 'F');
102             }       
103         }
104     }
105
106 void pqg()
107     {
108     char buf[1024];
109     int nmod=0;
110
111     while(fgets(buf,sizeof buf,stdin) != NULL)
112         {
113         if(!strncmp(buf,"[mod = ",7))
114             nmod=atoi(buf+7);
115         else if(!strncmp(buf,"N = ",4))
116             {
117             int n=atoi(buf+4);
118
119             printf("[mod = %d]\n\n",nmod);
120
121             while(n--)
122                 {
123                 unsigned char seed[20];
124                 DSA *dsa;
125                 int counter;
126                 unsigned long h;
127
128                 dsa=DSA_generate_parameters(nmod,seed,0,&counter,&h,NULL,NULL);
129                 printf("P = %s\n",BN_bn2hex(dsa->p));
130                 printf("Q = %s\n",BN_bn2hex(dsa->q));
131                 printf("G = %s\n",BN_bn2hex(dsa->g));
132                 pv("Seed",seed,20);
133                 printf("c = %d\n",counter);
134                 printf("H = %lx\n",h);
135                 putc('\n',stdout);
136                 }
137             }
138         else
139             fputs(buf,stdout);
140         }
141     }
142
143 void keypair()
144     {
145     char buf[1024];
146     int nmod=0;
147
148     while(fgets(buf,sizeof buf,stdin) != NULL)
149         {
150         if(!strncmp(buf,"[mod = ",7))
151             nmod=atoi(buf+7);
152         else if(!strncmp(buf,"N = ",4))
153             {
154             DSA *dsa;
155             int n=atoi(buf+4);
156
157             printf("[mod = %d]\n\n",nmod);
158
159             dsa=DSA_generate_parameters(nmod,NULL,0,NULL,NULL,NULL,NULL);
160             pbn("P",dsa->p);
161             pbn("Q",dsa->q);
162             pbn("G",dsa->g);
163             putc('\n',stdout);
164
165             while(n--)
166                 {
167                 DSA_generate_key(dsa);
168
169                 pbn("X",dsa->priv_key);
170                 pbn("Y",dsa->pub_key);
171                 putc('\n',stdout);
172                 }
173             }
174         }
175     }
176
177 void siggen()
178     {
179     char buf[1024];
180     int nmod=0;
181     DSA *dsa=NULL;
182
183     while(fgets(buf,sizeof buf,stdin) != NULL)
184         {
185         if(!strncmp(buf,"[mod = ",7))
186             {
187             nmod=atoi(buf+7);
188             printf("[mod = %d]\n\n",nmod);
189
190             dsa=DSA_generate_parameters(nmod,NULL,0,NULL,NULL,NULL,NULL);
191             pbn("P",dsa->p);
192             pbn("Q",dsa->q);
193             pbn("G",dsa->g);
194             putc('\n',stdout);
195             }
196         else if(!strncmp(buf,"Msg = ",6))
197             {
198             unsigned char msg[1024];
199             unsigned char hash[20];
200             int n;
201             DSA_SIG *sig;
202
203             n=hex2bin(buf+6,msg);
204             pv("Msg",msg,n);
205
206             DSA_generate_key(dsa);
207             pbn("Y",dsa->pub_key);
208
209             SHA1(msg,n,hash);
210             sig=DSA_do_sign(hash,sizeof hash,dsa);
211             pbn("R",sig->r);
212             pbn("S",sig->s);
213             putc('\n',stdout);
214             }
215         }
216     }
217
218 void sigver()
219     {
220     DSA *dsa=NULL;
221     char buf[1024];
222     int nmod=0;
223     unsigned char hash[20];
224     DSA_SIG *sig=DSA_SIG_new();
225
226     while(fgets(buf,sizeof buf,stdin) != NULL)
227         {
228         if(!strncmp(buf,"[mod = ",7))
229             {
230             nmod=atoi(buf+7);
231             if(dsa)
232                 DSA_free(dsa);
233             dsa=DSA_new();
234             }
235         else if(!strncmp(buf,"P = ",4))
236             dsa->p=hex2bn(buf+4);
237         else if(!strncmp(buf,"Q = ",4))
238             dsa->q=hex2bn(buf+4);
239         else if(!strncmp(buf,"G = ",4))
240             {
241             dsa->g=hex2bn(buf+4);
242
243             printf("[mod = %d]\n\n",nmod);
244             pbn("P",dsa->p);
245             pbn("Q",dsa->q);
246             pbn("G",dsa->g);
247             putc('\n',stdout);
248             }
249         else if(!strncmp(buf,"Msg = ",6))
250             {
251             unsigned char msg[1024];
252             int n;
253
254             n=hex2bin(buf+6,msg);
255             pv("Msg",msg,n);
256             SHA1(msg,n,hash);
257             }
258         else if(!strncmp(buf,"Y = ",4))
259             dsa->pub_key=hex2bn(buf+4);
260         else if(!strncmp(buf,"R = ",4))
261             sig->r=hex2bn(buf+4);
262         else if(!strncmp(buf,"S = ",4))
263             {
264             sig->s=hex2bn(buf+4);
265         
266             pbn("Y",dsa->pub_key);
267             pbn("R",sig->r);
268             pbn("S",sig->s);
269             printf("Result = %c\n",DSA_do_verify(hash,sizeof hash,sig,dsa)
270                    ? 'P' : 'F');
271             putc('\n',stdout);
272             }
273         }
274     }
275
276 int main(int argc,char **argv)
277     {
278     if(argc != 2)
279         {
280         fprintf(stderr,"%s [prime|pqg]\n",argv[0]);
281         exit(1);
282         }
283     if(!FIPS_mode_set(1,argv[0]))
284         {
285         ERR_load_crypto_strings();
286         ERR_print_errors(BIO_new_fp(stderr,BIO_NOCLOSE));
287         exit(1);
288         }
289     if(!strcmp(argv[1],"prime"))
290         primes();
291     else if(!strcmp(argv[1],"pqg"))
292         pqg();
293     else if(!strcmp(argv[1],"keypair"))
294         keypair();
295     else if(!strcmp(argv[1],"siggen"))
296         siggen();
297     else if(!strcmp(argv[1],"sigver"))
298         sigver();
299     else
300         {
301         fprintf(stderr,"Don't know how to %s.\n",argv[1]);
302         exit(1);
303         }
304
305     return 0;
306     }