SM2.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // 麻省理工学院许可证
  2. //
  3. // 版权所有 (c) 2021-2023 zuohuaijun,大名科技(天津)有限公司 联系电话/微信:18020030720 QQ:515096995
  4. //
  5. // 特此免费授予获得本软件的任何人以处理本软件的权利,但须遵守以下条件:在所有副本或重要部分的软件中必须包括上述版权声明和本许可声明。
  6. //
  7. // 软件按“原样”提供,不提供任何形式的明示或暗示的保证,包括但不限于对适销性、适用性和非侵权的保证。
  8. // 在任何情况下,作者或版权持有人均不对任何索赔、损害或其他责任负责,无论是因合同、侵权或其他方式引起的,与软件或其使用或其他交易有关。
  9. using Org.BouncyCastle.Crypto.Generators;
  10. using Org.BouncyCastle.Crypto.Parameters;
  11. using Org.BouncyCastle.Math;
  12. using Org.BouncyCastle.Math.EC;
  13. using Org.BouncyCastle.Security;
  14. namespace Admin.NET.Core;
  15. public class SM2
  16. {
  17. public static SM2 Instance
  18. {
  19. get
  20. {
  21. return new SM2();
  22. }
  23. }
  24. public static SM2 InstanceTest
  25. {
  26. get
  27. {
  28. return new SM2();
  29. }
  30. }
  31. public static readonly string[] sm2_param = {
  32. "FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFF",// p,0
  33. "FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFC",// a,1
  34. "28E9FA9E9D9F5E344D5A9E4BCF6509A7F39789F515AB8F92DDBCBD414D940E93",// b,2
  35. "FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFF7203DF6B21C6052B53BBF40939D54123",// n,3
  36. "32C4AE2C1F1981195F9904466A39C9948FE30BBFF2660BE1715A4589334C74C7",// gx,4
  37. "BC3736A2F4F6779C59BDCEE36B692153D0A9877CC62A474002DF32E52139F0A0" // gy,5
  38. };
  39. public string[] ecc_param = sm2_param;
  40. public readonly BigInteger ecc_p;
  41. public readonly BigInteger ecc_a;
  42. public readonly BigInteger ecc_b;
  43. public readonly BigInteger ecc_n;
  44. public readonly BigInteger ecc_gx;
  45. public readonly BigInteger ecc_gy;
  46. public readonly ECCurve ecc_curve;
  47. public readonly ECPoint ecc_point_g;
  48. public readonly ECDomainParameters ecc_bc_spec;
  49. public readonly ECKeyPairGenerator ecc_key_pair_generator;
  50. private SM2()
  51. {
  52. ecc_param = sm2_param;
  53. ecc_p = new BigInteger(ecc_param[0], 16);
  54. ecc_a = new BigInteger(ecc_param[1], 16);
  55. ecc_b = new BigInteger(ecc_param[2], 16);
  56. ecc_n = new BigInteger(ecc_param[3], 16);
  57. ecc_gx = new BigInteger(ecc_param[4], 16);
  58. ecc_gy = new BigInteger(ecc_param[5], 16);
  59. ecc_curve = new FpCurve(ecc_p, ecc_a, ecc_b, null, null);
  60. ecc_point_g = ecc_curve.CreatePoint(ecc_gx, ecc_gy);
  61. ecc_bc_spec = new ECDomainParameters(ecc_curve, ecc_point_g, ecc_n);
  62. ECKeyGenerationParameters ecc_ecgenparam;
  63. ecc_ecgenparam = new ECKeyGenerationParameters(ecc_bc_spec, new SecureRandom());
  64. ecc_key_pair_generator = new ECKeyPairGenerator();
  65. ecc_key_pair_generator.Init(ecc_ecgenparam);
  66. }
  67. public virtual byte[] Sm2GetZ(byte[] userId, ECPoint userKey)
  68. {
  69. var sm3 = new SM3Digest();
  70. byte[] p;
  71. // userId length
  72. int len = userId.Length * 8;
  73. sm3.Update((byte)(len >> 8 & 0x00ff));
  74. sm3.Update((byte)(len & 0x00ff));
  75. // userId
  76. sm3.BlockUpdate(userId, 0, userId.Length);
  77. // a,b
  78. p = ecc_a.ToByteArray();
  79. sm3.BlockUpdate(p, 0, p.Length);
  80. p = ecc_b.ToByteArray();
  81. sm3.BlockUpdate(p, 0, p.Length);
  82. // gx,gy
  83. p = ecc_gx.ToByteArray();
  84. sm3.BlockUpdate(p, 0, p.Length);
  85. p = ecc_gy.ToByteArray();
  86. sm3.BlockUpdate(p, 0, p.Length);
  87. // x,y
  88. p = userKey.AffineXCoord.ToBigInteger().ToByteArray();
  89. sm3.BlockUpdate(p, 0, p.Length);
  90. p = userKey.AffineYCoord.ToBigInteger().ToByteArray();
  91. sm3.BlockUpdate(p, 0, p.Length);
  92. // Z
  93. byte[] md = new byte[sm3.GetDigestSize()];
  94. sm3.DoFinal(md, 0);
  95. return md;
  96. }
  97. }