comparison sha256.c @ 20:b939f2d4431e libtomcrypt

Include files accidentally zeroed when merging 0.96 release
author Matt Johnston <matt@ucc.asn.au>
date Tue, 15 Jun 2004 16:47:55 +0000
parents 09ab3354aa21
children 7ed585a2c53b
comparison
equal deleted inserted replaced
18:712dd6dfb0eb 20:b939f2d4431e
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 * Tom St Denis, [email protected], http://libtomcrypt.org
10 */
11
12
13 /* SHA256 by Tom St Denis */
14
15 #include "mycrypt.h"
16
17 #ifdef SHA256
18
19 const struct _hash_descriptor sha256_desc =
20 {
21 "sha256",
22 0,
23 32,
24 64,
25
26 /* DER identifier */
27 { 0x30, 0x31, 0x30, 0x0D, 0x06, 0x09, 0x60, 0x86,
28 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
29 0x00, 0x04, 0x20 },
30 19,
31
32 &sha256_init,
33 &sha256_process,
34 &sha256_done,
35 &sha256_test
36 };
37
38 #ifdef SMALL_CODE
39 /* the K array */
40 static const unsigned long K[64] = {
41 0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL, 0x3956c25bUL,
42 0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL, 0xd807aa98UL, 0x12835b01UL,
43 0x243185beUL, 0x550c7dc3UL, 0x72be5d74UL, 0x80deb1feUL, 0x9bdc06a7UL,
44 0xc19bf174UL, 0xe49b69c1UL, 0xefbe4786UL, 0x0fc19dc6UL, 0x240ca1ccUL,
45 0x2de92c6fUL, 0x4a7484aaUL, 0x5cb0a9dcUL, 0x76f988daUL, 0x983e5152UL,
46 0xa831c66dUL, 0xb00327c8UL, 0xbf597fc7UL, 0xc6e00bf3UL, 0xd5a79147UL,
47 0x06ca6351UL, 0x14292967UL, 0x27b70a85UL, 0x2e1b2138UL, 0x4d2c6dfcUL,
48 0x53380d13UL, 0x650a7354UL, 0x766a0abbUL, 0x81c2c92eUL, 0x92722c85UL,
49 0xa2bfe8a1UL, 0xa81a664bUL, 0xc24b8b70UL, 0xc76c51a3UL, 0xd192e819UL,
50 0xd6990624UL, 0xf40e3585UL, 0x106aa070UL, 0x19a4c116UL, 0x1e376c08UL,
51 0x2748774cUL, 0x34b0bcb5UL, 0x391c0cb3UL, 0x4ed8aa4aUL, 0x5b9cca4fUL,
52 0x682e6ff3UL, 0x748f82eeUL, 0x78a5636fUL, 0x84c87814UL, 0x8cc70208UL,
53 0x90befffaUL, 0xa4506cebUL, 0xbef9a3f7UL, 0xc67178f2UL
54 };
55 #endif
56
57 /* Various logical functions */
58 #define Ch(x,y,z) (z ^ (x & (y ^ z)))
59 #define Maj(x,y,z) (((x | y) & z) | (x & y))
60 #define S(x, n) ROR((x),(n))
61 #define R(x, n) (((x)&0xFFFFFFFFUL)>>(n))
62 #define Sigma0(x) (S(x, 2) ^ S(x, 13) ^ S(x, 22))
63 #define Sigma1(x) (S(x, 6) ^ S(x, 11) ^ S(x, 25))
64 #define Gamma0(x) (S(x, 7) ^ S(x, 18) ^ R(x, 3))
65 #define Gamma1(x) (S(x, 17) ^ S(x, 19) ^ R(x, 10))
66
67 /* compress 512-bits */
68 #ifdef CLEAN_STACK
69 static void _sha256_compress(hash_state * md, unsigned char *buf)
70 #else
71 static void sha256_compress(hash_state * md, unsigned char *buf)
72 #endif
73 {
74 ulong32 S[8], W[64], t0, t1;
75 #ifdef SMALL_CODE
76 ulong32 t;
77 #endif
78 int i;
79
80 /* copy state into S */
81 for (i = 0; i < 8; i++) {
82 S[i] = md->sha256.state[i];
83 }
84
85 /* copy the state into 512-bits into W[0..15] */
86 for (i = 0; i < 16; i++) {
87 LOAD32H(W[i], buf + (4*i));
88 }
89
90 /* fill W[16..63] */
91 for (i = 16; i < 64; i++) {
92 W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) + W[i - 16];
93 }
94
95 /* Compress */
96 #ifdef SMALL_CODE
97 #define RND(a,b,c,d,e,f,g,h,i) \
98 t0 = h + Sigma1(e) + Ch(e, f, g) + K[i] + W[i]; \
99 t1 = Sigma0(a) + Maj(a, b, c); \
100 d += t0; \
101 h = t0 + t1;
102
103 for (i = 0; i < 64; ++i) {
104 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],i);
105 t = S[7]; S[7] = S[6]; S[6] = S[5]; S[5] = S[4];
106 S[4] = S[3]; S[3] = S[2]; S[2] = S[1]; S[1] = S[0]; S[0] = t;
107 }
108 #else
109 #define RND(a,b,c,d,e,f,g,h,i,ki) \
110 t0 = h + Sigma1(e) + Ch(e, f, g) + ki + W[i]; \
111 t1 = Sigma0(a) + Maj(a, b, c); \
112 d += t0; \
113 h = t0 + t1;
114
115 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],0,0x428a2f98);
116 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],1,0x71374491);
117 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],2,0xb5c0fbcf);
118 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],3,0xe9b5dba5);
119 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],4,0x3956c25b);
120 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],5,0x59f111f1);
121 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],6,0x923f82a4);
122 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],7,0xab1c5ed5);
123 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],8,0xd807aa98);
124 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],9,0x12835b01);
125 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],10,0x243185be);
126 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],11,0x550c7dc3);
127 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],12,0x72be5d74);
128 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],13,0x80deb1fe);
129 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],14,0x9bdc06a7);
130 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],15,0xc19bf174);
131 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],16,0xe49b69c1);
132 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],17,0xefbe4786);
133 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],18,0x0fc19dc6);
134 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],19,0x240ca1cc);
135 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],20,0x2de92c6f);
136 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],21,0x4a7484aa);
137 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],22,0x5cb0a9dc);
138 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],23,0x76f988da);
139 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],24,0x983e5152);
140 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],25,0xa831c66d);
141 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],26,0xb00327c8);
142 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],27,0xbf597fc7);
143 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],28,0xc6e00bf3);
144 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],29,0xd5a79147);
145 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],30,0x06ca6351);
146 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],31,0x14292967);
147 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],32,0x27b70a85);
148 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],33,0x2e1b2138);
149 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],34,0x4d2c6dfc);
150 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],35,0x53380d13);
151 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],36,0x650a7354);
152 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],37,0x766a0abb);
153 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],38,0x81c2c92e);
154 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],39,0x92722c85);
155 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],40,0xa2bfe8a1);
156 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],41,0xa81a664b);
157 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],42,0xc24b8b70);
158 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],43,0xc76c51a3);
159 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],44,0xd192e819);
160 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],45,0xd6990624);
161 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],46,0xf40e3585);
162 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],47,0x106aa070);
163 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],48,0x19a4c116);
164 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],49,0x1e376c08);
165 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],50,0x2748774c);
166 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],51,0x34b0bcb5);
167 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],52,0x391c0cb3);
168 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],53,0x4ed8aa4a);
169 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],54,0x5b9cca4f);
170 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],55,0x682e6ff3);
171 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],56,0x748f82ee);
172 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],57,0x78a5636f);
173 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],58,0x84c87814);
174 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],59,0x8cc70208);
175 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],60,0x90befffa);
176 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],61,0xa4506ceb);
177 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],62,0xbef9a3f7);
178 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],63,0xc67178f2);
179
180 #undef RND
181
182 #endif
183
184 /* feedback */
185 for (i = 0; i < 8; i++) {
186 md->sha256.state[i] = md->sha256.state[i] + S[i];
187 }
188
189 }
190
191 #ifdef CLEAN_STACK
192 static void sha256_compress(hash_state * md, unsigned char *buf)
193 {
194 _sha256_compress(md, buf);
195 burn_stack(sizeof(ulong32) * 74);
196 }
197 #endif
198
199 /* init the sha256 state */
200 void sha256_init(hash_state * md)
201 {
202 _ARGCHK(md != NULL);
203
204 md->sha256.curlen = 0;
205 md->sha256.length = 0;
206 md->sha256.state[0] = 0x6A09E667UL;
207 md->sha256.state[1] = 0xBB67AE85UL;
208 md->sha256.state[2] = 0x3C6EF372UL;
209 md->sha256.state[3] = 0xA54FF53AUL;
210 md->sha256.state[4] = 0x510E527FUL;
211 md->sha256.state[5] = 0x9B05688CUL;
212 md->sha256.state[6] = 0x1F83D9ABUL;
213 md->sha256.state[7] = 0x5BE0CD19UL;
214 }
215
216 HASH_PROCESS(sha256_process, sha256_compress, sha256, 64)
217
218 int sha256_done(hash_state * md, unsigned char *hash)
219 {
220 int i;
221
222 _ARGCHK(md != NULL);
223 _ARGCHK(hash != NULL);
224
225 if (md->sha256.curlen >= sizeof(md->sha256.buf)) {
226 return CRYPT_INVALID_ARG;
227 }
228
229
230 /* increase the length of the message */
231 md->sha256.length += md->sha256.curlen * 8;
232
233 /* append the '1' bit */
234 md->sha256.buf[md->sha256.curlen++] = (unsigned char)0x80;
235
236 /* if the length is currently above 56 bytes we append zeros
237 * then compress. Then we can fall back to padding zeros and length
238 * encoding like normal.
239 */
240 if (md->sha256.curlen > 56) {
241 while (md->sha256.curlen < 64) {
242 md->sha256.buf[md->sha256.curlen++] = (unsigned char)0;
243 }
244 sha256_compress(md, md->sha256.buf);
245 md->sha256.curlen = 0;
246 }
247
248 /* pad upto 56 bytes of zeroes */
249 while (md->sha256.curlen < 56) {
250 md->sha256.buf[md->sha256.curlen++] = (unsigned char)0;
251 }
252
253 /* store length */
254 STORE64H(md->sha256.length, md->sha256.buf+56);
255 sha256_compress(md, md->sha256.buf);
256
257 /* copy output */
258 for (i = 0; i < 8; i++) {
259 STORE32H(md->sha256.state[i], hash+(4*i));
260 }
261 #ifdef CLEAN_STACK
262 zeromem(md, sizeof(hash_state));
263 #endif
264 return CRYPT_OK;
265 }
266
267 int sha256_test(void)
268 {
269 #ifndef LTC_TEST
270 return CRYPT_NOP;
271 #else
272 static const struct {
273 char *msg;
274 unsigned char hash[32];
275 } tests[] = {
276 { "abc",
277 { 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea,
278 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23,
279 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c,
280 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad }
281 },
282 { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
283 { 0x24, 0x8d, 0x6a, 0x61, 0xd2, 0x06, 0x38, 0xb8,
284 0xe5, 0xc0, 0x26, 0x93, 0x0c, 0x3e, 0x60, 0x39,
285 0xa3, 0x3c, 0xe4, 0x59, 0x64, 0xff, 0x21, 0x67,
286 0xf6, 0xec, 0xed, 0xd4, 0x19, 0xdb, 0x06, 0xc1 }
287 },
288 };
289
290 int i;
291 unsigned char tmp[32];
292 hash_state md;
293
294 for (i = 0; i < (int)(sizeof(tests) / sizeof(tests[0])); i++) {
295 sha256_init(&md);
296 sha256_process(&md, (unsigned char*)tests[i].msg, (unsigned long)strlen(tests[i].msg));
297 sha256_done(&md, tmp);
298 if (memcmp(tmp, tests[i].hash, 32) != 0) {
299 return CRYPT_FAIL_TESTVECTOR;
300 }
301 }
302 return CRYPT_OK;
303 #endif
304 }
305
306 #ifdef SHA224
307 #include "sha224.c"
308 #endif
309
310 #endif
311
312