1 /* vi: set sw=4 ts=4: */
2 /*
3  * Copyright (C) 2003 Glenn L. McGrath
4  * Copyright (C) 2003-2004 Erik Andersen
5  *
6  * Licensed under GPLv2 or later, see file LICENSE in this source tree.
7  */
8 //config:config MD5SUM
9 //config:	bool "md5sum (6.5 kb)"
10 //config:	default y
11 //config:	help
12 //config:	Compute and check MD5 message digest
13 //config:
14 //config:config SHA1SUM
15 //config:	bool "sha1sum (5.9 kb)"
16 //config:	default y
17 //config:	help
18 //config:	Compute and check SHA1 message digest
19 //config:
20 //config:config SHA256SUM
21 //config:	bool "sha256sum (7 kb)"
22 //config:	default y
23 //config:	help
24 //config:	Compute and check SHA256 message digest
25 //config:
26 //config:config SHA512SUM
27 //config:	bool "sha512sum (7.4 kb)"
28 //config:	default y
29 //config:	help
30 //config:	Compute and check SHA512 message digest
31 //config:
32 //config:config SHA3SUM
33 //config:	bool "sha3sum (6.1 kb)"
34 //config:	default y
35 //config:	help
36 //config:	Compute and check SHA3 message digest
37 //config:
38 //config:comment "Common options for md5sum, sha1sum, sha256sum, sha512sum, sha3sum"
39 //config:	depends on MD5SUM || SHA1SUM || SHA256SUM || SHA512SUM || SHA3SUM
40 //config:
41 //config:config FEATURE_MD5_SHA1_SUM_CHECK
42 //config:	bool "Enable -c, -s and -w options"
43 //config:	default y
44 //config:	depends on MD5SUM || SHA1SUM || SHA256SUM || SHA512SUM || SHA3SUM
45 //config:	help
46 //config:	Enabling the -c options allows files to be checked
47 //config:	against pre-calculated hash values.
48 //config:	-s and -w are useful options when verifying checksums.
49 
50 //applet:IF_MD5SUM(APPLET_NOEXEC(md5sum, md5_sha1_sum, BB_DIR_USR_BIN, BB_SUID_DROP, md5sum))
51 //applet:IF_SHA1SUM(APPLET_NOEXEC(sha1sum, md5_sha1_sum, BB_DIR_USR_BIN, BB_SUID_DROP, sha1sum))
52 //applet:IF_SHA3SUM(APPLET_NOEXEC(sha3sum, md5_sha1_sum, BB_DIR_USR_BIN, BB_SUID_DROP, sha3sum))
53 //applet:IF_SHA256SUM(APPLET_NOEXEC(sha256sum, md5_sha1_sum, BB_DIR_USR_BIN, BB_SUID_DROP, sha256sum))
54 //applet:IF_SHA512SUM(APPLET_NOEXEC(sha512sum, md5_sha1_sum, BB_DIR_USR_BIN, BB_SUID_DROP, sha512sum))
55 
56 //kbuild:lib-$(CONFIG_MD5SUM)    += md5_sha1_sum.o
57 //kbuild:lib-$(CONFIG_SHA1SUM)   += md5_sha1_sum.o
58 //kbuild:lib-$(CONFIG_SHA256SUM) += md5_sha1_sum.o
59 //kbuild:lib-$(CONFIG_SHA512SUM) += md5_sha1_sum.o
60 //kbuild:lib-$(CONFIG_SHA3SUM)   += md5_sha1_sum.o
61 
62 //usage:#define md5sum_trivial_usage
63 //usage:	IF_FEATURE_MD5_SHA1_SUM_CHECK("[-c[sw]] ")"[FILE]..."
64 //usage:#define md5sum_full_usage "\n\n"
65 //usage:       "Print" IF_FEATURE_MD5_SHA1_SUM_CHECK(" or check") " MD5 checksums"
66 //usage:	IF_FEATURE_MD5_SHA1_SUM_CHECK( "\n"
67 //usage:     "\n	-c	Check sums against list in FILEs"
68 //usage:     "\n	-s	Don't output anything, status code shows success"
69 //usage:     "\n	-w	Warn about improperly formatted checksum lines"
70 //usage:	)
71 //usage:
72 //usage:#define md5sum_example_usage
73 //usage:       "$ md5sum < busybox\n"
74 //usage:       "6fd11e98b98a58f64ff3398d7b324003\n"
75 //usage:       "$ md5sum busybox\n"
76 //usage:       "6fd11e98b98a58f64ff3398d7b324003  busybox\n"
77 //usage:       "$ md5sum -c -\n"
78 //usage:       "6fd11e98b98a58f64ff3398d7b324003  busybox\n"
79 //usage:       "busybox: OK\n"
80 //usage:       "^D\n"
81 //usage:
82 //usage:#define sha1sum_trivial_usage
83 //usage:	IF_FEATURE_MD5_SHA1_SUM_CHECK("[-c[sw]] ")"[FILE]..."
84 //usage:#define sha1sum_full_usage "\n\n"
85 //usage:       "Print" IF_FEATURE_MD5_SHA1_SUM_CHECK(" or check") " SHA1 checksums"
86 //usage:	IF_FEATURE_MD5_SHA1_SUM_CHECK( "\n"
87 //usage:     "\n	-c	Check sums against list in FILEs"
88 //usage:     "\n	-s	Don't output anything, status code shows success"
89 //usage:     "\n	-w	Warn about improperly formatted checksum lines"
90 //usage:	)
91 //usage:
92 //usage:#define sha256sum_trivial_usage
93 //usage:	IF_FEATURE_MD5_SHA1_SUM_CHECK("[-c[sw]] ")"[FILE]..."
94 //usage:#define sha256sum_full_usage "\n\n"
95 //usage:       "Print" IF_FEATURE_MD5_SHA1_SUM_CHECK(" or check") " SHA256 checksums"
96 //usage:	IF_FEATURE_MD5_SHA1_SUM_CHECK( "\n"
97 //usage:     "\n	-c	Check sums against list in FILEs"
98 //usage:     "\n	-s	Don't output anything, status code shows success"
99 //usage:     "\n	-w	Warn about improperly formatted checksum lines"
100 //usage:	)
101 //usage:
102 //usage:#define sha512sum_trivial_usage
103 //usage:	IF_FEATURE_MD5_SHA1_SUM_CHECK("[-c[sw]] ")"[FILE]..."
104 //usage:#define sha512sum_full_usage "\n\n"
105 //usage:       "Print" IF_FEATURE_MD5_SHA1_SUM_CHECK(" or check") " SHA512 checksums"
106 //usage:	IF_FEATURE_MD5_SHA1_SUM_CHECK( "\n"
107 //usage:     "\n	-c	Check sums against list in FILEs"
108 //usage:     "\n	-s	Don't output anything, status code shows success"
109 //usage:     "\n	-w	Warn about improperly formatted checksum lines"
110 //usage:	)
111 //usage:
112 //usage:#define sha3sum_trivial_usage
113 //usage:	IF_FEATURE_MD5_SHA1_SUM_CHECK("[-c[sw]] ")"[-a BITS] [FILE]..."
114 //usage:#define sha3sum_full_usage "\n\n"
115 //usage:       "Print" IF_FEATURE_MD5_SHA1_SUM_CHECK(" or check") " SHA3 checksums"
116 //usage:	IF_FEATURE_MD5_SHA1_SUM_CHECK( "\n"
117 //usage:     "\n	-c	Check sums against list in FILEs"
118 //usage:     "\n	-s	Don't output anything, status code shows success"
119 //usage:     "\n	-w	Warn about improperly formatted checksum lines"
120 //usage:	)
121 //usage:     "\n	-a BITS	224 (default), 256, 384, 512"
122 
123 //FIXME: GNU coreutils 8.25 has no -s option, it has only these two long opts:
124 // --quiet   don't print OK for each successfully verified file
125 // --status  don't output anything, status code shows success
126 
127 #include "libbb.h"
128 
129 /* This is a NOEXEC applet. Be very careful! */
130 
131 enum {
132 	/* 4th letter of applet_name is... */
133 	HASH_MD5 = 's', /* "md5>s<um" */
134 	HASH_SHA1 = '1',
135 	HASH_SHA256 = '2',
136 	HASH_SHA3 = '3',
137 	HASH_SHA512 = '5',
138 };
139 
140 #define FLAG_SILENT  1
141 #define FLAG_CHECK   2
142 #define FLAG_WARN    4
143 
144 /* This might be useful elsewhere */
hash_bin_to_hex(unsigned char * hash_value,unsigned hash_length)145 static unsigned char *hash_bin_to_hex(unsigned char *hash_value,
146 				unsigned hash_length)
147 {
148 	/* xzalloc zero-terminates */
149 	char *hex_value = xzalloc((hash_length * 2) + 1);
150 	bin2hex(hex_value, (char*)hash_value, hash_length);
151 	return (unsigned char *)hex_value;
152 }
153 
154 #if !ENABLE_SHA3SUM
155 # define hash_file(f,w) hash_file(f)
156 #endif
hash_file(const char * filename,unsigned sha3_width)157 static uint8_t *hash_file(const char *filename, unsigned sha3_width)
158 {
159 	int src_fd, hash_len, count;
160 	union _ctx_ {
161 		sha3_ctx_t sha3;
162 		sha512_ctx_t sha512;
163 		sha256_ctx_t sha256;
164 		sha1_ctx_t sha1;
165 		md5_ctx_t md5;
166 	} context;
167 	uint8_t *hash_value;
168 	void FAST_FUNC (*update)(void*, const void*, size_t);
169 	unsigned FAST_FUNC (*final)(void*, void*);
170 	char hash_algo;
171 
172 	src_fd = open_or_warn_stdin(filename);
173 	if (src_fd < 0) {
174 		return NULL;
175 	}
176 
177 	hash_algo = applet_name[3];
178 
179 	/* figure specific hash algorithms */
180 	if (ENABLE_MD5SUM && hash_algo == HASH_MD5) {
181 		md5_begin(&context.md5);
182 		update = (void*)md5_hash;
183 		final = (void*)md5_end;
184 		hash_len = 16;
185 	}
186 	else if (ENABLE_SHA1SUM && hash_algo == HASH_SHA1) {
187 		sha1_begin(&context.sha1);
188 		update = (void*)sha1_hash;
189 		final = (void*)sha1_end;
190 		hash_len = 20;
191 	}
192 	else if (ENABLE_SHA256SUM && hash_algo == HASH_SHA256) {
193 		sha256_begin(&context.sha256);
194 		update = (void*)sha256_hash;
195 		final = (void*)sha256_end;
196 		hash_len = 32;
197 	}
198 	else if (ENABLE_SHA512SUM && hash_algo == HASH_SHA512) {
199 		sha512_begin(&context.sha512);
200 		update = (void*)sha512_hash;
201 		final = (void*)sha512_end;
202 		hash_len = 64;
203 	}
204 #if ENABLE_SHA3SUM
205 	else if (ENABLE_SHA3SUM && hash_algo == HASH_SHA3) {
206 		sha3_begin(&context.sha3);
207 		update = (void*)sha3_hash;
208 		final = (void*)sha3_end;
209 		/*
210 		 * Should support 224, 256, 384, 512.
211 		 * We allow any value which does not blow the algorithm up.
212 		 */
213 		if (sha3_width >= 1600/2 /* input block can't be <= 0 */
214 		 || sha3_width == 0      /* hash len can't be 0 */
215 		 || (sha3_width & 0x1f)  /* should be multiple of 32 */
216 		/* (because input uses up to 8 byte wide word XORs. 32/4=8) */
217 		) {
218 			bb_error_msg_and_die("bad -a%u", sha3_width);
219 		}
220 		sha3_width /= 4;
221 		context.sha3.input_block_bytes = 1600/8 - sha3_width;
222 		hash_len = sha3_width/2;
223 	}
224 #endif
225 	else {
226 		xfunc_die(); /* can't reach this */
227 	}
228 
229 	{
230 		RESERVE_CONFIG_UBUFFER(in_buf, 4096);
231 		while ((count = safe_read(src_fd, in_buf, 4096)) > 0) {
232 			update(&context, in_buf, count);
233 		}
234 		hash_value = NULL;
235 		if (count < 0)
236 			bb_perror_msg("can't read '%s'", filename);
237 		else /* count == 0 */ {
238 			final(&context, in_buf);
239 			hash_value = hash_bin_to_hex(in_buf, hash_len);
240 		}
241 		RELEASE_CONFIG_BUFFER(in_buf);
242 	}
243 
244 	if (src_fd != STDIN_FILENO) {
245 		close(src_fd);
246 	}
247 
248 	return hash_value;
249 }
250 
251 int md5_sha1_sum_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
md5_sha1_sum_main(int argc UNUSED_PARAM,char ** argv)252 int md5_sha1_sum_main(int argc UNUSED_PARAM, char **argv)
253 {
254 	int return_value = EXIT_SUCCESS;
255 	unsigned flags;
256 #if ENABLE_SHA3SUM
257 	unsigned sha3_width = 224;
258 #endif
259 
260 	if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK) {
261 		/* -b "binary", -t "text" are ignored (shaNNNsum compat) */
262 		/* -s and -w require -c */
263 #if ENABLE_SHA3SUM
264 		if (applet_name[3] == HASH_SHA3)
265 			flags = getopt32(argv, "^" "scwbta:+" "\0" "s?c:w?c", &sha3_width);
266 		else
267 #endif
268 			flags = getopt32(argv, "^" "scwbt" "\0" "s?c:w?c");
269 	} else {
270 #if ENABLE_SHA3SUM
271 		if (applet_name[3] == HASH_SHA3)
272 			getopt32(argv, "a:+", &sha3_width);
273 		else
274 #endif
275 			getopt32(argv, "");
276 	}
277 	argv += optind;
278 	//argc -= optind;
279 	if (!*argv)
280 		*--argv = (char*)"-";
281 
282 	do {
283 		if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK && (flags & FLAG_CHECK)) {
284 			FILE *pre_computed_stream;
285 			char *line;
286 			int count_total = 0;
287 			int count_failed = 0;
288 
289 			pre_computed_stream = xfopen_stdin(*argv);
290 
291 			while ((line = xmalloc_fgetline(pre_computed_stream)) != NULL) {
292 				uint8_t *hash_value;
293 				char *filename_ptr;
294 
295 				count_total++;
296 				filename_ptr = strstr(line, "  ");
297 				/* handle format for binary checksums */
298 				if (filename_ptr == NULL) {
299 					filename_ptr = strstr(line, " *");
300 				}
301 				if (filename_ptr == NULL) {
302 					if (flags & FLAG_WARN) {
303 						bb_simple_error_msg("invalid format");
304 					}
305 					count_failed++;
306 					return_value = EXIT_FAILURE;
307 					free(line);
308 					continue;
309 				}
310 				*filename_ptr = '\0';
311 				filename_ptr += 2;
312 
313 				hash_value = hash_file(filename_ptr, sha3_width);
314 
315 				if (hash_value && (strcmp((char*)hash_value, line) == 0)) {
316 					if (!(flags & FLAG_SILENT))
317 						printf("%s: OK\n", filename_ptr);
318 				} else {
319 					if (!(flags & FLAG_SILENT))
320 						printf("%s: FAILED\n", filename_ptr);
321 					count_failed++;
322 					return_value = EXIT_FAILURE;
323 				}
324 				/* possible free(NULL) */
325 				free(hash_value);
326 				free(line);
327 			}
328 			if (count_failed && !(flags & FLAG_SILENT)) {
329 				bb_error_msg("WARNING: %d of %d computed checksums did NOT match",
330 						count_failed, count_total);
331 			}
332 			if (count_total == 0) {
333 				return_value = EXIT_FAILURE;
334 				/*
335 				 * md5sum from GNU coreutils 8.25 says:
336 				 * md5sum: <FILE>: no properly formatted MD5 checksum lines found
337 				 */
338 				bb_error_msg("%s: no checksum lines found", *argv);
339 			}
340 			fclose_if_not_stdin(pre_computed_stream);
341 		} else {
342 			uint8_t *hash_value = hash_file(*argv, sha3_width);
343 			if (hash_value == NULL) {
344 				return_value = EXIT_FAILURE;
345 			} else {
346 				printf("%s  %s\n", hash_value, *argv);
347 				free(hash_value);
348 			}
349 		}
350 	} while (*++argv);
351 
352 	return return_value;
353 }
354