3 * Copyright (C) 2006-2012 Sawtooth Consulting Ltd.
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
26 #include <cyassl/ctaocrypt/misc.h>
28 /* inlining these functions is a huge speed increase and a small size decrease,
29 because the functions are smaller than function call setup/cleanup, e.g.,
30 md5 benchmark is twice as fast with inline. If you don't want it, then
31 define NO_INLINE and compile this file into cyassl, otherwise it's used as
42 #ifdef INTEL_INTRINSICS
44 #include <stdlib.h> /* get intrinsic definitions */
46 #pragma intrinsic(_lrotl, _lrotr)
48 STATIC INLINE word32 rotlFixed(word32 x, word32 y)
50 return y ? _lrotl(x, y) : x;
53 STATIC INLINE word32 rotrFixed(word32 x, word32 y)
55 return y ? _lrotr(x, y) : x;
60 STATIC INLINE word32 rotlFixed(word32 x, word32 y)
62 return (x << y) | (x >> (sizeof(y) * 8 - y));
66 STATIC INLINE word32 rotrFixed(word32 x, word32 y)
68 return (x >> y) | (x << (sizeof(y) * 8 - y));
74 STATIC INLINE word32 ByteReverseWord32(word32 value)
77 /* PPC: load reverse indexed instruction */
78 return (word32)__lwbrx(&value,0);
79 #elif defined(FAST_ROTATE)
80 /* 5 instructions with rotate instruction, 9 without */
81 return (rotrFixed(value, 8U) & 0xff00ff00) |
82 (rotlFixed(value, 8U) & 0x00ff00ff);
84 /* 6 instructions with rotate instruction, 8 without */
85 value = ((value & 0xFF00FF00) >> 8) | ((value & 0x00FF00FF) << 8);
86 return rotlFixed(value, 16U);
91 STATIC INLINE void ByteReverseWords(word32* out, const word32* in,
94 word32 count = byteCount/sizeof(word32), i;
96 for (i = 0; i < count; i++)
97 out[i] = ByteReverseWord32(in[i]);
102 #ifdef WORD64_AVAILABLE
105 STATIC INLINE word64 rotlFixed64(word64 x, word64 y)
107 return (x << y) | (x >> (sizeof(y) * 8 - y));
111 STATIC INLINE word64 rotrFixed64(word64 x, word64 y)
113 return (x >> y) | (x << (sizeof(y) * 8 - y));
117 STATIC INLINE word64 ByteReverseWord64(word64 value)
119 #ifdef CTAOCRYPT_SLOW_WORD64
120 return (word64)(ByteReverseWord32((word32)value)) << 32 |
121 ByteReverseWord32((word32)(value>>32));
123 value = ((value & W64LIT(0xFF00FF00FF00FF00)) >> 8) |
124 ((value & W64LIT(0x00FF00FF00FF00FF)) << 8);
125 value = ((value & W64LIT(0xFFFF0000FFFF0000)) >> 16) |
126 ((value & W64LIT(0x0000FFFF0000FFFF)) << 16);
127 return rotlFixed64(value, 32U);
132 STATIC INLINE void ByteReverseWords64(word64* out, const word64* in,
135 word32 count = byteCount/sizeof(word64), i;
137 for (i = 0; i < count; i++)
138 out[i] = ByteReverseWord64(in[i]);
142 #endif /* WORD64_AVAILABLE */
145 STATIC INLINE void ByteReverseBytes(byte* out, const byte* in, word32 byteCount)
147 word32* op = (word32*)out;
148 const word32* ip = (const word32*)in;
150 ByteReverseWords(op, ip, byteCount);
154 STATIC INLINE void XorWords(word* r, const word* a, word32 n)
158 for (i = 0; i < n; i++) r[i] ^= a[i];
162 STATIC INLINE void xorbuf(byte* buf, const byte* mask, word32 count)
164 if (((word)buf | (word)mask | count) % WORD_SIZE == 0)
165 XorWords( (word*)buf, (const word*)mask, count / WORD_SIZE);
168 for (i = 0; i < count; i++) buf[i] ^= mask[i];