Mercurial > hg > toybox
annotate toys/sha1sum.c @ 233:d4176f3f3835
Zap toys/Config.in and instead create generated/Config.in from contents of
toys/*.c. Move relevant info into comment at the top of each toys/*.c. Also
convert more of Makefile into a thin wrapper around shell scripts that actually
do the work. (Makefile is only still there for the user interface.)
author  Rob Landley <rob@landley.net> 

date  Sat, 19 Jan 2008 17:08:39 0600 
parents  8c0809cee2b0 
children  163498bf547b 
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 SHA1 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  9 * 
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 
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 SHA1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset

20 
187
c983a0af6d4e
Add sha1sum. (No c mode yet.) Slight tweaks to loopfiles() too.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset

21 #include <toys.h> 
170
58a97722d745
Start with public domain SHA1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset

22 
173
01d005e67225
More cleanup: consistent indents, uint32_t, rename functions and structs,
Rob Landley <rob@landley.net>
parents:
172
diff
changeset

23 struct sha1 { 
01d005e67225
More cleanup: consistent indents, uint32_t, rename functions and structs,
Rob Landley <rob@landley.net>
parents:
172
diff
changeset

24 uint32_t state[5]; 
175
318fc45fe7bf
Reroll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset

25 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

26 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

27 union { 
8819067ec603
Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents:
173
diff
changeset

28 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

29 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

30 } buffer; 
173
01d005e67225
More cleanup: consistent indents, uint32_t, rename functions and structs,
Rob Landley <rob@landley.net>
parents:
172
diff
changeset

31 }; 
170
58a97722d745
Start with public domain SHA1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset

32 
192  33 static void sha1_init(struct sha1 *this); 
34 static void sha1_transform(struct sha1 *this);  
35 static void sha1_update(struct sha1 *this, char *data, unsigned int len);  
36 static void sha1_final(struct sha1 *this, char digest[20]);  
170
58a97722d745
Start with public domain SHA1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset

37 
58a97722d745
Start with public domain SHA1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset

38 #define rol(value, bits) (((value) << (bits))  ((value) >> (32  (bits)))) 
58a97722d745
Start with public domain SHA1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset

39 
192  40 // blk0() and blk() perform the initial expand. 
41 // 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

42 #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

43 #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

44 (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

45 #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

46 #define blk0(i) block[i] 
170
58a97722d745
Start with public domain SHA1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset

47 #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

48 #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

49 ^block[(i+2)&15]^block[i&15],1)) 
170
58a97722d745
Start with public domain SHA1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset

50 
175
318fc45fe7bf
Reroll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset

51 static const uint32_t rconsts[]={0x5A827999,0x6ED9EBA1,0x8F1BBCDC,0xCA62C1D6}; 
170
58a97722d745
Start with public domain SHA1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset

52 
192  53 // Hash a single 512bit block. This is the core of the algorithm. 
170
58a97722d745
Start with public domain SHA1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset

54 
192  55 static void sha1_transform(struct sha1 *this) 
170
58a97722d745
Start with public domain SHA1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset

56 { 
175
318fc45fe7bf
Reroll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset

57 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

58 uint32_t *block = this>buffer.i; 
175
318fc45fe7bf
Reroll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset

59 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

60 
192  61 // Copy context>state[] to working vars 
175
318fc45fe7bf
Reroll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset

62 for (i=0; i<5; i++) { 
318fc45fe7bf
Reroll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset

63 this>oldstate[i] = this>state[i]; 
318fc45fe7bf
Reroll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset

64 rot[i] = this>state + i; 
318fc45fe7bf
Reroll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset

65 } 
192  66 // 4 rounds of 20 operations each. 
175
318fc45fe7bf
Reroll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset

67 for (i=count=0; i<4; i++) { 
318fc45fe7bf
Reroll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset

68 for (j=0; j<20; j++) { 
318fc45fe7bf
Reroll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset

69 uint32_t work; 
318fc45fe7bf
Reroll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset

70 
318fc45fe7bf
Reroll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset

71 work = *rot[2] ^ *rot[3]; 
318fc45fe7bf
Reroll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset

72 if (!i) work = (work & *rot[1]) ^ *rot[3]; 
318fc45fe7bf
Reroll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset

73 else { 
318fc45fe7bf
Reroll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset

74 if (i==2) 
318fc45fe7bf
Reroll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset

75 work = ((*rot[1]*rot[2])&*rot[3])(*rot[1]&*rot[2]); 
318fc45fe7bf
Reroll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset

76 else work ^= *rot[1]; 
318fc45fe7bf
Reroll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset

77 } 
318fc45fe7bf
Reroll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset

78 if (!i && j<16) work += blk0(count); 
318fc45fe7bf
Reroll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset

79 else work += blk(count); 
318fc45fe7bf
Reroll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset

80 *rot[4] += work + rol(*rot[0],5) + rconsts[i]; 
318fc45fe7bf
Reroll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset

81 *rot[1] = rol(*rot[1],30); 
318fc45fe7bf
Reroll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset

82 
318fc45fe7bf
Reroll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset

83 // Rotate by one for next time. 
318fc45fe7bf
Reroll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset

84 temp = rot[4]; 
318fc45fe7bf
Reroll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset

85 for (k=4; k; k) rot[k] = rot[k1]; 
318fc45fe7bf
Reroll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset

86 *rot = temp; 
318fc45fe7bf
Reroll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset

87 count++; 
318fc45fe7bf
Reroll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset

88 } 
318fc45fe7bf
Reroll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset

89 } 
192  90 // Add the previous values of state[] 
175
318fc45fe7bf
Reroll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset

91 for (i=0; i<5; i++) this>state[i] += this>oldstate[i]; 
170
58a97722d745
Start with public domain SHA1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset

92 } 
58a97722d745
Start with public domain SHA1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset

93 
58a97722d745
Start with public domain SHA1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset

94 
192  95 // Initialize a struct sha1. 
170
58a97722d745
Start with public domain SHA1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset

96 
192  97 static void sha1_init(struct sha1 *this) 
170
58a97722d745
Start with public domain SHA1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset

98 { 
172
19d16003190b
Whitespace change: 4 spaces become one tab.
Rob Landley <rob@landley.net>
parents:
171
diff
changeset

99 /* SHA1 initialization constants */ 
173
01d005e67225
More cleanup: consistent indents, uint32_t, rename functions and structs,
Rob Landley <rob@landley.net>
parents:
172
diff
changeset

