3103N/A/*
3103N/A * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
3103N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3103N/A *
3103N/A * This code is free software; you can redistribute it and/or modify it
3103N/A * under the terms of the GNU General Public License version 2 only, as
3103N/A * published by the Free Software Foundation.
3103N/A *
3103N/A * This code is distributed in the hope that it will be useful, but WITHOUT
3103N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3103N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
3103N/A * version 2 for more details (a copy is included in the LICENSE file that
3103N/A * accompanied this code).
3103N/A *
3103N/A * You should have received a copy of the GNU General Public License version
3103N/A * 2 along with this work; if not, write to the Free Software Foundation,
3103N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3103N/A *
3103N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3103N/A * or visit www.oracle.com if you need additional information or have any
3103N/A * questions.
3103N/A */
3103N/A
3103N/A/**
3103N/A * @test
3103N/A * @bug 6687725
3103N/A * @summary Test internal PKCS5Padding impl with various error conditions.
3103N/A * @author Valerie Peng
3103N/A * @library ..
3103N/A */
3103N/Aimport java.io.*;
3103N/Aimport java.nio.*;
3103N/Aimport java.util.*;
3103N/A
3103N/Aimport java.security.*;
3103N/Aimport java.security.spec.AlgorithmParameterSpec;
3103N/A
3103N/Aimport javax.crypto.*;
3103N/Aimport javax.crypto.spec.IvParameterSpec;
3103N/A
3103N/Apublic class TestPKCS5PaddingError extends PKCS11Test {
3103N/A private static class CI { // class for holding Cipher Information
3103N/A String transformation;
3103N/A String keyAlgo;
3103N/A
3103N/A CI(String transformation, String keyAlgo) {
3103N/A this.transformation = transformation;
3103N/A this.keyAlgo = keyAlgo;
3103N/A }
3103N/A }
3103N/A
3103N/A private static final CI[] TEST_LIST = {
3103N/A // algorithms which use the native padding impl
3103N/A new CI("DES/CBC/PKCS5Padding", "DES"),
3103N/A new CI("DESede/CBC/PKCS5Padding", "DESede"),
3103N/A new CI("AES/CBC/PKCS5Padding", "AES"),
3103N/A // algorithms which use SunPKCS11's own padding impl
3103N/A new CI("DES/ECB/PKCS5Padding", "DES"),
3103N/A new CI("DESede/ECB/PKCS5Padding", "DESede"),
3103N/A new CI("AES/ECB/PKCS5Padding", "AES"),
3103N/A };
3103N/A
3103N/A private static StringBuffer debugBuf = new StringBuffer();
3103N/A
3103N/A public void main(Provider p) throws Exception {
3103N/A boolean status = true;
3103N/A Random random = new Random();
3103N/A
3103N/A try {
3103N/A byte[] plainText = new byte[200];
3103N/A
3103N/A for (int i = 0; i < TEST_LIST.length; i++) {
3103N/A CI currTest = TEST_LIST[i];
3103N/A System.out.println("===" + currTest.transformation + "===");
3103N/A try {
3103N/A KeyGenerator kg =
3103N/A KeyGenerator.getInstance(currTest.keyAlgo, p);
3103N/A SecretKey key = kg.generateKey();
3103N/A Cipher c1 = Cipher.getInstance(currTest.transformation,
3103N/A "SunJCE");
3103N/A c1.init(Cipher.ENCRYPT_MODE, key);
3103N/A byte[] cipherText = c1.doFinal(plainText);
3103N/A AlgorithmParameters params = c1.getParameters();
3103N/A Cipher c2 = Cipher.getInstance(currTest.transformation, p);
3103N/A c2.init(Cipher.DECRYPT_MODE, key, params);
3103N/A
3103N/A // 1st test: wrong output length
3103N/A // NOTE: Skip NSS since it reports CKR_DEVICE_ERROR when
3103N/A // the data passed to its EncryptUpdate/DecryptUpdate is
3103N/A // not multiple of blocks
3103N/A if (!p.getName().equals("SunPKCS11-NSS")) {
3103N/A try {
3103N/A System.out.println("Testing with wrong cipherText length");
3103N/A c2.doFinal(cipherText, 0, cipherText.length - 2);
3103N/A } catch (IllegalBlockSizeException ibe) {
3103N/A // expected
3103N/A } catch (Exception ex) {
3103N/A System.out.println("Error: Unexpected Ex " + ex);
3103N/A ex.printStackTrace();
3103N/A }
3103N/A }
3103N/A // 2nd test: wrong padding value
3103N/A try {
3103N/A System.out.println("Testing with wrong padding bytes");
3103N/A cipherText[cipherText.length - 1]++;
3103N/A c2.doFinal(cipherText);
3103N/A } catch (BadPaddingException bpe) {
3103N/A // expected
3103N/A } catch (Exception ex) {
3103N/A System.out.println("Error: Unexpected Ex " + ex);
3103N/A ex.printStackTrace();
3103N/A }
3103N/A System.out.println("DONE");
3103N/A } catch (NoSuchAlgorithmException nsae) {
3103N/A System.out.println("Skipping unsupported algorithm: " +
3103N/A nsae);
3103N/A }
3103N/A }
3103N/A } catch (Exception ex) {
3103N/A // print out debug info when exception is encountered
3103N/A if (debugBuf != null) {
3103N/A System.out.println(debugBuf.toString());
3103N/A debugBuf = new StringBuffer();
3103N/A }
3103N/A throw ex;
3103N/A }
3103N/A }
3103N/A
3103N/A public static void main(String[] args) throws Exception {
3103N/A main(new TestPKCS5PaddingError());
3103N/A }
3103N/A}