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.
This commit is contained in:
Kim Alvefur 2022-06-24 16:59:54 +02:00
parent 8f3d837cd2
commit b1c7b93139

View file

@ -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) {
if(HMAC(evp, key, key_len, (const unsigned char*)msg, msg_len, (unsigned char*)hash, &out_len) == 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)) {
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));
}