summaryrefslogtreecommitdiff
path: root/src/libsodium/crypto_generichash/blake2/ref/generichash_blake2b.c
blob: 6a637eca5f6ef73e9df2177b686f875f0a5c5ce9 (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108

#include <assert.h>
#include <limits.h>
#include <stdint.h>

#include "api.h"
#include "blake2.h"

int
crypto_generichash_blake2b(unsigned char *out, size_t outlen,
                           const unsigned char *in, unsigned long long inlen,
                           const unsigned char *key, size_t keylen)
{
    if (outlen <= 0U || outlen > BLAKE2B_OUTBYTES ||
        keylen > BLAKE2B_KEYBYTES || inlen > UINT64_MAX) {
        return -1;
    }
    assert(outlen <= UINT8_MAX);
    assert(keylen <= UINT8_MAX);

    return blake2b((uint8_t *) out, in, key,
                   (uint8_t) outlen, (uint64_t) inlen, (uint8_t) keylen);
}

int
crypto_generichash_blake2b_salt_personal(unsigned char *out, size_t outlen,
                                         const unsigned char *in, unsigned long long inlen,
                                         const unsigned char *key, size_t keylen,
                                         const unsigned char *salt,
                                         const unsigned char *personal)
{
    if (outlen <= 0U || outlen > BLAKE2B_OUTBYTES ||
        keylen > BLAKE2B_KEYBYTES || inlen > UINT64_MAX) {
        return -1;
    }
    assert(outlen <= UINT8_MAX);
    assert(keylen <= UINT8_MAX);

    return blake2b_salt_personal((uint8_t *) out, in, key,
                                 (uint8_t) outlen, (uint64_t) inlen, (uint8_t) keylen,
                                 salt, personal);
}

int
crypto_generichash_blake2b_init(crypto_generichash_blake2b_state *state,
                                const unsigned char *key,
                                const size_t keylen, const size_t outlen)
{
    if (outlen <= 0U || outlen > BLAKE2B_OUTBYTES ||
        keylen > BLAKE2B_KEYBYTES) {
        return -1;
    }
    assert(outlen <= UINT8_MAX);
    assert(keylen <= UINT8_MAX);
    if (key == NULL || keylen <= 0U) {
        if (blake2b_init(state, (uint8_t) outlen) != 0) {
            return -1;
        }
    } else if (blake2b_init_key(state, (uint8_t) outlen, key,
                                (uint8_t) keylen) != 0) {
        return -1;
    }
    return 0;
}

int
crypto_generichash_blake2b_init_salt_personal(crypto_generichash_blake2b_state *state,
                                              const unsigned char *key,
                                              const size_t keylen, const size_t outlen,
                                              const unsigned char *salt,
                                              const unsigned char *personal)
{
    if (outlen <= 0U || outlen > BLAKE2B_OUTBYTES ||
        keylen > BLAKE2B_KEYBYTES) {
        return -1;
    }
    assert(outlen <= UINT8_MAX);
    assert(keylen <= UINT8_MAX);
    if (key == NULL || keylen <= 0U) {
        if (blake2b_init_salt_personal(state, (uint8_t) outlen,
                                       salt, personal) != 0) {
            return -1;
        }
    } else if (blake2b_init_key_salt_personal(state,
                                              (uint8_t) outlen, key,
                                              (uint8_t) keylen,
                                              salt, personal) != 0) {
        return -1;
    }
    return 0;
}

int
crypto_generichash_blake2b_update(crypto_generichash_blake2b_state *state,
                                  const unsigned char *in,
                                  unsigned long long inlen)
{
    return blake2b_update(state, (const uint8_t *) in, (uint64_t) inlen);
}

int
crypto_generichash_blake2b_final(crypto_generichash_blake2b_state *state,
                                 unsigned char *out,
                                 const size_t outlen)
{
    assert(outlen <= UINT8_MAX);
    return blake2b_final(state, (uint8_t *) out, (uint8_t) outlen);
}