Make the hmac_t interface a generic interface for message authentication codes
This commit is contained in:
@@ -16,23 +16,23 @@
|
||||
*/
|
||||
|
||||
/**
|
||||
* @defgroup hmac hmac
|
||||
* @defgroup mac mac
|
||||
* @{ @ingroup crypto
|
||||
*/
|
||||
|
||||
#ifndef HMAC_H_
|
||||
#define HMAC_H_
|
||||
#ifndef MAC_H_
|
||||
#define MAC_H_
|
||||
|
||||
typedef struct hmac_t hmac_t;
|
||||
typedef struct mac_t mac_t;
|
||||
|
||||
#include <library.h>
|
||||
|
||||
/**
|
||||
* Generic interface for message authentication using hash functions.
|
||||
* Generic interface for message authentication codes.
|
||||
*
|
||||
* Classes implementing this interface can use the PRF and signer wrappers.
|
||||
*/
|
||||
struct hmac_t {
|
||||
struct mac_t {
|
||||
|
||||
/**
|
||||
* Generate message authentication code.
|
||||
@@ -45,29 +45,28 @@ struct hmac_t {
|
||||
* @param data chunk of data to authenticate
|
||||
* @param out pointer where the generated bytes will be written
|
||||
*/
|
||||
void (*get_mac)(hmac_t *this, chunk_t data, u_int8_t *out);
|
||||
void (*get_mac)(mac_t *this, chunk_t data, u_int8_t *out);
|
||||
|
||||
/**
|
||||
* Get the size of the resulting MAC (i.e. the output size of the
|
||||
* underlying hash function).
|
||||
* Get the size of the resulting MAC.
|
||||
*
|
||||
* @return block size in bytes
|
||||
*/
|
||||
size_t (*get_mac_size)(hmac_t *this);
|
||||
size_t (*get_mac_size)(mac_t *this);
|
||||
|
||||
/**
|
||||
* Set the key to be used for the HMAC.
|
||||
* Set the key to be used for the MAC.
|
||||
*
|
||||
* Any key length must be accepted.
|
||||
*
|
||||
* @param key key to set
|
||||
*/
|
||||
void (*set_key) (hmac_t *this, chunk_t key);
|
||||
void (*set_key) (mac_t *this, chunk_t key);
|
||||
|
||||
/**
|
||||
* Destroys a hmac_t object.
|
||||
* Destroys a mac_t object.
|
||||
*/
|
||||
void (*destroy) (hmac_t *this);
|
||||
void (*destroy) (mac_t *this);
|
||||
};
|
||||
|
||||
#endif /** HMAC_H_ @}*/
|
||||
#endif /** MAC_H_ @}*/
|
||||
+15
-15
@@ -15,12 +15,12 @@
|
||||
* for more details.
|
||||
*/
|
||||
|
||||
#include "hmac_prf.h"
|
||||
#include "mac_prf.h"
|
||||
|
||||
typedef struct private_prf_t private_prf_t;
|
||||
|
||||
/**
|
||||
* Private data of a hmac_prf_t object.
|
||||
* Private data of a mac_prf_t object.
|
||||
*/
|
||||
struct private_prf_t {
|
||||
|
||||
@@ -30,15 +30,15 @@ struct private_prf_t {
|
||||
prf_t public;
|
||||
|
||||
/**
|
||||
* HMAC to use
|
||||
* MAC to use
|
||||
*/
|
||||
hmac_t *hmac;
|
||||
mac_t *mac;
|
||||
};
|
||||
|
||||
METHOD(prf_t, get_bytes, void,
|
||||
private_prf_t *this, chunk_t seed, u_int8_t *buffer)
|
||||
{
|
||||
this->hmac->get_mac(this->hmac, seed, buffer);
|
||||
this->mac->get_mac(this->mac, seed, buffer);
|
||||
}
|
||||
|
||||
METHOD(prf_t, allocate_bytes, void,
|
||||
@@ -46,45 +46,45 @@ METHOD(prf_t, allocate_bytes, void,
|
||||
{
|
||||
if (!chunk)
|
||||
{
|
||||
this->hmac->get_mac(this->hmac, seed, NULL);
|
||||
this->mac->get_mac(this->mac, seed, NULL);
|
||||
}
|
||||
else
|
||||
{
|
||||
*chunk = chunk_alloc(this->hmac->get_mac_size(this->hmac));
|
||||
this->hmac->get_mac(this->hmac, seed, chunk->ptr);
|
||||
*chunk = chunk_alloc(this->mac->get_mac_size(this->mac));
|
||||
this->mac->get_mac(this->mac, seed, chunk->ptr);
|
||||
}
|
||||
}
|
||||
|
||||
METHOD(prf_t, get_block_size, size_t,
|
||||
private_prf_t *this)
|
||||
{
|
||||
return this->hmac->get_mac_size(this->hmac);
|
||||
return this->mac->get_mac_size(this->mac);
|
||||
}
|
||||
|
||||
METHOD(prf_t, get_key_size, size_t,
|
||||
private_prf_t *this)
|
||||
{
|
||||
/* for HMAC PRFs, IKEv2 uses MAC size as key size */
|
||||
return this->hmac->get_mac_size(this->hmac);
|
||||
/* IKEv2 uses MAC size as key size */
|
||||
return this->mac->get_mac_size(this->mac);
|
||||
}
|
||||
|
||||
METHOD(prf_t, set_key, void,
|
||||
private_prf_t *this, chunk_t key)
|
||||
{
|
||||
this->hmac->set_key(this->hmac, key);
|
||||
this->mac->set_key(this->mac, key);
|
||||
}
|
||||
|
||||
METHOD(prf_t, destroy, void,
|
||||
private_prf_t *this)
|
||||
{
|
||||
this->hmac->destroy(this->hmac);
|
||||
this->mac->destroy(this->mac);
|
||||
free(this);
|
||||
}
|
||||
|
||||
/*
|
||||
* Described in header.
|
||||
*/
|
||||
prf_t *hmac_prf_create(hmac_t *hmac)
|
||||
prf_t *mac_prf_create(mac_t *mac)
|
||||
{
|
||||
private_prf_t *this;
|
||||
|
||||
@@ -97,7 +97,7 @@ prf_t *hmac_prf_create(hmac_t *hmac)
|
||||
.set_key = _set_key,
|
||||
.destroy = _destroy,
|
||||
},
|
||||
.hmac = hmac,
|
||||
.mac = mac,
|
||||
);
|
||||
|
||||
return &this->public;
|
||||
@@ -14,23 +14,23 @@
|
||||
*/
|
||||
|
||||
/**
|
||||
* @defgroup hmac_prf hmac_prf
|
||||
* @{ @ingroup hmac
|
||||
* @defgroup mac_prf mac_prf
|
||||
* @{ @ingroup crypto
|
||||
*/
|
||||
|
||||
#ifndef HMAC_PRF_H_
|
||||
#define HMAC_PRF_H_
|
||||
#ifndef MAC_PRF_H_
|
||||
#define MAC_PRF_H_
|
||||
|
||||
#include <crypto/mac.h>
|
||||
#include <crypto/prfs/prf.h>
|
||||
#include <crypto/hmacs/hmac.h>
|
||||
|
||||
/**
|
||||
* Creates an implementation of the prf_t interface using the provided hmac_t
|
||||
* Creates an implementation of the prf_t interface using the provided mac_t
|
||||
* implementation. Basically a simple wrapper to map the interface.
|
||||
*
|
||||
* @param hmac hmac_t implementation
|
||||
* @param mac mac_t implementation
|
||||
* @return prf_t object
|
||||
*/
|
||||
prf_t *hmac_prf_create(hmac_t *hmac);
|
||||
prf_t *mac_prf_create(mac_t *mac);
|
||||
|
||||
#endif /** HMAC_PRF_H_ @}*/
|
||||
#endif /** MAC_PRF_H_ @}*/
|
||||
+19
-19
@@ -15,12 +15,12 @@
|
||||
* for more details.
|
||||
*/
|
||||
|
||||
#include "hmac_signer.h"
|
||||
#include "mac_signer.h"
|
||||
|
||||
typedef struct private_signer_t private_signer_t;
|
||||
|
||||
/**
|
||||
* Private data of a hmac_signer_t object.
|
||||
* Private data of a mac_signer_t object.
|
||||
*/
|
||||
struct private_signer_t {
|
||||
|
||||
@@ -30,12 +30,12 @@ struct private_signer_t {
|
||||
signer_t public;
|
||||
|
||||
/**
|
||||
* HMAC to use
|
||||
* MAC to use
|
||||
*/
|
||||
hmac_t *hmac;
|
||||
mac_t *mac;
|
||||
|
||||
/**
|
||||
* Truncation of HMAC output
|
||||
* Truncation of MAC output
|
||||
*/
|
||||
size_t truncation;
|
||||
};
|
||||
@@ -45,13 +45,13 @@ METHOD(signer_t, get_signature, void,
|
||||
{
|
||||
if (buffer == NULL)
|
||||
{
|
||||
this->hmac->get_mac(this->hmac, data, NULL);
|
||||
this->mac->get_mac(this->mac, data, NULL);
|
||||
}
|
||||
else
|
||||
{
|
||||
u_int8_t mac[this->hmac->get_mac_size(this->hmac)];
|
||||
u_int8_t mac[this->mac->get_mac_size(this->mac)];
|
||||
|
||||
this->hmac->get_mac(this->hmac, data, mac);
|
||||
this->mac->get_mac(this->mac, data, mac);
|
||||
memcpy(buffer, mac, this->truncation);
|
||||
}
|
||||
}
|
||||
@@ -61,13 +61,13 @@ METHOD(signer_t, allocate_signature, void,
|
||||
{
|
||||
if (chunk == NULL)
|
||||
{
|
||||
this->hmac->get_mac(this->hmac, data, NULL);
|
||||
this->mac->get_mac(this->mac, data, NULL);
|
||||
}
|
||||
else
|
||||
{
|
||||
u_int8_t mac[this->hmac->get_mac_size(this->hmac)];
|
||||
u_int8_t mac[this->mac->get_mac_size(this->mac)];
|
||||
|
||||
this->hmac->get_mac(this->hmac, data, mac);
|
||||
this->mac->get_mac(this->mac, data, mac);
|
||||
|
||||
*chunk = chunk_alloc(this->truncation);
|
||||
memcpy(chunk->ptr, mac, this->truncation);
|
||||
@@ -77,20 +77,20 @@ METHOD(signer_t, allocate_signature, void,
|
||||
METHOD(signer_t, verify_signature, bool,
|
||||
private_signer_t *this, chunk_t data, chunk_t signature)
|
||||
{
|
||||
u_int8_t mac[this->hmac->get_mac_size(this->hmac)];
|
||||
u_int8_t mac[this->mac->get_mac_size(this->mac)];
|
||||
|
||||
if (signature.len != this->truncation)
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
this->hmac->get_mac(this->hmac, data, mac);
|
||||
this->mac->get_mac(this->mac, data, mac);
|
||||
return memeq(signature.ptr, mac, this->truncation);
|
||||
}
|
||||
|
||||
METHOD(signer_t, get_key_size, size_t,
|
||||
private_signer_t *this)
|
||||
{
|
||||
return this->hmac->get_mac_size(this->hmac);
|
||||
return this->mac->get_mac_size(this->mac);
|
||||
}
|
||||
|
||||
METHOD(signer_t, get_block_size, size_t,
|
||||
@@ -102,20 +102,20 @@ METHOD(signer_t, get_block_size, size_t,
|
||||
METHOD(signer_t, set_key, void,
|
||||
private_signer_t *this, chunk_t key)
|
||||
{
|
||||
this->hmac->set_key(this->hmac, key);
|
||||
this->mac->set_key(this->mac, key);
|
||||
}
|
||||
|
||||
METHOD(signer_t, destroy, void,
|
||||
private_signer_t *this)
|
||||
{
|
||||
this->hmac->destroy(this->hmac);
|
||||
this->mac->destroy(this->mac);
|
||||
free(this);
|
||||
}
|
||||
|
||||
/*
|
||||
* Described in header
|
||||
*/
|
||||
signer_t *hmac_signer_create(hmac_t *hmac, size_t len)
|
||||
signer_t *mac_signer_create(mac_t *mac, size_t len)
|
||||
{
|
||||
private_signer_t *this;
|
||||
|
||||
@@ -129,8 +129,8 @@ signer_t *hmac_signer_create(hmac_t *hmac, size_t len)
|
||||
.set_key = _set_key,
|
||||
.destroy = _destroy,
|
||||
},
|
||||
.truncation = min(len, hmac->get_mac_size(hmac)),
|
||||
.hmac = hmac,
|
||||
.truncation = min(len, mac->get_mac_size(mac)),
|
||||
.mac = mac,
|
||||
);
|
||||
|
||||
return &this->public;
|
||||
+12
-12
@@ -14,28 +14,28 @@
|
||||
*/
|
||||
|
||||
/**
|
||||
* @defgroup hmac_signer hmac_signer
|
||||
* @{ @ingroup hmac
|
||||
* @defgroup mac_signer mac_signer
|
||||
* @{ @ingroup crypto
|
||||
*/
|
||||
|
||||
#ifndef HMAC_SIGNER_H_
|
||||
#define HMAC_SIGNER_H_
|
||||
#ifndef MAC_SIGNER_H_
|
||||
#define MAC_SIGNER_H_
|
||||
|
||||
typedef struct hmac_signer_t hmac_signer_t;
|
||||
typedef struct mac_signer_t mac_signer_t;
|
||||
|
||||
#include <crypto/hmacs/hmac.h>
|
||||
#include <crypto/mac.h>
|
||||
#include <crypto/signers/signer.h>
|
||||
|
||||
/**
|
||||
* Creates an implementation of the signer_t interface using the provided hmac_t
|
||||
* Creates an implementation of the signer_t interface using the provided mac_t
|
||||
* implementation and truncation length.
|
||||
*
|
||||
* @note len will be set to hmac_t.get_mac_size() if it is greater than that.
|
||||
* @note len will be set to mac_t.get_mac_size() if it is greater than that.
|
||||
*
|
||||
* @param hmac hmac_t implementation
|
||||
* @param mac mac_t implementation
|
||||
* @param len length of resulting signature
|
||||
* @return hmac_signer_t
|
||||
* @return mac_signer_t
|
||||
*/
|
||||
signer_t *hmac_signer_create(hmac_t *hmac, size_t len);
|
||||
signer_t *mac_signer_create(mac_t *mac, size_t len);
|
||||
|
||||
#endif /** HMAC_SIGNER_H_ @}*/
|
||||
#endif /** MAC_SIGNER_H_ @}*/
|
||||
Reference in New Issue
Block a user