]> git.piffa.net Git - arduino/blob - books/pdummies/Libraries/Sha/sha256.cpp
first commit
[arduino] / books / pdummies / Libraries / Sha / sha256.cpp
1 #include <string.h>
2 #include <avr/io.h>
3 #include <avr/pgmspace.h>
4 #include "sha256.h"
5
6 //#include "debugstuff.c"
7
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
17 };
18
19 #define BUFFER_SIZE 64
20
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
30 };
31
32 void Sha256Class::init(void) {
33   memcpy_P(state.b,sha256InitState,32);
34   byteCount = 0;
35   bufferOffset = 0;
36 }
37
38 uint32_t Sha256Class::ror32(uint32_t number, uint8_t bits) {
39   return ((number << (32-bits)) | (number >> bits));
40 }
41
42 void Sha256Class::hashBlock() {
43   // Sha256 only for now
44   uint8_t i;
45   uint32_t a,b,c,d,e,f,g,h,t1,t2;
46
47   a=state.w[0];
48   b=state.w[1];
49   c=state.w[2];
50   d=state.w[3];
51   e=state.w[4];
52   f=state.w[5];
53   g=state.w[6];
54   h=state.w[7];
55
56   for (i=0; i<64; i++) {
57     if (i>=16) {
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);
63       buffer.w[i&15] = t1;
64     }
65     t1 = h;
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;
73   }
74   state.w[0] += a;
75   state.w[1] += b;
76   state.w[2] += c;
77   state.w[3] += d;
78   state.w[4] += e;
79   state.w[5] += f;
80   state.w[6] += g;
81   state.w[7] += h;
82 }
83
84 void Sha256Class::addUncounted(uint8_t data) {
85   buffer.b[bufferOffset ^ 3] = data;
86   bufferOffset++;
87   if (bufferOffset == BUFFER_SIZE) {
88     hashBlock();
89     bufferOffset = 0;
90   }
91 }
92
93 size_t Sha256Class::write(uint8_t data) {
94   ++byteCount;
95   addUncounted(data);
96   return 1;
97 }
98
99 void Sha256Class::pad() {
100   // Implement SHA-256 padding (fips180-2 §5.1.1)
101
102   // Pad with 0x80 followed by 0x00 until the end of the block
103   addUncounted(0x80);
104   while (bufferOffset != 56) addUncounted(0x00);
105
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);
115 }
116
117
118 uint8_t* Sha256Class::result(void) {
119   // Pad to complete the last block
120   pad();
121
122   // Swap byte order back
123   for (int i=0; i<8; i++) {
124     uint32_t a,b;
125     a=state.w[i];
126     b=a<<24;
127     b|=(a<<8) & 0x00ff0000;
128     b|=(a>>8) & 0x0000ff00;
129     b|=a>>24;
130     state.w[i]=b;
131   }
132
133   // Return pointer to hash (20 characters)
134   return state.b;
135 }
136
137
138 #define HMAC_IPAD 0x36
139 #define HMAC_OPAD 0x5c
140
141 uint8_t keyBuffer[BLOCK_LENGTH]; // K0 in FIPS-198a
142 uint8_t innerHash[HASH_LENGTH];
143
144 void Sha256Class::initHmac(const uint8_t* key, int keyLength) {
145   uint8_t i;
146   memset(keyBuffer,0,BLOCK_LENGTH);
147   if (keyLength > BLOCK_LENGTH) {
148     // Hash long keys
149     init();
150     for (;keyLength--;) write(*key++);
151     memcpy(keyBuffer,result(),HASH_LENGTH);
152   } else {
153     // Block length keys are used as is
154     memcpy(keyBuffer,key,keyLength);
155   }
156   //for (i=0; i<BLOCK_LENGTH; i++) debugHH(keyBuffer[i]);
157   // Start inner hash
158   init();
159   for (i=0; i<BLOCK_LENGTH; i++) {
160     write(keyBuffer[i] ^ HMAC_IPAD);
161   }
162 }
163
164 uint8_t* Sha256Class::resultHmac(void) {
165   uint8_t i;
166     // Complete inner hash
167   memcpy(innerHash,result(),HASH_LENGTH);
168   // now innerHash[] contains H((K0 xor ipad)||text)
169
170   // Calculate outer hash
171   init();
172   for (i=0; i<BLOCK_LENGTH; i++) write(keyBuffer[i] ^ HMAC_OPAD);
173   for (i=0; i<HASH_LENGTH; i++) write(innerHash[i]);
174   return result();
175 }
176 Sha256Class Sha256;