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