diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2017-08-13 23:21:06 +0700 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2017-08-13 23:21:06 +0700 |
commit | 65e6df94b8098e8b990b7f28668b5ff3169bc237 (patch) | |
tree | f1c9bdddf49791458ad5acf1884ecc48be80d482 | |
parent | Fix Dstu7624WrapEngine to respect input offsets and lengths (diff) | |
download | BouncyCastle.NET-ed25519-65e6df94b8098e8b990b7f28668b5ff3169bc237.tar.xz |
Fix exception type in DoFinal
- reformatted and refactored
-rw-r--r-- | crypto/src/crypto/macs/DSTU7564Mac.cs | 69 |
1 files changed, 28 insertions, 41 deletions
diff --git a/crypto/src/crypto/macs/DSTU7564Mac.cs b/crypto/src/crypto/macs/DSTU7564Mac.cs index 907355487..36e86418a 100644 --- a/crypto/src/crypto/macs/DSTU7564Mac.cs +++ b/crypto/src/crypto/macs/DSTU7564Mac.cs @@ -6,38 +6,35 @@ using Org.BouncyCastle.Crypto.Utilities; namespace Org.BouncyCastle.Crypto.Macs { - /// <summary> - /// Implementation of DSTU7564 mac mode - /// </summary> - public class Dstu7564Mac : IMac - { - private Dstu7564Digest engine; - private int macSize; + /// <summary> + /// Implementation of DSTU7564 mac mode + /// </summary> + public class Dstu7564Mac + : IMac + { + private Dstu7564Digest engine; + private int macSize; private ulong inputLength; byte[] paddedKey; - byte[] invertedKey; - byte[] paddedInput; + byte[] invertedKey; public string AlgorithmName { - get - { - return "DSTU7564Mac"; - } + get { return "DSTU7564Mac"; } } public Dstu7564Mac(int macSizeBits) - { + { engine = new Dstu7564Digest(macSizeBits); macSize = macSizeBits / 8; - } + } - public void Init(ICipherParameters parameters) - { - if (parameters is KeyParameter) + public void Init(ICipherParameters parameters) { + if (parameters is KeyParameter) + { byte[] key = ((KeyParameter)parameters).GetKey(); invertedKey = new byte[key.Length]; @@ -49,35 +46,29 @@ namespace Org.BouncyCastle.Crypto.Macs invertedKey[byteIndex] = (byte)(key[byteIndex] ^ (byte)0xFF); } } - else - { + else + { throw new ArgumentException("Bad parameter passed"); } engine.BlockUpdate(paddedKey, 0, paddedKey.Length); } - public int GetMacSize() - { - return macSize; - } + public int GetMacSize() + { + return macSize; + } - public void BlockUpdate(byte[] input, int inOff, int len) - { - if (input.Length - inOff < len) - { - throw new DataLengthException("Input buffer too short"); - } + public void BlockUpdate(byte[] input, int inOff, int len) + { + Check.DataLength(input, inOff, len, "Input buffer too short"); if (paddedKey == null) - { throw new InvalidOperationException(AlgorithmName + " not initialised"); - } engine.BlockUpdate(input, inOff, len); inputLength += (ulong)len; - - } + } public void Update(byte input) { @@ -87,14 +78,10 @@ namespace Org.BouncyCastle.Crypto.Macs public int DoFinal(byte[] output, int outOff) { - if (output.Length - outOff < macSize) - { - throw new DataLengthException("Output buffer too short"); - } + Check.OutputLength(output, outOff, macSize, "Output buffer too short"); + if (paddedKey == null) - { throw new InvalidOperationException(AlgorithmName + " not initialised"); - } Pad(); @@ -135,7 +122,7 @@ namespace Org.BouncyCastle.Crypto.Macs private byte[] PadKey(byte[] input) { - int paddedLen = ((input.Length + engine.GetByteLength() - 1) / engine.GetByteLength()) *engine.GetByteLength(); + int paddedLen = ((input.Length + engine.GetByteLength() - 1) / engine.GetByteLength()) * engine.GetByteLength(); int extra = engine.GetByteLength() - (int)(input.Length % engine.GetByteLength()); if (extra < 13) // terminator byte + 96 bits of length |