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 AES Algorithm Validation Suite
53 Donated to OpenSSL by:
55 20250 Century Blvd, Suite 300
58 ----------------------------------------------*/
66 #include <openssl/aes.h>
67 #include <openssl/evp.h>
68 #include <openssl/bn.h>
70 #include <openssl/err.h>
75 int main(int argc, char *argv[])
77 printf("No FIPS AES support\n");
83 #include <openssl/fips.h>
86 #define AES_BLOCK_SIZE 16
90 /*-----------------------------------------------*/
92 int AESTest(EVP_CIPHER_CTX *ctx,
93 char *amode, int akeysz, unsigned char *aKey,
95 int dir, /* 0 = decrypt, 1 = encrypt */
96 unsigned char *plaintext, unsigned char *ciphertext, int len)
98 const EVP_CIPHER *cipher = NULL;
100 if (strcasecmp(amode, "CBC") == 0)
105 cipher = EVP_aes_128_cbc();
109 cipher = EVP_aes_192_cbc();
113 cipher = EVP_aes_256_cbc();
118 else if (strcasecmp(amode, "ECB") == 0)
123 cipher = EVP_aes_128_ecb();
127 cipher = EVP_aes_192_ecb();
131 cipher = EVP_aes_256_ecb();
135 else if (strcasecmp(amode, "CFB128") == 0)
140 cipher = EVP_aes_128_cfb128();
144 cipher = EVP_aes_192_cfb128();
148 cipher = EVP_aes_256_cfb128();
153 else if (strncasecmp(amode, "OFB", 3) == 0)
158 cipher = EVP_aes_128_ofb();
162 cipher = EVP_aes_192_ofb();
166 cipher = EVP_aes_256_ofb();
170 else if(!strcasecmp(amode,"CFB1"))
175 cipher = EVP_aes_128_cfb1();
179 cipher = EVP_aes_192_cfb1();
183 cipher = EVP_aes_256_cfb1();
187 else if(!strcasecmp(amode,"CFB8"))
192 cipher = EVP_aes_128_cfb8();
196 cipher = EVP_aes_192_cfb8();
200 cipher = EVP_aes_256_cfb8();
206 printf("Unknown mode: %s\n", amode);
211 printf("Invalid key size: %d\n", akeysz);
214 if (EVP_CipherInit_ex(ctx, cipher, NULL, aKey, iVec, dir) <= 0)
216 if(!strcasecmp(amode,"CFB1"))
217 M_EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS);
219 EVP_Cipher(ctx, ciphertext, plaintext, len);
221 EVP_Cipher(ctx, plaintext, ciphertext, len);
225 /*-----------------------------------------------*/
226 char *t_tag[2] = {"PLAINTEXT", "CIPHERTEXT"};
227 char *t_mode[6] = {"CBC","ECB","OFB","CFB1","CFB8","CFB128"};
228 enum Mode {CBC, ECB, OFB, CFB1, CFB8, CFB128};
229 enum XCrypt {XDECRYPT, XENCRYPT};
231 /*=============================*/
232 /* Monte Carlo Tests */
233 /*-----------------------------*/
235 /*#define gb(a,b) (((a)[(b)/8] >> ((b)%8))&1)*/
236 /*#define sb(a,b,v) ((a)[(b)/8]=((a)[(b)/8]&~(1 << ((b)%8)))|(!!(v) << ((b)%8)))*/
238 #define gb(a,b) (((a)[(b)/8] >> (7-(b)%8))&1)
239 #define sb(a,b,v) ((a)[(b)/8]=((a)[(b)/8]&~(1 << (7-(b)%8)))|(!!(v) << (7-(b)%8)))
241 int do_mct(char *amode,
242 int akeysz, unsigned char *aKey,unsigned char *iVec,
243 int dir, unsigned char *text, int len,
247 unsigned char key[101][32];
248 unsigned char iv[101][AES_BLOCK_SIZE];
249 unsigned char ptext[1001][32];
250 unsigned char ctext[1001][32];
251 unsigned char ciphertext[64+4];
253 int imode = 0, nkeysz = akeysz/8;
255 EVP_CIPHER_CTX_init(&ctx);
259 printf("\n>>>> Length exceeds 32 for %s %d <<<<\n\n",
263 for (imode = 0; imode < 6; ++imode)
264 if (strcmp(amode, t_mode[imode]) == 0)
268 printf("Unrecognized mode: %s\n", amode);
272 memcpy(key[0], aKey, nkeysz);
274 memcpy(iv[0], iVec, AES_BLOCK_SIZE);
276 memcpy(ptext[0], text, len);
278 memcpy(ctext[0], text, len);
279 for (i = 0; i < 100; ++i)
281 /* printf("Iteration %d\n", i); */
284 fprintf(rfp,"COUNT = %d\n",i);
285 OutputValue("KEY",key[i],nkeysz,rfp,0);
286 if (imode != ECB) /* ECB */
287 OutputValue("IV",iv[i],AES_BLOCK_SIZE,rfp,0);
288 /* Output Ciphertext | Plaintext */
289 OutputValue(t_tag[dir^1],dir ? ptext[0] : ctext[0],len,rfp,
292 for (j = 0; j < 1000; ++j)
298 { /* set up encryption */
299 ret = AESTest(&ctx, amode, akeysz, key[i], NULL,
300 dir, /* 0 = decrypt, 1 = encrypt */
301 ptext[j], ctext[j], len);
303 memcpy(ptext[j+1], ctext[j], len);
305 memcpy(ctext[j+1], ptext[j], len);
311 EVP_Cipher(&ctx, ctext[j], ptext[j], len);
312 memcpy(ptext[j+1], ctext[j], len);
316 EVP_Cipher(&ctx, ptext[j], ctext[j], len);
317 memcpy(ctext[j+1], ptext[j], len);
327 ret = AESTest(&ctx, amode, akeysz, key[i], iv[i],
328 dir, /* 0 = decrypt, 1 = encrypt */
329 ptext[j], ctext[j], len);
331 memcpy(ptext[j+1], iv[i], len);
333 memcpy(ctext[j+1], iv[i], len);
339 EVP_Cipher(&ctx, ctext[j], ptext[j], len);
340 memcpy(ptext[j+1], ctext[j-1], len);
344 EVP_Cipher(&ctx, ptext[j], ctext[j], len);
345 memcpy(ctext[j+1], ptext[j-1], len);
353 ret = AESTest(&ctx, amode, akeysz, key[i], iv[i],
354 dir, /* 0 = decrypt, 1 = encrypt */
355 ptext[j], ctext[j], len);
360 EVP_Cipher(&ctx, ctext[j], ptext[j], len);
362 EVP_Cipher(&ctx, ptext[j], ctext[j], len);
367 memcpy(ptext[j+1], &iv[i][j], len);
369 memcpy(ptext[j+1], ctext[j-16], len);
374 memcpy(ctext[j+1], &iv[i][j], len);
376 memcpy(ctext[j+1], ptext[j-16], len);
384 /* compensate for wrong endianness of input file */
388 ret = AESTest(&ctx,amode,akeysz,key[i],iv[i],dir,
389 ptext[j], ctext[j], len);
394 EVP_Cipher(&ctx, ctext[j], ptext[j], len);
396 EVP_Cipher(&ctx, ptext[j], ctext[j], len);
402 sb(ptext[j+1],0,gb(iv[i],j));
404 sb(ptext[j+1],0,gb(ctext[j-128],0));
409 sb(ctext[j+1],0,gb(iv[i],j));
411 sb(ctext[j+1],0,gb(ptext[j-128],0));
416 --j; /* reset to last of range */
417 /* Output Ciphertext | Plaintext */
418 OutputValue(t_tag[dir],dir ? ctext[j] : ptext[j],len,rfp,
420 fprintf(rfp, "\n"); /* add separator */
422 /* Compute next KEY */
426 { /* ct = CT[j-15] || CT[j-14] || ... || CT[j] */
427 for (n1 = 0, n2 = nkeysz-1; n1 < nkeysz; ++n1, --n2)
428 ciphertext[n1] = ctext[j-n2][0];
430 else if(imode == CFB1)
432 for(n1=0,n2=akeysz-1 ; n1 < akeysz ; ++n1,--n2)
433 sb(ciphertext,n1,gb(ctext[j-n2],0));
439 memcpy(ciphertext, ctext[j], 16);
442 memcpy(ciphertext, ctext[j-1]+8, 8);
443 memcpy(ciphertext+8, ctext[j], 16);
446 memcpy(ciphertext, ctext[j-1], 16);
447 memcpy(ciphertext+16, ctext[j], 16);
454 { /* ct = CT[j-15] || CT[j-14] || ... || CT[j] */
455 for (n1 = 0, n2 = nkeysz-1; n1 < nkeysz; ++n1, --n2)
456 ciphertext[n1] = ptext[j-n2][0];
458 else if(imode == CFB1)
460 for(n1=0,n2=akeysz-1 ; n1 < akeysz ; ++n1,--n2)
461 sb(ciphertext,n1,gb(ptext[j-n2],0));
467 memcpy(ciphertext, ptext[j], 16);
470 memcpy(ciphertext, ptext[j-1]+8, 8);
471 memcpy(ciphertext+8, ptext[j], 16);
474 memcpy(ciphertext, ptext[j-1], 16);
475 memcpy(ciphertext+16, ptext[j], 16);
479 /* Compute next key: Key[i+1] = Key[i] xor ct */
480 for (n = 0; n < nkeysz; ++n)
481 key[i+1][n] = key[i][n] ^ ciphertext[n];
483 /* Compute next IV and text */
489 memcpy(ptext[0], ctext[j], AES_BLOCK_SIZE);
494 memcpy(iv[i+1], ctext[j], AES_BLOCK_SIZE);
495 memcpy(ptext[0], ctext[j-1], AES_BLOCK_SIZE);
499 for (n1 = 0, n2 = 15; n1 < 16; ++n1, --n2)
500 iv[i+1][n1] = ctext[j-n2][0];
501 ptext[0][0] = ctext[j-16][0];
504 for(n1=0,n2=127 ; n1 < 128 ; ++n1,--n2)
505 sb(iv[i+1],n1,gb(ctext[j-n2],0));
506 ptext[0][0]=ctext[j-128][0]&0x80;
515 memcpy(ctext[0], ptext[j], AES_BLOCK_SIZE);
520 memcpy(iv[i+1], ptext[j], AES_BLOCK_SIZE);
521 memcpy(ctext[0], ptext[j-1], AES_BLOCK_SIZE);
524 for (n1 = 0, n2 = 15; n1 < 16; ++n1, --n2)
525 iv[i+1][n1] = ptext[j-n2][0];
526 ctext[0][0] = ptext[j-16][0];
529 for(n1=0,n2=127 ; n1 < 128 ; ++n1,--n2)
530 sb(iv[i+1],n1,gb(ptext[j-n2],0));
531 ctext[0][0]=ptext[j-128][0]&0x80;
540 /*================================================*/
541 /*----------------------------
542 # Config info for v-one
543 # AESVS MMT test data for ECB
544 # State : Encrypt and Decrypt
546 # Fri Aug 30 04:07:22 PM
547 ----------------------------*/
549 int proc_file(char *rqfile, char *rspfile)
551 char afn[256], rfn[256];
552 FILE *afp = NULL, *rfp = NULL;
555 int ilen, len, ret = 0;
560 unsigned char iVec[20], aKey[40];
561 int dir = -1, err = 0, step = 0;
562 unsigned char plaintext[2048];
563 unsigned char ciphertext[2048];
566 EVP_CIPHER_CTX_init(&ctx);
568 if (!rqfile || !(*rqfile))
570 printf("No req file\n");
575 if ((afp = fopen(afn, "r")) == NULL)
577 printf("Cannot open file: %s, %s\n",
578 afn, strerror(errno));
584 rp=strstr(rfn,"req/");
585 #ifdef OPENSSL_SYS_WIN32
587 rp=strstr(rfn,"req\\");
591 rp = strstr(rfn, ".req");
592 memcpy(rp, ".rsp", 4);
595 if ((rfp = fopen(rspfile, "w")) == NULL)
597 printf("Cannot open file: %s, %s\n",
598 rfn, strerror(errno));
603 while (!err && (fgets(ibuf, sizeof(ibuf), afp)) != NULL)
605 tidy_line(tbuf, ibuf);
607 /* printf("step=%d ibuf=%s",step,ibuf); */
610 case 0: /* read preamble */
612 { /* end of preamble */
613 if ((*algo == '\0') ||
617 printf("Missing Algorithm, Mode or KeySize (%s/%s/%d)\n",
627 else if (ibuf[0] != '#')
629 printf("Invalid preamble item: %s\n", ibuf);
633 { /* process preamble */
634 char *xp, *pp = ibuf+2;
637 { /* insert current time & date */
638 time_t rtim = time(0);
639 fprintf(rfp, "# %s", ctime(&rtim));
644 if (strncmp(pp, "AESVS ", 6) == 0)
649 xp = strchr(pp, ' ');
651 strncpy(atest, pp, n);
654 xp = strrchr(pp, ' '); /* get mode" */
656 strncpy(amode, xp+1, n);
658 /* amode[3] = '\0'; */
660 printf("Test = %s, Mode = %s\n", atest, amode);
662 else if (strncasecmp(pp, "Key Length : ", 13) == 0)
664 akeysz = atoi(pp+13);
666 printf("Key size = %d\n", akeysz);
672 case 1: /* [ENCRYPT] | [DECRYPT] */
677 if (strncasecmp(ibuf, "[ENCRYPT]", 9) == 0)
679 else if (strncasecmp(ibuf, "[DECRYPT]", 9) == 0)
683 printf("Invalid keyword: %s\n", ibuf);
691 printf("Missing ENCRYPT/DECRYPT keyword\n");
697 case 2: /* KEY = xxxx */
701 if(!strncasecmp(ibuf,"COUNT = ",8))
704 if (strncasecmp(ibuf, "KEY = ", 6) != 0)
706 printf("Missing KEY\n");
711 len = hex2bin((char*)ibuf+6, aKey);
714 printf("Invalid KEY\n");
718 PrintValue("KEY", aKey, len);
719 if (strcmp(amode, "ECB") == 0)
721 memset(iVec, 0, sizeof(iVec));
722 step = (dir)? 4: 5; /* no ivec for ECB */
729 case 3: /* IV = xxxx */
731 if (strncasecmp(ibuf, "IV = ", 5) != 0)
733 printf("Missing IV\n");
738 len = hex2bin((char*)ibuf+5, iVec);
741 printf("Invalid IV\n");
745 PrintValue("IV", iVec, len);
750 case 4: /* PLAINTEXT = xxxx */
752 if (strncasecmp(ibuf, "PLAINTEXT = ", 12) != 0)
754 printf("Missing PLAINTEXT\n");
759 int nn = strlen(ibuf+12);
760 if(!strcmp(amode,"CFB1"))
761 len=bint2bin(ibuf+12,nn-1,plaintext);
763 len=hex2bin(ibuf+12, plaintext);
766 printf("Invalid PLAINTEXT: %s", ibuf+12);
770 if (len >= sizeof(plaintext))
772 printf("Buffer overflow\n");
774 PrintValue("PLAINTEXT", (unsigned char*)plaintext, len);
775 if (strcmp(atest, "MCT") == 0) /* Monte Carlo Test */
777 if(do_mct(amode, akeysz, aKey, iVec,
778 dir, (unsigned char*)plaintext, len,
784 ret = AESTest(&ctx, amode, akeysz, aKey, iVec,
785 dir, /* 0 = decrypt, 1 = encrypt */
786 plaintext, ciphertext, len);
787 OutputValue("CIPHERTEXT",ciphertext,len,rfp,
788 !strcmp(amode,"CFB1"));
794 case 5: /* CIPHERTEXT = xxxx */
796 if (strncasecmp(ibuf, "CIPHERTEXT = ", 13) != 0)
798 printf("Missing KEY\n");
803 if(!strcmp(amode,"CFB1"))
804 len=bint2bin(ibuf+13,strlen(ibuf+13)-1,ciphertext);
806 len = hex2bin(ibuf+13,ciphertext);
809 printf("Invalid CIPHERTEXT\n");
814 PrintValue("CIPHERTEXT", ciphertext, len);
815 if (strcmp(atest, "MCT") == 0) /* Monte Carlo Test */
817 do_mct(amode, akeysz, aKey, iVec,
818 dir, ciphertext, len, rfp);
822 ret = AESTest(&ctx, amode, akeysz, aKey, iVec,
823 dir, /* 0 = decrypt, 1 = encrypt */
824 plaintext, ciphertext, len);
825 OutputValue("PLAINTEXT",(unsigned char *)plaintext,len,rfp,
826 !strcmp(amode,"CFB1"));
836 printf("Missing terminator\n");
838 else if (strcmp(atest, "MCT") != 0)
839 { /* MCT already added terminating nl */
853 /*--------------------------------------------------
854 Processes either a single file or
855 a set of files whose names are passed in a file.
856 A single file is specified as:
858 A set of files is specified as:
859 aes_test -d xxxxx.xxx
860 The default is: -d req.txt
861 --------------------------------------------------*/
862 int main(int argc, char **argv)
864 char *rqlist = "req.txt", *rspfile = NULL;
866 char fn[250] = "", rfn[256] = "";
867 int f_opt = 0, d_opt = 1;
870 if(!FIPS_mode_set(1))
878 if (strcasecmp(argv[1], "-d") == 0)
882 else if (strcasecmp(argv[1], "-f") == 0)
889 printf("Invalid parameter: %s\n", argv[1]);
894 printf("Missing parameter\n");
906 { /* list of files (directory) */
907 if (!(fp = fopen(rqlist, "r")))
909 printf("Cannot open req list file\n");
912 while (fgets(fn, sizeof(fn), fp))
917 printf("Processing: %s\n", rfn);
918 if (proc_file(rfn, rspfile))
920 printf(">>> Processing failed for: %s <<<\n", rfn);
926 else /* single file */
929 printf("Processing: %s\n", fn);
930 if (proc_file(fn, rspfile))
932 printf(">>> Processing failed for: %s <<<\n", fn);