]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Source/CyaSSL/ctaocrypt/src/misc.c
Update CyaSSL to latest version.
[freertos] / FreeRTOS-Plus / Source / CyaSSL / ctaocrypt / src / misc.c
1 /* misc.c
2  *
3  * Copyright (C) 2006-2014 wolfSSL Inc.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #ifdef HAVE_CONFIG_H
23     #include <config.h>
24 #endif
25
26 #include <cyassl/ctaocrypt/settings.h>
27
28 #include <cyassl/ctaocrypt/misc.h>
29
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
34    a source header
35  */
36
37 #ifdef NO_INLINE
38     #define STATIC
39 #else
40     #define STATIC static
41 #endif
42
43
44 #ifdef INTEL_INTRINSICS
45
46     #include <stdlib.h>      /* get intrinsic definitions */
47
48     /* for non visual studio probably need no long version, 32 bit only
49      * i.e., _rotl and _rotr */
50     #pragma intrinsic(_lrotl, _lrotr)
51
52     STATIC INLINE word32 rotlFixed(word32 x, word32 y)
53     {
54         return y ? _lrotl(x, y) : x;
55     }
56
57     STATIC INLINE word32 rotrFixed(word32 x, word32 y)
58     {
59         return y ? _lrotr(x, y) : x;
60     }
61
62 #else /* generic */
63
64     STATIC INLINE word32 rotlFixed(word32 x, word32 y)
65     {
66         return (x << y) | (x >> (sizeof(y) * 8 - y));
67     }   
68
69
70     STATIC INLINE word32 rotrFixed(word32 x, word32 y)
71     {
72         return (x >> y) | (x << (sizeof(y) * 8 - y));
73     }
74
75 #endif
76
77
78 STATIC INLINE word32 ByteReverseWord32(word32 value)
79 {
80 #ifdef PPC_INTRINSICS
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);
89 #else
90     /* 6 instructions with rotate instruction, 8 without */
91     value = ((value & 0xFF00FF00) >> 8) | ((value & 0x00FF00FF) << 8);
92     return rotlFixed(value, 16U);
93 #endif
94 }
95
96
97 STATIC INLINE void ByteReverseWords(word32* out, const word32* in,
98                                     word32 byteCount)
99 {
100     word32 count = byteCount/(word32)sizeof(word32), i;
101
102     for (i = 0; i < count; i++)
103         out[i] = ByteReverseWord32(in[i]);
104
105 }
106
107
108 #ifdef WORD64_AVAILABLE
109
110
111 STATIC INLINE word64 rotlFixed64(word64 x, word64 y)
112 {
113     return (x << y) | (x >> (sizeof(y) * 8 - y));
114 }  
115
116
117 STATIC INLINE word64 rotrFixed64(word64 x, word64 y)
118 {
119     return (x >> y) | (x << (sizeof(y) * 8 - y));
120 }
121
122
123 STATIC INLINE word64 ByteReverseWord64(word64 value)
124 {
125 #ifdef CTAOCRYPT_SLOW_WORD64
126         return (word64)(ByteReverseWord32((word32)value)) << 32 | 
127                     ByteReverseWord32((word32)(value>>32));
128 #else
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);
134 #endif
135 }
136
137
138 STATIC INLINE void ByteReverseWords64(word64* out, const word64* in,
139                                       word32 byteCount)
140 {
141     word32 count = byteCount/(word32)sizeof(word64), i;
142
143     for (i = 0; i < count; i++)
144         out[i] = ByteReverseWord64(in[i]);
145
146 }
147
148 #endif /* WORD64_AVAILABLE */
149
150
151 STATIC INLINE void XorWords(word* r, const word* a, word32 n)
152 {
153     word32 i;
154
155     for (i = 0; i < n; i++) r[i] ^= a[i];
156 }
157
158
159 STATIC INLINE void xorbuf(void* buf, const void* mask, word32 count)
160 {
161     if (((word)buf | (word)mask | count) % CYASSL_WORD_SIZE == 0)
162         XorWords( (word*)buf, (const word*)mask, count / CYASSL_WORD_SIZE);
163     else {
164         word32 i;
165         byte*       b = (byte*)buf;
166         const byte* m = (const byte*)mask;
167
168         for (i = 0; i < count; i++) b[i] ^= m[i];
169     }
170 }
171 #undef STATIC
172