Path: blob/master/test/jdk/javax/crypto/Cipher/TestCipherMode.java
41152 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.*;3536import javax.crypto.*;37import javax.crypto.spec.SecretKeySpec;3839public class TestCipherMode {4041private static final String[] TRANSFORMATIONS = {42"DES/ECB/PKCS5Padding",43"AES/KW/NoPadding",44"AES/KW/PKCS5Padding",45"AES/KWP/NoPadding",46};4748private static final SecretKey DES_KEY =49new SecretKeySpec(new byte[8], "DES");50private static final SecretKey AES_KEY =51new SecretKeySpec(new byte[16], "AES");5253public static void main(String[] argv) throws Exception {54for (String t : TRANSFORMATIONS) {55System.out.println("Testing SunJCE provider, Cipher " + t );5657TestCipherMode test = new TestCipherMode(t);58System.out.println("Testing ENCRYPT_MODE...");59test.checkMode(Cipher.ENCRYPT_MODE, "encryption");60System.out.println("Testing DECRYPT_MODE...");61test.checkMode(Cipher.DECRYPT_MODE, "decryption");62System.out.println("Testing WRAP_MODE...");63test.checkMode(Cipher.WRAP_MODE, "key wrapping");64System.out.println("Testing UNWRAP_MODE...");65test.checkMode(Cipher.UNWRAP_MODE, "key unwrapping");66}67System.out.println("All Tests Passed");68}6970private Cipher c = null;71private SecretKey key = null;7273private TestCipherMode(String transformation)74throws NoSuchAlgorithmException, NoSuchProviderException,75NoSuchPaddingException {76c = Cipher.getInstance(transformation, "SunJCE");77this.key = switch (transformation.split("/")[0]) {78case "DES" -> DES_KEY;79case "AES" -> AES_KEY;80default -> throw new RuntimeException81("Error: Unsupported key algorithm");82};83}8485private void checkMode(int mode, String opString) throws Exception {86c.init(mode, key);8788switch (mode) {89case Cipher.ENCRYPT_MODE:90case Cipher.DECRYPT_MODE:91// call wrap()/unwrap() and see if ISE is thrown.92try {93c.wrap(key);94throw new Exception("ERROR: should throw ISE for wrap()");95} catch (IllegalStateException ise) {96System.out.println("expected ISE is thrown for wrap()");97}98try {99c.unwrap(new byte[16], key.getAlgorithm(), Cipher.SECRET_KEY);100throw new Exception("ERROR: should throw ISE for unwrap()");101} catch (IllegalStateException ise) {102System.out.println("expected ISE is thrown for unwrap()");103}104break;105case Cipher.WRAP_MODE:106case Cipher.UNWRAP_MODE:107try {108c.update(new byte[16]);109throw new Exception("ERROR: should throw ISE for update()");110} catch (IllegalStateException ise) {111System.out.println("expected ISE is thrown for update()");112}113try {114c.doFinal();115throw new Exception("ERROR: should throw ISE for doFinal()");116} catch (IllegalStateException ise) {117System.out.println("expected ISE is thrown for doFinal()");118}119break;120}121}122}123124125