1 | /* |
2 | BLAKE2 reference source code package - reference C implementations |
3 | |
4 | Copyright 2012, Samuel Neves <sneves@dei.uc.pt>. You may use this under the |
5 | terms of the CC0, the OpenSSL Licence, or the Apache Public License 2.0, at |
6 | your option. The terms of these licenses can be found at: |
7 | |
8 | - CC0 1.0 Universal : http://creativecommons.org/publicdomain/zero/1.0 |
9 | - OpenSSL license : https://www.openssl.org/source/license.html |
10 | - Apache 2.0 : http://www.apache.org/licenses/LICENSE-2.0 |
11 | |
12 | More information about the BLAKE2 hash function can be found at |
13 | https://blake2.net. |
14 | */ |
15 | #ifndef BLAKE2_H |
16 | #define BLAKE2_H |
17 | |
18 | #include <stddef.h> |
19 | #include <stdint.h> |
20 | |
21 | #if defined(_MSC_VER) |
22 | #define BLAKE2_PACKED(x) __pragma(pack(push, 1)) x __pragma(pack(pop)) |
23 | #else |
24 | #define BLAKE2_PACKED(x) x __attribute__((packed)) |
25 | #endif |
26 | |
27 | #if defined(__cplusplus) |
28 | extern "C" { |
29 | #endif |
30 | |
31 | enum blake2s_constant |
32 | { |
33 | BLAKE2S_BLOCKBYTES = 64, |
34 | BLAKE2S_OUTBYTES = 32, |
35 | BLAKE2S_KEYBYTES = 32, |
36 | BLAKE2S_SALTBYTES = 8, |
37 | BLAKE2S_PERSONALBYTES = 8 |
38 | }; |
39 | |
40 | enum blake2b_constant |
41 | { |
42 | BLAKE2B_BLOCKBYTES = 128, |
43 | BLAKE2B_OUTBYTES = 64, |
44 | BLAKE2B_KEYBYTES = 64, |
45 | BLAKE2B_SALTBYTES = 16, |
46 | BLAKE2B_PERSONALBYTES = 16 |
47 | }; |
48 | |
49 | typedef struct blake2s_state__ |
50 | { |
51 | uint32_t h[8]; |
52 | uint32_t t[2]; |
53 | uint32_t f[2]; |
54 | uint8_t buf[BLAKE2S_BLOCKBYTES]; |
55 | size_t buflen; |
56 | size_t outlen; |
57 | uint8_t last_node; |
58 | } blake2s_state; |
59 | |
60 | typedef struct blake2b_state__ |
61 | { |
62 | uint64_t h[8]; |
63 | uint64_t t[2]; |
64 | uint64_t f[2]; |
65 | uint8_t buf[BLAKE2B_BLOCKBYTES]; |
66 | size_t buflen; |
67 | size_t outlen; |
68 | uint8_t last_node; |
69 | } blake2b_state; |
70 | |
71 | typedef struct blake2sp_state__ |
72 | { |
73 | blake2s_state S[8][1]; |
74 | blake2s_state R[1]; |
75 | uint8_t buf[8 * BLAKE2S_BLOCKBYTES]; |
76 | size_t buflen; |
77 | size_t outlen; |
78 | } blake2sp_state; |
79 | |
80 | typedef struct blake2bp_state__ |
81 | { |
82 | blake2b_state S[4][1]; |
83 | blake2b_state R[1]; |
84 | uint8_t buf[4 * BLAKE2B_BLOCKBYTES]; |
85 | size_t buflen; |
86 | size_t outlen; |
87 | } blake2bp_state; |
88 | |
89 | |
90 | BLAKE2_PACKED(struct blake2s_param__ |
91 | { |
92 | uint8_t digest_length; /* 1 */ |
93 | uint8_t key_length; /* 2 */ |
94 | uint8_t fanout; /* 3 */ |
95 | uint8_t depth; /* 4 */ |
96 | uint32_t leaf_length; /* 8 */ |
97 | uint32_t node_offset; /* 12 */ |
98 | uint16_t xof_length; /* 14 */ |
99 | uint8_t node_depth; /* 15 */ |
100 | uint8_t inner_length; /* 16 */ |
101 | /* uint8_t reserved[0]; */ |
102 | uint8_t salt[BLAKE2S_SALTBYTES]; /* 24 */ |
103 | uint8_t personal[BLAKE2S_PERSONALBYTES]; /* 32 */ |
104 | }); |
105 | |
106 | typedef struct blake2s_param__ blake2s_param; |
107 | |
108 | BLAKE2_PACKED(struct blake2b_param__ |
109 | { |
110 | uint8_t digest_length; /* 1 */ |
111 | uint8_t key_length; /* 2 */ |
112 | uint8_t fanout; /* 3 */ |
113 | uint8_t depth; /* 4 */ |
114 | uint32_t leaf_length; /* 8 */ |
115 | uint32_t node_offset; /* 12 */ |
116 | uint32_t xof_length; /* 16 */ |
117 | uint8_t node_depth; /* 17 */ |
118 | uint8_t inner_length; /* 18 */ |
119 | uint8_t reserved[14]; /* 32 */ |
120 | uint8_t salt[BLAKE2B_SALTBYTES]; /* 48 */ |
121 | uint8_t personal[BLAKE2B_PERSONALBYTES]; /* 64 */ |
122 | }); |
123 | |
124 | typedef struct blake2b_param__ blake2b_param; |
125 | |
126 | typedef struct blake2xs_state__ |
127 | { |
128 | blake2s_state S[1]; |
129 | blake2s_param P[1]; |
130 | } blake2xs_state; |
131 | |
132 | typedef struct blake2xb_state__ |
133 | { |
134 | blake2b_state S[1]; |
135 | blake2b_param P[1]; |
136 | } blake2xb_state; |
137 | |
138 | /* Padded structs result in a compile-time error */ |
139 | enum { |
140 | BLAKE2_DUMMY_1 = 1/(int)(sizeof(blake2s_param) == BLAKE2S_OUTBYTES), |
141 | BLAKE2_DUMMY_2 = 1/(int)(sizeof(blake2b_param) == BLAKE2B_OUTBYTES) |
142 | }; |
143 | |
144 | /* Streaming API */ |
145 | int blake2s_init( blake2s_state *S, size_t outlen ); |
146 | int blake2s_init_key( blake2s_state *S, size_t outlen, const void *key, size_t keylen ); |
147 | int blake2s_init_param( blake2s_state *S, const blake2s_param *P ); |
148 | int blake2s_update( blake2s_state *S, const void *in, size_t inlen ); |
149 | int blake2s_final( blake2s_state *S, void *out, size_t outlen ); |
150 | |
151 | int blake2b_init( blake2b_state *S, size_t outlen ); |
152 | int blake2b_init_key( blake2b_state *S, size_t outlen, const void *key, size_t keylen ); |
153 | int blake2b_init_param( blake2b_state *S, const blake2b_param *P ); |
154 | int blake2b_update( blake2b_state *S, const void *in, size_t inlen ); |
155 | int blake2b_final( blake2b_state *S, void *out, size_t outlen ); |
156 | |
157 | int blake2sp_init( blake2sp_state *S, size_t outlen ); |
158 | int blake2sp_init_key( blake2sp_state *S, size_t outlen, const void *key, size_t keylen ); |
159 | int blake2sp_update( blake2sp_state *S, const void *in, size_t inlen ); |
160 | int blake2sp_final( blake2sp_state *S, void *out, size_t outlen ); |
161 | |
162 | int blake2bp_init( blake2bp_state *S, size_t outlen ); |
163 | int blake2bp_init_key( blake2bp_state *S, size_t outlen, const void *key, size_t keylen ); |
164 | int blake2bp_update( blake2bp_state *S, const void *in, size_t inlen ); |
165 | int blake2bp_final( blake2bp_state *S, void *out, size_t outlen ); |
166 | |
167 | /* Variable output length API */ |
168 | int blake2xs_init( blake2xs_state *S, const size_t outlen ); |
169 | int blake2xs_init_key( blake2xs_state *S, const size_t outlen, const void *key, size_t keylen ); |
170 | int blake2xs_update( blake2xs_state *S, const void *in, size_t inlen ); |
171 | int blake2xs_final(blake2xs_state *S, void *out, size_t outlen); |
172 | |
173 | int blake2xb_init( blake2xb_state *S, const size_t outlen ); |
174 | int blake2xb_init_key( blake2xb_state *S, const size_t outlen, const void *key, size_t keylen ); |
175 | int blake2xb_update( blake2xb_state *S, const void *in, size_t inlen ); |
176 | int blake2xb_final(blake2xb_state *S, void *out, size_t outlen); |
177 | |
178 | /* Simple API */ |
179 | int blake2s( void *out, size_t outlen, const void *in, size_t inlen, const void *key, size_t keylen ); |
180 | int blake2b( void *out, size_t outlen, const void *in, size_t inlen, const void *key, size_t keylen ); |
181 | |
182 | int blake2sp( void *out, size_t outlen, const void *in, size_t inlen, const void *key, size_t keylen ); |
183 | int blake2bp( void *out, size_t outlen, const void *in, size_t inlen, const void *key, size_t keylen ); |
184 | |
185 | int blake2xs( void *out, size_t outlen, const void *in, size_t inlen, const void *key, size_t keylen ); |
186 | int blake2xb( void *out, size_t outlen, const void *in, size_t inlen, const void *key, size_t keylen ); |
187 | |
188 | /* This is simply an alias for blake2b */ |
189 | int blake2( void *out, size_t outlen, const void *in, size_t inlen, const void *key, size_t keylen ); |
190 | |
191 | #if defined(__cplusplus) |
192 | } |
193 | #endif |
194 | |
195 | #endif |
196 | |