libdecaf
shake.hxx
Go to the documentation of this file.
1 
11 #ifndef __DECAF_SHAKE_HXX__
12 #define __DECAF_SHAKE_HXX__
13 
14 #include <decaf/shake.h>
15 #include <decaf/secure_buffer.hxx>
16 #include <sys/types.h>
17 
19 #if __cplusplus >= 201103L
20 #define DECAF_NOEXCEPT noexcept
21 #define DECAF_DELETE = delete
22 #else
23 #define DECAF_NOEXCEPT throw()
24 #define DECAF_DELETE
25 #endif
28 namespace decaf {
29 
34 class KeccakHash {
35 protected:
38  decaf_keccak_sponge_t wrapped;
39 
41  inline KeccakHash(const decaf_kparams_s *params) DECAF_NOEXCEPT { decaf_sha3_init(wrapped, params); }
44 public:
46  inline void update(const uint8_t *__restrict__ in, size_t len) DECAF_NOEXCEPT { decaf_sha3_update(wrapped,in,len); }
47 
49  inline void update(const Block &s) DECAF_NOEXCEPT { decaf_sha3_update(wrapped,s.data(),s.size()); }
50 
52  inline KeccakHash &operator<<(const Block &s) DECAF_NOEXCEPT { update(s); return *this; }
53 
55  inline KeccakHash &operator+=(const Block &s) DECAF_NOEXCEPT { return *this << s; }
56 
58  inline SecureBuffer output(size_t len) /*throw(std::bad_alloc, LengthException)*/ {
59  if (len > max_output_size()) throw LengthException();
60  SecureBuffer buffer(len);
61  if (DECAF_SUCCESS != decaf_sha3_output(wrapped,buffer.data(),len)) {
62  throw LengthException();
63  }
64  return buffer;
65  }
66 
68  inline SecureBuffer final(size_t len) /*throw(std::bad_alloc, LengthException)*/ {
69  if (len > max_output_size()) throw LengthException();
70  SecureBuffer buffer(len);
71  if (DECAF_SUCCESS != decaf_sha3_final(wrapped,buffer.data(),len)) {
72  throw LengthException();
73  }
74  return buffer;
75  }
76 
80  inline void output(Buffer b) /*throw(LengthException)*/ {
81  if (DECAF_SUCCESS != decaf_sha3_output(wrapped,b.data(),b.size())) {
82  throw LengthException();
83  }
84  }
85 
89  inline void final(Buffer b) /*throw(LengthException)*/ {
90  if (DECAF_SUCCESS != decaf_sha3_final(wrapped,b.data(),b.size())) {
91  throw LengthException();
92  }
93  }
94 
96  inline size_t default_output_size() const DECAF_NOEXCEPT {
97  return decaf_sha3_default_output_bytes(wrapped);
98  }
99 
101  inline size_t max_output_size() const DECAF_NOEXCEPT {
102  return decaf_sha3_max_output_bytes(wrapped);
103  }
104 
106  inline SecureBuffer output() /*throw(std::bad_alloc,LengthException)*/ {
107  return output(default_output_size());
108  }
109 
111  inline SecureBuffer final() /*throw(std::bad_alloc,LengthException)*/ {
112  return final(default_output_size());
113  }
114 
116  inline void reset() DECAF_NOEXCEPT { decaf_sha3_reset(wrapped); }
117 
119  inline ~KeccakHash() DECAF_NOEXCEPT { decaf_sha3_destroy(wrapped); }
120 };
121 
123 template<int bits> class SHA3 : public KeccakHash {
124 private:
126  static inline const struct decaf_kparams_s *get_params();
127 
128 public:
130  static const size_t MAX_OUTPUT_BYTES = bits/8;
131 
133  static const size_t DEFAULT_OUTPUT_BYTES = bits/8;
134 
136  inline SHA3() DECAF_NOEXCEPT : KeccakHash(get_params()) {}
137 
141  static inline SecureBuffer hash(const Block &b, size_t nbytes = MAX_OUTPUT_BYTES) /*throw(std::bad_alloc, LengthException)*/ {
142  if (nbytes > MAX_OUTPUT_BYTES) {
143  throw LengthException();
144  }
145  SHA3 s; s += b; return s.output(nbytes);
146  }
147 };
148 
150 template<int bits>
151 class SHAKE : public KeccakHash {
152 private:
154  static inline const struct decaf_kparams_s *get_params();
155 
156 public:
158 #if __cplusplus >= 201103L
159  static const size_t MAX_OUTPUT_BYTES = SIZE_MAX;
160 #else
161  static const size_t MAX_OUTPUT_BYTES = (size_t)-1;
162 #endif
163 
165  static const size_t DEFAULT_OUTPUT_BYTES = bits/4;
166 
168  inline SHAKE() DECAF_NOEXCEPT : KeccakHash(get_params()) {}
169 
171  static inline SecureBuffer hash(const Block &b, size_t outlen) /*throw(std::bad_alloc)*/ {
172  SHAKE s; s += b; return s.output(outlen);
173  }
174 };
175 
176 
177 #if defined _MSC_VER // MSVC does not want tempalte<> syntax, gcc cannot live without it
179 inline const struct decaf_kparams_s *SHAKE<128>::get_params() { return &DECAF_SHAKE128_params_s; }
180 inline const struct decaf_kparams_s *SHAKE<256>::get_params() { return &DECAF_SHAKE256_params_s; }
181 inline const struct decaf_kparams_s *SHA3<224>::get_params() { return &DECAF_SHA3_224_params_s; }
182 inline const struct decaf_kparams_s *SHA3<256>::get_params() { return &DECAF_SHA3_256_params_s; }
183 inline const struct decaf_kparams_s *SHA3<384>::get_params() { return &DECAF_SHA3_384_params_s; }
184 inline const struct decaf_kparams_s *SHA3<512>::get_params() { return &DECAF_SHA3_512_params_s; }
186 #else
188 template<> inline const struct decaf_kparams_s *SHAKE<128>::get_params() { return &DECAF_SHAKE128_params_s; }
189 template<> inline const struct decaf_kparams_s *SHAKE<256>::get_params() { return &DECAF_SHAKE256_params_s; }
190 template<> inline const struct decaf_kparams_s *SHA3<224>::get_params() { return &DECAF_SHA3_224_params_s; }
191 template<> inline const struct decaf_kparams_s *SHA3<256>::get_params() { return &DECAF_SHA3_256_params_s; }
192 template<> inline const struct decaf_kparams_s *SHA3<384>::get_params() { return &DECAF_SHA3_384_params_s; }
193 template<> inline const struct decaf_kparams_s *SHA3<512>::get_params() { return &DECAF_SHA3_512_params_s; }
195 #endif
196 
197 
198 } /* namespace decaf */
199 
200 #undef DECAF_NOEXCEPT
201 #undef DECAF_DELETE
202 
203 #endif /* __DECAF_SHAKE_HXX__ */
A reference to a block of data, which (when accessed through this base class) is const.
Definition: secure_buffer.hxx:159
size_t size() const DECAF_NOEXCEPT
Get the size.
Definition: secure_buffer.hxx:202
A reference to a writable block of data.
Definition: secure_buffer.hxx:264
const unsigned char * data() const DECAF_NOEXCEPT
Get const data.
Definition: secure_buffer.hxx:276
Hash function derived from Keccak FUTURE: throw ProtocolException when hash is misused by calling upd...
Definition: shake.hxx:34
SecureBuffer output()
Output the default number of bytes.
Definition: shake.hxx:106
void reset() DECAF_NOEXCEPT
Reset the hash to the empty string.
Definition: shake.hxx:116
SecureBuffer output(size_t len)
Output bytes from the sponge.
Definition: shake.hxx:58
~KeccakHash() DECAF_NOEXCEPT
Destructor zeroizes state.
Definition: shake.hxx:119
void update(const uint8_t *__restrict__ in, size_t len) DECAF_NOEXCEPT
Add more data to running hash.
Definition: shake.hxx:46
size_t default_output_size() const DECAF_NOEXCEPT
Return the sponge's default output size.
Definition: shake.hxx:96
void output(Buffer b)
Output bytes from the sponge.
Definition: shake.hxx:80
KeccakHash & operator<<(const Block &s) DECAF_NOEXCEPT
Add more data, stream version.
Definition: shake.hxx:52
size_t max_output_size() const DECAF_NOEXCEPT
Return the sponge's maximum output size.
Definition: shake.hxx:101
void update(const Block &s) DECAF_NOEXCEPT
Add more data to running hash, C++ version.
Definition: shake.hxx:49
KeccakHash & operator+=(const Block &s) DECAF_NOEXCEPT
Same as <<.
Definition: shake.hxx:55
An exception for when crypto (ie point decode) has failed.
Definition: secure_buffer.hxx:126
Fixed-output-length SHA3.
Definition: shake.hxx:123
SHA3() DECAF_NOEXCEPT
Initializer.
Definition: shake.hxx:136
static const size_t MAX_OUTPUT_BYTES
Number of bytes of output.
Definition: shake.hxx:130
static SecureBuffer hash(const Block &b, size_t nbytes=MAX_OUTPUT_BYTES)
Hash bytes with this SHA3 instance.
Definition: shake.hxx:141
static const size_t DEFAULT_OUTPUT_BYTES
Number of bytes of output.
Definition: shake.hxx:133
Variable-output-length SHAKE.
Definition: shake.hxx:151
static SecureBuffer hash(const Block &b, size_t outlen)
Hash bytes with this SHAKE instance.
Definition: shake.hxx:171
static const size_t DEFAULT_OUTPUT_BYTES
Default number of bytes to output.
Definition: shake.hxx:165
SHAKE() DECAF_NOEXCEPT
Initializer.
Definition: shake.hxx:168
static const size_t MAX_OUTPUT_BYTES
Number of bytes of output.
Definition: shake.hxx:161
@ DECAF_SUCCESS
The operation succeeded.
Definition: common.h:121
Namespace for all libdecaf C++ objects.
Definition: ed255.hxx:41
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
C++ self-zeroizing buffer.
SHA2-512.
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.
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...
struct decaf_keccak_sponge_s decaf_keccak_sponge_t[1]
Convenience GMP-style one-element array version.
Definition: shake.h:33
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_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.
void DECAF_API_VIS decaf_sha3_init(decaf_keccak_sponge_t sponge, const struct decaf_kparams_s *params)
Initialize a sponge context object.
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.
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...
void DECAF_API_VIS decaf_sha3_reset(decaf_keccak_sponge_t sponge)
Reset the sponge to the empty string.