Botan 1.10.17
Botan::HMAC Class Reference

#include <hmac.h>

Inheritance diagram for Botan::HMAC:
Botan::MessageAuthenticationCode Botan::Buffered_Computation Botan::SymmetricAlgorithm Botan::Algorithm

Public Member Functions

void clear ()
MessageAuthenticationCodeclone () const
SecureVector< bytefinal ()
void final (byte out[])
 HMAC (HashFunction *hash)
Key_Length_Specification key_spec () const
size_t maximum_keylength () const
size_t minimum_keylength () const
std::string name () const
size_t output_length () const
SecureVector< byteprocess (const byte in[], size_t length)
SecureVector< byteprocess (const MemoryRegion< byte > &in)
SecureVector< byteprocess (const std::string &in)
void set_key (const byte key[], size_t length)
void set_key (const SymmetricKey &key)
void update (byte in)
void update (const byte in[], size_t length)
void update (const MemoryRegion< byte > &in)
void update (const std::string &str)
template<typename T>
void update_be (const T in)
bool valid_keylength (size_t length) const
virtual bool verify_mac (const byte in[], size_t length)
 ~HMAC ()

Detailed Description

HMAC

Definition at line 19 of file hmac.h.

Constructor & Destructor Documentation

◆ HMAC()

Botan::HMAC::HMAC ( HashFunction * hash)
Parameters
hashthe hash to use for HMACing

Definition at line 87 of file hmac.cpp.

87 : hash(hash_in)
88 {
89 if(hash->hash_block_size() == 0)
90 throw Invalid_Argument("HMAC cannot be used with " + hash->name());
91
92 i_key.resize(hash->hash_block_size());
93 o_key.resize(hash->hash_block_size());
94 }
std::invalid_argument Invalid_Argument
Definition exceptn.h:20

Referenced by clone(), Botan::CryptoBox::decrypt(), Botan::CryptoBox::encrypt(), Botan::Core_Engine::find_mac(), and Botan::TLS_PRF::TLS_PRF().

◆ ~HMAC()

Botan::HMAC::~HMAC ( )
inline

Definition at line 38 of file hmac.h.

38{ delete hash; }

Member Function Documentation

◆ clear()

void Botan::HMAC::clear ( )
virtual

Zeroize internal state

Implements Botan::Algorithm.

Definition at line 61 of file hmac.cpp.

62 {
63 hash->clear();
64 zeroise(i_key);
65 zeroise(o_key);
66 }
void zeroise(MemoryRegion< T > &vec)
Definition secmem.h:428

References Botan::zeroise().

◆ clone()

MessageAuthenticationCode * Botan::HMAC::clone ( ) const
virtual

Get a new object representing the same algorithm as *this

Implements Botan::MessageAuthenticationCode.

Definition at line 79 of file hmac.cpp.

80 {
81 return new HMAC(hash->clone());
82 }
HMAC(HashFunction *hash)
Definition hmac.cpp:87

References HMAC().

◆ final() [1/2]

SecureVector< byte > Botan::Buffered_Computation::final ( )
inlineinherited

Complete the computation and retrieve the final result.

Returns
SecureVector holding the result

Definition at line 87 of file buf_comp.h.

88 {
89 SecureVector<byte> output(output_length());
90 final_result(&output[0]);
91 return output;
92 }
virtual size_t output_length() const =0

References output_length().

◆ final() [2/2]

void Botan::Buffered_Computation::final ( byte out[])
inlineinherited

Complete the computation and retrieve the final result.

Parameters
outThe byte array to be filled with the result. Must be of length output_length()

Definition at line 80 of file buf_comp.h.

80{ final_result(out); }

Referenced by Botan::X942_PRF::derive(), Botan::HandshakeHash::final(), and Botan::HandshakeHash::final_ssl3().

◆ key_spec()

Key_Length_Specification Botan::HMAC::key_spec ( ) const
inlinevirtual
Returns
object describing limits on key size

Implements Botan::SymmetricAlgorithm.

Definition at line 28 of file hmac.h.

29 {
30 // Absurd max length here is to support PBKDF2
31 return Key_Length_Specification(0, 512);
32 }
Key_Length_Specification(size_t keylen)
Definition key_spec.h:25

◆ maximum_keylength()

size_t Botan::SymmetricAlgorithm::maximum_keylength ( ) const
inlineinherited
Returns
minimum allowed key length

Definition at line 33 of file sym_algo.h.

34 {
35 return key_spec().maximum_keylength();
36 }
size_t maximum_keylength() const
Definition key_spec.h:69
virtual Key_Length_Specification key_spec() const =0

References key_spec().

◆ minimum_keylength()

size_t Botan::SymmetricAlgorithm::minimum_keylength ( ) const
inlineinherited
Returns
maxmium allowed key length

Definition at line 41 of file sym_algo.h.

42 {
43 return key_spec().minimum_keylength();
44 }
size_t minimum_keylength() const
Definition key_spec.h:61

References key_spec().

◆ name()

std::string Botan::HMAC::name ( ) const
virtual

Get the name of this algorithm.

Returns
name of this algorithm

Implements Botan::MessageAuthenticationCode.

Definition at line 71 of file hmac.cpp.

72 {
73 return "HMAC(" + hash->name() + ")";
74 }

◆ output_length()

size_t Botan::HMAC::output_length ( ) const
inlinevirtual
Returns
length of the output of this function in bytes

Implements Botan::Buffered_Computation.

Definition at line 26 of file hmac.h.

26{ return hash->output_length(); }

◆ process() [1/3]

