2 BLAKE2 reference source code package - reference C implementations
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:
8 - CC0 1.0 Universal : https://creativecommons.org/publicdomain/zero/1.0
9 - OpenSSL license : https://www.openssl.org/source/license.html
10 - Apache 2.0 : https://www.apache.org/licenses/LICENSE-2.0
12 More information about the BLAKE2 hash function can be found at
18 #ifndef WORDS_BIGENDIAN
19 # define NATIVE_LITTLE_ENDIAN 1
25 #if !defined(__cplusplus) && (!defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L)
27 #define BLAKE2_INLINE __inline
28 #elif defined(__GNUC__)
29 #define BLAKE2_INLINE __inline__
34 #define BLAKE2_INLINE inline
37 static BLAKE2_INLINE
uint32_t load32( const void *src
)
39 #if defined(NATIVE_LITTLE_ENDIAN)
41 memcpy(&w
, src
, sizeof w
);
44 const uint8_t *p
= ( const uint8_t * )src
;
45 return (( uint32_t )( p
[0] ) << 0) |
46 (( uint32_t )( p
[1] ) << 8) |
47 (( uint32_t )( p
[2] ) << 16) |
48 (( uint32_t )( p
[3] ) << 24) ;
52 static BLAKE2_INLINE
uint64_t load64( const void *src
)
54 #if defined(NATIVE_LITTLE_ENDIAN)
56 memcpy(&w
, src
, sizeof w
);
59 const uint8_t *p
= ( const uint8_t * )src
;
60 return (( uint64_t )( p
[0] ) << 0) |
61 (( uint64_t )( p
[1] ) << 8) |
62 (( uint64_t )( p
[2] ) << 16) |
63 (( uint64_t )( p
[3] ) << 24) |
64 (( uint64_t )( p
[4] ) << 32) |
65 (( uint64_t )( p
[5] ) << 40) |
66 (( uint64_t )( p
[6] ) << 48) |
67 (( uint64_t )( p
[7] ) << 56) ;
71 static BLAKE2_INLINE
uint16_t load16( const void *src
)
73 #if defined(NATIVE_LITTLE_ENDIAN)
75 memcpy(&w
, src
, sizeof w
);
78 const uint8_t *p
= ( const uint8_t * )src
;
79 return ( uint16_t )((( uint32_t )( p
[0] ) << 0) |
80 (( uint32_t )( p
[1] ) << 8));
84 static BLAKE2_INLINE
void store16( void *dst
, uint16_t w
)
86 #if defined(NATIVE_LITTLE_ENDIAN)
87 memcpy(dst
, &w
, sizeof w
);
89 uint8_t *p
= ( uint8_t * )dst
;
90 *p
++ = ( uint8_t )w
; w
>>= 8;
95 static BLAKE2_INLINE
void store32( void *dst
, uint32_t w
)
97 #if defined(NATIVE_LITTLE_ENDIAN)
98 memcpy(dst
, &w
, sizeof w
);
100 uint8_t *p
= ( uint8_t * )dst
;
101 p
[0] = (uint8_t)(w
>> 0);
102 p
[1] = (uint8_t)(w
>> 8);
103 p
[2] = (uint8_t)(w
>> 16);
104 p
[3] = (uint8_t)(w
>> 24);
108 static BLAKE2_INLINE
void store64( void *dst
, uint64_t w
)
110 #if defined(NATIVE_LITTLE_ENDIAN)
111 memcpy(dst
, &w
, sizeof w
);
113 uint8_t *p
= ( uint8_t * )dst
;
114 p
[0] = (uint8_t)(w
>> 0);
115 p
[1] = (uint8_t)(w
>> 8);
116 p
[2] = (uint8_t)(w
>> 16);
117 p
[3] = (uint8_t)(w
>> 24);
118 p
[4] = (uint8_t)(w
>> 32);
119 p
[5] = (uint8_t)(w
>> 40);
120 p
[6] = (uint8_t)(w
>> 48);
121 p
[7] = (uint8_t)(w
>> 56);
125 static BLAKE2_INLINE
uint64_t load48( const void *src
)
127 const uint8_t *p
= ( const uint8_t * )src
;
128 return (( uint64_t )( p
[0] ) << 0) |
129 (( uint64_t )( p
[1] ) << 8) |
130 (( uint64_t )( p
[2] ) << 16) |
131 (( uint64_t )( p
[3] ) << 24) |
132 (( uint64_t )( p
[4] ) << 32) |
133 (( uint64_t )( p
[5] ) << 40) ;
136 static BLAKE2_INLINE
void store48( void *dst
, uint64_t w
)
138 uint8_t *p
= ( uint8_t * )dst
;
139 p
[0] = (uint8_t)(w
>> 0);
140 p
[1] = (uint8_t)(w
>> 8);
141 p
[2] = (uint8_t)(w
>> 16);
142 p
[3] = (uint8_t)(w
>> 24);
143 p
[4] = (uint8_t)(w
>> 32);
144 p
[5] = (uint8_t)(w
>> 40);
147 static BLAKE2_INLINE
uint32_t rotr32( const uint32_t w
, const unsigned c
)
149 return ( w
>> c
) | ( w
<< ( 32 - c
) );
152 static BLAKE2_INLINE
uint64_t rotr64( const uint64_t w
, const unsigned c
)
154 return ( w
>> c
) | ( w
<< ( 64 - c
) );
157 /* prevents compiler optimizing out memset() */
158 static BLAKE2_INLINE
void secure_zero_memory(void *v
, size_t n
)
160 static void *(*const volatile memset_v
)(void *, int, size_t) = &memset
;