From fcfd07ceb23c04ad42c033d8869fbf6a51679d96 Mon Sep 17 00:00:00 2001 From: jsteube Date: Thu, 10 Jan 2019 20:51:38 +0100 Subject: [PATCH] Move parse_and_store_generic_salt to shared.c --- include/shared.h | 2 + src/interface.c | 116 ----------------------------------------------- src/shared.c | 116 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 118 insertions(+), 116 deletions(-) diff --git a/include/shared.h b/include/shared.h index 585cca01c..b89adbc9a 100644 --- a/include/shared.h +++ b/include/shared.h @@ -87,6 +87,8 @@ int select_read_timeout_console (const int sec); const char *strparser (const u32 parser_status); +bool parse_and_store_generic_salt (u8 *out_buf, int *out_len, const u8 *in_buf, const int in_len, MAYBE_UNUSED const hashconfig_t *hashconfig); + int input_tokenizer (const u8 *input_buf, const int input_len, token_t *token); void decoder_apply_optimizer (const hashconfig_t *hashconfig, void *data); diff --git a/src/interface.c b/src/interface.c index 707b5c713..836f7f73a 100644 --- a/src/interface.c +++ b/src/interface.c @@ -346,122 +346,6 @@ const char *stroptitype (const u32 opti_type) * parsing */ -static bool parse_and_store_generic_salt (u8 *out_buf, int *out_len, const u8 *in_buf, const int in_len, MAYBE_UNUSED hashconfig_t *hashconfig) -{ - u32 tmp_u32[(64 * 2) + 1] = { 0 }; - - if (in_len > 512) return false; // 512 = 2 * 256 -- (2 * because of hex), 256 because of maximum salt length in salt_t - - if (hashconfig->opts_type & OPTS_TYPE_ST_HEX) - { - if (in_len < (int) (hashconfig->salt_min * 2)) return false; - if (in_len > (int) (hashconfig->salt_max * 2)) return false; - } - else - { - if (in_len < (int) hashconfig->salt_min) return false; - if (in_len > (int) hashconfig->salt_max) return false; - } - - u8 *tmp_buf = (u8 *) tmp_u32; - - int tmp_len = 0; - - if (hashconfig->opts_type & OPTS_TYPE_ST_HEX) - { - if (tmp_len & 1) return false; - - tmp_len = in_len / 2; - - for (int i = 0, j = 0; i < tmp_len; i += 1, j += 2) - { - u8 p0 = in_buf[j + 0]; - u8 p1 = in_buf[j + 1]; - - tmp_buf[i] = hex_convert (p1) << 0; - tmp_buf[i] |= hex_convert (p0) << 4; - } - } - else if (hashconfig->opts_type & OPTS_TYPE_ST_BASE64) - { - tmp_len = base64_decode (base64_to_int, (const u8 *) in_buf, in_len, tmp_buf); - } - else - { - if (in_len) memcpy (tmp_buf, in_buf, in_len); - - tmp_len = in_len; - } - - if (hashconfig->opts_type & OPTS_TYPE_ST_UTF16LE) - { - if (tmp_len >= 128) return false; - - for (int i = 64 - 1; i >= 1; i -= 2) - { - const u32 v = tmp_u32[i / 2]; - - tmp_u32[i - 0] = ((v >> 8) & 0x00FF0000) | ((v >> 16) & 0x000000FF); - tmp_u32[i - 1] = ((v << 8) & 0x00FF0000) | ((v >> 0) & 0x000000FF); - } - - tmp_len = tmp_len * 2; - } - - if (hashconfig->opts_type & OPTS_TYPE_ST_LOWER) - { - lowercase (tmp_buf, tmp_len); - } - - if (hashconfig->opts_type & OPTS_TYPE_ST_UPPER) - { - uppercase (tmp_buf, tmp_len); - } - - int tmp2_len = tmp_len; - - if (hashconfig->opts_type & OPTS_TYPE_ST_ADD80) - { - if (tmp2_len >= 256) return false; - - tmp_buf[tmp2_len++] = 0x80; - } - - if (hashconfig->opts_type & OPTS_TYPE_ST_ADD01) - { - if (tmp2_len >= 256) return false; - - tmp_buf[tmp2_len++] = 0x01; - } - - if (hashconfig->opts_type & OPTS_TYPE_ST_GENERATE_LE) - { - u32 max = tmp2_len / 4; - - if (tmp2_len % 4) max++; - - for (u32 i = 0; i < max; i++) - { - tmp_u32[i] = byte_swap_32 (tmp_u32[i]); - } - - // Important: we may need to increase the length of memcpy since - // we don't want to "loose" some swapped bytes (could happen if - // they do not perfectly fit in the 4-byte blocks) - // Memcpy does always copy the bytes in the BE order, but since - // we swapped them, some important bytes could be in positions - // we normally skip with the original len - - if (tmp2_len % 4) tmp2_len += 4 - (tmp2_len % 4); - } - - memcpy (out_buf, tmp_buf, tmp2_len); - - *out_len = tmp_len; - - return true; -} - int ascii_digest (const hashconfig_t *hashconfig, const hashes_t *hashes, const module_ctx_t *module_ctx, char *out_buf, const int out_size, const u32 salt_pos, const u32 digest_pos) { void *digests_buf = hashes->digests_buf; diff --git a/src/shared.c b/src/shared.c index c9fd6776b..e1bebbb7f 100644 --- a/src/shared.c +++ b/src/shared.c @@ -1312,3 +1312,119 @@ void encoder_apply_options (const hashconfig_t *hashconfig, void *data) } } } + +bool parse_and_store_generic_salt (u8 *out_buf, int *out_len, const u8 *in_buf, const int in_len, MAYBE_UNUSED const hashconfig_t *hashconfig) +{ + u32 tmp_u32[(64 * 2) + 1] = { 0 }; + + if (in_len > 512) return false; // 512 = 2 * 256 -- (2 * because of hex), 256 because of maximum salt length in salt_t + + if (hashconfig->opts_type & OPTS_TYPE_ST_HEX) + { + if (in_len < (int) (hashconfig->salt_min * 2)) return false; + if (in_len > (int) (hashconfig->salt_max * 2)) return false; + } + else + { + if (in_len < (int) hashconfig->salt_min) return false; + if (in_len > (int) hashconfig->salt_max) return false; + } + + u8 *tmp_buf = (u8 *) tmp_u32; + + int tmp_len = 0; + + if (hashconfig->opts_type & OPTS_TYPE_ST_HEX) + { + if (tmp_len & 1) return false; + + tmp_len = in_len / 2; + + for (int i = 0, j = 0; i < tmp_len; i += 1, j += 2) + { + u8 p0 = in_buf[j + 0]; + u8 p1 = in_buf[j + 1]; + + tmp_buf[i] = hex_convert (p1) << 0; + tmp_buf[i] |= hex_convert (p0) << 4; + } + } + else if (hashconfig->opts_type & OPTS_TYPE_ST_BASE64) + { + tmp_len = base64_decode (base64_to_int, (const u8 *) in_buf, in_len, tmp_buf); + } + else + { + if (in_len) memcpy (tmp_buf, in_buf, in_len); + + tmp_len = in_len; + } + + if (hashconfig->opts_type & OPTS_TYPE_ST_UTF16LE) + { + if (tmp_len >= 128) return false; + + for (int i = 64 - 1; i >= 1; i -= 2) + { + const u32 v = tmp_u32[i / 2]; + + tmp_u32[i - 0] = ((v >> 8) & 0x00FF0000) | ((v >> 16) & 0x000000FF); + tmp_u32[i - 1] = ((v << 8) & 0x00FF0000) | ((v >> 0) & 0x000000FF); + } + + tmp_len = tmp_len * 2; + } + + if (hashconfig->opts_type & OPTS_TYPE_ST_LOWER) + { + lowercase (tmp_buf, tmp_len); + } + + if (hashconfig->opts_type & OPTS_TYPE_ST_UPPER) + { + uppercase (tmp_buf, tmp_len); + } + + int tmp2_len = tmp_len; + + if (hashconfig->opts_type & OPTS_TYPE_ST_ADD80) + { + if (tmp2_len >= 256) return false; + + tmp_buf[tmp2_len++] = 0x80; + } + + if (hashconfig->opts_type & OPTS_TYPE_ST_ADD01) + { + if (tmp2_len >= 256) return false; + + tmp_buf[tmp2_len++] = 0x01; + } + + if (hashconfig->opts_type & OPTS_TYPE_ST_GENERATE_LE) + { + u32 max = tmp2_len / 4; + + if (tmp2_len % 4) max++; + + for (u32 i = 0; i < max; i++) + { + tmp_u32[i] = byte_swap_32 (tmp_u32[i]); + } + + // Important: we may need to increase the length of memcpy since + // we don't want to "loose" some swapped bytes (could happen if + // they do not perfectly fit in the 4-byte blocks) + // Memcpy does always copy the bytes in the BE order, but since + // we swapped them, some important bytes could be in positions + // we normally skip with the original len + + if (tmp2_len % 4) tmp2_len += 4 - (tmp2_len % 4); + } + + memcpy (out_buf, tmp_buf, tmp2_len); + + *out_len = tmp_len; + + return true; +}