7253cbf3d4146c251eced5c353c208f50b4b4279
[trex.git] /
1
2 #include <assert.h>
3 #include <limits.h>
4 #include <stdint.h>
5
6 #include "api.h"
7 #include "blake2.h"
8
9 int
10 crypto_generichash_blake2b(unsigned char *out, size_t outlen,
11                            const unsigned char *in, unsigned long long inlen,
12                            const unsigned char *key, size_t keylen)
13 {
14     if (outlen <= 0U || outlen > BLAKE2B_OUTBYTES ||
15         keylen > BLAKE2B_KEYBYTES || inlen > UINT64_MAX) {
16         return -1;
17     }
18     assert(outlen <= UINT8_MAX);
19     assert(keylen <= UINT8_MAX);
20
21     return blake2b((uint8_t *) out, in, key,
22                    (uint8_t) outlen, (uint64_t) inlen, (uint8_t) keylen);
23 }
24
25 int
26 crypto_generichash_blake2b_salt_personal(unsigned char *out, size_t outlen,
27                                          const unsigned char *in, unsigned long long inlen,
28                                          const unsigned char *key, size_t keylen,
29                                          const unsigned char *salt,
30                                          const unsigned char *personal)
31 {
32     if (outlen <= 0U || outlen > BLAKE2B_OUTBYTES ||
33         keylen > BLAKE2B_KEYBYTES || inlen > UINT64_MAX) {
34         return -1;
35     }
36     assert(outlen <= UINT8_MAX);
37     assert(keylen <= UINT8_MAX);
38
39     return blake2b_salt_personal((uint8_t *) out, in, key,
40                                  (uint8_t) outlen, (uint64_t) inlen, (uint8_t) keylen,
41                                  salt, personal);
42 }
43
44 int
45 crypto_generichash_blake2b_init(crypto_generichash_blake2b_state *state,
46                                 const unsigned char *key,
47                                 const size_t keylen, const size_t outlen)
48 {
49     if (outlen <= 0U || outlen > BLAKE2B_OUTBYTES ||
50         keylen > BLAKE2B_KEYBYTES) {
51         return -1;
52     }
53     assert(outlen <= UINT8_MAX);
54     assert(keylen <= UINT8_MAX);
55     if (key == NULL || keylen <= 0U) {
56         if (blake2b_init(state, (uint8_t) outlen) != 0) {
57             return -1; /* LCOV_EXCL_LINE */
58         }
59     } else if (blake2b_init_key(state, (uint8_t) outlen, key,
60                                 (uint8_t) keylen) != 0) {
61         return -1; /* LCOV_EXCL_LINE */
62     }
63     return 0;
64 }
65
66 int
67 crypto_generichash_blake2b_init_salt_personal(crypto_generichash_blake2b_state *state,
68                                               const unsigned char *key,
69                                               const size_t keylen, const size_t outlen,
70                                               const unsigned char *salt,
71                                               const unsigned char *personal)
72 {
73     if (outlen <= 0U || outlen > BLAKE2B_OUTBYTES ||
74         keylen > BLAKE2B_KEYBYTES) {
75         return -1;
76     }
77     assert(outlen <= UINT8_MAX);
78     assert(keylen <= UINT8_MAX);
79     if (key == NULL || keylen <= 0U) {
80         if (blake2b_init_salt_personal(state, (uint8_t) outlen,
81                                        salt, personal) != 0) {
82             return -1; /* LCOV_EXCL_LINE */
83         }
84     } else if (blake2b_init_key_salt_personal(state,
85                                               (uint8_t) outlen, key,
86                                               (uint8_t) keylen,
87                                               salt, personal) != 0) {
88         return -1; /* LCOV_EXCL_LINE */
89     }
90     return 0;
91 }
92
93 int
94 crypto_generichash_blake2b_update(crypto_generichash_blake2b_state *state,
95                                   const unsigned char *in,
96                                   unsigned long long inlen)
97 {
98     return blake2b_update(state, (const uint8_t *) in, (uint64_t) inlen);
99 }
100
101 int
102 crypto_generichash_blake2b_final(crypto_generichash_blake2b_state *state,
103                                  unsigned char *out,
104                                  const size_t outlen)
105 {
106     assert(outlen <= UINT8_MAX);
107     return blake2b_final(state, (uint8_t *) out, (uint8_t) outlen);
108 }