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