# HG changeset patch # User Kim Alvefur # Date 1555672320 -7200 # Node ID 29bc3dff341963966b7bb9edaf784fc4aa4eaed5 # Parent c74c89a96cbf4508eb90d4cb4234ff295583706e util.hashes: Use HMAC function provided by OpenSSL (fixes #1345) diff -r c74c89a96cbf -r 29bc3dff3419 util-src/hashes.c --- a/util-src/hashes.c Sun Apr 14 02:06:20 2019 +0200 +++ b/util-src/hashes.c Fri Apr 19 13:12:00 2019 +0200 @@ -25,6 +25,7 @@ #include "lauxlib.h" #include #include +#include #if (LUA_VERSION_NUM == 501) #define luaL_setfuncs(L, R, N) luaL_register(L, NULL, R) @@ -112,35 +113,28 @@ desc->Final(result, desc->ctxo); } -#define MAKE_HMAC_FUNCTION(myFunc, func, size, type) \ +#define MAKE_HMAC_FUNCTION(myFunc, evp, size, type) \ static int myFunc(lua_State *L) { \ - type ctx, ctxo; \ unsigned char hash[size], result[2*size]; \ size_t key_len, msg_len; \ + unsigned int out_len; \ 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); \ - struct hash_desc desc; \ - desc.Init = (int (*)(void*))func##_Init; \ - desc.Update = (int (*)(void*, const void *, size_t))func##_Update; \ - desc.Final = (int (*)(unsigned char*, void*))func##_Final; \ - desc.digestLength = size; \ - desc.ctx = &ctx; \ - desc.ctxo = &ctxo; \ - hmac(&desc, key, key_len, msg, msg_len, hash); \ + HMAC(evp(), key, key_len, (const unsigned char*)msg, msg_len, (unsigned char*)hash, &out_len); \ if (hex_out) { \ - toHex(hash, size, result); \ - lua_pushlstring(L, (char*)result, size*2); \ + toHex(hash, out_len, result); \ + lua_pushlstring(L, (char*)result, out_len*2); \ } else { \ - lua_pushlstring(L, (char*)hash, size); \ + lua_pushlstring(L, (char*)hash, out_len); \ } \ return 1; \ } -MAKE_HMAC_FUNCTION(Lhmac_sha1, SHA1, SHA_DIGEST_LENGTH, SHA_CTX) -MAKE_HMAC_FUNCTION(Lhmac_sha256, SHA256, SHA256_DIGEST_LENGTH, SHA256_CTX) -MAKE_HMAC_FUNCTION(Lhmac_sha512, SHA512, SHA512_DIGEST_LENGTH, SHA512_CTX) -MAKE_HMAC_FUNCTION(Lhmac_md5, MD5, MD5_DIGEST_LENGTH, MD5_CTX) +MAKE_HMAC_FUNCTION(Lhmac_sha1, EVP_sha1, SHA_DIGEST_LENGTH, SHA_CTX) +MAKE_HMAC_FUNCTION(Lhmac_sha256, EVP_sha256, SHA256_DIGEST_LENGTH, SHA256_CTX) +MAKE_HMAC_FUNCTION(Lhmac_sha512, EVP_sha512, SHA512_DIGEST_LENGTH, SHA512_CTX) +MAKE_HMAC_FUNCTION(Lhmac_md5, EVP_md5, MD5_DIGEST_LENGTH, MD5_CTX) static int LscramHi(lua_State *L) { union xory {