diff options
Diffstat (limited to 'libdevcore')
-rw-r--r-- | libdevcore/CommonData.cpp | 2 | ||||
-rw-r--r-- | libdevcore/Keccak256.cpp (renamed from libdevcore/SHA3.cpp) | 96 | ||||
-rw-r--r-- | libdevcore/Keccak256.h (renamed from libdevcore/SHA3.h) | 8 | ||||
-rw-r--r-- | libdevcore/SwarmHash.cpp | 2 |
4 files changed, 23 insertions, 85 deletions
diff --git a/libdevcore/CommonData.cpp b/libdevcore/CommonData.cpp index 91c60ffe..8d2639c9 100644 --- a/libdevcore/CommonData.cpp +++ b/libdevcore/CommonData.cpp @@ -22,7 +22,7 @@ #include <libdevcore/CommonData.h> #include <libdevcore/Exceptions.h> #include <libdevcore/Assertions.h> -#include <libdevcore/SHA3.h> +#include <libdevcore/Keccak256.h> #include <boost/algorithm/string.hpp> diff --git a/libdevcore/SHA3.cpp b/libdevcore/Keccak256.cpp index e41a5e3b..7933fc7e 100644 --- a/libdevcore/SHA3.cpp +++ b/libdevcore/Keccak256.cpp @@ -19,7 +19,8 @@ * @date 2014 */ -#include "SHA3.h" +#include <libdevcore/Keccak256.h> + #include <cstdint> #include <cstdio> #include <cstdlib> @@ -31,7 +32,7 @@ using namespace dev; namespace dev { -namespace keccak +namespace { /** libkeccak-tiny @@ -43,26 +44,6 @@ namespace keccak * but not liability. */ -#define decshake(bits) \ - int shake##bits(uint8_t*, size_t, const uint8_t*, size_t); - -#define decsha3(bits) \ - int sha3_##bits(uint8_t*, size_t, const uint8_t*, size_t); - -#define deckeccak(bits) \ - int keccak##bits(uint8_t*, size_t, const uint8_t*, size_t); - -decshake(128) -decshake(256) -decsha3(224) -decsha3(256) -decsha3(384) -decsha3(512) -deckeccak(224) -deckeccak(256) -deckeccak(384) -deckeccak(512) - /******** The Keccak-f[1600] permutation ********/ /*** Constants. ***/ @@ -164,13 +145,15 @@ mkapply_sd(setout, dst[i] = src[i]) // setout } /** The sponge-based hash construction. **/ -static inline int hash(uint8_t* out, size_t outlen, - const uint8_t* in, size_t inlen, - size_t rate, uint8_t delim) { - if ((out == NULL) || ((in == NULL) && inlen != 0) || (rate >= Plen)) - { - return -1; - } +inline void hash( + uint8_t* out, + size_t outlen, + const uint8_t* in, + size_t inlen, + size_t rate, + uint8_t delim +) +{ uint8_t a[Plen] = {0}; // Absorb input. foldP(in, inlen, xorin); @@ -185,58 +168,19 @@ static inline int hash(uint8_t* out, size_t outlen, foldP(out, outlen, setout); setout(a, out, outlen); memset(a, 0, 200); - return 0; } -/*** Helper macros to define SHA3 and SHAKE instances. ***/ -#define defshake(bits) \ - int shake##bits(uint8_t* out, size_t outlen, \ - const uint8_t* in, size_t inlen) { \ - return hash(out, outlen, in, inlen, 200 - (bits / 4), 0x1f); \ - } -#define defsha3(bits) \ - int sha3_##bits(uint8_t* out, size_t outlen, \ - const uint8_t* in, size_t inlen) { \ - if (outlen > (bits/8)) { \ - return -1; \ - } \ - return hash(out, outlen, in, inlen, 200 - (bits / 4), 0x06); \ - } -#define defkeccak(bits) \ - int keccak##bits(uint8_t* out, size_t outlen, \ - const uint8_t* in, size_t inlen) { \ - if (outlen > (bits/8)) { \ - return -1; \ - } \ - return hash(out, outlen, in, inlen, 200 - (bits / 4), 0x01); \ - } - -/*** FIPS202 SHAKE VOFs ***/ -defshake(128) -defshake(256) - -/*** FIPS202 SHA3 FOFs ***/ -defsha3(224) -defsha3(256) -defsha3(384) -defsha3(512) - -/*** KECCAK FOFs ***/ -defkeccak(224) -defkeccak(256) -defkeccak(384) -defkeccak(512) - } -bool keccak256(bytesConstRef _input, bytesRef o_output) +h256 keccak256(bytesConstRef _input) { - // FIXME: What with unaligned memory? - if (o_output.size() != 32) - return false; - keccak::keccak256(o_output.data(), 32, _input.data(), _input.size()); -// keccak::keccak(ret.data(), 32, (uint64_t const*)_input.data(), _input.size()); - return true; + h256 output; + // Parameters used: + // The 0x01 is the specific padding for keccak (sha3 uses 0x06) and + // the way the round size (or window or whatever it was) is calculated. + // 200 - (256 / 4) is the "rate" + hash(output.data(), output.size, _input.data(), _input.size(), 200 - (256 / 4), 0x01); + return output; } } diff --git a/libdevcore/SHA3.h b/libdevcore/Keccak256.h index d1e2cc98..0d5f69bb 100644 --- a/libdevcore/SHA3.h +++ b/libdevcore/Keccak256.h @@ -30,14 +30,8 @@ namespace dev { -// Keccak-256 convenience routines. - -/// Calculate Keccak-256 hash of the given input and load it into the given output. -/// @returns false if o_output.size() != 32. -bool keccak256(bytesConstRef _input, bytesRef o_output); - /// Calculate Keccak-256 hash of the given input, returning as a 256-bit hash. -inline h256 keccak256(bytesConstRef _input) { h256 ret; keccak256(_input, ret.ref()); return ret; } +h256 keccak256(bytesConstRef _input); /// Calculate Keccak-256 hash of the given input, returning as a 256-bit hash. inline h256 keccak256(bytes const& _input) { return keccak256(bytesConstRef(&_input)); } diff --git a/libdevcore/SwarmHash.cpp b/libdevcore/SwarmHash.cpp index 1c718200..3b8d2f3e 100644 --- a/libdevcore/SwarmHash.cpp +++ b/libdevcore/SwarmHash.cpp @@ -19,7 +19,7 @@ #include <libdevcore/SwarmHash.h> -#include <libdevcore/SHA3.h> +#include <libdevcore/Keccak256.h> using namespace std; using namespace dev; |