EVP_CIPHER_CTX *FIPS_cipher_ctx_new(void) { EVP_CIPHER_CTX *ctx=OPENSSL_malloc(sizeof *ctx); if (ctx) FIPS_cipher_ctx_init(ctx); return ctx; }
static void gcmtest(FILE *in, FILE *out, int encrypt) { char *keyword, *value; int keylen = -1, ivlen = -1, aadlen = -1, taglen = -1, ptlen = -1; int rv; long l; unsigned char *key = NULL, *iv = NULL, *aad = NULL, *tag = NULL; unsigned char *ct = NULL, *pt = NULL; EVP_CIPHER_CTX ctx; const EVP_CIPHER *gcm = NULL; FIPS_cipher_ctx_init(&ctx); while(fgets(buf,sizeof buf,in) != NULL) { fputs(buf,out); if (!parse_line(&keyword, &value, lbuf, buf)) continue; if(!strcmp(keyword,"[Keylen")) { keylen = atoi(value); if (keylen == 128) gcm = EVP_aes_128_gcm(); else if (keylen == 192) gcm = EVP_aes_192_gcm(); else if (keylen == 256) gcm = EVP_aes_256_gcm(); else { fprintf(stderr, "Unsupported keylen %d\n", keylen); } keylen >>= 3; } else if (!strcmp(keyword, "[IVlen"))
static int FIPS_aes_gcm_test(void) { int ret = 0; unsigned char pltmp[16]; unsigned char citmp[16]; unsigned char tagtmp[16]; unsigned char key[16] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}; unsigned char iv[16] = {21,22,23,24,25,26,27,28,29,30,31,32}; unsigned char aad[] = "Some text AAD"; unsigned char plaintext[16] = "etaonrishdlcu"; EVP_CIPHER_CTX ctx; FIPS_cipher_ctx_init(&ctx); if (FIPS_cipherinit(&ctx, EVP_aes_128_gcm(), key, iv, 1) <= 0) goto err; FIPS_cipher(&ctx, NULL, aad, sizeof(aad)); FIPS_cipher(&ctx, citmp, plaintext, 16); FIPS_cipher(&ctx, NULL, NULL, 0); if (!FIPS_cipher_ctx_ctrl(&ctx, EVP_CTRL_GCM_GET_TAG, 16, tagtmp)) goto err; if (FIPS_cipherinit(&ctx, EVP_aes_128_gcm(), key, iv, 0) <= 0) goto err; if (!FIPS_cipher_ctx_ctrl(&ctx, EVP_CTRL_GCM_SET_TAG, 16, tagtmp)) goto err; FIPS_cipher(&ctx, NULL, aad, sizeof(aad)); FIPS_cipher(&ctx, pltmp, citmp, 16); if (FIPS_cipher(&ctx, NULL, NULL, 0) < 0) goto err; if (memcmp(pltmp, plaintext, 16)) goto err; ret = 1; err: FIPS_cipher_ctx_cleanup(&ctx); return ret; }
int FIPS_selftest_des() { int n, ret = 0; EVP_CIPHER_CTX ctx; FIPS_cipher_ctx_init(&ctx); /* Encrypt/decrypt with 3DES and compare to known answers */ for(n=0 ; n < 2 ; ++n) { if (!fips_cipher_test(FIPS_TEST_CIPHER, &ctx, EVP_des_ede3_ecb(), tests3[n].key, NULL, tests3[n].plaintext, tests3[n].ciphertext, 8)) goto err; } ret = 1; err: FIPS_cipher_ctx_cleanup(&ctx); if (ret == 0) FIPSerr(FIPS_F_FIPS_SELFTEST_DES,FIPS_R_SELFTEST_FAILED); return ret; }
/* AES: encrypt and decrypt known plaintext, verify result matches original plaintext */ static int FIPS_aes_test(void) { int ret = 0; unsigned char pltmp[16]; unsigned char citmp[16]; unsigned char key[16] = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}; unsigned char plaintext[16] = "etaonrishdlcu"; EVP_CIPHER_CTX ctx; FIPS_cipher_ctx_init(&ctx); if (FIPS_cipherinit(&ctx, EVP_aes_128_ecb(), key, NULL, 1) <= 0) goto err; FIPS_cipher(&ctx, citmp, plaintext, 16); if (FIPS_cipherinit(&ctx, EVP_aes_128_ecb(), key, NULL, 0) <= 0) goto err; FIPS_cipher(&ctx, pltmp, citmp, 16); if (memcmp(pltmp, plaintext, 16)) goto err; ret = 1; err: FIPS_cipher_ctx_cleanup(&ctx); return ret; }
static int FIPS_des3_test(void) { int ret = 0; unsigned char pltmp[8]; unsigned char citmp[8]; unsigned char key[] = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18, 19,20,21,22,23,24 }; unsigned char plaintext[] = { 'e', 't', 'a', 'o', 'n', 'r', 'i', 's' }; EVP_CIPHER_CTX ctx; FIPS_cipher_ctx_init(&ctx); if (FIPS_cipherinit(&ctx, EVP_des_ede3_ecb(), key, NULL, 1) <= 0) goto err; FIPS_cipher(&ctx, citmp, plaintext, 8); if (FIPS_cipherinit(&ctx, EVP_des_ede3_ecb(), key, NULL, 0) <= 0) goto err; FIPS_cipher(&ctx, pltmp, citmp, 8); if (memcmp(pltmp, plaintext, 8)) goto err; ret = 1; err: FIPS_cipher_ctx_cleanup(&ctx); return ret; }
static 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; FIPS_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 >= (int)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; }
static int do_mct(char *amode, int akeysz, unsigned char *aKey,unsigned char *iVec, int dir, unsigned char *text, int len, FILE *rfp) { int ret = 0; unsigned char key[101][32]; unsigned char iv[101][AES_BLOCK_SIZE]; unsigned char ptext[1001][32]; unsigned char ctext[1001][32]; unsigned char ciphertext[64+4]; int i, j, n, n1, n2; int imode = 0, nkeysz = akeysz/8; EVP_CIPHER_CTX ctx; FIPS_cipher_ctx_init(&ctx); if (len > 32) { printf("\n>>>> Length exceeds 32 for %s %d <<<<\n\n", amode, akeysz); return -1; } for (imode = 0; imode < 6; ++imode) if (strcmp(amode, t_mode[imode]) == 0) break; if (imode == 6) { printf("Unrecognized mode: %s\n", amode); return -1; } memcpy(key[0], aKey, nkeysz); if (iVec) memcpy(iv[0], iVec, AES_BLOCK_SIZE); if (dir == XENCRYPT) memcpy(ptext[0], text, len); else memcpy(ctext[0], text, len); for (i = 0; i < 100; ++i) { /* printf("Iteration %d\n", i); */ if (i > 0) { fprintf(rfp,"COUNT = %d\n",i); OutputValue("KEY",key[i],nkeysz,rfp,0); if (imode != ECB) /* ECB */ OutputValue("IV",iv[i],AES_BLOCK_SIZE,rfp,0); /* Output Ciphertext | Plaintext */ OutputValue(t_tag[dir^1],dir ? ptext[0] : ctext[0],len,rfp, imode == CFB1); } for (j = 0; j < 1000; ++j) { switch (imode) { case ECB: if (j == 0) { /* set up encryption */ ret = AESTest(&ctx, amode, akeysz, key[i], NULL, dir, /* 0 = decrypt, 1 = encrypt */ ptext[j], ctext[j], len); if (dir == XENCRYPT) memcpy(ptext[j+1], ctext[j], len); else memcpy(ctext[j+1], ptext[j], len); } else { if (dir == XENCRYPT) { FIPS_cipher(&ctx, ctext[j], ptext[j], len); memcpy(ptext[j+1], ctext[j], len); } else { FIPS_cipher(&ctx, ptext[j], ctext[j], len); memcpy(ctext[j+1], ptext[j], len); } } break; case CBC: case OFB: case CFB128: if (j == 0) { ret = AESTest(&ctx, amode, akeysz, key[i], iv[i], dir, /* 0 = decrypt, 1 = encrypt */ ptext[j], ctext[j], len); if (dir == XENCRYPT) memcpy(ptext[j+1], iv[i], len); else memcpy(ctext[j+1], iv[i], len); } else { if (dir == XENCRYPT) { FIPS_cipher(&ctx, ctext[j], ptext[j], len); memcpy(ptext[j+1], ctext[j-1], len); } else { FIPS_cipher(&ctx, ptext[j], ctext[j], len); memcpy(ctext[j+1], ptext[j-1], len); } } break; case CFB8: if (j == 0) { ret = AESTest(&ctx, amode, akeysz, key[i], iv[i], dir, /* 0 = decrypt, 1 = encrypt */ ptext[j], ctext[j], len); } else { if (dir == XENCRYPT) FIPS_cipher(&ctx, ctext[j], ptext[j], len); else FIPS_cipher(&ctx, ptext[j], ctext[j], len); } if (dir == XENCRYPT) { if (j < 16) memcpy(ptext[j+1], &iv[i][j], len); else memcpy(ptext[j+1], ctext[j-16], len); } else { if (j < 16) memcpy(ctext[j+1], &iv[i][j], len); else memcpy(ctext[j+1], ptext[j-16], len); } break; case CFB1: if(j == 0) { #if 0 /* compensate for wrong endianness of input file */ if(i == 0) ptext[0][0]<<=7; #endif ret = AESTest(&ctx,amode,akeysz,key[i],iv[i],dir, ptext[j], ctext[j], len); } else { if (dir == XENCRYPT) FIPS_cipher(&ctx, ctext[j], ptext[j], len); else FIPS_cipher(&ctx, ptext[j], ctext[j], len); } if(dir == XENCRYPT) { if(j < 128) sb(ptext[j+1],0,gb(iv[i],j)); else sb(ptext[j+1],0,gb(ctext[j-128],0)); } else { if(j < 128) sb(ctext[j+1],0,gb(iv[i],j)); else sb(ctext[j+1],0,gb(ptext[j-128],0)); } break; } } --j; /* reset to last of range */ /* Output Ciphertext | Plaintext */ OutputValue(t_tag[dir],dir ? ctext[j] : ptext[j],len,rfp, imode == CFB1); fprintf(rfp, "\n"); /* add separator */ /* Compute next KEY */ if (dir == XENCRYPT) { if (imode == CFB8) { /* ct = CT[j-15] || CT[j-14] || ... || CT[j] */ for (n1 = 0, n2 = nkeysz-1; n1 < nkeysz; ++n1, --n2) ciphertext[n1] = ctext[j-n2][0]; } else if(imode == CFB1) { for(n1=0,n2=akeysz-1 ; n1 < akeysz ; ++n1,--n2) sb(ciphertext,n1,gb(ctext[j-n2],0)); } else switch (akeysz) { case 128: memcpy(ciphertext, ctext[j], 16); break; case 192: memcpy(ciphertext, ctext[j-1]+8, 8); memcpy(ciphertext+8, ctext[j], 16); break; case 256: memcpy(ciphertext, ctext[j-1], 16); memcpy(ciphertext+16, ctext[j], 16); break; } } else { if (imode == CFB8) { /* ct = CT[j-15] || CT[j-14] || ... || CT[j] */ for (n1 = 0, n2 = nkeysz-1; n1 < nkeysz; ++n1, --n2) ciphertext[n1] = ptext[j-n2][0]; } else if(imode == CFB1) { for(n1=0,n2=akeysz-1 ; n1 < akeysz ; ++n1,--n2) sb(ciphertext,n1,gb(ptext[j-n2],0)); } else switch (akeysz) { case 128: memcpy(ciphertext, ptext[j], 16); break; case 192: memcpy(ciphertext, ptext[j-1]+8, 8); 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; }