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