From 38c49b7a356db0479f93dbe523404e811adc611a Mon Sep 17 00:00:00 2001 From: SChernykh Date: Fri, 8 Feb 2019 22:25:30 +0100 Subject: [PATCH] More fixes for big-endian machines and pointer aliasing --- src/crypto/slow-hash.c | 35 +++++++++++++++++++++---------- src/crypto/variant4_random_math.h | 10 +++++++-- 2 files changed, 32 insertions(+), 13 deletions(-) diff --git a/src/crypto/slow-hash.c b/src/crypto/slow-hash.c index 8da79dee0..5a8be4a61 100644 --- a/src/crypto/slow-hash.c +++ b/src/crypto/slow-hash.c @@ -215,30 +215,43 @@ extern void aesb_pseudo_round(const uint8_t *in, uint8_t *out, const uint8_t *ex lo ^= SWAP64LE(*(U64(hp_state + (j ^ 0x20)) + 1)); \ } while (0) +#define V4_REG_LOAD(dst, src) \ + do { \ + memcpy((dst), (src), sizeof(v4_reg)); \ + if (sizeof(v4_reg) == sizeof(uint32_t)) \ + *(dst) = SWAP32LE(*(dst)); \ + else \ + *(dst) = SWAP64LE(*(dst)); \ + } while (0) + #define VARIANT4_RANDOM_MATH_INIT() \ v4_reg r[8]; \ struct V4_Instruction code[TOTAL_LATENCY * ALU_COUNT + 1]; \ do if (variant >= 4) \ { \ - v4_reg* data = (v4_reg*)(state.hs.w + 12); \ - r[0] = data[0]; \ - r[1] = data[1]; \ - r[2] = data[2]; \ - r[3] = data[3]; \ + for (int i = 0; i < 4; ++i) \ + V4_REG_LOAD(r + i, (uint8_t*)(state.hs.w + 12) + sizeof(v4_reg) * i); \ v4_random_math_init(code, height); \ } while (0) #define VARIANT4_RANDOM_MATH(a, b, r, _b, _b1) \ do if (variant >= 4) \ { \ + uint64_t t; \ + memcpy(&t, b, sizeof(uint64_t)); \ + \ if (sizeof(v4_reg) == sizeof(uint32_t)) \ - U64(b)[0] ^= (r[0] + r[1]) | ((uint64_t)(r[2] + r[3]) << 32); \ + t ^= SWAP64LE((r[0] + r[1]) | ((uint64_t)(r[2] + r[3]) << 32)); \ else \ - U64(b)[0] ^= (r[0] + r[1]) ^ (r[2] + r[3]); \ - r[4] = ((v4_reg*)(a))[0]; \ - r[5] = ((v4_reg*)(a))[sizeof(uint64_t) / sizeof(v4_reg)]; \ - r[6] = ((v4_reg*)(_b))[0]; \ - r[7] = ((v4_reg*)(_b1))[0]; \ + t ^= SWAP64LE((r[0] + r[1]) ^ (r[2] + r[3])); \ + \ + memcpy(b, &t, sizeof(uint64_t)); \ + \ + V4_REG_LOAD(r + 4, a); \ + V4_REG_LOAD(r + 5, (uint64_t*)(a) + 1); \ + V4_REG_LOAD(r + 6, _b); \ + V4_REG_LOAD(r + 7, _b1); \ + \ v4_random_math(code, r); \ } while (0) diff --git a/src/crypto/variant4_random_math.h b/src/crypto/variant4_random_math.h index 2024cf80e..00f0393c5 100644 --- a/src/crypto/variant4_random_math.h +++ b/src/crypto/variant4_random_math.h @@ -195,8 +195,12 @@ static inline int v4_random_math_init(struct V4_Instruction* code, const uint64_ char data[32]; memset(data, 0, sizeof(data)); - *((uint64_t*)data) = SWAP64LE(height); + uint64_t tmp = SWAP64LE(height); + memcpy(data, &tmp, sizeof(uint64_t)); + // Set data_index past the last byte in data + // to trigger full data update with blake hash + // before we start using it size_t data_index = sizeof(data); int code_size; @@ -355,7 +359,9 @@ static inline int v4_random_math_init(struct V4_Instruction* code, const uint64_ // ADD instruction requires 4 more random bytes for 32-bit constant "C" in "a = a + b + C" check_data(&data_index, sizeof(uint32_t), data, sizeof(data)); - code[code_size].C = SWAP32LE(*((uint32_t*)&data[data_index])); + uint32_t t; + memcpy(&t, data + data_index, sizeof(uint32_t)); + code[code_size].C = SWAP32LE(t); data_index += sizeof(uint32_t); }