001: package org.bouncycastle.jce.provider.test;
002:
003: import org.bouncycastle.util.encoders.Hex;
004: import org.bouncycastle.util.test.SimpleTest;
005: import org.bouncycastle.util.test.TestFailedException;
006:
007: import javax.crypto.Cipher;
008: import javax.crypto.KeyGenerator;
009: import javax.crypto.SecretKey;
010: import javax.crypto.spec.IvParameterSpec;
011: import javax.crypto.spec.SecretKeySpec;
012: import java.security.Key;
013:
014: public abstract class BaseBlockCipherTest extends SimpleTest {
015: String algorithm;
016:
017: BaseBlockCipherTest(String algorithm) {
018: this .algorithm = algorithm;
019: }
020:
021: public String getName() {
022: return algorithm;
023: }
024:
025: protected void oidTest(String[] oids, String[] names, int groupSize)
026: throws Exception {
027: byte[] data = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
028: 15, 16 };
029: IvParameterSpec ivSpec = new IvParameterSpec(new byte[16]);
030:
031: for (int i = 0; i != oids.length; i++) {
032: Cipher c1 = Cipher.getInstance(oids[i], "BC");
033: Cipher c2 = Cipher.getInstance(names[i], "BC");
034: KeyGenerator kg = KeyGenerator.getInstance(oids[i], "BC");
035:
036: SecretKey k = kg.generateKey();
037:
038: if (names[i].indexOf("/ECB/") > 0) {
039: c1.init(Cipher.ENCRYPT_MODE, k);
040: c2.init(Cipher.DECRYPT_MODE, k);
041: } else {
042: c1.init(Cipher.ENCRYPT_MODE, k, ivSpec);
043: c2.init(Cipher.DECRYPT_MODE, k, ivSpec);
044: }
045:
046: byte[] result = c2.doFinal(c1.doFinal(data));
047:
048: if (!areEqual(data, result)) {
049: fail("failed OID test");
050: }
051:
052: if (k.getEncoded().length != (16 + ((i / groupSize) * 8))) {
053: fail("failed key length test");
054: }
055: }
056: }
057:
058: protected void wrapOidTest(String[] oids, String name)
059: throws Exception {
060: byte[] data = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
061: 15, 16 };
062:
063: for (int i = 0; i != oids.length; i++) {
064: Cipher c1 = Cipher.getInstance(oids[i], "BC");
065: Cipher c2 = Cipher.getInstance(name, "BC");
066: KeyGenerator kg = KeyGenerator.getInstance(oids[i], "BC");
067:
068: SecretKey k = kg.generateKey();
069:
070: c1.init(Cipher.WRAP_MODE, k);
071: c2.init(Cipher.UNWRAP_MODE, k);
072:
073: Key wKey = c2.unwrap(c1.wrap(new SecretKeySpec(data,
074: algorithm)), algorithm, Cipher.SECRET_KEY);
075:
076: if (!areEqual(data, wKey.getEncoded())) {
077: fail("failed wrap OID test");
078: }
079:
080: if (k.getEncoded().length != (16 + (i * 8))) {
081: fail("failed key length test");
082: }
083: }
084: }
085:
086: protected void wrapTest(int id, String wrappingAlgorithm,
087: byte[] kek, byte[] in, byte[] out) throws Exception {
088: Cipher wrapper = Cipher.getInstance(wrappingAlgorithm, "BC");
089:
090: wrapper.init(Cipher.WRAP_MODE,
091: new SecretKeySpec(kek, algorithm));
092:
093: try {
094: byte[] cText = wrapper
095: .wrap(new SecretKeySpec(in, algorithm));
096: if (!areEqual(cText, out)) {
097: fail("failed wrap test " + id + " expected "
098: + new String(Hex.encode(out)) + " got "
099: + new String(Hex.encode(cText)));
100: }
101: } catch (TestFailedException e) {
102: throw e;
103: } catch (Exception e) {
104: fail("failed wrap test exception " + e.toString(), e);
105: }
106:
107: wrapper.init(Cipher.UNWRAP_MODE, new SecretKeySpec(kek,
108: algorithm));
109:
110: try {
111: Key pText = wrapper.unwrap(out, algorithm,
112: Cipher.SECRET_KEY);
113: if (!areEqual(pText.getEncoded(), in)) {
114: fail("failed unwrap test " + id + " expected "
115: + new String(Hex.encode(in)) + " got "
116: + new String(Hex.encode(pText.getEncoded())));
117: }
118: } catch (Exception e) {
119: fail("failed unwrap test exception " + e.toString(), e);
120: }
121: }
122: }
|