gosthash.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. // Free implementation of the GOST R 34.11-94 hash algorithm
  2. /*
  3. * gosthash.c
  4. * 21 Apr 1998 Markku-Juhani O. Saarinen <mjos@iki.fi>
  5. *
  6. * GOST R 34.11-94, Russian Standard Hash Function
  7. */
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include "gosthash.h"
  12. /* lookup tables : each of these has two rotated 4-bit S-Boxes */
  13. unsigned long gost_sbox_1[256];
  14. unsigned long gost_sbox_2[256];
  15. unsigned long gost_sbox_3[256];
  16. unsigned long gost_sbox_4[256];
  17. /* initialize the lookup tables */
  18. void gosthash_init()
  19. {
  20. int a, b, i;
  21. unsigned long ax, bx, cx, dx;
  22. /* 4-bit S-Boxes */
  23. unsigned long sbox[8][16] =
  24. {
  25. { 4, 10, 9, 2, 13, 8, 0, 14, 6, 11, 1, 12, 7, 15, 5, 3 },
  26. { 14, 11, 4, 12, 6, 13, 15, 10, 2, 3, 8, 1, 0, 7, 5, 9 },
  27. { 5, 8, 1, 13, 10, 3, 4, 2, 14, 15, 12, 7, 6, 0, 9, 11 },
  28. { 7, 13, 10, 1, 0, 8, 9, 15, 14, 4, 6, 12, 11, 2, 5, 3 },
  29. { 6, 12, 7, 1, 5, 15, 13, 8, 4, 10, 9, 14, 0, 3, 11, 2 },
  30. { 4, 11, 10, 0, 7, 2, 1, 13, 3, 6, 8, 5, 9, 12, 15, 14 },
  31. { 13, 11, 4, 1, 3, 15, 5, 9, 0, 10, 14, 7, 6, 8, 2, 12 },
  32. { 1, 15, 13, 0, 5, 7, 10, 4, 9, 2, 3, 14, 6, 11, 8, 12 }
  33. };
  34. /* s-box precomputation */
  35. i = 0;
  36. for (a = 0; a < 16; a++)
  37. {
  38. ax = sbox[1][a] << 15;
  39. bx = sbox[3][a] << 23;
  40. cx = sbox[5][a];
  41. cx = (cx >> 1) | (cx << 31);
  42. dx = sbox[7][a] << 7;
  43. for (b = 0; b < 16; b++)
  44. {
  45. gost_sbox_1[i] = ax | (sbox[0][b] << 11);
  46. gost_sbox_2[i] = bx | (sbox[2][b] << 19);
  47. gost_sbox_3[i] = cx | (sbox[4][b] << 27);
  48. gost_sbox_4[i++] = dx | (sbox[6][b] << 3);
  49. }
  50. }
  51. }
  52. /*
  53. * A macro that performs a full encryption round of GOST 28147-89.
  54. * Temporary variable t assumed and variables r and l for left and right
  55. * blocks
  56. */
  57. #define GOST_ENCRYPT_ROUND(k1, k2) \
  58. t = (k1) + r; \
  59. l ^= gost_sbox_1[t & 0xff] ^ gost_sbox_2[(t >> 8) & 0xff] ^ \
  60. gost_sbox_3[(t >> 16) & 0xff] ^ gost_sbox_4[t >> 24]; \
  61. t = (k2) + l; \
  62. r ^= gost_sbox_1[t & 0xff] ^ gost_sbox_2[(t >> 8) & 0xff] ^ \
  63. gost_sbox_3[(t >> 16) & 0xff] ^ gost_sbox_4[t >> 24]; \
  64. /* encrypt a block with the given key */
  65. #define GOST_ENCRYPT(key) \
  66. GOST_ENCRYPT_ROUND(key[0], key[1]) \
  67. GOST_ENCRYPT_ROUND(key[2], key[3]) \
  68. GOST_ENCRYPT_ROUND(key[4], key[5]) \
  69. GOST_ENCRYPT_ROUND(key[6], key[7]) \
  70. GOST_ENCRYPT_ROUND(key[0], key[1]) \
  71. GOST_ENCRYPT_ROUND(key[2], key[3]) \
  72. GOST_ENCRYPT_ROUND(key[4], key[5]) \
  73. GOST_ENCRYPT_ROUND(key[6], key[7]) \
  74. GOST_ENCRYPT_ROUND(key[0], key[1]) \
  75. GOST_ENCRYPT_ROUND(key[2], key[3]) \
  76. GOST_ENCRYPT_ROUND(key[4], key[5]) \
  77. GOST_ENCRYPT_ROUND(key[6], key[7]) \
  78. GOST_ENCRYPT_ROUND(key[7], key[6]) \
  79. GOST_ENCRYPT_ROUND(key[5], key[4]) \
  80. GOST_ENCRYPT_ROUND(key[3], key[2]) \
  81. GOST_ENCRYPT_ROUND(key[1], key[0]) \
  82. t = r; \
  83. r = l; \
  84. l = t;
  85. /*
  86. * "chi" compression function. the result is stored over h
  87. */
  88. void gosthash_compress(unsigned long *h, unsigned long *m)
  89. {
  90. int i;
  91. unsigned long l, r, t, key[8], u[8], v[8], w[8], s[8];
  92. memcpy(u, h, sizeof(u));
  93. memcpy(v, m, sizeof(u));
  94. for (i = 0; i < 8; i += 2)
  95. {
  96. w[0] = u[0] ^ v[0]; /* w = u xor v */
  97. w[1] = u[1] ^ v[1];
  98. w[2] = u[2] ^ v[2];
  99. w[3] = u[3] ^ v[3];
  100. w[4] = u[4] ^ v[4];
  101. w[5] = u[5] ^ v[5];
  102. w[6] = u[6] ^ v[6];
  103. w[7] = u[7] ^ v[7];
  104. /* P-Transformation */
  105. key[0] = (w[0] & 0x000000ff) | ((w[2] & 0x000000ff) << 8) |
  106. ((w[4] & 0x000000ff) << 16) | ((w[6] & 0x000000ff) << 24);
  107. key[1] = ((w[0] & 0x0000ff00) >> 8) | (w[2] & 0x0000ff00) |
  108. ((w[4] & 0x0000ff00) << 8) | ((w[6] & 0x0000ff00) << 16);
  109. key[2] = ((w[0] & 0x00ff0000) >> 16) | ((w[2] & 0x00ff0000) >> 8) |
  110. (w[4] & 0x00ff0000) | ((w[6] & 0x00ff0000) << 8);
  111. key[3] = ((w[0] & 0xff000000) >> 24) | ((w[2] & 0xff000000) >> 16) |
  112. ((w[4] & 0xff000000) >> 8) | (w[6] & 0xff000000);
  113. key[4] = (w[1] & 0x000000ff) | ((w[3] & 0x000000ff) << 8) |
  114. ((w[5] & 0x000000ff) << 16) | ((w[7] & 0x000000ff) << 24);
  115. key[5] = ((w[1] & 0x0000ff00) >> 8) | (w[3] & 0x0000ff00) |
  116. ((w[5] & 0x0000ff00) << 8) | ((w[7] & 0x0000ff00) << 16);
  117. key[6] = ((w[1] & 0x00ff0000) >> 16) | ((w[3] & 0x00ff0000) >> 8) |
  118. (w[5] & 0x00ff0000) | ((w[7] & 0x00ff0000) << 8);
  119. key[7] = ((w[1] & 0xff000000) >> 24) | ((w[3] & 0xff000000) >> 16) |
  120. ((w[5] & 0xff000000) >> 8) | (w[7] & 0xff000000);
  121. r = h[i]; /* encriphering transformation */
  122. l = h[i + 1];
  123. GOST_ENCRYPT(key);
  124. s[i] = r;
  125. s[i + 1] = l;
  126. if (i == 6)
  127. break;
  128. l = u[0] ^ u[2]; /* U = A(U) */
  129. r = u[1] ^ u[3];
  130. u[0] = u[2];
  131. u[1] = u[3];
  132. u[2] = u[4];
  133. u[3] = u[5];
  134. u[4] = u[6];
  135. u[5] = u[7];
  136. u[6] = l;
  137. u[7] = r;
  138. if (i == 2) /* Constant C_3 */
  139. {
  140. u[0] ^= 0xff00ff00;
  141. u[1] ^= 0xff00ff00;
  142. u[2] ^= 0x00ff00ff;
  143. u[3] ^= 0x00ff00ff;
  144. u[4] ^= 0x00ffff00;
  145. u[5] ^= 0xff0000ff;
  146. u[6] ^= 0x000000ff;
  147. u[7] ^= 0xff00ffff;
  148. }
  149. l = v[0]; /* V = A(A(V)) */
  150. r = v[2];
  151. v[0] = v[4];
  152. v[2] = v[6];
  153. v[4] = l ^ r;
  154. v[6] = v[0] ^ r;
  155. l = v[1];
  156. r = v[3];
  157. v[1] = v[5];
  158. v[3] = v[7];
  159. v[5] = l ^ r;
  160. v[7] = v[1] ^ r;
  161. }
  162. /* 12 rounds of the LFSR (computed from a product matrix) and xor in M */
  163. u[0] = m[0] ^ s[6];
  164. u[1] = m[1] ^ s[7];
  165. u[2] = m[2] ^ (s[0] << 16) ^ (s[0] >> 16) ^ (s[0] & 0xffff) ^
  166. (s[1] & 0xffff) ^ (s[1] >> 16) ^ (s[2] << 16) ^ s[6] ^ (s[6] << 16) ^
  167. (s[7] & 0xffff0000) ^ (s[7] >> 16);
  168. u[3] = m[3] ^ (s[0] & 0xffff) ^ (s[0] << 16) ^ (s[1] & 0xffff) ^
  169. (s[1] << 16) ^ (s[1] >> 16) ^ (s[2] << 16) ^ (s[2] >> 16) ^
  170. (s[3] << 16) ^ s[6] ^ (s[6] << 16) ^ (s[6] >> 16) ^ (s[7] & 0xffff) ^
  171. (s[7] << 16) ^ (s[7] >> 16);
  172. u[4] = m[4] ^
  173. (s[0] & 0xffff0000) ^ (s[0] << 16) ^ (s[0] >> 16) ^
  174. (s[1] & 0xffff0000) ^ (s[1] >> 16) ^ (s[2] << 16) ^ (s[2] >> 16) ^
  175. (s[3] << 16) ^ (s[3] >> 16) ^ (s[4] << 16) ^ (s[6] << 16) ^
  176. (s[6] >> 16) ^(s[7] & 0xffff) ^ (s[7] << 16) ^ (s[7] >> 16);
  177. u[5] = m[5] ^ (s[0] << 16) ^ (s[0] >> 16) ^ (s[0] & 0xffff0000) ^
  178. (s[1] & 0xffff) ^ s[2] ^ (s[2] >> 16) ^ (s[3] << 16) ^ (s[3] >> 16) ^
  179. (s[4] << 16) ^ (s[4] >> 16) ^ (s[5] << 16) ^ (s[6] << 16) ^
  180. (s[6] >> 16) ^ (s[7] & 0xffff0000) ^ (s[7] << 16) ^ (s[7] >> 16);
  181. u[6] = m[6] ^ s[0] ^ (s[1] >> 16) ^ (s[2] << 16) ^ s[3] ^ (s[3] >> 16) ^
  182. (s[4] << 16) ^ (s[4] >> 16) ^ (s[5] << 16) ^ (s[5] >> 16) ^ s[6] ^
  183. (s[6] << 16) ^ (s[6] >> 16) ^ (s[7] << 16);
  184. u[7] = m[7] ^ (s[0] & 0xffff0000) ^ (s[0] << 16) ^ (s[1] & 0xffff) ^
  185. (s[1] << 16) ^ (s[2] >> 16) ^ (s[3] << 16) ^ s[4] ^ (s[4] >> 16) ^
  186. (s[5] << 16) ^ (s[5] >> 16) ^ (s[6] >> 16) ^ (s[7] & 0xffff) ^
  187. (s[7] << 16) ^ (s[7] >> 16);
  188. /* 16 * 1 round of the LFSR and xor in H */
  189. v[0] = h[0] ^ (u[1] << 16) ^ (u[0] >> 16);
  190. v[1] = h[1] ^ (u[2] << 16) ^ (u[1] >> 16);
  191. v[2] = h[2] ^ (u[3] << 16) ^ (u[2] >> 16);
  192. v[3] = h[3] ^ (u[4] << 16) ^ (u[3] >> 16);
  193. v[4] = h[4] ^ (u[5] << 16) ^ (u[4] >> 16);
  194. v[5] = h[5] ^ (u[6] << 16) ^ (u[5] >> 16);
  195. v[6] = h[6] ^ (u[7] << 16) ^ (u[6] >> 16);
  196. v[7] = h[7] ^ (u[0] & 0xffff0000) ^ (u[0] << 16) ^ (u[7] >> 16) ^
  197. (u[1] & 0xffff0000) ^ (u[1] << 16) ^ (u[6] << 16) ^ (u[7] & 0xffff0000);
  198. /* 61 rounds of LFSR, mixing up h (computed from a product matrix) */
  199. h[0] = (v[0] & 0xffff0000) ^ (v[0] << 16) ^ (v[0] >> 16) ^ (v[1] >> 16) ^
  200. (v[1] & 0xffff0000) ^ (v[2] << 16) ^ (v[3] >> 16) ^ (v[4] << 16) ^
  201. (v[5] >> 16) ^ v[5] ^ (v[6] >> 16) ^ (v[7] << 16) ^ (v[7] >> 16) ^
  202. (v[7] & 0xffff);
  203. h[1] = (v[0] << 16) ^ (v[0] >> 16) ^ (v[0] & 0xffff0000) ^ (v[1] & 0xffff) ^
  204. v[2] ^ (v[2] >> 16) ^ (v[3] << 16) ^ (v[4] >> 16) ^ (v[5] << 16) ^
  205. (v[6] << 16) ^ v[6] ^ (v[7] & 0xffff0000) ^ (v[7] >> 16);
  206. h[2] = (v[0] & 0xffff) ^ (v[0] << 16) ^ (v[1] << 16) ^ (v[1] >> 16) ^
  207. (v[1] & 0xffff0000) ^ (v[2] << 16) ^ (v[3] >> 16) ^ v[3] ^ (v[4] << 16) ^
  208. (v[5] >> 16) ^ v[6] ^ (v[6] >> 16) ^ (v[7] & 0xffff) ^ (v[7] << 16) ^
  209. (v[7] >> 16);
  210. h[3] = (v[0] << 16) ^ (v[0] >> 16) ^ (v[0] & 0xffff0000) ^
  211. (v[1] & 0xffff0000) ^ (v[1] >> 16) ^ (v[2] << 16) ^ (v[2] >> 16) ^ v[2] ^
  212. (v[3] << 16) ^ (v[4] >> 16) ^ v[4] ^ (v[5] << 16) ^ (v[6] << 16) ^
  213. (v[7] & 0xffff) ^ (v[7] >> 16);
  214. h[4] = (v[0] >> 16) ^ (v[1] << 16) ^ v[1] ^ (v[2] >> 16) ^ v[2] ^
  215. (v[3] << 16) ^ (v[3] >> 16) ^ v[3] ^ (v[4] << 16) ^ (v[5] >> 16) ^
  216. v[5] ^ (v[6] << 16) ^ (v[6] >> 16) ^ (v[7] << 16);
  217. h[5] = (v[0] << 16) ^ (v[0] & 0xffff0000) ^ (v[1] << 16) ^ (v[1] >> 16) ^
  218. (v[1] & 0xffff0000) ^ (v[2] << 16) ^ v[2] ^ (v[3] >> 16) ^ v[3] ^
  219. (v[4] << 16) ^ (v[4] >> 16) ^ v[4] ^ (v[5] << 16) ^ (v[6] << 16) ^
  220. (v[6] >> 16) ^ v[6] ^ (v[7] << 16) ^ (v[7] >> 16) ^ (v[7] & 0xffff0000);
  221. h[6] = v[0] ^ v[2] ^ (v[2] >> 16) ^ v[3] ^ (v[3] << 16) ^ v[4] ^
  222. (v[4] >> 16) ^ (v[5] << 16) ^ (v[5] >> 16) ^ v[5] ^ (v[6] << 16) ^
  223. (v[6] >> 16) ^ v[6] ^ (v[7] << 16) ^ v[7];
  224. h[7] = v[0] ^ (v[0] >> 16) ^ (v[1] << 16) ^ (v[1] >> 16) ^ (v[2] << 16) ^
  225. (v[3] >> 16) ^ v[3] ^ (v[4] << 16) ^ v[4] ^ (v[5] >> 16) ^ v[5] ^
  226. (v[6] << 16) ^ (v[6] >> 16) ^ (v[7] << 16) ^ v[7];
  227. }
  228. /* Clear the state of the given context structure. */
  229. void gosthash_reset(GostHashCtx *ctx)
  230. {
  231. memset(ctx->sum, 0, 32);
  232. memset(ctx->hash, 0, 32);
  233. memset(ctx->len, 0, 32);
  234. memset(ctx->partial, 0, 32);
  235. ctx->partial_bytes = 0;
  236. }
  237. /* Mix in a 32-byte chunk ("stage 3") */
  238. void gosthash_bytes(GostHashCtx *ctx, const unsigned char *buf, size_t bits)
  239. {
  240. int i, j;
  241. unsigned long a, c, m[8];
  242. /* convert bytes to a long words and compute the sum */
  243. j = 0;
  244. c = 0;
  245. for (i = 0; i < 8; i++)
  246. {
  247. a = ((unsigned long) buf[j]) |
  248. (((unsigned long) buf[j + 1]) << 8) |
  249. (((unsigned long) buf[j + 2]) << 16) |
  250. (((unsigned long) buf[j + 3]) << 24);
  251. j += 4;
  252. m[i] = a;
  253. /* bugfix July 23, 2002 mjos@iki.fi. Thanks to Kaluzhinsky Anatoly */
  254. if (c)
  255. {
  256. c = a + ctx->sum[i] + 1;
  257. ctx->sum[i] = c;
  258. c = c <= a;
  259. }
  260. else
  261. {
  262. c = a + ctx->sum[i];
  263. ctx->sum[i] = c;
  264. c = c < a;
  265. }
  266. }
  267. /* compress */
  268. gosthash_compress(ctx->hash, m);
  269. /* a 64-bit counter should be sufficient */
  270. ctx->len[0] += bits;
  271. if (ctx->len[0] < bits)
  272. ctx->len[1]++;
  273. }
  274. /* Mix in len bytes of data for the given buffer. */
  275. void gosthash_update(GostHashCtx *ctx, const unsigned char *buf, size_t len)
  276. {
  277. size_t i, j;
  278. i = ctx->partial_bytes;
  279. j = 0;
  280. while (i < 32 && j < len)
  281. ctx->partial[i++] = buf[j++];
  282. if (i < 32)
  283. {
  284. ctx->partial_bytes = i;
  285. return;
  286. }
  287. gosthash_bytes(ctx, ctx->partial, 256);
  288. while ((j + 32) < len)
  289. {
  290. gosthash_bytes(ctx, &buf[j], 256);
  291. j += 32;
  292. }
  293. i = 0;
  294. while (j < len)
  295. ctx->partial[i++] = buf[j++];
  296. ctx->partial_bytes = i;
  297. }
  298. /* Compute and save the 32-byte digest. */
  299. void gosthash_final(GostHashCtx *ctx, unsigned char *digest)
  300. {
  301. int i, j;
  302. unsigned long a;
  303. /* adjust and mix in the last chunk */
  304. if (ctx->partial_bytes > 0)
  305. {
  306. memset(&ctx->partial[ctx->partial_bytes], 0, 32 - ctx->partial_bytes);
  307. gosthash_bytes(ctx, ctx->partial, ctx->partial_bytes << 3);
  308. }
  309. /* mix in the length and the sum */
  310. gosthash_compress(ctx->hash, ctx->len);
  311. gosthash_compress(ctx->hash, ctx->sum);
  312. /* convert the output to bytes */
  313. j = 0;
  314. for (i = 0; i < 8; i++)
  315. {
  316. a = ctx->hash[i];
  317. digest[j] = (unsigned char) a;
  318. digest[j + 1] = (unsigned char) (a >> 8);
  319. digest[j + 2] = (unsigned char) (a >> 16);
  320. digest[j + 3] = (unsigned char) (a >> 24);
  321. j += 4;
  322. }
  323. }