2013-04-21 05:26:34 -07:00
|
|
|
|
|
|
|
#define TEST_NAME "generichash2"
|
|
|
|
#include "cmptest.h"
|
|
|
|
|
2015-10-17 15:55:10 -07:00
|
|
|
int
|
|
|
|
main(void)
|
2013-04-21 05:26:34 -07:00
|
|
|
{
|
|
|
|
#define MAXLEN 64
|
2017-12-21 09:53:09 -07:00
|
|
|
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;
|
2013-04-21 05:26:34 -07:00
|
|
|
|
2017-12-21 09:53:09 -07:00
|
|
|
assert(crypto_generichash_statebytes() >= sizeof *st);
|
2017-12-21 14:17:40 -07:00
|
|
|
st = (crypto_generichash_state *)
|
|
|
|
sodium_malloc(crypto_generichash_statebytes());
|
2017-12-21 09:53:09 -07:00
|
|
|
for (h = 0; h < crypto_generichash_KEYBYTES_MAX; ++h) {
|
2016-03-07 07:45:59 -07:00
|
|
|
k[h] = (unsigned char) h;
|
2017-12-21 09:53:09 -07:00
|
|
|
}
|
2014-09-14 11:32:55 -07:00
|
|
|
for (i = 0; i < MAXLEN; ++i) {
|
2016-03-07 07:45:59 -07:00
|
|
|
in[i] = (unsigned char) i;
|
2017-12-21 09:53:09 -07:00
|
|
|
if (crypto_generichash_init(st, k,
|
2014-09-16 21:41:33 -07:00
|
|
|
1 + i % crypto_generichash_KEYBYTES_MAX,
|
|
|
|
1 + i % crypto_generichash_BYTES_MAX) != 0) {
|
|
|
|
printf("crypto_generichash_init()\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2017-12-21 09:53:09 -07:00
|
|
|
crypto_generichash_update(st, in, i);
|
|
|
|
crypto_generichash_update(st, in, i);
|
|
|
|
crypto_generichash_update(st, in, i);
|
|
|
|
if (crypto_generichash_final(st, out,
|
2016-04-05 15:57:46 -07:00
|
|
|
1 + i % crypto_generichash_BYTES_MAX) != 0) {
|
|
|
|
printf("crypto_generichash_final() should have returned 0\n");
|
|
|
|
}
|
2014-09-14 11:32:55 -07:00
|
|
|
for (j = 0; j < 1 + i % crypto_generichash_BYTES_MAX; ++j) {
|
2017-12-21 09:53:09 -07:00
|
|
|
printf("%02x", (unsigned int) out[j]);
|
2013-04-21 05:26:34 -07:00
|
|
|
}
|
|
|
|
printf("\n");
|
2017-12-21 09:53:09 -07:00
|
|
|
if (crypto_generichash_final(st, out,
|
2016-04-05 15:57:46 -07:00
|
|
|
1 + i % crypto_generichash_BYTES_MAX) != -1) {
|
|
|
|
printf("crypto_generichash_final() should have returned -1\n");
|
|
|
|
}
|
2013-04-21 05:26:34 -07:00
|
|
|
}
|
2014-09-14 12:56:32 -07:00
|
|
|
|
2017-12-21 09:53:09 -07:00
|
|
|
assert(crypto_generichash_init(st, k, sizeof k, 0U) == -1);
|
|
|
|
assert(crypto_generichash_init(st, k, sizeof k,
|
2014-09-14 12:56:32 -07:00
|
|
|
crypto_generichash_BYTES_MAX + 1U) == -1);
|
2017-12-21 09:53:09 -07:00
|
|
|
assert(crypto_generichash_init(st, k, crypto_generichash_KEYBYTES_MAX + 1U,
|
2014-09-14 12:56:32 -07:00
|
|
|
sizeof out) == -1);
|
2017-12-21 09:53:09 -07:00
|
|
|
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,
|
2014-09-16 14:44:38 -07:00
|
|
|
1U) == 0);
|
2017-12-21 09:53:09 -07:00
|
|
|
assert(crypto_generichash_init(st, NULL, crypto_generichash_KEYBYTES,
|
2014-09-16 14:44:38 -07:00
|
|
|
0U) == -1);
|
2017-12-21 09:53:09 -07:00
|
|
|
|
|
|
|
sodium_free(st);
|
|
|
|
|
2013-04-21 05:26:34 -07:00
|
|
|
return 0;
|
|
|
|
}
|