Add a test for aes256gcm_decrypt with long inputs, make it pass

This commit is contained in:
Frank Denis 2022-12-11 23:44:50 +01:00
parent dab102d404
commit 8b9a98c47f

View File

@ -3252,11 +3252,54 @@ tv(void)
return 0;
}
static int
tv2(void)
{
unsigned char *ciphertext;
unsigned char *message;
unsigned char *message2;
unsigned char *nonce;
unsigned char *key;
size_t message_len;
size_t ciphertext_len;
int i;
for (i = 0; i < 250; i++) {
message_len = randombytes_uniform(1000);
ciphertext_len = message_len + crypto_aead_aes256gcm_ABYTES;
message = (unsigned char *) sodium_malloc(message_len);
message2 = (unsigned char *) sodium_malloc(message_len);
ciphertext = (unsigned char *) sodium_malloc(ciphertext_len);
nonce = (unsigned char *) sodium_malloc(crypto_aead_aes256gcm_NPUBBYTES);
key = (unsigned char *) sodium_malloc(crypto_aead_aes256gcm_KEYBYTES);
crypto_aead_aes256gcm_keygen(key);
randombytes_buf(nonce, crypto_aead_aes256gcm_NPUBBYTES);
randombytes_buf(message, message_len);
crypto_aead_aes256gcm_encrypt(ciphertext, NULL, message, message_len,
NULL, 0, NULL, nonce, key);
if (crypto_aead_aes256gcm_decrypt(message2, NULL, NULL,
ciphertext, ciphertext_len,
NULL, 0, nonce, key) != 0) {
printf("Decryption of random ciphertext failed");
}
assert(memcmp(message, message2, message_len) == 0);
sodium_free(key);
sodium_free(nonce);
sodium_free(ciphertext);
sodium_free(message2);
sodium_free(message);
}
return 0;
}
int
main(void)
{
if (crypto_aead_aes256gcm_is_available()) {
tv();
tv2();
}
assert(crypto_aead_aes256gcm_keybytes() == crypto_aead_aes256gcm_KEYBYTES);
assert(crypto_aead_aes256gcm_nsecbytes() == crypto_aead_aes256gcm_NSECBYTES);