gosthash.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /**********************************************************************
  2. * gosthash.c *
  3. * Copyright (c) 2005-2006 Cryptocom LTD *
  4. * This file is distributed under the same license as OpenSSL *
  5. * *
  6. * Implementation of GOST R 34.11-94 hash function *
  7. * uses on gost89.c and gost89.h Doesn't need OpenSSL *
  8. **********************************************************************/
  9. #include <string.h>
  10. #include "gost89.h"
  11. #include "gosthash.h"
  12. /*
  13. * Use OPENSSL_malloc for memory allocation if compiled with
  14. * -DOPENSSL_BUILD, and libc malloc otherwise
  15. */
  16. #ifndef MYALLOC
  17. # ifdef OPENSSL_BUILD
  18. # include <openssl/crypto.h>
  19. # define MYALLOC(size) OPENSSL_malloc(size)
  20. # define MYFREE(ptr) OPENSSL_free(ptr)
  21. # else
  22. # define MYALLOC(size) malloc(size)
  23. # define MYFREE(ptr) free(ptr)
  24. # endif
  25. #endif
  26. /*
  27. * Following functions are various bit meshing routines used in GOST R
  28. * 34.11-94 algorithms
  29. */
  30. static void swap_bytes(byte * w, byte * k)
  31. {
  32. int i, j;
  33. for (i = 0; i < 4; i++)
  34. for (j = 0; j < 8; j++)
  35. k[i + 4 * j] = w[8 * i + j];
  36. }
  37. /* was A_A */
  38. static void circle_xor8(const byte * w, byte * k)
  39. {
  40. byte buf[8];
  41. int i;
  42. memcpy(buf, w, 8);
  43. memmove(k, w + 8, 24);
  44. for (i = 0; i < 8; i++)
  45. k[i + 24] = buf[i] ^ k[i];
  46. }
  47. /* was R_R */
  48. static void transform_3(byte * data)
  49. {
  50. unsigned short int acc;
  51. acc = (data[0] ^ data[2] ^ data[4] ^ data[6] ^ data[24] ^ data[30]) |
  52. ((data[1] ^ data[3] ^ data[5] ^ data[7] ^ data[25] ^ data[31]) << 8);
  53. memmove(data, data + 2, 30);
  54. data[30] = acc & 0xff;
  55. data[31] = acc >> 8;
  56. }
  57. /* Adds blocks of N bytes modulo 2**(8*n). Returns carry*/
  58. static int add_blocks(int n, byte * left, const byte * right)
  59. {
  60. int i;
  61. int carry = 0;
  62. int sum;
  63. for (i = 0; i < n; i++) {
  64. sum = (int)left[i] + (int)right[i] + carry;
  65. left[i] = sum & 0xff;
  66. carry = sum >> 8;
  67. }
  68. return carry;
  69. }
  70. /* Xor two sequences of bytes */
  71. static void xor_blocks(byte * result, const byte * a, const byte * b,
  72. size_t len)
  73. {
  74. size_t i;
  75. for (i = 0; i < len; i++)
  76. result[i] = a[i] ^ b[i];
  77. }
  78. /*
  79. * Calculate H(i+1) = Hash(Hi,Mi)
  80. * Where H and M are 32 bytes long
  81. */
  82. static int hash_step(gost_ctx * c, byte * H, const byte * M)
  83. {
  84. byte U[32], W[32], V[32], S[32], Key[32];
  85. int i;
  86. /* Compute first key */
  87. xor_blocks(W, H, M, 32);
  88. swap_bytes(W, Key);
  89. /* Encrypt first 8 bytes of H with first key */
  90. gost_enc_with_key(c, Key, H, S);
  91. /* Compute second key */
  92. circle_xor8(H, U);
  93. circle_xor8(M, V);
  94. circle_xor8(V, V);
  95. xor_blocks(W, U, V, 32);
  96. swap_bytes(W, Key);
  97. /* encrypt second 8 bytes of H with second key */
  98. gost_enc_with_key(c, Key, H + 8, S + 8);
  99. /* compute third key */
  100. circle_xor8(U, U);
  101. U[31] = ~U[31];
  102. U[29] = ~U[29];
  103. U[28] = ~U[28];
  104. U[24] = ~U[24];
  105. U[23] = ~U[23];
  106. U[20] = ~U[20];
  107. U[18] = ~U[18];
  108. U[17] = ~U[17];
  109. U[14] = ~U[14];
  110. U[12] = ~U[12];
  111. U[10] = ~U[10];
  112. U[8] = ~U[8];
  113. U[7] = ~U[7];
  114. U[5] = ~U[5];
  115. U[3] = ~U[3];
  116. U[1] = ~U[1];
  117. circle_xor8(V, V);
  118. circle_xor8(V, V);
  119. xor_blocks(W, U, V, 32);
  120. swap_bytes(W, Key);
  121. /* encrypt third 8 bytes of H with third key */
  122. gost_enc_with_key(c, Key, H + 16, S + 16);
  123. /* Compute fourth key */
  124. circle_xor8(U, U);
  125. circle_xor8(V, V);
  126. circle_xor8(V, V);
  127. xor_blocks(W, U, V, 32);
  128. swap_bytes(W, Key);
  129. /* Encrypt last 8 bytes with fourth key */
  130. gost_enc_with_key(c, Key, H + 24, S + 24);
  131. for (i = 0; i < 12; i++)
  132. transform_3(S);
  133. xor_blocks(S, S, M, 32);
  134. transform_3(S);
  135. xor_blocks(S, S, H, 32);
  136. for (i = 0; i < 61; i++)
  137. transform_3(S);
  138. memcpy(H, S, 32);
  139. return 1;
  140. }
  141. /*
  142. * Initialize gost_hash ctx - cleans up temporary structures and set up
  143. * substitution blocks
  144. */
  145. int init_gost_hash_ctx(gost_hash_ctx * ctx,
  146. const gost_subst_block * subst_block)
  147. {
  148. memset(ctx, 0, sizeof(gost_hash_ctx));
  149. ctx->cipher_ctx = (gost_ctx *) MYALLOC(sizeof(gost_ctx));
  150. if (!ctx->cipher_ctx) {
  151. return 0;
  152. }
  153. gost_init(ctx->cipher_ctx, subst_block);
  154. return 1;
  155. }
  156. /*
  157. * Free cipher CTX if it is dynamically allocated. Do not use
  158. * if cipher ctx is statically allocated as in OpenSSL implementation of
  159. * GOST hash algroritm
  160. *
  161. */
  162. void done_gost_hash_ctx(gost_hash_ctx * ctx)
  163. {
  164. /*
  165. * No need to use gost_destroy, because cipher keys are not really secret
  166. * when hashing
  167. */
  168. MYFREE(ctx->cipher_ctx);
  169. }
  170. /*
  171. * reset state of hash context to begin hashing new message
  172. */
  173. int start_hash(gost_hash_ctx * ctx)
  174. {
  175. if (!ctx->cipher_ctx)
  176. return 0;
  177. memset(&(ctx->H), 0, 32);
  178. memset(&(ctx->S), 0, 32);
  179. ctx->len = 0L;
  180. ctx->left = 0;
  181. return 1;
  182. }
  183. /*
  184. * Hash block of arbitrary length
  185. *
  186. *
  187. */
  188. int hash_block(gost_hash_ctx * ctx, const byte * block, size_t length)
  189. {
  190. if (ctx->left) {
  191. /*
  192. * There are some bytes from previous step
  193. */
  194. unsigned int add_bytes = 32 - ctx->left;
  195. if (add_bytes > length) {
  196. add_bytes = length;
  197. }
  198. memcpy(&(ctx->remainder[ctx->left]), block, add_bytes);
  199. ctx->left += add_bytes;
  200. if (ctx->left < 32) {
  201. return 1;
  202. }
  203. block += add_bytes;
  204. length -= add_bytes;
  205. hash_step(ctx->cipher_ctx, ctx->H, ctx->remainder);
  206. add_blocks(32, ctx->S, ctx->remainder);
  207. ctx->len += 32;
  208. ctx->left = 0;
  209. }
  210. while (length >= 32) {
  211. hash_step(ctx->cipher_ctx, ctx->H, block);
  212. add_blocks(32, ctx->S, block);
  213. ctx->len += 32;
  214. block += 32;
  215. length -= 32;
  216. }
  217. if (length) {
  218. memcpy(ctx->remainder, block, ctx->left = length);
  219. }
  220. return 1;
  221. }
  222. /*
  223. * Compute hash value from current state of ctx
  224. * state of hash ctx becomes invalid and cannot be used for further
  225. * hashing.
  226. */
  227. int finish_hash(gost_hash_ctx * ctx, byte * hashval)
  228. {
  229. byte buf[32];
  230. byte H[32];
  231. byte S[32];
  232. ghosthash_len fin_len = ctx->len;
  233. byte *bptr;
  234. memcpy(H, ctx->H, 32);
  235. memcpy(S, ctx->S, 32);
  236. if (ctx->left) {
  237. memset(buf, 0, 32);
  238. memcpy(buf, ctx->remainder, ctx->left);
  239. hash_step(ctx->cipher_ctx, H, buf);
  240. add_blocks(32, S, buf);
  241. fin_len += ctx->left;
  242. }
  243. memset(buf, 0, 32);
  244. bptr = buf;
  245. fin_len <<= 3; /* Hash length in BITS!! */
  246. while (fin_len > 0) {
  247. *(bptr++) = (byte) (fin_len & 0xFF);
  248. fin_len >>= 8;
  249. };
  250. hash_step(ctx->cipher_ctx, H, buf);
  251. hash_step(ctx->cipher_ctx, H, S);
  252. memcpy(hashval, H, 32);
  253. return 1;
  254. }