]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Source/WolfSSL/ctaocrypt/src/hc128.c
4937d6eb9d5698dec11c8cd51c559a77526794ce
[freertos] / FreeRTOS-Plus / Source / WolfSSL / ctaocrypt / src / hc128.c
1 /* hc128.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 #ifdef HAVE_HC128
29
30 #include <cyassl/ctaocrypt/hc128.h>
31 #include <cyassl/ctaocrypt/error-crypt.h>
32 #include <cyassl/ctaocrypt/logging.h>
33 #ifdef NO_INLINE
34     #include <cyassl/ctaocrypt/hc128.h>
35                 #include <cyassl/ctaocrypt/misc.h>
36 #else
37     #include <ctaocrypt/src/misc.c>
38 #endif
39
40
41 #ifdef BIG_ENDIAN_ORDER
42     #define LITTLE32(x) ByteReverseWord32(x)
43 #else
44     #define LITTLE32(x) (x)
45 #endif
46
47
48 /*h1 function*/
49 #define h1(ctx, x, y) {                         \
50      byte a,c;                                  \
51      a = (byte) (x);                            \
52      c = (byte) ((x) >> 16);                    \
53      y = (ctx->T[512+a])+(ctx->T[512+256+c]);   \
54 }
55
56 /*h2 function*/
57 #define h2(ctx, x, y) {                         \
58      byte a,c;                                  \
59      a = (byte) (x);                            \
60      c = (byte) ((x) >> 16);                    \
61      y = (ctx->T[a])+(ctx->T[256+c]);           \
62 }
63
64 /*one step of HC-128, update P and generate 32 bits keystream*/
65 #define step_P(ctx,u,v,a,b,c,d,n){              \
66      word32 tem0,tem1,tem2,tem3;                \
67      h1((ctx),(ctx->X[(d)]),tem3);              \
68      tem0 = rotrFixed((ctx->T[(v)]),23);        \
69      tem1 = rotrFixed((ctx->X[(c)]),10);        \
70      tem2 = rotrFixed((ctx->X[(b)]),8);         \
71      (ctx->T[(u)]) += tem2+(tem0 ^ tem1);       \
72      (ctx->X[(a)]) = (ctx->T[(u)]);             \
73      (n) = tem3 ^ (ctx->T[(u)]) ;               \
74 }       
75
76 /*one step of HC-128, update Q and generate 32 bits keystream*/
77 #define step_Q(ctx,u,v,a,b,c,d,n){              \
78      word32 tem0,tem1,tem2,tem3;                \
79      h2((ctx),(ctx->Y[(d)]),tem3);              \
80      tem0 = rotrFixed((ctx->T[(v)]),(32-23));   \
81      tem1 = rotrFixed((ctx->Y[(c)]),(32-10));   \
82      tem2 = rotrFixed((ctx->Y[(b)]),(32-8));    \
83      (ctx->T[(u)]) += tem2 + (tem0 ^ tem1);     \
84      (ctx->Y[(a)]) = (ctx->T[(u)]);             \
85      (n) = tem3 ^ (ctx->T[(u)]) ;               \
86 }   
87
88 /*16 steps of HC-128, generate 512 bits keystream*/
89 static void generate_keystream(HC128* ctx, word32* keystream)  
90 {
91    word32 cc,dd;
92    cc = ctx->counter1024 & 0x1ff;
93    dd = (cc+16)&0x1ff;
94
95    if (ctx->counter1024 < 512)  
96    {            
97       ctx->counter1024 = (ctx->counter1024 + 16) & 0x3ff;
98       step_P(ctx, cc+0, cc+1, 0, 6, 13,4, keystream[0]);
99       step_P(ctx, cc+1, cc+2, 1, 7, 14,5, keystream[1]);
100       step_P(ctx, cc+2, cc+3, 2, 8, 15,6, keystream[2]);
101       step_P(ctx, cc+3, cc+4, 3, 9, 0, 7, keystream[3]);
102       step_P(ctx, cc+4, cc+5, 4, 10,1, 8, keystream[4]);
103       step_P(ctx, cc+5, cc+6, 5, 11,2, 9, keystream[5]);
104       step_P(ctx, cc+6, cc+7, 6, 12,3, 10,keystream[6]);
105       step_P(ctx, cc+7, cc+8, 7, 13,4, 11,keystream[7]);
106       step_P(ctx, cc+8, cc+9, 8, 14,5, 12,keystream[8]);
107       step_P(ctx, cc+9, cc+10,9, 15,6, 13,keystream[9]);
108       step_P(ctx, cc+10,cc+11,10,0, 7, 14,keystream[10]);
109       step_P(ctx, cc+11,cc+12,11,1, 8, 15,keystream[11]);
110       step_P(ctx, cc+12,cc+13,12,2, 9, 0, keystream[12]);
111       step_P(ctx, cc+13,cc+14,13,3, 10,1, keystream[13]);
112       step_P(ctx, cc+14,cc+15,14,4, 11,2, keystream[14]);
113       step_P(ctx, cc+15,dd+0, 15,5, 12,3, keystream[15]);
114    }
115    else                             
116    {
117           ctx->counter1024 = (ctx->counter1024 + 16) & 0x3ff;
118       step_Q(ctx, 512+cc+0, 512+cc+1, 0, 6, 13,4, keystream[0]);
119       step_Q(ctx, 512+cc+1, 512+cc+2, 1, 7, 14,5, keystream[1]);
120       step_Q(ctx, 512+cc+2, 512+cc+3, 2, 8, 15,6, keystream[2]);
121       step_Q(ctx, 512+cc+3, 512+cc+4, 3, 9, 0, 7, keystream[3]);
122       step_Q(ctx, 512+cc+4, 512+cc+5, 4, 10,1, 8, keystream[4]);
123       step_Q(ctx, 512+cc+5, 512+cc+6, 5, 11,2, 9, keystream[5]);
124       step_Q(ctx, 512+cc+6, 512+cc+7, 6, 12,3, 10,keystream[6]);
125       step_Q(ctx, 512+cc+7, 512+cc+8, 7, 13,4, 11,keystream[7]);
126       step_Q(ctx, 512+cc+8, 512+cc+9, 8, 14,5, 12,keystream[8]);
127       step_Q(ctx, 512+cc+9, 512+cc+10,9, 15,6, 13,keystream[9]);
128       step_Q(ctx, 512+cc+10,512+cc+11,10,0, 7, 14,keystream[10]);
129       step_Q(ctx, 512+cc+11,512+cc+12,11,1, 8, 15,keystream[11]);
130       step_Q(ctx, 512+cc+12,512+cc+13,12,2, 9, 0, keystream[12]);
131       step_Q(ctx, 512+cc+13,512+cc+14,13,3, 10,1, keystream[13]);
132       step_Q(ctx, 512+cc+14,512+cc+15,14,4, 11,2, keystream[14]);
133       step_Q(ctx, 512+cc+15,512+dd+0, 15,5, 12,3, keystream[15]);
134    }
135 }
136
137
138 /* The following defines the initialization functions */
139 #define f1(x)  (rotrFixed((x),7)  ^ rotrFixed((x),18) ^ ((x) >> 3))
140 #define f2(x)  (rotrFixed((x),17) ^ rotrFixed((x),19) ^ ((x) >> 10))
141
142 /*update table P*/
143 #define update_P(ctx,u,v,a,b,c,d){                  \
144      word32 tem0,tem1,tem2,tem3;                    \
145      tem0 = rotrFixed((ctx->T[(v)]),23);            \
146      tem1 = rotrFixed((ctx->X[(c)]),10);            \
147      tem2 = rotrFixed((ctx->X[(b)]),8);             \
148      h1((ctx),(ctx->X[(d)]),tem3);                  \
149      (ctx->T[(u)]) = ((ctx->T[(u)]) + tem2+(tem0^tem1)) ^ tem3;     \
150      (ctx->X[(a)]) = (ctx->T[(u)]);                 \
151 }  
152
153 /*update table Q*/
154 #define update_Q(ctx,u,v,a,b,c,d){                  \
155      word32 tem0,tem1,tem2,tem3;                    \
156      tem0 = rotrFixed((ctx->T[(v)]),(32-23));       \
157      tem1 = rotrFixed((ctx->Y[(c)]),(32-10));       \
158      tem2 = rotrFixed((ctx->Y[(b)]),(32-8));        \
159      h2((ctx),(ctx->Y[(d)]),tem3);                  \
160      (ctx->T[(u)]) = ((ctx->T[(u)]) + tem2+(tem0^tem1)) ^ tem3;     \
161      (ctx->Y[(a)]) = (ctx->T[(u)]);                 \
162 }     
163
164 /*16 steps of HC-128, without generating keystream, */
165 /*but use the outputs to update P and Q*/
166 static void setup_update(HC128* ctx)  /*each time 16 steps*/
167 {
168    word32 cc,dd;
169    cc = ctx->counter1024 & 0x1ff;
170    dd = (cc+16)&0x1ff;
171
172    if (ctx->counter1024 < 512)  
173    {            
174       ctx->counter1024 = (ctx->counter1024 + 16) & 0x3ff;
175       update_P(ctx, cc+0, cc+1, 0, 6, 13, 4);
176       update_P(ctx, cc+1, cc+2, 1, 7, 14, 5);
177       update_P(ctx, cc+2, cc+3, 2, 8, 15, 6);
178       update_P(ctx, cc+3, cc+4, 3, 9, 0,  7);
179       update_P(ctx, cc+4, cc+5, 4, 10,1,  8);
180       update_P(ctx, cc+5, cc+6, 5, 11,2,  9);
181       update_P(ctx, cc+6, cc+7, 6, 12,3,  10);
182       update_P(ctx, cc+7, cc+8, 7, 13,4,  11);
183       update_P(ctx, cc+8, cc+9, 8, 14,5,  12);
184       update_P(ctx, cc+9, cc+10,9, 15,6,  13);
185       update_P(ctx, cc+10,cc+11,10,0, 7,  14);
186       update_P(ctx, cc+11,cc+12,11,1, 8,  15);
187       update_P(ctx, cc+12,cc+13,12,2, 9,  0);
188       update_P(ctx, cc+13,cc+14,13,3, 10, 1);
189       update_P(ctx, cc+14,cc+15,14,4, 11, 2);
190       update_P(ctx, cc+15,dd+0, 15,5, 12, 3);   
191    }
192    else                             
193    {
194       ctx->counter1024 = (ctx->counter1024 + 16) & 0x3ff;
195       update_Q(ctx, 512+cc+0, 512+cc+1, 0, 6, 13, 4);
196       update_Q(ctx, 512+cc+1, 512+cc+2, 1, 7, 14, 5);
197       update_Q(ctx, 512+cc+2, 512+cc+3, 2, 8, 15, 6);
198       update_Q(ctx, 512+cc+3, 512+cc+4, 3, 9, 0,  7);
199       update_Q(ctx, 512+cc+4, 512+cc+5, 4, 10,1,  8);
200       update_Q(ctx, 512+cc+5, 512+cc+6, 5, 11,2,  9);
201       update_Q(ctx, 512+cc+6, 512+cc+7, 6, 12,3,  10);
202       update_Q(ctx, 512+cc+7, 512+cc+8, 7, 13,4,  11);
203       update_Q(ctx, 512+cc+8, 512+cc+9, 8, 14,5,  12);
204       update_Q(ctx, 512+cc+9, 512+cc+10,9, 15,6,  13);
205       update_Q(ctx, 512+cc+10,512+cc+11,10,0, 7,  14);
206       update_Q(ctx, 512+cc+11,512+cc+12,11,1, 8,  15);
207       update_Q(ctx, 512+cc+12,512+cc+13,12,2, 9,  0);
208       update_Q(ctx, 512+cc+13,512+cc+14,13,3, 10, 1);
209       update_Q(ctx, 512+cc+14,512+cc+15,14,4, 11, 2);
210       update_Q(ctx, 512+cc+15,512+dd+0, 15,5, 12, 3); 
211    }       
212 }
213
214
215 /* for the 128-bit key:  key[0]...key[15]
216 *  key[0] is the least significant byte of ctx->key[0] (K_0);
217 *  key[3] is the most significant byte of ctx->key[0]  (K_0);
218 *  ...
219 *  key[12] is the least significant byte of ctx->key[3] (K_3)
220 *  key[15] is the most significant byte of ctx->key[3]  (K_3)
221 *
222 *  for the 128-bit iv:  iv[0]...iv[15]
223 *  iv[0] is the least significant byte of ctx->iv[0] (IV_0);
224 *  iv[3] is the most significant byte of ctx->iv[0]  (IV_0);
225 *  ...
226 *  iv[12] is the least significant byte of ctx->iv[3] (IV_3)
227 *  iv[15] is the most significant byte of ctx->iv[3]  (IV_3)
228 */
229
230
231
232 static void Hc128_SetIV(HC128* ctx, const byte* inIv)
233
234     word32 i;
235     word32 iv[4];
236
237     if (inIv)
238         XMEMCPY(iv, inIv, sizeof(iv));
239     else
240         XMEMSET(iv,    0, sizeof(iv));
241     
242         for (i = 0; i < (128 >> 5); i++)
243         ctx->iv[i] = LITTLE32(iv[i]);
244         
245     for (; i < 8; i++) ctx->iv[i] = ctx->iv[i-4];
246   
247     /* expand the key and IV into the table T */ 
248     /* (expand the key and IV into the table P and Q) */ 
249         
250         for (i = 0; i < 8;  i++)   ctx->T[i] = ctx->key[i];
251         for (i = 8; i < 16; i++)   ctx->T[i] = ctx->iv[i-8];
252
253     for (i = 16; i < (256+16); i++) 
254                 ctx->T[i] = f2(ctx->T[i-2]) + ctx->T[i-7] + f1(ctx->T[i-15]) +
255                                                        ctx->T[i-16]+i;
256     
257         for (i = 0; i < 16;  i++)  ctx->T[i] = ctx->T[256+i];
258
259         for (i = 16; i < 1024; i++) 
260                 ctx->T[i] = f2(ctx->T[i-2]) + ctx->T[i-7] + f1(ctx->T[i-15]) +
261                                                        ctx->T[i-16]+256+i;
262     
263     /* initialize counter1024, X and Y */
264         ctx->counter1024 = 0;
265         for (i = 0; i < 16; i++) ctx->X[i] = ctx->T[512-16+i];
266     for (i = 0; i < 16; i++) ctx->Y[i] = ctx->T[512+512-16+i];
267     
268     /* run the cipher 1024 steps before generating the output */
269         for (i = 0; i < 64; i++)  setup_update(ctx);  
270 }
271
272
273 static INLINE int DoKey(HC128* ctx, const byte* key, const byte* iv)
274
275   word32 i;  
276
277   /* Key size in bits 128 */ 
278   for (i = 0; i < (128 >> 5); i++)
279       ctx->key[i] = LITTLE32(((word32*)key)[i]);
280  
281   for ( ; i < 8 ; i++) ctx->key[i] = ctx->key[i-4];
282
283   Hc128_SetIV(ctx, iv);
284
285   return 0;
286 }
287
288
289 /* Key setup */
290 int Hc128_SetKey(HC128* ctx, const byte* key, const byte* iv)
291 {
292 #ifdef XSTREAM_ALIGN
293     if ((word)key % 4) {
294         int alignKey[4];
295
296         /* iv gets aligned in SetIV */
297         CYASSL_MSG("Hc128SetKey unaligned key");
298
299         XMEMCPY(alignKey, key, sizeof(alignKey));
300
301         return DoKey(ctx, (const byte*)alignKey, iv);
302     }
303 #endif /* XSTREAM_ALIGN */
304
305     return DoKey(ctx, key, iv);
306 }
307
308
309
310 /* The following defines the encryption of data stream */
311 static INLINE int DoProcess(HC128* ctx, byte* output, const byte* input,
312                             word32 msglen)
313 {
314   word32 i, keystream[16];
315
316   for ( ; msglen >= 64; msglen -= 64, input += 64, output += 64)
317   {
318           generate_keystream(ctx, keystream);
319
320       /* unroll loop */
321           ((word32*)output)[0]  = ((word32*)input)[0]  ^ LITTLE32(keystream[0]);
322           ((word32*)output)[1]  = ((word32*)input)[1]  ^ LITTLE32(keystream[1]);
323           ((word32*)output)[2]  = ((word32*)input)[2]  ^ LITTLE32(keystream[2]);
324           ((word32*)output)[3]  = ((word32*)input)[3]  ^ LITTLE32(keystream[3]);
325           ((word32*)output)[4]  = ((word32*)input)[4]  ^ LITTLE32(keystream[4]);
326           ((word32*)output)[5]  = ((word32*)input)[5]  ^ LITTLE32(keystream[5]);
327           ((word32*)output)[6]  = ((word32*)input)[6]  ^ LITTLE32(keystream[6]);
328           ((word32*)output)[7]  = ((word32*)input)[7]  ^ LITTLE32(keystream[7]);
329           ((word32*)output)[8]  = ((word32*)input)[8]  ^ LITTLE32(keystream[8]);
330           ((word32*)output)[9]  = ((word32*)input)[9]  ^ LITTLE32(keystream[9]);
331           ((word32*)output)[10] = ((word32*)input)[10] ^ LITTLE32(keystream[10]);
332           ((word32*)output)[11] = ((word32*)input)[11] ^ LITTLE32(keystream[11]);
333           ((word32*)output)[12] = ((word32*)input)[12] ^ LITTLE32(keystream[12]);
334           ((word32*)output)[13] = ((word32*)input)[13] ^ LITTLE32(keystream[13]);
335           ((word32*)output)[14] = ((word32*)input)[14] ^ LITTLE32(keystream[14]);
336           ((word32*)output)[15] = ((word32*)input)[15] ^ LITTLE32(keystream[15]);
337   }
338
339   if (msglen > 0)
340   {
341       XMEMSET(keystream, 0, sizeof(keystream)); /* hush the static analysis */
342       generate_keystream(ctx, keystream);
343
344 #ifdef BIG_ENDIAN_ORDER
345       {
346           word32 wordsLeft = msglen / sizeof(word32);
347           if (msglen % sizeof(word32)) wordsLeft++;
348           
349           ByteReverseWords(keystream, keystream, wordsLeft * sizeof(word32));
350       }
351 #endif
352
353       for (i = 0; i < msglen; i++)
354               output[i] = input[i] ^ ((byte*)keystream)[i];
355   }
356
357   return 0;
358 }
359
360
361 /* Encrypt/decrypt a message of any size */
362 int Hc128_Process(HC128* ctx, byte* output, const byte* input, word32 msglen)
363 {
364 #ifdef XSTREAM_ALIGN
365     if ((word)input % 4 || (word)output % 4) {
366         #ifndef NO_CYASSL_ALLOC_ALIGN
367             byte* tmp;
368             CYASSL_MSG("Hc128Process unaligned");
369
370             tmp = (byte*)XMALLOC(msglen, NULL, DYNAMIC_TYPE_TMP_BUFFER);
371             if (tmp == NULL) return MEMORY_E;
372
373             XMEMCPY(tmp, input, msglen);
374             DoProcess(ctx, tmp, tmp, msglen);
375             XMEMCPY(output, tmp, msglen);
376
377             XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
378
379             return 0;
380         #else
381             return BAD_ALIGN_E;
382         #endif
383     }
384 #endif /* XSTREAM_ALIGN */
385
386     return DoProcess(ctx, output, input, msglen);
387 }
388
389
390 #else  /* HAVE_HC128 */
391
392
393 #ifdef _MSC_VER
394     /* 4206 warning for blank file */
395     #pragma warning(disable: 4206)
396 #endif
397
398
399 #endif /* HAVE_HC128 */