/src/dropbear/libtomcrypt/src/hashes/sha2/sha256.c
Line | Count | Source |
1 | | /* LibTomCrypt, modular cryptographic library -- Tom St Denis |
2 | | * |
3 | | * LibTomCrypt is a library that provides various cryptographic |
4 | | * algorithms in a highly modular and flexible manner. |
5 | | * |
6 | | * The library is free for all purposes without any express |
7 | | * guarantee it works. |
8 | | */ |
9 | | #include "tomcrypt.h" |
10 | | |
11 | | /** |
12 | | @file sha256.c |
13 | | LTC_SHA256 by Tom St Denis |
14 | | */ |
15 | | |
16 | | #ifdef LTC_SHA256 |
17 | | |
18 | | const struct ltc_hash_descriptor sha256_desc = |
19 | | { |
20 | | "sha256", |
21 | | 0, |
22 | | 32, |
23 | | 64, |
24 | | |
25 | | /* OID */ |
26 | | { 2, 16, 840, 1, 101, 3, 4, 2, 1, }, |
27 | | 9, |
28 | | |
29 | | &sha256_init, |
30 | | &sha256_process, |
31 | | &sha256_done, |
32 | | &sha256_test, |
33 | | NULL |
34 | | }; |
35 | | |
36 | | #ifdef LTC_SMALL_CODE |
37 | | /* the K array */ |
38 | | static const ulong32 K[64] = { |
39 | | 0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL, 0x3956c25bUL, |
40 | | 0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL, 0xd807aa98UL, 0x12835b01UL, |
41 | | 0x243185beUL, 0x550c7dc3UL, 0x72be5d74UL, 0x80deb1feUL, 0x9bdc06a7UL, |
42 | | 0xc19bf174UL, 0xe49b69c1UL, 0xefbe4786UL, 0x0fc19dc6UL, 0x240ca1ccUL, |
43 | | 0x2de92c6fUL, 0x4a7484aaUL, 0x5cb0a9dcUL, 0x76f988daUL, 0x983e5152UL, |
44 | | 0xa831c66dUL, 0xb00327c8UL, 0xbf597fc7UL, 0xc6e00bf3UL, 0xd5a79147UL, |
45 | | 0x06ca6351UL, 0x14292967UL, 0x27b70a85UL, 0x2e1b2138UL, 0x4d2c6dfcUL, |
46 | | 0x53380d13UL, 0x650a7354UL, 0x766a0abbUL, 0x81c2c92eUL, 0x92722c85UL, |
47 | | 0xa2bfe8a1UL, 0xa81a664bUL, 0xc24b8b70UL, 0xc76c51a3UL, 0xd192e819UL, |
48 | | 0xd6990624UL, 0xf40e3585UL, 0x106aa070UL, 0x19a4c116UL, 0x1e376c08UL, |
49 | | 0x2748774cUL, 0x34b0bcb5UL, 0x391c0cb3UL, 0x4ed8aa4aUL, 0x5b9cca4fUL, |
50 | | 0x682e6ff3UL, 0x748f82eeUL, 0x78a5636fUL, 0x84c87814UL, 0x8cc70208UL, |
51 | | 0x90befffaUL, 0xa4506cebUL, 0xbef9a3f7UL, 0xc67178f2UL |
52 | | }; |
53 | | #endif |
54 | | |
55 | | /* Various logical functions */ |
56 | 74.2k | #define Ch(x,y,z) (z ^ (x & (y ^ z))) |
57 | 74.2k | #define Maj(x,y,z) (((x | y) & z) | (x & y)) |
58 | 668k | #define S(x, n) RORc((x),(n)) |
59 | 111k | #define R(x, n) (((x)&0xFFFFFFFFUL)>>(n)) |
60 | 74.2k | #define Sigma0(x) (S(x, 2) ^ S(x, 13) ^ S(x, 22)) |
61 | 74.2k | #define Sigma1(x) (S(x, 6) ^ S(x, 11) ^ S(x, 25)) |
62 | 55.6k | #define Gamma0(x) (S(x, 7) ^ S(x, 18) ^ R(x, 3)) |
63 | 55.6k | #define Gamma1(x) (S(x, 17) ^ S(x, 19) ^ R(x, 10)) |
64 | | |
65 | | /* compress 512-bits */ |
66 | | #ifdef LTC_CLEAN_STACK |
67 | | static int _sha256_compress(hash_state * md, unsigned char *buf) |
68 | | #else |
69 | | static int sha256_compress(hash_state * md, unsigned char *buf) |
70 | | #endif |
71 | 1.16k | { |
72 | 1.16k | ulong32 S[8], W[64], t0, t1; |
73 | 1.16k | #ifdef LTC_SMALL_CODE |
74 | 1.16k | ulong32 t; |
75 | 1.16k | #endif |
76 | 1.16k | int i; |
77 | | |
78 | | /* copy state into S */ |
79 | 10.4k | for (i = 0; i < 8; i++) { |
80 | 9.28k | S[i] = md->sha256.state[i]; |
81 | 9.28k | } |
82 | | |
83 | | /* copy the state into 512-bits into W[0..15] */ |
84 | 19.7k | for (i = 0; i < 16; i++) { |
85 | 18.5k | LOAD32H(W[i], buf + (4*i)); |
86 | 18.5k | } |
87 | | |
88 | | /* fill W[16..63] */ |
89 | 56.8k | for (i = 16; i < 64; i++) { |
90 | 55.6k | W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) + W[i - 16]; |
91 | 55.6k | } |
92 | | |
93 | | /* Compress */ |
94 | 1.16k | #ifdef LTC_SMALL_CODE |
95 | 1.16k | #define RND(a,b,c,d,e,f,g,h,i) \ |
96 | 74.2k | t0 = h + Sigma1(e) + Ch(e, f, g) + K[i] + W[i]; \ |
97 | 74.2k | t1 = Sigma0(a) + Maj(a, b, c); \ |
98 | 74.2k | d += t0; \ |
99 | 74.2k | h = t0 + t1; |
100 | | |
101 | 75.4k | for (i = 0; i < 64; ++i) { |
102 | 74.2k | RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],i); |
103 | 74.2k | t = S[7]; S[7] = S[6]; S[6] = S[5]; S[5] = S[4]; |
104 | 74.2k | S[4] = S[3]; S[3] = S[2]; S[2] = S[1]; S[1] = S[0]; S[0] = t; |
105 | 74.2k | } |
106 | | #else |
107 | | #define RND(a,b,c,d,e,f,g,h,i,ki) \ |
108 | | t0 = h + Sigma1(e) + Ch(e, f, g) + ki + W[i]; \ |
109 | | t1 = Sigma0(a) + Maj(a, b, c); \ |
110 | | d += t0; \ |
111 | | h = t0 + t1; |
112 | | |
113 | | RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],0,0x428a2f98); |
114 | | RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],1,0x71374491); |
115 | | RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],2,0xb5c0fbcf); |
116 | | RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],3,0xe9b5dba5); |
117 | | RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],4,0x3956c25b); |
118 | | RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],5,0x59f111f1); |
119 | | RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],6,0x923f82a4); |
120 | | RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],7,0xab1c5ed5); |
121 | | RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],8,0xd807aa98); |
122 | | RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],9,0x12835b01); |
123 | | RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],10,0x243185be); |
124 | | RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],11,0x550c7dc3); |
125 | | RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],12,0x72be5d74); |
126 | | RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],13,0x80deb1fe); |
127 | | RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],14,0x9bdc06a7); |
128 | | RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],15,0xc19bf174); |
129 | | RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],16,0xe49b69c1); |
130 | | RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],17,0xefbe4786); |
131 | | RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],18,0x0fc19dc6); |
132 | | RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],19,0x240ca1cc); |
133 | | RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],20,0x2de92c6f); |
134 | | RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],21,0x4a7484aa); |
135 | | RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],22,0x5cb0a9dc); |
136 | | RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],23,0x76f988da); |
137 | | RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],24,0x983e5152); |
138 | | RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],25,0xa831c66d); |
139 | | RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],26,0xb00327c8); |
140 | | RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],27,0xbf597fc7); |
141 | | RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],28,0xc6e00bf3); |
142 | | RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],29,0xd5a79147); |
143 | | RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],30,0x06ca6351); |
144 | | RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],31,0x14292967); |
145 | | RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],32,0x27b70a85); |
146 | | RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],33,0x2e1b2138); |
147 | | RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],34,0x4d2c6dfc); |
148 | | RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],35,0x53380d13); |
149 | | RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],36,0x650a7354); |
150 | | RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],37,0x766a0abb); |
151 | | RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],38,0x81c2c92e); |
152 | | RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],39,0x92722c85); |
153 | | RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],40,0xa2bfe8a1); |
154 | | RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],41,0xa81a664b); |
155 | | RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],42,0xc24b8b70); |
156 | | RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],43,0xc76c51a3); |
157 | | RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],44,0xd192e819); |
158 | | RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],45,0xd6990624); |
159 | | RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],46,0xf40e3585); |
160 | | RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],47,0x106aa070); |
161 | | RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],48,0x19a4c116); |
162 | | RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],49,0x1e376c08); |
163 | | RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],50,0x2748774c); |
164 | | RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],51,0x34b0bcb5); |
165 | | RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],52,0x391c0cb3); |
166 | | RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],53,0x4ed8aa4a); |
167 | | RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],54,0x5b9cca4f); |
168 | | RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],55,0x682e6ff3); |
169 | | RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],56,0x748f82ee); |
170 | | RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],57,0x78a5636f); |
171 | | RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],58,0x84c87814); |
172 | | RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],59,0x8cc70208); |
173 | | RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],60,0x90befffa); |
174 | | RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],61,0xa4506ceb); |
175 | | RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],62,0xbef9a3f7); |
176 | | RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],63,0xc67178f2); |
177 | | |
178 | | #undef RND |
179 | | |
180 | | #endif |
181 | | |
182 | | /* feedback */ |
183 | 10.4k | for (i = 0; i < 8; i++) { |
184 | 9.28k | md->sha256.state[i] = md->sha256.state[i] + S[i]; |
185 | 9.28k | } |
186 | 1.16k | return CRYPT_OK; |
187 | 1.16k | } |
188 | | |
189 | | #ifdef LTC_CLEAN_STACK |
190 | | static int sha256_compress(hash_state * md, unsigned char *buf) |
191 | | { |
192 | | int err; |
193 | | err = _sha256_compress(md, buf); |
194 | | burn_stack(sizeof(ulong32) * 74); |
195 | | return err; |
196 | | } |
197 | | #endif |
198 | | |
199 | | /** |
200 | | Initialize the hash state |
201 | | @param md The hash state you wish to initialize |
202 | | @return CRYPT_OK if successful |
203 | | */ |
204 | | int sha256_init(hash_state * md) |
205 | 952 | { |
206 | 952 | LTC_ARGCHK(md != NULL); |
207 | | |
208 | 952 | md->sha256.curlen = 0; |
209 | 952 | md->sha256.length = 0; |
210 | 952 | md->sha256.state[0] = 0x6A09E667UL; |
211 | 952 | md->sha256.state[1] = 0xBB67AE85UL; |
212 | 952 | md->sha256.state[2] = 0x3C6EF372UL; |
213 | 952 | md->sha256.state[3] = 0xA54FF53AUL; |
214 | 952 | md->sha256.state[4] = 0x510E527FUL; |
215 | 952 | md->sha256.state[5] = 0x9B05688CUL; |
216 | 952 | md->sha256.state[6] = 0x1F83D9ABUL; |
217 | 952 | md->sha256.state[7] = 0x5BE0CD19UL; |
218 | 952 | return CRYPT_OK; |
219 | 952 | } |
220 | | |
221 | | /** |
222 | | Process a block of memory though the hash |
223 | | @param md The hash state |
224 | | @param in The data to hash |
225 | | @param inlen The length of the data (octets) |
226 | | @return CRYPT_OK if successful |
227 | | */ |
228 | | HASH_PROCESS(sha256_process, sha256_compress, sha256, 64) |
229 | | |
230 | | /** |
231 | | Terminate the hash to get the digest |
232 | | @param md The hash state |
233 | | @param out [out] The destination of the hash (32 bytes) |
234 | | @return CRYPT_OK if successful |
235 | | */ |
236 | | int sha256_done(hash_state * md, unsigned char *out) |
237 | 952 | { |
238 | 952 | int i; |
239 | | |
240 | 952 | LTC_ARGCHK(md != NULL); |
241 | 952 | LTC_ARGCHK(out != NULL); |
242 | | |
243 | 952 | if (md->sha256.curlen >= sizeof(md->sha256.buf)) { |
244 | 0 | return CRYPT_INVALID_ARG; |
245 | 0 | } |
246 | | |
247 | | |
248 | | /* increase the length of the message */ |
249 | 952 | md->sha256.length += md->sha256.curlen * 8; |
250 | | |
251 | | /* append the '1' bit */ |
252 | 952 | md->sha256.buf[md->sha256.curlen++] = (unsigned char)0x80; |
253 | | |
254 | | /* if the length is currently above 56 bytes we append zeros |
255 | | * then compress. Then we can fall back to padding zeros and length |
256 | | * encoding like normal. |
257 | | */ |
258 | 952 | if (md->sha256.curlen > 56) { |
259 | 0 | while (md->sha256.curlen < 64) { |
260 | 0 | md->sha256.buf[md->sha256.curlen++] = (unsigned char)0; |
261 | 0 | } |
262 | 0 | sha256_compress(md, md->sha256.buf); |
263 | 0 | md->sha256.curlen = 0; |
264 | 0 | } |
265 | | |
266 | | /* pad upto 56 bytes of zeroes */ |
267 | 24.8k | while (md->sha256.curlen < 56) { |
268 | 23.9k | md->sha256.buf[md->sha256.curlen++] = (unsigned char)0; |
269 | 23.9k | } |
270 | | |
271 | | /* store length */ |
272 | 952 | STORE64H(md->sha256.length, md->sha256.buf+56); |
273 | 952 | sha256_compress(md, md->sha256.buf); |
274 | | |
275 | | /* copy output */ |
276 | 8.56k | for (i = 0; i < 8; i++) { |
277 | 7.61k | STORE32H(md->sha256.state[i], out+(4*i)); |
278 | 7.61k | } |
279 | | #ifdef LTC_CLEAN_STACK |
280 | | zeromem(md, sizeof(hash_state)); |
281 | | #endif |
282 | 952 | return CRYPT_OK; |
283 | 952 | } |
284 | | |
285 | | /** |
286 | | Self-test the hash |
287 | | @return CRYPT_OK if successful, CRYPT_NOP if self-tests have been disabled |
288 | | */ |
289 | | int sha256_test(void) |
290 | 0 | { |
291 | 0 | #ifndef LTC_TEST |
292 | 0 | return CRYPT_NOP; |
293 | | #else |
294 | | static const struct { |
295 | | const char *msg; |
296 | | unsigned char hash[32]; |
297 | | } tests[] = { |
298 | | { "abc", |
299 | | { 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea, |
300 | | 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23, |
301 | | 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c, |
302 | | 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad } |
303 | | }, |
304 | | { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", |
305 | | { 0x24, 0x8d, 0x6a, 0x61, 0xd2, 0x06, 0x38, 0xb8, |
306 | | 0xe5, 0xc0, 0x26, 0x93, 0x0c, 0x3e, 0x60, 0x39, |
307 | | 0xa3, 0x3c, 0xe4, 0x59, 0x64, 0xff, 0x21, 0x67, |
308 | | 0xf6, 0xec, 0xed, 0xd4, 0x19, 0xdb, 0x06, 0xc1 } |
309 | | }, |
310 | | }; |
311 | | |
312 | | int i; |
313 | | unsigned char tmp[32]; |
314 | | hash_state md; |
315 | | |
316 | | for (i = 0; i < (int)(sizeof(tests) / sizeof(tests[0])); i++) { |
317 | | sha256_init(&md); |
318 | | sha256_process(&md, (unsigned char*)tests[i].msg, (unsigned long)strlen(tests[i].msg)); |
319 | | sha256_done(&md, tmp); |
320 | | if (compare_testvector(tmp, sizeof(tmp), tests[i].hash, sizeof(tests[i].hash), "SHA256", i)) { |
321 | | return CRYPT_FAIL_TESTVECTOR; |
322 | | } |
323 | | } |
324 | | return CRYPT_OK; |
325 | | #endif |
326 | 0 | } |
327 | | |
328 | | #endif |
329 | | |
330 | | |
331 | | |
332 | | /* ref: $Format:%D$ */ |
333 | | /* git commit: $Format:%H$ */ |
334 | | /* commit time: $Format:%ai$ */ |