Path: blob/master/test/jdk/com/sun/crypto/provider/Cipher/AES/CTR.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.InvalidAlgorithmParameterException;24import java.security.InvalidKeyException;25import java.security.NoSuchAlgorithmException;26import java.security.NoSuchProviderException;27import java.security.spec.AlgorithmParameterSpec;28import java.util.Arrays;29import java.util.Random;30import javax.crypto.BadPaddingException;31import javax.crypto.Cipher;32import javax.crypto.IllegalBlockSizeException;33import javax.crypto.KeyGenerator;34import javax.crypto.NoSuchPaddingException;35import javax.crypto.SecretKey;36import javax.crypto.ShortBufferException;37import javax.crypto.spec.IvParameterSpec;383940/**41* @test42* @bug 804383643* @summary Test AES ciphers with 4 different modes with NoPadding. Check if44* data before encryption and after decryption is the same.45* @key randomness46*/4748public class CTR {4950private static final String ALGORITHM = "AES";5152private static final String PROVIDER = "SunJCE";5354private static final String[] MODES = {"CTR","CFB24","OFB32","GCM"};5556private static final String PADDING = "NoPadding";575859private static final int KEY_LENGTH = 128;6061public static void main(String argv[]) throws Exception {62CTR test = new CTR();63for (String mode : MODES) {64test.runTest(ALGORITHM, mode, PADDING);65}66}676869public void runTest(String algo, String mo, String pad) throws Exception {70Cipher ci = null;71byte[] iv = null;72AlgorithmParameterSpec aps = null;73SecretKey key = null;7475try {76Random rdm = new Random();77byte[] plainText;7879ci = Cipher.getInstance(algo + "/" + mo + "/" + pad, PROVIDER);80KeyGenerator kg = KeyGenerator.getInstance(algo, PROVIDER);81kg.init(KEY_LENGTH);82key = kg.generateKey();8384for (int i = 0; i < 15; i++) {85plainText = new byte[1600 + i + 1];86rdm.nextBytes(plainText);8788if (!mo.equalsIgnoreCase("GCM")) {89ci.init(Cipher.ENCRYPT_MODE, key, aps);90} else {91ci.init(Cipher.ENCRYPT_MODE, key);92}9394byte[] cipherText = new byte[ci.getOutputSize(plainText.length)];95int offset = ci.update(plainText, 0, plainText.length,96cipherText, 0);9798ci.doFinal(cipherText, offset);99100if (!mo.equalsIgnoreCase("ECB")) {101iv = ci.getIV();102aps = new IvParameterSpec(iv);103} else {104aps = null;105}106107if (!mo.equalsIgnoreCase("GCM")) {108ci.init(Cipher.DECRYPT_MODE, key, aps);109} else {110ci.init(Cipher.DECRYPT_MODE, key, ci.getParameters());111}112113byte[] recoveredText = new byte[ci.getOutputSize(cipherText.length)];114int len = ci.doFinal(cipherText, 0, cipherText.length,115recoveredText);116byte[] tmp = new byte[len];117118for (int j = 0; j < len; j++) {119tmp[j] = recoveredText[j];120}121Arrays.toString(plainText);122if (!java.util.Arrays.equals(plainText, tmp)) {123System.out.println("Original: ");124dumpBytes(plainText);125System.out.println("Recovered: ");126dumpBytes(tmp);127throw new RuntimeException("Original text is not equal with recovered text, with mode:" + mo);128}129}130} catch (NoSuchAlgorithmException | NoSuchProviderException | NoSuchPaddingException131| InvalidKeyException | InvalidAlgorithmParameterException132| ShortBufferException | IllegalBlockSizeException133| BadPaddingException e) {134System.out.println("Test failed!");135throw e;136}137}138139private void dumpBytes(byte[] bytes){140for (byte b : bytes){141System.out.print(Integer.toHexString(b));142}143System.out.println();144}145}146147148