3 * Copyright (C) 2006-2014 wolfSSL Inc.
5 * This file is part of CyaSSL.
7 * CyaSSL 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 * CyaSSL 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 <cyassl/ctaocrypt/settings.h>
28 #include <cyassl/ctaocrypt/misc.h>
30 /* inlining these functions is a huge speed increase and a small size decrease,
31 because the functions are smaller than function call setup/cleanup, e.g.,
32 md5 benchmark is twice as fast with inline. If you don't want it, then
33 define NO_INLINE and compile this file into cyassl, otherwise it's used as
44 #ifdef INTEL_INTRINSICS
46 #include <stdlib.h> /* get intrinsic definitions */
48 /* for non visual studio probably need no long version, 32 bit only
49 * i.e., _rotl and _rotr */
50 #pragma intrinsic(_lrotl, _lrotr)
52 STATIC INLINE word32 rotlFixed(word32 x, word32 y)
54 return y ? _lrotl(x, y) : x;
57 STATIC INLINE word32 rotrFixed(word32 x, word32 y)
59 return y ? _lrotr(x, y) : x;
64 STATIC INLINE word32 rotlFixed(word32 x, word32 y)
66 return (x << y) | (x >> (sizeof(y) * 8 - y));
70 STATIC INLINE word32 rotrFixed(word32 x, word32 y)
72 return (x >> y) | (x << (sizeof(y) * 8 - y));
78 STATIC INLINE word32 ByteReverseWord32(word32 value)
81 /* PPC: load reverse indexed instruction */
82 return (word32)__lwbrx(&value,0);
83 #elif defined(KEIL_INTRINSICS)
84 return (word32)__rev(value);
85 #elif defined(FAST_ROTATE)
86 /* 5 instructions with rotate instruction, 9 without */
87 return (rotrFixed(value, 8U) & 0xff00ff00) |
88 (rotlFixed(value, 8U) & 0x00ff00ff);
90 /* 6 instructions with rotate instruction, 8 without */
91 value = ((value & 0xFF00FF00) >> 8) | ((value & 0x00FF00FF) << 8);
92 return rotlFixed(value, 16U);
97 STATIC INLINE void ByteReverseWords(word32* out, const word32* in,
100 word32 count = byteCount/(word32)sizeof(word32), i;
102 for (i = 0; i < count; i++)
103 out[i] = ByteReverseWord32(in[i]);
108 #ifdef WORD64_AVAILABLE
111 STATIC INLINE word64 rotlFixed64(word64 x, word64 y)
113 return (x << y) | (x >> (sizeof(y) * 8 - y));
117 STATIC INLINE word64 rotrFixed64(word64 x, word64 y)
119 return (x >> y) | (x << (sizeof(y) * 8 - y));
123 STATIC INLINE word64 ByteReverseWord64(word64 value)
125 #ifdef CTAOCRYPT_SLOW_WORD64
126 return (word64)(ByteReverseWord32((word32)value)) << 32 |
127 ByteReverseWord32((word32)(value>>32));
129 value = ((value & W64LIT(0xFF00FF00FF00FF00)) >> 8) |
130 ((value & W64LIT(0x00FF00FF00FF00FF)) << 8);
131 value = ((value & W64LIT(0xFFFF0000FFFF0000)) >> 16) |
132 ((value & W64LIT(0x0000FFFF0000FFFF)) << 16);
133 return rotlFixed64(value, 32U);
138 STATIC INLINE void ByteReverseWords64(word64* out, const word64* in,
141 word32 count = byteCount/(word32)sizeof(word64), i;
143 for (i = 0; i < count; i++)
144 out[i] = ByteReverseWord64(in[i]);
148 #endif /* WORD64_AVAILABLE */
151 STATIC INLINE void XorWords(word* r, const word* a, word32 n)
155 for (i = 0; i < n; i++) r[i] ^= a[i];
159 STATIC INLINE void xorbuf(void* buf, const void* mask, word32 count)
161 if (((word)buf | (word)mask | count) % CYASSL_WORD_SIZE == 0)
162 XorWords( (word*)buf, (const word*)mask, count / CYASSL_WORD_SIZE);
165 byte* b = (byte*)buf;
166 const byte* m = (const byte*)mask;
168 for (i = 0; i < count; i++) b[i] ^= m[i];