F-5992: throw AEADBadTagException on AES-CCM tag failure
parent
d3d9bdf218
commit
4382551949
|
|
@ -1748,8 +1748,20 @@ public class WolfCryptCipher extends CipherSpi {
|
|||
tmpIn = Arrays.copyOfRange(tmpIn, 0,
|
||||
tmpIn.length - this.gcmTagLen);
|
||||
|
||||
tmpOut = this.aesCcm.decrypt(tmpIn, this.iv, tag,
|
||||
aad);
|
||||
try {
|
||||
tmpOut = this.aesCcm.decrypt(tmpIn, this.iv,
|
||||
tag, aad);
|
||||
|
||||
} catch (WolfCryptException e) {
|
||||
/* Convert to AEADBadTagException */
|
||||
if (e.getCode() ==
|
||||
WolfCryptError.AES_CCM_AUTH_E.getCode()) {
|
||||
/* Authentication check fail */
|
||||
throw new AEADBadTagException(
|
||||
e.getMessage());
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (cipherMode == CipherMode.WC_ECB) {
|
||||
|
|
|
|||
|
|
@ -3053,6 +3053,69 @@ public class WolfCryptCipherTest {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* AES-CCM decrypt failure should throw AEADBadTagException.
|
||||
*/
|
||||
@Test
|
||||
public void testAesCcmBadTagExceptionRegression()
|
||||
throws NoSuchProviderException, NoSuchAlgorithmException,
|
||||
NoSuchPaddingException, InvalidKeyException,
|
||||
IllegalBlockSizeException, InvalidAlgorithmParameterException,
|
||||
BadPaddingException {
|
||||
|
||||
if (!enabledJCEAlgos.contains("AES/CCM/NoPadding")) {
|
||||
/* skip if AES-CCM is not enabled */
|
||||
return;
|
||||
}
|
||||
|
||||
byte[] key = new byte[] {
|
||||
(byte)0x2b, (byte)0x7e, (byte)0x15, (byte)0x16,
|
||||
(byte)0x28, (byte)0xae, (byte)0xd2, (byte)0xa6,
|
||||
(byte)0xab, (byte)0xf7, (byte)0x15, (byte)0x88,
|
||||
(byte)0x09, (byte)0xcf, (byte)0x4f, (byte)0x3c
|
||||
};
|
||||
|
||||
byte[] nonce = new byte[] {
|
||||
(byte)0x00, (byte)0x01, (byte)0x02, (byte)0x03,
|
||||
(byte)0x04, (byte)0x05, (byte)0x06, (byte)0x07,
|
||||
(byte)0x08, (byte)0x09, (byte)0x0a, (byte)0x0b
|
||||
};
|
||||
|
||||
byte[] plaintext = new byte[] {
|
||||
(byte)0x48, (byte)0x65, (byte)0x6c, (byte)0x6c,
|
||||
(byte)0x6f, (byte)0x20, (byte)0x57, (byte)0x6f,
|
||||
(byte)0x72, (byte)0x6c, (byte)0x64, (byte)0x21
|
||||
};
|
||||
|
||||
Cipher cipher = Cipher.getInstance("AES/CCM/NoPadding", jceProvider);
|
||||
SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
|
||||
GCMParameterSpec ccmSpec = new GCMParameterSpec(128, nonce);
|
||||
|
||||
/* First encrypt to get valid ciphertext */
|
||||
cipher.init(Cipher.ENCRYPT_MODE, keySpec, ccmSpec);
|
||||
byte[] ciphertext = cipher.doFinal(plaintext);
|
||||
|
||||
/* Corrupt the authentication tag (last 16 bytes) */
|
||||
byte[] corruptedCiphertext = ciphertext.clone();
|
||||
int tagStart = corruptedCiphertext.length - 16;
|
||||
for (int i = tagStart; i < corruptedCiphertext.length; i++) {
|
||||
corruptedCiphertext[i] = (byte)0xFF;
|
||||
}
|
||||
|
||||
/* Attempt to decrypt with corrupted tag, should throw
|
||||
* AEADBadTagException */
|
||||
cipher.init(Cipher.DECRYPT_MODE, keySpec, ccmSpec);
|
||||
try {
|
||||
cipher.doFinal(corruptedCiphertext);
|
||||
fail("Expected AEADBadTagException for corrupted CCM tag");
|
||||
} catch (AEADBadTagException e) {
|
||||
/* Expected */
|
||||
} catch (Exception e) {
|
||||
fail("Expected AEADBadTagException but got: " +
|
||||
e.getClass().getSimpleName() + " - " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAesEcbNoPadding()
|
||||
throws NoSuchProviderException, NoSuchAlgorithmException,
|
||||
|
|
|
|||
Loading…
Reference in New Issue