2013-04-25 22:37:15 -07:00
|
|
|
#define TEST_NAME "sodium_utils"
|
|
|
|
#include "cmptest.h"
|
|
|
|
|
2017-02-23 03:24:48 -07:00
|
|
|
int
|
|
|
|
main(void)
|
2013-04-25 22:37:15 -07:00
|
|
|
{
|
2017-08-09 06:56:58 -07:00
|
|
|
unsigned char buf_add[1000];
|
|
|
|
unsigned char buf1[1000];
|
|
|
|
unsigned char buf2[1000];
|
|
|
|
unsigned char buf1_rev[1000];
|
|
|
|
unsigned char buf2_rev[1000];
|
|
|
|
unsigned char nonce[24];
|
|
|
|
char nonce_hex[49];
|
2017-08-17 11:54:20 -07:00
|
|
|
unsigned char *bin_padded;
|
|
|
|
size_t bin_len, bin_len2;
|
|
|
|
size_t bin_padded_len;
|
|
|
|
size_t bin_padded_maxlen;
|
|
|
|
size_t blocksize;
|
2017-08-09 06:56:58 -07:00
|
|
|
unsigned int i;
|
|
|
|
unsigned int j;
|
2013-04-25 22:37:15 -07:00
|
|
|
|
2014-09-14 11:32:55 -07:00
|
|
|
randombytes_buf(buf1, sizeof buf1);
|
|
|
|
memcpy(buf2, buf1, sizeof buf2);
|
|
|
|
printf("%d\n", sodium_memcmp(buf1, buf2, sizeof buf1));
|
|
|
|
sodium_memzero(buf1, 0U);
|
|
|
|
printf("%d\n", sodium_memcmp(buf1, buf2, sizeof buf1));
|
|
|
|
sodium_memzero(buf1, sizeof buf1 / 2);
|
|
|
|
printf("%d\n", sodium_memcmp(buf1, buf2, sizeof buf1));
|
|
|
|
printf("%d\n", sodium_memcmp(buf1, buf2, 0U));
|
|
|
|
sodium_memzero(buf2, sizeof buf2 / 2);
|
|
|
|
printf("%d\n", sodium_memcmp(buf1, buf2, sizeof buf1));
|
2019-02-09 12:47:24 -07:00
|
|
|
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);
|
2017-08-09 06:56:58 -07:00
|
|
|
|
2015-06-22 04:53:35 -07:00
|
|
|
memset(nonce, 0, sizeof nonce);
|
|
|
|
sodium_increment(nonce, sizeof nonce);
|
2017-02-23 03:24:48 -07:00
|
|
|
printf("%s\n",
|
|
|
|
sodium_bin2hex(nonce_hex, sizeof nonce_hex, nonce, sizeof nonce));
|
2015-06-22 04:53:35 -07:00
|
|
|
memset(nonce, 255, sizeof nonce);
|
|
|
|
sodium_increment(nonce, sizeof nonce);
|
2017-02-23 03:24:48 -07:00
|
|
|
printf("%s\n",
|
|
|
|
sodium_bin2hex(nonce_hex, sizeof nonce_hex, nonce, sizeof nonce));
|
2015-06-22 04:53:35 -07:00
|
|
|
nonce[1] = 1U;
|
|
|
|
sodium_increment(nonce, sizeof nonce);
|
2017-02-23 03:24:48 -07:00
|
|
|
printf("%s\n",
|
|
|
|
sodium_bin2hex(nonce_hex, sizeof nonce_hex, nonce, sizeof nonce));
|
2015-06-22 04:53:35 -07:00
|
|
|
nonce[1] = 0U;
|
|
|
|
sodium_increment(nonce, sizeof nonce);
|
2017-02-23 03:24:48 -07:00
|
|
|
printf("%s\n",
|
|
|
|
sodium_bin2hex(nonce_hex, sizeof nonce_hex, nonce, sizeof nonce));
|
2015-06-22 04:53:35 -07:00
|
|
|
nonce[0] = 255U;
|
|
|
|
nonce[2] = 255U;
|
|
|
|
sodium_increment(nonce, sizeof nonce);
|
2017-02-23 03:24:48 -07:00
|
|
|
printf("%s\n",
|
|
|
|
sodium_bin2hex(nonce_hex, sizeof nonce_hex, nonce, sizeof nonce));
|
2015-10-31 14:36:54 -07:00
|
|
|
for (i = 0U; i < 1000U; i++) {
|
2015-10-17 12:25:30 -07:00
|
|
|
bin_len = (size_t) randombytes_uniform(sizeof buf1);
|
|
|
|
randombytes_buf(buf1, bin_len);
|
|
|
|
randombytes_buf(buf2, bin_len);
|
2015-10-31 14:36:54 -07:00
|
|
|
for (j = 0U; j < bin_len; j++) {
|
|
|
|
buf1_rev[bin_len - 1 - j] = buf1[j];
|
|
|
|
buf2_rev[bin_len - 1 - j] = buf2[j];
|
|
|
|
}
|
|
|
|
if (memcmp(buf1_rev, buf2_rev, bin_len) *
|
2019-01-02 07:32:59 -07:00
|
|
|
sodium_compare(buf1, buf2, bin_len) < 0) {
|
2015-10-17 12:32:25 -07:00
|
|
|
printf("sodium_compare() failure with length=%u\n",
|
|
|
|
(unsigned int) bin_len);
|
2015-10-17 12:25:30 -07:00
|
|
|
}
|
|
|
|
memcpy(buf1, buf2, bin_len);
|
|
|
|
if (sodium_compare(buf1, buf2, bin_len)) {
|
2015-10-17 12:32:25 -07:00
|
|
|
printf("sodium_compare() equality failure with length=%u\n",
|
|
|
|
(unsigned int) bin_len);
|
2015-10-17 12:25:30 -07:00
|
|
|
}
|
|
|
|
}
|
2019-02-02 14:47:31 -07:00
|
|
|
printf("%d\n", sodium_compare(buf1, NULL, 0U));
|
|
|
|
printf("%d\n", sodium_compare(NULL, buf1, 0U));
|
2015-11-12 17:48:34 -07:00
|
|
|
memset(buf1, 0, sizeof buf1);
|
|
|
|
if (sodium_is_zero(buf1, sizeof buf1) != 1) {
|
|
|
|
printf("sodium_is_zero() failed\n");
|
|
|
|
}
|
2015-11-16 15:22:01 -07:00
|
|
|
for (i = 0U; i < sizeof buf1; i++) {
|
|
|
|
buf1[i]++;
|
|
|
|
if (sodium_is_zero(buf1, sizeof buf1) != 0) {
|
|
|
|
printf("sodium_is_zero() failed\n");
|
|
|
|
}
|
|
|
|
buf1[i]--;
|
|
|
|
}
|
|
|
|
bin_len = randombytes_uniform(sizeof buf1);
|
|
|
|
randombytes_buf(buf1, bin_len);
|
|
|
|
memcpy(buf2, buf1, bin_len);
|
|
|
|
memset(buf_add, 0, bin_len);
|
|
|
|
j = randombytes_uniform(10000);
|
|
|
|
for (i = 0U; i < j; i++) {
|
|
|
|
sodium_increment(buf1, bin_len);
|
|
|
|
sodium_increment(buf_add, bin_len);
|
|
|
|
}
|
|
|
|
sodium_add(buf2, buf_add, bin_len);
|
|
|
|
if (sodium_compare(buf1, buf2, bin_len) != 0) {
|
|
|
|
printf("sodium_add() failed\n");
|
|
|
|
}
|
|
|
|
bin_len = randombytes_uniform(sizeof buf1);
|
|
|
|
randombytes_buf(buf1, bin_len);
|
|
|
|
memcpy(buf2, buf1, bin_len);
|
|
|
|
memset(buf_add, 0xff, bin_len);
|
|
|
|
sodium_increment(buf2, bin_len);
|
|
|
|
sodium_increment(buf2, 0U);
|
|
|
|
sodium_add(buf2, buf_add, bin_len);
|
|
|
|
sodium_add(buf2, buf_add, 0U);
|
|
|
|
if (sodium_compare(buf1, buf2, bin_len) != 0) {
|
|
|
|
printf("sodium_add() failed\n");
|
2015-11-12 17:48:34 -07:00
|
|
|
}
|
2019-01-02 07:32:59 -07:00
|
|
|
for (i = 0U; i < 1000U; i++) {
|
|
|
|
randombytes_buf(buf1, bin_len);
|
|
|
|
randombytes_buf(buf2, bin_len);
|
|
|
|
sodium_add(buf1, buf2, bin_len);
|
|
|
|
sodium_sub(buf1, buf2, bin_len);
|
|
|
|
sodium_sub(buf1, buf2, 0U);
|
|
|
|
if (sodium_is_zero(buf1, bin_len) &&
|
|
|
|
!sodium_is_zero(buf1, bin_len)) {
|
|
|
|
printf("sodium_sub() failed\n");
|
|
|
|
}
|
|
|
|
sodium_sub(buf1, buf1, bin_len);
|
|
|
|
if (!sodium_is_zero(buf1, bin_len)) {
|
|
|
|
printf("sodium_sub() failed\n");
|
|
|
|
}
|
|
|
|
}
|
2015-12-06 11:06:27 -07:00
|
|
|
assert(sizeof nonce >= 24U);
|
|
|
|
memset(nonce, 0xfe, 24U);
|
|
|
|
memset(nonce, 0xff, 6U);
|
|
|
|
sodium_increment(nonce, 8U);
|
2017-02-23 03:24:48 -07:00
|
|
|
printf("%s\n",
|
|
|
|
sodium_bin2hex(nonce_hex, sizeof nonce_hex, nonce, sizeof nonce));
|
2015-12-06 11:06:27 -07:00
|
|
|
memset(nonce, 0xfe, 24U);
|
|
|
|
memset(nonce, 0xff, 10U);
|
|
|
|
sodium_increment(nonce, 12U);
|
2017-02-23 03:24:48 -07:00
|
|
|
printf("%s\n",
|
|
|
|
sodium_bin2hex(nonce_hex, sizeof nonce_hex, nonce, sizeof nonce));
|
2015-12-06 11:06:27 -07:00
|
|
|
memset(nonce, 0xff, 22U);
|
|
|
|
sodium_increment(nonce, 24U);
|
2017-02-23 03:24:48 -07:00
|
|
|
printf("%s\n",
|
|
|
|
sodium_bin2hex(nonce_hex, sizeof nonce_hex, nonce, sizeof nonce));
|
2015-12-06 11:06:27 -07:00
|
|
|
|
|
|
|
assert(sizeof nonce >= 24U);
|
|
|
|
memset(nonce, 0xfe, 24U);
|
|
|
|
memset(nonce, 0xff, 6U);
|
|
|
|
sodium_add(nonce, nonce, 7U);
|
|
|
|
sodium_add(nonce, nonce, 8U);
|
2017-02-23 03:24:48 -07:00
|
|
|
printf("%s\n",
|
|
|
|
sodium_bin2hex(nonce_hex, sizeof nonce_hex, nonce, sizeof nonce));
|
2015-12-06 11:06:27 -07:00
|
|
|
memset(nonce, 0xfe, 24U);
|
|
|
|
memset(nonce, 0xff, 10U);
|
|
|
|
sodium_add(nonce, nonce, 11U);
|
|
|
|
sodium_add(nonce, nonce, 12U);
|
2017-02-23 03:24:48 -07:00
|
|
|
printf("%s\n",
|
|
|
|
sodium_bin2hex(nonce_hex, sizeof nonce_hex, nonce, sizeof nonce));
|
2015-12-06 11:06:27 -07:00
|
|
|
memset(nonce, 0xff, 22U);
|
|
|
|
sodium_add(nonce, nonce, 23U);
|
|
|
|
sodium_add(nonce, nonce, 24U);
|
2017-02-23 03:24:48 -07:00
|
|
|
printf("%s\n",
|
|
|
|
sodium_bin2hex(nonce_hex, sizeof nonce_hex, nonce, sizeof nonce));
|
2019-02-02 14:47:31 -07:00
|
|
|
sodium_add(nonce, nonce, 0U);
|
|
|
|
printf("%s\n",
|
|
|
|
sodium_bin2hex(nonce_hex, sizeof nonce_hex, nonce, sizeof nonce));
|
2019-02-09 12:47:24 -07:00
|
|
|
sodium_add(nonce, guard_page, 0U);
|
2019-02-02 14:47:31 -07:00
|
|
|
printf("%s\n",
|
|
|
|
sodium_bin2hex(nonce_hex, sizeof nonce_hex, nonce, sizeof nonce));
|
2019-02-09 12:47:24 -07:00
|
|
|
sodium_add(guard_page, nonce, 0U);
|
2019-02-02 14:47:31 -07:00
|
|
|
|
|
|
|
sodium_sub(nonce, nonce, 0U);
|
|
|
|
printf("%s\n",
|
|
|
|
sodium_bin2hex(nonce_hex, sizeof nonce_hex, nonce, sizeof nonce));
|
2019-02-09 12:47:24 -07:00
|
|
|
sodium_sub(nonce, guard_page, 0U);
|
2019-02-02 14:47:31 -07:00
|
|
|
printf("%s\n",
|
|
|
|
sodium_bin2hex(nonce_hex, sizeof nonce_hex, nonce, sizeof nonce));
|
2019-02-09 12:47:24 -07:00
|
|
|
sodium_sub(guard_page, nonce, 0U);
|
2015-12-06 11:06:27 -07:00
|
|
|
|
2019-01-05 13:17:48 -07:00
|
|
|
randombytes_buf(buf1, 64U);
|
|
|
|
randombytes_buf(buf2, 64U);
|
|
|
|
memset(buf_add, 0, 64U);
|
|
|
|
sodium_add(buf_add, buf1, 64U);
|
|
|
|
assert(!sodium_is_zero(buf_add, 64U));
|
|
|
|
sodium_add(buf_add, buf2, 64U);
|
|
|
|
assert(!sodium_is_zero(buf_add, 64U));
|
|
|
|
sodium_sub(buf_add, buf1, 64U);
|
|
|
|
assert(!sodium_is_zero(buf_add, 64U));
|
|
|
|
sodium_sub(buf_add, buf2, 64U);
|
|
|
|
assert(sodium_is_zero(buf_add, 64U));
|
|
|
|
|
2017-08-17 11:54:20 -07:00
|
|
|
for (i = 0; i < 2000U; i++) {
|
|
|
|
bin_len = randombytes_uniform(200U);
|
2018-08-27 02:42:49 -07:00
|
|
|
blocksize = 1U + randombytes_uniform(500U);
|
2017-08-17 11:54:20 -07:00
|
|
|
bin_padded_maxlen = bin_len + (blocksize - bin_len % blocksize);
|
2017-08-17 14:27:20 -07:00
|
|
|
bin_padded = (unsigned char *) sodium_malloc(bin_padded_maxlen);
|
2017-08-17 11:58:14 -07:00
|
|
|
randombytes_buf(bin_padded, bin_padded_maxlen);
|
2017-08-17 11:54:20 -07:00
|
|
|
|
|
|
|
assert(sodium_pad(&bin_padded_len, bin_padded, bin_len,
|
|
|
|
blocksize, bin_padded_maxlen - 1U) == -1);
|
2017-08-25 06:24:46 -07:00
|
|
|
assert(sodium_pad(NULL, bin_padded, bin_len,
|
|
|
|
blocksize, bin_padded_maxlen + 1U) == 0);
|
2017-08-17 11:54:20 -07:00
|
|
|
assert(sodium_pad(&bin_padded_len, bin_padded, bin_len,
|
|
|
|
blocksize, bin_padded_maxlen + 1U) == 0);
|
2017-08-17 12:00:02 -07:00
|
|
|
assert(sodium_pad(&bin_padded_len, bin_padded, bin_len,
|
|
|
|
0U, bin_padded_maxlen) == -1);
|
2017-08-17 11:54:20 -07:00
|
|
|
assert(sodium_pad(&bin_padded_len, bin_padded, bin_len,
|
|
|
|
blocksize, bin_padded_maxlen) == 0);
|
|
|
|
assert(bin_padded_len == bin_padded_maxlen);
|
|
|
|
|
|
|
|
assert(sodium_unpad(&bin_len2, bin_padded, bin_padded_len,
|
|
|
|
bin_padded_len + 1U) == -1);
|
2017-08-17 12:00:02 -07:00
|
|
|
assert(sodium_unpad(&bin_len2, bin_padded, bin_padded_len,
|
|
|
|
0U) == -1);
|
2017-08-17 11:54:20 -07:00
|
|
|
assert(sodium_unpad(&bin_len2, bin_padded, bin_padded_len,
|
|
|
|
blocksize) == 0);
|
|
|
|
assert(bin_len2 == bin_len);
|
|
|
|
|
|
|
|
sodium_free(bin_padded);
|
|
|
|
}
|
|
|
|
|
2017-12-05 10:49:58 -07:00
|
|
|
sodium_stackzero(512);
|
|
|
|
|
2014-09-14 11:32:55 -07:00
|
|
|
return 0;
|
2013-04-25 22:37:15 -07:00
|
|
|
}
|