From 2a562f8986d54136d4bc8ef14778297639b5379e Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Thu, 15 Jan 2015 00:44:28 +0100 Subject: [PATCH] Proper overlapping check; memmove() was called when it was superfluous. --- src/libsodium/crypto_secretbox/crypto_secretbox_easy.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/libsodium/crypto_secretbox/crypto_secretbox_easy.c b/src/libsodium/crypto_secretbox/crypto_secretbox_easy.c index 7c289a4c..e9b594c1 100644 --- a/src/libsodium/crypto_secretbox/crypto_secretbox_easy.c +++ b/src/libsodium/crypto_secretbox/crypto_secretbox_easy.c @@ -29,7 +29,10 @@ crypto_secretbox_detached(unsigned char *c, unsigned char *mac, crypto_core_hsalsa20(subkey, n, k, sigma); - if (c - m < mlen || c - m > -mlen) { + if (((uintptr_t) c >= (uintptr_t) m && + (uintptr_t) c - (uintptr_t) m < mlen) || + ((uintptr_t) m >= (uintptr_t) c && + (uintptr_t) m - (uintptr_t) c < mlen)) { memmove(c, m, mlen); m = c; } @@ -95,7 +98,10 @@ crypto_secretbox_open_detached(unsigned char *m, const unsigned char *c, sodium_memzero(subkey, sizeof subkey); return -1; } - if (m - c < clen || m - c > -clen) { + if (((uintptr_t) c >= (uintptr_t) m && + (uintptr_t) c - (uintptr_t) m < clen) || + ((uintptr_t) m >= (uintptr_t) c && + (uintptr_t) m - (uintptr_t) c < clen)) { memmove(m, c, clen); c = m; }