emsha  1.1.0
A compact HMAC-SHA-256 C++11 library.
emsha.h
Go to the documentation of this file.
1 
30 
31 #ifndef EMSHA_EMSHA_H
32 #define EMSHA_EMSHA_H
33 
34 
35 #include <cstdint>
36 
37 
38 // emsha is an EMbedded Secure HAshing interface.
39 namespace emsha {
40 
41 
42 #ifdef NDEBUG
47 #define EMSHA_CHECK(condition, retval) if (!(condition)) { return (retval); }
48 #else
53 #define EMSHA_CHECK(condition, retval) (assert((condition)))
54 #endif
55 
57 const std::uint32_t SHA256_HASH_SIZE = 32;
58 
59 
64 typedef enum class EMSHAResult : std::uint8_t {
67  Unknown = 0,
68 
70  OK = 1,
71 
73  TestFailure = 2,
74 
77  NullPointer = 3,
78 
80  InvalidState = 4,
81 
83  InputTooLong = 5,
84 
88 } ;
89 
90 
93 class Hash {
94 public:
95  virtual ~Hash() = default;
96 
117  virtual EMSHAResult Reset() = 0;
118 
125  virtual EMSHAResult Update(const std::uint8_t *message,
126  std::uint32_t messageLength) = 0;
127 
137  virtual EMSHAResult Finalise(std::uint8_t *digest) = 0;
138 
147  virtual EMSHAResult Result(std::uint8_t *digest) = 0;
148 
153  virtual std::uint32_t Size() = 0;
154 };
155 
177 bool HashEqual(const std::uint8_t *a, const std::uint8_t *b);
178 
179 
180 #ifndef EMSHA_NO_HEXSTRING
191 void HexString(std::uint8_t *dest, std::uint8_t *src, std::uint32_t srclen);
192 #endif // EMSHA_NO_HEXSTRING
193 
194 
195 } // end of namespace emsha
196 
197 
198 #endif // EMSHA_EMSHA_H
Definition: emsha.h:93
virtual std::uint32_t Size()=0
Return the output size of the Hash.
virtual EMSHAResult Reset()=0
Bring the Hash back to its initial state.
virtual EMSHAResult Finalise(std::uint8_t *digest)=0
Carry out any final operations on the Hash.
virtual EMSHAResult Update(const std::uint8_t *message, std::uint32_t messageLength)=0
Write message data into the Hash.
virtual EMSHAResult Result(std::uint8_t *digest)=0
Result transfers out the hash to the argument.
virtual ~Hash()=default
Definition: emsha.h:39
bool HashEqual(const std::uint8_t *a, const std::uint8_t *b)
Constant-time function for comparing two digests.
const std::uint32_t SHA256_HASH_SIZE
SHA256_HASH_SIZE is the output length of SHA-256 in bytes.
Definition: emsha.h:57
void HexString(std::uint8_t *dest, std::uint8_t *src, std::uint32_t srclen)
Write a hex-encoded version of a byte string.
EMSHAResult
Describe the result of an EMSHA operation.
Definition: emsha.h:64
@ TestFailure
The self-test failed.
@ InputTooLong
The input to SHA256::update is too large.
@ OK
All operations have completed successfully so far.
@ InvalidState
The Hash is in an invalid state.