blake2-impl.h - pee - Pee a password manager;Pee - because you have to... | |
git clone git://vernunftzentrum.de/pee.git | |
Log | |
Files | |
Refs | |
LICENSE | |
--- | |
blake2-impl.h (3399B) | |
--- | |
1 /* | |
2 BLAKE2 reference source code package - reference C implementations | |
3 | |
4 Written in 2012 by Samuel Neves <[email protected]> | |
5 | |
6 To the extent possible under law, the author(s) have dedicated all co… | |
7 and related and neighboring rights to this software to the public dom… | |
8 worldwide. This software is distributed without any warranty. | |
9 | |
10 You should have received a copy of the CC0 Public Domain Dedication a… | |
11 this software. If not, see <http://creativecommons.org/publicdomain/z… | |
12 */ | |
13 #pragma once | |
14 #ifndef __BLAKE2_IMPL_H__ | |
15 #define __BLAKE2_IMPL_H__ | |
16 | |
17 #include <stdint.h> | |
18 #include <string.h> | |
19 | |
20 static inline uint32_t load32( const void *src ) | |
21 { | |
22 #if defined(NATIVE_LITTLE_ENDIAN) | |
23 uint32_t w; | |
24 memcpy(&w, src, sizeof w); | |
25 return w; | |
26 #else | |
27 const uint8_t *p = ( const uint8_t * )src; | |
28 uint32_t w = *p++; | |
29 w |= ( uint32_t )( *p++ ) << 8; | |
30 w |= ( uint32_t )( *p++ ) << 16; | |
31 w |= ( uint32_t )( *p++ ) << 24; | |
32 return w; | |
33 #endif | |
34 } | |
35 | |
36 static inline uint64_t load64( const void *src ) | |
37 { | |
38 #if defined(NATIVE_LITTLE_ENDIAN) | |
39 uint64_t w; | |
40 memcpy(&w, src, sizeof w); | |
41 return w; | |
42 #else | |
43 const uint8_t *p = ( const uint8_t * )src; | |
44 uint64_t w = *p++; | |
45 w |= ( uint64_t )( *p++ ) << 8; | |
46 w |= ( uint64_t )( *p++ ) << 16; | |
47 w |= ( uint64_t )( *p++ ) << 24; | |
48 w |= ( uint64_t )( *p++ ) << 32; | |
49 w |= ( uint64_t )( *p++ ) << 40; | |
50 w |= ( uint64_t )( *p++ ) << 48; | |
51 w |= ( uint64_t )( *p++ ) << 56; | |
52 return w; | |
53 #endif | |
54 } | |
55 | |
56 static inline void store32( void *dst, uint32_t w ) | |
57 { | |
58 #if defined(NATIVE_LITTLE_ENDIAN) | |
59 memcpy(dst, &w, sizeof w); | |
60 #else | |
61 uint8_t *p = ( uint8_t * )dst; | |
62 *p++ = ( uint8_t )w; w >>= 8; | |
63 *p++ = ( uint8_t )w; w >>= 8; | |
64 *p++ = ( uint8_t )w; w >>= 8; | |
65 *p++ = ( uint8_t )w; | |
66 #endif | |
67 } | |
68 | |
69 static inline void store64( void *dst, uint64_t w ) | |
70 { | |
71 #if defined(NATIVE_LITTLE_ENDIAN) | |
72 memcpy(dst, &w, sizeof w); | |
73 #else | |
74 uint8_t *p = ( uint8_t * )dst; | |
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; w >>= 8; | |
80 *p++ = ( uint8_t )w; w >>= 8; | |
81 *p++ = ( uint8_t )w; w >>= 8; | |
82 *p++ = ( uint8_t )w; | |
83 #endif | |
84 } | |
85 | |
86 static inline uint64_t load48( const void *src ) | |
87 { | |
88 const uint8_t *p = ( const uint8_t * )src; | |
89 uint64_t w = *p++; | |
90 w |= ( uint64_t )( *p++ ) << 8; | |
91 w |= ( uint64_t )( *p++ ) << 16; | |
92 w |= ( uint64_t )( *p++ ) << 24; | |
93 w |= ( uint64_t )( *p++ ) << 32; | |
94 w |= ( uint64_t )( *p++ ) << 40; | |
95 return w; | |
96 } | |
97 | |
98 static inline void store48( void *dst, uint64_t w ) | |
99 { | |
100 uint8_t *p = ( uint8_t * )dst; | |
101 *p++ = ( uint8_t )w; w >>= 8; | |
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; | |
107 } | |
108 | |
109 static inline uint32_t rotl32( const uint32_t w, const unsigned c ) | |
110 { | |
111 return ( w << c ) | ( w >> ( 32 - c ) ); | |
112 } | |
113 | |
114 static inline uint64_t rotl64( const uint64_t w, const unsigned c ) | |
115 { | |
116 return ( w << c ) | ( w >> ( 64 - c ) ); | |
117 } | |
118 | |
119 static inline uint32_t rotr32( const uint32_t w, const unsigned c ) | |
120 { | |
121 return ( w >> c ) | ( w << ( 32 - c ) ); | |
122 } | |
123 | |
124 static inline uint64_t rotr64( const uint64_t w, const unsigned c ) | |
125 { | |
126 return ( w >> c ) | ( w << ( 64 - c ) ); | |
127 } | |
128 | |
129 /* prevents compiler optimizing out memset() */ | |
130 static inline void secure_zero_memory(void *v, size_t n) | |
131 { | |
132 static void *(*const volatile memset_v)(void *, int, size_t) = &memset; | |
133 memset_v(v, 0, n); | |
134 } | |
135 | |
136 #endif | |
137 |