SM2Engine.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Digests;
  5. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  6. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Utilities;
  7. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Math;
  8. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.EC;
  9. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.EC.Multiplier;
  10. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Security;
  11. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  12. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Engines
  13. {
  14. /// <summary>
  15. /// SM2 public key encryption engine - based on https://tools.ietf.org/html/draft-shen-sm2-ecdsa-02.
  16. /// </summary>
  17. public class SM2Engine
  18. {
  19. public enum Mode
  20. {
  21. C1C2C3, C1C3C2
  22. }
  23. private readonly IDigest mDigest;
  24. private readonly Mode mMode;
  25. private bool mForEncryption;
  26. private ECKeyParameters mECKey;
  27. private ECDomainParameters mECParams;
  28. private int mCurveLength;
  29. private SecureRandom mRandom;
  30. public SM2Engine()
  31. : this(new SM3Digest())
  32. {
  33. }
  34. public SM2Engine(Mode mode)
  35. : this(new SM3Digest(), mode)
  36. {
  37. }
  38. public SM2Engine(IDigest digest)
  39. : this(digest, Mode.C1C2C3)
  40. {
  41. }
  42. public SM2Engine(IDigest digest, Mode mode)
  43. {
  44. mDigest = digest;
  45. mMode = mode;
  46. }
  47. public virtual void Init(bool forEncryption, ICipherParameters param)
  48. {
  49. this.mForEncryption = forEncryption;
  50. if (forEncryption)
  51. {
  52. ParametersWithRandom rParam = (ParametersWithRandom)param;
  53. mECKey = (ECKeyParameters)rParam.Parameters;
  54. mECParams = mECKey.Parameters;
  55. ECPoint s = ((ECPublicKeyParameters)mECKey).Q.Multiply(mECParams.H);
  56. if (s.IsInfinity)
  57. throw new ArgumentException("invalid key: [h]Q at infinity");
  58. mRandom = rParam.Random;
  59. }
  60. else
  61. {
  62. mECKey = (ECKeyParameters)param;
  63. mECParams = mECKey.Parameters;
  64. }
  65. mCurveLength = (mECParams.Curve.FieldSize + 7) / 8;
  66. }
  67. public virtual byte[] ProcessBlock(byte[] input, int inOff, int inLen)
  68. {
  69. if ((inOff + inLen) > input.Length || inLen == 0)
  70. throw new DataLengthException("input buffer too short");
  71. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  72. return ProcessBlock(input.AsSpan(inOff, inLen));
  73. #else
  74. if (mForEncryption)
  75. {
  76. return Encrypt(input, inOff, inLen);
  77. }
  78. else
  79. {
  80. return Decrypt(input, inOff, inLen);
  81. }
  82. #endif
  83. }
  84. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  85. public virtual byte[] ProcessBlock(ReadOnlySpan<byte> input)
  86. {
  87. if (input.Length == 0)
  88. throw new DataLengthException("input buffer too short");
  89. if (mForEncryption)
  90. {
  91. return Encrypt(input);
  92. }
  93. else
  94. {
  95. return Decrypt(input);
  96. }
  97. }
  98. #endif
  99. protected virtual ECMultiplier CreateBasePointMultiplier()
  100. {
  101. return new FixedPointCombMultiplier();
  102. }
  103. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  104. private byte[] Encrypt(ReadOnlySpan<byte> input)
  105. {
  106. byte[] c2 = input.ToArray();
  107. ECMultiplier multiplier = CreateBasePointMultiplier();
  108. BigInteger k;
  109. ECPoint kPB;
  110. do
  111. {
  112. k = NextK();
  113. kPB = ((ECPublicKeyParameters)mECKey).Q.Multiply(k).Normalize();
  114. Kdf(mDigest, kPB, c2);
  115. }
  116. while (NotEncrypted(c2, input));
  117. ECPoint c1P = multiplier.Multiply(mECParams.G, k).Normalize();
  118. int c1PEncodedLength = c1P.GetEncodedLength(false);
  119. Span<byte> c1 = c1PEncodedLength <= 512
  120. ? stackalloc byte[c1PEncodedLength]
  121. : new byte[c1PEncodedLength];
  122. c1P.EncodeTo(false, c1);
  123. AddFieldElement(mDigest, kPB.AffineXCoord);
  124. mDigest.BlockUpdate(input);
  125. AddFieldElement(mDigest, kPB.AffineYCoord);
  126. int digestSize = mDigest.GetDigestSize();
  127. Span<byte> c3 = digestSize <= 128
  128. ? stackalloc byte[digestSize]
  129. : new byte[digestSize];
  130. mDigest.DoFinal(c3);
  131. switch (mMode)
  132. {
  133. case Mode.C1C3C2:
  134. return Arrays.Concatenate(c1, c3, c2);
  135. default:
  136. return Arrays.Concatenate(c1, c2, c3);
  137. }
  138. }
  139. private byte[] Decrypt(ReadOnlySpan<byte> input)
  140. {
  141. int c1Length = mCurveLength * 2 + 1;
  142. ECPoint c1P = mECParams.Curve.DecodePoint(input[..c1Length]);
  143. ECPoint s = c1P.Multiply(mECParams.H);
  144. if (s.IsInfinity)
  145. throw new InvalidCipherTextException("[h]C1 at infinity");
  146. c1P = c1P.Multiply(((ECPrivateKeyParameters)mECKey).D).Normalize();
  147. int digestSize = mDigest.GetDigestSize();
  148. int c2Length = input.Length - c1Length - digestSize;
  149. byte[] c2 = new byte[c2Length];
  150. if (mMode == Mode.C1C3C2)
  151. {
  152. input[(c1Length + digestSize)..].CopyTo(c2);
  153. }
  154. else
  155. {
  156. input[c1Length..(c1Length + c2Length)].CopyTo(c2);
  157. }
  158. Kdf(mDigest, c1P, c2);
  159. AddFieldElement(mDigest, c1P.AffineXCoord);
  160. mDigest.BlockUpdate(c2);
  161. AddFieldElement(mDigest, c1P.AffineYCoord);
  162. Span<byte> c3 = digestSize <= 128
  163. ? stackalloc byte[digestSize]
  164. : new byte[digestSize];
  165. mDigest.DoFinal(c3);
  166. int check = 0;
  167. if (mMode == Mode.C1C3C2)
  168. {
  169. for (int i = 0; i != c3.Length; i++)
  170. {
  171. check |= c3[i] ^ input[c1Length + i];
  172. }
  173. }
  174. else
  175. {
  176. for (int i = 0; i != c3.Length; i++)
  177. {
  178. check |= c3[i] ^ input[c1Length + c2.Length + i];
  179. }
  180. }
  181. c3.Fill(0);
  182. if (check != 0)
  183. {
  184. Arrays.Fill(c2, 0);
  185. throw new InvalidCipherTextException("invalid cipher text");
  186. }
  187. return c2;
  188. }
  189. private bool NotEncrypted(ReadOnlySpan<byte> encData, ReadOnlySpan<byte> input)
  190. {
  191. for (int i = 0; i != encData.Length; i++)
  192. {
  193. if (encData[i] != input[i])
  194. return false;
  195. }
  196. return true;
  197. }
  198. #else
  199. private byte[] Encrypt(byte[] input, int inOff, int inLen)
  200. {
  201. byte[] c2 = new byte[inLen];
  202. Array.Copy(input, inOff, c2, 0, c2.Length);
  203. ECMultiplier multiplier = CreateBasePointMultiplier();
  204. BigInteger k;
  205. ECPoint kPB;
  206. do
  207. {
  208. k = NextK();
  209. kPB = ((ECPublicKeyParameters)mECKey).Q.Multiply(k).Normalize();
  210. Kdf(mDigest, kPB, c2);
  211. }
  212. while (NotEncrypted(c2, input, inOff));
  213. ECPoint c1P = multiplier.Multiply(mECParams.G, k).Normalize();
  214. byte[] c1 = c1P.GetEncoded(false);
  215. AddFieldElement(mDigest, kPB.AffineXCoord);
  216. mDigest.BlockUpdate(input, inOff, inLen);
  217. AddFieldElement(mDigest, kPB.AffineYCoord);
  218. byte[] c3 = DigestUtilities.DoFinal(mDigest);
  219. switch (mMode)
  220. {
  221. case Mode.C1C3C2:
  222. return Arrays.ConcatenateAll(c1, c3, c2);
  223. default:
  224. return Arrays.ConcatenateAll(c1, c2, c3);
  225. }
  226. }
  227. private byte[] Decrypt(byte[] input, int inOff, int inLen)
  228. {
  229. byte[] c1 = new byte[mCurveLength * 2 + 1];
  230. Array.Copy(input, inOff, c1, 0, c1.Length);
  231. ECPoint c1P = mECParams.Curve.DecodePoint(c1);
  232. ECPoint s = c1P.Multiply(mECParams.H);
  233. if (s.IsInfinity)
  234. throw new InvalidCipherTextException("[h]C1 at infinity");
  235. c1P = c1P.Multiply(((ECPrivateKeyParameters)mECKey).D).Normalize();
  236. int digestSize = mDigest.GetDigestSize();
  237. byte[] c2 = new byte[inLen - c1.Length - digestSize];
  238. if (mMode == Mode.C1C3C2)
  239. {
  240. Array.Copy(input, inOff + c1.Length + digestSize, c2, 0, c2.Length);
  241. }
  242. else
  243. {
  244. Array.Copy(input, inOff + c1.Length, c2, 0, c2.Length);
  245. }
  246. Kdf(mDigest, c1P, c2);
  247. AddFieldElement(mDigest, c1P.AffineXCoord);
  248. mDigest.BlockUpdate(c2, 0, c2.Length);
  249. AddFieldElement(mDigest, c1P.AffineYCoord);
  250. byte[] c3 = DigestUtilities.DoFinal(mDigest);
  251. int check = 0;
  252. if (mMode == Mode.C1C3C2)
  253. {
  254. for (int i = 0; i != c3.Length; i++)
  255. {
  256. check |= c3[i] ^ input[inOff + c1.Length + i];
  257. }
  258. }
  259. else
  260. {
  261. for (int i = 0; i != c3.Length; i++)
  262. {
  263. check |= c3[i] ^ input[inOff + c1.Length + c2.Length + i];
  264. }
  265. }
  266. Arrays.Fill(c1, 0);
  267. Arrays.Fill(c3, 0);
  268. if (check != 0)
  269. {
  270. Arrays.Fill(c2, 0);
  271. throw new InvalidCipherTextException("invalid cipher text");
  272. }
  273. return c2;
  274. }
  275. private bool NotEncrypted(byte[] encData, byte[] input, int inOff)
  276. {
  277. for (int i = 0; i != encData.Length; i++)
  278. {
  279. if (encData[i] != input[inOff + i])
  280. return false;
  281. }
  282. return true;
  283. }
  284. #endif
  285. private void Kdf(IDigest digest, ECPoint c1, byte[] encData)
  286. {
  287. int digestSize = digest.GetDigestSize();
  288. int bufSize = System.Math.Max(4, digestSize);
  289. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  290. Span<byte> buf = bufSize <= 128
  291. ? stackalloc byte[bufSize]
  292. : new byte[bufSize];
  293. #else
  294. byte[] buf = new byte[bufSize];
  295. #endif
  296. int off = 0;
  297. IMemoable memo = digest as IMemoable;
  298. IMemoable copy = null;
  299. if (memo != null)
  300. {
  301. AddFieldElement(digest, c1.AffineXCoord);
  302. AddFieldElement(digest, c1.AffineYCoord);
  303. copy = memo.Copy();
  304. }
  305. uint ct = 0;
  306. while (off < encData.Length)
  307. {
  308. if (memo != null)
  309. {
  310. memo.Reset(copy);
  311. }
  312. else
  313. {
  314. AddFieldElement(digest, c1.AffineXCoord);
  315. AddFieldElement(digest, c1.AffineYCoord);
  316. }
  317. int xorLen = System.Math.Min(digestSize, encData.Length - off);
  318. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  319. Pack.UInt32_To_BE(++ct, buf);
  320. digest.BlockUpdate(buf[..4]);
  321. digest.DoFinal(buf);
  322. Xor(encData.AsSpan(off, xorLen), buf);
  323. #else
  324. Pack.UInt32_To_BE(++ct, buf, 0);
  325. digest.BlockUpdate(buf, 0, 4);
  326. digest.DoFinal(buf, 0);
  327. Xor(encData, buf, off, xorLen);
  328. #endif
  329. off += xorLen;
  330. }
  331. }
  332. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  333. private void Xor(Span<byte> data, ReadOnlySpan<byte> kdfOut)
  334. {
  335. for (int i = 0; i != data.Length; i++)
  336. {
  337. data[i] ^= kdfOut[i];
  338. }
  339. }
  340. #else
  341. private void Xor(byte[] data, byte[] kdfOut, int dOff, int dRemaining)
  342. {
  343. for (int i = 0; i != dRemaining; i++)
  344. {
  345. data[dOff + i] ^= kdfOut[i];
  346. }
  347. }
  348. #endif
  349. private BigInteger NextK()
  350. {
  351. int qBitLength = mECParams.N.BitLength;
  352. BigInteger k;
  353. do
  354. {
  355. k = new BigInteger(qBitLength, mRandom);
  356. }
  357. while (k.SignValue == 0 || k.CompareTo(mECParams.N) >= 0);
  358. return k;
  359. }
  360. private void AddFieldElement(IDigest digest, ECFieldElement v)
  361. {
  362. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  363. int encodedLength = v.GetEncodedLength();
  364. Span<byte> p = encodedLength <= 128
  365. ? stackalloc byte[encodedLength]
  366. : new byte[encodedLength];
  367. v.EncodeTo(p);
  368. digest.BlockUpdate(p);
  369. #else
  370. byte[] p = v.GetEncoded();
  371. digest.BlockUpdate(p, 0, p.Length);
  372. #endif
  373. }
  374. }
  375. }
  376. #pragma warning restore
  377. #endif