Loading...
Searching...
No Matches
shake.hxx
Go to the documentation of this file.
#ifndef __DECAF_SHAKE_HXX__
#define __DECAF_SHAKE_HXX__
#include <decaf/shake.h>
#include <decaf/secure_buffer.hxx>
#include <sys/types.h>
#if __cplusplus >= 201103L
#define DECAF_NOEXCEPT noexcept
#define DECAF_DELETE = delete
#else
#define DECAF_NOEXCEPT throw()
#define DECAF_DELETE
#endif
namespace decaf {
class KeccakHash {
protected:
decaf_keccak_sponge_t wrapped;
inline KeccakHash(const decaf_kparams_s *params) DECAF_NOEXCEPT { decaf_sha3_init(wrapped, params); }
public:
inline void update(const uint8_t *__restrict__ in, size_t len) DECAF_NOEXCEPT { decaf_sha3_update(wrapped,in,len); }
inline void update(const Block &s) DECAF_NOEXCEPT { decaf_sha3_update(wrapped,s.data(),s.size()); }
inline KeccakHash &operator<<(const Block &s) DECAF_NOEXCEPT { update(s); return *this; }
inline KeccakHash &operator+=(const Block &s) DECAF_NOEXCEPT { return *this << s; }
inline SecureBuffer output(size_t len) /*throw(std::bad_alloc, LengthException)*/ {
if (len > max_output_size()) throw LengthException();
SecureBuffer buffer(len);
if (DECAF_SUCCESS != decaf_sha3_output(wrapped,buffer.data(),len)) {
throw LengthException();
}
return buffer;
}
inline SecureBuffer final(size_t len) /*throw(std::bad_alloc, LengthException)*/ {
if (len > max_output_size()) throw LengthException();
SecureBuffer buffer(len);
if (DECAF_SUCCESS != decaf_sha3_final(wrapped,buffer.data(),len)) {
throw LengthException();
}
return buffer;
}
inline void output(Buffer b) /*throw(LengthException)*/ {
if (DECAF_SUCCESS != decaf_sha3_output(wrapped,b.data(),b.size())) {
throw LengthException();
}
}
inline void final(Buffer b) /*throw(LengthException)*/ {
if (DECAF_SUCCESS != decaf_sha3_final(wrapped,b.data(),b.size())) {
throw LengthException();
}
}
inline size_t default_output_size() const DECAF_NOEXCEPT {
return decaf_sha3_default_output_bytes(wrapped);
}
inline size_t max_output_size() const DECAF_NOEXCEPT {
return decaf_sha3_max_output_bytes(wrapped);
}
inline SecureBuffer output() /*throw(std::bad_alloc,LengthException)*/ {
return output(default_output_size());
}
inline SecureBuffer final() /*throw(std::bad_alloc,LengthException)*/ {
return final(default_output_size());
}
inline void reset() DECAF_NOEXCEPT { decaf_sha3_reset(wrapped); }
inline ~KeccakHash() DECAF_NOEXCEPT { decaf_sha3_destroy(wrapped); }
};
template<int bits> class SHA3 : public KeccakHash {
private:
static inline const struct decaf_kparams_s *get_params();
public:
static const size_t MAX_OUTPUT_BYTES = bits/8;
static const size_t DEFAULT_OUTPUT_BYTES = bits/8;
inline SHA3() DECAF_NOEXCEPT : KeccakHash(get_params()) {}
static inline SecureBuffer hash(const Block &b, size_t nbytes = MAX_OUTPUT_BYTES) /*throw(std::bad_alloc, LengthException)*/ {
if (nbytes > MAX_OUTPUT_BYTES) {
throw LengthException();
}
SHA3 s; s += b; return s.output(nbytes);
}
};
template<int bits>
class SHAKE : public KeccakHash {
private:
static inline const struct decaf_kparams_s *get_params();
public:
#if __cplusplus >= 201103L
static const size_t MAX_OUTPUT_BYTES = SIZE_MAX;
#else
static const size_t MAX_OUTPUT_BYTES = (size_t)-1;
#endif
static const size_t DEFAULT_OUTPUT_BYTES = bits/4;
inline SHAKE() DECAF_NOEXCEPT : KeccakHash(get_params()) {}
static inline SecureBuffer hash(const Block &b, size_t outlen) /*throw(std::bad_alloc)*/ {
SHAKE s; s += b; return s.output(outlen);
}
};
#if defined _MSC_VER // MSVC does not want tempalte<> syntax, gcc cannot live without it
inline const struct decaf_kparams_s *SHAKE<128>::get_params() { return &DECAF_SHAKE128_params_s; }
inline const struct decaf_kparams_s *SHAKE<256>::get_params() { return &DECAF_SHAKE256_params_s; }
inline const struct decaf_kparams_s *SHA3<224>::get_params() { return &DECAF_SHA3_224_params_s; }
inline const struct decaf_kparams_s *SHA3<256>::get_params() { return &DECAF_SHA3_256_params_s; }
inline const struct decaf_kparams_s *SHA3<384>::get_params() { return &DECAF_SHA3_384_params_s; }
inline const struct decaf_kparams_s *SHA3<512>::get_params() { return &DECAF_SHA3_512_params_s; }
#else
template<> inline const struct decaf_kparams_s *SHAKE<128>::get_params() { return &DECAF_SHAKE128_params_s; }
template<> inline const struct decaf_kparams_s *SHAKE<256>::get_params() { return &DECAF_SHAKE256_params_s; }
template<> inline const struct decaf_kparams_s *SHA3<224>::get_params() { return &DECAF_SHA3_224_params_s; }
template<> inline const struct decaf_kparams_s *SHA3<256>::get_params() { return &DECAF_SHA3_256_params_s; }
template<> inline const struct decaf_kparams_s *SHA3<384>::get_params() { return &DECAF_SHA3_384_params_s; }
template<> inline const struct decaf_kparams_s *SHA3<512>::get_params() { return &DECAF_SHA3_512_params_s; }
#endif
} /* namespace decaf */
#undef DECAF_NOEXCEPT
#undef DECAF_DELETE
#endif /* __DECAF_SHAKE_HXX__ */
decaf::Block
A reference to a block of data, which (when accessed through this base class) is const.
Definition secure_buffer.hxx:159
decaf::Block::data
const unsigned char * data() const DECAF_NOEXCEPT
Get const data.
Definition secure_buffer.hxx:199
decaf::Block::size
size_t size() const DECAF_NOEXCEPT
Get the size.
Definition secure_buffer.hxx:208
decaf::Buffer
A reference to a writable block of data.
Definition secure_buffer.hxx:270
decaf::Buffer::data
const unsigned char * data() const DECAF_NOEXCEPT
Get const data.
Definition secure_buffer.hxx:282
decaf::FixedBlock
A fixed-size block.
Definition secure_buffer.hxx:253
decaf::KeccakHash
Hash function derived from Keccak FUTURE: throw ProtocolException when hash is misused by calling upd...
Definition shake.hxx:34
decaf::KeccakHash::operator+=
KeccakHash & operator+=(const Block &s) DECAF_NOEXCEPT
Same as <<.
Definition shake.hxx:55
decaf::KeccakHash::output
SecureBuffer output()
Output the default number of bytes.
Definition shake.hxx:106
decaf::KeccakHash::reset
void reset() DECAF_NOEXCEPT
Reset the hash to the empty string.
Definition shake.hxx:116
decaf::KeccakHash::output
SecureBuffer output(size_t len)
Output bytes from the sponge.
Definition shake.hxx:58
decaf::KeccakHash::~KeccakHash
~KeccakHash() DECAF_NOEXCEPT
Destructor zeroizes state.
Definition shake.hxx:119
decaf::KeccakHash::update
void update(const uint8_t *__restrict__ in, size_t len) DECAF_NOEXCEPT
Add more data to running hash.
Definition shake.hxx:46
decaf::KeccakHash::default_output_size
size_t default_output_size() const DECAF_NOEXCEPT
Return the sponge's default output size.
Definition shake.hxx:96
decaf::KeccakHash::output
void output(Buffer b)
Output bytes from the sponge.
Definition shake.hxx:80
decaf::KeccakHash::operator<<
KeccakHash & operator<<(const Block &s) DECAF_NOEXCEPT
Add more data, stream version.
Definition shake.hxx:52
decaf::KeccakHash::max_output_size
size_t max_output_size() const DECAF_NOEXCEPT
Return the sponge's maximum output size.
Definition shake.hxx:101
decaf::KeccakHash::update
void update(const Block &s) DECAF_NOEXCEPT
Add more data to running hash, C++ version.
Definition shake.hxx:49
decaf::LengthException
An exception for when crypto (ie point decode) has failed.
Definition secure_buffer.hxx:126
decaf::SHA3
Fixed-output-length SHA3.
Definition shake.hxx:123
decaf::SHA3::SHA3
SHA3() DECAF_NOEXCEPT
Initializer.
Definition shake.hxx:136
decaf::SHA3::MAX_OUTPUT_BYTES
static const size_t MAX_OUTPUT_BYTES
Number of bytes of output.
Definition shake.hxx:130
decaf::SHA3::hash
static SecureBuffer hash(const Block &b, size_t nbytes=MAX_OUTPUT_BYTES)
Hash bytes with this SHA3 instance.
Definition shake.hxx:141
decaf::SHA3::DEFAULT_OUTPUT_BYTES
static const size_t DEFAULT_OUTPUT_BYTES
Number of bytes of output.
Definition shake.hxx:133
decaf::SHAKE
Variable-output-length SHAKE.
Definition shake.hxx:151
decaf::SHAKE::hash
static SecureBuffer hash(const Block &b, size_t outlen)
Hash bytes with this SHAKE instance.
Definition shake.hxx:171
decaf::SHAKE::DEFAULT_OUTPUT_BYTES
static const size_t DEFAULT_OUTPUT_BYTES
Default number of bytes to output.
Definition shake.hxx:165
decaf::SHAKE::SHAKE
SHAKE() DECAF_NOEXCEPT
Initializer.
Definition shake.hxx:168
decaf::SHAKE::MAX_OUTPUT_BYTES
static const size_t MAX_OUTPUT_BYTES
Number of bytes of output.
Definition shake.hxx:161
DECAF_SUCCESS
@ DECAF_SUCCESS
The operation succeeded.
Definition common.h:121
decaf
Namespace for all C++ decaf objects.
Definition decaf.hxx:22
decaf::SecureBuffer
std::vector< unsigned char, SanitizingAllocator< unsigned char, 0 > > SecureBuffer
A variant of std::vector which securely zerozes its state when destructed.
Definition secure_buffer.hxx:79
secure_buffer.hxx
C++ self-zeroizing buffer.
shake.h
SHA2-512.
decaf_sha3_destroy
void DECAF_API_VIS decaf_sha3_destroy(decaf_keccak_sponge_t sponge)
Destroy a DECAF_SHA3 or DECAF_SHAKE sponge context by overwriting it with 0.
decaf_sha3_max_output_bytes
size_t DECAF_API_VIS decaf_sha3_max_output_bytes(const decaf_keccak_sponge_t sponge)
Return the default output length of the sponge construction, for the purpose of C++ default operators...
decaf_keccak_sponge_t
struct decaf_keccak_sponge_s decaf_keccak_sponge_t[1]
Convenience GMP-style one-element array version.
Definition shake.h:33
decaf_sha3_update
decaf_error_t DECAF_API_VIS decaf_sha3_update(struct decaf_keccak_sponge_s *__restrict__ sponge, const uint8_t *in, size_t len)
Absorb data into a DECAF_SHA3 or DECAF_SHAKE hash context.
decaf_sha3_output
decaf_error_t DECAF_API_VIS decaf_sha3_output(decaf_keccak_sponge_t sponge, uint8_t *__restrict__ out, size_t len)
Squeeze output data from a DECAF_SHA3 or DECAF_SHAKE hash context.
decaf_sha3_init
void DECAF_API_VIS decaf_sha3_init(decaf_keccak_sponge_t sponge, const struct decaf_kparams_s *params)
Initialize a sponge context object.
decaf_sha3_final
decaf_error_t DECAF_API_VIS decaf_sha3_final(decaf_keccak_sponge_t sponge, uint8_t *__restrict__ out, size_t len)
Squeeze output data from a DECAF_SHA3 or DECAF_SHAKE hash context.
decaf_sha3_default_output_bytes
size_t DECAF_API_VIS decaf_sha3_default_output_bytes(const decaf_keccak_sponge_t sponge)
Return the default output length of the sponge construction, for the purpose of C++ default operators...
decaf_sha3_reset
void DECAF_API_VIS decaf_sha3_reset(decaf_keccak_sponge_t sponge)
Reset the sponge to the empty string.