diff options
author | Kim Alvefur <zash@zash.se> | 2020-09-10 21:58:25 +0200 |
---|---|---|
committer | Kim Alvefur <zash@zash.se> | 2020-09-10 21:58:25 +0200 |
commit | 9f932f75595640510f3d36ab6bfe10e673235d9f (patch) | |
tree | ed1f501992e6a6599f91938b1949f93827b81dd3 /util-src | |
parent | ae14dc12208406f54c3ac260dc1dfe701e77f3a3 (diff) | |
download | prosody-9f932f75595640510f3d36ab6bfe10e673235d9f.tar.gz prosody-9f932f75595640510f3d36ab6bfe10e673235d9f.zip |
util.hashes: Refactor PBKDF2 to deduplicate code
Diffstat (limited to 'util-src')
-rw-r--r-- | util-src/hashes.c | 25 |
1 files changed, 8 insertions, 17 deletions
diff --git a/util-src/hashes.c b/util-src/hashes.c index 00661531..80740866 100644 --- a/util-src/hashes.c +++ b/util-src/hashes.c @@ -190,38 +190,29 @@ static int Lhmac_md5(lua_State *L) { } -static int Lpbkdf2_sha1(lua_State *L) { - unsigned char out[SHA_DIGEST_LENGTH]; +static int Levp_pbkdf2(lua_State *L, const EVP_MD *evp, size_t out_len) { + unsigned char out[EVP_MAX_MD_SIZE]; size_t pass_len, salt_len; const char *pass = luaL_checklstring(L, 1, &pass_len); const unsigned char *salt = (unsigned char *)luaL_checklstring(L, 2, &salt_len); const int iter = luaL_checkinteger(L, 3); - if(PKCS5_PBKDF2_HMAC(pass, pass_len, salt, salt_len, iter, EVP_sha1(), SHA_DIGEST_LENGTH, out) == 0) { + if(PKCS5_PBKDF2_HMAC(pass, pass_len, salt, salt_len, iter, evp, out_len, out) == 0) { return luaL_error(L, "PKCS5_PBKDF2_HMAC() failed"); } - lua_pushlstring(L, (char *)out, SHA_DIGEST_LENGTH); + lua_pushlstring(L, (char *)out, out_len); return 1; } +static int Lpbkdf2_sha1(lua_State *L) { + return Levp_pbkdf2(L, EVP_sha1(), SHA_DIGEST_LENGTH); +} static int Lpbkdf2_sha256(lua_State *L) { - unsigned char out[SHA256_DIGEST_LENGTH]; - - size_t pass_len, salt_len; - const char *pass = luaL_checklstring(L, 1, &pass_len); - const unsigned char *salt = (unsigned char *)luaL_checklstring(L, 2, &salt_len); - const int iter = luaL_checkinteger(L, 3); - - if(PKCS5_PBKDF2_HMAC(pass, pass_len, salt, salt_len, iter, EVP_sha256(), SHA256_DIGEST_LENGTH, out) == 0) { - return luaL_error(L, "PKCS5_PBKDF2_HMAC() failed"); - } - - lua_pushlstring(L, (char *)out, SHA256_DIGEST_LENGTH); - return 1; + return Levp_pbkdf2(L, EVP_sha256(), SHA256_DIGEST_LENGTH); } static int Lhash_equals(lua_State *L) { |