hash/md4: prefer cruft::std types

This commit is contained in:
Danny Robson 2019-02-04 20:00:58 +11:00
parent c0e1e0b7b6
commit f23651a3f3

View File

@ -8,6 +8,7 @@
#include "md4.hpp"
#include <cruft/util/std.hpp>
#include <cruft/util/bitwise.hpp>
#include <cruft/util/debug.hpp>
@ -19,51 +20,29 @@ using cruft::crypto::hash::MD4;
///////////////////////////////////////////////////////////////////////////////
// Auxiliary functions for each set of rounds
static inline constexpr
uint32_t
F (uint32_t X, uint32_t Y, uint32_t Z)
{
return (X & Y) | (~X & Z);
}
static constexpr u32 F (u32 X, u32 Y, u32 Z) { return (X & Y) | (~X & Z); }
static constexpr u32 G (u32 X, u32 Y, u32 Z) { return (X & Y) | (X & Z) | (Y & Z); }
static constexpr u32 H (u32 X, u32 Y, u32 Z) { return X ^ Y ^ Z; }
//-----------------------------------------------------------------------------
static inline constexpr
uint32_t
G (uint32_t X, uint32_t Y, uint32_t Z)
{
return (X & Y) | (X & Z) | (Y & Z);
}
//-----------------------------------------------------------------------------
static inline constexpr
uint32_t
H (uint32_t X, uint32_t Y, uint32_t Z)
{
return X ^ Y ^ Z;
}
//-----------------------------------------------------------------------------
static constexpr uint32_t INITIAL_A = 0x67452301;
static constexpr uint32_t INITIAL_B = 0xefcdab89;
static constexpr uint32_t INITIAL_C = 0x98badcfe;
static constexpr uint32_t INITIAL_D = 0x10325476;
static constexpr u32 INITIAL_A = 0x67452301;
static constexpr u32 INITIAL_B = 0xefcdab89;
static constexpr u32 INITIAL_C = 0x98badcfe;
static constexpr u32 INITIAL_D = 0x10325476;
///////////////////////////////////////////////////////////////////////////////
static void
transform (std::array<uint32_t,4> &ABCD, const std::array<uint32_t,16> &X) noexcept
transform (std::array<u32,4> &ABCD, const std::array<u32,16> &X) noexcept
{
uint32_t A = ABCD[0],
u32 A = ABCD[0],
B = ABCD[1],
C = ABCD[2],
D = ABCD[3];
#define ROUND1(a,b,c,d,k,s) do { \
#define ROUND1(a,b,c,d,k,s) do { \
(a) += F((b), (c), (d)) + X[k]; \
(a) = cruft::rotatel ((a), (s)); \
(a) = cruft::rotatel ((a), (s)); \
} while (0)
ROUND1(A,B,C,D, 0, 3);
@ -86,9 +65,9 @@ transform (std::array<uint32_t,4> &ABCD, const std::array<uint32_t,16> &X) noexc
ROUND1(C,D,A,B, 14, 11);
ROUND1(B,C,D,A, 15, 19);
#define ROUND2(a,b,c,d,k,s) do { \
#define ROUND2(a,b,c,d,k,s) do { \
(a) += G((b),(c),(d)) + X[k] + 0x5A827999u; \
(a) = cruft::rotatel ((a), (s)); \
(a) = cruft::rotatel ((a), (s)); \
} while (0)
ROUND2(A,B,C,D, 0, 3);
@ -111,9 +90,9 @@ transform (std::array<uint32_t,4> &ABCD, const std::array<uint32_t,16> &X) noexc
ROUND2(C,D,A,B, 11, 9);
ROUND2(B,C,D,A, 15, 13);
#define ROUND3(a,b,c,d,k,s) do { \
#define ROUND3(a,b,c,d,k,s) do { \
(a) += H((b),(c),(d)) + X[k] + 0x6ED9EBA1u; \
(a) = cruft::rotatel ((a), (s)); \
(a) = cruft::rotatel ((a), (s)); \
} while (0)
ROUND3(A,B,C,D, 0, 3);
@ -145,12 +124,12 @@ transform (std::array<uint32_t,4> &ABCD, const std::array<uint32_t,16> &X) noexc
///////////////////////////////////////////////////////////////////////////////
MD4::digest_t
MD4::operator() (cruft::view<const uint8_t*> data) noexcept
MD4::operator() (cruft::view<const u08*> data) noexcept
{
/* RESET */
uint64_t total = 0;
u64 total = 0;
std::array<uint32_t,4> ABCD {
std::array<u32,4> ABCD {
INITIAL_A,
INITIAL_B,
INITIAL_C,
@ -158,8 +137,8 @@ MD4::operator() (cruft::view<const uint8_t*> data) noexcept
};
union {
std::array<uint32_t,16> X;
std::array<uint8_t, 64> Xb;
std::array<u32,16> X;
std::array<u08,64> Xb;
};
static_assert (sizeof (X) == sizeof (Xb));
static_assert (sizeof (ABCD) == sizeof (digest_t));
@ -180,7 +159,7 @@ MD4::operator() (cruft::view<const uint8_t*> data) noexcept
total += remain.size ();
}
uint64_t bits = total * 8;
u64 bits = total * 8;
/* FINISH */
{