]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/CyaSSL/ctaocrypt/src/rabbit.c
Commit 3 RX100 low power demos.
[freertos] / FreeRTOS-Plus / CyaSSL / ctaocrypt / src / rabbit.c
1 /* rabbit.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 #ifndef NO_RABBIT
27
28 #include <cyassl/ctaocrypt/rabbit.h>
29 #ifdef NO_INLINE
30     #include <cyassl/ctaocrypt/misc.h>
31 #else
32     #include <ctaocrypt/src/misc.c>
33 #endif
34
35
36 #ifdef BIG_ENDIAN_ORDER
37     #define LITTLE32(x) ByteReverseWord32(x)
38 #else
39     #define LITTLE32(x) (x)
40 #endif
41
42 #define U32V(x) (word32)(x)
43
44
45 /* Square a 32-bit unsigned integer to obtain the 64-bit result and return */
46 /* the upper 32 bits XOR the lower 32 bits */
47 static word32 RABBIT_g_func(word32 x)
48 {
49     /* Temporary variables */
50     word32 a, b, h, l;
51
52     /* Construct high and low argument for squaring */
53     a = x&0xFFFF;
54     b = x>>16;
55
56     /* Calculate high and low result of squaring */
57     h = (((U32V(a*a)>>17) + U32V(a*b))>>15) + b*b;
58     l = x*x;
59
60     /* Return high XOR low */
61     return U32V(h^l);
62 }
63
64
65 /* Calculate the next internal state */
66 static void RABBIT_next_state(RabbitCtx* ctx)
67 {
68     /* Temporary variables */
69     word32 g[8], c_old[8], i;
70
71     /* Save old counter values */
72     for (i=0; i<8; i++)
73         c_old[i] = ctx->c[i];
74
75     /* Calculate new counter values */
76     ctx->c[0] = U32V(ctx->c[0] + 0x4D34D34D + ctx->carry);
77     ctx->c[1] = U32V(ctx->c[1] + 0xD34D34D3 + (ctx->c[0] < c_old[0]));
78     ctx->c[2] = U32V(ctx->c[2] + 0x34D34D34 + (ctx->c[1] < c_old[1]));
79     ctx->c[3] = U32V(ctx->c[3] + 0x4D34D34D + (ctx->c[2] < c_old[2]));
80     ctx->c[4] = U32V(ctx->c[4] + 0xD34D34D3 + (ctx->c[3] < c_old[3]));
81     ctx->c[5] = U32V(ctx->c[5] + 0x34D34D34 + (ctx->c[4] < c_old[4]));
82     ctx->c[6] = U32V(ctx->c[6] + 0x4D34D34D + (ctx->c[5] < c_old[5]));
83     ctx->c[7] = U32V(ctx->c[7] + 0xD34D34D3 + (ctx->c[6] < c_old[6]));
84     ctx->carry = (ctx->c[7] < c_old[7]);
85    
86     /* Calculate the g-values */
87     for (i=0;i<8;i++)
88         g[i] = RABBIT_g_func(U32V(ctx->x[i] + ctx->c[i]));
89
90     /* Calculate new state values */
91     ctx->x[0] = U32V(g[0] + rotlFixed(g[7],16) + rotlFixed(g[6], 16));
92     ctx->x[1] = U32V(g[1] + rotlFixed(g[0], 8) + g[7]);
93     ctx->x[2] = U32V(g[2] + rotlFixed(g[1],16) + rotlFixed(g[0], 16));
94     ctx->x[3] = U32V(g[3] + rotlFixed(g[2], 8) + g[1]);
95     ctx->x[4] = U32V(g[4] + rotlFixed(g[3],16) + rotlFixed(g[2], 16));
96     ctx->x[5] = U32V(g[5] + rotlFixed(g[4], 8) + g[3]);
97     ctx->x[6] = U32V(g[6] + rotlFixed(g[5],16) + rotlFixed(g[4], 16));
98     ctx->x[7] = U32V(g[7] + rotlFixed(g[6], 8) + g[5]);
99 }
100
101
102 /* IV setup */
103 static void RabbitSetIV(Rabbit* ctx, const byte* iv)
104 {
105     /* Temporary variables */
106     word32 i0, i1, i2, i3, i;
107       
108     /* Generate four subvectors */
109     i0 = LITTLE32(*(word32*)(iv+0));
110     i2 = LITTLE32(*(word32*)(iv+4));
111     i1 = (i0>>16) | (i2&0xFFFF0000);
112     i3 = (i2<<16) | (i0&0x0000FFFF);
113
114     /* Modify counter values */
115     ctx->workCtx.c[0] = ctx->masterCtx.c[0] ^ i0;
116     ctx->workCtx.c[1] = ctx->masterCtx.c[1] ^ i1;
117     ctx->workCtx.c[2] = ctx->masterCtx.c[2] ^ i2;
118     ctx->workCtx.c[3] = ctx->masterCtx.c[3] ^ i3;
119     ctx->workCtx.c[4] = ctx->masterCtx.c[4] ^ i0;
120     ctx->workCtx.c[5] = ctx->masterCtx.c[5] ^ i1;
121     ctx->workCtx.c[6] = ctx->masterCtx.c[6] ^ i2;
122     ctx->workCtx.c[7] = ctx->masterCtx.c[7] ^ i3;
123
124     /* Copy state variables */
125     for (i=0; i<8; i++)
126         ctx->workCtx.x[i] = ctx->masterCtx.x[i];
127     ctx->workCtx.carry = ctx->masterCtx.carry;
128
129     /* Iterate the system four times */
130     for (i=0; i<4; i++)
131         RABBIT_next_state(&(ctx->workCtx));
132 }
133
134
135 /* Key setup */
136 void RabbitSetKey(Rabbit* ctx, const byte* key, const byte* iv)
137 {
138     /* Temporary variables */
139     word32 k0, k1, k2, k3, i;
140
141     /* Generate four subkeys */
142     k0 = LITTLE32(*(word32*)(key+ 0));
143     k1 = LITTLE32(*(word32*)(key+ 4));
144     k2 = LITTLE32(*(word32*)(key+ 8));
145     k3 = LITTLE32(*(word32*)(key+12));
146
147     /* Generate initial state variables */
148     ctx->masterCtx.x[0] = k0;
149     ctx->masterCtx.x[2] = k1;
150     ctx->masterCtx.x[4] = k2;
151     ctx->masterCtx.x[6] = k3;
152     ctx->masterCtx.x[1] = U32V(k3<<16) | (k2>>16);
153     ctx->masterCtx.x[3] = U32V(k0<<16) | (k3>>16);
154     ctx->masterCtx.x[5] = U32V(k1<<16) | (k0>>16);
155     ctx->masterCtx.x[7] = U32V(k2<<16) | (k1>>16);
156
157     /* Generate initial counter values */
158     ctx->masterCtx.c[0] = rotlFixed(k2, 16);
159     ctx->masterCtx.c[2] = rotlFixed(k3, 16);
160     ctx->masterCtx.c[4] = rotlFixed(k0, 16);
161     ctx->masterCtx.c[6] = rotlFixed(k1, 16);
162     ctx->masterCtx.c[1] = (k0&0xFFFF0000) | (k1&0xFFFF);
163     ctx->masterCtx.c[3] = (k1&0xFFFF0000) | (k2&0xFFFF);
164     ctx->masterCtx.c[5] = (k2&0xFFFF0000) | (k3&0xFFFF);
165     ctx->masterCtx.c[7] = (k3&0xFFFF0000) | (k0&0xFFFF);
166
167     /* Clear carry bit */
168     ctx->masterCtx.carry = 0;
169
170     /* Iterate the system four times */
171     for (i=0; i<4; i++)
172         RABBIT_next_state(&(ctx->masterCtx));
173
174     /* Modify the counters */
175     for (i=0; i<8; i++)
176         ctx->masterCtx.c[i] ^= ctx->masterCtx.x[(i+4)&0x7];
177
178     /* Copy master instance to work instance */
179     for (i=0; i<8; i++) {
180         ctx->workCtx.x[i] = ctx->masterCtx.x[i];
181         ctx->workCtx.c[i] = ctx->masterCtx.c[i];
182     }
183     ctx->workCtx.carry = ctx->masterCtx.carry;
184
185     if (iv) RabbitSetIV(ctx, iv);    
186 }
187
188
189 /* Encrypt/decrypt a message of any size */
190 void RabbitProcess(Rabbit* ctx, byte* output, const byte* input, word32 msglen)
191 {
192
193     /* Encrypt/decrypt all full blocks */
194     while (msglen >= 16) {
195         /* Iterate the system */
196         RABBIT_next_state(&(ctx->workCtx));
197
198         /* Encrypt/decrypt 16 bytes of data */
199         *(word32*)(output+ 0) = *(word32*)(input+ 0) ^
200                    LITTLE32(ctx->workCtx.x[0] ^ (ctx->workCtx.x[5]>>16) ^
201                    U32V(ctx->workCtx.x[3]<<16));
202         *(word32*)(output+ 4) = *(word32*)(input+ 4) ^
203                    LITTLE32(ctx->workCtx.x[2] ^ (ctx->workCtx.x[7]>>16) ^
204                    U32V(ctx->workCtx.x[5]<<16));
205         *(word32*)(output+ 8) = *(word32*)(input+ 8) ^
206                    LITTLE32(ctx->workCtx.x[4] ^ (ctx->workCtx.x[1]>>16) ^
207                    U32V(ctx->workCtx.x[7]<<16));
208         *(word32*)(output+12) = *(word32*)(input+12) ^
209                    LITTLE32(ctx->workCtx.x[6] ^ (ctx->workCtx.x[3]>>16) ^
210                    U32V(ctx->workCtx.x[1]<<16));
211
212         /* Increment pointers and decrement length */
213         input += 16;
214         output += 16;
215         msglen -= 16;
216     }
217
218     /* Encrypt/decrypt remaining data */
219     if (msglen) {
220
221         word32 i;
222         word32 tmp[4];
223         byte*  buffer = (byte*)tmp;
224
225         /* Iterate the system */
226         RABBIT_next_state(&(ctx->workCtx));
227
228         /* Generate 16 bytes of pseudo-random data */
229         tmp[0] = LITTLE32(ctx->workCtx.x[0] ^
230                   (ctx->workCtx.x[5]>>16) ^ U32V(ctx->workCtx.x[3]<<16));
231         tmp[1] = LITTLE32(ctx->workCtx.x[2] ^ 
232                   (ctx->workCtx.x[7]>>16) ^ U32V(ctx->workCtx.x[5]<<16));
233         tmp[2] = LITTLE32(ctx->workCtx.x[4] ^ 
234                   (ctx->workCtx.x[1]>>16) ^ U32V(ctx->workCtx.x[7]<<16));
235         tmp[3] = LITTLE32(ctx->workCtx.x[6] ^ 
236                   (ctx->workCtx.x[3]>>16) ^ U32V(ctx->workCtx.x[1]<<16));
237
238         /* Encrypt/decrypt the data */
239         for (i=0; i<msglen; i++)
240             output[i] = input[i] ^ buffer[i];
241     }
242 }
243
244
245
246 #endif /* NO_RABBIT */