- Timestamp:
- Jun 22, 2021, 9:00:19 PM (3 years ago)
- Location:
- azure_iot_hub_f767zi/trunk/wolfssl-4.7.0
- Files:
-
- 1 edited
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
azure_iot_hub_f767zi/trunk/wolfssl-4.7.0/wolfcrypt/src/misc.c
r457 r464 19 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA 20 20 */ 21 22 21 /* 22 23 DESCRIPTION 24 This module implements the arithmetic-shift right, left, byte swapping, XOR, 25 masking and clearing memory logic. 26 27 */ 23 28 #ifdef HAVE_CONFIG_H 24 29 #include <config.h> … … 78 83 79 84 #else /* generic */ 85 /* This routine performs a left circular arithmetic shift of <x> by <y> value. */ 80 86 81 87 WC_STATIC WC_INLINE word32 rotlFixed(word32 x, word32 y) … … 84 90 } 85 91 86 92 /* This routine performs a right circular arithmetic shift of <x> by <y> value. */ 87 93 WC_STATIC WC_INLINE word32 rotrFixed(word32 x, word32 y) 88 94 { … … 92 98 #endif 93 99 94 100 #ifdef WC_RC2 101 102 /* This routine performs a left circular arithmetic shift of <x> by <y> value */ 103 WC_STATIC WC_INLINE word16 rotlFixed16(word16 x, word16 y) 104 { 105 return (x << y) | (x >> (sizeof(y) * 8 - y)); 106 } 107 108 109 /* This routine performs a right circular arithmetic shift of <x> by <y> value */ 110 WC_STATIC WC_INLINE word16 rotrFixed16(word16 x, word16 y) 111 { 112 return (x >> y) | (x << (sizeof(y) * 8 - y)); 113 } 114 115 #endif /* WC_RC2 */ 116 117 /* This routine performs a byte swap of 32-bit word value. */ 95 118 WC_STATIC WC_INLINE word32 ByteReverseWord32(word32 value) 96 119 { … … 105 128 defined(__GNUC_PREREQ) && __GNUC_PREREQ(4, 3) 106 129 return (word32)__builtin_bswap32(value); 130 #elif defined(WOLFSSL_BYTESWAP32_ASM) && defined(__GNUC__) && \ 131 defined(__aarch64__) 132 __asm__ volatile ( 133 "REV32 %0, %0 \n" 134 : "+r" (value) 135 : 136 ); 137 return value; 138 #elif defined(WOLFSSL_BYTESWAP32_ASM) && defined(__GNUC__) && \ 139 (defined(__thumb__) || defined(__arm__)) 140 __asm__ volatile ( 141 "REV %0, %0 \n" 142 : "+r" (value) 143 : 144 ); 145 return value; 107 146 #elif defined(FAST_ROTATE) 108 147 /* 5 instructions with rotate instruction, 9 without */ … … 115 154 #endif 116 155 } 117 118 156 /* This routine performs a byte swap of words array of a given count. */ 119 157 WC_STATIC WC_INLINE void ByteReverseWords(word32* out, const word32* in, 120 158 word32 byteCount) … … 126 164 127 165 } 128 129 166 130 167 #if defined(WORD64_AVAILABLE) && !defined(WOLFSSL_NO_WORD64_OPS) … … 173 210 174 211 #ifndef WOLFSSL_NO_XOR_OPS 212 /* This routine performs a bitwise XOR operation of <*r> and <*a> for <n> number 213 of wolfssl_words, placing the result in <*r>. */ 214 WC_STATIC WC_INLINE void XorWordsOut(wolfssl_word* r, const wolfssl_word* a, 215 const wolfssl_word* b, word32 n) 216 { 217 word32 i; 218 219 for (i = 0; i < n; i++) r[i] = a[i] ^ b[i]; 220 } 221 222 /* This routine performs a bitwise XOR operation of <*buf> and <*mask> of n 223 counts, placing the result in <*buf>. */ 224 225 WC_STATIC WC_INLINE void xorbufout(void*out, const void* buf, const void* mask, 226 word32 count) 227 { 228 if (((wolfssl_word)out | (wolfssl_word)buf | (wolfssl_word)mask | count) % \ 229 WOLFSSL_WORD_SIZE == 0) 230 XorWordsOut( (wolfssl_word*)out, (wolfssl_word*)buf, 231 (const wolfssl_word*)mask, count / WOLFSSL_WORD_SIZE); 232 else { 233 word32 i; 234 byte* o = (byte*)out; 235 byte* b = (byte*)buf; 236 const byte* m = (const byte*)mask; 237 238 for (i = 0; i < count; i++) o[i] = b[i] ^ m[i]; 239 } 240 } 241 242 /* This routine performs a bitwise XOR operation of <*r> and <*a> for <n> number 243 of wolfssl_words, placing the result in <*r>. */ 175 244 WC_STATIC WC_INLINE void XorWords(wolfssl_word* r, const wolfssl_word* a, word32 n) 176 245 { … … 180 249 } 181 250 251 /* This routine performs a bitwise XOR operation of <*buf> and <*mask> of n 252 counts, placing the result in <*buf>. */ 182 253 183 254 WC_STATIC WC_INLINE void xorbuf(void* buf, const void* mask, word32 count) … … 197 268 198 269 #ifndef WOLFSSL_NO_FORCE_ZERO 199 /* Make sure compiler doesn't skip */ 270 /* This routine fills the first len bytes of the memory area pointed by mem 271 with zeros. It ensures compiler optimizations doesn't skip it */ 200 272 WC_STATIC WC_INLINE void ForceZero(const void* mem, word32 len) 201 273 { 202 274 volatile byte* z = (volatile byte*)mem; 203 275 204 #if defined(WOLFSSL_X86_64_BUILD) && defined(WORD64_AVAILABLE) 276 #if (defined(WOLFSSL_X86_64_BUILD) || defined(WOLFSSL_AARCH64_BUILD)) \ 277 && defined(WORD64_AVAILABLE) 205 278 volatile word64* w; 206 279 #ifndef WOLFSSL_UNALIGNED_64BIT_ACCESS … … 243 316 #define min min 244 317 #endif 318 /* returns the smaller of a and b */ 245 319 WC_STATIC WC_INLINE word32 min(word32 a, word32 b) 246 320 { … … 324 398 WC_STATIC WC_INLINE byte ctMaskGT(int a, int b) 325 399 { 326 return ( ((word32)a - b - 1) >> 31) - 1;400 return (byte)((((word32)a - b - 1) >> 31) - 1); 327 401 } 328 402 … … 330 404 WC_STATIC WC_INLINE byte ctMaskGTE(int a, int b) 331 405 { 332 return ( ((word32)a - b ) >> 31) - 1;406 return (byte)((((word32)a - b ) >> 31) - 1); 333 407 } 334 408 … … 336 410 WC_STATIC WC_INLINE int ctMaskIntGTE(int a, int b) 337 411 { 338 return ( ((word32)a - b ) >> 31) - 1;412 return (int)((((word32)a - b ) >> 31) - 1); 339 413 } 340 414 … … 342 416 WC_STATIC WC_INLINE byte ctMaskLT(int a, int b) 343 417 { 344 return ( ((word32)b - a - 1) >> 31) - 1;418 return (byte)((((word32)b - a - 1) >> 31) - 1); 345 419 } 346 420 … … 348 422 WC_STATIC WC_INLINE byte ctMaskLTE(int a, int b) 349 423 { 350 return ( ((word32)b - a ) >> 31) - 1;424 return (byte)((((word32)b - a ) >> 31) - 1); 351 425 } 352 426 … … 354 428 WC_STATIC WC_INLINE byte ctMaskEq(int a, int b) 355 429 { 356 return (~ctMaskGT(a, b)) & (~ctMaskLT(a, b)); 357 } 358 430 return (byte)(~ctMaskGT(a, b)) & (byte)(~ctMaskLT(a, b)); 431 } 432 433 /* Constant time - sets 16 bit integer mask when a > b */ 359 434 WC_STATIC WC_INLINE word16 ctMask16GT(int a, int b) 360 435 { 361 return (((word32)a - b - 1) >> 31) - 1; 362 } 363 436 return (word16)((((word32)a - b - 1) >> 31) - 1); 437 } 438 439 /* Constant time - sets 16 bit integer mask when a >= b */ 440 WC_STATIC WC_INLINE word16 ctMask16GTE(int a, int b) 441 { 442 return (word16)((((word32)a - b ) >> 31) - 1); 443 } 444 445 /* Constant time - sets 16 bit integer mask when a < b. */ 364 446 WC_STATIC WC_INLINE word16 ctMask16LT(int a, int b) 365 447 { 366 return (((word32)a - b - 1) >> 31) - 1; 367 } 368 448 return (word16)((((word32)b - a - 1) >> 31) - 1); 449 } 450 451 /* Constant time - sets 16 bit integer mask when a <= b. */ 452 WC_STATIC WC_INLINE word16 ctMask16LTE(int a, int b) 453 { 454 return (word16)((((word32)b - a ) >> 31) - 1); 455 } 456 457 /* Constant time - sets 16 bit integer mask when a == b. */ 369 458 WC_STATIC WC_INLINE word16 ctMask16Eq(int a, int b) 370 459 { 371 return ( ~ctMask16GT(a, b)) &(~ctMask16LT(a, b));460 return (word16)(~ctMask16GT(a, b)) & (word16)(~ctMask16LT(a, b)); 372 461 } 373 462 … … 375 464 WC_STATIC WC_INLINE byte ctMaskNotEq(int a, int b) 376 465 { 377 return ctMaskGT(a, b) |ctMaskLT(a, b);466 return (byte)ctMaskGT(a, b) | (byte)ctMaskLT(a, b); 378 467 } 379 468 … … 381 470 WC_STATIC WC_INLINE byte ctMaskSel(byte m, byte a, byte b) 382 471 { 383 return (b & ((byte)~(word32)m)) | (a & m);472 return (byte)((b & ((byte)~(word32)m)) | (a & m)); 384 473 } 385 474 … … 394 483 WC_STATIC WC_INLINE byte ctSetLTE(int a, int b) 395 484 { 396 return ( (word32)a - b - 1) >> 31;485 return (byte)(((word32)a - b - 1) >> 31); 397 486 } 398 487 #endif
Note:
See TracChangeset
for help on using the changeset viewer.