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
+54
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,58 @@ 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, NULL, 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, NULL, 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, NULL, 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);
+6
View File
@@ -25,7 +25,13 @@ 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));
// Test empty message with NULL pointer
crypto_auth_hmacsha256(a2, NULL, 0U, key);
assert(crypto_auth_hmacsha256_verify(a2, NULL, 0U, key) == 0);
return 0;
}
+6
View File
@@ -32,5 +32,11 @@ main(void)
}
}
}
// Test empty message with NULL pointer
crypto_auth_keygen(key);
crypto_auth(a, NULL, 0U, key);
assert(crypto_auth_verify(a, NULL, 0U, key) == 0);
return 0;
}
+6
View File
@@ -32,5 +32,11 @@ main(void)
}
}
}
// Test empty message with NULL pointer
crypto_auth_keygen(key);
crypto_auth_hmacsha512(a, NULL, 0U, key);
assert(crypto_auth_hmacsha512_verify(a, NULL, 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, NULL, 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);
+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);
+3
View File
@@ -1,4 +1,6 @@
30313233343536373839414243444546
bin2hex(..., NULL, 0):
bin2hex(..., "", 0):
4:cafe6942
dt1: 11
4:cafe6942
@@ -12,6 +14,7 @@ dt6: 11
-_DxMDEyMzQ1Njc4OUFCQ0RFRmFi
-_DxMDEyMzQ1Njc4OUFCQ0RFRmFiYw
YQ==
YWI=
YWJj
+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, NULL, 0U));
printf("%d\n", sodium_memcmp(NULL, buf2, 0U));
printf("%d\n", sodium_memcmp(NULL, NULL, 0U));
sodium_memzero(NULL, 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, NULL, 0U);
printf("%s\n",
sodium_bin2hex(nonce_hex, sizeof nonce_hex, nonce, sizeof nonce));
sodium_add(NULL, nonce, 0U);
sodium_sub(nonce, nonce, 0U);
printf("%s\n",
sodium_bin2hex(nonce_hex, sizeof nonce_hex, nonce, sizeof nonce));
sodium_sub(nonce, NULL, 0U);
printf("%s\n",
sodium_bin2hex(nonce_hex, sizeof nonce_hex, nonce, sizeof nonce));
sodium_sub(NULL, 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