Relax most __attribute__ ((nonnull)) to allow 0-length inputs to be NULL.

Justifications:
- crypto_(auth|hash|generichash|onetimeauth|shorthash)*:
  it's legal to hash or HMAC a 0-length message
- crypto_box*: it's legal to encrypt a 0-length message
- crypto_sign*: it's legal to sign a 0-length message
- utils:
  comparing two 0-length byte arrays is legal
  memzero on a 0-length byte array is a no-op
  converting an empty hex string to binary results in an empty binary string
  converting an empty binary string to hex results in an empty hex string
  converting an empty b64 string to binary results in an empty binary string
  converting an empty binary string to b64 results in an empty b64 string
  sodium_add / sodium_sub on zero-length arrays is a no-op

For the functions declared in utils.h, I moved the logic into private functions that
have the __attribute__ ((nonnull)) check, but they are only called when the
corresponding length argument is non-0. I didn't do this for the hash/box/sign
functions since it would have been a lot more work and quite a large refactor.

Only memset() may have issues with a zero length.

Fix tests, use guard page instead of NULL because of Wasm
This commit is contained in:
Ilya Maykov
2019-04-26 15:36:58 +02:00
committed by Frank Denis
parent 9b73106c8a
commit 6bece9c8c4
34 changed files with 203 additions and 74 deletions
+2 -2
View File
@@ -28,12 +28,12 @@ const char *crypto_auth_primitive(void);
SODIUM_EXPORT
int crypto_auth(unsigned char *out, const unsigned char *in,
unsigned long long inlen, const unsigned char *k)
__attribute__ ((nonnull));
__attribute__ ((nonnull(1, 4)));
SODIUM_EXPORT
int crypto_auth_verify(const unsigned char *h, const unsigned char *in,
unsigned long long inlen, const unsigned char *k)
__attribute__ ((warn_unused_result)) __attribute__ ((nonnull));
__attribute__ ((warn_unused_result)) __attribute__ ((nonnull(1, 4)));
SODIUM_EXPORT
void crypto_auth_keygen(unsigned char k[crypto_auth_KEYBYTES])
@@ -24,14 +24,14 @@ SODIUM_EXPORT
int crypto_auth_hmacsha256(unsigned char *out,
const unsigned char *in,
unsigned long long inlen,
const unsigned char *k) __attribute__ ((nonnull));
const unsigned char *k) __attribute__ ((nonnull(1, 4)));
SODIUM_EXPORT
int crypto_auth_hmacsha256_verify(const unsigned char *h,
const unsigned char *in,
unsigned long long inlen,
const unsigned char *k)
__attribute__ ((warn_unused_result)) __attribute__ ((nonnull));
__attribute__ ((warn_unused_result)) __attribute__ ((nonnull(1, 4)));
/* ------------------------------------------------------------------------- */
@@ -52,7 +52,7 @@ SODIUM_EXPORT
int crypto_auth_hmacsha256_update(crypto_auth_hmacsha256_state *state,
const unsigned char *in,
unsigned long long inlen)
__attribute__ ((nonnull));
__attribute__ ((nonnull(1)));
SODIUM_EXPORT
int crypto_auth_hmacsha256_final(crypto_auth_hmacsha256_state *state,
@@ -24,14 +24,14 @@ SODIUM_EXPORT
int crypto_auth_hmacsha512(unsigned char *out,
const unsigned char *in,
unsigned long long inlen,
const unsigned char *k) __attribute__ ((nonnull));
const unsigned char *k) __attribute__ ((nonnull(1, 4)));
SODIUM_EXPORT
int crypto_auth_hmacsha512_verify(const unsigned char *h,
const unsigned char *in,
unsigned long long inlen,
const unsigned char *k)
__attribute__ ((warn_unused_result)) __attribute__ ((nonnull));
__attribute__ ((warn_unused_result)) __attribute__ ((nonnull(1, 4)));
/* ------------------------------------------------------------------------- */
@@ -51,7 +51,7 @@ int crypto_auth_hmacsha512_init(crypto_auth_hmacsha512_state *state,
SODIUM_EXPORT
int crypto_auth_hmacsha512_update(crypto_auth_hmacsha512_state *state,
const unsigned char *in,
unsigned long long inlen) __attribute__ ((nonnull));
unsigned long long inlen) __attribute__ ((nonnull(1)));
SODIUM_EXPORT
int crypto_auth_hmacsha512_final(crypto_auth_hmacsha512_state *state,
@@ -21,15 +21,17 @@ SODIUM_EXPORT
size_t crypto_auth_hmacsha512256_keybytes(void);
SODIUM_EXPORT
int crypto_auth_hmacsha512256(unsigned char *out, const unsigned char *in,
unsigned long long inlen,const unsigned char *k);
int crypto_auth_hmacsha512256(unsigned char *out,
const unsigned char *in,
unsigned long long inlen,
const unsigned char *k) __attribute__ ((nonnull(1, 4)));
SODIUM_EXPORT
int crypto_auth_hmacsha512256_verify(const unsigned char *h,
const unsigned char *in,
unsigned long long inlen,
const unsigned char *k)
__attribute__ ((warn_unused_result)) __attribute__ ((nonnull));
__attribute__ ((warn_unused_result)) __attribute__ ((nonnull(1, 4)));
/* ------------------------------------------------------------------------- */
@@ -46,7 +48,7 @@ int crypto_auth_hmacsha512256_init(crypto_auth_hmacsha512256_state *state,
SODIUM_EXPORT
int crypto_auth_hmacsha512256_update(crypto_auth_hmacsha512256_state *state,
const unsigned char *in,
unsigned long long inlen) __attribute__ ((nonnull));
unsigned long long inlen) __attribute__ ((nonnull(1)));
SODIUM_EXPORT
int crypto_auth_hmacsha512256_final(crypto_auth_hmacsha512256_state *state,
+7 -7
View File
@@ -61,7 +61,7 @@ SODIUM_EXPORT
int 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)
__attribute__ ((warn_unused_result)) __attribute__ ((nonnull));
__attribute__ ((warn_unused_result)) __attribute__ ((nonnull(1, 4, 5, 6)));
SODIUM_EXPORT
int crypto_box_open_easy(unsigned char *m, const unsigned char *c,
@@ -74,7 +74,7 @@ int crypto_box_detached(unsigned char *c, unsigned char *mac,
const unsigned char *m, unsigned long long mlen,
const unsigned char *n, const unsigned char *pk,
const unsigned char *sk)
__attribute__ ((warn_unused_result)) __attribute__ ((nonnull));
__attribute__ ((warn_unused_result)) __attribute__ ((nonnull(1, 2, 5, 6, 7)));
SODIUM_EXPORT
int crypto_box_open_detached(unsigned char *m, const unsigned char *c,
@@ -99,7 +99,7 @@ int crypto_box_beforenm(unsigned char *k, const unsigned char *pk,
SODIUM_EXPORT
int crypto_box_easy_afternm(unsigned char *c, const unsigned char *m,
unsigned long long mlen, const unsigned char *n,
const unsigned char *k) __attribute__ ((nonnull));
const unsigned char *k) __attribute__ ((nonnull(1, 4, 5)));
SODIUM_EXPORT
int crypto_box_open_easy_afternm(unsigned char *m, const unsigned char *c,
@@ -111,7 +111,7 @@ SODIUM_EXPORT
int crypto_box_detached_afternm(unsigned char *c, unsigned char *mac,
const unsigned char *m, unsigned long long mlen,
const unsigned char *n, const unsigned char *k)
__attribute__ ((nonnull));
__attribute__ ((nonnull(1, 2, 5, 6)));
SODIUM_EXPORT
int crypto_box_open_detached_afternm(unsigned char *m, const unsigned char *c,
@@ -129,7 +129,7 @@ size_t crypto_box_sealbytes(void);
SODIUM_EXPORT
int crypto_box_seal(unsigned char *c, const unsigned char *m,
unsigned long long mlen, const unsigned char *pk)
__attribute__ ((nonnull));
__attribute__ ((nonnull(1, 4)));
SODIUM_EXPORT
int crypto_box_seal_open(unsigned char *m, const unsigned char *c,
@@ -151,7 +151,7 @@ SODIUM_EXPORT
int crypto_box(unsigned char *c, const unsigned char *m,
unsigned long long mlen, const unsigned char *n,
const unsigned char *pk, const unsigned char *sk)
__attribute__ ((warn_unused_result)) __attribute__ ((nonnull));
__attribute__ ((warn_unused_result)) __attribute__ ((nonnull(1, 4, 5, 6)));
SODIUM_EXPORT
int crypto_box_open(unsigned char *m, const unsigned char *c,
@@ -162,7 +162,7 @@ int crypto_box_open(unsigned char *m, const unsigned char *c,
SODIUM_EXPORT
int crypto_box_afternm(unsigned char *c, const unsigned char *m,
unsigned long long mlen, const unsigned char *n,
const unsigned char *k) __attribute__ ((nonnull));
const unsigned char *k) __attribute__ ((nonnull(1, 4, 5)));
SODIUM_EXPORT
int crypto_box_open_afternm(unsigned char *m, const unsigned char *c,
@@ -60,7 +60,7 @@ int crypto_box_curve25519xchacha20poly1305_easy(unsigned char *c,
const unsigned char *n,
const unsigned char *pk,
const unsigned char *sk)
__attribute__ ((warn_unused_result)) __attribute__ ((nonnull));
__attribute__ ((warn_unused_result)) __attribute__ ((nonnull(1, 4, 5, 6)));
SODIUM_EXPORT
int crypto_box_curve25519xchacha20poly1305_open_easy(unsigned char *m,
@@ -79,7 +79,7 @@ int crypto_box_curve25519xchacha20poly1305_detached(unsigned char *c,
const unsigned char *n,
const unsigned char *pk,
const unsigned char *sk)
__attribute__ ((warn_unused_result)) __attribute__ ((nonnull));
__attribute__ ((warn_unused_result)) __attribute__ ((nonnull(1, 2, 5, 6, 7)));
SODIUM_EXPORT
int crypto_box_curve25519xchacha20poly1305_open_detached(unsigned char *m,
@@ -105,7 +105,7 @@ int crypto_box_curve25519xchacha20poly1305_easy_afternm(unsigned char *c,
unsigned long long mlen,
const unsigned char *n,
const unsigned char *k)
__attribute__ ((nonnull));
__attribute__ ((nonnull(1, 4, 5)));
SODIUM_EXPORT
int crypto_box_curve25519xchacha20poly1305_open_easy_afternm(unsigned char *m,
@@ -122,7 +122,7 @@ int crypto_box_curve25519xchacha20poly1305_detached_afternm(unsigned char *c,
unsigned long long mlen,
const unsigned char *n,
const unsigned char *k)
__attribute__ ((nonnull));
__attribute__ ((nonnull(1, 2, 5, 6)));
SODIUM_EXPORT
int crypto_box_curve25519xchacha20poly1305_open_detached_afternm(unsigned char *m,
@@ -147,7 +147,7 @@ int crypto_box_curve25519xchacha20poly1305_seal(unsigned char *c,
const unsigned char *m,
unsigned long long mlen,
const unsigned char *pk)
__attribute__ ((nonnull));
__attribute__ ((nonnull(1, 4)));
SODIUM_EXPORT
int crypto_box_curve25519xchacha20poly1305_seal_open(unsigned char *m,
@@ -78,7 +78,7 @@ int crypto_box_curve25519xsalsa20poly1305(unsigned char *c,
const unsigned char *n,
const unsigned char *pk,
const unsigned char *sk)
__attribute__ ((warn_unused_result)) __attribute__ ((nonnull));
__attribute__ ((warn_unused_result)) __attribute__ ((nonnull(1, 4, 5, 6)));
SODIUM_EXPORT
int crypto_box_curve25519xsalsa20poly1305_open(unsigned char *m,
@@ -95,7 +95,7 @@ int crypto_box_curve25519xsalsa20poly1305_afternm(unsigned char *c,
unsigned long long mlen,
const unsigned char *n,
const unsigned char *k)
__attribute__ ((nonnull));
__attribute__ ((nonnull(1, 4, 5)));
SODIUM_EXPORT
int crypto_box_curve25519xsalsa20poly1305_open_afternm(unsigned char *m,
@@ -66,7 +66,7 @@ SODIUM_EXPORT
int crypto_generichash_update(crypto_generichash_state *state,
const unsigned char *in,
unsigned long long inlen)
__attribute__ ((nonnull));
__attribute__ ((nonnull(1)));
SODIUM_EXPORT
int crypto_generichash_final(crypto_generichash_state *state,
@@ -100,7 +100,7 @@ SODIUM_EXPORT
int crypto_generichash_blake2b_update(crypto_generichash_blake2b_state *state,
const unsigned char *in,
unsigned long long inlen)
__attribute__ ((nonnull));
__attribute__ ((nonnull(1)));
SODIUM_EXPORT
int crypto_generichash_blake2b_final(crypto_generichash_blake2b_state *state,
+1 -1
View File
@@ -26,7 +26,7 @@ size_t crypto_hash_bytes(void);
SODIUM_EXPORT
int crypto_hash(unsigned char *out, const unsigned char *in,
unsigned long long inlen) __attribute__ ((nonnull));
unsigned long long inlen) __attribute__ ((nonnull(1)));
#define crypto_hash_PRIMITIVE "sha512"
SODIUM_EXPORT
@@ -36,7 +36,7 @@ size_t crypto_hash_sha256_bytes(void);
SODIUM_EXPORT
int crypto_hash_sha256(unsigned char *out, const unsigned char *in,
unsigned long long inlen) __attribute__ ((nonnull));
unsigned long long inlen) __attribute__ ((nonnull(1)));
SODIUM_EXPORT
int crypto_hash_sha256_init(crypto_hash_sha256_state *state)
@@ -46,7 +46,7 @@ SODIUM_EXPORT
int crypto_hash_sha256_update(crypto_hash_sha256_state *state,
const unsigned char *in,
unsigned long long inlen)
__attribute__ ((nonnull));
__attribute__ ((nonnull(1)));
SODIUM_EXPORT
int crypto_hash_sha256_final(crypto_hash_sha256_state *state,
@@ -36,7 +36,7 @@ size_t crypto_hash_sha512_bytes(void);
SODIUM_EXPORT
int crypto_hash_sha512(unsigned char *out, const unsigned char *in,
unsigned long long inlen) __attribute__ ((nonnull));
unsigned long long inlen) __attribute__ ((nonnull(1)));
SODIUM_EXPORT
int crypto_hash_sha512_init(crypto_hash_sha512_state *state)
@@ -46,7 +46,7 @@ SODIUM_EXPORT
int crypto_hash_sha512_update(crypto_hash_sha512_state *state,
const unsigned char *in,
unsigned long long inlen)
__attribute__ ((nonnull));
__attribute__ ((nonnull(1)));
SODIUM_EXPORT
int crypto_hash_sha512_final(crypto_hash_sha512_state *state,
@@ -33,12 +33,12 @@ const char *crypto_onetimeauth_primitive(void);
SODIUM_EXPORT
int crypto_onetimeauth(unsigned char *out, const unsigned char *in,
unsigned long long inlen, const unsigned char *k)
__attribute__ ((nonnull));
__attribute__ ((nonnull(1, 4)));
SODIUM_EXPORT
int crypto_onetimeauth_verify(const unsigned char *h, const unsigned char *in,
unsigned long long inlen, const unsigned char *k)
__attribute__ ((warn_unused_result)) __attribute__ ((nonnull));
__attribute__ ((warn_unused_result)) __attribute__ ((nonnull(1, 4)));
SODIUM_EXPORT
int crypto_onetimeauth_init(crypto_onetimeauth_state *state,
@@ -48,7 +48,7 @@ SODIUM_EXPORT
int crypto_onetimeauth_update(crypto_onetimeauth_state *state,
const unsigned char *in,
unsigned long long inlen)
__attribute__ ((nonnull));
__attribute__ ((nonnull(1)));
SODIUM_EXPORT
int crypto_onetimeauth_final(crypto_onetimeauth_state *state,
@@ -36,14 +36,14 @@ int crypto_onetimeauth_poly1305(unsigned char *out,
const unsigned char *in,
unsigned long long inlen,
const unsigned char *k)
__attribute__ ((nonnull));
__attribute__ ((nonnull(1, 4)));
SODIUM_EXPORT
int crypto_onetimeauth_poly1305_verify(const unsigned char *h,
const unsigned char *in,
unsigned long long inlen,
const unsigned char *k)
__attribute__ ((warn_unused_result)) __attribute__ ((nonnull));
__attribute__ ((warn_unused_result)) __attribute__ ((nonnull(1, 4)));
SODIUM_EXPORT
int crypto_onetimeauth_poly1305_init(crypto_onetimeauth_poly1305_state *state,
@@ -54,7 +54,7 @@ SODIUM_EXPORT
int crypto_onetimeauth_poly1305_update(crypto_onetimeauth_poly1305_state *state,
const unsigned char *in,
unsigned long long inlen)
__attribute__ ((nonnull));
__attribute__ ((nonnull(1)));
SODIUM_EXPORT
int crypto_onetimeauth_poly1305_final(crypto_onetimeauth_poly1305_state *state,
@@ -36,7 +36,7 @@ size_t crypto_secretbox_messagebytes_max(void);
SODIUM_EXPORT
int crypto_secretbox_easy(unsigned char *c, const unsigned char *m,
unsigned long long mlen, const unsigned char *n,
const unsigned char *k) __attribute__ ((nonnull));
const unsigned char *k) __attribute__ ((nonnull(1, 4, 5)));
SODIUM_EXPORT
int crypto_secretbox_open_easy(unsigned char *m, const unsigned char *c,
@@ -50,7 +50,7 @@ int crypto_secretbox_detached(unsigned char *c, unsigned char *mac,
unsigned long long mlen,
const unsigned char *n,
const unsigned char *k)
__attribute__ ((nonnull));
__attribute__ ((nonnull(1, 2, 5, 6)));
SODIUM_EXPORT
int crypto_secretbox_open_detached(unsigned char *m,
@@ -78,7 +78,7 @@ size_t crypto_secretbox_boxzerobytes(void);
SODIUM_EXPORT
int crypto_secretbox(unsigned char *c, const unsigned char *m,
unsigned long long mlen, const unsigned char *n,
const unsigned char *k) __attribute__ ((nonnull));
const unsigned char *k) __attribute__ ((nonnull(1, 4, 5)));
SODIUM_EXPORT
int crypto_secretbox_open(unsigned char *m, const unsigned char *c,
@@ -35,7 +35,7 @@ int crypto_secretbox_xchacha20poly1305_easy(unsigned char *c,
unsigned long long mlen,
const unsigned char *n,
const unsigned char *k)
__attribute__ ((nonnull));
__attribute__ ((nonnull(1, 4, 5)));
SODIUM_EXPORT
int crypto_secretbox_xchacha20poly1305_open_easy(unsigned char *m,
@@ -52,7 +52,7 @@ int crypto_secretbox_xchacha20poly1305_detached(unsigned char *c,
unsigned long long mlen,
const unsigned char *n,
const unsigned char *k)
__attribute__ ((nonnull));
__attribute__ ((nonnull(1, 2, 5, 6)));
SODIUM_EXPORT
int crypto_secretbox_xchacha20poly1305_open_detached(unsigned char *m,
@@ -36,7 +36,7 @@ int crypto_secretbox_xsalsa20poly1305(unsigned char *c,
unsigned long long mlen,
const unsigned char *n,
const unsigned char *k)
__attribute__ ((nonnull));
__attribute__ ((nonnull(1, 4, 5)));
SODIUM_EXPORT
int crypto_secretbox_xsalsa20poly1305_open(unsigned char *m,
@@ -28,7 +28,7 @@ const char *crypto_shorthash_primitive(void);
SODIUM_EXPORT
int crypto_shorthash(unsigned char *out, const unsigned char *in,
unsigned long long inlen, const unsigned char *k)
__attribute__ ((nonnull));
__attribute__ ((nonnull(1, 4)));
SODIUM_EXPORT
void crypto_shorthash_keygen(unsigned char k[crypto_shorthash_KEYBYTES])
@@ -24,7 +24,7 @@ size_t crypto_shorthash_siphash24_keybytes(void);
SODIUM_EXPORT
int crypto_shorthash_siphash24(unsigned char *out, const unsigned char *in,
unsigned long long inlen, const unsigned char *k)
__attribute__ ((nonnull));
__attribute__ ((nonnull(1, 4)));
#ifndef SODIUM_LIBRARY_MINIMAL
/* -- 128-bit output -- */
@@ -40,7 +40,7 @@ size_t crypto_shorthash_siphashx24_keybytes(void);
SODIUM_EXPORT
int crypto_shorthash_siphashx24(unsigned char *out, const unsigned char *in,
unsigned long long inlen, const unsigned char *k)
__attribute__ ((nonnull));
__attribute__ ((nonnull(1, 4)));
#endif
#ifdef __cplusplus
+4 -4
View File
@@ -61,7 +61,7 @@ int crypto_sign_keypair(unsigned char *pk, unsigned char *sk)
SODIUM_EXPORT
int crypto_sign(unsigned char *sm, unsigned long long *smlen_p,
const unsigned char *m, unsigned long long mlen,
const unsigned char *sk) __attribute__ ((nonnull(1, 3, 5)));
const unsigned char *sk) __attribute__ ((nonnull(1, 5)));
SODIUM_EXPORT
int crypto_sign_open(unsigned char *m, unsigned long long *mlen_p,
@@ -72,14 +72,14 @@ int crypto_sign_open(unsigned char *m, unsigned long long *mlen_p,
SODIUM_EXPORT
int crypto_sign_detached(unsigned char *sig, unsigned long long *siglen_p,
const unsigned char *m, unsigned long long mlen,
const unsigned char *sk) __attribute__ ((nonnull(1, 3, 5)));
const unsigned char *sk) __attribute__ ((nonnull(1, 5)));
SODIUM_EXPORT
int crypto_sign_verify_detached(const unsigned char *sig,
const unsigned char *m,
unsigned long long mlen,
const unsigned char *pk)
__attribute__ ((warn_unused_result)) __attribute__ ((nonnull));
__attribute__ ((warn_unused_result)) __attribute__ ((nonnull(1, 4)));
SODIUM_EXPORT
int crypto_sign_init(crypto_sign_state *state);
@@ -87,7 +87,7 @@ int crypto_sign_init(crypto_sign_state *state);
SODIUM_EXPORT
int crypto_sign_update(crypto_sign_state *state,
const unsigned char *m, unsigned long long mlen)
__attribute__ ((nonnull));
__attribute__ ((nonnull(1)));
SODIUM_EXPORT
int crypto_sign_final_create(crypto_sign_state *state, unsigned char *sig,
@@ -43,7 +43,7 @@ SODIUM_EXPORT
int crypto_sign_ed25519(unsigned char *sm, unsigned long long *smlen_p,
const unsigned char *m, unsigned long long mlen,
const unsigned char *sk)
__attribute__ ((nonnull(1, 3, 5)));
__attribute__ ((nonnull(1, 5)));
SODIUM_EXPORT
int crypto_sign_ed25519_open(unsigned char *m, unsigned long long *mlen_p,
@@ -57,14 +57,14 @@ int crypto_sign_ed25519_detached(unsigned char *sig,
const unsigned char *m,
unsigned long long mlen,
const unsigned char *sk)
__attribute__ ((nonnull(1, 3)));
__attribute__ ((nonnull(1, 5)));
SODIUM_EXPORT
int crypto_sign_ed25519_verify_detached(const unsigned char *sig,
const unsigned char *m,
unsigned long long mlen,
const unsigned char *pk)
__attribute__ ((warn_unused_result));
__attribute__ ((warn_unused_result)) __attribute__ ((nonnull(1, 4)));
SODIUM_EXPORT
int crypto_sign_ed25519_keypair(unsigned char *pk, unsigned char *sk)
@@ -102,14 +102,14 @@ SODIUM_EXPORT
int crypto_sign_ed25519ph_update(crypto_sign_ed25519ph_state *state,
const unsigned char *m,
unsigned long long mlen)
__attribute__ ((nonnull));
__attribute__ ((nonnull(1)));
SODIUM_EXPORT
int crypto_sign_ed25519ph_final_create(crypto_sign_ed25519ph_state *state,
unsigned char *sig,
unsigned long long *siglen_p,
const unsigned char *sk)
__attribute__ ((nonnull));
__attribute__ ((nonnull(1, 2, 4)));
SODIUM_EXPORT
int crypto_sign_ed25519ph_final_verify(crypto_sign_ed25519ph_state *state,
@@ -33,7 +33,7 @@ int crypto_sign_edwards25519sha512batch(unsigned char *sm,
const unsigned char *m,
unsigned long long mlen,
const unsigned char *sk)
__attribute__ ((deprecated)) __attribute__ ((nonnull(1, 3, 5)));
__attribute__ ((deprecated)) __attribute__ ((nonnull(1, 5)));
SODIUM_EXPORT
int crypto_sign_edwards25519sha512batch_open(unsigned char *m,
+9 -12
View File
@@ -19,7 +19,7 @@ extern "C" {
#endif
SODIUM_EXPORT
void sodium_memzero(void * const pnt, const size_t len) __attribute__ ((nonnull));
void sodium_memzero(void * const pnt, const size_t len);
SODIUM_EXPORT
void sodium_stackzero(const size_t len);
@@ -32,7 +32,7 @@ void sodium_stackzero(const size_t len);
*/
SODIUM_EXPORT
int sodium_memcmp(const void * const b1_, const void * const b2_, size_t len)
__attribute__ ((warn_unused_result)) __attribute__ ((nonnull));
__attribute__ ((warn_unused_result));
/*
* sodium_compare() returns -1 if b1_ < b2_, 1 if b1_ > b2_ and 0 if b1_ == b2_
@@ -42,8 +42,7 @@ int sodium_memcmp(const void * const b1_, const void * const b2_, size_t len)
*/
SODIUM_EXPORT
int sodium_compare(const unsigned char *b1_, const unsigned char *b2_,
size_t len)
__attribute__ ((warn_unused_result)) __attribute__ ((nonnull));
size_t len) __attribute__ ((warn_unused_result));
SODIUM_EXPORT
int sodium_is_zero(const unsigned char *n, const size_t nlen);
@@ -52,24 +51,22 @@ SODIUM_EXPORT
void sodium_increment(unsigned char *n, const size_t nlen);
SODIUM_EXPORT
void sodium_add(unsigned char *a, const unsigned char *b, const size_t len)
__attribute__ ((nonnull));
void sodium_add(unsigned char *a, const unsigned char *b, const size_t len);
SODIUM_EXPORT
void sodium_sub(unsigned char *a, const unsigned char *b, const size_t len)
__attribute__ ((nonnull));
void sodium_sub(unsigned char *a, const unsigned char *b, const size_t len);
SODIUM_EXPORT
char *sodium_bin2hex(char * const hex, const size_t hex_maxlen,
const unsigned char * const bin, const size_t bin_len)
__attribute__ ((nonnull));
__attribute__ ((nonnull(1)));
SODIUM_EXPORT
int sodium_hex2bin(unsigned char * const bin, const size_t bin_maxlen,
const char * const hex, const size_t hex_len,
const char * const ignore, size_t * const bin_len,
const char ** const hex_end)
__attribute__ ((nonnull(1, 3)));
__attribute__ ((nonnull(1)));
#define sodium_base64_VARIANT_ORIGINAL 1
#define sodium_base64_VARIANT_ORIGINAL_NO_PADDING 3
@@ -91,14 +88,14 @@ size_t sodium_base64_encoded_len(const size_t bin_len, const int variant);
SODIUM_EXPORT
char *sodium_bin2base64(char * const b64, const size_t b64_maxlen,
const unsigned char * const bin, const size_t bin_len,
const int variant) __attribute__ ((nonnull));
const int variant) __attribute__ ((nonnull(1)));
SODIUM_EXPORT
int sodium_base642bin(unsigned char * const bin, const size_t bin_maxlen,
const char * const b64, const size_t b64_len,
const char * const ignore, size_t * const bin_len,
const char ** const b64_end, const int variant)
__attribute__ ((nonnull(1, 3)));
__attribute__ ((nonnull(1)));
SODIUM_EXPORT
int sodium_mlock(void * const addr, const size_t len)
+5 -3
View File
@@ -100,7 +100,7 @@ _sodium_dummy_symbol_to_prevent_memzero_lto(void *const pnt,
/* LCOV_EXCL_STOP */
void
sodium_memzero(void *const pnt, const size_t len)
sodium_memzero(void * const pnt, const size_t len)
{
#ifdef _WIN32
SecureZeroMemory(pnt, len);
@@ -113,8 +113,10 @@ sodium_memzero(void *const pnt, const size_t len)
#elif defined(HAVE_EXPLICIT_MEMSET)
explicit_memset(pnt, 0, len);
#elif HAVE_WEAK_SYMBOLS
memset(pnt, 0, len);
_sodium_dummy_symbol_to_prevent_memzero_lto(pnt, len);
if (len > 0U) {
memset(pnt, 0, len);
_sodium_dummy_symbol_to_prevent_memzero_lto(pnt, len);
}
# ifdef HAVE_INLINE_ASM
__asm__ __volatile__ ("" : : "r"(pnt) : "memory");
# endif
+56
View File
@@ -14,12 +14,14 @@ static unsigned char key2[] =
static unsigned char a[crypto_auth_BYTES];
static unsigned char a2[crypto_auth_hmacsha512_BYTES];
static unsigned char a3[crypto_auth_hmacsha512_BYTES];
int
main(void)
{
crypto_auth_hmacsha512_state st;
crypto_auth_hmacsha256_state st256;
crypto_auth_hmacsha512256_state st512_256;
size_t i;
assert(crypto_auth_hmacsha512_statebytes() ==
@@ -65,6 +67,60 @@ main(void)
printf("\n");
}
/* Empty message tests: HMAC-SHA512 */
memset(a2, 0, sizeof a2);
crypto_auth_hmacsha512_init(&st, key, sizeof key);
crypto_auth_hmacsha512_final(&st, a2);
memset(a3, 0, sizeof a3);
crypto_auth_hmacsha512_init(&st, key, sizeof key);
crypto_auth_hmacsha512_update(&st, a2, 0U);
crypto_auth_hmacsha512_final(&st, a3);
assert(sodium_memcmp(a2, a3, sizeof a2) == 0);
memset(a3, 0, sizeof a3);
crypto_auth_hmacsha512_init(&st, key, sizeof key);
crypto_auth_hmacsha512_update(&st, guard_page, 0U);
crypto_auth_hmacsha512_final(&st, a3);
assert(sodium_memcmp(a2, a3, sizeof a2) == 0);
/* Empty message tests: HMAC-SHA512-256 */
memset(a2, 0, sizeof a2);
crypto_auth_hmacsha512256_init(&st512_256, key, sizeof key);
crypto_auth_hmacsha512256_final(&st512_256, a2);
memset(a3, 0, sizeof a3);
crypto_auth_hmacsha512256_init(&st512_256, key, sizeof key);
crypto_auth_hmacsha512256_update(&st512_256, a2, 0U);
crypto_auth_hmacsha512256_final(&st512_256, a3);
assert(sodium_memcmp(a2, a3, sizeof a2) == 0);
memset(a3, 0, sizeof a3);
crypto_auth_hmacsha512256_init(&st512_256, key, sizeof key);
crypto_auth_hmacsha512256_update(&st512_256, guard_page, 0U);
crypto_auth_hmacsha512256_final(&st512_256, a3);
assert(sodium_memcmp(a2, a3, sizeof a2) == 0);
/* Empty message tests: HMAC-SHA256 */
memset(a2, 0, sizeof a2);
crypto_auth_hmacsha256_init(&st256, key, sizeof key);
crypto_auth_hmacsha256_final(&st256, a2);
memset(a3, 0, sizeof a3);
crypto_auth_hmacsha256_init(&st256, key, sizeof key);
crypto_auth_hmacsha256_update(&st256, a2, 0U);
crypto_auth_hmacsha256_final(&st256, a3);
assert(sodium_memcmp(a2, a3, sizeof a2) == 0);
memset(a3, 0, sizeof a3);
crypto_auth_hmacsha256_init(&st256, key, sizeof key);
crypto_auth_hmacsha256_update(&st256, guard_page, 0U);
crypto_auth_hmacsha256_final(&st256, a3);
assert(sodium_memcmp(a2, a3, sizeof a2) == 0);
/* --- */
assert(crypto_auth_bytes() > 0U);
assert(crypto_auth_keybytes() > 0U);
assert(strcmp(crypto_auth_primitive(), "hmacsha512256") == 0);
+5
View File
@@ -25,7 +25,12 @@ static unsigned char a[32] = { 0x37, 0x2e, 0xfc, 0xf9, 0xb4, 0x0b, 0x35, 0xc2,
int
main(void)
{
static unsigned char a2[crypto_auth_hmacsha256_BYTES];
printf("%d\n", crypto_auth_hmacsha256_verify(a, c, sizeof c, key));
crypto_auth_hmacsha256(a2, guard_page, 0U, key);
assert(crypto_auth_hmacsha256_verify(a2, guard_page, 0U, key) == 0);
return 0;
}
+5
View File
@@ -32,5 +32,10 @@ main(void)
}
}
}
crypto_auth_keygen(key);
crypto_auth(a, guard_page, 0U, key);
assert(crypto_auth_verify(a, guard_page, 0U, key) == 0);
return 0;
}
+5
View File
@@ -32,5 +32,10 @@ main(void)
}
}
}
crypto_auth_keygen(key);
crypto_auth_hmacsha512(a, guard_page, 0U, key);
assert(crypto_auth_hmacsha512_verify(a, guard_page, 0U, key) == 0);
return 0;
}
+2 -1
View File
@@ -50,12 +50,13 @@ main(void)
/* Null message */
ret = crypto_box_easy(c, c, 0, nonce, bobpk, alicesk);
ret = crypto_box_easy(c, guard_page, 0, nonce, bobpk, alicesk);
assert(ret == 0);
for (i = 0; i < 1 + crypto_box_MACBYTES; ++i) {
printf(",0x%02x", (unsigned int) c[i]);
}
printf("\n");
ret =
crypto_box_open_easy(c, c, crypto_box_MACBYTES, nonce, bobpk, alicesk);
assert(ret == 0);
+4
View File
@@ -178,11 +178,15 @@ int main(void)
if (sodium_init() != 0) {
return 99;
}
# if defined(__EMSCRIPTEN__) || defined(__SANITIZE_ADDRESS__)
guard_page = _guard_page = NULL;
#else
if ((_guard_page = (unsigned char *) sodium_malloc(0)) == NULL) {
perror("sodium_malloc()");
return 99;
}
guard_page = _guard_page + 1;
#endif
if (xmain() != 0) {
return 99;
}
+20
View File
@@ -20,6 +20,11 @@ main(void)
printf("%s\n",
sodium_bin2hex(buf3, 33U, (const unsigned char *) "0123456789ABCDEF",
16U));
printf("bin2hex(..., guard_page, 0):%s\n",
sodium_bin2hex(buf3, sizeof buf3, guard_page, 0U));
printf("bin2hex(..., \"\", 0):%s\n",
sodium_bin2hex(buf3, sizeof buf3, (const unsigned char *) "", 0U));
hex = "Cafe : 6942";
sodium_hex2bin(buf4, sizeof buf4, hex, strlen(hex), ": ", &bin_len,
&hex_end);
@@ -79,6 +84,13 @@ main(void)
printf("sodium_hex2bin() with an extra character and no end pointer\n");
}
assert(sodium_hex2bin(buf4, sizeof buf4, (const char *) guard_page, 0U,
NULL, &bin_len, NULL) == 0);
assert(bin_len == 0);
assert(sodium_hex2bin(buf4, sizeof buf4, "", 0U, NULL, &bin_len, NULL) == 0);
assert(bin_len == 0);
printf("%s\n",
sodium_bin2base64(buf3, 31U, (const unsigned char *) "\xfb\xf0\xf1" "0123456789ABCDEFab",
21U, sodium_base64_VARIANT_ORIGINAL));
@@ -201,6 +213,14 @@ main(void)
assert(sodium_base642bin(buf1, sizeof buf1, "ka*w*=*", (size_t) 7U, "*~", NULL, NULL,
sodium_base64_VARIANT_ORIGINAL) == 0);
assert(sodium_base642bin(buf1, sizeof buf1, (const char *) guard_page, 0U,
NULL, &bin_len, NULL, sodium_base64_VARIANT_ORIGINAL) == 0);
assert(bin_len == 0);
assert(sodium_base642bin(buf1, sizeof buf1, "", 0U, NULL, &bin_len, NULL,
sodium_base64_VARIANT_ORIGINAL) == 0);
assert(bin_len == 0);
for (i = 0; i < 1000; i++) {
assert(sizeof buf1 >= 100);
bin_len = (size_t) randombytes_uniform(100);
+2
View File
@@ -1,4 +1,6 @@
30313233343536373839414243444546
bin2hex(..., guard_page, 0):
bin2hex(..., "", 0):
4:cafe6942
dt1: 11
4:cafe6942
+21
View File
@@ -29,6 +29,10 @@ main(void)
printf("%d\n", sodium_memcmp(buf1, buf2, 0U));
sodium_memzero(buf2, sizeof buf2 / 2);
printf("%d\n", sodium_memcmp(buf1, buf2, sizeof buf1));
printf("%d\n", sodium_memcmp(buf1, guard_page, 0U));
printf("%d\n", sodium_memcmp(guard_page, buf2, 0U));
printf("%d\n", sodium_memcmp(guard_page, guard_page, 0U));
sodium_memzero(guard_page, 0U);
memset(nonce, 0, sizeof nonce);
sodium_increment(nonce, sizeof nonce);
@@ -70,6 +74,8 @@ main(void)
(unsigned int) bin_len);
}
}
printf("%d\n", sodium_compare(buf1, NULL, 0U));
printf("%d\n", sodium_compare(NULL, buf1, 0U));
memset(buf1, 0, sizeof buf1);
if (sodium_is_zero(buf1, sizeof buf1) != 1) {
printf("sodium_is_zero() failed\n");
@@ -154,6 +160,21 @@ main(void)
sodium_add(nonce, nonce, 24U);
printf("%s\n",
sodium_bin2hex(nonce_hex, sizeof nonce_hex, nonce, sizeof nonce));
sodium_add(nonce, nonce, 0U);
printf("%s\n",
sodium_bin2hex(nonce_hex, sizeof nonce_hex, nonce, sizeof nonce));
sodium_add(nonce, guard_page, 0U);
printf("%s\n",
sodium_bin2hex(nonce_hex, sizeof nonce_hex, nonce, sizeof nonce));
sodium_add(guard_page, nonce, 0U);
sodium_sub(nonce, nonce, 0U);
printf("%s\n",
sodium_bin2hex(nonce_hex, sizeof nonce_hex, nonce, sizeof nonce));
sodium_sub(nonce, guard_page, 0U);
printf("%s\n",
sodium_bin2hex(nonce_hex, sizeof nonce_hex, nonce, sizeof nonce));
sodium_sub(guard_page, nonce, 0U);
randombytes_buf(buf1, 64U);
randombytes_buf(buf2, 64U);
+9
View File
@@ -3,14 +3,23 @@
-1
0
0
0
0
0
010000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000
010100000000000000000000000000000000000000000000
020000000000000000000000000000000000000000000000
0001ff000000000000000000000000000000000000000000
0
0
000000000000fffefefefefefefefefefefefefefefefefe
00000000000000000000fffefefefefefefefefefefefefe
00000000000000000000000000000000000000000000fffe
fcfffffffffffbfdfefefefefefefefefefefefefefefefe
fcfffffffffffffffffffbfdfefefefefefefefefefefefe
fcfffffffffffffffffffffffffffffffffffffffffffbfd
fcfffffffffffffffffffffffffffffffffffffffffffbfd
fcfffffffffffffffffffffffffffffffffffffffffffbfd
fcfffffffffffffffffffffffffffffffffffffffffffbfd
fcfffffffffffffffffffffffffffffffffffffffffffbfd