250 lines
10 KiB
C++
250 lines
10 KiB
C++
#include <cruft/crypto/stream/salsa.hpp>
|
|
|
|
#include <cruft/util/tap.hpp>
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
void
|
|
test_quarter (cruft::TAP::logger &tap)
|
|
{
|
|
static const struct {
|
|
std::array<uint32_t, 4> a, b;
|
|
} TESTS[] = {
|
|
{ { 0x00000000, 0x00000000, 0x00000000, 0x00000000 },
|
|
{ 0x00000000, 0x00000000, 0x00000000, 0x00000000 } },
|
|
{ { 0x00000001, 0x00000000, 0x00000000, 0x00000000 },
|
|
{ 0x08008145, 0x00000080, 0x00010200, 0x20500000 } },
|
|
{ { 0x00000000, 0x00000001, 0x00000000, 0x00000000 },
|
|
{ 0x88000100, 0x00000001, 0x00000200, 0x00402000 } },
|
|
{ { 0x00000000, 0x00000000, 0x00000001, 0x00000000 },
|
|
{ 0x80040000, 0x00000000, 0x00000001, 0x00002000 } },
|
|
{ { 0x00000000, 0x00000000, 0x00000000, 0x00000001 },
|
|
{ 0x00048044, 0x00000080, 0x00010000, 0x20100001 } },
|
|
{ { 0xe7e8c006, 0xc4f9417d, 0x6479b4b2, 0x68c67137 },
|
|
{ 0xe876d72b, 0x9361dfd5, 0xf1460244, 0x948541a3 } },
|
|
{ { 0xd3917c5b, 0x55f1c407, 0x52a58a7a, 0x8f887a3b },
|
|
{ 0x3e2f308c, 0xd90a8f36, 0x6ab2a923, 0x2883524c } },
|
|
};
|
|
|
|
for (size_t i = 0; i < std::size (TESTS); ++i)
|
|
tap.expect_eq (
|
|
cruft::crypto::stream::salsa::quarter (TESTS[i].a),
|
|
TESTS[i].b,
|
|
"quarter {}", i
|
|
);
|
|
}
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
void
|
|
test_row (cruft::TAP::logger &tap)
|
|
{
|
|
static const struct {
|
|
std::array<uint32_t, 16> a, b;
|
|
} TESTS[] = {
|
|
{ { 0x00000001, 0x00000000, 0x00000000, 0x00000000,
|
|
0x00000001, 0x00000000, 0x00000000, 0x00000000,
|
|
0x00000001, 0x00000000, 0x00000000, 0x00000000,
|
|
0x00000001, 0x00000000, 0x00000000, 0x00000000, },
|
|
{ 0x08008145, 0x00000080, 0x00010200, 0x20500000,
|
|
0x20100001, 0x00048044, 0x00000080, 0x00010000,
|
|
0x00000001, 0x00002000, 0x80040000, 0x00000000,
|
|
0x00000001, 0x00000200, 0x00402000, 0x88000100, }
|
|
},
|
|
{ { 0x08521bd6, 0x1fe88837, 0xbb2aa576, 0x3aa26365,
|
|
0xc54c6a5b, 0x2fc74c2f, 0x6dd39cc3, 0xda0a64f6,
|
|
0x90a2f23d, 0x067f95a6, 0x06b35f61, 0x41e4732e,
|
|
0xe859c100, 0xea4d84b7, 0x0f619bff, 0xbc6e965a, },
|
|
{ 0xa890d39d, 0x65d71596, 0xe9487daa, 0xc8ca6a86,
|
|
0x949d2192, 0x764b7754, 0xe408d9b9, 0x7a41b4d1,
|
|
0x3402e183, 0x3c3af432, 0x50669f96, 0xd89ef0a8,
|
|
0x0040ede5, 0xb545fbce, 0xd257ed4f, 0x1818882d, },
|
|
}
|
|
};
|
|
|
|
for (size_t i = 0; i < std::size (TESTS); ++i)
|
|
tap.expect_eq (
|
|
cruft::crypto::stream::salsa::row (TESTS[i].a),
|
|
TESTS[i].b,
|
|
"row {}",
|
|
i);
|
|
}
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
void
|
|
test_col (cruft::TAP::logger &tap)
|
|
{
|
|
static const struct {
|
|
std::array<uint32_t,16> a, b;
|
|
} TESTS[] = {
|
|
{ { 0x00000001, 0x00000000, 0x00000000, 0x00000000,
|
|
0x00000001, 0x00000000, 0x00000000, 0x00000000,
|
|
0x00000001, 0x00000000, 0x00000000, 0x00000000,
|
|
0x00000001, 0x00000000, 0x00000000, 0x00000000, },
|
|
{ 0x10090288, 0x00000000, 0x00000000, 0x00000000,
|
|
0x00000101, 0x00000000, 0x00000000, 0x00000000,
|
|
0x00020401, 0x00000000, 0x00000000, 0x00000000,
|
|
0x40a04001, 0x00000000, 0x00000000, 0x00000000, } },
|
|
{ { 0x08521bd6, 0x1fe88837, 0xbb2aa576, 0x3aa26365,
|
|
0xc54c6a5b, 0x2fc74c2f, 0x6dd39cc3, 0xda0a64f6,
|
|
0x90a2f23d, 0x067f95a6, 0x06b35f61, 0x41e4732e,
|
|
0xe859c100, 0xea4d84b7, 0x0f619bff, 0xbc6e965a, },
|
|
{ 0x8c9d190a, 0xce8e4c90, 0x1ef8e9d3, 0x1326a71a,
|
|
0x90a20123, 0xead3c4f3, 0x63a091a0, 0xf0708d69,
|
|
0x789b010c, 0xd195a681, 0xeb7d5504, 0xa774135c,
|
|
0x481c2027, 0x53a8e4b5, 0x4c1f89c5, 0x3f78c9c8, } },
|
|
};
|
|
|
|
for (size_t i = 0; i < std::size (TESTS); ++i)
|
|
tap.expect_eq (
|
|
cruft::crypto::stream::salsa::col (TESTS[i].a),
|
|
TESTS[i].b,
|
|
"col {}",
|
|
i);
|
|
}
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
void
|
|
test_doubleround (cruft::TAP::logger &tap)
|
|
{
|
|
static const struct {
|
|
std::array<uint32_t,16> a, b;
|
|
} TESTS[] = {
|
|
{ { 0x00000001, 0x00000000, 0x00000000, 0x00000000,
|
|
0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
|
0x00000000, 0x00000000, 0x00000000, 0x00000000,
|
|
0x00000000, 0x00000000, 0x00000000, 0x00000000, },
|
|
{ 0x8186a22d, 0x0040a284, 0x82479210, 0x06929051,
|
|
0x08000090, 0x02402200, 0x00004000, 0x00800000,
|
|
0x00010200, 0x20400000, 0x08008104, 0x00000000,
|
|
0x20500000, 0xa0000040, 0x0008180a, 0x612a8020, } },
|
|
{ { 0xde501066, 0x6f9eb8f7, 0xe4fbbd9b, 0x454e3f57,
|
|
0xb75540d3, 0x43e93a4c, 0x3a6f2aa0, 0x726d6b36,
|
|
0x9243f484, 0x9145d1e8, 0x4fa9d247, 0xdc8dee11,
|
|
0x054bf545, 0x254dd653, 0xd9421b6d, 0x67b276c1, },
|
|
{ 0xccaaf672, 0x23d960f7, 0x9153e63a, 0xcd9a60d0,
|
|
0x50440492, 0xf07cad19, 0xae344aa0, 0xdf4cfdfc,
|
|
0xca531c29, 0x8e7943db, 0xac1680cd, 0xd503ca00,
|
|
0xa74b2ad6, 0xbc331c5c, 0x1dda24c7, 0xee928277, } }
|
|
};
|
|
|
|
for (size_t i = 0; i < std::size (TESTS); ++i)
|
|
tap.expect_eq (
|
|
cruft::crypto::stream::salsa::doubleround (TESTS[i].a),
|
|
TESTS[i].b,
|
|
"doubleround {}",
|
|
i);
|
|
}
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
void
|
|
test_salsa20 (cruft::TAP::logger &tap)
|
|
{
|
|
static const struct {
|
|
std::array<uint8_t,64> a, b;
|
|
} TESTS[] = {
|
|
{ { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
|
|
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
0x00,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, 0x51, 0x9c, 0x2f, 0xcb, 0x1a, 0xf4, 0xf3,
|
|
0xbf, 0xbb, 0xea, 0x88, 0xd3, 0x9f, 0x0d, 0x73,
|
|
0x4c, 0x37, 0x52, 0xb7, 0x03, 0x75, 0xde, 0x25,
|
|
0x56, 0x10, 0xb3, 0xcf, 0x31, 0xed, 0xb3, 0x30,
|
|
0x01, 0x6a, 0xb2, 0xdb, 0xaf, 0xc7, 0xa6, 0x30,
|
|
0xee, 0x37, 0xcc, 0x24, 0x1f, 0xf0, 0x20, 0x3f,
|
|
0x0f, 0x53, 0x5d, 0xa1, 0x74, 0x93, 0x30, 0x71 },
|
|
{ 0xb3, 0x13, 0x30, 0xca, 0xdb, 0xec, 0xe8, 0x87,
|
|
0x6f, 0x9b, 0x6e, 0x12, 0x18, 0xe8, 0x5f, 0x9e,
|
|
0x1a, 0x6e, 0xaa, 0x9a, 0x6d, 0x2a, 0xb2, 0xa8,
|
|
0x9c, 0xf0, 0xf8, 0xee, 0xa8, 0xc4, 0xbe, 0xcb,
|
|
0x45, 0x90, 0x33, 0x39, 0x1d, 0x1d, 0x96, 0x1a,
|
|
0x96, 0x1e, 0xeb, 0xf9, 0xbe, 0xa3, 0xfb, 0x30,
|
|
0x1b, 0x6f, 0x72, 0x72, 0x76, 0x28, 0x98, 0x9d,
|
|
0xb4, 0x39, 0x1b, 0x5e, 0x6b, 0x2a, 0xec, 0x23 } }
|
|
};
|
|
|
|
for (size_t i = 0; i < std::size (TESTS); ++i)
|
|
tap.expect_eq (
|
|
cruft::crypto::stream::salsa20 (TESTS[i].a),
|
|
TESTS[i].b,
|
|
"salsa20 {}",
|
|
i);
|
|
|
|
struct {
|
|
std::array<uint8_t,64> a, b;
|
|
} million = {
|
|
{ 0x06, 0x7c, 0x53, 0x92, 0x26, 0xbf, 0x09, 0x32,
|
|
0x04, 0xa1, 0x2f, 0xde, 0x7a, 0xb6, 0xdf, 0xb9,
|
|
0x4b, 0x1b, 0x00, 0xd8, 0x10, 0x7a, 0x07, 0x59,
|
|
0xa2, 0x68, 0x65, 0x93, 0xd5, 0x15, 0x36, 0x5f,
|
|
0xe1, 0xfd, 0x8b, 0xb0, 0x69, 0x84, 0x17, 0x74,
|
|
0x4c, 0x29, 0xb0, 0xcf, 0xdd, 0x22, 0x9d, 0x6c,
|
|
0x5e, 0x5e, 0x63, 0x34, 0x5a, 0x75, 0x5b, 0xdc,
|
|
0x92, 0xbe, 0xef, 0x8f, 0xc4, 0xb0, 0x82, 0xba },
|
|
{ 0x08, 0x12, 0x26, 0xc7, 0x77, 0x4c, 0xd7, 0x43,
|
|
0xad, 0x7f, 0x90, 0xa2, 0x67, 0xd4, 0xb0, 0xd9,
|
|
0xc0, 0x13, 0xe9, 0x21, 0x9f, 0xc5, 0x9a, 0xa0,
|
|
0x80, 0xf3, 0xdb, 0x41, 0xab, 0x88, 0x87, 0xe1,
|
|
0x7b, 0x0b, 0x44, 0x56, 0xed, 0x52, 0x14, 0x9b,
|
|
0x85, 0xbd, 0x09, 0x53, 0xa7, 0x74, 0xc2, 0x4e,
|
|
0x7a, 0x7f, 0xc3, 0xb9, 0xb9, 0xcc, 0xbc, 0x5a,
|
|
0xf5, 0x09, 0xb7, 0xf8, 0xe2, 0x55, 0xf5, 0x68 }
|
|
};
|
|
|
|
for (int i = 0; i < 1'000'000; ++i)
|
|
million.a = cruft::crypto::stream::salsa20 (million.a);
|
|
|
|
tap.expect_eq (million.a, million.b, "salsa20 million");
|
|
}
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
int
|
|
main (void)
|
|
{
|
|
cruft::TAP::logger tap;
|
|
|
|
test_quarter (tap);
|
|
test_row (tap);
|
|
test_col (tap);
|
|
test_doubleround (tap);
|
|
test_salsa20 (tap);
|
|
|
|
return tap.status ();
|
|
}
|