EncryptThenAuthenticate implements Aead { @Override public byte[] decrypt(final byte[] ciphertext, final byte[] associatedData) throws GeneralSecurityException { if (ciphertext.length < macLength) { throw new GeneralSecurityException("ciphertext too short"); } byte[] rawCiphertext = Arrays.copyOfRange(ciphertext, 0, ciphertext.length - macLength); byte[] macValue = Arrays.copyOfRange(ciphertext, ciphertext.length - macLength, ciphertext.length); byte[] aad = associatedData; if (aad == null) { aad = new byte[0]; } byte[] aadLengthInBits = Arrays.copyOf(ByteBuffer.allocate(8).putLong(8L * aad.length).array(), 8); mac.verifyMac(macValue, Bytes.concat(aad, rawCiphertext, aadLengthInBits)); return cipher.decrypt(rawCiphertext); } EncryptThenAuthenticate(final IndCpaCipher cipher, final Mac mac, int macLength); static Aead newAesCtrHmac(
      final byte[] aesCtrKey, int ivSize, String hmacAlgorithm, final byte[] hmacKey, int tagSize); @Override byte[] encrypt(final byte[] plaintext, final byte[] associatedData); @Override byte[] decrypt(final byte[] ciphertext, final byte[] associatedData);  }
@Test public void testRFCVectors() throws Exception { for (int i = 0; i < rfcTestVectors.length; i++) { RFCTestVector t = rfcTestVectors[i]; if (Cipher.getMaxAllowedKeyLength("AES") < 256 && t.encKey.length > 16) { System.out.println( "Unlimited Strength Jurisdiction Policy Files are required" + " but not installed. Skip tests with keys larger than 128 bits."); continue; } Aead aead = getAead(t.macKey, t.encKey, t.ivSize, t.tagLength, t.macAlg); try { aead.decrypt(t.ciphertext, t.aad); } catch (GeneralSecurityException e) { fail("Ciphertext and aad are valid, shouldn't reach here: " + i + " " + e); } } }