Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
misc.c
00001 /* misc.c 00002 * 00003 * Copyright (C) 2006-2016 wolfSSL Inc. 00004 * 00005 * This file is part of wolfSSL. 00006 * 00007 * wolfSSL is free software; you can redistribute it and/or modify 00008 * it under the terms of the GNU General Public License as published by 00009 * the Free Software Foundation; either version 2 of the License, or 00010 * (at your option) any later version. 00011 * 00012 * wolfSSL is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 * GNU General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU General Public License 00018 * along with this program; if not, write to the Free Software 00019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA 00020 */ 00021 00022 00023 #ifdef HAVE_CONFIG_H 00024 #include <config.h> 00025 #endif 00026 00027 #include <wolfssl/wolfcrypt/settings.h> 00028 00029 #ifndef WOLF_CRYPT_MISC_C 00030 #define WOLF_CRYPT_MISC_C 00031 00032 #include <wolfssl/wolfcrypt/misc.h> 00033 00034 /* inlining these functions is a huge speed increase and a small size decrease, 00035 because the functions are smaller than function call setup/cleanup, e.g., 00036 md5 benchmark is twice as fast with inline. If you don't want it, then 00037 define NO_INLINE and compile this file into wolfssl, otherwise it's used as 00038 a source header 00039 */ 00040 00041 #ifdef NO_INLINE 00042 #define STATIC 00043 #else 00044 #define STATIC static 00045 #endif 00046 00047 00048 #ifdef INTEL_INTRINSICS 00049 00050 #include <stdlib.h> /* get intrinsic definitions */ 00051 00052 /* for non visual studio probably need no long version, 32 bit only 00053 * i.e., _rotl and _rotr */ 00054 #pragma intrinsic(_lrotl, _lrotr) 00055 00056 STATIC INLINE word32 rotlFixed(word32 x, word32 y) 00057 { 00058 return y ? _lrotl(x, y) : x; 00059 } 00060 00061 STATIC INLINE word32 rotrFixed(word32 x, word32 y) 00062 { 00063 return y ? _lrotr(x, y) : x; 00064 } 00065 00066 #else /* generic */ 00067 00068 STATIC INLINE word32 rotlFixed(word32 x, word32 y) 00069 { 00070 return (x << y) | (x >> (sizeof(y) * 8 - y)); 00071 } 00072 00073 00074 STATIC INLINE word32 rotrFixed(word32 x, word32 y) 00075 { 00076 return (x >> y) | (x << (sizeof(y) * 8 - y)); 00077 } 00078 00079 #endif 00080 00081 00082 STATIC INLINE word32 ByteReverseWord32(word32 value) 00083 { 00084 #ifdef PPC_INTRINSICS 00085 /* PPC: load reverse indexed instruction */ 00086 return (word32)__lwbrx(&value,0); 00087 #elif defined(KEIL_INTRINSICS) 00088 return (word32)__rev(value); 00089 #elif defined(FAST_ROTATE) 00090 /* 5 instructions with rotate instruction, 9 without */ 00091 return (rotrFixed(value, 8U) & 0xff00ff00) | 00092 (rotlFixed(value, 8U) & 0x00ff00ff); 00093 #else 00094 /* 6 instructions with rotate instruction, 8 without */ 00095 value = ((value & 0xFF00FF00) >> 8) | ((value & 0x00FF00FF) << 8); 00096 return rotlFixed(value, 16U); 00097 #endif 00098 } 00099 00100 00101 STATIC INLINE void ByteReverseWords(word32* out, const word32* in, 00102 word32 byteCount) 00103 { 00104 word32 count = byteCount/(word32)sizeof(word32), i; 00105 00106 for (i = 0; i < count; i++) 00107 out[i] = ByteReverseWord32(in[i]); 00108 00109 } 00110 00111 00112 #ifdef WORD64_AVAILABLE 00113 00114 00115 STATIC INLINE word64 rotlFixed64(word64 x, word64 y) 00116 { 00117 return (x << y) | (x >> (sizeof(y) * 8 - y)); 00118 } 00119 00120 00121 STATIC INLINE word64 rotrFixed64(word64 x, word64 y) 00122 { 00123 return (x >> y) | (x << (sizeof(y) * 8 - y)); 00124 } 00125 00126 00127 STATIC INLINE word64 ByteReverseWord64(word64 value) 00128 { 00129 #ifdef WOLFCRYPT_SLOW_WORD64 00130 return (word64)(ByteReverseWord32((word32)value)) << 32 | 00131 ByteReverseWord32((word32)(value>>32)); 00132 #else 00133 value = ((value & W64LIT(0xFF00FF00FF00FF00)) >> 8) | 00134 ((value & W64LIT(0x00FF00FF00FF00FF)) << 8); 00135 value = ((value & W64LIT(0xFFFF0000FFFF0000)) >> 16) | 00136 ((value & W64LIT(0x0000FFFF0000FFFF)) << 16); 00137 return rotlFixed64(value, 32U); 00138 #endif 00139 } 00140 00141 00142 STATIC INLINE void ByteReverseWords64(word64* out, const word64* in, 00143 word32 byteCount) 00144 { 00145 word32 count = byteCount/(word32)sizeof(word64), i; 00146 00147 for (i = 0; i < count; i++) 00148 out[i] = ByteReverseWord64(in[i]); 00149 00150 } 00151 00152 #endif /* WORD64_AVAILABLE */ 00153 00154 00155 STATIC INLINE void XorWords(wolfssl_word* r, const wolfssl_word* a, word32 n) 00156 { 00157 word32 i; 00158 00159 for (i = 0; i < n; i++) r[i] ^= a[i]; 00160 } 00161 00162 00163 STATIC INLINE void xorbuf(void* buf, const void* mask, word32 count) 00164 { 00165 if (((wolfssl_word)buf | (wolfssl_word)mask | count) % WOLFSSL_WORD_SIZE == 0) 00166 XorWords( (wolfssl_word*)buf, 00167 (const wolfssl_word*)mask, count / WOLFSSL_WORD_SIZE); 00168 else { 00169 word32 i; 00170 byte* b = (byte*)buf; 00171 const byte* m = (const byte*)mask; 00172 00173 for (i = 0; i < count; i++) b[i] ^= m[i]; 00174 } 00175 } 00176 00177 00178 /* Make sure compiler doesn't skip */ 00179 STATIC INLINE void ForceZero(const void* mem, word32 len) 00180 { 00181 volatile byte* z = (volatile byte*)mem; 00182 00183 while (len--) *z++ = 0; 00184 } 00185 00186 00187 /* check all length bytes for equality, return 0 on success */ 00188 STATIC INLINE int ConstantCompare(const byte* a, const byte* b, int length) 00189 { 00190 int i; 00191 int compareSum = 0; 00192 00193 for (i = 0; i < length; i++) { 00194 compareSum |= a[i] ^ b[i]; 00195 } 00196 00197 return compareSum; 00198 } 00199 00200 #undef STATIC 00201 00202 #endif /* WOLF_CRYPT_MISC_C */ 00203
Generated on Tue Jul 12 2022 15:55:20 by
1.7.2