summaryrefslogtreecommitdiff
path: root/libs/libsodium/src/crypto_kdf/blake2b/kdf_blake2b.c
blob: 951db121549c0c271b9b59ae506ca30eee03f394 (plain)
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
#include <errno.h>

#include "crypto_kdf_blake2b.h"
#include "crypto_generichash_blake2b.h"
#include "private/common.h"

size_t
crypto_kdf_blake2b_bytes_min(void)
{
    return crypto_kdf_blake2b_BYTES_MIN;
}

size_t
crypto_kdf_blake2b_bytes_max(void)
{
    return crypto_kdf_blake2b_BYTES_MAX;
}

size_t
crypto_kdf_blake2b_contextbytes(void)
{
    return crypto_kdf_blake2b_CONTEXTBYTES;
}

size_t
crypto_kdf_blake2b_keybytes(void)
{
    return crypto_kdf_blake2b_KEYBYTES;
}

int crypto_kdf_blake2b_derive_from_key(unsigned char *subkey, size_t subkey_len,
                                       uint64_t subkey_id,
                                       const char ctx[crypto_kdf_blake2b_CONTEXTBYTES],
                                       const unsigned char key[crypto_kdf_blake2b_KEYBYTES])
{
    unsigned char ctx_padded[crypto_generichash_blake2b_PERSONALBYTES];
    unsigned char salt[crypto_generichash_blake2b_SALTBYTES];

    memcpy(ctx_padded, ctx, crypto_kdf_blake2b_CONTEXTBYTES);
    memset(ctx_padded + crypto_kdf_blake2b_CONTEXTBYTES, 0, sizeof ctx_padded - crypto_kdf_blake2b_CONTEXTBYTES);
    STORE64_LE(salt, subkey_id);
    memset(salt + 8, 0, (sizeof salt) - 8);
    if (subkey_len < crypto_kdf_blake2b_BYTES_MIN ||
        subkey_len > crypto_kdf_blake2b_BYTES_MAX) {
        errno = EINVAL;
        return -1;
    }
    return crypto_generichash_blake2b_salt_personal(subkey, subkey_len,
                                                    NULL, 0,
                                                    key, crypto_kdf_blake2b_KEYBYTES,
                                                    salt, ctx_padded);
}