kdf.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #define TEST_NAME "kdf"
  2. #include "cmptest.h"
  3. static void
  4. tv_kdf(void)
  5. {
  6. unsigned char *master_key;
  7. unsigned char *subkey;
  8. char *context;
  9. char hex[crypto_kdf_BYTES_MAX * 2 + 1];
  10. uint64_t i;
  11. context = (char *) sodium_malloc(crypto_kdf_CONTEXTBYTES);
  12. memcpy(context, "KDF test", strlen("KDF test"));
  13. master_key = (unsigned char *) sodium_malloc(crypto_kdf_KEYBYTES);
  14. for (i = 0; i < crypto_kdf_KEYBYTES; i++) {
  15. master_key[i] = i;
  16. }
  17. subkey = (unsigned char *) sodium_malloc(crypto_kdf_BYTES_MAX);
  18. for (i = 0; i < 10; i++) {
  19. assert(crypto_kdf_derive_from_key(subkey, crypto_kdf_BYTES_MAX,
  20. i, context, master_key) == 0);
  21. sodium_bin2hex(hex, sizeof hex, subkey, crypto_kdf_BYTES_MAX);
  22. printf("%s\n", hex);
  23. }
  24. sodium_free(subkey);
  25. for (i = 0; i < crypto_kdf_BYTES_MAX + 2; i++) {
  26. subkey = (unsigned char *) sodium_malloc(crypto_kdf_BYTES_MAX);
  27. if (crypto_kdf_derive_from_key(subkey, (size_t) i,
  28. i, context, master_key) == 0) {
  29. sodium_bin2hex(hex, sizeof hex, subkey, (size_t) i);
  30. printf("%s\n", hex);
  31. } else {
  32. printf("Failure -- probably expected for output length=%u\n",
  33. (unsigned int) i);
  34. }
  35. sodium_free(subkey);
  36. }
  37. assert(strcmp(crypto_kdf_primitive(), crypto_kdf_PRIMITIVE) == 0);
  38. assert(crypto_kdf_BYTES_MAX > 0);
  39. assert(crypto_kdf_BYTES_MIN <= crypto_kdf_BYTES_MAX);
  40. assert(crypto_kdf_bytes_min() == crypto_kdf_BYTES_MIN);
  41. assert(crypto_kdf_bytes_max() == crypto_kdf_BYTES_MAX);
  42. assert(crypto_kdf_CONTEXTBYTES > 0);
  43. assert(crypto_kdf_contextbytes() == crypto_kdf_CONTEXTBYTES);
  44. assert(crypto_kdf_KEYBYTES >= 16);
  45. assert(crypto_kdf_keybytes() == crypto_kdf_KEYBYTES);
  46. assert(crypto_kdf_bytes_min() == crypto_kdf_blake2b_bytes_min());
  47. assert(crypto_kdf_bytes_max() == crypto_kdf_blake2b_bytes_max());
  48. assert(crypto_kdf_contextbytes() == crypto_kdf_blake2b_contextbytes());
  49. assert(crypto_kdf_keybytes() == crypto_kdf_blake2b_keybytes());
  50. printf("tv_kdf: ok\n");
  51. }
  52. int
  53. main(void)
  54. {
  55. tv_kdf();
  56. return 0;
  57. }