1 /* ====================================================================
2 * Copyright (c) 2004 The OpenSSL Project. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
30 * 6. Redistributions of any form whatsoever must retain the following
32 * "This product includes software developed by the OpenSSL Project
33 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
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.
49 /*---------------------------------------------
50 NIST DES Modes of Operation Validation System
53 Based on the AES Validation Suite, which was:
54 Donated to OpenSSL by:
56 20250 Century Blvd, Suite 300
59 ----------------------------------------------*/
67 #include <openssl/des.h>
68 #include <openssl/evp.h>
69 #include <openssl/bn.h>
71 #include <openssl/err.h>
76 int main(int argc, char *argv[])
78 printf("No FIPS DES support\n");
84 #include <openssl/fips.h>
87 #define DES_BLOCK_SIZE 8
91 static int DESTest(EVP_CIPHER_CTX *ctx,
92 char *amode, int akeysz, unsigned char *aKey,
94 int dir, /* 0 = decrypt, 1 = encrypt */
95 unsigned char *out, unsigned char *in, int len)
97 const EVP_CIPHER *cipher = NULL;
101 printf("Invalid key size: %d\n", akeysz);
105 if (strcasecmp(amode, "CBC") == 0)
106 cipher = EVP_des_ede3_cbc();
107 else if (strcasecmp(amode, "ECB") == 0)
108 cipher = EVP_des_ede3_ecb();
109 else if (strcasecmp(amode, "CFB64") == 0)
110 cipher = EVP_des_ede3_cfb64();
111 else if (strncasecmp(amode, "OFB", 3) == 0)
112 cipher = EVP_des_ede3_ofb();
113 else if(!strcasecmp(amode,"CFB8"))
114 cipher = EVP_des_ede3_cfb8();
115 else if(!strcasecmp(amode,"CFB1"))
116 cipher = EVP_des_ede3_cfb1();
119 printf("Unknown mode: %s\n", amode);
123 if (EVP_CipherInit_ex(ctx, cipher, NULL, aKey, iVec, dir) <= 0)
125 if(!strcasecmp(amode,"CFB1"))
126 M_EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS);
127 EVP_Cipher(ctx, out, in, len);
132 static void DebugValue(char *tag, unsigned char *val, int len)
136 olen = bin2hex(val, len, obuf);
137 printf("%s = %.*s\n", tag, olen, obuf);
140 static void shiftin(unsigned char *dst,unsigned char *src,int nbits)
144 /* move the bytes... */
145 memmove(dst,dst+nbits/8,3*8-nbits/8);
146 /* append new data */
147 memcpy(dst+3*8-nbits/8,src,(nbits+7)/8);
148 /* left shift the bits */
150 for(n=0 ; n < 3*8 ; ++n)
151 dst[n]=(dst[n] << (nbits%8))|(dst[n+1] >> (8-nbits%8));
154 /*-----------------------------------------------*/
155 char *t_tag[2] = {"PLAINTEXT", "CIPHERTEXT"};
156 char *t_mode[6] = {"CBC","ECB","OFB","CFB1","CFB8","CFB64"};
157 enum Mode {CBC, ECB, OFB, CFB1, CFB8, CFB64};
158 int Sizes[6]={64,64,64,1,8,64};
160 static void do_mct(char *amode,
161 int akeysz, int numkeys, unsigned char *akey,unsigned char *ivec,
162 int dir, unsigned char *text, int len,
166 unsigned char nk[4*8]; /* longest key+8 */
167 unsigned char text0[8];
169 for (imode=0 ; imode < 6 ; ++imode)
170 if(!strcmp(amode,t_mode[imode]))
174 printf("Unrecognized mode: %s\n", amode);
178 for(i=0 ; i < 400 ; ++i)
183 unsigned char old_iv[8];
185 EVP_CIPHER_CTX_init(&ctx);
187 fprintf(rfp,"\nCOUNT = %d\n",i);
189 OutputValue("KEY",akey,8,rfp,0);
191 for(n=0 ; n < kp ; ++n)
193 fprintf(rfp,"KEY%d",n+1);
194 OutputValue("",akey+n*8,8,rfp,0);
198 OutputValue("IV",ivec,8,rfp,0);
199 OutputValue(t_tag[dir^1],text,len,rfp,imode == CFB1);
201 /* compensate for endianness */
205 memcpy(text0,text,8);
207 for(j=0 ; j < 10000 ; ++j)
209 unsigned char old_text[8];
211 memcpy(old_text,text,8);
214 memcpy(old_iv,ivec,8);
215 DESTest(&ctx,amode,akeysz,akey,ivec,dir,text,text,len);
219 memcpy(old_iv,ctx.iv,8);
220 EVP_Cipher(&ctx,text,text,len);
224 OutputValue(t_tag[dir],text,len,rfp,imode == CFB1);
225 /* memcpy(ivec,text,8); */
227 /* DebugValue("iv",ctx.iv,8); */
228 /* accumulate material for the next key */
229 shiftin(nk,text,Sizes[imode]);
230 /* DebugValue("nk",nk,24);*/
231 if((dir && (imode == CFB1 || imode == CFB8 || imode == CFB64
232 || imode == CBC)) || imode == OFB)
233 memcpy(text,old_iv,8);
235 if(!dir && (imode == CFB1 || imode == CFB8 || imode == CFB64))
237 /* the test specifies using the output of the raw DES operation
238 which we don't have, so reconstruct it... */
239 for(n=0 ; n < 8 ; ++n)
240 text[n]^=old_text[n];
243 for(n=0 ; n < 8 ; ++n)
245 for(n=0 ; n < 8 ; ++n)
247 for(n=0 ; n < 8 ; ++n)
250 memcpy(&akey[2*8],akey,8);
252 memcpy(&akey[8],akey,8);
253 DES_set_odd_parity((DES_cblock *)akey);
254 DES_set_odd_parity((DES_cblock *)(akey+8));
255 DES_set_odd_parity((DES_cblock *)(akey+16));
256 memcpy(ivec,ctx.iv,8);
258 /* pointless exercise - the final text doesn't depend on the
259 initial text in OFB mode, so who cares what it is? (Who
260 designed these tests?) */
262 for(n=0 ; n < 8 ; ++n)
263 text[n]=text0[n]^old_iv[n];
267 static int proc_file(char *rqfile, char *rspfile)
269 char afn[256], rfn[256];
270 FILE *afp = NULL, *rfp = NULL;
271 char ibuf[2048], tbuf[2048];
272 int ilen, len, ret = 0;
274 char atest[100] = "";
276 unsigned char iVec[20], aKey[40];
277 int dir = -1, err = 0, step = 0;
278 unsigned char plaintext[2048];
279 unsigned char ciphertext[2048];
283 EVP_CIPHER_CTX_init(&ctx);
285 if (!rqfile || !(*rqfile))
287 printf("No req file\n");
292 if ((afp = fopen(afn, "r")) == NULL)
294 printf("Cannot open file: %s, %s\n",
295 afn, strerror(errno));
301 rp=strstr(rfn,"req/");
302 #ifdef OPENSSL_SYS_WIN32
304 rp=strstr(rfn,"req\\");
308 rp = strstr(rfn, ".req");
309 memcpy(rp, ".rsp", 4);
312 if ((rfp = fopen(rspfile, "w")) == NULL)
314 printf("Cannot open file: %s, %s\n",
315 rfn, strerror(errno));
320 while (!err && (fgets(ibuf, sizeof(ibuf), afp)) != NULL)
322 tidy_line(tbuf, ibuf);
324 /* printf("step=%d ibuf=%s",step,ibuf);*/
325 if(step == 3 && !strcmp(amode,"ECB"))
327 memset(iVec, 0, sizeof(iVec));
328 step = (dir)? 4: 5; /* no ivec for ECB */
332 case 0: /* read preamble */
334 { /* end of preamble */
337 printf("Missing Mode\n");
346 else if (ibuf[0] != '#')
348 printf("Invalid preamble item: %s\n", ibuf);
352 { /* process preamble */
353 char *xp, *pp = ibuf+2;
356 { /* insert current time & date */
357 time_t rtim = time(0);
358 fprintf(rfp, "# %s", ctime(&rtim));
363 if(!strncmp(pp,"INVERSE ",8) || !strncmp(pp,"DES ",4)
364 || !strncmp(pp,"TDES ",5)
365 || !strncmp(pp,"PERMUTATION ",12)
366 || !strncmp(pp,"SUBSTITUTION ",13)
367 || !strncmp(pp,"VARIABLE ",9))
370 if(!strncmp(pp,"DES ",4))
372 else if(!strncmp(pp,"TDES ",5))
374 xp = strchr(pp, ' ');
376 strncpy(atest, pp, n);
379 xp = strrchr(pp, ' '); /* get mode" */
381 strncpy(amode, xp+1, n);
383 /* amode[3] = '\0'; */
385 printf("Test=%s, Mode=%s\n",atest,amode);
391 case 1: /* [ENCRYPT] | [DECRYPT] */
398 if (strncasecmp(ibuf, "[ENCRYPT]", 9) == 0)
400 else if (strncasecmp(ibuf, "[DECRYPT]", 9) == 0)
404 printf("Invalid keyword: %s\n", ibuf);
412 printf("Missing ENCRYPT/DECRYPT keyword\n");
418 case 2: /* KEY = xxxx */
424 if(!strncasecmp(ibuf,"COUNT = ",8))
429 if(!strncasecmp(ibuf,"COUNT=",6))
434 if(!strncasecmp(ibuf,"NumKeys = ",10))
436 numkeys=atoi(ibuf+10);
441 if(!strncasecmp(ibuf,"KEY = ",6))
444 len = hex2bin((char*)ibuf+6, aKey);
447 printf("Invalid KEY\n");
451 PrintValue("KEY", aKey, len);
454 else if(!strncasecmp(ibuf,"KEYs = ",7))
457 len=hex2bin(ibuf+7,aKey);
460 printf("Invalid KEY\n");
464 memcpy(aKey+8,aKey,8);
465 memcpy(aKey+16,aKey,8);
467 PrintValue("KEYs",aKey,len);
470 else if(!strncasecmp(ibuf,"KEY",3))
475 len=hex2bin(ibuf+7,aKey+n*8);
478 printf("Invalid KEY\n");
483 PrintValue(ibuf,aKey,len);
489 printf("Missing KEY\n");
494 case 3: /* IV = xxxx */
496 if (strncasecmp(ibuf, "IV = ", 5) != 0)
498 printf("Missing IV\n");
503 len = hex2bin((char*)ibuf+5, iVec);
506 printf("Invalid IV\n");
510 PrintValue("IV", iVec, len);
515 case 4: /* PLAINTEXT = xxxx */
517 if (strncasecmp(ibuf, "PLAINTEXT = ", 12) != 0)
519 printf("Missing PLAINTEXT\n");
524 int nn = strlen(ibuf+12);
525 if(!strcmp(amode,"CFB1"))
526 len=bint2bin(ibuf+12,nn-1,plaintext);
528 len=hex2bin(ibuf+12, plaintext);
531 printf("Invalid PLAINTEXT: %s", ibuf+12);
535 if (len >= (int)sizeof(plaintext))
537 printf("Buffer overflow\n");
539 PrintValue("PLAINTEXT", (unsigned char*)plaintext, len);
540 if (strcmp(atest, "Monte") == 0) /* Monte Carlo Test */
542 do_mct(amode,akeysz,numkeys,aKey,iVec,dir,plaintext,len,rfp);
547 ret = DESTest(&ctx, amode, akeysz, aKey, iVec,
548 dir, /* 0 = decrypt, 1 = encrypt */
549 ciphertext, plaintext, len);
550 OutputValue("CIPHERTEXT",ciphertext,len,rfp,
551 !strcmp(amode,"CFB1"));
557 case 5: /* CIPHERTEXT = xxxx */
559 if (strncasecmp(ibuf, "CIPHERTEXT = ", 13) != 0)
561 printf("Missing KEY\n");
566 if(!strcmp(amode,"CFB1"))
567 len=bint2bin(ibuf+13,strlen(ibuf+13)-1,ciphertext);
569 len = hex2bin(ibuf+13,ciphertext);
572 printf("Invalid CIPHERTEXT\n");
577 PrintValue("CIPHERTEXT", ciphertext, len);
578 if (strcmp(atest, "Monte") == 0) /* Monte Carlo Test */
580 do_mct(amode, akeysz, numkeys, aKey, iVec,
581 dir, ciphertext, len, rfp);
586 ret = DESTest(&ctx, amode, akeysz, aKey, iVec,
587 dir, /* 0 = decrypt, 1 = encrypt */
588 plaintext, ciphertext, len);
589 OutputValue("PLAINTEXT",(unsigned char *)plaintext,len,rfp,
590 !strcmp(amode,"CFB1"));
600 printf("Missing terminator\n");
602 else if (strcmp(atest, "MCT") != 0)
603 { /* MCT already added terminating nl */
617 /*--------------------------------------------------
618 Processes either a single file or
619 a set of files whose names are passed in a file.
620 A single file is specified as:
622 A set of files is specified as:
623 aes_test -d xxxxx.xxx
624 The default is: -d req.txt
625 --------------------------------------------------*/
626 int main(int argc, char **argv)
628 char *rqlist = "req.txt", *rspfile = NULL;
630 char fn[250] = "", rfn[256] = "";
631 int f_opt = 0, d_opt = 1;
634 if(!FIPS_mode_set(1))
642 if (strcasecmp(argv[1], "-d") == 0)
646 else if (strcasecmp(argv[1], "-f") == 0)
653 printf("Invalid parameter: %s\n", argv[1]);
658 printf("Missing parameter\n");
670 { /* list of files (directory) */
671 if (!(fp = fopen(rqlist, "r")))
673 printf("Cannot open req list file\n");
676 while (fgets(fn, sizeof(fn), fp))
680 printf("Processing: %s\n", rfn);
681 if (proc_file(rfn, rspfile))
683 printf(">>> Processing failed for: %s <<<\n", rfn);
689 else /* single file */
692 printf("Processing: %s\n", fn);
693 if (proc_file(fn, rspfile))
695 printf(">>> Processing failed for: %s <<<\n", fn);