2013-04-26 01:37:15 -04:00
|
|
|
#define TEST_NAME "sodium_utils"
|
|
|
|
#include "cmptest.h"
|
|
|
|
|
2017-02-23 05:24:48 -05:00
|
|
|
int
|
|
|
|
main(void)
|
2013-04-26 01:37:15 -04:00
|
|
|
{
|
2017-08-09 09:56:58 -04:00
|
|
|
unsigned char buf_add[1000];
|
|
|
|
unsigned char buf1[1000];
|
|
|
|
unsigned char buf2[1000];
|
|
|
|
unsigned char buf1_rev[1000];
|
|
|
|
unsigned char buf2_rev[1000];
|
|
|
|
char buf3[33];
|
|
|
|
unsigned char buf4[4];
|
|
|
|
unsigned char nonce[24];
|
|
|
|
char nonce_hex[49];
|
|
|
|
const char *b64;
|
|
|
|
char *b64_;
|
|
|
|
const char *b64_end;
|
|
|
|
unsigned char *bin;
|
2017-08-17 14:54:20 -04:00
|
|
|
unsigned char *bin_padded;
|
2017-08-09 09:56:58 -04:00
|
|
|
const char *hex;
|
|
|
|
const char *hex_end;
|
|
|
|
size_t b64_len;
|
2017-08-17 14:54:20 -04:00
|
|
|
size_t bin_len, bin_len2;
|
2017-08-09 09:56:58 -04:00
|
|
|
size_t hex_len;
|
2017-08-17 14:54:20 -04:00
|
|
|
size_t bin_padded_len;
|
|
|
|
size_t bin_padded_maxlen;
|
|
|
|
size_t blocksize;
|
2017-08-09 09:56:58 -04:00
|
|
|
unsigned int i;
|
|
|
|
unsigned int j;
|
2013-04-26 01:37:15 -04:00
|
|
|
|
2014-09-14 14:32:55 -04: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));
|
|
|
|
printf("%s\n",
|
2017-02-23 05:24:48 -05:00
|
|
|
sodium_bin2hex(buf3, 33U, (const unsigned char *) "0123456789ABCDEF",
|
2014-09-14 14:32:55 -04:00
|
|
|
16U));
|
|
|
|
hex = "Cafe : 6942";
|
2017-02-23 05:24:48 -05:00
|
|
|
sodium_hex2bin(buf4, sizeof buf4, hex, strlen(hex), ": ", &bin_len,
|
|
|
|
&hex_end);
|
|
|
|
printf("%lu:%02x%02x%02x%02x\n", (unsigned long) bin_len, buf4[0], buf4[1],
|
2014-09-14 14:32:55 -04:00
|
|
|
buf4[2], buf4[3]);
|
2014-09-16 23:46:43 -04:00
|
|
|
printf("dt1: %ld\n", (long) (hex_end - hex));
|
|
|
|
|
|
|
|
hex = "Cafe : 6942";
|
|
|
|
sodium_hex2bin(buf4, sizeof buf4, hex, strlen(hex), ": ", &bin_len, NULL);
|
2017-08-09 09:56:58 -04:00
|
|
|
printf("%lu:%02x%02x%02x%02x\n", (unsigned long) bin_len,
|
|
|
|
buf4[2], buf4[3], buf4[2], buf4[3]);
|
2014-09-16 23:46:43 -04:00
|
|
|
|
|
|
|
hex = "deadbeef";
|
|
|
|
if (sodium_hex2bin(buf1, 1U, hex, 8U, NULL, &bin_len, &hex_end) != -1) {
|
|
|
|
printf("sodium_hex2bin() overflow not detected\n");
|
|
|
|
}
|
|
|
|
printf("dt2: %ld\n", (long) (hex_end - hex));
|
|
|
|
|
|
|
|
hex = "de:ad:be:eff";
|
|
|
|
if (sodium_hex2bin(buf1, 4U, hex, 12U, ":", &bin_len, &hex_end) != -1) {
|
2017-02-23 05:24:48 -05:00
|
|
|
printf(
|
|
|
|
"sodium_hex2bin() with an odd input length and a short output "
|
|
|
|
"buffer\n");
|
2014-09-16 23:46:43 -04:00
|
|
|
}
|
|
|
|
printf("dt3: %ld\n", (long) (hex_end - hex));
|
|
|
|
|
|
|
|
hex = "de:ad:be:eff";
|
2017-02-23 05:24:48 -05:00
|
|
|
if (sodium_hex2bin(buf1, sizeof buf1, hex, 12U, ":", &bin_len, &hex_end) !=
|
|
|
|
0) {
|
2014-09-16 23:46:43 -04:00
|
|
|
printf("sodium_hex2bin() with an odd input length\n");
|
|
|
|
}
|
|
|
|
printf("dt4: %ld\n", (long) (hex_end - hex));
|
|
|
|
|
|
|
|
hex = "de:ad:be:eff";
|
2017-02-23 05:24:48 -05:00
|
|
|
if (sodium_hex2bin(buf1, sizeof buf1, hex, 13U, ":", &bin_len, &hex_end) !=
|
|
|
|
0) {
|
2014-09-16 23:46:43 -04:00
|
|
|
printf("sodium_hex2bin() with an odd input length\n");
|
|
|
|
}
|
|
|
|
printf("dt5: %ld\n", (long) (hex_end - hex));
|
2014-04-15 03:39:14 -04:00
|
|
|
|
2017-08-09 09:56:58 -04:00
|
|
|
printf("%s\n",
|
|
|
|
sodium_bin2base64(buf3, 31U, (const unsigned char *) "\xfb\xf0\xf1" "0123456789ABCDEFab",
|
|
|
|
21U, sodium_base64_VARIANT_ORIGINAL));
|
|
|
|
printf("%s\n",
|
|
|
|
sodium_bin2base64(buf3, 33U, (const unsigned char *) "\xfb\xf0\xf1" "0123456789ABCDEFabc",
|
|
|
|
22U, sodium_base64_VARIANT_ORIGINAL_NO_PADDING));
|
|
|
|
printf("%s\n",
|
|
|
|
sodium_bin2base64(buf3, 31U, (const unsigned char *) "\xfb\xf0\xf1" "0123456789ABCDEFab",
|
|
|
|
21U, sodium_base64_VARIANT_URLSAFE));
|
|
|
|
printf("%s\n",
|
|
|
|
sodium_bin2base64(buf3, 33U, (const unsigned char *) "\xfb\xf0\xf1" "0123456789ABCDEFabc",
|
|
|
|
22U, sodium_base64_VARIANT_URLSAFE_NO_PADDING));
|
|
|
|
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));
|
|
|
|
printf("%s\n",
|
|
|
|
sodium_bin2base64(buf3, 5U, (const unsigned char *) "ab",
|
|
|
|
2U, sodium_base64_VARIANT_ORIGINAL));
|
|
|
|
printf("%s\n",
|
|
|
|
sodium_bin2base64(buf3, 5U, (const unsigned char *) "abc",
|
|
|
|
3U, sodium_base64_VARIANT_ORIGINAL));
|
|
|
|
printf("%s\n",
|
|
|
|
sodium_bin2base64(buf3, 1U, NULL,
|
|
|
|
0U, sodium_base64_VARIANT_ORIGINAL_NO_PADDING));
|
|
|
|
printf("%s\n",
|
|
|
|
sodium_bin2base64(buf3, 3U, (const unsigned char *) "a",
|
|
|
|
1U, sodium_base64_VARIANT_ORIGINAL_NO_PADDING));
|
|
|
|
printf("%s\n",
|
|
|
|
sodium_bin2base64(buf3, 4U, (const unsigned char *) "ab",
|
|
|
|
2U, sodium_base64_VARIANT_ORIGINAL_NO_PADDING));
|
|
|
|
printf("%s\n",
|
|
|
|
sodium_bin2base64(buf3, 5U, (const unsigned char *) "abc",
|
|
|
|
3U, sodium_base64_VARIANT_ORIGINAL_NO_PADDING));
|
|
|
|
|
|
|
|
b64 = "VGhpcyBpcyBhIGpvdXJu" "\n" "ZXkgaW50by" " " "Bzb3VuZA==";
|
|
|
|
memset(buf4, '*', sizeof buf4);
|
2017-08-09 16:23:16 -04:00
|
|
|
assert(sodium_base642bin(buf4, sizeof buf4, b64, strlen(b64), "\n\r ", &bin_len,
|
|
|
|
&b64_end, sodium_base64_VARIANT_ORIGINAL) == -1);
|
2017-08-09 09:56:58 -04:00
|
|
|
buf4[bin_len] = 0;
|
|
|
|
printf("[%s]\n", (const char *) buf4);
|
|
|
|
printf("[%s]\n", b64_end);
|
|
|
|
|
|
|
|
memset(buf1, '*', sizeof buf1);
|
|
|
|
sodium_base642bin(buf1, sizeof buf1, b64, strlen(b64), "\n\r ", &bin_len,
|
|
|
|
&b64_end, sodium_base64_VARIANT_ORIGINAL);
|
|
|
|
buf1[bin_len] = 0;
|
|
|
|
printf("[%s]\n", (const char *) buf1);
|
|
|
|
assert(*b64_end == 0);
|
|
|
|
|
|
|
|
memset(buf1, '*', sizeof buf1);
|
|
|
|
sodium_base642bin(buf1, sizeof buf1, b64, strlen(b64), NULL, &bin_len,
|
|
|
|
&b64_end, sodium_base64_VARIANT_ORIGINAL);
|
|
|
|
buf1[bin_len] = 0;
|
|
|
|
printf("[%s]\n", (const char *) buf1);
|
|
|
|
printf("[%s]\n", b64_end);
|
|
|
|
|
|
|
|
assert(sodium_base642bin(buf1, sizeof buf1, b64, strlen(b64), NULL, NULL,
|
|
|
|
NULL, sodium_base64_VARIANT_ORIGINAL) == 0);
|
|
|
|
|
|
|
|
assert(sodium_base642bin(buf1, sizeof buf1, b64, strlen(b64), NULL, NULL,
|
|
|
|
NULL, sodium_base64_VARIANT_ORIGINAL_NO_PADDING) == 0);
|
|
|
|
assert(sodium_base642bin(buf1, sizeof buf1, b64, strlen(b64), " \r\n", NULL,
|
|
|
|
NULL, sodium_base64_VARIANT_ORIGINAL_NO_PADDING) == 0);
|
|
|
|
assert(sodium_base642bin(buf1, sizeof buf1, b64, strlen(b64), NULL, NULL,
|
|
|
|
NULL, sodium_base64_VARIANT_URLSAFE_NO_PADDING) == 0);
|
|
|
|
assert(sodium_base642bin(buf1, sizeof buf1, b64, strlen(b64), " \r\n", NULL,
|
|
|
|
NULL, sodium_base64_VARIANT_URLSAFE_NO_PADDING) == 0);
|
|
|
|
|
|
|
|
for (i = 0; i < 1000; i++) {
|
|
|
|
assert(sizeof buf1 >= 100);
|
|
|
|
bin_len = (size_t) randombytes_uniform(100);
|
2017-08-09 10:08:03 -04:00
|
|
|
bin = (unsigned char *) sodium_malloc(bin_len);
|
2017-08-09 09:56:58 -04:00
|
|
|
b64_len = 1 + (bin_len + 2) / 3 * 4;
|
2017-08-09 10:08:03 -04:00
|
|
|
b64_ = (char *) sodium_malloc(b64_len);
|
2017-08-09 09:56:58 -04:00
|
|
|
randombytes_buf(bin, bin_len);
|
|
|
|
memcpy(buf1, bin, bin_len);
|
2017-08-09 12:03:56 -04:00
|
|
|
b64 = sodium_bin2base64(b64_, b64_len + 10, bin, bin_len,
|
2017-08-09 09:56:58 -04:00
|
|
|
sodium_base64_VARIANT_URLSAFE);
|
|
|
|
assert(b64 != NULL);
|
2017-08-09 12:03:56 -04:00
|
|
|
assert(sodium_base642bin(bin, bin_len + 10, b64, b64_len,
|
2017-08-09 09:56:58 -04:00
|
|
|
NULL, NULL, &b64_end,
|
|
|
|
sodium_base64_VARIANT_URLSAFE) == 0);
|
|
|
|
assert(b64_end == &b64[b64_len - 1]);
|
|
|
|
assert(memcmp(bin, buf1, bin_len) == 0);
|
|
|
|
sodium_free(bin);
|
|
|
|
sodium_free(b64_);
|
|
|
|
}
|
|
|
|
|
2015-06-22 07:53:35 -04:00
|
|
|
memset(nonce, 0, sizeof nonce);
|
|
|
|
sodium_increment(nonce, sizeof nonce);
|
2017-02-23 05:24:48 -05:00
|
|
|
printf("%s\n",
|
|
|
|
sodium_bin2hex(nonce_hex, sizeof nonce_hex, nonce, sizeof nonce));
|
2015-06-22 07:53:35 -04:00
|
|
|
memset(nonce, 255, sizeof nonce);
|
|
|
|
sodium_increment(nonce, sizeof nonce);
|
2017-02-23 05:24:48 -05:00
|
|
|
printf("%s\n",
|
|
|
|
sodium_bin2hex(nonce_hex, sizeof nonce_hex, nonce, sizeof nonce));
|
2015-06-22 07:53:35 -04:00
|
|
|
nonce[1] = 1U;
|
|
|
|
sodium_increment(nonce, sizeof nonce);
|
2017-02-23 05:24:48 -05:00
|
|
|
printf("%s\n",
|
|
|
|
sodium_bin2hex(nonce_hex, sizeof nonce_hex, nonce, sizeof nonce));
|
2015-06-22 07:53:35 -04:00
|
|
|
nonce[1] = 0U;
|
|
|
|
sodium_increment(nonce, sizeof nonce);
|
2017-02-23 05:24:48 -05:00
|
|
|
printf("%s\n",
|
|
|
|
sodium_bin2hex(nonce_hex, sizeof nonce_hex, nonce, sizeof nonce));
|
2015-06-22 07:53:35 -04:00
|
|
|
nonce[0] = 255U;
|
|
|
|
nonce[2] = 255U;
|
|
|
|
sodium_increment(nonce, sizeof nonce);
|
2017-02-23 05:24:48 -05:00
|
|
|
printf("%s\n",
|
|
|
|
sodium_bin2hex(nonce_hex, sizeof nonce_hex, nonce, sizeof nonce));
|
2015-10-31 17:36:54 -04:00
|
|
|
for (i = 0U; i < 1000U; i++) {
|
2015-10-17 15:25:30 -04:00
|
|
|
bin_len = (size_t) randombytes_uniform(sizeof buf1);
|
|
|
|
randombytes_buf(buf1, bin_len);
|
|
|
|
randombytes_buf(buf2, bin_len);
|
2015-10-31 17:36:54 -04: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) *
|
2017-02-23 05:24:48 -05:00
|
|
|
sodium_compare(buf1, buf2, bin_len) <
|
|
|
|
0) {
|
2015-10-17 15:32:25 -04:00
|
|
|
printf("sodium_compare() failure with length=%u\n",
|
|
|
|
(unsigned int) bin_len);
|
2015-10-17 15:25:30 -04:00
|
|
|
}
|
|
|
|
memcpy(buf1, buf2, bin_len);
|
|
|
|
if (sodium_compare(buf1, buf2, bin_len)) {
|
2015-10-17 15:32:25 -04:00
|
|
|
printf("sodium_compare() equality failure with length=%u\n",
|
|
|
|
(unsigned int) bin_len);
|
2015-10-17 15:25:30 -04:00
|
|
|
}
|
|
|
|
}
|
2015-11-12 19:48:34 -05:00
|
|
|
memset(buf1, 0, sizeof buf1);
|
|
|
|
if (sodium_is_zero(buf1, sizeof buf1) != 1) {
|
|
|
|
printf("sodium_is_zero() failed\n");
|
|
|
|
}
|
2015-11-16 17:22:01 -05: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 19:48:34 -05:00
|
|
|
}
|
2015-12-06 13:06:27 -05:00
|
|
|
|
|
|
|
assert(sizeof nonce >= 24U);
|
|
|
|
memset(nonce, 0xfe, 24U);
|
|
|
|
memset(nonce, 0xff, 6U);
|
|
|
|
sodium_increment(nonce, 8U);
|
2017-02-23 05:24:48 -05:00
|
|
|
printf("%s\n",
|
|
|
|
sodium_bin2hex(nonce_hex, sizeof nonce_hex, nonce, sizeof nonce));
|
2015-12-06 13:06:27 -05:00
|
|
|
memset(nonce, 0xfe, 24U);
|
|
|
|
memset(nonce, 0xff, 10U);
|
|
|
|
sodium_increment(nonce, 12U);
|
2017-02-23 05:24:48 -05:00
|
|
|
printf("%s\n",
|
|
|
|
sodium_bin2hex(nonce_hex, sizeof nonce_hex, nonce, sizeof nonce));
|
2015-12-06 13:06:27 -05:00
|
|
|
memset(nonce, 0xff, 22U);
|
|
|
|
sodium_increment(nonce, 24U);
|
2017-02-23 05:24:48 -05:00
|
|
|
printf("%s\n",
|
|
|
|
sodium_bin2hex(nonce_hex, sizeof nonce_hex, nonce, sizeof nonce));
|
2015-12-06 13:06:27 -05: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 05:24:48 -05:00
|
|
|
printf("%s\n",
|
|
|
|
sodium_bin2hex(nonce_hex, sizeof nonce_hex, nonce, sizeof nonce));
|
2015-12-06 13:06:27 -05:00
|
|
|
memset(nonce, 0xfe, 24U);
|
|
|
|
memset(nonce, 0xff, 10U);
|
|
|
|
sodium_add(nonce, nonce, 11U);
|
|
|
|
sodium_add(nonce, nonce, 12U);
|
2017-02-23 05:24:48 -05:00
|
|
|
printf("%s\n",
|
|
|
|
sodium_bin2hex(nonce_hex, sizeof nonce_hex, nonce, sizeof nonce));
|
2015-12-06 13:06:27 -05:00
|
|
|
memset(nonce, 0xff, 22U);
|
|
|
|
sodium_add(nonce, nonce, 23U);
|
|
|
|
sodium_add(nonce, nonce, 24U);
|
2017-02-23 05:24:48 -05:00
|
|
|
printf("%s\n",
|
|
|
|
sodium_bin2hex(nonce_hex, sizeof nonce_hex, nonce, sizeof nonce));
|
2015-12-06 13:06:27 -05:00
|
|
|
|
2017-08-17 14:54:20 -04:00
|
|
|
for (i = 0; i < 2000U; i++) {
|
|
|
|
bin_len = randombytes_uniform(200U);
|
|
|
|
blocksize = 1U + randombytes_uniform(100U);
|
|
|
|
bin_padded_maxlen = bin_len + (blocksize - bin_len % blocksize);
|
2017-08-17 17:27:20 -04:00
|
|
|
bin_padded = (unsigned char *) sodium_malloc(bin_padded_maxlen);
|
2017-08-17 14:58:14 -04:00
|
|
|
randombytes_buf(bin_padded, bin_padded_maxlen);
|
2017-08-17 14:54:20 -04:00
|
|
|
|
|
|
|
assert(sodium_pad(&bin_padded_len, bin_padded, bin_len,
|
|
|
|
blocksize, bin_padded_maxlen - 1U) == -1);
|
|
|
|
assert(sodium_pad(&bin_padded_len, bin_padded, bin_len,
|
|
|
|
blocksize, bin_padded_maxlen + 1U) == 0);
|
2017-08-17 15:00:02 -04:00
|
|
|
assert(sodium_pad(&bin_padded_len, bin_padded, bin_len,
|
|
|
|
0U, bin_padded_maxlen) == -1);
|
2017-08-17 14:54:20 -04: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 15:00:02 -04:00
|
|
|
assert(sodium_unpad(&bin_len2, bin_padded, bin_padded_len,
|
|
|
|
0U) == -1);
|
2017-08-17 14:54:20 -04:00
|
|
|
assert(sodium_unpad(&bin_len2, bin_padded, bin_padded_len,
|
|
|
|
blocksize) == 0);
|
|
|
|
assert(bin_len2 == bin_len);
|
|
|
|
|
|
|
|
sodium_free(bin_padded);
|
|
|
|
}
|
|
|
|
|
2014-09-14 14:32:55 -04:00
|
|
|
return 0;
|
2013-04-26 01:37:15 -04:00
|
|
|
}
|