Loading src/crypto/sha.cpp +30 −11 Changes for src/crypto/sha.cpp: 30 added lines, 11 removed lines. Original line number Diff line number Diff line Loading @@ -289,7 +289,9 @@ std::vector<uint8_t> netplus::sha256_hash(const std::vector<uint8_t>& input) { } // SHA-384: Native implementation (SHA-512 with different IVs, truncated to 48 bytes) std::vector<uint8_t> netplus::sha384_hash(const std::vector<uint8_t>& input) { // SHA-384 and SHA-512 share the compression function; they differ in the // initial hash values and in how much of the final state is output. static std::vector<uint8_t> sha512_family(const std::vector<uint8_t>& input, const uint64_t iv[8], size_t outWords) { #define SHA512_ROR(value, bits) (((value) >> (bits)) | ((value) << (64 - (bits)))) #define SHA512_SIG0(x) (SHA512_ROR(x, 1) ^ SHA512_ROR(x, 8) ^ ((x) >> 7)) Loading @@ -297,13 +299,8 @@ std::vector<uint8_t> netplus::sha384_hash(const std::vector<uint8_t>& input) { #define SHA512_EPS0(x) (SHA512_ROR(x, 28) ^ SHA512_ROR(x, 34) ^ SHA512_ROR(x, 39)) #define SHA512_EPS1(x) (SHA512_ROR(x, 14) ^ SHA512_ROR(x, 18) ^ SHA512_ROR(x, 41)) // SHA-384 initial hash values (from the fractional parts of the square roots of the 9th-16th primes) uint64_t h[8] = { 0xcbbb9d5dc1059ed8ULL, 0x629a292a367cd507ULL, 0x9159015a3070dd17ULL, 0x152fecd8f70e5939ULL, 0x67332667ffc00b31ULL, 0x8eb44a8768581511ULL, 0xdb0c2e0d64f98fa7ULL, 0x47b5481dbefa4fa4ULL }; uint64_t h[8]; for (int i = 0; i < 8; ++i) h[i] = iv[i]; // SHA-512 round constants static const uint64_t k[80] = { Loading Loading @@ -368,9 +365,9 @@ std::vector<uint8_t> netplus::sha384_hash(const std::vector<uint8_t>& input) { h[4] += e; h[5] += f; h[6] += g; h[7] += l; } // Produce final hash - SHA-384 uses first 6 words of SHA-512 state (48 bytes) std::vector<uint8_t> hash(48); for (int i = 0; i < 6; ++i) { // Produce final hash: SHA-384 outputs the first 6 state words, SHA-512 all 8 std::vector<uint8_t> hash(outWords * 8); for (size_t i = 0; i < outWords; ++i) { hash[i * 8] = (h[i] >> 56) & 0xFF; hash[i * 8 + 1] = (h[i] >> 48) & 0xFF; hash[i * 8 + 2] = (h[i] >> 40) & 0xFF; Loading @@ -389,6 +386,28 @@ std::vector<uint8_t> netplus::sha384_hash(const std::vector<uint8_t>& input) { #undef SHA512_EPS1 } std::vector<uint8_t> netplus::sha384_hash(const std::vector<uint8_t>& input) { // fractional parts of the square roots of the 9th-16th primes static const uint64_t iv[8] = { 0xcbbb9d5dc1059ed8ULL, 0x629a292a367cd507ULL, 0x9159015a3070dd17ULL, 0x152fecd8f70e5939ULL, 0x67332667ffc00b31ULL, 0x8eb44a8768581511ULL, 0xdb0c2e0d64f98fa7ULL, 0x47b5481dbefa4fa4ULL }; return sha512_family(input, iv, 6); } std::vector<uint8_t> netplus::sha512_hash(const std::vector<uint8_t>& input) { // fractional parts of the square roots of the first 8 primes static const uint64_t iv[8] = { 0x6a09e667f3bcc908ULL, 0xbb67ae8584caa73bULL, 0x3c6ef372fe94f82bULL, 0xa54ff53a5f1d36f1ULL, 0x510e527fade682d1ULL, 0x9b05688c2b3e6c1fULL, 0x1f83d9abfb41bd6bULL, 0x5be0cd19137e2179ULL }; return sha512_family(input, iv, 8); } // HMAC-SHA256 implementation std::vector<uint8_t> netplus::hmac_sha256(const std::vector<uint8_t>& key, const std::vector<uint8_t>& msg) { Loading src/crypto/sha.h +1 −0 Changes for src/crypto/sha.h: 1 added line, 0 removed lines. Original line number Diff line number Diff line Loading @@ -7,6 +7,7 @@ namespace netplus { extern std::vector<uint8_t> sha1_hash(const std::vector<uint8_t>& input); extern std::vector<uint8_t> sha256_hash(const std::vector<uint8_t>& input); extern std::vector<uint8_t> sha384_hash(const std::vector<uint8_t>& input); extern std::vector<uint8_t> sha512_hash(const std::vector<uint8_t>& input); // HMAC-SHA1 functions (RFC 2104, needed for Kerberos RFC 3962 PBKDF2) extern std::vector<uint8_t> hmac_sha1(const std::vector<uint8_t>& key, Loading test/sha_nist.cpp +57 −0 Changes for test/sha_nist.cpp: 57 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -159,6 +159,63 @@ int main() { print_result("SHA-384: One million 'a's", got, expected); } std::cout << std::endl << "========== SHA-512 NIST FIPS 180-4 Test Vectors ==========" << std::endl; { std::vector<uint8_t> input; auto hash = sha512_hash(input); std::string got = to_hex(hash); std::string expected = "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e"; if (got == expected) passed++; else failed++; print_result("SHA-512: Empty string", got, expected); } { std::vector<uint8_t> input = {'a', 'b', 'c'}; auto hash = sha512_hash(input); std::string got = to_hex(hash); std::string expected = "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f"; if (got == expected) passed++; else failed++; print_result("SHA-512: 'abc'", got, expected); } { std::string input_str = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu"; std::vector<uint8_t> input(input_str.begin(), input_str.end()); auto hash = sha512_hash(input); std::string got = to_hex(hash); std::string expected = "8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909"; if (got == expected) passed++; else failed++; print_result("SHA-512: Long string", got, expected); } { std::vector<uint8_t> input(1000000, 'a'); auto hash = sha512_hash(input); std::string got = to_hex(hash); std::string expected = "e718483d0ce769644e2e42c7bc15b4638e1f98b13b2044285632a803afa973ebde0ff244877ea60a4cb0432ce577c31beb009c5c2c49aa2e4eadb217ad8cc09b"; if (got == expected) passed++; else failed++; print_result("SHA-512: One million 'a's", got, expected); } { std::vector<uint8_t> input(111, 'x'); auto hash = sha512_hash(input); std::string got = to_hex(hash); std::string expected = "9a2a120825c2319867758ec277924f6faa254968bf752046dacdd948d8ad299b10359fd04bfd7d3810b5fa1b16a294236138baff981cbb85248478053ac4d3dd"; if (got == expected) passed++; else failed++; print_result("SHA-512: 111 bytes (padding edge)", got, expected); } { std::vector<uint8_t> input(112, 'x'); auto hash = sha512_hash(input); std::string got = to_hex(hash); std::string expected = "a3722b515ef40c910f2419f6e0da8ca51d410114ce6272faae64045f9e9f630e7fa8dd5a3243c9860b899d148c3da4bc0f9e07454542604d030bb55531fe0d5b"; if (got == expected) passed++; else failed++; print_result("SHA-512: 112 bytes (padding edge)", got, expected); } std::cout << std::endl << "========== Summary ==========" << std::endl; std::cout << "Passed: " << passed << std::endl; std::cout << "Failed: " << failed << std::endl; Loading Loading
src/crypto/sha.cpp +30 −11 Changes for src/crypto/sha.cpp: 30 added lines, 11 removed lines. Original line number Diff line number Diff line Loading @@ -289,7 +289,9 @@ std::vector<uint8_t> netplus::sha256_hash(const std::vector<uint8_t>& input) { } // SHA-384: Native implementation (SHA-512 with different IVs, truncated to 48 bytes) std::vector<uint8_t> netplus::sha384_hash(const std::vector<uint8_t>& input) { // SHA-384 and SHA-512 share the compression function; they differ in the // initial hash values and in how much of the final state is output. static std::vector<uint8_t> sha512_family(const std::vector<uint8_t>& input, const uint64_t iv[8], size_t outWords) { #define SHA512_ROR(value, bits) (((value) >> (bits)) | ((value) << (64 - (bits)))) #define SHA512_SIG0(x) (SHA512_ROR(x, 1) ^ SHA512_ROR(x, 8) ^ ((x) >> 7)) Loading @@ -297,13 +299,8 @@ std::vector<uint8_t> netplus::sha384_hash(const std::vector<uint8_t>& input) { #define SHA512_EPS0(x) (SHA512_ROR(x, 28) ^ SHA512_ROR(x, 34) ^ SHA512_ROR(x, 39)) #define SHA512_EPS1(x) (SHA512_ROR(x, 14) ^ SHA512_ROR(x, 18) ^ SHA512_ROR(x, 41)) // SHA-384 initial hash values (from the fractional parts of the square roots of the 9th-16th primes) uint64_t h[8] = { 0xcbbb9d5dc1059ed8ULL, 0x629a292a367cd507ULL, 0x9159015a3070dd17ULL, 0x152fecd8f70e5939ULL, 0x67332667ffc00b31ULL, 0x8eb44a8768581511ULL, 0xdb0c2e0d64f98fa7ULL, 0x47b5481dbefa4fa4ULL }; uint64_t h[8]; for (int i = 0; i < 8; ++i) h[i] = iv[i]; // SHA-512 round constants static const uint64_t k[80] = { Loading Loading @@ -368,9 +365,9 @@ std::vector<uint8_t> netplus::sha384_hash(const std::vector<uint8_t>& input) { h[4] += e; h[5] += f; h[6] += g; h[7] += l; } // Produce final hash - SHA-384 uses first 6 words of SHA-512 state (48 bytes) std::vector<uint8_t> hash(48); for (int i = 0; i < 6; ++i) { // Produce final hash: SHA-384 outputs the first 6 state words, SHA-512 all 8 std::vector<uint8_t> hash(outWords * 8); for (size_t i = 0; i < outWords; ++i) { hash[i * 8] = (h[i] >> 56) & 0xFF; hash[i * 8 + 1] = (h[i] >> 48) & 0xFF; hash[i * 8 + 2] = (h[i] >> 40) & 0xFF; Loading @@ -389,6 +386,28 @@ std::vector<uint8_t> netplus::sha384_hash(const std::vector<uint8_t>& input) { #undef SHA512_EPS1 } std::vector<uint8_t> netplus::sha384_hash(const std::vector<uint8_t>& input) { // fractional parts of the square roots of the 9th-16th primes static const uint64_t iv[8] = { 0xcbbb9d5dc1059ed8ULL, 0x629a292a367cd507ULL, 0x9159015a3070dd17ULL, 0x152fecd8f70e5939ULL, 0x67332667ffc00b31ULL, 0x8eb44a8768581511ULL, 0xdb0c2e0d64f98fa7ULL, 0x47b5481dbefa4fa4ULL }; return sha512_family(input, iv, 6); } std::vector<uint8_t> netplus::sha512_hash(const std::vector<uint8_t>& input) { // fractional parts of the square roots of the first 8 primes static const uint64_t iv[8] = { 0x6a09e667f3bcc908ULL, 0xbb67ae8584caa73bULL, 0x3c6ef372fe94f82bULL, 0xa54ff53a5f1d36f1ULL, 0x510e527fade682d1ULL, 0x9b05688c2b3e6c1fULL, 0x1f83d9abfb41bd6bULL, 0x5be0cd19137e2179ULL }; return sha512_family(input, iv, 8); } // HMAC-SHA256 implementation std::vector<uint8_t> netplus::hmac_sha256(const std::vector<uint8_t>& key, const std::vector<uint8_t>& msg) { Loading
src/crypto/sha.h +1 −0 Changes for src/crypto/sha.h: 1 added line, 0 removed lines. Original line number Diff line number Diff line Loading @@ -7,6 +7,7 @@ namespace netplus { extern std::vector<uint8_t> sha1_hash(const std::vector<uint8_t>& input); extern std::vector<uint8_t> sha256_hash(const std::vector<uint8_t>& input); extern std::vector<uint8_t> sha384_hash(const std::vector<uint8_t>& input); extern std::vector<uint8_t> sha512_hash(const std::vector<uint8_t>& input); // HMAC-SHA1 functions (RFC 2104, needed for Kerberos RFC 3962 PBKDF2) extern std::vector<uint8_t> hmac_sha1(const std::vector<uint8_t>& key, Loading
test/sha_nist.cpp +57 −0 Changes for test/sha_nist.cpp: 57 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -159,6 +159,63 @@ int main() { print_result("SHA-384: One million 'a's", got, expected); } std::cout << std::endl << "========== SHA-512 NIST FIPS 180-4 Test Vectors ==========" << std::endl; { std::vector<uint8_t> input; auto hash = sha512_hash(input); std::string got = to_hex(hash); std::string expected = "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e"; if (got == expected) passed++; else failed++; print_result("SHA-512: Empty string", got, expected); } { std::vector<uint8_t> input = {'a', 'b', 'c'}; auto hash = sha512_hash(input); std::string got = to_hex(hash); std::string expected = "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f"; if (got == expected) passed++; else failed++; print_result("SHA-512: 'abc'", got, expected); } { std::string input_str = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu"; std::vector<uint8_t> input(input_str.begin(), input_str.end()); auto hash = sha512_hash(input); std::string got = to_hex(hash); std::string expected = "8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909"; if (got == expected) passed++; else failed++; print_result("SHA-512: Long string", got, expected); } { std::vector<uint8_t> input(1000000, 'a'); auto hash = sha512_hash(input); std::string got = to_hex(hash); std::string expected = "e718483d0ce769644e2e42c7bc15b4638e1f98b13b2044285632a803afa973ebde0ff244877ea60a4cb0432ce577c31beb009c5c2c49aa2e4eadb217ad8cc09b"; if (got == expected) passed++; else failed++; print_result("SHA-512: One million 'a's", got, expected); } { std::vector<uint8_t> input(111, 'x'); auto hash = sha512_hash(input); std::string got = to_hex(hash); std::string expected = "9a2a120825c2319867758ec277924f6faa254968bf752046dacdd948d8ad299b10359fd04bfd7d3810b5fa1b16a294236138baff981cbb85248478053ac4d3dd"; if (got == expected) passed++; else failed++; print_result("SHA-512: 111 bytes (padding edge)", got, expected); } { std::vector<uint8_t> input(112, 'x'); auto hash = sha512_hash(input); std::string got = to_hex(hash); std::string expected = "a3722b515ef40c910f2419f6e0da8ca51d410114ce6272faae64045f9e9f630e7fa8dd5a3243c9860b899d148c3da4bc0f9e07454542604d030bb55531fe0d5b"; if (got == expected) passed++; else failed++; print_result("SHA-512: 112 bytes (padding edge)", got, expected); } std::cout << std::endl << "========== Summary ==========" << std::endl; std::cout << "Passed: " << passed << std::endl; std::cout << "Failed: " << failed << std::endl; Loading