From 42e440611a1ca7b9dada7b608ed58efd82f0de0f Mon Sep 17 00:00:00 2001 From: jsteube Date: Sun, 17 Mar 2019 11:09:32 +0100 Subject: [PATCH] Fix some broken strict-aliasing rules --- include/convert.h | 8 ++++++ include/types.h | 54 ++++++++++++++++++++++++++++++++++++ src/convert.c | 56 ++++++++++++++++++++++++++++++++++++++ src/modules/module_00022.c | 50 ++++++++++++++++------------------ src/modules/module_01722.c | 18 ++++++------ src/modules/module_01731.c | 18 ++++++------ 6 files changed, 158 insertions(+), 46 deletions(-) diff --git a/include/convert.h b/include/convert.h index 1ee824b39..8c7212a80 100644 --- a/include/convert.h +++ b/include/convert.h @@ -58,4 +58,12 @@ size_t base64_encode (u8 (*f) (const u8), const u8 *in_buf, const size_t in_len, void lowercase (u8 *buf, const size_t len); void uppercase (u8 *buf, const size_t len); +u16 v16a_from_v32 (const u32 v32); +u16 v16b_from_v32 (const u32 v32); +u32 v32_from_v16ab (const u16 v16a, const u16 v16b); + +u32 v32a_from_v64 (const u64 v64); +u32 v32b_from_v64 (const u64 v64); +u64 v64_from_v32ab (const u32 v32a, const u32 v32b); + #endif // _CONVERT_H diff --git a/include/types.h b/include/types.h index 88605b6a5..0c61a6662 100644 --- a/include/types.h +++ b/include/types.h @@ -75,6 +75,60 @@ typedef pthread_mutex_t hc_thread_mutex_t; typedef sem_t hc_thread_semaphore_t; #endif +// unions + +typedef union vconv32 +{ + u64 v32; + + struct + { + u16 v16a; + u16 v16b; + }; + + struct + { + u8 v8a; + u8 v8b; + u8 v8c; + u8 v8d; + }; + +} vconv32_t; + +typedef union vconv64 +{ + u64 v64; + + struct + { + u32 v32a; + u32 v32b; + }; + + struct + { + u16 v16a; + u16 v16b; + u16 v16c; + u16 v16d; + }; + + struct + { + u8 v8a; + u8 v8b; + u8 v8c; + u8 v8d; + u8 v8e; + u8 v8f; + u8 v8g; + u8 v8h; + }; + +} vconv64_t; + // enums typedef enum loglevel diff --git a/src/convert.c b/src/convert.c index 47ca4164c..5308e97d8 100644 --- a/src/convert.c +++ b/src/convert.c @@ -838,3 +838,59 @@ void uppercase (u8 *buf, const size_t len) { for (size_t i = 0; i < len; i++) buf[i] = (u8) toupper ((int) buf[i]); } + +u16 v16a_from_v32 (const u32 v32) +{ + vconv32_t v; + + v.v32 = v32; + + return v.v16a; +} + +u16 v16b_from_v32 (const u32 v32) +{ + vconv32_t v; + + v.v32 = v32; + + return v.v16b; +} + +u32 v32_from_v16ab (const u16 v16a, const u16 v16b) +{ + vconv32_t v; + + v.v16a = v16a; + v.v16b = v16b; + + return v.v32; +} + +u32 v32a_from_v64 (const u64 v64) +{ + vconv64_t v; + + v.v64 = v64; + + return v.v32a; +} + +u32 v32b_from_v64 (const u64 v64) +{ + vconv64_t v; + + v.v64 = v64; + + return v.v32b; +} + +u64 v64_from_v32ab (const u32 v32a, const u32 v32b) +{ + vconv64_t v; + + v.v32a = v32a; + v.v32b = v32b; + + return v.v64; +} diff --git a/src/modules/module_00022.c b/src/modules/module_00022.c index 91421f478..3df8fb62b 100644 --- a/src/modules/module_00022.c +++ b/src/modules/module_00022.c @@ -203,39 +203,37 @@ int module_hash_encode (MAYBE_UNUSED const hashconfig_t *hashconfig, MAYBE_UNUSE tmp[2] = byte_swap_32 (tmp[2]); tmp[3] = byte_swap_32 (tmp[3]); - u16 *ptr = (u16 *) tmp; - u8 tmp_buf[32]; tmp_buf[ 0] = sig[0]; - tmp_buf[ 1] = int_to_base64 (((ptr[1]) >> 12) & 0x3f); - tmp_buf[ 2] = int_to_base64 (((ptr[1]) >> 6) & 0x3f); - tmp_buf[ 3] = int_to_base64 (((ptr[1]) >> 0) & 0x3f); - tmp_buf[ 4] = int_to_base64 (((ptr[0]) >> 12) & 0x3f); - tmp_buf[ 5] = int_to_base64 (((ptr[0]) >> 6) & 0x3f); + tmp_buf[ 1] = int_to_base64 (((v16b_from_v32 (tmp[0])) >> 12) & 0x3f); + tmp_buf[ 2] = int_to_base64 (((v16b_from_v32 (tmp[0])) >> 6) & 0x3f); + tmp_buf[ 3] = int_to_base64 (((v16b_from_v32 (tmp[0])) >> 0) & 0x3f); + tmp_buf[ 4] = int_to_base64 (((v16a_from_v32 (tmp[0])) >> 12) & 0x3f); + tmp_buf[ 5] = int_to_base64 (((v16a_from_v32 (tmp[0])) >> 6) & 0x3f); tmp_buf[ 6] = sig[1]; - tmp_buf[ 7] = int_to_base64 (((ptr[0]) >> 0) & 0x3f); - tmp_buf[ 8] = int_to_base64 (((ptr[3]) >> 12) & 0x3f); - tmp_buf[ 9] = int_to_base64 (((ptr[3]) >> 6) & 0x3f); - tmp_buf[10] = int_to_base64 (((ptr[3]) >> 0) & 0x3f); - tmp_buf[11] = int_to_base64 (((ptr[2]) >> 12) & 0x3f); + tmp_buf[ 7] = int_to_base64 (((v16a_from_v32 (tmp[0])) >> 0) & 0x3f); + tmp_buf[ 8] = int_to_base64 (((v16b_from_v32 (tmp[1])) >> 12) & 0x3f); + tmp_buf[ 9] = int_to_base64 (((v16b_from_v32 (tmp[1])) >> 6) & 0x3f); + tmp_buf[10] = int_to_base64 (((v16b_from_v32 (tmp[1])) >> 0) & 0x3f); + tmp_buf[11] = int_to_base64 (((v16a_from_v32 (tmp[1])) >> 12) & 0x3f); tmp_buf[12] = sig[2]; - tmp_buf[13] = int_to_base64 (((ptr[2]) >> 6) & 0x3f); - tmp_buf[14] = int_to_base64 (((ptr[2]) >> 0) & 0x3f); - tmp_buf[15] = int_to_base64 (((ptr[5]) >> 12) & 0x3f); - tmp_buf[16] = int_to_base64 (((ptr[5]) >> 6) & 0x3f); + tmp_buf[13] = int_to_base64 (((v16a_from_v32 (tmp[1])) >> 6) & 0x3f); + tmp_buf[14] = int_to_base64 (((v16a_from_v32 (tmp[1])) >> 0) & 0x3f); + tmp_buf[15] = int_to_base64 (((v16b_from_v32 (tmp[2])) >> 12) & 0x3f); + tmp_buf[16] = int_to_base64 (((v16b_from_v32 (tmp[2])) >> 6) & 0x3f); tmp_buf[17] = sig[3]; - tmp_buf[18] = int_to_base64 (((ptr[5]) >> 0) & 0x3f); - tmp_buf[19] = int_to_base64 (((ptr[4]) >> 12) & 0x3f); - tmp_buf[20] = int_to_base64 (((ptr[4]) >> 6) & 0x3f); - tmp_buf[21] = int_to_base64 (((ptr[4]) >> 0) & 0x3f); - tmp_buf[22] = int_to_base64 (((ptr[7]) >> 12) & 0x3f); + tmp_buf[18] = int_to_base64 (((v16b_from_v32 (tmp[2])) >> 0) & 0x3f); + tmp_buf[19] = int_to_base64 (((v16a_from_v32 (tmp[2])) >> 12) & 0x3f); + tmp_buf[20] = int_to_base64 (((v16a_from_v32 (tmp[2])) >> 6) & 0x3f); + tmp_buf[21] = int_to_base64 (((v16a_from_v32 (tmp[2])) >> 0) & 0x3f); + tmp_buf[22] = int_to_base64 (((v16b_from_v32 (tmp[3])) >> 12) & 0x3f); tmp_buf[23] = sig[4]; - tmp_buf[24] = int_to_base64 (((ptr[7]) >> 6) & 0x3f); - tmp_buf[25] = int_to_base64 (((ptr[7]) >> 0) & 0x3f); - tmp_buf[26] = int_to_base64 (((ptr[6]) >> 12) & 0x3f); - tmp_buf[27] = int_to_base64 (((ptr[6]) >> 6) & 0x3f); - tmp_buf[28] = int_to_base64 (((ptr[6]) >> 0) & 0x3f); + tmp_buf[24] = int_to_base64 (((v16b_from_v32 (tmp[3])) >> 6) & 0x3f); + tmp_buf[25] = int_to_base64 (((v16b_from_v32 (tmp[3])) >> 0) & 0x3f); + tmp_buf[26] = int_to_base64 (((v16a_from_v32 (tmp[3])) >> 12) & 0x3f); + tmp_buf[27] = int_to_base64 (((v16a_from_v32 (tmp[3])) >> 6) & 0x3f); + tmp_buf[28] = int_to_base64 (((v16a_from_v32 (tmp[3])) >> 0) & 0x3f); tmp_buf[29] = sig[5]; tmp_buf[30] = 0; diff --git a/src/modules/module_01722.c b/src/modules/module_01722.c index 957edc571..8e250af7a 100644 --- a/src/modules/module_01722.c +++ b/src/modules/module_01722.c @@ -156,8 +156,6 @@ int module_hash_encode (MAYBE_UNUSED const hashconfig_t *hashconfig, MAYBE_UNUSE tmp[7] += SHA512M_H; } - u32 *ptr = (u32 *) tmp; - char tmp_salt[SALT_MAX * 2]; const int salt_len = generic_salt_encode (hashconfig, (const u8 *) salt->salt_buf, (const int) salt->salt_len, (u8 *) tmp_salt); @@ -166,14 +164,14 @@ int module_hash_encode (MAYBE_UNUSED const hashconfig_t *hashconfig, MAYBE_UNUSE const int line_len = snprintf (line_buf, line_size, "%s%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x", tmp_salt, - ptr[ 1], ptr[ 0], - ptr[ 3], ptr[ 2], - ptr[ 5], ptr[ 4], - ptr[ 7], ptr[ 6], - ptr[ 9], ptr[ 8], - ptr[11], ptr[10], - ptr[13], ptr[12], - ptr[15], ptr[14]); + v32b_from_v64 (tmp[0]), v32a_from_v64 (tmp[0]), + v32b_from_v64 (tmp[1]), v32a_from_v64 (tmp[1]), + v32b_from_v64 (tmp[2]), v32a_from_v64 (tmp[2]), + v32b_from_v64 (tmp[3]), v32a_from_v64 (tmp[3]), + v32b_from_v64 (tmp[4]), v32a_from_v64 (tmp[4]), + v32b_from_v64 (tmp[5]), v32a_from_v64 (tmp[5]), + v32b_from_v64 (tmp[6]), v32a_from_v64 (tmp[6]), + v32b_from_v64 (tmp[7]), v32a_from_v64 (tmp[7])); return line_len; } diff --git a/src/modules/module_01731.c b/src/modules/module_01731.c index 6fc3a14b0..33dd5a60c 100644 --- a/src/modules/module_01731.c +++ b/src/modules/module_01731.c @@ -149,8 +149,6 @@ int module_hash_encode (MAYBE_UNUSED const hashconfig_t *hashconfig, MAYBE_UNUSE tmp[7] += SHA512M_H; } - const u32 *ptr = (const u32 *) tmp; - char tmp_salt[SALT_MAX * 2]; const int salt_len = generic_salt_encode (hashconfig, (const u8 *) salt->salt_buf, (const int) salt->salt_len, (u8 *) tmp_salt); @@ -159,14 +157,14 @@ int module_hash_encode (MAYBE_UNUSED const hashconfig_t *hashconfig, MAYBE_UNUSE const int line_len = snprintf (line_buf, line_size, "0x0200%s%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x", tmp_salt, - ptr[ 1], ptr[ 0], - ptr[ 3], ptr[ 2], - ptr[ 5], ptr[ 4], - ptr[ 7], ptr[ 6], - ptr[ 9], ptr[ 8], - ptr[11], ptr[10], - ptr[13], ptr[12], - ptr[15], ptr[14]); + v32b_from_v64 (tmp[0]), v32a_from_v64 (tmp[0]), + v32b_from_v64 (tmp[1]), v32a_from_v64 (tmp[1]), + v32b_from_v64 (tmp[2]), v32a_from_v64 (tmp[2]), + v32b_from_v64 (tmp[3]), v32a_from_v64 (tmp[3]), + v32b_from_v64 (tmp[4]), v32a_from_v64 (tmp[4]), + v32b_from_v64 (tmp[5]), v32a_from_v64 (tmp[5]), + v32b_from_v64 (tmp[6]), v32a_from_v64 (tmp[6]), + v32b_from_v64 (tmp[7]), v32a_from_v64 (tmp[7])); return line_len; }