01: package org.bouncycastle.crypto.test;
02:
03: import org.bouncycastle.crypto.Mac;
04: import org.bouncycastle.crypto.engines.GOST28147Engine;
05: import org.bouncycastle.crypto.macs.GOST28147Mac;
06: import org.bouncycastle.crypto.params.KeyParameter;
07: import org.bouncycastle.crypto.params.ParametersWithSBox;
08: import org.bouncycastle.util.Arrays;
09: import org.bouncycastle.util.encoders.Hex;
10: import org.bouncycastle.util.test.SimpleTestResult;
11: import org.bouncycastle.util.test.Test;
12: import org.bouncycastle.util.test.TestResult;
13:
14: /**
15: * GOST 28147 MAC tester
16: */
17: public class GOST28147MacTest implements Test {
18: //
19: // these GOSTMac for testing.
20: //
21: static byte[] gkeyBytes1 = Hex
22: .decode("6d145dc993f4019e104280df6fcd8cd8e01e101e4c113d7ec4f469ce6dcd9e49");
23: static byte[] gkeyBytes2 = Hex
24: .decode("6d145dc993f4019e104280df6fcd8cd8e01e101e4c113d7ec4f469ce6dcd9e49");
25:
26: static byte[] input3 = Hex
27: .decode("7768617420646f2079612077616e7420666f72206e6f7468696e673f");
28: static byte[] input4 = Hex
29: .decode("7768617420646f2079612077616e7420666f72206e6f7468696e673f");
30:
31: static byte[] output7 = Hex.decode("93468a46");
32: static byte[] output8 = Hex.decode("93468a46");
33:
34: public GOST28147MacTest() {
35: }
36:
37: public TestResult perform() {
38: // test1
39: Mac mac = new GOST28147Mac();
40: KeyParameter key = new KeyParameter(gkeyBytes1);
41:
42: mac.init(key);
43:
44: mac.update(input3, 0, input3.length);
45:
46: byte[] out = new byte[4];
47:
48: mac.doFinal(out, 0);
49:
50: if (!Arrays.areEqual(out, output7)) {
51: return new SimpleTestResult(false, getName()
52: + ": Failed test 1 - expected "
53: + new String(Hex.encode(output7)) + " got "
54: + new String(Hex.encode(out)));
55: }
56:
57: // test2
58: key = new KeyParameter(gkeyBytes2);
59:
60: ParametersWithSBox gparam = new ParametersWithSBox(key,
61: GOST28147Engine.getSBox("E-A"));
62:
63: mac.init(gparam);
64:
65: mac.update(input4, 0, input4.length);
66:
67: out = new byte[4];
68:
69: mac.doFinal(out, 0);
70:
71: if (!Arrays.areEqual(out, output8)) {
72: return new SimpleTestResult(false, getName()
73: + ": Failed test 2 - expected "
74: + new String(Hex.encode(output8)) + " got "
75: + new String(Hex.encode(out)));
76: }
77:
78: return new SimpleTestResult(true, getName() + ": Okay");
79: }
80:
81: public String getName() {
82: return "GOST28147Mac";
83: }
84:
85: public static void main(String[] args) {
86: GOST28147MacTest test = new GOST28147MacTest();
87: TestResult result = test.perform();
88:
89: System.out.println(result);
90: }
91: }
|