Path: blob/master/test/jdk/com/sun/crypto/provider/Cipher/PBE/PKCS12CipherKAT.java
41161 views
/*1* Copyright (c) 2003, 2007, 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 489395926* @summary basic test for PBEWithSHA1AndDESede and27* PBEWithSHA1AndRC2_4028* @author Valerie Peng29*/3031import java.io.*;32import java.util.*;3334import java.security.*;3536import javax.crypto.*;37import javax.crypto.spec.*;3839public class PKCS12CipherKAT {4041private 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";42private final static String SALT = "7d:60:43:5f:02:e9:e0:ae";43private final static int ITER_COUNT = 2048;4445private final static char[] hexDigits = "0123456789abcdef".toCharArray();4647public static byte[] parse(String s) {48try {49int n = s.length();50ByteArrayOutputStream out = new ByteArrayOutputStream(n/3);51StringReader r = new StringReader(s);52while (true) {53int b1 = nextNibble(r);54if (b1 < 0) {55break;56}57int b2 = nextNibble(r);58if (b2 < 0) {59throw new RuntimeException("Invalid string " + s);60}61int b = (b1 << 4) | b2;62out.write(b);63}64return out.toByteArray();65} catch (IOException e) {66throw new RuntimeException(e);67}68}6970public static byte[] b(String s) {71return parse(s);72}7374private static int nextNibble(StringReader r) throws IOException {75while (true) {76int ch = r.read();77if (ch == -1) {78return -1;79} else if ((ch >= '0') && (ch <= '9')) {80return ch - '0';81} else if ((ch >= 'a') && (ch <= 'f')) {82return ch - 'a' + 10;83} else if ((ch >= 'A') && (ch <= 'F')) {84return ch - 'A' + 10;85}86}87}8889static abstract class Test {90abstract void run(Provider p) throws Exception;91}9293static class CipherTest extends Test {94private final String alg;95private final byte[] plaintext;96private final byte[] ciphertext;97private final char[] password;98private final byte[] salt;99private final int ic;100101CipherTest(String alg, byte[] plaintext, byte[] ciphertext,102char[] password, byte[] salt, int ic) {103this.alg = alg;104this.plaintext = plaintext;105this.ciphertext = ciphertext;106this.password = password;107this.salt = salt;108this.ic = ic;109}110111String hexDump(byte[] b) {112if (b == null) {113return "(null)";114}115StringBuffer sb = new StringBuffer(b.length * 3);116for (int i = 0; i < b.length; i++) {117int k = b[i] & 0xff;118if (i != 0) {119sb.append(':');120}121sb.append(hexDigits[k >>> 4]);122sb.append(hexDigits[k & 0xf]);123}124return sb.toString();125}126127void run(Provider p) throws Exception {128Cipher cipher = Cipher.getInstance(alg, p);129PBEKeySpec pbeKeySpec = new PBEKeySpec(password);130SecretKeyFactory keyFac = SecretKeyFactory.getInstance("PBE", p);131PBEParameterSpec pbeParamSpec = new PBEParameterSpec(salt, ic);132SecretKey key = keyFac.generateSecret(pbeKeySpec);133cipher.init(Cipher.ENCRYPT_MODE, key, pbeParamSpec);134byte[] enc = cipher.doFinal(plaintext);135if (Arrays.equals(ciphertext, enc) == false) {136System.out.println(137"Cipher test encryption for " + alg + " failed:");138System.out.println("plaintext: " + hexDump(plaintext));139System.out.println("ciphertext: " + hexDump(ciphertext));140System.out.println("encrypted: " + hexDump(enc));141System.out.println("password: " + password);142System.out.println("salt: " + hexDump(salt));143System.out.println("iterationCount: " + ic);144throw new Exception("encryption test for " + alg + " failed");145}146enc = cipher.doFinal(plaintext);147if (Arrays.equals(ciphertext, enc) == false) {148throw new Exception("Re-encryption test failed");149}150cipher.init(Cipher.DECRYPT_MODE, key, pbeParamSpec);151byte[] dec = cipher.doFinal(ciphertext);152if (Arrays.equals(plaintext, dec) == false) {153System.out.println("plaintext: " + hexDump(plaintext));154System.out.println("ciphertext: " + hexDump(ciphertext));155System.out.println("decrypted: " + hexDump(dec));156System.out.println("password: " + password);157System.out.println("salt: " + hexDump(salt));158System.out.println("iterationCount: " + ic);159throw new Exception("decryption test for " + alg + " failed");160}161System.out.println("passed: " + alg);162}163}164165private static Test t(String alg, String plaintext, char[] password,166String salt, int iterationCount, String ciphertext) {167return new CipherTest(alg, b(plaintext), b(ciphertext), password,168b(salt), iterationCount);169}170171private final static char[] PASSWD = { 'p','a','s','s','w','o','r','d' };172private final static Test[] tests = {173t("PBEWithSHA1AndDESede", INPUT, PASSWD, SALT, ITER_COUNT,174"95:94:49:5a:a2:cf:c9:a5:bb:21:08:23:45:41:46:a3:9c:c5:84:da:b5:04:ae:1a"),175t("PBEWithSHA1AndRC2_40", INPUT, PASSWD, SALT, ITER_COUNT,176"ec:32:f4:68:29:29:8b:c8:55:75:cb:ac:a4:01:d9:9c:b3:27:d6:b6:9f:26:98:f1")177};178179static void runTests(Test[] tests) throws Exception {180long start = System.currentTimeMillis();181Provider p = Security.getProvider("SunJCE");182System.out.println("Testing provider " + p.getName() + "...");183Cipher.getInstance("PBEWithSHA1AndRC2_40", p);184Cipher.getInstance("PBEWithSHA1AndDESede", p);185for (int i = 0; i < tests.length; i++) {186Test test = tests[i];187test.run(p);188}189System.out.println("All tests passed");190long stop = System.currentTimeMillis();191System.out.println("Done (" + (stop - start) + " ms).");192}193194public static void main(String[] args) throws Exception {195runTests(tests);196}197}198199200