3 * Copyright (C) 2006-2015 wolfSSL Inc.
5 * This file is part of wolfSSL. (formerly known as CyaSSL)
7 * wolfSSL is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * wolfSSL is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
26 #include <wolfssl/wolfcrypt/settings.h>
28 #ifndef WOLF_CRYPT_MISC_C
29 #define WOLF_CRYPT_MISC_C
31 #include <wolfssl/wolfcrypt/misc.h>
33 /* inlining these functions is a huge speed increase and a small size decrease,
34 because the functions are smaller than function call setup/cleanup, e.g.,
35 md5 benchmark is twice as fast with inline. If you don't want it, then
36 define NO_INLINE and compile this file into wolfssl, otherwise it's used as
47 #ifdef INTEL_INTRINSICS
49 #include <stdlib.h> /* get intrinsic definitions */
51 /* for non visual studio probably need no long version, 32 bit only
52 * i.e., _rotl and _rotr */
53 #pragma intrinsic(_lrotl, _lrotr)
55 STATIC INLINE word32 rotlFixed(word32 x, word32 y)
57 return y ? _lrotl(x, y) : x;
60 STATIC INLINE word32 rotrFixed(word32 x, word32 y)
62 return y ? _lrotr(x, y) : x;
67 STATIC INLINE word32 rotlFixed(word32 x, word32 y)
69 return (x << y) | (x >> (sizeof(y) * 8 - y));
73 STATIC INLINE word32 rotrFixed(word32 x, word32 y)
75 return (x >> y) | (x << (sizeof(y) * 8 - y));
81 STATIC INLINE word32 ByteReverseWord32(word32 value)
84 /* PPC: load reverse indexed instruction */
85 return (word32)__lwbrx(&value,0);
86 #elif defined(KEIL_INTRINSICS)
87 return (word32)__rev(value);
88 #elif defined(FAST_ROTATE)
89 /* 5 instructions with rotate instruction, 9 without */
90 return (rotrFixed(value, 8U) & 0xff00ff00) |
91 (rotlFixed(value, 8U) & 0x00ff00ff);
93 /* 6 instructions with rotate instruction, 8 without */
94 value = ((value & 0xFF00FF00) >> 8) | ((value & 0x00FF00FF) << 8);
95 return rotlFixed(value, 16U);
100 STATIC INLINE void ByteReverseWords(word32* out, const word32* in,
103 word32 count = byteCount/(word32)sizeof(word32), i;
105 for (i = 0; i < count; i++)
106 out[i] = ByteReverseWord32(in[i]);
111 #ifdef WORD64_AVAILABLE
114 STATIC INLINE word64 rotlFixed64(word64 x, word64 y)
116 return (x << y) | (x >> (sizeof(y) * 8 - y));
120 STATIC INLINE word64 rotrFixed64(word64 x, word64 y)
122 return (x >> y) | (x << (sizeof(y) * 8 - y));
126 STATIC INLINE word64 ByteReverseWord64(word64 value)
128 #ifdef WOLFCRYPT_SLOW_WORD64
129 return (word64)(ByteReverseWord32((word32)value)) << 32 |
130 ByteReverseWord32((word32)(value>>32));
132 value = ((value & W64LIT(0xFF00FF00FF00FF00)) >> 8) |
133 ((value & W64LIT(0x00FF00FF00FF00FF)) << 8);
134 value = ((value & W64LIT(0xFFFF0000FFFF0000)) >> 16) |
135 ((value & W64LIT(0x0000FFFF0000FFFF)) << 16);
136 return rotlFixed64(value, 32U);
141 STATIC INLINE void ByteReverseWords64(word64* out, const word64* in,
144 word32 count = byteCount/(word32)sizeof(word64), i;
146 for (i = 0; i < count; i++)
147 out[i] = ByteReverseWord64(in[i]);
151 #endif /* WORD64_AVAILABLE */
154 STATIC INLINE void XorWords(wolfssl_word* r, const wolfssl_word* a, word32 n)
158 for (i = 0; i < n; i++) r[i] ^= a[i];
162 STATIC INLINE void xorbuf(void* buf, const void* mask, word32 count)
164 if (((wolfssl_word)buf | (wolfssl_word)mask | count) % WOLFSSL_WORD_SIZE == 0)
165 XorWords( (wolfssl_word*)buf,
166 (const wolfssl_word*)mask, count / WOLFSSL_WORD_SIZE);
169 byte* b = (byte*)buf;
170 const byte* m = (const byte*)mask;
172 for (i = 0; i < count; i++) b[i] ^= m[i];
177 /* Make sure compiler doesn't skip */
178 STATIC INLINE void ForceZero(const void* mem, word32 len)
180 volatile byte* z = (volatile byte*)mem;
182 while (len--) *z++ = 0;
186 /* check all length bytes for equality, return 0 on success */
187 STATIC INLINE int ConstantCompare(const byte* a, const byte* b, int length)
192 for (i = 0; i < length; i++) {
193 compareSum |= a[i] ^ b[i];
201 #endif /* WOLF_CRYPT_MISC_C */