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.
This commit is contained in:
Ilya Maykov
2019-02-09 20:26:10 +01:00
committed by Frank Denis
parent be1f8b4d67
commit 6934a8d0c8
34 changed files with 202 additions and 73 deletions
+21
View File
@@ -20,6 +20,11 @@ main(void)
printf("%s\n",
sodium_bin2hex(buf3, 33U, (const unsigned char *) "0123456789ABCDEF",
16U));
printf("bin2hex(..., NULL, 0):%s\n",
sodium_bin2hex(buf4, sizeof(buf4), NULL, 0U));
printf("bin2hex(..., \"\", 0):%s\n",
sodium_bin2hex(buf4, sizeof(buf4), (const unsigned char *) "", 0U));
hex = "Cafe : 6942";
sodium_hex2bin(buf4, sizeof buf4, hex, strlen(hex), ": ", &bin_len,
&hex_end);
@@ -79,6 +84,12 @@ main(void)
printf("sodium_hex2bin() with an extra character and no end pointer\n");
}
assert(sodium_hex2bin(buf4, sizeof(buf4), NULL, 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));
@@ -94,6 +105,9 @@ main(void)
printf("%s\n",
sodium_bin2base64(buf3, 1U, guard_page,
0U, sodium_base64_VARIANT_ORIGINAL));
printf("%s\n",
sodium_bin2base64(buf3, 1U, NULL,
0U, sodium_base64_VARIANT_ORIGINAL));
printf("%s\n",
sodium_bin2base64(buf3, 5U, (const unsigned char *) "a",
1U, sodium_base64_VARIANT_ORIGINAL));
@@ -201,6 +215,13 @@ 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, "", 0U, NULL, &bin_len, NULL,
sodium_base64_VARIANT_ORIGINAL) == 0);
assert(bin_len == 0);
assert(sodium_base642bin(buf1, sizeof buf1, NULL, 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);