Make fips algorithm test utilities use RESP_EOL for end of line character(s).
[openssl.git] / fips / des / fips_desmovs.c
1 /* ====================================================================
2  * Copyright (c) 2004 The OpenSSL Project.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer. 
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in
13  *    the documentation and/or other materials provided with the
14  *    distribution.
15  *
16  * 3. All advertising materials mentioning features or use of this
17  *    software must display the following acknowledgment:
18  *    "This product includes software developed by the OpenSSL Project
19  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
20  *
21  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
22  *    endorse or promote products derived from this software without
23  *    prior written permission. For written permission, please contact
24  *    openssl-core@openssl.org.
25  *
26  * 5. Products derived from this software may not be called "OpenSSL"
27  *    nor may "OpenSSL" appear in their names without prior written
28  *    permission of the OpenSSL Project.
29  *
30  * 6. Redistributions of any form whatsoever must retain the following
31  *    acknowledgment:
32  *    "This product includes software developed by the OpenSSL Project
33  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
34  *
35  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
36  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
38  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
41  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
42  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
44  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
46  * OF THE POSSIBILITY OF SUCH DAMAGE.
47  *
48  */
49 /*---------------------------------------------
50   NIST DES Modes of Operation Validation System
51   Test Program
52
53   Based on the AES Validation Suite, which was:
54   Donated to OpenSSL by:
55   V-ONE Corporation
56   20250 Century Blvd, Suite 300
57   Germantown, MD 20874
58   U.S.A.
59   ----------------------------------------------*/
60
61 #define OPENSSL_FIPSAPI
62
63 #include <stdio.h>
64 #include <stdlib.h>
65 #include <string.h>
66 #include <errno.h>
67 #include <assert.h>
68 #include <ctype.h>
69 #include <openssl/crypto.h>
70 #include <openssl/des.h>
71 #include <openssl/evp.h>
72 #include <openssl/bn.h>
73
74 #include <openssl/err.h>
75 #include "e_os.h"
76
77 #ifndef OPENSSL_FIPS
78
79 int main(int argc, char *argv[])
80 {
81     printf("No FIPS DES support\n");
82     return(0);
83 }
84
85 #else
86
87 #include "fips_utl.h"
88 #include <openssl/fips.h>
89
90 #define DES_BLOCK_SIZE 8
91
92 #define VERBOSE 0
93
94 static int DESTest(EVP_CIPHER_CTX *ctx,
95             char *amode, int akeysz, unsigned char *aKey, 
96             unsigned char *iVec, 
97             int dir,  /* 0 = decrypt, 1 = encrypt */
98             unsigned char *out, unsigned char *in, int len)
99     {
100     const EVP_CIPHER *cipher = NULL;
101
102     if (akeysz != 192)
103         {
104         printf("Invalid key size: %d\n", akeysz);
105         EXIT(1);
106         }
107
108     if (fips_strcasecmp(amode, "CBC") == 0)
109         cipher = EVP_des_ede3_cbc();
110     else if (fips_strcasecmp(amode, "ECB") == 0)
111         cipher = EVP_des_ede3_ecb();
112     else if (fips_strcasecmp(amode, "CFB64") == 0)
113         cipher = EVP_des_ede3_cfb64();
114     else if (fips_strncasecmp(amode, "OFB", 3) == 0)
115         cipher = EVP_des_ede3_ofb();
116     else if(!fips_strcasecmp(amode,"CFB8"))
117         cipher = EVP_des_ede3_cfb8();
118     else if(!fips_strcasecmp(amode,"CFB1"))
119         cipher = EVP_des_ede3_cfb1();
120     else
121         {
122         printf("Unknown mode: %s\n", amode);
123         EXIT(1);
124         }
125
126     if (FIPS_cipherinit(ctx, cipher, aKey, iVec, dir) <= 0)
127         return 0;
128     if(!fips_strcasecmp(amode,"CFB1"))
129         M_EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS);
130     FIPS_cipher(ctx, out, in, len);
131
132     return 1;
133     }
134 #if 0
135 static void DebugValue(char *tag, unsigned char *val, int len)
136     {
137     char obuf[2048];
138     int olen;
139     olen = bin2hex(val, len, obuf);
140     printf("%s = %.*s\n", tag, olen, obuf);
141     }
142 #endif
143 static void shiftin(unsigned char *dst,unsigned char *src,int nbits)
144     {
145     int n;
146
147     /* move the bytes... */
148     memmove(dst,dst+nbits/8,3*8-nbits/8);
149     /* append new data */
150     memcpy(dst+3*8-nbits/8,src,(nbits+7)/8);
151     /* left shift the bits */
152     if(nbits%8)
153         for(n=0 ; n < 3*8 ; ++n)
154             dst[n]=(dst[n] << (nbits%8))|(dst[n+1] >> (8-nbits%8));
155     }   
156
157 /*-----------------------------------------------*/
158 char *t_tag[2] = {"PLAINTEXT", "CIPHERTEXT"};
159 char *t_mode[6] = {"CBC","ECB","OFB","CFB1","CFB8","CFB64"};
160 enum Mode {CBC, ECB, OFB, CFB1, CFB8, CFB64};
161 int Sizes[6]={64,64,64,1,8,64};
162
163 static void do_mct(char *amode, 
164             int akeysz, int numkeys, unsigned char *akey,unsigned char *ivec,
165             int dir, unsigned char *text, int len,
166             FILE *rfp)
167     {
168     int i,imode;
169     unsigned char nk[4*8]; /* longest key+8 */
170     unsigned char text0[8];
171
172     for (imode=0 ; imode < 6 ; ++imode)
173         if(!strcmp(amode,t_mode[imode]))
174             break;
175     if (imode == 6)
176         { 
177         printf("Unrecognized mode: %s\n", amode);
178         EXIT(1);
179         }
180     for(i=0 ; i < 400 ; ++i)
181         {
182         int j;
183         int n;
184         int kp=akeysz/64;
185         unsigned char old_iv[8];
186         EVP_CIPHER_CTX ctx;
187         FIPS_cipher_ctx_init(&ctx);
188
189         fprintf(rfp,RESP_EOL "COUNT = %d" RESP_EOL,i);
190         if(kp == 1)
191             OutputValue("KEY",akey,8,rfp,0);
192         else
193             for(n=0 ; n < kp ; ++n)
194                 {
195                 fprintf(rfp,"KEY%d",n+1);
196                 OutputValue("",akey+n*8,8,rfp,0);
197                 }
198
199         if(imode != ECB)
200             OutputValue("IV",ivec,8,rfp,0);
201         OutputValue(t_tag[dir^1],text,len,rfp,imode == CFB1);
202 #if 0
203         /* compensate for endianness */
204         if(imode == CFB1)
205             text[0]<<=7;
206 #endif
207         memcpy(text0,text,8);
208
209         for(j=0 ; j < 10000 ; ++j)
210             {
211             unsigned char old_text[8];
212
213             memcpy(old_text,text,8);
214             if(j == 0)
215                 {
216                 memcpy(old_iv,ivec,8);
217                 DESTest(&ctx,amode,akeysz,akey,ivec,dir,text,text,len);
218                 }
219             else
220                 {
221                 memcpy(old_iv,ctx.iv,8);
222                 FIPS_cipher(&ctx,text,text,len);
223                 }
224             if(j == 9999)
225                 {
226                 OutputValue(t_tag[dir],text,len,rfp,imode == CFB1);
227                 /*              memcpy(ivec,text,8); */
228                 }
229             /*      DebugValue("iv",ctx.iv,8); */
230             /* accumulate material for the next key */
231             shiftin(nk,text,Sizes[imode]);
232             /*      DebugValue("nk",nk,24);*/
233             if((dir && (imode == CFB1 || imode == CFB8 || imode == CFB64
234                         || imode == CBC)) || imode == OFB)
235                 memcpy(text,old_iv,8);
236
237             if(!dir && (imode == CFB1 || imode == CFB8 || imode == CFB64))
238                 {
239                 /* the test specifies using the output of the raw DES operation
240                    which we don't have, so reconstruct it... */
241                 for(n=0 ; n < 8 ; ++n)
242                     text[n]^=old_text[n];
243                 }
244             }
245         for(n=0 ; n < 8 ; ++n)
246             akey[n]^=nk[16+n];
247         for(n=0 ; n < 8 ; ++n)
248             akey[8+n]^=nk[8+n];
249         for(n=0 ; n < 8 ; ++n)
250             akey[16+n]^=nk[n];
251         if(numkeys < 3)
252             memcpy(&akey[2*8],akey,8);
253         if(numkeys < 2)
254             memcpy(&akey[8],akey,8);
255         DES_set_odd_parity((DES_cblock *)akey);
256         DES_set_odd_parity((DES_cblock *)(akey+8));
257         DES_set_odd_parity((DES_cblock *)(akey+16));
258         memcpy(ivec,ctx.iv,8);
259
260         /* pointless exercise - the final text doesn't depend on the
261            initial text in OFB mode, so who cares what it is? (Who
262            designed these tests?) */
263         if(imode == OFB)
264             for(n=0 ; n < 8 ; ++n)
265                 text[n]=text0[n]^old_iv[n];
266         }
267     }
268     
269 static int proc_file(char *rqfile, char *rspfile)
270     {
271     char afn[256], rfn[256];
272     FILE *afp = NULL, *rfp = NULL;
273     char ibuf[2048], tbuf[2048];
274     int ilen, len, ret = 0;
275     char amode[8] = "";
276     char atest[100] = "";
277     int akeysz=0;
278     unsigned char iVec[20], aKey[40];
279     int dir = -1, err = 0, step = 0, echo = 1;
280     unsigned char plaintext[2048];
281     unsigned char ciphertext[2048];
282     char *rp;
283     EVP_CIPHER_CTX ctx;
284     int numkeys=1;
285     FIPS_cipher_ctx_init(&ctx);
286
287     if (!rqfile || !(*rqfile))
288         {
289         printf("No req file\n");
290         return -1;
291         }
292     strcpy(afn, rqfile);
293
294     if ((afp = fopen(afn, "r")) == NULL)
295         {
296         printf("Cannot open file: %s, %s\n", 
297                afn, strerror(errno));
298         return -1;
299         }
300     if (!rspfile)
301         {
302         strcpy(rfn,afn);
303         rp=strstr(rfn,"req/");
304 #ifdef OPENSSL_SYS_WIN32
305         if (!rp)
306             rp=strstr(rfn,"req\\");
307 #endif
308         assert(rp);
309         memcpy(rp,"rsp",3);
310         rp = strstr(rfn, ".req");
311         memcpy(rp, ".rsp", 4);
312         rspfile = rfn;
313         }
314     if ((rfp = fopen(rspfile, "w")) == NULL)
315         {
316         printf("Cannot open file: %s, %s\n", 
317                rfn, strerror(errno));
318         fclose(afp);
319         afp = NULL;
320         return -1;
321         }
322     while (!err && (fgets(ibuf, sizeof(ibuf), afp)) != NULL)
323         {
324         tidy_line(tbuf, ibuf);
325         ilen = strlen(ibuf);
326         /*      printf("step=%d ibuf=%s",step,ibuf);*/
327         if(step == 3 && !strcmp(amode,"ECB"))
328             {
329             memset(iVec, 0, sizeof(iVec));
330             step = (dir)? 4: 5;  /* no ivec for ECB */
331             }
332         switch (step)
333             {
334         case 0:  /* read preamble */
335             if (ibuf[0] == '\n')
336                 { /* end of preamble */
337                 if (*amode == '\0')
338                     {
339                     printf("Missing Mode\n");
340                     err = 1;
341                     }
342                 else
343                     {
344                     copy_line(ibuf, rfp);
345                     ++ step;
346                     }
347                 }
348             else if (ibuf[0] != '#')
349                 {
350                 printf("Invalid preamble item: %s\n", ibuf);
351                 err = 1;
352                 }
353             else
354                 { /* process preamble */
355                 char *xp, *pp = ibuf+2;
356                 int n;
357                 if(*amode)
358                     { /* insert current time & date */
359                     time_t rtim = time(0);
360                     fputs("# ", rfp);
361                     copy_line(ctime(&rtim), rfp);
362                     }
363                 else
364                     {
365                     copy_line(ibuf, rfp);
366                     if(!strncmp(pp,"INVERSE ",8) || !strncmp(pp,"DES ",4)
367                        || !strncmp(pp,"TDES ",5)
368                        || !strncmp(pp,"PERMUTATION ",12)
369                        || !strncmp(pp,"SUBSTITUTION ",13)
370                        || !strncmp(pp,"VARIABLE ",9))
371                         {
372                         /* get test type */
373                         if(!strncmp(pp,"DES ",4))
374                             pp+=4;
375                         else if(!strncmp(pp,"TDES ",5))
376                             pp+=5;
377                         xp = strchr(pp, ' ');
378                         n = xp-pp;
379                         strncpy(atest, pp, n);
380                         atest[n] = '\0';
381                         /* get mode */
382                         xp = strrchr(pp, ' '); /* get mode" */
383                         n = strlen(xp+1)-1;
384                         strncpy(amode, xp+1, n);
385                         amode[n] = '\0';
386                         if (!strcmp(atest, "Monte"))
387                                 echo = 0;
388                         /* amode[3] = '\0'; */
389                         if (VERBOSE)
390                                 printf("Test=%s, Mode=%s\n",atest,amode);
391                         }
392                     }
393                 }
394             break;
395
396         case 1:  /* [ENCRYPT] | [DECRYPT] */
397             if(ibuf[0] == '\n')
398                 break;
399             if (ibuf[0] == '[')
400                 {
401                 copy_line(ibuf, rfp);
402                 ++step;
403                 if (fips_strncasecmp(ibuf, "[ENCRYPT]", 9) == 0)
404                     dir = 1;
405                 else if (fips_strncasecmp(ibuf, "[DECRYPT]", 9) == 0)
406                     dir = 0;
407                 else
408                     {
409                     printf("Invalid keyword: %s\n", ibuf);
410                     err = 1;
411                     }
412                 break;
413                 }
414             else if (dir == -1)
415                 {
416                 err = 1;
417                 printf("Missing ENCRYPT/DECRYPT keyword\n");
418                 break;
419                 }
420             else 
421                 step = 2;
422
423         case 2: /* KEY = xxxx */
424             if(*ibuf == '\n')
425                 {
426                 copy_line(ibuf, rfp);
427                 break;
428                 }
429             if(!fips_strncasecmp(ibuf,"COUNT = ",8))
430                 {
431                 copy_line(ibuf, rfp);
432                 break;
433                 }
434             if(!fips_strncasecmp(ibuf,"COUNT=",6))
435                 {
436                 copy_line(ibuf, rfp);
437                 break;
438                 }
439             if(!fips_strncasecmp(ibuf,"NumKeys = ",10))
440                 {
441                 numkeys=atoi(ibuf+10);
442                 break;
443                 }
444             if (echo) 
445                 copy_line(ibuf, rfp);
446             if(!fips_strncasecmp(ibuf,"KEY = ",6))
447                 {
448                 akeysz=64;
449                 len = hex2bin((char*)ibuf+6, aKey);
450                 if (len < 0)
451                     {
452                     printf("Invalid KEY\n");
453                     err=1;
454                     break;
455                     }
456                 PrintValue("KEY", aKey, len);
457                 ++step;
458                 }
459             else if(!fips_strncasecmp(ibuf,"KEYs = ",7))
460                 {
461                 akeysz=64*3;
462                 len=hex2bin(ibuf+7,aKey);
463                 if(len != 8)
464                     {
465                     printf("Invalid KEY\n");
466                     err=1;
467                     break;
468                     }
469                 memcpy(aKey+8,aKey,8);
470                 memcpy(aKey+16,aKey,8);
471                 ibuf[4]='\0';
472                 PrintValue("KEYs",aKey,len);
473                 ++step;
474                 }
475             else if(!fips_strncasecmp(ibuf,"KEY",3))
476                 {
477                 int n=ibuf[3]-'1';
478
479                 akeysz=64*3;
480                 len=hex2bin(ibuf+7,aKey+n*8);
481                 if(len != 8)
482                     {
483                     printf("Invalid KEY\n");
484                     err=1;
485                     break;
486                     }
487                 ibuf[4]='\0';
488                 PrintValue(ibuf,aKey,len);
489                 if(n == 2)
490                     ++step;
491                 }
492             else
493                 {
494                 printf("Missing KEY\n");
495                 err = 1;
496                 }
497             break;
498
499         case 3: /* IV = xxxx */
500             if (echo)
501                 copy_line(ibuf, rfp);
502             if (fips_strncasecmp(ibuf, "IV = ", 5) != 0)
503                 {
504                 printf("Missing IV\n");
505                 err = 1;
506                 }
507             else
508                 {
509                 len = hex2bin((char*)ibuf+5, iVec);
510                 if (len < 0)
511                     {
512                     printf("Invalid IV\n");
513                     err =1;
514                     break;
515                     }
516                 PrintValue("IV", iVec, len);
517                 step = (dir)? 4: 5;
518                 }
519             break;
520
521         case 4: /* PLAINTEXT = xxxx */
522             if (echo)
523                 copy_line(ibuf, rfp);
524             if (fips_strncasecmp(ibuf, "PLAINTEXT = ", 12) != 0)
525                 {
526                 printf("Missing PLAINTEXT\n");
527                 err = 1;
528                 }
529             else
530                 {
531                 int nn = strlen(ibuf+12);
532                 if(!strcmp(amode,"CFB1"))
533                     len=bint2bin(ibuf+12,nn-1,plaintext);
534                 else
535                     len=hex2bin(ibuf+12, plaintext);
536                 if (len < 0)
537                     {
538                     printf("Invalid PLAINTEXT: %s", ibuf+12);
539                     err =1;
540                     break;
541                     }
542                 if (len >= (int)sizeof(plaintext))
543                     {
544                     printf("Buffer overflow\n");
545                     }
546                 PrintValue("PLAINTEXT", (unsigned char*)plaintext, len);
547                 if (strcmp(atest, "Monte") == 0)  /* Monte Carlo Test */
548                     {
549                     do_mct(amode,akeysz,numkeys,aKey,iVec,dir,plaintext,len,rfp);
550                     }
551                 else
552                     {
553                     assert(dir == 1);
554                     ret = DESTest(&ctx, amode, akeysz, aKey, iVec, 
555                                   dir,  /* 0 = decrypt, 1 = encrypt */
556                                   ciphertext, plaintext, len);
557                     OutputValue("CIPHERTEXT",ciphertext,len,rfp,
558                                 !strcmp(amode,"CFB1"));
559                     }
560                 step = 6;
561                 }
562             break;
563
564         case 5: /* CIPHERTEXT = xxxx */
565             if (echo)
566                 copy_line(ibuf, rfp);
567             if (fips_strncasecmp(ibuf, "CIPHERTEXT = ", 13) != 0)
568                 {
569                 printf("Missing KEY\n");
570                 err = 1;
571                 }
572             else
573                 {
574                 if(!strcmp(amode,"CFB1"))
575                     len=bint2bin(ibuf+13,strlen(ibuf+13)-1,ciphertext);
576                 else
577                     len = hex2bin(ibuf+13,ciphertext);
578                 if (len < 0)
579                     {
580                     printf("Invalid CIPHERTEXT\n");
581                     err =1;
582                     break;
583                     }
584                 
585                 PrintValue("CIPHERTEXT", ciphertext, len);
586                 if (strcmp(atest, "Monte") == 0)  /* Monte Carlo Test */
587                     {
588                     do_mct(amode, akeysz, numkeys, aKey, iVec, 
589                            dir, ciphertext, len, rfp);
590                     }
591                 else
592                     {
593                     assert(dir == 0);
594                     ret = DESTest(&ctx, amode, akeysz, aKey, iVec, 
595                                   dir,  /* 0 = decrypt, 1 = encrypt */
596                                   plaintext, ciphertext, len);
597                     OutputValue("PLAINTEXT",(unsigned char *)plaintext,len,rfp,
598                                 !strcmp(amode,"CFB1"));
599                     }
600                 step = 6;
601                 }
602             break;
603
604         case 6:
605             if (ibuf[0] != '\n')
606                 {
607                 err = 1;
608                 printf("Missing terminator\n");
609                 }
610             else if (strcmp(atest, "MCT") != 0)
611                 { /* MCT already added terminating nl */
612                 copy_line(ibuf, rfp);
613                 }
614             step = 1;
615             break;
616             }
617         }
618     if (rfp)
619         fclose(rfp);
620     if (afp)
621         fclose(afp);
622     return err;
623     }
624
625 /*--------------------------------------------------
626   Processes either a single file or 
627   a set of files whose names are passed in a file.
628   A single file is specified as:
629     aes_test -f xxx.req
630   A set of files is specified as:
631     aes_test -d xxxxx.xxx
632   The default is: -d req.txt
633 --------------------------------------------------*/
634 int main(int argc, char **argv)
635     {
636     char *rqlist = "req.txt", *rspfile = NULL;
637     FILE *fp = NULL;
638     char fn[250] = "", rfn[256] = "";
639     int f_opt = 0, d_opt = 1;
640
641     fips_algtest_init();
642     if (argc > 1)
643         {
644         if (fips_strcasecmp(argv[1], "-d") == 0)
645             {
646             d_opt = 1;
647             }
648         else if (fips_strcasecmp(argv[1], "-f") == 0)
649             {
650             f_opt = 1;
651             d_opt = 0;
652             }
653         else
654             {
655             printf("Invalid parameter: %s\n", argv[1]);
656             return 0;
657             }
658         if (argc < 3)
659             {
660             printf("Missing parameter\n");
661             return 0;
662             }
663         if (d_opt)
664             rqlist = argv[2];
665         else
666             {
667             strcpy(fn, argv[2]);
668             rspfile = argv[3];
669             }
670         }
671     if (d_opt)
672         { /* list of files (directory) */
673         if (!(fp = fopen(rqlist, "r")))
674             {
675             printf("Cannot open req list file\n");
676             return -1;
677             }
678         while (fgets(fn, sizeof(fn), fp))
679             {
680             strtok(fn, "\r\n");
681             strcpy(rfn, fn);
682             printf("Processing: %s\n", rfn);
683             if (proc_file(rfn, rspfile))
684                 {
685                 printf(">>> Processing failed for: %s <<<\n", rfn);
686                 EXIT(1);
687                 }
688             }
689         fclose(fp);
690         }
691     else /* single file */
692         {
693         if (VERBOSE)
694                 printf("Processing: %s\n", fn);
695         if (proc_file(fn, rspfile))
696             {
697             printf(">>> Processing failed for: %s <<<\n", fn);
698             }
699         }
700     EXIT(0);
701     return 0;
702     }
703
704 #endif