From f0b287e8388f1cefb4dcbdb7fff9cc79e0af2066 Mon Sep 17 00:00:00 2001 From: jsteube Date: Mon, 26 Dec 2016 12:52:06 +0100 Subject: [PATCH] Get rid of bin_to_hex_lower(), add u8_to_hex_lower(), u32_to_hex_lower() and u64_to_hex_lower() --- include/convert.h | 4 ++- src/convert.c | 69 ++++++++++++++++++++++++++++++++++------------- src/interface.c | 35 +++++++++++------------- 3 files changed, 70 insertions(+), 38 deletions(-) diff --git a/include/convert.h b/include/convert.h index 8aec653f8..96343babe 100644 --- a/include/convert.h +++ b/include/convert.h @@ -20,7 +20,9 @@ u8 hex_to_u8 (const u8 hex[2]); u32 hex_to_u32 (const u8 hex[8]); u64 hex_to_u64 (const u8 hex[16]); -void bin_to_hex_lower (const u32 v, u8 hex[8]); +void u8_to_hex_lower (const u8 v, u8 hex[2]); +void u32_to_hex_lower (const u32 v, u8 hex[8]); +void u64_to_hex_lower (const u64 v, u8 hex[16]); u8 int_to_base32 (const u8 c); u8 base32_to_int (const u8 c); diff --git a/src/convert.c b/src/convert.c index f41a5186a..4847cc340 100644 --- a/src/convert.c +++ b/src/convert.c @@ -216,27 +216,60 @@ u64 hex_to_u64 (const u8 hex[16]) return (v); } -void bin_to_hex_lower (const u32 v, u8 hex[8]) +void u8_to_hex_lower (const u8 v, u8 hex[2]) { - hex[0] = v >> 28 & 15; - hex[1] = v >> 24 & 15; - hex[2] = v >> 20 & 15; - hex[3] = v >> 16 & 15; - hex[4] = v >> 12 & 15; - hex[5] = v >> 8 & 15; - hex[6] = v >> 4 & 15; - hex[7] = v >> 0 & 15; + const u8 tbl[0x10] = + { + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', + 'a', 'b', 'c', 'd', 'e', 'f', + }; - u32 add; + hex[1] = tbl[v >> 0 & 15]; + hex[0] = tbl[v >> 4 & 15]; +} - hex[0] += 6; add = ((hex[0] & 0x10u) >> 4) * 39; hex[0] += 42 + add; - hex[1] += 6; add = ((hex[1] & 0x10u) >> 4) * 39; hex[1] += 42 + add; - hex[2] += 6; add = ((hex[2] & 0x10u) >> 4) * 39; hex[2] += 42 + add; - hex[3] += 6; add = ((hex[3] & 0x10u) >> 4) * 39; hex[3] += 42 + add; - hex[4] += 6; add = ((hex[4] & 0x10u) >> 4) * 39; hex[4] += 42 + add; - hex[5] += 6; add = ((hex[5] & 0x10u) >> 4) * 39; hex[5] += 42 + add; - hex[6] += 6; add = ((hex[6] & 0x10u) >> 4) * 39; hex[6] += 42 + add; - hex[7] += 6; add = ((hex[7] & 0x10u) >> 4) * 39; hex[7] += 42 + add; +void u32_to_hex_lower (const u32 v, u8 hex[8]) +{ + const u8 tbl[0x10] = + { + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', + 'a', 'b', 'c', 'd', 'e', 'f', + }; + + hex[1] = tbl[v >> 0 & 15]; + hex[0] = tbl[v >> 4 & 15]; + hex[3] = tbl[v >> 8 & 15]; + hex[2] = tbl[v >> 12 & 15]; + hex[5] = tbl[v >> 16 & 15]; + hex[4] = tbl[v >> 20 & 15]; + hex[7] = tbl[v >> 24 & 15]; + hex[6] = tbl[v >> 28 & 15]; +} + +void u64_to_hex_lower (const u64 v, u8 hex[16]) +{ + const u8 tbl[0x10] = + { + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', + 'a', 'b', 'c', 'd', 'e', 'f', + }; + + hex[ 1] = tbl[v >> 0 & 15]; + hex[ 0] = tbl[v >> 4 & 15]; + hex[ 3] = tbl[v >> 8 & 15]; + hex[ 2] = tbl[v >> 12 & 15]; + hex[ 5] = tbl[v >> 16 & 15]; + hex[ 4] = tbl[v >> 20 & 15]; + hex[ 7] = tbl[v >> 24 & 15]; + hex[ 6] = tbl[v >> 28 & 15]; + hex[ 9] = tbl[v >> 32 & 15]; + hex[ 8] = tbl[v >> 36 & 15]; + hex[11] = tbl[v >> 40 & 15]; + hex[10] = tbl[v >> 44 & 15]; + hex[13] = tbl[v >> 48 & 15]; + hex[12] = tbl[v >> 52 & 15]; + hex[15] = tbl[v >> 56 & 15]; + hex[14] = tbl[v >> 60 & 15]; } u8 int_to_base32 (const u8 c) diff --git a/src/interface.c b/src/interface.c index 372ffd10b..7eabc6dd9 100644 --- a/src/interface.c +++ b/src/interface.c @@ -4046,15 +4046,23 @@ int ipb2_parse_hash (u8 *input_buf, u32 input_len, hash_t *hash_buf, MAYBE_UNUSE u32 salt_len = input_len - 32 - 1; - u8 *salt_buf = (u8 *) input_buf + 32 + 1; + u8 *salt_buf = input_buf + 32 + 1; + + u8 *salt_buf_ptr = (u8 *) salt->salt_buf; + + salt_len = parse_and_store_salt (salt_buf_ptr, salt_buf, salt_len, hashconfig); + + if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH); + + salt->salt_len = salt_len; + + // precomput first md5 u32 salt_pc_block[16] = { 0 }; u8 *salt_pc_block_ptr = (u8 *) salt_pc_block; - salt_len = parse_and_store_salt (salt_pc_block_ptr, salt_buf, salt_len, hashconfig); - - if (salt_len == UINT_MAX) return (PARSER_SALT_LENGTH); + memcpy (salt_pc_block_ptr, salt_buf_ptr, salt_len); salt_pc_block_ptr[salt_len] = 0x80; @@ -4064,23 +4072,12 @@ int ipb2_parse_hash (u8 *input_buf, u32 input_len, hash_t *hash_buf, MAYBE_UNUSE md5_64 (salt_pc_block, salt_pc_digest); - salt_pc_digest[0] = byte_swap_32 (salt_pc_digest[0]); - salt_pc_digest[1] = byte_swap_32 (salt_pc_digest[1]); - salt_pc_digest[2] = byte_swap_32 (salt_pc_digest[2]); - salt_pc_digest[3] = byte_swap_32 (salt_pc_digest[3]); - - u8 *salt_buf_ptr = (u8 *) salt->salt_buf; - - memcpy (salt_buf_ptr, salt_buf, salt_len); - u8 *salt_buf_pc_ptr = (u8 *) salt->salt_buf_pc; - bin_to_hex_lower (salt_pc_digest[0], salt_buf_pc_ptr + 0); - bin_to_hex_lower (salt_pc_digest[1], salt_buf_pc_ptr + 8); - bin_to_hex_lower (salt_pc_digest[2], salt_buf_pc_ptr + 16); - bin_to_hex_lower (salt_pc_digest[3], salt_buf_pc_ptr + 24); - - salt->salt_len = 32; // changed, was salt_len before -- was a bug? 32 should be correct + u32_to_hex_lower (salt_pc_digest[0], salt_buf_pc_ptr + 0); + u32_to_hex_lower (salt_pc_digest[1], salt_buf_pc_ptr + 8); + u32_to_hex_lower (salt_pc_digest[2], salt_buf_pc_ptr + 16); + u32_to_hex_lower (salt_pc_digest[3], salt_buf_pc_ptr + 24); return (PARSER_OK); }