Fixed chacha20, verfied unittests for chacha20 and added some more tests for chacha20.

This commit is contained in:
Johannes Loher 2016-07-31 17:49:42 +02:00
parent 755939afd9
commit 4e60a77a29

View file

@ -12,8 +12,6 @@ import utility;
public:
// TODO: Check unittests (Use reliable software to check if the results are correct)
// TODO: Implement random access
auto chacha20Cipher(R)(R range, ubyte[32] key, ubyte[8] nonce)
@ -44,6 +42,7 @@ auto chacha20Cipher(R)(R range, ubyte[32] key, ubyte[8] nonce)
if (++chachaCounter == 64)
{
chachaSection = chacha20Exp(key, concat!(ubyte[16])(nonce, littleEndianInv(++count)));
chachaCounter = 0;
}
range.popFront();
}
@ -62,14 +61,14 @@ auto chacha20Cipher(R)(R range, ubyte[32] key, ubyte[8] nonce)
@safe unittest
{
ubyte[] test = new ubyte[64];
ubyte[32] key;
ubyte[8] nonce;
test = test.chacha20Cipher(key, nonce).array;
assert(test == [140,111, 5,175,106, 91,125,127, 60,125, 18, 49, 73, 47,186, 94,
234,193,178,253,211,130, 18, 77,243,176, 91,203,126, 83, 82,194,
77,251,230, 8,208,202, 74,201,254, 13,148,163, 20, 1,151,129,
168,147,213,247, 92, 23,242, 73,135, 68,217,123, 87,123,234,199]);
ubyte[] test1 = new ubyte[64];
ubyte[32] key1;
ubyte[8] nonce1;
test1 = test1.chacha20Cipher(key1, nonce1).array;
assert(test1 == [118,184,224,173,160,241, 61,144, 64, 93,106,229, 83,134,189, 40,
189,210, 25,184,160,141,237, 26,168, 54,239,204,139,119, 13,199,
218, 65, 89,124, 81, 87, 72,141,119, 36,224, 63,184,216, 74, 55,
106, 67,184,244, 21, 24,161, 28,195,135,182,105,178,238,101,134]);
}
private:
@ -313,12 +312,11 @@ ubyte[64] chacha20Exp(in ubyte[32] key, in ubyte[16] n) @safe pure nothrow @nogc
{
return chacha20(concat!(ubyte[64])(sigma0,
key[0..16],
sigma1,
n,
sigma2,
key[16..$],
sigma3));
sigma1,
sigma2,
sigma3,
key,
n));
}
@safe unittest
@ -331,8 +329,30 @@ ubyte[64] chacha20Exp(in ubyte[32] key, in ubyte[16] n) @safe pure nothrow @nogc
immutable ubyte[16] n = [101,102,103,104,105,106,107,108,
109,110,111,112,113,114,115,116];
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]);
}
assert(chacha20Exp(key, n) == [ 91, 78,219,119,210,190, 8, 62, 37,143, 86,137,217,173,192, 90,
200, 33, 74, 29,135,175, 94, 6, 23,221,182, 9, 17,163, 46,191,
222,214,172,130,247,120,162, 47,190,213, 83, 21,242,210, 49,174,
198, 64, 8, 65, 98, 73, 10,144, 56, 48,201, 68, 1,174,183,172]);
// Test vectors consistent with http://tools.ietf.org/html/draft-agl-tls-chacha20poly1305-04#section-7
immutable ubyte[32] key1 = [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];
immutable ubyte[16] n1 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
immutable ubyte[32] key2 = [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, 1];
immutable ubyte[16] n2 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
immutable ubyte[32] key3 = [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];
immutable ubyte[16] n3 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1];
immutable ubyte[32] key4 = [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];
immutable ubyte[16] n4 = [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0];
assert(chacha20Exp(key1, n1) == [118, 184, 224, 173, 160, 241, 61, 144, 64, 93, 106, 229, 83, 134, 189, 40, 189, 210, 25, 184, 160, 141, 237, 26, 168, 54, 239, 204, 139, 119, 13, 199, 218, 65, 89, 124, 81, 87, 72, 141, 119, 36, 224, 63, 184, 216, 74, 55, 106, 67, 184, 244, 21, 24, 161, 28, 195, 135, 182, 105, 178, 238, 101, 134]);
assert(chacha20Exp(key2, n2) == [69, 64, 240, 90, 159, 31, 178, 150, 215, 115, 110, 123, 32, 142, 60, 150, 235, 79, 225, 131, 70, 136, 210, 96, 79, 69, 9, 82, 237, 67, 45, 65, 187, 226, 160, 182, 234, 117, 102, 210, 165, 209, 231, 226, 13, 66, 175, 44, 83, 215, 146, 177, 196, 63, 234, 129, 126, 154, 210, 117, 174, 84, 105, 99]);
assert(chacha20Exp(key3, n3) == [222, 156, 186, 123, 243, 214, 158, 245, 231, 134, 220, 99, 151, 63, 101, 58, 11, 73, 224, 21, 173, 191, 247, 19, 79, 203, 125, 241, 55, 130, 16, 49, 232, 90, 5, 2, 120, 167, 8, 69, 39, 33, 79, 115, 239, 199, 250, 91, 82, 119, 6, 46, 183, 160, 67, 62, 68, 95, 65, 227, 26, 250, 183, 87]);
assert(chacha20Exp(key4, n4) == [239, 63, 223, 214, 198, 21, 120, 251, 245, 207, 53, 189, 61, 211, 59, 128, 9, 99, 22, 52, 210, 30, 66, 172, 51, 150, 11, 209, 56, 229, 13, 50, 17, 30, 76, 175, 35, 126, 229, 60, 168, 173, 100, 38, 25, 74, 136, 84, 93, 220, 73, 122, 11, 70, 110, 125, 107, 189, 176, 4, 27, 47, 88, 107]);
}