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 <string.h>
20 
21 BLAKE2_LOCAL_INLINE(uint32_t) load32( const void *src )
22 {
23 #if defined(NATIVE_LITTLE_ENDIAN)
24   uint32_t w;
25   memcpy(&w, src, sizeof w);
26   return w;
27 #else
28   const uint8_t *p = ( const uint8_t * )src;
29   uint32_t w = *p++;
30   w |= ( uint32_t )( *p++ ) <<  8;
31   w |= ( uint32_t )( *p++ ) << 16;
32   w |= ( uint32_t )( *p++ ) << 24;
33   return w;
34 #endif
35 }
36 
37 BLAKE2_LOCAL_INLINE(uint64_t) load64( const void *src )
38 {
39 #if defined(NATIVE_LITTLE_ENDIAN)
40   uint64_t w;
41   memcpy(&w, src, sizeof w);
42   return w;
43 #else
44   const uint8_t *p = ( const uint8_t * )src;
45   uint64_t w = *p++;
46   w |= ( uint64_t )( *p++ ) <<  8;
47   w |= ( uint64_t )( *p++ ) << 16;
48   w |= ( uint64_t )( *p++ ) << 24;
49   w |= ( uint64_t )( *p++ ) << 32;
50   w |= ( uint64_t )( *p++ ) << 40;
51   w |= ( uint64_t )( *p++ ) << 48;
52   w |= ( uint64_t )( *p++ ) << 56;
53   return w;
54 #endif
55 }
56 
57 BLAKE2_LOCAL_INLINE(void) store32( void *dst, uint32_t w )
58 {
59 #if defined(NATIVE_LITTLE_ENDIAN)
60   memcpy(dst, &w, sizeof w);
61 #else
62   uint8_t *p = ( uint8_t * )dst;
63   *p++ = ( uint8_t )w; w >>= 8;
64   *p++ = ( uint8_t )w; w >>= 8;
65   *p++ = ( uint8_t )w; w >>= 8;
66   *p++ = ( uint8_t )w;
67 #endif
68 }
69 
70 BLAKE2_LOCAL_INLINE(void) store64( void *dst, uint64_t w )
71 {
72 #if defined(NATIVE_LITTLE_ENDIAN)
73   memcpy(dst, &w, sizeof w);
74 #else
75   uint8_t *p = ( uint8_t * )dst;
76   *p++ = ( uint8_t )w; w >>= 8;
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;
84 #endif
85 }
86 
87 BLAKE2_LOCAL_INLINE(uint64_t) load48( const void *src )
88 {
89   const uint8_t *p = ( const uint8_t * )src;
90   uint64_t w = *p++;
91   w |= ( uint64_t )( *p++ ) <<  8;
92   w |= ( uint64_t )( *p++ ) << 16;
93   w |= ( uint64_t )( *p++ ) << 24;
94   w |= ( uint64_t )( *p++ ) << 32;
95   w |= ( uint64_t )( *p++ ) << 40;
96   return w;
97 }
98 
99 BLAKE2_LOCAL_INLINE(void) store48( void *dst, uint64_t w )
100 {
101   uint8_t *p = ( uint8_t * )dst;
102   *p++ = ( uint8_t )w; w >>= 8;
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;
108 }
109 
110 BLAKE2_LOCAL_INLINE(uint32_t) rotl32( const uint32_t w, const unsigned c )
111 {
112   return ( w << c ) | ( w >> ( 32 - c ) );
113 }
114 
115 BLAKE2_LOCAL_INLINE(uint64_t) rotl64( const uint64_t w, const unsigned c )
116 {
117   return ( w << c ) | ( w >> ( 64 - c ) );
118 }
119 
120 BLAKE2_LOCAL_INLINE(uint32_t) rotr32( const uint32_t w, const unsigned c )
121 {
122   return ( w >> c ) | ( w << ( 32 - c ) );
123 }
124 
125 BLAKE2_LOCAL_INLINE(uint64_t) rotr64( const uint64_t w, const unsigned c )
126 {
127   return ( w >> c ) | ( w << ( 64 - c ) );
128 }
129 
130 /* prevents compiler optimizing out memset() */
131 BLAKE2_LOCAL_INLINE(void) secure_zero_memory(void *v, size_t n)
132 {
133   static void *(*const volatile memset_v)(void *, int, size_t) = &memset;
134   memset_v(v, 0, n);
135 }
136 
137 #endif
138 
139