Software /
code /
prosody
Comparison
util-src/hashes.c @ 12559:865631ebb9f2
util.hashes: Refactor hash functions to use OpenSSL EVP methods (fix #1698)
MD5() is deprecated, but EVP_md5() is not.
Functions in macros like this make it awkward to apply static analysis
and code formatting.
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Thu, 10 Sep 2020 21:58:23 +0200 |
parent | 11562:0becc168f4f9 |
child | 12560:1e41dd0f8353 |
comparison
equal
deleted
inserted
replaced
12558:0f71106448af | 12559:865631ebb9f2 |
---|---|
44 out[i * 2] = hex_tab[(in[i] >> 4) & 0xF]; | 44 out[i * 2] = hex_tab[(in[i] >> 4) & 0xF]; |
45 out[i * 2 + 1] = hex_tab[(in[i]) & 0xF]; | 45 out[i * 2 + 1] = hex_tab[(in[i]) & 0xF]; |
46 } | 46 } |
47 } | 47 } |
48 | 48 |
49 #define MAKE_HASH_FUNCTION(myFunc, func, size) \ | 49 static int Levp_hash(lua_State *L, const EVP_MD *evp) { |
50 static int myFunc(lua_State *L) { \ | 50 size_t len; |
51 size_t len; \ | 51 unsigned int size = EVP_MAX_MD_SIZE; |
52 const char *s = luaL_checklstring(L, 1, &len); \ | 52 const char *s = luaL_checklstring(L, 1, &len); |
53 int hex_out = lua_toboolean(L, 2); \ | 53 int hex_out = lua_toboolean(L, 2); |
54 unsigned char hash[size], result[size*2]; \ | 54 |
55 func((const unsigned char*)s, len, hash); \ | 55 unsigned char hash[EVP_MAX_MD_SIZE], result[EVP_MAX_MD_SIZE * 2]; |
56 if (hex_out) { \ | 56 |
57 toHex(hash, size, result); \ | 57 EVP_MD_CTX *ctx = EVP_MD_CTX_new(); |
58 lua_pushlstring(L, (char*)result, size*2); \ | 58 |
59 } else { \ | 59 if(ctx == NULL) { |
60 lua_pushlstring(L, (char*)hash, size);\ | 60 goto fail; |
61 } \ | 61 } |
62 return 1; \ | 62 |
63 } | 63 if(!EVP_DigestInit_ex(ctx, evp, NULL)) { |
64 | 64 goto fail; |
65 MAKE_HASH_FUNCTION(Lsha1, SHA1, SHA_DIGEST_LENGTH) | 65 } |
66 MAKE_HASH_FUNCTION(Lsha224, SHA224, SHA224_DIGEST_LENGTH) | 66 |
67 MAKE_HASH_FUNCTION(Lsha256, SHA256, SHA256_DIGEST_LENGTH) | 67 if(!EVP_DigestUpdate(ctx, s, len)) { |
68 MAKE_HASH_FUNCTION(Lsha384, SHA384, SHA384_DIGEST_LENGTH) | 68 goto fail; |
69 MAKE_HASH_FUNCTION(Lsha512, SHA512, SHA512_DIGEST_LENGTH) | 69 } |
70 MAKE_HASH_FUNCTION(Lmd5, MD5, MD5_DIGEST_LENGTH) | 70 |
71 if(!EVP_DigestFinal_ex(ctx, hash, &size)) { | |
72 goto fail; | |
73 } | |
74 | |
75 EVP_MD_CTX_free(ctx); | |
76 | |
77 if(hex_out) { | |
78 toHex(hash, size, result); | |
79 lua_pushlstring(L, (char *)result, size * 2); | |
80 } else { | |
81 lua_pushlstring(L, (char *)hash, size); | |
82 } | |
83 | |
84 return 1; | |
85 | |
86 fail: | |
87 EVP_MD_CTX_free(ctx); | |
88 return luaL_error(L, "hash function failed"); | |
89 } | |
90 | |
91 static int Lsha1(lua_State *L) { | |
92 return Levp_hash(L, EVP_sha1()); | |
93 } | |
94 | |
95 static int Lsha224(lua_State *L) { | |
96 return Levp_hash(L, EVP_sha224()); | |
97 } | |
98 | |
99 static int Lsha256(lua_State *L) { | |
100 return Levp_hash(L, EVP_sha256()); | |
101 } | |
102 | |
103 static int Lsha384(lua_State *L) { | |
104 return Levp_hash(L, EVP_sha384()); | |
105 } | |
106 | |
107 static int Lsha512(lua_State *L) { | |
108 return Levp_hash(L, EVP_sha512()); | |
109 } | |
110 | |
111 static int Lmd5(lua_State *L) { | |
112 return Levp_hash(L, EVP_md5()); | |
113 } | |
71 | 114 |
72 struct hash_desc { | 115 struct hash_desc { |
73 int (*Init)(void *); | 116 int (*Init)(void *); |
74 int (*Update)(void *, const void *, size_t); | 117 int (*Update)(void *, const void *, size_t); |
75 int (*Final)(unsigned char *, void *); | 118 int (*Final)(unsigned char *, void *); |