diff options
author | Martin Willi <martin@strongswan.org> | 2005-11-22 09:27:41 +0000 |
---|---|---|
committer | Martin Willi <martin@strongswan.org> | 2005-11-22 09:27:41 +0000 |
commit | a217b51d6d4d036c3488fec0fa47991964679fd3 (patch) | |
tree | 37386c2d3cf9c1b3298eee8dce56329c8038821b /Source/charon/transforms | |
parent | 722000481bbb924dd5155ddbbad05bf4769b2703 (diff) | |
download | strongswan-a217b51d6d4d036c3488fec0fa47991964679fd3.tar.bz2 strongswan-a217b51d6d4d036c3488fec0fa47991964679fd3.tar.xz |
- removed key from constructor
- added set_key method
Diffstat (limited to 'Source/charon/transforms')
-rw-r--r-- | Source/charon/transforms/hmac.c | 62 | ||||
-rw-r--r-- | Source/charon/transforms/hmac.h | 16 |
2 files changed, 47 insertions, 31 deletions
diff --git a/Source/charon/transforms/hmac.c b/Source/charon/transforms/hmac.c index 93bbffade..1f078ebf0 100644 --- a/Source/charon/transforms/hmac.c +++ b/Source/charon/transforms/hmac.c @@ -110,6 +110,37 @@ static size_t get_block_size(private_hmac_t *this) } /** + * 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 */ static status_t destroy(private_hmac_t *this) @@ -125,10 +156,9 @@ static status_t destroy(private_hmac_t *this) /* * 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; - u_int8_t i; this = allocator_alloc_thing(private_hmac_t); 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.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.set_key = (status_t (*)(hmac_t *,chunk_t))set_key; this->public.destroy = (status_t (*)(hmac_t *))destroy; /* set b, according to hasher */ @@ -160,26 +191,7 @@ hmac_t *hmac_create(hash_algorithm_t hash_algorithm, chunk_t key) 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 */ 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) { this->h->destroy(this->h); - allocator_free(this->k.ptr); allocator_free(this->opaded_key.ptr); allocator_free(this); 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); } diff --git a/Source/charon/transforms/hmac.h b/Source/charon/transforms/hmac.h index f374b14f8..14cec6818 100644 --- a/Source/charon/transforms/hmac.h +++ b/Source/charon/transforms/hmac.h @@ -64,7 +64,18 @@ struct hmac_s { * @param this calling hmac * @return block size in bytes */ - 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. @@ -80,11 +91,10 @@ struct hmac_s { * Creates a new hmac_t object * * @param hash_algorithm hash algorithm to use - * @param key A chunk containing the key * @return * - hmac_t if successfully * - 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_*/ |