2022-05-19 20:14:14 +00:00
|
|
|
/* radare - LGPL - Copyright 2009-2022 - pancake */
|
2009-03-31 00:50:02 +00:00
|
|
|
|
|
|
|
#include "r_crypto.h"
|
2022-09-21 16:35:36 +00:00
|
|
|
#include "r_hash.h"
|
2017-05-26 00:43:26 +00:00
|
|
|
#include "config.h"
|
2022-09-06 23:41:58 +00:00
|
|
|
#include "r_util/r_assert.h"
|
2009-09-14 23:45:58 +00:00
|
|
|
|
2013-08-30 00:59:20 +00:00
|
|
|
R_LIB_VERSION (r_crypto);
|
2013-06-14 00:51:33 +00:00
|
|
|
|
2016-03-07 01:38:50 +00:00
|
|
|
static RCryptoPlugin *crypto_static_plugins[] = {
|
|
|
|
R_CRYPTO_STATIC_PLUGINS
|
|
|
|
};
|
2009-09-14 23:45:58 +00:00
|
|
|
|
2022-09-16 09:22:21 +00:00
|
|
|
R_API void r_crypto_init(RCrypto *cry) {
|
|
|
|
r_return_if_fail (cry);
|
2009-09-14 23:45:58 +00:00
|
|
|
int i;
|
2022-09-16 09:22:21 +00:00
|
|
|
cry->user = NULL;
|
2022-10-16 21:37:24 +00:00
|
|
|
cry->plugins = r_list_newf (free);
|
2022-09-16 09:22:21 +00:00
|
|
|
for (i = 0; crypto_static_plugins[i]; i++) {
|
|
|
|
RCryptoPlugin *p = R_NEW0 (RCryptoPlugin);
|
|
|
|
if (p) {
|
|
|
|
memcpy (p, crypto_static_plugins[i], sizeof (RCryptoPlugin));
|
|
|
|
r_crypto_add (cry, p);
|
2009-09-15 11:24:28 +00:00
|
|
|
}
|
2009-09-14 23:45:58 +00:00
|
|
|
}
|
2022-09-16 09:22:21 +00:00
|
|
|
// TODO register hash algorithms
|
2009-09-14 23:45:58 +00:00
|
|
|
}
|
|
|
|
|
2022-09-15 01:25:53 +00:00
|
|
|
R_API bool r_crypto_add(RCrypto *cry, RCryptoPlugin *h) {
|
2022-09-06 23:41:58 +00:00
|
|
|
r_return_val_if_fail (cry && cry->plugins && h, false);
|
2016-03-07 01:38:50 +00:00
|
|
|
r_list_append (cry->plugins, h);
|
2015-09-14 00:08:31 +00:00
|
|
|
return true;
|
2009-09-14 23:45:58 +00:00
|
|
|
}
|
|
|
|
|
2022-09-15 01:25:53 +00:00
|
|
|
R_API bool r_crypto_del(RCrypto *cry, RCryptoPlugin *h) {
|
2022-09-06 23:41:58 +00:00
|
|
|
r_return_val_if_fail (cry && h, false);
|
2016-03-07 01:38:50 +00:00
|
|
|
r_list_delete_data (cry->plugins, h);
|
2015-09-14 00:08:31 +00:00
|
|
|
return true;
|
2009-09-15 11:24:28 +00:00
|
|
|
}
|
2009-09-14 23:45:58 +00:00
|
|
|
|
2022-09-06 23:41:58 +00:00
|
|
|
R_API RCrypto *r_crypto_new(void) {
|
2016-03-07 01:38:50 +00:00
|
|
|
RCrypto *cry = R_NEW0 (RCrypto);
|
2022-09-16 09:22:21 +00:00
|
|
|
r_crypto_init (cry);
|
|
|
|
return cry;
|
2009-09-14 23:45:58 +00:00
|
|
|
}
|
|
|
|
|
2022-09-15 01:25:53 +00:00
|
|
|
R_API void r_crypto_job_free(RCryptoJob *cj) {
|
|
|
|
if (cj) {
|
2022-09-21 17:04:55 +00:00
|
|
|
if (cj->h->fini) {
|
|
|
|
cj->h->fini (cj);
|
|
|
|
}
|
2022-09-15 01:25:53 +00:00
|
|
|
free (cj->output);
|
|
|
|
free (cj->key);
|
|
|
|
free (cj->iv);
|
|
|
|
free (cj);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
R_API void r_crypto_free(RCrypto *cry) {
|
2022-09-06 23:41:58 +00:00
|
|
|
if (cry) {
|
2022-09-15 01:25:53 +00:00
|
|
|
#if 0
|
|
|
|
RListIter *iter;
|
|
|
|
RCryptoPlugin *p;
|
|
|
|
r_list_foreach (cry->plugins, iter, p) {
|
|
|
|
if (p->fini) {
|
|
|
|
p->fini (cry, p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2022-09-06 23:41:58 +00:00
|
|
|
r_list_free (cry->plugins);
|
2022-09-26 16:23:07 +00:00
|
|
|
free (cry);
|
2022-09-06 23:41:58 +00:00
|
|
|
}
|
2009-09-14 23:45:58 +00:00
|
|
|
}
|
|
|
|
|
2022-09-15 01:25:53 +00:00
|
|
|
R_API RCryptoJob *r_crypto_use(RCrypto *cry, const char *algo) {
|
2022-09-06 23:41:58 +00:00
|
|
|
r_return_val_if_fail (cry && algo, false);
|
2022-09-15 01:25:53 +00:00
|
|
|
RListIter *iter, *iter2;
|
2016-03-07 01:38:50 +00:00
|
|
|
RCryptoPlugin *h;
|
|
|
|
r_list_foreach (cry->plugins, iter, h) {
|
2022-09-15 01:25:53 +00:00
|
|
|
if (h && R_STR_ISNOTEMPTY (h->implements)) {
|
|
|
|
char *impls = strdup (h->implements);
|
|
|
|
RList *l = r_str_split_list (impls, ",", 0);
|
|
|
|
const char *s;
|
|
|
|
r_list_foreach (l, iter2, s) {
|
|
|
|
if (!strcmp (s, algo)) {
|
|
|
|
cry->h = h;
|
|
|
|
r_list_free (l);
|
|
|
|
return r_crypto_job_new (cry, h);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
r_list_free (l);
|
|
|
|
free (impls);
|
|
|
|
}
|
|
|
|
// XXX deprecate
|
|
|
|
if (h && h->check && h->check (algo)) {
|
|
|
|
// R_DEPRECATE cry->h = h;
|
|
|
|
return r_crypto_job_new (cry, h);
|
2009-09-14 23:45:58 +00:00
|
|
|
}
|
|
|
|
}
|
2022-09-15 01:25:53 +00:00
|
|
|
return NULL;
|
2009-09-14 23:45:58 +00:00
|
|
|
}
|
|
|
|
|
2022-09-15 01:25:53 +00:00
|
|
|
R_API bool r_crypto_job_set_key(RCryptoJob *cj, const ut8* key, int keylen, int mode, int direction) {
|
|
|
|
r_return_val_if_fail (cj, false);
|
2017-03-25 23:38:57 +00:00
|
|
|
if (keylen < 0) {
|
2016-03-07 01:38:50 +00:00
|
|
|
keylen = strlen ((const char *)key);
|
2017-03-25 23:38:57 +00:00
|
|
|
}
|
2022-09-15 01:25:53 +00:00
|
|
|
if (!cj->h || !cj->h->set_key) {
|
2017-03-25 23:38:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
2022-09-15 01:25:53 +00:00
|
|
|
cj->key_len = keylen;
|
|
|
|
cj->key = calloc (1, cj->key_len);
|
|
|
|
return cj->h->set_key (cj, key, keylen, mode, direction);
|
2009-09-14 23:45:58 +00:00
|
|
|
}
|
|
|
|
|
2022-09-15 01:25:53 +00:00
|
|
|
R_API int r_crypto_job_get_key_size(RCryptoJob *cj) {
|
|
|
|
r_return_val_if_fail (cj, false);
|
|
|
|
return (cj->h && cj->h->get_key_size)?
|
|
|
|
cj->h->get_key_size (cj): 0;
|
2009-09-14 23:45:58 +00:00
|
|
|
}
|
|
|
|
|
2022-09-15 01:25:53 +00:00
|
|
|
R_API bool r_crypto_job_set_iv(RCryptoJob *cj, const ut8 *iv, int ivlen) {
|
|
|
|
r_return_val_if_fail (cj, false);
|
|
|
|
return (cj->h && cj->h->set_iv)?
|
|
|
|
cj->h->set_iv (cj, iv, ivlen): 0;
|
2009-09-14 23:45:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// return the number of bytes written in the output buffer
|
2022-09-15 01:25:53 +00:00
|
|
|
R_API bool r_crypto_job_update(RCryptoJob *cj, const ut8 *buf, int len) {
|
|
|
|
r_return_val_if_fail (cj, 0);
|
|
|
|
return (cj->h && cj->h->update)? cj->h->update (cj, buf, len): 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
R_API RCryptoJob *r_crypto_job_new(RCrypto *cry, RCryptoPlugin *cp) {
|
|
|
|
RCryptoJob *cj = R_NEW0 (RCryptoJob);
|
|
|
|
if (R_UNLIKELY (cj)) {
|
|
|
|
cj->h = cp;
|
|
|
|
cj->c = cry;
|
|
|
|
}
|
|
|
|
return cj;
|
2009-09-14 23:45:58 +00:00
|
|
|
}
|
|
|
|
|
2022-09-15 01:25:53 +00:00
|
|
|
R_API bool r_crypto_job_end(RCryptoJob *cj, const ut8 *buf, int len) {
|
|
|
|
r_return_val_if_fail (cj, 0);
|
|
|
|
return (cj->h && cj->h->end)? cj->h->end (cj, buf, len): 0;
|
2009-09-14 23:45:58 +00:00
|
|
|
}
|
|
|
|
|
2016-03-07 01:38:50 +00:00
|
|
|
// TODO: internal api?? used from plugins? TODO: use r_buf here
|
2022-09-15 01:25:53 +00:00
|
|
|
R_API int r_crypto_job_append(RCryptoJob *cj, const ut8 *buf, int len) {
|
|
|
|
r_return_val_if_fail (cj&& buf, -1);
|
|
|
|
if (cj->output_len+len > cj->output_size) {
|
|
|
|
cj->output_size += 4096 + len;
|
|
|
|
cj->output = realloc (cj->output, cj->output_size);
|
2009-09-14 23:45:58 +00:00
|
|
|
}
|
2022-09-15 01:25:53 +00:00
|
|
|
memcpy (cj->output + cj->output_len, buf, len);
|
|
|
|
cj->output_len += len;
|
|
|
|
return cj->output_len;
|
2009-09-14 23:45:58 +00:00
|
|
|
}
|
2009-03-31 00:50:02 +00:00
|
|
|
|
2022-09-15 01:25:53 +00:00
|
|
|
R_API ut8 *r_crypto_job_get_output(RCryptoJob *cj, int *size) {
|
|
|
|
r_return_val_if_fail (cj, NULL);
|
|
|
|
if (cj->output_size < 1) {
|
2016-11-08 14:09:52 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2022-09-15 01:25:53 +00:00
|
|
|
ut8 *buf = calloc (1, cj->output_size);
|
2016-11-08 14:09:52 +00:00
|
|
|
if (!buf) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-03-07 01:38:50 +00:00
|
|
|
if (size) {
|
2022-09-15 01:25:53 +00:00
|
|
|
*size = cj->output_len;
|
|
|
|
memcpy (buf, cj->output, *size);
|
2016-03-07 01:38:50 +00:00
|
|
|
} else {
|
2022-05-19 15:51:58 +00:00
|
|
|
size_t newlen = 4096;
|
|
|
|
ut8 *newbuf = realloc (buf, newlen);
|
2022-05-19 13:53:30 +00:00
|
|
|
if (!newbuf) {
|
2016-05-24 20:22:15 +00:00
|
|
|
free (buf);
|
|
|
|
return NULL;
|
|
|
|
}
|
2022-05-19 20:14:14 +00:00
|
|
|
buf = newbuf;
|
2022-09-15 01:25:53 +00:00
|
|
|
cj->output = newbuf;
|
|
|
|
cj->output_len = 0;
|
|
|
|
cj->output_size = newlen;
|
2016-03-07 01:38:50 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2009-09-14 23:45:58 +00:00
|
|
|
return buf;
|
2009-03-31 00:50:02 +00:00
|
|
|
}
|
2022-09-21 16:35:36 +00:00
|
|
|
|
|
|
|
R_API void r_crypto_list(RCrypto *cry, PrintfCallback cb_printf, int mode) {
|
|
|
|
if (!cb_printf) {
|
|
|
|
cb_printf = (PrintfCallback)printf;
|
|
|
|
}
|
|
|
|
RListIter *iter;
|
|
|
|
RCryptoPlugin *cp;
|
|
|
|
r_list_foreach (cry->plugins, iter, cp) {
|
2022-09-21 18:06:07 +00:00
|
|
|
switch (mode) {
|
|
|
|
case 'q':
|
|
|
|
cb_printf ("%s\n", cp->name);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
char mode = cp->type? cp->type: 'c';
|
|
|
|
const char *license = cp->license? cp->license: "LGPL";
|
|
|
|
const char *desc = r_str_get (cp->desc);
|
|
|
|
const char *author = r_str_get (cp->author);
|
|
|
|
cb_printf ("%c %12s %5s %s %s\n", mode, cp->name, license, desc, author);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2022-09-21 16:35:36 +00:00
|
|
|
}
|
|
|
|
// TODO: move all those static hashes into crypto plugins
|
|
|
|
int i;
|
2022-09-21 17:18:15 +00:00
|
|
|
for (i = 0; i < 64; i++) {
|
|
|
|
ut64 bits = ((ut64)1) << i;
|
2022-09-21 16:35:36 +00:00
|
|
|
const char *name = r_hash_name (bits);
|
|
|
|
if R_STR_ISEMPTY (name) {
|
2022-09-21 17:18:15 +00:00
|
|
|
continue;
|
2022-09-21 16:35:36 +00:00
|
|
|
}
|
2022-09-21 18:06:07 +00:00
|
|
|
switch (mode) {
|
|
|
|
case 'q':
|
|
|
|
cb_printf ("%s\n", name);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
cb_printf ("h %12s\n", name);
|
|
|
|
break;
|
|
|
|
}
|
2022-09-21 16:35:36 +00:00
|
|
|
}
|
|
|
|
}
|