]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Source/WolfSSL/ctaocrypt/src/rabbit.c
Rename the CyaSSL directory to WolfSSL
[freertos] / FreeRTOS-Plus / Source / WolfSSL / ctaocrypt / src / rabbit.c
1 /* rabbit.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 #ifndef NO_RABBIT
29
30 #include <cyassl/ctaocrypt/rabbit.h>
31 #include <cyassl/ctaocrypt/error-crypt.h>
32 #include <cyassl/ctaocrypt/logging.h>
33 #ifdef NO_INLINE
34     #include <cyassl/ctaocrypt/misc.h>
35 #else
36     #include <ctaocrypt/src/misc.c>
37 #endif
38
39
40 #ifdef BIG_ENDIAN_ORDER
41     #define LITTLE32(x) ByteReverseWord32(x)
42 #else
43     #define LITTLE32(x) (x)
44 #endif
45
46 #define U32V(x) ((word32)(x) & 0xFFFFFFFFU)
47
48
49 /* Square a 32-bit unsigned integer to obtain the 64-bit result and return */
50 /* the upper 32 bits XOR the lower 32 bits */
51 static word32 RABBIT_g_func(word32 x)
52 {
53     /* Temporary variables */
54     word32 a, b, h, l;
55
56     /* Construct high and low argument for squaring */
57     a = x&0xFFFF;
58     b = x>>16;
59
60     /* Calculate high and low result of squaring */
61     h = (((U32V(a*a)>>17) + U32V(a*b))>>15) + b*b;
62     l = x*x;
63
64     /* Return high XOR low */
65     return U32V(h^l);
66 }
67
68
69 /* Calculate the next internal state */
70 static void RABBIT_next_state(RabbitCtx* ctx)
71 {
72     /* Temporary variables */
73     word32 g[8], c_old[8], i;
74
75     /* Save old counter values */
76     for (i=0; i<8; i++)
77         c_old[i] = ctx->c[i];
78
79     /* Calculate new counter values */
80     ctx->c[0] = U32V(ctx->c[0] + 0x4D34D34D + ctx->carry);
81     ctx->c[1] = U32V(ctx->c[1] + 0xD34D34D3 + (ctx->c[0] < c_old[0]));
82     ctx->c[2] = U32V(ctx->c[2] + 0x34D34D34 + (ctx->c[1] < c_old[1]));
83     ctx->c[3] = U32V(ctx->c[3] + 0x4D34D34D + (ctx->c[2] < c_old[2]));
84     ctx->c[4] = U32V(ctx->c[4] + 0xD34D34D3 + (ctx->c[3] < c_old[3]));
85     ctx->c[5] = U32V(ctx->c[5] + 0x34D34D34 + (ctx->c[4] < c_old[4]));
86     ctx->c[6] = U32V(ctx->c[6] + 0x4D34D34D + (ctx->c[5] < c_old[5]));
87     ctx->c[7] = U32V(ctx->c[7] + 0xD34D34D3 + (ctx->c[6] < c_old[6]));
88     ctx->carry = (ctx->c[7] < c_old[7]);
89    
90     /* Calculate the g-values */
91     for (i=0;i<8;i++)
92         g[i] = RABBIT_g_func(U32V(ctx->x[i] + ctx->c[i]));
93
94     /* Calculate new state values */
95     ctx->x[0] = U32V(g[0] + rotlFixed(g[7],16) + rotlFixed(g[6], 16));
96     ctx->x[1] = U32V(g[1] + rotlFixed(g[0], 8) + g[7]);
97     ctx->x[2] = U32V(g[2] + rotlFixed(g[1],16) + rotlFixed(g[0], 16));
98     ctx->x[3] = U32V(g[3] + rotlFixed(g[2], 8) + g[1]);
99     ctx->x[4] = U32V(g[4] + rotlFixed(g[3],16) + rotlFixed(g[2], 16));
100     ctx->x[5] = U32V(g[5] + rotlFixed(g[4], 8) + g[3]);
101     ctx->x[6] = U32V(g[6] + rotlFixed(g[5],16) + rotlFixed(g[4], 16));
102     ctx->x[7] = U32V(g[7] + rotlFixed(g[6], 8) + g[5]);
103 }
104
105
106 /* IV setup */
107 static void RabbitSetIV(Rabbit* ctx, const byte* inIv)
108 {
109     /* Temporary variables */
110     word32 i0, i1, i2, i3, i;
111     word32 iv[2];
112
113     if (inIv)
114         XMEMCPY(iv, inIv, sizeof(iv));
115     else
116         XMEMSET(iv,    0, sizeof(iv));
117       
118     /* Generate four subvectors */
119     i0 = LITTLE32(iv[0]);
120     i2 = LITTLE32(iv[1]);
121     i1 = (i0>>16) | (i2&0xFFFF0000);
122     i3 = (i2<<16) | (i0&0x0000FFFF);
123
124     /* Modify counter values */
125     ctx->workCtx.c[0] = ctx->masterCtx.c[0] ^ i0;
126     ctx->workCtx.c[1] = ctx->masterCtx.c[1] ^ i1;
127     ctx->workCtx.c[2] = ctx->masterCtx.c[2] ^ i2;
128     ctx->workCtx.c[3] = ctx->masterCtx.c[3] ^ i3;
129     ctx->workCtx.c[4] = ctx->masterCtx.c[4] ^ i0;
130     ctx->workCtx.c[5] = ctx->masterCtx.c[5] ^ i1;
131     ctx->workCtx.c[6] = ctx->masterCtx.c[6] ^ i2;
132     ctx->workCtx.c[7] = ctx->masterCtx.c[7] ^ i3;
133
134     /* Copy state variables */
135     for (i=0; i<8; i++)
136         ctx->workCtx.x[i] = ctx->masterCtx.x[i];
137     ctx->workCtx.carry = ctx->masterCtx.carry;
138
139     /* Iterate the system four times */
140     for (i=0; i<4; i++)
141         RABBIT_next_state(&(ctx->workCtx));
142 }
143
144
145 /* Key setup */
146 static INLINE int DoKey(Rabbit* ctx, const byte* key, const byte* iv)
147 {
148     /* Temporary variables */
149     word32 k0, k1, k2, k3, i;
150
151     /* Generate four subkeys */
152     k0 = LITTLE32(*(word32*)(key+ 0));
153     k1 = LITTLE32(*(word32*)(key+ 4));
154     k2 = LITTLE32(*(word32*)(key+ 8));
155     k3 = LITTLE32(*(word32*)(key+12));
156
157     /* Generate initial state variables */
158     ctx->masterCtx.x[0] = k0;
159     ctx->masterCtx.x[2] = k1;
160     ctx->masterCtx.x[4] = k2;
161     ctx->masterCtx.x[6] = k3;
162     ctx->masterCtx.x[1] = U32V(k3<<16) | (k2>>16);
163     ctx->masterCtx.x[3] = U32V(k0<<16) | (k3>>16);
164     ctx->masterCtx.x[5] = U32V(k1<<16) | (k0>>16);
165     ctx->masterCtx.x[7] = U32V(k2<<16) | (k1>>16);
166
167     /* Generate initial counter values */
168     ctx->masterCtx.c[0] = rotlFixed(k2, 16);
169     ctx->masterCtx.c[2] = rotlFixed(k3, 16);
170     ctx->masterCtx.c[4] = rotlFixed(k0, 16);
171     ctx->masterCtx.c[6] = rotlFixed(k1, 16);
172     ctx->masterCtx.c[1] = (k0&0xFFFF0000) | (k1&0xFFFF);
173     ctx->masterCtx.c[3] = (k1&0xFFFF0000) | (k2&0xFFFF);
174     ctx->masterCtx.c[5] = (k2&0xFFFF0000) | (k3&0xFFFF);
175     ctx->masterCtx.c[7] = (k3&0xFFFF0000) | (k0&0xFFFF);
176
177     /* Clear carry bit */
178     ctx->masterCtx.carry = 0;
179
180     /* Iterate the system four times */
181     for (i=0; i<4; i++)
182         RABBIT_next_state(&(ctx->masterCtx));
183
184     /* Modify the counters */
185     for (i=0; i<8; i++)
186         ctx->masterCtx.c[i] ^= ctx->masterCtx.x[(i+4)&0x7];
187
188     /* Copy master instance to work instance */
189     for (i=0; i<8; i++) {
190         ctx->workCtx.x[i] = ctx->masterCtx.x[i];
191         ctx->workCtx.c[i] = ctx->masterCtx.c[i];
192     }
193     ctx->workCtx.carry = ctx->masterCtx.carry;
194
195     RabbitSetIV(ctx, iv);
196
197     return 0;
198 }
199
200
201 /* Key setup */
202 int RabbitSetKey(Rabbit* ctx, const byte* key, const byte* iv)
203 {
204 #ifdef XSTREAM_ALIGN
205     if ((word)key % 4) {
206         int alignKey[4];
207
208         /* iv aligned in SetIV */
209         CYASSL_MSG("RabbitSetKey unaligned key");
210
211         XMEMCPY(alignKey, key, sizeof(alignKey));
212
213         return DoKey(ctx, (const byte*)alignKey, iv);
214     }
215 #endif /* XSTREAM_ALIGN */
216
217     return DoKey(ctx, key, iv);
218 }
219
220
221 /* Encrypt/decrypt a message of any size */
222 static INLINE int DoProcess(Rabbit* ctx, byte* output, const byte* input,
223                             word32 msglen)
224 {
225     /* Encrypt/decrypt all full blocks */
226     while (msglen >= 16) {
227         /* Iterate the system */
228         RABBIT_next_state(&(ctx->workCtx));
229
230         /* Encrypt/decrypt 16 bytes of data */
231         *(word32*)(output+ 0) = *(word32*)(input+ 0) ^
232                    LITTLE32(ctx->workCtx.x[0] ^ (ctx->workCtx.x[5]>>16) ^
233                    U32V(ctx->workCtx.x[3]<<16));
234         *(word32*)(output+ 4) = *(word32*)(input+ 4) ^
235                    LITTLE32(ctx->workCtx.x[2] ^ (ctx->workCtx.x[7]>>16) ^
236                    U32V(ctx->workCtx.x[5]<<16));
237         *(word32*)(output+ 8) = *(word32*)(input+ 8) ^
238                    LITTLE32(ctx->workCtx.x[4] ^ (ctx->workCtx.x[1]>>16) ^
239                    U32V(ctx->workCtx.x[7]<<16));
240         *(word32*)(output+12) = *(word32*)(input+12) ^
241                    LITTLE32(ctx->workCtx.x[6] ^ (ctx->workCtx.x[3]>>16) ^
242                    U32V(ctx->workCtx.x[1]<<16));
243
244         /* Increment pointers and decrement length */
245         input  += 16;
246         output += 16;
247         msglen -= 16;
248     }
249
250     /* Encrypt/decrypt remaining data */
251     if (msglen) {
252
253         word32 i;
254         word32 tmp[4];
255         byte*  buffer = (byte*)tmp;
256
257         XMEMSET(tmp, 0, sizeof(tmp));   /* help static analysis */
258
259         /* Iterate the system */
260         RABBIT_next_state(&(ctx->workCtx));
261
262         /* Generate 16 bytes of pseudo-random data */
263         tmp[0] = LITTLE32(ctx->workCtx.x[0] ^
264                   (ctx->workCtx.x[5]>>16) ^ U32V(ctx->workCtx.x[3]<<16));
265         tmp[1] = LITTLE32(ctx->workCtx.x[2] ^ 
266                   (ctx->workCtx.x[7]>>16) ^ U32V(ctx->workCtx.x[5]<<16));
267         tmp[2] = LITTLE32(ctx->workCtx.x[4] ^ 
268                   (ctx->workCtx.x[1]>>16) ^ U32V(ctx->workCtx.x[7]<<16));
269         tmp[3] = LITTLE32(ctx->workCtx.x[6] ^ 
270                   (ctx->workCtx.x[3]>>16) ^ U32V(ctx->workCtx.x[1]<<16));
271
272         /* Encrypt/decrypt the data */
273         for (i=0; i<msglen; i++)
274             output[i] = input[i] ^ buffer[i];
275     }
276
277     return 0;
278 }
279
280
281 /* Encrypt/decrypt a message of any size */
282 int RabbitProcess(Rabbit* ctx, byte* output, const byte* input, word32 msglen)
283 {
284 #ifdef XSTREAM_ALIGN
285     if ((word)input % 4 || (word)output % 4) {
286         #ifndef NO_CYASSL_ALLOC_ALIGN
287             byte* tmp;
288             CYASSL_MSG("RabbitProcess unaligned");
289
290             tmp = (byte*)XMALLOC(msglen, NULL, DYNAMIC_TYPE_TMP_BUFFER);
291             if (tmp == NULL) return MEMORY_E;
292
293             XMEMCPY(tmp, input, msglen);
294             DoProcess(ctx, tmp, tmp, msglen);
295             XMEMCPY(output, tmp, msglen);
296
297             XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
298
299             return 0;
300         #else
301             return BAD_ALIGN_E;
302         #endif
303     }
304 #endif /* XSTREAM_ALIGN */
305
306     return DoProcess(ctx, output, input, msglen);
307 }
308
309
310 #endif /* NO_RABBIT */