refactor the code and fix rc2 ecb implementation

Cleanup in cmd_write.
This commit is contained in:
Rakholiya Jenish 2016-04-13 15:53:35 +05:30 committed by pancake
parent 27fba6fa10
commit cfe1cc8c6c
6 changed files with 56 additions and 39 deletions

View File

@ -288,12 +288,12 @@ int is_power_of_two(const ut64 x) {
return x && !(x & (x - 1));
}
int encrypt_or_decrypt (const char *algo, bool to_encrypt, const char *hashstr, int hashstr_len) {
int encrypt_or_decrypt(const char *algo, bool to_encrypt, const char *hashstr, int hashstr_len) {
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 (no_key_mode || r_crypto_set_key (cry, s.buf, s.len, 0, 0)) {
if (r_crypto_set_key (cry, s.buf, s.len, 0, 0)) {
const char *buf = hashstr;
int buflen = hashstr_len;
@ -325,7 +325,7 @@ int encrypt_or_decrypt_file (const char *algo, bool to_encrypt, char *filename)
if (no_key_mode || s.len > 0) {
RCrypto *cry = r_crypto_new ();
if (r_crypto_use (cry, algo)) {
if (no_key_mode || r_crypto_set_key (cry, s.buf, s.len, 0, 0)) {
if (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) {

View File

@ -36,7 +36,7 @@ static bool encrypt_or_decrypt_block(RCore *core, const char *algo, const char *
} else {
keylen = len;
}
if (no_key_mode || r_crypto_set_key (cry, binkey, keylen, 0, 0)) {
if (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);

View File

@ -72,11 +72,9 @@ 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;
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;
cry->key_len = h->get_key_size (cry);
cry->key = calloc (1, cry->key_len);
return cry->key != NULL;
}
}
return false;

View File

@ -2,6 +2,14 @@
#include <r_crypto.h>
#include <r_util.h>
static int base64_set_key(RCrypto *cry, const ut8 *key, int keylen, int mode, int direction) {
return true;
}
static int base64_get_key_size(RCrypto *cry) {
return 0;
}
static bool base64_use(const char *algo) {
return !strcmp (algo, "base64");
}
@ -31,6 +39,8 @@ static int final(RCrypto *cry, const ut8 *buf, int len, bool to_encode) {
RCryptoPlugin r_crypto_plugin_base64 = {
.name = "base64",
.set_key = base64_set_key,
.get_key_size = base64_get_key_size,
.use = base64_use,
.update = update,
.final = final

View File

@ -4,6 +4,14 @@
#define INSIZE 32768
static int base91_set_key(RCrypto *cry, const ut8 *key, int keylen, int mode, int direction) {
return true;
}
static int base91_get_key_size(RCrypto *cry) {
return 0;
}
static bool base91_use(const char *algo) {
return !strcmp (algo, "base91");
}
@ -27,6 +35,8 @@ static int final(RCrypto *cry, const ut8 *buf, int len, bool to_encode) {
RCryptoPlugin r_crypto_plugin_base91 = {
.name = "base91",
.set_key = base91_set_key,
.get_key_size = base91_get_key_size,
.use = base91_use,
.update = update,
.final = final

View File

@ -31,34 +31,29 @@ struct rc2_state {
// takes a 8-128 len ut8 key
// expands it to a 64 len ut16 key
static bool rc2_expandKey(const ut8 *key,
int key_len, // len(key)
int bits, // the effective key len in bits
struct rc2_state *state) {
static bool rc2_expandKey(struct rc2_state *state, const ut8 *key, int key_len) {
int i;
char tempKey[129];
if (key_len < 1) return false;
strncpy(tempKey, (char *) key, sizeof(tempKey) - 1);
if (key_len < 1 || key_len > 128) return false;
memcpy(state->ekey, key, key_len);
// first loop
for (i = key_len; i < sizeof(tempKey) - 1; i++) {
tempKey[i] = PITABLE[(key[i - key_len] + key[i - 1]) & 255];
for (i = key_len; i < 128; i++) {
((ut8 *)state->ekey)[i] = PITABLE[(((ut8 *)state->ekey)[i - key_len] + ((ut8 *)state->ekey)[i - 1]) & 255];
}
int ekey_len = (bits + 7) >> 3; // in bytes
int mask = 255 >> (8 * ekey_len - bits);
tempKey[sizeof(tempKey) - 1 - ekey_len] = PITABLE[tempKey[sizeof(tempKey) - 1 - ekey_len] & mask];
int ekey_len = (BITS + 7) >> 3;
i = 128 - ekey_len;
((ut8 *)state->ekey)[i] = PITABLE[((ut8 *)state->ekey)[i] & (255 >> (7 & -BITS))];
// second loop
for (i = sizeof(tempKey) - 2 - ekey_len; i >= 0; i--) {
tempKey[i] = PITABLE[tempKey[i + 1] ^ tempKey[i + ekey_len]];
while (i--) {
((ut8 *)state->ekey)[i] = PITABLE[((ut8 *)state->ekey)[i + 1] ^ ((ut8 *)state->ekey)[i + ekey_len]];
}
// generate the ut16 key
for (i = 0; i < RC2_KEY_SIZE; i++) {
state->ekey[i] = (ut8)tempKey[i * 2] + ((ut8)tempKey[i * 2 + 1] << 8);
for (i = RC2_KEY_SIZE - 1; i >= 0; i--) {
state->ekey[i] = ((ut8 *)state->ekey)[i * 2] + (((ut8 *)state->ekey)[i * 2 + 1] << 8);
}
return true;
@ -74,17 +69,17 @@ static void rc2_crypt8(struct rc2_state *state, const ut8 *inbuf, ut8 *outbuf) {
x10 = (inbuf[1] << 8) | inbuf[0];
for (i = 0; i < 16; i++) {
x10 += ((x32 & ~x76) | (x54 & x76)) + state->ekey[4 * i + 0];
x10 = (x10 << 1) | (x10 >> 15 & 1);
x10 += ((x32 & ~x76) + (x54 & x76)) + state->ekey[4 * i + 0];
x10 = (x10 << 1) + (x10 >> 15 & 1);
x32 += ((x54 & ~x10) | (x76 & x10)) + state->ekey[4 * i + 1];
x32 = (x32 << 2) | (x32 >> 14 & 3);
x32 += ((x54 & ~x10) + (x76 & x10)) + state->ekey[4 * i + 1];
x32 = (x32 << 2) + (x32 >> 14 & 3);
x54 += ((x76 & ~x32) | (x10 & x32)) + state->ekey[4 * i + 2];
x54 = (x54 << 3) | (x54 >> 13 & 7);
x54 += ((x76 & ~x32) + (x10 & x32)) + state->ekey[4 * i + 2];
x54 = (x54 << 3) + (x54 >> 13 & 7);
x76 += ((x10 & ~x54) | (x32 & x54)) + state->ekey[4 * i + 3];
x76 = (x76 << 5) | (x76 >> 11 & 31);
x76 += ((x10 & ~x54) + (x32 & x54)) + state->ekey[4 * i + 3];
x76 = (x76 << 5) + (x76 >> 11 & 31);
if (i == 4 || i == 10) {
x10 += state->ekey[x76 & 63];
@ -115,15 +110,19 @@ static void rc2_dcrypt8(struct rc2_state *state, const ut8 *inbuf, ut8 *outbuf)
x10 = (inbuf[1] << 8) | inbuf[0];
for (i = 15; i >= 0; i--) {
x76 &= 65535;
x76 = (x76 << 11) | (x76 >> 5);
x76 -= ((x10 & ~x54) | (x32 & x54)) + state->ekey[4 * i + 3];
x76 &= 65535;
x54 = (x54 << 13) | (x54 >> 3);
x54 -= ((x76 & ~x32) | (x10 & x32)) + state->ekey[4 * i + 2];
x32 &= 65535;
x32 = (x32 << 14) | (x32 >> 2);
x32 -= ((x54 & ~x10) | (x76 & x10)) + state->ekey[4 * i + 1];
x10 &= 65535;
x10 = (x10 << 15) | (x10 >> 1);
x10 -= ((x32 & ~x76) | (x54 & x76)) + state->ekey[4 * i + 0];
@ -196,8 +195,8 @@ static void rc2_crypt(struct rc2_state *state, const ut8 *inbuf, ut8 *outbuf, in
static struct rc2_state state;
static int rc2_set_key(RCrypto *cry, const ut8 *key, int keylen, int mode, int direction) {
state.key_size = keylen;
return rc2_expandKey(key, keylen, BITS, &state);
state.key_size = 1024;
return rc2_expandKey(&state, key, keylen);
}
static int rc2_get_key_size(RCrypto *cry) {