diff --git a/src/libsodium/crypto_aead/aes256gcm/aesni/aead_aes256gcm_aesni.c b/src/libsodium/crypto_aead/aes256gcm/aesni/aead_aes256gcm_aesni.c index babf4f4b..842d0649 100644 --- a/src/libsodium/crypto_aead/aes256gcm/aesni/aead_aes256gcm_aesni.c +++ b/src/libsodium/crypto_aead/aes256gcm/aesni/aead_aes256gcm_aesni.c @@ -648,8 +648,7 @@ crypto_aead_aes256gcm_encrypt_afternm(unsigned char *c, unsigned long long *clen } int -crypto_aead_aes256gcm_decrypt_detached_afternm(unsigned char *m, unsigned long long *mlen_p, - unsigned char *nsec, +crypto_aead_aes256gcm_decrypt_detached_afternm(unsigned char *m, unsigned char *nsec, const unsigned char *c, unsigned long long clen, const unsigned char *mac, const unsigned char *ad, unsigned long long adlen, @@ -674,9 +673,6 @@ crypto_aead_aes256gcm_decrypt_detached_afternm(unsigned char *m, unsigned long l if (clen > 16ULL * (1ULL << 32)) { abort(); /* LCOV_EXCL_LINE */ } - if (mlen_p != NULL) { - *mlen_p = 0U; - } mlen = clen; memcpy(&n2[0], npub, 3 * 4); @@ -799,9 +795,6 @@ crypto_aead_aes256gcm_decrypt_detached_afternm(unsigned char *m, unsigned long l LOOPDRND128; LOOPDRMD128; - if (mlen_p != NULL) { - *mlen_p = mlen; - } return 0; } @@ -813,14 +806,22 @@ crypto_aead_aes256gcm_decrypt_afternm(unsigned char *m, unsigned long long *mlen const unsigned char *npub, const crypto_aead_aes256gcm_state *ctx_) { - if (clen < 16) { - return -1; + unsigned long long mlen = 0ULL; + int ret = -1; + + if (clen >= crypto_aead_aes256gcm_ABYTES) { + ret = crypto_aead_aes256gcm_decrypt_detached_afternm + (m, nsec, c, clen - crypto_aead_aes256gcm_ABYTES, + c + clen - crypto_aead_aes256gcm_ABYTES, + ad, adlen, npub, ctx_); } - return crypto_aead_aes256gcm_decrypt_detached_afternm(m, mlen_p, nsec, - c, clen - crypto_aead_aes256gcm_ABYTES, - c + clen - crypto_aead_aes256gcm_ABYTES, - ad, adlen, - npub, ctx_); + if (mlen_p != NULL) { + if (ret == 0) { + mlen = clen - crypto_aead_aes256gcm_ABYTES; + } + *mlen_p = mlen; + } + return ret; } int @@ -866,7 +867,6 @@ crypto_aead_aes256gcm_encrypt(unsigned char *c, int crypto_aead_aes256gcm_decrypt_detached(unsigned char *m, - unsigned long long *mlen_p, unsigned char *nsec, const unsigned char *c, unsigned long long clen, @@ -881,7 +881,7 @@ crypto_aead_aes256gcm_decrypt_detached(unsigned char *m, crypto_aead_aes256gcm_beforenm(&ctx, k); return crypto_aead_aes256gcm_decrypt_detached_afternm - (m, mlen_p, nsec, c, clen, mac, ad, adlen, npub, + (m, nsec, c, clen, mac, ad, adlen, npub, (const crypto_aead_aes256gcm_state *) &ctx); } @@ -902,7 +902,7 @@ crypto_aead_aes256gcm_decrypt(unsigned char *m, return crypto_aead_aes256gcm_decrypt_afternm (m, mlen_p, nsec, c, clen, ad, adlen, npub, - (const crypto_aead_aes256gcm_state *) &ctx); + (const crypto_aead_aes256gcm_state *) &ctx); } int @@ -942,7 +942,6 @@ crypto_aead_aes256gcm_encrypt(unsigned char *c, unsigned long long *clen_p, int crypto_aead_aes256gcm_decrypt_detached(unsigned char *m, - unsigned long long *mlen_p, unsigned char *nsec, const unsigned char *c, unsigned long long clen, @@ -1000,8 +999,7 @@ crypto_aead_aes256gcm_encrypt_afternm(unsigned char *c, unsigned long long *clen } int -crypto_aead_aes256gcm_decrypt_detached_afternm(unsigned char *m, unsigned long long *mlen_p, - unsigned char *nsec, +crypto_aead_aes256gcm_decrypt_detached_afternm(unsigned char *m, unsigned char *nsec, const unsigned char *c, unsigned long long clen, const unsigned char *mac, const unsigned char *ad, unsigned long long adlen, diff --git a/src/libsodium/crypto_aead/chacha20poly1305/sodium/aead_chacha20poly1305.c b/src/libsodium/crypto_aead/chacha20poly1305/sodium/aead_chacha20poly1305.c index 215e80de..0318bfb2 100644 --- a/src/libsodium/crypto_aead/chacha20poly1305/sodium/aead_chacha20poly1305.c +++ b/src/libsodium/crypto_aead/chacha20poly1305/sodium/aead_chacha20poly1305.c @@ -178,7 +178,6 @@ crypto_aead_chacha20poly1305_ietf_encrypt(unsigned char *c, int crypto_aead_chacha20poly1305_decrypt_detached(unsigned char *m, - unsigned long long *mlen_p, unsigned char *nsec, const unsigned char *c, unsigned long long clen, @@ -196,9 +195,6 @@ crypto_aead_chacha20poly1305_decrypt_detached(unsigned char *m, int ret; (void) nsec; - if (mlen_p != NULL) { - *mlen_p = 0ULL; - } crypto_stream_chacha20(block0, sizeof block0, npub, k); crypto_onetimeauth_poly1305_init(&state, block0); sodium_memzero(block0, sizeof block0); @@ -223,9 +219,7 @@ crypto_aead_chacha20poly1305_decrypt_detached(unsigned char *m, return -1; } crypto_stream_chacha20_xor_ic(m, c, mlen, npub, 1U, k); - if (mlen_p != NULL) { - *mlen_p = mlen; - } + return 0; } @@ -240,20 +234,27 @@ crypto_aead_chacha20poly1305_decrypt(unsigned char *m, const unsigned char *npub, const unsigned char *k) { - if (clen < crypto_aead_chacha20poly1305_ABYTES) { - return -1; + unsigned long long mlen = 0ULL; + int ret = -1; + + if (clen >= crypto_aead_chacha20poly1305_ABYTES) { + ret = crypto_aead_chacha20poly1305_decrypt_detached + (m, nsec, + c, clen - crypto_aead_chacha20poly1305_ABYTES, + c + clen - crypto_aead_chacha20poly1305_ABYTES, + ad, adlen, npub, k); } - return crypto_aead_chacha20poly1305_decrypt_detached - (m, mlen_p, nsec, - c, clen - crypto_aead_chacha20poly1305_ABYTES, - c + clen - crypto_aead_chacha20poly1305_ABYTES, - ad, adlen, - npub, k); + if (mlen_p != NULL) { + if (ret == 0) { + mlen = clen - crypto_aead_chacha20poly1305_ABYTES; + } + *mlen_p = mlen; + } + return ret; } int crypto_aead_chacha20poly1305_ietf_decrypt_detached(unsigned char *m, - unsigned long long *mlen_p, unsigned char *nsec, const unsigned char *c, unsigned long long clen, @@ -271,9 +272,6 @@ crypto_aead_chacha20poly1305_ietf_decrypt_detached(unsigned char *m, int ret; (void) nsec; - if (mlen_p != NULL) { - *mlen_p = 0ULL; - } crypto_stream_chacha20_ietf(block0, sizeof block0, npub, k); crypto_onetimeauth_poly1305_init(&state, block0); sodium_memzero(block0, sizeof block0); @@ -302,9 +300,7 @@ crypto_aead_chacha20poly1305_ietf_decrypt_detached(unsigned char *m, return -1; } crypto_stream_chacha20_ietf_xor_ic(m, c, mlen, npub, 1U, k); - if (mlen_p != NULL) { - *mlen_p = mlen; - } + return 0; } @@ -319,15 +315,23 @@ crypto_aead_chacha20poly1305_ietf_decrypt(unsigned char *m, const unsigned char *npub, const unsigned char *k) { - if (clen < crypto_aead_chacha20poly1305_ietf_ABYTES) { - return -1; + unsigned long long mlen = 0ULL; + int ret = -1; + + if (clen >= crypto_aead_chacha20poly1305_ietf_ABYTES) { + ret = crypto_aead_chacha20poly1305_ietf_decrypt_detached + (m, nsec, + c, clen - crypto_aead_chacha20poly1305_ietf_ABYTES, + c + clen - crypto_aead_chacha20poly1305_ietf_ABYTES, + ad, adlen, npub, k); } - return crypto_aead_chacha20poly1305_ietf_decrypt_detached - (m, mlen_p, nsec, - c, clen - crypto_aead_chacha20poly1305_ietf_ABYTES, - c + clen - crypto_aead_chacha20poly1305_ietf_ABYTES, - ad, adlen, - npub, k); + if (mlen_p != NULL) { + if (ret == 0) { + mlen = clen - crypto_aead_chacha20poly1305_ietf_ABYTES; + } + *mlen_p = mlen; + } + return ret; } size_t diff --git a/src/libsodium/include/sodium/crypto_aead_aes256gcm.h b/src/libsodium/include/sodium/crypto_aead_aes256gcm.h index 6651c3c2..d7be4848 100644 --- a/src/libsodium/include/sodium/crypto_aead_aes256gcm.h +++ b/src/libsodium/include/sodium/crypto_aead_aes256gcm.h @@ -71,7 +71,6 @@ int crypto_aead_aes256gcm_encrypt_detached(unsigned char *c, SODIUM_EXPORT int crypto_aead_aes256gcm_decrypt_detached(unsigned char *m, - unsigned long long *mlen_p, unsigned char *nsec, const unsigned char *c, unsigned long long clen, @@ -125,7 +124,6 @@ int crypto_aead_aes256gcm_encrypt_detached_afternm(unsigned char *c, SODIUM_EXPORT int crypto_aead_aes256gcm_decrypt_detached_afternm(unsigned char *m, - unsigned long long *mlen_p, unsigned char *nsec, const unsigned char *c, unsigned long long clen, diff --git a/src/libsodium/include/sodium/crypto_aead_chacha20poly1305.h b/src/libsodium/include/sodium/crypto_aead_chacha20poly1305.h index 48378ba6..8ee5e42d 100644 --- a/src/libsodium/include/sodium/crypto_aead_chacha20poly1305.h +++ b/src/libsodium/include/sodium/crypto_aead_chacha20poly1305.h @@ -67,7 +67,6 @@ int crypto_aead_chacha20poly1305_ietf_encrypt_detached(unsigned char *c, SODIUM_EXPORT int crypto_aead_chacha20poly1305_ietf_decrypt_detached(unsigned char *m, - unsigned long long *mlen_p, unsigned char *nsec, const unsigned char *c, unsigned long long clen, @@ -133,7 +132,6 @@ int crypto_aead_chacha20poly1305_encrypt_detached(unsigned char *c, SODIUM_EXPORT int crypto_aead_chacha20poly1305_decrypt_detached(unsigned char *m, - unsigned long long *mlen_p, unsigned char *nsec, const unsigned char *c, unsigned long long clen, diff --git a/test/default/aead_aes256gcm.c b/test/default/aead_aes256gcm.c index 07fadf47..cd6760b5 100644 --- a/test/default/aead_aes256gcm.c +++ b/test/default/aead_aes256gcm.c @@ -3167,6 +3167,7 @@ tv(void) } decrypted = (unsigned char *) sodium_malloc(message_len); + found_message_len = 1; if (crypto_aead_aes256gcm_decrypt(decrypted, &found_message_len, NULL, ciphertext, randombytes_uniform(ciphertext_len), @@ -3174,6 +3175,9 @@ tv(void) printf("Verification of test vector #%u after truncation succeeded\n", (unsigned int) i); } + if (found_message_len != 0) { + printf("Message length should have been set to zero after a failure\n"); + } if (crypto_aead_aes256gcm_decrypt(decrypted, &found_message_len, NULL, ciphertext, randombytes_uniform(crypto_aead_aes256gcm_ABYTES), @@ -3190,15 +3194,13 @@ 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, + if (crypto_aead_aes256gcm_decrypt_detached(decrypted, 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); } diff --git a/test/default/aead_chacha20poly1305.c b/test/default/aead_chacha20poly1305.c index 568751b8..f8e14e11 100644 --- a/test/default/aead_chacha20poly1305.c +++ b/test/default/aead_chacha20poly1305.c @@ -66,15 +66,12 @@ tv(void) printf("m != m2\n"); } memset(m2, 0, m2len); - if (crypto_aead_chacha20poly1305_decrypt_detached(m2, &m2len, NULL, + if (crypto_aead_chacha20poly1305_decrypt_detached(m2, NULL, c, MLEN, mac, ad, ADLEN, nonce, firstkey) != 0) { printf("crypto_aead_chacha20poly1305_decrypt_detached() failed\n"); } - if (m2len != MLEN) { - printf("detached m2len is not properly set\n"); - } if (memcmp(m, m2, MLEN) != 0) { printf("detached m != m2\n"); } @@ -112,18 +109,25 @@ tv(void) if (memcmp(m, m2, MLEN) != 0) { printf("m != m2 (adlen=0)\n"); } - + m2len = 1; if (crypto_aead_chacha20poly1305_decrypt( m2, &m2len, NULL, c, crypto_aead_chacha20poly1305_ABYTES / 2, NULL, 0U, nonce, firstkey) != -1) { printf("crypto_aead_chacha20poly1305_decrypt() worked with a short " "ciphertext\n"); } + if (m2len != 0) { + printf("Message length should have been set to zero after a failure\n"); + } + m2len = 1; if (crypto_aead_chacha20poly1305_decrypt(m2, &m2len, NULL, c, 0U, NULL, 0U, nonce, firstkey) != -1) { printf("crypto_aead_chacha20poly1305_decrypt() worked with an empty " "ciphertext\n"); } + if (m2len != 0) { + printf("Message length should have been set to zero after a failure\n"); + } memcpy(c, m, MLEN); crypto_aead_chacha20poly1305_encrypt(c, &found_clen, c, MLEN, @@ -234,15 +238,12 @@ tv_ietf(void) printf("m != m2\n"); } memset(m2, 0, m2len); - if (crypto_aead_chacha20poly1305_ietf_decrypt_detached(m2, &m2len, NULL, + if (crypto_aead_chacha20poly1305_ietf_decrypt_detached(m2, NULL, c, MLEN, mac, ad, ADLEN, nonce, firstkey) != 0) { printf("crypto_aead_chacha20poly1305_ietf_decrypt_detached() failed\n"); } - if (m2len != MLEN) { - printf("detached m2len is not properly set\n"); - } if (memcmp(m, m2, MLEN) != 0) { printf("detached m != m2\n"); } @@ -278,18 +279,25 @@ tv_ietf(void) if (memcmp(m, m2, MLEN) != 0) { printf("m != m2 (adlen=0)\n"); } - + m2len = 1; if (crypto_aead_chacha20poly1305_ietf_decrypt( m2, &m2len, NULL, c, crypto_aead_chacha20poly1305_ietf_ABYTES / 2, NULL, 0U, nonce, firstkey) != -1) { printf("crypto_aead_chacha20poly1305_ietf_decrypt() worked with a short " "ciphertext\n"); } + if (m2len != 0) { + printf("Message length should have been set to zero after a failure\n"); + } + m2len = 1; if (crypto_aead_chacha20poly1305_ietf_decrypt(m2, &m2len, NULL, c, 0U, NULL, 0U, nonce, firstkey) != -1) { printf("crypto_aead_chacha20poly1305_ietf_decrypt() worked with an empty " "ciphertext\n"); } + if (m2len != 0) { + printf("Message length should have been set to zero after a failure\n"); + } memcpy(c, m, MLEN); crypto_aead_chacha20poly1305_ietf_encrypt(c, &found_clen, c, MLEN,