comparison sha256.c @ 15:6362d3854bb4 libtomcrypt-orig

0.96 release of LibTomCrypt
author Matt Johnston <matt@ucc.asn.au>
date Tue, 15 Jun 2004 14:07:21 +0000
parents 7faae8f46238
children 09ab3354aa21 5d99163f7e32
comparison
equal deleted inserted replaced
3:7faae8f46238 15:6362d3854bb4
20 { 20 {
21 "sha256", 21 "sha256",
22 0, 22 0,
23 32, 23 32,
24 64, 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
25 &sha256_init, 32 &sha256_init,
26 &sha256_process, 33 &sha256_process,
27 &sha256_done, 34 &sha256_done,
28 &sha256_test 35 &sha256_test
29 }; 36 };
30 37
38 #ifdef SMALL_CODE
31 /* the K array */ 39 /* the K array */
32 static const unsigned long K[64] = { 40 static const unsigned long K[64] = {
33 0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL, 0x3956c25bUL, 41 0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL, 0x3956c25bUL,
34 0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL, 0xd807aa98UL, 0x12835b01UL, 42 0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL, 0xd807aa98UL, 0x12835b01UL,
35 0x243185beUL, 0x550c7dc3UL, 0x72be5d74UL, 0x80deb1feUL, 0x9bdc06a7UL, 43 0x243185beUL, 0x550c7dc3UL, 0x72be5d74UL, 0x80deb1feUL, 0x9bdc06a7UL,
42 0xd6990624UL, 0xf40e3585UL, 0x106aa070UL, 0x19a4c116UL, 0x1e376c08UL, 50 0xd6990624UL, 0xf40e3585UL, 0x106aa070UL, 0x19a4c116UL, 0x1e376c08UL,
43 0x2748774cUL, 0x34b0bcb5UL, 0x391c0cb3UL, 0x4ed8aa4aUL, 0x5b9cca4fUL, 51 0x2748774cUL, 0x34b0bcb5UL, 0x391c0cb3UL, 0x4ed8aa4aUL, 0x5b9cca4fUL,
44 0x682e6ff3UL, 0x748f82eeUL, 0x78a5636fUL, 0x84c87814UL, 0x8cc70208UL, 52 0x682e6ff3UL, 0x748f82eeUL, 0x78a5636fUL, 0x84c87814UL, 0x8cc70208UL,
45 0x90befffaUL, 0xa4506cebUL, 0xbef9a3f7UL, 0xc67178f2UL 53 0x90befffaUL, 0xa4506cebUL, 0xbef9a3f7UL, 0xc67178f2UL
46 }; 54 };
55 #endif
47 56
48 /* Various logical functions */ 57 /* Various logical functions */
49 #define Ch(x,y,z) (z ^ (x & (y ^ z))) 58 #define Ch(x,y,z) (z ^ (x & (y ^ z)))
50 #define Maj(x,y,z) (((x | y) & z) | (x & y)) 59 #define Maj(x,y,z) (((x | y) & z) | (x & y))
51 #define S(x, n) ROR((x),(n)) 60 #define S(x, n) ROR((x),(n))
61 #else 70 #else
62 static void sha256_compress(hash_state * md, unsigned char *buf) 71 static void sha256_compress(hash_state * md, unsigned char *buf)
63 #endif 72 #endif
64 { 73 {
65 ulong32 S[8], W[64], t0, t1; 74 ulong32 S[8], W[64], t0, t1;
75 #ifdef SMALL_CODE
76 ulong32 t;
77 #endif
66 int i; 78 int i;
67 79
68 /* copy state into S */ 80 /* copy state into S */
69 for (i = 0; i < 8; i++) { 81 for (i = 0; i < 8; i++) {
70 S[i] = md->sha256.state[i]; 82 S[i] = md->sha256.state[i];
80 W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) + W[i - 16]; 92 W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) + W[i - 16];
81 } 93 }
82 94
83 /* Compress */ 95 /* Compress */
84 #ifdef SMALL_CODE 96 #ifdef SMALL_CODE
85 #define RND(a,b,c,d,e,f,g,h,i) \ 97 #define RND(a,b,c,d,e,f,g,h,i) \
86 t0 = h + Sigma1(e) + Ch(e, f, g) + K[i] + W[i]; \ 98 t0 = h + Sigma1(e) + Ch(e, f, g) + K[i] + W[i]; \
87 t1 = Sigma0(a) + Maj(a, b, c); \ 99 t1 = Sigma0(a) + Maj(a, b, c); \
88 d += t0; \ 100 d += t0; \
89 h = t0 + t1; 101 h = t0 + t1;
90 102
91 for (i = 0; i < 64; i += 8) { 103 for (i = 0; i < 64; ++i) {
92 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],i+0); 104 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],i);
93 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],i+1); 105 t = S[7]; S[7] = S[6]; S[6] = S[5]; S[5] = S[4];
94 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],i+2); 106 S[4] = S[3]; S[3] = S[2]; S[2] = S[1]; S[1] = S[0]; S[0] = t;
95 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],i+3); 107 }
96 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],i+4);
97 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],i+5);
98 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],i+6);
99 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],i+7);
100 }
101 #else 108 #else
102 #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) \
103 t0 = h + Sigma1(e) + Ch(e, f, g) + ki + W[i]; \ 110 t0 = h + Sigma1(e) + Ch(e, f, g) + ki + W[i]; \
104 t1 = Sigma0(a) + Maj(a, b, c); \ 111 t1 = Sigma0(a) + Maj(a, b, c); \
105 d += t0; \ 112 d += t0; \