* Added r_hash_name_to_bits

* Initial implementation of rahash2 with md4 and md5 :)
  - No block bases hashing support yet. Just a POC
* Make asm.vala example work
This commit is contained in:
pancake 2009-04-15 21:19:42 +00:00
parent 29ed9ca8d3
commit 997446e5ab
6 changed files with 137 additions and 12 deletions

View file

@ -54,3 +54,40 @@ u8 r_hash_mod255(const u8 *b, u64 len)
c += b[i];
return c%255;
}
/* TODO: ignore case.. we have to use strcasestr */
u64 r_hash_name_to_bits(const char *name)
{
u64 bits = R_HASH_NONE;
if (strstr(name, "md4"))
bits |= R_HASH_MD4;
if (strstr(name, "md5"))
bits |= R_HASH_MD5;
if (strstr(name, "sha1"))
bits |= R_HASH_SHA1;
if (strstr(name, "sha256"))
bits |= R_HASH_SHA256;
if (strstr(name, "sha384"))
bits |= R_HASH_SHA384;
if (strstr(name, "sha512"))
bits |= R_HASH_SHA512;
if (strstr(name, "crc16"))
bits |= R_HASH_CRC16;
if (strstr(name, "crc32"))
bits |= R_HASH_CRC32;
if (strstr(name, "xorpair"))
bits |= R_HASH_XORPAIR;
else if (strstr(name, "xor")) /* XXX: hacky solution */
bits |= R_HASH_XOR;
if (strstr(name, "parity"))
bits |= R_HASH_PARITY;
if (strstr(name, "entropy"))
bits |= R_HASH_ENTROPY;
if (strstr(name, "hamdist"))
bits |= R_HASH_HAMDIST;
if (strstr(name, "pcprint"))
bits |= R_HASH_PCPRINT;
if (strstr(name, "mod255"))
bits |= R_HASH_MOD255;
return bits;
}

View file

@ -11,4 +11,6 @@ float get_px(u8 x, u8 const *data, u64 size);
u16 crc16(u16 crc, const u8 *buffer, u64 len);
void mdfour(u8 *out, const u8 *in, u64 n);
u32 crc32(u8 *buf, u64 len);
u64 r_hash_name_to_bits(const char *name);
//extern u16 const crc16_table[256];

View file

@ -1,6 +1,6 @@
OBJ=hello.o
BIN=hello
BINDEPS=r_io r_hash
OBJ=rahash2.o
BIN=rahash2
BINDEPS=r_io r_hash r_util
LIBS=-lm
include ../../rules.mk

View file

@ -1,6 +1,79 @@
#include <stdio.h>
#include <string.h>
#include <getopt.h>
#include <r_hash.h>
#include <r_util.h>
static int do_hash(const char *algo, const u8 *buf, int len, int bsize)
{
struct r_hash_t ctx;
const u8 *c;
int i;
u64 algobit = r_hash_name_to_bits (algo);
if (algobit == R_HASH_NONE) {
fprintf(stderr, "Invalid hashing algorithm specified\n");
return 1;
}
if (bsize>len)
bsize = len;
r_hash_state_init(&ctx, R_HASH_ALL);
/* TODO: Loop here for blocks */
if (algobit & R_HASH_MD4) {
c = r_hash_state_md4(&ctx, buf, len);
printf("MD4: ");
for(i=0;i<R_HASH_SIZE_MD4;i++) { printf("%02x", c[i]); }
printf("\n");
}
if (algobit & R_HASH_MD5) {
c = r_hash_state_md5(&ctx, buf, len);
printf("MD5: ");
for(i=0;i<R_HASH_SIZE_MD5;i++) { printf("%02x", c[i]); }
printf("\n");
}
return 0;
}
static int do_help(int line)
{
printf("Usage: rahash2 [-b bsize] [-a algo] [-s str] [file] ...\n");
if (line) return 0;
printf(
" -a algo Hashing algorithm to use (md5, crc32, ...)\n"
" -b bsize Specify the size of the block\n"
" -s string Hash this string instead of files\n");
return 0;
}
int main(int argc, char **argv)
{
fprintf(stderr, "TODO\n");
char *algo = "md5"; /* default hashing algorithm */
u8 *buf = NULL;
int c, buf_len = 0;
int bsize = 0;
while ((c = getopt(argc, argv, "a:s:b:h")) != -1)
{
switch( c ) {
case 'a':
algo = optarg;
break;
case 'b':
bsize = (int)r_num_math(NULL, optarg);
break;
case 's':
buf = optarg;
buf_len = strlen(optarg);
break;
case 'h':
return do_help(1);
}
}
if (buf == NULL) {
do_help(0);
return 1;
}
return do_hash(algo, buf, buf_len, bsize);
}

View file

@ -28,14 +28,16 @@ typedef struct {
unsigned int H[5];
unsigned int W[80];
int lenW;
unsigned int sizeHi,sizeLo;
unsigned int sizeHi, sizeLo;
} SHA_CTX;
#define SHA256_BLOCK_LENGTH 64
typedef struct _SHA256_CTX {
u32 state[8];
u64 bitcount;
u8 buffer[SHA256_BLOCK_LENGTH];
} SHA256_CTX;
#define SHA384_BLOCK_LENGTH 128
#define SHA512_BLOCK_LENGTH 128
typedef struct _SHA512_CTX {
@ -62,12 +64,23 @@ struct r_hash_t {
#define R_HASH_SIZE_SHA384 64
#define R_HASH_SIZE_SHA512 64
#define R_HASH_ALL 0
#define R_HASH_NONE 0
#define R_HASH_MD5 1
#define R_HASH_SHA1 2
#define R_HASH_SHA256 4
#define R_HASH_SHA384 8
#define R_HASH_SHA512 16
#define R_HASH_CRC16 32
#define R_HASH_CRC32 64
#define R_HASH_MD4 128
#define R_HASH_XOR 256
#define R_HASH_XORPAIR 512
#define R_HASH_PARITY 1024
#define R_HASH_ENTROPY 2048
#define R_HASH_HAMDIST 4096
#define R_HASH_PCPRINT 8192
#define R_HASH_MOD255 16384
#define R_HASH_ALL 0xFFFF
const u8 *r_hash_state_md4(struct r_hash_t *ctx, const u8 *input, u32 len);
const u8 *r_hash_state_md5(struct r_hash_t *ctx, const u8 *input, u32 len);

View file

@ -6,10 +6,8 @@ public class AsmExample
{
public static void main(string[] args)
{
string pseudo = "";
Asm st = new Asm();
//st.set_arch(Asm.Arch.X86);
st.set("asm_x86");
st.set("asm_x86_olly");
st.set_syntax(Asm.Syntax.INTEL);
st.set_bits(32);
st.set_big_endian(false);
@ -24,8 +22,10 @@ public class AsmExample
Asm.Aop op;
uint8 *buf = "\x83\xe4\xf0";
st.disassemble(out op, buf, 3);
// st.parse();
stdout.printf("asm: %s\n", op.buf_asm);
if (st.disassemble(out op, buf, 3) <1) {
stderr.printf("internal error\n");
} else {
stdout.printf("asm: %s\n", op.buf_asm);
}
}
}