Serenity Operating System
at master 147 lines 6.0 kB view raw
1/* 2 * Copyright (c) 2021, [your name here] <[your email here]> 3 * 4 * SPDX-License-Identifier: BSD-2-Clause 5 */ 6 7#include <LibCrypto/Authentication/HMAC.h> 8#include <LibCrypto/Hash/MD5.h> 9#include <LibCrypto/Hash/SHA1.h> 10#include <LibCrypto/Hash/SHA2.h> 11#include <LibTest/TestCase.h> 12#include <cstring> 13 14TEST_CASE(test_hmac_md5_name) 15{ 16 Crypto::Authentication::HMAC<Crypto::Hash::MD5> hmac("Well Hello Friends"sv); 17 EXPECT_EQ(hmac.class_name(), "HMAC-MD5"sv); 18} 19 20TEST_CASE(test_hmac_md5_process) 21{ 22 Crypto::Authentication::HMAC<Crypto::Hash::MD5> hmac("Well Hello Friends"sv); 23 u8 result[] { 24 0x3b, 0x5b, 0xde, 0x30, 0x3a, 0x54, 0x7b, 0xbb, 0x09, 0xfe, 0x78, 0x89, 0xbc, 0x9f, 0x22, 0xa3 25 }; 26 auto mac = hmac.process("Some bogus data"sv); 27 EXPECT(memcmp(result, mac.data, hmac.digest_size()) == 0); 28} 29 30TEST_CASE(test_hmac_md5_process_reuse) 31{ 32 Crypto::Authentication::HMAC<Crypto::Hash::MD5> hmac("Well Hello Friends"sv); 33 34 auto mac_0 = hmac.process("Some bogus data"sv); 35 auto mac_1 = hmac.process("Some bogus data"sv); 36 37 EXPECT(memcmp(mac_0.data, mac_1.data, hmac.digest_size()) == 0); 38} 39 40TEST_CASE(test_hmac_sha1_name) 41{ 42 Crypto::Authentication::HMAC<Crypto::Hash::SHA1> hmac("Well Hello Friends"sv); 43 EXPECT_EQ(hmac.class_name(), "HMAC-SHA1"sv); 44} 45 46TEST_CASE(test_hmac_sha1_process) 47{ 48 u8 key[] { 0xc8, 0x52, 0xe5, 0x4a, 0x2c, 0x03, 0x2b, 0xc9, 0x63, 0xd3, 0xc2, 0x79, 0x0f, 0x76, 0x43, 0xef, 0x36, 0xc3, 0x7a, 0xca }; 49 Crypto::Authentication::HMAC<Crypto::Hash::SHA1> hmac(ReadonlyBytes { key, sizeof(key) }); 50 u8 result[] { 51 0x2c, 0x57, 0x32, 0x61, 0x3b, 0xa7, 0x84, 0x87, 0x0e, 0x4f, 0x42, 0x07, 0x2f, 0xf0, 0xe7, 0x41, 0xd7, 0x15, 0xf4, 0x56 52 }; 53 u8 value[] { 54 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x16, 0x03, 0x03, 0x00, 0x10, 0x14, 0x00, 0x00, 0x0c, 0xa1, 0x91, 0x1a, 0x20, 0x59, 0xb5, 0x45, 0xa9, 0xb4, 0xad, 0x75, 0x3e 55 }; 56 auto mac = hmac.process(value, 29); 57 EXPECT(memcmp(result, mac.data, hmac.digest_size()) == 0); 58} 59 60TEST_CASE(test_hmac_sha1_process_reuse) 61{ 62 u8 key[] { 0xc8, 0x52, 0xe5, 0x4a, 0x2c, 0x03, 0x2b, 0xc9, 0x63, 0xd3, 0xc2, 0x79, 0x0f, 0x76, 0x43, 0xef, 0x36, 0xc3, 0x7a, 0xca }; 63 Crypto::Authentication::HMAC<Crypto::Hash::SHA1> hmac(ReadonlyBytes { key, sizeof(key) }); 64 u8 result[] { 65 0x2c, 0x57, 0x32, 0x61, 0x3b, 0xa7, 0x84, 0x87, 0x0e, 0x4f, 0x42, 0x07, 0x2f, 0xf0, 0xe7, 0x41, 0xd7, 0x15, 0xf4, 0x56 66 }; 67 u8 value[] { 68 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x16, 0x03, 0x03, 0x00, 0x10, 0x14, 0x00, 0x00, 0x0c, 0xa1, 0x91, 0x1a, 0x20, 0x59, 0xb5, 0x45, 0xa9, 0xb4, 0xad, 0x75, 0x3e 69 }; 70 hmac.update(value, 8); 71 hmac.update(value + 8, 5); 72 hmac.update(value + 13, 16); 73 auto mac = hmac.digest(); 74 EXPECT(memcmp(result, mac.data, hmac.digest_size()) == 0); 75} 76 77TEST_CASE(test_hmac_sha256_name) 78{ 79 Crypto::Authentication::HMAC<Crypto::Hash::SHA256> hmac("Well Hello Friends"sv); 80 EXPECT_EQ(hmac.class_name(), "HMAC-SHA256"sv); 81} 82 83TEST_CASE(test_hmac_sha256_process) 84{ 85 Crypto::Authentication::HMAC<Crypto::Hash::SHA256> hmac("Well Hello Friends"sv); 86 u8 result[] { 87 0x1a, 0xf2, 0x20, 0x62, 0xde, 0x3b, 0x84, 0x65, 0xc1, 0x25, 0x23, 0x99, 0x76, 0x15, 0x1b, 0xec, 0x15, 0x21, 0x82, 0x1f, 0x23, 0xca, 0x11, 0x66, 0xdd, 0x8c, 0x6e, 0xf1, 0x81, 0x3b, 0x7f, 0x1b 88 }; 89 auto mac = hmac.process("Some bogus data"sv); 90 EXPECT(memcmp(result, mac.data, hmac.digest_size()) == 0); 91} 92 93TEST_CASE(test_hmac_sha256_reuse) 94{ 95 Crypto::Authentication::HMAC<Crypto::Hash::SHA256> hmac("Well Hello Friends"sv); 96 97 auto mac_0 = hmac.process("Some bogus data"sv); 98 auto mac_1 = hmac.process("Some bogus data"sv); 99 100 EXPECT(memcmp(mac_0.data, mac_1.data, hmac.digest_size()) == 0); 101} 102 103TEST_CASE(test_hmac_sha256_data_is_same_size_as_block) 104{ 105 Crypto::Authentication::HMAC<Crypto::Hash::SHA256> hmac("Well Hello Friends"sv); 106 u8 result[] = { 107 0x1d, 0x90, 0xce, 0x68, 0x45, 0x0b, 0xba, 0xd6, 0xbe, 0x1c, 0xb2, 0x3a, 0xea, 0x7f, 0xac, 0x4b, 0x68, 0x08, 0xa4, 0x77, 0x81, 0x2a, 0xad, 0x5d, 0x05, 0xe2, 0x15, 0xe8, 0xf4, 0xcb, 0x06, 0xaf 108 }; 109 auto mac = hmac.process("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"sv); 110 EXPECT(memcmp(result, mac.data, hmac.digest_size()) == 0); 111} 112 113TEST_CASE(test_hmac_sha256_data_is_bigger_size_as_block) 114{ 115 Crypto::Authentication::HMAC<Crypto::Hash::SHA256> hmac("Well Hello Friends"sv); 116 u8 result[] = { 117 0x9b, 0xa3, 0x9e, 0xf3, 0xb4, 0x30, 0x5f, 0x6f, 0x67, 0xd0, 0xa8, 0xb0, 0xf0, 0xcb, 0x12, 0xf5, 0x85, 0xe2, 0x19, 0xba, 0x0c, 0x8b, 0xe5, 0x43, 0xf0, 0x93, 0x39, 0xa8, 0xa3, 0x07, 0xf1, 0x95 118 }; 119 auto mac = hmac.process("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"sv); 120 EXPECT(memcmp(result, mac.data, hmac.digest_size()) == 0); 121} 122 123TEST_CASE(test_hmac_sha512_name) 124{ 125 Crypto::Authentication::HMAC<Crypto::Hash::SHA512> hmac("Well Hello Friends"sv); 126 EXPECT_EQ(hmac.class_name(), "HMAC-SHA512"); 127} 128 129TEST_CASE(test_hmac_sha512_process) 130{ 131 Crypto::Authentication::HMAC<Crypto::Hash::SHA512> hmac("Well Hello Friends"sv); 132 u8 result[] { 133 0xeb, 0xa8, 0x34, 0x11, 0xfd, 0x5b, 0x46, 0x5b, 0xef, 0xbb, 0x67, 0x5e, 0x7d, 0xc2, 0x7c, 0x2c, 0x6b, 0xe1, 0xcf, 0xe6, 0xc7, 0xe4, 0x7d, 0xeb, 0xca, 0x97, 0xb7, 0x4c, 0xd3, 0x4d, 0x6f, 0x08, 0x9f, 0x0d, 0x3a, 0xf1, 0xcb, 0x00, 0x79, 0x78, 0x2f, 0x05, 0x8e, 0xeb, 0x94, 0x48, 0x0d, 0x50, 0x64, 0x3b, 0xca, 0x70, 0xe2, 0x69, 0x38, 0x4f, 0xe4, 0xb0, 0x49, 0x0f, 0xc5, 0x4c, 0x7a, 0xa7 134 }; 135 auto mac = hmac.process("Some bogus data"sv); 136 EXPECT(memcmp(result, mac.data, hmac.digest_size()) == 0); 137} 138 139TEST_CASE(test_hmac_sha512_reuse) 140{ 141 Crypto::Authentication::HMAC<Crypto::Hash::SHA512> hmac("Well Hello Friends"sv); 142 143 auto mac_0 = hmac.process("Some bogus data"sv); 144 auto mac_1 = hmac.process("Some bogus data"sv); 145 146 EXPECT(memcmp(mac_0.data, mac_1.data, hmac.digest_size()) == 0); 147}