REDAC HybridController
Firmware for LUCIDAC/REDAC Teensy
Loading...
Searching...
No Matches
dcp.cpp
Go to the documentation of this file.
1
2#include <array>
3#include <cstdint>
4#include <cstring>
5
6namespace utils {
7
8// SHA-256 constants
9constexpr std::array<uint32_t, 64> K = {
10 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
11 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
12 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
13 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
14 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
15 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
16 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
17 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2};
18
19// SHA-1 constants
20constexpr std::array<uint32_t, 4> H1 = {0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476};
21
22// Helper functions
23inline uint32_t rotr(uint32_t x, uint32_t n) { return (x >> n) | (x << (32 - n)); }
24
25inline uint32_t ch(uint32_t x, uint32_t y, uint32_t z) { return (x & y) ^ (~x & z); }
26
27inline uint32_t maj(uint32_t x, uint32_t y, uint32_t z) { return (x & y) ^ (x & z) ^ (y & z); }
28
29inline uint32_t sigma0(uint32_t x) { return rotr(x, 2) ^ rotr(x, 13) ^ rotr(x, 22); }
30
31inline uint32_t sigma1(uint32_t x) { return rotr(x, 6) ^ rotr(x, 11) ^ rotr(x, 25); }
32
33void hash_sha256(const uint8_t *msg, size_t msg_len, uint8_t *out_hash) {
34 // Initialize hash values
35 uint32_t h0 = 0x6a09e667;
36 uint32_t h1 = 0xbb67ae85;
37 uint32_t h2 = 0x3c6ef372;
38 uint32_t h3 = 0xa54ff53a;
39 uint32_t h4 = 0x510e527f;
40 uint32_t h5 = 0x9b05688c;
41 uint32_t h6 = 0x1f83d9ab;
42 uint32_t h7 = 0x5be0cd19;
43
44 // Pre-processing: Padding the message
45 size_t new_len = ((((msg_len + 8) / 64) + 1) * 64) - 8;
46 uint8_t *padded = new uint8_t[new_len + 64];
47 memcpy(padded, msg, msg_len);
48 padded[msg_len] = 0x80;
49
50 for (size_t i = msg_len + 1; i < new_len; i++) {
51 padded[i] = 0;
52 }
53
54 // Append length in bits
55 uint64_t bits_len = msg_len * 8;
56 for (int i = 0; i < 8; i++) {
57 padded[new_len + i] = (bits_len >> (56 - i * 8)) & 0xFF;
58 }
59
60 // Process the message in 512-bit chunks
61 for (size_t chunk = 0; chunk < new_len + 64; chunk += 64) {
62 uint32_t w[64];
63
64 // Create message schedule
65 for (int i = 0; i < 16; i++) {
66 w[i] = (padded[chunk + i * 4] << 24) | (padded[chunk + i * 4 + 1] << 16) |
67 (padded[chunk + i * 4 + 2] << 8) | padded[chunk + i * 4 + 3];
68 }
69
70 for (int i = 16; i < 64; i++) {
71 uint32_t s0 = rotr(w[i - 15], 7) ^ rotr(w[i - 15], 18) ^ (w[i - 15] >> 3);
72 uint32_t s1 = rotr(w[i - 2], 17) ^ rotr(w[i - 2], 19) ^ (w[i - 2] >> 10);
73 w[i] = w[i - 16] + s0 + w[i - 7] + s1;
74 }
75
76 // Initialize working variables
77 uint32_t a = h0;
78 uint32_t b = h1;
79 uint32_t c = h2;
80 uint32_t d = h3;
81 uint32_t e = h4;
82 uint32_t f = h5;
83 uint32_t g = h6;
84 uint32_t h = h7;
85
86 // Main loop
87 for (int i = 0; i < 64; i++) {
88 uint32_t S1 = sigma1(e);
89 uint32_t ch_val = ch(e, f, g);
90 uint32_t temp1 = h + S1 + ch_val + K[i] + w[i];
91 uint32_t S0 = sigma0(a);
92 uint32_t maj_val = maj(a, b, c);
93 uint32_t temp2 = S0 + maj_val;
94
95 h = g;
96 g = f;
97 f = e;
98 e = d + temp1;
99 d = c;
100 c = b;
101 b = a;
102 a = temp1 + temp2;
103 }
104
105 // Update hash values
106 h0 += a;
107 h1 += b;
108 h2 += c;
109 h3 += d;
110 h4 += e;
111 h5 += f;
112 h6 += g;
113 h7 += h;
114 }
115
116 delete[] padded;
117
118 // Produce the final hash value
119 uint32_t hash[8] = {h0, h1, h2, h3, h4, h5, h6, h7};
120 memcpy(out_hash, hash, 32);
121}
122
123void hash_sha1(const uint8_t *msg, size_t msg_len, uint8_t *out_hash) {
124 // Initialize variables
125 uint32_t h0 = 0x67452301;
126 uint32_t h1 = 0xEFCDAB89;
127 uint32_t h2 = 0x98BADCFE;
128 uint32_t h3 = 0x10325476;
129 uint32_t h4 = 0xC3D2E1F0;
130
131 // Pre-processing
132 size_t new_len = ((((msg_len + 8) / 64) + 1) * 64) - 8;
133 uint8_t *padded = new uint8_t[new_len + 64];
134 memcpy(padded, msg, msg_len);
135 padded[msg_len] = 0x80;
136
137 for (size_t i = msg_len + 1; i < new_len; i++) {
138 padded[i] = 0;
139 }
140
141 // Append length in bits
142 uint64_t bits_len = msg_len * 8;
143 for (int i = 0; i < 8; i++) {
144 padded[new_len + i] = (bits_len >> (56 - i * 8)) & 0xFF;
145 }
146
147 // Process the message in 512-bit chunks
148 for (size_t chunk = 0; chunk < new_len + 64; chunk += 64) {
149 uint32_t w[80];
150
151 // Break chunk into sixteen 32-bit big-endian words
152 for (int i = 0; i < 16; i++) {
153 w[i] = (padded[chunk + i * 4] << 24) | (padded[chunk + i * 4 + 1] << 16) |
154 (padded[chunk + i * 4 + 2] << 8) | padded[chunk + i * 4 + 3];
155 }
156
157 // Extend the sixteen 32-bit words into eighty 32-bit words
158 for (int i = 16; i < 80; i++) {
159 w[i] = rotr((w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16]), 31);
160 }
161
162 // Initialize hash value for this chunk
163 uint32_t a = h0;
164 uint32_t b = h1;
165 uint32_t c = h2;
166 uint32_t d = h3;
167 uint32_t e = h4;
168
169 // Main loop
170 for (int i = 0; i < 80; i++) {
171 uint32_t f, k;
172
173 if (i < 20) {
174 f = (b & c) | ((~b) & d);
175 k = 0x5A827999;
176 } else if (i < 40) {
177 f = b ^ c ^ d;
178 k = 0x6ED9EBA1;
179 } else if (i < 60) {
180 f = (b & c) | (b & d) | (c & d);
181 k = 0x8F1BBCDC;
182 } else {
183 f = b ^ c ^ d;
184 k = 0xCA62C1D6;
185 }
186
187 uint32_t temp = rotr(a, 27) + f + e + k + w[i];
188 e = d;
189 d = c;
190 c = rotr(b, 2);
191 b = a;
192 a = temp;
193 }
194
195 // Add this chunk's hash to result so far
196 h0 += a;
197 h1 += b;
198 h2 += c;
199 h3 += d;
200 h4 += e;
201 }
202
203 delete[] padded;
204
205 // Produce the final hash value
206 uint32_t hash[5] = {h0, h1, h2, h3, h4};
207 memcpy(out_hash, hash, 20);
208}
209
210} // namespace utils
uint32_t
Definition flasher.cpp:195
uint32_t ch(uint32_t x, uint32_t y, uint32_t z)
Definition dcp.cpp:25
uint32_t rotr(uint32_t x, uint32_t n)
Definition dcp.cpp:23
FLASHMEM void hash(const uint8_t *msg, size_t msg_len, uint8_t *out_hash, dcp_hash_algo_t algo)
Definition dcp.cpp:610
FLASHMEM void hash_sha1(const uint8_t *msg, size_t msg_len, uint8_t *out_hash)
Definition dcp.cpp:634
FLASHMEM void hash_sha256(const uint8_t *msg, size_t msg_len, uint8_t *out_hash)
Computes the SHA256 sum of an arbitrary message (large memory segment).
Definition dcp.cpp:630
constexpr std::array< uint32_t, 4 > H1
Definition dcp.cpp:20
constexpr std::array< uint32_t, 64 > K
Definition dcp.cpp:9
uint32_t sigma0(uint32_t x)
Definition dcp.cpp:29
uint32_t maj(uint32_t x, uint32_t y, uint32_t z)
Definition dcp.cpp:27
uint32_t sigma1(uint32_t x)
Definition dcp.cpp:31