Mercurial > hg > toybox
annotate toys/other/sha1sum.c @ 674:7e846e281e38
New build infrastructure to generate FLAG_ macros and TT alias, #define FOR_commandname before #including toys.h to trigger it. Rename DEFINE_GLOBALS() to just GLOBALS() (because I could never remember if it was DECLARE_GLOBALS). Convert existing commands to use new infrastructure, and replace optflag constants with FLAG_ macros where appropriate.
author | Rob Landley <rob@landley.net> |
---|---|
date | Mon, 08 Oct 2012 00:02:30 -0500 |
parents | b88859043af2 |
children | 522ec2aad996 |
rev | line source |
---|---|
233
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
192
diff
changeset
|
1 /* vi: set sw=4 ts=4: |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
192
diff
changeset
|
2 * |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
192
diff
changeset
|
3 * sha1sum.c - Calculate sha1 cryptographic hash for input. |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
192
diff
changeset
|
4 * |
187
c983a0af6d4e
Add sha1sum. (No -c mode yet.) Slight tweaks to loopfiles() too.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
5 * Copyright 2007 Rob Landley <rob@landley.net> |
c983a0af6d4e
Add sha1sum. (No -c mode yet.) Slight tweaks to loopfiles() too.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
6 * |
173
01d005e67225
More cleanup: consistent indents, uint32_t, rename functions and structs,
Rob Landley <rob@landley.net>
parents:
172
diff
changeset
|
7 * Based on the public domain SHA-1 in C by Steve Reid <steve@edmweb.com> |
01d005e67225
More cleanup: consistent indents, uint32_t, rename functions and structs,
Rob Landley <rob@landley.net>
parents:
172
diff
changeset
|
8 * from http://www.mirrors.wiretapped.net/security/cryptography/hashes/sha1/ |
233
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
192
diff
changeset
|
9 |
234
163498bf547b
Move NEWTOY() list from end of toylist.h to generated/newtoys.h.
Rob Landley <rob@landley.net>
parents:
233
diff
changeset
|
10 USE_SHA1SUM(NEWTOY(sha1sum, NULL, TOYFLAG_USR|TOYFLAG_BIN)) |
163498bf547b
Move NEWTOY() list from end of toylist.h to generated/newtoys.h.
Rob Landley <rob@landley.net>
parents:
233
diff
changeset
|
11 |
233
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
192
diff
changeset
|
12 config SHA1SUM |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
192
diff
changeset
|
13 bool "sha1sum" |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
192
diff
changeset
|
14 default y |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
192
diff
changeset
|
15 help |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
192
diff
changeset
|
16 usage: sha1sum [file...] |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
192
diff
changeset
|
17 |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
192
diff
changeset
|
18 Calculate sha1 hash of files (or stdin). |
d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents:
192
diff
changeset
|
19 */ |
170
58a97722d745
Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
20 |
674
7e846e281e38
New build infrastructure to generate FLAG_ macros and TT alias, #define FOR_commandname before #including toys.h to trigger it. Rename DEFINE_GLOBALS() to just GLOBALS() (because I could never remember if it was DECLARE_GLOBALS). Convert existing commands to use new infrastructure, and replace optflag constants with FLAG_ macros where appropriate.
Rob Landley <rob@landley.net>
parents:
666
diff
changeset
|
21 #define FOR_sha1sum |
187
c983a0af6d4e
Add sha1sum. (No -c mode yet.) Slight tweaks to loopfiles() too.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
22 #include <toys.h> |
170
58a97722d745
Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
23 |
674
7e846e281e38
New build infrastructure to generate FLAG_ macros and TT alias, #define FOR_commandname before #including toys.h to trigger it. Rename DEFINE_GLOBALS() to just GLOBALS() (because I could never remember if it was DECLARE_GLOBALS). Convert existing commands to use new infrastructure, and replace optflag constants with FLAG_ macros where appropriate.
Rob Landley <rob@landley.net>
parents:
666
diff
changeset
|
24 GLOBALS( |
173
01d005e67225
More cleanup: consistent indents, uint32_t, rename functions and structs,
Rob Landley <rob@landley.net>
parents:
172
diff
changeset
|
25 uint32_t state[5]; |
175
318fc45fe7bf
Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset
|
26 uint32_t oldstate[5]; |
174
8819067ec603
Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents:
173
diff
changeset
|
27 uint64_t count; |
8819067ec603
Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents:
173
diff
changeset
|
28 union { |
8819067ec603
Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents:
173
diff
changeset
|
29 unsigned char c[64]; |
8819067ec603
Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents:
173
diff
changeset
|
30 uint32_t i[16]; |
8819067ec603
Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents:
173
diff
changeset
|
31 } buffer; |
666
b88859043af2
Clean up sha1sum, make it use global union properly.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
32 ) |
170
58a97722d745
Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
33 |
58a97722d745
Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
34 #define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits)))) |
58a97722d745
Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
35 |
192 | 36 // blk0() and blk() perform the initial expand. |
37 // The idea of expanding during the round function comes from SSLeay | |
187
c983a0af6d4e
Add sha1sum. (No -c mode yet.) Slight tweaks to loopfiles() too.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
38 #if 1 |
174
8819067ec603
Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents:
173
diff
changeset
|
39 #define blk0(i) (block[i] = (rol(block[i],24)&0xFF00FF00) \ |
8819067ec603
Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents:
173
diff
changeset
|
40 |(rol(block[i],8)&0x00FF00FF)) |
187
c983a0af6d4e
Add sha1sum. (No -c mode yet.) Slight tweaks to loopfiles() too.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
41 #else // big endian? |
174
8819067ec603
Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents:
173
diff
changeset
|
42 #define blk0(i) block[i] |
170
58a97722d745
Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
43 #endif |
174
8819067ec603
Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents:
173
diff
changeset
|
44 #define blk(i) (block[i&15] = rol(block[(i+13)&15]^block[(i+8)&15] \ |
8819067ec603
Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents:
173
diff
changeset
|
45 ^block[(i+2)&15]^block[i&15],1)) |
170
58a97722d745
Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
46 |
175
318fc45fe7bf
Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset
|
47 static const uint32_t rconsts[]={0x5A827999,0x6ED9EBA1,0x8F1BBCDC,0xCA62C1D6}; |
170
58a97722d745
Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
48 |
192 | 49 // Hash a single 512-bit block. This is the core of the algorithm. |
170
58a97722d745
Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
50 |
666
b88859043af2
Clean up sha1sum, make it use global union properly.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
51 static void sha1_transform(void) |
170
58a97722d745
Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
52 { |
175
318fc45fe7bf
Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset
|
53 int i, j, k, count; |
666
b88859043af2
Clean up sha1sum, make it use global union properly.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
54 uint32_t *block = TT.buffer.i; |
175
318fc45fe7bf
Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset
|
55 uint32_t *rot[5], *temp; |
173
01d005e67225
More cleanup: consistent indents, uint32_t, rename functions and structs,
Rob Landley <rob@landley.net>
parents:
172
diff
changeset
|
56 |
192 | 57 // Copy context->state[] to working vars |
175
318fc45fe7bf
Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset
|
58 for (i=0; i<5; i++) { |
666
b88859043af2
Clean up sha1sum, make it use global union properly.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
59 TT.oldstate[i] = TT.state[i]; |
b88859043af2
Clean up sha1sum, make it use global union properly.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
60 rot[i] = TT.state + i; |
175
318fc45fe7bf
Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset
|
61 } |
192 | 62 // 4 rounds of 20 operations each. |
175
318fc45fe7bf
Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset
|
63 for (i=count=0; i<4; i++) { |
318fc45fe7bf
Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset
|
64 for (j=0; j<20; j++) { |
318fc45fe7bf
Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset
|
65 uint32_t work; |
318fc45fe7bf
Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset
|
66 |
318fc45fe7bf
Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset
|
67 work = *rot[2] ^ *rot[3]; |
318fc45fe7bf
Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset
|
68 if (!i) work = (work & *rot[1]) ^ *rot[3]; |
318fc45fe7bf
Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset
|
69 else { |
318fc45fe7bf
Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset
|
70 if (i==2) |
318fc45fe7bf
Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset
|
71 work = ((*rot[1]|*rot[2])&*rot[3])|(*rot[1]&*rot[2]); |
318fc45fe7bf
Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset
|
72 else work ^= *rot[1]; |
318fc45fe7bf
Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset
|
73 } |
318fc45fe7bf
Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset
|
74 if (!i && j<16) work += blk0(count); |
318fc45fe7bf
Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset
|
75 else work += blk(count); |
318fc45fe7bf
Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset
|
76 *rot[4] += work + rol(*rot[0],5) + rconsts[i]; |
318fc45fe7bf
Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset
|
77 *rot[1] = rol(*rot[1],30); |
318fc45fe7bf
Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset
|
78 |
318fc45fe7bf
Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset
|
79 // Rotate by one for next time. |
318fc45fe7bf
Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset
|
80 temp = rot[4]; |
318fc45fe7bf
Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset
|
81 for (k=4; k; k--) rot[k] = rot[k-1]; |
318fc45fe7bf
Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset
|
82 *rot = temp; |
318fc45fe7bf
Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset
|
83 count++; |
318fc45fe7bf
Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset
|
84 } |
318fc45fe7bf
Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset
|
85 } |
192 | 86 // Add the previous values of state[] |
666
b88859043af2
Clean up sha1sum, make it use global union properly.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
87 for (i=0; i<5; i++) TT.state[i] += TT.oldstate[i]; |
170
58a97722d745
Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
88 } |
58a97722d745
Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
89 |
192 | 90 // Fill the 64-byte working buffer and call sha1_transform() when full. |
170
58a97722d745
Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
91 |
666
b88859043af2
Clean up sha1sum, make it use global union properly.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
92 static void sha1_update(char *data, unsigned int len) |
170
58a97722d745
Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
93 { |
173
01d005e67225
More cleanup: consistent indents, uint32_t, rename functions and structs,
Rob Landley <rob@landley.net>
parents:
172
diff
changeset
|
94 unsigned int i, j; |
170
58a97722d745
Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
95 |
666
b88859043af2
Clean up sha1sum, make it use global union properly.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
96 j = TT.count & 63; |
b88859043af2
Clean up sha1sum, make it use global union properly.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
97 TT.count += len; |
174
8819067ec603
Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents:
173
diff
changeset
|
98 |
8819067ec603
Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents:
173
diff
changeset
|
99 // Enough data to process a frame? |
172
19d16003190b
Whitespace change: 4 spaces become one tab.
Rob Landley <rob@landley.net>
parents:
171
diff
changeset
|
100 if ((j + len) > 63) { |
174
8819067ec603
Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents:
173
diff
changeset
|
101 i = 64-j; |
666
b88859043af2
Clean up sha1sum, make it use global union properly.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
102 memcpy(TT.buffer.c + j, data, i); |
b88859043af2
Clean up sha1sum, make it use global union properly.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
103 sha1_transform(); |
174
8819067ec603
Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents:
173
diff
changeset
|
104 for ( ; i + 63 < len; i += 64) { |
666
b88859043af2
Clean up sha1sum, make it use global union properly.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
105 memcpy(TT.buffer.c, data + i, 64); |
b88859043af2
Clean up sha1sum, make it use global union properly.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
106 sha1_transform(); |
174
8819067ec603
Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents:
173
diff
changeset
|
107 } |
172
19d16003190b
Whitespace change: 4 spaces become one tab.
Rob Landley <rob@landley.net>
parents:
171
diff
changeset
|
108 j = 0; |
174
8819067ec603
Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents:
173
diff
changeset
|
109 } else i = 0; |
8819067ec603
Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents:
173
diff
changeset
|
110 // Grab remaining chunk |
666
b88859043af2
Clean up sha1sum, make it use global union properly.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
111 memcpy(TT.buffer.c + j, data + i, len - i); |
170
58a97722d745
Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
112 } |
58a97722d745
Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
113 |
666
b88859043af2
Clean up sha1sum, make it use global union properly.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
114 // Callback for loopfiles() |
170
58a97722d745
Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
115 |
666
b88859043af2
Clean up sha1sum, make it use global union properly.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
116 static void do_sha1(int fd, char *name) |
170
58a97722d745
Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
117 { |
666
b88859043af2
Clean up sha1sum, make it use global union properly.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
118 uint64_t count; |
b88859043af2
Clean up sha1sum, make it use global union properly.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
119 int i; |
187
c983a0af6d4e
Add sha1sum. (No -c mode yet.) Slight tweaks to loopfiles() too.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
120 char buf; |
174
8819067ec603
Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents:
173
diff
changeset
|
121 |
666
b88859043af2
Clean up sha1sum, make it use global union properly.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
122 /* SHA1 initialization constants */ |
b88859043af2
Clean up sha1sum, make it use global union properly.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
123 TT.state[0] = 0x67452301; |
b88859043af2
Clean up sha1sum, make it use global union properly.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
124 TT.state[1] = 0xEFCDAB89; |
b88859043af2
Clean up sha1sum, make it use global union properly.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
125 TT.state[2] = 0x98BADCFE; |
b88859043af2
Clean up sha1sum, make it use global union properly.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
126 TT.state[3] = 0x10325476; |
b88859043af2
Clean up sha1sum, make it use global union properly.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
127 TT.state[4] = 0xC3D2E1F0; |
b88859043af2
Clean up sha1sum, make it use global union properly.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
128 TT.count = 0; |
b88859043af2
Clean up sha1sum, make it use global union properly.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
129 |
b88859043af2
Clean up sha1sum, make it use global union properly.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
130 for (;;) { |
b88859043af2
Clean up sha1sum, make it use global union properly.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
131 i = read(fd, toybuf, sizeof(toybuf)); |
b88859043af2
Clean up sha1sum, make it use global union properly.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
132 if (i<1) break; |
b88859043af2
Clean up sha1sum, make it use global union properly.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
133 sha1_update(toybuf, i); |
b88859043af2
Clean up sha1sum, make it use global union properly.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
134 } |
b88859043af2
Clean up sha1sum, make it use global union properly.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
135 |
b88859043af2
Clean up sha1sum, make it use global union properly.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
136 count = TT.count << 3; |
b88859043af2
Clean up sha1sum, make it use global union properly.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
137 |
174
8819067ec603
Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents:
173
diff
changeset
|
138 // End the message by appending a "1" bit to the data, ending with the |
8819067ec603
Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents:
173
diff
changeset
|
139 // message size (in bits, big endian), and adding enough zero bits in |
192 | 140 // between to pad to the end of the next 64-byte frame. |
141 // | |
142 // Since our input up to now has been in whole bytes, we can deal with | |
143 // bytes here too. | |
170
58a97722d745
Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
144 |
174
8819067ec603
Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents:
173
diff
changeset
|
145 buf = 0x80; |
8819067ec603
Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents:
173
diff
changeset
|
146 do { |
666
b88859043af2
Clean up sha1sum, make it use global union properly.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
147 sha1_update(&buf, 1); |
174
8819067ec603
Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents:
173
diff
changeset
|
148 buf = 0; |
666
b88859043af2
Clean up sha1sum, make it use global union properly.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
149 } while ((TT.count & 63) != 56); |
174
8819067ec603
Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents:
173
diff
changeset
|
150 for (i = 0; i < 8; i++) |
666
b88859043af2
Clean up sha1sum, make it use global union properly.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
151 TT.buffer.c[56+i] = count >> (8*(7-i)); |
b88859043af2
Clean up sha1sum, make it use global union properly.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
152 sha1_transform(); |
174
8819067ec603
Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents:
173
diff
changeset
|
153 |
187
c983a0af6d4e
Add sha1sum. (No -c mode yet.) Slight tweaks to loopfiles() too.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
154 for (i = 0; i < 20; i++) |
666
b88859043af2
Clean up sha1sum, make it use global union properly.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
155 toybuf[i] = TT.state[i>>2] >> ((3-(i & 3)) * 8); |
192 | 156 // Wipe variables. Cryptogropher paranoia. |
666
b88859043af2
Clean up sha1sum, make it use global union properly.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
157 memset(&TT, 0, sizeof(TT)); |
170
58a97722d745
Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
158 |
666
b88859043af2
Clean up sha1sum, make it use global union properly.
Rob Landley <rob@landley.net>
parents:
656
diff
changeset
|
159 for (i = 0; i < 20; i++) printf("%02x", toybuf[i]); |
187
c983a0af6d4e
Add sha1sum. (No -c mode yet.) Slight tweaks to loopfiles() too.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
160 printf(" %s\n", name); |
170
58a97722d745
Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
161 } |
187
c983a0af6d4e
Add sha1sum. (No -c mode yet.) Slight tweaks to loopfiles() too.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
162 |
c983a0af6d4e
Add sha1sum. (No -c mode yet.) Slight tweaks to loopfiles() too.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
163 void sha1sum_main(void) |
c983a0af6d4e
Add sha1sum. (No -c mode yet.) Slight tweaks to loopfiles() too.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
164 { |
c983a0af6d4e
Add sha1sum. (No -c mode yet.) Slight tweaks to loopfiles() too.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
165 loopfiles(toys.optargs, do_sha1); |
c983a0af6d4e
Add sha1sum. (No -c mode yet.) Slight tweaks to loopfiles() too.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset
|
166 } |