Path: blob/master/test/jdk/javax/crypto/Cipher/TestCipherMode.java
66644 views
/*1* Copyright (c) 2004, 2021, 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*/2223/*24* @test25* @bug 4953556 8210838 824826826* @summary ensure that IllegalStateException is thrown if the27* Cipher object is initialized with a wrong mode, e.g. WRAP_MODE28* for update()/doFinal() calls.29* @author Valerie Peng30*/313233import java.security.*;34import java.security.spec.*;35import java.util.Arrays;3637import javax.crypto.*;38import javax.crypto.spec.SecretKeySpec;3940public class TestCipherMode {4142private static final String[] TRANSFORMATIONS = {43"DES/ECB/PKCS5Padding", // CipherCore44"AES/GCM/NoPadding", // GaloisCounterMode45"AES/KW/NoPadding", // KeyWrapCipher46"AES/KW/PKCS5Padding", // KeyWrapCipher47"AES/KWP/NoPadding", // KeyWrapCipher48"RSA/ECB/NoPadding", // RSACipher49"DESedeWrap/CBC/NoPadding", // DESedeWrapCipher50"ChaCha20-Poly1305", // ChaCha20Cipher51};5253private static final byte[] BYTES32 =54Arrays.copyOf(TRANSFORMATIONS[0].getBytes(), 32);55private static final SecretKey DES_KEY =56new SecretKeySpec(BYTES32, 0, 8, "DES");57private static final SecretKey AES_KEY =58new SecretKeySpec(BYTES32, 0, 16, "AES");5960private static enum CipherMode {61ENCRYPT(Cipher.ENCRYPT_MODE),62DECRYPT(Cipher.DECRYPT_MODE),63WRAP(Cipher.WRAP_MODE),64UNWRAP(Cipher.UNWRAP_MODE),65NONEXISTENT(100);6667int value;6869CipherMode(int value) {70this.value = value;71}72}7374private static Key getKey(String t, CipherMode m)75throws NoSuchAlgorithmException, NoSuchProviderException {76Key key;77String algo = t.split("/")[0];78switch (algo) {79case "AES":80key = AES_KEY;81break;82case "RSA":83KeyPairGenerator kpg = KeyPairGenerator.getInstance(algo);84KeyPair kp = kpg.generateKeyPair();85key = ((m == CipherMode.ENCRYPT || m == CipherMode.UNWRAP)?86kp.getPrivate() : kp.getPublic());87break;88case "ChaCha20-Poly1305":89key = new SecretKeySpec(BYTES32, 0, 32, "ChaCha20");90break;91case "DES":92key = new SecretKeySpec(BYTES32, 0, 8, algo);93break;94case "DESedeWrap":95key = new SecretKeySpec(BYTES32, 0, 24, "DESede");96break;97default:98throw new RuntimeException("Unknown transformation: " + t);99}100return key;101}102103public static void main(String[] argv) throws Exception {104105TestCipherMode test = new TestCipherMode("SunJCE", TRANSFORMATIONS);106System.out.println("All Tests Passed");107}108109private Cipher c = null;110private SecretKey key = null;111112private TestCipherMode(String provName, String... transformations)113throws Exception {114115System.out.println("Testing " + provName);116117for (String t : transformations) {118for (CipherMode m : CipherMode.values()) {119checkMode(t, m, provName);120}121}122}123124private void checkMode(String t, CipherMode mode, String provName)125throws Exception {126Cipher c = Cipher.getInstance(t, provName);127Key key = getKey(t, mode);128129System.out.println(c.getAlgorithm() + " with " + mode.name());130try {131c.init(mode.value, key, c.getParameters());132if (mode == CipherMode.NONEXISTENT) {133throw new Exception("ERROR: should throw IPE for init()");134}135} catch (UnsupportedOperationException uoe) {136// some may not support wrap/unwrap or enc/dec137if (mode != CipherMode.NONEXISTENT) {138System.out.println("Expected UOE thrown with init()");139return;140}141throw uoe;142} catch (InvalidParameterException ipe) {143if (mode == CipherMode.NONEXISTENT) {144System.out.println("=> expected IPE thrown for init()");145return;146}147throw ipe;148}149150switch (mode) {151case ENCRYPT:152case DECRYPT:153// call wrap()/unwrap() and see if ISE is thrown.154try {155c.wrap(key);156throw new Exception("ERROR: should throw ISE for wrap()");157} catch (IllegalStateException ise) {158System.out.println("=> expected ISE thrown for wrap()");159}160try {161c.unwrap(new byte[16], key.getAlgorithm(), Cipher.SECRET_KEY);162throw new Exception("ERROR: should throw ISE for unwrap()");163} catch (IllegalStateException ise) {164System.out.println("=> expected ISE thrown for unwrap()");165}166break;167case WRAP:168case UNWRAP:169try {170c.update(new byte[16]);171throw new Exception("ERROR: should throw ISE for update()");172} catch (IllegalStateException ise) {173System.out.println("=> expected ISE thrown for update()");174}175try {176c.doFinal();177throw new Exception("ERROR: should throw ISE for doFinal()");178} catch (IllegalStateException ise) {179System.out.println("=> expected ISE thrown for doFinal()");180}181break;182}183}184}185186187