1/* 2 * Copyright (C) 2005-2007, 2009 Internet Systems Consortium, Inc. ("ISC") 3 * 4 * Permission to use, copy, modify, and/or distribute this software for any 5 * purpose with or without fee is hereby granted, provided that the above 6 * copyright notice and this permission notice appear in all copies. 7 * 8 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH 9 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 10 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, 11 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 12 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE 13 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 14 * PERFORMANCE OF THIS SOFTWARE. 15 */ 16 17/* $Id: sha2.h,v 1.12 2009/10/22 02:21:31 each Exp $ */ 18 19/* $FreeBSD: 258945 2013-12-04 21:33:17Z roberto $ */ 20/* $KAME: sha2.h,v 1.3 2001/03/12 08:27:48 itojun Exp $ */ 21 22/* 23 * sha2.h 24 * 25 * Version 1.0.0beta1 26 * 27 * Written by Aaron D. Gifford <me@aarongifford.com> 28 * 29 * Copyright 2000 Aaron D. Gifford. All rights reserved. 30 * 31 * Redistribution and use in source and binary forms, with or without 32 * modification, are permitted provided that the following conditions 33 * are met: 34 * 1. Redistributions of source code must retain the above copyright 35 * notice, this list of conditions and the following disclaimer. 36 * 2. Redistributions in binary form must reproduce the above copyright 37 * notice, this list of conditions and the following disclaimer in the 38 * documentation and/or other materials provided with the distribution. 39 * 3. Neither the name of the copyright holder nor the names of contributors 40 * may be used to endorse or promote products derived from this software 41 * without specific prior written permission. 42 * 43 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) AND CONTRIBUTOR(S) ``AS IS'' AND 44 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 45 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 46 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) OR CONTRIBUTOR(S) BE LIABLE 47 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 48 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 49 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 50 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 51 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 52 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 53 * SUCH DAMAGE. 54 * 55 */ 56 57#ifndef ISC_SHA2_H 58#define ISC_SHA2_H 59 60#include <isc/lang.h> 61#include <isc/platform.h> 62#include <isc/types.h> 63 64/*** SHA-224/256/384/512 Various Length Definitions ***********************/ 65 66#define ISC_SHA224_BLOCK_LENGTH 64U 67#define ISC_SHA224_DIGESTLENGTH 28U 68#define ISC_SHA224_DIGESTSTRINGLENGTH (ISC_SHA224_DIGESTLENGTH * 2 + 1) 69#define ISC_SHA256_BLOCK_LENGTH 64U 70#define ISC_SHA256_DIGESTLENGTH 32U 71#define ISC_SHA256_DIGESTSTRINGLENGTH (ISC_SHA256_DIGESTLENGTH * 2 + 1) 72#define ISC_SHA384_BLOCK_LENGTH 128 73#define ISC_SHA384_DIGESTLENGTH 48U 74#define ISC_SHA384_DIGESTSTRINGLENGTH (ISC_SHA384_DIGESTLENGTH * 2 + 1) 75#define ISC_SHA512_BLOCK_LENGTH 128U 76#define ISC_SHA512_DIGESTLENGTH 64U 77#define ISC_SHA512_DIGESTSTRINGLENGTH (ISC_SHA512_DIGESTLENGTH * 2 + 1) 78 79/*** SHA-256/384/512 Context Structures *******************************/ 80 81#ifdef ISC_PLATFORM_OPENSSLHASH 82#include <openssl/evp.h> 83 84typedef EVP_MD_CTX isc_sha256_t; 85typedef EVP_MD_CTX isc_sha512_t; 86 87#else 88 89/* 90 * Keep buffer immediately after bitcount to preserve alignment. 91 */ 92typedef struct { 93 isc_uint32_t state[8]; 94 isc_uint64_t bitcount; 95 isc_uint8_t buffer[ISC_SHA256_BLOCK_LENGTH]; 96} isc_sha256_t; 97 98/* 99 * Keep buffer immediately after bitcount to preserve alignment. 100 */ 101typedef struct { 102 isc_uint64_t state[8]; 103 isc_uint64_t bitcount[2]; 104 isc_uint8_t buffer[ISC_SHA512_BLOCK_LENGTH]; 105} isc_sha512_t; 106#endif 107 108typedef isc_sha256_t isc_sha224_t; 109typedef isc_sha512_t isc_sha384_t; 110 111ISC_LANG_BEGINDECLS 112 113/*** SHA-224/256/384/512 Function Prototypes ******************************/ 114 115void isc_sha224_init (isc_sha224_t *); 116void isc_sha224_invalidate (isc_sha224_t *); 117void isc_sha224_update (isc_sha224_t *, const isc_uint8_t *, size_t); 118void isc_sha224_final (isc_uint8_t[ISC_SHA224_DIGESTLENGTH], isc_sha224_t *); 119char *isc_sha224_end (isc_sha224_t *, char[ISC_SHA224_DIGESTSTRINGLENGTH]); 120char *isc_sha224_data (const isc_uint8_t *, size_t, char[ISC_SHA224_DIGESTSTRINGLENGTH]); 121 122void isc_sha256_init (isc_sha256_t *); 123void isc_sha256_invalidate (isc_sha256_t *); 124void isc_sha256_update (isc_sha256_t *, const isc_uint8_t *, size_t); 125void isc_sha256_final (isc_uint8_t[ISC_SHA256_DIGESTLENGTH], isc_sha256_t *); 126char *isc_sha256_end (isc_sha256_t *, char[ISC_SHA256_DIGESTSTRINGLENGTH]); 127char *isc_sha256_data (const isc_uint8_t *, size_t, char[ISC_SHA256_DIGESTSTRINGLENGTH]); 128 129void isc_sha384_init (isc_sha384_t *); 130void isc_sha384_invalidate (isc_sha384_t *); 131void isc_sha384_update (isc_sha384_t *, const isc_uint8_t *, size_t); 132void isc_sha384_final (isc_uint8_t[ISC_SHA384_DIGESTLENGTH], isc_sha384_t *); 133char *isc_sha384_end (isc_sha384_t *, char[ISC_SHA384_DIGESTSTRINGLENGTH]); 134char *isc_sha384_data (const isc_uint8_t *, size_t, char[ISC_SHA384_DIGESTSTRINGLENGTH]); 135 136void isc_sha512_init (isc_sha512_t *); 137void isc_sha512_invalidate (isc_sha512_t *); 138void isc_sha512_update (isc_sha512_t *, const isc_uint8_t *, size_t); 139void isc_sha512_final (isc_uint8_t[ISC_SHA512_DIGESTLENGTH], isc_sha512_t *); 140char *isc_sha512_end (isc_sha512_t *, char[ISC_SHA512_DIGESTSTRINGLENGTH]); 141char *isc_sha512_data (const isc_uint8_t *, size_t, char[ISC_SHA512_DIGESTSTRINGLENGTH]); 142 143ISC_LANG_ENDDECLS 144 145#endif /* ISC_SHA2_H */ 146