From 4e2adc031a49ac36cb9749b9aeeb4da3799a6d2d Mon Sep 17 00:00:00 2001 From: jsteube Date: Thu, 23 Feb 2017 10:39:17 +0100 Subject: [PATCH] Add some compiler independant integer overflow functions --- include/shared.h | 6 ++++-- src/mpsp.c | 2 +- src/shared.c | 42 +++++++++++++++++++++++++++++++++++------- src/wordlist.c | 8 ++++---- 4 files changed, 44 insertions(+), 14 deletions(-) diff --git a/include/shared.h b/include/shared.h index 583682186..3e1968cb7 100644 --- a/include/shared.h +++ b/include/shared.h @@ -13,8 +13,10 @@ #include #include -bool overflow_check_int64_add (const u64 a, const u64 b); -bool overflow_check_int64_mul (const u64 a, const u64 b); +bool overflow_check_u32_add (const u32 a, const u32 b); +bool overflow_check_u32_mul (const u32 a, const u32 b); +bool overflow_check_u64_add (const u64 a, const u64 b); +bool overflow_check_u64_mul (const u64 a, const u64 b); bool is_power_of_2 (const u32 v); diff --git a/src/mpsp.c b/src/mpsp.c index f08c9680f..3f5e29506 100644 --- a/src/mpsp.c +++ b/src/mpsp.c @@ -815,7 +815,7 @@ static int sp_get_sum (u32 start, u32 stop, cs_t *root_css_buf, u64 *result) for (i = start; i < stop; i++) { - if (overflow_check_int64_mul (sum, root_css_buf[i].cs_len) == true) return -1; + if (overflow_check_u64_mul (sum, root_css_buf[i].cs_len) == false) return -1; sum *= root_css_buf[i].cs_len; } diff --git a/src/shared.c b/src/shared.c index 15ad31001..e3f696eca 100644 --- a/src/shared.c +++ b/src/shared.c @@ -7,18 +7,46 @@ #include "types.h" #include "shared.h" -bool overflow_check_int64_add (const u64 a, const u64 b) +static inline int get_msb32 (const u32 v) { - u64 t; - - return __builtin_add_overflow (a, b, &t); + return 32 - __builtin_clz (v); } -bool overflow_check_int64_mul (const u64 a, const u64 b) +static inline int get_msb64 (const u64 v) { - u64 t; + return 64 - __builtin_clzll (v); +} - return __builtin_mul_overflow (a, b, &t); +bool overflow_check_u32_add (const u32 a, const u32 b) +{ + const int a_msb = get_msb32 (a); + const int b_msb = get_msb32 (b); + + return ((a_msb < 32) && (b_msb < 32)); +} + +bool overflow_check_u32_mul (const u32 a, const u32 b) +{ + const int a_msb = get_msb32 (a); + const int b_msb = get_msb32 (b); + + return ((a_msb + b_msb) < 32); +} + +bool overflow_check_u64_add (const u64 a, const u64 b) +{ + const int a_msb = get_msb64 (a); + const int b_msb = get_msb64 (b); + + return ((a_msb < 64) && (b_msb < 64)); +} + +bool overflow_check_u64_mul (const u64 a, const u64 b) +{ + const int a_msb = get_msb64 (a); + const int b_msb = get_msb64 (b); + + return ((a_msb + b_msb) < 64); } bool is_power_of_2 (const u32 v) diff --git a/src/wordlist.c b/src/wordlist.c index 0d991c7d4..9e2137ca1 100644 --- a/src/wordlist.c +++ b/src/wordlist.c @@ -305,13 +305,13 @@ int count_words (hashcat_ctx_t *hashcat_ctx, FILE *fd, const char *dictfile, u64 if (user_options_extra->attack_kern == ATTACK_KERN_STRAIGHT) { - if (overflow_check_int64_mul (keyspace, (u64) straight_ctx->kernel_rules_cnt) == true) return -1; + if (overflow_check_u64_mul (keyspace, straight_ctx->kernel_rules_cnt) == false) return -1; keyspace *= straight_ctx->kernel_rules_cnt; } else if (user_options_extra->attack_kern == ATTACK_KERN_COMBI) { - if (overflow_check_int64_mul (keyspace, combinator_ctx->combs_cnt) == true) return -1; + if (overflow_check_u64_mul (keyspace, combinator_ctx->combs_cnt) == false) return -1; keyspace *= combinator_ctx->combs_cnt; } @@ -378,13 +378,13 @@ int count_words (hashcat_ctx_t *hashcat_ctx, FILE *fd, const char *dictfile, u64 { if (user_options_extra->attack_kern == ATTACK_KERN_STRAIGHT) { - if (overflow_check_int64_add (cnt, (u64) straight_ctx->kernel_rules_cnt) == true) return -1; + if (overflow_check_u64_add (cnt, straight_ctx->kernel_rules_cnt) == false) return -1; cnt += straight_ctx->kernel_rules_cnt; } else if (user_options_extra->attack_kern == ATTACK_KERN_COMBI) { - if (overflow_check_int64_add (cnt, combinator_ctx->combs_cnt) == true) return -1; + if (overflow_check_u64_add (cnt, combinator_ctx->combs_cnt) == false) return -1; cnt += combinator_ctx->combs_cnt; }