001: package org.bouncycastle.crypto.test;
002:
003: import org.bouncycastle.crypto.CipherParameters;
004: import org.bouncycastle.crypto.PBEParametersGenerator;
005: import org.bouncycastle.crypto.digests.SHA1Digest;
006: import org.bouncycastle.crypto.generators.PKCS12ParametersGenerator;
007: import org.bouncycastle.crypto.params.KeyParameter;
008: import org.bouncycastle.crypto.params.ParametersWithIV;
009: import org.bouncycastle.util.encoders.Hex;
010: import org.bouncycastle.util.test.SimpleTestResult;
011: import org.bouncycastle.util.test.Test;
012: import org.bouncycastle.util.test.TestResult;
013:
014: /**
015: * test for PKCS12 key generation - vectors from
016: * <a href=http://www.drh-consultancy.demon.co.uk/test.txt>
017: * http://www.drh-consultancy.demon.co.uk/test.txt</a>
018: */
019: public class PKCS12Test implements Test {
020: char[] password1 = { 's', 'm', 'e', 'g' };
021: char[] password2 = { 'q', 'u', 'e', 'e', 'g' };
022:
023: private boolean isEqual(byte[] a, byte[] b) {
024: if (a.length != b.length) {
025: return false;
026: }
027:
028: for (int i = 0; i != a.length; i++) {
029: if (a[i] != b[i]) {
030: return false;
031: }
032: }
033:
034: return true;
035: }
036:
037: private TestResult run1(int id, char[] password, byte[] salt,
038: int iCount, byte[] result) {
039: PBEParametersGenerator generator = new PKCS12ParametersGenerator(
040: new SHA1Digest());
041:
042: generator.init(PBEParametersGenerator
043: .PKCS12PasswordToBytes(password), salt, iCount);
044:
045: CipherParameters key = generator
046: .generateDerivedParameters(24 * 8);
047:
048: if (isEqual(result, ((KeyParameter) key).getKey())) {
049: return new SimpleTestResult(true, "PKCS12Test: Okay");
050: } else {
051: return new SimpleTestResult(false, "PKCS12Test: id " + id
052: + " Failed");
053: }
054: }
055:
056: private TestResult run2(int id, char[] password, byte[] salt,
057: int iCount, byte[] result) {
058: PBEParametersGenerator generator = new PKCS12ParametersGenerator(
059: new SHA1Digest());
060:
061: generator.init(PBEParametersGenerator
062: .PKCS12PasswordToBytes(password), salt, iCount);
063:
064: ParametersWithIV params = (ParametersWithIV) generator
065: .generateDerivedParameters(64, 64);
066:
067: if (isEqual(result, params.getIV())) {
068: return new SimpleTestResult(true, "PKCS12Test: Okay");
069: } else {
070: return new SimpleTestResult(false, "PKCS12Test: id " + id
071: + " Failed");
072: }
073: }
074:
075: private TestResult run3(int id, char[] password, byte[] salt,
076: int iCount, byte[] result) {
077: PBEParametersGenerator generator = new PKCS12ParametersGenerator(
078: new SHA1Digest());
079:
080: generator.init(PBEParametersGenerator
081: .PKCS12PasswordToBytes(password), salt, iCount);
082:
083: CipherParameters key = generator
084: .generateDerivedMacParameters(160);
085:
086: if (isEqual(result, ((KeyParameter) key).getKey())) {
087: return new SimpleTestResult(true, "PKCS12Test: Okay");
088: } else {
089: return new SimpleTestResult(false, "PKCS12Test: id " + id
090: + " Failed");
091: }
092: }
093:
094: public String getName() {
095: return "PKCS12Test";
096: }
097:
098: public TestResult perform() {
099: TestResult result;
100:
101: result = run1(
102: 1,
103: password1,
104: Hex.decode("0A58CF64530D823F"),
105: 1,
106: Hex
107: .decode("8AAAE6297B6CB04642AB5B077851284EB7128F1A2A7FBCA3"));
108:
109: if (result.isSuccessful()) {
110: result = run2(2, password1, Hex.decode("0A58CF64530D823F"),
111: 1, Hex.decode("79993DFE048D3B76"));
112: }
113:
114: if (result.isSuccessful()) {
115: result = run1(
116: 3,
117: password1,
118: Hex.decode("642B99AB44FB4B1F"),
119: 1,
120: Hex
121: .decode("F3A95FEC48D7711E985CFE67908C5AB79FA3D7C5CAA5D966"));
122: }
123:
124: if (result.isSuccessful()) {
125: result = run2(4, password1, Hex.decode("642B99AB44FB4B1F"),
126: 1, Hex.decode("C0A38D64A79BEA1D"));
127: }
128:
129: if (result.isSuccessful()) {
130: result = run3(
131: 5,
132: password1,
133: Hex.decode("3D83C0E4546AC140"),
134: 1,
135: Hex
136: .decode("8D967D88F6CAA9D714800AB3D48051D63F73A312"));
137: }
138:
139: if (result.isSuccessful()) {
140: result = run1(
141: 6,
142: password2,
143: Hex.decode("05DEC959ACFF72F7"),
144: 1000,
145: Hex
146: .decode("ED2034E36328830FF09DF1E1A07DD357185DAC0D4F9EB3D4"));
147: }
148:
149: if (result.isSuccessful()) {
150: result = run2(7, password2, Hex.decode("05DEC959ACFF72F7"),
151: 1000, Hex.decode("11DEDAD7758D4860"));
152: }
153:
154: if (result.isSuccessful()) {
155: result = run1(
156: 8,
157: password2,
158: Hex.decode("1682C0FC5B3F7EC5"),
159: 1000,
160: Hex
161: .decode("483DD6E919D7DE2E8E648BA8F862F3FBFBDC2BCB2C02957F"));
162: }
163:
164: if (result.isSuccessful()) {
165: result = run2(9, password2, Hex.decode("1682C0FC5B3F7EC5"),
166: 1000, Hex.decode("9D461D1B00355C50"));
167: }
168:
169: if (result.isSuccessful()) {
170: result = run3(
171: 10,
172: password2,
173: Hex.decode("263216FCC2FAB31C"),
174: 1000,
175: Hex
176: .decode("5EC4C7A80DF652294C3925B6489A7AB857C83476"));
177: }
178:
179: return result;
180: }
181:
182: public static void main(String[] args) {
183: PKCS12Test test = new PKCS12Test();
184: TestResult result = test.perform();
185:
186: System.out.println(result);
187: }
188: }
|