opnsense-src/sys/contrib/libsodium/test/default/generichash2.c
Conrad Meyer 0ac341f145 Bring in libsodium to sys/contrib
Bring in https://github.com/jedisct1/libsodium at
461ac93b260b91db8ad957f5a576860e3e9c88a1 (August 7, 2018), unmodified.

libsodium is derived from Daniel J. Bernstein et al.'s 2011 NaCl
("Networking and Cryptography Library," pronounced "salt") software library.
At the risk of oversimplifying, libsodium primarily exists to make it easier
to use NaCl.  NaCl and libsodium provide high quality implementations of a
number of useful cryptographic concepts (as well as the underlying
primitics) seeing some adoption in newer network protocols.

I considered but dismissed cleaning up the directory hierarchy and
discarding artifacts of other build systems in favor of remaining close to
upstream (and easing future updates).

Nothing is integrated into the build system yet, so in that sense, no
functional change.
2018-08-17 00:23:50 +00:00

62 lines
2.4 KiB
C

#define TEST_NAME "generichash2"
#include "cmptest.h"
int
main(void)
{
#define MAXLEN 64
crypto_generichash_state *st;
unsigned char in[MAXLEN];
unsigned char out[crypto_generichash_BYTES_MAX];
unsigned char k[crypto_generichash_KEYBYTES_MAX];
size_t h, i, j;
assert(crypto_generichash_statebytes() >= sizeof *st);
st = (crypto_generichash_state *)
sodium_malloc(crypto_generichash_statebytes());
for (h = 0; h < crypto_generichash_KEYBYTES_MAX; ++h) {
k[h] = (unsigned char) h;
}
for (i = 0; i < MAXLEN; ++i) {
in[i] = (unsigned char) i;
if (crypto_generichash_init(st, k,
1 + i % crypto_generichash_KEYBYTES_MAX,
1 + i % crypto_generichash_BYTES_MAX) != 0) {
printf("crypto_generichash_init()\n");
return 1;
}
crypto_generichash_update(st, in, i);
crypto_generichash_update(st, in, i);
crypto_generichash_update(st, in, i);
if (crypto_generichash_final(st, out,
1 + i % crypto_generichash_BYTES_MAX) != 0) {
printf("crypto_generichash_final() should have returned 0\n");
}
for (j = 0; j < 1 + i % crypto_generichash_BYTES_MAX; ++j) {
printf("%02x", (unsigned int) out[j]);
}
printf("\n");
if (crypto_generichash_final(st, out,
1 + i % crypto_generichash_BYTES_MAX) != -1) {
printf("crypto_generichash_final() should have returned -1\n");
}
}
assert(crypto_generichash_init(st, k, sizeof k, 0U) == -1);
assert(crypto_generichash_init(st, k, sizeof k,
crypto_generichash_BYTES_MAX + 1U) == -1);
assert(crypto_generichash_init(st, k, crypto_generichash_KEYBYTES_MAX + 1U,
sizeof out) == -1);
assert(crypto_generichash_init(st, k, 0U, sizeof out) == 0);
assert(crypto_generichash_init(st, k, 1U, sizeof out) == 0);
assert(crypto_generichash_init(st, NULL, 1U, 0U) == -1);
assert(crypto_generichash_init(st, NULL, crypto_generichash_KEYBYTES,
1U) == 0);
assert(crypto_generichash_init(st, NULL, crypto_generichash_KEYBYTES,
0U) == -1);
sodium_free(st);
return 0;
}