Path: blob/master/test/jdk/sun/security/pkcs11/Cipher/TestCipherMode.java
66646 views
/*1* Copyright (c) 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 826550026* @summary27* @library /test/lib ..28* @modules jdk.crypto.cryptoki29* @run main/othervm TestCipherMode30*/3132import java.security.Provider;33import java.security.Key;34import java.security.KeyPair;35import java.security.KeyPairGenerator;36import java.security.PrivateKey;37import java.security.PublicKey;38import java.security.InvalidParameterException;39import java.security.NoSuchAlgorithmException;40import java.util.Arrays;41import javax.crypto.Cipher;42import javax.crypto.SecretKey;43import javax.crypto.spec.SecretKeySpec;4445public class TestCipherMode extends PKCS11Test {4647private static String[] TRANSFORMATIONS = {48"AES/ECB/PKCS5Padding", "AES/GCM/NoPadding",49"RSA/ECB/PKCS1Padding"50};5152private static byte[] BYTES16 =53Arrays.copyOf(TRANSFORMATIONS[0].getBytes(), 16);54private static SecretKey AES_KEY = new SecretKeySpec(BYTES16, "AES");55private static PublicKey RSA_PUBKEY = null;56private static PrivateKey RSA_PRIVKEY = null;5758enum CipherMode {59ENCRYPT(Cipher.ENCRYPT_MODE),60DECRYPT(Cipher.DECRYPT_MODE),61WRAP(Cipher.WRAP_MODE),62UNWRAP(Cipher.UNWRAP_MODE),63NONEXISTENT(100);6465int value;6667CipherMode(int value) {68this.value = value;69}70}7172private static Key getKey(String t, CipherMode m, Provider p)73throws NoSuchAlgorithmException {74if (t.startsWith("AES")) {75return AES_KEY;76} else if (t.startsWith("RSA")) {77if (RSA_PUBKEY == null) {78KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", p);79KeyPair kp = kpg.generateKeyPair();80RSA_PUBKEY = kp.getPublic();81RSA_PRIVKEY = kp.getPrivate();82}83return ((m == CipherMode.ENCRYPT || m == CipherMode.UNWRAP)?84RSA_PRIVKEY : RSA_PUBKEY);85} else {86throw new RuntimeException("Unknown transformation: " + t);87}88}8990public static void main(String[] args) throws Exception {91main(new TestCipherMode(), args);92}9394@Override95public void main(Provider p) throws Exception {9697// test all cipher impls, e.g. P11Cipher, P11AEADCipher, and98// P11RSACipher99for (String t : TRANSFORMATIONS) {100checkModes(t, p);101}102System.out.println("All tests passed");103}104105private static void checkModes(String t, Provider p) throws Exception {106try {107Cipher.getInstance(t, p);108} catch (Exception e) {109System.out.println("Skip " + t + " due to " + e.getMessage());110return;111}112113for (CipherMode m : CipherMode.values()) {114System.out.println("Testing " + t + " with " + m.name());115Cipher c;116try {117c = Cipher.getInstance(t, p);118// try init and see if the expected Exception is thrown119c.init(m.value, getKey(t, m, p), c.getParameters());120if (m == CipherMode.NONEXISTENT) {121throw new Exception("ERROR: should throw IPE with init()");122}123} catch (UnsupportedOperationException uoe) {124// some may not support wrap/unwrap125if (m == CipherMode.WRAP || m == CipherMode.UNWRAP) {126System.out.println("Expected UOE thrown with init()");127continue;128}129throw uoe;130} catch (InvalidParameterException ipe) {131if (m == CipherMode.NONEXISTENT) {132System.out.println("Expected IPE thrown for init()");133continue;134}135throw ipe;136}137switch (m) {138case ENCRYPT:139case DECRYPT:140// call wrap()/unwrap() and see if ISE is thrown.141try {142c.wrap(AES_KEY);143throw new Exception("ERROR: should throw ISE for wrap()");144} catch (IllegalStateException ise) {145System.out.println("Expected ISE thrown for wrap()");146}147try {148c.unwrap(BYTES16, "AES", Cipher.SECRET_KEY);149throw new Exception("ERROR: should throw ISE for unwrap()");150} catch (IllegalStateException ise) {151System.out.println("Expected ISE thrown for unwrap()");152}153break;154case WRAP:155case UNWRAP:156try {157c.update(BYTES16);158throw new Exception("ERROR: should throw ISE for update()");159} catch (IllegalStateException ise) {160System.out.println("Expected ISE thrown for update()");161}162try {163c.doFinal();164throw new Exception("ERROR: should throw ISE for" +165" doFinal()");166} catch (IllegalStateException ise) {167System.out.println("Expected ISE thrown for doFinal()");168}169break;170default:171throw new AssertionError();172}173}174}175}176177178