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 : 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
12 More information about the BLAKE2 hash function can be found at
16 #ifndef ARCHIVE_BLAKE2_IMPL_H
17 #define ARCHIVE_BLAKE2_IMPL_H
22 #if !defined(__cplusplus) && (!defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L)
24 #define BLAKE2_INLINE __inline
25 #elif defined(__GNUC__)
26 #define BLAKE2_INLINE __inline__
31 #define BLAKE2_INLINE inline
34 static BLAKE2_INLINE uint32_t load32( const void *src )
36 #if defined(NATIVE_LITTLE_ENDIAN)
38 memcpy(&w, src, sizeof w);
41 const uint8_t *p = ( const uint8_t * )src;
42 return (( uint32_t )( p[0] ) << 0) |
43 (( uint32_t )( p[1] ) << 8) |
44 (( uint32_t )( p[2] ) << 16) |
45 (( uint32_t )( p[3] ) << 24) ;
49 static BLAKE2_INLINE uint64_t load64( const void *src )
51 #if defined(NATIVE_LITTLE_ENDIAN)
53 memcpy(&w, src, sizeof w);
56 const uint8_t *p = ( const uint8_t * )src;
57 return (( uint64_t )( p[0] ) << 0) |
58 (( uint64_t )( p[1] ) << 8) |
59 (( uint64_t )( p[2] ) << 16) |
60 (( uint64_t )( p[3] ) << 24) |
61 (( uint64_t )( p[4] ) << 32) |
62 (( uint64_t )( p[5] ) << 40) |
63 (( uint64_t )( p[6] ) << 48) |
64 (( uint64_t )( p[7] ) << 56) ;
68 static BLAKE2_INLINE uint16_t load16( const void *src )
70 #if defined(NATIVE_LITTLE_ENDIAN)
72 memcpy(&w, src, sizeof w);
75 const uint8_t *p = ( const uint8_t * )src;
76 return ( uint16_t )((( uint32_t )( p[0] ) << 0) |
77 (( uint32_t )( p[1] ) << 8));
81 static BLAKE2_INLINE void store16( void *dst, uint16_t w )
83 #if defined(NATIVE_LITTLE_ENDIAN)
84 memcpy(dst, &w, sizeof w);
86 uint8_t *p = ( uint8_t * )dst;
87 *p++ = ( uint8_t )w; w >>= 8;
92 static BLAKE2_INLINE void store32( void *dst, uint32_t w )
94 #if defined(NATIVE_LITTLE_ENDIAN)
95 memcpy(dst, &w, sizeof w);
97 uint8_t *p = ( uint8_t * )dst;
98 p[0] = (uint8_t)(w >> 0);
99 p[1] = (uint8_t)(w >> 8);
100 p[2] = (uint8_t)(w >> 16);
101 p[3] = (uint8_t)(w >> 24);
105 static BLAKE2_INLINE void store64( void *dst, uint64_t w )
107 #if defined(NATIVE_LITTLE_ENDIAN)
108 memcpy(dst, &w, sizeof w);
110 uint8_t *p = ( uint8_t * )dst;
111 p[0] = (uint8_t)(w >> 0);
112 p[1] = (uint8_t)(w >> 8);
113 p[2] = (uint8_t)(w >> 16);
114 p[3] = (uint8_t)(w >> 24);
115 p[4] = (uint8_t)(w >> 32);
116 p[5] = (uint8_t)(w >> 40);
117 p[6] = (uint8_t)(w >> 48);
118 p[7] = (uint8_t)(w >> 56);
122 static BLAKE2_INLINE uint64_t load48( const void *src )
124 const uint8_t *p = ( const uint8_t * )src;
125 return (( uint64_t )( p[0] ) << 0) |
126 (( uint64_t )( p[1] ) << 8) |
127 (( uint64_t )( p[2] ) << 16) |
128 (( uint64_t )( p[3] ) << 24) |
129 (( uint64_t )( p[4] ) << 32) |
130 (( uint64_t )( p[5] ) << 40) ;
133 static BLAKE2_INLINE void store48( void *dst, uint64_t w )
135 uint8_t *p = ( uint8_t * )dst;
136 p[0] = (uint8_t)(w >> 0);
137 p[1] = (uint8_t)(w >> 8);
138 p[2] = (uint8_t)(w >> 16);
139 p[3] = (uint8_t)(w >> 24);
140 p[4] = (uint8_t)(w >> 32);
141 p[5] = (uint8_t)(w >> 40);
144 static BLAKE2_INLINE uint32_t rotr32( const uint32_t w, const unsigned c )
146 return ( w >> c ) | ( w << ( 32 - c ) );
149 static BLAKE2_INLINE uint64_t rotr64( const uint64_t w, const unsigned c )
151 return ( w >> c ) | ( w << ( 64 - c ) );
154 /* prevents compiler optimizing out memset() */
155 static BLAKE2_INLINE void secure_zero_memory(void *v, size_t n)
157 static void *(__LA_LIBC_CC *const volatile memset_v)(void *, int, size_t) = &memset;