Skip to content

Commit

Permalink
security: add HMAC calculation functions
Browse files Browse the repository at this point in the history
  • Loading branch information
xweissada committed Aug 31, 2024
1 parent bb26bf9 commit 7a5cfce
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
1 change: 1 addition & 0 deletions vanetza/security/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ add_vanetza_component(security
ecc_point.cpp
ecdsa256.cpp
hashed_id.cpp
hmac.cpp
secured_message.cpp
sha.cpp
sign_service.cpp
Expand Down
27 changes: 27 additions & 0 deletions vanetza/security/hmac.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <vanetza/security/hmac.hpp>
#include <cryptopp/osrng.h>
#include <cryptopp/hmac.h>
#include <cryptopp/sha.h>

namespace vanetza
{
namespace security
{

KeyTag create_hmac_tag(const ByteBuffer& data, const HmacKey& hmacKey)
{
KeyTag keyTag;

// Calculate tag.
CryptoPP::HMAC<CryptoPP::SHA256> mac(hmacKey.data(), hmacKey.size());
unsigned char tag[hmacKey.size()];
mac.Update(data.data(), data.size());
mac.Final(tag);

// Tag is truncated to leftmost 128 bits.
std::copy_n(tag, keyTag.size(), keyTag.data());
return keyTag;
}

} // namespace security
} // namespace vanetza
23 changes: 23 additions & 0 deletions vanetza/security/hmac.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once
#include <vanetza/common/byte_buffer.hpp>
#include <array>
#include <cstdint>

namespace vanetza
{
namespace security
{

using HmacKey = std::array<uint8_t, 32>;
using KeyTag = std::array<uint8_t, 16>;

/**
* \brief generate HMAC key and create HMAC tag on data
* \param data data to be tagged
* \param hmacKey generated HMAC key
* \return tag of data generated with hmacKey
*/
KeyTag create_hmac_tag(const vanetza::ByteBuffer& data, const HmacKey& hmacKey);

} // namespace security
} // namespace vanetza

0 comments on commit 7a5cfce

Please sign in to comment.