001: package org.bouncycastle.jce.provider.test;
002:
003: import java.io.ByteArrayInputStream;
004: import java.io.ByteArrayOutputStream;
005: import java.io.DataInputStream;
006: import java.security.Key;
007: import java.security.Security;
008:
009: import javax.crypto.Cipher;
010: import javax.crypto.CipherInputStream;
011: import javax.crypto.CipherOutputStream;
012: import javax.crypto.KeyGenerator;
013: import javax.crypto.SecretKey;
014: import javax.crypto.spec.SecretKeySpec;
015: import javax.crypto.spec.IvParameterSpec;
016:
017: import org.bouncycastle.asn1.cryptopro.CryptoProObjectIdentifiers;
018: import org.bouncycastle.jce.provider.BouncyCastleProvider;
019: import org.bouncycastle.util.encoders.Hex;
020: import org.bouncycastle.util.test.SimpleTest;
021:
022: /**
023: * basic test class for the GOST28147 cipher
024: */
025: public class GOST28147Test extends SimpleTest {
026: static String[] cipherTests = {
027: "256",
028: "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
029: "4e6f77206973207468652074696d6520666f7220616c6c20",
030: "281630d0d5770030068c252d841e84149ccc1912052dbc02",
031:
032: "256",
033: "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
034: "4e6f77206973207468652074696d65208a920c6ed1a804f5",
035: "88e543dfc04dc4f764fa7b624741cec07de49b007bf36065" };
036:
037: public String getName() {
038: return "GOST28147";
039: }
040:
041: public void testECB(int strength, byte[] keyBytes, byte[] input,
042: byte[] output) throws Exception {
043: Key key;
044: Cipher in, out;
045: CipherInputStream cIn;
046: CipherOutputStream cOut;
047: ByteArrayInputStream bIn;
048: ByteArrayOutputStream bOut;
049:
050: key = new SecretKeySpec(keyBytes, "GOST28147");
051:
052: in = Cipher.getInstance("GOST28147/ECB/NoPadding", "BC");
053: out = Cipher.getInstance("GOST28147/ECB/NoPadding", "BC");
054: out.init(Cipher.ENCRYPT_MODE, key);
055: in.init(Cipher.DECRYPT_MODE, key);
056:
057: //
058: // encryption pass
059: //
060: bOut = new ByteArrayOutputStream();
061:
062: cOut = new CipherOutputStream(bOut, out);
063:
064: for (int i = 0; i != input.length / 2; i++) {
065: cOut.write(input[i]);
066: }
067: cOut.write(input, input.length / 2, input.length - input.length
068: / 2);
069: cOut.close();
070:
071: byte[] bytes;
072:
073: bytes = bOut.toByteArray();
074:
075: if (!areEqual(bytes, output)) {
076: fail("GOST28147 failed encryption - expected "
077: + new String(Hex.encode(output)) + " got "
078: + new String(Hex.encode(bytes)));
079: }
080:
081: //
082: // decryption pass
083: //
084: bIn = new ByteArrayInputStream(bytes);
085:
086: cIn = new CipherInputStream(bIn, in);
087:
088: DataInputStream dIn = new DataInputStream(cIn);
089:
090: bytes = new byte[input.length];
091:
092: for (int i = 0; i != input.length / 2; i++) {
093: bytes[i] = (byte) dIn.read();
094: }
095: dIn.readFully(bytes, input.length / 2, bytes.length
096: - input.length / 2);
097:
098: if (!areEqual(bytes, input)) {
099: fail("GOST28147 failed decryption - expected "
100: + new String(Hex.encode(input)) + " got "
101: + new String(Hex.encode(bytes)));
102: }
103: }
104:
105: public void testCFB(int strength, byte[] keyBytes, byte[] input,
106: byte[] output) throws Exception {
107: Key key;
108: Cipher in, out;
109: CipherInputStream cIn;
110: CipherOutputStream cOut;
111: ByteArrayInputStream bIn;
112: ByteArrayOutputStream bOut;
113:
114: key = new SecretKeySpec(keyBytes, "GOST28147");
115:
116: in = Cipher.getInstance("GOST28147/CFB8/NoPadding", "BC");
117: out = Cipher.getInstance("GOST28147/CFB8/NoPadding", "BC");
118: byte[] iv = { 1, 2, 3, 4, 5, 6, 7, 8 };
119:
120: out.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(iv));
121: in.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv));
122:
123: //
124: // encryption pass
125: //
126: bOut = new ByteArrayOutputStream();
127:
128: cOut = new CipherOutputStream(bOut, out);
129:
130: for (int i = 0; i != input.length / 2; i++) {
131: cOut.write(input[i]);
132: }
133: cOut.write(input, input.length / 2, input.length - input.length
134: / 2);
135: cOut.close();
136:
137: byte[] bytes;
138:
139: bytes = bOut.toByteArray();
140:
141: if (!areEqual(bytes, output)) {
142: fail("GOST28147 failed encryption - expected "
143: + new String(Hex.encode(output)) + " got "
144: + new String(Hex.encode(bytes)));
145: }
146:
147: //
148: // decryption pass
149: //
150: bIn = new ByteArrayInputStream(bytes);
151:
152: cIn = new CipherInputStream(bIn, in);
153:
154: DataInputStream dIn = new DataInputStream(cIn);
155:
156: bytes = new byte[input.length];
157:
158: for (int i = 0; i != input.length / 2; i++) {
159: bytes[i] = (byte) dIn.read();
160: }
161: dIn.readFully(bytes, input.length / 2, bytes.length
162: - input.length / 2);
163:
164: if (!areEqual(bytes, input)) {
165: fail("GOST28147 failed decryption - expected "
166: + new String(Hex.encode(input)) + " got "
167: + new String(Hex.encode(bytes)));
168: }
169: }
170:
171: private void oidTest() {
172: String[] oids = { CryptoProObjectIdentifiers.gostR28147_cbc
173: .getId(), };
174:
175: String[] names = { "GOST28147/CBC/PKCS7Padding" };
176:
177: try {
178:
179: byte[] data = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
180: 14, 15, 16 };
181: IvParameterSpec ivSpec = new IvParameterSpec(new byte[8]);
182:
183: for (int i = 0; i != oids.length; i++) {
184: Cipher c1 = Cipher.getInstance(oids[i], "BC");
185: Cipher c2 = Cipher.getInstance(names[i], "BC");
186: KeyGenerator kg = KeyGenerator.getInstance(oids[i],
187: "BC");
188:
189: SecretKey k = kg.generateKey();
190:
191: c1.init(Cipher.ENCRYPT_MODE, k, ivSpec);
192: c2.init(Cipher.DECRYPT_MODE, k, ivSpec);
193:
194: byte[] result = c2.doFinal(c1.doFinal(data));
195:
196: if (!areEqual(data, result)) {
197: fail("failed OID test");
198: }
199: }
200: } catch (Exception ex) {
201: fail("failed exception " + ex.toString(), ex);
202: }
203: }
204:
205: public void performTest() throws Exception {
206: for (int i = 0; i != cipherTests.length; i += 8) {
207: testECB(Integer.parseInt(cipherTests[i]), Hex
208: .decode(cipherTests[i + 1]), Hex
209: .decode(cipherTests[i + 2]), Hex
210: .decode(cipherTests[i + 3]));
211:
212: testCFB(Integer.parseInt(cipherTests[i + 4]), Hex
213: .decode(cipherTests[i + 4 + 1]), Hex
214: .decode(cipherTests[i + 4 + 2]), Hex
215: .decode(cipherTests[i + 4 + 3]));
216:
217: oidTest();
218: }
219: }
220:
221: public static void main(String[] args) {
222: Security.addProvider(new BouncyCastleProvider());
223:
224: runTest(new GOST28147Test());
225: }
226: }
|