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