libsodium 1.0.8
[tomato.git] / release / src / router / libsodium / src / libsodium / crypto_generichash / blake2 / ref / blake2-impl.h
blobb14728dfd9b5402d5b8cb2f32499580b461a6c78
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>
18 #include <string.h>
20 #include "utils.h"
22 static inline uint32_t load32( const void *src )
24 #ifdef 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
38 static inline uint64_t load64( const void *src )
40 #ifdef 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
58 static inline void store32( void *dst, uint32_t w )
60 #ifdef 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
71 static inline void store64( void *dst, uint64_t w )
73 #ifdef 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
88 static inline uint64_t load48( const void *src )
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;
100 static inline void store48( void *dst, uint64_t w )
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;
111 static inline uint32_t rotl32( const uint32_t w, const unsigned c )
113 return ( w << c ) | ( w >> ( 32 - c ) );
116 static inline uint64_t rotl64( const uint64_t w, const unsigned c )
118 return ( w << c ) | ( w >> ( 64 - c ) );
121 static inline uint32_t rotr32( const uint32_t w, const unsigned c )
123 return ( w >> c ) | ( w << ( 32 - c ) );
126 static inline uint64_t rotr64( const uint64_t w, const unsigned c )
128 return ( w >> c ) | ( w << ( 64 - c ) );
131 /* prevents compiler optimizing out memset() */
132 static inline void secure_zero_memory( void *v, size_t n )
134 sodium_memzero(v, n);
137 #endif