1
mirror of https://github.com/hashcat/hashcat synced 2024-12-27 05:13:45 +01:00

Add some compiler independant integer overflow functions

This commit is contained in:
jsteube 2017-02-23 10:39:17 +01:00
parent 7797826c8d
commit 4e2adc031a
4 changed files with 44 additions and 14 deletions

View File

@ -13,8 +13,10 @@
#include <time.h>
#include <fcntl.h>
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);

View File

@ -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;
}

View File

@ -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)

View File

@ -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;
}