libdecaf
sha512.hxx
Go to the documentation of this file.
1 
11 #ifndef __DECAF_SHA512_HXX__
12 #define __DECAF_SHA512_HXX__
13 
14 #include <decaf/secure_buffer.hxx>
15 #include <decaf/sha512.h>
16 #include <sys/types.h>
17 
19 #if __cplusplus >= 201103L
20  #define DECAF_NOEXCEPT noexcept
21 #else
22  #define DECAF_NOEXCEPT throw()
23 #endif
26 namespace decaf {
27 
29 class SHA512 {
30 protected:
33  decaf_sha512_ctx_t wrapped;
36 public:
38  static const size_t OUTPUT_BYTES = 64;
39 
41  static const size_t MAX_OUTPUT_BYTES = OUTPUT_BYTES;
42 
44  static const size_t DEFAULT_OUTPUT_BYTES = OUTPUT_BYTES;
45 
47  inline SHA512() DECAF_NOEXCEPT { decaf_sha512_init(wrapped); }
48 
50  inline void update(const uint8_t *__restrict__ in, size_t len) DECAF_NOEXCEPT { decaf_sha512_update(wrapped,in,len); }
51 
53  inline void update(const Block &s) DECAF_NOEXCEPT { update(s.data(),s.size()); }
54 
56  inline SHA512 &operator<<(const Block &s) { update(s); return *this; }
57 
59  inline SHA512 &operator+=(const Block &s) { return *this << s; }
60 
62  inline void final(Buffer b) /*throw(LengthException)*/ {
63  if (b.size() > OUTPUT_BYTES) throw LengthException();
64  decaf_sha512_final(wrapped,b.data(),b.size());
65  }
66 
68  inline void reset() DECAF_NOEXCEPT { decaf_sha512_init(wrapped); }
69 
71  inline SecureBuffer final(size_t len = OUTPUT_BYTES) /*throw(LengthException)*/ {
72  if (len > OUTPUT_BYTES) throw LengthException();
73  SecureBuffer buffer(len);
74  decaf_sha512_final(wrapped,buffer.data(),len);
75  return buffer;
76  }
77 
79  inline size_t default_output_size() const DECAF_NOEXCEPT { return OUTPUT_BYTES; }
80 
82  inline size_t max_output_size() const DECAF_NOEXCEPT { return MAX_OUTPUT_BYTES; }
83 
85  static inline SecureBuffer hash (
86  const Block &message,
87  size_t outlen = OUTPUT_BYTES
88  ) /*throw(LengthException, std::bad_alloc)*/ {
89  if (outlen > OUTPUT_BYTES) throw LengthException();
90  SecureBuffer buffer(outlen);
91  decaf_sha512_hash(buffer.data(),outlen,message.data(),message.size());
92  return buffer;
93  }
94 
96  inline ~SHA512() DECAF_NOEXCEPT { decaf_sha512_destroy(wrapped); }
97 };
98 
99 } /* namespace decaf */
100 
101 #undef DECAF_NOEXCEPT
102 
103 #endif /* __DECAF_SHA512_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:208
const unsigned char * data() const DECAF_NOEXCEPT
Get const data.
Definition: secure_buffer.hxx:199
A reference to a writable block of data.
Definition: secure_buffer.hxx:270
An exception for when crypto (ie point decode) has failed.
Definition: secure_buffer.hxx:126
SHA512 wrapper function.
Definition: sha512.hxx:29
static SecureBuffer hash(const Block &message, size_t outlen=OUTPUT_BYTES)
Hash a message in one pass.
Definition: sha512.hxx:85
void reset() DECAF_NOEXCEPT
Resets the SHA context.
Definition: sha512.hxx:68
static const size_t DEFAULT_OUTPUT_BYTES
Default number of bytes to output.
Definition: sha512.hxx:44
void update(const uint8_t *__restrict__ in, size_t len) DECAF_NOEXCEPT
Add more data to running hash.
Definition: sha512.hxx:50
SHA512 & operator<<(const Block &s)
Add more data, stream version.
Definition: sha512.hxx:56
void update(const Block &s) DECAF_NOEXCEPT
Add more data to running hash, C++ version.
Definition: sha512.hxx:53
size_t max_output_size() const DECAF_NOEXCEPT
Return the sponge's maximum output size.
Definition: sha512.hxx:82
~SHA512() DECAF_NOEXCEPT
Destructor zeroizes state.
Definition: sha512.hxx:96
static const size_t OUTPUT_BYTES
Number of bytes ouf output.
Definition: sha512.hxx:38
size_t default_output_size() const DECAF_NOEXCEPT
Return the sponge's default output size.
Definition: sha512.hxx:79
static const size_t MAX_OUTPUT_BYTES
Number of bytes of output.
Definition: sha512.hxx:41
SHA512() DECAF_NOEXCEPT
Constructor.
Definition: sha512.hxx:47
SHA512 & operator+=(const Block &s)
Same as <<.
Definition: sha512.hxx:59
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.