001: package org.bouncycastle.crypto.test;
002:
003: import org.bouncycastle.util.test.SimpleTest;
004: import org.bouncycastle.util.encoders.Hex;
005: import org.bouncycastle.crypto.digests.GOST3411Digest;
006: import org.bouncycastle.crypto.engines.GOST28147Engine;
007: import org.bouncycastle.crypto.params.KeyParameter;
008: import org.bouncycastle.crypto.params.ParametersWithIV;
009: import org.bouncycastle.crypto.params.ParametersWithSBox;
010: import org.bouncycastle.crypto.modes.CFBBlockCipher;
011: import org.bouncycastle.crypto.modes.GOFBBlockCipher;
012: import org.bouncycastle.crypto.modes.CBCBlockCipher;
013: import org.bouncycastle.crypto.CipherParameters;
014: import org.bouncycastle.crypto.BufferedBlockCipher;
015: import org.bouncycastle.crypto.CryptoException;
016:
017: public class GOST28147Test extends CipherTest {
018: static String input1 = "0000000000000000";
019: static String output1 = "1b0bbc32cebcab42";
020: static String input2 = "bc350e71aac5f5c2";
021: static String output2 = "d35ab653493b49f5";
022: static String input3 = "bc350e71aa11345709acde";
023: static String output3 = "8824c124c4fd14301fb1e8";
024: static String input4 = "000102030405060708090a0b0c0d0e0fff0102030405060708090a0b0c0d0e0f";
025: static String output4 = "29b7083e0a6d955ca0ec5b04fdb4ea41949f1dd2efdf17baffc1780b031f3934";
026:
027: static byte TestSBox[] = { 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
028: 0x8, 0x9, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF, 0xF, 0xE, 0xD, 0xC,
029: 0xB, 0xA, 0x9, 0x8, 0x7, 0x6, 0x5, 0x4, 0x3, 0x2, 0x1, 0x0,
030: 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xA, 0xB,
031: 0xC, 0xD, 0xE, 0xF, 0xF, 0xE, 0xD, 0xC, 0xB, 0xA, 0x9, 0x8,
032: 0x7, 0x6, 0x5, 0x4, 0x3, 0x2, 0x1, 0x0, 0x0, 0x1, 0x2, 0x3,
033: 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF,
034: 0xF, 0xE, 0xD, 0xC, 0xB, 0xA, 0x9, 0x8, 0x7, 0x6, 0x5, 0x4,
035: 0x3, 0x2, 0x1, 0x0, 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
036: 0x8, 0x9, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF, 0xF, 0xE, 0xD, 0xC,
037: 0xB, 0xA, 0x9, 0x8, 0x7, 0x6, 0x5, 0x4, 0x3, 0x2, 0x1, 0x0 };
038:
039: static SimpleTest[] tests = {
040: new BlockCipherVectorTest(
041: 1,
042: new GOST28147Engine(),
043: new KeyParameter(
044: Hex
045: .decode("546d203368656c326973652073736e62206167796967747473656865202c3d73")),
046: input1, output1),
047: new BlockCipherVectorTest(
048: 2,
049: new CBCBlockCipher(new GOST28147Engine()),
050: new ParametersWithIV(
051: new KeyParameter(
052: Hex
053: .decode("00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF")),
054: Hex.decode("1234567890abcdef")), input2,
055: output2),
056: new BlockCipherVectorTest(
057: 3,
058: new GOFBBlockCipher(new GOST28147Engine()),
059: new ParametersWithIV(
060: new KeyParameter(
061: Hex
062: .decode("0011223344556677889900112233445566778899001122334455667788990011")),
063: Hex.decode("1234567890abcdef")), //IV
064: input3, output3),
065: new BlockCipherVectorTest(
066: 4,
067: new CFBBlockCipher(new GOST28147Engine(), 64),
068: new ParametersWithIV(
069: new KeyParameter(
070: Hex
071: .decode("aafd12f659cae63489b479e5076ddec2f06cb58faafd12f659cae63489b479e5")),
072: Hex.decode("aafd12f659cae634")), input4,
073: output4),
074:
075: //tests with parameters, set S-box.
076: new BlockCipherVectorTest(
077: 5,
078: new GOST28147Engine(),
079: new KeyParameter(
080: Hex
081: .decode("546d203368656c326973652073736e62206167796967747473656865202c3d73")),//key , default parameter S-box set to D-Test
082: input1, output1),
083: new BlockCipherVectorTest(
084: 6,
085: new CFBBlockCipher(new GOST28147Engine(), 64),
086: new ParametersWithIV(
087: new ParametersWithSBox(
088: new KeyParameter(
089: Hex
090: .decode("546d203368656c326973652073736e62206167796967747473656865202c3d73")), //key
091: GOST28147Engine.getSBox("D-Test")), //type S-box
092: Hex.decode("1234567890abcdef")), //IV
093: "0000000000000000", //input message
094: "b587f7a0814c911d"), //encrypt message
095: new BlockCipherVectorTest(
096: 7,
097: new CFBBlockCipher(new GOST28147Engine(), 64),
098: new ParametersWithIV(
099: new ParametersWithSBox(
100: new KeyParameter(
101: Hex
102: .decode("546d203368656c326973652073736e62206167796967747473656865202c3d73")), //key
103: GOST28147Engine.getSBox("E-Test")), //type S-box
104: Hex.decode("1234567890abcdef")), //IV
105: "0000000000000000", //input message
106: "e8287f53f991d52b"), //encrypt message
107: new BlockCipherVectorTest(
108: 8,
109: new CFBBlockCipher(new GOST28147Engine(), 64),
110: new ParametersWithIV(
111: new ParametersWithSBox(
112: new KeyParameter(
113: Hex
114: .decode("546d203368656c326973652073736e62206167796967747473656865202c3d73")), //key
115: GOST28147Engine.getSBox("E-A")), //type S-box
116: Hex.decode("1234567890abcdef")), //IV
117: "0000000000000000", //input message
118: "c41009dba22ebe35"), //encrypt message
119: new BlockCipherVectorTest(
120: 9,
121: new CFBBlockCipher(new GOST28147Engine(), 8),
122: new ParametersWithIV(
123: new ParametersWithSBox(
124: new KeyParameter(
125: Hex
126: .decode("546d203368656c326973652073736e62206167796967747473656865202c3d73")), //key
127: GOST28147Engine.getSBox("E-B")), //type S-box
128: Hex.decode("1234567890abcdef")), //IV
129: "0000000000000000", //input message
130: "80d8723fcd3aba28"), //encrypt message
131: new BlockCipherVectorTest(
132: 10,
133: new CFBBlockCipher(new GOST28147Engine(), 8),
134: new ParametersWithIV(
135: new ParametersWithSBox(
136: new KeyParameter(
137: Hex
138: .decode("546d203368656c326973652073736e62206167796967747473656865202c3d73")), //key
139: GOST28147Engine.getSBox("E-C")), //type S-box
140: Hex.decode("1234567890abcdef")), //IV
141: "0000000000000000", //input message
142: "739f6f95068499b5"), //encrypt message
143: new BlockCipherVectorTest(
144: 11,
145: new CFBBlockCipher(new GOST28147Engine(), 8),
146: new ParametersWithIV(
147: new ParametersWithSBox(
148: new KeyParameter(
149: Hex
150: .decode("546d203368656c326973652073736e62206167796967747473656865202c3d73")), //key
151: GOST28147Engine.getSBox("E-D")), //type S-box
152: Hex.decode("1234567890abcdef")), //IV
153: "0000000000000000", //input message
154: "4663f720f4340f57"), //encrypt message
155: new BlockCipherVectorTest(
156: 12,
157: new CFBBlockCipher(new GOST28147Engine(), 8),
158: new ParametersWithIV(
159: new ParametersWithSBox(
160: new KeyParameter(
161: Hex
162: .decode("546d203368656c326973652073736e62206167796967747473656865202c3d73")), //key
163: GOST28147Engine.getSBox("D-A")), //type S-box
164: Hex.decode("1234567890abcdef")), //IV
165: "0000000000000000", //input message
166: "5bb0a31d218ed564"), //encrypt message
167: new BlockCipherVectorTest(
168: 13,
169: new CFBBlockCipher(new GOST28147Engine(), 8),
170: new ParametersWithIV(
171: new ParametersWithSBox(
172: new KeyParameter(
173: Hex
174: .decode("546d203368656c326973652073736e62206167796967747473656865202c3d73")), //key
175: TestSBox), //set own S-box
176: Hex.decode("1234567890abcdef")), //IV
177: "0000000000000000", //input message
178: "c3af96ef788667c5"), //encrypt message
179: new BlockCipherVectorTest(
180: 14,
181: new GOFBBlockCipher(new GOST28147Engine()),
182: new ParametersWithIV(
183: new ParametersWithSBox(
184: new KeyParameter(
185: Hex
186: .decode("4ef72b778f0b0bebeef4f077551cb74a927b470ad7d7f2513454569a247e989d")), //key
187: GOST28147Engine.getSBox("E-A")), //type S-box
188: Hex.decode("1234567890abcdef")), //IV
189: "bc350e71aa11345709acde", //input message
190: "1bcc2282707c676fb656dc"), //encrypt message
191:
192: };
193:
194: static private final int GOST28147_KEY_LENGTH = 32;
195:
196: private byte[] generateKey(byte[] startkey) {
197: byte[] newKey = new byte[GOST28147_KEY_LENGTH];
198:
199: GOST3411Digest digest = new GOST3411Digest();
200:
201: digest.update(startkey, 0, startkey.length);
202: digest.doFinal(newKey, 0);
203:
204: return newKey;
205: }
206:
207: GOST28147Test() {
208: super (tests, new GOST28147Engine(), new KeyParameter(
209: new byte[32]));
210: }
211:
212: public void performTest() throws Exception {
213: super .performTest();
214:
215: //advanced tests with GOST28147KeyGenerator:
216: //encrypt on hesh message; ECB mode:
217: byte[] in = Hex
218: .decode("4e6f77206973207468652074696d6520666f7220616c6c20");
219: byte[] output = Hex
220: .decode("8ad3c8f56b27ff1fbd46409359bdc796bc350e71aac5f5c0");
221: byte[] out = new byte[in.length];
222:
223: byte[] key = generateKey(Hex.decode("0123456789abcdef")); //!!! heshing start_key - get 256 bits !!!
224: // System.out.println(new String(Hex.encode(key)));
225: CipherParameters param = new ParametersWithSBox(
226: new KeyParameter(key), GOST28147Engine.getSBox("E-A"));
227: //CipherParameters param = new GOST28147Parameters(key,"D-Test");
228: BufferedBlockCipher cipher = new BufferedBlockCipher(
229: new GOST28147Engine());
230:
231: cipher.init(true, param);
232: int len1 = cipher.processBytes(in, 0, in.length, out, 0);
233: try {
234: cipher.doFinal(out, len1);
235: } catch (CryptoException e) {
236: fail("failed - exception " + e.toString(), e);
237: }
238: if (out.length != output.length) {
239: fail("failed - " + "expected "
240: + new String(Hex.encode(output)) + " got "
241: + new String(Hex.encode(out)));
242: }
243: for (int i = 0; i != out.length; i++) {
244: if (out[i] != output[i]) {
245: fail("failed - " + "expected "
246: + new String(Hex.encode(output)) + " got "
247: + new String(Hex.encode(out)));
248: }
249: }
250:
251: //encrypt on hesh message; CFB mode:
252: in = Hex.decode("bc350e71aac5f5c2");
253: output = Hex.decode("0ebbbafcf38f14a5");
254: out = new byte[in.length];
255:
256: key = generateKey(Hex.decode("0123456789abcdef")); //!!! heshing start_key - get 256 bits !!!
257: param = new ParametersWithIV(new ParametersWithSBox(
258: new KeyParameter(key), //key
259: GOST28147Engine.getSBox("E-A")), //type S-box
260: Hex.decode("1234567890abcdef")); //IV
261:
262: cipher = new BufferedBlockCipher(new CFBBlockCipher(
263: new GOST28147Engine(), 64));
264:
265: cipher.init(true, param);
266: len1 = cipher.processBytes(in, 0, in.length, out, 0);
267: try {
268: cipher.doFinal(out, len1);
269: } catch (CryptoException e) {
270: fail("failed - exception " + e.toString(), e);
271: }
272: if (out.length != output.length) {
273: fail("failed - " + "expected "
274: + new String(Hex.encode(output)) + " got "
275: + new String(Hex.encode(out)));
276: }
277: for (int i = 0; i != out.length; i++) {
278: if (out[i] != output[i]) {
279: fail("failed - " + "expected "
280: + new String(Hex.encode(output)) + " got "
281: + new String(Hex.encode(out)));
282: }
283: }
284:
285: //encrypt on hesh message; CFB mode:
286: in = Hex
287: .decode("000102030405060708090a0b0c0d0e0fff0102030405060708090a0b0c0d0e0f");
288: output = Hex
289: .decode("64988982819f0a1655e226e19ecad79d10cc73bac95c5d7da034786c12294225");
290: out = new byte[in.length];
291:
292: key = generateKey(Hex
293: .decode("aafd12f659cae63489b479e5076ddec2f06cb58faafd12f659cae63489b479e5")); //!!! heshing start_key - get 256 bits !!!
294: param = new ParametersWithIV(new ParametersWithSBox(
295: new KeyParameter(key), //key
296: GOST28147Engine.getSBox("E-A")), //type S-box
297: Hex.decode("aafd12f659cae634")); //IV
298:
299: cipher = new BufferedBlockCipher(new CFBBlockCipher(
300: new GOST28147Engine(), 64));
301:
302: cipher.init(true, param);
303: len1 = cipher.processBytes(in, 0, in.length, out, 0);
304:
305: cipher.doFinal(out, len1);
306:
307: if (out.length != output.length) {
308: fail("failed - " + "expected "
309: + new String(Hex.encode(output)) + " got "
310: + new String(Hex.encode(out)));
311: }
312:
313: for (int i = 0; i != out.length; i++) {
314: if (out[i] != output[i]) {
315: fail("failed - " + "expected "
316: + new String(Hex.encode(output)) + " got "
317: + new String(Hex.encode(out)));
318: }
319: }
320:
321: //encrypt on hesh message; OFB mode:
322: in = Hex.decode("bc350e71aa11345709acde");
323: output = Hex.decode("1bcc2282707c676fb656dc");
324: out = new byte[in.length];
325:
326: key = generateKey(Hex.decode("0123456789abcdef")); //!!! heshing start_key - get 256 bits !!!
327: param = new ParametersWithIV(new ParametersWithSBox(
328: new KeyParameter(key), //key
329: GOST28147Engine.getSBox("E-A")), //type S-box
330: Hex.decode("1234567890abcdef")); //IV
331:
332: cipher = new BufferedBlockCipher(new GOFBBlockCipher(
333: new GOST28147Engine()));
334:
335: cipher.init(true, param);
336: len1 = cipher.processBytes(in, 0, in.length, out, 0);
337:
338: cipher.doFinal(out, len1);
339:
340: if (out.length != output.length) {
341: fail("failed - " + "expected "
342: + new String(Hex.encode(output)) + " got "
343: + new String(Hex.encode(out)));
344: }
345: for (int i = 0; i != out.length; i++) {
346: if (out[i] != output[i]) {
347: fail("failed - " + "expected "
348: + new String(Hex.encode(output)) + " got "
349: + new String(Hex.encode(out)));
350: }
351: }
352: }
353:
354: public String getName() {
355: return "GOST28147";
356: }
357:
358: public static void main(String[] args) {
359: runTest(new GOST28147Test());
360: }
361: }
|