E-MailRelay
Classes | Public Member Functions | Static Public Member Functions | List of all members
G::Hash Class Reference

A class for creating HMACs using an arbitrary cryptographic hash function as per RFC-2104. More...

#include <ghash.h>

Classes

struct  Masked
 An overload discriminator for G::Hash::hmac() More...
 

Public Member Functions

template<typename Fn , typename Fn2 >
std::string mask (Fn predigest_fn, Fn2 digest_fn, std::size_t blocksize, const std::string &k)
 

Static Public Member Functions

template<typename Fn2 >
static std::string hmac (Fn2 digest, std::size_t blocksize, const std::string &key, const std::string &input)
 Computes a Hashed Message Authentication Code using the given hash function. More...
 
template<typename Fn2 >
static std::string hmac (Fn2 postdigest, const std::string &masked_key, const std::string &input, Masked)
 An hmac() overload using a masked key. More...
 
template<typename Fn1 , typename Fn2 >
static std::string mask (Fn1 predigest, Fn2 digest, std::size_t blocksize, const std::string &shared_key)
 Computes a masked key from the given shared key, returning a non-printable string. More...
 
static std::string printable (const std::string &input)
 Converts a binary string into a printable form, using a lowercase hexadecimal encoding. More...
 

Detailed Description

A class for creating HMACs using an arbitrary cryptographic hash function as per RFC-2104.

Definition at line 37 of file ghash.h.

Member Function Documentation

◆ hmac() [1/2]

template<typename Fn2 >
std::string G::Hash::hmac ( Fn2  digest,
std::size_t  blocksize,
const std::string &  key,
const std::string &  input 
)
static

Computes a Hashed Message Authentication Code using the given hash function.

This is typically for challenge-response authentication where the plaintext input is an arbitrary challenge string from the server that the client needs to hmac() using their shared private key.

See also RFC-2104 [HMAC-MD5].

For hash function H with block size B (64) using shared key SK:

< K = large(SK) ? H(SK) : SK
< ipad = 0x36 repeated B times
< opad = 0x5C repeated B times
< HMAC = H( K XOR opad , H( K XOR ipad , plaintext ) )
<

The H() function processes a stream of blocks; the first parameter above represents the first block, and the second parameter is the rest of the stream (zero-padded up to a block boundary).

The shared key can be up to B bytes, or if more than B bytes then K is the L-byte result of hashing the shared-key. K is zero-padded up to B bytes for XOR-ing.

Definition at line 120 of file ghash.h.

◆ hmac() [2/2]

template<typename Fn2 >
std::string G::Hash::hmac ( Fn2  postdigest,
const std::string &  masked_key,
const std::string &  input,
Masked   
)
static

An hmac() overload using a masked key.

The postdigest function should behave like G::Md5::postdigest() and it must throw an exception if the masked key is invalid.

Definition at line 136 of file ghash.h.

◆ mask() [1/2]

template<typename Fn , typename Fn2 >
std::string G::Hash::mask ( Fn  predigest_fn,
Fn2  digest_fn,
std::size_t  blocksize,
const std::string &  k 
)

Definition at line 127 of file ghash.h.

◆ mask() [2/2]

template<typename Fn1 , typename Fn2 >
static std::string G::Hash::mask ( Fn1  predigest,
Fn2  digest,
std::size_t  blocksize,
const std::string &  shared_key 
)
static

Computes a masked key from the given shared key, returning a non-printable string.

This can be passed to the 'masked' overload of hmac() once the message is known.

The predigest and digest functions must behave like G::Md5::predigest() and G::Md5::digest2().

A masked key (MK) is the result of doing the initial, plaintext-independent parts of HMAC computation, taking the intermediate state of both the inner and outer hash functions.

< K = large(SK) ? H(SK) : SK
< HKipad = H( K XOR ipad , )
< HKopad = H( K XOR opad , )
< MK := ( HKipad , HKopad )
<

◆ printable()

std::string G::Hash::printable ( const std::string &  input)
static

Converts a binary string into a printable form, using a lowercase hexadecimal encoding.

Definition at line 53 of file ghash.cpp.


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