Initial import of rahash2 '-S' seed and '-i' iterations

This commit is contained in:
pancake 2013-11-02 11:49:32 +01:00
parent 5879f30297
commit cbecfb8f4a
4 changed files with 140 additions and 30 deletions

View File

@ -12,7 +12,33 @@
static ut64 from = 0LL;
static ut64 to = 0LL;
static int incremental = 1;
static int iterations = 0;
static int quiet = 0;
static RHashSeed s = {0}, *_s = NULL;
static void do_hash_seed(const char *seed) {
const char *sptr = seed;
if (!seed) {
_s = NULL;
return;
}
_s = &s;
s.buf = (ut8*)malloc (strlen (seed)+128);
if (*seed=='^') {
s.prefix = 1;
sptr++;
} else s.prefix = 0;
if (!strncmp (sptr, "s:", 2)) {
strcpy ((char*)s.buf, sptr+2);
s.len = strlen (sptr+2);
} else {
s.len = r_hex_str2bin (sptr, s.buf);
if (s.len<1) {
strcpy ((char*)s.buf, sptr);
s.len = strlen (sptr);
}
}
}
static void do_hash_print(RHash *ctx, int hash, int dlen, int rad) {
int i;
@ -56,10 +82,15 @@ static int do_hash_internal(RHash *ctx, int hash, const ut8 *buf, int len, int r
r_print_progressbar (NULL, 12.5 * e, 60);
printf ("\n");
}
} else do_hash_print (ctx, hash, dlen, rad);
} else {
if (iterations>0)
r_hash_do_spice (ctx, hash, iterations, _s);
do_hash_print (ctx, hash, dlen, rad);
}
return 1;
}
static int do_hash(const char *file, const char *algo, RIO *io, int bsize, int rad) {
ut64 j, fsize, algobit = r_hash_name_to_bits (algo);
RHash *ctx;
@ -94,20 +125,34 @@ static int do_hash(const char *file, const char *algo, RIO *io, int bsize, int r
int hashbit = i & algobit;
int dlen = r_hash_size (hashbit);
r_hash_do_begin (ctx, i);
if (s.buf && s.prefix) {
do_hash_internal (ctx,
hashbit, s.buf, s.len, rad, 0);
}
for (j=from; j<to; j+=bsize) {
r_io_read_at (io, j, buf, bsize);
do_hash_internal (ctx,
hashbit, buf, ((j+bsize)<fsize)?
bsize: (fsize-j), rad, 0);
}
if (s.buf && !s.prefix) {
do_hash_internal (ctx,
hashbit, s.buf, s.len, rad, 0);
}
r_hash_do_end (ctx, i);
if (iterations>0)
r_hash_do_spice (ctx, i, iterations, _s);
if (!quiet)
printf ("%s: ", file);
do_hash_print (ctx, i, dlen, rad);
}
}
if (_s)
free (_s->buf);
} else {
/* iterate over all algorithm bits */
if (s.buf)
eprintf ("Warning: Seed ignored on per-block hashing.\n");
for (i=1; i<0x800000; i<<=1) {
ut64 f, t, ofrom, oto;
if (algobit & i) {
@ -142,6 +187,7 @@ static int do_help(int line) {
" -B show per-block hash\n"
" -f from start hashing at given address\n"
" -i num repeat hash N iterations\n"
" -S seed use given seed (hexa or s:string) use ^ to prefix\n"
" -k show hash using the openssh's randomkey algorithm\n"
" -q run in quiet mode (only show results)\n"
" -L list all available algorithms (see -a)\n"
@ -164,16 +210,19 @@ static void algolist() {
}
int main(int argc, char **argv) {
RIO *io;
RHash *ctx;
ut64 algobit;
const char *hashstr = NULL;
const char *algo = "sha256"; /* default hashing algorithm */
int i, ret, c, rad = 0, quit = 0, bsize = 0, numblocks = 0;
const char *algo = "sha256"; /* default hashing algorithm */
const char *seed = NULL;
char *hashstr = NULL;
ut64 algobit;
RHash *ctx;
RIO *io;
while ((c = getopt (argc, argv, "rva:s:b:nBhf:t:kLq")) != -1) {
while ((c = getopt (argc, argv, "rva:i:S:s:b:nBhf:t:kLq")) != -1) {
switch (c) {
case 'q': quiet = 1; break;
case 'i': iterations = atoi (optarg); break;
case 'S': seed = optarg; break;
case 'n': numblocks = 1; break;
case 'L': algolist (); return 0;
case 'r': rad = 1; break;
@ -189,21 +238,45 @@ int main(int argc, char **argv) {
default: eprintf ("rahash2: Unknown flag\n"); return 1;
}
}
do_hash_seed (seed);
if (hashstr && !strcmp (hashstr, "-")) {
hashstr = malloc(1024);
fread ((void*)hashstr, 1, 1023, stdin);
hashstr[1023] = 0;
}
if (hashstr) {
char *str = (char *)hashstr;
int strsz = strlen (hashstr);
if (_s) {
// alloc/concat/resize
str = malloc (strsz + s.len);
if (s.prefix) {
memcpy (str, s.buf, s.len);
strcpy (str+s.len, hashstr);
} else {
strcpy (str, hashstr);
memcpy (str+strsz, s.buf, s.len);
}
strsz += s.len;
str[strsz] = 0;
}
algobit = r_hash_name_to_bits (algo);
for (i=1; i<0x800000; i<<=1) {
if (algobit & i) {
int hashbit = i & algobit;
ctx = r_hash_new (R_TRUE, hashbit);
from = 0;
to = strlen (hashstr);
to = strsz;
do_hash_internal (ctx, hashbit,
(const ut8*) hashstr,
strlen (hashstr), rad, 1);
(const ut8*)str, strsz, rad, 1);
r_hash_free (ctx);
quit = R_TRUE;
}
}
if (_s) {
free (str);
free (s.buf);
}
return 0;
}
if (quit)

View File

@ -1,29 +1,32 @@
/* radare - LGPL - Copyright 2007-2013 pancake */
#include "r_hash.h"
#include "r_util.h"
R_LIB_VERSION(r_hash);
struct { const char *name; ut64 bit; } static const hash_name_bytes[] = {
{"all", UT64_MAX},
{"xor", R_HASH_XOR},
{"xorpair", R_HASH_XORPAIR},
{"md4", R_HASH_MD4},
{"md5", R_HASH_MD5},
{"sha1", R_HASH_SHA1},
{"sha256", R_HASH_SHA256},
{"sha384", R_HASH_SHA384},
{"sha512", R_HASH_SHA512},
{"crc16", R_HASH_CRC16},
{"crc32", R_HASH_CRC32},
{"adler32", R_HASH_ADLER32},
{"xxhash", R_HASH_XXHASH},
{"parity", R_HASH_PARITY},
{"entropy", R_HASH_ENTROPY},
{"hamdist", R_HASH_HAMDIST},
{"pcprint", R_HASH_PCPRINT},
{"mod255", R_HASH_MOD255},
{NULL, 0}};
struct { const char *name; ut64 bit; }
static const hash_name_bytes[] = {
{"all", UT64_MAX},
{"xor", R_HASH_XOR},
{"xorpair", R_HASH_XORPAIR},
{"md4", R_HASH_MD4},
{"md5", R_HASH_MD5},
{"sha1", R_HASH_SHA1},
{"sha256", R_HASH_SHA256},
{"sha384", R_HASH_SHA384},
{"sha512", R_HASH_SHA512},
{"crc16", R_HASH_CRC16},
{"crc32", R_HASH_CRC32},
{"adler32", R_HASH_ADLER32},
{"xxhash", R_HASH_XXHASH},
{"parity", R_HASH_PARITY},
{"entropy", R_HASH_ENTROPY},
{"hamdist", R_HASH_HAMDIST},
{"pcprint", R_HASH_PCPRINT},
{"mod255", R_HASH_MOD255},
{NULL, 0}
};
/* returns 0-100 */
R_API int r_hash_pcprint(const ut8 *buffer, ut64 len) {
@ -139,3 +142,24 @@ R_API ut64 r_hash_name_to_bits(const char *name) {
}
return bits;
}
R_API void r_hash_do_spice(RHash *ctx, int algo, int loops, RHashSeed *seed) {
ut8 buf[1024];
int i, len, dlen, hlen = r_hash_size (algo);
for (i = 0; i< loops; i++) {
if (seed) {
if (seed->prefix) {
memcpy (buf, seed->buf, seed->len);
memcpy (buf+seed->len, ctx->digest, hlen);
} else {
memcpy (buf, ctx->digest, hlen);
memcpy (buf+hlen, seed->buf, seed->len);
}
len = hlen + seed->len;
} else {
memcpy (buf, ctx->digest, hlen);
len = hlen;
}
dlen = r_hash_calculate (ctx, algo, buf, len);
}
}

View File

@ -49,6 +49,12 @@ typedef struct r_hash_t {
ut8 digest[128];
} RHash;
typedef struct r_hash_seed_t {
int prefix;
ut8 *buf;
int len;
} RHashSeed;
#define R_HASH_SIZE_CRC16 2
#define R_HASH_SIZE_CRC32 4
#define R_HASH_SIZE_XXHASH 4
@ -122,6 +128,7 @@ R_API int r_hash_pcprint(const ut8 *buffer, ut64 len);
/* lifecycle */
R_API void r_hash_do_begin(RHash *ctx, int flags);
R_API void r_hash_do_end(RHash *ctx, int flags);
R_API void r_hash_do_spice(RHash *ctx, int algo, int loops, RHashSeed *seed);
#endif
#ifdef __cplusplus

View File

@ -9,6 +9,8 @@ hasher2 \- block based hashing utility
.Op Fl a Ar algorithm
.Op Fl b Ar size
.Op Fl s Ar string
.Op Fl i Ar iterations
.Op Fl S Ar seed
.Op Fl f Ar from
.Op Fl t Ar to
.Op [file] ...
@ -27,12 +29,16 @@ This is the command used by the '#' command of radare.
Select an algorithm for the hashing. Valid values are md5, crc32 and sha1
.It Fl b Ar blocksize
Define the block size
.It Fl i Ar iters
Apply the hash Iters times to itself+seed
.It Fl B
Show per-block hash
.It Fl k
Show result using OpenSSH's VisualHostKey randomart algorithm
.It Fl s Ar string
Hash this string instead of using the 'source' and 'hash-file' arguments.
.It Fl S Ar [^]s:string|hexstr
Set seed to hash with, use ^to prefix seed, otherwise its suffixed.
.It Fl f Ar from
Start hashing at given address
.It Fl t Ar to