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