Migrated hmac plugin to INIT/METHOD macros

This commit is contained in:
Martin Willi
2010-08-13 17:11:53 +02:00
parent af403cafa1
commit 5ab7d9c296
6 changed files with 105 additions and 143 deletions
+24 -32
View File
@@ -30,7 +30,7 @@ struct private_hmac_t {
/** /**
* Public hmac_t interface. * Public hmac_t interface.
*/ */
hmac_t hmac; hmac_t public;
/** /**
* Block size, as in RFC. * Block size, as in RFC.
@@ -53,10 +53,8 @@ struct private_hmac_t {
chunk_t ipaded_key; chunk_t ipaded_key;
}; };
/** METHOD(hmac_t, get_mac, void,
* Implementation of hmac_t.get_mac. private_hmac_t *this, chunk_t data, u_int8_t *out)
*/
static void get_mac(private_hmac_t *this, chunk_t data, u_int8_t *out)
{ {
/* H(K XOR opad, H(K XOR ipad, text)) /* H(K XOR opad, H(K XOR ipad, text))
* *
@@ -91,37 +89,31 @@ static void get_mac(private_hmac_t *this, chunk_t data, u_int8_t *out)
} }
} }
/** METHOD(hmac_t, allocate_mac, void,
* Implementation of hmac_t.allocate_mac. private_hmac_t *this, chunk_t data, chunk_t *out)
*/
static void allocate_mac(private_hmac_t *this, chunk_t data, chunk_t *out)
{ {
/* allocate space and use get_mac */ /* allocate space and use get_mac */
if (out == NULL) if (out == NULL)
{ {
/* append mode */ /* append mode */
this->hmac.get_mac(&(this->hmac), data, NULL); get_mac(this, data, NULL);
} }
else else
{ {
out->len = this->h->get_hash_size(this->h); out->len = this->h->get_hash_size(this->h);
out->ptr = malloc(out->len); out->ptr = malloc(out->len);
this->hmac.get_mac(&(this->hmac), data, out->ptr); get_mac(this, data, out->ptr);
} }
} }
/** METHOD(hmac_t, get_block_size, size_t,
* Implementation of hmac_t.get_block_size. private_hmac_t *this)
*/
static size_t get_block_size(private_hmac_t *this)
{ {
return this->h->get_hash_size(this->h); return this->h->get_hash_size(this->h);
} }
/** METHOD(hmac_t, set_key, void,
* Implementation of hmac_t.set_key. private_hmac_t *this, chunk_t key)
*/
static void set_key(private_hmac_t *this, chunk_t key)
{ {
int i; int i;
u_int8_t buffer[this->b]; u_int8_t buffer[this->b];
@@ -151,10 +143,8 @@ static void set_key(private_hmac_t *this, chunk_t key)
this->h->get_hash(this->h, this->ipaded_key, NULL); this->h->get_hash(this->h, this->ipaded_key, NULL);
} }
/** METHOD(hmac_t, destroy, void,
* Implementation of hmac_t.destroy. private_hmac_t *this)
*/
static void destroy(private_hmac_t *this)
{ {
this->h->destroy(this->h); this->h->destroy(this->h);
free(this->opaded_key.ptr); free(this->opaded_key.ptr);
@@ -167,14 +157,17 @@ static void destroy(private_hmac_t *this)
*/ */
hmac_t *hmac_create(hash_algorithm_t hash_algorithm) hmac_t *hmac_create(hash_algorithm_t hash_algorithm)
{ {
private_hmac_t *this = malloc_thing(private_hmac_t); private_hmac_t *this;
/* set hmac_t methods */ INIT(this,
this->hmac.get_mac = (void (*)(hmac_t *,chunk_t,u_int8_t*))get_mac; .public = {
this->hmac.allocate_mac = (void (*)(hmac_t *,chunk_t,chunk_t*))allocate_mac; .get_mac = _get_mac,
this->hmac.get_block_size = (size_t (*)(hmac_t *))get_block_size; .allocate_mac = _allocate_mac,
this->hmac.set_key = (void (*)(hmac_t *,chunk_t))set_key; .get_block_size = _get_block_size,
this->hmac.destroy = (void (*)(hmac_t *))destroy; .set_key = _set_key,
.destroy = _destroy,
},
);
/* set b, according to hasher */ /* set b, according to hasher */
switch (hash_algorithm) switch (hash_algorithm)
@@ -193,7 +186,6 @@ hmac_t *hmac_create(hash_algorithm_t hash_algorithm)
return NULL; return NULL;
} }
/* build the hasher */
this->h = lib->crypto->create_hasher(lib->crypto, hash_algorithm); this->h = lib->crypto->create_hasher(lib->crypto, hash_algorithm);
if (this->h == NULL) if (this->h == NULL)
{ {
@@ -208,5 +200,5 @@ hmac_t *hmac_create(hash_algorithm_t hash_algorithm)
this->ipaded_key.ptr = malloc(this->b); this->ipaded_key.ptr = malloc(this->b);
this->ipaded_key.len = this->b; this->ipaded_key.len = this->b;
return &(this->hmac); return &this->public;
} }
+6 -6
View File
@@ -32,10 +32,8 @@ struct private_hmac_plugin_t {
hmac_plugin_t public; hmac_plugin_t public;
}; };
/** METHOD(plugin_t, destroy, void,
* Implementation of hmac_plugin_t.hmactroy private_hmac_plugin_t *this)
*/
static void destroy(private_hmac_plugin_t *this)
{ {
lib->crypto->remove_prf(lib->crypto, lib->crypto->remove_prf(lib->crypto,
(prf_constructor_t)hmac_prf_create); (prf_constructor_t)hmac_prf_create);
@@ -49,9 +47,11 @@ static void destroy(private_hmac_plugin_t *this)
*/ */
plugin_t *hmac_plugin_create() plugin_t *hmac_plugin_create()
{ {
private_hmac_plugin_t *this = malloc_thing(private_hmac_plugin_t); private_hmac_plugin_t *this;
this->public.plugin.destroy = (void(*)(plugin_t*))destroy; INIT(this,
.public.plugin.destroy = _destroy,
);
lib->crypto->add_prf(lib->crypto, PRF_HMAC_SHA2_256, lib->crypto->add_prf(lib->crypto, PRF_HMAC_SHA2_256,
(prf_constructor_t)hmac_prf_create); (prf_constructor_t)hmac_prf_create);
+31 -42
View File
@@ -36,51 +36,39 @@ struct private_hmac_prf_t {
hmac_t *hmac; hmac_t *hmac;
}; };
/** METHOD(prf_t, get_bytes, void,
* Implementation of prf_t.get_bytes. private_hmac_prf_t *this, chunk_t seed, u_int8_t *buffer)
*/
static void get_bytes(private_hmac_prf_t *this, chunk_t seed, u_int8_t *buffer)
{ {
this->hmac->get_mac(this->hmac, seed, buffer); this->hmac->get_mac(this->hmac, seed, buffer);
} }
/** METHOD(prf_t, allocate_bytes, void,
* Implementation of prf_t.allocate_bytes. private_hmac_prf_t *this, chunk_t seed, chunk_t *chunk)
*/
static void allocate_bytes(private_hmac_prf_t *this, chunk_t seed, chunk_t *chunk)
{ {
this->hmac->allocate_mac(this->hmac, seed, chunk); this->hmac->allocate_mac(this->hmac, seed, chunk);
} }
/** METHOD(prf_t, get_block_size, size_t,
* Implementation of prf_t.get_block_size. private_hmac_prf_t *this)
*/
static size_t get_block_size(private_hmac_prf_t *this)
{ {
return this->hmac->get_block_size(this->hmac); return this->hmac->get_block_size(this->hmac);
} }
/** METHOD(prf_t, get_key_size, size_t,
* Implementation of prf_t.get_block_size. private_hmac_prf_t *this)
*/
static size_t get_key_size(private_hmac_prf_t *this)
{ {
/* for HMAC prfs, IKEv2 uses block size as key size */ /* for HMAC prfs, IKEv2 uses block size as key size */
return this->hmac->get_block_size(this->hmac); return this->hmac->get_block_size(this->hmac);
} }
/** METHOD(prf_t, set_key, void,
* Implementation of prf_t.set_key. private_hmac_prf_t *this, chunk_t key)
*/
static void set_key(private_hmac_prf_t *this, chunk_t key)
{ {
this->hmac->set_key(this->hmac, key); this->hmac->set_key(this->hmac, key);
} }
/** METHOD(prf_t, destroy, void,
* Implementation of prf_t.destroy. private_hmac_prf_t *this)
*/
static void destroy(private_hmac_prf_t *this)
{ {
this->hmac->destroy(this->hmac); this->hmac->destroy(this->hmac);
free(this); free(this);
@@ -92,44 +80,45 @@ static void destroy(private_hmac_prf_t *this)
hmac_prf_t *hmac_prf_create(pseudo_random_function_t algo) hmac_prf_t *hmac_prf_create(pseudo_random_function_t algo)
{ {
private_hmac_prf_t *this; private_hmac_prf_t *this;
hash_algorithm_t hash; hmac_t *hmac;
switch (algo) switch (algo)
{ {
case PRF_HMAC_SHA1: case PRF_HMAC_SHA1:
hash = HASH_SHA1; hmac = hmac_create(HASH_SHA1);
break; break;
case PRF_HMAC_MD5: case PRF_HMAC_MD5:
hash = HASH_MD5; hmac = hmac_create(HASH_MD5);
break; break;
case PRF_HMAC_SHA2_256: case PRF_HMAC_SHA2_256:
hash = HASH_SHA256; hmac = hmac_create(HASH_SHA256);
break; break;
case PRF_HMAC_SHA2_384: case PRF_HMAC_SHA2_384:
hash = HASH_SHA384; hmac = hmac_create(HASH_SHA384);
break; break;
case PRF_HMAC_SHA2_512: case PRF_HMAC_SHA2_512:
hash = HASH_SHA512; hmac = hmac_create(HASH_SHA512);
break; break;
default: default:
return NULL; return NULL;
} }
if (hmac == NULL)
this = malloc_thing(private_hmac_prf_t);
this->hmac = hmac_create(hash);
if (this->hmac == NULL)
{ {
free(this);
return NULL; return NULL;
} }
this->public.prf_interface.get_bytes = (void (*) (prf_t *,chunk_t,u_int8_t*))get_bytes; INIT(this,
this->public.prf_interface.allocate_bytes = (void (*) (prf_t*,chunk_t,chunk_t*))allocate_bytes; .public.prf = {
this->public.prf_interface.get_block_size = (size_t (*) (prf_t*))get_block_size; .get_bytes = _get_bytes,
this->public.prf_interface.get_key_size = (size_t (*) (prf_t*))get_key_size; .allocate_bytes = _allocate_bytes,
this->public.prf_interface.set_key = (void (*) (prf_t *,chunk_t))set_key; .get_block_size = _get_block_size,
this->public.prf_interface.destroy = (void (*) (prf_t *))destroy; .get_key_size = _get_key_size,
.set_key = _set_key,
.destroy = _destroy,
},
.hmac = hmac,
);
return &(this->public); return &this->public;
} }
+2 -2
View File
@@ -35,9 +35,9 @@ typedef struct hmac_prf_t hmac_prf_t;
struct hmac_prf_t { struct hmac_prf_t {
/** /**
* Generic prf_t interface for this hmac_prf_t class. * Implements prf_t interface.
*/ */
prf_t prf_interface; prf_t prf;
}; };
/** /**
+39 -57
View File
@@ -41,11 +41,8 @@ struct private_hmac_signer_t {
size_t block_size; size_t block_size;
}; };
/** METHOD(signer_t, get_signature, void,
* Implementation of signer_t.get_signature. private_hmac_signer_t *this, chunk_t data, u_int8_t *buffer)
*/
static void get_signature(private_hmac_signer_t *this,
chunk_t data, u_int8_t *buffer)
{ {
if (buffer == NULL) if (buffer == NULL)
{ /* append mode */ { /* append mode */
@@ -60,11 +57,8 @@ static void get_signature(private_hmac_signer_t *this,
} }
} }
/** METHOD(signer_t, allocate_signature, void,
* Implementation of signer_t.allocate_signature. private_hmac_signer_t *this, chunk_t data, chunk_t *chunk)
*/
static void allocate_signature (private_hmac_signer_t *this,
chunk_t data, chunk_t *chunk)
{ {
if (chunk == NULL) if (chunk == NULL)
{ /* append mode */ { /* append mode */
@@ -83,11 +77,8 @@ static void allocate_signature (private_hmac_signer_t *this,
} }
} }
/** METHOD(signer_t, verify_signature, bool,
* Implementation of signer_t.verify_signature. private_hmac_signer_t *this, chunk_t data, chunk_t signature)
*/
static bool verify_signature(private_hmac_signer_t *this,
chunk_t data, chunk_t signature)
{ {
u_int8_t mac[this->hmac->get_block_size(this->hmac)]; u_int8_t mac[this->hmac->get_block_size(this->hmac)];
@@ -100,38 +91,29 @@ static bool verify_signature(private_hmac_signer_t *this,
return memeq(signature.ptr, mac, this->block_size); return memeq(signature.ptr, mac, this->block_size);
} }
/** METHOD(signer_t, get_key_size, size_t,
* Implementation of signer_t.get_key_size. private_hmac_signer_t *this)
*/
static size_t get_key_size(private_hmac_signer_t *this)
{ {
return this->hmac->get_block_size(this->hmac); return this->hmac->get_block_size(this->hmac);
} }
/** METHOD(signer_t, get_block_size, size_t,
* Implementation of signer_t.get_block_size. private_hmac_signer_t *this)
*/
static size_t get_block_size(private_hmac_signer_t *this)
{ {
return this->block_size; return this->block_size;
} }
/** METHOD(signer_t, set_key, void,
* Implementation of signer_t.set_key. private_hmac_signer_t *this, chunk_t key)
*/
static void set_key(private_hmac_signer_t *this, chunk_t key)
{ {
this->hmac->set_key(this->hmac, key); this->hmac->set_key(this->hmac, key);
} }
/** METHOD(signer_t, destroy, void,
* Implementation of signer_t.destroy. private_hmac_signer_t *this)
*/
static status_t destroy(private_hmac_signer_t *this)
{ {
this->hmac->destroy(this->hmac); this->hmac->destroy(this->hmac);
free(this); free(this);
return SUCCESS;
} }
/* /*
@@ -140,69 +122,69 @@ static status_t destroy(private_hmac_signer_t *this)
hmac_signer_t *hmac_signer_create(integrity_algorithm_t algo) hmac_signer_t *hmac_signer_create(integrity_algorithm_t algo)
{ {
private_hmac_signer_t *this; private_hmac_signer_t *this;
hmac_t *hmac;
size_t trunc; size_t trunc;
hash_algorithm_t hash;
switch (algo) switch (algo)
{ {
case AUTH_HMAC_SHA1_96: case AUTH_HMAC_SHA1_96:
hash = HASH_SHA1; hmac = hmac_create(HASH_SHA1);
trunc = 12; trunc = 12;
break; break;
case AUTH_HMAC_SHA1_128: case AUTH_HMAC_SHA1_128:
hash = HASH_SHA1; hmac = hmac_create(HASH_SHA1);
trunc = 16; trunc = 16;
break; break;
case AUTH_HMAC_SHA1_160: case AUTH_HMAC_SHA1_160:
hash = HASH_SHA1; hmac = hmac_create(HASH_SHA1);
trunc = 20; trunc = 20;
break; break;
case AUTH_HMAC_MD5_96: case AUTH_HMAC_MD5_96:
hash = HASH_MD5; hmac = hmac_create(HASH_MD5);
trunc = 12; trunc = 12;
break; break;
case AUTH_HMAC_MD5_128: case AUTH_HMAC_MD5_128:
hash = HASH_MD5; hmac = hmac_create(HASH_MD5);
trunc = 16; trunc = 16;
break; break;
case AUTH_HMAC_SHA2_256_128: case AUTH_HMAC_SHA2_256_128:
hash = HASH_SHA256; hmac = hmac_create(HASH_SHA256);
trunc = 16; trunc = 16;
break; break;
case AUTH_HMAC_SHA2_384_192: case AUTH_HMAC_SHA2_384_192:
hash = HASH_SHA384; hmac = hmac_create(HASH_SHA384);
trunc = 24; trunc = 24;
break; break;
case AUTH_HMAC_SHA2_512_256: case AUTH_HMAC_SHA2_512_256:
hash = HASH_SHA512; hmac = hmac_create(HASH_SHA512);
trunc = 32; trunc = 32;
break; break;
case AUTH_HMAC_SHA2_256_256: case AUTH_HMAC_SHA2_256_256:
hash = HASH_SHA256; hmac = hmac_create(HASH_SHA256);
trunc = 32; trunc = 32;
default: default:
return NULL; return NULL;
} }
this = malloc_thing(private_hmac_signer_t); if (hmac == NULL)
this->hmac = hmac_create(hash);
if (this->hmac == NULL)
{ {
free(this);
return NULL; return NULL;
} }
/* prevent invalid truncation */
this->block_size = min(trunc, this->hmac->get_block_size(this->hmac));
/* interface functions */ INIT(this,
this->public.signer_interface.get_signature = (void (*) (signer_t*, chunk_t, u_int8_t*))get_signature; .public.signer = {
this->public.signer_interface.allocate_signature = (void (*) (signer_t*, chunk_t, chunk_t*))allocate_signature; .get_signature = _get_signature,
this->public.signer_interface.verify_signature = (bool (*) (signer_t*, chunk_t, chunk_t))verify_signature; .allocate_signature = _allocate_signature,
this->public.signer_interface.get_key_size = (size_t (*) (signer_t*))get_key_size; .verify_signature = _verify_signature,
this->public.signer_interface.get_block_size = (size_t (*) (signer_t*))get_block_size; .get_key_size = _get_key_size,
this->public.signer_interface.set_key = (void (*) (signer_t*,chunk_t))set_key; .get_block_size = _get_block_size,
this->public.signer_interface.destroy = (void (*) (signer_t*))destroy; .set_key = _set_key,
.destroy = _destroy,
},
.block_size = min(trunc, hmac->get_block_size(hmac)),
.hmac = hmac,
);
return &(this->public); return &this->public;
} }
+3 -4
View File
@@ -34,9 +34,9 @@ typedef struct hmac_signer_t hmac_signer_t;
struct hmac_signer_t { struct hmac_signer_t {
/** /**
* generic signer_t interface for this signer * Implements signer_t interface.
*/ */
signer_t signer_interface; signer_t signer;
}; };
/** /**
@@ -44,8 +44,7 @@ struct hmac_signer_t {
* *
* HMAC signatures are often truncated to shorten them to a more usable, but * HMAC signatures are often truncated to shorten them to a more usable, but
* still secure enough length. * still secure enough length.
* Block size must be equal or smaller then the hash algorithms * Block size must be equal or smaller then the hash algorithms hash.
* hash.
* *
* @param algo algorithm to implement * @param algo algorithm to implement
* @return hmac_signer_t, NULL if not supported * @return hmac_signer_t, NULL if not supported