1
/* $NetBSD: sha2hl.c,v 1.1 2001/03/12 09:08:40 agc Exp $ */
2
/* $RoughId: sha2hl.c,v 1.2 2001/07/13 19:49:10 knu Exp $ */
3
/* $Id: sha2hl.c,v 1.1 2001/07/13 20:06:14 knu Exp $ */
7
* This code includes some functions taken from sha2.c, hence the
8
* following licence reproduction.
10
* This code is not a verbatim copy, since some routines have been added,
11
* and some bugs have been fixed.
15
* Written by Aaron D. Gifford <me@aarongifford.com>
17
* Copyright 2000 Aaron D. Gifford. All rights reserved.
19
* Redistribution and use in source and binary forms, with or without
20
* modification, are permitted provided that the following conditions
22
* 1. Redistributions of source code must retain the above copyright
23
* notice, this list of conditions and the following disclaimer.
24
* 2. Redistributions in binary form must reproduce the above copyright
25
* notice, this list of conditions and the following disclaimer in the
26
* documentation and/or other materials provided with the distribution.
27
* 3. Neither the name of the copyright holder nor the names of contributors
28
* may be used to endorse or promote products derived from this software
29
* without specific prior written permission.
31
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) AND CONTRIBUTOR(S) ``AS IS'' AND
32
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
33
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) OR CONTRIBUTOR(S) BE LIABLE
35
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
39
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
40
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48
/* __RCSID("$NetBSD: sha2hl.c,v 1.1 2001/03/12 09:08:40 agc Exp $"); */
51
/* #include "namespace.h" */
59
#if defined(HAVE_UNISTD_H)
64
#define _DIAGASSERT(cond) assert(cond)
68
* Constant used by SHA256/384/512_End() functions for converting the
69
* digest to a readable hexadecimal character string:
71
static const char sha2_hex_digits[] = "0123456789abcdef";
74
SHA256_File(char *filename, char *buf)
76
uint8_t buffer[BUFSIZ * 20];
80
_DIAGASSERT(filename != NULL);
81
/* XXX: buf may be NULL ? */
85
if ((fd = open(filename, O_RDONLY)) < 0)
88
while ((num = read(fd, buffer, sizeof(buffer))) > 0)
89
SHA256_Update(&ctx, buffer, (size_t) num);
94
return (num < 0 ? 0 : SHA256_End(&ctx, buf));
99
SHA256_End(SHA256_CTX *ctx, char buffer[])
101
uint8_t digest[SHA256_DIGEST_LENGTH], *d = digest;
108
if ((ret = buffer) != NULL) {
109
SHA256_Final(digest, ctx);
111
for (i = 0; i < SHA256_DIGEST_LENGTH; i++) {
112
*buffer++ = sha2_hex_digits[(*d & 0xf0) >> 4];
113
*buffer++ = sha2_hex_digits[*d & 0x0f];
118
(void) memset(ctx, 0, sizeof(SHA256_CTX));
120
(void) memset(digest, 0, SHA256_DIGEST_LENGTH);
125
SHA256_Data(const uint8_t * data, size_t len, char *digest)
130
SHA256_Update(&ctx, data, len);
131
return SHA256_End(&ctx, digest);
135
SHA384_File(char *filename, char *buf)
138
uint8_t buffer[BUFSIZ * 20];
141
_DIAGASSERT(filename != NULL);
142
/* XXX: buf may be NULL ? */
146
if ((fd = open(filename, O_RDONLY)) < 0)
149
while ((num = read(fd, buffer, sizeof(buffer))) > 0)
150
SHA384_Update(&ctx, buffer, (size_t) num);
155
return (num < 0 ? 0 : SHA384_End(&ctx, buf));
159
SHA384_End(SHA384_CTX * ctx, char buffer[])
161
uint8_t digest[SHA384_DIGEST_LENGTH], *d = digest;
168
if ((ret = buffer) != NULL) {
169
SHA384_Final(digest, ctx);
171
for (i = 0; i < SHA384_DIGEST_LENGTH; i++) {
172
*buffer++ = sha2_hex_digits[(*d & 0xf0) >> 4];
173
*buffer++ = sha2_hex_digits[*d & 0x0f];
178
(void) memset(ctx, 0, sizeof(SHA384_CTX));
180
(void) memset(digest, 0, SHA384_DIGEST_LENGTH);
185
SHA384_Data(const uint8_t * data, size_t len, char *digest)
190
SHA384_Update(&ctx, data, len);
191
return SHA384_End(&ctx, digest);
195
SHA512_File(char *filename, char *buf)
198
uint8_t buffer[BUFSIZ * 20];
201
_DIAGASSERT(filename != NULL);
202
/* XXX: buf may be NULL ? */
206
if ((fd = open(filename, O_RDONLY)) < 0)
209
while ((num = read(fd, buffer, sizeof(buffer))) > 0)
210
SHA512_Update(&ctx, buffer, (size_t) num);
215
return (num < 0 ? 0 : SHA512_End(&ctx, buf));
219
SHA512_End(SHA512_CTX * ctx, char buffer[])
221
uint8_t digest[SHA512_DIGEST_LENGTH], *d = digest;
228
if ((ret = buffer) != NULL) {
229
SHA512_Final(digest, ctx);
231
for (i = 0; i < SHA512_DIGEST_LENGTH; i++) {
232
*buffer++ = sha2_hex_digits[(*d & 0xf0) >> 4];
233
*buffer++ = sha2_hex_digits[*d & 0x0f];
238
(void) memset(ctx, 0, sizeof(SHA512_CTX));
240
(void) memset(digest, 0, SHA512_DIGEST_LENGTH);
245
SHA512_Data(const uint8_t * data, size_t len, char *digest)
250
SHA512_Update(&ctx, data, len);
251
return SHA512_End(&ctx, digest);