Software /
code /
prosody
Changeset
12568:fc6213104d78
util.hashes: Revert to HMAC() convenience function
Reverts some of 1e41dd0f8353
Seems HMAC() isn't deprecated after all? Must have been at some point
according to #1589
Twice as fast for some reason.
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Fri, 24 Jun 2022 16:59:54 +0200 |
parents | 12567:96871ee6e26c |
children | 12569:b5d9f1829b15 |
files | util-src/hashes.c |
diffstat | 1 files changed, 2 insertions(+), 22 deletions(-) [+] |
line wrap: on
line diff
--- a/util-src/hashes.c Fri Jun 24 16:49:03 2022 +0200 +++ b/util-src/hashes.c Fri Jun 24 16:59:54 2022 +0200 @@ -129,33 +129,15 @@ static int Levp_hmac(lua_State *L, const EVP_MD *evp) { unsigned char hash[EVP_MAX_MD_SIZE], result[EVP_MAX_MD_SIZE * 2]; size_t key_len, msg_len; - size_t out_len = EVP_MAX_MD_SIZE; + unsigned int out_len = EVP_MAX_MD_SIZE; const char *key = luaL_checklstring(L, 1, &key_len); const char *msg = luaL_checklstring(L, 2, &msg_len); const int hex_out = lua_toboolean(L, 3); - EVP_PKEY *pkey = EVP_PKEY_new_raw_private_key(EVP_PKEY_HMAC, NULL, (unsigned char *)key, key_len); - EVP_MD_CTX *ctx = EVP_MD_CTX_new(); - - if(ctx == NULL || pkey == NULL) { - goto fail; - } - - if(!EVP_DigestSignInit(ctx, NULL, evp, NULL, pkey)) { + if(HMAC(evp, key, key_len, (const unsigned char*)msg, msg_len, (unsigned char*)hash, &out_len) == NULL) { goto fail; } - if(!EVP_DigestSignUpdate(ctx, msg, msg_len)) { - goto fail; - } - - if(!EVP_DigestSignFinal(ctx, hash, &out_len)) { - goto fail; - } - - EVP_MD_CTX_free(ctx); - EVP_PKEY_free(pkey); - if(hex_out) { toHex(hash, out_len, result); lua_pushlstring(L, (char *)result, out_len * 2); @@ -166,8 +148,6 @@ return 1; fail: - EVP_MD_CTX_free(ctx); - EVP_PKEY_free(pkey); return luaL_error(L, ERR_error_string(ERR_get_error(), NULL)); }