Import OpenSSL-0.9.8i.
[dragonfly.git] / crypto / openssl-0.9.7e / fips / sha1 / fips_sha1test.c
blob5a5ed2b69b258805fd5d2267811138314d11e436
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>
10 #ifndef OPENSSL_FIPS
11 int main(int argc, char *argv[])
13 printf("No FIPS SHA1 support\n");
14 return(0);
16 #else
18 #define MAX_TEST_BITS 103432
20 static void dump(const unsigned char *b,int n)
22 while(n-- > 0)
23 printf("%02X",*b++);
26 static void bitfill(unsigned char *buf,int bit,int b,int n)
28 for( ; n > 0 ; --n,++bit)
30 assert(bit < MAX_TEST_BITS);
31 buf[bit/8]|=b << (7-bit%8);
35 void montecarlo(unsigned char *seed,int n)
37 int i,j;
38 unsigned char m[10240];
40 memcpy(m,seed,n);
41 for(j=0 ; j < 100 ; ++j)
43 for(i=1 ; i <= 50000 ; ++i)
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;
57 dump(m,20);
58 puts(" ^");
62 int main(int argc,char **argv)
64 FILE *fp;
65 int phase;
67 if(argc != 2)
69 fprintf(stderr,"%s <test vector file>\n",argv[0]);
70 exit(1);
73 if(!FIPS_mode_set(1,argv[0]))
75 ERR_load_crypto_strings();
76 ERR_print_errors(BIO_new_fp(stderr,BIO_NOCLOSE));
77 exit(1);
79 fp=fopen(argv[1],"r");
80 if(!fp)
82 perror(argv[1]);
83 exit(2);
86 for(phase=0 ; ; )
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;
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;
102 if(!isdigit(line[0]))
104 puts(line);
105 continue;
107 for( ; ; )
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));
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," "))
125 assert(t-- > 0);
126 bitfill(buf,bit,b,atoi(p));
127 bit+=atoi(p);
128 b=1-b;
130 assert(t == 0);
131 assert((bit%8) == 0);
132 /* dump(buf,bit/8); */
133 /* putchar('\n'); */
134 if(phase < 3)
136 SHA1(buf,bit/8,md);
137 dump(md,20);
138 puts(" ^");
140 else
141 montecarlo(buf,bit/8);
143 return 0;
145 #endif