/
crypto_ror.c
78 lines (66 loc) · 1.6 KB
/
crypto_ror.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <r_lib.h>
#include <r_crypto.h>
#define MAX_ror_KEY_SIZE 32768
struct ror_state {
ut8 key[MAX_ror_KEY_SIZE];
int key_size;
};
static bool ror_init(struct ror_state *const state, const ut8 *key, int keylen) {
if (!state || !key || keylen < 1 || keylen > MAX_ror_KEY_SIZE) {
return false;
}
int i;
state->key_size = keylen;
for (i = 0; i < keylen; i++) {
state->key[i] = key[i];
}
return true;
}
static void ror_crypt(struct ror_state *const state, const ut8 *inbuf, ut8 *outbuf, int buflen) {
int i;
for (i = 0; i < buflen; i++) {
outbuf[i] = inbuf[i] >> state->key[i%state->key_size];
}
}
static struct ror_state st;
static int flag = 0;
static bool ror_set_key(RCrypto *cry, const ut8 *key, int keylen, int mode, int direction) {
flag = direction;
return ror_init (&st, key, keylen);
}
static int ror_get_key_size(RCrypto *cry) {
return st.key_size;
}
static bool ror_use(const char *algo) {
return !strcmp (algo, "ror");
}
static bool update(RCrypto *cry, const ut8 *buf, int len) {
if (flag) {
eprintf ("USE ROL\n");
return false;
}
ut8 *obuf = calloc (1, len);
if (!obuf) return false;
ror_crypt (&st, buf, obuf, len);
r_crypto_append (cry, obuf, len);
free (obuf);
return true;
}
static bool final(RCrypto *cry, const ut8 *buf, int len) {
return update (cry, buf, len);
}
RCryptoPlugin r_crypto_plugin_ror = {
.name = "ror",
.set_key = ror_set_key,
.get_key_size = ror_get_key_size,
.use = ror_use,
.update = update,
.final = final
};
#ifndef CORELIB
RLibStruct radare_plugin = {
.type = R_LIB_TYPE_CRYPTO,
.data = &r_crypto_plugin_ror,
.version = R2_VERSION
};
#endif