diff options
author | Kim Alvefur <zash@zash.se> | 2019-04-19 13:12:00 +0200 |
---|---|---|
committer | Kim Alvefur <zash@zash.se> | 2019-04-19 13:12:00 +0200 |
commit | 7ef028d8d49684247dfcb0ae276426684bf2d778 (patch) | |
tree | 587aacfbbe671fdc7b67f2a03fa1f88173727dda /util-src/hashes.c | |
parent | 86b34563ebbd63da7c4653b06537cca12e046c3c (diff) | |
download | prosody-7ef028d8d49684247dfcb0ae276426684bf2d778.tar.gz prosody-7ef028d8d49684247dfcb0ae276426684bf2d778.zip |
util.hashes: Use HMAC function provided by OpenSSL (fixes #1345)
Diffstat (limited to 'util-src/hashes.c')
-rw-r--r-- | util-src/hashes.c | 28 |
1 files changed, 11 insertions, 17 deletions
diff --git a/util-src/hashes.c b/util-src/hashes.c index 82f5876e..903ecb6e 100644 --- a/util-src/hashes.c +++ b/util-src/hashes.c @@ -25,6 +25,7 @@ typedef unsigned __int32 uint32_t; #include "lauxlib.h" #include <openssl/sha.h> #include <openssl/md5.h> +#include <openssl/hmac.h> #if (LUA_VERSION_NUM == 501) #define luaL_setfuncs(L, R, N) luaL_register(L, NULL, R) @@ -112,35 +113,28 @@ static void hmac(struct hash_desc *desc, const char *key, size_t key_len, 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 { |