build: remove unnecessary executable bits
[vpp.git] / src / plugins / wireguard / blake / blake2-impl.h
1 /*
2  * Copyright (c) 2020 Doc.ai and/or its affiliates.
3  * Copyright (c) 2012 Samuel Neves <sneves@dei.uc.pt>.
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /*
18    More information about the BLAKE2 hash function can be found at
19    https://blake2.net.
20 */
21 #ifndef __included_crypto_blake2_impl_h__
22 #define __included_crypto_blake2_impl_h__
23
24 #include <stdint.h>
25 #include <string.h>
26 #include <vppinfra/byte_order.h>
27
28 #if defined(CLIB_ARCH_IS_LITTLE_ENDIAN)
29 #define NATIVE_LITTLE_ENDIAN
30 #endif
31
32 #if !defined(__cplusplus) && (!defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L)
33 #if   defined(_MSC_VER)
34 #define BLAKE2_INLINE __inline
35 #elif defined(__GNUC__)
36 #define BLAKE2_INLINE __inline__
37 #else
38 #define BLAKE2_INLINE
39 #endif
40 #else
41 #define BLAKE2_INLINE inline
42 #endif
43
44 static BLAKE2_INLINE uint32_t
45 load32 (const void *src)
46 {
47 #if defined(NATIVE_LITTLE_ENDIAN)
48   uint32_t w;
49   memcpy (&w, src, sizeof w);
50   return w;
51 #else
52   const uint8_t *p = (const uint8_t *) src;
53   return ((uint32_t) (p[0]) << 0) |
54     ((uint32_t) (p[1]) << 8) |
55     ((uint32_t) (p[2]) << 16) | ((uint32_t) (p[3]) << 24);
56 #endif
57 }
58
59 static BLAKE2_INLINE uint64_t
60 load64 (const void *src)
61 {
62 #if defined(NATIVE_LITTLE_ENDIAN)
63   uint64_t w;
64   memcpy (&w, src, sizeof w);
65   return w;
66 #else
67   const uint8_t *p = (const uint8_t *) src;
68   return ((uint64_t) (p[0]) << 0) |
69     ((uint64_t) (p[1]) << 8) |
70     ((uint64_t) (p[2]) << 16) |
71     ((uint64_t) (p[3]) << 24) |
72     ((uint64_t) (p[4]) << 32) |
73     ((uint64_t) (p[5]) << 40) |
74     ((uint64_t) (p[6]) << 48) | ((uint64_t) (p[7]) << 56);
75 #endif
76 }
77
78 static BLAKE2_INLINE uint16_t
79 load16 (const void *src)
80 {
81 #if defined(NATIVE_LITTLE_ENDIAN)
82   uint16_t w;
83   memcpy (&w, src, sizeof w);
84   return w;
85 #else
86   const uint8_t *p = (const uint8_t *) src;
87   return (uint16_t) (((uint32_t) (p[0]) << 0) | ((uint32_t) (p[1]) << 8));
88 #endif
89 }
90
91 static BLAKE2_INLINE void
92 store16 (void *dst, uint16_t w)
93 {
94 #if defined(NATIVE_LITTLE_ENDIAN)
95   memcpy (dst, &w, sizeof w);
96 #else
97   uint8_t *p = (uint8_t *) dst;
98   *p++ = (uint8_t) w;
99   w >>= 8;
100   *p++ = (uint8_t) w;
101 #endif
102 }
103
104 static BLAKE2_INLINE void
105 store32 (void *dst, uint32_t w)
106 {
107 #if defined(NATIVE_LITTLE_ENDIAN)
108   memcpy (dst, &w, sizeof w);
109 #else
110   uint8_t *p = (uint8_t *) dst;
111   p[0] = (uint8_t) (w >> 0);
112   p[1] = (uint8_t) (w >> 8);
113   p[2] = (uint8_t) (w >> 16);
114   p[3] = (uint8_t) (w >> 24);
115 #endif
116 }
117
118 static BLAKE2_INLINE void
119 store64 (void *dst, uint64_t w)
120 {
121 #if defined(NATIVE_LITTLE_ENDIAN)
122   memcpy (dst, &w, sizeof w);
123 #else
124   uint8_t *p = (uint8_t *) dst;
125   p[0] = (uint8_t) (w >> 0);
126   p[1] = (uint8_t) (w >> 8);
127   p[2] = (uint8_t) (w >> 16);
128   p[3] = (uint8_t) (w >> 24);
129   p[4] = (uint8_t) (w >> 32);
130   p[5] = (uint8_t) (w >> 40);
131   p[6] = (uint8_t) (w >> 48);
132   p[7] = (uint8_t) (w >> 56);
133 #endif
134 }
135
136 static BLAKE2_INLINE uint64_t
137 load48 (const void *src)
138 {
139   const uint8_t *p = (const uint8_t *) src;
140   return ((uint64_t) (p[0]) << 0) |
141     ((uint64_t) (p[1]) << 8) |
142     ((uint64_t) (p[2]) << 16) |
143     ((uint64_t) (p[3]) << 24) |
144     ((uint64_t) (p[4]) << 32) | ((uint64_t) (p[5]) << 40);
145 }
146
147 static BLAKE2_INLINE void
148 store48 (void *dst, uint64_t w)
149 {
150   uint8_t *p = (uint8_t *) dst;
151   p[0] = (uint8_t) (w >> 0);
152   p[1] = (uint8_t) (w >> 8);
153   p[2] = (uint8_t) (w >> 16);
154   p[3] = (uint8_t) (w >> 24);
155   p[4] = (uint8_t) (w >> 32);
156   p[5] = (uint8_t) (w >> 40);
157 }
158
159 static BLAKE2_INLINE uint32_t
160 rotr32 (const uint32_t w, const unsigned c)
161 {
162   return (w >> c) | (w << (32 - c));
163 }
164
165 static BLAKE2_INLINE uint64_t
166 rotr64 (const uint64_t w, const unsigned c)
167 {
168   return (w >> c) | (w << (64 - c));
169 }
170
171 /* prevents compiler optimizing out memset() */
172 static BLAKE2_INLINE void
173 secure_zero_memory (void *v, size_t n)
174 {
175   static void *(*const volatile memset_v) (void *, int, size_t) = &memset;
176   memset_v (v, 0, n);
177 }
178
179 #endif //__included_crypto_blake2_impl_h__
180
181 /*
182  * fd.io coding-style-patch-verification: ON
183  *
184  * Local Variables:
185  * eval: (c-set-style "gnu")
186  * End:
187  */