xref: /netbsd-src/lib/libc/hash/sha2/sha256hl.c (revision 5b84b3983f71fd20a534cfa5d1556623a8aaa717)
1 /* $NetBSD: sha256hl.c,v 1.3 2005/08/26 15:58:17 elad Exp $ */
2 
3 /*
4  * ----------------------------------------------------------------------------
5  * "THE BEER-WARE LICENSE" (Revision 42):
6  * <phk@login.dkuug.dk> wrote this file.  As long as you retain this notice you
7  * can do whatever you want with this stuff. If we meet some day, and you think
8  * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
9  * ----------------------------------------------------------------------------
10  */
11 
12 #include "namespace.h"
13 
14 #include <sys/param.h>
15 #include <sys/stat.h>
16 
17 #include <errno.h>
18 #include <fcntl.h>
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <unistd.h>
23 
24 #include <crypto/sha2.h>
25 
26 /* ARGSUSED */
27 char *
28 SHA256_End(SHA256_CTX *ctx, char *buf)
29 {
30 	int i;
31 	u_int8_t digest[SHA256_DIGEST_LENGTH];
32 	static const char hex[] = "0123456789abcdef";
33 
34 	if (buf == NULL && (buf = malloc(SHA256_DIGEST_STRING_LENGTH)) == NULL)
35 		return (NULL);
36 
37 	SHA256_Final(digest, ctx);
38 	for (i = 0; i < SHA256_DIGEST_LENGTH; i++) {
39 		buf[i + i] = hex[(u_int32_t)digest[i] >> 4];
40 		buf[i + i + 1] = hex[digest[i] & 0x0f];
41 	}
42 	buf[i + i] = '\0';
43 	memset(digest, 0, sizeof(digest));
44 	return (buf);
45 }
46 
47 char *
48 SHA256_FileChunk(const char *filename, char *buf, off_t off, off_t len)
49 {
50 	struct stat sb;
51 	u_char buffer[BUFSIZ];
52 	SHA256_CTX ctx;
53 	int fd, save_errno;
54 	ssize_t nr;
55 
56 	SHA256_Init(&ctx);
57 
58 	if ((fd = open(filename, O_RDONLY)) < 0)
59 		return (NULL);
60 	if (len == 0) {
61 		if (fstat(fd, &sb) == -1) {
62 			close(fd);
63 			return (NULL);
64 		}
65 		len = sb.st_size;
66 	}
67 	if (off > 0 && lseek(fd, off, SEEK_SET) < 0)
68 		return (NULL);
69 
70 	while ((nr = read(fd, buffer, (size_t) MIN(sizeof(buffer), len)))
71 	    > 0) {
72 		SHA256_Update(&ctx, buffer, (size_t)nr);
73 		if (len > 0 && (len -= nr) == 0)
74 			break;
75 	}
76 
77 	save_errno = errno;
78 	close(fd);
79 	errno = save_errno;
80 	return (nr < 0 ? NULL : SHA256_End(&ctx, buf));
81 }
82 
83 char *
84 SHA256_File(const char *filename, char *buf)
85 {
86 	return (SHA256_FileChunk(filename, buf, (off_t)0, (off_t)0));
87 }
88 
89 char *
90 SHA256_Data(const u_char *data, size_t len, char *buf)
91 {
92 	SHA256_CTX ctx;
93 
94 	SHA256_Init(&ctx);
95 	SHA256_Update(&ctx, data, len);
96 	return (SHA256_End(&ctx, buf));
97 }
98