mirror of
https://github.com/radareorg/radare2.git
synced 2024-10-08 19:33:31 +00:00
Add crypto plugin for base64 and base91
update error occurence.
This commit is contained in:
parent
d7b27e40f0
commit
27fba6fa10
@ -289,10 +289,11 @@ int is_power_of_two(const ut64 x) {
|
||||
}
|
||||
|
||||
int encrypt_or_decrypt (const char *algo, bool to_encrypt, const char *hashstr, int hashstr_len) {
|
||||
if (s.len > 0) {
|
||||
bool no_key_mode = !strcmp ("base64", algo) || !strcmp ("base91", algo); //TODO: generalise this for all non key encoding/decoding.
|
||||
if (no_key_mode || s.len > 0) {
|
||||
RCrypto *cry = r_crypto_new ();
|
||||
if (r_crypto_use (cry, algo)) {
|
||||
if (r_crypto_set_key (cry, s.buf, s.len, 0, 0)) {
|
||||
if (no_key_mode || r_crypto_set_key (cry, s.buf, s.len, 0, 0)) {
|
||||
const char *buf = hashstr;
|
||||
int buflen = hashstr_len;
|
||||
|
||||
@ -320,10 +321,11 @@ int encrypt_or_decrypt (const char *algo, bool to_encrypt, const char *hashstr,
|
||||
}
|
||||
|
||||
int encrypt_or_decrypt_file (const char *algo, bool to_encrypt, char *filename) {
|
||||
if (s.len > 0) {
|
||||
bool no_key_mode = !strcmp ("base64", algo) || !strcmp ("base91", algo); //TODO: generalise this for all non key encoding/decoding.
|
||||
if (no_key_mode || s.len > 0) {
|
||||
RCrypto *cry = r_crypto_new ();
|
||||
if (r_crypto_use (cry, algo)) {
|
||||
if (r_crypto_set_key (cry, s.buf, s.len, 0, 0)) {
|
||||
if (no_key_mode || r_crypto_set_key (cry, s.buf, s.len, 0, 0)) {
|
||||
int file_size;
|
||||
ut8 *buf = (ut8*)r_file_slurp (filename, &file_size);
|
||||
if (!buf) {
|
||||
@ -413,8 +415,14 @@ int main(int argc, char **argv) {
|
||||
eprintf ("rahash2: Option -c incompatible with -b and -B options.\n");
|
||||
return 1;
|
||||
}
|
||||
if ((decrypt && !strcmp (decrypt, "b64")) || (encrypt && !strcmp (encrypt, "b64"))) {
|
||||
eprintf ("rahash2: Option -c incompatible with -E b64 or -D b64 options.\n");
|
||||
bool flag = false;
|
||||
if (encrypt) {
|
||||
flag = !strcmp (encrypt, "base64") || !strcmp (encrypt, "base91");
|
||||
} else if (decrypt) {
|
||||
flag = !strcmp (decrypt, "base64") || !strcmp (decrypt, "base91");
|
||||
}
|
||||
if (flag) {
|
||||
eprintf ("rahash2: Option -c incompatible with -E base64, -E base91, -D base64 or -D base91 options.\n");
|
||||
return 1;
|
||||
}
|
||||
algobit = r_hash_name_to_bits(algo);
|
||||
@ -499,31 +507,9 @@ int main(int argc, char **argv) {
|
||||
hashstr_len = r_str_unescape (hashstr);
|
||||
}
|
||||
if (encrypt) {
|
||||
if (!strcmp (encrypt, "b64")) {
|
||||
char *out = malloc ((((hashstr_len + 1) * 4) / 3) + 1);
|
||||
if (out) {
|
||||
r_base64_encode (out, (const ut8*)hashstr, hashstr_len);
|
||||
printf ("%s\n", out);
|
||||
fflush (stdout);
|
||||
free (out);
|
||||
}
|
||||
return ret;
|
||||
} else {
|
||||
return encrypt_or_decrypt (encrypt, true, hashstr, hashstr_len);
|
||||
}
|
||||
return encrypt_or_decrypt (encrypt, true, hashstr, hashstr_len);
|
||||
} else if (decrypt) {
|
||||
if (!strcmp (decrypt, "b64")) {
|
||||
ut8 *out = malloc (INSIZE);
|
||||
if (out) {
|
||||
int outlen = r_base64_decode (out,
|
||||
(const char *)hashstr, hashstr_len);
|
||||
write (1, out, outlen);
|
||||
free (out);
|
||||
}
|
||||
return ret;
|
||||
} else {
|
||||
return encrypt_or_decrypt (decrypt, false, hashstr, hashstr_len);
|
||||
}
|
||||
return encrypt_or_decrypt (decrypt, false, hashstr, hashstr_len);
|
||||
} else {
|
||||
char *str = (char *)hashstr;
|
||||
int strsz = hashstr_len;
|
||||
@ -573,47 +559,13 @@ int main(int argc, char **argv) {
|
||||
io = r_io_new ();
|
||||
for (ret = 0, i = optind; i < argc; i++) {
|
||||
if (encrypt) {//for encrytion when files are provided
|
||||
if (!strcmp (encrypt, "b64")) {
|
||||
int binlen;
|
||||
char *out;
|
||||
ut8 *bin = (ut8*)r_file_slurp (argv[i], &binlen);
|
||||
if (!bin) {
|
||||
eprintf ("Cannot open file\n");
|
||||
continue;
|
||||
}
|
||||
out = malloc (((binlen + 1) * 4) / 3);
|
||||
if (out) {
|
||||
r_base64_encode (out, bin, binlen);
|
||||
printf ("%s\n", out);
|
||||
fflush (stdout);
|
||||
free (out);
|
||||
}
|
||||
free (bin);
|
||||
} else {
|
||||
int rt = encrypt_or_decrypt_file (encrypt, true, argv[1]);
|
||||
if (rt == -1) continue;
|
||||
else return rt;
|
||||
}
|
||||
int rt = encrypt_or_decrypt_file (encrypt, true, argv[i]);
|
||||
if (rt == -1) continue;
|
||||
else return rt;
|
||||
} else if (decrypt) {
|
||||
if (!strcmp (decrypt, "b64")) {
|
||||
int binlen, outlen;
|
||||
ut8 *out, *bin = (ut8*)r_file_slurp (argv[i], &binlen);
|
||||
if (!bin) {
|
||||
eprintf ("Cannot open file\n");
|
||||
continue;
|
||||
}
|
||||
out = malloc (binlen + 1);
|
||||
if (out) {
|
||||
outlen = r_base64_decode (out, (const char*)bin, binlen);
|
||||
write (1, out, outlen);
|
||||
free (out);
|
||||
}
|
||||
free (bin);
|
||||
} else {
|
||||
int rt = encrypt_or_decrypt_file (decrypt, false, argv[1]);
|
||||
if (rt == -1) continue;
|
||||
else return rt;
|
||||
}
|
||||
int rt = encrypt_or_decrypt_file (decrypt, false, argv[i]);
|
||||
if (rt == -1) continue;
|
||||
else return rt;
|
||||
} else {
|
||||
if (!strcmp (argv[i], "-")) {
|
||||
int sz = 0;
|
||||
|
@ -21,20 +21,22 @@ R_API int cmd_write_hexpair(RCore* core, const char* pairs) {
|
||||
}
|
||||
|
||||
static bool encrypt_or_decrypt_block(RCore *core, const char *algo, const char *key, bool to_encrypt) {
|
||||
int keylen = key? strlen (key): 0;
|
||||
if (keylen > 0) {
|
||||
//TODO: generalise no_key_mode for all non key encoding/decoding.
|
||||
int keylen = key ? strlen (key): 0;
|
||||
bool no_key_mode = !strcmp ("base64", algo) || !strcmp ("base91", algo);
|
||||
if (no_key_mode || keylen > 0) {
|
||||
RCrypto *cry = r_crypto_new ();
|
||||
if (r_crypto_use (cry, algo)) {
|
||||
ut8 *binkey = malloc (keylen + 1);
|
||||
if (binkey) {
|
||||
int len = r_hex_str2bin (key, binkey);
|
||||
int len = no_key_mode ? 1 : r_hex_str2bin (key, binkey);
|
||||
if (len < 1) {
|
||||
len = keylen;
|
||||
strcpy ((char *)binkey, key);
|
||||
} else {
|
||||
keylen = len;
|
||||
}
|
||||
if (r_crypto_set_key (cry, binkey, keylen, 0, 0)) {
|
||||
if (no_key_mode || r_crypto_set_key (cry, binkey, keylen, 0, 0)) {
|
||||
r_crypto_update (cry, (const ut8*)core->block, core->blocksize, to_encrypt);
|
||||
r_crypto_final (cry, NULL, 0, to_encrypt);
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
OBJS = crypto.c ;
|
||||
OBJS += p/crypto_aes.c p/crypto_aes_algo.c p/crypto_xor.c p/crypto_blowfish.c p/crypto_rc2.c p/crypto_rot.c p/crypto_rol.c p/crypto_ror.c;
|
||||
OBJS += p/crypto_aes.c p/crypto_aes_algo.c p/crypto_xor.c p/crypto_blowfish.c p/crypto_rc2.c p/crypto_rot.c p/crypto_rol.c p/crypto_ror.c p/crypto_base64.c p/crypto_base91.c;
|
||||
|
||||
lib r_crypto : $(OBJS) : <include>../include <library>../util ;
|
||||
|
@ -72,9 +72,11 @@ R_API bool r_crypto_use(RCrypto *cry, const char *algo) {
|
||||
r_list_foreach (cry->plugins, iter, h) {
|
||||
if (h && h->use && h->use (algo)) {
|
||||
cry->h = h;
|
||||
cry->key_len = h->get_key_size (cry);
|
||||
cry->key = calloc (1, cry->key_len);
|
||||
return cry->key != NULL;
|
||||
if (h->get_key_size) { //should i change this or make base64/base91 return 0 for get_key_size?
|
||||
cry->key_len = h->get_key_size (cry);
|
||||
cry->key = calloc (1, cry->key_len);
|
||||
}
|
||||
return (h->get_key_size) ? (cry->key != NULL) : true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
@ -137,4 +139,3 @@ R_API ut8 *r_crypto_get_output(RCrypto *cry, int *size) {
|
||||
}
|
||||
return buf;
|
||||
}
|
||||
|
||||
|
9
libr/crypto/p/base64.mk
Normal file
9
libr/crypto/p/base64.mk
Normal file
@ -0,0 +1,9 @@
|
||||
OBJ_B64=crypto_base64.o
|
||||
|
||||
STATIC_OBJ+=${OBJ_B64}
|
||||
TARGET_B64=crypto_base64.${EXT_SO}
|
||||
|
||||
ALL_TARGETS+=${TARGET_B64}
|
||||
|
||||
${TARGET_B64}: ${OBJ_B64}
|
||||
${CC} $(call libname,crypto_base64) ${LDFLAGS} ${CFLAGS} -o ${TARGET_B64} ${OBJ_B64}
|
9
libr/crypto/p/base91.mk
Normal file
9
libr/crypto/p/base91.mk
Normal file
@ -0,0 +1,9 @@
|
||||
OBJ_B91=crypto_base91.o
|
||||
|
||||
STATIC_OBJ+=${OBJ_B91}
|
||||
TARGET_B91=crypto_base91.${EXT_SO}
|
||||
|
||||
ALL_TARGETS+=${TARGET_B91}
|
||||
|
||||
${TARGET_B91}: ${OBJ_B91}
|
||||
${CC} $(call libname,crypto_base91) ${LDFLAGS} ${CFLAGS} -o ${TARGET_B91} ${OBJ_B91}
|
45
libr/crypto/p/crypto_base64.c
Normal file
45
libr/crypto/p/crypto_base64.c
Normal file
@ -0,0 +1,45 @@
|
||||
#include <r_lib.h>
|
||||
#include <r_crypto.h>
|
||||
#include <r_util.h>
|
||||
|
||||
static bool base64_use(const char *algo) {
|
||||
return !strcmp (algo, "base64");
|
||||
}
|
||||
|
||||
static int update(RCrypto *cry, const ut8 *buf, int len, bool to_encode) {
|
||||
int olen;
|
||||
ut8 *obuf;
|
||||
if (to_encode) {
|
||||
olen = ((len + 2) / 3 ) * 4;
|
||||
obuf = malloc (olen + 1);
|
||||
r_base64_encode (obuf, buf, len);
|
||||
} else {
|
||||
olen = (len / 4) * 3;
|
||||
if (len > 0) //to prevent invalid access of memory
|
||||
olen -= (buf[len-1] == '=') ? ((buf[len-2] == '=') ? 2 : 1) : 0;
|
||||
obuf = malloc (olen + 1);
|
||||
olen = r_base64_decode (obuf, buf, len);
|
||||
}
|
||||
r_crypto_append (cry, obuf, olen);
|
||||
free (obuf);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int final(RCrypto *cry, const ut8 *buf, int len, bool to_encode) {
|
||||
return update (cry, buf, len, to_encode);
|
||||
}
|
||||
|
||||
RCryptoPlugin r_crypto_plugin_base64 = {
|
||||
.name = "base64",
|
||||
.use = base64_use,
|
||||
.update = update,
|
||||
.final = final
|
||||
};
|
||||
|
||||
#ifndef CORELIB
|
||||
struct r_lib_struct_t radare_plugin = {
|
||||
.type = R_LIB_TYPE_CRYPTO,
|
||||
.data = &r_crypto_plugin_base64,
|
||||
.version = R2_VERSION
|
||||
};
|
||||
#endif
|
41
libr/crypto/p/crypto_base91.c
Normal file
41
libr/crypto/p/crypto_base91.c
Normal file
@ -0,0 +1,41 @@
|
||||
#include <r_lib.h>
|
||||
#include <r_crypto.h>
|
||||
#include <r_util.h>
|
||||
|
||||
#define INSIZE 32768
|
||||
|
||||
static bool base91_use(const char *algo) {
|
||||
return !strcmp (algo, "base91");
|
||||
}
|
||||
|
||||
static int update(RCrypto *cry, const ut8 *buf, int len, bool to_encode) {
|
||||
int olen = INSIZE; //a way to optimise memory allocation.
|
||||
ut8 *obuf = malloc (olen);
|
||||
if (to_encode) {
|
||||
olen = r_base91_encode (obuf, buf, len);
|
||||
} else {
|
||||
olen = r_base91_decode (obuf, buf, len);
|
||||
}
|
||||
r_crypto_append (cry, obuf, olen);
|
||||
free (obuf);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int final(RCrypto *cry, const ut8 *buf, int len, bool to_encode) {
|
||||
return update (cry, buf, len, to_encode);
|
||||
}
|
||||
|
||||
RCryptoPlugin r_crypto_plugin_base91 = {
|
||||
.name = "base91",
|
||||
.use = base91_use,
|
||||
.update = update,
|
||||
.final = final
|
||||
};
|
||||
|
||||
#ifndef CORELIB
|
||||
struct r_lib_struct_t radare_plugin = {
|
||||
.type = R_LIB_TYPE_CRYPTO,
|
||||
.data = &r_crypto_plugin_base91,
|
||||
.version = R2_VERSION
|
||||
};
|
||||
#endif
|
@ -69,6 +69,8 @@ extern RCryptoPlugin r_crypto_plugin_rc2;
|
||||
extern RCryptoPlugin r_crypto_plugin_rot;
|
||||
extern RCryptoPlugin r_crypto_plugin_rol;
|
||||
extern RCryptoPlugin r_crypto_plugin_ror;
|
||||
extern RCryptoPlugin r_crypto_plugin_base64;
|
||||
extern RCryptoPlugin r_crypto_plugin_base91;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
@ -479,6 +479,9 @@ R_API int r_base64_decode(ut8 *bout, const char *bin, int len);
|
||||
R_API ut8 *r_base64_decode_dyn(const char *in, int len);
|
||||
R_API char *r_base64_encode_dyn(const char *str, int len);
|
||||
|
||||
R_API int r_base91_encode(char *bout, const ut8 *bin, int len);
|
||||
R_API int r_base91_decode(ut8 *bout, const char *bin, int len);
|
||||
|
||||
/* strings */
|
||||
static inline void r_str_rmch (char *s, char ch) {
|
||||
for (;*s; s++) {
|
||||
|
@ -4,7 +4,7 @@ NAME=r_util
|
||||
DEPS=
|
||||
|
||||
OBJS=mem.o pool.o num.o str.o hex.o file.o range.o
|
||||
OBJS+=prof.o cache.o sys.o buf.o w32-sys.o base64.o base85.o
|
||||
OBJS+=prof.o cache.o sys.o buf.o w32-sys.o base64.o base85.o base91.o
|
||||
OBJS+=list.o flist.o ht.o ht64.o mixed.o btree.o chmod.o graph.o
|
||||
OBJS+=regex/regcomp.o regex/regerror.o regex/regexec.o uleb128.o
|
||||
OBJS+=sandbox.o calc.o thread.o thread_lock.o thread_msg.o
|
||||
|
93
libr/util/base91.c
Normal file
93
libr/util/base91.c
Normal file
@ -0,0 +1,93 @@
|
||||
#include <string.h>
|
||||
|
||||
#include <r_util.h>
|
||||
|
||||
static const char b91[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
|
||||
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
|
||||
'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
|
||||
'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
|
||||
'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
|
||||
'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7',
|
||||
'8', '9', '!', '#', '$', '%', '&', '(', ')', '*',
|
||||
'+', ',', '.', '/', ':', ';', '<', '=', '>', '?',
|
||||
'@', '[', ']', '^', '_', '`', '{', '|', '}', '~', '"'};
|
||||
|
||||
int get_char_index(const char c) {
|
||||
int i;
|
||||
for (i = 0; i < 91; i++ ) {
|
||||
if (b91[i] == c)
|
||||
return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
R_API int r_base91_decode(ut8* bout, const char *bin, int len) {
|
||||
int in, out;
|
||||
int v = -1;
|
||||
int b = 0;
|
||||
int n = 0;
|
||||
int c;
|
||||
if (len < 0)
|
||||
len = strlen (bin);
|
||||
for (in = out = 0; in < len; in++) {
|
||||
c = get_char_index(bin[in]);
|
||||
if (c == -1)
|
||||
continue;
|
||||
if (v < 0) {
|
||||
v = c;
|
||||
} else {
|
||||
v += c * 91;
|
||||
b |= (v << n);
|
||||
if ((v&8191) > 88) {
|
||||
n += 13;
|
||||
} else {
|
||||
n += 14;
|
||||
}
|
||||
while (true) {
|
||||
bout[out++] = b & 255;
|
||||
b >>= 8;
|
||||
n -= 8;
|
||||
if (n <= 7)
|
||||
break;
|
||||
}
|
||||
v = -1;
|
||||
}
|
||||
}
|
||||
if (v+1) {
|
||||
bout[out++] = (b | v << n) & 255;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
R_API int r_base91_encode(char *bout, const ut8 *bin, int len) {
|
||||
int in, out;
|
||||
int v = 0;
|
||||
int b = 0;
|
||||
int n = 0;
|
||||
if (len < 0)
|
||||
len = strlen ((const char *)bin);
|
||||
for (in = out = 0; in < len; in++) {
|
||||
b |= (bin[in] << n);
|
||||
n += 8;
|
||||
if (n > 13) {
|
||||
v = b & 8191;
|
||||
if (v > 88) {
|
||||
b >>= 13;
|
||||
n -= 13;
|
||||
} else {
|
||||
v = b & 16383;
|
||||
b >>= 14;
|
||||
n -= 14;
|
||||
}
|
||||
bout[out++] = b91[v % 91];
|
||||
bout[out++] = b91[v / 91];
|
||||
}
|
||||
}
|
||||
if (n) {
|
||||
bout[out++] = b91[b % 91];
|
||||
if (n > 7 || b > 90) {
|
||||
bout[out++] = b91[b / 91];
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
@ -156,6 +156,8 @@ crypto.rc2
|
||||
crypto.rot
|
||||
crypto.rol
|
||||
crypto.ror
|
||||
crypto.base64
|
||||
crypto.base91
|
||||
debug.bf
|
||||
debug.esil
|
||||
debug.gdb
|
||||
|
Loading…
Reference in New Issue
Block a user