comparison sha256.c @ 143:5d99163f7e32 libtomcrypt-orig

import of libtomcrypt 0.99
author Matt Johnston <matt@ucc.asn.au>
date Sun, 19 Dec 2004 11:34:45 +0000
parents 6362d3854bb4
children 7ed585a2c53b
comparison
equal deleted inserted replaced
15:6362d3854bb4 143:5d99163f7e32
64 #define Gamma0(x) (S(x, 7) ^ S(x, 18) ^ R(x, 3)) 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)) 65 #define Gamma1(x) (S(x, 17) ^ S(x, 19) ^ R(x, 10))
66 66
67 /* compress 512-bits */ 67 /* compress 512-bits */
68 #ifdef CLEAN_STACK 68 #ifdef CLEAN_STACK
69 static void _sha256_compress(hash_state * md, unsigned char *buf) 69 static int _sha256_compress(hash_state * md, unsigned char *buf)
70 #else 70 #else
71 static void sha256_compress(hash_state * md, unsigned char *buf) 71 static int sha256_compress(hash_state * md, unsigned char *buf)
72 #endif 72 #endif
73 { 73 {
74 ulong32 S[8], W[64], t0, t1; 74 ulong32 S[8], W[64], t0, t1;
75 #ifdef SMALL_CODE 75 #ifdef SMALL_CODE
76 ulong32 t; 76 ulong32 t;
102 102
103 for (i = 0; i < 64; ++i) { 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); 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]; 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; 106 S[4] = S[3]; S[3] = S[2]; S[2] = S[1]; S[1] = S[0]; S[0] = t;
107 } 107 }
108 #else 108 #else
109 #define RND(a,b,c,d,e,f,g,h,i,ki) \ 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]; \ 110 t0 = h + Sigma1(e) + Ch(e, f, g) + ki + W[i]; \
111 t1 = Sigma0(a) + Maj(a, b, c); \ 111 t1 = Sigma0(a) + Maj(a, b, c); \
112 d += t0; \ 112 d += t0; \
183 183
184 /* feedback */ 184 /* feedback */
185 for (i = 0; i < 8; i++) { 185 for (i = 0; i < 8; i++) {
186 md->sha256.state[i] = md->sha256.state[i] + S[i]; 186 md->sha256.state[i] = md->sha256.state[i] + S[i];
187 } 187 }
188 188 return CRYPT_OK;
189 } 189 }
190 190
191 #ifdef CLEAN_STACK 191 #ifdef CLEAN_STACK
192 static void sha256_compress(hash_state * md, unsigned char *buf) 192 static int sha256_compress(hash_state * md, unsigned char *buf)
193 { 193 {
194 _sha256_compress(md, buf); 194 int err;
195 err = _sha256_compress(md, buf);
195 burn_stack(sizeof(ulong32) * 74); 196 burn_stack(sizeof(ulong32) * 74);
197 return err;
196 } 198 }
197 #endif 199 #endif
198 200
199 /* init the sha256 state */ 201 /* init the sha256 state */
200 void sha256_init(hash_state * md) 202 int sha256_init(hash_state * md)
201 { 203 {
202 _ARGCHK(md != NULL); 204 _ARGCHK(md != NULL);
203 205
204 md->sha256.curlen = 0; 206 md->sha256.curlen = 0;
205 md->sha256.length = 0; 207 md->sha256.length = 0;
209 md->sha256.state[3] = 0xA54FF53AUL; 211 md->sha256.state[3] = 0xA54FF53AUL;
210 md->sha256.state[4] = 0x510E527FUL; 212 md->sha256.state[4] = 0x510E527FUL;
211 md->sha256.state[5] = 0x9B05688CUL; 213 md->sha256.state[5] = 0x9B05688CUL;
212 md->sha256.state[6] = 0x1F83D9ABUL; 214 md->sha256.state[6] = 0x1F83D9ABUL;
213 md->sha256.state[7] = 0x5BE0CD19UL; 215 md->sha256.state[7] = 0x5BE0CD19UL;
216 return CRYPT_OK;
214 } 217 }
215 218
216 HASH_PROCESS(sha256_process, sha256_compress, sha256, 64) 219 HASH_PROCESS(sha256_process, sha256_compress, sha256, 64)
217 220
218 int sha256_done(hash_state * md, unsigned char *hash) 221 int sha256_done(hash_state * md, unsigned char *hash)