100 this>state[0] = 0x67452301; 
01d005e67225
More cleanup: consistent indents, uint32_t, rename functions and structs,
Rob Landley <rob@landley.net>
parents:
172
diff
changeset

101 this>state[1] = 0xEFCDAB89; 
01d005e67225
More cleanup: consistent indents, uint32_t, rename functions and structs,
Rob Landley <rob@landley.net>
parents:
172
diff
changeset

102 this>state[2] = 0x98BADCFE; 
01d005e67225
More cleanup: consistent indents, uint32_t, rename functions and structs,
Rob Landley <rob@landley.net>
parents:
172
diff
changeset

103 this>state[3] = 0x10325476; 
01d005e67225
More cleanup: consistent indents, uint32_t, rename functions and structs,
Rob Landley <rob@landley.net>
parents:
172
diff
changeset

104 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

105 this>count = 0; 
170
58a97722d745
Start with public domain SHA1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset

106 } 
58a97722d745
Start with public domain SHA1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset

107 
192  108 // Fill the 64byte working buffer and call sha1_transform() when full. 
170
58a97722d745
Start with public domain SHA1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset

109 
187
c983a0af6d4e
Add sha1sum. (No c mode yet.) Slight tweaks to loopfiles() too.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset

110 void sha1_update(struct sha1 *this, char *data, unsigned int len) 
170
58a97722d745
Start with public domain SHA1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset

111 { 
173
01d005e67225
More cleanup: consistent indents, uint32_t, rename functions and structs,
Rob Landley <rob@landley.net>
parents:
172
diff
changeset

112 unsigned int i, j; 
170
58a97722d745
Start with public domain SHA1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset

113 
174
8819067ec603
Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents:
173
diff
changeset

114 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

115 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

116 
8819067ec603
Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents:
173
diff
changeset

117 // Enough data to process a frame? 
172
19d16003190b
Whitespace change: 4 spaces become one tab.
Rob Landley <rob@landley.net>
parents:
171
diff
changeset

118 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

119 i = 64j; 
8819067ec603
Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents:
173
diff
changeset

120 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

121 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

122 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

123 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

124 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

125 } 
172
19d16003190b
Whitespace change: 4 spaces become one tab.
Rob Landley <rob@landley.net>
parents:
171
diff
changeset

