hc128.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /* hc128.c
  2. *
  3. * Copyright (C) 2006-2011 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. #ifndef NO_HC128
  22. #include "hc128.h"
  23. #include "misc.c"
  24. #ifdef BIG_ENDIAN_ORDER
  25. #define LITTLE32(x) ByteReverseWord32(x)
  26. #else
  27. #define LITTLE32(x) (x)
  28. #endif
  29. /*h1 function*/
  30. #define h1(ctx, x, y) { \
  31. byte a,c; \
  32. a = (byte) (x); \
  33. c = (byte) ((x) >> 16); \
  34. y = (ctx->T[512+a])+(ctx->T[512+256+c]); \
  35. }
  36. /*h2 function*/
  37. #define h2(ctx, x, y) { \
  38. byte a,c; \
  39. a = (byte) (x); \
  40. c = (byte) ((x) >> 16); \
  41. y = (ctx->T[a])+(ctx->T[256+c]); \
  42. }
  43. /*one step of HC-128, update P and generate 32 bits keystream*/
  44. #define step_P(ctx,u,v,a,b,c,d,n){ \
  45. word32 tem0,tem1,tem2,tem3; \
  46. h1((ctx),(ctx->X[(d)]),tem3); \
  47. tem0 = rotrFixed((ctx->T[(v)]),23); \
  48. tem1 = rotrFixed((ctx->X[(c)]),10); \
  49. tem2 = rotrFixed((ctx->X[(b)]),8); \
  50. (ctx->T[(u)]) += tem2+(tem0 ^ tem1); \
  51. (ctx->X[(a)]) = (ctx->T[(u)]); \
  52. (n) = tem3 ^ (ctx->T[(u)]) ; \
  53. }
  54. /*one step of HC-128, update Q and generate 32 bits keystream*/
  55. #define step_Q(ctx,u,v,a,b,c,d,n){ \
  56. word32 tem0,tem1,tem2,tem3; \
  57. h2((ctx),(ctx->Y[(d)]),tem3); \
  58. tem0 = rotrFixed((ctx->T[(v)]),(32-23)); \
  59. tem1 = rotrFixed((ctx->Y[(c)]),(32-10)); \
  60. tem2 = rotrFixed((ctx->Y[(b)]),(32-8)); \
  61. (ctx->T[(u)]) += tem2 + (tem0 ^ tem1); \
  62. (ctx->Y[(a)]) = (ctx->T[(u)]); \
  63. (n) = tem3 ^ (ctx->T[(u)]) ; \
  64. }
  65. /*16 steps of HC-128, generate 512 bits keystream*/
  66. static void generate_keystream(HC128* ctx, word32* keystream)
  67. {
  68. word32 cc,dd;
  69. cc = ctx->counter1024 & 0x1ff;
  70. dd = (cc+16)&0x1ff;
  71. if (ctx->counter1024 < 512)
  72. {
  73. ctx->counter1024 = (ctx->counter1024 + 16) & 0x3ff;
  74. step_P(ctx, cc+0, cc+1, 0, 6, 13,4, keystream[0]);
  75. step_P(ctx, cc+1, cc+2, 1, 7, 14,5, keystream[1]);
  76. step_P(ctx, cc+2, cc+3, 2, 8, 15,6, keystream[2]);
  77. step_P(ctx, cc+3, cc+4, 3, 9, 0, 7, keystream[3]);
  78. step_P(ctx, cc+4, cc+5, 4, 10,1, 8, keystream[4]);
  79. step_P(ctx, cc+5, cc+6, 5, 11,2, 9, keystream[5]);
  80. step_P(ctx, cc+6, cc+7, 6, 12,3, 10,keystream[6]);
  81. step_P(ctx, cc+7, cc+8, 7, 13,4, 11,keystream[7]);
  82. step_P(ctx, cc+8, cc+9, 8, 14,5, 12,keystream[8]);
  83. step_P(ctx, cc+9, cc+10,9, 15,6, 13,keystream[9]);
  84. step_P(ctx, cc+10,cc+11,10,0, 7, 14,keystream[10]);
  85. step_P(ctx, cc+11,cc+12,11,1, 8, 15,keystream[11]);
  86. step_P(ctx, cc+12,cc+13,12,2, 9, 0, keystream[12]);
  87. step_P(ctx, cc+13,cc+14,13,3, 10,1, keystream[13]);
  88. step_P(ctx, cc+14,cc+15,14,4, 11,2, keystream[14]);
  89. step_P(ctx, cc+15,dd+0, 15,5, 12,3, keystream[15]);
  90. }
  91. else
  92. {
  93. ctx->counter1024 = (ctx->counter1024 + 16) & 0x3ff;
  94. step_Q(ctx, 512+cc+0, 512+cc+1, 0, 6, 13,4, keystream[0]);
  95. step_Q(ctx, 512+cc+1, 512+cc+2, 1, 7, 14,5, keystream[1]);
  96. step_Q(ctx, 512+cc+2, 512+cc+3, 2, 8, 15,6, keystream[2]);
  97. step_Q(ctx, 512+cc+3, 512+cc+4, 3, 9, 0, 7, keystream[3]);
  98. step_Q(ctx, 512+cc+4, 512+cc+5, 4, 10,1, 8, keystream[4]);
  99. step_Q(ctx, 512+cc+5, 512+cc+6, 5, 11,2, 9, keystream[5]);
  100. step_Q(ctx, 512+cc+6, 512+cc+7, 6, 12,3, 10,keystream[6]);
  101. step_Q(ctx, 512+cc+7, 512+cc+8, 7, 13,4, 11,keystream[7]);
  102. step_Q(ctx, 512+cc+8, 512+cc+9, 8, 14,5, 12,keystream[8]);
  103. step_Q(ctx, 512+cc+9, 512+cc+10,9, 15,6, 13,keystream[9]);
  104. step_Q(ctx, 512+cc+10,512+cc+11,10,0, 7, 14,keystream[10]);
  105. step_Q(ctx, 512+cc+11,512+cc+12,11,1, 8, 15,keystream[11]);
  106. step_Q(ctx, 512+cc+12,512+cc+13,12,2, 9, 0, keystream[12]);
  107. step_Q(ctx, 512+cc+13,512+cc+14,13,3, 10,1, keystream[13]);
  108. step_Q(ctx, 512+cc+14,512+cc+15,14,4, 11,2, keystream[14]);
  109. step_Q(ctx, 512+cc+15,512+dd+0, 15,5, 12,3, keystream[15]);
  110. }
  111. }
  112. /* The following defines the initialization functions */
  113. #define f1(x) (rotrFixed((x),7) ^ rotrFixed((x),18) ^ ((x) >> 3))
  114. #define f2(x) (rotrFixed((x),17) ^ rotrFixed((x),19) ^ ((x) >> 10))
  115. /*update table P*/
  116. #define update_P(ctx,u,v,a,b,c,d){ \
  117. word32 tem0,tem1,tem2,tem3; \
  118. tem0 = rotrFixed((ctx->T[(v)]),23); \
  119. tem1 = rotrFixed((ctx->X[(c)]),10); \
  120. tem2 = rotrFixed((ctx->X[(b)]),8); \
  121. h1((ctx),(ctx->X[(d)]),tem3); \
  122. (ctx->T[(u)]) = ((ctx->T[(u)]) + tem2+(tem0^tem1)) ^ tem3; \
  123. (ctx->X[(a)]) = (ctx->T[(u)]); \
  124. }
  125. /*update table Q*/
  126. #define update_Q(ctx,u,v,a,b,c,d){ \
  127. word32 tem0,tem1,tem2,tem3; \
  128. tem0 = rotrFixed((ctx->T[(v)]),(32-23)); \
  129. tem1 = rotrFixed((ctx->Y[(c)]),(32-10)); \
  130. tem2 = rotrFixed((ctx->Y[(b)]),(32-8)); \
  131. h2((ctx),(ctx->Y[(d)]),tem3); \
  132. (ctx->T[(u)]) = ((ctx->T[(u)]) + tem2+(tem0^tem1)) ^ tem3; \
  133. (ctx->Y[(a)]) = (ctx->T[(u)]); \
  134. }
  135. /*16 steps of HC-128, without generating keystream, */
  136. /*but use the outputs to update P and Q*/
  137. static void setup_update(HC128* ctx) /*each time 16 steps*/
  138. {
  139. word32 cc,dd;
  140. cc = ctx->counter1024 & 0x1ff;
  141. dd = (cc+16)&0x1ff;
  142. if (ctx->counter1024 < 512)
  143. {
  144. ctx->counter1024 = (ctx->counter1024 + 16) & 0x3ff;
  145. update_P(ctx, cc+0, cc+1, 0, 6, 13, 4);
  146. update_P(ctx, cc+1, cc+2, 1, 7, 14, 5);
  147. update_P(ctx, cc+2, cc+3, 2, 8, 15, 6);
  148. update_P(ctx, cc+3, cc+4, 3, 9, 0, 7);
  149. update_P(ctx, cc+4, cc+5, 4, 10,1, 8);
  150. update_P(ctx, cc+5, cc+6, 5, 11,2, 9);
  151. update_P(ctx, cc+6, cc+7, 6, 12,3, 10);
  152. update_P(ctx, cc+7, cc+8, 7, 13,4, 11);
  153. update_P(ctx, cc+8, cc+9, 8, 14,5, 12);
  154. update_P(ctx, cc+9, cc+10,9, 15,6, 13);
  155. update_P(ctx, cc+10,cc+11,10,0, 7, 14);
  156. update_P(ctx, cc+11,cc+12,11,1, 8, 15);
  157. update_P(ctx, cc+12,cc+13,12,2, 9, 0);
  158. update_P(ctx, cc+13,cc+14,13,3, 10, 1);
  159. update_P(ctx, cc+14,cc+15,14,4, 11, 2);
  160. update_P(ctx, cc+15,dd+0, 15,5, 12, 3);
  161. }
  162. else
  163. {
  164. ctx->counter1024 = (ctx->counter1024 + 16) & 0x3ff;
  165. update_Q(ctx, 512+cc+0, 512+cc+1, 0, 6, 13, 4);
  166. update_Q(ctx, 512+cc+1, 512+cc+2, 1, 7, 14, 5);
  167. update_Q(ctx, 512+cc+2, 512+cc+3, 2, 8, 15, 6);
  168. update_Q(ctx, 512+cc+3, 512+cc+4, 3, 9, 0, 7);
  169. update_Q(ctx, 512+cc+4, 512+cc+5, 4, 10,1, 8);
  170. update_Q(ctx, 512+cc+5, 512+cc+6, 5, 11,2, 9);
  171. update_Q(ctx, 512+cc+6, 512+cc+7, 6, 12,3, 10);
  172. update_Q(ctx, 512+cc+7, 512+cc+8, 7, 13,4, 11);
  173. update_Q(ctx, 512+cc+8, 512+cc+9, 8, 14,5, 12);
  174. update_Q(ctx, 512+cc+9, 512+cc+10,9, 15,6, 13);
  175. update_Q(ctx, 512+cc+10,512+cc+11,10,0, 7, 14);
  176. update_Q(ctx, 512+cc+11,512+cc+12,11,1, 8, 15);
  177. update_Q(ctx, 512+cc+12,512+cc+13,12,2, 9, 0);
  178. update_Q(ctx, 512+cc+13,512+cc+14,13,3, 10, 1);
  179. update_Q(ctx, 512+cc+14,512+cc+15,14,4, 11, 2);
  180. update_Q(ctx, 512+cc+15,512+dd+0, 15,5, 12, 3);
  181. }
  182. }
  183. /* for the 128-bit key: key[0]...key[15]
  184. * key[0] is the least significant byte of ctx->key[0] (K_0);
  185. * key[3] is the most significant byte of ctx->key[0] (K_0);
  186. * ...
  187. * key[12] is the least significant byte of ctx->key[3] (K_3)
  188. * key[15] is the most significant byte of ctx->key[3] (K_3)
  189. *
  190. * for the 128-bit iv: iv[0]...iv[15]
  191. * iv[0] is the least significant byte of ctx->iv[0] (IV_0);
  192. * iv[3] is the most significant byte of ctx->iv[0] (IV_0);
  193. * ...
  194. * iv[12] is the least significant byte of ctx->iv[3] (IV_3)
  195. * iv[15] is the most significant byte of ctx->iv[3] (IV_3)
  196. */
  197. static void Hc128_SetIV(HC128* ctx, const byte* iv)
  198. {
  199. word32 i;
  200. for (i = 0; i < (128 >> 5); i++)
  201. ctx->iv[i] = LITTLE32(((word32*)iv)[i]);
  202. for (; i < 8; i++) ctx->iv[i] = ctx->iv[i-4];
  203. /* expand the key and IV into the table T */
  204. /* (expand the key and IV into the table P and Q) */
  205. for (i = 0; i < 8; i++) ctx->T[i] = ctx->key[i];
  206. for (i = 8; i < 16; i++) ctx->T[i] = ctx->iv[i-8];
  207. for (i = 16; i < (256+16); i++)
  208. ctx->T[i] = f2(ctx->T[i-2]) + ctx->T[i-7] + f1(ctx->T[i-15]) +
  209. ctx->T[i-16]+i;
  210. for (i = 0; i < 16; i++) ctx->T[i] = ctx->T[256+i];
  211. for (i = 16; i < 1024; i++)
  212. ctx->T[i] = f2(ctx->T[i-2]) + ctx->T[i-7] + f1(ctx->T[i-15]) +
  213. ctx->T[i-16]+256+i;
  214. /* initialize counter1024, X and Y */
  215. ctx->counter1024 = 0;
  216. for (i = 0; i < 16; i++) ctx->X[i] = ctx->T[512-16+i];
  217. for (i = 0; i < 16; i++) ctx->Y[i] = ctx->T[512+512-16+i];
  218. /* run the cipher 1024 steps before generating the output */
  219. for (i = 0; i < 64; i++) setup_update(ctx);
  220. }
  221. void Hc128_SetKey(HC128* ctx, const byte* key, const byte* iv)
  222. {
  223. word32 i;
  224. /* Key size in bits 128 */
  225. for (i = 0; i < (128 >> 5); i++)
  226. ctx->key[i] = LITTLE32(((word32*)key)[i]);
  227. for ( ; i < 8 ; i++) ctx->key[i] = ctx->key[i-4];
  228. Hc128_SetIV(ctx, iv);
  229. }
  230. /* The following defines the encryption of data stream */
  231. void Hc128_Process(HC128* ctx, byte* output, const byte* input, word32 msglen)
  232. {
  233. word32 i, keystream[16];
  234. for ( ; msglen >= 64; msglen -= 64, input += 64, output += 64)
  235. {
  236. generate_keystream(ctx, keystream);
  237. /* unroll loop */
  238. ((word32*)output)[0] = ((word32*)input)[0] ^ LITTLE32(keystream[0]);
  239. ((word32*)output)[1] = ((word32*)input)[1] ^ LITTLE32(keystream[1]);
  240. ((word32*)output)[2] = ((word32*)input)[2] ^ LITTLE32(keystream[2]);
  241. ((word32*)output)[3] = ((word32*)input)[3] ^ LITTLE32(keystream[3]);
  242. ((word32*)output)[4] = ((word32*)input)[4] ^ LITTLE32(keystream[4]);
  243. ((word32*)output)[5] = ((word32*)input)[5] ^ LITTLE32(keystream[5]);
  244. ((word32*)output)[6] = ((word32*)input)[6] ^ LITTLE32(keystream[6]);
  245. ((word32*)output)[7] = ((word32*)input)[7] ^ LITTLE32(keystream[7]);
  246. ((word32*)output)[8] = ((word32*)input)[8] ^ LITTLE32(keystream[8]);
  247. ((word32*)output)[9] = ((word32*)input)[9] ^ LITTLE32(keystream[9]);
  248. ((word32*)output)[10] = ((word32*)input)[10] ^ LITTLE32(keystream[10]);
  249. ((word32*)output)[11] = ((word32*)input)[11] ^ LITTLE32(keystream[11]);
  250. ((word32*)output)[12] = ((word32*)input)[12] ^ LITTLE32(keystream[12]);
  251. ((word32*)output)[13] = ((word32*)input)[13] ^ LITTLE32(keystream[13]);
  252. ((word32*)output)[14] = ((word32*)input)[14] ^ LITTLE32(keystream[14]);
  253. ((word32*)output)[15] = ((word32*)input)[15] ^ LITTLE32(keystream[15]);
  254. }
  255. if (msglen > 0)
  256. {
  257. generate_keystream(ctx, keystream);
  258. #ifdef BIG_ENDIAN_ORDER
  259. {
  260. word32 wordsLeft = msglen / sizeof(word32);
  261. if (msglen % sizeof(word32)) wordsLeft++;
  262. ByteReverseWords(keystream, keystream, wordsLeft * sizeof(word32));
  263. }
  264. #endif
  265. for (i = 0; i < msglen; i++)
  266. output[i] = input[i] ^ ((byte*)keystream)[i];
  267. }
  268. }
  269. #endif /* NO_HC128 */