From 92fc0b34b07c5514ff224782636e6c0a3e18b243 Mon Sep 17 00:00:00 2001 From: Vincent Richard Date: Thu, 28 Nov 2013 23:23:35 +0100 Subject: [PATCH] Enforce strict aliasing rule and avoid alignment issues. --- src/security/digest/md5/md5MessageDigest.cpp | 24 +++++++++---------- .../security/digest/md5/md5MessageDigest.hpp | 8 ++++++- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/src/security/digest/md5/md5MessageDigest.cpp b/src/security/digest/md5/md5MessageDigest.cpp index fa9904b8..88f9c9de 100644 --- a/src/security/digest/md5/md5MessageDigest.cpp +++ b/src/security/digest/md5/md5MessageDigest.cpp @@ -147,11 +147,11 @@ void md5MessageDigest::update(const byte_t* data, const unsigned long length) if (avail > len) { - copyUint8Array(m_block + (64 - avail), data, len); + copyUint8Array(m_block.b8 + (64 - avail), data, len); return; } - copyUint8Array(m_block + (64 - avail), data, avail); + copyUint8Array(m_block.b8 + (64 - avail), data, avail); transformHelper(); data += avail; @@ -159,14 +159,14 @@ void md5MessageDigest::update(const byte_t* data, const unsigned long length) while (len >= 64) { - copyUint8Array(m_block, data, 64); + copyUint8Array(m_block.b8, data, 64); transformHelper(); data += 64; len -= 64; } - copyUint8Array(m_block, data, len); + copyUint8Array(m_block.b8, data, len); } @@ -196,7 +196,7 @@ void md5MessageDigest::finalize() { const long offset = m_byteCount & 0x3f; - vmime_uint8* p = m_block + offset; + vmime_uint8* p = m_block.b8 + offset; long padding = 56 - (offset + 1); *p++ = 0x80; @@ -205,23 +205,23 @@ void md5MessageDigest::finalize() { memset(p, 0x00, padding + 8); transformHelper(); - p = m_block; + p = m_block.b8; padding = 56; } memset(p, 0, padding); - reinterpret_cast (m_block)[14] = static_cast (m_byteCount << 3); - reinterpret_cast (m_block)[15] = static_cast (m_byteCount >> 29); + m_block.b32[14] = static_cast (m_byteCount << 3); + m_block.b32[15] = static_cast (m_byteCount >> 29); #if VMIME_BYTE_ORDER_BIG_ENDIAN - swapUint32Array((vmime_uint32*) m_block, (64 - 8) / 4); + swapUint32Array(m_block.b32, (64 - 8) / 4); #endif transform(); #if VMIME_BYTE_ORDER_BIG_ENDIAN - swapUint32Array((vmime_uint32*) m_hash, 4); + swapUint32Array(m_hash, 4); #endif m_finalized = true; @@ -231,7 +231,7 @@ void md5MessageDigest::finalize() void md5MessageDigest::transformHelper() { #if VMIME_BYTE_ORDER_BIG_ENDIAN - swapUint32Array((vmime_uint32*) m_block, 64 / 4); + swapUint32Array(m_block.b32, 64 / 4); #endif transform(); } @@ -239,7 +239,7 @@ void md5MessageDigest::transformHelper() void md5MessageDigest::transform() { - const vmime_uint32* const in = reinterpret_cast (m_block); + const vmime_uint32* const in = m_block.b32; vmime_uint32 a = m_hash[0]; vmime_uint32 b = m_hash[1]; diff --git a/vmime/security/digest/md5/md5MessageDigest.hpp b/vmime/security/digest/md5/md5MessageDigest.hpp index 0d9892a1..f7dff079 100644 --- a/vmime/security/digest/md5/md5MessageDigest.hpp +++ b/vmime/security/digest/md5/md5MessageDigest.hpp @@ -63,8 +63,14 @@ protected: vmime_uint32 m_hash[4]; + union BlockType + { + vmime_uint32 b32[16]; + vmime_uint8 b8[64]; + }; + unsigned long m_byteCount; - vmime_uint8 m_block[64]; + BlockType m_block; bool m_finalized; };