diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2022-03-12 00:39:34 +0700 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2022-03-12 00:39:34 +0700 |
commit | a172fd5b8b658a99e4ed8152b08ef3b0054de795 (patch) | |
tree | bca0d530a303df4a16bbc2069558f2742ecd4e91 /crypto | |
parent | RFC 8879 preliminaries (diff) | |
download | BouncyCastle.NET-ed25519-a172fd5b8b658a99e4ed8152b08ef3b0054de795.tar.xz |
Refactoring
Diffstat (limited to 'crypto')
4 files changed, 24 insertions, 30 deletions
diff --git a/crypto/src/tls/crypto/impl/bc/BcDefaultTlsCredentialedAgreement.cs b/crypto/src/tls/crypto/impl/bc/BcDefaultTlsCredentialedAgreement.cs index 15944cd89..863b96634 100644 --- a/crypto/src/tls/crypto/impl/bc/BcDefaultTlsCredentialedAgreement.cs +++ b/crypto/src/tls/crypto/impl/bc/BcDefaultTlsCredentialedAgreement.cs @@ -100,7 +100,7 @@ namespace Org.BouncyCastle.Tls.Crypto.Impl.BC { BcTlsCertificate bcCert = BcTlsCertificate.Convert(m_crypto, peerCertificate); ECPublicKeyParameters peerPublicKey = bcCert.GetPubKeyEC(); - return BcTlsECDomain.CalculateBasicAgreement(m_crypto, m_privateKey, peerPublicKey); + return BcTlsECDomain.CalculateECDHAgreement(m_crypto, m_privateKey, peerPublicKey); } public Certificate Certificate diff --git a/crypto/src/tls/crypto/impl/bc/BcTlsDH.cs b/crypto/src/tls/crypto/impl/bc/BcTlsDH.cs index 8af94f7c6..63fa00ce4 100644 --- a/crypto/src/tls/crypto/impl/bc/BcTlsDH.cs +++ b/crypto/src/tls/crypto/impl/bc/BcTlsDH.cs @@ -1,5 +1,4 @@ using System; -using System.IO; using Org.BouncyCastle.Crypto; using Org.BouncyCastle.Crypto.Parameters; @@ -20,7 +19,6 @@ namespace Org.BouncyCastle.Tls.Crypto.Impl.BC this.m_domain = domain; } - /// <exception cref="IOException"/> public virtual byte[] GenerateEphemeral() { this.m_localKeyPair = m_domain.GenerateKeyPair(); @@ -28,13 +26,11 @@ namespace Org.BouncyCastle.Tls.Crypto.Impl.BC return m_domain.EncodePublicKey((DHPublicKeyParameters)m_localKeyPair.Public); } - /// <exception cref="IOException"/> public virtual void ReceivePeerValue(byte[] peerValue) { this.m_peerPublicKey = m_domain.DecodePublicKey(peerValue); } - /// <exception cref="IOException"/> public virtual TlsSecret CalculateSecret() { return m_domain.CalculateDHAgreement((DHPrivateKeyParameters)m_localKeyPair.Private, m_peerPublicKey); diff --git a/crypto/src/tls/crypto/impl/bc/BcTlsDHDomain.cs b/crypto/src/tls/crypto/impl/bc/BcTlsDHDomain.cs index 90b8ce94f..faf6b4576 100644 --- a/crypto/src/tls/crypto/impl/bc/BcTlsDHDomain.cs +++ b/crypto/src/tls/crypto/impl/bc/BcTlsDHDomain.cs @@ -37,7 +37,7 @@ namespace Org.BouncyCastle.Tls.Crypto.Impl.BC return crypto.AdoptLocalSecret(secret); } - public static DHParameters GetParameters(TlsDHConfig dhConfig) + public static DHParameters GetDomainParameters(TlsDHConfig dhConfig) { DHGroup dhGroup = TlsDHUtilities.GetDHGroup(dhConfig); if (dhGroup == null) @@ -46,21 +46,21 @@ namespace Org.BouncyCastle.Tls.Crypto.Impl.BC return new DHParameters(dhGroup.P, dhGroup.G, dhGroup.Q, dhGroup.L); } - protected readonly BcTlsCrypto crypto; - protected readonly TlsDHConfig dhConfig; - protected readonly DHParameters dhParameters; + protected readonly BcTlsCrypto m_crypto; + protected readonly TlsDHConfig m_config; + protected readonly DHParameters m_domainParameters; public BcTlsDHDomain(BcTlsCrypto crypto, TlsDHConfig dhConfig) { - this.crypto = crypto; - this.dhConfig = dhConfig; - this.dhParameters = GetParameters(dhConfig); + this.m_crypto = crypto; + this.m_config = dhConfig; + this.m_domainParameters = GetDomainParameters(dhConfig); } public virtual BcTlsSecret CalculateDHAgreement(DHPrivateKeyParameters privateKey, DHPublicKeyParameters publicKey) { - return CalculateDHAgreement(crypto, privateKey, publicKey, dhConfig.IsPadded); + return CalculateDHAgreement(m_crypto, privateKey, publicKey, m_config.IsPadded); } public virtual TlsAgreement CreateDH() @@ -71,7 +71,7 @@ namespace Org.BouncyCastle.Tls.Crypto.Impl.BC /// <exception cref="IOException"/> public virtual BigInteger DecodeParameter(byte[] encoding) { - if (dhConfig.IsPadded && GetValueLength(dhParameters) != encoding.Length) + if (m_config.IsPadded && GetValueLength(m_domainParameters) != encoding.Length) throw new TlsFatalAlert(AlertDescription.illegal_parameter); return new BigInteger(1, encoding); @@ -89,7 +89,7 @@ namespace Org.BouncyCastle.Tls.Crypto.Impl.BC { BigInteger y = DecodeParameter(encoding); - return new DHPublicKeyParameters(y, dhParameters); + return new DHPublicKeyParameters(y, m_domainParameters); } catch (Exception e) { @@ -97,22 +97,20 @@ namespace Org.BouncyCastle.Tls.Crypto.Impl.BC } } - /// <exception cref="IOException"/> public virtual byte[] EncodeParameter(BigInteger x) { - return EncodeValue(dhParameters, dhConfig.IsPadded, x); + return EncodeValue(m_domainParameters, m_config.IsPadded, x); } - /// <exception cref="IOException"/> public virtual byte[] EncodePublicKey(DHPublicKeyParameters publicKey) { - return EncodeValue(dhParameters, true, publicKey.Y); + return EncodeValue(m_domainParameters, true, publicKey.Y); } public virtual AsymmetricCipherKeyPair GenerateKeyPair() { DHBasicKeyPairGenerator keyPairGenerator = new DHBasicKeyPairGenerator(); - keyPairGenerator.Init(new DHKeyGenerationParameters(crypto.SecureRandom, dhParameters)); + keyPairGenerator.Init(new DHKeyGenerationParameters(m_crypto.SecureRandom, m_domainParameters)); return keyPairGenerator.GenerateKeyPair(); } } diff --git a/crypto/src/tls/crypto/impl/bc/BcTlsECDomain.cs b/crypto/src/tls/crypto/impl/bc/BcTlsECDomain.cs index 61d11fb42..ab3481924 100644 --- a/crypto/src/tls/crypto/impl/bc/BcTlsECDomain.cs +++ b/crypto/src/tls/crypto/impl/bc/BcTlsECDomain.cs @@ -4,7 +4,6 @@ using System.IO; using Org.BouncyCastle.Asn1.X9; using Org.BouncyCastle.Crypto; using Org.BouncyCastle.Crypto.Agreement; -using Org.BouncyCastle.Crypto.EC; using Org.BouncyCastle.Crypto.Generators; using Org.BouncyCastle.Crypto.Parameters; using Org.BouncyCastle.Math; @@ -19,7 +18,7 @@ namespace Org.BouncyCastle.Tls.Crypto.Impl.BC public class BcTlsECDomain : TlsECDomain { - public static BcTlsSecret CalculateBasicAgreement(BcTlsCrypto crypto, ECPrivateKeyParameters privateKey, + public static BcTlsSecret CalculateECDHAgreement(BcTlsCrypto crypto, ECPrivateKeyParameters privateKey, ECPublicKeyParameters publicKey) { ECDHBasicAgreement basicAgreement = new ECDHBasicAgreement(); @@ -57,20 +56,20 @@ namespace Org.BouncyCastle.Tls.Crypto.Impl.BC } protected readonly BcTlsCrypto m_crypto; - protected readonly TlsECConfig m_ecConfig; - protected readonly ECDomainParameters m_ecDomainParameters; + protected readonly TlsECConfig m_config; + protected readonly ECDomainParameters m_domainParameters; public BcTlsECDomain(BcTlsCrypto crypto, TlsECConfig ecConfig) { this.m_crypto = crypto; - this.m_ecConfig = ecConfig; - this.m_ecDomainParameters = GetDomainParameters(ecConfig); + this.m_config = ecConfig; + this.m_domainParameters = GetDomainParameters(ecConfig); } public virtual BcTlsSecret CalculateECDHAgreement(ECPrivateKeyParameters privateKey, ECPublicKeyParameters publicKey) { - return CalculateBasicAgreement(m_crypto, privateKey, publicKey); + return CalculateECDHAgreement(m_crypto, privateKey, publicKey); } public virtual TlsAgreement CreateECDH() @@ -80,16 +79,17 @@ namespace Org.BouncyCastle.Tls.Crypto.Impl.BC public virtual ECPoint DecodePoint(byte[] encoding) { - return m_ecDomainParameters.Curve.DecodePoint(encoding); + return m_domainParameters.Curve.DecodePoint(encoding); } + /// <exception cref="IOException"/> public virtual ECPublicKeyParameters DecodePublicKey(byte[] encoding) { try { ECPoint point = DecodePoint(encoding); - return new ECPublicKeyParameters(point, m_ecDomainParameters); + return new ECPublicKeyParameters(point, m_domainParameters); } catch (IOException e) { @@ -114,7 +114,7 @@ namespace Org.BouncyCastle.Tls.Crypto.Impl.BC public virtual AsymmetricCipherKeyPair GenerateKeyPair() { ECKeyPairGenerator keyPairGenerator = new ECKeyPairGenerator(); - keyPairGenerator.Init(new ECKeyGenerationParameters(m_ecDomainParameters, m_crypto.SecureRandom)); + keyPairGenerator.Init(new ECKeyGenerationParameters(m_domainParameters, m_crypto.SecureRandom)); return keyPairGenerator.GenerateKeyPair(); } } |