Path: blob/master/test/jdk/com/sun/crypto/provider/Cipher/AES/TestNonexpanding.java
41161 views
/*1* Copyright (c) 2014, 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.security.InvalidKeyException;24import java.security.NoSuchAlgorithmException;25import java.security.NoSuchProviderException;26import java.security.spec.InvalidParameterSpecException;27import java.util.Random;28import javax.crypto.BadPaddingException;29import javax.crypto.Cipher;30import javax.crypto.IllegalBlockSizeException;31import javax.crypto.KeyGenerator;32import javax.crypto.NoSuchPaddingException;33import javax.crypto.SecretKey;34import javax.crypto.ShortBufferException;35import javax.crypto.spec.GCMParameterSpec;3637/**38* @test39* @bug 804383640* @summary Test AES encryption with no padding. Expect the original data length41* is the same as the encrypted data.42* @key randomness43*/44public class TestNonexpanding {4546private static final String ALGORITHM = "AES";47private static final String PROVIDER = "SunJCE";48private static final String[] MODES = { "ECb", "CbC", "OFB", "OFB150",49"cFB", "CFB7", "cFB8", "cFB16", "cFB24", "cFB32", "Cfb40", "cfB48",50"cfB56", "cfB64", "cfB72", "cfB80", "cfB88", "cfB96", "cfb104",51"cfB112", "cfB120", "GCM" };52private static final String PADDING = "NoPadding";53private static final int KEY_LENGTH = 128;5455public static void main(String argv[]) throws Exception {56TestNonexpanding test = new TestNonexpanding();57for (String mode : MODES) {58test.runTest(ALGORITHM, mode, PADDING);59}60}6162public void runTest(String algo, String mo, String pad) throws Exception {63Cipher ci = null;64SecretKey key = null;65try {66// Initialization67Random rdm = new Random();68byte[] plainText = new byte[128];69rdm.nextBytes(plainText);7071ci = Cipher.getInstance(algo + "/" + mo + "/" + pad, PROVIDER);7273KeyGenerator kg = KeyGenerator.getInstance(algo, PROVIDER);74kg.init(KEY_LENGTH);75key = kg.generateKey();7677// encrypt78ci.init(Cipher.ENCRYPT_MODE, key);79byte[] cipherText = new byte[ci.getOutputSize(plainText.length)];80int offset = ci.update(plainText, 0, plainText.length, cipherText,810);82ci.doFinal(cipherText, offset);8384// Comparison85if (!(plainText.length == cipherText.length)) {86// The result of encryption in GCM is a combination of an87// authentication tag and cipher text.88if (mo.equalsIgnoreCase("GCM")) {89GCMParameterSpec spec = ci.getParameters().getParameterSpec(GCMParameterSpec.class);90int cipherTextLength = cipherText.length - spec.getTLen()91/ 8;92if (plainText.length == cipherTextLength) {93return;94}95}96System.out.println("Original length: " + plainText.length);97System.out.println("Cipher text length: " + cipherText.length);98throw new RuntimeException("Test failed!");99}100} catch (NoSuchAlgorithmException e) {101//CFB7 and OFB150 are for negative testing102if (!mo.equalsIgnoreCase("CFB7") && !mo.equalsIgnoreCase("OFB150")) {103System.out.println("Unexpected NoSuchAlgorithmException with mode: "104+ mo);105throw new RuntimeException("Test failed!");106}107} catch ( NoSuchProviderException | NoSuchPaddingException108| InvalidKeyException | InvalidParameterSpecException109| ShortBufferException | IllegalBlockSizeException110| BadPaddingException e) {111System.out.println("Test failed!");112throw e;113}114}115}116117118