From 442210124374ab5bf842dec889e943aa4286ac21 Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Fri, 21 Feb 2025 16:33:22 +0100 Subject: [PATCH] fix #2547 - compilation warning error: dereferencing type-punned pointer will break strict-aliasing rules [-Werror=strict-aliasing] --- armsrc/hitag2.c | 13 +++++++++---- armsrc/hitagS.c | 10 +++++----- common/hitag2/hitag2_crypto.c | 13 ++++--------- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/armsrc/hitag2.c b/armsrc/hitag2.c index 18e6866dd..0c880685c 100644 --- a/armsrc/hitag2.c +++ b/armsrc/hitag2.c @@ -821,12 +821,17 @@ static bool hitag2_crypto(uint8_t *rx, const size_t rxlen, uint8_t *tx, size_t * // stage 1, got UID if (bCrypto == false) { + uint64_t ui64key = key[0] | + ((uint64_t)key[1]) << 8 | + ((uint64_t)key[2]) << 16 | + ((uint64_t)key[3]) << 24 | + ((uint64_t)key[4]) << 32 | + ((uint64_t)key[5]) << 40; + + uint32_t ui32uid = MemLeToUint4byte(rx); + DBG Dbprintf("hitag2_crypto: key array "); DBG Dbhexdump(6, key, false); - - uint64_t ui64key = key[0] | ((uint64_t)key[1]) << 8 | ((uint64_t)key[2]) << 16 | ((uint64_t)key[3]) << 24 | ((uint64_t)key[4]) << 32 | ((uint64_t)key[5]) << 40; - - uint32_t ui32uid = rx[0] | ((uint32_t)rx[1]) << 8 | ((uint32_t)rx[2]) << 16 | ((uint32_t)rx[3]) << 24; DBG Dbprintf("hitag2_crypto: key=0x%x%x uid=0x%x" , (uint32_t)((REV64(ui64key)) >> 32) , (uint32_t)((REV64(ui64key)) & 0xffffffff) diff --git a/armsrc/hitagS.c b/armsrc/hitagS.c index b80210f66..924240ea7 100644 --- a/armsrc/hitagS.c +++ b/armsrc/hitagS.c @@ -538,7 +538,8 @@ static void hts_handle_reader_command(uint8_t *rx, const size_t rxlen, rotate_uid++; *txlen = 32; // init crypt engine - state = ht2_hitag2_init(REV64(tag.data.s.key), REV32(tag.data.s.uid_le), REV32(*(uint32_t *)rx)); + uint32_t le_rx = MemLeToUint4byte(rx); + state = ht2_hitag2_init(REV64(tag.data.s.key), REV32(tag.data.s.uid_le), REV32(le_rx)); DBG Dbhexdump(8, tx, false); for (int i = 0; i < 4; i++) { @@ -1129,10 +1130,7 @@ static int hts_select_tag(const lf_hitag_data_t *packet, uint8_t *tx, size_t siz // if the tag is in authentication mode try the key or challenge if (packet->cmd == HTSF_KEY) { - DBG DbpString("Authenticating using key:"); - DBG Dbhexdump(6, packet->key, false); - - key_le = *(uint64_t *)packet->key; + key_le = MemLeToUint6byte(packet->key); uint32_t le_val = MemLeToUint4byte(rnd); uint64_t state = ht2_hitag2_init(REV64(key_le), REV32(tag.data.s.uid_le), REV32(le_val)); @@ -1146,6 +1144,8 @@ static int hts_select_tag(const lf_hitag_data_t *packet, uint8_t *tx, size_t siz txlen = concatbits(tx, txlen, rnd, 0, 32); txlen = concatbits(tx, txlen, auth_ks, 0, 32); + DBG DbpString("Authenticating using key:"); + DBG Dbhexdump(6, packet->key, false); DBG Dbprintf("%02X %02X %02X %02X %02X %02X %02X %02X", tx[0], tx[1], tx[2], tx[3], tx[4], tx[5], tx[6], tx[7]); } else if (packet->cmd == HTSF_CHALLENGE) { diff --git a/common/hitag2/hitag2_crypto.c b/common/hitag2/hitag2_crypto.c index 9e9499426..381451645 100644 --- a/common/hitag2/hitag2_crypto.c +++ b/common/hitag2/hitag2_crypto.c @@ -404,15 +404,10 @@ void ht2_hitag2_cipher_reset(hitag2_t *tag, const uint8_t *iv) { ((uint64_t)tag->sectors[1][1] << 24) | ((uint64_t)tag->sectors[1][2] << 32) | ((uint64_t)tag->sectors[1][3] << 40); - uint32_t uid = ((uint32_t)tag->sectors[0][0]) | - ((uint32_t)tag->sectors[0][1] << 8) | - ((uint32_t)tag->sectors[0][2] << 16) | - ((uint32_t)tag->sectors[0][3] << 24); - uint32_t iv_ = (((uint32_t)(iv[0]))) | - (((uint32_t)(iv[1])) << 8) | - (((uint32_t)(iv[2])) << 16) | - (((uint32_t)(iv[3])) << 24); - tag->cs = ht2_hitag2_init(REV64(key), REV32(uid), REV32(iv_)); + uint32_t uid = MemLeToUint4byte(tag->sectors[0]); + uint32_t riv = MemLeToUint4byte(iv); + + tag->cs = ht2_hitag2_init(REV64(key), REV32(uid), REV32(riv)); } int ht2_hitag2_cipher_authenticate(uint64_t *state, const uint8_t *authenticator_is) {