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.SHA1Digest;
06: import org.bouncycastle.crypto.digests.SHA256Digest;
07: import org.bouncycastle.crypto.digests.ShortenedDigest;
08: import org.bouncycastle.crypto.generators.MGF1BytesGenerator;
09: import org.bouncycastle.crypto.params.MGFParameters;
10: import org.bouncycastle.util.encoders.Hex;
11: import org.bouncycastle.util.test.SimpleTest;
12:
13: /**
14: * MGF1 tests - vectors from ISO 18033 for KDF1 (equivalent).
15: */
16: public class MGF1GeneratorTest 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 void performTest() {
42: checkMask(1, new MGF1BytesGenerator(new ShortenedDigest(
43: new SHA256Digest(), 20)), seed1, mask1);
44: checkMask(2, new MGF1BytesGenerator(new SHA1Digest()), seed2,
45: mask2);
46: checkMask(3, new MGF1BytesGenerator(new ShortenedDigest(
47: new SHA256Digest(), 20)), seed3, mask3);
48:
49: try {
50: new MGF1BytesGenerator(new SHA1Digest()).generateBytes(
51: new byte[10], 0, 20);
52:
53: fail("short input array not caught");
54: } catch (DataLengthException e) {
55: // expected
56: }
57: }
58:
59: private void checkMask(int count, DerivationFunction kdf,
60: byte[] seed, byte[] result) {
61: byte[] data = new byte[result.length];
62:
63: kdf.init(new MGFParameters(seed));
64:
65: kdf.generateBytes(data, 0, data.length);
66:
67: if (!areEqual(result, data)) {
68: fail("MGF1 failed generator test " + count);
69: }
70: }
71:
72: public String getName() {
73: return "MGF1";
74: }
75:
76: public static void main(String[] args) {
77: runTest(new MGF1GeneratorTest());
78: }
79: }
|