Software /
code /
prosody
Comparison
util-src/hashes.c @ 12568:fc6213104d78
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.
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Fri, 24 Jun 2022 16:59:54 +0200 |
parent | 12567:96871ee6e26c |
child | 12575:1f6f05a98fcd |
comparison
equal
deleted
inserted
replaced
12567:96871ee6e26c | 12568:fc6213104d78 |
---|---|
127 } | 127 } |
128 | 128 |
129 static int Levp_hmac(lua_State *L, const EVP_MD *evp) { | 129 static int Levp_hmac(lua_State *L, const EVP_MD *evp) { |
130 unsigned char hash[EVP_MAX_MD_SIZE], result[EVP_MAX_MD_SIZE * 2]; | 130 unsigned char hash[EVP_MAX_MD_SIZE], result[EVP_MAX_MD_SIZE * 2]; |
131 size_t key_len, msg_len; | 131 size_t key_len, msg_len; |
132 size_t out_len = EVP_MAX_MD_SIZE; | 132 unsigned int out_len = EVP_MAX_MD_SIZE; |
133 const char *key = luaL_checklstring(L, 1, &key_len); | 133 const char *key = luaL_checklstring(L, 1, &key_len); |
134 const char *msg = luaL_checklstring(L, 2, &msg_len); | 134 const char *msg = luaL_checklstring(L, 2, &msg_len); |
135 const int hex_out = lua_toboolean(L, 3); | 135 const int hex_out = lua_toboolean(L, 3); |
136 | 136 |
137 EVP_PKEY *pkey = EVP_PKEY_new_raw_private_key(EVP_PKEY_HMAC, NULL, (unsigned char *)key, key_len); | 137 if(HMAC(evp, key, key_len, (const unsigned char*)msg, msg_len, (unsigned char*)hash, &out_len) == NULL) { |
138 EVP_MD_CTX *ctx = EVP_MD_CTX_new(); | 138 goto fail; |
139 | 139 } |
140 if(ctx == NULL || pkey == NULL) { | |
141 goto fail; | |
142 } | |
143 | |
144 if(!EVP_DigestSignInit(ctx, NULL, evp, NULL, pkey)) { | |
145 goto fail; | |
146 } | |
147 | |
148 if(!EVP_DigestSignUpdate(ctx, msg, msg_len)) { | |
149 goto fail; | |
150 } | |
151 | |
152 if(!EVP_DigestSignFinal(ctx, hash, &out_len)) { | |
153 goto fail; | |
154 } | |
155 | |
156 EVP_MD_CTX_free(ctx); | |
157 EVP_PKEY_free(pkey); | |
158 | 140 |
159 if(hex_out) { | 141 if(hex_out) { |
160 toHex(hash, out_len, result); | 142 toHex(hash, out_len, result); |
161 lua_pushlstring(L, (char *)result, out_len * 2); | 143 lua_pushlstring(L, (char *)result, out_len * 2); |
162 } else { | 144 } else { |
164 } | 146 } |
165 | 147 |
166 return 1; | 148 return 1; |
167 | 149 |
168 fail: | 150 fail: |
169 EVP_MD_CTX_free(ctx); | |
170 EVP_PKEY_free(pkey); | |
171 return luaL_error(L, ERR_error_string(ERR_get_error(), NULL)); | 151 return luaL_error(L, ERR_error_string(ERR_get_error(), NULL)); |
172 } | 152 } |
173 | 153 |
174 static int Lhmac_sha1(lua_State *L) { | 154 static int Lhmac_sha1(lua_State *L) { |
175 return Levp_hmac(L, EVP_sha1()); | 155 return Levp_hmac(L, EVP_sha1()); |