GOST3411Digest.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Engines;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Utilities;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  8. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Digests
  9. {
  10. /**
  11. * implementation of GOST R 34.11-94
  12. */
  13. public class Gost3411Digest
  14. : IDigest, IMemoable
  15. {
  16. private const int DIGEST_LENGTH = 32;
  17. private byte[] H = new byte[32], L = new byte[32],
  18. M = new byte[32], Sum = new byte[32];
  19. private byte[][] C = MakeC();
  20. private byte[] xBuf = new byte[32];
  21. private int xBufOff;
  22. private ulong byteCount;
  23. private readonly IBlockCipher cipher = new Gost28147Engine();
  24. private byte[] sBox;
  25. private static byte[][] MakeC()
  26. {
  27. byte[][] c = new byte[4][];
  28. for (int i = 0; i < 4; ++i)
  29. {
  30. c[i] = new byte[32];
  31. }
  32. return c;
  33. }
  34. /**
  35. * Standard constructor
  36. */
  37. public Gost3411Digest()
  38. {
  39. sBox = Gost28147Engine.GetSBox("D-A");
  40. cipher.Init(true, new ParametersWithSBox(null, sBox));
  41. Reset();
  42. }
  43. /**
  44. * Constructor to allow use of a particular sbox with GOST28147
  45. * @see GOST28147Engine#getSBox(String)
  46. */
  47. public Gost3411Digest(byte[] sBoxParam)
  48. {
  49. sBox = Arrays.Clone(sBoxParam);
  50. cipher.Init(true, new ParametersWithSBox(null, sBox));
  51. Reset();
  52. }
  53. /**
  54. * Copy constructor. This will copy the state of the provided
  55. * message digest.
  56. */
  57. public Gost3411Digest(Gost3411Digest t)
  58. {
  59. Reset(t);
  60. }
  61. public string AlgorithmName
  62. {
  63. get { return "Gost3411"; }
  64. }
  65. public int GetDigestSize()
  66. {
  67. return DIGEST_LENGTH;
  68. }
  69. public void Update(
  70. byte input)
  71. {
  72. xBuf[xBufOff++] = input;
  73. if (xBufOff == xBuf.Length)
  74. {
  75. sumByteArray(xBuf); // calc sum M
  76. processBlock(xBuf, 0);
  77. xBufOff = 0;
  78. }
  79. byteCount++;
  80. }
  81. public void BlockUpdate(
  82. byte[] input,
  83. int inOff,
  84. int length)
  85. {
  86. while ((xBufOff != 0) && (length > 0))
  87. {
  88. Update(input[inOff]);
  89. inOff++;
  90. length--;
  91. }
  92. while (length > xBuf.Length)
  93. {
  94. Array.Copy(input, inOff, xBuf, 0, xBuf.Length);
  95. sumByteArray(xBuf); // calc sum M
  96. processBlock(xBuf, 0);
  97. inOff += xBuf.Length;
  98. length -= xBuf.Length;
  99. byteCount += (uint)xBuf.Length;
  100. }
  101. // load in the remainder.
  102. while (length > 0)
  103. {
  104. Update(input[inOff]);
  105. inOff++;
  106. length--;
  107. }
  108. }
  109. // (i + 1 + 4(k - 1)) = 8i + k i = 0-3, k = 1-8
  110. private byte[] K = new byte[32];
  111. private byte[] P(byte[] input)
  112. {
  113. int fourK = 0;
  114. for(int k = 0; k < 8; k++)
  115. {
  116. K[fourK++] = input[k];
  117. K[fourK++] = input[8 + k];
  118. K[fourK++] = input[16 + k];
  119. K[fourK++] = input[24 + k];
  120. }
  121. return K;
  122. }
  123. //A (x) = (x0 ^ x1) || x3 || x2 || x1
  124. byte[] a = new byte[8];
  125. private byte[] A(byte[] input)
  126. {
  127. for(int j=0; j<8; j++)
  128. {
  129. a[j]=(byte)(input[j] ^ input[j+8]);
  130. }
  131. Array.Copy(input, 8, input, 0, 24);
  132. Array.Copy(a, 0, input, 24, 8);
  133. return input;
  134. }
  135. //Encrypt function, ECB mode
  136. private void E(byte[] key, byte[] s, int sOff, byte[] input, int inOff)
  137. {
  138. cipher.Init(true, new KeyParameter(key));
  139. cipher.ProcessBlock(input, inOff, s, sOff);
  140. }
  141. // (in:) n16||..||n1 ==> (out:) n1^n2^n3^n4^n13^n16||n16||..||n2
  142. internal short[] wS = new short[16], w_S = new short[16];
  143. private void fw(byte[] input)
  144. {
  145. cpyBytesToShort(input, wS);
  146. w_S[15] = (short)(wS[0] ^ wS[1] ^ wS[2] ^ wS[3] ^ wS[12] ^ wS[15]);
  147. Array.Copy(wS, 1, w_S, 0, 15);
  148. cpyShortToBytes(w_S, input);
  149. }
  150. // block processing
  151. internal byte[] S = new byte[32], U = new byte[32], V = new byte[32], W = new byte[32];
  152. private void processBlock(byte[] input, int inOff)
  153. {
  154. Array.Copy(input, inOff, M, 0, 32);
  155. //key step 1
  156. // H = h3 || h2 || h1 || h0
  157. // S = s3 || s2 || s1 || s0
  158. H.CopyTo(U, 0);
  159. M.CopyTo(V, 0);
  160. for (int j=0; j<32; j++)
  161. {
  162. W[j] = (byte)(U[j]^V[j]);
  163. }
  164. // Encrypt gost28147-ECB
  165. E(P(W), S, 0, H, 0); // s0 = EK0 [h0]
  166. //keys step 2,3,4
  167. for (int i=1; i<4; i++)
  168. {
  169. byte[] tmpA = A(U);
  170. for (int j=0; j<32; j++)
  171. {
  172. U[j] = (byte)(tmpA[j] ^ C[i][j]);
  173. }
  174. V = A(A(V));
  175. for (int j=0; j<32; j++)
  176. {
  177. W[j] = (byte)(U[j]^V[j]);
  178. }
  179. // Encrypt gost28147-ECB
  180. E(P(W), S, i * 8, H, i * 8); // si = EKi [hi]
  181. }
  182. // x(M, H) = y61(H^y(M^y12(S)))
  183. for(int n = 0; n < 12; n++)
  184. {
  185. fw(S);
  186. }
  187. for(int n = 0; n < 32; n++)
  188. {
  189. S[n] = (byte)(S[n] ^ M[n]);
  190. }
  191. fw(S);
  192. for(int n = 0; n < 32; n++)
  193. {
  194. S[n] = (byte)(H[n] ^ S[n]);
  195. }
  196. for(int n = 0; n < 61; n++)
  197. {
  198. fw(S);
  199. }
  200. Array.Copy(S, 0, H, 0, H.Length);
  201. }
  202. private void finish()
  203. {
  204. ulong bitCount = byteCount * 8;
  205. Pack.UInt64_To_LE(bitCount, L);
  206. while (xBufOff != 0)
  207. {
  208. Update((byte)0);
  209. }
  210. processBlock(L, 0);
  211. processBlock(Sum, 0);
  212. }
  213. public int DoFinal(
  214. byte[] output,
  215. int outOff)
  216. {
  217. finish();
  218. H.CopyTo(output, outOff);
  219. Reset();
  220. return DIGEST_LENGTH;
  221. }
  222. /**
  223. * reset the chaining variables to the IV values.
  224. */
  225. private static readonly byte[] C2 = {
  226. 0x00,(byte)0xFF,0x00,(byte)0xFF,0x00,(byte)0xFF,0x00,(byte)0xFF,
  227. (byte)0xFF,0x00,(byte)0xFF,0x00,(byte)0xFF,0x00,(byte)0xFF,0x00,
  228. 0x00,(byte)0xFF,(byte)0xFF,0x00,(byte)0xFF,0x00,0x00,(byte)0xFF,
  229. (byte)0xFF,0x00,0x00,0x00,(byte)0xFF,(byte)0xFF,0x00,(byte)0xFF
  230. };
  231. public void Reset()
  232. {
  233. byteCount = 0;
  234. xBufOff = 0;
  235. Array.Clear(H, 0, H.Length);
  236. Array.Clear(L, 0, L.Length);
  237. Array.Clear(M, 0, M.Length);
  238. Array.Clear(C[1], 0, C[1].Length); // real index C = +1 because index array with 0.
  239. Array.Clear(C[3], 0, C[3].Length);
  240. Array.Clear(Sum, 0, Sum.Length);
  241. Array.Clear(xBuf, 0, xBuf.Length);
  242. C2.CopyTo(C[2], 0);
  243. }
  244. // 256 bitsblock modul -> (Sum + a mod (2^256))
  245. private void sumByteArray(
  246. byte[] input)
  247. {
  248. int carry = 0;
  249. for (int i = 0; i != Sum.Length; i++)
  250. {
  251. int sum = (Sum[i] & 0xff) + (input[i] & 0xff) + carry;
  252. Sum[i] = (byte)sum;
  253. carry = sum >> 8;
  254. }
  255. }
  256. private static void cpyBytesToShort(byte[] S, short[] wS)
  257. {
  258. for(int i = 0; i < S.Length / 2; i++)
  259. {
  260. wS[i] = (short)(((S[i*2+1]<<8)&0xFF00)|(S[i*2]&0xFF));
  261. }
  262. }
  263. private static void cpyShortToBytes(short[] wS, byte[] S)
  264. {
  265. for(int i=0; i<S.Length/2; i++)
  266. {
  267. S[i*2 + 1] = (byte)(wS[i] >> 8);
  268. S[i*2] = (byte)wS[i];
  269. }
  270. }
  271. public int GetByteLength()
  272. {
  273. return 32;
  274. }
  275. public IMemoable Copy()
  276. {
  277. return new Gost3411Digest(this);
  278. }
  279. public void Reset(IMemoable other)
  280. {
  281. Gost3411Digest t = (Gost3411Digest)other;
  282. this.sBox = t.sBox;
  283. cipher.Init(true, new ParametersWithSBox(null, sBox));
  284. Reset();
  285. Array.Copy(t.H, 0, this.H, 0, t.H.Length);
  286. Array.Copy(t.L, 0, this.L, 0, t.L.Length);
  287. Array.Copy(t.M, 0, this.M, 0, t.M.Length);
  288. Array.Copy(t.Sum, 0, this.Sum, 0, t.Sum.Length);
  289. Array.Copy(t.C[1], 0, this.C[1], 0, t.C[1].Length);
  290. Array.Copy(t.C[2], 0, this.C[2], 0, t.C[2].Length);
  291. Array.Copy(t.C[3], 0, this.C[3], 0, t.C[3].Length);
  292. Array.Copy(t.xBuf, 0, this.xBuf, 0, t.xBuf.Length);
  293. this.xBufOff = t.xBufOff;
  294. this.byteCount = t.byteCount;
  295. }
  296. }
  297. }
  298. #pragma warning restore
  299. #endif