3 #include <avr/pgmspace.h>
6 //#include "debugstuff.c"
8 uint32_t sha256K[] PROGMEM = {
9 0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5,0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5,
10 0xd807aa98,0x12835b01,0x243185be,0x550c7dc3,0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174,
11 0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc,0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da,
12 0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7,0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967,
13 0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13,0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85,
14 0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3,0xd192e819,0xd6990624,0xf40e3585,0x106aa070,
15 0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5,0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3,
16 0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208,0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2
19 #define BUFFER_SIZE 64
21 uint8_t sha256InitState[] PROGMEM = {
22 0x67,0xe6,0x09,0x6a, // H0
23 0x85,0xae,0x67,0xbb, // H1
24 0x72,0xf3,0x6e,0x3c, // H2
25 0x3a,0xf5,0x4f,0xa5, // H3
26 0x7f,0x52,0x0e,0x51, // H4
27 0x8c,0x68,0x05,0x9b, // H5
28 0xab,0xd9,0x83,0x1f, // H6
29 0x19,0xcd,0xe0,0x5b // H7
32 void Sha256Class::init(void) {
33 memcpy_P(state.b,sha256InitState,32);
38 uint32_t Sha256Class::ror32(uint32_t number, uint8_t bits) {
39 return ((number << (32-bits)) | (number >> bits));
42 void Sha256Class::hashBlock() {
43 // Sha256 only for now
45 uint32_t a,b,c,d,e,f,g,h,t1,t2;
56 for (i=0; i<64; i++) {
58 t1 = buffer.w[i&15] + buffer.w[(i-7)&15];
59 t2 = buffer.w[(i-2)&15];
60 t1 += ror32(t2,17) ^ ror32(t2,19) ^ (t2>>10);
61 t2 = buffer.w[(i-15)&15];
62 t1 += ror32(t2,7) ^ ror32(t2,18) ^ (t2>>3);
66 t1 += ror32(e,6) ^ ror32(e,11) ^ ror32(e,25); // ∑1(e)
67 t1 += g ^ (e & (g ^ f)); // Ch(e,f,g)
68 t1 += pgm_read_dword(sha256K+i); // Ki
69 t1 += buffer.w[i&15]; // Wi
70 t2 = ror32(a,2) ^ ror32(a,13) ^ ror32(a,22); // ∑0(a)
71 t2 += ((b & c) | (a & (b | c))); // Maj(a,b,c)
72 h=g; g=f; f=e; e=d+t1; d=c; c=b; b=a; a=t1+t2;
84 void Sha256Class::addUncounted(uint8_t data) {
85 buffer.b[bufferOffset ^ 3] = data;
87 if (bufferOffset == BUFFER_SIZE) {
93 size_t Sha256Class::write(uint8_t data) {
99 void Sha256Class::pad() {
100 // Implement SHA-256 padding (fips180-2 §5.1.1)
102 // Pad with 0x80 followed by 0x00 until the end of the block
104 while (bufferOffset != 56) addUncounted(0x00);
106 // Append length in the last 8 bytes
107 addUncounted(0); // We're only using 32 bit lengths
108 addUncounted(0); // But SHA-1 supports 64 bit lengths
109 addUncounted(0); // So zero pad the top bits
110 addUncounted(byteCount >> 29); // Shifting to multiply by 8
111 addUncounted(byteCount >> 21); // as SHA-1 supports bitstreams as well as
112 addUncounted(byteCount >> 13); // byte.
113 addUncounted(byteCount >> 5);
114 addUncounted(byteCount << 3);
118 uint8_t* Sha256Class::result(void) {
119 // Pad to complete the last block
122 // Swap byte order back
123 for (int i=0; i<8; i++) {
127 b|=(a<<8) & 0x00ff0000;
128 b|=(a>>8) & 0x0000ff00;
133 // Return pointer to hash (20 characters)
138 #define HMAC_IPAD 0x36
139 #define HMAC_OPAD 0x5c
141 uint8_t keyBuffer[BLOCK_LENGTH]; // K0 in FIPS-198a
142 uint8_t innerHash[HASH_LENGTH];
144 void Sha256Class::initHmac(const uint8_t* key, int keyLength) {
146 memset(keyBuffer,0,BLOCK_LENGTH);
147 if (keyLength > BLOCK_LENGTH) {
150 for (;keyLength--;) write(*key++);
151 memcpy(keyBuffer,result(),HASH_LENGTH);
153 // Block length keys are used as is
154 memcpy(keyBuffer,key,keyLength);
156 //for (i=0; i<BLOCK_LENGTH; i++) debugHH(keyBuffer[i]);
159 for (i=0; i<BLOCK_LENGTH; i++) {
160 write(keyBuffer[i] ^ HMAC_IPAD);
164 uint8_t* Sha256Class::resultHmac(void) {
166 // Complete inner hash
167 memcpy(innerHash,result(),HASH_LENGTH);
168 // now innerHash[] contains H((K0 xor ipad)||text)
170 // Calculate outer hash
172 for (i=0; i<BLOCK_LENGTH; i++) write(keyBuffer[i] ^ HMAC_OPAD);
173 for (i=0; i<HASH_LENGTH; i++) write(innerHash[i]);