}
/**
+ * 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)
/*
* 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)
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 */
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);
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);
}
* @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.
* 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_*/