186d7f5d3SJohn Marino /* $FreeBSD: src/sys/crypto/sha2/sha2.h,v 1.2 2002/03/20 05:13:51 alfred Exp $ */ 286d7f5d3SJohn Marino /* $KAME: sha2.h,v 1.3 2001/03/12 08:27:48 itojun Exp $ */ 386d7f5d3SJohn Marino 486d7f5d3SJohn Marino /* 586d7f5d3SJohn Marino * sha2.h 686d7f5d3SJohn Marino * 786d7f5d3SJohn Marino * Version 1.0.0beta1 886d7f5d3SJohn Marino * 986d7f5d3SJohn Marino * Written by Aaron D. Gifford <me@aarongifford.com> 1086d7f5d3SJohn Marino * 1186d7f5d3SJohn Marino * Copyright 2000 Aaron D. Gifford. All rights reserved. 1286d7f5d3SJohn Marino * 1386d7f5d3SJohn Marino * Redistribution and use in source and binary forms, with or without 1486d7f5d3SJohn Marino * modification, are permitted provided that the following conditions 1586d7f5d3SJohn Marino * are met: 1686d7f5d3SJohn Marino * 1. Redistributions of source code must retain the above copyright 1786d7f5d3SJohn Marino * notice, this list of conditions and the following disclaimer. 1886d7f5d3SJohn Marino * 2. Redistributions in binary form must reproduce the above copyright 1986d7f5d3SJohn Marino * notice, this list of conditions and the following disclaimer in the 2086d7f5d3SJohn Marino * documentation and/or other materials provided with the distribution. 2186d7f5d3SJohn Marino * 3. Neither the name of the copyright holder nor the names of contributors 2286d7f5d3SJohn Marino * may be used to endorse or promote products derived from this software 2386d7f5d3SJohn Marino * without specific prior written permission. 2486d7f5d3SJohn Marino * 2586d7f5d3SJohn Marino * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) AND CONTRIBUTOR(S) ``AS IS'' AND 2686d7f5d3SJohn Marino * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2786d7f5d3SJohn Marino * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2886d7f5d3SJohn Marino * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) OR CONTRIBUTOR(S) BE LIABLE 2986d7f5d3SJohn Marino * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 3086d7f5d3SJohn Marino * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 3186d7f5d3SJohn Marino * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3286d7f5d3SJohn Marino * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3386d7f5d3SJohn Marino * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3486d7f5d3SJohn Marino * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3586d7f5d3SJohn Marino * SUCH DAMAGE. 3686d7f5d3SJohn Marino * 3786d7f5d3SJohn Marino */ 3886d7f5d3SJohn Marino 3986d7f5d3SJohn Marino #ifndef __SHA2_H__ 4086d7f5d3SJohn Marino #define __SHA2_H__ 4186d7f5d3SJohn Marino 4286d7f5d3SJohn Marino #ifdef __cplusplus 4386d7f5d3SJohn Marino extern "C" { 4486d7f5d3SJohn Marino #endif 4586d7f5d3SJohn Marino 4686d7f5d3SJohn Marino 4786d7f5d3SJohn Marino /*** SHA-256/384/512 Various Length Definitions ***********************/ 4886d7f5d3SJohn Marino #define SHA256_BLOCK_LENGTH 64 4986d7f5d3SJohn Marino #define SHA256_DIGEST_LENGTH 32 5086d7f5d3SJohn Marino #define SHA256_DIGEST_STRING_LENGTH (SHA256_DIGEST_LENGTH * 2 + 1) 5186d7f5d3SJohn Marino #define SHA384_BLOCK_LENGTH 128 5286d7f5d3SJohn Marino #define SHA384_DIGEST_LENGTH 48 5386d7f5d3SJohn Marino #define SHA384_DIGEST_STRING_LENGTH (SHA384_DIGEST_LENGTH * 2 + 1) 5486d7f5d3SJohn Marino #define SHA512_BLOCK_LENGTH 128 5586d7f5d3SJohn Marino #define SHA512_DIGEST_LENGTH 64 5686d7f5d3SJohn Marino #define SHA512_DIGEST_STRING_LENGTH (SHA512_DIGEST_LENGTH * 2 + 1) 5786d7f5d3SJohn Marino 5886d7f5d3SJohn Marino 5986d7f5d3SJohn Marino /*** SHA-256/384/512 Context Structures *******************************/ 6086d7f5d3SJohn Marino /* NOTE: If your architecture does not define either u_intXX_t types or 6186d7f5d3SJohn Marino * uintXX_t (from inttypes.h), you may need to define things by hand 6286d7f5d3SJohn Marino * for your system: 6386d7f5d3SJohn Marino */ 6486d7f5d3SJohn Marino #if 0 6586d7f5d3SJohn Marino typedef unsigned char u_int8_t; /* 1-byte (8-bits) */ 6686d7f5d3SJohn Marino typedef unsigned int u_int32_t; /* 4-bytes (32-bits) */ 6786d7f5d3SJohn Marino typedef unsigned long long u_int64_t; /* 8-bytes (64-bits) */ 6886d7f5d3SJohn Marino #endif 6986d7f5d3SJohn Marino /* 7086d7f5d3SJohn Marino * Most BSD systems already define u_intXX_t types, as does Linux. 7186d7f5d3SJohn Marino * Some systems, however, like Compaq's Tru64 Unix instead can use 7286d7f5d3SJohn Marino * uintXX_t types defined by very recent ANSI C standards and included 7386d7f5d3SJohn Marino * in the file: 7486d7f5d3SJohn Marino * 7586d7f5d3SJohn Marino * #include <inttypes.h> 7686d7f5d3SJohn Marino * 7786d7f5d3SJohn Marino * If you choose to use <inttypes.h> then please define: 7886d7f5d3SJohn Marino * 7986d7f5d3SJohn Marino * #define SHA2_USE_INTTYPES_H 8086d7f5d3SJohn Marino * 8186d7f5d3SJohn Marino * Or on the command line during compile: 8286d7f5d3SJohn Marino * 8386d7f5d3SJohn Marino * cc -DSHA2_USE_INTTYPES_H ... 8486d7f5d3SJohn Marino */ 8586d7f5d3SJohn Marino #if 0 /*def SHA2_USE_INTTYPES_H*/ 8686d7f5d3SJohn Marino 8786d7f5d3SJohn Marino typedef struct _SHA256_CTX { 8886d7f5d3SJohn Marino uint32_t state[8]; 8986d7f5d3SJohn Marino uint64_t bitcount; 9086d7f5d3SJohn Marino uint8_t buffer[SHA256_BLOCK_LENGTH]; 9186d7f5d3SJohn Marino } SHA256_CTX; 9286d7f5d3SJohn Marino typedef struct _SHA512_CTX { 9386d7f5d3SJohn Marino uint64_t state[8]; 9486d7f5d3SJohn Marino uint64_t bitcount[2]; 9586d7f5d3SJohn Marino uint8_t buffer[SHA512_BLOCK_LENGTH]; 9686d7f5d3SJohn Marino } SHA512_CTX; 9786d7f5d3SJohn Marino 9886d7f5d3SJohn Marino #else /* SHA2_USE_INTTYPES_H */ 9986d7f5d3SJohn Marino 10086d7f5d3SJohn Marino typedef struct _SHA256_CTX { 10186d7f5d3SJohn Marino u_int32_t state[8]; 10286d7f5d3SJohn Marino u_int64_t bitcount; 10386d7f5d3SJohn Marino u_int8_t buffer[SHA256_BLOCK_LENGTH]; 10486d7f5d3SJohn Marino } SHA256_CTX; 10586d7f5d3SJohn Marino typedef struct _SHA512_CTX { 10686d7f5d3SJohn Marino u_int64_t state[8]; 10786d7f5d3SJohn Marino u_int64_t bitcount[2]; 10886d7f5d3SJohn Marino u_int8_t buffer[SHA512_BLOCK_LENGTH]; 10986d7f5d3SJohn Marino } SHA512_CTX; 11086d7f5d3SJohn Marino 11186d7f5d3SJohn Marino #endif /* SHA2_USE_INTTYPES_H */ 11286d7f5d3SJohn Marino 11386d7f5d3SJohn Marino typedef SHA512_CTX SHA384_CTX; 11486d7f5d3SJohn Marino 11586d7f5d3SJohn Marino 11686d7f5d3SJohn Marino /*** SHA-256/384/512 Function Prototypes ******************************/ 11786d7f5d3SJohn Marino 11886d7f5d3SJohn Marino void SHA256_Init(SHA256_CTX *); 11986d7f5d3SJohn Marino void SHA256_Update(SHA256_CTX*, const u_int8_t*, size_t); 12086d7f5d3SJohn Marino void SHA256_Final(u_int8_t[SHA256_DIGEST_LENGTH], SHA256_CTX*); 12186d7f5d3SJohn Marino char* SHA256_End(SHA256_CTX*, char[SHA256_DIGEST_STRING_LENGTH]); 12286d7f5d3SJohn Marino char* SHA256_Data(const u_int8_t*, size_t, char[SHA256_DIGEST_STRING_LENGTH]); 12386d7f5d3SJohn Marino 12486d7f5d3SJohn Marino void SHA384_Init(SHA384_CTX*); 12586d7f5d3SJohn Marino void SHA384_Update(SHA384_CTX*, const u_int8_t*, size_t); 12686d7f5d3SJohn Marino void SHA384_Final(u_int8_t[SHA384_DIGEST_LENGTH], SHA384_CTX*); 12786d7f5d3SJohn Marino char* SHA384_End(SHA384_CTX*, char[SHA384_DIGEST_STRING_LENGTH]); 12886d7f5d3SJohn Marino char* SHA384_Data(const u_int8_t*, size_t, char[SHA384_DIGEST_STRING_LENGTH]); 12986d7f5d3SJohn Marino 13086d7f5d3SJohn Marino void SHA512_Init(SHA512_CTX*); 13186d7f5d3SJohn Marino void SHA512_Update(SHA512_CTX*, const u_int8_t*, size_t); 13286d7f5d3SJohn Marino void SHA512_Final(u_int8_t[SHA512_DIGEST_LENGTH], SHA512_CTX*); 13386d7f5d3SJohn Marino char* SHA512_End(SHA512_CTX*, char[SHA512_DIGEST_STRING_LENGTH]); 13486d7f5d3SJohn Marino char* SHA512_Data(const u_int8_t*, size_t, char[SHA512_DIGEST_STRING_LENGTH]); 13586d7f5d3SJohn Marino 13686d7f5d3SJohn Marino #ifdef __cplusplus 13786d7f5d3SJohn Marino } 13886d7f5d3SJohn Marino #endif /* __cplusplus */ 13986d7f5d3SJohn Marino 14086d7f5d3SJohn Marino #endif /* __SHA2_H__ */ 14186d7f5d3SJohn Marino 142