Latest libsodium-1.0.18-stable.tar.gz.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4193 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
2023-02-19 23:23:53 +00:00
parent 86bc46a11e
commit 961109635b
44 changed files with 2426 additions and 1477 deletions

View File

@ -3081,6 +3081,7 @@ tv(void)
{
unsigned char *ad;
unsigned char *ciphertext;
unsigned char *ciphertext2;
unsigned char *decrypted;
unsigned char *detached_ciphertext;
unsigned char *expected_ciphertext;
@ -3210,6 +3211,32 @@ tv(void)
printf("Incorrect decryption of test vector #%u\n", (unsigned int) i);
}
ciphertext2 = (unsigned char *) sodium_malloc(ciphertext_len);
crypto_aead_aes256gcm_encrypt(ciphertext, &found_ciphertext_len, message,
message_len, ad, ad_len, NULL, nonce, key);
assert(found_ciphertext_len == ciphertext_len);
memcpy(ciphertext2, message, message_len);
crypto_aead_aes256gcm_encrypt(ciphertext2, &found_ciphertext_len,
ciphertext2, message_len, ad, ad_len, NULL,
nonce, key);
assert(found_ciphertext_len == ciphertext_len);
assert(memcmp(ciphertext, ciphertext2, ciphertext_len) == 0);
if (crypto_aead_aes256gcm_decrypt(ciphertext2, &found_message_len, NULL,
ciphertext2, ciphertext_len, ad, ad_len,
nonce, key) != 0) {
printf("In-place decryption of vector #%u failed\n", (unsigned int) i);
}
assert(found_message_len == message_len);
assert(memcmp(ciphertext2, message, message_len) == 0);
if (crypto_aead_aes256gcm_decrypt(message, &found_message_len, NULL,
ciphertext, ciphertext_len, ad, ad_len,
nonce, key) != 0) {
printf("Decryption of vector #%u failed\n", (unsigned int) i);
}
assert(found_message_len == message_len);
assert(memcmp(ciphertext2, message, message_len) == 0);
sodium_free(ciphertext2);
sodium_free(message);
sodium_free(ad);
sodium_free(expected_ciphertext);
@ -3225,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(message_len == 0 || 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);