01: package org.bouncycastle.crypto.test;
02:
03: import org.bouncycastle.crypto.DataLengthException;
04: import org.bouncycastle.crypto.DerivationFunction;
05: import org.bouncycastle.crypto.digests.ShortenedDigest;
06: import org.bouncycastle.crypto.digests.SHA1Digest;
07: import org.bouncycastle.crypto.digests.SHA256Digest;
08: import org.bouncycastle.crypto.generators.KDF1BytesGenerator;
09: import org.bouncycastle.crypto.params.ISO18033KDFParameters;
10: import org.bouncycastle.util.encoders.Hex;
11: import org.bouncycastle.util.test.SimpleTest;
12:
13: /**
14: * KDF1 tests - vectors from ISO 18033.
15: */
16: public class KDF1GeneratorTest extends SimpleTest {
17: private byte[] seed1 = Hex
18: .decode("d6e168c5f256a2dcff7ef12facd390f393c7a88d");
19: private byte[] mask1 = Hex
20: .decode("0742ba966813af75536bb6149cc44fc256fd6406df79665bc31dc5"
21: + "a62f70535e52c53015b9d37d412ff3c1193439599e1b628774c50d9c"
22: + "cb78d82c425e4521ee47b8c36a4bcffe8b8112a89312fc04420a39de"
23: + "99223890e74ce10378bc515a212b97b8a6447ba6a8870278");
24:
25: private byte[] seed2 = Hex
26: .decode("032e45326fa859a72ec235acff929b15d1372e30b207255f0611b8f785d7643741"
27: + "52e0ac009e509e7ba30cd2f1778e113b64e135cf4e2292c75efe5288edfda4");
28: private byte[] mask2 = Hex
29: .decode("5f8de105b5e96b2e490ddecbd147dd1def7e3b8e0e6a26eb7b956ccb8b3bdc1ca9"
30: + "75bc57c3989e8fbad31a224655d800c46954840ff32052cdf0d640562bdfadfa263c"
31: + "fccf3c52b29f2af4a1869959bc77f854cf15bd7a25192985a842dbff8e13efee5b7e"
32: + "7e55bbe4d389647c686a9a9ab3fb889b2d7767d3837eea4e0a2f04");
33:
34: private byte[] seed3 = seed2;
35: private byte[] mask3 = Hex
36: .decode("09e2decf2a6e1666c2f6071ff4298305e2643fd510a2403db42a8743cb989de86e"
37: + "668d168cbe604611ac179f819a3d18412e9eb45668f2923c087c12fee0c5a0d2a8aa"
38: + "70185401fbbd99379ec76c663e875a60b4aacb1319fa11c3365a8b79a44669f26fb5"
39: + "55c80391847b05eca1cb5cf8c2d531448d33fbaca19f6410ee1fcb");
40:
41: public KDF1GeneratorTest() {
42: }
43:
44: public void performTest() {
45: checkMask(1, new KDF1BytesGenerator(new ShortenedDigest(
46: new SHA256Digest(), 20)), seed1, mask1);
47: checkMask(2, new KDF1BytesGenerator(new SHA1Digest()), seed2,
48: mask2);
49: checkMask(3, new KDF1BytesGenerator(new ShortenedDigest(
50: new SHA256Digest(), 20)), seed3, mask3);
51:
52: try {
53: new KDF1BytesGenerator(new SHA1Digest()).generateBytes(
54: new byte[10], 0, 20);
55:
56: fail("short input array not caught");
57: } catch (DataLengthException e) {
58: // expected
59: }
60: }
61:
62: private void checkMask(int count, DerivationFunction kdf,
63: byte[] seed, byte[] result) {
64: byte[] data = new byte[result.length];
65:
66: kdf.init(new ISO18033KDFParameters(seed));
67:
68: kdf.generateBytes(data, 0, data.length);
69:
70: if (!areEqual(result, data)) {
71: fail("KDF1 failed generator test " + count);
72: }
73: }
74:
75: public String getName() {
76: return "KDF1";
77: }
78:
79: public static void main(String[] args) {
80: runTest(new KDF1GeneratorTest());
81: }
82: }
|