]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/CyaSSL/ctaocrypt/src/hc128.c
20081fc5442b8baaefb3db883aec4a634a01cf99
[freertos] / FreeRTOS-Plus / CyaSSL / ctaocrypt / src / hc128.c
1 /* hc128.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 #ifdef HAVE_HC128
27
28 #include <cyassl/ctaocrypt/hc128.h>
29 #ifdef NO_INLINE
30     #include <cyassl/ctaocrypt/hc128.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
43 /*h1 function*/
44 #define h1(ctx, x, y) {                         \
45      byte a,c;                                  \
46      a = (byte) (x);                            \
47      c = (byte) ((x) >> 16);                    \
48      y = (ctx->T[512+a])+(ctx->T[512+256+c]);   \
49 }
50
51 /*h2 function*/
52 #define h2(ctx, x, y) {                         \
53      byte a,c;                                  \
54      a = (byte) (x);                            \
55      c = (byte) ((x) >> 16);                    \
56      y = (ctx->T[a])+(ctx->T[256+c]);           \
57 }
58
59 /*one step of HC-128, update P and generate 32 bits keystream*/
60 #define step_P(ctx,u,v,a,b,c,d,n){              \
61      word32 tem0,tem1,tem2,tem3;                \
62      h1((ctx),(ctx->X[(d)]),tem3);              \
63      tem0 = rotrFixed((ctx->T[(v)]),23);        \
64      tem1 = rotrFixed((ctx->X[(c)]),10);        \
65      tem2 = rotrFixed((ctx->X[(b)]),8);         \
66      (ctx->T[(u)]) += tem2+(tem0 ^ tem1);       \
67      (ctx->X[(a)]) = (ctx->T[(u)]);             \
68      (n) = tem3 ^ (ctx->T[(u)]) ;               \
69 }       
70
71 /*one step of HC-128, update Q and generate 32 bits keystream*/
72 #define step_Q(ctx,u,v,a,b,c,d,n){              \
73      word32 tem0,tem1,tem2,tem3;                \
74      h2((ctx),(ctx->Y[(d)]),tem3);              \
75      tem0 = rotrFixed((ctx->T[(v)]),(32-23));   \
76      tem1 = rotrFixed((ctx->Y[(c)]),(32-10));   \
77      tem2 = rotrFixed((ctx->Y[(b)]),(32-8));    \
78      (ctx->T[(u)]) += tem2 + (tem0 ^ tem1);     \
79      (ctx->Y[(a)]) = (ctx->T[(u)]);             \
80      (n) = tem3 ^ (ctx->T[(u)]) ;               \
81 }   
82
83 /*16 steps of HC-128, generate 512 bits keystream*/
84 static void generate_keystream(HC128* ctx, word32* keystream)  
85 {
86    word32 cc,dd;
87    cc = ctx->counter1024 & 0x1ff;
88    dd = (cc+16)&0x1ff;
89
90    if (ctx->counter1024 < 512)  
91    {            
92       ctx->counter1024 = (ctx->counter1024 + 16) & 0x3ff;
93       step_P(ctx, cc+0, cc+1, 0, 6, 13,4, keystream[0]);
94       step_P(ctx, cc+1, cc+2, 1, 7, 14,5, keystream[1]);
95       step_P(ctx, cc+2, cc+3, 2, 8, 15,6, keystream[2]);
96       step_P(ctx, cc+3, cc+4, 3, 9, 0, 7, keystream[3]);
97       step_P(ctx, cc+4, cc+5, 4, 10,1, 8, keystream[4]);
98       step_P(ctx, cc+5, cc+6, 5, 11,2, 9, keystream[5]);
99       step_P(ctx, cc+6, cc+7, 6, 12,3, 10,keystream[6]);
100       step_P(ctx, cc+7, cc+8, 7, 13,4, 11,keystream[7]);
101       step_P(ctx, cc+8, cc+9, 8, 14,5, 12,keystream[8]);
102       step_P(ctx, cc+9, cc+10,9, 15,6, 13,keystream[9]);
103       step_P(ctx, cc+10,cc+11,10,0, 7, 14,keystream[10]);
104       step_P(ctx, cc+11,cc+12,11,1, 8, 15,keystream[11]);
105       step_P(ctx, cc+12,cc+13,12,2, 9, 0, keystream[12]);
106       step_P(ctx, cc+13,cc+14,13,3, 10,1, keystream[13]);
107       step_P(ctx, cc+14,cc+15,14,4, 11,2, keystream[14]);
108       step_P(ctx, cc+15,dd+0, 15,5, 12,3, keystream[15]);
109    }
110    else                             
111    {
112           ctx->counter1024 = (ctx->counter1024 + 16) & 0x3ff;
113       step_Q(ctx, 512+cc+0, 512+cc+1, 0, 6, 13,4, keystream[0]);
114       step_Q(ctx, 512+cc+1, 512+cc+2, 1, 7, 14,5, keystream[1]);
115       step_Q(ctx, 512+cc+2, 512+cc+3, 2, 8, 15,6, keystream[2]);
116       step_Q(ctx, 512+cc+3, 512+cc+4, 3, 9, 0, 7, keystream[3]);
117       step_Q(ctx, 512+cc+4, 512+cc+5, 4, 10,1, 8, keystream[4]);
118       step_Q(ctx, 512+cc+5, 512+cc+6, 5, 11,2, 9, keystream[5]);
119       step_Q(ctx, 512+cc+6, 512+cc+7, 6, 12,3, 10,keystream[6]);
120       step_Q(ctx, 512+cc+7, 512+cc+8, 7, 13,4, 11,keystream[7]);
121       step_Q(ctx, 512+cc+8, 512+cc+9, 8, 14,5, 12,keystream[8]);
122       step_Q(ctx, 512+cc+9, 512+cc+10,9, 15,6, 13,keystream[9]);
123       step_Q(ctx, 512+cc+10,512+cc+11,10,0, 7, 14,keystream[10]);
124       step_Q(ctx, 512+cc+11,512+cc+12,11,1, 8, 15,keystream[11]);
125       step_Q(ctx, 512+cc+12,512+cc+13,12,2, 9, 0, keystream[12]);
126       step_Q(ctx, 512+cc+13,512+cc+14,13,3, 10,1, keystream[13]);
127       step_Q(ctx, 512+cc+14,512+cc+15,14,4, 11,2, keystream[14]);
128       step_Q(ctx, 512+cc+15,512+dd+0, 15,5, 12,3, keystream[15]);
129    }
130 }
131
132
133 /* The following defines the initialization functions */
134 #define f1(x)  (rotrFixed((x),7)  ^ rotrFixed((x),18) ^ ((x) >> 3))
135 #define f2(x)  (rotrFixed((x),17) ^ rotrFixed((x),19) ^ ((x) >> 10))
136
137 /*update table P*/
138 #define update_P(ctx,u,v,a,b,c,d){                  \
139      word32 tem0,tem1,tem2,tem3;                    \
140      tem0 = rotrFixed((ctx->T[(v)]),23);            \
141      tem1 = rotrFixed((ctx->X[(c)]),10);            \
142      tem2 = rotrFixed((ctx->X[(b)]),8);             \
143      h1((ctx),(ctx->X[(d)]),tem3);                  \
144      (ctx->T[(u)]) = ((ctx->T[(u)]) + tem2+(tem0^tem1)) ^ tem3;     \
145      (ctx->X[(a)]) = (ctx->T[(u)]);                 \
146 }  
147
148 /*update table Q*/
149 #define update_Q(ctx,u,v,a,b,c,d){                  \
150      word32 tem0,tem1,tem2,tem3;                    \
151      tem0 = rotrFixed((ctx->T[(v)]),(32-23));       \
152      tem1 = rotrFixed((ctx->Y[(c)]),(32-10));       \
153      tem2 = rotrFixed((ctx->Y[(b)]),(32-8));        \
154      h2((ctx),(ctx->Y[(d)]),tem3);                  \
155      (ctx->T[(u)]) = ((ctx->T[(u)]) + tem2+(tem0^tem1)) ^ tem3;     \
156      (ctx->Y[(a)]) = (ctx->T[(u)]);                 \
157 }     
158
159 /*16 steps of HC-128, without generating keystream, */
160 /*but use the outputs to update P and Q*/
161 static void setup_update(HC128* ctx)  /*each time 16 steps*/
162 {
163    word32 cc,dd;
164    cc = ctx->counter1024 & 0x1ff;
165    dd = (cc+16)&0x1ff;
166
167    if (ctx->counter1024 < 512)  
168    {            
169       ctx->counter1024 = (ctx->counter1024 + 16) & 0x3ff;
170       update_P(ctx, cc+0, cc+1, 0, 6, 13, 4);
171       update_P(ctx, cc+1, cc+2, 1, 7, 14, 5);
172       update_P(ctx, cc+2, cc+3, 2, 8, 15, 6);
173       update_P(ctx, cc+3, cc+4, 3, 9, 0,  7);
174       update_P(ctx, cc+4, cc+5, 4, 10,1,  8);
175       update_P(ctx, cc+5, cc+6, 5, 11,2,  9);
176       update_P(ctx, cc+6, cc+7, 6, 12,3,  10);
177       update_P(ctx, cc+7, cc+8, 7, 13,4,  11);
178       update_P(ctx, cc+8, cc+9, 8, 14,5,  12);
179       update_P(ctx, cc+9, cc+10,9, 15,6,  13);
180       update_P(ctx, cc+10,cc+11,10,0, 7,  14);
181       update_P(ctx, cc+11,cc+12,11,1, 8,  15);
182       update_P(ctx, cc+12,cc+13,12,2, 9,  0);
183       update_P(ctx, cc+13,cc+14,13,3, 10, 1);
184       update_P(ctx, cc+14,cc+15,14,4, 11, 2);
185       update_P(ctx, cc+15,dd+0, 15,5, 12, 3);   
186    }
187    else                             
188    {
189       ctx->counter1024 = (ctx->counter1024 + 16) & 0x3ff;
190       update_Q(ctx, 512+cc+0, 512+cc+1, 0, 6, 13, 4);
191       update_Q(ctx, 512+cc+1, 512+cc+2, 1, 7, 14, 5);
192       update_Q(ctx, 512+cc+2, 512+cc+3, 2, 8, 15, 6);
193       update_Q(ctx, 512+cc+3, 512+cc+4, 3, 9, 0,  7);
194       update_Q(ctx, 512+cc+4, 512+cc+5, 4, 10,1,  8);
195       update_Q(ctx, 512+cc+5, 512+cc+6, 5, 11,2,  9);
196       update_Q(ctx, 512+cc+6, 512+cc+7, 6, 12,3,  10);
197       update_Q(ctx, 512+cc+7, 512+cc+8, 7, 13,4,  11);
198       update_Q(ctx, 512+cc+8, 512+cc+9, 8, 14,5,  12);
199       update_Q(ctx, 512+cc+9, 512+cc+10,9, 15,6,  13);
200       update_Q(ctx, 512+cc+10,512+cc+11,10,0, 7,  14);
201       update_Q(ctx, 512+cc+11,512+cc+12,11,1, 8,  15);
202       update_Q(ctx, 512+cc+12,512+cc+13,12,2, 9,  0);
203       update_Q(ctx, 512+cc+13,512+cc+14,13,3, 10, 1);
204       update_Q(ctx, 512+cc+14,512+cc+15,14,4, 11, 2);
205       update_Q(ctx, 512+cc+15,512+dd+0, 15,5, 12, 3); 
206    }       
207 }
208
209
210 /* for the 128-bit key:  key[0]...key[15]
211 *  key[0] is the least significant byte of ctx->key[0] (K_0);
212 *  key[3] is the most significant byte of ctx->key[0]  (K_0);
213 *  ...
214 *  key[12] is the least significant byte of ctx->key[3] (K_3)
215 *  key[15] is the most significant byte of ctx->key[3]  (K_3)
216 *
217 *  for the 128-bit iv:  iv[0]...iv[15]
218 *  iv[0] is the least significant byte of ctx->iv[0] (IV_0);
219 *  iv[3] is the most significant byte of ctx->iv[0]  (IV_0);
220 *  ...
221 *  iv[12] is the least significant byte of ctx->iv[3] (IV_3)
222 *  iv[15] is the most significant byte of ctx->iv[3]  (IV_3)
223 */
224
225
226
227 static void Hc128_SetIV(HC128* ctx, const byte* iv)
228
229     word32 i;
230         
231         for (i = 0; i < (128 >> 5); i++)
232         ctx->iv[i] = LITTLE32(((word32*)iv)[i]);
233         
234     for (; i < 8; i++) ctx->iv[i] = ctx->iv[i-4];
235   
236     /* expand the key and IV into the table T */ 
237     /* (expand the key and IV into the table P and Q) */ 
238         
239         for (i = 0; i < 8;  i++)   ctx->T[i] = ctx->key[i];
240         for (i = 8; i < 16; i++)   ctx->T[i] = ctx->iv[i-8];
241
242     for (i = 16; i < (256+16); i++) 
243                 ctx->T[i] = f2(ctx->T[i-2]) + ctx->T[i-7] + f1(ctx->T[i-15]) +
244                                                        ctx->T[i-16]+i;
245     
246         for (i = 0; i < 16;  i++)  ctx->T[i] = ctx->T[256+i];
247
248         for (i = 16; i < 1024; i++) 
249                 ctx->T[i] = f2(ctx->T[i-2]) + ctx->T[i-7] + f1(ctx->T[i-15]) +
250                                                        ctx->T[i-16]+256+i;
251     
252     /* initialize counter1024, X and Y */
253         ctx->counter1024 = 0;
254         for (i = 0; i < 16; i++) ctx->X[i] = ctx->T[512-16+i];
255     for (i = 0; i < 16; i++) ctx->Y[i] = ctx->T[512+512-16+i];
256     
257     /* run the cipher 1024 steps before generating the output */
258         for (i = 0; i < 64; i++)  setup_update(ctx);  
259 }
260
261
262 void Hc128_SetKey(HC128* ctx, const byte* key, const byte* iv)
263
264   word32 i;  
265
266   /* Key size in bits 128 */ 
267   for (i = 0; i < (128 >> 5); i++)
268       ctx->key[i] = LITTLE32(((word32*)key)[i]);
269  
270   for ( ; i < 8 ; i++) ctx->key[i] = ctx->key[i-4];
271
272   Hc128_SetIV(ctx, iv);
273 }
274
275
276 /* The following defines the encryption of data stream */
277 void Hc128_Process(HC128* ctx, byte* output, const byte* input, word32 msglen)
278 {
279   word32 i, keystream[16];
280
281   for ( ; msglen >= 64; msglen -= 64, input += 64, output += 64)
282   {
283           generate_keystream(ctx, keystream);
284
285       /* unroll loop */
286           ((word32*)output)[0]  = ((word32*)input)[0]  ^ LITTLE32(keystream[0]);
287           ((word32*)output)[1]  = ((word32*)input)[1]  ^ LITTLE32(keystream[1]);
288           ((word32*)output)[2]  = ((word32*)input)[2]  ^ LITTLE32(keystream[2]);
289           ((word32*)output)[3]  = ((word32*)input)[3]  ^ LITTLE32(keystream[3]);
290           ((word32*)output)[4]  = ((word32*)input)[4]  ^ LITTLE32(keystream[4]);
291           ((word32*)output)[5]  = ((word32*)input)[5]  ^ LITTLE32(keystream[5]);
292           ((word32*)output)[6]  = ((word32*)input)[6]  ^ LITTLE32(keystream[6]);
293           ((word32*)output)[7]  = ((word32*)input)[7]  ^ LITTLE32(keystream[7]);
294           ((word32*)output)[8]  = ((word32*)input)[8]  ^ LITTLE32(keystream[8]);
295           ((word32*)output)[9]  = ((word32*)input)[9]  ^ LITTLE32(keystream[9]);
296           ((word32*)output)[10] = ((word32*)input)[10] ^ LITTLE32(keystream[10]);
297           ((word32*)output)[11] = ((word32*)input)[11] ^ LITTLE32(keystream[11]);
298           ((word32*)output)[12] = ((word32*)input)[12] ^ LITTLE32(keystream[12]);
299           ((word32*)output)[13] = ((word32*)input)[13] ^ LITTLE32(keystream[13]);
300           ((word32*)output)[14] = ((word32*)input)[14] ^ LITTLE32(keystream[14]);
301           ((word32*)output)[15] = ((word32*)input)[15] ^ LITTLE32(keystream[15]);
302   }
303
304   if (msglen > 0)
305   {
306       generate_keystream(ctx, keystream);
307
308 #ifdef BIG_ENDIAN_ORDER
309       {
310           word32 wordsLeft = msglen / sizeof(word32);
311           if (msglen % sizeof(word32)) wordsLeft++;
312           
313           ByteReverseWords(keystream, keystream, wordsLeft * sizeof(word32));
314       }
315 #endif
316
317       for (i = 0; i < msglen; i++)
318               output[i] = input[i] ^ ((byte*)keystream)[i];
319   }
320
321 }
322
323
324 #else  /* HAVE_HC128 */
325
326
327 #ifdef _MSC_VER
328     /* 4206 warning for blank file */
329     #pragma warning(disable: 4206)
330 #endif
331
332
333 #endif /* HAVE_HC128 */