libsodium: Needed for Dnscrypto-proxy Release 1.3.0
[tomato.git] / release / src / router / libsodium / src / libsodium / crypto_generichash / blake2 / ref / blake2-impl.h
blob78a05f34958c7a38f95ed8d24b140f0ad6d30f22
1 /*
2 BLAKE2 reference source code package - reference C implementations
4 Written in 2012 by Samuel Neves <sneves@dei.uc.pt>
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.
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/>.
14 #ifndef __BLAKE2_IMPL_H__
15 #define __BLAKE2_IMPL_H__
17 #include <stdint.h>
19 #include "utils.h"
21 static inline uint32_t load32( const void *src )
23 #if defined(NATIVE_LITTLE_ENDIAN)
24 return *( uint32_t * )( src );
25 #else
26 const uint8_t *p = ( uint8_t * )src;
27 uint32_t w = *p++;
28 w |= ( uint32_t )( *p++ ) << 8;
29 w |= ( uint32_t )( *p++ ) << 16;
30 w |= ( uint32_t )( *p++ ) << 24;
31 return w;
32 #endif
35 static inline uint64_t load64( const void *src )
37 #if defined(NATIVE_LITTLE_ENDIAN)
38 return *( uint64_t * )( src );
39 #else
40 const uint8_t *p = ( uint8_t * )src;
41 uint64_t w = *p++;
42 w |= ( uint64_t )( *p++ ) << 8;
43 w |= ( uint64_t )( *p++ ) << 16;
44 w |= ( uint64_t )( *p++ ) << 24;
45 w |= ( uint64_t )( *p++ ) << 32;
46 w |= ( uint64_t )( *p++ ) << 40;
47 w |= ( uint64_t )( *p++ ) << 48;
48 w |= ( uint64_t )( *p++ ) << 56;
49 return w;
50 #endif
53 static inline void store32( void *dst, uint32_t w )
55 #if defined(NATIVE_LITTLE_ENDIAN)
56 *( uint32_t * )( dst ) = w;
57 #else
58 uint8_t *p = ( uint8_t * )dst;
59 *p++ = ( uint8_t )w; w >>= 8;
60 *p++ = ( uint8_t )w; w >>= 8;
61 *p++ = ( uint8_t )w; w >>= 8;
62 *p++ = ( uint8_t )w;
63 #endif
66 static inline void store64( void *dst, uint64_t w )
68 #if defined(NATIVE_LITTLE_ENDIAN)
69 *( uint64_t * )( dst ) = w;
70 #else
71 uint8_t *p = ( uint8_t * )dst;
72 *p++ = ( uint8_t )w; w >>= 8;
73 *p++ = ( uint8_t )w; w >>= 8;
74 *p++ = ( uint8_t )w; w >>= 8;
75 *p++ = ( uint8_t )w; w >>= 8;
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;
80 #endif
83 static inline uint64_t load48( const void *src )
85 const uint8_t *p = ( const uint8_t * )src;
86 uint64_t w = *p++;
87 w |= ( uint64_t )( *p++ ) << 8;
88 w |= ( uint64_t )( *p++ ) << 16;
89 w |= ( uint64_t )( *p++ ) << 24;
90 w |= ( uint64_t )( *p++ ) << 32;
91 w |= ( uint64_t )( *p++ ) << 40;
92 return w;
95 static inline void store48( void *dst, uint64_t w )
97 uint8_t *p = ( uint8_t * )dst;
98 *p++ = ( uint8_t )w; w >>= 8;
99 *p++ = ( uint8_t )w; w >>= 8;
100 *p++ = ( uint8_t )w; w >>= 8;
101 *p++ = ( uint8_t )w; w >>= 8;
102 *p++ = ( uint8_t )w; w >>= 8;
103 *p++ = ( uint8_t )w;
106 static inline uint32_t rotl32( const uint32_t w, const unsigned c )
108 return ( w << c ) | ( w >> ( 32 - c ) );
111 static inline uint64_t rotl64( const uint64_t w, const unsigned c )
113 return ( w << c ) | ( w >> ( 64 - c ) );
116 static inline uint32_t rotr32( const uint32_t w, const unsigned c )
118 return ( w >> c ) | ( w << ( 32 - c ) );
121 static inline uint64_t rotr64( const uint64_t w, const unsigned c )
123 return ( w >> c ) | ( w << ( 64 - c ) );
126 /* prevents compiler optimizing out memset() */
127 static inline void secure_zero_memory( void *v, size_t n )
129 sodium_memzero(v, n);
132 #endif