From 65e6df94b8098e8b990b7f28668b5ff3169bc237 Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Sun, 13 Aug 2017 23:21:06 +0700 Subject: Fix exception type in DoFinal - reformatted and refactored --- crypto/src/crypto/macs/DSTU7564Mac.cs | 69 ++++++++++++++--------------------- 1 file changed, 28 insertions(+), 41 deletions(-) (limited to 'crypto') 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 { - /// - /// Implementation of DSTU7564 mac mode - /// - public class Dstu7564Mac : IMac - { - private Dstu7564Digest engine; - private int macSize; + /// + /// Implementation of DSTU7564 mac mode + /// + 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 -- cgit 1.4.1