emsha  1.1.1
A compact HMAC-SHA-256 C++11 library.
sha256.h
Go to the documentation of this file.
1 
30 #ifndef EMSHA_SHA256_H
31 #define EMSHA_SHA256_H
32 
33 
34 #include <cstdint>
35 
36 #include <array>
37 #include <emsha/emsha.h>
38 
39 
40 namespace emsha {
41 
42 
44 const uint32_t SHA256_MB_SIZE = 64;
45 
46 class SHA256 : Hash {
47 public:
52  SHA256();
53 
58 
63  EMSHAResult Reset() override;
64 
88  EMSHAResult Update(const std::uint8_t *message, std::uint32_t messageLength) override;
89 
108  EMSHAResult Finalise(std::uint8_t *digest) override;
109 
129  EMSHAResult Result(std::uint8_t *digest) override;
130 
138  std::uint32_t Size() override;
139 
140 private:
141  uint64_t mlen; // Current message length.
142  uint32_t i_hash[8]; // The intermediate hash is 8x 32-bit blocks.
143 
144  // hStatus is the hash status, and hComplete indicates
145  // whether the hash has been finalised.
146  EMSHAResult hStatus;
147  uint8_t hComplete;
148 
149  // mb is the message block, and mbi is the message
150  // block index.
151  uint8_t mbi;
152  std::array<uint8_t, SHA256_MB_SIZE> mb;
153 
154  inline EMSHAResult addLength(const uint32_t);
155  inline void updateMessageBlock(void);
156  inline void padMessage(uint8_t pc);
157  uint32_t chunkToUint32(uint32_t offset);
158  uint32_t uint32ToChunk(uint32_t offset);
159  EMSHAResult reset();
160 }; // end class SHA256
161 
162 
172 EMSHAResult SHA256Digest(const uint8_t *m, uint32_t ml, uint8_t *d);
173 
189 
190 
191 } // end of namespace emsha
192 
193 
194 #endif // EMSHA_SHA256_H
Definition: emsha.h:93
Definition: sha256.h:46
EMSHAResult Update(const std::uint8_t *message, std::uint32_t messageLength) override
Writes data into the SHA256.
SHA256()
A SHA256 context does not need any special construction.
EMSHAResult Finalise(std::uint8_t *digest) override
Complete the digest.
EMSHAResult Result(std::uint8_t *digest) override
Copy the result from the SHA-256 context into the buffer pointed to by d, running Finalise if needed....
EMSHAResult Reset() override
Clear the internal state of the SHA256 context, returning it to its initial state.
std::uint32_t Size() override
Returns the output size of SHA-256.
Declares an interface for an EMbedded Secure HAshing interface.
Definition: emsha.h:39
EMSHAResult SHA256SelfTest()
SHA256SelfTest runs through two test cases to ensure that the SHA-256 functions are working correctly...
const uint32_t SHA256_MB_SIZE
SHA256_MB_SIZE is the size of a message block.
Definition: sha256.h:44
EMSHAResult
Describe the result of an EMSHA operation.
Definition: emsha.h:64
EMSHAResult SHA256Digest(const uint8_t *m, uint32_t ml, uint8_t *d)
SHA256Digest performs a single pass hashing of the message passed in.