Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

std::span for MessageAuthenticationCode #3397

Merged
merged 1 commit into from
Mar 21, 2023
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
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
42 changes: 21 additions & 21 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 @@ -59,20 +60,12 @@ class BOTAN_PUBLIC_API(2,0) MessageAuthenticationCode : public Buffered_Computat
* an empty string is an error. One MAC which *requires* a per-message
* nonce be specified is GMAC.
*
* @param nonce the message nonce bytes
* @param nonce_len the size of len in bytes
* 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
* @param nonce the message nonce bytes
*/
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