annotate toys/sha1sum.c @ 562:4d802d438983

Match uint64_t with PRIu64 to avoid warnings on 64 bit builds.
author Rob Landley <rob@landley.net>
date Sat, 14 Apr 2012 21:27:00 -0500
parents 163498bf547b
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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/
192
8c0809cee2b0 Minor cleanups to sha1sum.
Rob Landley <rob@landley.net>
parents: 187
diff changeset
9 *
8c0809cee2b0 Minor cleanups to sha1sum.
Rob Landley <rob@landley.net>
parents: 187
diff changeset
10 * Not in SUSv3.
233
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 192
diff changeset
11
234
163498bf547b Move NEWTOY() list from end of toylist.h to generated/newtoys.h.
Rob Landley <rob@landley.net>
parents: 233
diff changeset
12 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
13
233
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 192
diff changeset
14 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
15 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
16 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
17 help
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 192
diff changeset
18 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
19
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 192
diff changeset
20 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
21 */
170
58a97722d745 Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff changeset
22
187
c983a0af6d4e Add sha1sum. (No -c mode yet.) Slight tweaks to loopfiles() too.
Rob Landley <rob@landley.net>
parents: 186
diff changeset
23 #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
24
173
01d005e67225 More cleanup: consistent indents, uint32_t, rename functions and structs,
Rob Landley <rob@landley.net>
parents: 172
diff changeset
25 struct sha1 {
01d005e67225 More cleanup: consistent indents, uint32_t, rename functions and structs,
Rob Landley <rob@landley.net>
parents: 172
diff changeset
26 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
27 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
28 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
29 union {
8819067ec603 Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents: 173
diff changeset
30 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
31 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
32 } buffer;
173
01d005e67225 More cleanup: consistent indents, uint32_t, rename functions and structs,
Rob Landley <rob@landley.net>
parents: 172
diff changeset
33 };
170
58a97722d745 Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff changeset
34
192
8c0809cee2b0 Minor cleanups to sha1sum.
Rob Landley <rob@landley.net>
parents: 187
diff changeset
35 static void sha1_init(struct sha1 *this);
8c0809cee2b0 Minor cleanups to sha1sum.
Rob Landley <rob@landley.net>
parents: 187
diff changeset
36 static void sha1_transform(struct sha1 *this);
8c0809cee2b0 Minor cleanups to sha1sum.
Rob Landley <rob@landley.net>
parents: 187
diff changeset
37 static void sha1_update(struct sha1 *this, char *data, unsigned int len);
8c0809cee2b0 Minor cleanups to sha1sum.
Rob Landley <rob@landley.net>
parents: 187
diff changeset
38 static void sha1_final(struct sha1 *this, char digest[20]);
170
58a97722d745 Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff changeset
39
58a97722d745 Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff changeset
40 #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
41
192
8c0809cee2b0 Minor cleanups to sha1sum.
Rob Landley <rob@landley.net>
parents: 187
diff changeset
42 // blk0() and blk() perform the initial expand.
8c0809cee2b0 Minor cleanups to sha1sum.
Rob Landley <rob@landley.net>
parents: 187
diff changeset
43 // 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
44 #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
45 #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
46 |(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
47 #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
48 #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
49 #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
50 #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
51 ^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
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 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
54
192
8c0809cee2b0 Minor cleanups to sha1sum.
Rob Landley <rob@landley.net>
parents: 187
diff changeset
55 // 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
56
192
8c0809cee2b0 Minor cleanups to sha1sum.
Rob Landley <rob@landley.net>
parents: 187
diff changeset
57 static void sha1_transform(struct sha1 *this)
170
58a97722d745 Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff changeset
58 {
175
318fc45fe7bf Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents: 174
diff changeset
59 int i, j, k, count;
174
8819067ec603 Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents: 173
diff changeset
60 uint32_t *block = this->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
61 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
62
192
8c0809cee2b0 Minor cleanups to sha1sum.
Rob Landley <rob@landley.net>
parents: 187
diff changeset
63 // 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
64 for (i=0; i<5; i++) {
318fc45fe7bf Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents: 174
diff changeset
65 this->oldstate[i] = this->state[i];
318fc45fe7bf Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents: 174
diff changeset
66 rot[i] = this->state + i;
318fc45fe7bf Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents: 174
diff changeset
67 }
192
8c0809cee2b0 Minor cleanups to sha1sum.
Rob Landley <rob@landley.net>
parents: 187
diff changeset
68 // 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
69 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
70 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
71 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
72
318fc45fe7bf Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents: 174
diff changeset
73 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
74 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
75 else {
318fc45fe7bf Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents: 174
diff changeset
76 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
77 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
78 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
79 }
318fc45fe7bf Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents: 174
diff changeset
80 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
81 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
82 *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
83 *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
84
318fc45fe7bf Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents: 174
diff changeset
85 // 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
86 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
87 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
88 *rot = temp;
318fc45fe7bf Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents: 174
diff changeset
89 count++;
318fc45fe7bf Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents: 174
diff changeset
90 }
318fc45fe7bf Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents: 174
diff changeset
91 }
192
8c0809cee2b0 Minor cleanups to sha1sum.
Rob Landley <rob@landley.net>
parents: 187
diff changeset
92 // Add the previous values of state[]
175
318fc45fe7bf Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents: 174
diff changeset
93 for (i=0; i<5; i++) this->state[i] += this->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
94 }
58a97722d745 Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff changeset
95
58a97722d745 Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff changeset
96
192
8c0809cee2b0 Minor cleanups to sha1sum.
Rob Landley <rob@landley.net>
parents: 187
diff changeset
97 // Initialize a struct sha1.
170
58a97722d745 Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff changeset
98
192
8c0809cee2b0 Minor cleanups to sha1sum.
Rob Landley <rob@landley.net>
parents: 187
diff changeset
99 static void sha1_init(struct sha1 *this)
170
58a97722d745 Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff changeset
100 {
172
19d16003190b Whitespace change: 4 spaces become one tab.
Rob Landley <rob@landley.net>
parents: 171
diff changeset
101 /* SHA1 initialization constants */
173
01d005e67225 More cleanup: consistent indents, uint32_t, rename functions and structs,
Rob Landley <rob@landley.net>
parents: 172
diff changeset
102 this->state[0] = 0x67452301;
01d005e67225 More cleanup: consistent indents, uint32_t, rename functions and structs,
Rob Landley <rob@landley.net>
parents: 172
diff changeset
103 this->state[1] = 0xEFCDAB89;
01d005e67225 More cleanup: consistent indents, uint32_t, rename functions and structs,
Rob Landley <rob@landley.net>
parents: 172
diff changeset
104 this->state[2] = 0x98BADCFE;
01d005e67225 More cleanup: consistent indents, uint32_t, rename functions and structs,
Rob Landley <rob@landley.net>
parents: 172
diff changeset
105 this->state[3] = 0x10325476;
01d005e67225 More cleanup: consistent indents, uint32_t, rename functions and structs,
Rob Landley <rob@landley.net>
parents: 172
diff changeset
106 this->state[4] = 0xC3D2E1F0;
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 this->count = 0;
170
58a97722d745 Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff changeset
108 }
58a97722d745 Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff changeset
109
192
8c0809cee2b0 Minor cleanups to sha1sum.
Rob Landley <rob@landley.net>
parents: 187
diff changeset
110 // 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
111
187
c983a0af6d4e Add sha1sum. (No -c mode yet.) Slight tweaks to loopfiles() too.
Rob Landley <rob@landley.net>
parents: 186
diff changeset
112 void sha1_update(struct sha1 *this, 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
113 {
173
01d005e67225 More cleanup: consistent indents, uint32_t, rename functions and structs,
Rob Landley <rob@landley.net>
parents: 172
diff changeset
114 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
115
174
8819067ec603 Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents: 173
diff changeset
116 j = this->count & 63;
8819067ec603 Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents: 173
diff changeset
117 this->count += len;
8819067ec603 Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents: 173
diff changeset
118
8819067ec603 Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents: 173
diff changeset
119 // Enough data to process a frame?
172
19d16003190b Whitespace change: 4 spaces become one tab.
Rob Landley <rob@landley.net>
parents: 171
diff changeset
120 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
121 i = 64-j;
8819067ec603 Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents: 173
diff changeset
122 memcpy(this->buffer.c + j, data, i);
8819067ec603 Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents: 173
diff changeset
123 sha1_transform(this);
8819067ec603 Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents: 173
diff changeset
124 for ( ; i + 63 < len; i += 64) {
8819067ec603 Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents: 173
diff changeset
125 memcpy(this->buffer.c, data + i, 64);
8819067ec603 Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents: 173
diff changeset
126 sha1_transform(this);
8819067ec603 Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents: 173
diff changeset
127 }
172
19d16003190b Whitespace change: 4 spaces become one tab.
Rob Landley <rob@landley.net>
parents: 171
diff changeset
128 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
129 } 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
130 // Grab remaining chunk
8819067ec603 Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents: 173
diff changeset
131 memcpy(this->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
132 }
58a97722d745 Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff changeset
133
192
8c0809cee2b0 Minor cleanups to sha1sum.
Rob Landley <rob@landley.net>
parents: 187
diff changeset
134 // Add padding and return the message digest.
170
58a97722d745 Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff changeset
135
187
c983a0af6d4e Add sha1sum. (No -c mode yet.) Slight tweaks to loopfiles() too.
Rob Landley <rob@landley.net>
parents: 186
diff changeset
136 void sha1_final(struct sha1 *this, char digest[20])
170
58a97722d745 Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
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 uint64_t count = this->count << 3;
8819067ec603 Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents: 173
diff changeset
139 unsigned int i;
187
c983a0af6d4e Add sha1sum. (No -c mode yet.) Slight tweaks to loopfiles() too.
Rob Landley <rob@landley.net>
parents: 186
diff changeset
140 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
141
8819067ec603 Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents: 173
diff changeset
142 // 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
143 // message size (in bits, big endian), and adding enough zero bits in
192
8c0809cee2b0 Minor cleanups to sha1sum.
Rob Landley <rob@landley.net>
parents: 187
diff changeset
144 // between to pad to the end of the next 64-byte frame.
8c0809cee2b0 Minor cleanups to sha1sum.
Rob Landley <rob@landley.net>
parents: 187
diff changeset
145 //
8c0809cee2b0 Minor cleanups to sha1sum.
Rob Landley <rob@landley.net>
parents: 187
diff changeset
146 // Since our input up to now has been in whole bytes, we can deal with
8c0809cee2b0 Minor cleanups to sha1sum.
Rob Landley <rob@landley.net>
parents: 187
diff changeset
147 // 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
148
174
8819067ec603 Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents: 173
diff changeset
149 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
150 do {
8819067ec603 Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents: 173
diff changeset
151 sha1_update(this, &buf, 1);
8819067ec603 Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents: 173
diff changeset
152 buf = 0;
8819067ec603 Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents: 173
diff changeset
153 } while ((this->count & 63) != 56);
8819067ec603 Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents: 173
diff changeset
154 for (i = 0; i < 8; i++)
8819067ec603 Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents: 173
diff changeset
155 this->buffer.c[56+i] = count >> (8*(7-i));
8819067ec603 Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents: 173
diff changeset
156 sha1_transform(this);
8819067ec603 Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents: 173
diff changeset
157
187
c983a0af6d4e Add sha1sum. (No -c mode yet.) Slight tweaks to loopfiles() too.
Rob Landley <rob@landley.net>
parents: 186
diff changeset
158 for (i = 0; i < 20; i++)
c983a0af6d4e Add sha1sum. (No -c mode yet.) Slight tweaks to loopfiles() too.
Rob Landley <rob@landley.net>
parents: 186
diff changeset
159 digest[i] = this->state[i>>2] >> ((3-(i & 3)) * 8);
192
8c0809cee2b0 Minor cleanups to sha1sum.
Rob Landley <rob@landley.net>
parents: 187
diff changeset
160 // Wipe variables. Cryptogropher paranoia.
175
318fc45fe7bf Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents: 174
diff changeset
161 memset(this, 0, sizeof(struct sha1));
170
58a97722d745 Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff changeset
162 }
58a97722d745 Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff changeset
163
187
c983a0af6d4e Add sha1sum. (No -c mode yet.) Slight tweaks to loopfiles() too.
Rob Landley <rob@landley.net>
parents: 186
diff changeset
164 // 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
165
187
c983a0af6d4e Add sha1sum. (No -c mode yet.) Slight tweaks to loopfiles() too.
Rob Landley <rob@landley.net>
parents: 186
diff changeset
166 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
167 {
173
01d005e67225 More cleanup: consistent indents, uint32_t, rename functions and structs,
Rob Landley <rob@landley.net>
parents: 172
diff changeset
168 struct sha1 this;
187
c983a0af6d4e Add sha1sum. (No -c mode yet.) Slight tweaks to loopfiles() too.
Rob Landley <rob@landley.net>
parents: 186
diff changeset
169 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
170
187
c983a0af6d4e Add sha1sum. (No -c mode yet.) Slight tweaks to loopfiles() too.
Rob Landley <rob@landley.net>
parents: 186
diff changeset
171 sha1_init(&this);
c983a0af6d4e Add sha1sum. (No -c mode yet.) Slight tweaks to loopfiles() too.
Rob Landley <rob@landley.net>
parents: 186
diff changeset
172 for (;;) {
c983a0af6d4e Add sha1sum. (No -c mode yet.) Slight tweaks to loopfiles() too.
Rob Landley <rob@landley.net>
parents: 186
diff changeset
173 len = read(fd, toybuf, sizeof(toybuf));
c983a0af6d4e Add sha1sum. (No -c mode yet.) Slight tweaks to loopfiles() too.
Rob Landley <rob@landley.net>
parents: 186
diff changeset
174 if (len<1) break;
c983a0af6d4e Add sha1sum. (No -c mode yet.) Slight tweaks to loopfiles() too.
Rob Landley <rob@landley.net>
parents: 186
diff changeset
175 sha1_update(&this, toybuf, len);
172
19d16003190b Whitespace change: 4 spaces become one tab.
Rob Landley <rob@landley.net>
parents: 171
diff changeset
176 }
187
c983a0af6d4e Add sha1sum. (No -c mode yet.) Slight tweaks to loopfiles() too.
Rob Landley <rob@landley.net>
parents: 186
diff changeset
177 sha1_final(&this, toybuf);
c983a0af6d4e Add sha1sum. (No -c mode yet.) Slight tweaks to loopfiles() too.
Rob Landley <rob@landley.net>
parents: 186
diff changeset
178 for (len = 0; len < 20; len++) printf("%02x", toybuf[len]);
c983a0af6d4e Add sha1sum. (No -c mode yet.) Slight tweaks to loopfiles() too.
Rob Landley <rob@landley.net>
parents: 186
diff changeset
179 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
180 }
187
c983a0af6d4e Add sha1sum. (No -c mode yet.) Slight tweaks to loopfiles() too.
Rob Landley <rob@landley.net>
parents: 186
diff changeset
181
c983a0af6d4e Add sha1sum. (No -c mode yet.) Slight tweaks to loopfiles() too.
Rob Landley <rob@landley.net>
parents: 186
diff changeset
182 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
183 {
c983a0af6d4e Add sha1sum. (No -c mode yet.) Slight tweaks to loopfiles() too.
Rob Landley <rob@landley.net>
parents: 186
diff changeset
184 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
185 }