diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2022-09-12 19:55:32 +0700 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2022-09-12 19:55:32 +0700 |
commit | a07a920f0e68f9234d8c4a29857ffbf4b106b7cd (patch) | |
tree | c9771154704b2cebf5d8bd398f1759b265caf750 | |
parent | Mark some classes sealed (diff) | |
download | BouncyCastle.NET-ed25519-a07a920f0e68f9234d8c4a29857ffbf4b106b7cd.tar.xz |
GetUnderlyingCipher() => UnderlyingCipher
-rw-r--r-- | crypto/src/crypto/encodings/ISO9796d1Encoding.cs | 26 | ||||
-rw-r--r-- | crypto/src/crypto/encodings/OaepEncoding.cs | 21 | ||||
-rw-r--r-- | crypto/src/crypto/encodings/Pkcs1Encoding.cs | 18 | ||||
-rw-r--r-- | crypto/src/crypto/macs/GMac.cs | 2 | ||||
-rw-r--r-- | crypto/src/crypto/modes/CcmBlockCipher.cs | 40 | ||||
-rw-r--r-- | crypto/src/crypto/modes/EAXBlockCipher.cs | 34 | ||||
-rw-r--r-- | crypto/src/crypto/modes/GCMBlockCipher.cs | 10 | ||||
-rw-r--r-- | crypto/src/crypto/modes/GcmSivBlockCipher.cs | 21 | ||||
-rw-r--r-- | crypto/src/crypto/modes/IAeadBlockCipher.cs | 2 | ||||
-rw-r--r-- | crypto/src/crypto/modes/KCcmBlockCipher.cs | 13 | ||||
-rw-r--r-- | crypto/src/crypto/modes/OCBBlockCipher.cs | 18 | ||||
-rw-r--r-- | crypto/test/src/crypto/test/RSABlindedTest.cs | 4 | ||||
-rw-r--r-- | crypto/test/src/crypto/test/RsaTest.cs | 6 |
13 files changed, 65 insertions, 150 deletions
diff --git a/crypto/src/crypto/encodings/ISO9796d1Encoding.cs b/crypto/src/crypto/encodings/ISO9796d1Encoding.cs index 30e988356..c08de1f9a 100644 --- a/crypto/src/crypto/encodings/ISO9796d1Encoding.cs +++ b/crypto/src/crypto/encodings/ISO9796d1Encoding.cs @@ -29,31 +29,21 @@ namespace Org.BouncyCastle.Crypto.Encodings private int padBits = 0; private BigInteger modulus; - public ISO9796d1Encoding( - IAsymmetricBlockCipher cipher) + public ISO9796d1Encoding(IAsymmetricBlockCipher cipher) { this.engine = cipher; } - public string AlgorithmName - { - get { return engine.AlgorithmName + "/ISO9796-1Padding"; } - } + public string AlgorithmName => engine.AlgorithmName + "/ISO9796-1Padding"; - public IAsymmetricBlockCipher GetUnderlyingCipher() - { - return engine; - } + public IAsymmetricBlockCipher UnderlyingCipher => engine; - public void Init( - bool forEncryption, - ICipherParameters parameters) - { - RsaKeyParameters kParam; - if (parameters is ParametersWithRandom) + public void Init(bool forEncryption, ICipherParameters parameters) + { + RsaKeyParameters kParam; + if (parameters is ParametersWithRandom withRandom) { - ParametersWithRandom rParam = (ParametersWithRandom)parameters; - kParam = (RsaKeyParameters)rParam.Parameters; + kParam = (RsaKeyParameters)withRandom.Parameters; } else { diff --git a/crypto/src/crypto/encodings/OaepEncoding.cs b/crypto/src/crypto/encodings/OaepEncoding.cs index f3550b9cd..af83a6f77 100644 --- a/crypto/src/crypto/encodings/OaepEncoding.cs +++ b/crypto/src/crypto/encodings/OaepEncoding.cs @@ -62,31 +62,22 @@ namespace Org.BouncyCastle.Crypto.Encodings hash.DoFinal(defHash, 0); } - public IAsymmetricBlockCipher GetUnderlyingCipher() - { - return engine; - } + public string AlgorithmName => engine.AlgorithmName + "/OAEPPadding"; - public string AlgorithmName - { - get { return engine.AlgorithmName + "/OAEPPadding"; } - } + public IAsymmetricBlockCipher UnderlyingCipher => engine; - public void Init( - bool forEncryption, - ICipherParameters param) + public void Init(bool forEncryption, ICipherParameters parameters) { - if (param is ParametersWithRandom) + if (parameters is ParametersWithRandom withRandom) { - ParametersWithRandom rParam = (ParametersWithRandom)param; - this.random = rParam.Random; + this.random = withRandom.Random; } else { this.random = new SecureRandom(); } - engine.Init(forEncryption, param); + engine.Init(forEncryption, parameters); this.forEncryption = forEncryption; } diff --git a/crypto/src/crypto/encodings/Pkcs1Encoding.cs b/crypto/src/crypto/encodings/Pkcs1Encoding.cs index 53c046a8a..5faf7f385 100644 --- a/crypto/src/crypto/encodings/Pkcs1Encoding.cs +++ b/crypto/src/crypto/encodings/Pkcs1Encoding.cs @@ -96,25 +96,17 @@ namespace Org.BouncyCastle.Crypto.Encodings this.pLen = fallback.Length; } - public IAsymmetricBlockCipher GetUnderlyingCipher() - { - return engine; - } + public string AlgorithmName => engine.AlgorithmName + "/PKCS1Padding"; - public string AlgorithmName - { - get { return engine.AlgorithmName + "/PKCS1Padding"; } - } + public IAsymmetricBlockCipher UnderlyingCipher => engine; public void Init(bool forEncryption, ICipherParameters parameters) { AsymmetricKeyParameter kParam; - if (parameters is ParametersWithRandom) + if (parameters is ParametersWithRandom withRandom) { - ParametersWithRandom rParam = (ParametersWithRandom)parameters; - - this.random = rParam.Random; - kParam = (AsymmetricKeyParameter)rParam.Parameters; + this.random = withRandom.Random; + kParam = (AsymmetricKeyParameter)withRandom.Parameters; } else { diff --git a/crypto/src/crypto/macs/GMac.cs b/crypto/src/crypto/macs/GMac.cs index e1555f1e6..78c51b1ec 100644 --- a/crypto/src/crypto/macs/GMac.cs +++ b/crypto/src/crypto/macs/GMac.cs @@ -68,7 +68,7 @@ namespace Org.BouncyCastle.Crypto.Macs public string AlgorithmName { - get { return cipher.GetUnderlyingCipher().AlgorithmName + "-GMAC"; } + get { return cipher.UnderlyingCipher.AlgorithmName + "-GMAC"; } } public int GetMacSize() diff --git a/crypto/src/crypto/modes/CcmBlockCipher.cs b/crypto/src/crypto/modes/CcmBlockCipher.cs index fdd664a32..3569ac2b7 100644 --- a/crypto/src/crypto/modes/CcmBlockCipher.cs +++ b/crypto/src/crypto/modes/CcmBlockCipher.cs @@ -49,35 +49,26 @@ namespace Org.BouncyCastle.Crypto.Modes * * @return the underlying block cipher that we are wrapping. */ - public virtual IBlockCipher GetUnderlyingCipher() - { - return cipher; - } + public virtual IBlockCipher UnderlyingCipher => cipher; - public virtual void Init( - bool forEncryption, - ICipherParameters parameters) + public virtual void Init(bool forEncryption, ICipherParameters parameters) { this.forEncryption = forEncryption; ICipherParameters cipherParameters; - if (parameters is AeadParameters) + if (parameters is AeadParameters aeadParameters) { - AeadParameters param = (AeadParameters) parameters; - - nonce = param.GetNonce(); - initialAssociatedText = param.GetAssociatedText(); - macSize = GetMacSize(forEncryption, param.MacSize); - cipherParameters = param.Key; + nonce = aeadParameters.GetNonce(); + initialAssociatedText = aeadParameters.GetAssociatedText(); + macSize = GetMacSize(forEncryption, aeadParameters.MacSize); + cipherParameters = aeadParameters.Key; } - else if (parameters is ParametersWithIV) + else if (parameters is ParametersWithIV parametersWithIV) { - ParametersWithIV param = (ParametersWithIV) parameters; - - nonce = param.GetIV(); + nonce = parametersWithIV.GetIV(); initialAssociatedText = null; macSize = GetMacSize(forEncryption, 64); - cipherParameters = param.Parameters; + cipherParameters = parametersWithIV.Parameters; } else { @@ -96,10 +87,7 @@ namespace Org.BouncyCastle.Crypto.Modes Reset(); } - public virtual string AlgorithmName - { - get { return cipher.AlgorithmName + "/CCM"; } - } + public virtual string AlgorithmName => cipher.AlgorithmName + "/CCM"; public virtual int GetBlockSize() { @@ -206,14 +194,12 @@ namespace Org.BouncyCastle.Crypto.Modes return Arrays.CopyOfRange(macBlock, 0, macSize); } - public virtual int GetUpdateOutputSize( - int len) + public virtual int GetUpdateOutputSize(int len) { return 0; } - public virtual int GetOutputSize( - int len) + public virtual int GetOutputSize(int len) { int totalData = Convert.ToInt32(data.Length) + len; diff --git a/crypto/src/crypto/modes/EAXBlockCipher.cs b/crypto/src/crypto/modes/EAXBlockCipher.cs index acf33ccb7..5d7f05d02 100644 --- a/crypto/src/crypto/modes/EAXBlockCipher.cs +++ b/crypto/src/crypto/modes/EAXBlockCipher.cs @@ -59,47 +59,35 @@ namespace Org.BouncyCastle.Crypto.Modes this.cipher = new SicBlockCipher(cipher); } - public virtual string AlgorithmName - { - get { return cipher.UnderlyingCipher.AlgorithmName + "/EAX"; } - } + public virtual string AlgorithmName => cipher.UnderlyingCipher.AlgorithmName + "/EAX"; - public virtual IBlockCipher GetUnderlyingCipher() - { - return cipher; - } + public virtual IBlockCipher UnderlyingCipher => cipher; public virtual int GetBlockSize() { return cipher.GetBlockSize(); } - public virtual void Init( - bool forEncryption, - ICipherParameters parameters) + public virtual void Init(bool forEncryption, ICipherParameters parameters) { this.forEncryption = forEncryption; byte[] nonce; ICipherParameters keyParam; - if (parameters is AeadParameters) + if (parameters is AeadParameters aeadParameters) { - AeadParameters param = (AeadParameters) parameters; - - nonce = param.GetNonce(); - initialAssociatedText = param.GetAssociatedText(); - macSize = param.MacSize / 8; - keyParam = param.Key; + nonce = aeadParameters.GetNonce(); + initialAssociatedText = aeadParameters.GetAssociatedText(); + macSize = aeadParameters.MacSize / 8; + keyParam = aeadParameters.Key; } - else if (parameters is ParametersWithIV) + else if (parameters is ParametersWithIV parametersWithIV) { - ParametersWithIV param = (ParametersWithIV) parameters; - - nonce = param.GetIV(); + nonce = parametersWithIV.GetIV(); initialAssociatedText = null; macSize = mac.GetMacSize() / 2; - keyParam = param.Parameters; + keyParam = parametersWithIV.Parameters; } else { diff --git a/crypto/src/crypto/modes/GCMBlockCipher.cs b/crypto/src/crypto/modes/GCMBlockCipher.cs index 538e0e749..c19f5f904 100644 --- a/crypto/src/crypto/modes/GCMBlockCipher.cs +++ b/crypto/src/crypto/modes/GCMBlockCipher.cs @@ -86,15 +86,9 @@ namespace Org.BouncyCastle.Crypto.Modes this.multiplier = m; } - public string AlgorithmName - { - get { return cipher.AlgorithmName + "/GCM"; } - } + public string AlgorithmName => cipher.AlgorithmName + "/GCM"; - public IBlockCipher GetUnderlyingCipher() - { - return cipher; - } + public IBlockCipher UnderlyingCipher => cipher; public int GetBlockSize() { diff --git a/crypto/src/crypto/modes/GcmSivBlockCipher.cs b/crypto/src/crypto/modes/GcmSivBlockCipher.cs index 2abe5eece..14f908da3 100644 --- a/crypto/src/crypto/modes/GcmSivBlockCipher.cs +++ b/crypto/src/crypto/modes/GcmSivBlockCipher.cs @@ -155,10 +155,7 @@ namespace Org.BouncyCastle.Crypto.Modes theDataHasher = new GcmSivHasher(this); } - public virtual IBlockCipher GetUnderlyingCipher() - { - return theCipher; - } + public virtual IBlockCipher UnderlyingCipher => theCipher; public virtual int GetBlockSize() { @@ -169,8 +166,8 @@ namespace Org.BouncyCastle.Crypto.Modes { /* Set defaults */ byte[] myInitialAEAD = null; - byte[] myNonce = null; - KeyParameter myKey = null; + byte[] myNonce; + KeyParameter myKey; /* Access parameters */ if (cipherParameters is AeadParameters) @@ -205,8 +202,7 @@ namespace Org.BouncyCastle.Crypto.Modes byte[] k = myKey.GetKey(); - if (k.Length != BUFLEN - && k.Length != (BUFLEN << 1)) + if (k.Length != BUFLEN && k.Length != (BUFLEN << 1)) { throw new ArgumentException("Invalid key"); } @@ -217,14 +213,11 @@ namespace Org.BouncyCastle.Crypto.Modes theNonce = myNonce; /* Initialise the keys */ - deriveKeys(myKey); + DeriveKeys(myKey); ResetStreams(); } - public virtual string AlgorithmName - { - get { return theCipher.AlgorithmName + "-GCM-SIV"; } - } + public virtual string AlgorithmName => theCipher.AlgorithmName + "-GCM-SIV"; /** * check AEAD status. @@ -842,7 +835,7 @@ namespace Org.BouncyCastle.Crypto.Modes * Derive Keys. * @param pKey the keyGeneration key */ - private void deriveKeys(KeyParameter pKey) + private void DeriveKeys(KeyParameter pKey) { /* Create the buffers */ byte[] myIn = new byte[BUFLEN]; diff --git a/crypto/src/crypto/modes/IAeadBlockCipher.cs b/crypto/src/crypto/modes/IAeadBlockCipher.cs index ebe5ef234..a4dc0857c 100644 --- a/crypto/src/crypto/modes/IAeadBlockCipher.cs +++ b/crypto/src/crypto/modes/IAeadBlockCipher.cs @@ -10,6 +10,6 @@ namespace Org.BouncyCastle.Crypto.Modes int GetBlockSize(); /// <summary>The block cipher underlying this algorithm.</summary> - IBlockCipher GetUnderlyingCipher(); + IBlockCipher UnderlyingCipher { get; } } } diff --git a/crypto/src/crypto/modes/KCcmBlockCipher.cs b/crypto/src/crypto/modes/KCcmBlockCipher.cs index 493bf56e1..a733379cc 100644 --- a/crypto/src/crypto/modes/KCcmBlockCipher.cs +++ b/crypto/src/crypto/modes/KCcmBlockCipher.cs @@ -131,23 +131,14 @@ namespace Org.BouncyCastle.Crypto.Modes } } - public virtual string AlgorithmName - { - get - { - return engine.AlgorithmName + "/KCCM"; - } - } + public virtual string AlgorithmName => engine.AlgorithmName + "/KCCM"; public virtual int GetBlockSize() { return engine.GetBlockSize(); } - public virtual IBlockCipher GetUnderlyingCipher() - { - return engine; - } + public virtual IBlockCipher UnderlyingCipher => engine; public virtual void ProcessAadByte(byte input) { diff --git a/crypto/src/crypto/modes/OCBBlockCipher.cs b/crypto/src/crypto/modes/OCBBlockCipher.cs index ee327f200..9ba60dd49 100644 --- a/crypto/src/crypto/modes/OCBBlockCipher.cs +++ b/crypto/src/crypto/modes/OCBBlockCipher.cs @@ -80,15 +80,9 @@ namespace Org.BouncyCastle.Crypto.Modes this.mainCipher = mainCipher; } - public virtual IBlockCipher GetUnderlyingCipher() - { - return mainCipher; - } + public virtual string AlgorithmName => mainCipher.AlgorithmName + "/OCB"; - public virtual string AlgorithmName - { - get { return mainCipher.AlgorithmName + "/OCB"; } - } + public virtual IBlockCipher UnderlyingCipher => mainCipher; public virtual void Init(bool forEncryption, ICipherParameters parameters) { @@ -99,10 +93,8 @@ namespace Org.BouncyCastle.Crypto.Modes KeyParameter keyParameter; byte[] N; - if (parameters is AeadParameters) + if (parameters is AeadParameters aeadParameters) { - AeadParameters aeadParameters = (AeadParameters) parameters; - N = aeadParameters.GetNonce(); initialAssociatedText = aeadParameters.GetAssociatedText(); @@ -113,10 +105,8 @@ namespace Org.BouncyCastle.Crypto.Modes macSize = macSizeBits / 8; keyParameter = aeadParameters.Key; } - else if (parameters is ParametersWithIV) + else if (parameters is ParametersWithIV parametersWithIV) { - ParametersWithIV parametersWithIV = (ParametersWithIV) parameters; - N = parametersWithIV.GetIV(); initialAssociatedText = null; macSize = 16; diff --git a/crypto/test/src/crypto/test/RSABlindedTest.cs b/crypto/test/src/crypto/test/RSABlindedTest.cs index b6e8eb0ea..99d4a9deb 100644 --- a/crypto/test/src/crypto/test/RSABlindedTest.cs +++ b/crypto/test/src/crypto/test/RSABlindedTest.cs @@ -267,7 +267,7 @@ namespace Org.BouncyCastle.Crypto.Tests eng.Init(true, pubParameters); - if (eng.GetOutputBlockSize() != ((Pkcs1Encoding)eng).GetUnderlyingCipher().GetOutputBlockSize()) + if (eng.GetOutputBlockSize() != ((Pkcs1Encoding)eng).UnderlyingCipher.GetOutputBlockSize()) { Fail("PKCS1 output block size incorrect"); } @@ -300,7 +300,7 @@ namespace Org.BouncyCastle.Crypto.Tests // // PKCS1 - private encrypt, public decrypt // - eng = new Pkcs1Encoding(((Pkcs1Encoding)eng).GetUnderlyingCipher()); + eng = new Pkcs1Encoding(((Pkcs1Encoding)eng).UnderlyingCipher); eng.Init(true, privParameters); diff --git a/crypto/test/src/crypto/test/RsaTest.cs b/crypto/test/src/crypto/test/RsaTest.cs index c8945b49f..3c01baa85 100644 --- a/crypto/test/src/crypto/test/RsaTest.cs +++ b/crypto/test/src/crypto/test/RsaTest.cs @@ -398,7 +398,7 @@ namespace Org.BouncyCastle.Crypto.Tests eng.Init(true, encParameters); - if (eng.GetOutputBlockSize() != ((Pkcs1Encoding)eng).GetUnderlyingCipher().GetOutputBlockSize()) + if (eng.GetOutputBlockSize() != ((Pkcs1Encoding)eng).UnderlyingCipher.GetOutputBlockSize()) { Fail("PKCS1 output block size incorrect"); } @@ -526,7 +526,7 @@ namespace Org.BouncyCastle.Crypto.Tests eng.Init(true, pubParameters); - if (eng.GetOutputBlockSize() != ((Pkcs1Encoding)eng).GetUnderlyingCipher().GetOutputBlockSize()) + if (eng.GetOutputBlockSize() != ((Pkcs1Encoding)eng).UnderlyingCipher.GetOutputBlockSize()) { Fail("PKCS1 output block size incorrect"); } @@ -594,7 +594,7 @@ namespace Org.BouncyCastle.Crypto.Tests // // PKCS1 - private encrypt, public decrypt // - eng = new Pkcs1Encoding(((Pkcs1Encoding)eng).GetUnderlyingCipher()); + eng = new Pkcs1Encoding(((Pkcs1Encoding)eng).UnderlyingCipher); eng.Init(true, privParameters); |