1
mirror of https://github.com/jedisct1/libsodium.git synced 2024-12-19 18:15:18 -07:00

Add tests for the detached aes256gcm API

This commit is contained in:
Frank Denis 2016-03-15 11:21:23 +01:00
parent e8dfc764d3
commit cf4f0c48dc

View File

@ -3082,20 +3082,25 @@ tv(void)
unsigned char *ad;
unsigned char *ciphertext;
unsigned char *decrypted;
unsigned char *detached_ciphertext;
unsigned char *expected_ciphertext;
unsigned char *key;
unsigned char *message;
unsigned char *mac;
unsigned char *nonce;
char *hex;
unsigned long long found_ciphertext_len;
unsigned long long found_mac_len;
unsigned long long found_message_len;
size_t ad_len;
size_t ciphertext_len;
size_t detached_ciphertext_len;
size_t i = 0U;
size_t message_len;
key = (unsigned char *) sodium_malloc(crypto_aead_aes256gcm_KEYBYTES);
nonce = (unsigned char *) sodium_malloc(crypto_aead_aes256gcm_NPUBBYTES);
mac = (unsigned char *) sodium_malloc(crypto_aead_aes256gcm_ABYTES);
do {
assert(strlen(tests[i].key_hex) == 2 * crypto_aead_aes256gcm_KEYBYTES);
@ -3117,6 +3122,7 @@ tv(void)
tests[i].ad_hex, strlen(tests[i].ad_hex),
NULL, NULL, NULL);
ciphertext_len = message_len + crypto_aead_aes256gcm_ABYTES;
detached_ciphertext_len = message_len;
expected_ciphertext = (unsigned char *) sodium_malloc(ciphertext_len);
assert(strlen(tests[i].ciphertext_hex) == 2 * message_len);
sodium_hex2bin(expected_ciphertext, message_len,
@ -3127,6 +3133,24 @@ tv(void)
tests[i].mac_hex, strlen(tests[i].mac_hex),
NULL, NULL, NULL);
ciphertext = (unsigned char *) sodium_malloc(ciphertext_len);
detached_ciphertext = (unsigned char *) sodium_malloc(detached_ciphertext_len);
crypto_aead_aes256gcm_encrypt_detached(detached_ciphertext, mac,
&found_mac_len,
message, message_len,
ad, ad_len, NULL, nonce, key);
assert(found_mac_len == crypto_aead_aes256gcm_ABYTES);
if (memcmp(detached_ciphertext, expected_ciphertext,
detached_ciphertext_len) != 0 ||
memcmp(mac, expected_ciphertext + message_len,
crypto_aead_aes256gcm_ABYTES) != 0) {
printf("Detached encryption of test vector #%u failed\n", (unsigned int) i);
hex = (char *) sodium_malloc((size_t) found_ciphertext_len * 2 + 1);
sodium_bin2hex(hex, (size_t) found_ciphertext_len * 2 + 1,
ciphertext, ciphertext_len);
printf("Computed: [%s]\n", hex);
sodium_free(hex);
}
crypto_aead_aes256gcm_encrypt(ciphertext, &found_ciphertext_len,
message, message_len,
@ -3141,6 +3165,7 @@ tv(void)
printf("Computed: [%s]\n", hex);
sodium_free(hex);
}
decrypted = (unsigned char *) sodium_malloc(message_len);
if (crypto_aead_aes256gcm_decrypt(decrypted, &found_message_len,
NULL, ciphertext,
@ -3165,14 +3190,29 @@ tv(void)
if (memcmp(decrypted, message, message_len) != 0) {
printf("Incorrect decryption of test vector #%u\n", (unsigned int) i);
}
memset(decrypted, 0xd0, message_len);
if (crypto_aead_aes256gcm_decrypt_detached(decrypted, &found_message_len,
NULL, detached_ciphertext,
detached_ciphertext_len,
mac, ad, ad_len, nonce, key) != 0) {
printf("Detached verification of test vector #%u failed\n", (unsigned int) i);
}
assert((size_t) found_message_len == message_len);
if (memcmp(decrypted, message, message_len) != 0) {
printf("Incorrect decryption of test vector #%u\n", (unsigned int) i);
}
sodium_free(message);
sodium_free(ad);
sodium_free(expected_ciphertext);
sodium_free(ciphertext);
sodium_free(decrypted);
sodium_free(detached_ciphertext);
} while (++i < (sizeof tests) / (sizeof tests[0]));
sodium_free(key);
sodium_free(mac);
sodium_free(nonce);
return 0;