- removed key from constructor
- added set_key method
This commit is contained in:
@@ -169,7 +169,8 @@ void test_hmac_sha1(tester_t *tester)
|
|||||||
|
|
||||||
for (i=0; i<3; i++)
|
for (i=0; i<3; i++)
|
||||||
{
|
{
|
||||||
hmac_t *hmac = hmac_create(HASH_SHA1, keys[i]);
|
hmac_t *hmac = hmac_create(HASH_SHA1);
|
||||||
|
hmac->set_key(hmac, keys[i]);
|
||||||
hmac->allocate_mac(hmac, data[i], &digest[i]);
|
hmac->allocate_mac(hmac, data[i], &digest[i]);
|
||||||
hmac->destroy(hmac);
|
hmac->destroy(hmac);
|
||||||
|
|
||||||
|
|||||||
@@ -109,6 +109,37 @@ static size_t get_block_size(private_hmac_t *this)
|
|||||||
return this->h->get_block_size(this->h);
|
return this->h->get_block_size(this->h);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* implementation of hmac_t.set_key
|
||||||
|
*/
|
||||||
|
static status_t set_key(private_hmac_t *this, chunk_t key)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
u_int8_t buffer[this->b];
|
||||||
|
|
||||||
|
memset(buffer, 0, this->b);
|
||||||
|
|
||||||
|
if (key.len > this->b)
|
||||||
|
{
|
||||||
|
/* if key is too long, it will be hashed */
|
||||||
|
this->h->get_hash(this->h, key, buffer);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* if not, just copy it in our pre-padded k */
|
||||||
|
memcpy(buffer, key.ptr, key.len);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* apply ipad and opad to key */
|
||||||
|
for (i = 0; i < this->b; i++)
|
||||||
|
{
|
||||||
|
this->ipaded_key.ptr[i] = buffer[i] ^ 0x36;
|
||||||
|
this->opaded_key.ptr[i] = buffer[i] ^ 0x5C;
|
||||||
|
}
|
||||||
|
|
||||||
|
return SUCCESS;;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* implementation of hmac_t.destroy
|
* implementation of hmac_t.destroy
|
||||||
*/
|
*/
|
||||||
@@ -125,10 +156,9 @@ static status_t destroy(private_hmac_t *this)
|
|||||||
/*
|
/*
|
||||||
* Described in header
|
* Described in header
|
||||||
*/
|
*/
|
||||||
hmac_t *hmac_create(hash_algorithm_t hash_algorithm, chunk_t key)
|
hmac_t *hmac_create(hash_algorithm_t hash_algorithm)
|
||||||
{
|
{
|
||||||
private_hmac_t *this;
|
private_hmac_t *this;
|
||||||
u_int8_t i;
|
|
||||||
|
|
||||||
this = allocator_alloc_thing(private_hmac_t);
|
this = allocator_alloc_thing(private_hmac_t);
|
||||||
if (this == NULL)
|
if (this == NULL)
|
||||||
@@ -139,6 +169,7 @@ hmac_t *hmac_create(hash_algorithm_t hash_algorithm, chunk_t key)
|
|||||||
this->public.get_mac = (size_t (*)(hmac_t *,chunk_t,u_int8_t*))get_mac;
|
this->public.get_mac = (size_t (*)(hmac_t *,chunk_t,u_int8_t*))get_mac;
|
||||||
this->public.allocate_mac = (size_t (*)(hmac_t *,chunk_t,chunk_t*))allocate_mac;
|
this->public.allocate_mac = (size_t (*)(hmac_t *,chunk_t,chunk_t*))allocate_mac;
|
||||||
this->public.get_block_size = (size_t (*)(hmac_t *))get_block_size;
|
this->public.get_block_size = (size_t (*)(hmac_t *))get_block_size;
|
||||||
|
this->public.set_key = (status_t (*)(hmac_t *,chunk_t))set_key;
|
||||||
this->public.destroy = (status_t (*)(hmac_t *))destroy;
|
this->public.destroy = (status_t (*)(hmac_t *))destroy;
|
||||||
|
|
||||||
/* set b, according to hasher */
|
/* set b, according to hasher */
|
||||||
@@ -160,26 +191,7 @@ hmac_t *hmac_create(hash_algorithm_t hash_algorithm, chunk_t key)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* k must be b long, padded with 0x00 */
|
|
||||||
this->k.ptr = allocator_alloc(this->b);
|
|
||||||
this->k.len = this->b;
|
|
||||||
if (this->k.ptr == NULL)
|
|
||||||
{
|
|
||||||
this->h->destroy(this->h);
|
|
||||||
allocator_free(this);
|
|
||||||
}
|
|
||||||
memset(this->k.ptr, 0, this->k.len);
|
|
||||||
|
|
||||||
if (key.len > this->h->get_block_size(this->h))
|
|
||||||
{
|
|
||||||
/* if key is too long, it will be hashed */
|
|
||||||
this->h->get_hash(this->h, key, this->k.ptr);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
/* if not, just copy it in our pre-padded k */
|
|
||||||
memcpy(this->k.ptr, key.ptr, key.len);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* build ipad and opad */
|
/* build ipad and opad */
|
||||||
this->opaded_key.ptr = allocator_alloc(this->b);
|
this->opaded_key.ptr = allocator_alloc(this->b);
|
||||||
@@ -196,17 +208,11 @@ hmac_t *hmac_create(hash_algorithm_t hash_algorithm, chunk_t key)
|
|||||||
if (this->ipaded_key.ptr == NULL)
|
if (this->ipaded_key.ptr == NULL)
|
||||||
{
|
{
|
||||||
this->h->destroy(this->h);
|
this->h->destroy(this->h);
|
||||||
allocator_free(this->k.ptr);
|
|
||||||
allocator_free(this->opaded_key.ptr);
|
allocator_free(this->opaded_key.ptr);
|
||||||
allocator_free(this);
|
allocator_free(this);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i = 0; i < this->b; i++)
|
|
||||||
{
|
|
||||||
this->ipaded_key.ptr[i] = this->k.ptr[i] ^ 0x36;
|
|
||||||
this->opaded_key.ptr[i] = this->k.ptr[i] ^ 0x5C;
|
|
||||||
}
|
|
||||||
|
|
||||||
return &(this->public);
|
return &(this->public);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -66,6 +66,17 @@ struct hmac_s {
|
|||||||
*/
|
*/
|
||||||
size_t (*get_block_size) (hmac_t *this);
|
size_t (*get_block_size) (hmac_t *this);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief set the key for this hmac
|
||||||
|
*
|
||||||
|
* Any key length is accepted.
|
||||||
|
*
|
||||||
|
* @param this calling hmac
|
||||||
|
* @param key key to set
|
||||||
|
* @return block size in bytes
|
||||||
|
*/
|
||||||
|
size_t (*set_key) (hmac_t *this, chunk_t key);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Destroys a hmac object.
|
* @brief Destroys a hmac object.
|
||||||
*
|
*
|
||||||
@@ -80,11 +91,10 @@ struct hmac_s {
|
|||||||
* Creates a new hmac_t object
|
* Creates a new hmac_t object
|
||||||
*
|
*
|
||||||
* @param hash_algorithm hash algorithm to use
|
* @param hash_algorithm hash algorithm to use
|
||||||
* @param key A chunk containing the key
|
|
||||||
* @return
|
* @return
|
||||||
* - hmac_t if successfully
|
* - hmac_t if successfully
|
||||||
* - NULL if out of ressources or hash not supported
|
* - NULL if out of ressources or hash not supported
|
||||||
*/
|
*/
|
||||||
hmac_t *hmac_create(hash_algorithm_t hash_algorithm, chunk_t key);
|
hmac_t *hmac_create(hash_algorithm_t hash_algorithm);
|
||||||
|
|
||||||
#endif /*HMAC_H_*/
|
#endif /*HMAC_H_*/
|
||||||
|
|||||||
Reference in New Issue
Block a user