001: package org.bouncycastle.crypto.test;
002:
003: import org.bouncycastle.crypto.StreamCipher;
004: import org.bouncycastle.crypto.engines.HC128Engine;
005: import org.bouncycastle.crypto.engines.HC256Engine;
006: import org.bouncycastle.crypto.params.KeyParameter;
007: import org.bouncycastle.crypto.params.ParametersWithIV;
008: import org.bouncycastle.util.encoders.Hex;
009: import org.bouncycastle.util.test.SimpleTest;
010:
011: /**
012: * HC-128 and HC-256 Tests. Based on the test vectors in the official reference
013: * papers, respectively:
014: *
015: * http://www.ecrypt.eu.org/stream/p3ciphers/hc/hc128_p3.pdf
016: * http://www.ecrypt.eu.org/stream/p3ciphers/hc/hc256_p3.pdf
017: */
018: public class HCFamilyTest extends SimpleTest {
019: private static final byte[] MSG = new byte[64];
020:
021: private static final byte[] K256A = new byte[32];
022: private static final byte[] K256B = Hex
023: .decode("55000000000000000000000000000000"
024: + "00000000000000000000000000000000");
025:
026: private static final byte[] IVA = new byte[32];
027: private static final byte[] IVB = new byte[] { 0x1 };
028:
029: private static final byte[] HC256A = Hex
030: .decode("8589075b0df3f6d82fc0c5425179b6a6"
031: + "3465f053f2891f808b24744e18480b72"
032: + "ec2792cdbf4dcfeb7769bf8dfa14aee4"
033: + "7b4c50e8eaf3a9c8f506016c81697e32");
034: private static final byte[] HC256B = Hex
035: .decode("bfa2e2afe9ce174f8b05c2feb18bb1d1"
036: + "ee42c05f01312b71c61f50dd502a080b"
037: + "edfec706633d9241a6dac448af8561ff"
038: + "5e04135a9448c4342de7e9f337520bdf");
039: private static final byte[] HC256C = Hex
040: .decode("fe4a401ced5fe24fd19a8f956fc036ae"
041: + "3c5aa68823e2abc02f90b3aea8d30e42"
042: + "59f03a6c6e39eb448f7579fb70137a5e"
043: + "6d10b7d8add0f7cd723423daf575dde6");
044: private static final byte[] HC256D = Hex
045: .decode("c6b6fb99f2ae1440a7d4ca342011694e"
046: + "6f36b4be420db05d4745fd907c630695"
047: + "5f1d7bda13ae7e36aebc5399733b7f37"
048: + "95f34066b601d21f2d8cf830a9c08937");
049:
050: private static final byte[] K128A = new byte[16];
051: private static final byte[] K128B = Hex
052: .decode("55000000000000000000000000000000");
053:
054: private static final byte[] HC128A = Hex
055: .decode("731500823bfd03a0fb2fd77faa63af0e"
056: + "de122fc6a7dc29b662a685278b75ec68"
057: + "9036db1e8189600500ade078491fbf9a"
058: + "1cdc30136c3d6e2490f664b29cd57102");
059: private static final byte[] HC128B = Hex
060: .decode("c01893d5b7dbe9588f65ec9864176604"
061: + "36fc6724c82c6eec1b1c38a7c9b42a95"
062: + "323ef1230a6a908bce757b689f14f7bb"
063: + "e4cde011aeb5173f89608c94b5cf46ca");
064: private static final byte[] HC128C = Hex
065: .decode("518251a404b4930ab02af9310639f032"
066: + "bcb4a47a5722480b2bf99f72cdc0e566"
067: + "310f0c56d3cc83e8663db8ef62dfe07f"
068: + "593e1790c5ceaa9cab03806fc9a6e5a0");
069: private static final byte[] HC128D = Hex
070: .decode("a4eac0267e4911266a2a384f5c4e1329"
071: + "da407fa155e6b1ae05c6fdf3bbdc8a86"
072: + "7a699aa01a4dc11763658cccd3e62474"
073: + "9cf8236f0131be21c3a51de9d12290de");
074:
075: public String getName() {
076: return "HC-128 and HC-256";
077: }
078:
079: public void performTest() {
080: StreamCipher hc = new HC256Engine();
081: HCTest(hc, "HC-256 - A", K256A, IVA, HC256A);
082: HCTest(hc, "HC-256 - B", K256A, IVB, HC256B);
083: HCTest(hc, "HC-256 - C", K256B, IVA, HC256C);
084: HCTest2(hc, "HC-256 - D", K256A, IVA, HC256D, 0x10000);
085:
086: hc = new HC128Engine();
087: HCTest(hc, "HC-128 - A", K128A, IVA, HC128A);
088: HCTest(hc, "HC-128 - B", K128A, IVB, HC128B);
089: HCTest(hc, "HC-128 - C", K128B, IVA, HC128C);
090: HCTest2(hc, "HC-128 - D", K128A, IVA, HC128D, 0x100000);
091: }
092:
093: private void HCTest(StreamCipher hc, String test, byte[] key,
094: byte[] IV, byte[] expected) {
095: KeyParameter kp = new KeyParameter(key);
096: ParametersWithIV ivp = new ParametersWithIV(kp, IV);
097: hc.init(true, ivp);
098: for (int i = 0; i < 64; i++) {
099: if (hc.returnByte(MSG[i]) != expected[i]) {
100: fail(test + " failure");
101: }
102: }
103: }
104:
105: private void HCTest2(StreamCipher hc, String test, byte[] key,
106: byte[] IV, byte[] expected, int times) {
107: KeyParameter kp = new KeyParameter(key);
108: ParametersWithIV ivp = new ParametersWithIV(kp, IV);
109: hc.init(true, ivp);
110: byte[] result = new byte[64];
111: for (int j = 0; j < times; j++) {
112: for (int i = 0; i < 64; i++) {
113: result[i] = hc.returnByte(result[i]);
114: }
115: }
116:
117: for (int i = 0; i < 64; i++) {
118: if (result[i] != expected[i]) {
119: fail(test + " failure at byte " + i);
120: }
121: }
122: }
123:
124: public static void main(String[] args) {
125: runTest(new HCFamilyTest());
126: }
127: }
|