0N/A/*
2362N/A * Copyright (c) 2003, 2007, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/**
0N/A * @test
0N/A * @bug 4893959
0N/A * @summary basic test for PBEWithSHA1AndDESede and
0N/A * PBEWithSHA1AndRC2_40
0N/A * @author Valerie Peng
0N/A */
0N/A
0N/Aimport java.io.*;
0N/Aimport java.util.*;
0N/A
0N/Aimport java.security.*;
0N/A
0N/Aimport javax.crypto.*;
0N/Aimport javax.crypto.spec.*;
0N/A
0N/Apublic class PKCS12CipherKAT {
0N/A
0N/A private final static String INPUT = "12:34:56:78:90:ab:cd:ef:ab:cd:ef:12:34:56:78:90:fe:db:ca:09:87:65";
0N/A private final static String SALT = "7d:60:43:5f:02:e9:e0:ae";
0N/A private final static int ITER_COUNT = 2048;
0N/A
0N/A private final static char[] hexDigits = "0123456789abcdef".toCharArray();
0N/A
0N/A public static byte[] parse(String s) {
0N/A try {
0N/A int n = s.length();
0N/A ByteArrayOutputStream out = new ByteArrayOutputStream(n/3);
0N/A StringReader r = new StringReader(s);
0N/A while (true) {
0N/A int b1 = nextNibble(r);
0N/A if (b1 < 0) {
0N/A break;
0N/A }
0N/A int b2 = nextNibble(r);
0N/A if (b2 < 0) {
0N/A throw new RuntimeException("Invalid string " + s);
0N/A }
0N/A int b = (b1 << 4) | b2;
0N/A out.write(b);
0N/A }
0N/A return out.toByteArray();
0N/A } catch (IOException e) {
0N/A throw new RuntimeException(e);
0N/A }
0N/A }
0N/A
0N/A public static byte[] b(String s) {
0N/A return parse(s);
0N/A }
0N/A
0N/A private static int nextNibble(StringReader r) throws IOException {
0N/A while (true) {
0N/A int ch = r.read();
0N/A if (ch == -1) {
0N/A return -1;
0N/A } else if ((ch >= '0') && (ch <= '9')) {
0N/A return ch - '0';
0N/A } else if ((ch >= 'a') && (ch <= 'f')) {
0N/A return ch - 'a' + 10;
0N/A } else if ((ch >= 'A') && (ch <= 'F')) {
0N/A return ch - 'A' + 10;
0N/A }
0N/A }
0N/A }
0N/A
0N/A static abstract class Test {
0N/A abstract void run(Provider p) throws Exception;
0N/A }
0N/A
0N/A static class CipherTest extends Test {
0N/A private final String alg;
0N/A private final byte[] plaintext;
0N/A private final byte[] ciphertext;
0N/A private final char[] password;
0N/A private final byte[] salt;
0N/A private final int ic;
0N/A
0N/A CipherTest(String alg, byte[] plaintext, byte[] ciphertext,
0N/A char[] password, byte[] salt, int ic) {
0N/A this.alg = alg;
0N/A this.plaintext = plaintext;
0N/A this.ciphertext = ciphertext;
0N/A this.password = password;
0N/A this.salt = salt;
0N/A this.ic = ic;
0N/A }
0N/A
0N/A String hexDump(byte[] b) {
0N/A if (b == null) {
0N/A return "(null)";
0N/A }
0N/A StringBuffer sb = new StringBuffer(b.length * 3);
0N/A for (int i = 0; i < b.length; i++) {
0N/A int k = b[i] & 0xff;
0N/A if (i != 0) {
0N/A sb.append(':');
0N/A }
0N/A sb.append(hexDigits[k >>> 4]);
0N/A sb.append(hexDigits[k & 0xf]);
0N/A }
0N/A return sb.toString();
0N/A }
0N/A
0N/A void run(Provider p) throws Exception {
0N/A Cipher cipher = Cipher.getInstance(alg, p);
0N/A PBEKeySpec pbeKeySpec = new PBEKeySpec(password);
0N/A SecretKeyFactory keyFac = SecretKeyFactory.getInstance("PBE", p);
0N/A PBEParameterSpec pbeParamSpec = new PBEParameterSpec(salt, ic);
0N/A SecretKey key = keyFac.generateSecret(pbeKeySpec);
0N/A cipher.init(Cipher.ENCRYPT_MODE, key, pbeParamSpec);
0N/A byte[] enc = cipher.doFinal(plaintext);
0N/A if (Arrays.equals(ciphertext, enc) == false) {
0N/A System.out.println(
0N/A "Cipher test encryption for " + alg + " failed:");
0N/A System.out.println("plaintext: " + hexDump(plaintext));
0N/A System.out.println("ciphertext: " + hexDump(ciphertext));
0N/A System.out.println("encrypted: " + hexDump(enc));
0N/A System.out.println("password: " + password);
0N/A System.out.println("salt: " + hexDump(salt));
0N/A System.out.println("iterationCount: " + ic);
0N/A throw new Exception("encryption test for " + alg + " failed");
0N/A }
0N/A enc = cipher.doFinal(plaintext);
0N/A if (Arrays.equals(ciphertext, enc) == false) {
0N/A throw new Exception("Re-encryption test failed");
0N/A }
0N/A cipher.init(Cipher.DECRYPT_MODE, key, pbeParamSpec);
0N/A byte[] dec = cipher.doFinal(ciphertext);
0N/A if (Arrays.equals(plaintext, dec) == false) {
0N/A System.out.println("plaintext: " + hexDump(plaintext));
0N/A System.out.println("ciphertext: " + hexDump(ciphertext));
0N/A System.out.println("decrypted: " + hexDump(dec));
0N/A System.out.println("password: " + password);
0N/A System.out.println("salt: " + hexDump(salt));
0N/A System.out.println("iterationCount: " + ic);
0N/A throw new Exception("decryption test for " + alg + " failed");
0N/A }
0N/A System.out.println("passed: " + alg);
0N/A }
0N/A }
0N/A
0N/A private static Test t(String alg, String plaintext, char[] password,
0N/A String salt, int iterationCount, String ciphertext) {
0N/A return new CipherTest(alg, b(plaintext), b(ciphertext), password,
0N/A b(salt), iterationCount);
0N/A }
0N/A
0N/A private final static char[] PASSWD = { 'p','a','s','s','w','o','r','d' };
0N/A private final static Test[] tests = {
0N/A t("PBEWithSHA1AndDESede", INPUT, PASSWD, SALT, ITER_COUNT,
0N/A "95:94:49:5a:a2:cf:c9:a5:bb:21:08:23:45:41:46:a3:9c:c5:84:da:b5:04:ae:1a"),
0N/A t("PBEWithSHA1AndRC2_40", INPUT, PASSWD, SALT, ITER_COUNT,
0N/A "ec:32:f4:68:29:29:8b:c8:55:75:cb:ac:a4:01:d9:9c:b3:27:d6:b6:9f:26:98:f1")
0N/A };
0N/A
0N/A static void runTests(Test[] tests) throws Exception {
0N/A long start = System.currentTimeMillis();
0N/A Provider p = Security.getProvider("SunJCE");
0N/A System.out.println("Testing provider " + p.getName() + "...");
0N/A Cipher.getInstance("PBEWithSHA1AndRC2_40", p);
0N/A Cipher.getInstance("PBEWithSHA1AndDESede", p);
0N/A for (int i = 0; i < tests.length; i++) {
0N/A Test test = tests[i];
0N/A test.run(p);
0N/A }
0N/A System.out.println("All tests passed");
0N/A long stop = System.currentTimeMillis();
0N/A System.out.println("Done (" + (stop - start) + " ms).");
0N/A }
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A runTests(tests);
0N/A }
0N/A}