Software /
code /
prosody
Comparison
util-src/hashes.c @ 9969:61bc5c52c941
util.hashes: Allow specifying output key length
This is not needed for SCRAM but PBKDF2 takes this argument.
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sat, 20 Apr 2019 15:11:04 +0200 |
parent | 9968:d536796a305f |
child | 9970:4a43feb9ab15 |
comparison
equal
deleted
inserted
replaced
9968:d536796a305f | 9969:61bc5c52c941 |
---|---|
98 MAKE_HMAC_FUNCTION(Lhmac_sha256, EVP_sha256, SHA256_DIGEST_LENGTH, SHA256_CTX) | 98 MAKE_HMAC_FUNCTION(Lhmac_sha256, EVP_sha256, SHA256_DIGEST_LENGTH, SHA256_CTX) |
99 MAKE_HMAC_FUNCTION(Lhmac_sha512, EVP_sha512, SHA512_DIGEST_LENGTH, SHA512_CTX) | 99 MAKE_HMAC_FUNCTION(Lhmac_sha512, EVP_sha512, SHA512_DIGEST_LENGTH, SHA512_CTX) |
100 MAKE_HMAC_FUNCTION(Lhmac_md5, EVP_md5, MD5_DIGEST_LENGTH, MD5_CTX) | 100 MAKE_HMAC_FUNCTION(Lhmac_md5, EVP_md5, MD5_DIGEST_LENGTH, MD5_CTX) |
101 | 101 |
102 static int Lpbkdf2_sha1(lua_State *L) { | 102 static int Lpbkdf2_sha1(lua_State *L) { |
103 unsigned char out[SHA_DIGEST_LENGTH]; | |
104 | |
105 size_t pass_len, salt_len; | 103 size_t pass_len, salt_len; |
106 const char *pass = luaL_checklstring(L, 1, &pass_len); | 104 const char *pass = luaL_checklstring(L, 1, &pass_len); |
107 const unsigned char *salt = (unsigned char *)luaL_checklstring(L, 2, &salt_len); | 105 const unsigned char *salt = (unsigned char *)luaL_checklstring(L, 2, &salt_len); |
108 const int iter = luaL_checkinteger(L, 3); | 106 const int iter = luaL_checkinteger(L, 3); |
107 const size_t len = luaL_optinteger(L, 4, SHA_DIGEST_LENGTH); | |
109 | 108 |
110 if(PKCS5_PBKDF2_HMAC(pass, pass_len, salt, salt_len, iter, EVP_sha1(), SHA_DIGEST_LENGTH, out) == 0) { | 109 luaL_Buffer b; |
110 unsigned char *out = (unsigned char *)luaL_buffinitsize(L, &b, len); | |
111 | |
112 if(PKCS5_PBKDF2_HMAC(pass, pass_len, salt, salt_len, iter, EVP_sha1(), len, out) == 0) { | |
111 return luaL_error(L, "PKCS5_PBKDF2_HMAC() failed"); | 113 return luaL_error(L, "PKCS5_PBKDF2_HMAC() failed"); |
112 } | 114 } |
113 | 115 |
114 lua_pushlstring(L, (char *)out, SHA_DIGEST_LENGTH); | 116 luaL_pushresultsize(&b, len); |
115 | |
116 return 1; | 117 return 1; |
117 } | 118 } |
118 | 119 |
119 | 120 |
120 static int Lpbkdf2_sha256(lua_State *L) { | 121 static int Lpbkdf2_sha256(lua_State *L) { |
121 unsigned char out[SHA256_DIGEST_LENGTH]; | |
122 | |
123 size_t pass_len, salt_len; | 122 size_t pass_len, salt_len; |
124 const char *pass = luaL_checklstring(L, 1, &pass_len); | 123 const char *pass = luaL_checklstring(L, 1, &pass_len); |
125 const unsigned char *salt = (unsigned char *)luaL_checklstring(L, 2, &salt_len); | 124 const unsigned char *salt = (unsigned char *)luaL_checklstring(L, 2, &salt_len); |
126 const int iter = luaL_checkinteger(L, 3); | 125 const int iter = luaL_checkinteger(L, 3); |
126 const int len = luaL_optinteger(L, 4, SHA256_DIGEST_LENGTH); | |
127 | 127 |
128 if(PKCS5_PBKDF2_HMAC(pass, pass_len, salt, salt_len, iter, EVP_sha256(), SHA256_DIGEST_LENGTH, out) == 0) { | 128 luaL_Buffer b; |
129 unsigned char *out = (unsigned char *)luaL_buffinitsize(L, &b, len); | |
130 | |
131 if(PKCS5_PBKDF2_HMAC(pass, pass_len, salt, salt_len, iter, EVP_sha256(), len, out) == 0) { | |
129 return luaL_error(L, "PKCS5_PBKDF2_HMAC() failed"); | 132 return luaL_error(L, "PKCS5_PBKDF2_HMAC() failed"); |
130 } | 133 } |
131 | 134 |
132 lua_pushlstring(L, (char *)out, SHA_DIGEST_LENGTH); | 135 luaL_pushresultsize(&b, len); |
133 return 1; | 136 return 1; |
134 } | 137 } |
135 | 138 |
136 static const luaL_Reg Reg[] = { | 139 static const luaL_Reg Reg[] = { |
137 { "sha1", Lsha1 }, | 140 { "sha1", Lsha1 }, |