1
mirror of https://github.com/jedisct1/libsodium.git synced 2024-12-23 20:15:19 -07:00

Tests: use explicit casts when shortening types

This commit is contained in:
Frank Denis 2014-11-20 13:12:08 -08:00
parent 0d8bbcff20
commit d42634d466
6 changed files with 121 additions and 96 deletions

View File

@ -14,40 +14,45 @@ unsigned char mac[crypto_box_MACBYTES];
int main(void)
{
unsigned long long mlen;
unsigned long long i;
size_t mlen;
size_t i;
crypto_box_keypair(alicepk, alicesk);
crypto_box_keypair(bobpk, bobsk);
mlen = (unsigned long long)randombytes_uniform((uint32_t)sizeof m);
mlen = (size_t) randombytes_uniform((uint32_t)sizeof m);
randombytes_buf(m, mlen);
randombytes_buf(nonce, sizeof nonce);
crypto_box_easy(c, m, mlen, nonce, bobpk, alicesk);
if (crypto_box_open_easy(m2, c, mlen + crypto_box_MACBYTES, nonce, alicepk,
bobsk) != 0) {
if (crypto_box_open_easy(m2, c,
(unsigned long long) mlen + crypto_box_MACBYTES,
nonce, alicepk, bobsk) != 0) {
printf("open() failed");
return 1;
}
printf("%d\n", memcmp(m, m2, mlen));
for (i = 0; i < mlen + crypto_box_MACBYTES - 1; i++) {
if (crypto_box_open_easy(m2, c, i, nonce, alicepk, bobsk) == 0) {
if (crypto_box_open_easy(m2, c, (unsigned long long) i,
nonce, alicepk, bobsk) == 0) {
printf("short open() should have failed");
return 1;
}
}
memcpy(c, m, mlen);
crypto_box_easy(c, c, mlen, nonce, bobpk, alicesk);
crypto_box_easy(c, c, (unsigned long long) mlen, nonce, bobpk, alicesk);
printf("%d\n", memcmp(m, c, mlen) == 0);
printf("%d\n", memcmp(m, c + crypto_box_MACBYTES, mlen) == 0);
if (crypto_box_open_easy(c, c, mlen + crypto_box_MACBYTES, nonce, alicepk,
bobsk) != 0) {
if (crypto_box_open_easy(c, c,
(unsigned long long) mlen + crypto_box_MACBYTES,
nonce, alicepk, bobsk) != 0) {
printf("crypto_box_open_easy() failed\n");
}
crypto_box_detached(c, mac, m, mlen, nonce, bobsk, alicepk);
crypto_box_open_detached(m2, c, mac, mlen, nonce, alicepk, bobsk);
crypto_box_detached(c, mac, m, (unsigned long long) mlen,
nonce, bobsk, alicepk);
crypto_box_open_detached(m2, c, mac, (unsigned long long) mlen,
nonce, alicepk, bobsk);
printf("%d\n", memcmp(m, m2, mlen));
return 0;

View File

@ -2,43 +2,51 @@
#define TEST_NAME "generichash"
#include "cmptest.h"
#define MAXLEN 64
int main(void)
{
#define MAXLEN 64
unsigned char in[MAXLEN], out[crypto_generichash_BYTES_MAX],
k[crypto_generichash_KEYBYTES_MAX];
size_t h, i, j;
unsigned char in[MAXLEN];
unsigned char out[crypto_generichash_BYTES_MAX];
unsigned char k[crypto_generichash_KEYBYTES_MAX];
size_t h;
size_t i;
size_t j;
for (h = 0; h < crypto_generichash_KEYBYTES_MAX; ++h)
k[h] = h;
for (h = 0; h < crypto_generichash_KEYBYTES_MAX; ++h) {
k[h] = (unsigned char) h;
}
for (i = 0; i < MAXLEN; ++i) {
in[i] = i;
crypto_generichash(out, 1 + i % crypto_generichash_BYTES_MAX, in, i, k,
in[i] = (unsigned char) i;
crypto_generichash(out, 1 + i % crypto_generichash_BYTES_MAX, in,
(unsigned long long) i, k,
1 + i % crypto_generichash_KEYBYTES_MAX);
for (j = 0; j < 1 + i % crypto_generichash_BYTES_MAX; ++j) {
printf("%02x", (unsigned int)out[j]);
printf("%02x", (unsigned int) out[j]);
}
printf("\n");
}
memset(out, 0, sizeof out);
crypto_generichash(out, crypto_generichash_BYTES_MAX, in, i, k, 0U);
crypto_generichash(out, crypto_generichash_BYTES_MAX, in,
(unsigned long long) i, k, 0U);
for (j = 0; j < crypto_generichash_BYTES_MAX; ++j) {
printf("%02x", (unsigned int)out[j]);
printf("%02x", (unsigned int) out[j]);
}
printf("\n");
memset(out, 0, sizeof out);
crypto_generichash(out, crypto_generichash_BYTES_MAX, in, i, NULL, 1U);
crypto_generichash(out, crypto_generichash_BYTES_MAX, in,
(unsigned long long) i, NULL, 1U);
for (j = 0; j < crypto_generichash_BYTES_MAX; ++j) {
printf("%02x", (unsigned int)out[j]);
printf("%02x", (unsigned int) out[j]);
}
printf("\n");
assert(crypto_generichash(out, 0U, in, sizeof in, k, sizeof k) == -1);
assert(crypto_generichash(out, crypto_generichash_BYTES_MAX + 1U, in, sizeof in,
k, sizeof k) == -1);
assert(crypto_generichash(out, crypto_generichash_BYTES_MAX + 1U,
in, sizeof in, k, sizeof k) == -1);
assert(crypto_generichash(out, sizeof out, in, sizeof in,
k, crypto_generichash_KEYBYTES_MAX + 1U) == -1);

View File

@ -12,23 +12,27 @@ int main(void)
unsigned char personal[crypto_generichash_blake2b_PERSONALBYTES]
= { '5', '1', '2', '6', 'f', 'b', '2', 'a',
'3', '7', '4', '0', '0', 'd', '2', 'a' };
unsigned char in[MAXLEN], out[crypto_generichash_blake2b_BYTES_MAX],
k[crypto_generichash_blake2b_KEYBYTES_MAX];
size_t h, i, j;
unsigned char in[MAXLEN];
unsigned char out[crypto_generichash_blake2b_BYTES_MAX];
unsigned char k[crypto_generichash_blake2b_KEYBYTES_MAX];
size_t h;
size_t i;
size_t j;
for (h = 0; h < crypto_generichash_blake2b_KEYBYTES_MAX; ++h)
k[h] = h;
for (h = 0; h < crypto_generichash_blake2b_KEYBYTES_MAX; ++h) {
k[h] = (unsigned char) h;
}
for (i = 0; i < MAXLEN; ++i) {
in[i] = i;
in[i] = (unsigned char) i;
crypto_generichash_blake2b_init_salt_personal(
&st, k, 1 + i % crypto_generichash_blake2b_KEYBYTES_MAX,
1 + i % crypto_generichash_blake2b_BYTES_MAX, salt, personal);
crypto_generichash_blake2b_update(&st, in, i);
crypto_generichash_blake2b_update(&st, in, (unsigned long long) i);
crypto_generichash_blake2b_final(
&st, out, 1 + i % crypto_generichash_blake2b_BYTES_MAX);
for (j = 0; j < 1 + i % crypto_generichash_blake2b_BYTES_MAX; ++j) {
printf("%02x", (unsigned int)out[j]);
printf("%02x", (unsigned int) out[j]);
}
printf("\n");
}
@ -40,7 +44,7 @@ int main(void)
crypto_generichash_blake2b_final(&st, out,
crypto_generichash_blake2b_BYTES_MAX);
for (j = 0; j < crypto_generichash_blake2b_BYTES_MAX; ++j) {
printf("%02x", (unsigned int)out[j]);
printf("%02x", (unsigned int) out[j]);
}
printf("\n");
@ -51,7 +55,7 @@ int main(void)
crypto_generichash_blake2b_final(&st, out,
crypto_generichash_blake2b_BYTES_MAX);
for (j = 0; j < crypto_generichash_blake2b_BYTES_MAX; ++j) {
printf("%02x", (unsigned int)out[j]);
printf("%02x", (unsigned int) out[j]);
}
printf("\n");
@ -63,7 +67,7 @@ int main(void)
crypto_generichash_blake2b_final(&st, out,
crypto_generichash_blake2b_BYTES_MAX);
for (j = 0; j < crypto_generichash_blake2b_BYTES_MAX; ++j) {
printf("%02x", (unsigned int)out[j]);
printf("%02x", (unsigned int) out[j]);
}
printf("\n");
@ -77,7 +81,7 @@ int main(void)
crypto_generichash_blake2b_final(
&st, out, crypto_generichash_blake2b_BYTES_MAX);
for (j = 0; j < crypto_generichash_blake2b_BYTES_MAX; ++j) {
printf("%02x", (unsigned int)out[j]);
printf("%02x", (unsigned int) out[j]);
}
printf("\n");
@ -86,7 +90,7 @@ int main(void)
out, crypto_generichash_blake2b_BYTES_MAX, in, MAXLEN,
k, 0U, salt, personal);
for (j = 0; j < crypto_generichash_blake2b_BYTES_MAX; ++j) {
printf("%02x", (unsigned int)out[j]);
printf("%02x", (unsigned int) out[j]);
}
printf("\n");
@ -95,7 +99,7 @@ int main(void)
out, crypto_generichash_blake2b_BYTES_MAX, in, MAXLEN,
NULL, crypto_generichash_blake2b_KEYBYTES_MAX, salt, personal);
for (j = 0; j < crypto_generichash_blake2b_BYTES_MAX; ++j) {
printf("%02x", (unsigned int)out[j]);
printf("%02x", (unsigned int) out[j]);
}
printf("\n");
@ -104,7 +108,7 @@ int main(void)
out, crypto_generichash_blake2b_BYTES_MAX, in, MAXLEN,
k, crypto_generichash_blake2b_KEYBYTES_MAX, salt, personal);
for (j = 0; j < crypto_generichash_blake2b_BYTES_MAX; ++j) {
printf("%02x", (unsigned int)out[j]);
printf("%02x", (unsigned int) out[j]);
}
printf("\n");
@ -113,7 +117,7 @@ int main(void)
out, crypto_generichash_blake2b_BYTES_MAX, in, MAXLEN,
k, crypto_generichash_blake2b_KEYBYTES_MAX, NULL, personal);
for (j = 0; j < crypto_generichash_blake2b_BYTES_MAX; ++j) {
printf("%02x", (unsigned int)out[j]);
printf("%02x", (unsigned int) out[j]);
}
printf("\n");
@ -122,7 +126,7 @@ int main(void)
out, crypto_generichash_blake2b_BYTES_MAX, in, MAXLEN,
k, crypto_generichash_blake2b_KEYBYTES_MAX, salt, NULL);
for (j = 0; j < crypto_generichash_blake2b_BYTES_MAX; ++j) {
printf("%02x", (unsigned int)out[j]);
printf("%02x", (unsigned int) out[j]);
}
printf("\n");
@ -131,7 +135,7 @@ int main(void)
crypto_generichash_blake2b_update(&st, in, MAXLEN);
crypto_generichash_blake2b_final(&st, out, crypto_generichash_blake2b_BYTES_MAX);
for (j = 0; j < crypto_generichash_blake2b_BYTES_MAX; ++j) {
printf("%02x", (unsigned int)out[j]);
printf("%02x", (unsigned int) out[j]);
}
printf("\n");
@ -140,7 +144,7 @@ int main(void)
crypto_generichash_blake2b_update(&st, in, MAXLEN);
crypto_generichash_blake2b_final(&st, out, crypto_generichash_blake2b_BYTES_MAX);
for (j = 0; j < crypto_generichash_blake2b_BYTES_MAX; ++j) {
printf("%02x", (unsigned int)out[j]);
printf("%02x", (unsigned int) out[j]);
}
printf("\n");

View File

@ -5,12 +5,12 @@
static void tv(void)
{
static struct {
const char *passwd_hex;
unsigned long long passwdlen;
const char *salt_hex;
unsigned long long outlen;
unsigned long long opslimit;
size_t memlimit;
const char *passwd_hex;
size_t passwdlen;
const char *salt_hex;
size_t outlen;
unsigned long long opslimit;
size_t memlimit;
} tests[] = {
{ "a347ae92bce9f80f6f595a4480fc9c2fe7e7d7148d371e9487d75f5c23008ffae0"
"65577a928febd9b1973a5a95073acdbeb6a030cfc0d79caa2dc5cd011cef02c08d"
@ -85,21 +85,22 @@ static void tv(void)
"3d968b2752b8838431165059319f3ff8910b7b8ecb54ea01d3f54769e9d98daf",
167, 717248, 10784179 },
};
char passwd[256];
char passwd[256];
unsigned char salt[crypto_pwhash_scryptsalsa208sha256_SALTBYTES];
unsigned char out[256];
char out_hex[256 * 2 + 1];
size_t i = 0U;
char out_hex[256 * 2 + 1];
size_t i = 0U;
do {
sodium_hex2bin((unsigned char *)passwd, sizeof passwd,
sodium_hex2bin((unsigned char *) passwd, sizeof passwd,
tests[i].passwd_hex, strlen(tests[i].passwd_hex), NULL,
NULL, NULL);
sodium_hex2bin(salt, sizeof salt, tests[i].salt_hex,
strlen(tests[i].salt_hex), NULL, NULL, NULL);
if (crypto_pwhash_scryptsalsa208sha256(
out, tests[i].outlen, passwd, tests[i].passwdlen,
(const unsigned char *)salt, tests[i].opslimit,
out, (unsigned long long) tests[i].outlen,
passwd, tests[i].passwdlen,
(const unsigned char *) salt, tests[i].opslimit,
tests[i].memlimit) != 0) {
printf("pwhash failure\n");
}
@ -111,12 +112,12 @@ static void tv(void)
static void tv2(void)
{
static struct {
const char *passwd_hex;
unsigned long long passwdlen;
const char *salt_hex;
unsigned long long outlen;
unsigned long long opslimit;
size_t memlimit;
const char *passwd_hex;
size_t passwdlen;
const char *salt_hex;
size_t outlen;
unsigned long long opslimit;
size_t memlimit;
} tests[] = {
{ "a347ae92bce9f80f6f595a4480fc9c2fe7e7d7148d371e9487d75f5c23008ffae0"
"65577a928febd9b1973a5a95073acdbeb6a030cfc0d79caa2dc5cd011cef02c08d"
@ -133,11 +134,11 @@ static void tv2(void)
"5541fbc995d5c197ba290346d2c559dedf405cf97e5f95482143202f9e74f5c2",
155, 32768, 1397645 },
};
char passwd[256];
char passwd[256];
unsigned char salt[crypto_pwhash_scryptsalsa208sha256_SALTBYTES];
unsigned char out[256];
char out_hex[256 * 2 + 1];
size_t i = 0U;
char out_hex[256 * 2 + 1];
size_t i = 0U;
do {
sodium_hex2bin((unsigned char *)passwd, sizeof passwd,
@ -146,8 +147,9 @@ static void tv2(void)
sodium_hex2bin(salt, sizeof salt, tests[i].salt_hex,
strlen(tests[i].salt_hex), NULL, NULL, NULL);
if (crypto_pwhash_scryptsalsa208sha256(
out, tests[i].outlen, passwd, tests[i].passwdlen,
(const unsigned char *)salt, tests[i].opslimit,
out, (unsigned long long) tests[i].outlen,
passwd, tests[i].passwdlen,
(const unsigned char *) salt, tests[i].opslimit,
tests[i].memlimit) != 0) {
printf("pwhash failure\n");
}
@ -283,13 +285,10 @@ static void tv3(void)
int main(void)
{
char str_out[crypto_pwhash_scryptsalsa208sha256_STRBYTES];
char str_out2[crypto_pwhash_scryptsalsa208sha256_STRBYTES];
unsigned char out[OUT_LEN];
char out_hex[OUT_LEN * 2 + 1];
char str_out[crypto_pwhash_scryptsalsa208sha256_STRBYTES];
char str_out2[crypto_pwhash_scryptsalsa208sha256_STRBYTES];
const char *salt = "[<~A 32-bytes salt for scrypt~>]";
const char *passwd = "Correct Horse Battery Staple";
size_t i;
tv();
tv2();

View File

@ -11,36 +11,40 @@ unsigned char mac[crypto_secretbox_MACBYTES];
int main(void)
{
unsigned long long mlen;
unsigned long long i;
size_t mlen;
size_t i;
randombytes_buf(k, sizeof k);
mlen = (unsigned long long)randombytes_uniform((uint32_t)sizeof m);
randombytes_buf(m, mlen);
mlen = (size_t) randombytes_uniform((uint32_t) sizeof m);
randombytes_buf(m, (unsigned long long) mlen);
randombytes_buf(nonce, sizeof nonce);
crypto_secretbox_easy(c, m, mlen, nonce, k);
crypto_secretbox_open_easy(m2, c, mlen + crypto_secretbox_MACBYTES, nonce,
k);
crypto_secretbox_easy(c, m, (unsigned long long) mlen, nonce, k);
crypto_secretbox_open_easy(m2, c,
(unsigned long long) mlen + crypto_secretbox_MACBYTES,
nonce, k);
printf("%d\n", memcmp(m, m2, mlen));
for (i = 0; i < mlen + crypto_secretbox_MACBYTES - 1; i++) {
if (crypto_secretbox_open_easy(m2, c, i, nonce, k) == 0) {
if (crypto_secretbox_open_easy(m2, c, (unsigned long long) i,
nonce, k) == 0) {
printf("short open() should have failed\n");
return 1;
}
}
crypto_secretbox_detached(c, mac, m, mlen, nonce, k);
if (crypto_secretbox_open_detached(m2, c, mac, mlen, nonce, k) != 0) {
crypto_secretbox_detached(c, mac, m, (unsigned long long) mlen, nonce, k);
if (crypto_secretbox_open_detached(m2, c, mac, (unsigned long long) mlen,
nonce, k) != 0) {
printf("crypto_secretbox_open_detached() failed\n");
}
printf("%d\n", memcmp(m, m2, mlen));
memcpy(c, m, mlen);
crypto_secretbox_easy(c, c, mlen, nonce, k);
crypto_secretbox_easy(c, c, (unsigned long long) mlen, nonce, k);
printf("%d\n", memcmp(m, c, mlen) == 0);
printf("%d\n", memcmp(m, c + crypto_secretbox_MACBYTES, mlen) == 0);
if (crypto_secretbox_open_easy(c, c, mlen + crypto_secretbox_MACBYTES, nonce,
k) != 0) {
if (crypto_secretbox_open_easy(c, c,
(unsigned long long) mlen + crypto_secretbox_MACBYTES,
nonce, k) != 0) {
printf("crypto_secretbox_open_easy() failed\n");
}
printf("%d\n", memcmp(m, c, mlen));

View File

@ -2,21 +2,26 @@
#define TEST_NAME "shorthash"
#include "cmptest.h"
#define MAXLEN 64
int main(void)
{
#define MAXLEN 64
unsigned char in[MAXLEN], out[crypto_shorthash_BYTES],
k[crypto_shorthash_KEYBYTES];
size_t i, j;
unsigned char in[MAXLEN];
unsigned char out[crypto_shorthash_BYTES];
unsigned char k[crypto_shorthash_KEYBYTES];
size_t i;
size_t j;
for (i = 0; i < crypto_shorthash_KEYBYTES; ++i)
k[i] = i;
for (i = 0; i < crypto_shorthash_KEYBYTES; ++i) {
k[i] = (unsigned char) i;
}
for (i = 0; i < MAXLEN; ++i) {
in[i] = i;
crypto_shorthash(out, in, i, k);
for (j = 0; j < crypto_shorthash_BYTES; ++j)
printf("%02x", (unsigned int)out[j]);
in[i] = (unsigned char) i;
crypto_shorthash(out, in, (unsigned long long) i, k);
for (j = 0; j < crypto_shorthash_BYTES; ++j) {
printf("%02x", (unsigned int) out[j]);
}
printf("\n");
}
assert(crypto_shorthash_bytes() > 0);