Rizin
unix-like reverse engineering framework and cli tools
openssl_common.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2021 deroad <wargio@libero.it>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 #ifndef RZ_OPENSSL_COMMON_H
5 #define RZ_OPENSSL_COMMON_H
6 
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>
12 
13 /*
14 EVP_md2
15 EVP_md5
16 EVP_sha
17 EVP_sha1
18 EVP_dss
19 EVP_dss1
20 EVP_mdc2
21 EVP_ripemd160
22 EVP_sha224
23 EVP_sha256
24 EVP_sha384
25 EVP_sha512
26 */
27 
28 #define rz_openssl_plugin_context_new(pluginname) \
29  static void *openssl_plugin_##pluginname##_context_new() { \
30  return EVP_MD_CTX_new(); \
31  }
32 
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); \
37  }
38 
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()); \
42  }
43 
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()); \
47  }
48 
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) { \
53  return false; \
54  } \
55  return true; \
56  }
57 
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); \
61  if (size < 1) { \
62  return true; \
63  } \
64  if (EVP_DigestUpdate((EVP_MD_CTX *)context, data, size) != 1) { \
65  return false; \
66  } \
67  return true; \
68  }
69 
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) { \
74  return false; \
75  } \
76  return true; \
77  }
78 
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(); \
83  if (!evp_md) { \
84  return false; \
85  } \
86  RzHashSize dgst_size = EVP_MD_size(evp_md); \
87  ut8 *dgst = malloc(dgst_size); \
88  if (!dgst) { \
89  return false; \
90  } \
91  EVP_MD_CTX *context = EVP_MD_CTX_new(); \
92  if (!context) { \
93  free(dgst); \
94  return false; \
95  } \
96  if (EVP_DigestInit_ex(context, evp_md, NULL) != 1) { \
97  EVP_MD_CTX_free(context); \
98  free(dgst); \
99  return false; \
100  } \
101  if (EVP_DigestUpdate(context, data, size) != 1) { \
102  EVP_MD_CTX_free(context); \
103  free(dgst); \
104  return false; \
105  } \
106  if (EVP_DigestFinal_ex(context, dgst, NULL) != 1) { \
107  EVP_MD_CTX_free(context); \
108  free(dgst); \
109  return false; \
110  } \
111  *digest = dgst; \
112  if (digest_size) { \
113  *digest_size = dgst_size; \
114  } \
115  EVP_MD_CTX_free(context); \
116  return true; \
117  }
118 
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, \
141  }
142 
143 #endif /* RZ_OPENSSL_COMMON_H */