Path: blob/master/test/jdk/sun/security/pkcs11/Cipher/TestPKCS5PaddingError.java
41152 views
/*1* Copyright (c) 2010, 2018, 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 668772526* @summary Test internal PKCS5Padding impl with various error conditions.27* @author Valerie Peng28* @library /test/lib ..29* @modules jdk.crypto.cryptoki30* @run main/othervm TestPKCS5PaddingError31* @run main/othervm -Djava.security.manager=allow TestPKCS5PaddingError sm32*/3334import java.security.AlgorithmParameters;35import java.security.NoSuchAlgorithmException;36import java.security.Provider;37import javax.crypto.BadPaddingException;38import javax.crypto.Cipher;39import javax.crypto.IllegalBlockSizeException;40import javax.crypto.KeyGenerator;41import javax.crypto.SecretKey;4243public class TestPKCS5PaddingError extends PKCS11Test {44private static class CI { // class for holding Cipher Information45String transformation;46String keyAlgo;4748CI(String transformation, String keyAlgo) {49this.transformation = transformation;50this.keyAlgo = keyAlgo;51}52}5354private static final CI[] TEST_LIST = {55// algorithms which use the native padding impl56new CI("DES/CBC/PKCS5Padding", "DES"),57new CI("DESede/CBC/PKCS5Padding", "DESede"),58new CI("AES/CBC/PKCS5Padding", "AES"),59// algorithms which use SunPKCS11's own padding impl60new CI("DES/ECB/PKCS5Padding", "DES"),61new CI("DESede/ECB/PKCS5Padding", "DESede"),62new CI("AES/ECB/PKCS5Padding", "AES"),63};6465private static StringBuffer debugBuf = new StringBuffer();6667@Override68public void main(Provider p) throws Exception {69try {70byte[] plainText = new byte[200];7172for (int i = 0; i < TEST_LIST.length; i++) {73CI currTest = TEST_LIST[i];74System.out.println("===" + currTest.transformation + "===");75try {76KeyGenerator kg =77KeyGenerator.getInstance(currTest.keyAlgo, p);78SecretKey key = kg.generateKey();79Cipher c1 = Cipher.getInstance(currTest.transformation,80"SunJCE");81c1.init(Cipher.ENCRYPT_MODE, key);82byte[] cipherText = c1.doFinal(plainText);83AlgorithmParameters params = c1.getParameters();84Cipher c2 = Cipher.getInstance(currTest.transformation, p);85c2.init(Cipher.DECRYPT_MODE, key, params);8687// 1st test: wrong output length88// NOTE: Skip NSS since it reports CKR_DEVICE_ERROR when89// the data passed to its EncryptUpdate/DecryptUpdate is90// not multiple of blocks91if (!p.getName().equals("SunPKCS11-NSS")) {92try {93System.out.println("Testing with wrong cipherText length");94c2.doFinal(cipherText, 0, cipherText.length - 2);95} catch (IllegalBlockSizeException ibe) {96// expected97} catch (Exception ex) {98System.out.println("Error: Unexpected Ex " + ex);99ex.printStackTrace();100}101}102// 2nd test: wrong padding value103try {104System.out.println("Testing with wrong padding bytes");105cipherText[cipherText.length - 1]++;106c2.doFinal(cipherText);107} catch (BadPaddingException bpe) {108// expected109} catch (Exception ex) {110System.out.println("Error: Unexpected Ex " + ex);111ex.printStackTrace();112}113System.out.println("DONE");114} catch (NoSuchAlgorithmException nsae) {115System.out.println("Skipping unsupported algorithm: " +116nsae);117}118}119} catch (Exception ex) {120// print out debug info when exception is encountered121if (debugBuf != null) {122System.out.println(debugBuf.toString());123debugBuf = new StringBuffer();124}125throw ex;126}127}128129public static void main(String[] args) throws Exception {130main(new TestPKCS5PaddingError(), args);131}132}133134135