From 14c59956fa16106639ca3e17a0c89c200f7f17f6 Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Mon, 26 Aug 2019 18:43:14 +0700 Subject: Check CCM tag length during initialization --- crypto/src/crypto/modes/CcmBlockCipher.cs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'crypto/src') diff --git a/crypto/src/crypto/modes/CcmBlockCipher.cs b/crypto/src/crypto/modes/CcmBlockCipher.cs index fa97ec840..0e4bb7fc9 100644 --- a/crypto/src/crypto/modes/CcmBlockCipher.cs +++ b/crypto/src/crypto/modes/CcmBlockCipher.cs @@ -68,7 +68,7 @@ namespace Org.BouncyCastle.Crypto.Modes nonce = param.GetNonce(); initialAssociatedText = param.GetAssociatedText(); - macSize = param.MacSize / 8; + macSize = GetMacSize(param.MacSize); cipherParameters = param.Key; } else if (parameters is ParametersWithIV) @@ -77,7 +77,7 @@ namespace Org.BouncyCastle.Crypto.Modes nonce = param.GetIV(); initialAssociatedText = null; - macSize = macBlock.Length / 2; + macSize = GetMacSize(64); cipherParameters = param.Parameters; } else @@ -434,6 +434,14 @@ namespace Org.BouncyCastle.Crypto.Modes return cMac.DoFinal(macBlock, 0); } + private int GetMacSize(int requestedMacBits) + { + if (requestedMacBits < 32 || requestedMacBits > 128 || 0 != (requestedMacBits & 15)) + throw new ArgumentException("tag length in octets must be one of {4,6,8,10,12,14,16}"); + + return requestedMacBits >> 3; + } + private int GetAssociatedTextLength() { return (int)associatedText.Length + ((initialAssociatedText == null) ? 0 : initialAssociatedText.Length); -- cgit 1.5.1