crypto_(secret)box_easy: check SIZE_MAX overflow only where needed

This commit is contained in:
Frank Denis 2014-09-16 21:09:55 -07:00
parent c54b05a1e5
commit 877bf76716
2 changed files with 10 additions and 3 deletions

View File

@ -1,4 +1,8 @@
#include <limits.h>
#include <stdint.h>
#include <stdlib.h>
#include "crypto_box.h" #include "crypto_box.h"
#include "crypto_secretbox.h" #include "crypto_secretbox.h"
#include "utils.h" #include "utils.h"
@ -26,6 +30,9 @@ crypto_box_easy(unsigned char *c, const unsigned char *m,
unsigned long long mlen, const unsigned char *n, unsigned long long mlen, const unsigned char *n,
const unsigned char *pk, const unsigned char *sk) const unsigned char *pk, const unsigned char *sk)
{ {
if (mlen > SIZE_MAX - crypto_box_MACBYTES) {
return -1;
}
return crypto_box_detached(c + crypto_box_MACBYTES, c, m, mlen, n, return crypto_box_detached(c + crypto_box_MACBYTES, c, m, mlen, n,
pk, sk); pk, sk);
} }

View File

@ -27,9 +27,6 @@ crypto_secretbox_detached(unsigned char *c, unsigned char *mac,
unsigned long long i; unsigned long long i;
unsigned long long mlen0; unsigned long long mlen0;
if (mlen > SIZE_MAX - crypto_secretbox_MACBYTES) {
return -1;
}
crypto_core_hsalsa20(subkey, n, k, sigma); crypto_core_hsalsa20(subkey, n, k, sigma);
memset(block0, 0U, crypto_secretbox_ZEROBYTES); memset(block0, 0U, crypto_secretbox_ZEROBYTES);
@ -68,6 +65,9 @@ crypto_secretbox_easy(unsigned char *c, const unsigned char *m,
unsigned long long mlen, const unsigned char *n, unsigned long long mlen, const unsigned char *n,
const unsigned char *k) const unsigned char *k)
{ {
if (mlen > SIZE_MAX - crypto_secretbox_MACBYTES) {
return -1;
}
return crypto_secretbox_detached(c + crypto_secretbox_MACBYTES, return crypto_secretbox_detached(c + crypto_secretbox_MACBYTES,
c, m, mlen, n, k); c, m, mlen, n, k);
} }