25 #include "utility/RotateUtil.h"
26 #include "utility/EndianUtil.h"
27 #include "utility/ProgMemUtil.h"
68 state.h[0] = 0x6a09e667;
69 state.h[1] = 0xbb67ae85;
70 state.h[2] = 0x3c6ef372;
71 state.h[3] = 0xa54ff53a,
72 state.h[4] = 0x510e527f;
73 state.h[5] = 0x9b05688c;
74 state.h[6] = 0x1f83d9ab;
75 state.h[7] = 0x5be0cd19;
77 state.finalized =
false;
88 state.length += ((uint64_t)len) << 3;
91 const uint8_t *d = (
const uint8_t *)data;
93 uint8_t size = 64 - state.chunkSize;
96 memcpy(((uint8_t *)state.w) + state.chunkSize, d, size);
97 state.chunkSize += size;
100 if (state.chunkSize == 64) {
110 if (!state.finalized) {
113 uint8_t *wbytes = (uint8_t *)state.w;
114 if (state.chunkSize <= (64 - 9)) {
115 wbytes[state.chunkSize] = 0x80;
116 memset(wbytes + state.chunkSize + 1, 0x00, 64 - 8 - (state.chunkSize + 1));
117 state.w[14] = htobe32((uint32_t)(state.length >> 32));
118 state.w[15] = htobe32((uint32_t)state.length);
121 wbytes[state.chunkSize] = 0x80;
122 memset(wbytes + state.chunkSize + 1, 0x00, 64 - (state.chunkSize + 1));
124 memset(wbytes, 0x00, 64 - 8);
125 state.w[14] = htobe32((uint32_t)(state.length >> 32));
126 state.w[15] = htobe32((uint32_t)state.length);
131 for (uint8_t posn = 0; posn < 8; ++posn)
132 state.w[posn] = htobe32(state.h[posn]);
133 state.finalized =
true;
139 memcpy(hash, state.w, len);
153 void SHA256::processChunk()
156 static uint32_t
const k[64] PROGMEM = {
157 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
158 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
159 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
160 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
161 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
162 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
163 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
164 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
165 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
166 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
167 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
168 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
169 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
170 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
171 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
172 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
177 for (index = 0; index < 16; ++index)
178 state.w[index] = be32toh(state.w[index]);
181 uint32_t a = state.h[0];
182 uint32_t b = state.h[1];
183 uint32_t c = state.h[2];
184 uint32_t d = state.h[3];
185 uint32_t e = state.h[4];
186 uint32_t f = state.h[5];
187 uint32_t g = state.h[6];
188 uint32_t h = state.h[7];
191 uint32_t temp1, temp2;
192 for (index = 0; index < 16; ++index) {
193 temp1 = h + pgm_read_dword(k + index) + state.w[index] +
194 (rightRotate6(e) ^ rightRotate11(e) ^ rightRotate25(e)) +
195 ((e & f) ^ ((~e) & g));
196 temp2 = (rightRotate2(a) ^ rightRotate13(a) ^ rightRotate22(a)) +
197 ((a & b) ^ (a & c) ^ (b & c));
211 for (; index < 64; ++index) {
213 temp1 = state.w[(index - 15) & 0x0F];
214 temp2 = state.w[(index - 2) & 0x0F];
215 temp1 = state.w[index & 0x0F] =
216 state.w[(index - 16) & 0x0F] + state.w[(index - 7) & 0x0F] +
217 (rightRotate7(temp1) ^ rightRotate18(temp1) ^ (temp1 >> 3)) +
218 (rightRotate17(temp2) ^ rightRotate19(temp2) ^ (temp2 >> 10));
221 temp1 = h + pgm_read_dword(k + index) + temp1 +
222 (rightRotate6(e) ^ rightRotate11(e) ^ rightRotate25(e)) +
223 ((e & f) ^ ((~e) & g));
224 temp2 = (rightRotate2(a) ^ rightRotate13(a) ^ rightRotate22(a)) +
225 ((a & b) ^ (a & c) ^ (b & c));
247 a = b = c = d = e = f = g = h = temp1 = temp2 = 0;
void reset()
Resets the hash ready for a new hashing process.
SHA256()
Constructs a SHA-256 hash object.
void finalize(void *hash, size_t len)
Finalizes the hashing process and returns the hash.
virtual ~SHA256()
Destroys this SHA-256 hash object after clearing sensitive information.
size_t blockSize() const
Size of the internal block used by the hash algorithm.
void update(const void *data, size_t len)
Updates the hash with more data.
size_t hashSize() const
Size of the hash result from finalize().
void clear()
Clears the hash state, removing all sensitive data, and then resets the hash ready for a new hashing ...