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 ----------------------------------------------*/
67 #include <openssl/aes.h>
68 #include <openssl/evp.h>
69 #include <openssl/fips.h>
70 #include <openssl/err.h>
75 int main(int argc, char *argv[])
77 printf("No FIPS AES support\n");
85 #define AES_BLOCK_SIZE 16
89 /*-----------------------------------------------*/
94 unsigned char tiv[AES_BLOCK_SIZE];
95 int dir, cmode, cbits, num;
98 int AES_Cipher(AES_CTX *ctx,
104 unsigned long len = inl;
108 case EVP_CIPH_ECB_MODE:
111 AES_ecb_encrypt(in, out, &ctx->ks, ctx->dir);
112 in += AES_BLOCK_SIZE;
113 out += AES_BLOCK_SIZE;
114 len -= AES_BLOCK_SIZE;
118 case EVP_CIPH_CBC_MODE:
119 AES_cbc_encrypt(in, out, len, &ctx->ks, ctx->tiv, ctx->dir);
122 case EVP_CIPH_CFB_MODE:
124 AES_cfb1_encrypt(in, out, len, &ctx->ks, ctx->tiv,
125 &ctx->num, ctx->dir);
126 else if (ctx->cbits == 8)
127 AES_cfb8_encrypt(in, out, len, &ctx->ks, ctx->tiv,
128 &ctx->num, ctx->dir);
129 else if (ctx->cbits == 128)
130 AES_cfb128_encrypt(in, out, len, &ctx->ks, ctx->tiv,
131 &ctx->num, ctx->dir);
134 case EVP_CIPH_OFB_MODE:
135 AES_ofb128_encrypt(in, out, len, &ctx->ks, ctx->tiv,
151 int AESTest(AES_CTX *ctx,
152 char *amode, int akeysz, unsigned char *aKey,
154 int dir, /* 0 = decrypt, 1 = encrypt */
155 unsigned char *plaintext, unsigned char *ciphertext, int len)
163 if (strcasecmp(amode, "CBC") == 0)
164 ctx->cmode = EVP_CIPH_CBC_MODE;
165 else if (strcasecmp(amode, "ECB") == 0)
166 ctx->cmode = EVP_CIPH_ECB_MODE;
167 else if (strcasecmp(amode, "CFB128") == 0)
170 ctx->cmode = EVP_CIPH_CFB_MODE;
172 else if (strncasecmp(amode, "OFB", 3) == 0)
173 ctx->cmode = EVP_CIPH_OFB_MODE;
174 else if(!strcasecmp(amode,"CFB1"))
177 ctx->cmode = EVP_CIPH_CFB_MODE;
179 else if(!strcasecmp(amode,"CFB8"))
182 ctx->cmode = EVP_CIPH_CFB_MODE;
186 printf("Unknown mode: %s\n", amode);
191 if ((akeysz != 128) && (akeysz != 192) && (akeysz != 256))
193 printf("Invalid key size: %d\n", akeysz);
197 || (ctx->cmode == EVP_CIPH_CFB_MODE)
198 || (ctx->cmode == EVP_CIPH_OFB_MODE))
199 AES_set_encrypt_key(aKey, akeysz, &ctx->ks);
201 AES_set_decrypt_key(aKey, akeysz, &ctx->ks);
203 memcpy(ctx->tiv, iVec, AES_BLOCK_SIZE);
205 AES_Cipher(ctx, ciphertext, plaintext, len);
207 AES_Cipher(ctx, plaintext, ciphertext, len);
212 /*-----------------------------------------------*/
213 char *t_tag[2] = {"PLAINTEXT", "CIPHERTEXT"};
214 char *t_mode[6] = {"CBC","ECB","OFB","CFB1","CFB8","CFB128"};
215 enum Mode {CBC, ECB, OFB, CFB1, CFB8, CFB128};
216 enum XCrypt {XDECRYPT, XENCRYPT};
218 /*=============================*/
219 /* Monte Carlo Tests */
220 /*-----------------------------*/
222 /*#define gb(a,b) (((a)[(b)/8] >> ((b)%8))&1)*/
223 /*#define sb(a,b,v) ((a)[(b)/8]=((a)[(b)/8]&~(1 << ((b)%8)))|(!!(v) << ((b)%8)))*/
225 #define gb(a,b) (((a)[(b)/8] >> (7-(b)%8))&1)
226 #define sb(a,b,v) ((a)[(b)/8]=((a)[(b)/8]&~(1 << (7-(b)%8)))|(!!(v) << (7-(b)%8)))
228 int do_mct(char *amode,
229 int akeysz, unsigned char *aKey,unsigned char *iVec,
230 int dir, unsigned char *text, int len,
234 unsigned char key[101][32];
235 unsigned char iv[101][AES_BLOCK_SIZE];
236 unsigned char ptext[1001][32];
237 unsigned char ctext[1001][32];
238 unsigned char ciphertext[64+4];
240 int imode = 0, nkeysz = akeysz/8;
245 printf("\n>>>> Length exceeds 32 for %s %d <<<<\n\n",
249 for (imode = 0; imode < 6; ++imode)
250 if (strcmp(amode, t_mode[imode]) == 0)
254 printf("Unrecognized mode: %s\n", amode);
258 memcpy(key[0], aKey, nkeysz);
260 memcpy(iv[0], iVec, AES_BLOCK_SIZE);
262 memcpy(ptext[0], text, len);
264 memcpy(ctext[0], text, len);
265 for (i = 0; i < 100; ++i)
267 /* printf("Iteration %d\n", i); */
270 fprintf(rfp,"COUNT = %d\n",i);
271 OutputValue("KEY",key[i],nkeysz,rfp,0);
272 if (imode != ECB) /* ECB */
273 OutputValue("IV",iv[i],AES_BLOCK_SIZE,rfp,0);
274 /* Output Ciphertext | Plaintext */
275 OutputValue(t_tag[dir^1],dir ? ptext[0] : ctext[0],len,rfp,
278 for (j = 0; j < 1000; ++j)
284 { /* set up encryption */
285 ret = AESTest(&ctx, amode, akeysz, key[i], NULL,
286 dir, /* 0 = decrypt, 1 = encrypt */
287 ptext[j], ctext[j], len);
289 memcpy(ptext[j+1], ctext[j], len);
291 memcpy(ctext[j+1], ptext[j], len);
297 AES_Cipher(&ctx, ctext[j], ptext[j], len);
298 memcpy(ptext[j+1], ctext[j], len);
302 AES_Cipher(&ctx, ptext[j], ctext[j], len);
303 memcpy(ctext[j+1], ptext[j], len);
313 ret = AESTest(&ctx, amode, akeysz, key[i], iv[i],
314 dir, /* 0 = decrypt, 1 = encrypt */
315 ptext[j], ctext[j], len);
317 memcpy(ptext[j+1], iv[i], len);
319 memcpy(ctext[j+1], iv[i], len);
325 AES_Cipher(&ctx, ctext[j], ptext[j], len);
326 memcpy(ptext[j+1], ctext[j-1], len);
330 AES_Cipher(&ctx, ptext[j], ctext[j], len);
331 memcpy(ctext[j+1], ptext[j-1], len);
339 ret = AESTest(&ctx, amode, akeysz, key[i], iv[i],
340 dir, /* 0 = decrypt, 1 = encrypt */
341 ptext[j], ctext[j], len);
346 AES_Cipher(&ctx, ctext[j], ptext[j], len);
348 AES_Cipher(&ctx, ptext[j], ctext[j], len);
353 memcpy(ptext[j+1], &iv[i][j], len);
355 memcpy(ptext[j+1], ctext[j-16], len);
360 memcpy(ctext[j+1], &iv[i][j], len);
362 memcpy(ctext[j+1], ptext[j-16], len);
369 /* compensate for wrong endianness of input file */
372 ret=AESTest(&ctx,amode,akeysz,key[i],iv[i],dir,
373 ptext[j], ctext[j], len);
378 AES_Cipher(&ctx, ctext[j], ptext[j], len);
380 AES_Cipher(&ctx, ptext[j], ctext[j], len);
386 sb(ptext[j+1],0,gb(iv[i],j));
388 sb(ptext[j+1],0,gb(ctext[j-128],0));
393 sb(ctext[j+1],0,gb(iv[i],j));
395 sb(ctext[j+1],0,gb(ptext[j-128],0));
400 --j; /* reset to last of range */
401 /* Output Ciphertext | Plaintext */
402 OutputValue(t_tag[dir],dir ? ctext[j] : ptext[j],len,rfp,
404 fprintf(rfp, "\n"); /* add separator */
406 /* Compute next KEY */
410 { /* ct = CT[j-15] || CT[j-14] || ... || CT[j] */
411 for (n1 = 0, n2 = nkeysz-1; n1 < nkeysz; ++n1, --n2)
412 ciphertext[n1] = ctext[j-n2][0];
414 else if(imode == CFB1)
416 for(n1=0,n2=akeysz-1 ; n1 < akeysz ; ++n1,--n2)
417 sb(ciphertext,n1,gb(ctext[j-n2],0));
423 memcpy(ciphertext, ctext[j], 16);
426 memcpy(ciphertext, ctext[j-1]+8, 8);
427 memcpy(ciphertext+8, ctext[j], 16);
430 memcpy(ciphertext, ctext[j-1], 16);
431 memcpy(ciphertext+16, ctext[j], 16);
438 { /* ct = CT[j-15] || CT[j-14] || ... || CT[j] */
439 for (n1 = 0, n2 = nkeysz-1; n1 < nkeysz; ++n1, --n2)
440 ciphertext[n1] = ptext[j-n2][0];
442 else if(imode == CFB1)
444 for(n1=0,n2=akeysz-1 ; n1 < akeysz ; ++n1,--n2)
445 sb(ciphertext,n1,gb(ptext[j-n2],0));
451 memcpy(ciphertext, ptext[j], 16);
454 memcpy(ciphertext, ptext[j-1]+8, 8);
455 memcpy(ciphertext+8, ptext[j], 16);
458 memcpy(ciphertext, ptext[j-1], 16);
459 memcpy(ciphertext+16, ptext[j], 16);
463 /* Compute next key: Key[i+1] = Key[i] xor ct */
464 for (n = 0; n < nkeysz; ++n)
465 key[i+1][n] = key[i][n] ^ ciphertext[n];
467 /* Compute next IV and text */
473 memcpy(ptext[0], ctext[j], AES_BLOCK_SIZE);
478 memcpy(iv[i+1], ctext[j], AES_BLOCK_SIZE);
479 memcpy(ptext[0], ctext[j-1], AES_BLOCK_SIZE);
483 for (n1 = 0, n2 = 15; n1 < 16; ++n1, --n2)
484 iv[i+1][n1] = ctext[j-n2][0];
485 ptext[0][0] = ctext[j-16][0];
488 for(n1=0,n2=127 ; n1 < 128 ; ++n1,--n2)
489 sb(iv[i+1],n1,gb(ctext[j-n2],0));
490 ptext[0][0]=ctext[j-128][0]&0x80;
499 memcpy(ctext[0], ptext[j], AES_BLOCK_SIZE);
504 memcpy(iv[i+1], ptext[j], AES_BLOCK_SIZE);
505 memcpy(ctext[0], ptext[j-1], AES_BLOCK_SIZE);
508 for (n1 = 0, n2 = 15; n1 < 16; ++n1, --n2)
509 iv[i+1][n1] = ptext[j-n2][0];
510 ctext[0][0] = ptext[j-16][0];
513 for(n1=0,n2=127 ; n1 < 128 ; ++n1,--n2)
514 sb(iv[i+1],n1,gb(ptext[j-n2],0));
515 ctext[0][0]=ptext[j-128][0]&0x80;
524 /*================================================*/
525 /*----------------------------
526 # Config info for v-one
527 # AESVS MMT test data for ECB
528 # State : Encrypt and Decrypt
530 # Fri Aug 30 04:07:22 PM
531 ----------------------------*/
533 int proc_file(char *rqfile)
535 char afn[256], rfn[256];
536 FILE *afp = NULL, *rfp = NULL;
539 int ilen, len, ret = 0;
544 unsigned char iVec[20], aKey[40];
545 int dir = -1, err = 0, step = 0;
546 unsigned char plaintext[2048];
547 unsigned char ciphertext[2048];
551 if (!rqfile || !(*rqfile))
553 printf("No req file\n");
558 if ((afp = fopen(afn, "r")) == NULL)
560 printf("Cannot open file: %s, %s\n",
561 afn, strerror(errno));
565 rp=strstr(rfn,"req/");
566 #ifdef OPENSSL_SYS_WIN32
568 rp=strstr(rfn,"req\\");
572 rp = strstr(rfn, ".req");
573 memcpy(rp, ".rsp", 4);
574 if ((rfp = fopen(rfn, "w")) == NULL)
576 printf("Cannot open file: %s, %s\n",
577 rfn, strerror(errno));
582 while (!err && (fgets(ibuf, sizeof(ibuf), afp)) != NULL)
584 tidy_line(tbuf, ibuf);
586 /* printf("step=%d ibuf=%s",step,ibuf); */
589 case 0: /* read preamble */
591 { /* end of preamble */
592 if ((*algo == '\0') ||
596 printf("Missing Algorithm, Mode or KeySize (%s/%s/%d)\n",
606 else if (ibuf[0] != '#')
608 printf("Invalid preamble item: %s\n", ibuf);
612 { /* process preamble */
613 char *xp, *pp = ibuf+2;
616 { /* insert current time & date */
617 time_t rtim = time(0);
618 fprintf(rfp, "# %s", ctime(&rtim));
623 if (strncmp(pp, "AESVS ", 6) == 0)
628 xp = strchr(pp, ' ');
630 strncpy(atest, pp, n);
633 xp = strrchr(pp, ' '); /* get mode" */
635 strncpy(amode, xp+1, n);
637 /* amode[3] = '\0'; */
638 printf("Test = %s, Mode = %s\n", atest, amode);
640 else if (strncasecmp(pp, "Key Length : ", 13) == 0)
642 akeysz = atoi(pp+13);
643 printf("Key size = %d\n", akeysz);
649 case 1: /* [ENCRYPT] | [DECRYPT] */
654 if (strncasecmp(ibuf, "[ENCRYPT]", 9) == 0)
656 else if (strncasecmp(ibuf, "[DECRYPT]", 9) == 0)
660 printf("Invalid keyword: %s\n", ibuf);
668 printf("Missing ENCRYPT/DECRYPT keyword\n");
674 case 2: /* KEY = xxxx */
678 if(!strncasecmp(ibuf,"COUNT = ",8))
681 if (strncasecmp(ibuf, "KEY = ", 6) != 0)
683 printf("Missing KEY\n");
688 len = hex2bin((char*)ibuf+6, aKey);
691 printf("Invalid KEY\n");
695 PrintValue("KEY", aKey, len);
696 if (strcmp(amode, "ECB") == 0)
698 memset(iVec, 0, sizeof(iVec));
699 step = (dir)? 4: 5; /* no ivec for ECB */
706 case 3: /* IV = xxxx */
708 if (strncasecmp(ibuf, "IV = ", 5) != 0)
710 printf("Missing IV\n");
715 len = hex2bin((char*)ibuf+5, iVec);
718 printf("Invalid IV\n");
722 PrintValue("IV", iVec, len);
727 case 4: /* PLAINTEXT = xxxx */
729 if (strncasecmp(ibuf, "PLAINTEXT = ", 12) != 0)
731 printf("Missing PLAINTEXT\n");
736 int nn = strlen(ibuf+12);
737 if(!strcmp(amode,"CFB1"))
738 len=bint2bin(ibuf+12,nn-1,plaintext);
740 len=hex2bin(ibuf+12, plaintext);
743 printf("Invalid PLAINTEXT: %s", ibuf+12);
747 if (len >= sizeof(plaintext))
749 printf("Buffer overflow\n");
751 PrintValue("PLAINTEXT", (unsigned char*)plaintext, len);
752 if (strcmp(atest, "MCT") == 0) /* Monte Carlo Test */
754 if(do_mct(amode, akeysz, aKey, iVec,
755 dir, (unsigned char*)plaintext, len,
761 ret = AESTest(&ctx, amode, akeysz, aKey, iVec,
762 dir, /* 0 = decrypt, 1 = encrypt */
763 plaintext, ciphertext, len);
764 OutputValue("CIPHERTEXT",ciphertext,len,rfp,
765 !strcmp(amode,"CFB1"));
771 case 5: /* CIPHERTEXT = xxxx */
773 if (strncasecmp(ibuf, "CIPHERTEXT = ", 13) != 0)
775 printf("Missing KEY\n");
780 if(!strcmp(amode,"CFB1"))
781 len=bint2bin(ibuf+13,strlen(ibuf+13)-1,ciphertext);
783 len = hex2bin(ibuf+13,ciphertext);
786 printf("Invalid CIPHERTEXT\n");
791 PrintValue("CIPHERTEXT", ciphertext, len);
792 if (strcmp(atest, "MCT") == 0) /* Monte Carlo Test */
794 do_mct(amode, akeysz, aKey, iVec,
795 dir, ciphertext, len, rfp);
799 ret = AESTest(&ctx, amode, akeysz, aKey, iVec,
800 dir, /* 0 = decrypt, 1 = encrypt */
801 plaintext, ciphertext, len);
802 OutputValue("PLAINTEXT",(unsigned char *)plaintext,len,rfp,
803 !strcmp(amode,"CFB1"));
813 printf("Missing terminator\n");
815 else if (strcmp(atest, "MCT") != 0)
816 { /* MCT already added terminating nl */
830 /*--------------------------------------------------
831 Processes either a single file or
832 a set of files whose names are passed in a file.
833 A single file is specified as:
835 A set of files is specified as:
836 aes_test -d xxxxx.xxx
837 The default is: -d req.txt
838 --------------------------------------------------*/
839 int main(int argc, char **argv)
841 char *rqlist = "req.txt";
843 char fn[250] = "", rfn[256] = "";
844 int f_opt = 0, d_opt = 1;
847 if(!FIPS_mode_set(1))
855 if (strcasecmp(argv[1], "-d") == 0)
859 else if (strcasecmp(argv[1], "-f") == 0)
866 printf("Invalid parameter: %s\n", argv[1]);
871 printf("Missing parameter\n");
880 { /* list of files (directory) */
881 if (!(fp = fopen(rqlist, "r")))
883 printf("Cannot open req list file\n");
886 while (fgets(fn, sizeof(fn), fp))
890 printf("Processing: %s\n", rfn);
893 printf(">>> Processing failed for: %s <<<\n", rfn);
899 else /* single file */
901 printf("Processing: %s\n", fn);
904 printf(">>> Processing failed for: %s <<<\n", fn);