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
28 #include <cyassl/ctaocrypt/rabbit.h>
30 #include <cyassl/ctaocrypt/misc.h>
32 #include <ctaocrypt/src/misc.c>
36 #ifdef BIG_ENDIAN_ORDER
37 #define LITTLE32(x) ByteReverseWord32(x)
39 #define LITTLE32(x) (x)
42 #define U32V(x) (word32)(x)
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)
49 /* Temporary variables */
52 /* Construct high and low argument for squaring */
56 /* Calculate high and low result of squaring */
57 h = (((U32V(a*a)>>17) + U32V(a*b))>>15) + b*b;
60 /* Return high XOR low */
65 /* Calculate the next internal state */
66 static void RABBIT_next_state(RabbitCtx* ctx)
68 /* Temporary variables */
69 word32 g[8], c_old[8], i;
71 /* Save old counter values */
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]);
86 /* Calculate the g-values */
88 g[i] = RABBIT_g_func(U32V(ctx->x[i] + ctx->c[i]));
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]);
103 static void RabbitSetIV(Rabbit* ctx, const byte* iv)
105 /* Temporary variables */
106 word32 i0, i1, i2, i3, i;
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);
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;
124 /* Copy state variables */
126 ctx->workCtx.x[i] = ctx->masterCtx.x[i];
127 ctx->workCtx.carry = ctx->masterCtx.carry;
129 /* Iterate the system four times */
131 RABBIT_next_state(&(ctx->workCtx));
136 void RabbitSetKey(Rabbit* ctx, const byte* key, const byte* iv)
138 /* Temporary variables */
139 word32 k0, k1, k2, k3, i;
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));
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);
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);
167 /* Clear carry bit */
168 ctx->masterCtx.carry = 0;
170 /* Iterate the system four times */
172 RABBIT_next_state(&(ctx->masterCtx));
174 /* Modify the counters */
176 ctx->masterCtx.c[i] ^= ctx->masterCtx.x[(i+4)&0x7];
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];
183 ctx->workCtx.carry = ctx->masterCtx.carry;
185 if (iv) RabbitSetIV(ctx, iv);
189 /* Encrypt/decrypt a message of any size */
190 void RabbitProcess(Rabbit* ctx, byte* output, const byte* input, word32 msglen)
193 /* Encrypt/decrypt all full blocks */
194 while (msglen >= 16) {
195 /* Iterate the system */
196 RABBIT_next_state(&(ctx->workCtx));
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));
212 /* Increment pointers and decrement length */
218 /* Encrypt/decrypt remaining data */
223 byte* buffer = (byte*)tmp;
225 /* Iterate the system */
226 RABBIT_next_state(&(ctx->workCtx));
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));
238 /* Encrypt/decrypt the data */
239 for (i=0; i<msglen; i++)
240 output[i] = input[i] ^ buffer[i];
246 #endif /* NO_RABBIT */