2 * Based on shasum from http://www.netsw.org/crypto/hash/
4 * shasum fixed with reference to coreutils and the nist fip180-1 document
5 * which is incorrect, in section 5
6 * - ft(B,C,D) = (B AND C) OR ((NOT B) AND D) ( 0 <= t <= 19)
7 * + ft(B,C,D) = (D XOR (B AND (C XOR S))) ( 0 <= t <= 19)
9 * Copyright (C) 1999 Scott G. Miller
10 * Copyright (C) 2003 Glenn L. McGrath
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
35 #if __BYTE_ORDER == __BIG_ENDIAN
39 (((n) << 24) | (((n) & 0xff00) << 8) | (((n) >> 8) & 0xff00) | ((n) >> 24))
42 #define f1(X,Y,Z) (Z ^ (X & (Y ^ Z)))
43 #define f2(X,Y,Z) (X ^ Y ^ Z)
44 #define f3(X,Y,Z) ((X & Y) | (Z & (X | Y)))
46 #define rol1(x) (x<<1) | ((x>>31) & 1)
47 #define rol5(x) ((x<<5) | ((x>>27) & 0x1f))
48 #define rol30(x) (x<<30) | ((x>>2) & 0x3fffffff)
50 static void sha_hash(unsigned int *data, int *hash)
52 RESERVE_CONFIG_BUFFER(word, 80 * sizeof(unsigned int));
53 int *W = (unsigned int *) &word;
62 for (t = 0; t < 16; t++) {
66 /** Data expansion from 16 to 80 blocks **/
67 for (t = 16; t < 80; t++) {
68 int x = W[t - 3] ^ W[t - 8] ^ W[t - 14] ^ W[t - 16];
73 for (t = 0; t < 20; t++) {
74 TEMP = rol5(a) + f1(b, c, d) + e + W[t] + 0x5a827999;
82 TEMP = rol5(a) + f2(b, c, d) + e + W[t] + 0x6ed9eba1;
90 TEMP = rol5(a) + f3(b, c, d) + e + W[t] + 0x8f1bbcdc;
98 TEMP = rol5(a) + f2(b, c, d) + e + W[t] + 0xca62c1d6;
106 RELEASE_CONFIG_BUFFER(word);
115 static void sha1sum_stream(FILE * fd, unsigned int *hashval)
117 RESERVE_CONFIG_BUFFER(buffer, 64);
120 hashval[0] = 0x67452301;
121 hashval[1] = 0xefcdab89;
122 hashval[2] = 0x98badcfe;
123 hashval[3] = 0x10325476;
124 hashval[4] = 0xc3d2e1f0;
126 while (!feof(fd) && !ferror(fd)) {
127 int c = fread(&buffer, 1, 64, fd);
129 if (feof(fd) || ferror(fd)) {
131 /* If reading from stdin we need to get rid of a tailing character */
136 for (i = c; i < 61; i++) {
141 /* This ends up being swaped twice */
142 ((unsigned int *) &buffer)[15] = SWAP(length * 8);
148 sha_hash((unsigned int *) &buffer, hashval);
151 RELEASE_CONFIG_BUFFER(buffer);
156 static void print_hash(unsigned short hash_length, unsigned int *hash_val, char *filename)
160 for (x = 0; x < hash_length; x++) {
161 printf("%08x", hash_val[x]);
163 if (filename != NULL) {
171 /* This should become a common function used by sha1sum and md5sum,
172 * it needs extra functionality first
174 extern int authenticate(const int argc, char **argv, void (*hash_ptr)(FILE *stream, unsigned int *hashval), const unsigned short hash_length)
177 unsigned int *hashval;
179 while ((opt = getopt(argc, argv, "sc:w")) != -1) {
182 case 's': /* Dont output anything, status code shows success */
184 case 'c': /* Check a list of checksums against stored values */
186 case 'w': /* Warn of bad formatting when checking files */
194 hashval = xmalloc(hash_length * sizeof(unsigned int));
196 if (argc == optind) {
197 hash_ptr(stdin, hashval);
198 print_hash(hash_length, hashval, NULL);
202 for (i = optind; i < argc; i++) {
203 if (!strcmp(argv[i], "-")) {
204 hash_ptr(stdin, hashval);
205 print_hash(hash_length, hashval, NULL);
207 FILE *stream = bb_xfopen(argv[i], "r");
208 hash_ptr(stream, hashval);
210 print_hash(hash_length, hashval, argv[i]);
220 extern int sha1sum_main(int argc, char **argv)
222 /* sha1 length is 5 nibbles */
223 return (authenticate(argc, argv, sha1sum_stream, 5));