This is a port of cyaSSL 2.7.0.
Dependents: CyaSSL_DTLS_Cellular CyaSSL_DTLS_Ethernet
misc.c
00001 /* misc.c 00002 * 00003 * Copyright (C) 2006-2013 wolfSSL Inc. 00004 * 00005 * This file is part of CyaSSL. 00006 * 00007 * CyaSSL 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 * CyaSSL 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA 00020 */ 00021 00022 #ifdef HAVE_CONFIG_H 00023 #include <config.h> 00024 #endif 00025 00026 #include <cyassl/ctaocrypt/settings.h> 00027 00028 #include <cyassl/ctaocrypt/misc.h> 00029 00030 /* inlining these functions is a huge speed increase and a small size decrease, 00031 because the functions are smaller than function call setup/cleanup, e.g., 00032 md5 benchmark is twice as fast with inline. If you don't want it, then 00033 define NO_INLINE and compile this file into cyassl, otherwise it's used as 00034 a source header 00035 */ 00036 00037 #ifdef NO_INLINE 00038 #define STATIC 00039 #else 00040 #define STATIC static 00041 #endif 00042 00043 00044 #ifdef INTEL_INTRINSICS 00045 00046 #include <stdlib.h> /* get intrinsic definitions */ 00047 00048 #pragma intrinsic(_lrotl, _lrotr) 00049 00050 STATIC INLINE word32 rotlFixed(word32 x, word32 y) 00051 { 00052 return y ? _lrotl(x, y) : x; 00053 } 00054 00055 STATIC INLINE word32 rotrFixed(word32 x, word32 y) 00056 { 00057 return y ? _lrotr(x, y) : x; 00058 } 00059 00060 #else /* generic */ 00061 00062 STATIC INLINE word32 rotlFixed(word32 x, word32 y) 00063 { 00064 return (x << y) | (x >> (sizeof(y) * 8 - y)); 00065 } 00066 00067 00068 STATIC INLINE word32 rotrFixed(word32 x, word32 y) 00069 { 00070 return (x >> y) | (x << (sizeof(y) * 8 - y)); 00071 } 00072 00073 #endif 00074 00075 00076 STATIC INLINE word32 ByteReverseWord32(word32 value) 00077 { 00078 #ifdef PPC_INTRINSICS 00079 /* PPC: load reverse indexed instruction */ 00080 return (word32)__lwbrx(&value,0); 00081 #elif defined(KEIL_INTRINSICS) 00082 return (word32)__rev(value); 00083 #elif defined(FAST_ROTATE) 00084 /* 5 instructions with rotate instruction, 9 without */ 00085 return (rotrFixed(value, 8U) & 0xff00ff00) | 00086 (rotlFixed(value, 8U) & 0x00ff00ff); 00087 #else 00088 /* 6 instructions with rotate instruction, 8 without */ 00089 value = ((value & 0xFF00FF00) >> 8) | ((value & 0x00FF00FF) << 8); 00090 return rotlFixed(value, 16U); 00091 #endif 00092 } 00093 00094 00095 STATIC INLINE void ByteReverseWords(word32* out, const word32* in, 00096 word32 byteCount) 00097 { 00098 word32 count = byteCount/(word32)sizeof(word32), i; 00099 00100 for (i = 0; i < count; i++) 00101 out[i] = ByteReverseWord32(in[i]); 00102 00103 } 00104 00105 00106 #ifdef WORD64_AVAILABLE 00107 00108 00109 STATIC INLINE word64 rotlFixed64(word64 x, word64 y) 00110 { 00111 return (x << y) | (x >> (sizeof(y) * 8 - y)); 00112 } 00113 00114 00115 STATIC INLINE word64 rotrFixed64(word64 x, word64 y) 00116 { 00117 return (x >> y) | (x << (sizeof(y) * 8 - y)); 00118 } 00119 00120 00121 STATIC INLINE word64 ByteReverseWord64(word64 value) 00122 { 00123 #ifdef CTAOCRYPT_SLOW_WORD64 00124 return (word64)(ByteReverseWord32((word32)value)) << 32 | 00125 ByteReverseWord32((word32)(value>>32)); 00126 #else 00127 value = ((value & W64LIT(0xFF00FF00FF00FF00)) >> 8) | 00128 ((value & W64LIT(0x00FF00FF00FF00FF)) << 8); 00129 value = ((value & W64LIT(0xFFFF0000FFFF0000)) >> 16) | 00130 ((value & W64LIT(0x0000FFFF0000FFFF)) << 16); 00131 return rotlFixed64(value, 32U); 00132 #endif 00133 } 00134 00135 00136 STATIC INLINE void ByteReverseWords64(word64* out, const word64* in, 00137 word32 byteCount) 00138 { 00139 word32 count = byteCount/(word32)sizeof(word64), i; 00140 00141 for (i = 0; i < count; i++) 00142 out[i] = ByteReverseWord64(in[i]); 00143 00144 } 00145 00146 #endif /* WORD64_AVAILABLE */ 00147 00148 00149 STATIC INLINE void ByteReverseBytes(byte* out, const byte* in, word32 byteCount) 00150 { 00151 word32* op = (word32*)out; 00152 const word32* ip = (const word32*)in; 00153 00154 ByteReverseWords(op, ip, byteCount); 00155 } 00156 00157 00158 STATIC INLINE void XorWords(word* r, const word* a, word32 n) 00159 { 00160 word32 i; 00161 00162 for (i = 0; i < n; i++) r[i] ^= a[i]; 00163 } 00164 00165 00166 STATIC INLINE void xorbuf(byte* buf, const byte* mask, word32 count) 00167 { 00168 if (((word)buf | (word)mask | count) % CYASSL_WORD_SIZE == 0) 00169 XorWords( (word*)buf, (const word*)mask, count / CYASSL_WORD_SIZE); 00170 else { 00171 word32 i; 00172 for (i = 0; i < count; i++) buf[i] ^= mask[i]; 00173 } 00174 } 00175 #undef STATIC 00176
Generated on Tue Jul 12 2022 20:44:51 by 1.7.2