source: azure_iot_hub/trunk/wolfssl-3.15.7/wolfcrypt/src/rabbit.c@ 388

Last change on this file since 388 was 388, checked in by coas-nagasima, 5 years ago

Azure IoT Hub Device C SDK を使ったサンプルの追加

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