4 #ifndef RZ_OPENSSL_COMMON_H
5 #define RZ_OPENSSL_COMMON_H
7 #include <openssl/evp.h>
8 #include <openssl/err.h>
9 #include <openssl/sha.h>
10 #include <openssl/md4.h>
11 #include <openssl/md5.h>
28 #define rz_openssl_plugin_context_new(pluginname) \
29 static void *openssl_plugin_##pluginname##_context_new() { \
30 return EVP_MD_CTX_new(); \
33 #define rz_openssl_plugin_context_free(pluginname) \
34 static void openssl_plugin_##pluginname##_context_free(void *context) { \
35 rz_return_if_fail(context); \
36 EVP_MD_CTX_free((EVP_MD_CTX *)context); \
39 #define rz_openssl_plugin_digest_size(pluginname, evpmd) \
40 static RzHashSize openssl_plugin_##pluginname##_digest_size(void *context) { \
41 return EVP_MD_size(evpmd()); \
44 #define rz_openssl_plugin_block_size(pluginname, evpmd) \
45 static RzHashSize openssl_plugin_##pluginname##_block_size(void *context) { \
46 return EVP_MD_block_size(evpmd()); \
49 #define rz_openssl_plugin_init(pluginname, evpmd) \
50 static bool openssl_plugin_##pluginname##_init(void *context) { \
51 rz_return_val_if_fail(context, false); \
52 if (EVP_DigestInit_ex((EVP_MD_CTX *)context, evpmd(), NULL) != 1) { \
58 #define rz_openssl_plugin_update(pluginname) \
59 static bool openssl_plugin_##pluginname##_update(void *context, const ut8 *data, ut64 size) { \
60 rz_return_val_if_fail((context) && (data), false); \
64 if (EVP_DigestUpdate((EVP_MD_CTX *)context, data, size) != 1) { \
70 #define rz_openssl_plugin_final(pluginname) \
71 static bool openssl_plugin_##pluginname##_final(void *context, ut8 *digest) { \
72 rz_return_val_if_fail((context) && (digest), false); \
73 if (EVP_DigestFinal_ex((EVP_MD_CTX *)context, digest, NULL) != 1) { \
79 #define rz_openssl_plugin_small_block(pluginname, evpmd) \
80 static bool openssl_plugin_##pluginname##_small_block(const ut8 *data, ut64 size, ut8 **digest, RzHashSize *digest_size) { \
81 rz_return_val_if_fail((data) && (digest), false); \
82 const EVP_MD *evp_md = evpmd(); \
86 RzHashSize dgst_size = EVP_MD_size(evp_md); \
87 ut8 *dgst = malloc(dgst_size); \
91 EVP_MD_CTX *context = EVP_MD_CTX_new(); \
96 if (EVP_DigestInit_ex(context, evp_md, NULL) != 1) { \
97 EVP_MD_CTX_free(context); \
101 if (EVP_DigestUpdate(context, data, size) != 1) { \
102 EVP_MD_CTX_free(context); \
106 if (EVP_DigestFinal_ex(context, dgst, NULL) != 1) { \
107 EVP_MD_CTX_free(context); \
113 *digest_size = dgst_size; \
115 EVP_MD_CTX_free(context); \
119 #define rz_openssl_plugin_define_hash_cfg(pluginname, evpmd, canhmac) \
120 rz_openssl_plugin_context_new(pluginname); \
121 rz_openssl_plugin_context_free(pluginname); \
122 rz_openssl_plugin_digest_size(pluginname, evpmd); \
123 rz_openssl_plugin_block_size(pluginname, evpmd); \
124 rz_openssl_plugin_init(pluginname, evpmd); \
125 rz_openssl_plugin_update(pluginname); \
126 rz_openssl_plugin_final(pluginname); \
127 rz_openssl_plugin_small_block(pluginname, evpmd); \
128 RzHashPlugin rz_hash_plugin_##pluginname = { \
129 .name = #pluginname, \
130 .license = "Apache 2.0", \
131 .author = "OpenSSL Team", \
132 .support_hmac = canhmac, \
133 .context_new = openssl_plugin_##pluginname##_context_new, \
134 .context_free = openssl_plugin_##pluginname##_context_free, \
135 .digest_size = openssl_plugin_##pluginname##_digest_size, \
136 .block_size = openssl_plugin_##pluginname##_block_size, \
137 .init = openssl_plugin_##pluginname##_init, \
138 .update = openssl_plugin_##pluginname##_update, \
139 .final = openssl_plugin_##pluginname##_final, \
140 .small_block = openssl_plugin_##pluginname##_small_block, \