Make tests work (CFB1 still doesn't produce the right answers, strangely).
[oweals/openssl.git] / fips / des / fips_desmovs.c
1 /*---------------------------------------------
2   NIST DES Modes of Operation Validation System
3   Test Program
4
5   Based on the AES Validation Suite, which was:
6   Copyright
7   V-ONE Corporation
8   20250 Century Blvd, Suite 300
9   Germantown, MD 20874
10   U.S.A.
11   ----------------------------------------------*/
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <errno.h>
17 #include <assert.h>
18
19 #include <openssl/des.h>
20 #include <openssl/evp.h>
21 #include <openssl/fips.h>
22 #include <openssl/err.h>
23
24 //#define AES_BLOCK_SIZE 16
25
26 #define VERBOSE 0
27
28 /*-----------------------------------------------*/
29
30 int DESTest(EVP_CIPHER_CTX *ctx,
31             char *amode, int akeysz, unsigned char *aKey, 
32             unsigned char *iVec, 
33             int dir,  /* 0 = decrypt, 1 = encrypt */
34             unsigned char *out, unsigned char *in, int len)
35     {
36     const EVP_CIPHER *cipher = NULL;
37     int ret = 1;
38     int kt = 0;
39
40     if (ctx)
41         memset(ctx, 0, sizeof(EVP_CIPHER_CTX));
42
43     if (strcasecmp(amode, "CBC") == 0)
44         kt = 1000;
45     else if (strcasecmp(amode, "ECB") == 0)
46         kt = 2000;
47     else if (strcasecmp(amode, "CFB64") == 0)
48         kt = 3000;
49     else if (strncasecmp(amode, "OFB", 3) == 0)
50         kt = 4000;
51     else if(!strcasecmp(amode,"CFB1"))
52         kt=5000;
53     else if(!strcasecmp(amode,"CFB8"))
54         kt=6000;
55     else
56         {
57         printf("Unknown mode: %s\n", amode);
58         exit(1);
59         }
60     if (ret)
61         {
62         if (akeysz != 64)
63             {
64             printf("Invalid key size: %d\n", akeysz);
65             ret = 0;
66             }
67         else
68             {
69             kt += akeysz;
70             switch (kt)
71                 {
72             case 1064:
73                 cipher=EVP_des_cbc();
74                 break;
75             case 2064:
76                 cipher=EVP_des_ecb();
77                 break;
78             case 3064:
79                 cipher=EVP_des_cfb64();
80                 break;
81             case 4064:
82                 cipher=EVP_des_ofb();
83                 break;
84             case 5064:
85                 cipher=EVP_des_cfb1();
86                 break;
87             case 6064:
88                 cipher=EVP_des_cfb8();
89                 break;
90             default:
91                 printf("Didn't handle mode %d\n",kt);
92                 exit(1);
93                 }
94             if(!EVP_CipherInit(ctx, cipher, aKey, iVec, dir))
95                 {
96                 ERR_print_errors_fp(stderr);
97                 exit(1);
98                 }
99             EVP_Cipher(ctx, out, in, len);
100             }
101         }
102     return ret;
103     }
104
105 /*-----------------------------------------------*/
106
107 int hex2bin(char *in, int len, unsigned char *out)
108     {
109     int n1, n2;
110     unsigned char ch;
111
112     for (n1 = 0, n2 = 0; n1 < len; )
113         { /* first byte */
114         if ((in[n1] >= '0') && (in[n1] <= '9'))
115             ch = in[n1++] - '0';
116         else if ((in[n1] >= 'A') && (in[n1] <= 'F'))
117             ch = in[n1++] - 'A' + 10;
118         else if ((in[n1] >= 'a') && (in[n1] <= 'f'))
119             ch = in[n1++] - 'a' + 10;
120         else
121             return -1;
122         if(len == 1)
123             {
124             out[n2++]=ch;
125             break;
126             }
127         out[n2] = ch << 4;
128         /* second byte */
129         if ((in[n1] >= '0') && (in[n1] <= '9'))
130             ch = in[n1++] - '0';
131         else if ((in[n1] >= 'A') && (in[n1] <= 'F'))
132             ch = in[n1++] - 'A' + 10;
133         else if ((in[n1] >= 'a') && (in[n1] <= 'f'))
134             ch = in[n1++] - 'a' + 10;
135         else
136             return -1;
137         out[n2++] |= ch;
138         }
139     return n2;
140     }
141
142 /*-----------------------------------------------*/
143
144 int bin2hex(unsigned char *in, int len, char *out)
145     {
146     int n1, n2;
147     unsigned char ch;
148
149     for (n1 = 0, n2 = 0; n1 < len; ++n1)
150         {
151         /* first nibble */
152         ch = in[n1] >> 4;
153         if (ch <= 0x09)
154             out[n2++] = ch + '0';
155         else
156             out[n2++] = ch - 10 + 'a';
157         /* second nibble */
158         ch = in[n1] & 0x0f;
159         if (ch <= 0x09)
160             out[n2++] = ch + '0';
161         else
162             out[n2++] = ch - 10 + 'a';
163         }
164     return n2;
165     }
166
167 /* NB: this return the number of _bits_ read */
168 int bint2bin(const char *in, int len, unsigned char *out)
169     {
170     int n;
171
172     memset(out,0,len);
173     for(n=0 ; n < len ; ++n)
174         if(in[n] == '1')
175             out[n/8]|=(0x80 >> (n%8));
176     return len;
177     }
178
179 int bin2bint(const unsigned char *in,int len,char *out)
180     {
181     int n;
182
183     for(n=0 ; n < len ; ++n)
184         out[n]=(in[n/8]&(0x80 >> (n%8))) ? '1' : '0';
185     return n;
186     }
187
188 /*-----------------------------------------------*/
189
190 void PrintValue(char *tag, unsigned char *val, int len)
191     {
192 #if VERBOSE
193     char obuf[2048];
194     int olen;
195     olen = bin2hex(val, len, obuf);
196     printf("%s = %.*s\n", tag, olen, obuf);
197 #endif
198     }
199
200 void DebugValue(char *tag, unsigned char *val, int len)
201     {
202     char obuf[2048];
203     int olen;
204     olen = bin2hex(val, len, obuf);
205     printf("%s = %.*s\n", tag, olen, obuf);
206     }
207
208 void OutputValue(char *tag, unsigned char *val, int len, FILE *rfp,int bitmode)
209     {
210     char obuf[2048];
211     int olen;
212
213     if(bitmode)
214         olen=bin2bint(val,len,obuf);
215     else
216         olen=bin2hex(val,len,obuf);
217
218     fprintf(rfp, "%s = %.*s\n", tag, olen, obuf);
219 #if VERBOSE
220     printf("%s = %.*s\n", tag, olen, obuf);
221 #endif
222     }
223
224 void shiftin(unsigned char *dst,unsigned char *src,int nbits)
225     {
226     int n;
227
228     /* move the bytes... */
229     memmove(dst,dst+nbits/8,8-nbits/8);
230     /* append new data */
231     memcpy(dst+8-nbits/8,src,(nbits+7)/8);
232     /* left shift the bits */
233     if(nbits%8)
234         for(n=0 ; n < 8 ; ++n)
235             dst[n]=(dst[n] << (nbits%8))|(dst[n+1] >> (8-nbits%8));
236     }   
237
238 /*-----------------------------------------------*/
239 char *t_tag[2] = {"PLAINTEXT", "CIPHERTEXT"};
240 char *t_mode[6] = {"CBC","ECB","OFB","CFB1","CFB8","CFB64"};
241 enum Mode {CBC, ECB, OFB, CFB1, CFB8, CFB64};
242 int Sizes[6]={64,0,64,1,8,64};
243 enum XCrypt {XDECRYPT, XENCRYPT};
244
245 void do_mct(char *amode, 
246             int akeysz, unsigned char *akey,unsigned char *ivec,
247             int dir, unsigned char *text, int len,
248             FILE *rfp)
249     {
250     int i,imode;
251     unsigned char nk[16]; // double size to make the bitshift easier
252
253     for (imode=0 ; imode < 6 ; ++imode)
254         if(!strcmp(amode,t_mode[imode]))
255             break;
256     if (imode == 6)
257         { 
258         printf("Unrecognized mode: %s\n", amode);
259         exit(1);
260         }
261
262     for(i=0 ; i < 400 ; ++i)
263         {
264         int j;
265         int n;
266         EVP_CIPHER_CTX ctx;
267         unsigned char old_iv[8];
268
269         fprintf(rfp,"\nCOUNT = %d\n",i);
270         OutputValue("KEY",akey,akeysz/8,rfp,0);
271         if(imode != ECB)
272             OutputValue("IV",ivec,8,rfp,0);
273         OutputValue(t_tag[dir^1],text,len,rfp,imode == CFB1);
274
275         /* compensate for endianness */
276         if(imode == CFB1)
277             text[0]<<=7;
278
279         for(j=0 ; j < 10000 ; ++j)
280             {
281             if(j == 0)
282                 {
283                 memcpy(old_iv,ivec,8);
284                 DESTest(&ctx,amode,akeysz,akey,ivec,dir,text,text,len);
285                 }
286             else
287                 {
288                 memcpy(old_iv,ctx.iv,8);
289                 EVP_Cipher(&ctx,text,text,len);
290                 }
291             if(j == 9999)
292                 {
293                 OutputValue(t_tag[dir],text,len,rfp,imode == CFB1);
294                 //              memcpy(ivec,text,8);
295                 }
296             //      DebugValue("iv",ctx.iv,8);
297             /* accumulate material for the next key */
298             shiftin(nk,text,Sizes[imode]);
299             //      DebugValue("nk",nk,8);
300             if(imode == CFB1 || imode == CFB8 || imode == CBC)
301                 memcpy(text,old_iv,8);
302             }
303         for(n=0 ; n < 8 ; ++n)
304             akey[n]^=nk[n];
305         memcpy(ivec,ctx.iv,8);
306         }
307     }
308     
309 int proc_file(char *rqfile)
310     {
311     char afn[256], rfn[256];
312     FILE *afp = NULL, *rfp = NULL;
313     char ibuf[2048];
314     int ilen, len, ret = 0;
315     char algo[8] = "";
316     char amode[8] = "";
317     char atest[100] = "";
318     int akeysz=0;
319     unsigned char iVec[20], aKey[40];
320     int dir = -1, err = 0, step = 0;
321     unsigned char plaintext[2048];
322     unsigned char ciphertext[2048];
323     char *rp;
324     EVP_CIPHER_CTX ctx;
325
326     if (!rqfile || !(*rqfile))
327         {
328         printf("No req file\n");
329         return -1;
330         }
331     strcpy(afn, rqfile);
332
333     if ((afp = fopen(afn, "r")) == NULL)
334         {
335         printf("Cannot open file: %s, %s\n", 
336                afn, strerror(errno));
337         return -1;
338         }
339     strcpy(rfn,afn);
340     rp=strstr(rfn,"req/");
341     assert(rp);
342     memcpy(rp,"rsp",3);
343     rp = strstr(rfn, ".req");
344     memcpy(rp, ".rsp", 4);
345     if ((rfp = fopen(rfn, "w")) == NULL)
346         {
347         printf("Cannot open file: %s, %s\n", 
348                rfn, strerror(errno));
349         fclose(afp);
350         afp = NULL;
351         return -1;
352         }
353     while (!err && (fgets(ibuf, sizeof(ibuf), afp)) != NULL)
354         {
355         ilen = strlen(ibuf);
356         //      printf("step=%d ibuf=%s",step,ibuf);
357         switch (step)
358             {
359         case 0:  /* read preamble */
360             if (ibuf[0] == '\n')
361                 { /* end of preamble */
362                 if ((*algo == '\0') ||
363                     (*amode == '\0') ||
364                     (akeysz == 0))
365                     {
366                     printf("Missing Algorithm, Mode or KeySize (%s/%s/%d)\n",
367                            algo,amode,akeysz);
368                     err = 1;
369                     }
370                 else
371                     {
372                     fputs(ibuf, rfp);
373                     ++ step;
374                     }
375                 }
376             else if (ibuf[0] != '#')
377                 {
378                 printf("Invalid preamble item: %s\n", ibuf);
379                 err = 1;
380                 }
381             else
382                 { /* process preamble */
383                 char *xp, *pp = ibuf+2;
384                 int n;
385                 if (akeysz)
386                     { /* insert current time & date */
387                     time_t rtim = time(0);
388                     fprintf(rfp, "# %s", ctime(&rtim));
389                     }
390                 else
391                     {
392                     fputs(ibuf, rfp);
393                     if(!strncmp(pp,"INVERSE ",8) || !strncmp(pp,"DES ",4)
394                        || !strncmp(pp,"PERMUTATION ",12)
395                        || !strncmp(pp,"SUBSTITUTION ",13)
396                        || !strncmp(pp,"VARIABLE ",9))
397                         {
398                         strcpy(algo, "DES");
399                         /* get test type */
400                         if(!strncmp(pp,"DES ",4))
401                             pp+=4;
402                         xp = strchr(pp, ' ');
403                         n = xp-pp;
404                         strncpy(atest, pp, n);
405                         atest[n] = '\0';
406                         /* get mode */
407                         xp = strrchr(pp, ' '); /* get mode" */
408                         n = strlen(xp+1)-1;
409                         strncpy(amode, xp+1, n);
410                         amode[n] = '\0';
411                         /* amode[3] = '\0'; */
412                         printf("Test = %s, Mode = %s\n", atest, amode);
413                         }
414                     else if(!strncmp(pp,"State :",7))
415                         akeysz=64;
416                     }
417                 }
418             break;
419
420         case 1:  /* [ENCRYPT] | [DECRYPT] */
421             if(ibuf[0] == '\n')
422                 break;
423             if (ibuf[0] == '[')
424                 {
425                 fputs(ibuf, rfp);
426                 ++step;
427                 if (strncasecmp(ibuf, "[ENCRYPT]", 9) == 0)
428                     dir = 1;
429                 else if (strncasecmp(ibuf, "[DECRYPT]", 9) == 0)
430                     dir = 0;
431                 else
432                     {
433                     printf("Invalid keyword: %s\n", ibuf);
434                     err = 1;
435                     }
436                 break;
437                 }
438             else if (dir == -1)
439                 {
440                 err = 1;
441                 printf("Missing ENCRYPT/DECRYPT keyword\n");
442                 break;
443                 }
444             else 
445                 step = 2;
446
447         case 2: /* KEY = xxxx */
448             fputs(ibuf, rfp);
449             if(*ibuf == '\n')
450                 break;
451             if(!strncasecmp(ibuf,"COUNT = ",8))
452                 break;
453           
454             if (strncasecmp(ibuf, "KEY = ", 6) != 0)
455                 {
456                 printf("Missing KEY\n");
457                 err = 1;
458                 }
459             else
460                 {
461                 len = hex2bin((char*)ibuf+6, strlen(ibuf+6)-1, aKey);
462                 if (len < 0)
463                     {
464                     printf("Invalid KEY\n");
465                     err =1;
466                     break;
467                     }
468                 PrintValue("KEY", aKey, len);
469                 if (strcmp(amode, "ECB") == 0)
470                     {
471                     memset(iVec, 0, sizeof(iVec));
472                     step = (dir)? 4: 5;  /* no ivec for ECB */
473                     }
474                 else
475                     ++step;
476                 }
477             break;
478
479         case 3: /* IV = xxxx */
480             fputs(ibuf, rfp);
481             if (strncasecmp(ibuf, "IV = ", 5) != 0)
482                 {
483                 printf("Missing IV\n");
484                 err = 1;
485                 }
486             else
487                 {
488                 len = hex2bin((char*)ibuf+5, strlen(ibuf+5)-1, iVec);
489                 if (len < 0)
490                     {
491                     printf("Invalid IV\n");
492                     err =1;
493                     break;
494                     }
495                 PrintValue("IV", iVec, len);
496                 step = (dir)? 4: 5;
497                 }
498             break;
499
500         case 4: /* PLAINTEXT = xxxx */
501             fputs(ibuf, rfp);
502             if (strncasecmp(ibuf, "PLAINTEXT = ", 12) != 0)
503                 {
504                 printf("Missing PLAINTEXT\n");
505                 err = 1;
506                 }
507             else
508                 {
509                 int nn = strlen(ibuf+12);
510                 if(!strcmp(amode,"CFB1"))
511                     len=bint2bin(ibuf+12,nn-1,plaintext);
512                 else
513                     len=hex2bin(ibuf+12, nn-1,plaintext);
514                 if (len < 0)
515                     {
516                     printf("Invalid PLAINTEXT: %s", ibuf+12);
517                     err =1;
518                     break;
519                     }
520                 if (len >= sizeof(plaintext))
521                     {
522                     printf("Buffer overflow\n");
523                     }
524                 PrintValue("PLAINTEXT", (unsigned char*)plaintext, len);
525                 if (strcmp(atest, "Monte") == 0)  /* Monte Carlo Test */
526                     {
527                     do_mct(amode,akeysz,aKey,iVec,dir,plaintext,len,rfp);
528                     }
529                 else
530                     {
531                     assert(dir == 1);
532                     ret = DESTest(&ctx, amode, akeysz, aKey, iVec, 
533                                   dir,  /* 0 = decrypt, 1 = encrypt */
534                                   ciphertext, plaintext, len);
535                     OutputValue("CIPHERTEXT",ciphertext,len,rfp,
536                                 !strcmp(amode,"CFB1"));
537                     }
538                 step = 6;
539                 }
540             break;
541
542         case 5: /* CIPHERTEXT = xxxx */
543             fputs(ibuf, rfp);
544             if (strncasecmp(ibuf, "CIPHERTEXT = ", 13) != 0)
545                 {
546                 printf("Missing KEY\n");
547                 err = 1;
548                 }
549             else
550                 {
551                 if(!strcmp(amode,"CFB1"))
552                     len=bint2bin(ibuf+13,strlen(ibuf+13)-1,ciphertext);
553                 else
554                     len = hex2bin(ibuf+13,strlen(ibuf+13)-1,ciphertext);
555                 if (len < 0)
556                     {
557                     printf("Invalid CIPHERTEXT\n");
558                     err =1;
559                     break;
560                     }
561                 
562                 PrintValue("CIPHERTEXT", ciphertext, len);
563                 if (strcmp(atest, "Monte") == 0)  /* Monte Carlo Test */
564                     {
565                     do_mct(amode, akeysz, aKey, iVec, 
566                            dir, ciphertext, len, rfp);
567                     }
568                 else
569                     {
570                     assert(dir == 0);
571                     ret = DESTest(&ctx, amode, akeysz, aKey, iVec, 
572                                   dir,  /* 0 = decrypt, 1 = encrypt */
573                                   plaintext, ciphertext, len);
574                     OutputValue("PLAINTEXT",(unsigned char *)plaintext,len,rfp,
575                                 !strcmp(amode,"CFB1"));
576                     }
577                 step = 6;
578                 }
579             break;
580
581         case 6:
582             if (ibuf[0] != '\n')
583                 {
584                 err = 1;
585                 printf("Missing terminator\n");
586                 }
587             else if (strcmp(atest, "MCT") != 0)
588                 { /* MCT already added terminating nl */
589                 fputs(ibuf, rfp);
590                 }
591             step = 1;
592             break;
593             }
594         }
595     if (rfp)
596         fclose(rfp);
597     if (afp)
598         fclose(afp);
599     return err;
600     }
601
602 /*--------------------------------------------------
603   Processes either a single file or 
604   a set of files whose names are passed in a file.
605   A single file is specified as:
606     aes_test -f xxx.req
607   A set of files is specified as:
608     aes_test -d xxxxx.xxx
609   The default is: -d req.txt
610 --------------------------------------------------*/
611 int main(int argc, char **argv)
612     {
613     char *rqlist = "req.txt";
614     FILE *fp = NULL;
615     char fn[250] = "", rfn[256] = "";
616     int f_opt = 0, d_opt = 1;
617
618 #ifdef FIPS
619     FIPS_mode_set(1);
620 #endif
621     ERR_load_crypto_strings();
622     if (argc > 1)
623         {
624         if (strcasecmp(argv[1], "-d") == 0)
625             {
626             d_opt = 1;
627             }
628         else if (strcasecmp(argv[1], "-f") == 0)
629             {
630             f_opt = 1;
631             d_opt = 0;
632             }
633         else
634             {
635             printf("Invalid parameter: %s\n", argv[1]);
636             return 0;
637             }
638         if (argc < 3)
639             {
640             printf("Missing parameter\n");
641             return 0;
642             }
643         if (d_opt)
644             rqlist = argv[2];
645         else
646             strcpy(fn, argv[2]);
647         }
648     if (d_opt)
649         { /* list of files (directory) */
650         if (!(fp = fopen(rqlist, "r")))
651             {
652             printf("Cannot open req list file\n");
653             return -1;
654             }
655         while (fgets(fn, sizeof(fn), fp))
656             {
657             strtok(fn, "\r\n");
658             strcpy(rfn, fn);
659             printf("Processing: %s\n", rfn);
660             if (proc_file(rfn))
661                 {
662                 printf(">>> Processing failed for: %s <<<\n", rfn);
663                 exit(1);
664                 }
665             }
666         fclose(fp);
667         }
668     else /* single file */
669         {
670         printf("Processing: %s\n", fn);
671         if (proc_file(fn))
672             {
673             printf(">>> Processing failed for: %s <<<\n", fn);
674             }
675         }
676     return 0;
677     }