2 BLAKE2 reference source code package - reference 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 #include "crypto_generichash_blake2b.h"
22 #define blake2b_init_param crypto_generichash_blake2b__init_param
23 #define blake2b_init crypto_generichash_blake2b__init
24 #define blake2b_init_key crypto_generichash_blake2b__init_key
25 #define blake2b_update crypto_generichash_blake2b__update
26 #define blake2b_final crypto_generichash_blake2b__final
27 #define blake2b crypto_generichash_blake2b__blake2b
30 #define ALIGN(x) __declspec(align(x))
32 #define ALIGN(x) __attribute__((aligned(x)))
35 #if defined(__cplusplus)
41 BLAKE2S_BLOCKBYTES
= 64,
42 BLAKE2S_OUTBYTES
= 32,
43 BLAKE2S_KEYBYTES
= 32,
44 BLAKE2S_SALTBYTES
= 8,
45 BLAKE2S_PERSONALBYTES
= 8
50 BLAKE2B_BLOCKBYTES
= 128,
51 BLAKE2B_OUTBYTES
= 64,
52 BLAKE2B_KEYBYTES
= 64,
53 BLAKE2B_SALTBYTES
= 16,
54 BLAKE2B_PERSONALBYTES
= 16
58 typedef struct __blake2s_param
60 uint8_t digest_length
; // 1
61 uint8_t key_length
; // 2
64 uint32_t leaf_length
; // 8
65 uint8_t node_offset
[6];// 14
66 uint8_t node_depth
; // 15
67 uint8_t inner_length
; // 16
68 // uint8_t reserved[0];
69 uint8_t salt
[BLAKE2S_SALTBYTES
]; // 24
70 uint8_t personal
[BLAKE2S_PERSONALBYTES
]; // 32
73 ALIGN( 64 ) typedef struct __blake2s_state
78 uint8_t buf
[2 * BLAKE2S_BLOCKBYTES
];
83 typedef struct __blake2b_param
85 uint8_t digest_length
; // 1
86 uint8_t key_length
; // 2
89 uint32_t leaf_length
; // 8
90 uint64_t node_offset
; // 16
91 uint8_t node_depth
; // 17
92 uint8_t inner_length
; // 18
93 uint8_t reserved
[14]; // 32
94 uint8_t salt
[BLAKE2B_SALTBYTES
]; // 48
95 uint8_t personal
[BLAKE2B_PERSONALBYTES
]; // 64
98 #ifndef DEFINE_BLAKE2B_STATE
99 typedef crypto_generichash_blake2b_state blake2b_state
;
101 ALIGN( 64 ) typedef struct __blake2b_state
106 uint8_t buf
[2 * BLAKE2B_BLOCKBYTES
];
112 typedef struct __blake2sp_state
114 blake2s_state S
[8][1];
116 uint8_t buf
[8 * BLAKE2S_BLOCKBYTES
];
120 typedef struct __blake2bp_state
122 blake2b_state S
[4][1];
124 uint8_t buf
[4 * BLAKE2B_BLOCKBYTES
];
130 int blake2s_init( blake2s_state
*S
, const uint8_t outlen
);
131 int blake2s_init_key( blake2s_state
*S
, const uint8_t outlen
, const void *key
, const uint8_t keylen
);
132 int blake2s_init_param( blake2s_state
*S
, const blake2s_param
*P
);
133 int blake2s_update( blake2s_state
*S
, const uint8_t *in
, uint64_t inlen
);
134 int blake2s_final( blake2s_state
*S
, uint8_t *out
, uint8_t outlen
);
136 int blake2b_init( blake2b_state
*S
, const uint8_t outlen
);
137 int blake2b_init_key( blake2b_state
*S
, const uint8_t outlen
, const void *key
, const uint8_t keylen
);
138 int blake2b_init_param( blake2b_state
*S
, const blake2b_param
*P
);
139 int blake2b_update( blake2b_state
*S
, const uint8_t *in
, uint64_t inlen
);
140 int blake2b_final( blake2b_state
*S
, uint8_t *out
, uint8_t outlen
);
142 int blake2sp_init( blake2sp_state
*S
, const uint8_t outlen
);
143 int blake2sp_init_key( blake2sp_state
*S
, const uint8_t outlen
, const void *key
, const uint8_t keylen
);
144 int blake2sp_update( blake2sp_state
*S
, const uint8_t *in
, uint64_t inlen
);
145 int blake2sp_final( blake2sp_state
*S
, uint8_t *out
, uint8_t outlen
);
147 int blake2bp_init( blake2bp_state
*S
, const uint8_t outlen
);
148 int blake2bp_init_key( blake2bp_state
*S
, const uint8_t outlen
, const void *key
, const uint8_t keylen
);
149 int blake2bp_update( blake2bp_state
*S
, const uint8_t *in
, uint64_t inlen
);
150 int blake2bp_final( blake2bp_state
*S
, uint8_t *out
, uint8_t outlen
);
153 int blake2s( uint8_t *out
, const void *in
, const void *key
, const uint8_t outlen
, const uint64_t inlen
, uint8_t keylen
);
154 int blake2b( uint8_t *out
, const void *in
, const void *key
, const uint8_t outlen
, const uint64_t inlen
, uint8_t keylen
);
156 int blake2sp( uint8_t *out
, const void *in
, const void *key
, const uint8_t outlen
, const uint64_t inlen
, uint8_t keylen
);
157 int blake2bp( uint8_t *out
, const void *in
, const void *key
, const uint8_t outlen
, const uint64_t inlen
, uint8_t keylen
);
159 static inline int blake2( uint8_t *out
, const void *in
, const void *key
, const uint8_t outlen
, const uint64_t inlen
, uint8_t keylen
)
161 return blake2b( out
, in
, key
, outlen
, inlen
, keylen
);
164 #if defined(__cplusplus)