Path: blob/master/test/jdk/com/sun/crypto/provider/NSASuiteB/TestAESOids.java
41155 views
/*1* Copyright (c) 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 static javax.crypto.Cipher.ENCRYPT_MODE;24import static javax.crypto.Cipher.getMaxAllowedKeyLength;2526import java.security.InvalidAlgorithmParameterException;27import java.security.InvalidKeyException;28import java.security.NoSuchAlgorithmException;29import java.security.NoSuchProviderException;30import java.security.spec.AlgorithmParameterSpec;31import java.util.Arrays;32import java.util.List;3334import javax.crypto.BadPaddingException;35import javax.crypto.Cipher;36import javax.crypto.IllegalBlockSizeException;37import javax.crypto.KeyGenerator;38import javax.crypto.NoSuchPaddingException;39import javax.crypto.SecretKey;40import javax.crypto.ShortBufferException;41import javax.crypto.spec.IvParameterSpec;4243/*44* @test45* @bug 807528646* @summary Test the AES algorithm OIDs in JDK.47* OID and Algorithm transformation string should match.48* Both could be able to be used to generate the algorithm instance.49* @run main TestAESOids50*/51public class TestAESOids {5253private static final String PROVIDER_NAME = "SunJCE";54private static final byte[] INPUT = "1234567890123456".getBytes();5556private static final List<DataTuple> DATA = Arrays.asList(57new DataTuple("2.16.840.1.101.3.4.1.1", "AES_128/ECB/NoPadding",58128, "ECB"),59new DataTuple("2.16.840.1.101.3.4.1.2", "AES_128/CBC/NoPadding",60128, "CBC"),61new DataTuple("2.16.840.1.101.3.4.1.3", "AES_128/OFB/NoPadding",62128, "OFB"),63new DataTuple("2.16.840.1.101.3.4.1.4", "AES_128/CFB/NoPadding",64128, "CFB"),65new DataTuple("2.16.840.1.101.3.4.1.21", "AES_192/ECB/NoPadding",66192, "ECB"),67new DataTuple("2.16.840.1.101.3.4.1.22", "AES_192/CBC/NoPadding",68192, "CBC"),69new DataTuple("2.16.840.1.101.3.4.1.23", "AES_192/OFB/NoPadding",70192, "OFB"),71new DataTuple("2.16.840.1.101.3.4.1.24", "AES_192/CFB/NoPadding",72192, "CFB"),73new DataTuple("2.16.840.1.101.3.4.1.41", "AES_256/ECB/NoPadding",74256, "ECB"),75new DataTuple("2.16.840.1.101.3.4.1.42", "AES_256/CBC/NoPadding",76256, "CBC"),77new DataTuple("2.16.840.1.101.3.4.1.43", "AES_256/OFB/NoPadding",78256, "OFB"),79new DataTuple("2.16.840.1.101.3.4.1.44", "AES_256/CFB/NoPadding",80256, "CFB"));8182public static void main(String[] args) throws Exception {83for (DataTuple dataTuple : DATA) {84int maxAllowedKeyLength =85getMaxAllowedKeyLength(dataTuple.algorithm);86boolean supportedKeyLength =87maxAllowedKeyLength >= dataTuple.keyLength;8889try {90runTest(dataTuple, supportedKeyLength);91System.out.println("passed");92} catch (InvalidKeyException ike) {93if (supportedKeyLength) {94throw new RuntimeException(String.format(95"The key length %d is supported, but test failed.",96dataTuple.keyLength), ike);97} else {98System.out.printf(99"Catch expected InvalidKeyException due "100+ "to the key length %d is greater than "101+ "max supported key length %d%n",102dataTuple.keyLength, maxAllowedKeyLength);103}104}105}106}107108private static void runTest(DataTuple dataTuple,109boolean supportedKeyLength) throws NoSuchAlgorithmException,110NoSuchProviderException, NoSuchPaddingException,111InvalidKeyException, ShortBufferException,112IllegalBlockSizeException, BadPaddingException,113InvalidAlgorithmParameterException {114Cipher algorithmCipher = Cipher.getInstance(dataTuple.algorithm,115PROVIDER_NAME);116Cipher oidCipher = Cipher.getInstance(dataTuple.oid, PROVIDER_NAME);117118if (algorithmCipher == null) {119throw new RuntimeException(120String.format("Test failed: algorithm string %s getInstance"121+ " failed.%n", dataTuple.algorithm));122}123124if (oidCipher == null) {125throw new RuntimeException(126String.format("Test failed: OID %s getInstance failed.%n",127dataTuple.oid));128}129130if (!algorithmCipher.getAlgorithm().equals(dataTuple.algorithm)) {131throw new RuntimeException(String.format(132"Test failed: algorithm string %s getInstance "133+ "doesn't generate expected algorithm.%n",134dataTuple.algorithm));135}136137KeyGenerator kg = KeyGenerator.getInstance("AES");138kg.init(dataTuple.keyLength);139SecretKey key = kg.generateKey();140141// encrypt142algorithmCipher.init(ENCRYPT_MODE, key);143if (!supportedKeyLength) {144throw new RuntimeException(String.format(145"The key length %d is not supported, so the initialization "146+ "of algorithmCipher should fail.%n",147dataTuple.keyLength));148}149150byte[] cipherText = new byte[algorithmCipher.getOutputSize(INPUT.length)];151int offset = algorithmCipher.update(INPUT, 0, INPUT.length,152cipherText, 0);153algorithmCipher.doFinal(cipherText, offset);154155AlgorithmParameterSpec aps = null;156if (!dataTuple.mode.equalsIgnoreCase("ECB")) {157aps = new IvParameterSpec(algorithmCipher.getIV());158}159160oidCipher.init(Cipher.DECRYPT_MODE, key, aps);161if (!supportedKeyLength) {162throw new RuntimeException(String.format(163"The key length %d is not supported, so the "164+ "initialization of oidCipher should fail.%n",165dataTuple.keyLength));166}167168byte[] recoveredText = new byte[oidCipher.getOutputSize(cipherText.length)];169oidCipher.doFinal(cipherText, 0, cipherText.length, recoveredText);170171// Comparison172if (!Arrays.equals(INPUT, recoveredText)) {173throw new RuntimeException(174"Decrypted data is not the same as the original text");175}176}177178private static class DataTuple {179180private final String oid;181private final String algorithm;182private final int keyLength;183private final String mode;184185private DataTuple(String oid, String algorithm, int keyLength,186String mode) {187this.oid = oid;188this.algorithm = algorithm;189this.keyLength = keyLength;190this.mode = mode;191}192}193}194195196