xref: /illumos-gate/usr/src/boot/libsa/crypto/digest.c (revision dd72704bd9e794056c558153663c739e2012d721)
1 
2 #include <sys/cdefs.h>
3 #include <stand.h>
4 #include <sys/sha1.h>
5 
6 #include <bootstrap.h>
7 
8 void
9 sha1(void *data, size_t size, uint8_t *result)
10 {
11 	SHA1_CTX sha1_ctx;
12 
13 	SHA1Init(&sha1_ctx);
14 	SHA1Update(&sha1_ctx, data, size);
15 	SHA1Final(result, &sha1_ctx);
16 }
17 
18 static int
19 command_sha1(int argc, char **argv)
20 {
21 	void *ptr;
22 	size_t size, i;
23 	uint8_t resultbuf[SHA1_DIGEST_LENGTH];
24 
25 	/*
26 	 * usage: address size
27 	 */
28 	if (argc != 3) {
29 		command_errmsg = "usage: address size";
30 		return (CMD_ERROR);
31 	}
32 
33 	ptr = (void *)(uintptr_t)strtol(argv[1], NULL, 0);
34 	size = strtol(argv[2], NULL, 0);
35 	sha1(ptr, size, resultbuf);
36 
37 	for (i = 0; i < SHA1_DIGEST_LENGTH; i++)
38 		printf("%02x", resultbuf[i]);
39 	printf("\n");
40 	return (CMD_OK);
41 }
42 
43 COMMAND_SET(sha1, "sha1", "print the sha1 checksum", command_sha1);
44