1 | /* misc.c
|
---|
2 | *
|
---|
3 | * Copyright (C) 2006-2017 wolfSSL Inc.
|
---|
4 | *
|
---|
5 | * This file is part of wolfSSL.
|
---|
6 | *
|
---|
7 | * wolfSSL is free software; you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation; either version 2 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * wolfSSL is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with this program; if not, write to the Free Software
|
---|
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
|
---|
20 | */
|
---|
21 |
|
---|
22 |
|
---|
23 | #ifdef HAVE_CONFIG_H
|
---|
24 | #include <config.h>
|
---|
25 | #endif
|
---|
26 |
|
---|
27 | #include <wolfssl/wolfcrypt/settings.h>
|
---|
28 |
|
---|
29 | #ifndef WOLF_CRYPT_MISC_C
|
---|
30 | #define WOLF_CRYPT_MISC_C
|
---|
31 |
|
---|
32 | #include <wolfssl/wolfcrypt/misc.h>
|
---|
33 |
|
---|
34 | /* inlining these functions is a huge speed increase and a small size decrease,
|
---|
35 | because the functions are smaller than function call setup/cleanup, e.g.,
|
---|
36 | md5 benchmark is twice as fast with inline. If you don't want it, then
|
---|
37 | define NO_INLINE and compile this file into wolfssl, otherwise it's used as
|
---|
38 | a source header
|
---|
39 | */
|
---|
40 |
|
---|
41 | #ifdef NO_INLINE
|
---|
42 | #define STATIC
|
---|
43 | #else
|
---|
44 | #define STATIC static
|
---|
45 | #endif
|
---|
46 |
|
---|
47 | /* Check for if compiling misc.c when not needed. */
|
---|
48 | #if !defined(WOLFSSL_MISC_INCLUDED) && !defined(NO_INLINE)
|
---|
49 | #warning misc.c does not need to be compiled when using inline (NO_INLINE not defined)
|
---|
50 |
|
---|
51 | #else
|
---|
52 |
|
---|
53 |
|
---|
54 | #if defined(__ICCARM__)
|
---|
55 | #include <intrinsics.h>
|
---|
56 | #endif
|
---|
57 |
|
---|
58 |
|
---|
59 | #ifdef INTEL_INTRINSICS
|
---|
60 |
|
---|
61 | #include <stdlib.h> /* get intrinsic definitions */
|
---|
62 |
|
---|
63 | /* for non visual studio probably need no long version, 32 bit only
|
---|
64 | * i.e., _rotl and _rotr */
|
---|
65 | #pragma intrinsic(_lrotl, _lrotr)
|
---|
66 |
|
---|
67 | STATIC INLINE word32 rotlFixed(word32 x, word32 y)
|
---|
68 | {
|
---|
69 | return y ? _lrotl(x, y) : x;
|
---|
70 | }
|
---|
71 |
|
---|
72 | STATIC INLINE word32 rotrFixed(word32 x, word32 y)
|
---|
73 | {
|
---|
74 | return y ? _lrotr(x, y) : x;
|
---|
75 | }
|
---|
76 |
|
---|
77 | #else /* generic */
|
---|
78 |
|
---|
79 | STATIC INLINE word32 rotlFixed(word32 x, word32 y)
|
---|
80 | {
|
---|
81 | return (x << y) | (x >> (sizeof(y) * 8 - y));
|
---|
82 | }
|
---|
83 |
|
---|
84 |
|
---|
85 | STATIC INLINE word32 rotrFixed(word32 x, word32 y)
|
---|
86 | {
|
---|
87 | return (x >> y) | (x << (sizeof(y) * 8 - y));
|
---|
88 | }
|
---|
89 |
|
---|
90 | #endif
|
---|
91 |
|
---|
92 |
|
---|
93 | STATIC INLINE word32 ByteReverseWord32(word32 value)
|
---|
94 | {
|
---|
95 | #ifdef PPC_INTRINSICS
|
---|
96 | /* PPC: load reverse indexed instruction */
|
---|
97 | return (word32)__lwbrx(&value,0);
|
---|
98 | #elif defined(__ICCARM__)
|
---|
99 | return (word32)__REV(value);
|
---|
100 | #elif defined(KEIL_INTRINSICS)
|
---|
101 | return (word32)__rev(value);
|
---|
102 | #elif defined(FAST_ROTATE)
|
---|
103 | /* 5 instructions with rotate instruction, 9 without */
|
---|
104 | return (rotrFixed(value, 8U) & 0xff00ff00) |
|
---|
105 | (rotlFixed(value, 8U) & 0x00ff00ff);
|
---|
106 | #else
|
---|
107 | /* 6 instructions with rotate instruction, 8 without */
|
---|
108 | value = ((value & 0xFF00FF00) >> 8) | ((value & 0x00FF00FF) << 8);
|
---|
109 | return rotlFixed(value, 16U);
|
---|
110 | #endif
|
---|
111 | }
|
---|
112 |
|
---|
113 |
|
---|
114 | STATIC INLINE void ByteReverseWords(word32* out, const word32* in,
|
---|
115 | word32 byteCount)
|
---|
116 | {
|
---|
117 | word32 count = byteCount/(word32)sizeof(word32), i;
|
---|
118 |
|
---|
119 | for (i = 0; i < count; i++)
|
---|
120 | out[i] = ByteReverseWord32(in[i]);
|
---|
121 |
|
---|
122 | }
|
---|
123 |
|
---|
124 |
|
---|
125 | #ifdef WORD64_AVAILABLE
|
---|
126 |
|
---|
127 |
|
---|
128 | STATIC INLINE word64 rotlFixed64(word64 x, word64 y)
|
---|
129 | {
|
---|
130 | return (x << y) | (x >> (sizeof(y) * 8 - y));
|
---|
131 | }
|
---|
132 |
|
---|
133 |
|
---|
134 | STATIC INLINE word64 rotrFixed64(word64 x, word64 y)
|
---|
135 | {
|
---|
136 | return (x >> y) | (x << (sizeof(y) * 8 - y));
|
---|
137 | }
|
---|
138 |
|
---|
139 |
|
---|
140 | STATIC INLINE word64 ByteReverseWord64(word64 value)
|
---|
141 | {
|
---|
142 | #if defined(WOLFCRYPT_SLOW_WORD64)
|
---|
143 | return (word64)(ByteReverseWord32((word32)value)) << 32 |
|
---|
144 | ByteReverseWord32((word32)(value>>32));
|
---|
145 | #else
|
---|
146 | value = ((value & W64LIT(0xFF00FF00FF00FF00)) >> 8) |
|
---|
147 | ((value & W64LIT(0x00FF00FF00FF00FF)) << 8);
|
---|
148 | value = ((value & W64LIT(0xFFFF0000FFFF0000)) >> 16) |
|
---|
149 | ((value & W64LIT(0x0000FFFF0000FFFF)) << 16);
|
---|
150 | return rotlFixed64(value, 32U);
|
---|
151 | #endif
|
---|
152 | }
|
---|
153 |
|
---|
154 |
|
---|
155 | STATIC INLINE void ByteReverseWords64(word64* out, const word64* in,
|
---|
156 | word32 byteCount)
|
---|
157 | {
|
---|
158 | word32 count = byteCount/(word32)sizeof(word64), i;
|
---|
159 |
|
---|
160 | for (i = 0; i < count; i++)
|
---|
161 | out[i] = ByteReverseWord64(in[i]);
|
---|
162 |
|
---|
163 | }
|
---|
164 |
|
---|
165 | #endif /* WORD64_AVAILABLE */
|
---|
166 |
|
---|
167 |
|
---|
168 | STATIC INLINE void XorWords(wolfssl_word* r, const wolfssl_word* a, word32 n)
|
---|
169 | {
|
---|
170 | word32 i;
|
---|
171 |
|
---|
172 | for (i = 0; i < n; i++) r[i] ^= a[i];
|
---|
173 | }
|
---|
174 |
|
---|
175 |
|
---|
176 | STATIC INLINE void xorbuf(void* buf, const void* mask, word32 count)
|
---|
177 | {
|
---|
178 | if (((wolfssl_word)buf | (wolfssl_word)mask | count) % WOLFSSL_WORD_SIZE == 0)
|
---|
179 | XorWords( (wolfssl_word*)buf,
|
---|
180 | (const wolfssl_word*)mask, count / WOLFSSL_WORD_SIZE);
|
---|
181 | else {
|
---|
182 | word32 i;
|
---|
183 | byte* b = (byte*)buf;
|
---|
184 | const byte* m = (const byte*)mask;
|
---|
185 |
|
---|
186 | for (i = 0; i < count; i++) b[i] ^= m[i];
|
---|
187 | }
|
---|
188 | }
|
---|
189 |
|
---|
190 |
|
---|
191 | /* Make sure compiler doesn't skip */
|
---|
192 | STATIC INLINE void ForceZero(const void* mem, word32 len)
|
---|
193 | {
|
---|
194 | volatile byte* z = (volatile byte*)mem;
|
---|
195 | #ifdef WOLFSSL_X86_64_BUILD
|
---|
196 | volatile word64* w;
|
---|
197 |
|
---|
198 | for (w = (volatile word64*)z; len >= sizeof(*w); len -= sizeof(*w))
|
---|
199 | *w++ = 0;
|
---|
200 | z = (volatile byte*)w;
|
---|
201 | #endif
|
---|
202 | while (len--) *z++ = 0;
|
---|
203 | }
|
---|
204 |
|
---|
205 |
|
---|
206 | /* check all length bytes for equality, return 0 on success */
|
---|
207 | STATIC INLINE int ConstantCompare(const byte* a, const byte* b, int length)
|
---|
208 | {
|
---|
209 | int i;
|
---|
210 | int compareSum = 0;
|
---|
211 |
|
---|
212 | for (i = 0; i < length; i++) {
|
---|
213 | compareSum |= a[i] ^ b[i];
|
---|
214 | }
|
---|
215 |
|
---|
216 | return compareSum;
|
---|
217 | }
|
---|
218 |
|
---|
219 |
|
---|
220 | #ifndef WOLFSSL_HAVE_MIN
|
---|
221 | #define WOLFSSL_HAVE_MIN
|
---|
222 | #if defined(HAVE_FIPS) && !defined(min) /* so ifdef check passes */
|
---|
223 | #define min min
|
---|
224 | #endif
|
---|
225 | STATIC INLINE word32 min(word32 a, word32 b)
|
---|
226 | {
|
---|
227 | return a > b ? b : a;
|
---|
228 | }
|
---|
229 | #endif /* !WOLFSSL_HAVE_MIN */
|
---|
230 |
|
---|
231 | #ifndef WOLFSSL_HAVE_MAX
|
---|
232 | #define WOLFSSL_HAVE_MAX
|
---|
233 | #if defined(HAVE_FIPS) && !defined(max) /* so ifdef check passes */
|
---|
234 | #define max max
|
---|
235 | #endif
|
---|
236 | STATIC INLINE word32 max(word32 a, word32 b)
|
---|
237 | {
|
---|
238 | return a > b ? a : b;
|
---|
239 | }
|
---|
240 | #endif /* !WOLFSSL_HAVE_MAX */
|
---|
241 |
|
---|
242 | /* converts a 32 bit integer to 24 bit */
|
---|
243 | STATIC INLINE void c32to24(word32 in, word24 out)
|
---|
244 | {
|
---|
245 | out[0] = (in >> 16) & 0xff;
|
---|
246 | out[1] = (in >> 8) & 0xff;
|
---|
247 | out[2] = in & 0xff;
|
---|
248 | }
|
---|
249 |
|
---|
250 | /* convert 16 bit integer to opaque */
|
---|
251 | STATIC INLINE void c16toa(word16 u16, byte* c)
|
---|
252 | {
|
---|
253 | c[0] = (u16 >> 8) & 0xff;
|
---|
254 | c[1] = u16 & 0xff;
|
---|
255 | }
|
---|
256 |
|
---|
257 | /* convert 32 bit integer to opaque */
|
---|
258 | STATIC INLINE void c32toa(word32 u32, byte* c)
|
---|
259 | {
|
---|
260 | c[0] = (u32 >> 24) & 0xff;
|
---|
261 | c[1] = (u32 >> 16) & 0xff;
|
---|
262 | c[2] = (u32 >> 8) & 0xff;
|
---|
263 | c[3] = u32 & 0xff;
|
---|
264 | }
|
---|
265 |
|
---|
266 | /* convert a 24 bit integer into a 32 bit one */
|
---|
267 | STATIC INLINE void c24to32(const word24 u24, word32* u32)
|
---|
268 | {
|
---|
269 | *u32 = (u24[0] << 16) | (u24[1] << 8) | u24[2];
|
---|
270 | }
|
---|
271 |
|
---|
272 |
|
---|
273 | /* convert opaque to 24 bit integer */
|
---|
274 | STATIC INLINE void ato24(const byte* c, word32* u24)
|
---|
275 | {
|
---|
276 | *u24 = (c[0] << 16) | (c[1] << 8) | c[2];
|
---|
277 | }
|
---|
278 |
|
---|
279 | /* convert opaque to 16 bit integer */
|
---|
280 | STATIC INLINE void ato16(const byte* c, word16* u16)
|
---|
281 | {
|
---|
282 | *u16 = (word16) ((c[0] << 8) | (c[1]));
|
---|
283 | }
|
---|
284 |
|
---|
285 | /* convert opaque to 32 bit integer */
|
---|
286 | STATIC INLINE void ato32(const byte* c, word32* u32)
|
---|
287 | {
|
---|
288 | *u32 = (c[0] << 24) | (c[1] << 16) | (c[2] << 8) | c[3];
|
---|
289 | }
|
---|
290 |
|
---|
291 |
|
---|
292 | STATIC INLINE word32 btoi(byte b)
|
---|
293 | {
|
---|
294 | return (word32)(b - 0x30);
|
---|
295 | }
|
---|
296 |
|
---|
297 |
|
---|
298 |
|
---|
299 | #undef STATIC
|
---|
300 |
|
---|
301 | #endif /* !WOLFSSL_MISC_INCLUDED && !NO_INLINE */
|
---|
302 |
|
---|
303 | #endif /* WOLF_CRYPT_MISC_C */
|
---|