SecureVector< byte > Botan::Buffered_Computation::process ( const byte in[],
size_t length )
inlineinherited

Update and finalize computation. Does the same as calling update() and final() consecutively.

Parameters
inthe input to process as a byte array
lengththe length of the byte array
Returns
the result of the call to final()

Definition at line 101 of file buf_comp.h.

102 {
103 add_data(in, length);
104 return final();
105 }

Referenced by Botan::EME1::EME1(), Botan::RTSS_Share::split(), and Botan::Cert_Extension::Subject_Key_ID::Subject_Key_ID().

◆ process() [2/3]

SecureVector< byte > Botan::Buffered_Computation::process ( const MemoryRegion< byte > & in)
inlineinherited

Update and finalize computation. Does the same as calling update() and final() consecutively.

Parameters
inthe input to process
Returns
the result of the call to final()

Definition at line 113 of file buf_comp.h.

114 {
115 add_data(&in[0], in.size());
116 return final();
117 }

References Botan::MemoryRegion< T >::size().

◆ process() [3/3]

SecureVector< byte > Botan::Buffered_Computation::process ( const std::string & in)
inlineinherited

Update and finalize computation. Does the same as calling update() and final() consecutively.

Parameters
inthe input to process as a string
Returns
the result of the call to final()

Definition at line 125 of file buf_comp.h.

126 {
127 update(in);
128 return final();
129 }
void update(const byte in[], size_t length)
Definition buf_comp.h:33

References update().

◆ set_key() [1/2]

void Botan::SymmetricAlgorithm::set_key ( const byte key[],
size_t length )
inlineinherited

Set the symmetric key of this object.

Parameters
keythe to be set as a byte array.
lengthin bytes of key param

Definition at line 68 of file sym_algo.h.

69 {
70 if(!valid_keylength(length))
71 throw Invalid_Key_Length(name(), length);
72 key_schedule(key, length);
73 }
virtual std::string name() const =0
bool valid_keylength(size_t length) const
Definition sym_algo.h:51
Invalid_Key_Length(const std::string &name, size_t length)
Definition exceptn.h:57

References Botan::Algorithm::name(), and valid_keylength().

◆ set_key() [2/2]

void Botan::SymmetricAlgorithm::set_key ( const SymmetricKey & key)
inlineinherited

◆ update() [1/4]

void Botan::Buffered_Computation::update ( byte in)
inlineinherited

Process a single byte.

Parameters
inthe byte to process

Definition at line 72 of file buf_comp.h.

72{ add_data(&in, 1); }

◆ update() [2/4]

void Botan::Buffered_Computation::update ( const byte in[],
size_t length )
inlineinherited

Add new input to process.

Parameters
inthe input to process as a byte array
lengthof param in in bytes

Definition at line 33 of file buf_comp.h.

33{ add_data(in, length); }

Referenced by Botan::X942_PRF::derive(), Botan::HandshakeHash::final(), Botan::HandshakeHash::final_ssl3(), process(), and Botan::EAX_Base::start_msg().

◆ update() [3/4]

void Botan::Buffered_Computation::update ( const MemoryRegion< byte > & in)
inlineinherited

Add new input to process.

Parameters
inthe input to process as a MemoryRegion

Definition at line 39 of file buf_comp.h.

40 {
41 add_data(&in[0], in.size());
42 }

References Botan::MemoryRegion< T >::size().

◆ update() [4/4]

void Botan::Buffered_Computation::update ( const std::string & str)
inlineinherited

Add new input to process.

Parameters
strthe input to process as a std::string. Will be interpreted as a byte array based on the strings encoding.

Definition at line 63 of file buf_comp.h.

64 {
65 add_data(reinterpret_cast<const byte*>(str.data()), str.size());
66 }

◆ update_be()

template<typename T>
void Botan::Buffered_Computation::update_be ( const T in)
inlineinherited

Add an integer in big-endian order

Parameters
inthe value

Definition at line 48 of file buf_comp.h.

49 {
50 for(size_t i = 0; i != sizeof(T); ++i)
51 {
52 byte b = get_byte(i, in);
53 add_data(&b, 1);
54 }
55 }
byte get_byte(size_t byte_num, T input)
Definition get_byte.h:21

References Botan::get_byte().

◆ valid_keylength()

bool Botan::SymmetricAlgorithm::valid_keylength ( size_t length) const
inlineinherited

Check whether a given key length is valid for this algorithm.

Parameters
lengththe key length to be checked.
Returns
true if the key length is valid.

Definition at line 51 of file sym_algo.h.

52 {
53 return key_spec().valid_keylength(length);
54 }
bool valid_keylength(size_t length) const
Definition key_spec.h:51

References key_spec().

Referenced by Botan::aont_package(), Botan::aont_unpackage(), set_key(), and Botan::EAX_Base::valid_keylength().

◆ verify_mac()

bool Botan::MessageAuthenticationCode::verify_mac ( const byte in[],
size_t length )
virtualinherited

Verify a MAC.

Parameters
inthe MAC to verify as a byte array
lengththe length of param in
Returns
true if the MAC is valid, false otherwise

Definition at line 16 of file mac.cpp.

17 {
18 SecureVector<byte> our_mac = final();
19
20 if(our_mac.size() != length)
21 return false;
22
23 return same_mem(&our_mac[0], &mac[0], length);
24 }
bool same_mem(const T *p1, const T *p2, size_t n)
Definition mem_ops.h:57

References Botan::same_mem(), and Botan::MemoryRegion< T >::size().


The documentation for this class was generated from the following files: