]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/CyaSSL/ctaocrypt/src/misc.c
Add FreeRTOS-Plus directory.
[freertos] / FreeRTOS-Plus / CyaSSL / ctaocrypt / src / misc.c
1 /* misc.c
2  *
3  * Copyright (C) 2006-2012 Sawtooth Consulting Ltd.
4  *
5  * This file is part of CyaSSL.
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #ifdef HAVE_CONFIG_H
23     #include <config.h>
24 #endif
25
26 #include <cyassl/ctaocrypt/misc.h>
27
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
32    a source header
33  */
34
35 #ifdef NO_INLINE
36     #define STATIC
37 #else
38     #define STATIC static
39 #endif
40
41
42 #ifdef INTEL_INTRINSICS
43
44     #include <stdlib.h>      /* get intrinsic definitions */
45
46     #pragma intrinsic(_lrotl, _lrotr)
47
48     STATIC INLINE word32 rotlFixed(word32 x, word32 y)
49     {
50         return y ? _lrotl(x, y) : x;
51     }
52
53     STATIC INLINE word32 rotrFixed(word32 x, word32 y)
54     {
55         return y ? _lrotr(x, y) : x;
56     }
57
58 #else /* generic */
59
60     STATIC INLINE word32 rotlFixed(word32 x, word32 y)
61     {
62         return (x << y) | (x >> (sizeof(y) * 8 - y));
63     }   
64
65
66     STATIC INLINE word32 rotrFixed(word32 x, word32 y)
67     {
68         return (x >> y) | (x << (sizeof(y) * 8 - y));
69     }
70
71 #endif
72
73
74 STATIC INLINE word32 ByteReverseWord32(word32 value)
75 {
76 #ifdef PPC_INTRINSICS
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);
83 #else
84     /* 6 instructions with rotate instruction, 8 without */
85     value = ((value & 0xFF00FF00) >> 8) | ((value & 0x00FF00FF) << 8);
86     return rotlFixed(value, 16U);
87 #endif
88 }
89
90
91 STATIC INLINE void ByteReverseWords(word32* out, const word32* in,
92                                     word32 byteCount)
93 {
94     word32 count = byteCount/sizeof(word32), i;
95
96     for (i = 0; i < count; i++)
97         out[i] = ByteReverseWord32(in[i]);
98
99 }
100
101
102 #ifdef WORD64_AVAILABLE
103
104
105 STATIC INLINE word64 rotlFixed64(word64 x, word64 y)
106 {
107     return (x << y) | (x >> (sizeof(y) * 8 - y));
108 }  
109
110
111 STATIC INLINE word64 rotrFixed64(word64 x, word64 y)
112 {
113     return (x >> y) | (x << (sizeof(y) * 8 - y));
114 }
115
116
117 STATIC INLINE word64 ByteReverseWord64(word64 value)
118 {
119 #ifdef CTAOCRYPT_SLOW_WORD64
120         return (word64)(ByteReverseWord32((word32)value)) << 32 | 
121                     ByteReverseWord32((word32)(value>>32));
122 #else
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);
128 #endif
129 }
130
131
132 STATIC INLINE void ByteReverseWords64(word64* out, const word64* in,
133                                       word32 byteCount)
134 {
135     word32 count = byteCount/sizeof(word64), i;
136
137     for (i = 0; i < count; i++)
138         out[i] = ByteReverseWord64(in[i]);
139
140 }
141
142 #endif /* WORD64_AVAILABLE */
143
144
145 STATIC INLINE void ByteReverseBytes(byte* out, const byte* in, word32 byteCount)
146 {
147     word32* op       = (word32*)out;
148     const word32* ip = (const word32*)in;
149
150     ByteReverseWords(op, ip, byteCount);
151 }
152
153
154 STATIC INLINE void XorWords(word* r, const word* a, word32 n)
155 {
156     word32 i;
157
158     for (i = 0; i < n; i++) r[i] ^= a[i];
159 }
160
161
162 STATIC INLINE void xorbuf(byte* buf, const byte* mask, word32 count)
163 {
164     if (((word)buf | (word)mask | count) % WORD_SIZE == 0)
165         XorWords( (word*)buf, (const word*)mask, count / WORD_SIZE);
166     else {
167         word32 i;
168         for (i = 0; i < count; i++) buf[i] ^= mask[i];
169     }
170 }
171
172
173 #undef STATIC
174