Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions hmac.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <algorithm>
#include <stdexcept>
#include "hmac.hpp"

namespace hmac {
Expand Down Expand Up @@ -82,6 +83,8 @@ namespace hmac {
}

std::vector<uint8_t> get_hmac(const void* key_ptr, size_t key_len, const void* msg_ptr, size_t msg_len, TypeHash type) {
if ((key_len > 0 && key_ptr == nullptr) || (msg_len > 0 && msg_ptr == nullptr))
throw std::invalid_argument("Null pointer with non-zero length");
size_t block_size = 0;
size_t digest_size = 0;

Expand Down
5 changes: 3 additions & 2 deletions hmac.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,13 @@ namespace hmac {
}

/// \brief Computes HMAC for raw binary data using the specified hash function.
/// \param key_ptr Pointer to the key buffer
/// \param key_ptr Pointer to the key buffer; must be non-null if key_len > 0
/// \param key_len Length of the key in bytes
/// \param msg_ptr Pointer to the message buffer
/// \param msg_ptr Pointer to the message buffer; must be non-null if msg_len > 0
/// \param msg_len Length of the message in bytes
/// \param type Hash function type
/// \return HMAC result as a vector of bytes
/// \throws std::invalid_argument If any pointer is null while the corresponding length is non-zero
std::vector<uint8_t> get_hmac(const void* key_ptr, size_t key_len, const void* msg_ptr, size_t msg_len, TypeHash type);

/// \brief Computes HMAC from key and message byte vectors using the specified hash function
Expand Down
8 changes: 8 additions & 0 deletions test_all.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <gtest/gtest.h>
#include <string>
#include <stdexcept>
#include "hmac.hpp"
#include "hmac_utils.hpp"

Expand Down Expand Up @@ -43,6 +44,13 @@ TEST(HMACTest, SHA512Uppercase) {
"C54DDF9647A949D0DF925A1C1F8BA1C9D721A671C396FDE1062A71F9F7FFAE5DC10F6BE15BE63BB0363D051365E23F890368C54828497B9AEF2EB2FC65B633E6");
}

TEST(HMACTest, NullPointersThrow) {
const char* msg = "abc";
EXPECT_THROW(hmac::get_hmac(nullptr, 1, msg, 3, hmac::TypeHash::SHA256), std::invalid_argument);
const char* key = "key";
EXPECT_THROW(hmac::get_hmac(key, 3, nullptr, 1, hmac::TypeHash::SHA256), std::invalid_argument);
}

TEST(TOTPTest, AtTime) {
const std::string totp_key = "12345678901234567890";
uint64_t test_time = 1234567890;
Expand Down
Loading