/src/u-boot/lib/blake2/blake2-impl.h
Line | Count | Source |
1 | | /* SPDX-License-Identifier: CC0-1.0 */ |
2 | | /* |
3 | | BLAKE2 reference source code package - reference C implementations |
4 | | |
5 | | Copyright 2012, Samuel Neves <sneves@dei.uc.pt>. You may use this under the |
6 | | terms of the CC0, the OpenSSL Licence, or the Apache Public License 2.0, at |
7 | | your option. The terms of these licenses can be found at: |
8 | | |
9 | | - CC0 1.0 Universal : http://creativecommons.org/publicdomain/zero/1.0 |
10 | | - OpenSSL license : https://www.openssl.org/source/license.html |
11 | | - Apache 2.0 : http://www.apache.org/licenses/LICENSE-2.0 |
12 | | |
13 | | More information about the BLAKE2 hash function can be found at |
14 | | https://blake2.net. |
15 | | */ |
16 | | |
17 | | /* |
18 | | * Cross-ported from BLAKE2 (https://github.com/BLAKE2/BLAKE2). |
19 | | * Modifications includes: |
20 | | * |
21 | | * - Remove unsupported compilers like MSC/CPP |
22 | | * - Use u-boot library functions/macros |
23 | | */ |
24 | | #ifndef BLAKE2_IMPL_H |
25 | | #define BLAKE2_IMPL_H |
26 | | |
27 | | #include <stdint.h> |
28 | | #include <string.h> |
29 | | |
30 | | #define BLAKE2_INLINE inline |
31 | | |
32 | | #ifdef __LITTLE_ENDIAN |
33 | | # define NATIVE_LITTLE_ENDIAN |
34 | | #endif |
35 | | |
36 | | static BLAKE2_INLINE uint32_t load32( const void *src ) |
37 | 0 | { |
38 | 0 | #if defined(NATIVE_LITTLE_ENDIAN) |
39 | 0 | uint32_t w; |
40 | 0 | memcpy(&w, src, sizeof w); |
41 | 0 | return w; |
42 | 0 | #else |
43 | 0 | const uint8_t *p = ( const uint8_t * )src; |
44 | 0 | return (( uint32_t )( p[0] ) << 0) | |
45 | 0 | (( uint32_t )( p[1] ) << 8) | |
46 | 0 | (( uint32_t )( p[2] ) << 16) | |
47 | 0 | (( uint32_t )( p[3] ) << 24) ; |
48 | 0 | #endif |
49 | 0 | } |
50 | | |
51 | | static BLAKE2_INLINE uint64_t load64( const void *src ) |
52 | 0 | { |
53 | | #if defined(NATIVE_LITTLE_ENDIAN) |
54 | | uint64_t w; |
55 | | memcpy(&w, src, sizeof w); |
56 | | return w; |
57 | | #else |
58 | 0 | const uint8_t *p = ( const uint8_t * )src; |
59 | 0 | return (( uint64_t )( p[0] ) << 0) | |
60 | 0 | (( uint64_t )( p[1] ) << 8) | |
61 | 0 | (( uint64_t )( p[2] ) << 16) | |
62 | 0 | (( uint64_t )( p[3] ) << 24) | |
63 | 0 | (( uint64_t )( p[4] ) << 32) | |
64 | 0 | (( uint64_t )( p[5] ) << 40) | |
65 | 0 | (( uint64_t )( p[6] ) << 48) | |
66 | 0 | (( uint64_t )( p[7] ) << 56) ; |
67 | 0 | #endif |
68 | 0 | } |
69 | | |
70 | | static BLAKE2_INLINE uint16_t load16( const void *src ) |
71 | 0 | { |
72 | 0 | #if defined(NATIVE_LITTLE_ENDIAN) |
73 | 0 | uint16_t w; |
74 | 0 | memcpy(&w, src, sizeof w); |
75 | 0 | return w; |
76 | 0 | #else |
77 | 0 | const uint8_t *p = ( const uint8_t * )src; |
78 | 0 | return ( uint16_t )((( uint32_t )( p[0] ) << 0) | |
79 | 0 | (( uint32_t )( p[1] ) << 8)); |
80 | 0 | #endif |
81 | 0 | } |
82 | | |
83 | | static BLAKE2_INLINE void store16( void *dst, uint16_t w ) |
84 | 0 | { |
85 | 0 | #if defined(NATIVE_LITTLE_ENDIAN) |
86 | 0 | memcpy(dst, &w, sizeof w); |
87 | 0 | #else |
88 | 0 | uint8_t *p = ( uint8_t * )dst; |
89 | 0 | *p++ = ( uint8_t )w; w >>= 8; |
90 | 0 | *p++ = ( uint8_t )w; |
91 | 0 | #endif |
92 | 0 | } |
93 | | |
94 | | static BLAKE2_INLINE void store32( void *dst, uint32_t w ) |
95 | 0 | { |
96 | | #if defined(NATIVE_LITTLE_ENDIAN) |
97 | | memcpy(dst, &w, sizeof w); |
98 | | #else |
99 | 0 | uint8_t *p = ( uint8_t * )dst; |
100 | 0 | p[0] = (uint8_t)(w >> 0); |
101 | 0 | p[1] = (uint8_t)(w >> 8); |
102 | 0 | p[2] = (uint8_t)(w >> 16); |
103 | 0 | p[3] = (uint8_t)(w >> 24); |
104 | 0 | #endif |
105 | 0 | } |
106 | | |
107 | | static BLAKE2_INLINE void store64( void *dst, uint64_t w ) |
108 | 0 | { |
109 | | #if defined(NATIVE_LITTLE_ENDIAN) |
110 | | memcpy(dst, &w, sizeof w); |
111 | | #else |
112 | 0 | uint8_t *p = ( uint8_t * )dst; |
113 | 0 | p[0] = (uint8_t)(w >> 0); |
114 | 0 | p[1] = (uint8_t)(w >> 8); |
115 | 0 | p[2] = (uint8_t)(w >> 16); |
116 | 0 | p[3] = (uint8_t)(w >> 24); |
117 | 0 | p[4] = (uint8_t)(w >> 32); |
118 | 0 | p[5] = (uint8_t)(w >> 40); |
119 | 0 | p[6] = (uint8_t)(w >> 48); |
120 | 0 | p[7] = (uint8_t)(w >> 56); |
121 | 0 | #endif |
122 | 0 | } |
123 | | |
124 | | static BLAKE2_INLINE uint64_t load48( const void *src ) |
125 | 0 | { |
126 | 0 | const uint8_t *p = ( const uint8_t * )src; |
127 | 0 | return (( uint64_t )( p[0] ) << 0) | |
128 | 0 | (( uint64_t )( p[1] ) << 8) | |
129 | 0 | (( uint64_t )( p[2] ) << 16) | |
130 | 0 | (( uint64_t )( p[3] ) << 24) | |
131 | 0 | (( uint64_t )( p[4] ) << 32) | |
132 | 0 | (( uint64_t )( p[5] ) << 40) ; |
133 | 0 | } |
134 | | |
135 | | static BLAKE2_INLINE void store48( void *dst, uint64_t w ) |
136 | 0 | { |
137 | 0 | uint8_t *p = ( uint8_t * )dst; |
138 | 0 | p[0] = (uint8_t)(w >> 0); |
139 | 0 | p[1] = (uint8_t)(w >> 8); |
140 | 0 | p[2] = (uint8_t)(w >> 16); |
141 | 0 | p[3] = (uint8_t)(w >> 24); |
142 | 0 | p[4] = (uint8_t)(w >> 32); |
143 | 0 | p[5] = (uint8_t)(w >> 40); |
144 | 0 | } |
145 | | |
146 | | static BLAKE2_INLINE uint32_t rotr32( const uint32_t w, const unsigned c ) |
147 | 0 | { |
148 | 0 | return ( w >> c ) | ( w << ( 32 - c ) ); |
149 | 0 | } |
150 | | |
151 | | static BLAKE2_INLINE uint64_t rotr64( const uint64_t w, const unsigned c ) |
152 | 0 | { |
153 | 0 | return ( w >> c ) | ( w << ( 64 - c ) ); |
154 | 0 | } |
155 | | |
156 | | /* prevents compiler optimizing out memset() */ |
157 | | static BLAKE2_INLINE void secure_zero_memory(void *v, size_t n) |
158 | 0 | { |
159 | 0 | static void *(*const volatile memset_v)(void *, int, size_t) = &memset; |
160 | 0 | memset_v(v, 0, n); |
161 | 0 | } |
162 | | |
163 | | #endif |