diff options
author | Kim Alvefur <zash@zash.se> | 2022-06-24 16:59:54 +0200 |
---|---|---|
committer | Kim Alvefur <zash@zash.se> | 2022-06-24 16:59:54 +0200 |
commit | b1c7b93139f52acd11ac22494b06a3678135ffdc (patch) | |
tree | 112bf583b391701dc011c510dd888eb1325f48ce /util-src | |
parent | 8f3d837cd253a226a30fd05529ff7b8bf0d535fa (diff) | |
download | prosody-b1c7b93139f52acd11ac22494b06a3678135ffdc.tar.gz prosody-b1c7b93139f52acd11ac22494b06a3678135ffdc.zip |
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.
Diffstat (limited to 'util-src')
-rw-r--r-- | util-src/hashes.c | 24 |
1 files changed, 2 insertions, 22 deletions
diff --git a/util-src/hashes.c b/util-src/hashes.c index 4ad786ae..fbc86081 100644 --- a/util-src/hashes.c +++ b/util-src/hashes.c @@ -129,33 +129,15 @@ static int Lsha3_512(lua_State *L) { 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)) { - goto fail; - } - - if(!EVP_DigestSignUpdate(ctx, msg, msg_len)) { - goto fail; - } - - if(!EVP_DigestSignFinal(ctx, hash, &out_len)) { + if(HMAC(evp, key, key_len, (const unsigned char*)msg, msg_len, (unsigned char*)hash, &out_len) == NULL) { 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 @@ static int Levp_hmac(lua_State *L, const EVP_MD *evp) { return 1; fail: - EVP_MD_CTX_free(ctx); - EVP_PKEY_free(pkey); return luaL_error(L, ERR_error_string(ERR_get_error(), NULL)); } |