librsync  2.0.1
blake2-impl.h
1 /*
2  BLAKE2 reference source code package - reference C implementations
3 
4  Written in 2012 by Samuel Neves <sneves@dei.uc.pt>
5 
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.
9 
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/>.
12 */
13 
14 /* This code, but not the algorithm, has been slightly modified for use in
15  * librsync.
16  */
17 
18 #pragma once
19 #ifndef __BLAKE2_IMPL_H__
20 #define __BLAKE2_IMPL_H__
21 
22 #include <stdint.h>
23 
24 #ifndef WORDS_BIGENDIAN /* from librsync config.h */
25 # define NATIVE_LITTLE_ENDIAN
26 #endif /* ndef WORDS_BIGENDIAN */
27 
28 static inline uint32_t load32( const void *src )
29 {
30 #if defined(NATIVE_LITTLE_ENDIAN)
31  return *( const uint32_t * )( src );
32 #else
33  const uint8_t *p = ( const uint8_t * )src;
34  uint32_t w = *p++;
35  w |= ( uint32_t )( *p++ ) << 8;
36  w |= ( uint32_t )( *p++ ) << 16;
37  w |= ( uint32_t )( *p++ ) << 24;
38  return w;
39 #endif
40 }
41 
42 static inline uint64_t load64( const void *src )
43 {
44 #if defined(NATIVE_LITTLE_ENDIAN)
45  return *( const uint64_t * )( src );
46 #else
47  const uint8_t *p = ( const uint8_t * )src;
48  uint64_t w = *p++;
49  w |= ( uint64_t )( *p++ ) << 8;
50  w |= ( uint64_t )( *p++ ) << 16;
51  w |= ( uint64_t )( *p++ ) << 24;
52  w |= ( uint64_t )( *p++ ) << 32;
53  w |= ( uint64_t )( *p++ ) << 40;
54  w |= ( uint64_t )( *p++ ) << 48;
55  w |= ( uint64_t )( *p++ ) << 56;
56  return w;
57 #endif
58 }
59 
60 static inline void store32( void *dst, uint32_t w )
61 {
62 #if defined(NATIVE_LITTLE_ENDIAN)
63  *( uint32_t * )( dst ) = w;
64 #else
65  uint8_t *p = ( uint8_t * )dst;
66  *p++ = ( uint8_t )w; w >>= 8;
67  *p++ = ( uint8_t )w; w >>= 8;
68  *p++ = ( uint8_t )w; w >>= 8;
69  *p++ = ( uint8_t )w;
70 #endif
71 }
72 
73 static inline void store64( void *dst, uint64_t w )
74 {
75 #if defined(NATIVE_LITTLE_ENDIAN)
76  *( uint64_t * )( dst ) = w;
77 #else
78  uint8_t *p = ( uint8_t * )dst;
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; w >>= 8;
85  *p++ = ( uint8_t )w; w >>= 8;
86  *p++ = ( uint8_t )w;
87 #endif
88 }
89 
90 static inline uint64_t load48( const void *src )
91 {
92  const uint8_t *p = ( const uint8_t * )src;
93  uint64_t w = *p++;
94  w |= ( uint64_t )( *p++ ) << 8;
95  w |= ( uint64_t )( *p++ ) << 16;
96  w |= ( uint64_t )( *p++ ) << 24;
97  w |= ( uint64_t )( *p++ ) << 32;
98  w |= ( uint64_t )( *p++ ) << 40;
99  return w;
100 }
101 
102 static inline void store48( void *dst, uint64_t w )
103 {
104  uint8_t *p = ( uint8_t * )dst;
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; w >>= 8;
109  *p++ = ( uint8_t )w; w >>= 8;
110  *p++ = ( uint8_t )w;
111 }
112 
113 static inline uint32_t rotl32( const uint32_t w, const unsigned c )
114 {
115  return ( w << c ) | ( w >> ( 32 - c ) );
116 }
117 
118 static inline uint64_t rotl64( const uint64_t w, const unsigned c )
119 {
120  return ( w << c ) | ( w >> ( 64 - c ) );
121 }
122 
123 static inline uint32_t rotr32( const uint32_t w, const unsigned c )
124 {
125  return ( w >> c ) | ( w << ( 32 - c ) );
126 }
127 
128 static inline uint64_t rotr64( const uint64_t w, const unsigned c )
129 {
130  return ( w >> c ) | ( w << ( 64 - c ) );
131 }
132 
133 /* prevents compiler optimizing out memset() */
134 static inline void secure_zero_memory( void *v, size_t n )
135 {
136  volatile uint8_t *p = ( volatile uint8_t * )v;
137 
138  while( n-- ) *p++ = 0;
139 }
140 
141 #endif
142