2 BLAKE2 reference source code package - optimized C implementations
4 Written in 2012 by Samuel Neves <sneves@dei.uc.pt>
6 To the extent possible under law, the author(s) have dedicated all copyright
7 and related and neighboring rights to this software to the public domain
8 worldwide. This software is distributed without any warranty.
10 You should have received a copy of the CC0 Public Domain Dedication along with
11 this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
20 #if defined(_WIN32) || defined(__CYGWIN__)
21 #define BLAKE2_DLL_IMPORT __declspec(dllimport)
22 #define BLAKE2_DLL_EXPORT __declspec(dllexport)
23 #define BLAKE2_DLL_PRIVATE
25 #define BLAKE2_DLL_IMPORT __attribute__ ((visibility ("default")))
26 #define BLAKE2_DLL_EXPORT __attribute__ ((visibility ("default")))
27 #define BLAKE2_DLL_PRIVATE __attribute__ ((visibility ("hidden")))
29 #define BLAKE2_DLL_IMPORT
30 #define BLAKE2_DLL_EXPORT
31 #define BLAKE2_DLL_PRIVATE
34 #if defined(BLAKE2_DLL)
35 #if defined(BLAKE2_DLL_EXPORTS) // defined if we are building the DLL
36 #define BLAKE2_API BLAKE2_DLL_EXPORT
38 #define BLAKE2_API BLAKE2_DLL_IMPORT
40 #define BLAKE2_PRIVATE BLAKE2_DLL_PRIVATE // must only be used by hidden logic
43 #define BLAKE2_PRIVATE
46 #if defined(__cplusplus)
48 #elif defined(_MSC_VER) && !defined(inline)
49 #define inline __inline
54 BLAKE2S_BLOCKBYTES = 64,
55 BLAKE2S_OUTBYTES = 32,
56 BLAKE2S_KEYBYTES = 32,
57 BLAKE2S_SALTBYTES = 8,
58 BLAKE2S_PERSONALBYTES = 8
63 BLAKE2B_BLOCKBYTES = 128,
64 BLAKE2B_OUTBYTES = 64,
65 BLAKE2B_KEYBYTES = 64,
66 BLAKE2B_SALTBYTES = 16,
67 BLAKE2B_PERSONALBYTES = 16
71 typedef struct __blake2s_param
73 uint8_t digest_length; // 1
74 uint8_t key_length; // 2
77 uint32_t leaf_length; // 8
78 uint8_t node_offset[6];// 14
79 uint8_t node_depth; // 15
80 uint8_t inner_length; // 16
81 // uint8_t reserved[0];
82 uint8_t salt[BLAKE2S_SALTBYTES]; // 24
83 uint8_t personal[BLAKE2S_PERSONALBYTES]; // 32
86 typedef struct __blake2s_state
91 uint8_t buf[2 * BLAKE2S_BLOCKBYTES];
97 typedef struct __blake2b_param
99 uint8_t digest_length; // 1
100 uint8_t key_length; // 2
103 uint32_t leaf_length; // 8
104 uint64_t node_offset; // 16
105 uint8_t node_depth; // 17
106 uint8_t inner_length; // 18
107 uint8_t reserved[14]; // 32
108 uint8_t salt[BLAKE2B_SALTBYTES]; // 48
109 uint8_t personal[BLAKE2B_PERSONALBYTES]; // 64
112 typedef struct __blake2b_state
117 uint8_t buf[2 * BLAKE2B_BLOCKBYTES];
123 typedef struct __blake2sp_state
125 blake2s_state S[8][1];
127 uint8_t buf[8 * BLAKE2S_BLOCKBYTES];
132 typedef struct __blake2bp_state
134 blake2b_state S[4][1];
136 uint8_t buf[4 * BLAKE2B_BLOCKBYTES];
143 BLAKE2_API int blake2s_init( blake2s_state *S, size_t outlen );
144 BLAKE2_API int blake2s_init_key( blake2s_state *S, size_t outlen, const void *key, size_t keylen );
145 BLAKE2_API int blake2s_init_param( blake2s_state *S, const blake2s_param *P );
146 BLAKE2_API int blake2s_update( blake2s_state *S, const uint8_t *in, size_t inlen );
147 BLAKE2_API int blake2s_final( blake2s_state *S, uint8_t *out, size_t outlen );
149 BLAKE2_API int blake2b_init( blake2b_state *S, size_t outlen );
150 BLAKE2_API int blake2b_init_key( blake2b_state *S, size_t outlen, const void *key, size_t keylen );
151 BLAKE2_API int blake2b_init_param( blake2b_state *S, const blake2b_param *P );
152 BLAKE2_API int blake2b_update( blake2b_state *S, const uint8_t *in, size_t inlen );
153 BLAKE2_API int blake2b_final( blake2b_state *S, uint8_t *out, size_t outlen );
155 BLAKE2_API int blake2sp_init( blake2sp_state *S, size_t outlen );
156 BLAKE2_API int blake2sp_init_key( blake2sp_state *S, size_t outlen, const void *key, size_t keylen );
157 BLAKE2_API int blake2sp_update( blake2sp_state *S, const uint8_t *in, size_t inlen );
158 BLAKE2_API int blake2sp_final( blake2sp_state *S, uint8_t *out, size_t outlen );
160 BLAKE2_API int blake2bp_init( blake2bp_state *S, size_t outlen );
161 BLAKE2_API int blake2bp_init_key( blake2bp_state *S, size_t outlen, const void *key, size_t keylen );
162 BLAKE2_API int blake2bp_update( blake2bp_state *S, const uint8_t *in, size_t inlen );
163 BLAKE2_API int blake2bp_final( blake2bp_state *S, uint8_t *out, size_t outlen );
166 BLAKE2_API int blake2s( uint8_t *out, const void *in, const void *key, size_t outlen, size_t inlen, size_t keylen );
167 BLAKE2_API int blake2b( uint8_t *out, const void *in, const void *key, size_t outlen, size_t inlen, size_t keylen );
169 BLAKE2_API int blake2sp( uint8_t *out, const void *in, const void *key, size_t outlen, size_t inlen, size_t keylen );
170 BLAKE2_API int blake2bp( uint8_t *out, const void *in, const void *key, size_t outlen, size_t inlen, size_t keylen );
172 static inline int blake2( uint8_t *out, const void *in, const void *key, size_t outlen, size_t inlen, size_t keylen )
174 return blake2b( out, in, key, outlen, inlen, keylen );
177 #if defined(__cplusplus)