hc128.c 14 KB

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