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-2017 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 <wolfcrypt/settings.h> 00028 00029 #ifndef WOLF_CRYPT_MISC_C 00030 #define WOLF_CRYPT_MISC_C 00031 00032 #include <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 /* Check for if compiling misc.c when not needed. */ 00048 #if !defined(WOLFSSL_MISC_INCLUDED) && !defined(NO_INLINE) 00049 #ifndef WOLFSSL_IGNORE_FILE_WARN 00050 #warning misc.c does not need to be compiled when using inline (NO_INLINE not defined) 00051 #endif 00052 00053 #else 00054 00055 00056 #if defined(__ICCARM__) 00057 #include <intrinsics.h> 00058 #endif 00059 00060 00061 #ifdef INTEL_INTRINSICS 00062 00063 #include <stdlib.h> /* get intrinsic definitions */ 00064 00065 /* for non visual studio probably need no long version, 32 bit only 00066 * i.e., _rotl and _rotr */ 00067 #pragma intrinsic(_lrotl, _lrotr) 00068 00069 STATIC WC_INLINE word32 rotlFixed(word32 x, word32 y) 00070 { 00071 return y ? _lrotl(x, y) : x; 00072 } 00073 00074 STATIC WC_INLINE word32 rotrFixed(word32 x, word32 y) 00075 { 00076 return y ? _lrotr(x, y) : x; 00077 } 00078 00079 #else /* generic */ 00080 00081 STATIC WC_INLINE word32 rotlFixed(word32 x, word32 y) 00082 { 00083 return (x << y) | (x >> (sizeof(y) * 8 - y)); 00084 } 00085 00086 00087 STATIC WC_INLINE word32 rotrFixed(word32 x, word32 y) 00088 { 00089 return (x >> y) | (x << (sizeof(y) * 8 - y)); 00090 } 00091 00092 #endif 00093 00094 00095 STATIC WC_INLINE word32 ByteReverseWord32(word32 value) 00096 { 00097 #ifdef PPC_INTRINSICS 00098 /* PPC: load reverse indexed instruction */ 00099 return (word32)__lwbrx(&value,0); 00100 #elif defined(__ICCARM__) 00101 return (word32)__REV(value); 00102 #elif defined(KEIL_INTRINSICS) 00103 return (word32)__rev(value); 00104 #elif defined(WOLF_ALLOW_BUILTIN) && \ 00105 defined(__GNUC_PREREQ) && __GNUC_PREREQ(4, 3) 00106 return (word32)__builtin_bswap32(value); 00107 #elif defined(FAST_ROTATE) 00108 /* 5 instructions with rotate instruction, 9 without */ 00109 return (rotrFixed(value, 8U) & 0xff00ff00) | 00110 (rotlFixed(value, 8U) & 0x00ff00ff); 00111 #else 00112 /* 6 instructions with rotate instruction, 8 without */ 00113 value = ((value & 0xFF00FF00) >> 8) | ((value & 0x00FF00FF) << 8); 00114 return rotlFixed(value, 16U); 00115 #endif 00116 } 00117 00118 00119 STATIC WC_INLINE void ByteReverseWords(word32* out, const word32* in, 00120 word32 byteCount) 00121 { 00122 word32 count = byteCount/(word32)sizeof(word32), i; 00123 00124 for (i = 0; i < count; i++) 00125 out[i] = ByteReverseWord32(in[i]); 00126 00127 } 00128 00129 00130 #ifdef WORD64_AVAILABLE 00131 00132 00133 STATIC WC_INLINE word64 rotlFixed64(word64 x, word64 y) 00134 { 00135 return (x << y) | (x >> (sizeof(y) * 8 - y)); 00136 } 00137 00138 00139 STATIC WC_INLINE word64 rotrFixed64(word64 x, word64 y) 00140 { 00141 return (x >> y) | (x << (sizeof(y) * 8 - y)); 00142 } 00143 00144 00145 STATIC WC_INLINE word64 ByteReverseWord64(word64 value) 00146 { 00147 #if defined(WOLF_ALLOW_BUILTIN) && defined(__GNUC_PREREQ) && __GNUC_PREREQ(4, 3) 00148 return (word64)__builtin_bswap64(value); 00149 #elif defined(WOLFCRYPT_SLOW_WORD64) 00150 return (word64)((word64)ByteReverseWord32((word32) value)) << 32 | 00151 (word64)ByteReverseWord32((word32)(value >> 32)); 00152 #else 00153 value = ((value & W64LIT(0xFF00FF00FF00FF00)) >> 8) | 00154 ((value & W64LIT(0x00FF00FF00FF00FF)) << 8); 00155 value = ((value & W64LIT(0xFFFF0000FFFF0000)) >> 16) | 00156 ((value & W64LIT(0x0000FFFF0000FFFF)) << 16); 00157 return rotlFixed64(value, 32U); 00158 #endif 00159 } 00160 00161 00162 STATIC WC_INLINE void ByteReverseWords64(word64* out, const word64* in, 00163 word32 byteCount) 00164 { 00165 word32 count = byteCount/(word32)sizeof(word64), i; 00166 00167 for (i = 0; i < count; i++) 00168 out[i] = ByteReverseWord64(in[i]); 00169 00170 } 00171 00172 #endif /* WORD64_AVAILABLE */ 00173 00174 00175 STATIC WC_INLINE void XorWords(wolfssl_word* r, const wolfssl_word* a, word32 n) 00176 { 00177 word32 i; 00178 00179 for (i = 0; i < n; i++) r[i] ^= a[i]; 00180 } 00181 00182 00183 STATIC WC_INLINE void xorbuf(void* buf, const void* mask, word32 count) 00184 { 00185 if (((wolfssl_word)buf | (wolfssl_word)mask | count) % WOLFSSL_WORD_SIZE == 0) 00186 XorWords( (wolfssl_word*)buf, 00187 (const wolfssl_word*)mask, count / WOLFSSL_WORD_SIZE); 00188 else { 00189 word32 i; 00190 byte* b = (byte*)buf; 00191 const byte* m = (const byte*)mask; 00192 00193 for (i = 0; i < count; i++) b[i] ^= m[i]; 00194 } 00195 } 00196 00197 00198 /* Make sure compiler doesn't skip */ 00199 STATIC WC_INLINE void ForceZero(const void* mem, word32 len) 00200 { 00201 volatile byte* z = (volatile byte*)mem; 00202 00203 #if defined(WOLFSSL_X86_64_BUILD) && defined(WORD64_AVAILABLE) 00204 volatile word64* w; 00205 #ifndef WOLFSSL_UNALIGNED_64BIT_ACCESS 00206 word32 l = (sizeof(word64) - ((size_t)z & (sizeof(word64)-1))) & 00207 (sizeof(word64)-1); 00208 00209 if (len < l) l = len; 00210 len -= l; 00211 while (l--) *z++ = 0; 00212 #endif 00213 for (w = (volatile word64*)z; len >= sizeof(*w); len -= sizeof(*w)) 00214 *w++ = 0; 00215 z = (volatile byte*)w; 00216 #endif 00217 00218 while (len--) *z++ = 0; 00219 } 00220 00221 00222 /* check all length bytes for equality, return 0 on success */ 00223 STATIC WC_INLINE int ConstantCompare(const byte* a, const byte* b, int length) 00224 { 00225 int i; 00226 int compareSum = 0; 00227 00228 for (i = 0; i < length; i++) { 00229 compareSum |= a[i] ^ b[i]; 00230 } 00231 00232 return compareSum; 00233 } 00234 00235 00236 #ifndef WOLFSSL_HAVE_MIN 00237 #define WOLFSSL_HAVE_MIN 00238 #if defined(HAVE_FIPS) && !defined(min) /* so ifdef check passes */ 00239 #define min min 00240 #endif 00241 STATIC WC_INLINE word32 min(word32 a, word32 b) 00242 { 00243 return a > b ? b : a; 00244 } 00245 #endif /* !WOLFSSL_HAVE_MIN */ 00246 00247 #ifndef WOLFSSL_HAVE_MAX 00248 #define WOLFSSL_HAVE_MAX 00249 #if defined(HAVE_FIPS) && !defined(max) /* so ifdef check passes */ 00250 #define max max 00251 #endif 00252 STATIC WC_INLINE word32 max(word32 a, word32 b) 00253 { 00254 return a > b ? a : b; 00255 } 00256 #endif /* !WOLFSSL_HAVE_MAX */ 00257 00258 /* converts a 32 bit integer to 24 bit */ 00259 STATIC WC_INLINE void c32to24(word32 in, word24 out) 00260 { 00261 out[0] = (in >> 16) & 0xff; 00262 out[1] = (in >> 8) & 0xff; 00263 out[2] = in & 0xff; 00264 } 00265 00266 /* convert 16 bit integer to opaque */ 00267 STATIC WC_INLINE void c16toa(word16 wc_u16, byte* c) 00268 { 00269 c[0] = (wc_u16 >> 8) & 0xff; 00270 c[1] = wc_u16 & 0xff; 00271 } 00272 00273 /* convert 32 bit integer to opaque */ 00274 STATIC WC_INLINE void c32toa(word32 wc_u32, byte* c) 00275 { 00276 c[0] = (wc_u32 >> 24) & 0xff; 00277 c[1] = (wc_u32 >> 16) & 0xff; 00278 c[2] = (wc_u32 >> 8) & 0xff; 00279 c[3] = wc_u32 & 0xff; 00280 } 00281 00282 /* convert a 24 bit integer into a 32 bit one */ 00283 STATIC WC_INLINE void c24to32(const word24 wc_u24, word32* wc_u32) 00284 { 00285 *wc_u32 = (wc_u24[0] << 16) | (wc_u24[1] << 8) | wc_u24[2]; 00286 } 00287 00288 00289 /* convert opaque to 24 bit integer */ 00290 STATIC WC_INLINE void ato24(const byte* c, word32* wc_u24) 00291 { 00292 *wc_u24 = (c[0] << 16) | (c[1] << 8) | c[2]; 00293 } 00294 00295 /* convert opaque to 16 bit integer */ 00296 STATIC WC_INLINE void ato16(const byte* c, word16* wc_u16) 00297 { 00298 *wc_u16 = (word16) ((c[0] << 8) | (c[1])); 00299 } 00300 00301 /* convert opaque to 32 bit integer */ 00302 STATIC WC_INLINE void ato32(const byte* c, word32* wc_u32) 00303 { 00304 *wc_u32 = ((word32)c[0] << 24) | (c[1] << 16) | (c[2] << 8) | c[3]; 00305 } 00306 00307 00308 STATIC WC_INLINE word32 btoi(byte b) 00309 { 00310 return (word32)(b - 0x30); 00311 } 00312 00313 00314 /* Constant time - mask set when a > b. */ 00315 STATIC WC_INLINE byte ctMaskGT(int a, int b) 00316 { 00317 return (((word32)a - b - 1) >> 31) - 1; 00318 } 00319 00320 /* Constant time - mask set when a >= b. */ 00321 STATIC WC_INLINE byte ctMaskGTE(int a, int b) 00322 { 00323 return (((word32)a - b ) >> 31) - 1; 00324 } 00325 00326 /* Constant time - mask set when a < b. */ 00327 STATIC WC_INLINE byte ctMaskLT(int a, int b) 00328 { 00329 return (((word32)b - a - 1) >> 31) - 1; 00330 } 00331 00332 /* Constant time - mask set when a <= b. */ 00333 STATIC WC_INLINE byte ctMaskLTE(int a, int b) 00334 { 00335 return (((word32)b - a ) >> 31) - 1; 00336 } 00337 00338 /* Constant time - mask set when a == b. */ 00339 STATIC WC_INLINE byte ctMaskEq(int a, int b) 00340 { 00341 return 0 - (a == b); 00342 } 00343 00344 /* Constant time - select b when mask is set and a otherwise. */ 00345 STATIC WC_INLINE byte ctMaskSel(byte m, byte a, byte b) 00346 { 00347 return (a & ((byte)~(word32)m)) | (b & m); 00348 } 00349 00350 /* Constant time - bit set when a <= b. */ 00351 STATIC WC_INLINE byte ctSetLTE(int a, int b) 00352 { 00353 return ((word32)a - b - 1) >> 31; 00354 } 00355 00356 00357 #undef STATIC 00358 00359 #endif /* !WOLFSSL_MISC_INCLUDED && !NO_INLINE */ 00360 00361 #endif /* WOLF_CRYPT_MISC_C */ 00362
Generated on Tue Jul 12 2022 16:58:06 by
1.7.2