1
mirror of https://github.com/jedisct1/libsodium.git synced 2024-12-31 22:42:57 -07:00

Introduce *_BYTES_MAX constants

*_BYTES_MAX constants constants represent the maximum size of
a message.

No accessor functions for now. They will be renamed, as the
*_BYTES_MAX suffix was previously also used for the maximum output
size of stream ciphers.

These macros are designed to be used by language bindings, so they
can perform some sanity checks before calling the sodium API.
This commit is contained in:
Frank Denis 2017-07-29 17:39:31 +02:00
parent 568adb570d
commit 16179b87f3
39 changed files with 108 additions and 30 deletions

View File

@ -524,7 +524,7 @@ crypto_aead_aes256gcm_encrypt_detached_afternm(unsigned char *c,
(void) nsec;
memcpy(H, ctx->H, sizeof H);
if (mlen > 16ULL * ((1ULL << 32) - 2)) {
if (mlen > crypto_aead_aes256gcm_BYTES_MAX) {
sodium_misuse(); /* LCOV_EXCL_LINE */
}
memcpy(&n2[0], npub, 3 * 4);
@ -662,7 +662,7 @@ crypto_aead_aes256gcm_decrypt_detached_afternm(unsigned char *m, unsigned char *
CRYPTO_ALIGN(16) unsigned char fb[16];
(void) nsec;
if (clen > 16ULL * (1ULL << 32)) {
if (clen > crypto_aead_aes256gcm_BYTES_MAX) {
sodium_misuse(); /* LCOV_EXCL_LINE */
}
mlen = clen;

View File

@ -70,7 +70,7 @@ crypto_aead_chacha20poly1305_encrypt(unsigned char *c,
unsigned long long clen = 0ULL;
int ret;
if (mlen > UINT64_MAX - crypto_aead_chacha20poly1305_ABYTES) {
if (mlen > crypto_aead_chacha20poly1305_BYTES_MAX) {
sodium_misuse();
}
ret = crypto_aead_chacha20poly1305_encrypt_detached(c,
@ -145,7 +145,7 @@ crypto_aead_chacha20poly1305_ietf_encrypt(unsigned char *c,
unsigned long long clen = 0ULL;
int ret;
if (mlen > UINT64_MAX - crypto_aead_chacha20poly1305_ietf_ABYTES) {
if (mlen > crypto_aead_chacha20poly1305_ietf_BYTES_MAX) {
sodium_misuse();
}
ret = crypto_aead_chacha20poly1305_ietf_encrypt_detached(c,

View File

@ -40,7 +40,7 @@ crypto_box_easy_afternm(unsigned char *c, const unsigned char *m,
unsigned long long mlen, const unsigned char *n,
const unsigned char *k)
{
if (mlen > SIZE_MAX - crypto_box_MACBYTES) {
if (mlen > crypto_box_BYTES_MAX) {
return -1;
}
return crypto_box_detached_afternm(c + crypto_box_MACBYTES, c, m, mlen, n,
@ -52,7 +52,7 @@ crypto_box_easy(unsigned char *c, const unsigned char *m,
unsigned long long mlen, const unsigned char *n,
const unsigned char *pk, const unsigned char *sk)
{
if (mlen > SIZE_MAX - crypto_box_MACBYTES) {
if (mlen > crypto_box_BYTES_MAX) {
return -1;
}
return crypto_box_detached(c + crypto_box_MACBYTES, c, m, mlen, n,

View File

@ -86,7 +86,7 @@ crypto_box_curve25519xchacha20poly1305_easy_afternm(unsigned char *c,
const unsigned char *n,
const unsigned char *k)
{
if (mlen > SIZE_MAX - crypto_box_curve25519xchacha20poly1305_MACBYTES) {
if (mlen > crypto_box_curve25519xchacha20poly1305_BYTES_MAX) {
return -1;
}
return crypto_box_curve25519xchacha20poly1305_detached_afternm(
@ -98,7 +98,7 @@ crypto_box_curve25519xchacha20poly1305_easy(
unsigned char *c, const unsigned char *m, unsigned long long mlen,
const unsigned char *n, const unsigned char *pk, const unsigned char *sk)
{
if (mlen > SIZE_MAX - crypto_box_curve25519xchacha20poly1305_MACBYTES) {
if (mlen > crypto_box_curve25519xchacha20poly1305_BYTES_MAX) {
return -1;
}
return crypto_box_curve25519xchacha20poly1305_detached(

View File

@ -33,6 +33,7 @@
#include "core.h"
#include "crypto_auth_hmacsha256.h"
#include "crypto_pwhash_scryptsalsa208sha256.h"
#include "pbkdf2-sha256.h"
#include "private/common.h"
#include "utils.h"
@ -56,6 +57,8 @@ PBKDF2_SHA256(const uint8_t *passwd, size_t passwdlen, const uint8_t *salt,
size_t clen;
#if SIZE_MAX > 0x1fffffffe0ULL
COMPILER_ASSERT(crypto_pwhash_scryptsalsa208sha256_BYTES_MAX
<= 0x1fffffffe0ULL);
if (dkLen > 0x1fffffffe0ULL) {
sodium_misuse(); /* LCOV_EXCL_LINE */
}

View File

@ -71,7 +71,7 @@ crypto_secretbox_easy(unsigned char *c, const unsigned char *m,
unsigned long long mlen, const unsigned char *n,
const unsigned char *k)
{
if (mlen > SIZE_MAX - crypto_secretbox_MACBYTES) {
if (mlen > crypto_secretbox_BYTES_MAX) {
return -1;
}
return crypto_secretbox_detached(c + crypto_secretbox_MACBYTES,

View File

@ -77,7 +77,7 @@ crypto_secretbox_xchacha20poly1305_easy(unsigned char *c,
const unsigned char *n,
const unsigned char *k)
{
if (mlen > SIZE_MAX - crypto_secretbox_xchacha20poly1305_MACBYTES) {
if (mlen > crypto_secretbox_xchacha20poly1305_BYTES_MAX) {
return -1;
}
return crypto_secretbox_xchacha20poly1305_detached

View File

@ -83,7 +83,7 @@ crypto_sign_edwards25519sha512batch_open(unsigned char *m,
ge_p3 cs3;
*mlen_p = 0;
if (smlen < 64 || smlen > SIZE_MAX) {
if (smlen < 64 || smlen - 64 > crypto_sign_edwards25519sha512batch_BYTES_MAX) {
return -1;
}
mlen = smlen - 64;

View File

@ -171,7 +171,7 @@ crypto_sign_ed25519_open(unsigned char *m, unsigned long long *mlen_p,
{
unsigned long long mlen;
if (smlen < 64 || smlen > SIZE_MAX) {
if (smlen < 64 || smlen - 64 > crypto_sign_ed25519_BYTES_MAX) {
goto badsig;
}
mlen = smlen - 64;

View File

@ -77,7 +77,7 @@ chacha20_encrypt_bytes(chacha_ctx *ctx, const uint8_t *m, uint8_t *c,
if (!bytes) {
return; /* LCOV_EXCL_LINE */
}
if (bytes > 64ULL * (1ULL << 32) - 64ULL) {
if (bytes > crypto_stream_chacha20_BYTES_MAX) {
sodium_misuse();
}
# include "u8.h"

View File

@ -72,7 +72,7 @@ chacha20_encrypt_bytes(chacha_ctx *ctx, const uint8_t *m, uint8_t *c,
if (!bytes) {
return; /* LCOV_EXCL_LINE */
}
if (bytes > 64ULL * (1ULL << 32) - 64ULL) {
if (bytes > crypto_stream_chacha20_BYTES_MAX) {
sodium_misuse();
}
# include "u4.h"

View File

@ -92,7 +92,7 @@ chacha20_encrypt_bytes(chacha_ctx *ctx, const uint8_t *m, uint8_t *c,
if (!bytes) {
return; /* LCOV_EXCL_LINE */
}
if (bytes > 64ULL * (1ULL << 32) - 64ULL) {
if (bytes > crypto_stream_chacha20_BYTES_MAX) {
sodium_misuse();
}
j0 = ctx->input[0];

View File

@ -30,6 +30,10 @@ size_t crypto_aead_aes256gcm_npubbytes(void);
SODIUM_EXPORT
size_t crypto_aead_aes256gcm_abytes(void);
#define crypto_aead_aes256gcm_BYTES_MAX \
SODIUM_MIN(SODIUM_SIZE_MAX - crypto_aead_aes256gcm_ABYTES, \
(16ULL * ((1ULL << 32) - 2ULL)) - crypto_aead_aes256gcm_ABYTES)
typedef CRYPTO_ALIGN(16) unsigned char crypto_aead_aes256gcm_state[512];
SODIUM_EXPORT

View File

@ -30,6 +30,10 @@ size_t crypto_aead_chacha20poly1305_ietf_npubbytes(void);
SODIUM_EXPORT
size_t crypto_aead_chacha20poly1305_ietf_abytes(void);
#define crypto_aead_chacha20poly1305_ietf_BYTES_MAX \
SODIUM_MIN(SODIUM_SIZE_MAX - crypto_aead_chacha20poly1305_ietf_ABYTES, \
(64ULL * (1ULL << 32) - 64ULL) - crypto_aead_chacha20poly1305_ietf_ABYTES)
SODIUM_EXPORT
int crypto_aead_chacha20poly1305_ietf_encrypt(unsigned char *c,
unsigned long long *clen_p,
@ -98,6 +102,9 @@ size_t crypto_aead_chacha20poly1305_npubbytes(void);
SODIUM_EXPORT
size_t crypto_aead_chacha20poly1305_abytes(void);
#define crypto_aead_chacha20poly1305_BYTES_MAX \
(SODIUM_SIZE_MAX - crypto_aead_chacha20poly1305_ABYTES)
SODIUM_EXPORT
int crypto_aead_chacha20poly1305_encrypt(unsigned char *c,
unsigned long long *clen_p,

View File

@ -27,6 +27,9 @@ size_t crypto_aead_xchacha20poly1305_ietf_npubbytes(void);
SODIUM_EXPORT
size_t crypto_aead_xchacha20poly1305_ietf_abytes(void);
#define crypto_aead_xchacha20poly1305_ietf_BYTES_MAX \
(SODIUM_SIZE_MAX - crypto_aead_xchacha20poly1305_ietf_ABYTES)
SODIUM_EXPORT
int crypto_aead_xchacha20poly1305_ietf_encrypt(unsigned char *c,
unsigned long long *clen_p,

View File

@ -40,6 +40,8 @@ size_t crypto_box_noncebytes(void);
SODIUM_EXPORT
size_t crypto_box_macbytes(void);
#define crypto_box_BYTES_MAX crypto_box_curve25519xsalsa20poly1305_BYTES_MAX
#define crypto_box_PRIMITIVE "curve25519xsalsa20poly1305"
SODIUM_EXPORT
const char *crypto_box_primitive(void);

View File

@ -3,6 +3,7 @@
#define crypto_box_curve25519xchacha20poly1305_H
#include <stddef.h>
#include "crypto_stream_xchacha20.h"
#include "export.h"
#ifdef __cplusplus
@ -36,6 +37,9 @@ size_t crypto_box_curve25519xchacha20poly1305_noncebytes(void);
SODIUM_EXPORT
size_t crypto_box_curve25519xchacha20poly1305_macbytes(void);
#define crypto_box_curve25519xchacha20poly1305_BYTES_MAX \
(crypto_stream_xchacha20_BYTES_MAX - crypto_box_curve25519xchacha20poly1305_MACBYTES)
SODIUM_EXPORT
int crypto_box_curve25519xchacha20poly1305_seed_keypair(unsigned char *pk,
unsigned char *sk,

View File

@ -2,6 +2,7 @@
#define crypto_box_curve25519xsalsa20poly1305_H
#include <stddef.h>
#include "crypto_stream_xsalsa20.h"
#include "export.h"
#ifdef __cplusplus
@ -11,6 +12,8 @@
extern "C" {
#endif
/* -- NaCl compatibility interface ; Requires padding -- */
#define crypto_box_curve25519xsalsa20poly1305_SEEDBYTES 32U
SODIUM_EXPORT
size_t crypto_box_curve25519xsalsa20poly1305_seedbytes(void);
@ -45,6 +48,9 @@ size_t crypto_box_curve25519xsalsa20poly1305_boxzerobytes(void);
SODIUM_EXPORT
size_t crypto_box_curve25519xsalsa20poly1305_zerobytes(void);
#define crypto_box_curve25519xsalsa20poly1305_BYTES_MAX \
(crypto_stream_xsalsa20_BYTES_MAX - crypto_box_curve25519xsalsa20poly1305_ZEROBYTES)
SODIUM_EXPORT
int crypto_box_curve25519xsalsa20poly1305(unsigned char *c,
const unsigned char *m,

View File

@ -58,7 +58,8 @@ size_t crypto_pwhash_argon2i_opslimit_max(void);
SODIUM_EXPORT
size_t crypto_pwhash_argon2i_memlimit_min(void);
#define crypto_pwhash_argon2i_MEMLIMIT_MAX ((SIZE_MAX >= 4398046510080U) ? 4398046510080U : (SIZE_MAX >= 2147483648U) ? 2147483648U : 32768U)
#define crypto_pwhash_argon2i_MEMLIMIT_MAX \
((SIZE_MAX >= 4398046510080U) ? 4398046510080U : (SIZE_MAX >= 2147483648U) ? 2147483648U : 32768U)
SODIUM_EXPORT
size_t crypto_pwhash_argon2i_memlimit_max(void);

View File

@ -58,7 +58,8 @@ size_t crypto_pwhash_argon2id_opslimit_max(void);
SODIUM_EXPORT
size_t crypto_pwhash_argon2id_memlimit_min(void);
#define crypto_pwhash_argon2id_MEMLIMIT_MAX ((SIZE_MAX >= 4398046510080U) ? 4398046510080U : (SIZE_MAX >= 2147483648U) ? 2147483648U : 32768U)
#define crypto_pwhash_argon2id_MEMLIMIT_MAX \
((SIZE_MAX >= 4398046510080U) ? 4398046510080U : (SIZE_MAX >= 2147483648U) ? 2147483648U : 32768U)
SODIUM_EXPORT
size_t crypto_pwhash_argon2id_memlimit_max(void);

View File

@ -18,7 +18,8 @@ extern "C" {
SODIUM_EXPORT
size_t crypto_pwhash_scryptsalsa208sha256_bytes_min(void);
#define crypto_pwhash_scryptsalsa208sha256_BYTES_MAX (SIZE_MAX > 0x1fffffffe0ULL ? 0x1fffffffe0ULL : SIZE_MAX)
#define crypto_pwhash_scryptsalsa208sha256_BYTES_MAX \
SODIUM_MIN(SODIUM_SIZE_MAX, 0x1fffffffe0ULL)
SODIUM_EXPORT
size_t crypto_pwhash_scryptsalsa208sha256_bytes_max(void);
@ -26,7 +27,7 @@ size_t crypto_pwhash_scryptsalsa208sha256_bytes_max(void);
SODIUM_EXPORT
size_t crypto_pwhash_scryptsalsa208sha256_passwd_min(void);
#define crypto_pwhash_scryptsalsa208sha256_PASSWD_MAX SIZE_MAX
#define crypto_pwhash_scryptsalsa208sha256_PASSWD_MAX SODIUM_SIZE_MAX
SODIUM_EXPORT
size_t crypto_pwhash_scryptsalsa208sha256_passwd_max(void);
@ -54,7 +55,8 @@ size_t crypto_pwhash_scryptsalsa208sha256_opslimit_max(void);
SODIUM_EXPORT
size_t crypto_pwhash_scryptsalsa208sha256_memlimit_min(void);
#define crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_MAX ((SIZE_MAX >= 68719476736U) ? 68719476736U : SIZE_MAX)
#define crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_MAX \
SODIUM_MIN(SIZE_MAX, 68719476736ULL)
SODIUM_EXPORT
size_t crypto_pwhash_scryptsalsa208sha256_memlimit_max(void);

View File

@ -29,6 +29,8 @@ size_t crypto_secretbox_macbytes(void);
SODIUM_EXPORT
const char *crypto_secretbox_primitive(void);
#define crypto_secretbox_BYTES_MAX crypto_secretbox_xsalsa20poly1305_BYTES_MAX
SODIUM_EXPORT
int crypto_secretbox_easy(unsigned char *c, const unsigned char *m,
unsigned long long mlen, const unsigned char *n,

View File

@ -2,6 +2,7 @@
#define crypto_secretbox_xchacha20poly1305_H
#include <stddef.h>
#include "crypto_stream_xchacha20.h"
#include "export.h"
#ifdef __cplusplus
@ -23,6 +24,9 @@ size_t crypto_secretbox_xchacha20poly1305_noncebytes(void);
SODIUM_EXPORT
size_t crypto_secretbox_xchacha20poly1305_macbytes(void);
#define crypto_secretbox_xchacha20poly1305_BYTES_MAX \
(crypto_stream_xchacha20_BYTES_MAX - crypto_secretbox_xchacha20poly1305_MACBYTES)
SODIUM_EXPORT
int crypto_secretbox_xchacha20poly1305_easy(unsigned char *c,
const unsigned char *m,

View File

@ -2,6 +2,7 @@
#define crypto_secretbox_xsalsa20poly1305_H
#include <stddef.h>
#include "crypto_stream_xsalsa20.h"
#include "export.h"
#ifdef __cplusplus
@ -23,15 +24,8 @@ size_t crypto_secretbox_xsalsa20poly1305_noncebytes(void);
SODIUM_EXPORT
size_t crypto_secretbox_xsalsa20poly1305_macbytes(void);
#define crypto_secretbox_xsalsa20poly1305_BOXZEROBYTES 16U
SODIUM_EXPORT
size_t crypto_secretbox_xsalsa20poly1305_boxzerobytes(void);
#define crypto_secretbox_xsalsa20poly1305_ZEROBYTES \
(crypto_secretbox_xsalsa20poly1305_BOXZEROBYTES + \
crypto_secretbox_xsalsa20poly1305_MACBYTES)
SODIUM_EXPORT
size_t crypto_secretbox_xsalsa20poly1305_zerobytes(void);
#define crypto_secretbox_xsalsa20poly1305_BYTES_MAX \
(crypto_stream_xsalsa20_BYTES_MAX - crypto_secretbox_xsalsa20poly1305_ZEROBYTES)
SODIUM_EXPORT
int crypto_secretbox_xsalsa20poly1305(unsigned char *c,
@ -51,6 +45,18 @@ int crypto_secretbox_xsalsa20poly1305_open(unsigned char *m,
SODIUM_EXPORT
void crypto_secretbox_xsalsa20poly1305_keygen(unsigned char k[crypto_secretbox_xsalsa20poly1305_KEYBYTES]);
/* -- NaCl compatibility interface ; Requires padding -- */
#define crypto_secretbox_xsalsa20poly1305_BOXZEROBYTES 16U
SODIUM_EXPORT
size_t crypto_secretbox_xsalsa20poly1305_boxzerobytes(void);
#define crypto_secretbox_xsalsa20poly1305_ZEROBYTES \
(crypto_secretbox_xsalsa20poly1305_BOXZEROBYTES + \
crypto_secretbox_xsalsa20poly1305_MACBYTES)
SODIUM_EXPORT
size_t crypto_secretbox_xsalsa20poly1305_zerobytes(void);
#ifdef __cplusplus
}
#endif

View File

@ -41,6 +41,8 @@ size_t crypto_sign_publickeybytes(void);
SODIUM_EXPORT
size_t crypto_sign_secretkeybytes(void);
#define crypto_sign_BYTES_MAX crypto_sign_ed25519_BYTES_MAX
#define crypto_sign_PRIMITIVE "ed25519"
SODIUM_EXPORT
const char *crypto_sign_primitive(void);

View File

@ -35,6 +35,8 @@ size_t crypto_sign_ed25519_publickeybytes(void);
SODIUM_EXPORT
size_t crypto_sign_ed25519_secretkeybytes(void);
#define crypto_sign_ed25519_BYTES_MAX (SODIUM_SIZE_MAX - crypto_sign_ed25519_BYTES)
SODIUM_EXPORT
int crypto_sign_ed25519(unsigned char *sm, unsigned long long *smlen_p,
const unsigned char *m, unsigned long long mlen,

View File

@ -25,6 +25,7 @@ extern "C" {
#define crypto_sign_edwards25519sha512batch_BYTES 64U
#define crypto_sign_edwards25519sha512batch_PUBLICKEYBYTES 32U
#define crypto_sign_edwards25519sha512batch_SECRETKEYBYTES (32U + 32U)
#define crypto_sign_edwards25519sha512batch_BYTES_MAX (SODIUM_SIZE_MAX - crypto_sign_edwards25519sha512batch_BYTES)
SODIUM_EXPORT
int crypto_sign_edwards25519sha512batch(unsigned char *sm,

View File

@ -29,6 +29,8 @@ size_t crypto_stream_keybytes(void);
SODIUM_EXPORT
size_t crypto_stream_noncebytes(void);
#define crypto_stream_BYTES_MAX crypto_stream_xsalsa20_BYTES_MAX
#define crypto_stream_PRIMITIVE "xsalsa20"
SODIUM_EXPORT
const char *crypto_stream_primitive(void);

View File

@ -31,6 +31,9 @@ size_t crypto_stream_aes128ctr_noncebytes(void);
SODIUM_EXPORT
size_t crypto_stream_aes128ctr_beforenmbytes(void);
#define crypto_stream_aes128ctr_SIZE_MAX \
SODIUM_MIN(SODIUM_SIZE_MAX, 16ULL * (1ULL << 32))
SODIUM_EXPORT
int crypto_stream_aes128ctr(unsigned char *out, unsigned long long outlen,
const unsigned char *n, const unsigned char *k)

View File

@ -28,6 +28,8 @@ size_t crypto_stream_chacha20_keybytes(void);
SODIUM_EXPORT
size_t crypto_stream_chacha20_noncebytes(void);
#define crypto_stream_chacha20_BYTES_MAX SODIUM_SIZE_MAX
/* ChaCha20 with a 64-bit nonce and a 64-bit counter, as originally designed */
SODIUM_EXPORT
@ -58,6 +60,9 @@ size_t crypto_stream_chacha20_ietf_keybytes(void);
SODIUM_EXPORT
size_t crypto_stream_chacha20_ietf_noncebytes(void);
#define crypto_stream_chacha20_ietf_BYTES_MAX \
SODIUM_MIN(SODIUM_SIZE_MAX, 64ULL * (1ULL << 32))
SODIUM_EXPORT
int crypto_stream_chacha20_ietf(unsigned char *c, unsigned long long clen,
const unsigned char *n, const unsigned char *k);
@ -80,6 +85,7 @@ void crypto_stream_chacha20_ietf_keygen(unsigned char k[crypto_stream_chacha20_i
#define crypto_stream_chacha20_IETF_KEYBYTES crypto_stream_chacha20_ietf_KEYBYTES
#define crypto_stream_chacha20_IETF_NONCEBYTES crypto_stream_chacha20_ietf_NONCEBYTES
#define crypto_stream_chacha20_IETF_BYTES_MAX crypto_stream_chacha20_ietf_BYTES_MAX
#ifdef __cplusplus
}

View File

@ -28,6 +28,8 @@ size_t crypto_stream_salsa20_keybytes(void);
SODIUM_EXPORT
size_t crypto_stream_salsa20_noncebytes(void);
#define crypto_stream_salsa20_BYTES_MAX SODIUM_SIZE_MAX
SODIUM_EXPORT
int crypto_stream_salsa20(unsigned char *c, unsigned long long clen,
const unsigned char *n, const unsigned char *k);

View File

@ -27,6 +27,8 @@ size_t crypto_stream_salsa2012_keybytes(void);
SODIUM_EXPORT
size_t crypto_stream_salsa2012_noncebytes(void);
#define crypto_stream_salsa2012_BYTES_MAX SODIUM_SIZE_MAX
SODIUM_EXPORT
int crypto_stream_salsa2012(unsigned char *c, unsigned long long clen,
const unsigned char *n, const unsigned char *k);

View File

@ -27,6 +27,8 @@ size_t crypto_stream_salsa208_keybytes(void);
SODIUM_EXPORT
size_t crypto_stream_salsa208_noncebytes(void);
#define crypto_stream_salsa208_BYTES_MAX SODIUM_SIZE_MAX
SODIUM_EXPORT
int crypto_stream_salsa208(unsigned char *c, unsigned long long clen,
const unsigned char *n, const unsigned char *k);

View File

@ -28,6 +28,8 @@ size_t crypto_stream_xchacha20_keybytes(void);
SODIUM_EXPORT
size_t crypto_stream_xchacha20_noncebytes(void);
#define crypto_stream_xchacha20_BYTES_MAX SODIUM_SIZE_MAX
SODIUM_EXPORT
int crypto_stream_xchacha20(unsigned char *c, unsigned long long clen,
const unsigned char *n, const unsigned char *k);

View File

@ -28,6 +28,8 @@ size_t crypto_stream_xsalsa20_keybytes(void);
SODIUM_EXPORT
size_t crypto_stream_xsalsa20_noncebytes(void);
#define crypto_stream_xsalsa20_BYTES_MAX SODIUM_SIZE_MAX
SODIUM_EXPORT
int crypto_stream_xsalsa20(unsigned char *c, unsigned long long clen,
const unsigned char *n, const unsigned char *k);

View File

@ -41,4 +41,7 @@
# endif
#endif
#define SODIUM_MIN(A, B) ((A) < (B) ? (A) : (B))
#define SODIUM_SIZE_MAX SODIUM_MIN(UINT64_MAX, SIZE_MAX)
#endif

View File

@ -25,6 +25,8 @@ typedef struct randombytes_implementation {
int (*close)(void); /* optional */
} randombytes_implementation;
#define randombytes_BYTES_MAX SODIUM_MIN(SODIUM_SIZE_MAX, 0xffffffffUL)
#define randombytes_SEEDBYTES 32U
SODIUM_EXPORT
size_t randombytes_seedbytes(void);

View File

@ -174,6 +174,7 @@ randombytes_buf_deterministic(void * const buf, const size_t size,
COMPILER_ASSERT(randombytes_SEEDBYTES == crypto_stream_chacha20_ietf_KEYBYTES);
#if SIZE_MAX > 0x4000000000ULL
COMPILER_ASSERT(randombytes_BYTES_MAX <= 0x4000000000ULL);
if (size > 0x4000000000ULL) {
sodium_misuse();
}

View File

@ -334,7 +334,8 @@ randombytes_sysrandom_buf(void * const buf, const size_t size)
sodium_misuse(); /* LCOV_EXCL_LINE */
}
#else
if (size > (size_t) 0xffffffff) {
COMPILER_ASSERT(randombytes_BYTES_MAX <= 0xffffffffUL);
if (size > (size_t) 0xffffffffUL) {
sodium_misuse(); /* LCOV_EXCL_LINE */
}
if (! RtlGenRandom((PVOID) buf, (ULONG) size)) {