source: UsbWattMeter/trunk/wolfssl-3.7.0/wolfcrypt/src/misc.c@ 164

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

TOPPERS/ECNLサンプルアプリ「USB充電器電力計」を追加

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-csrc
File size: 5.0 KB
Line 
1/* misc.c
2 *
3 * Copyright (C) 2006-2015 wolfSSL Inc.
4 *
5 * This file is part of wolfSSL. (formerly known as CyaSSL)
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-1301, USA
20 */
21
22#ifdef HAVE_CONFIG_H
23 #include <config.h>
24#endif
25
26#include <wolfssl/wolfcrypt/settings.h>
27
28#ifndef WOLF_CRYPT_MISC_C
29#define WOLF_CRYPT_MISC_C
30
31#include <wolfssl/wolfcrypt/misc.h>
32
33/* inlining these functions is a huge speed increase and a small size decrease,
34 because the functions are smaller than function call setup/cleanup, e.g.,
35 md5 benchmark is twice as fast with inline. If you don't want it, then
36 define NO_INLINE and compile this file into wolfssl, otherwise it's used as
37 a source header
38 */
39
40#ifdef NO_INLINE
41 #define STATIC
42#else
43 #define STATIC static
44#endif
45
46
47#ifdef INTEL_INTRINSICS
48
49 #include <stdlib.h> /* get intrinsic definitions */
50
51 /* for non visual studio probably need no long version, 32 bit only
52 * i.e., _rotl and _rotr */
53 #pragma intrinsic(_lrotl, _lrotr)
54
55 STATIC INLINE word32 rotlFixed(word32 x, word32 y)
56 {
57 return y ? _lrotl(x, y) : x;
58 }
59
60 STATIC INLINE word32 rotrFixed(word32 x, word32 y)
61 {
62 return y ? _lrotr(x, y) : x;
63 }
64
65#else /* generic */
66
67 STATIC INLINE word32 rotlFixed(word32 x, word32 y)
68 {
69 return (x << y) | (x >> (sizeof(y) * 8 - y));
70 }
71
72
73 STATIC INLINE word32 rotrFixed(word32 x, word32 y)
74 {
75 return (x >> y) | (x << (sizeof(y) * 8 - y));
76 }
77
78#endif
79
80
81STATIC INLINE word32 ByteReverseWord32(word32 value)
82{
83#ifdef PPC_INTRINSICS
84 /* PPC: load reverse indexed instruction */
85 return (word32)__lwbrx(&value,0);
86#elif defined(KEIL_INTRINSICS)
87 return (word32)__rev(value);
88#elif defined(FAST_ROTATE)
89 /* 5 instructions with rotate instruction, 9 without */
90 return (rotrFixed(value, 8U) & 0xff00ff00) |
91 (rotlFixed(value, 8U) & 0x00ff00ff);
92#else
93 /* 6 instructions with rotate instruction, 8 without */
94 value = ((value & 0xFF00FF00) >> 8) | ((value & 0x00FF00FF) << 8);
95 return rotlFixed(value, 16U);
96#endif
97}
98
99
100STATIC INLINE void ByteReverseWords(word32* out, const word32* in,
101 word32 byteCount)
102{
103 word32 count = byteCount/(word32)sizeof(word32), i;
104
105 for (i = 0; i < count; i++)
106 out[i] = ByteReverseWord32(in[i]);
107
108}
109
110
111#ifdef WORD64_AVAILABLE
112
113
114STATIC INLINE word64 rotlFixed64(word64 x, word64 y)
115{
116 return (x << y) | (x >> (sizeof(y) * 8 - y));
117}
118
119
120STATIC INLINE word64 rotrFixed64(word64 x, word64 y)
121{
122 return (x >> y) | (x << (sizeof(y) * 8 - y));
123}
124
125
126STATIC INLINE word64 ByteReverseWord64(word64 value)
127{
128#ifdef WOLFCRYPT_SLOW_WORD64
129 return (word64)(ByteReverseWord32((word32)value)) << 32 |
130 ByteReverseWord32((word32)(value>>32));
131#else
132 value = ((value & W64LIT(0xFF00FF00FF00FF00)) >> 8) |
133 ((value & W64LIT(0x00FF00FF00FF00FF)) << 8);
134 value = ((value & W64LIT(0xFFFF0000FFFF0000)) >> 16) |
135 ((value & W64LIT(0x0000FFFF0000FFFF)) << 16);
136 return rotlFixed64(value, 32U);
137#endif
138}
139
140
141STATIC INLINE void ByteReverseWords64(word64* out, const word64* in,
142 word32 byteCount)
143{
144 word32 count = byteCount/(word32)sizeof(word64), i;
145
146 for (i = 0; i < count; i++)
147 out[i] = ByteReverseWord64(in[i]);
148
149}
150
151#endif /* WORD64_AVAILABLE */
152
153
154STATIC INLINE void XorWords(wolfssl_word* r, const wolfssl_word* a, word32 n)
155{
156 word32 i;
157
158 for (i = 0; i < n; i++) r[i] ^= a[i];
159}
160
161
162STATIC INLINE void xorbuf(void* buf, const void* mask, word32 count)
163{
164 if (((wolfssl_word)buf | (wolfssl_word)mask | count) % WOLFSSL_WORD_SIZE == 0)
165 XorWords( (wolfssl_word*)buf,
166 (const wolfssl_word*)mask, count / WOLFSSL_WORD_SIZE);
167 else {
168 word32 i;
169 byte* b = (byte*)buf;
170 const byte* m = (const byte*)mask;
171
172 for (i = 0; i < count; i++) b[i] ^= m[i];
173 }
174}
175
176
177/* Make sure compiler doesn't skip */
178STATIC INLINE void ForceZero(const void* mem, word32 len)
179{
180 volatile byte* z = (volatile byte*)mem;
181
182 while (len--) *z++ = 0;
183}
184
185
186/* check all length bytes for equality, return 0 on success */
187STATIC INLINE int ConstantCompare(const byte* a, const byte* b, int length)
188{
189 int i;
190 int compareSum = 0;
191
192 for (i = 0; i < length; i++) {
193 compareSum |= a[i] ^ b[i];
194 }
195
196 return compareSum;
197}
198
199#undef STATIC
200
201#endif /* WOLF_CRYPT_MISC_C */
Note: See TracBrowser for help on using the repository browser.