Mercurial > hg > toybox
annotate toys/sha1.c @ 175:318fc45fe7bf
Re-roll loop, making code smaller. Also localize all the crypto information
into a struct that can be blanked in finalize (no more zeroing local variables).
author | Rob Landley <rob@landley.net> |
---|---|
date | Sat, 24 Nov 2007 22:05:29 -0600 |
parents | 8819067ec603 |
children | 25447caf1b4b |
rev | line source |
---|---|
170
58a97722d745
Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
1 /* |
173
01d005e67225
More cleanup: consistent indents, uint32_t, rename functions and structs,
Rob Landley <rob@landley.net>
parents:
172
diff
changeset
|
2 * 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
|
3 * from http://www.mirrors.wiretapped.net/security/cryptography/hashes/sha1/ |
01d005e67225
More cleanup: consistent indents, uint32_t, rename functions and structs,
Rob Landley <rob@landley.net>
parents:
172
diff
changeset
|
4 */ |
170
58a97722d745
Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
5 |
173
01d005e67225
More cleanup: consistent indents, uint32_t, rename functions and structs,
Rob Landley <rob@landley.net>
parents:
172
diff
changeset
|
6 /* #define LITTLE_ENDIAN * This should be #define'd if true. */ |
170
58a97722d745
Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
7 /* #define SHA1HANDSOFF * Copies data before messing with it. */ |
173
01d005e67225
More cleanup: consistent indents, uint32_t, rename functions and structs,
Rob Landley <rob@landley.net>
parents:
172
diff
changeset
|
8 #define LITTLE_ENDIAN |
170
58a97722d745
Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
9 |
58a97722d745
Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
10 #include <stdio.h> |
58a97722d745
Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
11 #include <string.h> |
173
01d005e67225
More cleanup: consistent indents, uint32_t, rename functions and structs,
Rob Landley <rob@landley.net>
parents:
172
diff
changeset
|
12 #include <inttypes.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
|
13 |
173
01d005e67225
More cleanup: consistent indents, uint32_t, rename functions and structs,
Rob Landley <rob@landley.net>
parents:
172
diff
changeset
|
14 struct sha1 { |
01d005e67225
More cleanup: consistent indents, uint32_t, rename functions and structs,
Rob Landley <rob@landley.net>
parents:
172
diff
changeset
|
15 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
|
16 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
|
17 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
|
18 union { |
8819067ec603
Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents:
173
diff
changeset
|
19 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
|
20 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
|
21 } buffer; |
173
01d005e67225
More cleanup: consistent indents, uint32_t, rename functions and structs,
Rob Landley <rob@landley.net>
parents:
172
diff
changeset
|
22 }; |
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 |
173
01d005e67225
More cleanup: consistent indents, uint32_t, rename functions and structs,
Rob Landley <rob@landley.net>
parents:
172
diff
changeset
|
24 void sha1_init(struct sha1 *this); |
174
8819067ec603
Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents:
173
diff
changeset
|
25 void sha1_transform(struct sha1 *this); |
173
01d005e67225
More cleanup: consistent indents, uint32_t, rename functions and structs,
Rob Landley <rob@landley.net>
parents:
172
diff
changeset
|
26 void sha1_update(struct sha1 *this, unsigned char *data, unsigned int len); |
01d005e67225
More cleanup: consistent indents, uint32_t, rename functions and structs,
Rob Landley <rob@landley.net>
parents:
172
diff
changeset
|
27 void sha1_final(struct sha1 *this, unsigned 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
|
28 |
58a97722d745
Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
29 #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
|
30 |
58a97722d745
Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
31 /* blk0() and blk() perform the initial expand. */ |
58a97722d745
Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
32 /* I got the idea of expanding during the round function from SSLeay */ |
58a97722d745
Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
33 #ifdef LITTLE_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
|
34 #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
|
35 |(rol(block[i],8)&0x00FF00FF)) |
170
58a97722d745
Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
36 #else |
174
8819067ec603
Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents:
173
diff
changeset
|
37 #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
|
38 #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
|
39 #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
|
40 ^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
|
41 |
175
318fc45fe7bf
Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset
|
42 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
|
43 |
173
01d005e67225
More cleanup: consistent indents, uint32_t, rename functions and structs,
Rob Landley <rob@landley.net>
parents:
172
diff
changeset
|
44 void printy(unsigned char *this) |
01d005e67225
More cleanup: consistent indents, uint32_t, rename functions and structs,
Rob Landley <rob@landley.net>
parents:
172
diff
changeset
|
45 { |
01d005e67225
More cleanup: consistent indents, uint32_t, rename functions and structs,
Rob Landley <rob@landley.net>
parents:
172
diff
changeset
|
46 int i; |
01d005e67225
More cleanup: consistent indents, uint32_t, rename functions and structs,
Rob Landley <rob@landley.net>
parents:
172
diff
changeset
|
47 |
01d005e67225
More cleanup: consistent indents, uint32_t, rename functions and structs,
Rob Landley <rob@landley.net>
parents:
172
diff
changeset
|
48 for (i = 0; i < 20; i++) printf("%02x", this[i]); |
01d005e67225
More cleanup: consistent indents, uint32_t, rename functions and structs,
Rob Landley <rob@landley.net>
parents:
172
diff
changeset
|
49 putchar('\n'); |
01d005e67225
More cleanup: consistent indents, uint32_t, rename functions and structs,
Rob Landley <rob@landley.net>
parents:
172
diff
changeset
|
50 } |
170
58a97722d745
Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
51 |
58a97722d745
Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
52 /* Hash a single 512-bit block. This is the core of the algorithm. */ |
58a97722d745
Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
53 |
174
8819067ec603
Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents:
173
diff
changeset
|
54 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
|
55 { |
175
318fc45fe7bf
Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset
|
56 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
|
57 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
|
58 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
|
59 |
172
19d16003190b
Whitespace change: 4 spaces become one tab.
Rob Landley <rob@landley.net>
parents:
171
diff
changeset
|
60 /* 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
|
61 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
|
62 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
|
63 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
|
64 } |
318fc45fe7bf
Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset
|
65 /* 4 rounds of 20 operations each. */ |
318fc45fe7bf
Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset
|
66 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
|
67 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
|
68 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
|
69 |
318fc45fe7bf
Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset
|
70 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
|
71 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
|
72 else { |
318fc45fe7bf
Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset
|
73 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
|
74 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
|
75 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
|
76 } |
318fc45fe7bf
Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset
|
77 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
|
78 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
|
79 *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
|
80 *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
|
81 |
318fc45fe7bf
Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset
|
82 // 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
|
83 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
|
84 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
|
85 *rot = temp; |
318fc45fe7bf
Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset
|
86 count++; |
318fc45fe7bf
Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset
|
87 } |
318fc45fe7bf
Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset
|
88 } |
318fc45fe7bf
Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset
|
89 /* Add the previous values of state[] */ |
318fc45fe7bf
Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset
|
90 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
|
91 } |
58a97722d745
Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
92 |
58a97722d745
Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
93 |
58a97722d745
Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
94 /* SHA1Init - Initialize new context */ |
58a97722d745
Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
95 |
173
01d005e67225
More cleanup: consistent indents, uint32_t, rename functions and structs,
Rob Landley <rob@landley.net>
parents:
172
diff
changeset
|
96 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
|
97 { |
172
19d16003190b
Whitespace change: 4 spaces become one tab.
Rob Landley <rob@landley.net>
parents:
171
diff
changeset
|
98 /* SHA1 initialization constants */ |
173
01d005e67225
More cleanup: consistent indents, uint32_t, rename functions and structs,
Rob Landley <rob@landley.net>
parents:
172
diff
changeset
|
99 this->state[0] = 0x67452301; |
01d005e67225
More cleanup: consistent indents, uint32_t, rename functions and structs,
Rob Landley <rob@landley.net>
parents:
172
diff
changeset
|
100 this->state[1] = 0xEFCDAB89; |
01d005e67225
More cleanup: consistent indents, uint32_t, rename functions and structs,
Rob Landley <rob@landley.net>
parents:
172
diff
changeset
|
101 this->state[2] = 0x98BADCFE; |
01d005e67225
More cleanup: consistent indents, uint32_t, rename functions and structs,
Rob Landley <rob@landley.net>
parents:
172
diff
changeset
|
102 this->state[3] = 0x10325476; |
01d005e67225
More cleanup: consistent indents, uint32_t, rename functions and structs,
Rob Landley <rob@landley.net>
parents:
172
diff
changeset
|
103 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
|
104 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
|
105 } |
58a97722d745
Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
106 |
173
01d005e67225
More cleanup: consistent indents, uint32_t, rename functions and structs,
Rob Landley <rob@landley.net>
parents:
172
diff
changeset
|
107 /* Run your data through this function. */ |
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 |
173
01d005e67225
More cleanup: consistent indents, uint32_t, rename functions and structs,
Rob Landley <rob@landley.net>
parents:
172
diff
changeset
|
109 void sha1_update(struct sha1 *this, unsigned 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
|
110 { |
173
01d005e67225
More cleanup: consistent indents, uint32_t, rename functions and structs,
Rob Landley <rob@landley.net>
parents:
172
diff
changeset
|
111 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
|
112 |
174
8819067ec603
Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents:
173
diff
changeset
|
113 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
|
114 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
|
115 |
8819067ec603
Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents:
173
diff
changeset
|
116 // Enough data to process a frame? |
172
19d16003190b
Whitespace change: 4 spaces become one tab.
Rob Landley <rob@landley.net>
parents:
171
diff
changeset
|
117 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
|
118 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
|
119 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
|
120 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
|
121 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
|
122 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
|
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 } |
172
19d16003190b
Whitespace change: 4 spaces become one tab.
Rob Landley <rob@landley.net>
parents:
171
diff
changeset
|
125 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
|
126 } 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
|
127 // 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
|
128 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
|
129 } |
58a97722d745
Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
130 |
58a97722d745
Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
131 /* Add padding and return the message digest. */ |
58a97722d745
Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
132 |
173
01d005e67225
More cleanup: consistent indents, uint32_t, rename functions and structs,
Rob Landley <rob@landley.net>
parents:
172
diff
changeset
|
133 void sha1_final(struct sha1 *this, unsigned 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
|
134 { |
174
8819067ec603
Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents:
173
diff
changeset
|
135 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
|
136 unsigned int i; |
8819067ec603
Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents:
173
diff
changeset
|
137 unsigned char buf; |
8819067ec603
Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents:
173
diff
changeset
|
138 |
8819067ec603
Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents:
173
diff
changeset
|
139 // 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
|
140 // message size (in bits, big endian), and adding enough zero bits in |
8819067ec603
Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents:
173
diff
changeset
|
141 // between to pad to the end of the next 64-byte frame. Since our input |
8819067ec603
Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents:
173
diff
changeset
|
142 // up to now has been in whole bytes, we can deal with 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
|
143 |
174
8819067ec603
Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents:
173
diff
changeset
|
144 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
|
145 do { |
8819067ec603
Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents:
173
diff
changeset
|
146 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
|
147 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
|
148 } 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
|
149 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
|
150 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
|
151 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
|
152 |
172
19d16003190b
Whitespace change: 4 spaces become one tab.
Rob Landley <rob@landley.net>
parents:
171
diff
changeset
|
153 for (i = 0; i < 20; i++) { |
19d16003190b
Whitespace change: 4 spaces become one tab.
Rob Landley <rob@landley.net>
parents:
171
diff
changeset
|
154 digest[i] = (unsigned char) |
173
01d005e67225
More cleanup: consistent indents, uint32_t, rename functions and structs,
Rob Landley <rob@landley.net>
parents:
172
diff
changeset
|
155 ((this->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255); |
172
19d16003190b
Whitespace change: 4 spaces become one tab.
Rob Landley <rob@landley.net>
parents:
171
diff
changeset
|
156 } |
19d16003190b
Whitespace change: 4 spaces become one tab.
Rob Landley <rob@landley.net>
parents:
171
diff
changeset
|
157 /* Wipe variables */ |
175
318fc45fe7bf
Re-roll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset
|
158 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
|
159 } |
58a97722d745
Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
160 |
58a97722d745
Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
161 |
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 |
58a97722d745
Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
164 |
58a97722d745
Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
165 int main(int argc, char** argv) |
58a97722d745
Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
166 { |
173
01d005e67225
More cleanup: consistent indents, uint32_t, rename functions and structs,
Rob Landley <rob@landley.net>
parents:
172
diff
changeset
|
167 int i, j; |
01d005e67225
More cleanup: consistent indents, uint32_t, rename functions and structs,
Rob Landley <rob@landley.net>
parents:
172
diff
changeset
|
168 struct sha1 this; |
01d005e67225
More cleanup: consistent indents, uint32_t, rename functions and structs,
Rob Landley <rob@landley.net>
parents:
172
diff
changeset
|
169 unsigned char digest[20], buffer[16384]; |
01d005e67225
More cleanup: consistent indents, uint32_t, rename functions and structs,
Rob Landley <rob@landley.net>
parents:
172
diff
changeset
|
170 FILE* file; |
170
58a97722d745
Start with public domain SHA-1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset
|
171 |
172
19d16003190b
Whitespace change: 4 spaces become one tab.
Rob Landley <rob@landley.net>
parents:
171
diff
changeset
|
172 if (argc < 2) { |
19d16003190b
Whitespace change: 4 spaces become one tab.
Rob Landley <rob@landley.net>
parents:
171
diff
changeset
|
173 file = stdin; |
19d16003190b
Whitespace change: 4 spaces become one tab.
Rob Landley <rob@landley.net>
parents:
171
diff
changeset
|
174 } |
19d16003190b
Whitespace change: 4 spaces become one tab.
Rob Landley <rob@landley.net>
parents:
171
diff
changeset
|
175 else { |
19d16003190b
Whitespace change: 4 spaces become one tab.
Rob Landley <rob@landley.net>
parents:
171
diff
changeset
|
176 if (!(file = fopen(argv[1], "rb"))) { |
19d16003190b
Whitespace change: 4 spaces become one tab.
Rob Landley <rob@landley.net>
parents:
171
diff
changeset
|
177 fputs("Unable to open file.", stderr); |
19d16003190b
Whitespace change: 4 spaces become one tab.
Rob Landley <rob@landley.net>
parents:
171
diff
changeset
|
178 exit(-1); |
19d16003190b
Whitespace change: 4 spaces become one tab.
Rob Landley <rob@landley.net>
parents:
171
diff
changeset
|
179 } |
173
01d005e67225
More cleanup: consistent indents, uint32_t, rename functions and structs,
Rob Landley <rob@landley.net>
parents:
172
diff
changeset
|
180 } |
01d005e67225
More cleanup: consistent indents, uint32_t, rename functions and structs,
Rob Landley <rob@landley.net>
parents:
172
diff
changeset
|
181 sha1_init(&this); |
172
19d16003190b
Whitespace change: 4 spaces become one tab.
Rob Landley <rob@landley.net>
parents:
171
diff
changeset
|
182 while (!feof(file)) { /* note: what if ferror(file) */ |
19d16003190b
Whitespace change: 4 spaces become one tab.
Rob Landley <rob@landley.net>
parents:
171
diff
changeset
|
183 i = fread(buffer, 1, 16384, file); |
173
01d005e67225
More cleanup: consistent indents, uint32_t, rename functions and structs,
Rob Landley <rob@landley.net>
parents:
172
diff
changeset
|
184 sha1_update(&this, buffer, i); |
172
19d16003190b
Whitespace change: 4 spaces become one tab.
Rob Landley <rob@landley.net>
parents:
171
diff
changeset
|
185 } |
173
01d005e67225
More cleanup: consistent indents, uint32_t, rename functions and structs,
Rob Landley <rob@landley.net>
parents:
172
diff
changeset
|
186 sha1_final(&this, digest); |
172
19d16003190b
Whitespace change: 4 spaces become one tab.
Rob Landley <rob@landley.net>
parents:
171
diff
changeset
|
187 fclose(file); |
173
01d005e67225
More cleanup: consistent indents, uint32_t, rename functions and structs,
Rob Landley <rob@landley.net>
parents:
172
diff
changeset
|
188 printy(digest); |
172
19d16003190b
Whitespace change: 4 spaces become one tab.
Rob Landley <rob@landley.net>
parents:
171
diff
changeset
|
189 exit(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
|
190 } |