Check CCM tag length during initialization
@@ -73,7 +73,7 @@ public void init(boolean forEncryption, CipherParameters params)
nonce = param.getNonce();
initialAssociatedText = param.getAssociatedText();
- macSize = param.getMacSize() / 8;
+ macSize = getMacSize(param.getMacSize());
cipherParameters = param.getKey();
}
else if (params instanceof ParametersWithIV)
@@ -82,7 +82,7 @@ else if (params instanceof ParametersWithIV)
nonce = param.getIV();
initialAssociatedText = null;
- macSize = macBlock.length / 2;
+ macSize = getMacSize(64);
cipherParameters = param.getParameters();
}
else
@@ -434,6 +434,16 @@ private int calculateMac(byte[] data, int dataOff, int dataLen, byte[] macBlock)
return cMac.doFinal(macBlock, 0);
}
+ private int getMacSize(int requestedMacBits)
+ {
+ if (requestedMacBits < 32 || requestedMacBits > 128 || 0 != (requestedMacBits & 15))
+ {
+ throw new IllegalArgumentException("tag length in octets must be one of {4,6,8,10,12,14,16}");
+ }
+
+ return requestedMacBits >>> 3;
+ }
+
private int getAssociatedTextLength()
{
return associatedText.size() + ((initialAssociatedText == null) ? 0 : initialAssociatedText.length);
