SM3.cs 9.7 KB

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