Path: blob/master/test/jdk/com/sun/crypto/provider/Cipher/AEAD/WrongAAD.java
41161 views
/*1* Copyright (c) 2007, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.io.ByteArrayInputStream;24import java.io.ByteArrayOutputStream;25import java.io.IOException;26import java.security.AlgorithmParameters;27import java.security.InvalidAlgorithmParameterException;28import java.security.InvalidKeyException;29import java.security.NoSuchAlgorithmException;30import java.security.NoSuchProviderException;31import java.util.Arrays;32import javax.crypto.CipherInputStream;33import javax.crypto.CipherOutputStream;34import javax.crypto.NoSuchPaddingException;35import javax.crypto.SecretKey;36import javax.crypto.Cipher;37import javax.crypto.KeyGenerator;3839/*40* @test41* @bug 804859642* @summary Check if wrong or empty AAD is rejected43*/44public class WrongAAD {4546private static final String PROVIDER = "SunJCE";47private static final String TRANSFORMATION = "AES/GCM/NoPadding";48private static final int TEXT_SIZE = 800;49private static final int KEY_SIZE = 128;50private static final int AAD_SIZE = 128;5152private final SecretKey key;53private final byte[] plainText;54private final Cipher encryptCipher;5556public WrongAAD() throws Exception {57// init a secret key58KeyGenerator kg = KeyGenerator.getInstance("AES", PROVIDER);59kg.init(KEY_SIZE);60key = kg.generateKey();6162// generate a plain text63plainText = Helper.generateBytes(TEXT_SIZE);6465// init AADs66byte[] AAD = Helper.generateBytes(AAD_SIZE);6768// init a cipher69encryptCipher = createCipher(Cipher.ENCRYPT_MODE, null);70encryptCipher.updateAAD(AAD);71}7273public static void main(String[] args) throws Exception {74WrongAAD test = new WrongAAD();75test.decryptWithEmptyAAD();76test.decryptWithWrongAAD();77}7879/*80* Attempt to decrypt a cipher text using Cipher object81* initialized without AAD used for encryption.82*/83private void decryptWithEmptyAAD() throws Exception {84System.out.println("decryptWithEmptyAAD() started");85// initialize it with empty AAD to get exception during decryption86Cipher decryptCipher = createCipher(Cipher.DECRYPT_MODE,87encryptCipher.getParameters());88try (ByteArrayOutputStream baOutput = new ByteArrayOutputStream();89CipherOutputStream ciOutput = new CipherOutputStream(baOutput,90decryptCipher)) {91if (decrypt(ciOutput, baOutput)) {92throw new RuntimeException(93"Decryption has been perfomed successfully in"94+ " spite of the decrypt Cipher has NOT been"95+ " initialized with AAD");96}97}98System.out.println("decryptWithEmptyAAD() passed");99}100101/*102* Attempt to decrypt the cipher text using Cipher object103* initialized with some fake AAD.104*/105private void decryptWithWrongAAD() throws Exception {106System.out.println("decrypt with wrong AAD");107108// initialize it with wrong AAD to get an exception during decryption109Cipher decryptCipher = createCipher(Cipher.DECRYPT_MODE,110encryptCipher.getParameters());111byte[] someAAD = Helper.generateBytes(AAD_SIZE + 1);112decryptCipher.updateAAD(someAAD);113114// init output stream115try (ByteArrayOutputStream baOutput = new ByteArrayOutputStream();116CipherOutputStream ciOutput = new CipherOutputStream(baOutput,117decryptCipher);) {118if (decrypt(ciOutput, baOutput)) {119throw new RuntimeException(120"A decryption has been perfomed successfully in"121+ " spite of the decrypt Cipher has been"122+ " initialized with fake AAD");123}124}125126System.out.println("Passed");127}128129private boolean decrypt(CipherOutputStream ciOutput,130ByteArrayOutputStream baOutput) throws IOException {131try (ByteArrayInputStream baInput = new ByteArrayInputStream(plainText);132CipherInputStream ciInput = new CipherInputStream(baInput,133encryptCipher)) {134byte[] buffer = new byte[TEXT_SIZE];135int len = ciInput.read(buffer);136137while (len != -1) {138ciOutput.write(buffer, 0, len);139len = ciInput.read(buffer);140}141ciOutput.flush();142byte[] recoveredText = baOutput.toByteArray();143System.out.println("recoveredText: " + new String(recoveredText));144145/*146* See bug 8012900, AEADBadTagException is swalloed by CI/CO streams147* If recovered text is empty, than decryption failed148*/149if (recoveredText.length == 0) {150return false;151}152return Arrays.equals(plainText, recoveredText);153} catch (IllegalStateException e) {154System.out.println("Expected IllegalStateException: "155+ e.getMessage());156e.printStackTrace(System.out);157return false;158}159}160161private Cipher createCipher(int mode, AlgorithmParameters params)162throws NoSuchAlgorithmException, NoSuchProviderException,163NoSuchPaddingException, InvalidKeyException,164InvalidAlgorithmParameterException {165Cipher cipher = Cipher.getInstance(TRANSFORMATION, PROVIDER);166if (params != null) {167cipher.init(mode, key, params);168} else {169cipher.init(mode, key);170}171return cipher;172}173}174175176