Unify crypto plugin api to match all the other plugins. (#3773)

This commit is contained in:
Giovanni 2023-08-18 23:25:41 +08:00 committed by GitHub
parent 8b792f5a81
commit ac389d95dd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 132 additions and 40 deletions

55
librz/core/ccrypto.c Normal file
View file

@ -0,0 +1,55 @@
// SPDX-FileCopyrightText: 2023 deroad <wargio@libero.it>
// SPDX-License-Identifier: LGPL-3.0-only
#include <rz_core.h>
static RzCmdStatus core_crypto_plugin_print(RzCmdStateOutput *state, const RzCryptoPlugin *plugin) {
PJ *pj = state->d.pj;
switch (state->mode) {
case RZ_OUTPUT_MODE_QUIET: {
rz_cons_printf("%s ", plugin->name);
break;
}
case RZ_OUTPUT_MODE_JSON: {
pj_o(pj);
pj_ks(pj, "name", plugin->name);
pj_ks(pj, "license", plugin->license);
pj_ks(pj, "author", plugin->author);
pj_end(pj);
break;
}
case RZ_OUTPUT_MODE_STANDARD: {
rz_cons_printf("%-14s %-10s %s\n", plugin->name, plugin->license, plugin->author);
break;
}
default: {
rz_warn_if_reached();
return RZ_CMD_STATUS_NONEXISTINGCMD;
}
}
return RZ_CMD_STATUS_OK;
}
RZ_API RzCmdStatus rz_core_crypto_plugins_print(RzCrypto *cry, RzCmdStateOutput *state) {
rz_return_val_if_fail(cry, RZ_CMD_STATUS_ERROR);
const RzCryptoPlugin *plugin = NULL;
RzCmdStatus status;
RzListIter *it;
rz_cmd_state_output_array_start(state);
if (state->mode == RZ_OUTPUT_MODE_STANDARD) {
rz_cons_println("algorithm license author");
}
rz_list_foreach (cry->plugins, it, plugin) {
status = core_crypto_plugin_print(state, plugin);
if (status != RZ_CMD_STATUS_OK) {
return status;
}
}
if (state->mode == RZ_OUTPUT_MODE_QUIET) {
rz_cons_newline();
}
rz_cmd_state_output_array_end(state);
return RZ_CMD_STATUS_OK;
}

View file

@ -24,6 +24,10 @@ RZ_IPI RzCmdStatus rz_plugins_core_print_handler(RzCore *core, int argc, const c
return rz_core_core_plugins_print(core, state);
}
RZ_IPI RzCmdStatus rz_plugins_crypto_print_handler(RzCore *core, int argc, const char **argv, RzCmdStateOutput *state) {
return rz_core_crypto_plugins_print(core->crypto, state);
}
RZ_IPI RzCmdStatus rz_plugins_debug_print_handler(RzCore *core, int argc, const char **argv, RzCmdStateOutput *state) {
if (argc > 1) {
return rz_config_set(core->config, "dbg.backend", argv[1]) ? RZ_CMD_STATUS_OK : RZ_CMD_STATUS_ERROR;

View file

@ -11504,6 +11504,14 @@ static const RzCmdDescHelp plugins_core_print_help = {
.args = plugins_core_print_args,
};
static const RzCmdDescArg plugins_crypto_print_args[] = {
{ 0 },
};
static const RzCmdDescHelp plugins_crypto_print_help = {
.summary = "List the crypto plugins",
.args = plugins_crypto_print_args,
};
static const RzCmdDescArg plugins_debug_print_args[] = {
{
.name = "handler",
@ -20924,6 +20932,9 @@ RZ_IPI void rzshell_cmddescs_init(RzCore *core) {
RzCmdDesc *plugins_core_print_cd = rz_cmd_desc_argv_state_new(core->rcmd, L_cd, "Lc", RZ_OUTPUT_MODE_STANDARD | RZ_OUTPUT_MODE_JSON, rz_plugins_core_print_handler, &plugins_core_print_help);
rz_warn_if_fail(plugins_core_print_cd);
RzCmdDesc *plugins_crypto_print_cd = rz_cmd_desc_argv_state_new(core->rcmd, L_cd, "LC", RZ_OUTPUT_MODE_STANDARD | RZ_OUTPUT_MODE_JSON, rz_plugins_crypto_print_handler, &plugins_crypto_print_help);
rz_warn_if_fail(plugins_crypto_print_cd);
RzCmdDesc *plugins_debug_print_cd = rz_cmd_desc_argv_state_new(core->rcmd, L_cd, "Ld", RZ_OUTPUT_MODE_QUIET | RZ_OUTPUT_MODE_STANDARD | RZ_OUTPUT_MODE_JSON, rz_plugins_debug_print_handler, &plugins_debug_print_help);
rz_warn_if_fail(plugins_debug_print_cd);

View file

@ -1581,6 +1581,8 @@ RZ_IPI RzCmdStatus rz_plugins_lang_print_handler(RzCore *core, int argc, const c
RZ_IPI RzCmdStatus rz_plugins_asm_print_handler(RzCore *core, int argc, const char **argv, RzCmdStateOutput *state);
// "Lc"
RZ_IPI RzCmdStatus rz_plugins_core_print_handler(RzCore *core, int argc, const char **argv, RzCmdStateOutput *state);
// "LC"
RZ_IPI RzCmdStatus rz_plugins_crypto_print_handler(RzCore *core, int argc, const char **argv, RzCmdStateOutput *state);
// "Ld"
RZ_IPI RzCmdStatus rz_plugins_debug_print_handler(RzCore *core, int argc, const char **argv, RzCmdStateOutput *state);
// "Lh"

View file

@ -40,6 +40,14 @@ commands:
modes:
- RZ_OUTPUT_MODE_STANDARD
- RZ_OUTPUT_MODE_JSON
- name: LC
cname: plugins_crypto_print
summary: List the crypto plugins
type: RZ_CMD_DESC_TYPE_ARGV_STATE
args: []
modes:
- RZ_OUTPUT_MODE_STANDARD
- RZ_OUTPUT_MODE_JSON
- name: Ld
summary: List debug plugins / Set debug backend (e dbg.backend)
cname: plugins_debug_print
@ -90,4 +98,4 @@ commands:
args: []
modes:
- RZ_OUTPUT_MODE_STANDARD
- RZ_OUTPUT_MODE_JSON
- RZ_OUTPUT_MODE_JSON

View file

@ -23,6 +23,7 @@ rz_core_sources = [
'cautocmpl.c',
'cbin.c',
'cconfig.c',
'ccrypto.c',
'cdebug.c',
'cdwarf.c',
'cesil.c',

View file

@ -42,7 +42,7 @@ static const struct {
{ "punycode", RZ_CODEC_PUNYCODE },
};
RZ_API const char *rz_crypto_name(const RzCryptoSelector bit) {
RZ_API RZ_BORROW const char *rz_crypto_name(const RzCryptoSelector bit) {
size_t i;
for (i = 1; i < RZ_ARRAY_SIZE(crypto_name_bytes); i++) {
if (bit == crypto_name_bytes[i].bit) {
@ -52,7 +52,7 @@ RZ_API const char *rz_crypto_name(const RzCryptoSelector bit) {
return "";
}
RZ_API const char *rz_crypto_codec_name(const RzCryptoSelector bit) {
RZ_API RZ_BORROW const char *rz_crypto_codec_name(const RzCryptoSelector bit) {
size_t i;
for (i = 1; i < RZ_ARRAY_SIZE(codec_name_bytes); i++) {
if (bit == codec_name_bytes[i].bit) {
@ -62,21 +62,29 @@ RZ_API const char *rz_crypto_codec_name(const RzCryptoSelector bit) {
return "";
}
RZ_API const RzCryptoPlugin *rz_crypto_plugin_by_index(size_t index) {
const size_t size = RZ_ARRAY_SIZE(crypto_static_plugins);
if (index >= size) {
return NULL;
RZ_API RZ_BORROW const RzCryptoPlugin *rz_crypto_plugin_by_index(RZ_NONNULL RzCrypto *cry, size_t index) {
rz_return_val_if_fail(cry, NULL);
RzListIter *it;
const RzCryptoPlugin *plugin;
size_t i = 0;
rz_list_foreach (cry->plugins, it, plugin) {
if (i == index) {
return plugin;
}
i++;
}
return crypto_static_plugins[index];
return NULL;
}
RZ_API bool rz_crypto_plugin_add(RzCrypto *cry, RZ_NONNULL RzCryptoPlugin *plugin) {
RZ_API bool rz_crypto_plugin_add(RZ_NONNULL RzCrypto *cry, RZ_NONNULL RzCryptoPlugin *plugin) {
rz_return_val_if_fail(cry && plugin, false);
RZ_PLUGIN_CHECK_AND_ADD(cry->plugins, plugin, RzCryptoPlugin);
return true;
}
RZ_API bool rz_crypto_plugin_del(RzCrypto *cry, RZ_NONNULL RzCryptoPlugin *plugin) {
RZ_API bool rz_crypto_plugin_del(RZ_NONNULL RzCrypto *cry, RZ_NONNULL RzCryptoPlugin *plugin) {
rz_return_val_if_fail(cry && plugin, false);
if (cry->h == plugin && cry->h->fini) {
cry->h->fini(cry);
@ -86,7 +94,7 @@ RZ_API bool rz_crypto_plugin_del(RzCrypto *cry, RZ_NONNULL RzCryptoPlugin *plugi
return true;
}
RZ_API RzCrypto *rz_crypto_new(void) {
RZ_API RZ_OWN RzCrypto *rz_crypto_new(void) {
RzCrypto *cry = RZ_NEW0(RzCrypto);
if (!cry) {
goto rz_crypto_new_bad;
@ -110,7 +118,7 @@ rz_crypto_new_bad:
return NULL;
}
RZ_API void rz_crypto_free(RzCrypto *cry) {
RZ_API void rz_crypto_free(RZ_NULLABLE RzCrypto *cry) {
if (!cry) {
return;
}
@ -132,7 +140,7 @@ RZ_API void rz_crypto_free(RzCrypto *cry) {
*
* \param cry RzCrypto reference
*/
RZ_API void rz_crypto_reset(RzCrypto *cry) {
RZ_API void rz_crypto_reset(RZ_NONNULL RzCrypto *cry) {
rz_return_if_fail(cry);
if (cry->h && cry->h->fini && !cry->h->fini(cry)) {
@ -144,7 +152,8 @@ RZ_API void rz_crypto_reset(RzCrypto *cry) {
cry->output_len = 0;
}
RZ_API bool rz_crypto_use(RzCrypto *cry, const char *algo) {
RZ_API bool rz_crypto_use(RZ_NONNULL RzCrypto *cry, RZ_NONNULL const char *algo) {
rz_return_val_if_fail(cry && algo, false);
RzListIter *iter;
RzCryptoPlugin *h;
if (cry->h && cry->h->fini && !cry->h->fini(cry)) {
@ -165,9 +174,9 @@ RZ_API bool rz_crypto_use(RzCrypto *cry, const char *algo) {
return false;
}
RZ_API bool rz_crypto_set_key(RzCrypto *cry, const ut8 *key, int keylen, int mode, int direction) {
RZ_API bool rz_crypto_set_key(RZ_NULLABLE RzCrypto *cry, RZ_NULLABLE const ut8 *key, int keylen, int mode, int direction) {
if (keylen < 0) {
keylen = strlen((const char *)key);
keylen = key ? strlen((const char *)key) : 0;
}
if (!cry || !cry->h || !cry->h->set_key) {
return false;
@ -175,24 +184,22 @@ RZ_API bool rz_crypto_set_key(RzCrypto *cry, const ut8 *key, int keylen, int mod
return cry->h->set_key(cry, key, keylen, mode, direction);
}
RZ_API bool rz_crypto_set_iv(RzCrypto *cry, const ut8 *iv, int ivlen) {
RZ_API bool rz_crypto_set_iv(RZ_NULLABLE RzCrypto *cry, RZ_NULLABLE const ut8 *iv, int ivlen) {
return (cry && cry->h && cry->h->set_iv) ? cry->h->set_iv(cry, iv, ivlen) : 0;
}
// return the number of bytes written in the output buffer
RZ_API int rz_crypto_update(RzCrypto *cry, const ut8 *buf, int len) {
RZ_API int rz_crypto_update(RZ_NULLABLE RzCrypto *cry, RZ_NULLABLE const ut8 *buf, int len) {
return (cry && cry->h && cry->h->update) ? cry->h->update(cry, buf, len) : 0;
}
RZ_API int rz_crypto_final(RzCrypto *cry, const ut8 *buf, int len) {
RZ_API int rz_crypto_final(RZ_NULLABLE RzCrypto *cry, RZ_NULLABLE const ut8 *buf, int len) {
return (cry && cry->h && cry->h->final) ? cry->h->final(cry, buf, len) : 0;
}
// TODO: internal api?? used from plugins? TODO: use rz_buf here
RZ_API int rz_crypto_append(RzCrypto *cry, const ut8 *buf, int len) {
if (!cry || !buf) {
return -1;
}
RZ_API int rz_crypto_append(RZ_NONNULL RzCrypto *cry, RZ_NONNULL const ut8 *buf, int len) {
rz_return_val_if_fail(cry && buf, -1);
if (cry->output_len + len > cry->output_size) {
cry->output_size += 4096 + len;
cry->output = realloc(cry->output, cry->output_size);
@ -207,7 +214,8 @@ RZ_API int rz_crypto_append(RzCrypto *cry, const ut8 *buf, int len) {
return cry->output_len;
}
RZ_API const ut8 *rz_crypto_get_output(RzCrypto *cry, int *size) {
RZ_API RZ_BORROW const ut8 *rz_crypto_get_output(RZ_NONNULL RzCrypto *cry, RZ_NULLABLE int *size) {
rz_return_val_if_fail(cry, NULL);
if (cry->output_size < 1 || !cry->output) {
if (size) {
*size = 0;

View file

@ -586,6 +586,9 @@ RZ_API void rz_core_debug_map_print(RzCore *core, ut64 addr, RzCmdStateOutput *s
/* chash.c */
RZ_API RzCmdStatus rz_core_hash_plugins_print(RzHash *hash, RzCmdStateOutput *state);
/* ccrypto.c */
RZ_API RzCmdStatus rz_core_crypto_plugins_print(RzCrypto *cry, RzCmdStateOutput *state);
/* cio.c */
RZ_API RzCmdStatus rz_core_io_plugins_print(RzIO *io, RzCmdStateOutput *state);

View file

@ -54,21 +54,21 @@ typedef struct rz_crypto_plugin_t {
typedef ut64 RzCryptoSelector;
#ifdef RZ_API
RZ_API bool rz_crypto_plugin_add(RzCrypto *cry, RZ_NONNULL RzCryptoPlugin *h);
RZ_API bool rz_crypto_plugin_del(RzCrypto *cry, RZ_NONNULL RzCryptoPlugin *h);
RZ_API RzCrypto *rz_crypto_new(void);
RZ_API void rz_crypto_free(RzCrypto *cry);
RZ_API void rz_crypto_reset(RzCrypto *cry);
RZ_API bool rz_crypto_use(RzCrypto *cry, const char *algo);
RZ_API bool rz_crypto_set_key(RzCrypto *cry, const ut8 *key, int keylen, int mode, int direction);
RZ_API bool rz_crypto_set_iv(RzCrypto *cry, const ut8 *iv, int ivlen);
RZ_API int rz_crypto_update(RzCrypto *cry, const ut8 *buf, int len);
RZ_API int rz_crypto_final(RzCrypto *cry, const ut8 *buf, int len);
RZ_API int rz_crypto_append(RzCrypto *cry, const ut8 *buf, int len);
RZ_API const ut8 *rz_crypto_get_output(RzCrypto *cry, int *size);
RZ_API const char *rz_crypto_name(const RzCryptoSelector bit);
RZ_API const char *rz_crypto_codec_name(const RzCryptoSelector bit);
RZ_API const RzCryptoPlugin *rz_crypto_plugin_by_index(size_t index);
RZ_API bool rz_crypto_plugin_add(RZ_NONNULL RzCrypto *cry, RZ_NONNULL RzCryptoPlugin *h);
RZ_API bool rz_crypto_plugin_del(RZ_NONNULL RzCrypto *cry, RZ_NONNULL RzCryptoPlugin *h);
RZ_API RZ_OWN RzCrypto *rz_crypto_new(void);
RZ_API void rz_crypto_free(RZ_NULLABLE RzCrypto *cry);
RZ_API void rz_crypto_reset(RZ_NONNULL RzCrypto *cry);
RZ_API bool rz_crypto_use(RZ_NONNULL RzCrypto *cry, RZ_NONNULL const char *algo);
RZ_API bool rz_crypto_set_key(RZ_NULLABLE RzCrypto *cry, RZ_NULLABLE const ut8 *key, int keylen, int mode, int direction);
RZ_API bool rz_crypto_set_iv(RZ_NULLABLE RzCrypto *cry, RZ_NULLABLE const ut8 *iv, int ivlen);
RZ_API int rz_crypto_update(RZ_NULLABLE RzCrypto *cry, RZ_NULLABLE const ut8 *buf, int len);
RZ_API int rz_crypto_final(RZ_NULLABLE RzCrypto *cry, RZ_NULLABLE const ut8 *buf, int len);
RZ_API int rz_crypto_append(RZ_NONNULL RzCrypto *cry, RZ_NONNULL const ut8 *buf, int len);
RZ_API RZ_BORROW const ut8 *rz_crypto_get_output(RZ_NONNULL RzCrypto *cry, RZ_NULLABLE int *size);
RZ_API RZ_BORROW const char *rz_crypto_name(const RzCryptoSelector bit);
RZ_API RZ_BORROW const char *rz_crypto_codec_name(const RzCryptoSelector bit);
RZ_API RZ_BORROW const RzCryptoPlugin *rz_crypto_plugin_by_index(RZ_NONNULL RzCrypto *cry, size_t index);
#endif
/* plugin pointers */

View file

@ -119,7 +119,7 @@ static void rz_hash_show_algorithms(RzHashContext *ctx) {
}
const RzCryptoPlugin *rcp;
for (size_t i = 0; (rcp = rz_crypto_plugin_by_index(i)); i++) {
for (size_t i = 0; (rcp = rz_crypto_plugin_by_index(ctx->rc, i)); i++) {
if (!strncmp("base", rcp->name, 4) || !strcmp("punycode", rcp->name)) {
snprintf(flags, sizeof(flags), "__ed__");
} else if (!strcmp("rol", rcp->name)) {