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