Added unittests for chacha20

This commit is contained in:
Johannes Loher 2016-02-28 13:38:27 +01:00
parent 7acebd713e
commit 753a0cd366

View file

@ -9,7 +9,7 @@ private import bitmanip;
public:
// TODO: Create unittests!!!!
// TODO: Check unittests (Use reliable software to check if the results are correct)
auto chacha20Cipher(R)(R range, ubyte[32] key, ubyte[8] nonce)
if(isInputRange!R && is(ElementType!R : ubyte))
@ -81,7 +81,7 @@ enum string quarterRound(alias _x0, alias _x1, alias _x2, alias _x3) = q{
}.format(__traits(identifier, _x0), __traits(identifier, _x1),
__traits(identifier, _x2), __traits(identifier, _x3));
/*unittest
unittest
{
uint a1 = 0x00000000, a2 = 0x00000000, a3 = 0x00000000, a4 = 0x00000000,
b1 = 0x00000001, b2 = 0x00000000, b3 = 0x00000000, b4 = 0x00000000,
@ -100,13 +100,13 @@ enum string quarterRound(alias _x0, alias _x1, alias _x2, alias _x3) = q{
mixin(quarterRound!(g1, g2, g3, g4));
assert([a1, a2, a3, a4] == [0x00000000, 0x00000000, 0x00000000, 0x00000000]);
assert([b1, b2, b3, b4] == [0x08008145, 0x00000080, 0x00010200, 0x20500000]);
assert([c1, c2, c3, c4] == [0x88000100, 0x00000001, 0x00000200, 0x00402000]);
assert([d1, d2, d3, d4] == [0x80040000, 0x00000000, 0x00000001, 0x00002000]);
assert([e1, e2, e3, e4] == [0x00048044, 0x00000080, 0x00010000, 0x20100001]);
assert([f1, f2, f3, f4] == [0xe876d72b, 0x9361dfd5, 0xf1460244, 0x948541a3]);
assert([g1, g2, g3, g4] == [0x3e2f308c, 0xd90a8f36, 0x6ab2a923, 0x2883524c]);
}*/
assert([b1, b2, b3, b4] == [0x10000001, 0x80808808, 0x01010110, 0x01000110]);
assert([c1, c2, c3, c4] == [0x10001001, 0x88888808, 0x01110110, 0x01100110]);
assert([d1, d2, d3, d4] == [0x00001000, 0x08080080, 0x00100001, 0x00100000]);
assert([e1, e2, e3, e4] == [0x10000000, 0x80800808, 0x01010010, 0x01000010]);
assert([f1, f2, f3, f4] == [0x207cb2a0, 0x1f261df7, 0x9da4fd26, 0xc8768450]);
assert([g1, g2, g3, g4] == [0x18cb6df2, 0x41821bf8, 0x1fcb29a7, 0x92cbf922]);
}
enum string rowRound(alias _x00, alias _x01, alias _x02, alias _x03,
alias _x04, alias _x05, alias _x06, alias _x07,
@ -125,7 +125,7 @@ enum string rowRound(alias _x00, alias _x01, alias _x02, alias _x03,
__traits(identifier, _x12), __traits(identifier, _x13),
__traits(identifier, _x14), __traits(identifier, _x15));
/*unittest
unittest
{
uint y00 = 0x00000001, y01 = 0x00000000, y02 = 0x00000000, y03 = 0x00000000,
y04 = 0x00000001, y05 = 0x00000000, y06 = 0x00000000, y07 = 0x00000000,
@ -149,16 +149,16 @@ enum string rowRound(alias _x00, alias _x01, alias _x02, alias _x03,
uint[] test1 = [x00, x01, x02, x03, x04, x05, x06, x07,
x08, x09, x10, x11, x12, x13, x14, x15];
assert(test0 == [0x08008145, 0x00000080, 0x00010200, 0x20500000,
0x20100001, 0x00048044, 0x00000080, 0x00010000,
0x00000001, 0x00002000, 0x80040000, 0x00000000,
0x00000001, 0x00000200, 0x00402000, 0x88000100]);
assert(test0 == [0x10000001, 0x10000000, 0x00001000, 0x10001001,
0x88888808, 0x80808808, 0x80800808, 0x08080080,
0x00100001, 0x01110110, 0x01010110, 0x01010010,
0x01000010, 0x00100000, 0x01100110, 0x01000110]);
assert(test1 == [0xa890d39d, 0x65d71596, 0xe9487daa, 0xc8ca6a86,
0x949d2192, 0x764b7754, 0xe408d9b9, 0x7a41b4d1,
0x3402e183, 0x3c3af432, 0x50669f96, 0xd89ef0a8,
0x0040ede5, 0xb545fbce, 0xd257ed4f, 0x1818882d]);
}*/
assert(test1 == [0x9318dab2, 0x6219299a, 0xd68946c3, 0x3eb5b75f,
0xe0df816c, 0x476df40b, 0xbb2e1d6f, 0x7608ad18,
0x71b82d0d, 0xe707569d, 0x4c71a945, 0x0b2b589a,
0xe34c7f87, 0x5239bb58, 0x8a47d068, 0x475ec56d]);
}
enum string colRound(alias _x00, alias _x01, alias _x02, alias _x03,
alias _x04, alias _x05, alias _x06, alias _x07,
@ -177,7 +177,7 @@ enum string colRound(alias _x00, alias _x01, alias _x02, alias _x03,
__traits(identifier, _x12), __traits(identifier, _x13),
__traits(identifier, _x14), __traits(identifier, _x15));
/*unittest{
unittest{
uint y00 = 0x00000001, y01 = 0x00000000, y02 = 0x00000000, y03 = 0x00000000,
y04 = 0x00000001, y05 = 0x00000000, y06 = 0x00000000, y07 = 0x00000000,
y08 = 0x00000001, y09 = 0x00000000, y10 = 0x00000000, y11 = 0x00000000,
@ -201,16 +201,16 @@ enum string colRound(alias _x00, alias _x01, alias _x02, alias _x03,
uint[] test1 = [x00, x01, x02, x03, x04, x05, x06, x07,
x08, x09, x10, x11, x12, x13, x14, x15];
assert(test0 == [0x10090288, 0x00000000, 0x00000000, 0x00000000,
0x00000101, 0x00000000, 0x00000000, 0x00000000,
0x00020401, 0x00000000, 0x00000000, 0x00000000,
0x40a04001, 0x00000000, 0x00000000, 0x00000000]);
assert(test0 == [0x30000002, 0x00000000, 0x00000000, 0x00000000,
0x81811899, 0x00000000, 0x00000000, 0x00000000,
0x03030231, 0x00000000, 0x00000000, 0x00000000,
0x03000230, 0x00000000, 0x00000000, 0x00000000]);
assert(test1 == [0x8c9d190a, 0xce8e4c90, 0x1ef8e9d3, 0x1326a71a,
0x90a20123, 0xead3c4f3, 0x63a091a0, 0xf0708d69,
0x789b010c, 0xd195a681, 0xeb7d5504, 0xa774135c,
0x481c2027, 0x53a8e4b5, 0x4c1f89c5, 0x3f78c9c8]);
}*/
assert(test1 == [0x54c4775a, 0xb72a4bef, 0xcaba7b13, 0xdc9d2cb9,
0x01a21f25, 0x229f731a, 0xb515e4fe, 0xcd79967d,
0xcd26b517, 0x533f496f, 0x5cd61313, 0x3c6a9772,
0xf5529d13, 0xfbee0de7, 0x7c5c8c13, 0x9c847b82]);
}
ubyte[] chacha20(in ubyte[] input) @safe nothrow pure
in
@ -256,7 +256,7 @@ body
littleEndianInv(x14 + y14) ~ littleEndianInv(x15 + y15);
}
/*unittest
unittest
{
ubyte[] test0 = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
@ -288,21 +288,21 @@ body
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
assert(test1 == [109, 42,178,168,156,240,248,238,168,196,190,203, 26,110,170,154,
29, 29,150, 26,150, 30,235,249,190,163,251, 48, 69,144, 51, 57,
118, 40,152,157,180, 57, 27, 94,107, 42,236, 35, 27,111,114,114,
219,236,232,135,111,155,110, 18, 24,232, 95,158,179, 19, 48,202]);
assert(test1 == [ 19,135,149, 14, 40,180,217,109,187,122,124,114, 23, 14,107,241,
231, 18, 85,210, 56,159, 57,224, 96, 71, 85,167,202,180, 98, 96,
185, 76, 56,161,168,230,132,189, 84, 59, 55,254, 39, 21, 41,153,
144,151, 5,208,189, 9,140, 64,226,151,219, 26, 80,221,237,194]);
assert(test2 == [179, 19, 48,202,219,236,232,135,111,155,110, 18, 24,232, 95,158,
26,110,170,154,109, 42,178,168,156,240,248,238,168,196,190,203,
69,144, 51, 57, 29, 29,150, 26,150, 30,235,249,190,163,251, 48,
27,111,114,114,118, 40,152,157,180, 57, 27, 94,107, 42,236, 35]);
assert(test2 == [ 76, 1, 30, 79,130,234,169,252,194, 25,110,243,141, 64, 68,140,
106, 32,248,152,219, 73,115,133, 10,143, 99,162, 72,155,138, 55,
58,214, 79, 49, 97, 63,201,155,178,203, 15, 94, 66,224,216,184,
64, 85,186,170, 65, 79, 11, 91, 22,247,191, 72, 68,250,229,108]);
assert(test3 == [ 8, 18, 38,199,119, 76,215, 67,173,127,144,162,103,212,176,217,
192, 19,233, 33,159,197,154,160,128,243,219, 65,171,136,135,225,
123, 11, 68, 86,237, 82, 20,155,133,189, 9, 83,167,116,194, 78,
122,127,195,185,185,204,188, 90,245, 9,183,248,226, 85,245,104]);
}*/
assert(test3 == [ 88, 97,168, 54,215,192,249,115,185,160,231, 86,203,147, 27, 34,
237,252,228, 40, 1,135,107, 88,160,239, 5,182,250, 16,217,224,
101, 27,113, 5, 50,192, 52, 70, 7,110,217, 10, 3,226, 85,129,
29,214,235,224, 96, 42, 55, 53,188, 35, 81,171, 81,218,221, 44]);
}
enum ubyte[4] σ0 = [101, 120, 112, 97];
enum ubyte[4] σ1 = [110, 100, 32, 51];
@ -324,7 +324,7 @@ body
return chacha20(σ0 ~ key[0..16] ~ σ1 ~ n ~ σ2 ~ key[16..$] ~ σ3);
}
/*unittest
unittest
{
ubyte[] key;
ubyte[] n;
@ -338,8 +338,8 @@ body
foreach(i; 0..16)
n[i] = cast(ubyte)(i + 1+ 100);
assert(chacha20Exp(key, n) == [ 69, 37, 68, 39, 41, 15,107,193,255,139,122, 6,170,233,217, 98,
89,144,182,106, 21, 51,200, 65,239, 49,222, 34,215,114, 40,126,
104,197, 7,225,197,153, 31, 2,102, 78, 76,176, 84,245,246,184,
177,160,133,130, 6, 72,149,119,192,195,132,236,234,103,246, 74]);
}*/
assert(chacha20Exp(key, n) == [ 2, 7, 55,183,240,232, 0,145,207,208,120,131,146, 9,130, 31,
99,154, 60, 98,194,161,191, 80,167, 61,100,101,173,193, 48,203,
248, 45, 55, 12, 69, 21,147,216,142,141,137,131, 14, 7,181, 1,
63,126,214,246, 74,167, 55,124,119,140,129,165,170,250,173, 94]);
}