2 * BLAKE2 reference source code package - reference C implementations
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/>.
22 #define inline __inline
23 #define ALIGN(x) __declspec(align(x))
25 #define ALIGN(x) __attribute__((aligned(x)))
30 static inline uint32_t load32(const void *src)
32 #if defined(NATIVE_LITTLE_ENDIAN)
33 return *(uint32_t *)(src);
35 const uint8_t *p = (uint8_t *)src;
37 w |= (uint32_t)(*p++) << 8;
38 w |= (uint32_t)(*p++) << 16;
39 w |= (uint32_t)(*p++) << 24;
44 static inline void store32(void *dst, uint32_t w)
46 #if defined(NATIVE_LITTLE_ENDIAN)
47 *(uint32_t *)(dst) = w;
49 uint8_t *p = (uint8_t *)dst;
50 *p++ = (uint8_t)w; w >>= 8;
51 *p++ = (uint8_t)w; w >>= 8;
52 *p++ = (uint8_t)w; w >>= 8;
57 static inline uint64_t load48(const void *src)
59 const uint8_t *p = (const uint8_t *)src;
61 w |= (uint64_t)(*p++) << 8;
62 w |= (uint64_t)(*p++) << 16;
63 w |= (uint64_t)(*p++) << 24;
64 w |= (uint64_t)(*p++) << 32;
65 w |= (uint64_t)(*p++) << 40;
69 static inline void store48(void *dst, uint64_t w)
71 uint8_t *p = (uint8_t *)dst;
72 *p++ = (uint8_t)w; w >>= 8;
73 *p++ = (uint8_t)w; w >>= 8;
74 *p++ = (uint8_t)w; w >>= 8;
75 *p++ = (uint8_t)w; w >>= 8;
76 *p++ = (uint8_t)w; w >>= 8;
80 /* prevents compiler optimizing out memset() */
81 static inline void secure_zero_memory(void *v, size_t n)
83 volatile uint8_t *p = ( volatile uint8_t * )v;
85 while( n-- ) *p++ = 0;
90 #if defined(__cplusplus)
96 BLAKE2S_BLOCKBYTES = 64,
97 BLAKE2S_OUTBYTES = 32,
98 BLAKE2S_KEYBYTES = 32,
99 BLAKE2S_SALTBYTES = 8,
100 BLAKE2S_PERSONALBYTES = 8
103 #pragma pack(push, 1)
104 typedef struct __blake2s_param
106 uint8_t digest_length; // 1
107 uint8_t key_length; // 2
110 uint32_t leaf_length; // 8
111 uint8_t node_offset[6];// 14
112 uint8_t node_depth; // 15
113 uint8_t inner_length; // 16
114 // uint8_t reserved[0];
115 uint8_t salt[BLAKE2S_SALTBYTES]; // 24
116 uint8_t personal[BLAKE2S_PERSONALBYTES]; // 32
119 ALIGN( 64 ) typedef struct __blake2s_state
124 uint8_t buf[2 * BLAKE2S_BLOCKBYTES];
130 int blake2s_compress( blake2s_state *S, const uint8_t block[BLAKE2S_BLOCKBYTES] );
133 int blake2s_init( blake2s_state *S, const uint8_t outlen );
134 int blake2s_init_key( blake2s_state *S, const uint8_t outlen, const void *key, const uint8_t keylen );
135 int blake2s_init_param( blake2s_state *S, const blake2s_param *P );
136 int blake2s_update( blake2s_state *S, const uint8_t *in, uint64_t inlen );
137 int blake2s_final( blake2s_state *S, uint8_t *out, uint8_t outlen );
140 int blake2s( uint8_t *out, const void *in, const void *key, const uint8_t outlen, const uint64_t inlen, uint8_t keylen );
142 // Direct Hash Mining Helpers
143 #define blake2s_salt32(out, in, inlen, key32) blake2s(out, in, key32, 32, inlen, 32) /* neoscrypt */
144 #define blake2s_simple(out, in, inlen) blake2s(out, in, NULL, 32, inlen, 0)
146 #if defined(__cplusplus)