Skip to content

Commit

Permalink
Refactorization before adding SHA512
Browse files Browse the repository at this point in the history
Add a template parameter to manage the size of the splitted words
handled by the compression function.
  • Loading branch information
matlo607 committed Dec 10, 2016
1 parent 42140b0 commit 41a7c33
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 58 deletions.
6 changes: 3 additions & 3 deletions include/HashingStrategy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace crypto {
template <size_t N>
using CryptoHash = CryptoHash_uint8<N>;

template <size_t N_tmpdigest, size_t N_msgBlock, size_t N_digest = N_tmpdigest>
template <size_t N_tmpdigest, typename T_workWord, size_t N_msgBlock, size_t N_digest = N_tmpdigest>
class HashingStrategy
{
public:
Expand Down Expand Up @@ -74,9 +74,9 @@ namespace crypto {
protected:

MsgBlock_uint8<N_msgBlock> m_msgBlock;
uint16_t m_msgBlockIndex;
size_t m_msgBlockIndex;

CryptoHash_uint32<N_tmpdigest> m_intermediateHash;
std::array<T_workWord, N_tmpdigest / sizeof(T_workWord)> m_intermediateHash;

virtual void process(void) = 0;
virtual CryptoHash<N_digest> getDigest(void) = 0;
Expand Down
51 changes: 27 additions & 24 deletions include/HashingStrategy.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,20 @@ namespace crypto {
return stream;
}

template <size_t N_tmpdigest, size_t N_msgBlock, size_t N_digest>
HashingStrategy<N_tmpdigest,N_msgBlock,N_digest>::HashingStrategy(std::unique_ptr<StrategyBlockCipherLike>&& p) :
template <size_t N_tmpdigest, typename T_workWord, size_t N_msgBlock, size_t N_digest>
HashingStrategy<N_tmpdigest,T_workWord,N_msgBlock,N_digest>::HashingStrategy(std::unique_ptr<StrategyBlockCipherLike>&& p) :
m_msgLength(0),
m_blockCipherStrategy(std::move(p))
{}

template <size_t N_tmpdigest, size_t N_msgBlock, size_t N_digest>
HashingStrategy<N_tmpdigest,N_msgBlock,N_digest>::HashingStrategy(HashingStrategy&& other) :
template <size_t N_tmpdigest, typename T_workWord, size_t N_msgBlock, size_t N_digest>
HashingStrategy<N_tmpdigest,T_workWord,N_msgBlock,N_digest>::HashingStrategy(HashingStrategy&& other) :
m_msgLength(other.m_msgLength),
m_blockCipherStrategy(std::move(other.m_blockCipherStrategy))
{}

template <size_t N_tmpdigest, size_t N_msgBlock, size_t N_digest>
HashingStrategy<N_tmpdigest,N_msgBlock,N_digest>& HashingStrategy<N_tmpdigest,N_msgBlock,N_digest>::operator=(HashingStrategy&& other)
template <size_t N_tmpdigest, typename T_workWord, size_t N_msgBlock, size_t N_digest>
HashingStrategy<N_tmpdigest,T_workWord,N_msgBlock,N_digest>& HashingStrategy<N_tmpdigest,T_workWord,N_msgBlock,N_digest>::operator=(HashingStrategy&& other)
{
if (this != &other) {
m_msgLength = other.m_msgLength;
Expand All @@ -57,8 +57,8 @@ namespace crypto {
return *this;
}

template <size_t N_tmpdigest, size_t N_msgBlock, size_t N_digest>
bool HashingStrategy<N_tmpdigest,N_msgBlock,N_digest>::update(const uint8_t buf[], size_t len, size_t offset)
template <size_t N_tmpdigest, typename T_workWord, size_t N_msgBlock, size_t N_digest>
bool HashingStrategy<N_tmpdigest,T_workWord,N_msgBlock,N_digest>::update(const uint8_t buf[], size_t len, size_t offset)
{
assert(buf != NULL && offset <= len);

Expand All @@ -79,8 +79,8 @@ namespace crypto {
return true;
}

template <size_t N_tmpdigest, size_t N_msgBlock, size_t N_digest>
CryptoHash<N_digest> HashingStrategy<N_tmpdigest,N_msgBlock,N_digest>::getHash(void)
template <size_t N_tmpdigest, typename T_workWord, size_t N_msgBlock, size_t N_digest>
CryptoHash<N_digest> HashingStrategy<N_tmpdigest,T_workWord,N_msgBlock,N_digest>::getHash(void)
{
// size of the message in bits
CryptoHash<N_digest> digest = m_blockCipherStrategy->addPadding(m_msgLength * 8);
Expand All @@ -94,20 +94,20 @@ namespace crypto {
return std::move(digest);
}

template <size_t N_tmpdigest, size_t N_msgBlock, size_t N_digest>
HashingStrategy<N_tmpdigest,N_msgBlock,N_digest>::StrategyBlockCipherLike::StrategyBlockCipherLike() :
template <size_t N_tmpdigest, typename T_workWord, size_t N_msgBlock, size_t N_digest>
HashingStrategy<N_tmpdigest,T_workWord,N_msgBlock,N_digest>::StrategyBlockCipherLike::StrategyBlockCipherLike() :
m_msgBlockIndex(0) {}

template <size_t N_tmpdigest, size_t N_msgBlock, size_t N_digest>
HashingStrategy<N_tmpdigest,N_msgBlock,N_digest>::StrategyBlockCipherLike::StrategyBlockCipherLike(StrategyBlockCipherLike&& other) :
template <size_t N_tmpdigest, typename T_workWord, size_t N_msgBlock, size_t N_digest>
HashingStrategy<N_tmpdigest,T_workWord,N_msgBlock,N_digest>::StrategyBlockCipherLike::StrategyBlockCipherLike(StrategyBlockCipherLike&& other) :
m_msgBlock(std::move(other.m_msgBlock)),
m_msgBlockIndex(other.m_msgBlockIndex),
m_intermediateHash(std::move(other.m_intermediateHash))
{}

template <size_t N_tmpdigest, size_t N_msgBlock, size_t N_digest>
typename HashingStrategy<N_tmpdigest,N_msgBlock,N_digest>::StrategyBlockCipherLike&
HashingStrategy<N_tmpdigest,N_msgBlock,N_digest>::StrategyBlockCipherLike::operator=(StrategyBlockCipherLike&& other)
template <size_t N_tmpdigest, typename T_workWord, size_t N_msgBlock, size_t N_digest>
typename HashingStrategy<N_tmpdigest,T_workWord,N_msgBlock,N_digest>::StrategyBlockCipherLike&
HashingStrategy<N_tmpdigest,T_workWord,N_msgBlock,N_digest>::StrategyBlockCipherLike::operator=(StrategyBlockCipherLike&& other)
{
if (this != &other) {
m_msgBlockIndex = other.m_msgBlockIndex;
Expand All @@ -118,14 +118,14 @@ namespace crypto {
}


template <size_t N_tmpdigest, size_t N_msgBlock, size_t N_digest>
size_t HashingStrategy<N_tmpdigest,N_msgBlock,N_digest>::StrategyBlockCipherLike::write(const uint8_t buf[], size_t len)
template <size_t N_tmpdigest, typename T_workWord, size_t N_msgBlock, size_t N_digest>
size_t HashingStrategy<N_tmpdigest,T_workWord,N_msgBlock,N_digest>::StrategyBlockCipherLike::write(const uint8_t buf[], size_t len)
{
assert(buf != NULL && len > 0);

uint8_t writable = sizeof(MsgBlock_uint8<N_msgBlock>) - m_msgBlockIndex;
auto writable = sizeof(MsgBlock_uint8<N_msgBlock>) - m_msgBlockIndex;
bool processing = (len >= writable);
uint8_t toWrite = processing ? writable : len;
auto toWrite = processing ? writable : len;

memcpy(m_msgBlock.data() + m_msgBlockIndex, buf, toWrite);

Expand All @@ -138,10 +138,13 @@ namespace crypto {
return toWrite;
}

template <size_t N_tmpdigest, size_t N_msgBlock, size_t N_digest>
CryptoHash<N_digest> HashingStrategy<N_tmpdigest,N_msgBlock,N_digest>::StrategyBlockCipherLike::addPadding(size_t len)
template <size_t N_tmpdigest, typename T_workWord, size_t N_msgBlock, size_t N_digest>
CryptoHash<N_digest> HashingStrategy<N_tmpdigest,T_workWord,N_msgBlock,N_digest>::StrategyBlockCipherLike::addPadding(size_t len)
{
const size_t MSGLENGTH_offset = sizeof(MsgBlock_uint8<N_msgBlock>) - sizeof(uint64_t);
auto const MSGLENGTH_offset =
(std::is_same<T_workWord, uint64_t>::value) ?
sizeof(MsgBlock_uint8<N_msgBlock>) - sizeof(uint64_t) * 2 :
sizeof(MsgBlock_uint8<N_msgBlock>) - sizeof(uint64_t);

// Write a "1" followed by 7 "0"s
m_msgBlock[m_msgBlockIndex++] = 0x80;
Expand Down
2 changes: 1 addition & 1 deletion include/MD4.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace crypto {

using MD4hash = CryptoHash<MD4_HASH_SIZE>;

class MD4hashing : public HashingStrategy<MD4_HASH_SIZE, MD4_MSGBLOCK_SIZE>
class MD4hashing : public HashingStrategy<MD4_HASH_SIZE, uint32_t, MD4_MSGBLOCK_SIZE>
{
public:

Expand Down
2 changes: 1 addition & 1 deletion include/MD5.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace crypto {

using MD5hash = CryptoHash<MD5_HASH_SIZE>;

class MD5hashing : public HashingStrategy<MD5_HASH_SIZE, MD5_MSGBLOCK_SIZE>
class MD5hashing : public HashingStrategy<MD5_HASH_SIZE, uint32_t, MD5_MSGBLOCK_SIZE>
{
public:

Expand Down
2 changes: 1 addition & 1 deletion include/SHA1.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace crypto {

using SHA1hash = CryptoHash<SHA1_HASH_SIZE>;

class SHA1hashing final : public HashingStrategy<SHA1_HASH_SIZE, SHA1_MSGBLOCK_SIZE>
class SHA1hashing final : public HashingStrategy<SHA1_HASH_SIZE, uint32_t, SHA1_MSGBLOCK_SIZE>
{
public:

Expand Down
8 changes: 3 additions & 5 deletions include/SHA256224.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,15 @@ namespace crypto {
template <size_t N>
using SHA256224hash = CryptoHash<N>;


template <size_t N>
using HS = HashingStrategy<SHA256224_TMPHASH_SIZE, SHA256224_MSGBLOCK_SIZE, N>;

template <size_t N_digest>
class SHA256224hashing : public HS<N_digest>
class SHA256224hashing : public HashingStrategy<SHA256224_TMPHASH_SIZE, uint32_t, SHA256224_MSGBLOCK_SIZE, N_digest>
{
protected:

class SHA256224BlockCipherLike;

template <size_t N>
using HS = HashingStrategy<SHA256224_TMPHASH_SIZE, uint32_t, SHA256224_MSGBLOCK_SIZE, N>;

SHA256224hashing(std::unique_ptr< typename HS<N_digest>::StrategyBlockCipherLike >&& p);
SHA256224hashing(void) = delete;
Expand Down
9 changes: 7 additions & 2 deletions include/SHA256224.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@ namespace crypto {
using SHA256224MsgBlock_uint32 = MsgBlock_uint32<SHA256224_MSGBLOCK_SIZE>;
using SHA256224MsgBlock_uint64 = MsgBlock_uint64<SHA256224_MSGBLOCK_SIZE>;

namespace sha256224_detail {
template <size_t N>
using HS = HashingStrategy<SHA256224_TMPHASH_SIZE, uint32_t, SHA256224_MSGBLOCK_SIZE, N>;
} /* namespace sha256224_detail */

template <size_t N_digest>
SHA256224hashing<N_digest>::SHA256224hashing(std::unique_ptr< typename HS<N_digest>::StrategyBlockCipherLike >&& p) :
HS<N_digest>(std::move(p))
SHA256224hashing<N_digest>::SHA256224hashing(std::unique_ptr< typename sha256224_detail::HS<N_digest>::StrategyBlockCipherLike >&& p) :
sha256224_detail::HS<N_digest>(std::move(p))
{
}

Expand Down
6 changes: 4 additions & 2 deletions src/MD4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ using MD4hash_uint32 = CryptoHash_uint32<MD4_HASH_SIZE>;
using MD4MsgBlock_uint32 = MsgBlock_uint32<MD4_MSGBLOCK_SIZE>;
using MD4MsgBlock_uint64 = MsgBlock_uint64<MD4_MSGBLOCK_SIZE>;

using HS = HashingStrategy<MD4_HASH_SIZE, uint32_t, MD4_MSGBLOCK_SIZE>;

MD4hashing::MD4hashing(void) :
HashingStrategy<MD4_HASH_SIZE, MD4_MSGBLOCK_SIZE>(std::make_unique<MD4hashing::MD4BlockCipherLike>())
HS(std::make_unique<MD4hashing::MD4BlockCipherLike>())
{
}

MD4hashing::MD4BlockCipherLike::MD4BlockCipherLike(void)
: HashingStrategy<MD4_HASH_SIZE, MD4_MSGBLOCK_SIZE>::StrategyBlockCipherLike()
: HS::StrategyBlockCipherLike()
{
reset();
}
Expand Down
2 changes: 1 addition & 1 deletion src/MD5.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ using MD5hash_uint32 = CryptoHash_uint32<MD5_HASH_SIZE>;
using MD5MsgBlock_uint32 = MsgBlock_uint32<MD5_MSGBLOCK_SIZE>;
using MD5MsgBlock_uint64 = MsgBlock_uint64<MD5_MSGBLOCK_SIZE>;

using HS = HashingStrategy<MD5_HASH_SIZE, MD5_MSGBLOCK_SIZE>;
using HS = HashingStrategy<MD5_HASH_SIZE, uint32_t, MD5_MSGBLOCK_SIZE>;

MD5hashing::MD5hashing(void) :
HS(std::make_unique<MD5hashing::MD5BlockCipherLike>())
Expand Down
35 changes: 21 additions & 14 deletions src/SHA1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ using SHA1hash_uint32 = CryptoHash_uint32<SHA1_HASH_SIZE>;
using SHA1MsgBlock_uint32 = MsgBlock_uint32<SHA1_MSGBLOCK_SIZE>;
using SHA1MsgBlock_uint64 = MsgBlock_uint64<SHA1_MSGBLOCK_SIZE>;

using HS = HashingStrategy<SHA1_HASH_SIZE, uint32_t, SHA1_MSGBLOCK_SIZE>;

SHA1hashing::SHA1hashing(void) :
HashingStrategy<SHA1_HASH_SIZE, SHA1_MSGBLOCK_SIZE>(std::make_unique<SHA1hashing::SHA1BlockCipherLike>())
HS(std::make_unique<SHA1hashing::SHA1BlockCipherLike>())
{
}

SHA1hashing::SHA1BlockCipherLike::SHA1BlockCipherLike(void)
: HashingStrategy<SHA1_HASH_SIZE, SHA1_MSGBLOCK_SIZE>::StrategyBlockCipherLike()
: HS::StrategyBlockCipherLike()
{
reset();
}
Expand All @@ -42,10 +44,13 @@ SHA1hash SHA1hashing::SHA1BlockCipherLike::getDigest(void)
SHA1hash digest;

#if __BYTE_ORDER == __LITTLE_ENDIAN
auto& dest = *reinterpret_cast<SHA1hash_uint32*>(digest.data());

// write the hash in big endian
for (uint8_t i = 0; i < m_intermediateHash.size(); ++i) {
(*reinterpret_cast<SHA1hash_uint32*>(digest.data()))[i] = htobe32(m_intermediateHash[i]);
}
std::transform(m_intermediateHash.begin(),
m_intermediateHash.end(),
dest.begin(),
[] (uint32_t n) { return htobe32(n); });
#else
memcpy(digest.data(), m_intermediateHash.data(), digest.size());
#endif
Expand Down Expand Up @@ -75,12 +80,14 @@ void SHA1hashing::SHA1BlockCipherLike::process(void)
std::array<uint32_t, 80> W; // word sequence
uint32_t A, B, C, D, E; // word buffers

// initialize the first 16 words in the array W
for (uint8_t t = 0; t < 16; ++t) {
W[t] = htobe32((*reinterpret_cast<SHA1MsgBlock_uint32*>(m_msgBlock.data()))[t]);
}
// initialize the first 16 words in the array W with the message block
auto& msgBlockUInt32 = *reinterpret_cast<SHA1MsgBlock_uint32*>(m_msgBlock.data());
std::transform(msgBlockUInt32.begin(),
msgBlockUInt32.end(),
W.begin(),
[] (uint32_t n) { return htobe32(n); });

for (uint8_t t = 16; t < 80; ++t) {
for (auto t = 16; t < 80; ++t) {
W[t] = rotate_left(W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16], 1);
}

Expand All @@ -90,7 +97,7 @@ void SHA1hashing::SHA1BlockCipherLike::process(void)
D = m_intermediateHash[3];
E = m_intermediateHash[4];

for (uint8_t t = 0; t < 20; ++t) {
for (auto t = 0; t < 20; ++t) {
auto temp = rotate_left(A,5) + f1(B,C,D) + E + W[t] + K[0];
E = D;
D = C;
Expand All @@ -99,7 +106,7 @@ void SHA1hashing::SHA1BlockCipherLike::process(void)
A = temp;
}

for(uint8_t t = 20; t < 40; ++t) {
for(auto t = 20; t < 40; ++t) {
auto temp = rotate_left(A,5) + f2(B,C,D) + E + W[t] + K[1];
E = D;
D = C;
Expand All @@ -108,7 +115,7 @@ void SHA1hashing::SHA1BlockCipherLike::process(void)
A = temp;
}

for(uint8_t t = 40; t < 60; ++t) {
for(auto t = 40; t < 60; ++t) {
auto temp = rotate_left(A,5) + f3(B,C,D) + E + W[t] + K[2];
E = D;
D = C;
Expand All @@ -117,7 +124,7 @@ void SHA1hashing::SHA1BlockCipherLike::process(void)
A = temp;
}

for(uint8_t t = 60; t < 80; ++t) {
for(auto t = 60; t < 80; ++t) {
auto temp = rotate_left(A,5) + f4(B,C,D) + E + W[t] + K[3];
E = D;
D = C;
Expand Down
8 changes: 4 additions & 4 deletions test/test_libcrypto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ using crypto::operator<<;
template <size_t N>
using HashChallenges = std::array< std::pair<const std::string, const std::string>, N >;

template <size_t N_tmphashSize, size_t N_blockSize, size_t N_hashSize>
using HS = crypto::HashingStrategy< N_tmphashSize, N_blockSize, N_hashSize >;
template <size_t N_tmphashSize, typename T_workWord, size_t N_blockSize, size_t N_hashSize>
using HS = crypto::HashingStrategy< N_tmphashSize, T_workWord, N_blockSize, N_hashSize >;

template <size_t N_challenges, size_t N_tmphashSize, size_t N_blockSize, size_t N_hashSize>
void hashProve(HashChallenges<N_challenges>& challenges, HS<N_tmphashSize,N_blockSize, N_hashSize>&& strategy)
template <size_t N_challenges, typename T_workWord, size_t N_tmphashSize, size_t N_blockSize, size_t N_hashSize>
void hashProve(HashChallenges<N_challenges>& challenges, HS<N_tmphashSize,T_workWord,N_blockSize, N_hashSize>&& strategy)
{
std::stringstream ss;
#ifdef SHOW_TIMING
Expand Down

0 comments on commit 41a7c33

Please sign in to comment.