001: package org.bouncycastle.crypto.test;
002:
003: import java.security.SecureRandom;
004:
005: import org.bouncycastle.crypto.CipherParameters;
006: import org.bouncycastle.crypto.Wrapper;
007: import org.bouncycastle.crypto.engines.RC2WrapEngine;
008: import org.bouncycastle.crypto.params.ParametersWithIV;
009: import org.bouncycastle.crypto.params.ParametersWithRandom;
010: import org.bouncycastle.crypto.params.RC2Parameters;
011: import org.bouncycastle.util.Arrays;
012: import org.bouncycastle.util.encoders.Hex;
013: import org.bouncycastle.util.test.SimpleTestResult;
014: import org.bouncycastle.util.test.Test;
015: import org.bouncycastle.util.test.TestResult;
016:
017: /**
018: * RC2 wrap tester
019: */
020: public class RC2WrapTest implements Test {
021: private class RFCRandom extends SecureRandom {
022: public void nextBytes(byte[] nextBytes) {
023: System.arraycopy(Hex.decode("4845cce7fd1250"), 0,
024: nextBytes, 0, nextBytes.length);
025: }
026: }
027:
028: private TestResult wrapTest(int id, CipherParameters paramsWrap,
029: CipherParameters paramsUnwrap, byte[] in, byte[] out) {
030: Wrapper wrapper = new RC2WrapEngine();
031:
032: wrapper.init(true, paramsWrap);
033:
034: try {
035: byte[] cText = wrapper.wrap(in, 0, in.length);
036: if (!Arrays.areEqual(cText, out)) {
037: return new SimpleTestResult(false, getName()
038: + ": failed wrap test " + id + " expected "
039: + new String(Hex.encode(out)) + " got "
040: + new String(Hex.encode(cText)));
041: }
042: } catch (Exception e) {
043: return new SimpleTestResult(false, getName()
044: + ": failed wrap test exception " + e.toString(), e);
045: }
046:
047: wrapper.init(false, paramsUnwrap);
048:
049: try {
050: byte[] pText = wrapper.unwrap(out, 0, out.length);
051: if (!Arrays.areEqual(pText, in)) {
052: return new SimpleTestResult(false, getName()
053: + ": failed unwrap test " + id + " expected "
054: + new String(Hex.encode(in)) + " got "
055: + new String(Hex.encode(pText)));
056: }
057: } catch (Exception e) {
058: return new SimpleTestResult(false, getName()
059: + ": failed unwrap test exception " + e.toString(),
060: e);
061: }
062:
063: return new SimpleTestResult(true, getName() + ": Okay");
064: }
065:
066: public TestResult perform() {
067: byte[] kek1 = Hex.decode("fd04fd08060707fb0003fefffd02fe05");
068: byte[] iv1 = Hex.decode("c7d90059b29e97f7");
069: byte[] in1 = Hex.decode("b70a25fbc9d86a86050ce0d711ead4d9");
070: byte[] out1 = Hex
071: .decode("70e699fb5701f7833330fb71e87c85a420bdc99af05d22af5a0e48d35f3138986cbaafb4b28d4f35");
072: //
073: // note the RFC 3217 test specifies a key to be used with an effective key size of
074: // 40 bits which is why it is done here - in practice nothing less than 128 bits should be used.
075: //
076: CipherParameters paramWrap = new ParametersWithRandom(
077: new ParametersWithIV(new RC2Parameters(kek1, 40), iv1),
078: new RFCRandom());
079: CipherParameters paramUnwrap = new RC2Parameters(kek1, 40);
080:
081: TestResult result = wrapTest(1, paramWrap, paramUnwrap, in1,
082: out1);
083:
084: if (!result.isSuccessful()) {
085: return result;
086: }
087:
088: return new SimpleTestResult(true, getName() + ": Okay");
089: }
090:
091: public String getName() {
092: return "RC2Wrap";
093: }
094:
095: public static void main(String[] args) {
096: RC2WrapTest test = new RC2WrapTest();
097: TestResult result = test.perform();
098:
099: System.out.println(result);
100: }
101: }
|