Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added AES-128/192/256 with ECB mode to rahash2 #4402

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 80 additions & 4 deletions libr/crypto/p/crypto_aes.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,86 @@
/* */
#include <r_lib.h>
#include <r_crypto.h>
#include "crypto_aes_algo.h"

struct r_crypto_plugin_t r_crypto_plugin_aes = {
.name = "aes",
/* TODO */
static struct aes_state st;

static int aes_set_key (RCrypto *cry, const ut8 *key, int keylen, int mode, int direction) {
if (!(keylen == 128 / 8 || keylen == 192 / 8 || keylen == 256 / 8))
return false;
st.key_size = keylen;
st.rounds = 6 + (int)(keylen / 4);
st.columns = (int)(keylen / 4);
memcpy(st.key, key, keylen);

// printf("*** State:\n \
// Key: %s\n \
// Received keylen: %d\t\tkey_size: %d\n \
// columns: %d\n \
// rounds: %d\n \
// Finished!\n", st.key, keylen, st.key_size, st.columns, st.rounds);
return true;
}

static int aes_get_key_size (RCrypto *cry) {
return st.key_size;
}

static bool aes_use (const char *algo) {
return !strcmp (algo, "aes-ecb");
}

#define BLOCK_SIZE 16

static int update (RCrypto *cry, const ut8 *buf, int len) {
// Pad to the block size, do not append dummy block
const int diff = (BLOCK_SIZE - (len % BLOCK_SIZE)) % BLOCK_SIZE;
const int size = len + diff;
const int blocks = size / st.key_size;

ut8 *const obuf = calloc (1, size);
if (!obuf) return false;

ut8 *const ibuf = calloc (1, size);
if (!ibuf) return false;

memset(ibuf, 0, size);
memcpy (ibuf, buf, len);
// Padding should start like 100000...
if (diff) {
ibuf[len] = 0b1000;
}

// printf("*** State:\n \
// Key: %s\n \
// key_size: %d\n \
// columns: %d\n \
// rounds: %d\n", st.key, st.key_size, st.columns, st.rounds);
int i;
for (i = 0; i < blocks; i++) {
// printf("Block: %d\n", i);
aes_encrypt (&st, ibuf + BLOCK_SIZE * i, obuf + BLOCK_SIZE * i);
// printf("Block finished: %d\n", i);
}

// printf("%128s\n", obuf);

r_crypto_append (cry, obuf, size);
free (obuf);
free (ibuf);
return 0;
}

static int final (RCrypto *cry, const ut8 *buf, int len) {
return update (cry, buf, len);
}

RCryptoPlugin r_crypto_plugin_aes = {
.name = "aes-ecb",
.set_key = aes_set_key,
.get_key_size = aes_get_key_size,
.use = aes_use,
.update = update,
.final = final
};

#ifndef CORELIB
Expand Down
Loading