File: kdf.c

package info (click to toggle)
libsodium 1.0.18-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,480 kB
  • sloc: ansic: 45,158; asm: 4,264; makefile: 870; sh: 640; python: 405; xml: 30; pascal: 11
file content (71 lines) | stat: -rw-r--r-- 2,366 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71

#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;
    int            ret;

    context = (char *) sodium_malloc(crypto_kdf_CONTEXTBYTES);
    master_key = (unsigned char *) sodium_malloc(crypto_kdf_KEYBYTES);

    memcpy(context, "KDF test", sizeof "KDF test" -1U);
    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++) {
        ret = crypto_kdf_derive_from_key(subkey, crypto_kdf_BYTES_MAX,
                                         i, context, master_key);
        assert(ret == 0);
        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++) {
        subkey = (unsigned char *) sodium_malloc(crypto_kdf_BYTES_MAX);
        if (crypto_kdf_derive_from_key(subkey, (size_t) i,
                                       i, context, master_key) == 0) {
            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);
        }
        sodium_free(subkey);
    }

    sodium_free(master_key);
    sodium_free(context);

    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);
    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());

    printf("tv_kdf: ok\n");
}

int
main(void)
{
    tv_kdf();

    return 0;
}