Support void return values in OpenSSL 0.9.8 HMAC functions

This commit is contained in:
Martin Willi
2012-07-17 10:58:53 +02:00
parent 931da8202b
commit 082b0d7249
@@ -67,24 +67,42 @@ struct private_mac_t {
HMAC_CTX hmac; HMAC_CTX hmac;
}; };
/** METHOD(mac_t, set_key, bool,
* Resets HMAC context private_mac_t *this, chunk_t key)
*/
static bool reset(private_mac_t *this)
{ {
return HMAC_Init_ex(&this->hmac, NULL, 0, this->hasher, NULL); #if OPENSSL_VERSION_NUMBER >= 0x10000000L
return HMAC_Init_ex(&this->hmac, key.ptr, key.len, this->hasher, NULL);
#else /* OPENSSL_VERSION_NUMBER < 1.0 */
HMAC_Init_ex(&this->hmac, key.ptr, key.len, this->hasher, NULL);
return TRUE;
#endif
} }
METHOD(mac_t, get_mac, bool, METHOD(mac_t, get_mac, bool,
private_mac_t *this, chunk_t data, u_int8_t *out) private_mac_t *this, chunk_t data, u_int8_t *out)
{ {
#if OPENSSL_VERSION_NUMBER >= 0x10000000L
if (!HMAC_Update(&this->hmac, data.ptr, data.len))
{
return FALSE;
}
if (out == NULL) if (out == NULL)
{ {
return HMAC_Update(&this->hmac, data.ptr, data.len); return TRUE;
} }
return HMAC_Update(&this->hmac, data.ptr, data.len) && if (!HMAC_Final(&this->hmac, out, NULL))
HMAC_Final(&this->hmac, out, NULL) && {
reset(this); return FALSE;
}
#else /* OPENSSL_VERSION_NUMBER < 1.0 */
HMAC_Update(&this->hmac, data.ptr, data.len);
if (out == NULL)
{
return TRUE;
}
HMAC_Final(&this->hmac, out, NULL);
#endif
return set_key(this, chunk_empty);
} }
METHOD(mac_t, get_mac_size, size_t, METHOD(mac_t, get_mac_size, size_t,
@@ -93,12 +111,6 @@ METHOD(mac_t, get_mac_size, size_t,
return EVP_MD_size(this->hasher); return EVP_MD_size(this->hasher);
} }
METHOD(mac_t, set_key, bool,
private_mac_t *this, chunk_t key)
{
return HMAC_Init_ex(&this->hmac, key.ptr, key.len, this->hasher, NULL);
}
METHOD(mac_t, destroy, void, METHOD(mac_t, destroy, void,
private_mac_t *this) private_mac_t *this)
{ {
@@ -150,7 +162,7 @@ static mac_t *hmac_create(hash_algorithm_t algo)
} }
HMAC_CTX_init(&this->hmac); HMAC_CTX_init(&this->hmac);
if (!HMAC_Init_ex(&this->hmac, NULL, 0, this->hasher, NULL)) if (!set_key(this, chunk_empty))
{ {
destroy(this); destroy(this);
return NULL; return NULL;
@@ -190,4 +202,3 @@ signer_t *openssl_hmac_signer_create(integrity_algorithm_t algo)
return NULL; return NULL;
} }