-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
448 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#ifndef _SHA384_HASHING_ | ||
#define _SHA384_HASHING_ | ||
|
||
#include "SHA512384.hpp" | ||
|
||
namespace crypto { | ||
|
||
#define SHA384_HASH_SIZE 48 // (in bytes) | ||
|
||
using SHA384hash = CryptoHash<SHA384_HASH_SIZE>; | ||
|
||
class SHA384hashing final : public SHA512384hashing<SHA384_HASH_SIZE> | ||
{ | ||
public: | ||
|
||
SHA384hashing(void); | ||
virtual ~SHA384hashing() = default; | ||
|
||
SHA384hashing(const SHA384hashing& other) = delete; | ||
SHA384hashing& operator=(const SHA384hashing& other) = delete; | ||
|
||
SHA384hashing(SHA384hashing&& other) = default; | ||
SHA384hashing& operator=(SHA384hashing&& other) = default; | ||
|
||
private: | ||
|
||
class SHA384BlockCipherLike final : public SHA512384BlockCipherLike | ||
{ | ||
public: | ||
SHA384BlockCipherLike(void); | ||
virtual ~SHA384BlockCipherLike() = default; | ||
|
||
SHA384BlockCipherLike(const SHA384BlockCipherLike& other) = delete; | ||
SHA384BlockCipherLike& operator=(const SHA384BlockCipherLike& other) = delete; | ||
|
||
SHA384BlockCipherLike(SHA384BlockCipherLike&& other) = default; | ||
SHA384BlockCipherLike& operator=(SHA384BlockCipherLike&& other) = default; | ||
|
||
virtual void reset(void) final override; | ||
}; | ||
}; | ||
|
||
} /* namespace crypto */ | ||
|
||
#endif | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#ifndef _SHA512_HASHING_ | ||
#define _SHA512_HASHING_ | ||
|
||
#include "SHA512384.hpp" | ||
|
||
namespace crypto { | ||
|
||
#define SHA512_HASH_SIZE 64 // (in bytes) | ||
|
||
using SHA512hash = CryptoHash<SHA512_HASH_SIZE>; | ||
|
||
class SHA512hashing final : public SHA512384hashing<SHA512_HASH_SIZE> | ||
{ | ||
public: | ||
|
||
SHA512hashing(void); | ||
virtual ~SHA512hashing() = default; | ||
|
||
SHA512hashing(const SHA512hashing& other) = delete; | ||
SHA512hashing& operator=(const SHA512hashing& other) = delete; | ||
|
||
SHA512hashing(SHA512hashing&& other) = default; | ||
SHA512hashing& operator=(SHA512hashing&& other) = default; | ||
|
||
private: | ||
|
||
class SHA512BlockCipherLike final : public SHA512384BlockCipherLike | ||
{ | ||
public: | ||
SHA512BlockCipherLike(void); | ||
virtual ~SHA512BlockCipherLike() = default; | ||
|
||
SHA512BlockCipherLike(const SHA512BlockCipherLike& other) = delete; | ||
SHA512BlockCipherLike& operator=(const SHA512BlockCipherLike& other) = delete; | ||
|
||
SHA512BlockCipherLike(SHA512BlockCipherLike&& other) = default; | ||
SHA512BlockCipherLike& operator=(SHA512BlockCipherLike&& other) = default; | ||
|
||
virtual void reset(void) final override; | ||
}; | ||
}; | ||
|
||
} /* namespace crypto */ | ||
|
||
#endif | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#ifndef _SHA512384_HASHING_ | ||
#define _SHA512384_HASHING_ | ||
|
||
#include "HashingStrategy.hpp" | ||
|
||
namespace crypto { | ||
|
||
#define SHA512384_TMPHASH_SIZE 64 // (in bytes) | ||
#define SHA512384_MSGBLOCK_SIZE 128 // (in bytes) | ||
|
||
template <size_t N> | ||
using SHA512384hash = CryptoHash<N>; | ||
|
||
template <size_t N_digest> | ||
class SHA512384hashing : public HashingStrategy<SHA512384_TMPHASH_SIZE, uint64_t, SHA512384_MSGBLOCK_SIZE, N_digest> | ||
{ | ||
protected: | ||
|
||
class SHA512384BlockCipherLike; | ||
|
||
template <size_t N> | ||
using HS = HashingStrategy<SHA512384_TMPHASH_SIZE, uint64_t, SHA512384_MSGBLOCK_SIZE, N>; | ||
|
||
SHA512384hashing(std::unique_ptr< typename HS<N_digest>::StrategyBlockCipherLike >&& p); | ||
SHA512384hashing(void) = delete; | ||
virtual ~SHA512384hashing() = default; | ||
|
||
SHA512384hashing(const SHA512384hashing& other) = delete; | ||
SHA512384hashing& operator=(const SHA512384hashing& other) = delete; | ||
|
||
SHA512384hashing(SHA512384hashing&& other) = default; | ||
SHA512384hashing& operator=(SHA512384hashing&& other) = default; | ||
|
||
class SHA512384BlockCipherLike : public HS<N_digest>::StrategyBlockCipherLike | ||
{ | ||
private: | ||
virtual void process(void) final override; | ||
virtual SHA512384hash<N_digest> getDigest(void) final override; | ||
virtual void setMsgSize(size_t size) final override; | ||
|
||
public: | ||
SHA512384BlockCipherLike(void) = default; | ||
virtual ~SHA512384BlockCipherLike() = default; | ||
|
||
SHA512384BlockCipherLike(const SHA512384BlockCipherLike& other) = delete; | ||
SHA512384BlockCipherLike& operator=(const SHA512384BlockCipherLike& other) = delete; | ||
|
||
SHA512384BlockCipherLike(SHA512384BlockCipherLike&& other) = default; | ||
SHA512384BlockCipherLike& operator=(SHA512384BlockCipherLike&& other) = default; | ||
|
||
virtual void reset(void) = 0; | ||
}; | ||
|
||
}; | ||
|
||
} /* namespace crypto */ | ||
|
||
#include "SHA512384.ipp" | ||
|
||
#endif | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
#include "utils.hpp" | ||
|
||
#include <endian.h> | ||
#include <cstring> | ||
|
||
namespace crypto { | ||
|
||
using namespace utils; | ||
|
||
using SHA512384MsgBlock_uint64 = MsgBlock_uint64<SHA512384_MSGBLOCK_SIZE>; | ||
|
||
namespace sha512384_detail { | ||
template <size_t N> | ||
using HS = HashingStrategy<SHA512384_TMPHASH_SIZE, uint64_t, SHA512384_MSGBLOCK_SIZE, N>; | ||
} /* namespace sha512384_detail */ | ||
|
||
template <size_t N_digest> | ||
SHA512384hashing<N_digest>::SHA512384hashing(std::unique_ptr< typename sha512384_detail::HS<N_digest>::StrategyBlockCipherLike >&& p) : | ||
sha512384_detail::HS<N_digest>(std::move(p)) | ||
{ | ||
} | ||
|
||
template <size_t N_digest> | ||
SHA512384hash<N_digest> SHA512384hashing<N_digest>::SHA512384BlockCipherLike::getDigest(void) | ||
{ | ||
using SHA512384hash_uint64 = CryptoHash_uint64<N_digest>; | ||
SHA512384hash<N_digest> digest; | ||
|
||
#if __BYTE_ORDER == __LITTLE_ENDIAN | ||
auto& dest = *reinterpret_cast<SHA512384hash_uint64*>(digest.data()); | ||
|
||
// write the hash in big endian | ||
auto it_end = this->m_intermediateHash.begin(); | ||
std::advance(it_end, N_digest / sizeof(uint64_t)); | ||
std::transform(this->m_intermediateHash.begin(), | ||
it_end, | ||
dest.begin(), | ||
[] (uint64_t n) { return htobe64(n); }); | ||
#else | ||
memcpy(digest.data(), m_intermediateHash.data(), digest.size()); | ||
#endif | ||
|
||
return std::move(digest); | ||
} | ||
|
||
template <size_t N_digest> | ||
void SHA512384hashing<N_digest>::SHA512384BlockCipherLike::setMsgSize(size_t size) | ||
{ | ||
auto it = (*reinterpret_cast<SHA512384MsgBlock_uint64*>(this->m_msgBlock.data())).end(); | ||
*--it = htobe64(size); | ||
*--it = 0; // assume that a file with a size grather than 2^61 bytes does not exist for now | ||
} | ||
|
||
template <size_t N_digest> | ||
void SHA512384hashing<N_digest>::SHA512384BlockCipherLike::process(void) | ||
{ | ||
auto F0 = [](auto x, auto y, auto z) { return (x & y) | (z & (x | y)); }; | ||
auto F1 = [](auto x, auto y, auto z) { return z ^ (x & (y ^ z)); }; | ||
|
||
auto EP0 = [](uint64_t x) { return rotate_right(x,28) ^ rotate_right(x,34) ^ rotate_right(x,39); }; | ||
auto EP1 = [](uint64_t x) { return rotate_right(x,14) ^ rotate_right(x,18) ^ rotate_right(x,41); }; | ||
|
||
auto SIG0 = [](uint64_t x) { return rotate_right(x,1) ^ rotate_right(x,8) ^ (x >> 7); }; | ||
auto SIG1 = [](uint64_t x) { return rotate_right(x,19) ^ rotate_right(x,61) ^ (x >> 6); }; | ||
|
||
static std::array<const uint64_t,80> K = { | ||
0x428a2f98d728ae22, 0x7137449123ef65cd, | ||
0xb5c0fbcfec4d3b2f, 0xe9b5dba58189dbbc, | ||
0x3956c25bf348b538, 0x59f111f1b605d019, | ||
0x923f82a4af194f9b, 0xab1c5ed5da6d8118, | ||
0xd807aa98a3030242, 0x12835b0145706fbe, | ||
0x243185be4ee4b28c, 0x550c7dc3d5ffb4e2, | ||
0x72be5d74f27b896f, 0x80deb1fe3b1696b1, | ||
0x9bdc06a725c71235, 0xc19bf174cf692694, | ||
0xe49b69c19ef14ad2, 0xefbe4786384f25e3, | ||
0x0fc19dc68b8cd5b5, 0x240ca1cc77ac9c65, | ||
0x2de92c6f592b0275, 0x4a7484aa6ea6e483, | ||
0x5cb0a9dcbd41fbd4, 0x76f988da831153b5, | ||
0x983e5152ee66dfab, 0xa831c66d2db43210, | ||
0xb00327c898fb213f, 0xbf597fc7beef0ee4, | ||
0xc6e00bf33da88fc2, 0xd5a79147930aa725, | ||
0x06ca6351e003826f, 0x142929670a0e6e70, | ||
0x27b70a8546d22ffc, 0x2e1b21385c26c926, | ||
0x4d2c6dfc5ac42aed, 0x53380d139d95b3df, | ||
0x650a73548baf63de, 0x766a0abb3c77b2a8, | ||
0x81c2c92e47edaee6, 0x92722c851482353b, | ||
0xa2bfe8a14cf10364, 0xa81a664bbc423001, | ||
0xc24b8b70d0f89791, 0xc76c51a30654be30, | ||
0xd192e819d6ef5218, 0xd69906245565a910, | ||
0xf40e35855771202a, 0x106aa07032bbd1b8, | ||
0x19a4c116b8d2d0c8, 0x1e376c085141ab53, | ||
0x2748774cdf8eeb99, 0x34b0bcb5e19b48a8, | ||
0x391c0cb3c5c95a63, 0x4ed8aa4ae3418acb, | ||
0x5b9cca4f7763e373, 0x682e6ff3d6b2b8a3, | ||
0x748f82ee5defb2fc, 0x78a5636f43172f60, | ||
0x84c87814a1f0ab72, 0x8cc702081a6439ec, | ||
0x90befffa23631e28, 0xa4506cebde82bde9, | ||
0xbef9a3f7b2c67915, 0xc67178f2e372532b, | ||
0xca273eceea26619c, 0xd186b8c721c0c207, | ||
0xeada7dd6cde0eb1e, 0xf57d4f7fee6ed178, | ||
0x06f067aa72176fba, 0x0a637dc5a2c898a6, | ||
0x113f9804bef90dae, 0x1b710b35131c471b, | ||
0x28db77f523047d84, 0x32caab7b40c72493, | ||
0x3c9ebe0a15c9bebc, 0x431d67c49c100d4c, | ||
0x4cc5d4becb3e42b6, 0x597f299cfc657e2a, | ||
0x5fcb6fab3ad6faec, 0x6c44198c4a475817 | ||
}; | ||
|
||
std::array<uint64_t, 80> W; // word sequence | ||
uint64_t A, B, C, D, E, F, G, H; // word buffers | ||
|
||
// initialize the first 16 words in the array W with the message block | ||
auto& msgBlockUInt64 = *reinterpret_cast<SHA512384MsgBlock_uint64*>(this->m_msgBlock.data()); | ||
std::transform(msgBlockUInt64.begin(), | ||
msgBlockUInt64.end(), | ||
W.begin(), | ||
[] (uint64_t n) { return htobe64(n); }); | ||
|
||
for (auto t = msgBlockUInt64.size(); t < W.size(); ++t) { | ||
W[t] = SIG1(W[t - 2]) + W[t - 7] + SIG0(W[t - 15]) + W[t - 16]; | ||
} | ||
|
||
A = this->m_intermediateHash[0]; | ||
B = this->m_intermediateHash[1]; | ||
C = this->m_intermediateHash[2]; | ||
D = this->m_intermediateHash[3]; | ||
E = this->m_intermediateHash[4]; | ||
F = this->m_intermediateHash[5]; | ||
G = this->m_intermediateHash[6]; | ||
H = this->m_intermediateHash[7]; | ||
|
||
for (auto t = 0U; t < W.size(); ++t) { | ||
auto T1 = H + EP1(E) + F1(E,F,G) + K[t] + W[t]; | ||
auto T2 = EP0(A) + F0(A,B,C); | ||
H = G; | ||
G = F; | ||
F = E; | ||
E = D + T1; | ||
D = C; | ||
C = B; | ||
B = A; | ||
A = T1 + T2; | ||
} | ||
|
||
this->m_intermediateHash[0] += A; | ||
this->m_intermediateHash[1] += B; | ||
this->m_intermediateHash[2] += C; | ||
this->m_intermediateHash[3] += D; | ||
this->m_intermediateHash[4] += E; | ||
this->m_intermediateHash[5] += F; | ||
this->m_intermediateHash[6] += G; | ||
this->m_intermediateHash[7] += H; | ||
|
||
this->m_msgBlockIndex = 0; | ||
} | ||
|
||
} /* namespace crypto */ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#include "SHA384.hpp" | ||
#include <cstring> | ||
|
||
namespace crypto { | ||
|
||
using HS512384 = SHA512384hashing<SHA384_HASH_SIZE>; | ||
|
||
SHA384hashing::SHA384hashing(void) : | ||
HS512384(std::make_unique<SHA384hashing::SHA384BlockCipherLike>()) | ||
{ | ||
} | ||
|
||
SHA384hashing::SHA384BlockCipherLike::SHA384BlockCipherLike(void) | ||
: HS512384::SHA512384BlockCipherLike() | ||
{ | ||
reset(); | ||
} | ||
|
||
void SHA384hashing::SHA384BlockCipherLike::reset(void) | ||
{ | ||
memset(m_msgBlock.data(), 0, sizeof(m_msgBlock)); | ||
m_msgBlockIndex = 0; | ||
m_intermediateHash = { | ||
0xcbbb9d5dc1059ed8, | ||
0x629a292a367cd507, | ||
0x9159015a3070dd17, | ||
0x152fecd8f70e5939, | ||
0x67332667ffc00b31, | ||
0x8eb44a8768581511, | ||
0xdb0c2e0d64f98fa7, | ||
0x47b5481dbefa4fa4 | ||
}; | ||
} | ||
|
||
} /* namespace crypto */ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#include "SHA512.hpp" | ||
#include <cstring> | ||
|
||
namespace crypto { | ||
|
||
using HS512384 = SHA512384hashing<SHA512_HASH_SIZE>; | ||
|
||
SHA512hashing::SHA512hashing(void) : | ||
HS512384(std::make_unique<SHA512hashing::SHA512BlockCipherLike>()) | ||
{ | ||
} | ||
|
||
SHA512hashing::SHA512BlockCipherLike::SHA512BlockCipherLike(void) | ||
: HS512384::SHA512384BlockCipherLike() | ||
{ | ||
reset(); | ||
} | ||
|
||
void SHA512hashing::SHA512BlockCipherLike::reset(void) | ||
{ | ||
memset(m_msgBlock.data(), 0, sizeof(m_msgBlock)); | ||
m_msgBlockIndex = 0; | ||
m_intermediateHash = { | ||
0x6a09e667f3bcc908, | ||
0xbb67ae8584caa73b, | ||
0x3c6ef372fe94f82b, | ||
0xa54ff53a5f1d36f1, | ||
0x510e527fade682d1, | ||
0x9b05688c2b3e6c1f, | ||
0x1f83d9abfb41bd6b, | ||
0x5be0cd19137e2179 | ||
}; | ||
} | ||
|
||
} /* namespace crypto */ | ||
|
Oops, something went wrong.