Created
August 11, 2023 10:38
-
-
Save uemuraj/da870e1e7f10ca24c8438235453091a9 to your computer and use it in GitHub Desktop.
HMAC は関数一発で計算できたんかい。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#ifndef WIN32_LEAN_AND_MEAN | |
#define WIN32_LEAN_AND_MEAN | |
#endif | |
#pragma comment (lib, "bcrypt") | |
#include <Windows.h> | |
#include <bcrypt.h> | |
#include <array> | |
#include <system_error> | |
class ntstatus_error_category : public std::error_category | |
{ | |
public: | |
const char * name() const noexcept override | |
{ | |
return "ntstatus"; | |
} | |
std::string message(int ev) const override | |
{ | |
std::string msg; | |
if (HMODULE src = ::LoadLibrary(TEXT("NTDLL.DLL"))) | |
{ | |
void * buf{}; | |
constexpr DWORD flags = FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_FROM_HMODULE; | |
if (::FormatMessageA(flags, src, ev, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR) &buf, 0, nullptr)) | |
{ | |
msg = (const char *) buf; | |
} | |
::LocalFree(buf); | |
::FreeLibrary(src); | |
} | |
return msg; | |
} | |
}; | |
inline void hmac_md5(unsigned char * data, size_t data_len, unsigned char * key, size_t key_len, std::array<unsigned char, 16> & digest) | |
{ | |
if (auto status = ::BCryptHash(BCRYPT_HMAC_MD5_ALG_HANDLE, key, key_len, data, data_len, digest.data(), digest.size())) | |
{ | |
throw std::system_error(status, ntstatus_error_category(), __FUNCTION__); | |
} | |
} | |
inline void hmac_sha1(unsigned char * data, size_t data_len, unsigned char * key, size_t key_len, std::array<unsigned char, 20> & digest) | |
{ | |
if (auto status = ::BCryptHash(BCRYPT_HMAC_SHA1_ALG_HANDLE, key, key_len, data, data_len, digest.data(), digest.size())) | |
{ | |
throw std::system_error(status, ntstatus_error_category(), __FUNCTION__); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "gtest/gtest.h" | |
#include "hmac.h" | |
#include <numeric> | |
TEST(RFC2202, MD5_test_case_1) | |
{ | |
std::array<unsigned char, 16> key; | |
key.fill(0x0b); | |
unsigned char data[] = "Hi There"; | |
std::array<unsigned char, 16> expected{ 0x92, 0x94, 0x72, 0x7a, 0x36, 0x38, 0xbb, 0x1c, 0x13, 0xf4, 0x8e, 0xf8, 0x15, 0x8b, 0xfc, 0x9d }; | |
std::array<unsigned char, 16> actual; | |
hmac_md5(data, sizeof(data) - 1, key.data(), key.size(), actual); | |
EXPECT_EQ(actual, expected); | |
} | |
TEST(RFC2202, MD5_test_case_2) | |
{ | |
unsigned char key[] = "Jefe"; | |
unsigned char data[] = "what do ya want for nothing?"; | |
std::array<unsigned char, 16> expected{ 0x75, 0x0c, 0x78, 0x3e, 0x6a, 0xb0, 0xb5, 0x03, 0xea, 0xa8, 0x6e, 0x31, 0x0a, 0x5d, 0xb7, 0x38 }; | |
std::array<unsigned char, 16> actual; | |
hmac_md5(data, sizeof(data) - 1, key, sizeof(key) - 1, actual); | |
EXPECT_EQ(actual, expected); | |
} | |
TEST(RFC2202, MD5_test_case_3) | |
{ | |
std::array<unsigned char, 16> key; | |
key.fill(0xAA); | |
std::array<unsigned char, 50> data; | |
data.fill(0xDD); | |
std::array<unsigned char, 16> expected{ 0x56, 0xbe, 0x34, 0x52, 0x1d, 0x14, 0x4c, 0x88, 0xdb, 0xb8, 0xc7, 0x33, 0xf0, 0xe8, 0xb3, 0xf6 }; | |
std::array<unsigned char, 16> actual; | |
hmac_md5(data.data(), data.size(), key.data(), key.size(), actual); | |
EXPECT_EQ(actual, expected); | |
} | |
TEST(RFC2202, MD5_test_case_4) | |
{ | |
std::array<unsigned char, 25> key; | |
std::iota(key.begin(), key.end(), 1); | |
std::array<unsigned char, 50> data; | |
data.fill(0xCD); | |
std::array<unsigned char, 16> expected{ 0x69, 0x7e, 0xaf, 0x0a, 0xca, 0x3a, 0x3a, 0xea, 0x3a, 0x75, 0x16, 0x47, 0x46, 0xff, 0xaa, 0x79 }; | |
std::array<unsigned char, 16> actual; | |
hmac_md5(data.data(), data.size(), key.data(), key.size(), actual); | |
EXPECT_EQ(actual, expected); | |
} | |
TEST(RFC2202, MD5_test_case_5) | |
{ | |
std::array<unsigned char, 16> key; | |
key.fill(0x0C); | |
unsigned char data[] = "Test With Truncation"; | |
std::array<unsigned char, 16> expected{ 0x56, 0x46, 0x1e, 0xf2, 0x34, 0x2e, 0xdc, 0x00, 0xf9, 0xba, 0xb9, 0x95, 0x69, 0x0e, 0xfd, 0x4c }; | |
std::array<unsigned char, 16> actual; | |
hmac_md5(data, sizeof(data) - 1, key.data(), key.size(), actual); | |
EXPECT_EQ(actual, expected); | |
} | |
TEST(RFC2202, MD5_test_case_6) | |
{ | |
std::array<unsigned char, 80> key; | |
key.fill(0xAA); | |
unsigned char data[] = "Test Using Larger Than Block-Size Key - Hash Key First"; | |
std::array<unsigned char, 16> expected{ 0x6b, 0x1a, 0xb7, 0xfe, 0x4b, 0xd7, 0xbf, 0x8f, 0x0b, 0x62, 0xe6, 0xce, 0x61, 0xb9, 0xd0, 0xcd }; | |
std::array<unsigned char, 16> actual; | |
hmac_md5(data, sizeof(data) - 1, key.data(), key.size(), actual); | |
EXPECT_EQ(actual, expected); | |
} | |
TEST(RFC2202, MD5_test_case_7) | |
{ | |
std::array<unsigned char, 80> key; | |
key.fill(0xAA); | |
unsigned char data[] = "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data"; | |
std::array<unsigned char, 16> expected{ 0x6f, 0x63, 0x0f, 0xad, 0x67, 0xcd, 0xa0, 0xee, 0x1f, 0xb1, 0xf5, 0x62, 0xdb, 0x3a, 0xa5, 0x3e }; | |
std::array<unsigned char, 16> actual; | |
hmac_md5(data, sizeof(data) - 1, key.data(), key.size(), actual); | |
EXPECT_EQ(actual, expected); | |
} | |
TEST(RFC2202, SHA1_test_case_1) | |
{ | |
std::array<unsigned char, 20> key; | |
key.fill(0x0B); | |
unsigned char data[] = "Hi There"; | |
std::array<unsigned char, 20> expected{ 0xb6, 0x17, 0x31, 0x86, 0x55, 0x05, 0x72, 0x64, 0xe2, 0x8b, 0xc0, 0xb6, 0xfb, 0x37, 0x8c, 0x8e, 0xf1, 0x46, 0xbe, 0x00 }; | |
std::array<unsigned char, 20> actual; | |
hmac_sha1(data, sizeof(data) - 1, key.data(), key.size(), actual); | |
EXPECT_EQ(actual, expected); | |
} | |
TEST(RFC2202, SHA1_test_case_2) | |
{ | |
unsigned char key[] = "Jefe"; | |
unsigned char data[] = "what do ya want for nothing?"; | |
std::array<unsigned char, 20> expected{ 0xef, 0xfc, 0xdf, 0x6a, 0xe5, 0xeb, 0x2f, 0xa2, 0xd2, 0x74, 0x16, 0xd5, 0xf1, 0x84, 0xdf, 0x9c, 0x25, 0x9a, 0x7c, 0x79 }; | |
std::array<unsigned char, 20> actual; | |
hmac_sha1(data, sizeof(data) - 1, key, sizeof(key) - 1, actual); | |
EXPECT_EQ(actual, expected); | |
} | |
TEST(RFC2202, SHA1_test_case_3) | |
{ | |
std::array<unsigned char, 20> key; | |
key.fill(0xAA); | |
std::array<unsigned char, 50> data; | |
data.fill(0xDD); | |
std::array<unsigned char, 20> expected{ 0x12, 0x5d, 0x73, 0x42, 0xb9, 0xac, 0x11, 0xcd, 0x91, 0xa3, 0x9a, 0xf4, 0x8a, 0xa1, 0x7b, 0x4f, 0x63, 0xf1, 0x75, 0xd3 }; | |
std::array<unsigned char, 20> actual; | |
hmac_sha1(data.data(), data.size(), key.data(), key.size(), actual); | |
EXPECT_EQ(actual, expected); | |
} | |
TEST(RFC2202, SHA1_test_case_4) | |
{ | |
std::array<unsigned char, 25> key; | |
std::iota(key.begin(), key.end(), 1); | |
std::array<unsigned char, 50> data; | |
data.fill(0xCD); | |
std::array<unsigned char, 20> expected{ 0x4c, 0x90, 0x07, 0xf4, 0x02, 0x62, 0x50, 0xc6, 0xbc, 0x84, 0x14, 0xf9, 0xbf, 0x50, 0xc8, 0x6c, 0x2d, 0x72, 0x35, 0xda }; | |
std::array<unsigned char, 20> actual; | |
hmac_sha1(data.data(), data.size(), key.data(), key.size(), actual); | |
EXPECT_EQ(actual, expected); | |
} | |
TEST(RFC2202, SHA1_test_case_5) | |
{ | |
std::array<unsigned char, 20> key; | |
key.fill(0x0C); | |
unsigned char data[] = "Test With Truncation"; | |
std::array<unsigned char, 20> expected{ 0x4c, 0x1a, 0x03, 0x42, 0x4b, 0x55, 0xe0, 0x7f, 0xe7, 0xf2, 0x7b, 0xe1, 0xd5, 0x8b, 0xb9, 0x32, 0x4a, 0x9a, 0x5a, 0x04 }; | |
std::array<unsigned char, 20> actual; | |
hmac_sha1(data, sizeof(data) - 1, key.data(), key.size(), actual); | |
EXPECT_EQ(actual, expected); | |
} | |
TEST(RFC2202, SHA1_test_case_6) | |
{ | |
std::array<unsigned char, 80> key; | |
key.fill(0xAA); | |
unsigned char data[] = "Test Using Larger Than Block-Size Key - Hash Key First"; | |
std::array<unsigned char, 20> expected{ 0xaa, 0x4a, 0xe5, 0xe1, 0x52, 0x72, 0xd0, 0x0e, 0x95, 0x70, 0x56, 0x37, 0xce, 0x8a, 0x3b, 0x55, 0xed, 0x40, 0x21, 0x12 }; | |
std::array<unsigned char, 20> actual; | |
hmac_sha1(data, sizeof(data) - 1, key.data(), key.size(), actual); | |
EXPECT_EQ(actual, expected); | |
} | |
TEST(RFC2202, SHA1_test_case_7) | |
{ | |
std::array<unsigned char, 80> key; | |
key.fill(0xAA); | |
unsigned char data[] = "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data"; | |
std::array<unsigned char, 20> expected{ 0xe8, 0xe9, 0x9d, 0x0f, 0x45, 0x23, 0x7d, 0x78, 0x6d, 0x6b, 0xba, 0xa7, 0x96, 0x5c, 0x78, 0x08, 0xbb, 0xff, 0x1a, 0x91 }; | |
std::array<unsigned char, 20> actual; | |
hmac_sha1(data, sizeof(data) - 1, key.data(), key.size(), actual); | |
EXPECT_EQ(actual, expected); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Copilot がコメントからテストデータのリテラルを書いてくれるの、ずっごい楽。