Firmware SDK
twr_sha256.c
1 #include <twr_sha256.h>
2 
3 #define _TWR_SHA256_RL(a, b) (((a) << (b)) | ((a) >> (32 - (b))))
4 #define _TWR_SHA256_RR(a, b) (((a) >> (b)) | ((a) << (32 - (b))))
5 #define _TWR_SHA256_CH(x, y, z) (((x) & (y)) ^ (~(x) & (z)))
6 #define _TWR_SHA256_MAJ(x, y, z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
7 #define _TWR_SHA256_EP0(x) (_TWR_SHA256_RR(x, 2) ^ _TWR_SHA256_RR(x, 13) ^ _TWR_SHA256_RR(x, 22))
8 #define _TWR_SHA256_EP1(x) (_TWR_SHA256_RR(x, 6) ^ _TWR_SHA256_RR(x, 11) ^ _TWR_SHA256_RR(x, 25))
9 #define _TWR_SHA256_SIG0(x) (_TWR_SHA256_RR(x, 7) ^ _TWR_SHA256_RR(x, 18) ^ ((x) >> 3))
10 #define _TWR_SHA256_SIG1(x) (_TWR_SHA256_RR(x, 17) ^ _TWR_SHA256_RR(x, 19) ^ ((x) >> 10))
11 
12 static const uint32_t _twr_sha256_k[64] =
13 {
14  0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
15  0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
16  0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
17  0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
18  0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
19  0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
20  0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
21  0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
22 };
23 
24 static void _twr_sha256_transform(twr_sha256_t *self, const uint8_t *buffer);
25 
26 void twr_sha256_init(twr_sha256_t *self)
27 {
28  memset(self, 0, sizeof(*self));
29 
30  self->_state[0] = 0x6a09e667;
31  self->_state[1] = 0xbb67ae85;
32  self->_state[2] = 0x3c6ef372;
33  self->_state[3] = 0xa54ff53a;
34  self->_state[4] = 0x510e527f;
35  self->_state[5] = 0x9b05688c;
36  self->_state[6] = 0x1f83d9ab;
37  self->_state[7] = 0x5be0cd19;
38 }
39 
40 void twr_sha256_update(twr_sha256_t *self, const void *buffer, size_t length)
41 {
42  const uint8_t *p = buffer;
43 
44  for (size_t i = 0; i < length; i++)
45  {
46  self->_buffer[self->_length++] = p[i];
47 
48  if (self->_length == 64)
49  {
50  _twr_sha256_transform(self, self->_buffer);
51 
52  self->_bit_length += 512;
53  self->_length = 0;
54  }
55  }
56 }
57 
58 void twr_sha256_final(twr_sha256_t *self, uint8_t *hash, bool little_endian)
59 {
60  size_t i = self->_length;
61 
62  if (i < 56)
63  {
64  self->_buffer[i++] = 0x80;
65 
66  while (i < 56)
67  {
68  self->_buffer[i++] = 0;
69  }
70  }
71  else
72  {
73  self->_buffer[i++] = 0x80;
74 
75  while (i < 64)
76  {
77  self->_buffer[i++] = 0;
78  }
79 
80  _twr_sha256_transform(self, self->_buffer);
81 
82  memset(self->_buffer, 0, 56);
83  }
84 
85  self->_bit_length += (uint64_t) self->_length * 8;
86 
87  self->_buffer[63] = self->_bit_length;
88  self->_buffer[62] = self->_bit_length >> 8;
89  self->_buffer[61] = self->_bit_length >> 16;
90  self->_buffer[60] = self->_bit_length >> 24;
91  self->_buffer[59] = self->_bit_length >> 32;
92  self->_buffer[58] = self->_bit_length >> 40;
93  self->_buffer[57] = self->_bit_length >> 48;
94  self->_buffer[56] = self->_bit_length >> 56;
95 
96  _twr_sha256_transform(self, self->_buffer);
97 
98  if (little_endian)
99  {
100  for (i = 0; i < 4; i++)
101  {
102  hash[i] = self->_state[7] >> i * 8;
103  hash[i + 4] = self->_state[6] >> i * 8;
104  hash[i + 8] = self->_state[5] >> i * 8;
105  hash[i + 12] = self->_state[4] >> i * 8;
106  hash[i + 16] = self->_state[3] >> i * 8;
107  hash[i + 20] = self->_state[2] >> i * 8;
108  hash[i + 24] = self->_state[1] >> i * 8;
109  hash[i + 28] = self->_state[0] >> i * 8;
110  }
111  }
112  else
113  {
114  for (i = 0; i < 4; i++)
115  {
116  hash[i] = self->_state[0] >> (24 - i * 8);
117  hash[i + 4] = self->_state[1] >> (24 - i * 8);
118  hash[i + 8] = self->_state[2] >> (24 - i * 8);
119  hash[i + 12] = self->_state[3] >> (24 - i * 8);
120  hash[i + 16] = self->_state[4] >> (24 - i * 8);
121  hash[i + 20] = self->_state[5] >> (24 - i * 8);
122  hash[i + 24] = self->_state[6] >> (24 - i * 8);
123  hash[i + 28] = self->_state[7] >> (24 - i * 8);
124  }
125  }
126 }
127 
128 static void _twr_sha256_transform(twr_sha256_t *self, const uint8_t *buffer)
129 {
130  uint32_t a, b, c, d, e, f, g, h, t1, t2, m[64];
131 
132  size_t i, j;
133 
134  for (i = 0, j = 0; i < 16; i++, j += 4)
135  {
136  m[i] = buffer[j] << 24 | buffer[j + 1] << 16 | buffer[j + 2] << 8 | buffer[j + 3];
137  }
138 
139  for (; i < 64; i++)
140  {
141  m[i] = _TWR_SHA256_SIG1(m[i - 2]) + m[i - 7] + _TWR_SHA256_SIG0(m[i - 15]) + m[i - 16];
142  }
143 
144  a = self->_state[0];
145  b = self->_state[1];
146  c = self->_state[2];
147  d = self->_state[3];
148  e = self->_state[4];
149  f = self->_state[5];
150  g = self->_state[6];
151  h = self->_state[7];
152 
153  for (i = 0; i < 64; i++)
154  {
155  t1 = h + _TWR_SHA256_EP1(e) + _TWR_SHA256_CH(e, f, g) + _twr_sha256_k[i] + m[i];
156  t2 = _TWR_SHA256_EP0(a) + _TWR_SHA256_MAJ(a, b, c);
157  h = g;
158  g = f;
159  f = e;
160  e = d + t1;
161  d = c;
162  c = b;
163  b = a;
164  a = t1 + t2;
165  }
166 
167  self->_state[0] += a;
168  self->_state[1] += b;
169  self->_state[2] += c;
170  self->_state[3] += d;
171  self->_state[4] += e;
172  self->_state[5] += f;
173  self->_state[6] += g;
174  self->_state[7] += h;
175 }
void twr_sha256_update(twr_sha256_t *self, const void *buffer, size_t length)
Compute SHA256 from data.
Definition: twr_sha256.c:40
void twr_sha256_final(twr_sha256_t *self, uint8_t *hash, bool little_endian)
Finalize SHA256 computation and read the final hash.
Definition: twr_sha256.c:58
void twr_sha256_init(twr_sha256_t *self)
Initialize SHA256 structure.
Definition: twr_sha256.c:26