Fix md5 issue for size(0) in rabin2 -SK md5

This commit is contained in:
pancake 2016-12-09 19:01:59 +01:00
parent d26917a0a2
commit 748ba2ce4a
4 changed files with 50 additions and 44 deletions

View File

@ -1760,12 +1760,15 @@ static char *build_hash_string(int mode, const char *chksum, ut8 *data, ut32 dat
char tmp[128];
int i;
do {
for (i = 0; *ptr && *ptr != ',' && i < sizeof(tmp) -1; i++)
for (i = 0; *ptr && *ptr != ',' && i < sizeof (tmp) -1; i++) {
tmp[i] = *ptr++;
}
tmp[i] = '\0';
chkstr = r_hash_to_string (NULL, tmp, data, datalen);
if (!chkstr) {
if (*ptr && *ptr == ',') ptr++;
if (*ptr && *ptr == ',') {
ptr++;
}
continue;
}
if (IS_MODE_SIMPLE (mode)) {

View File

@ -62,8 +62,9 @@ R_API int r_hash_parity(const ut8 *buf, ut64 len) {
/* fmi: nopcode.org/0xFFFF */
R_API ut16 r_hash_xorpair(const ut8 *a, ut64 len) {
ut16 result = 0, *b = (ut16 *)a;
for (len>>=1; len--; b++)
for (len >>= 1; len--; b++) {
result ^= *b;
}
return result;
}
@ -201,8 +202,6 @@ R_API char *r_hash_to_string(RHash *ctx, const char *name, const ut8 *data, int
digest_hex[digest_size * 2] = 0;
}
}
if (myctx) {
r_hash_free (myctx);
}
r_hash_free (myctx);
return digest_hex;
}

View File

@ -48,10 +48,8 @@ documentation and/or software.
static void MD5Transform (ut32 [4], const unsigned char [64]);
static void Encode (unsigned char *, ut32 *, unsigned int);
static void Decode (ut32 *, const unsigned char *, unsigned int);
static void MD5_memcpy (void*, const void*, unsigned int);
static void MD5_memset (void*, int, unsigned int);
static unsigned char PADDING[64] = {
static const unsigned char PADDING[64] = {
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
@ -114,8 +112,9 @@ void MD5Update (R_MD5_CTX *context, const ut8 *input, unsigned int inputLen) {
/* Update number of bits */
if ((context->count[0] += ((ut32)inputLen << 3))
< ((ut32)inputLen << 3))
< ((ut32)inputLen << 3)) {
context->count[1]++;
}
context->count[1] += ((ut32)inputLen >> 29);
partLen = 64 - index;
@ -123,26 +122,22 @@ void MD5Update (R_MD5_CTX *context, const ut8 *input, unsigned int inputLen) {
/* Transform as many times as possible.
*/
if (inputLen >= partLen) {
MD5_memcpy
((void*)&context->buffer[index], (void*)input, partLen);
memmove ((void*)&context->buffer[index], (void*)input, partLen);
MD5Transform (context->state, context->buffer);
for (i = partLen; i + 63 < inputLen; i += 64)
for (i = partLen; i + 63 < inputLen; i += 64) {
MD5Transform (context->state, &input[i]);
}
index = 0;
} else i = 0;
/* Buffer remaining input */
MD5_memcpy
((void*)&context->buffer[index], (void*)&input[i],
inputLen-i);
memmove ((void*)&context->buffer[index], (void*)&input[i], inputLen - i);
}
/* MD5 finalization. Ends an MD5 message-digest operation, writing the
the message digest and zeroizing the context */
void MD5Final (ut8 digest[16], R_MD5_CTX *context) {
unsigned char bits[8];
ut8 bits[8];
unsigned int index, padLen;
/* Save number of bits */
@ -160,7 +155,7 @@ void MD5Final (ut8 digest[16], R_MD5_CTX *context) {
Encode (digest, context->state, 16);
/* Zeroize sensitive information. */
MD5_memset ((void*)context, 0, sizeof (*context));
memset ((void*)context, 0, sizeof (*context));
}
/* MD5 basic transformation. Transforms state based on block */
@ -248,7 +243,7 @@ static void MD5Transform (ut32 state[4], const ut8 block[64]) {
/* Zeroize sensitive information.
*/
MD5_memset ((void*)x, 0, sizeof (x));
memset ((void*)x, 0, sizeof (x));
}
/* Encodes input (ut32) into output (unsigned char). Assumes len is a multiple of 4. */
@ -269,13 +264,3 @@ static void Decode (ut32 *output, const ut8 *input, unsigned int len) {
output[i] = ((ut32)input[j]) | (((ut32)input[j+1]) << 8) |
(((ut32)input[j+2]) << 16) | (((ut32)input[j+3]) << 24);
}
/* Note: Replace "for loop" with standard memcpy if possible. */
static void MD5_memcpy (void *output, const void *input, unsigned int len) {
memmove(output, input, len);
}
/* Note: Replace "for loop" with standard memset if possible. */
static void MD5_memset (void *output, int value, unsigned int len) {
memset(output, value, len);
}

View File

@ -41,14 +41,20 @@ R_API void r_hash_free(RHash *ctx) {
}
R_API ut8 *r_hash_do_md5(RHash *ctx, const ut8 *input, int len) {
if (len<0)
if (len < 0) {
return NULL;
if (ctx->rst)
}
if (ctx->rst) {
MD5Init (&ctx->md5);
if (len>0)
}
if (len > 0) {
MD5Update (&ctx->md5, input, len);
if (ctx->rst || len == 0)
} else {
MD5Update (&ctx->md5, (const ut8*)"", 0);
}
if (ctx->rst) {
MD5Final (ctx->digest, &ctx->md5);
}
return ctx->digest;
}
@ -64,15 +70,20 @@ R_API ut8 *r_hash_do_sha1(RHash *ctx, const ut8 *input, int len) {
}
R_API ut8 *r_hash_do_md4(RHash *ctx, const ut8 *input, int len) {
if (len<0) return NULL;
if (len < 0) {
return NULL;
}
mdfour (ctx->digest, input, len);
return ctx->digest;
}
R_API ut8 *r_hash_do_sha256(RHash *ctx, const ut8 *input, int len) {
if (len<0) return NULL;
if (ctx->rst)
if (len < 0) {
return NULL;
}
if (ctx->rst) {
SHA256_Init (&ctx->sha256);
}
SHA256_Update (&ctx->sha256, input, len);
if (ctx->rst || len == 0)
SHA256_Final (ctx->digest, &ctx->sha256);
@ -80,21 +91,29 @@ R_API ut8 *r_hash_do_sha256(RHash *ctx, const ut8 *input, int len) {
}
R_API ut8 *r_hash_do_sha384(RHash *ctx, const ut8 *input, int len) {
if (len<0) return NULL;
if (ctx->rst)
if (len < 0) {
return NULL;
}
if (ctx->rst) {
SHA384_Init (&ctx->sha384);
}
SHA384_Update (&ctx->sha384, input, len);
if (ctx->rst || len == 0)
if (ctx->rst || len == 0) {
SHA384_Final (ctx->digest, &ctx->sha384);
}
return ctx->digest;
}
R_API ut8 *r_hash_do_sha512(RHash *ctx, const ut8 *input, int len) {
if (len<0) return NULL;
if (ctx->rst)
if (len < 0) {
return NULL;
}
if (ctx->rst) {
SHA512_Init (&ctx->sha512);
}
SHA512_Update (&ctx->sha512, input, len);
if (ctx->rst || len == 0)
if (ctx->rst || len == 0) {
SHA512_Final (ctx->digest, &ctx->sha512);
}
return ctx->digest;
}