From 0a590b07b23bc8d6c707bbcb2705ee0dd89fac3f Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Mon, 11 Apr 2016 18:33:50 +0200 Subject: [PATCH] Decryption functions can now accept a `NULL` pointer for the output This checks the MAC without writing the decrypted message. --- .../crypto_aead/aes256gcm/aesni/aead_aes256gcm_aesni.c | 7 ++++++- .../chacha20poly1305/sodium/aead_chacha20poly1305.c | 6 ++++++ src/libsodium/crypto_secretbox/crypto_secretbox_easy.c | 3 +++ 3 files changed, 15 insertions(+), 1 deletion(-) 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 d0ad674c..ca30da2f 100644 --- a/src/libsodium/crypto_aead/aes256gcm/aesni/aead_aes256gcm_aesni.c +++ b/src/libsodium/crypto_aead/aes256gcm/aesni/aead_aes256gcm_aesni.c @@ -787,9 +787,14 @@ crypto_aead_aes256gcm_decrypt_detached_afternm(unsigned char *m, unsigned char * d |= (mac[i] ^ (T[i] ^ accum[15 - i])); } if (d != 0) { - memset(m, 0, mlen); + if (m != NULL) { + memset(m, 0, mlen); + } return -1; } + if (m == NULL) { + return 0; + } } n2[3] = 0U; COUNTER_INC2(n2); diff --git a/src/libsodium/crypto_aead/chacha20poly1305/sodium/aead_chacha20poly1305.c b/src/libsodium/crypto_aead/chacha20poly1305/sodium/aead_chacha20poly1305.c index 768578e4..89165186 100644 --- a/src/libsodium/crypto_aead/chacha20poly1305/sodium/aead_chacha20poly1305.c +++ b/src/libsodium/crypto_aead/chacha20poly1305/sodium/aead_chacha20poly1305.c @@ -198,6 +198,9 @@ crypto_aead_chacha20poly1305_decrypt_detached(unsigned char *m, (void) sizeof(int[sizeof computed_mac == 16U ? 1 : -1]); ret = crypto_verify_16(computed_mac, mac); sodium_memzero(computed_mac, sizeof computed_mac); + if (m == NULL) { + return ret; + } if (ret != 0) { memset(m, 0, mlen); return -1; @@ -279,6 +282,9 @@ crypto_aead_chacha20poly1305_ietf_decrypt_detached(unsigned char *m, (void) sizeof(int[sizeof computed_mac == 16U ? 1 : -1]); ret = crypto_verify_16(computed_mac, mac); sodium_memzero(computed_mac, sizeof computed_mac); + if (m == NULL) { + return ret; + } if (ret != 0) { memset(m, 0, mlen); return -1; diff --git a/src/libsodium/crypto_secretbox/crypto_secretbox_easy.c b/src/libsodium/crypto_secretbox/crypto_secretbox_easy.c index dcc21b33..008c680b 100644 --- a/src/libsodium/crypto_secretbox/crypto_secretbox_easy.c +++ b/src/libsodium/crypto_secretbox/crypto_secretbox_easy.c @@ -96,6 +96,9 @@ crypto_secretbox_open_detached(unsigned char *m, const unsigned char *c, sodium_memzero(subkey, sizeof subkey); return -1; } + if (m == NULL) { + return 0; + } if (((uintptr_t) c >= (uintptr_t) m && (uintptr_t) c - (uintptr_t) m < clen) || ((uintptr_t) m >= (uintptr_t) c &&