126 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

127 } 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

128 // 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

129 memcpy(this>buffer.c + j, data + i, len  i); 
170
58a97722d745
Start with public domain SHA1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset

130 } 
58a97722d745
Start with public domain SHA1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset

131 
192  132 // Add padding and return the message digest. 
170
58a97722d745
Start with public domain SHA1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset

133 
187
c983a0af6d4e
Add sha1sum. (No c mode yet.) Slight tweaks to loopfiles() too.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset

134 void sha1_final(struct sha1 *this, char digest[20]) 
170
58a97722d745
Start with public domain SHA1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset

135 { 
174
8819067ec603
Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents:
173
diff
changeset

136 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

137 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

138 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

139 
8819067ec603
Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents:
173
diff
changeset

140 // 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

141 // message size (in bits, big endian), and adding enough zero bits in 
192  142 // between to pad to the end of the next 64byte frame. 
143 //  
144 // Since our input up to now has been in whole bytes, we can deal with  
145 // bytes here too.  
170
58a97722d745
Start with public domain SHA1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset

146 
174
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 = 0x80; 
8819067ec603
Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents:
173
diff
changeset

148 do { 
8819067ec603
Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents:
173
diff
changeset

149 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

150 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

151 } 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

152 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

153 this>buffer.c[56+i] = count >> (8*(7i)); 
8819067ec603
Untangle algorithm: use uint64_t for count, move union into struct, use struct
Rob Landley <rob@landley.net>
parents:
173
diff
changeset

154 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

155 
187
c983a0af6d4e
Add sha1sum. (No c mode yet.) Slight tweaks to loopfiles() too.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset

156 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

157 digest[i] = this>state[i>>2] >> ((3(i & 3)) * 8); 
192  158 // Wipe variables. Cryptogropher paranoia. 
175
318fc45fe7bf
Reroll loop, making code smaller. Also localize all the crypto information
Rob Landley <rob@landley.net>
parents:
174
diff
changeset

159 memset(this, 0, sizeof(struct sha1)); 
170
58a97722d745
Start with public domain SHA1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset

160 } 
58a97722d745
Start with public domain SHA1 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 // Callback for loopfiles() 
170
58a97722d745
Start with public domain SHA1 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 static void do_sha1(int fd, char *name) 
170
58a97722d745
Start with public domain SHA1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset

165 { 
173
01d005e67225
More cleanup: consistent indents, uint32_t, rename functions and structs,
Rob Landley <rob@landley.net>
parents:
172
diff
changeset

166 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

167 int len; 
170
58a97722d745
Start with public domain SHA1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset

168 
187
c983a0af6d4e
Add sha1sum. (No c mode yet.) Slight tweaks to loopfiles() too.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset

169 sha1_init(&this); 
c983a0af6d4e
Add sha1sum. (No c mode yet.) Slight tweaks to loopfiles() too.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset

170 for (;;) { 
c983a0af6d4e
Add sha1sum. (No c mode yet.) Slight tweaks to loopfiles() too.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset

171 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

172 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

173 sha1_update(&this, toybuf, len); 
172
19d16003190b
Whitespace change: 4 spaces become one tab.
Rob Landley <rob@landley.net>
parents:
171
diff
changeset

174 } 
187
c983a0af6d4e
Add sha1sum. (No c mode yet.) Slight tweaks to loopfiles() too.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset

175 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

176 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

177 printf(" %s\n", name); 
170
58a97722d745
Start with public domain SHA1 in C by Steve Reid <steve@edmweb.com>
Rob Landley <rob@landley.net>
parents:
diff
changeset

178 } 
187
c983a0af6d4e
Add sha1sum. (No c mode yet.) Slight tweaks to loopfiles() too.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset

179 
c983a0af6d4e
Add sha1sum. (No c mode yet.) Slight tweaks to loopfiles() too.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset

180 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

181 { 
c983a0af6d4e
Add sha1sum. (No c mode yet.) Slight tweaks to loopfiles() too.
Rob Landley <rob@landley.net>
parents:
186
diff
changeset

182 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

183 } 