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_cipher_test(int id, EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, const unsigned char *key, const unsigned char *iv, const unsigned char *plaintext, const unsigned char *ciphertext, int len) { unsigned char pltmp[FIPS_MAX_CIPHER_TEST_SIZE]; unsigned char citmp[FIPS_MAX_CIPHER_TEST_SIZE]; int subid = M_EVP_CIPHER_nid(cipher); int rv = 0; OPENSSL_assert(len <= FIPS_MAX_CIPHER_TEST_SIZE); memset(pltmp, 0, FIPS_MAX_CIPHER_TEST_SIZE); memset(citmp, 0, FIPS_MAX_CIPHER_TEST_SIZE); if (!fips_post_started(id, subid, NULL)) return 1; if (FIPS_cipherinit(ctx, cipher, key, iv, 1) <= 0) goto error; if (!FIPS_cipher(ctx, citmp, plaintext, len)) goto error; if (memcmp(citmp, ciphertext, len)) goto error; if (!fips_post_corrupt(id, subid, NULL)) citmp[0] ^= 0x1; if (FIPS_cipherinit(ctx, cipher, key, iv, 0) <= 0) goto error; FIPS_cipher(ctx, pltmp, citmp, len); if (memcmp(pltmp, plaintext, len)) goto error; rv = 1; error: if (rv == 0) { fips_post_failed(id, subid, NULL); return 0; } return fips_post_success(id, subid, NULL); }
/* 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 AESTest(EVP_CIPHER_CTX *ctx, char *amode, int akeysz, unsigned char *aKey, unsigned char *iVec, int dir, /* 0 = decrypt, 1 = encrypt */ unsigned char *plaintext, unsigned char *ciphertext, int len) { const EVP_CIPHER *cipher = NULL; if (strcasecmp(amode, "CBC") == 0) { switch (akeysz) { case 128: cipher = EVP_aes_128_cbc(); break; case 192: cipher = EVP_aes_192_cbc(); break; case 256: cipher = EVP_aes_256_cbc(); break; } } else if (strcasecmp(amode, "ECB") == 0) { switch (akeysz) { case 128: cipher = EVP_aes_128_ecb(); break; case 192: cipher = EVP_aes_192_ecb(); break; case 256: cipher = EVP_aes_256_ecb(); break; } } else if (strcasecmp(amode, "CFB128") == 0) { switch (akeysz) { case 128: cipher = EVP_aes_128_cfb128(); break; case 192: cipher = EVP_aes_192_cfb128(); break; case 256: cipher = EVP_aes_256_cfb128(); break; } } else if (strncasecmp(amode, "OFB", 3) == 0) { switch (akeysz) { case 128: cipher = EVP_aes_128_ofb(); break; case 192: cipher = EVP_aes_192_ofb(); break; case 256: cipher = EVP_aes_256_ofb(); break; } } else if(!strcasecmp(amode,"CFB1")) { switch (akeysz) { case 128: cipher = EVP_aes_128_cfb1(); break; case 192: cipher = EVP_aes_192_cfb1(); break; case 256: cipher = EVP_aes_256_cfb1(); break; } } else if(!strcasecmp(amode,"CFB8")) { switch (akeysz) { case 128: cipher = EVP_aes_128_cfb8(); break; case 192: cipher = EVP_aes_192_cfb8(); break; case 256: cipher = EVP_aes_256_cfb8(); break; } } else { printf("Unknown mode: %s\n", amode); return 0; } if (!cipher) { printf("Invalid key size: %d\n", akeysz); return 0; } if (FIPS_cipherinit(ctx, cipher, aKey, iVec, dir) <= 0) return 0; if(!strcasecmp(amode,"CFB1")) M_EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS); if (dir) FIPS_cipher(ctx, ciphertext, plaintext, len); else FIPS_cipher(ctx, plaintext, ciphertext, len); return 1; }