- Timestamp:
- Feb 7, 2019, 8:36:33 AM (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
asp3_tinet_ecnl_rx/trunk/wolfssl-3.12.2/wolfcrypt/src/misc.c
r337 r372 47 47 /* Check for if compiling misc.c when not needed. */ 48 48 #if !defined(WOLFSSL_MISC_INCLUDED) && !defined(NO_INLINE) 49 #ifndef WOLFSSL_IGNORE_FILE_WARN 49 50 #warning misc.c does not need to be compiled when using inline (NO_INLINE not defined) 51 #endif 50 52 51 53 #else … … 65 67 #pragma intrinsic(_lrotl, _lrotr) 66 68 67 STATIC INLINE word32 rotlFixed(word32 x, word32 y)69 STATIC WC_INLINE word32 rotlFixed(word32 x, word32 y) 68 70 { 69 71 return y ? _lrotl(x, y) : x; 70 72 } 71 73 72 STATIC INLINE word32 rotrFixed(word32 x, word32 y)74 STATIC WC_INLINE word32 rotrFixed(word32 x, word32 y) 73 75 { 74 76 return y ? _lrotr(x, y) : x; … … 77 79 #else /* generic */ 78 80 79 STATIC INLINE word32 rotlFixed(word32 x, word32 y)81 STATIC WC_INLINE word32 rotlFixed(word32 x, word32 y) 80 82 { 81 83 return (x << y) | (x >> (sizeof(y) * 8 - y)); … … 83 85 84 86 85 STATIC INLINE word32 rotrFixed(word32 x, word32 y)87 STATIC WC_INLINE word32 rotrFixed(word32 x, word32 y) 86 88 { 87 89 return (x >> y) | (x << (sizeof(y) * 8 - y)); … … 91 93 92 94 93 STATIC INLINE word32 ByteReverseWord32(word32 value)95 STATIC WC_INLINE word32 ByteReverseWord32(word32 value) 94 96 { 95 97 #ifdef PPC_INTRINSICS … … 100 102 #elif defined(KEIL_INTRINSICS) 101 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); 102 107 #elif defined(FAST_ROTATE) 103 108 /* 5 instructions with rotate instruction, 9 without */ … … 112 117 113 118 114 STATIC INLINE void ByteReverseWords(word32* out, const word32* in,119 STATIC WC_INLINE void ByteReverseWords(word32* out, const word32* in, 115 120 word32 byteCount) 116 121 { … … 123 128 124 129 125 #if def WORD64_AVAILABLE126 127 128 STATIC INLINE word64 rotlFixed64(word64 x, word64 y)130 #if defined(WORD64_AVAILABLE) && !defined(WOLFSSL_NO_WORD64_OPS) 131 132 133 STATIC WC_INLINE word64 rotlFixed64(word64 x, word64 y) 129 134 { 130 135 return (x << y) | (x >> (sizeof(y) * 8 - y)); … … 132 137 133 138 134 STATIC INLINE word64 rotrFixed64(word64 x, word64 y)139 STATIC WC_INLINE word64 rotrFixed64(word64 x, word64 y) 135 140 { 136 141 return (x >> y) | (x << (sizeof(y) * 8 - y)); … … 138 143 139 144 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 STATIC 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)); 145 152 #else 146 153 value = ((value & W64LIT(0xFF00FF00FF00FF00)) >> 8) | … … 153 160 154 161 155 STATIC INLINE void ByteReverseWords64(word64* out, const word64* in,162 STATIC WC_INLINE void ByteReverseWords64(word64* out, const word64* in, 156 163 word32 byteCount) 157 164 { … … 163 170 } 164 171 165 #endif /* WORD64_AVAILABLE */166 167 168 STATIC INLINE void XorWords(wolfssl_word* r, const wolfssl_word* a, word32 n)172 #endif /* WORD64_AVAILABLE && !WOLFSSL_NO_WORD64_OPS */ 173 174 #ifndef WOLFSSL_NO_XOR_OPS 175 STATIC WC_INLINE void XorWords(wolfssl_word* r, const wolfssl_word* a, word32 n) 169 176 { 170 177 word32 i; … … 174 181 175 182 176 STATIC INLINE void xorbuf(void* buf, const void* mask, word32 count)183 STATIC WC_INLINE void xorbuf(void* buf, const void* mask, word32 count) 177 184 { 178 185 if (((wolfssl_word)buf | (wolfssl_word)mask | count) % WOLFSSL_WORD_SIZE == 0) … … 187 194 } 188 195 } 189 190 196 #endif 197 198 #ifndef WOLFSSL_NO_FORCE_ZERO 191 199 /* Make sure compiler doesn't skip */ 192 STATIC INLINE void ForceZero(const void* mem, word32 len)200 STATIC WC_INLINE void ForceZero(const void* mem, word32 len) 193 201 { 194 202 volatile byte* z = (volatile byte*)mem; 195 #ifdef WOLFSSL_X86_64_BUILD 203 204 #if defined(WOLFSSL_X86_64_BUILD) && defined(WORD64_AVAILABLE) 196 205 volatile word64* w; 197 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 198 214 for (w = (volatile word64*)z; len >= sizeof(*w); len -= sizeof(*w)) 199 215 *w++ = 0; 200 216 z = (volatile byte*)w; 201 217 #endif 218 202 219 while (len--) *z++ = 0; 203 220 } 204 205 221 #endif 222 223 224 #ifndef WOLFSSL_NO_CONST_CMP 206 225 /* check all length bytes for equality, return 0 on success */ 207 STATIC INLINE int ConstantCompare(const byte* a, const byte* b, int length)226 STATIC WC_INLINE int ConstantCompare(const byte* a, const byte* b, int length) 208 227 { 209 228 int i; … … 216 235 return compareSum; 217 236 } 237 #endif 218 238 219 239 … … 223 243 #define min min 224 244 #endif 225 STATIC INLINE word32 min(word32 a, word32 b)245 STATIC WC_INLINE word32 min(word32 a, word32 b) 226 246 { 227 247 return a > b ? b : a; … … 234 254 #define max max 235 255 #endif 236 STATIC INLINE word32 max(word32 a, word32 b)256 STATIC WC_INLINE word32 max(word32 a, word32 b) 237 257 { 238 258 return a > b ? a : b; … … 240 260 #endif /* !WOLFSSL_HAVE_MAX */ 241 261 262 #ifndef WOLFSSL_NO_INT_ENCODE 242 263 /* converts a 32 bit integer to 24 bit */ 243 STATIC INLINE void c32to24(word32 in, word24 out)264 STATIC WC_INLINE void c32to24(word32 in, word24 out) 244 265 { 245 266 out[0] = (in >> 16) & 0xff; … … 249 270 250 271 /* convert 16 bit integer to opaque */ 251 STATIC INLINE void c16toa(word16u16, byte* c)252 { 253 c[0] = ( u16 >> 8) & 0xff;254 c[1] = u16 & 0xff;272 STATIC WC_INLINE void c16toa(word16 wc_u16, byte* c) 273 { 274 c[0] = (wc_u16 >> 8) & 0xff; 275 c[1] = wc_u16 & 0xff; 255 276 } 256 277 257 278 /* 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 279 STATIC 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 266 289 /* 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];290 STATIC 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]; 270 293 } 271 294 272 295 273 296 /* 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];297 STATIC WC_INLINE void ato24(const byte* c, word32* wc_u24) 298 { 299 *wc_u24 = (c[0] << 16) | (c[1] << 8) | c[2]; 277 300 } 278 301 279 302 /* 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]));303 STATIC WC_INLINE void ato16(const byte* c, word16* wc_u16) 304 { 305 *wc_u16 = (word16) ((c[0] << 8) | (c[1])); 283 306 } 284 307 285 308 /* 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)309 STATIC 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 315 STATIC WC_INLINE word32 btoi(byte b) 293 316 { 294 317 return (word32)(b - 0x30); 295 318 } 296 319 #endif 320 321 322 #ifndef WOLFSSL_NO_CT_OPS 323 /* Constant time - mask set when a > b. */ 324 STATIC 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. */ 330 STATIC 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. */ 336 STATIC 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. */ 342 STATIC 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. */ 348 STATIC WC_INLINE byte ctMaskEq(int a, int b) 349 { 350 return 0 - (a == b); 351 } 352 353 /* Constant time - mask set when a != b. */ 354 STATIC 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. */ 360 STATIC 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. */ 366 STATIC 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. */ 373 STATIC WC_INLINE byte ctSetLTE(int a, int b) 374 { 375 return ((word32)a - b - 1) >> 31; 376 } 377 #endif 297 378 298 379
Note:
See TracChangeset
for help on using the changeset viewer.