J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | * accompanied this code). |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License version |
| 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
| 19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /* |
| 25 | * @test |
| 26 | * @bug 4953556 |
| 27 | * @summary ensure that IllegalStateException is thrown if the |
| 28 | * Cipher object is initialized with a wrong mode, e.g. WRAP_MODE |
| 29 | * for update()/doFinal() calls. |
| 30 | * @author Valerie Peng |
| 31 | */ |
| 32 | |
| 33 | |
| 34 | import java.security.*; |
| 35 | import java.security.spec.*; |
| 36 | |
| 37 | import javax.crypto.*; |
| 38 | import javax.crypto.spec.SecretKeySpec; |
| 39 | |
| 40 | public class TestCipherMode { |
| 41 | |
| 42 | private static final String ALGO = "DES"; |
| 43 | |
| 44 | public static void main(String[] argv) throws Exception { |
| 45 | TestCipherMode test = new TestCipherMode(); |
| 46 | System.out.println("Testing ENCRYPT_MODE..."); |
| 47 | test.checkMode(Cipher.ENCRYPT_MODE); |
| 48 | System.out.println("Testing DECRYPT_MODE..."); |
| 49 | test.checkMode(Cipher.DECRYPT_MODE); |
| 50 | System.out.println("Testing WRAP_MODE..."); |
| 51 | test.checkMode(Cipher.WRAP_MODE); |
| 52 | System.out.println("Testing UNWRAP_MODE..."); |
| 53 | test.checkMode(Cipher.UNWRAP_MODE); |
| 54 | System.out.println("All Tests Passed"); |
| 55 | } |
| 56 | |
| 57 | private Cipher c = null; |
| 58 | private SecretKey key = null; |
| 59 | |
| 60 | private TestCipherMode() throws NoSuchAlgorithmException, |
| 61 | NoSuchProviderException, NoSuchPaddingException { |
| 62 | c = Cipher.getInstance(ALGO + "/ECB/PKCS5Padding", "SunJCE"); |
| 63 | key = new SecretKeySpec(new byte[8], ALGO); |
| 64 | } |
| 65 | |
| 66 | private void checkMode(int mode) throws Exception { |
| 67 | c.init(mode, key); |
| 68 | |
| 69 | switch (mode) { |
| 70 | case Cipher.ENCRYPT_MODE: |
| 71 | case Cipher.DECRYPT_MODE: |
| 72 | // call wrap()/unwrap() and see if ISE is thrown. |
| 73 | try { |
| 74 | c.wrap(key); |
| 75 | throw new Exception("ERROR: should throw ISE for wrap()"); |
| 76 | } catch (IllegalStateException ise) { |
| 77 | System.out.println("expected ISE is thrown for wrap()"); |
| 78 | } |
| 79 | try { |
| 80 | c.unwrap(new byte[16], ALGO, Cipher.SECRET_KEY); |
| 81 | throw new Exception("ERROR: should throw ISE for unwrap()"); |
| 82 | } catch (IllegalStateException ise) { |
| 83 | System.out.println("expected ISE is thrown for unwrap()"); |
| 84 | } |
| 85 | break; |
| 86 | case Cipher.WRAP_MODE: |
| 87 | case Cipher.UNWRAP_MODE: |
| 88 | try { |
| 89 | c.update(new byte[16]); |
| 90 | throw new Exception("ERROR: should throw ISE for update()"); |
| 91 | } catch (IllegalStateException ise) { |
| 92 | System.out.println("expected ISE is thrown for update()"); |
| 93 | } |
| 94 | try { |
| 95 | c.doFinal(); |
| 96 | throw new Exception("ERROR: should throw ISE for doFinal()"); |
| 97 | } catch (IllegalStateException ise) { |
| 98 | System.out.println("expected ISE is thrown for doFinal()"); |
| 99 | } |
| 100 | break; |
| 101 | } |
| 102 | } |
| 103 | } |