diff --git a/CMakeLists.txt b/CMakeLists.txt index f143c93a..4bad0e2d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -199,6 +199,8 @@ list ( crypto/arc4.hpp crypto/ice.cpp crypto/ice.hpp + crypto/salsa.cpp + crypto/salsa.hpp crypto/tea.cpp crypto/tea.hpp crypto/xtea.cpp @@ -456,6 +458,7 @@ if (TESTS) coord crypto/arc4 crypto/ice + crypto/salsa crypto/tea crypto/xtea crypto/xxtea diff --git a/crypto/salsa.cpp b/crypto/salsa.cpp new file mode 100644 index 00000000..2cf5590b --- /dev/null +++ b/crypto/salsa.cpp @@ -0,0 +1,24 @@ +#include "./salsa.hpp" + + +/////////////////////////////////////////////////////////////////////////////// +std::array +util::crypto::salsa20 (const std::array bytes) noexcept +{ + auto x = *reinterpret_cast*> (&bytes); + auto y = x; + + for (auto &t: x) + t = util::ltoh (t); + + for (int i = 0; i < 10; ++i) + x = salsa::doubleround (x); + + for (size_t i = 0; i < std::size (y); ++i) + x[i] += y[i]; + + for (auto &t: x) + t = util::htol (t); + + return *reinterpret_cast*> (&x); +} diff --git a/crypto/salsa.hpp b/crypto/salsa.hpp new file mode 100644 index 00000000..c8d81c64 --- /dev/null +++ b/crypto/salsa.hpp @@ -0,0 +1,86 @@ +#include +#include + +#include "../bitwise.hpp" +#include "../endian.hpp" + +namespace util::crypto::salsa { + + /////////////////////////////////////////////////////////////////////////// + constexpr + uint32_t + R (uint32_t a, uint32_t b, uint32_t c, uint32_t k) + { + return b ^ util::rotatel (a + c, k); + } + + + /////////////////////////////////////////////////////////////////////////// + constexpr + std::array + quarter (std::array y) noexcept + { + std::array z {}; + + z[1] = R (y[0], y[1], y[3], 7); + z[2] = R (z[1], y[2], y[0], 9); + z[3] = R (z[2], y[3], z[1], 13); + z[0] = R (z[3], y[0], z[2], 18); + + return z; + } + + + /////////////////////////////////////////////////////////////////////////// + constexpr + std::array + row (const std::array y) noexcept + { + const auto [z00, z01, z02, z03] = quarter ({y[ 0], y[ 1], y[ 2], y[ 3]}); + const auto [z05, z06, z07, z04] = quarter ({y[ 5], y[ 6], y[ 7], y[ 4]}); + const auto [z10, z11, z08, z09] = quarter ({y[10], y[11], y[ 8], y[ 9]}); + const auto [z15, z12, z13, z14] = quarter ({y[15], y[12], y[13], y[14]}); + + return { + z00, z01, z02, z03, + z04, z05, z06, z07, + z08, z09, z10, z11, + z12, z13, z14, z15 + }; + } + + + /////////////////////////////////////////////////////////////////////////// + constexpr + std::array + col (const std::array x) noexcept + { + const auto [y00, y04, y08, y12] = quarter ({x[ 0], x[ 4], x[ 8], x[12]}); + const auto [y05, y09, y13, y01] = quarter ({x[ 5], x[ 9], x[13], x[ 1]}); + const auto [y10, y14, y02, y06] = quarter ({x[10], x[14], x[ 2], x[ 6]}); + const auto [y15, y03, y07, y11] = quarter ({x[15], x[ 3], x[ 7], x[11]}); + + return { + y00, y01, y02, y03, + y04, y05, y06, y07, + y08, y09, y10, y11, + y12, y13, y14, y15, + }; + } + + + /////////////////////////////////////////////////////////////////////////// + constexpr + std::array + doubleround (const std::array x) noexcept + { + return row (col (x)); + } +} + + +namespace util::crypto { + /////////////////////////////////////////////////////////////////////////// + std::array + salsa20 (const std::array) noexcept; +} diff --git a/test/crypto/salsa.cpp b/test/crypto/salsa.cpp new file mode 100644 index 00000000..e3478be9 --- /dev/null +++ b/test/crypto/salsa.cpp @@ -0,0 +1,229 @@ +#include "crypto/salsa.hpp" + +#include "tap.hpp" + + +/////////////////////////////////////////////////////////////////////////////// +void +test_quarter (util::TAP::logger &tap) +{ + static const struct { + std::array 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 (util::crypto::salsa::quarter (TESTS[i].a), TESTS[i].b, "quarter %zu", i); +} + + +/////////////////////////////////////////////////////////////////////////////// +void +test_row (util::TAP::logger &tap) +{ + static const struct { + std::array 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 (util::crypto::salsa::row (TESTS[i].a), TESTS[i].b, "row %zu", i); +} + + +/////////////////////////////////////////////////////////////////////////////// +void +test_col (util::TAP::logger &tap) +{ + static const struct { + std::array 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 (util::crypto::salsa::col (TESTS[i].a), TESTS[i].b, "col %zu", i); +} + + +/////////////////////////////////////////////////////////////////////////////// +void +test_doubleround (util::TAP::logger &tap) +{ + static const struct { + std::array 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 (util::crypto::salsa::doubleround (TESTS[i].a), TESTS[i].b, "doubleround %zu", i); +} + + +/////////////////////////////////////////////////////////////////////////////// +void +test_salsa20 (util::TAP::logger &tap) +{ + static const struct { + std::array 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, 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 } }, + + { { 0xd3, 0x9f, 0x0d, 0x73, 0x4c, 0x37, 0x52, 0xb7, + 0x03, 0x75, 0xde, 0x25, 0xbf, 0xbb, 0xea, 0x88, + 0x31, 0xed, 0xb3, 0x30, 0x01, 0x6a, 0xb2, 0xdb, + 0xaf, 0xc7, 0xa6, 0x30, 0x56, 0x10, 0xb3, 0xcf, + 0x1f, 0xf0, 0x20, 0x3f, 0x0f, 0x53, 0x5d, 0xa1, + 0x74, 0x93, 0x30, 0x71, 0xee, 0x37, 0xcc, 0x24, + 0x4f, 0xc9, 0xeb, 0x4f, 0x03, 0x51, 0x9c, 0x2f, + 0xcb, 0x1a, 0xf4, 0xf3, 0x58, 0x76, 0x68, 0x36 }, + { 0x6d, 0x2a, 0xb2, 0xa8, 0x9c, 0xf0, 0xf8, 0xee, + 0xa8, 0xc4, 0xbe, 0xcb, 0x1a, 0x6e, 0xaa, 0x9a, + 0x1d, 0x1d, 0x96, 0x1a, 0x96, 0x1e, 0xeb, 0xf9, + 0xbe, 0xa3, 0xfb, 0x30, 0x45, 0x90, 0x33, 0x39, + 0x76, 0x28, 0x98, 0x9d, 0xb4, 0x39, 0x1b, 0x5e, + 0x6b, 0x2a, 0xec, 0x23, 0x1b, 0x6f, 0x72, 0x72, + 0xdb, 0xec, 0xe8, 0x87, 0x6f, 0x9b, 0x6e, 0x12, + 0x18, 0xe8, 0x5f, 0x9e, 0xb3, 0x13, 0x30, 0xca } }, + + { { 0x58, 0x76, 0x68, 0x36, 0x4f, 0xc9, 0xeb, 0x4f, + 0x03, 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 (util::crypto::salsa20 (TESTS[i].a), TESTS[i].b, "salsa20 %zu", i); + + struct { + std::array 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 = util::crypto::salsa20 (million.a); + + tap.expect_eq (million.a, million.b, "salsa20 million"); +} + + +/////////////////////////////////////////////////////////////////////////////// +int +main (void) +{ + util::TAP::logger tap; + + test_quarter (tap); + test_row (tap); + test_col (tap); + test_doubleround (tap); + test_salsa20 (tap); + + return 0; +}