Skip to content

Commit

Permalink
std::span<> for MessageAuthenticationCode
Browse files Browse the repository at this point in the history
  • Loading branch information
reneme committed Mar 20, 2023
1 parent eb51e40 commit 6632be6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/lib/mac/mac.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ void MessageAuthenticationCode::start_msg(const uint8_t nonce[], size_t nonce_le
/*
* Default (deterministic) MAC verification operation
*/
bool MessageAuthenticationCode::verify_mac(const uint8_t mac[], size_t length)
bool MessageAuthenticationCode::verify_mac_result(const uint8_t mac[], size_t length)
{
secure_vector<uint8_t> our_mac = final();

Expand Down
40 changes: 20 additions & 20 deletions src/lib/mac/mac.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <botan/buf_comp.h>
#include <botan/sym_algo.h>
#include <string>
#include <span>
#include <memory>

namespace Botan {
Expand Down Expand Up @@ -64,15 +65,7 @@ class BOTAN_PUBLIC_API(2,0) MessageAuthenticationCode : public Buffered_Computat
* Default implementation simply rejects all non-empty nonces
* since most hash/MAC algorithms do not support randomization
*/
virtual void start_msg(const uint8_t nonce[], size_t nonce_len);

/**
* Begin processing a message with a nonce
*
* @param nonce the per message nonce
*/
template<typename Alloc>
void start(const std::vector<uint8_t, Alloc>& nonce)
void start(std::span<const uint8_t> nonce)
{
start_msg(nonce.data(), nonce.size());
}
Expand Down Expand Up @@ -101,26 +94,19 @@ class BOTAN_PUBLIC_API(2,0) MessageAuthenticationCode : public Buffered_Computat
* @param length the length of param in
* @return true if the MAC is valid, false otherwise
*/
virtual bool verify_mac(const uint8_t in[], size_t length);

/**
* Verify a MAC.
* @param in the MAC to verify as a byte array
* @return true if the MAC is valid, false otherwise
*/
virtual bool verify_mac(const std::vector<uint8_t>& in)
bool verify_mac(const uint8_t in[], size_t length)
{
return verify_mac(in.data(), in.size());
return verify_mac_result(in, length);
}

/**
* Verify a MAC.
* @param in the MAC to verify as a byte array
* @return true if the MAC is valid, false otherwise
*/
virtual bool verify_mac(const secure_vector<uint8_t>& in)
bool verify_mac(std::span<const uint8_t> in)
{
return verify_mac(in.data(), in.size());
return verify_mac_result(in.data(), in.size());
}

/**
Expand Down Expand Up @@ -149,6 +135,20 @@ class BOTAN_PUBLIC_API(2,0) MessageAuthenticationCode : public Buffered_Computat
* if a key is ever reused for two different messages.
*/
virtual bool fresh_key_required_per_message() const { return false; }

protected:
/**
* Prepare for processing a message under the specified nonce
*
* If the MAC does not support nonces, it should not override the default
* implementation.
*/
virtual void start_msg(const uint8_t nonce[], size_t nonce_len);

/**
* Verify the MACs final result
*/
virtual bool verify_mac_result(const uint8_t in[], size_t length);
};

typedef MessageAuthenticationCode MAC;
Expand Down

0 comments on commit 6632be6

Please sign in to comment.