xref: /openbsd-src/usr.bin/dig/lib/isc/sha1.c (revision b9558d14c675017cf470d7469a47201d05e39444)
15185a700Sflorian /*
25185a700Sflorian  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
35185a700Sflorian  *
45185a700Sflorian  * Permission to use, copy, modify, and/or distribute this software for any
55185a700Sflorian  * purpose with or without fee is hereby granted, provided that the above
65185a700Sflorian  * copyright notice and this permission notice appear in all copies.
75185a700Sflorian  *
85185a700Sflorian  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
95185a700Sflorian  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
105185a700Sflorian  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
115185a700Sflorian  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
125185a700Sflorian  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
135185a700Sflorian  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
145185a700Sflorian  * PERFORMANCE OF THIS SOFTWARE.
155185a700Sflorian  */
165185a700Sflorian 
17*b9558d14Sjsg /* $Id: sha1.c,v 1.5 2020/02/25 05:00:43 jsg Exp $ */
185185a700Sflorian 
195185a700Sflorian /*	$NetBSD: sha1.c,v 1.5 2000/01/22 22:19:14 mycroft Exp $	*/
20*b9558d14Sjsg /*	$OpenBSD: sha1.c,v 1.5 2020/02/25 05:00:43 jsg Exp $	*/
215185a700Sflorian 
225185a700Sflorian /*! \file
235185a700Sflorian  * SHA-1 in C
245185a700Sflorian  * \author By Steve Reid <steve@edmweb.com>
255185a700Sflorian  * 100% Public Domain
265185a700Sflorian  * \verbatim
275185a700Sflorian  * Test Vectors (from FIPS PUB 180-1)
285185a700Sflorian  * "abc"
295185a700Sflorian  *   A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D
305185a700Sflorian  * "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
315185a700Sflorian  *   84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1
325185a700Sflorian  * A million repetitions of "a"
335185a700Sflorian  *   34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
345185a700Sflorian  * \endverbatim
355185a700Sflorian  */
365185a700Sflorian 
375185a700Sflorian #include <isc/sha1.h>
385185a700Sflorian #include <string.h>
395185a700Sflorian #include <isc/util.h>
405185a700Sflorian 
415185a700Sflorian void
isc_sha1_init(isc_sha1_t * context)425185a700Sflorian isc_sha1_init(isc_sha1_t *context)
435185a700Sflorian {
445185a700Sflorian 	INSIST(context != NULL);
455185a700Sflorian 
465185a700Sflorian 	context->ctx = EVP_MD_CTX_new();
475185a700Sflorian 	RUNTIME_CHECK(context->ctx != NULL);
485185a700Sflorian 	if (EVP_DigestInit(context->ctx, EVP_sha1()) != 1) {
495185a700Sflorian 		FATAL_ERROR(__FILE__, __LINE__, "Cannot initialize SHA1.");
505185a700Sflorian 	}
515185a700Sflorian }
525185a700Sflorian 
535185a700Sflorian void
isc_sha1_update(isc_sha1_t * context,const unsigned char * data,unsigned int len)545185a700Sflorian isc_sha1_update(isc_sha1_t *context, const unsigned char *data,
555185a700Sflorian 		unsigned int len)
565185a700Sflorian {
575185a700Sflorian 	INSIST(context != 0);
585185a700Sflorian 	INSIST(context->ctx != 0);
595185a700Sflorian 	INSIST(data != 0);
605185a700Sflorian 
615185a700Sflorian 	RUNTIME_CHECK(EVP_DigestUpdate(context->ctx,
625185a700Sflorian 				       (const void *) data,
635185a700Sflorian 				       (size_t) len) == 1);
645185a700Sflorian }
655185a700Sflorian 
665185a700Sflorian void
isc_sha1_final(isc_sha1_t * context,unsigned char * digest)675185a700Sflorian isc_sha1_final(isc_sha1_t *context, unsigned char *digest) {
685185a700Sflorian 	INSIST(digest != 0);
695185a700Sflorian 	INSIST(context != 0);
705185a700Sflorian 	INSIST(context->ctx != 0);
715185a700Sflorian 
725185a700Sflorian 	RUNTIME_CHECK(EVP_DigestFinal(context->ctx, digest, NULL) == 1);
735185a700Sflorian 	EVP_MD_CTX_free(context->ctx);
745185a700Sflorian 	context->ctx = NULL;
755185a700Sflorian }
76