SM3.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. using Org.BouncyCastle.Crypto;
  2. namespace Admin.NET.Core;
  3. public abstract class GeneralDigest : IDigest
  4. {
  5. private const int BYTE_LENGTH = 64;
  6. private readonly byte[] xBuf;
  7. private int xBufOff;
  8. private long byteCount;
  9. internal GeneralDigest()
  10. {
  11. xBuf = new byte[4];
  12. }
  13. internal GeneralDigest(GeneralDigest t)
  14. {
  15. xBuf = new byte[t.xBuf.Length];
  16. Array.Copy(t.xBuf, 0, xBuf, 0, t.xBuf.Length);
  17. xBufOff = t.xBufOff;
  18. byteCount = t.byteCount;
  19. }
  20. public void Update(byte input)
  21. {
  22. xBuf[xBufOff++] = input;
  23. if (xBufOff == xBuf.Length)
  24. {
  25. ProcessWord(xBuf, 0);
  26. xBufOff = 0;
  27. }
  28. byteCount++;
  29. }
  30. public void BlockUpdate(
  31. byte[] input,
  32. int inOff,
  33. int length)
  34. {
  35. //
  36. // fill the current word
  37. //
  38. while ((xBufOff != 0) && (length > 0))
  39. {
  40. Update(input[inOff]);
  41. inOff++;
  42. length--;
  43. }
  44. //
  45. // process whole words.
  46. //
  47. while (length > xBuf.Length)
  48. {
  49. ProcessWord(input, inOff);
  50. inOff += xBuf.Length;
  51. length -= xBuf.Length;
  52. byteCount += xBuf.Length;
  53. }
  54. //
  55. // load in the remainder.
  56. //
  57. while (length > 0)
  58. {
  59. Update(input[inOff]);
  60. inOff++;
  61. length--;
  62. }
  63. }
  64. public void Finish()
  65. {
  66. long bitLength = (byteCount << 3);
  67. //
  68. // add the pad bytes.
  69. //
  70. Update(unchecked((byte)128));
  71. while (xBufOff != 0) Update(unchecked((byte)0));
  72. ProcessLength(bitLength);
  73. ProcessBlock();
  74. }
  75. public virtual void Reset()
  76. {
  77. byteCount = 0;
  78. xBufOff = 0;
  79. Array.Clear(xBuf, 0, xBuf.Length);
  80. }
  81. public int GetByteLength()
  82. {
  83. return BYTE_LENGTH;
  84. }
  85. internal abstract void ProcessWord(byte[] input, int inOff);
  86. internal abstract void ProcessLength(long bitLength);
  87. internal abstract void ProcessBlock();
  88. public abstract string AlgorithmName { get; }
  89. public abstract int GetDigestSize();
  90. public abstract int DoFinal(byte[] output, int outOff);
  91. }
  92. public class SupportClass
  93. {
  94. /// <summary>
  95. /// Performs an unsigned bitwise right shift with the specified number
  96. /// </summary>
  97. /// <param name="number">Number to operate on</param>
  98. /// <param name="bits">Ammount of bits to shift</param>
  99. /// <returns>The resulting number from the shift operation</returns>
  100. public static int URShift(int number, int bits)
  101. {
  102. if (number >= 0)
  103. return number >> bits;
  104. else
  105. return (number >> bits) + (2 << ~bits);
  106. }
  107. /// <summary>
  108. /// Performs an unsigned bitwise right shift with the specified number
  109. /// </summary>
  110. /// <param name="number">Number to operate on</param>
  111. /// <param name="bits">Ammount of bits to shift</param>
  112. /// <returns>The resulting number from the shift operation</returns>
  113. public static int URShift(int number, long bits)
  114. {
  115. return URShift(number, (int)bits);
  116. }
  117. /// <summary>
  118. /// Performs an unsigned bitwise right shift with the specified number
  119. /// </summary>
  120. /// <param name="number">Number to operate on</param>
  121. /// <param name="bits">Ammount of bits to shift</param>
  122. /// <returns>The resulting number from the shift operation</returns>
  123. public static long URShift(long number, int bits)
  124. {
  125. if (number >= 0)
  126. return number >> bits;
  127. else
  128. return (number >> bits) + (2L << ~bits);
  129. }
  130. /// <summary>
  131. /// Performs an unsigned bitwise right shift with the specified number
  132. /// </summary>
  133. /// <param name="number">Number to operate on</param>
  134. /// <param name="bits">Ammount of bits to shift</param>
  135. /// <returns>The resulting number from the shift operation</returns>
  136. public static long URShift(long number, long bits)
  137. {
  138. return URShift(number, (int)bits);
  139. }
  140. }
  141. public class SM3Digest : GeneralDigest
  142. {
  143. public override string AlgorithmName
  144. {
  145. get
  146. {
  147. return "SM3";
  148. }
  149. }
  150. public override int GetDigestSize()
  151. {
  152. return DIGEST_LENGTH;
  153. }
  154. private const int DIGEST_LENGTH = 32;
  155. private static readonly int[] v0 = new int[] { 0x7380166f, 0x4914b2b9, 0x172442d7, unchecked((int)0xda8a0600), unchecked((int)0xa96f30bc), 0x163138aa, unchecked((int)0xe38dee4d), unchecked((int)0xb0fb0e4e) };
  156. private readonly int[] v = new int[8];
  157. private readonly int[] v_ = new int[8];
  158. private static readonly int[] X0 = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  159. private readonly int[] X = new int[68];
  160. private int xOff;
  161. private readonly int T_00_15 = 0x79cc4519;
  162. private readonly int T_16_63 = 0x7a879d8a;
  163. public SM3Digest()
  164. {
  165. Reset();
  166. }
  167. public SM3Digest(SM3Digest t) : base(t)
  168. {
  169. Array.Copy(t.X, 0, X, 0, t.X.Length);
  170. xOff = t.xOff;
  171. Array.Copy(t.v, 0, v, 0, t.v.Length);
  172. }
  173. public override void Reset()
  174. {
  175. base.Reset();
  176. Array.Copy(v0, 0, v, 0, v0.Length);
  177. xOff = 0;
  178. Array.Copy(X0, 0, X, 0, X0.Length);
  179. }
  180. internal override void ProcessBlock()
  181. {
  182. int i;
  183. int[] ww = X;
  184. int[] ww_ = new int[64];
  185. for (i = 16; i < 68; i++)
  186. {
  187. ww[i] = P1(ww[i - 16] ^ ww[i - 9] ^ (ROTATE(ww[i - 3], 15))) ^ (ROTATE(ww[i - 13], 7)) ^ ww[i - 6];
  188. }
  189. for (i = 0; i < 64; i++)
  190. {
  191. ww_[i] = ww[i] ^ ww[i + 4];
  192. }
  193. int[] vv = v;
  194. int[] vv_ = v_;
  195. Array.Copy(vv, 0, vv_, 0, v0.Length);
  196. int SS1, SS2, TT1, TT2, aaa;
  197. for (i = 0; i < 16; i++)
  198. {
  199. aaa = ROTATE(vv_[0], 12);
  200. SS1 = aaa + vv_[4] + ROTATE(T_00_15, i);
  201. SS1 = ROTATE(SS1, 7);
  202. SS2 = SS1 ^ aaa;
  203. TT1 = FF_00_15(vv_[0], vv_[1], vv_[2]) + vv_[3] + SS2 + ww_[i];
  204. TT2 = GG_00_15(vv_[4], vv_[5], vv_[6]) + vv_[7] + SS1 + ww[i];
  205. vv_[3] = vv_[2];
  206. vv_[2] = ROTATE(vv_[1], 9);
  207. vv_[1] = vv_[0];
  208. vv_[0] = TT1;
  209. vv_[7] = vv_[6];
  210. vv_[6] = ROTATE(vv_[5], 19);
  211. vv_[5] = vv_[4];
  212. vv_[4] = P0(TT2);
  213. }
  214. for (i = 16; i < 64; i++)
  215. {
  216. aaa = ROTATE(vv_[0], 12);
  217. SS1 = aaa + vv_[4] + ROTATE(T_16_63, i);
  218. SS1 = ROTATE(SS1, 7);
  219. SS2 = SS1 ^ aaa;
  220. TT1 = FF_16_63(vv_[0], vv_[1], vv_[2]) + vv_[3] + SS2 + ww_[i];
  221. TT2 = GG_16_63(vv_[4], vv_[5], vv_[6]) + vv_[7] + SS1 + ww[i];
  222. vv_[3] = vv_[2];
  223. vv_[2] = ROTATE(vv_[1], 9);
  224. vv_[1] = vv_[0];
  225. vv_[0] = TT1;
  226. vv_[7] = vv_[6];
  227. vv_[6] = ROTATE(vv_[5], 19);
  228. vv_[5] = vv_[4];
  229. vv_[4] = P0(TT2);
  230. }
  231. for (i = 0; i < 8; i++)
  232. {
  233. vv[i] ^= vv_[i];
  234. }
  235. // Reset
  236. xOff = 0;
  237. Array.Copy(X0, 0, X, 0, X0.Length);
  238. }
  239. internal override void ProcessWord(byte[] in_Renamed, int inOff)
  240. {
  241. int n = in_Renamed[inOff] << 24;
  242. n |= (in_Renamed[++inOff] & 0xff) << 16;
  243. n |= (in_Renamed[++inOff] & 0xff) << 8;
  244. n |= (in_Renamed[++inOff] & 0xff);
  245. X[xOff] = n;
  246. if (++xOff == 16)
  247. {
  248. ProcessBlock();
  249. }
  250. }
  251. internal override void ProcessLength(long bitLength)
  252. {
  253. if (xOff > 14)
  254. {
  255. ProcessBlock();
  256. }
  257. X[14] = (int)(SupportClass.URShift(bitLength, 32));
  258. X[15] = (int)(bitLength & unchecked((int)0xffffffff));
  259. }
  260. public static void IntToBigEndian(int n, byte[] bs, int off)
  261. {
  262. bs[off] = (byte)(SupportClass.URShift(n, 24));
  263. bs[++off] = (byte)(SupportClass.URShift(n, 16));
  264. bs[++off] = (byte)(SupportClass.URShift(n, 8));
  265. bs[++off] = (byte)(n);
  266. }
  267. public override int DoFinal(byte[] out_Renamed, int outOff)
  268. {
  269. Finish();
  270. for (int i = 0; i < 8; i++)
  271. {
  272. IntToBigEndian(v[i], out_Renamed, outOff + i * 4);
  273. }
  274. Reset();
  275. return DIGEST_LENGTH;
  276. }
  277. private static int ROTATE(int x, int n)
  278. {
  279. return (x << n) | (SupportClass.URShift(x, (32 - n)));
  280. }
  281. private static int P0(int X)
  282. {
  283. return ((X) ^ ROTATE((X), 9) ^ ROTATE((X), 17));
  284. }
  285. private static int P1(int X)
  286. {
  287. return ((X) ^ ROTATE((X), 15) ^ ROTATE((X), 23));
  288. }
  289. private static int FF_00_15(int X, int Y, int Z)
  290. {
  291. return (X ^ Y ^ Z);
  292. }
  293. private static int FF_16_63(int X, int Y, int Z)
  294. {
  295. return ((X & Y) | (X & Z) | (Y & Z));
  296. }
  297. private static int GG_00_15(int X, int Y, int Z)
  298. {
  299. return (X ^ Y ^ Z);
  300. }
  301. private static int GG_16_63(int X, int Y, int Z)
  302. {
  303. return ((X & Y) | (~X & Z));
  304. }
  305. //[STAThread]
  306. //public static void Main()
  307. //{
  308. // byte[] md = new byte[32];
  309. // byte[] msg1 = Encoding.Default.GetBytes("ererfeiisgod");
  310. // SM3Digest sm3 = new SM3Digest();
  311. // sm3.BlockUpdate(msg1, 0, msg1.Length);
  312. // sm3.DoFinal(md, 0);
  313. // System.String s = new UTF8Encoding().GetString(Hex.Encode(md));
  314. // System.Console.Out.WriteLine(s.ToUpper());
  315. // Console.ReadLine();
  316. //}
  317. }