source: azure_iot_hub/trunk/wolfssl-3.15.7/wolfcrypt/src/misc.c@ 388

Last change on this file since 388 was 388, checked in by coas-nagasima, 5 years ago

Azure IoT Hub Device C SDK を使ったサンプルの追加

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-csrc
File size: 9.6 KB
Line 
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 #ifndef WOLFSSL_IGNORE_FILE_WARN
50 #warning misc.c does not need to be compiled when using inline (NO_INLINE not defined)
51 #endif
52
53#else
54
55
56#if defined(__ICCARM__)
57 #include <intrinsics.h>
58#endif
59
60
61#ifdef INTEL_INTRINSICS
62
63 #include <stdlib.h> /* get intrinsic definitions */
64
65 /* for non visual studio probably need no long version, 32 bit only
66 * i.e., _rotl and _rotr */
67 #pragma intrinsic(_lrotl, _lrotr)
68
69 STATIC WC_INLINE word32 rotlFixed(word32 x, word32 y)
70 {
71 return y ? _lrotl(x, y) : x;
72 }
73
74 STATIC WC_INLINE word32 rotrFixed(word32 x, word32 y)
75 {
76 return y ? _lrotr(x, y) : x;
77 }
78
79#else /* generic */
80
81 STATIC WC_INLINE word32 rotlFixed(word32 x, word32 y)
82 {
83 return (x << y) | (x >> (sizeof(y) * 8 - y));
84 }
85
86
87 STATIC WC_INLINE word32 rotrFixed(word32 x, word32 y)
88 {
89 return (x >> y) | (x << (sizeof(y) * 8 - y));
90 }
91
92#endif
93
94
95STATIC WC_INLINE word32 ByteReverseWord32(word32 value)
96{
97#ifdef PPC_INTRINSICS
98 /* PPC: load reverse indexed instruction */
99 return (word32)__lwbrx(&value,0);
100#elif defined(__ICCARM__)
101 return (word32)__REV(value);
102#elif defined(KEIL_INTRINSICS)
103 return (word32)__rev(value);
104#elif defined(WOLF_ALLOW_BUILTIN) && \
105 defined(__GNUC_PREREQ) && __GNUC_PREREQ(4, 3)
106 return (word32)__builtin_bswap32(value);
107#elif defined(FAST_ROTATE)
108 /* 5 instructions with rotate instruction, 9 without */
109 return (rotrFixed(value, 8U) & 0xff00ff00) |
110 (rotlFixed(value, 8U) & 0x00ff00ff);
111#else
112 /* 6 instructions with rotate instruction, 8 without */
113 value = ((value & 0xFF00FF00) >> 8) | ((value & 0x00FF00FF) << 8);
114 return rotlFixed(value, 16U);
115#endif
116}
117
118
119STATIC WC_INLINE void ByteReverseWords(word32* out, const word32* in,
120 word32 byteCount)
121{
122 word32 count = byteCount/(word32)sizeof(word32), i;
123
124 for (i = 0; i < count; i++)
125 out[i] = ByteReverseWord32(in[i]);
126
127}
128
129
130#if defined(WORD64_AVAILABLE) && !defined(WOLFSSL_NO_WORD64_OPS)
131
132
133STATIC WC_INLINE word64 rotlFixed64(word64 x, word64 y)
134{
135 return (x << y) | (x >> (sizeof(y) * 8 - y));
136}
137
138
139STATIC WC_INLINE word64 rotrFixed64(word64 x, word64 y)
140{
141 return (x >> y) | (x << (sizeof(y) * 8 - y));
142}
143
144
145STATIC WC_INLINE word64 ByteReverseWord64(word64 value)
146{
147#if defined(WOLF_ALLOW_BUILTIN) && defined(__GNUC_PREREQ) && __GNUC_PREREQ(4, 3)
148 return (word64)__builtin_bswap64(value);
149#elif defined(WOLFCRYPT_SLOW_WORD64)
150 return (word64)((word64)ByteReverseWord32((word32) value)) << 32 |
151 (word64)ByteReverseWord32((word32)(value >> 32));
152#else
153 value = ((value & W64LIT(0xFF00FF00FF00FF00)) >> 8) |
154 ((value & W64LIT(0x00FF00FF00FF00FF)) << 8);
155 value = ((value & W64LIT(0xFFFF0000FFFF0000)) >> 16) |
156 ((value & W64LIT(0x0000FFFF0000FFFF)) << 16);
157 return rotlFixed64(value, 32U);
158#endif
159}
160
161
162STATIC WC_INLINE void ByteReverseWords64(word64* out, const word64* in,
163 word32 byteCount)
164{
165 word32 count = byteCount/(word32)sizeof(word64), i;
166
167 for (i = 0; i < count; i++)
168 out[i] = ByteReverseWord64(in[i]);
169
170}
171
172#endif /* WORD64_AVAILABLE && !WOLFSSL_NO_WORD64_OPS */
173
174#ifndef WOLFSSL_NO_XOR_OPS
175STATIC WC_INLINE void XorWords(wolfssl_word* r, const wolfssl_word* a, word32 n)
176{
177 word32 i;
178
179 for (i = 0; i < n; i++) r[i] ^= a[i];
180}
181
182
183STATIC WC_INLINE void xorbuf(void* buf, const void* mask, word32 count)
184{
185 if (((wolfssl_word)buf | (wolfssl_word)mask | count) % WOLFSSL_WORD_SIZE == 0)
186 XorWords( (wolfssl_word*)buf,
187 (const wolfssl_word*)mask, count / WOLFSSL_WORD_SIZE);
188 else {
189 word32 i;
190 byte* b = (byte*)buf;
191 const byte* m = (const byte*)mask;
192
193 for (i = 0; i < count; i++) b[i] ^= m[i];
194 }
195}
196#endif
197
198#ifndef WOLFSSL_NO_FORCE_ZERO
199/* Make sure compiler doesn't skip */
200STATIC WC_INLINE void ForceZero(const void* mem, word32 len)
201{
202 volatile byte* z = (volatile byte*)mem;
203
204#if defined(WOLFSSL_X86_64_BUILD) && defined(WORD64_AVAILABLE)
205 volatile word64* w;
206 #ifndef WOLFSSL_UNALIGNED_64BIT_ACCESS
207 word32 l = (sizeof(word64) - ((size_t)z & (sizeof(word64)-1))) &
208 (sizeof(word64)-1);
209
210 if (len < l) l = len;
211 len -= l;
212 while (l--) *z++ = 0;
213 #endif
214 for (w = (volatile word64*)z; len >= sizeof(*w); len -= sizeof(*w))
215 *w++ = 0;
216 z = (volatile byte*)w;
217#endif
218
219 while (len--) *z++ = 0;
220}
221#endif
222
223
224#ifndef WOLFSSL_NO_CONST_CMP
225/* check all length bytes for equality, return 0 on success */
226STATIC WC_INLINE int ConstantCompare(const byte* a, const byte* b, int length)
227{
228 int i;
229 int compareSum = 0;
230
231 for (i = 0; i < length; i++) {
232 compareSum |= a[i] ^ b[i];
233 }
234
235 return compareSum;
236}
237#endif
238
239
240#ifndef WOLFSSL_HAVE_MIN
241 #define WOLFSSL_HAVE_MIN
242 #if defined(HAVE_FIPS) && !defined(min) /* so ifdef check passes */
243 #define min min
244 #endif
245 STATIC WC_INLINE word32 min(word32 a, word32 b)
246 {
247 return a > b ? b : a;
248 }
249#endif /* !WOLFSSL_HAVE_MIN */
250
251#ifndef WOLFSSL_HAVE_MAX
252 #define WOLFSSL_HAVE_MAX
253 #if defined(HAVE_FIPS) && !defined(max) /* so ifdef check passes */
254 #define max max
255 #endif
256 STATIC WC_INLINE word32 max(word32 a, word32 b)
257 {
258 return a > b ? a : b;
259 }
260#endif /* !WOLFSSL_HAVE_MAX */
261
262#ifndef WOLFSSL_NO_INT_ENCODE
263/* converts a 32 bit integer to 24 bit */
264STATIC WC_INLINE void c32to24(word32 in, word24 out)
265{
266 out[0] = (in >> 16) & 0xff;
267 out[1] = (in >> 8) & 0xff;
268 out[2] = in & 0xff;
269}
270
271/* convert 16 bit integer to opaque */
272STATIC WC_INLINE void c16toa(word16 wc_u16, byte* c)
273{
274 c[0] = (wc_u16 >> 8) & 0xff;
275 c[1] = wc_u16 & 0xff;
276}
277
278/* convert 32 bit integer to opaque */
279STATIC WC_INLINE void c32toa(word32 wc_u32, byte* c)
280{
281 c[0] = (wc_u32 >> 24) & 0xff;
282 c[1] = (wc_u32 >> 16) & 0xff;
283 c[2] = (wc_u32 >> 8) & 0xff;
284 c[3] = wc_u32 & 0xff;
285}
286#endif
287
288#ifndef WOLFSSL_NO_INT_DECODE
289/* convert a 24 bit integer into a 32 bit one */
290STATIC WC_INLINE void c24to32(const word24 wc_u24, word32* wc_u32)
291{
292 *wc_u32 = (wc_u24[0] << 16) | (wc_u24[1] << 8) | wc_u24[2];
293}
294
295
296/* convert opaque to 24 bit integer */
297STATIC WC_INLINE void ato24(const byte* c, word32* wc_u24)
298{
299 *wc_u24 = (c[0] << 16) | (c[1] << 8) | c[2];
300}
301
302/* convert opaque to 16 bit integer */
303STATIC WC_INLINE void ato16(const byte* c, word16* wc_u16)
304{
305 *wc_u16 = (word16) ((c[0] << 8) | (c[1]));
306}
307
308/* convert opaque to 32 bit integer */
309STATIC WC_INLINE void ato32(const byte* c, word32* wc_u32)
310{
311 *wc_u32 = ((word32)c[0] << 24) | (c[1] << 16) | (c[2] << 8) | c[3];
312}
313
314
315STATIC WC_INLINE word32 btoi(byte b)
316{
317 return (word32)(b - 0x30);
318}
319#endif
320
321
322#ifndef WOLFSSL_NO_CT_OPS
323/* Constant time - mask set when a > b. */
324STATIC WC_INLINE byte ctMaskGT(int a, int b)
325{
326 return (((word32)a - b - 1) >> 31) - 1;
327}
328
329/* Constant time - mask set when a >= b. */
330STATIC WC_INLINE byte ctMaskGTE(int a, int b)
331{
332 return (((word32)a - b ) >> 31) - 1;
333}
334
335/* Constant time - mask set when a < b. */
336STATIC WC_INLINE byte ctMaskLT(int a, int b)
337{
338 return (((word32)b - a - 1) >> 31) - 1;
339}
340
341/* Constant time - mask set when a <= b. */
342STATIC WC_INLINE byte ctMaskLTE(int a, int b)
343{
344 return (((word32)b - a ) >> 31) - 1;
345}
346
347/* Constant time - mask set when a == b. */
348STATIC WC_INLINE byte ctMaskEq(int a, int b)
349{
350 return 0 - (a == b);
351}
352
353/* Constant time - mask set when a != b. */
354STATIC WC_INLINE byte ctMaskNotEq(int a, int b)
355{
356 return 0 - (a != b);
357}
358
359/* Constant time - select a when mask is set and b otherwise. */
360STATIC WC_INLINE byte ctMaskSel(byte m, byte a, byte b)
361{
362 return (b & ((byte)~(word32)m)) | (a & m);
363}
364
365/* Constant time - select integer a when mask is set and integer b otherwise. */
366STATIC WC_INLINE int ctMaskSelInt(byte m, int a, int b)
367{
368 return (b & (~(signed int)(signed char)m)) |
369 (a & ( (signed int)(signed char)m));
370}
371
372/* Constant time - bit set when a <= b. */
373STATIC WC_INLINE byte ctSetLTE(int a, int b)
374{
375 return ((word32)a - b - 1) >> 31;
376}
377#endif
378
379
380#undef STATIC
381
382#endif /* !WOLFSSL_MISC_INCLUDED && !NO_INLINE */
383
384#endif /* WOLF_CRYPT_MISC_C */
Note: See TracBrowser for help on using the repository browser.