| | | 1 | | using System; |
| | | 2 | | |
| | | 3 | | using Renci.SshNet.Security.Org.BouncyCastle.Crypto.Parameters; |
| | | 4 | | using Renci.SshNet.Security.Org.BouncyCastle.Math; |
| | | 5 | | using Renci.SshNet.Security.Org.BouncyCastle.Math.EC; |
| | | 6 | | using Renci.SshNet.Security.Org.BouncyCastle.Math.EC.Multiplier; |
| | | 7 | | using Renci.SshNet.Security.Org.BouncyCastle.Security; |
| | | 8 | | |
| | | 9 | | namespace Renci.SshNet.Security.Org.BouncyCastle.Crypto.Generators |
| | | 10 | | { |
| | | 11 | | internal class ECKeyPairGenerator |
| | | 12 | | : IAsymmetricCipherKeyPairGenerator |
| | | 13 | | { |
| | | 14 | | private readonly string algorithm; |
| | | 15 | | |
| | | 16 | | private ECDomainParameters parameters; |
| | | 17 | | private SecureRandom random; |
| | | 18 | | |
| | | 19 | | public ECKeyPairGenerator() |
| | 9 | 20 | | : this("EC") |
| | 9 | 21 | | { |
| | 9 | 22 | | } |
| | | 23 | | |
| | 9 | 24 | | public ECKeyPairGenerator( |
| | 9 | 25 | | string algorithm) |
| | 9 | 26 | | { |
| | 9 | 27 | | if (algorithm == null) |
| | 0 | 28 | | throw new ArgumentNullException("algorithm"); |
| | | 29 | | |
| | 9 | 30 | | this.algorithm = ECKeyParameters.VerifyAlgorithmName(algorithm); |
| | 9 | 31 | | } |
| | | 32 | | |
| | | 33 | | public void Init( |
| | | 34 | | KeyGenerationParameters parameters) |
| | 9 | 35 | | { |
| | 9 | 36 | | if (parameters is ECKeyGenerationParameters) |
| | 9 | 37 | | { |
| | 9 | 38 | | ECKeyGenerationParameters ecP = (ECKeyGenerationParameters) parameters; |
| | | 39 | | |
| | 9 | 40 | | this.parameters = ecP.DomainParameters; |
| | 9 | 41 | | } |
| | | 42 | | |
| | 9 | 43 | | this.random = parameters.Random; |
| | | 44 | | |
| | 9 | 45 | | if (this.random == null) |
| | 0 | 46 | | { |
| | 0 | 47 | | this.random = new SecureRandom(); |
| | 0 | 48 | | } |
| | 9 | 49 | | } |
| | | 50 | | |
| | | 51 | | public AsymmetricCipherKeyPair GenerateKeyPair() |
| | 9 | 52 | | { |
| | 9 | 53 | | BigInteger n = parameters.N; |
| | | 54 | | BigInteger d; |
| | 9 | 55 | | int minWeight = n.BitLength >> 2; |
| | | 56 | | |
| | | 57 | | for (;;) |
| | 9 | 58 | | { |
| | 9 | 59 | | d = new BigInteger(n.BitLength, random); |
| | | 60 | | |
| | 9 | 61 | | if (d.CompareTo(BigInteger.Two) < 0 || d.CompareTo(n) >= 0) |
| | 0 | 62 | | continue; |
| | | 63 | | |
| | 9 | 64 | | if (WNafUtilities.GetNafWeight(d) < minWeight) |
| | 0 | 65 | | continue; |
| | | 66 | | |
| | 9 | 67 | | break; |
| | | 68 | | } |
| | | 69 | | |
| | 9 | 70 | | ECPoint q = CreateBasePointMultiplier().Multiply(parameters.G, d); |
| | | 71 | | |
| | 9 | 72 | | return new AsymmetricCipherKeyPair( |
| | 9 | 73 | | new ECPublicKeyParameters(algorithm, q, parameters), |
| | 9 | 74 | | new ECPrivateKeyParameters(algorithm, d, parameters)); |
| | 9 | 75 | | } |
| | | 76 | | |
| | | 77 | | protected virtual ECMultiplier CreateBasePointMultiplier() |
| | 9 | 78 | | { |
| | 9 | 79 | | return new FixedPointCombMultiplier(); |
| | 9 | 80 | | } |
| | | 81 | | |
| | | 82 | | internal static ECPublicKeyParameters GetCorrespondingPublicKey( |
| | | 83 | | ECPrivateKeyParameters privKey) |
| | 0 | 84 | | { |
| | 0 | 85 | | ECDomainParameters ec = privKey.Parameters; |
| | 0 | 86 | | ECPoint q = new FixedPointCombMultiplier().Multiply(ec.G, privKey.D); |
| | | 87 | | |
| | 0 | 88 | | return new ECPublicKeyParameters(privKey.AlgorithmName, q, ec); |
| | 0 | 89 | | } |
| | | 90 | | } |
| | | 91 | | } |