Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/pkcs11/Cipher/TestPKCS5PaddingError.java
38854 views
/*1* Copyright (c) 2010, 2016, 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 ..29* @run main/othervm TestPKCS5PaddingError30* @run main/othervm TestPKCS5PaddingError sm31*/3233import java.security.AlgorithmParameters;34import java.security.NoSuchAlgorithmException;35import java.security.Provider;36import javax.crypto.BadPaddingException;37import javax.crypto.Cipher;38import javax.crypto.IllegalBlockSizeException;39import javax.crypto.KeyGenerator;40import javax.crypto.SecretKey;4142public class TestPKCS5PaddingError extends PKCS11Test {43private static class CI { // class for holding Cipher Information44String transformation;45String keyAlgo;4647CI(String transformation, String keyAlgo) {48this.transformation = transformation;49this.keyAlgo = keyAlgo;50}51}5253private static final CI[] TEST_LIST = {54// algorithms which use the native padding impl55new CI("DES/CBC/PKCS5Padding", "DES"),56new CI("DESede/CBC/PKCS5Padding", "DESede"),57new CI("AES/CBC/PKCS5Padding", "AES"),58// algorithms which use SunPKCS11's own padding impl59new CI("DES/ECB/PKCS5Padding", "DES"),60new CI("DESede/ECB/PKCS5Padding", "DESede"),61new CI("AES/ECB/PKCS5Padding", "AES"),62};6364private static StringBuffer debugBuf = new StringBuffer();6566@Override67public void main(Provider p) throws Exception {68try {69byte[] plainText = new byte[200];7071for (int i = 0; i < TEST_LIST.length; i++) {72CI currTest = TEST_LIST[i];73System.out.println("===" + currTest.transformation + "===");74try {75KeyGenerator kg =76KeyGenerator.getInstance(currTest.keyAlgo, p);77SecretKey key = kg.generateKey();78Cipher c1 = Cipher.getInstance(currTest.transformation,79"SunJCE");80c1.init(Cipher.ENCRYPT_MODE, key);81byte[] cipherText = c1.doFinal(plainText);82AlgorithmParameters params = c1.getParameters();83Cipher c2 = Cipher.getInstance(currTest.transformation, p);84c2.init(Cipher.DECRYPT_MODE, key, params);8586// 1st test: wrong output length87// NOTE: Skip NSS since it reports CKR_DEVICE_ERROR when88// the data passed to its EncryptUpdate/DecryptUpdate is89// not multiple of blocks90if (!p.getName().equals("SunPKCS11-NSS")) {91try {92System.out.println("Testing with wrong cipherText length");93c2.doFinal(cipherText, 0, cipherText.length - 2);94} catch (IllegalBlockSizeException ibe) {95// expected96} catch (Exception ex) {97System.out.println("Error: Unexpected Ex " + ex);98ex.printStackTrace();99}100}101// 2nd test: wrong padding value102try {103System.out.println("Testing with wrong padding bytes");104cipherText[cipherText.length - 1]++;105c2.doFinal(cipherText);106} catch (BadPaddingException bpe) {107// expected108} catch (Exception ex) {109System.out.println("Error: Unexpected Ex " + ex);110ex.printStackTrace();111}112System.out.println("DONE");113} catch (NoSuchAlgorithmException nsae) {114System.out.println("Skipping unsupported algorithm: " +115nsae);116}117}118} catch (Exception ex) {119// print out debug info when exception is encountered120if (debugBuf != null) {121System.out.println(debugBuf.toString());122debugBuf = new StringBuffer();123}124throw ex;125}126}127128public static void main(String[] args) throws Exception {129main(new TestPKCS5PaddingError(), args);130}131}132133134