Add our readmes.
[dragonfly.git] / crypto / openssl-0.9.7e / test / fips_sha1test.c
1 #include <stdio.h>
2 #include <assert.h>
3 #include <ctype.h>
4 #include <string.h>
5 #include <stdlib.h>
6 #include <openssl/sha.h>
7 #include <openssl/err.h>
8 #include <openssl/fips.h>
9
10 #ifndef OPENSSL_FIPS
11 int main(int argc, char *argv[])
12 {
13     printf("No FIPS SHA1 support\n");
14     return(0);
15 }
16 #else
17
18 #define MAX_TEST_BITS 103432
19
20 static void dump(const unsigned char *b,int n)
21     {
22     while(n-- > 0)
23         printf("%02X",*b++);
24     }
25
26 static void bitfill(unsigned char *buf,int bit,int b,int n)
27     {
28     for( ; n > 0 ; --n,++bit)
29         {
30         assert(bit < MAX_TEST_BITS);
31         buf[bit/8]|=b << (7-bit%8);
32         }
33     }
34
35 void montecarlo(unsigned char *seed,int n)
36     {
37     int i,j;
38     unsigned char m[10240];
39
40     memcpy(m,seed,n);
41     for(j=0 ; j < 100 ; ++j)
42         {
43         for(i=1 ; i <= 50000 ; ++i)
44             {
45             memset(m+n,'\0',j/4+3);
46             n+=j/4+3;
47             m[n++]=i >> 24;
48             m[n++]=i >> 16;
49             m[n++]=i >> 8;
50             m[n++]=i;
51 /*          putchar(' '); */
52 /*          dump(m,bit/8); */
53 /*          putchar('\n'); */
54             SHA1(m,n,m);
55             n=20;
56             }
57         dump(m,20);
58         puts(" ^");
59         }
60     }
61
62 int main(int argc,char **argv)
63     {
64     FILE *fp;
65     int phase;
66
67     if(argc != 2)
68         {
69         fprintf(stderr,"%s <test vector file>\n",argv[0]);
70         exit(1);
71         }
72
73     if(!FIPS_mode_set(1,argv[0]))
74         {
75         ERR_load_crypto_strings();
76         ERR_print_errors(BIO_new_fp(stderr,BIO_NOCLOSE));
77         exit(1);
78         }
79     fp=fopen(argv[1],"r");
80     if(!fp)
81         {
82         perror(argv[1]);
83         exit(2);
84         }
85
86     for(phase=0 ; ; )
87         {
88         unsigned char buf[MAX_TEST_BITS/8];
89         unsigned char md[20];
90         char line[10240];
91         int n,t,b,bit;
92         char *p;
93
94         fgets(line,1024,fp);
95         if(feof(fp))
96             break;
97         n=strlen(line);
98         line[n-1]='\0';
99         if(!strcmp(line,"D>"))
100             ++phase;
101
102         if(!isdigit(line[0]))
103             {
104             puts(line);
105             continue;
106             }
107         for( ; ; )
108             {
109             assert(n > 1);
110             if(line[n-2] == '^')
111                 break;
112             fgets(line+n-1,sizeof(line)-n+1,fp);
113             n=strlen(line);
114             /*      printf("line=%s\n",line); */
115             assert(!feof(fp));
116             }
117
118         p=strtok(line," ");
119         t=atoi(p);
120         p=strtok(NULL," ");
121         b=atoi(p);
122         memset(buf,'\0',sizeof buf);
123         for(bit=0,p=strtok(NULL," ") ; p && *p != '^' ; p=strtok(NULL," "))
124             {
125             assert(t-- > 0);
126             bitfill(buf,bit,b,atoi(p));
127             bit+=atoi(p);
128             b=1-b;
129             }
130         assert(t == 0);
131         assert((bit%8) == 0);
132         /*      dump(buf,bit/8); */
133         /*      putchar('\n'); */
134         if(phase < 3)
135             {
136             SHA1(buf,bit/8,md);
137             dump(md,20);
138             puts(" ^");
139             }
140         else
141             montecarlo(buf,bit/8);
142         }
143     return 0;
144     }
145 #endif