1 /*
2 BLAKE2 reference source code package - optimized C implementations
3
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:
7
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
11
12 More information about the BLAKE2 hash function can be found at
13 https://blake2.net.
14 */
15 #pragma once
16 #ifndef __BLAKE2_IMPL_H__
17 #define __BLAKE2_IMPL_H__
18
19 #include <stdint.h>
20 #include <string.h>
21
load32(const void * src)22 BLAKE2_LOCAL_INLINE(uint32_t) load32( const void *src )
23 {
24 #if defined(NATIVE_LITTLE_ENDIAN)
25 uint32_t w;
26 memcpy(&w, src, sizeof w);
27 return w;
28 #else
29 const uint8_t *p = ( const uint8_t * )src;
30 uint32_t w = *p++;
31 w |= ( uint32_t )( *p++ ) << 8;
32 w |= ( uint32_t )( *p++ ) << 16;
33 w |= ( uint32_t )( *p++ ) << 24;
34 return w;
35 #endif
36 }
37
load64(const void * src)38 BLAKE2_LOCAL_INLINE(uint64_t) load64( const void *src )
39 {
40 #if defined(NATIVE_LITTLE_ENDIAN)
41 uint64_t w;
42 memcpy(&w, src, sizeof w);
43 return w;
44 #else
45 const uint8_t *p = ( const uint8_t * )src;
46 uint64_t w = *p++;
47 w |= ( uint64_t )( *p++ ) << 8;
48 w |= ( uint64_t )( *p++ ) << 16;
49 w |= ( uint64_t )( *p++ ) << 24;
50 w |= ( uint64_t )( *p++ ) << 32;
51 w |= ( uint64_t )( *p++ ) << 40;
52 w |= ( uint64_t )( *p++ ) << 48;
53 w |= ( uint64_t )( *p++ ) << 56;
54 return w;
55 #endif
56 }
57
store32(void * dst,uint32_t w)58 BLAKE2_LOCAL_INLINE(void) store32( void *dst, uint32_t w )
59 {
60 #if defined(NATIVE_LITTLE_ENDIAN)
61 memcpy(dst, &w, sizeof w);
62 #else
63 uint8_t *p = ( uint8_t * )dst;
64 *p++ = ( uint8_t )w; w >>= 8;
65 *p++ = ( uint8_t )w; w >>= 8;
66 *p++ = ( uint8_t )w; w >>= 8;
67 *p++ = ( uint8_t )w;
68 #endif
69 }
70
store64(void * dst,uint64_t w)71 BLAKE2_LOCAL_INLINE(void) store64( void *dst, uint64_t w )
72 {
73 #if defined(NATIVE_LITTLE_ENDIAN)
74 memcpy(dst, &w, sizeof w);
75 #else
76 uint8_t *p = ( uint8_t * )dst;
77 *p++ = ( uint8_t )w; w >>= 8;
78 *p++ = ( uint8_t )w; w >>= 8;
79 *p++ = ( uint8_t )w; w >>= 8;
80 *p++ = ( uint8_t )w; w >>= 8;
81 *p++ = ( uint8_t )w; w >>= 8;
82 *p++ = ( uint8_t )w; w >>= 8;
83 *p++ = ( uint8_t )w; w >>= 8;
84 *p++ = ( uint8_t )w;
85 #endif
86 }
87
load48(const void * src)88 BLAKE2_LOCAL_INLINE(uint64_t) load48( const void *src )
89 {
90 const uint8_t *p = ( const uint8_t * )src;
91 uint64_t w = *p++;
92 w |= ( uint64_t )( *p++ ) << 8;
93 w |= ( uint64_t )( *p++ ) << 16;
94 w |= ( uint64_t )( *p++ ) << 24;
95 w |= ( uint64_t )( *p++ ) << 32;
96 w |= ( uint64_t )( *p++ ) << 40;
97 return w;
98 }
99
store48(void * dst,uint64_t w)100 BLAKE2_LOCAL_INLINE(void) store48( void *dst, uint64_t w )
101 {
102 uint8_t *p = ( uint8_t * )dst;
103 *p++ = ( uint8_t )w; w >>= 8;
104 *p++ = ( uint8_t )w; w >>= 8;
105 *p++ = ( uint8_t )w; w >>= 8;
106 *p++ = ( uint8_t )w; w >>= 8;
107 *p++ = ( uint8_t )w; w >>= 8;
108 *p++ = ( uint8_t )w;
109 }
110
rotl32(const uint32_t w,const unsigned c)111 BLAKE2_LOCAL_INLINE(uint32_t) rotl32( const uint32_t w, const unsigned c )
112 {
113 return ( w << c ) | ( w >> ( 32 - c ) );
114 }
115
rotl64(const uint64_t w,const unsigned c)116 BLAKE2_LOCAL_INLINE(uint64_t) rotl64( const uint64_t w, const unsigned c )
117 {
118 return ( w << c ) | ( w >> ( 64 - c ) );
119 }
120
rotr32(const uint32_t w,const unsigned c)121 BLAKE2_LOCAL_INLINE(uint32_t) rotr32( const uint32_t w, const unsigned c )
122 {
123 return ( w >> c ) | ( w << ( 32 - c ) );
124 }
125
rotr64(const uint64_t w,const unsigned c)126 BLAKE2_LOCAL_INLINE(uint64_t) rotr64( const uint64_t w, const unsigned c )
127 {
128 return ( w >> c ) | ( w << ( 64 - c ) );
129 }
130
131 /* prevents compiler optimizing out memset() */
secure_zero_memory(void * v,size_t n)132 BLAKE2_LOCAL_INLINE(void) secure_zero_memory(void *v, size_t n)
133 {
134 static void *(*const volatile memset_v)(void *, int, size_t) = &memset;
135 memset_v(v, 0, n);
136 }
137
138 #endif
139
140