Make the hmac_t interface a generic interface for message authentication codes
This commit is contained in:
@@ -10,6 +10,6 @@ plugin_LTLIBRARIES = libstrongswan-hmac.la
|
||||
endif
|
||||
|
||||
libstrongswan_hmac_la_SOURCES = \
|
||||
hmac_plugin.h hmac_plugin.c hmac_hmac.h hmac_hmac.c
|
||||
hmac_plugin.h hmac_plugin.c hmac.h hmac.c
|
||||
|
||||
libstrongswan_hmac_la_LDFLAGS = -module -avoid-version
|
||||
|
||||
+26
-26
@@ -15,25 +15,25 @@
|
||||
* for more details.
|
||||
*/
|
||||
|
||||
#include "hmac_hmac.h"
|
||||
#include "hmac.h"
|
||||
|
||||
#include <crypto/hmacs/hmac.h>
|
||||
#include <crypto/hmacs/hmac_prf.h>
|
||||
#include <crypto/hmacs/hmac_signer.h>
|
||||
#include <crypto/mac.h>
|
||||
#include <crypto/prfs/mac_prf.h>
|
||||
#include <crypto/signers/mac_signer.h>
|
||||
|
||||
typedef struct private_hmac_t private_hmac_t;
|
||||
typedef struct private_mac_t private_mac_t;
|
||||
|
||||
/**
|
||||
* Private data of a hmac_hmac_t object.
|
||||
* Private data of a mac_t object.
|
||||
*
|
||||
* The variable names are the same as in the RFC.
|
||||
*/
|
||||
struct private_hmac_t {
|
||||
struct private_mac_t {
|
||||
|
||||
/**
|
||||
* Implements hmac_t interface
|
||||
* Implements mac_t interface
|
||||
*/
|
||||
hmac_t public;
|
||||
mac_t public;
|
||||
|
||||
/**
|
||||
* Block size, as in RFC.
|
||||
@@ -56,8 +56,8 @@ struct private_hmac_t {
|
||||
chunk_t ipaded_key;
|
||||
};
|
||||
|
||||
METHOD(hmac_t, get_mac, void,
|
||||
private_hmac_t *this, chunk_t data, u_int8_t *out)
|
||||
METHOD(mac_t, get_mac, void,
|
||||
private_mac_t *this, chunk_t data, u_int8_t *out)
|
||||
{
|
||||
/* H(K XOR opad, H(K XOR ipad, text))
|
||||
*
|
||||
@@ -92,14 +92,14 @@ METHOD(hmac_t, get_mac, void,
|
||||
}
|
||||
}
|
||||
|
||||
METHOD(hmac_t, get_mac_size, size_t,
|
||||
private_hmac_t *this)
|
||||
METHOD(mac_t, get_mac_size, size_t,
|
||||
private_mac_t *this)
|
||||
{
|
||||
return this->h->get_hash_size(this->h);
|
||||
}
|
||||
|
||||
METHOD(hmac_t, set_key, void,
|
||||
private_hmac_t *this, chunk_t key)
|
||||
METHOD(mac_t, set_key, void,
|
||||
private_mac_t *this, chunk_t key)
|
||||
{
|
||||
int i;
|
||||
u_int8_t buffer[this->b];
|
||||
@@ -129,8 +129,8 @@ METHOD(hmac_t, set_key, void,
|
||||
this->h->get_hash(this->h, this->ipaded_key, NULL);
|
||||
}
|
||||
|
||||
METHOD(hmac_t, destroy, void,
|
||||
private_hmac_t *this)
|
||||
METHOD(mac_t, destroy, void,
|
||||
private_mac_t *this)
|
||||
{
|
||||
this->h->destroy(this->h);
|
||||
chunk_clear(&this->opaded_key);
|
||||
@@ -139,11 +139,11 @@ METHOD(hmac_t, destroy, void,
|
||||
}
|
||||
|
||||
/*
|
||||
* Creates an hmac_t object
|
||||
* Creates an mac_t object
|
||||
*/
|
||||
static hmac_t *hmac_create(hash_algorithm_t hash_algorithm)
|
||||
static mac_t *hmac_create(hash_algorithm_t hash_algorithm)
|
||||
{
|
||||
private_hmac_t *this;
|
||||
private_mac_t *this;
|
||||
|
||||
INIT(this,
|
||||
.public = {
|
||||
@@ -191,14 +191,14 @@ static hmac_t *hmac_create(hash_algorithm_t hash_algorithm)
|
||||
/*
|
||||
* Described in header
|
||||
*/
|
||||
prf_t *hmac_hmac_prf_create(pseudo_random_function_t algo)
|
||||
prf_t *hmac_prf_create(pseudo_random_function_t algo)
|
||||
{
|
||||
hmac_t *hmac;
|
||||
mac_t *hmac;
|
||||
|
||||
hmac = hmac_create(hasher_algorithm_from_prf(algo));
|
||||
if (hmac)
|
||||
{
|
||||
return hmac_prf_create(hmac);
|
||||
return mac_prf_create(hmac);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
@@ -206,15 +206,15 @@ prf_t *hmac_hmac_prf_create(pseudo_random_function_t algo)
|
||||
/*
|
||||
* Described in header
|
||||
*/
|
||||
signer_t *hmac_hmac_signer_create(integrity_algorithm_t algo)
|
||||
signer_t *hmac_signer_create(integrity_algorithm_t algo)
|
||||
{
|
||||
hmac_t *hmac;
|
||||
mac_t *hmac;
|
||||
size_t trunc;
|
||||
|
||||
hmac = hmac_create(hasher_algorithm_from_integrity(algo, &trunc));
|
||||
if (hmac)
|
||||
{
|
||||
return hmac_signer_create(hmac, trunc);
|
||||
return mac_signer_create(hmac, trunc);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
@@ -18,12 +18,12 @@
|
||||
*
|
||||
* It uses a hash function, which must be implemented as a hasher_t class.
|
||||
*
|
||||
* @defgroup hmac_hmac hmac
|
||||
* @defgroup hmac_mac mac
|
||||
* @{ @ingroup hmac_p
|
||||
*/
|
||||
|
||||
#ifndef HMAC_HMAC_H_
|
||||
#define HMAC_HMAC_H_
|
||||
#ifndef HMAC_H_
|
||||
#define HMAC_H_
|
||||
|
||||
#include <crypto/prfs/prf.h>
|
||||
#include <crypto/signers/signer.h>
|
||||
@@ -34,7 +34,7 @@
|
||||
* @param algo algorithm to implement
|
||||
* @return prf_t object, NULL if not supported
|
||||
*/
|
||||
prf_t *hmac_hmac_prf_create(pseudo_random_function_t algo);
|
||||
prf_t *hmac_prf_create(pseudo_random_function_t algo);
|
||||
|
||||
/**
|
||||
* Creates a new signer_t object based on an HMAC.
|
||||
@@ -42,6 +42,6 @@ prf_t *hmac_hmac_prf_create(pseudo_random_function_t algo);
|
||||
* @param algo algorithm to implement
|
||||
* @return signer_t, NULL if not supported
|
||||
*/
|
||||
signer_t *hmac_hmac_signer_create(integrity_algorithm_t algo);
|
||||
signer_t *hmac_signer_create(integrity_algorithm_t algo);
|
||||
|
||||
#endif /** HMAC_HMAC_H_ @}*/
|
||||
#endif /** HMAC_H_ @}*/
|
||||
@@ -16,7 +16,7 @@
|
||||
#include "hmac_plugin.h"
|
||||
|
||||
#include <library.h>
|
||||
#include "hmac_hmac.h"
|
||||
#include "hmac.h"
|
||||
|
||||
typedef struct private_hmac_plugin_t private_hmac_plugin_t;
|
||||
|
||||
@@ -41,7 +41,7 @@ METHOD(plugin_t, get_features, int,
|
||||
private_hmac_plugin_t *this, plugin_feature_t *features[])
|
||||
{
|
||||
static plugin_feature_t f[] = {
|
||||
PLUGIN_REGISTER(PRF, hmac_hmac_prf_create),
|
||||
PLUGIN_REGISTER(PRF, hmac_prf_create),
|
||||
PLUGIN_PROVIDE(PRF, PRF_HMAC_SHA1),
|
||||
PLUGIN_DEPENDS(HASHER, HASH_SHA1),
|
||||
PLUGIN_PROVIDE(PRF, PRF_HMAC_MD5),
|
||||
@@ -52,7 +52,7 @@ METHOD(plugin_t, get_features, int,
|
||||
PLUGIN_DEPENDS(HASHER, HASH_SHA384),
|
||||
PLUGIN_PROVIDE(PRF, PRF_HMAC_SHA2_512),
|
||||
PLUGIN_DEPENDS(HASHER, HASH_SHA512),
|
||||
PLUGIN_REGISTER(SIGNER, hmac_hmac_signer_create),
|
||||
PLUGIN_REGISTER(SIGNER, hmac_signer_create),
|
||||
PLUGIN_PROVIDE(SIGNER, AUTH_HMAC_SHA1_96),
|
||||
PLUGIN_DEPENDS(HASHER, HASH_SHA1),
|
||||
PLUGIN_PROVIDE(SIGNER, AUTH_HMAC_SHA1_128),
|
||||
|
||||
@@ -40,21 +40,21 @@
|
||||
|
||||
#include "openssl_hmac.h"
|
||||
|
||||
#include <crypto/hmacs/hmac.h>
|
||||
#include <crypto/hmacs/hmac_prf.h>
|
||||
#include <crypto/hmacs/hmac_signer.h>
|
||||
#include <crypto/mac.h>
|
||||
#include <crypto/prfs/mac_prf.h>
|
||||
#include <crypto/signers/mac_signer.h>
|
||||
|
||||
typedef struct private_hmac_t private_hmac_t;
|
||||
typedef struct private_mac_t private_mac_t;
|
||||
|
||||
/**
|
||||
* Private data of a hmac_t object.
|
||||
* Private data of a mac_t object.
|
||||
*/
|
||||
struct private_hmac_t {
|
||||
struct private_mac_t {
|
||||
|
||||
/**
|
||||
* Public interface
|
||||
*/
|
||||
hmac_t public;
|
||||
mac_t public;
|
||||
|
||||
/**
|
||||
* Hasher to use
|
||||
@@ -75,13 +75,13 @@ struct private_hmac_t {
|
||||
/**
|
||||
* Resets HMAC context
|
||||
*/
|
||||
static void reset(private_hmac_t *this)
|
||||
static void reset(private_mac_t *this)
|
||||
{
|
||||
HMAC_Init_ex(&this->hmac, this->key.ptr, this->key.len, this->hasher, NULL);
|
||||
}
|
||||
|
||||
METHOD(hmac_t, get_mac, void,
|
||||
private_hmac_t *this, chunk_t data, u_int8_t *out)
|
||||
METHOD(mac_t, get_mac, void,
|
||||
private_mac_t *this, chunk_t data, u_int8_t *out)
|
||||
{
|
||||
if (out == NULL)
|
||||
{
|
||||
@@ -95,22 +95,22 @@ METHOD(hmac_t, get_mac, void,
|
||||
}
|
||||
}
|
||||
|
||||
METHOD(hmac_t, get_mac_size, size_t,
|
||||
private_hmac_t *this)
|
||||
METHOD(mac_t, get_mac_size, size_t,
|
||||
private_mac_t *this)
|
||||
{
|
||||
return EVP_MD_size(this->hasher);
|
||||
}
|
||||
|
||||
METHOD(hmac_t, set_key, void,
|
||||
private_hmac_t *this, chunk_t key)
|
||||
METHOD(mac_t, set_key, void,
|
||||
private_mac_t *this, chunk_t key)
|
||||
{
|
||||
chunk_clear(&this->key);
|
||||
this->key = chunk_clone(key);
|
||||
reset(this);
|
||||
}
|
||||
|
||||
METHOD(hmac_t, destroy, void,
|
||||
private_hmac_t *this)
|
||||
METHOD(mac_t, destroy, void,
|
||||
private_mac_t *this)
|
||||
{
|
||||
HMAC_CTX_cleanup(&this->hmac);
|
||||
chunk_clear(&this->key);
|
||||
@@ -118,11 +118,11 @@ METHOD(hmac_t, destroy, void,
|
||||
}
|
||||
|
||||
/*
|
||||
* Create an OpenSSL-backed implementation of the hmac_t interface
|
||||
* Create an OpenSSL-backed implementation of the mac_t interface
|
||||
*/
|
||||
static hmac_t *hmac_create(hash_algorithm_t algo)
|
||||
static mac_t *hmac_create(hash_algorithm_t algo)
|
||||
{
|
||||
private_hmac_t *this;
|
||||
private_mac_t *this;
|
||||
|
||||
INIT(this,
|
||||
.public = {
|
||||
@@ -170,12 +170,12 @@ static hmac_t *hmac_create(hash_algorithm_t algo)
|
||||
*/
|
||||
prf_t *openssl_hmac_prf_create(pseudo_random_function_t algo)
|
||||
{
|
||||
hmac_t *hmac;
|
||||
mac_t *hmac;
|
||||
|
||||
hmac = hmac_create(hasher_algorithm_from_prf(algo));
|
||||
if (hmac)
|
||||
{
|
||||
return hmac_prf_create(hmac);
|
||||
return mac_prf_create(hmac);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
@@ -185,13 +185,13 @@ prf_t *openssl_hmac_prf_create(pseudo_random_function_t algo)
|
||||
*/
|
||||
signer_t *openssl_hmac_signer_create(integrity_algorithm_t algo)
|
||||
{
|
||||
hmac_t *hmac;
|
||||
mac_t *hmac;
|
||||
size_t trunc;
|
||||
|
||||
hmac = hmac_create(hasher_algorithm_from_integrity(algo, &trunc));
|
||||
if (hmac)
|
||||
{
|
||||
return hmac_signer_create(hmac, trunc);
|
||||
return mac_signer_create(hmac, trunc);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user