Mercurial > hg > toybox
comparison toys/other/sha1sum.c @ 676:522ec2aad996
Simplify sha1sum a little.
author | Rob Landley <rob@landley.net> |
---|---|
date | Mon, 08 Oct 2012 21:27:58 -0500 |
parents | 7e846e281e38 |
children |
comparison
equal
deleted
inserted
replaced
675:fa858a67e231 | 676:522ec2aad996 |
---|---|
29 unsigned char c[64]; | 29 unsigned char c[64]; |
30 uint32_t i[16]; | 30 uint32_t i[16]; |
31 } buffer; | 31 } buffer; |
32 ) | 32 ) |
33 | 33 |
34 #define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits)))) | |
35 | 34 |
36 // blk0() and blk() perform the initial expand. | 35 static const unsigned rconsts[]={0x5A827999,0x6ED9EBA1,0x8F1BBCDC,0xCA62C1D6}; |
37 // The idea of expanding during the round function comes from SSLeay | |
38 #if 1 | |
39 #define blk0(i) (block[i] = (rol(block[i],24)&0xFF00FF00) \ | |
40 |(rol(block[i],8)&0x00FF00FF)) | |
41 #else // big endian? | |
42 #define blk0(i) block[i] | |
43 #endif | |
44 #define blk(i) (block[i&15] = rol(block[(i+13)&15]^block[(i+8)&15] \ | |
45 ^block[(i+2)&15]^block[i&15],1)) | |
46 | |
47 static const uint32_t rconsts[]={0x5A827999,0x6ED9EBA1,0x8F1BBCDC,0xCA62C1D6}; | |
48 | 36 |
49 // Hash a single 512-bit block. This is the core of the algorithm. | 37 // Hash a single 512-bit block. This is the core of the algorithm. |
50 | 38 |
39 #define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits)))) | |
51 static void sha1_transform(void) | 40 static void sha1_transform(void) |
52 { | 41 { |
53 int i, j, k, count; | 42 int i, j, k, count; |
54 uint32_t *block = TT.buffer.i; | 43 unsigned *block = TT.buffer.i; |
55 uint32_t *rot[5], *temp; | 44 unsigned *rot[5], *temp; |
56 | 45 |
57 // Copy context->state[] to working vars | 46 // Copy context->state[] to working vars |
58 for (i=0; i<5; i++) { | 47 for (i=0; i<5; i++) { |
59 TT.oldstate[i] = TT.state[i]; | 48 TT.oldstate[i] = TT.state[i]; |
60 rot[i] = TT.state + i; | 49 rot[i] = TT.state + i; |
61 } | 50 } |
62 // 4 rounds of 20 operations each. | 51 // 4 rounds of 20 operations each. |
63 for (i=count=0; i<4; i++) { | 52 for (i=count=0; i<4; i++) { |
64 for (j=0; j<20; j++) { | 53 for (j=0; j<20; j++) { |
65 uint32_t work; | 54 unsigned work; |
66 | 55 |
67 work = *rot[2] ^ *rot[3]; | 56 work = *rot[2] ^ *rot[3]; |
68 if (!i) work = (work & *rot[1]) ^ *rot[3]; | 57 if (!i) work = (work & *rot[1]) ^ *rot[3]; |
69 else { | 58 else { |
70 if (i==2) | 59 if (i==2) |
71 work = ((*rot[1]|*rot[2])&*rot[3])|(*rot[1]&*rot[2]); | 60 work = ((*rot[1]|*rot[2])&*rot[3])|(*rot[1]&*rot[2]); |
72 else work ^= *rot[1]; | 61 else work ^= *rot[1]; |
73 } | 62 } |
74 if (!i && j<16) work += blk0(count); | 63 |
75 else work += blk(count); | 64 if (!i && j<16) work += block[count] = (rol(block[count],24)&0xFF00FF00) | (rol(block[count],8)&0x00FF00FF); |
65 else work += block[count&15] = rol(block[(count+13)&15]^block[(count+8)&15]^block[(count+2)&15]^block[count&15],1); | |
76 *rot[4] += work + rol(*rot[0],5) + rconsts[i]; | 66 *rot[4] += work + rol(*rot[0],5) + rconsts[i]; |
77 *rot[1] = rol(*rot[1],30); | 67 *rot[1] = rol(*rot[1],30); |
78 | 68 |
79 // Rotate by one for next time. | 69 // Rotate by one for next time. |
80 temp = rot[4]; | 70 temp = rot[4]; |