2017-02-19 10:13:10 -07:00
|
|
|
|
|
|
|
#define TEST_NAME "kdf"
|
|
|
|
#include "cmptest.h"
|
|
|
|
|
|
|
|
static void
|
|
|
|
tv_kdf(void)
|
|
|
|
{
|
|
|
|
unsigned char *master_key;
|
|
|
|
unsigned char *subkey;
|
|
|
|
char *context;
|
|
|
|
char hex[crypto_kdf_BYTES_MAX * 2 + 1];
|
|
|
|
uint64_t i;
|
2017-12-06 05:16:37 -07:00
|
|
|
int ret;
|
2017-02-19 10:13:10 -07:00
|
|
|
|
|
|
|
context = (char *) sodium_malloc(crypto_kdf_CONTEXTBYTES);
|
2017-02-19 13:17:42 -07:00
|
|
|
master_key = (unsigned char *) sodium_malloc(crypto_kdf_KEYBYTES);
|
2017-12-16 06:31:01 -07:00
|
|
|
|
2019-01-02 08:14:15 -07:00
|
|
|
memcpy(context, "KDF test", sizeof "KDF test" -1U);
|
2017-02-19 10:13:10 -07:00
|
|
|
for (i = 0; i < crypto_kdf_KEYBYTES; i++) {
|
|
|
|
master_key[i] = i;
|
|
|
|
}
|
|
|
|
subkey = (unsigned char *) sodium_malloc(crypto_kdf_BYTES_MAX);
|
|
|
|
for (i = 0; i < 10; i++) {
|
2017-12-06 05:16:37 -07:00
|
|
|
ret = crypto_kdf_derive_from_key(subkey, crypto_kdf_BYTES_MAX,
|
|
|
|
i, context, master_key);
|
|
|
|
assert(ret == 0);
|
2017-02-19 10:13:10 -07:00
|
|
|
sodium_bin2hex(hex, sizeof hex, subkey, crypto_kdf_BYTES_MAX);
|
|
|
|
printf("%s\n", hex);
|
|
|
|
}
|
|
|
|
sodium_free(subkey);
|
|
|
|
|
|
|
|
for (i = 0; i < crypto_kdf_BYTES_MAX + 2; i++) {
|
2017-02-19 13:17:42 -07:00
|
|
|
subkey = (unsigned char *) sodium_malloc(crypto_kdf_BYTES_MAX);
|
2017-07-17 14:09:44 -07:00
|
|
|
if (crypto_kdf_derive_from_key(subkey, (size_t) i,
|
|
|
|
i, context, master_key) == 0) {
|
2017-02-19 10:13:10 -07:00
|
|
|
sodium_bin2hex(hex, sizeof hex, subkey, (size_t) i);
|
|
|
|
printf("%s\n", hex);
|
|
|
|
} else {
|
|
|
|
printf("Failure -- probably expected for output length=%u\n",
|
|
|
|
(unsigned int) i);
|
|
|
|
}
|
2017-02-19 13:17:42 -07:00
|
|
|
sodium_free(subkey);
|
2017-02-19 10:13:10 -07:00
|
|
|
}
|
2017-02-26 13:30:40 -07:00
|
|
|
|
2017-12-16 06:31:01 -07:00
|
|
|
sodium_free(master_key);
|
|
|
|
sodium_free(context);
|
|
|
|
|
2017-02-26 13:30:40 -07:00
|
|
|
assert(strcmp(crypto_kdf_primitive(), crypto_kdf_PRIMITIVE) == 0);
|
|
|
|
assert(crypto_kdf_BYTES_MAX > 0);
|
|
|
|
assert(crypto_kdf_BYTES_MIN <= crypto_kdf_BYTES_MAX);
|
|
|
|
assert(crypto_kdf_bytes_min() == crypto_kdf_BYTES_MIN);
|
|
|
|
assert(crypto_kdf_bytes_max() == crypto_kdf_BYTES_MAX);
|
|
|
|
assert(crypto_kdf_CONTEXTBYTES > 0);
|
|
|
|
assert(crypto_kdf_contextbytes() == crypto_kdf_CONTEXTBYTES);
|
|
|
|
assert(crypto_kdf_KEYBYTES >= 16);
|
|
|
|
assert(crypto_kdf_keybytes() == crypto_kdf_KEYBYTES);
|
2017-07-17 14:09:44 -07:00
|
|
|
assert(crypto_kdf_bytes_min() == crypto_kdf_blake2b_bytes_min());
|
|
|
|
assert(crypto_kdf_bytes_max() == crypto_kdf_blake2b_bytes_max());
|
|
|
|
assert(crypto_kdf_contextbytes() == crypto_kdf_blake2b_contextbytes());
|
|
|
|
assert(crypto_kdf_keybytes() == crypto_kdf_blake2b_keybytes());
|
2017-02-26 13:30:40 -07:00
|
|
|
|
2017-02-19 13:17:42 -07:00
|
|
|
printf("tv_kdf: ok\n");
|
2017-02-19 10:13:10 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(void)
|
|
|
|
{
|
|
|
|
tv_kdf();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|