1 /* @(#)sha2.h 1.5 10/08/27 2009-2010 J. Schilling */ 2 /* 3 * SHA2 hash code taken from OpenBSD 4 * 5 * Portions Copyright (c) 2009-2010 J. Schilling 6 */ 7 8 /* $OpenBSD: sha2.h,v 1.7 2008/09/06 12:00:19 djm Exp $ */ 9 10 /* 11 * FILE: sha2.h 12 * AUTHOR: Aaron D. Gifford <me@aarongifford.com> 13 * 14 * Copyright (c) 2000-2001, Aaron D. Gifford 15 * All rights reserved. 16 * 17 * Redistribution and use in source and binary forms, with or without 18 * modification, are permitted provided that the following conditions 19 * are met: 20 * 1. Redistributions of source code must retain the above copyright 21 * notice, this list of conditions and the following disclaimer. 22 * 2. Redistributions in binary form must reproduce the above copyright 23 * notice, this list of conditions and the following disclaimer in the 24 * documentation and/or other materials provided with the distribution. 25 * 3. Neither the name of the copyright holder nor the names of contributors 26 * may be used to endorse or promote products derived from this software 27 * without specific prior written permission. 28 * 29 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTOR(S) ``AS IS'' AND 30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 32 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTOR(S) BE LIABLE 33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 39 * SUCH DAMAGE. 40 * 41 * $From: sha2.h,v 1.1 2001/11/08 00:02:01 adg Exp adg $ 42 */ 43 44 #ifndef _SCHILY_SHA2_H 45 #define _SCHILY_SHA2_H 46 47 #ifndef _SCHILY_MCONFIG_H 48 #include <schily/mconfig.h> 49 #endif 50 #include <schily/utypes.h> 51 52 53 /* ** SHA-256/384/512 Various Length Definitions ********************** */ 54 #define SHA256_BLOCK_LENGTH 64 55 #define SHA256_DIGEST_LENGTH 32 56 #define SHA256_DIGEST_STRING_LENGTH (SHA256_DIGEST_LENGTH * 2 + 1) 57 #ifdef HAVE_LONGLONG 58 #define SHA384_BLOCK_LENGTH 128 59 #define SHA384_DIGEST_LENGTH 48 60 #define SHA384_DIGEST_STRING_LENGTH (SHA384_DIGEST_LENGTH * 2 + 1) 61 #define SHA512_BLOCK_LENGTH 128 62 #define _SHA512_BLOCK_LENGTH 128 63 #define SHA512_DIGEST_LENGTH 64 64 #define SHA512_DIGEST_STRING_LENGTH (SHA512_DIGEST_LENGTH * 2 + 1) 65 #else 66 #define _SHA512_BLOCK_LENGTH 128 67 #endif 68 69 70 /* ** SHA-256/384/512 Context Structure ****************************** */ 71 typedef struct _SHA2_CTX { 72 union { 73 UInt32_t st32[8]; 74 #ifdef HAVE_LONGLONG 75 UInt64_t st64[8]; 76 #else 77 UInt32_t st64[16]; /* Keep original size */ 78 #endif 79 } state; 80 #ifdef HAVE_LONGLONG 81 UInt64_t bitcount[2]; 82 #else 83 UInt32_t bitcount[4]; 84 #endif 85 UInt8_t buffer[_SHA512_BLOCK_LENGTH]; 86 } SHA2_CTX; 87 88 89 #ifdef __cplusplus 90 extern "C" { 91 #endif 92 93 extern void SHA256Init __PR((SHA2_CTX *)); 94 extern void SHA256Transform __PR((UInt32_t state[8], 95 const UInt8_t [SHA256_BLOCK_LENGTH])); 96 extern void SHA256Update __PR((SHA2_CTX *, const UInt8_t *, size_t)); 97 extern void SHA256Pad __PR((SHA2_CTX *)); 98 extern void SHA256Final __PR((UInt8_t [SHA256_DIGEST_LENGTH], 99 SHA2_CTX *)); 100 extern char *SHA256End __PR((SHA2_CTX *, char *)); 101 extern char *SHA256File __PR((const char *, char *)); 102 extern char *SHA256FileChunk __PR((const char *, char *, off_t, off_t)); 103 extern char *SHA256Data __PR((const UInt8_t *, size_t, char *)); 104 105 #ifdef HAVE_LONGLONG 106 extern void SHA384Init __PR((SHA2_CTX *)); 107 extern void SHA384Transform __PR((UInt64_t state[8], 108 const UInt8_t [SHA384_BLOCK_LENGTH])); 109 extern void SHA384Update __PR((SHA2_CTX *, const UInt8_t *, size_t)); 110 extern void SHA384Pad __PR((SHA2_CTX *)); 111 extern void SHA384Final __PR((UInt8_t [SHA384_DIGEST_LENGTH], 112 SHA2_CTX *)); 113 extern char *SHA384End __PR((SHA2_CTX *, char *)); 114 extern char *SHA384File __PR((const char *, char *)); 115 extern char *SHA384FileChunk __PR((const char *, char *, off_t, off_t)); 116 extern char *SHA384Data __PR((const UInt8_t *, size_t, char *)); 117 118 extern void SHA512Init __PR((SHA2_CTX *)); 119 extern void SHA512Transform __PR((UInt64_t state[8], 120 const UInt8_t [SHA512_BLOCK_LENGTH])); 121 extern void SHA512Update __PR((SHA2_CTX *, const UInt8_t *, size_t)); 122 extern void SHA512Pad __PR((SHA2_CTX *)); 123 extern void SHA512Final __PR((UInt8_t [SHA512_DIGEST_LENGTH], 124 SHA2_CTX *)); 125 extern char *SHA512End __PR((SHA2_CTX *, char *)); 126 extern char *SHA512File __PR((const char *, char *)); 127 extern char *SHA512FileChunk __PR((const char *, char *, off_t, off_t)); 128 extern char *SHA512Data __PR((const UInt8_t *, size_t, char *)); 129 #endif 130 131 #ifdef __cplusplus 132 } 133 #endif 134 135 #endif /* _SCHILY_SHA2_H */ 136