Skip to content

Commit

Permalink
Refactorize SHA256 and add SHA224
Browse files Browse the repository at this point in the history
  • Loading branch information
matlo607 committed Nov 26, 2016
1 parent bc8c713 commit 42140b0
Show file tree
Hide file tree
Showing 11 changed files with 325 additions and 147 deletions.
4 changes: 2 additions & 2 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_digest, size_t N_msgBlock>
template <size_t N_tmpdigest, size_t N_msgBlock, size_t N_digest = N_tmpdigest>
class HashingStrategy
{
public:
Expand Down Expand Up @@ -76,7 +76,7 @@ namespace crypto {
MsgBlock_uint8<N_msgBlock> m_msgBlock;
uint16_t m_msgBlockIndex;

CryptoHash_uint32<N_digest> m_intermediateHash;
CryptoHash_uint32<N_tmpdigest> m_intermediateHash;

virtual void process(void) = 0;
virtual CryptoHash<N_digest> getDigest(void) = 0;
Expand Down
41 changes: 21 additions & 20 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_digest, size_t N_msgBlock>
HashingStrategy<N_digest,N_msgBlock>::HashingStrategy(std::unique_ptr<StrategyBlockCipherLike>&& p) :
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) :
m_msgLength(0),
m_blockCipherStrategy(std::move(p))
{}

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

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

template <size_t N_digest, size_t N_msgBlock>
bool HashingStrategy<N_digest,N_msgBlock>::update(const uint8_t buf[], size_t len, size_t offset)
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)
{
assert(buf != NULL && offset <= len);

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

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

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

template <size_t N_digest, size_t N_msgBlock>
HashingStrategy<N_digest,N_msgBlock>::StrategyBlockCipherLike::StrategyBlockCipherLike(StrategyBlockCipherLike&& other) :
template <size_t N_tmpdigest, size_t N_msgBlock, size_t N_digest>
HashingStrategy<N_tmpdigest,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_digest, size_t N_msgBlock>
typename HashingStrategy<N_digest,N_msgBlock>::StrategyBlockCipherLike& HashingStrategy<N_digest,N_msgBlock>::StrategyBlockCipherLike::operator=(StrategyBlockCipherLike&& other)
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)
{
if (this != &other) {
m_msgBlockIndex = other.m_msgBlockIndex;
Expand All @@ -117,8 +118,8 @@ namespace crypto {
}


template <size_t N_digest, size_t N_msgBlock>
size_t HashingStrategy<N_digest,N_msgBlock>::StrategyBlockCipherLike::write(const uint8_t buf[], size_t len)
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)
{
assert(buf != NULL && len > 0);

Expand All @@ -137,8 +138,8 @@ namespace crypto {
return toWrite;
}

template <size_t N_digest, size_t N_msgBlock>
CryptoHash<N_digest> HashingStrategy<N_digest,N_msgBlock>::StrategyBlockCipherLike::addPadding(size_t len)
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)
{
const size_t MSGLENGTH_offset = sizeof(MsgBlock_uint8<N_msgBlock>) - sizeof(uint64_t);

Expand Down
46 changes: 46 additions & 0 deletions include/SHA224.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#ifndef _SHA224_HASHING_
#define _SHA224_HASHING_

#include "SHA256224.hpp"

namespace crypto {

#define SHA224_HASH_SIZE 28 // (in bytes)

using SHA224hash = CryptoHash<SHA224_HASH_SIZE>;

class SHA224hashing final : public SHA256224hashing<SHA224_HASH_SIZE>
{
public:

SHA224hashing(void);
virtual ~SHA224hashing() = default;

SHA224hashing(const SHA224hashing& other) = delete;
SHA224hashing& operator=(const SHA224hashing& other) = delete;

SHA224hashing(SHA224hashing&& other) = default;
SHA224hashing& operator=(SHA224hashing&& other) = default;

private:

class SHA224BlockCipherLike final : public SHA256224BlockCipherLike
{
public:
SHA224BlockCipherLike(void);
virtual ~SHA224BlockCipherLike() = default;

SHA224BlockCipherLike(const SHA224BlockCipherLike& other) = delete;
SHA224BlockCipherLike& operator=(const SHA224BlockCipherLike& other) = delete;

SHA224BlockCipherLike(SHA224BlockCipherLike&& other) = default;
SHA224BlockCipherLike& operator=(SHA224BlockCipherLike&& other) = default;

virtual void reset(void) final override;
};
};

} /* namespace crypto */

#endif

18 changes: 6 additions & 12 deletions include/SHA256.hpp
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
#ifndef _SHA256_HASHING_
#define _SHA256_HASHING_

#include "HashingStrategy.hpp"
#include "SHA256224.hpp"

namespace crypto {

#define SHA256_HASH_SIZE 32 // (in bytes)
#define SHA256_MSGBLOCK_SIZE 64 // (in bytes)

using SHA256hash = CryptoHash<SHA256_HASH_SIZE>;

class SHA256hashing final : public HashingStrategy<SHA256_HASH_SIZE, SHA256_MSGBLOCK_SIZE>
class SHA256hashing final : public SHA256224hashing<SHA256_HASH_SIZE>
{
public:

SHA256hashing(void);
~SHA256hashing() = default;
virtual ~SHA256hashing() = default;

SHA256hashing(const SHA256hashing& other) = delete;
SHA256hashing& operator=(const SHA256hashing& other) = delete;
Expand All @@ -25,24 +24,19 @@ namespace crypto {

private:

class SHA256BlockCipherLike final : public StrategyBlockCipherLike
class SHA256BlockCipherLike final : public SHA256224BlockCipherLike
{
private:
virtual void process(void) final override;
virtual SHA256hash getDigest(void) final override;
virtual void setMsgSize(size_t size) final override;

public:
SHA256BlockCipherLike(void);
~SHA256BlockCipherLike() = default;
virtual ~SHA256BlockCipherLike() = default;

SHA256BlockCipherLike(const SHA256BlockCipherLike& other) = delete;
SHA256BlockCipherLike& operator=(const SHA256BlockCipherLike& other) = delete;

SHA256BlockCipherLike(SHA256BlockCipherLike&& other) = default;
SHA256BlockCipherLike& operator=(SHA256BlockCipherLike&& other) = default;

void reset(void) final override;
virtual void reset(void) final override;
};
};

Expand Down
63 changes: 63 additions & 0 deletions include/SHA256224.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#ifndef _SHA256224_HASHING_
#define _SHA256224_HASHING_

#include "HashingStrategy.hpp"

namespace crypto {

#define SHA256224_TMPHASH_SIZE 32 // (in bytes)
#define SHA256224_MSGBLOCK_SIZE 64 // (in bytes)

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>
{
protected:

class SHA256224BlockCipherLike;


SHA256224hashing(std::unique_ptr< typename HS<N_digest>::StrategyBlockCipherLike >&& p);
SHA256224hashing(void) = delete;
virtual ~SHA256224hashing() = default;

SHA256224hashing(const SHA256224hashing& other) = delete;
SHA256224hashing& operator=(const SHA256224hashing& other) = delete;

SHA256224hashing(SHA256224hashing&& other) = default;
SHA256224hashing& operator=(SHA256224hashing&& other) = default;

class SHA256224BlockCipherLike : public HS<N_digest>::StrategyBlockCipherLike
{
private:
virtual void process(void) final override;
virtual SHA256224hash<N_digest> getDigest(void) final override;
virtual void setMsgSize(size_t size) final override;

public:
SHA256224BlockCipherLike(void) = default;
virtual ~SHA256224BlockCipherLike() = default;

SHA256224BlockCipherLike(const SHA256224BlockCipherLike& other) = delete;
SHA256224BlockCipherLike& operator=(const SHA256224BlockCipherLike& other) = delete;

SHA256224BlockCipherLike(SHA256224BlockCipherLike&& other) = default;
SHA256224BlockCipherLike& operator=(SHA256224BlockCipherLike&& other) = default;

virtual void reset(void) = 0;
};

};

} /* namespace crypto */

#include "SHA256224.ipp"

#endif

Loading

0 comments on commit 42140b0

Please sign in to comment.