|
1 /* |
|
2 * Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
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". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * This program generates keys with Bouncy Castle for compatibility testing. |
|
16 * |
|
17 */ |
|
18 |
|
19 |
|
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; |
|
25 |
|
26 public class GenTestDKs |
|
27 { |
|
28 public static void main(String[] args) |
|
29 { |
|
30 PKCS12ParametersGenerator pgen = new PKCS12ParametersGenerator(new SHA1Digest()); |
|
31 |
|
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}; |
|
35 |
|
36 // SB.4 iteration count is recommended to be 1024 or more |
|
37 final int[] iterCounts = {1, 2, 4, 8, 128, 1024, 1536, 2048}; |
|
38 |
|
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]); |
|
45 |
|
46 // calls to nextBytes() are only executed once |
|
47 /* SecureRandom sr; |
|
48 try { sr = SecureRandom.getInstance("SHA1PRNG", "SUN"); } |
|
49 catch (Exception e) |
|
50 { |
|
51 System.err.println("UNABLE TO GET RANDOM SOURCE"); |
|
52 return; |
|
53 } |
|
54 */ |
|
55 // salts[1] = new byte[160 / 8]; |
|
56 // sr.nextBytes(salts[1]); |
|
57 salts[1] = new byte[] |
|
58 { |
|
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 |
|
64 }; |
|
65 System.out.println("20 byte salt (same size as SHA1 output)"); |
|
66 printByteArray(salts[1]); |
|
67 |
|
68 // salts[2] = new byte[200 / 8]; |
|
69 // sr.nextBytes(salts[2]); |
|
70 salts[2] = new byte[] |
|
71 { |
|
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, |
|
78 (byte) 0x61 |
|
79 }; |
|
80 System.out.println("25 byte salt"); |
|
81 printByteArray(salts[2]); |
|
82 |
|
83 final String passwds[] = {"0000", "0001", "PSWD", "password", "abcdefghijklmnopqrstuvwxyz"}; |
|
84 |
|
85 for (int keyLenIdx = 0; keyLenIdx < keyLens.length; ++keyLenIdx) |
|
86 { |
|
87 for (int iterIdx = 0; iterIdx < iterCounts.length; ++iterIdx) |
|
88 { |
|
89 for (int saltIdx = 0; saltIdx < salts.length; ++saltIdx) |
|
90 { |
|
91 for (int pwdIdx = 0; pwdIdx < passwds.length; ++pwdIdx) |
|
92 { |
|
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) |
|
98 } |
|
99 |
|
100 private static void testKey( |
|
101 PKCS12ParametersGenerator pgen, |
|
102 int keyLen, int iterCount, String password, byte[] salt) |
|
103 { |
|
104 System.out.println( |
|
105 "key len = " + keyLen + ", iter count = " + iterCount |
|
106 + ", password = \"" + password + "\", salt len = " + salt.length); |
|
107 |
|
108 char[] pwChars = password.toCharArray(); |
|
109 byte[] pwBytes = PBEParametersGenerator.PKCS12PasswordToBytes(pwChars); |
|
110 |
|
111 pgen.init(pwBytes, salt, iterCount); |
|
112 KeyParameter kp = (KeyParameter) pgen.generateDerivedParameters(keyLen); |
|
113 printByteArray(kp.getKey()); |
|
114 } |
|
115 |
|
116 private static void printByteArray(byte[] a) |
|
117 { |
|
118 final int BLOCK_SIZE = 16; |
|
119 int keyLen = a.length; |
|
120 int rowCount = keyLen / BLOCK_SIZE; |
|
121 if ((keyLen % BLOCK_SIZE) != 0) |
|
122 ++rowCount; |
|
123 |
|
124 for (int row = 0; row < rowCount; ++row) |
|
125 { |
|
126 int start = row * BLOCK_SIZE; |
|
127 int end = Math.min(start + BLOCK_SIZE, keyLen); |
|
128 |
|
129 StringBuffer line = new StringBuffer("[" + hexStr(start, 4) + "]"); |
|
130 |
|
131 for (int i = start; i < end; ++i) |
|
132 line.append(" " + hexStr(a[i], 2)); |
|
133 System.out.println(line); |
|
134 } |
|
135 System.out.println(); |
|
136 } |
|
137 |
|
138 private static String hexStr(int val, int width) |
|
139 { |
|
140 StringBuffer result = new StringBuffer(); |
|
141 while (--width >= 0) |
|
142 { |
|
143 int bitPos = 4 * width; |
|
144 int nybble = (val & (0xf << bitPos)) >> bitPos; |
|
145 result.append(Integer.toHexString(nybble)); |
|
146 } |
|
147 |
|
148 return result.toString(); |
|
149 } |
|
150 } |