Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/crypto/Cipher/TestCipherMode.java
38838 views
/*1* Copyright (c) 2004, 2007, 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 495355626* @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 ALGO = "DES";4243public static void main(String[] argv) throws Exception {44TestCipherMode test = new TestCipherMode();45System.out.println("Testing ENCRYPT_MODE...");46test.checkMode(Cipher.ENCRYPT_MODE);47System.out.println("Testing DECRYPT_MODE...");48test.checkMode(Cipher.DECRYPT_MODE);49System.out.println("Testing WRAP_MODE...");50test.checkMode(Cipher.WRAP_MODE);51System.out.println("Testing UNWRAP_MODE...");52test.checkMode(Cipher.UNWRAP_MODE);53System.out.println("All Tests Passed");54}5556private Cipher c = null;57private SecretKey key = null;5859private TestCipherMode() throws NoSuchAlgorithmException,60NoSuchProviderException, NoSuchPaddingException {61c = Cipher.getInstance(ALGO + "/ECB/PKCS5Padding", "SunJCE");62key = new SecretKeySpec(new byte[8], ALGO);63}6465private void checkMode(int mode) throws Exception {66c.init(mode, key);6768switch (mode) {69case Cipher.ENCRYPT_MODE:70case Cipher.DECRYPT_MODE:71// call wrap()/unwrap() and see if ISE is thrown.72try {73c.wrap(key);74throw new Exception("ERROR: should throw ISE for wrap()");75} catch (IllegalStateException ise) {76System.out.println("expected ISE is thrown for wrap()");77}78try {79c.unwrap(new byte[16], ALGO, Cipher.SECRET_KEY);80throw new Exception("ERROR: should throw ISE for unwrap()");81} catch (IllegalStateException ise) {82System.out.println("expected ISE is thrown for unwrap()");83}84break;85case Cipher.WRAP_MODE:86case Cipher.UNWRAP_MODE:87try {88c.update(new byte[16]);89throw new Exception("ERROR: should throw ISE for update()");90} catch (IllegalStateException ise) {91System.out.println("expected ISE is thrown for update()");92}93try {94c.doFinal();95throw new Exception("ERROR: should throw ISE for doFinal()");96} catch (IllegalStateException ise) {97System.out.println("expected ISE is thrown for doFinal()");98}99break;100}101}102}103104105