Update contrib.
2 * Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
4 * This component and the accompanying materials are made available
5 * under the terms of the License "Eclipse Public License v1.0"
6 * which accompanies this distribution, and is available
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
15 * This program generates keys with Bouncy Castle for compatibility testing.
20 import java.security.SecureRandom;
21 import org.bouncycastle.crypto.PBEParametersGenerator;
22 import org.bouncycastle.crypto.digests.SHA1Digest;
23 import org.bouncycastle.crypto.generators.PKCS12ParametersGenerator;
24 import org.bouncycastle.crypto.params.KeyParameter;
26 public class GenTestDKs
28 public static void main(String[] args)
30 PKCS12ParametersGenerator pgen = new PKCS12ParametersGenerator(new SHA1Digest());
32 // SB.4: key lengths for defined OIDs
33 // (168 for triple DES will first exercise chaining.)
34 final int[] keyLens = {40, 128, 168, 368};
36 // SB.4 iteration count is recommended to be 1024 or more
37 final int[] iterCounts = {1, 2, 4, 8, 128, 1024, 1536, 2048};
39 // SB.4 salt should be same length as hash function output
40 // (=160 bits for SHA1.)
41 byte[][] salts = new byte[3][];
42 salts[0] = new byte[] {'S', 'A', 'L', 'T'};
43 System.out.println("4 byte salt");
44 printByteArray(salts[0]);
46 // calls to nextBytes() are only executed once
48 try { sr = SecureRandom.getInstance("SHA1PRNG", "SUN"); }
51 System.err.println("UNABLE TO GET RANDOM SOURCE");
55 // salts[1] = new byte[160 / 8];
56 // sr.nextBytes(salts[1]);
59 (byte) 0x1d, (byte) 0x56, (byte) 0x50, (byte) 0x78,
60 (byte) 0xc3, (byte) 0x50, (byte) 0x6f, (byte) 0x89,
61 (byte) 0xbd, (byte) 0xa7, (byte) 0x3b, (byte) 0xb6,
62 (byte) 0xe3, (byte) 0xe5, (byte) 0xb8, (byte) 0xa3,
63 (byte) 0x68, (byte) 0x3d, (byte) 0xd3, (byte) 0x62
65 System.out.println("20 byte salt (same size as SHA1 output)");
66 printByteArray(salts[1]);
68 // salts[2] = new byte[200 / 8];
69 // sr.nextBytes(salts[2]);
72 (byte) 0xe2, (byte) 0x2c, (byte) 0x7b, (byte) 0x03,
73 (byte) 0x16, (byte) 0x3a, (byte) 0xe5, (byte) 0x47,
74 (byte) 0xf8, (byte) 0x23, (byte) 0x9d, (byte) 0xa4,
75 (byte) 0x0d, (byte) 0x6f, (byte) 0x46, (byte) 0xd7,
76 (byte) 0x9e, (byte) 0xa3, (byte) 0xc6, (byte) 0xff,
77 (byte) 0xb3, (byte) 0xf0, (byte) 0x4e, (byte) 0xbe,
80 System.out.println("25 byte salt");
81 printByteArray(salts[2]);
83 final String passwds[] = {"0000", "0001", "PSWD", "password", "abcdefghijklmnopqrstuvwxyz"};
85 for (int keyLenIdx = 0; keyLenIdx < keyLens.length; ++keyLenIdx)
87 for (int iterIdx = 0; iterIdx < iterCounts.length; ++iterIdx)
89 for (int saltIdx = 0; saltIdx < salts.length; ++saltIdx)
91 for (int pwdIdx = 0; pwdIdx < passwds.length; ++pwdIdx)
93 testKey(pgen, keyLens[keyLenIdx], iterCounts[iterIdx], passwds[pwdIdx], salts[saltIdx]);
94 } // for (int pwdIdx = 0; pwdIdx < passwds.length; ++pwdIdx)
95 } // for (int saltIdx = 0; saltIdx < salts.length; ++saltIdx)
96 } // for (int iterIdx = 0; iterIdx < iterCounts.length; ++iterIdx)
97 } // for (int keyLenIdx = 0; keyLenIdx < keyLens.length; ++keyLenIdx)
100 private static void testKey(
101 PKCS12ParametersGenerator pgen,
102 int keyLen, int iterCount, String password, byte[] salt)
105 "key len = " + keyLen + ", iter count = " + iterCount
106 + ", password = \"" + password + "\", salt len = " + salt.length);
108 char[] pwChars = password.toCharArray();
109 byte[] pwBytes = PBEParametersGenerator.PKCS12PasswordToBytes(pwChars);
111 pgen.init(pwBytes, salt, iterCount);
112 KeyParameter kp = (KeyParameter) pgen.generateDerivedParameters(keyLen);
113 printByteArray(kp.getKey());
116 private static void printByteArray(byte[] a)
118 final int BLOCK_SIZE = 16;
119 int keyLen = a.length;
120 int rowCount = keyLen / BLOCK_SIZE;
121 if ((keyLen % BLOCK_SIZE) != 0)
124 for (int row = 0; row < rowCount; ++row)
126 int start = row * BLOCK_SIZE;
127 int end = Math.min(start + BLOCK_SIZE, keyLen);
129 StringBuffer line = new StringBuffer("[" + hexStr(start, 4) + "]");
131 for (int i = start; i < end; ++i)
132 line.append(" " + hexStr(a[i], 2));
133 System.out.println(line);
135 System.out.println();
138 private static String hexStr(int val, int width)
140 StringBuffer result = new StringBuffer();
143 int bitPos = 4 * width;
144 int nybble = (val & (0xf << bitPos)) >> bitPos;
145 result.append(Integer.toHexString(nybble));
148 return result.toString();