📄 fips_aesavs.c
字号:
memcpy(ciphertext+8, ptext[j], 16); break; case 256: memcpy(ciphertext, ptext[j-1], 16); memcpy(ciphertext+16, ptext[j], 16); break; } } /* Compute next key: Key[i+1] = Key[i] xor ct */ for (n = 0; n < nkeysz; ++n) key[i+1][n] = key[i][n] ^ ciphertext[n]; /* Compute next IV and text */ if (dir == XENCRYPT) { switch (imode) { case ECB: memcpy(ptext[0], ctext[j], AES_BLOCK_SIZE); break; case CBC: case OFB: case CFB128: memcpy(iv[i+1], ctext[j], AES_BLOCK_SIZE); memcpy(ptext[0], ctext[j-1], AES_BLOCK_SIZE); break; case CFB8: /* IV[i+1] = ct */ for (n1 = 0, n2 = 15; n1 < 16; ++n1, --n2) iv[i+1][n1] = ctext[j-n2][0]; ptext[0][0] = ctext[j-16][0]; break; case CFB1: for(n1=0,n2=127 ; n1 < 128 ; ++n1,--n2) sb(iv[i+1],n1,gb(ctext[j-n2],0)); ptext[0][0]=ctext[j-128][0]&0x80; break; } } else { switch (imode) { case ECB: memcpy(ctext[0], ptext[j], AES_BLOCK_SIZE); break; case CBC: case OFB: case CFB128: memcpy(iv[i+1], ptext[j], AES_BLOCK_SIZE); memcpy(ctext[0], ptext[j-1], AES_BLOCK_SIZE); break; case CFB8: for (n1 = 0, n2 = 15; n1 < 16; ++n1, --n2) iv[i+1][n1] = ptext[j-n2][0]; ctext[0][0] = ptext[j-16][0]; break; case CFB1: for(n1=0,n2=127 ; n1 < 128 ; ++n1,--n2) sb(iv[i+1],n1,gb(ptext[j-n2],0)); ctext[0][0]=ptext[j-128][0]&0x80; break; } } } return ret; }/*================================================*//*---------------------------- # Config info for v-one # AESVS MMT test data for ECB # State : Encrypt and Decrypt # Key Length : 256 # Fri Aug 30 04:07:22 PM ----------------------------*/int proc_file(char *rqfile, char *rspfile) { char afn[256], rfn[256]; FILE *afp = NULL, *rfp = NULL; char ibuf[2048]; char tbuf[2048]; int ilen, len, ret = 0; char algo[8] = ""; char amode[8] = ""; char atest[8] = ""; int akeysz = 0; unsigned char iVec[20], aKey[40]; int dir = -1, err = 0, step = 0; unsigned char plaintext[2048]; unsigned char ciphertext[2048]; char *rp; EVP_CIPHER_CTX ctx; EVP_CIPHER_CTX_init(&ctx); if (!rqfile || !(*rqfile)) { printf("No req file\n"); return -1; } strcpy(afn, rqfile); if ((afp = fopen(afn, "r")) == NULL) { printf("Cannot open file: %s, %s\n", afn, strerror(errno)); return -1; } if (!rspfile) { strcpy(rfn,afn); rp=strstr(rfn,"req/");#ifdef OPENSSL_SYS_WIN32 if (!rp) rp=strstr(rfn,"req\\");#endif assert(rp); memcpy(rp,"rsp",3); rp = strstr(rfn, ".req"); memcpy(rp, ".rsp", 4); rspfile = rfn; } if ((rfp = fopen(rspfile, "w")) == NULL) { printf("Cannot open file: %s, %s\n", rfn, strerror(errno)); fclose(afp); afp = NULL; return -1; } while (!err && (fgets(ibuf, sizeof(ibuf), afp)) != NULL) { tidy_line(tbuf, ibuf); ilen = strlen(ibuf); /* printf("step=%d ibuf=%s",step,ibuf); */ switch (step) { case 0: /* read preamble */ if (ibuf[0] == '\n') { /* end of preamble */ if ((*algo == '\0') || (*amode == '\0') || (akeysz == 0)) { printf("Missing Algorithm, Mode or KeySize (%s/%s/%d)\n", algo,amode,akeysz); err = 1; } else { fputs(ibuf, rfp); ++ step; } } else if (ibuf[0] != '#') { printf("Invalid preamble item: %s\n", ibuf); err = 1; } else { /* process preamble */ char *xp, *pp = ibuf+2; int n; if (akeysz) { /* insert current time & date */ time_t rtim = time(0); fprintf(rfp, "# %s", ctime(&rtim)); } else { fputs(ibuf, rfp); if (strncmp(pp, "AESVS ", 6) == 0) { strcpy(algo, "AES"); /* get test type */ pp += 6; xp = strchr(pp, ' '); n = xp-pp; strncpy(atest, pp, n); atest[n] = '\0'; /* get mode */ xp = strrchr(pp, ' '); /* get mode" */ n = strlen(xp+1)-1; strncpy(amode, xp+1, n); amode[n] = '\0'; /* amode[3] = '\0'; */ if (VERBOSE) printf("Test = %s, Mode = %s\n", atest, amode); } else if (strncasecmp(pp, "Key Length : ", 13) == 0) { akeysz = atoi(pp+13); if (VERBOSE) printf("Key size = %d\n", akeysz); } } } break; case 1: /* [ENCRYPT] | [DECRYPT] */ if (ibuf[0] == '[') { fputs(ibuf, rfp); ++step; if (strncasecmp(ibuf, "[ENCRYPT]", 9) == 0) dir = 1; else if (strncasecmp(ibuf, "[DECRYPT]", 9) == 0) dir = 0; else { printf("Invalid keyword: %s\n", ibuf); err = 1; } break; } else if (dir == -1) { err = 1; printf("Missing ENCRYPT/DECRYPT keyword\n"); break; } else step = 2; case 2: /* KEY = xxxx */ fputs(ibuf, rfp); if(*ibuf == '\n') break; if(!strncasecmp(ibuf,"COUNT = ",8)) break; if (strncasecmp(ibuf, "KEY = ", 6) != 0) { printf("Missing KEY\n"); err = 1; } else { len = hex2bin((char*)ibuf+6, aKey); if (len < 0) { printf("Invalid KEY\n"); err =1; break; } PrintValue("KEY", aKey, len); if (strcmp(amode, "ECB") == 0) { memset(iVec, 0, sizeof(iVec)); step = (dir)? 4: 5; /* no ivec for ECB */ } else ++step; } break; case 3: /* IV = xxxx */ fputs(ibuf, rfp); if (strncasecmp(ibuf, "IV = ", 5) != 0) { printf("Missing IV\n"); err = 1; } else { len = hex2bin((char*)ibuf+5, iVec); if (len < 0) { printf("Invalid IV\n"); err =1; break; } PrintValue("IV", iVec, len); step = (dir)? 4: 5; } break; case 4: /* PLAINTEXT = xxxx */ fputs(ibuf, rfp); if (strncasecmp(ibuf, "PLAINTEXT = ", 12) != 0) { printf("Missing PLAINTEXT\n"); err = 1; } else { int nn = strlen(ibuf+12); if(!strcmp(amode,"CFB1")) len=bint2bin(ibuf+12,nn-1,plaintext); else len=hex2bin(ibuf+12, plaintext); if (len < 0) { printf("Invalid PLAINTEXT: %s", ibuf+12); err =1; break; } if (len >= sizeof(plaintext)) { printf("Buffer overflow\n"); } PrintValue("PLAINTEXT", (unsigned char*)plaintext, len); if (strcmp(atest, "MCT") == 0) /* Monte Carlo Test */ { if(do_mct(amode, akeysz, aKey, iVec, dir, (unsigned char*)plaintext, len, rfp) < 0) EXIT(1); } else { ret = AESTest(&ctx, amode, akeysz, aKey, iVec, dir, /* 0 = decrypt, 1 = encrypt */ plaintext, ciphertext, len); OutputValue("CIPHERTEXT",ciphertext,len,rfp, !strcmp(amode,"CFB1")); } step = 6; } break; case 5: /* CIPHERTEXT = xxxx */ fputs(ibuf, rfp); if (strncasecmp(ibuf, "CIPHERTEXT = ", 13) != 0) { printf("Missing KEY\n"); err = 1; } else { if(!strcmp(amode,"CFB1")) len=bint2bin(ibuf+13,strlen(ibuf+13)-1,ciphertext); else len = hex2bin(ibuf+13,ciphertext); if (len < 0) { printf("Invalid CIPHERTEXT\n"); err =1; break; } PrintValue("CIPHERTEXT", ciphertext, len); if (strcmp(atest, "MCT") == 0) /* Monte Carlo Test */ { do_mct(amode, akeysz, aKey, iVec, dir, ciphertext, len, rfp); } else { ret = AESTest(&ctx, amode, akeysz, aKey, iVec, dir, /* 0 = decrypt, 1 = encrypt */ plaintext, ciphertext, len); OutputValue("PLAINTEXT",(unsigned char *)plaintext,len,rfp, !strcmp(amode,"CFB1")); } step = 6; } break; case 6: if (ibuf[0] != '\n') { err = 1; printf("Missing terminator\n"); } else if (strcmp(atest, "MCT") != 0) { /* MCT already added terminating nl */ fputs(ibuf, rfp); } step = 1; break; } } if (rfp) fclose(rfp); if (afp) fclose(afp); return err; }/*-------------------------------------------------- Processes either a single file or a set of files whose names are passed in a file. A single file is specified as: aes_test -f xxx.req A set of files is specified as: aes_test -d xxxxx.xxx The default is: -d req.txt--------------------------------------------------*/int main(int argc, char **argv) { char *rqlist = "req.txt", *rspfile = NULL; FILE *fp = NULL; char fn[250] = "", rfn[256] = ""; int f_opt = 0, d_opt = 1;#ifdef OPENSSL_FIPS if(!FIPS_mode_set(1)) { do_print_errors(); EXIT(1); }#endif if (argc > 1) { if (strcasecmp(argv[1], "-d") == 0) { d_opt = 1; } else if (strcasecmp(argv[1], "-f") == 0) { f_opt = 1; d_opt = 0; } else { printf("Invalid parameter: %s\n", argv[1]); return 0; } if (argc < 3) { printf("Missing parameter\n"); return 0; } if (d_opt) rqlist = argv[2]; else { strcpy(fn, argv[2]); rspfile = argv[3]; } } if (d_opt) { /* list of files (directory) */ if (!(fp = fopen(rqlist, "r"))) { printf("Cannot open req list file\n"); return -1; } while (fgets(fn, sizeof(fn), fp)) { strtok(fn, "\r\n"); strcpy(rfn, fn); if (VERBOSE) printf("Processing: %s\n", rfn); if (proc_file(rfn, rspfile)) { printf(">>> Processing failed for: %s <<<\n", rfn); EXIT(1); } } fclose(fp); } else /* single file */ { if (VERBOSE) printf("Processing: %s\n", fn); if (proc_file(fn, rspfile)) { printf(">>> Processing failed for: %s <<<\n", fn); } } EXIT(0); return 0; }#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -