SM3Digest.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Utilities;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  6. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Digests
  7. {
  8. /// <summary>
  9. /// Implementation of Chinese SM3 digest as described at
  10. /// http://tools.ietf.org/html/draft-shen-sm3-hash-00
  11. /// and at .... ( Chinese PDF )
  12. /// </summary>
  13. /// <remarks>
  14. /// The specification says "process a bit stream",
  15. /// but this is written to process bytes in blocks of 4,
  16. /// meaning this will process 32-bit word groups.
  17. /// But so do also most other digest specifications,
  18. /// including the SHA-256 which was a origin for
  19. /// this specification.
  20. /// </remarks>
  21. public class SM3Digest
  22. : GeneralDigest
  23. {
  24. private const int DIGEST_LENGTH = 32; // bytes
  25. private const int BLOCK_SIZE = 64 / 4; // of 32 bit ints (16 ints)
  26. private uint[] V = new uint[DIGEST_LENGTH / 4]; // in 32 bit ints (8 ints)
  27. private uint[] inwords = new uint[BLOCK_SIZE];
  28. private int xOff;
  29. // Work-bufs used within processBlock()
  30. private uint[] W = new uint[68];
  31. // Round constant T for processBlock() which is 32 bit integer rolled left up to (63 MOD 32) bit positions.
  32. private static readonly uint[] T = new uint[64];
  33. static SM3Digest()
  34. {
  35. for (int i = 0; i < 16; ++i)
  36. {
  37. uint t = 0x79CC4519;
  38. T[i] = (t << i) | (t >> (32 - i));
  39. }
  40. for (int i = 16; i < 64; ++i)
  41. {
  42. int n = i % 32;
  43. uint t = 0x7A879D8A;
  44. T[i] = (t << n) | (t >> (32 - n));
  45. }
  46. }
  47. /// <summary>
  48. /// Standard constructor
  49. /// </summary>
  50. public SM3Digest()
  51. {
  52. Reset();
  53. }
  54. /// <summary>
  55. /// Copy constructor. This will copy the state of the provided
  56. /// message digest.
  57. /// </summary>
  58. public SM3Digest(SM3Digest t)
  59. : base(t)
  60. {
  61. CopyIn(t);
  62. }
  63. private void CopyIn(SM3Digest t)
  64. {
  65. Array.Copy(t.V, 0, this.V, 0, this.V.Length);
  66. Array.Copy(t.inwords, 0, this.inwords, 0, this.inwords.Length);
  67. xOff = t.xOff;
  68. }
  69. public override string AlgorithmName
  70. {
  71. get { return "SM3"; }
  72. }
  73. public override int GetDigestSize()
  74. {
  75. return DIGEST_LENGTH;
  76. }
  77. public override IMemoable Copy()
  78. {
  79. return new SM3Digest(this);
  80. }
  81. public override void Reset(IMemoable other)
  82. {
  83. SM3Digest d = (SM3Digest)other;
  84. base.CopyIn(d);
  85. CopyIn(d);
  86. }
  87. /// <summary>
  88. /// reset the chaining variables
  89. /// </summary>
  90. public override void Reset()
  91. {
  92. base.Reset();
  93. this.V[0] = 0x7380166F;
  94. this.V[1] = 0x4914B2B9;
  95. this.V[2] = 0x172442D7;
  96. this.V[3] = 0xDA8A0600;
  97. this.V[4] = 0xA96F30BC;
  98. this.V[5] = 0x163138AA;
  99. this.V[6] = 0xE38DEE4D;
  100. this.V[7] = 0xB0FB0E4E;
  101. this.xOff = 0;
  102. }
  103. public override int DoFinal(byte[] output, int outOff)
  104. {
  105. Finish();
  106. Pack.UInt32_To_BE(V, output, outOff);
  107. Reset();
  108. return DIGEST_LENGTH;
  109. }
  110. internal override void ProcessWord(byte[] input,
  111. int inOff)
  112. {
  113. uint n = Pack.BE_To_UInt32(input, inOff);
  114. this.inwords[this.xOff] = n;
  115. ++this.xOff;
  116. if (this.xOff >= 16)
  117. {
  118. ProcessBlock();
  119. }
  120. }
  121. internal override void ProcessLength(long bitLength)
  122. {
  123. if (this.xOff > (BLOCK_SIZE - 2))
  124. {
  125. // xOff == 15 --> can't fit the 64 bit length field at tail..
  126. this.inwords[this.xOff] = 0; // fill with zero
  127. ++this.xOff;
  128. ProcessBlock();
  129. }
  130. // Fill with zero words, until reach 2nd to last slot
  131. while (this.xOff < (BLOCK_SIZE - 2))
  132. {
  133. this.inwords[this.xOff] = 0;
  134. ++this.xOff;
  135. }
  136. // Store input data length in BITS
  137. this.inwords[this.xOff++] = (uint)(bitLength >> 32);
  138. this.inwords[this.xOff++] = (uint)(bitLength);
  139. }
  140. /*
  141. 3.4.2. Constants
  142. Tj = 79cc4519 when 0 < = j < = 15
  143. Tj = 7a879d8a when 16 < = j < = 63
  144. 3.4.3. Boolean function
  145. FFj(X;Y;Z) = X XOR Y XOR Z when 0 < = j < = 15
  146. = (X AND Y) OR (X AND Z) OR (Y AND Z) when 16 < = j < = 63
  147. GGj(X;Y;Z) = X XOR Y XOR Z when 0 < = j < = 15
  148. = (X AND Y) OR (NOT X AND Z) when 16 < = j < = 63
  149. The X, Y, Z in the fomular are words!GBP
  150. 3.4.4. Permutation function
  151. P0(X) = X XOR (X <<< 9) XOR (X <<< 17) ## ROLL, not SHIFT
  152. P1(X) = X XOR (X <<< 15) XOR (X <<< 23) ## ROLL, not SHIFT
  153. The X in the fomular are a word.
  154. ----------
  155. Each ROLL converted to Java expression:
  156. ROLL 9 : ((x << 9) | (x >> (32-9))))
  157. ROLL 17 : ((x << 17) | (x >> (32-17)))
  158. ROLL 15 : ((x << 15) | (x >> (32-15)))
  159. ROLL 23 : ((x << 23) | (x >> (32-23)))
  160. */
  161. private uint P0(uint x)
  162. {
  163. uint r9 = ((x << 9) | (x >> (32 - 9)));
  164. uint r17 = ((x << 17) | (x >> (32 - 17)));
  165. return (x ^ r9 ^ r17);
  166. }
  167. private uint P1(uint x)
  168. {
  169. uint r15 = ((x << 15) | (x >> (32 - 15)));
  170. uint r23 = ((x << 23) | (x >> (32 - 23)));
  171. return (x ^ r15 ^ r23);
  172. }
  173. private uint FF0(uint x, uint y, uint z)
  174. {
  175. return (x ^ y ^ z);
  176. }
  177. private uint FF1(uint x, uint y, uint z)
  178. {
  179. return ((x & y) | (x & z) | (y & z));
  180. }
  181. private uint GG0(uint x, uint y, uint z)
  182. {
  183. return (x ^ y ^ z);
  184. }
  185. private uint GG1(uint x, uint y, uint z)
  186. {
  187. return ((x & y) | ((~x) & z));
  188. }
  189. internal override void ProcessBlock()
  190. {
  191. for (int j = 0; j < 16; ++j)
  192. {
  193. this.W[j] = this.inwords[j];
  194. }
  195. for (int j = 16; j < 68; ++j)
  196. {
  197. uint wj3 = this.W[j - 3];
  198. uint r15 = ((wj3 << 15) | (wj3 >> (32 - 15)));
  199. uint wj13 = this.W[j - 13];
  200. uint r7 = ((wj13 << 7) | (wj13 >> (32 - 7)));
  201. this.W[j] = P1(this.W[j - 16] ^ this.W[j - 9] ^ r15) ^ r7 ^ this.W[j - 6];
  202. }
  203. uint A = this.V[0];
  204. uint B = this.V[1];
  205. uint C = this.V[2];
  206. uint D = this.V[3];
  207. uint E = this.V[4];
  208. uint F = this.V[5];
  209. uint G = this.V[6];
  210. uint H = this.V[7];
  211. for (int j = 0; j < 16; ++j)
  212. {
  213. uint a12 = ((A << 12) | (A >> (32 - 12)));
  214. uint s1_ = a12 + E + T[j];
  215. uint SS1 = ((s1_ << 7) | (s1_ >> (32 - 7)));
  216. uint SS2 = SS1 ^ a12;
  217. uint Wj = W[j];
  218. uint W1j = Wj ^ W[j + 4];
  219. uint TT1 = FF0(A, B, C) + D + SS2 + W1j;
  220. uint TT2 = GG0(E, F, G) + H + SS1 + Wj;
  221. D = C;
  222. C = ((B << 9) | (B >> (32 - 9)));
  223. B = A;
  224. A = TT1;
  225. H = G;
  226. G = ((F << 19) | (F >> (32 - 19)));
  227. F = E;
  228. E = P0(TT2);
  229. }
  230. // Different FF,GG functions on rounds 16..63
  231. for (int j = 16; j < 64; ++j)
  232. {
  233. uint a12 = ((A << 12) | (A >> (32 - 12)));
  234. uint s1_ = a12 + E + T[j];
  235. uint SS1 = ((s1_ << 7) | (s1_ >> (32 - 7)));
  236. uint SS2 = SS1 ^ a12;
  237. uint Wj = W[j];
  238. uint W1j = Wj ^ W[j + 4];
  239. uint TT1 = FF1(A, B, C) + D + SS2 + W1j;
  240. uint TT2 = GG1(E, F, G) + H + SS1 + Wj;
  241. D = C;
  242. C = ((B << 9) | (B >> (32 - 9)));
  243. B = A;
  244. A = TT1;
  245. H = G;
  246. G = ((F << 19) | (F >> (32 - 19)));
  247. F = E;
  248. E = P0(TT2);
  249. }
  250. this.V[0] ^= A;
  251. this.V[1] ^= B;
  252. this.V[2] ^= C;
  253. this.V[3] ^= D;
  254. this.V[4] ^= E;
  255. this.V[5] ^= F;
  256. this.V[6] ^= G;
  257. this.V[7] ^= H;
  258. this.xOff = 0;
  259. }
  260. }
  261. }
  262. #pragma warning restore
  263. #endif