I'm writing code to provide an AEAD utility. It uses AES-256-CBC for encryption and SHA-512(key+data) truncated to 256 bits as the MAC.
encrypt(
aes_key: byte[32],
mac_key: byte[32],
plain_text: byte[],
aad: byte[],
) {
iv = crypto_random_bytes(16)
cipher_text = aes_256_cbc(aes_key, iv, plain_text)
aad_encoded_length = int_to_little_endian_4_bytes(aad.length);
auth_state = sha_512(key + aad + iv + cipher_text + aad_encoded_length)
auth_tag = truncate_bytes(auth_state, 32)
return cipher_text + auth_tag
}
I'm primarily interested in how to order the data I'm passing to sha_512
. I based it off of this: https://www.ietf.org/archive/id/draft-mcgrew-aead-aes-cbc-hmac-sha2-05.txt
Is that the best ordering?
(P.S. Though not really part of this question, my decision to use AES-256-CBC and truncated SHA-512 was based on library availability, performance, and resistance to accidental nonce reuse.)