hash/md5: prefer std types

This commit is contained in:
Danny Robson 2019-02-09 15:20:13 +11:00
parent e213be2415
commit 103ede647b

View File

@ -3,11 +3,12 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
* *
* Copyright 2013 Danny Robson <danny@nerdcruft.net> * Copyright 2013-2019 Danny Robson <danny@nerdcruft.net>
*/ */
#include "md5.hpp" #include "md5.hpp"
#include <cruft/util/std.hpp>
#include <cruft/util/iterator.hpp> #include <cruft/util/iterator.hpp>
#include <cruft/util/bitwise.hpp> #include <cruft/util/bitwise.hpp>
#include <cruft/util/debug.hpp> #include <cruft/util/debug.hpp>
@ -19,45 +20,16 @@ using cruft::crypto::hash::MD5;
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// Per-round mixing functions // Per-round mixing functions
static constexpr static constexpr u32 F (u32 x, u32 y, u32 z) { return z ^ (x & (y ^ z)); }
uint32_t static constexpr u32 G (u32 x, u32 y, u32 z) { return F (z, x, y); }
F (uint32_t x, uint32_t y, uint32_t z) static constexpr u32 H (u32 x, u32 y, u32 z) { return x ^ y ^ z; }
{ static constexpr u32 I (u32 x, u32 y, u32 z) { return y ^ (x | ~z); }
return z ^ (x & (y ^ z));
}
//-----------------------------------------------------------------------------
static constexpr
uint32_t
G (uint32_t x, uint32_t y, uint32_t z)
{
return F (z, x, y);
}
//-----------------------------------------------------------------------------
static constexpr
uint32_t
H (uint32_t x, uint32_t y, uint32_t z)
{
return x ^ y ^ z;
}
//-----------------------------------------------------------------------------
static constexpr
uint32_t
I (uint32_t x, uint32_t y, uint32_t z)
{
return y ^ (x | ~z);
}
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// Mixing constants for all rounds. They are defined as 'abs(sin(i)) * max_uint32', but we use the // Mixing constants for all rounds. They are defined as 'abs(sin(i)) * max_u32', but we use the
// literals to avoid any stupid maths issues during compilation. // literals to avoid any stupid maths issues during compilation.
const std::array<uint32_t, 65> T = { { const std::array<u32, 65> T = { {
0x00000000, 0x00000000,
// Round 1 // Round 1
@ -87,15 +59,15 @@ const std::array<uint32_t, 65> T = { {
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
static constexpr uint32_t INITIAL_A = 0x67452301; static constexpr u32 INITIAL_A = 0x67452301;
static constexpr uint32_t INITIAL_B = 0xefcdab89; static constexpr u32 INITIAL_B = 0xefcdab89;
static constexpr uint32_t INITIAL_C = 0x98badcfe; static constexpr u32 INITIAL_C = 0x98badcfe;
static constexpr uint32_t INITIAL_D = 0x10325476; static constexpr u32 INITIAL_D = 0x10325476;
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
typename MD5::digest_t typename MD5::digest_t
MD5::operator() (cruft::view<const uint8_t*> a) const noexcept MD5::operator() (cruft::view<const u08*> a) const noexcept
{ {
return (*this)(a, nullptr); return (*this)(a, nullptr);
} }
@ -103,7 +75,7 @@ MD5::operator() (cruft::view<const uint8_t*> a) const noexcept
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
typename MD5::digest_t typename MD5::digest_t
MD5::operator() (cruft::view<const uint8_t*> a, cruft::view<const uint8_t*> b) const noexcept MD5::operator() (cruft::view<const u08*> a, cruft::view<const u08*> b) const noexcept
{ {
return (*this)(a, b, nullptr); return (*this)(a, b, nullptr);
} }
@ -112,13 +84,13 @@ MD5::operator() (cruft::view<const uint8_t*> a, cruft::view<const uint8_t*> b) c
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
typename MD5::digest_t typename MD5::digest_t
MD5::operator() ( MD5::operator() (
const cruft::view<const uint8_t*> data_a, const cruft::view<const u08*> data_a,
const cruft::view<const uint8_t*> data_b, const cruft::view<const u08*> data_b,
const cruft::view<const uint8_t*> data_c const cruft::view<const u08*> data_c
) const noexcept { ) const noexcept {
union { union {
std::array<uint32_t,16> w; std::array<u32,16> w;
std::array<uint8_t, 64> b; std::array<u08, 64> b;
} X; } X;
static_assert (sizeof (X.w) == BLOCK_SIZE); static_assert (sizeof (X.w) == BLOCK_SIZE);
@ -127,8 +99,8 @@ MD5::operator() (
static_assert (sizeof (X.b) == BLOCK_SIZE); static_assert (sizeof (X.b) == BLOCK_SIZE);
static_assert (X.b.size () == BLOCK_SIZE); static_assert (X.b.size () == BLOCK_SIZE);
uint64_t m_total = 0; u64 m_total = 0;
std::array<uint32_t,4> ABCD = { std::array<u32,4> ABCD = {
INITIAL_A, INITIAL_A,
INITIAL_B, INITIAL_B,
INITIAL_C, INITIAL_C,
@ -144,7 +116,7 @@ MD5::operator() (
); );
m_total = data_a.size () + data_b.size () + data_c.size (); m_total = data_a.size () + data_b.size () + data_c.size ();
uint64_t bits = m_total * 8; u64 bits = m_total * 8;
// Pad with the mandatory 1 bit // Pad with the mandatory 1 bit
X.b[m_total % BLOCK_SIZE] = 0x80; X.b[m_total % BLOCK_SIZE] = 0x80;
@ -180,20 +152,17 @@ MD5::operator() (
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
std::array<uint32_t,4> std::array<u32,4>
MD5::transform ( MD5::transform (
const std::array<uint32_t,4> &ABCD, const std::array<u32,4> &ABCD,
const std::array<uint32_t,16> &X) const noexcept const std::array<u32,16> &X) const noexcept
{ {
uint32_t A = ABCD[0], auto [A,B,C,D] = ABCD;
B = ABCD[1],
C = ABCD[2],
D = ABCD[3];
#define ROUNDx(a,b,c,d,k,s,i,f) do { \ #define ROUNDx(a,b,c,d,k,s,i,f) do { \
(a) += (f)((b), (c), (d)) + X[k] + T[i]; \ (a) += (f)((b), (c), (d)) + X[k] + T[i]; \
(a) = cruft::rotatel ((a), (s)); \ (a) = cruft::rotatel ((a), (s)); \
(a) += (b); \ (a) += (b); \
} while (0) } while (0)