static void test_eax(void) { uint8_t cipher[2], tag[16]; /* * MSG: F7FB * KEY: 91945D3F4DCBEE0BF45EF52255F095A4 * NONCE: BECAF043B0A23D843194BA972C66DEBD * HEADER: FA3BFD4806EB53FA * CIPHER: 19DD5C4C9331049D0BDAB0277408F67967E5 */ const void *key = "\x91\x94\x5d\x3f\x4d\xcb\xee\x0b\xf4\x5e\xf5\x22\x55\xf0\x95\xa4"; const void *nonce = "\xbe\xca\xf0\x43\xb0\xa2\x3d\x84\x31\x94\xba\x97\x2c\x66\xde\xbd"; size_t nnonce = 16; const void *header = "\xfa\x3b\xfd\x48\x06\xeb\x53\xfa"; size_t nheader = 8; const void *msg = "\xf7\xfb"; size_t nmsg = 2; cf_aes_context aes; cf_aes_init(&aes, key, 16); cf_eax_encrypt(&cf_aes, &aes, msg, nmsg, header, nheader, nonce, nnonce, cipher, tag, sizeof tag); TEST_CHECK(memcmp("\x19\xdd", cipher, 2) == 0); TEST_CHECK(memcmp("\x5c\x4c\x93\x31\x04\x9d\x0b\xda\xb0\x27\x74\x08\xf6\x79\x67\xe5", tag, sizeof tag) == 0); int rc; uint8_t tmp[2]; rc = cf_eax_decrypt(&cf_aes, &aes, cipher, sizeof cipher, header, nheader, nonce, nnonce, tag, sizeof tag, tmp); TEST_CHECK(rc == 0); TEST_CHECK(memcmp(tmp, msg, nmsg) == 0); tag[0] ^= 0xff; rc = cf_eax_decrypt(&cf_aes, &aes, cipher, sizeof cipher, header, nheader, nonce, nnonce, tag, sizeof tag, tmp); TEST_CHECK(rc == 1); }
static void check_eax(const void *key, size_t nkey, const void *msg, size_t nmsg, const void *nonce, size_t nnonce, const void *header, size_t nheader, const void *expect_cipher, const void *expect_tag, size_t ntag) { uint8_t cipher[32]; uint8_t tag[16]; assert(nmsg <= sizeof cipher); assert(ntag <= ntag); cf_aes_context aes; cf_aes_init(&aes, key, nkey); cf_eax_encrypt(&cf_aes, &aes, msg, nmsg, header, nheader, nonce, nnonce, cipher, tag, ntag); TEST_CHECK(memcmp(expect_cipher, cipher, nmsg) == 0); TEST_CHECK(memcmp(expect_tag, tag, ntag) == 0); int rc; uint8_t tmp[sizeof cipher]; rc = cf_eax_decrypt(&cf_aes, &aes, cipher, nmsg, header, nheader, nonce, nnonce, tag, ntag, tmp); TEST_CHECK(rc == 0); TEST_CHECK(memcmp(tmp, msg, nmsg) == 0); tag[0] ^= 0xff; rc = cf_eax_decrypt(&cf_aes, &aes, cipher, nmsg, header, nheader, nonce, nnonce, tag, ntag, tmp); TEST_CHECK(rc == 1); }