gosthash.c 6.0 KB

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