ECKeyPairGenerator.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. using System;
  3. using Org.BouncyCastle.Asn1;
  4. using Org.BouncyCastle.Asn1.Nist;
  5. using Org.BouncyCastle.Asn1.Sec;
  6. using Org.BouncyCastle.Asn1.TeleTrust;
  7. using Org.BouncyCastle.Asn1.X9;
  8. using Org.BouncyCastle.Crypto;
  9. using Org.BouncyCastle.Crypto.EC;
  10. using Org.BouncyCastle.Crypto.Parameters;
  11. using Org.BouncyCastle.Math;
  12. using Org.BouncyCastle.Math.EC;
  13. using Org.BouncyCastle.Math.EC.Multiplier;
  14. using Org.BouncyCastle.Security;
  15. using Org.BouncyCastle.Utilities;
  16. namespace Org.BouncyCastle.Crypto.Generators
  17. {
  18. public class ECKeyPairGenerator
  19. : IAsymmetricCipherKeyPairGenerator
  20. {
  21. private readonly string algorithm;
  22. private ECDomainParameters parameters;
  23. private DerObjectIdentifier publicKeyParamSet;
  24. private SecureRandom random;
  25. public ECKeyPairGenerator()
  26. : this("EC")
  27. {
  28. }
  29. public ECKeyPairGenerator(
  30. string algorithm)
  31. {
  32. if (algorithm == null)
  33. throw new ArgumentNullException("algorithm");
  34. this.algorithm = ECKeyParameters.VerifyAlgorithmName(algorithm);
  35. }
  36. public void Init(
  37. KeyGenerationParameters parameters)
  38. {
  39. if (parameters is ECKeyGenerationParameters)
  40. {
  41. ECKeyGenerationParameters ecP = (ECKeyGenerationParameters) parameters;
  42. this.publicKeyParamSet = ecP.PublicKeyParamSet;
  43. this.parameters = ecP.DomainParameters;
  44. }
  45. else
  46. {
  47. DerObjectIdentifier oid;
  48. switch (parameters.Strength)
  49. {
  50. case 192:
  51. oid = X9ObjectIdentifiers.Prime192v1;
  52. break;
  53. case 224:
  54. oid = SecObjectIdentifiers.SecP224r1;
  55. break;
  56. case 239:
  57. oid = X9ObjectIdentifiers.Prime239v1;
  58. break;
  59. case 256:
  60. oid = X9ObjectIdentifiers.Prime256v1;
  61. break;
  62. case 384:
  63. oid = SecObjectIdentifiers.SecP384r1;
  64. break;
  65. case 521:
  66. oid = SecObjectIdentifiers.SecP521r1;
  67. break;
  68. default:
  69. throw new InvalidParameterException("unknown key size.");
  70. }
  71. X9ECParameters ecps = FindECCurveByOid(oid);
  72. this.publicKeyParamSet = oid;
  73. this.parameters = new ECDomainParameters(
  74. ecps.Curve, ecps.G, ecps.N, ecps.H, ecps.GetSeed());
  75. }
  76. this.random = parameters.Random;
  77. if (this.random == null)
  78. {
  79. this.random = new SecureRandom();
  80. }
  81. }
  82. /**
  83. * Given the domain parameters this routine generates an EC key
  84. * pair in accordance with X9.62 section 5.2.1 pages 26, 27.
  85. */
  86. public AsymmetricCipherKeyPair GenerateKeyPair()
  87. {
  88. BigInteger n = parameters.N;
  89. BigInteger d;
  90. int minWeight = n.BitLength >> 2;
  91. for (;;)
  92. {
  93. d = new BigInteger(n.BitLength, random);
  94. if (d.CompareTo(BigInteger.Two) < 0 || d.CompareTo(n) >= 0)
  95. continue;
  96. if (WNafUtilities.GetNafWeight(d) < minWeight)
  97. continue;
  98. break;
  99. }
  100. ECPoint q = CreateBasePointMultiplier().Multiply(parameters.G, d);
  101. if (publicKeyParamSet != null)
  102. {
  103. return new AsymmetricCipherKeyPair(
  104. new ECPublicKeyParameters(algorithm, q, publicKeyParamSet),
  105. new ECPrivateKeyParameters(algorithm, d, publicKeyParamSet));
  106. }
  107. return new AsymmetricCipherKeyPair(
  108. new ECPublicKeyParameters(algorithm, q, parameters),
  109. new ECPrivateKeyParameters(algorithm, d, parameters));
  110. }
  111. protected virtual ECMultiplier CreateBasePointMultiplier()
  112. {
  113. return new FixedPointCombMultiplier();
  114. }
  115. internal static X9ECParameters FindECCurveByOid(DerObjectIdentifier oid)
  116. {
  117. // TODO ECGost3410NamedCurves support (returns ECDomainParameters though)
  118. X9ECParameters ecP = CustomNamedCurves.GetByOid(oid);
  119. if (ecP == null)
  120. {
  121. ecP = ECNamedCurveTable.GetByOid(oid);
  122. }
  123. return ecP;
  124. }
  125. internal static ECPublicKeyParameters GetCorrespondingPublicKey(
  126. ECPrivateKeyParameters privKey)
  127. {
  128. ECDomainParameters ec = privKey.Parameters;
  129. ECPoint q = new FixedPointCombMultiplier().Multiply(ec.G, privKey.D);
  130. if (privKey.PublicKeyParamSet != null)
  131. {
  132. return new ECPublicKeyParameters(privKey.AlgorithmName, q, privKey.PublicKeyParamSet);
  133. }
  134. return new ECPublicKeyParameters(privKey.AlgorithmName, q, ec);
  135. }
  136. }
  137. }
  138. #endif