1 /* $NetBSD: sha2.h,v 1.1.1.2 2014/05/28 09:58:28 tron Exp $ */ 2 3 /* $OpenLDAP$ */ 4 /* 5 * FILE: sha2.h 6 * AUTHOR: Aaron D. Gifford - http://www.aarongifford.com/ 7 * 8 * Copyright (c) 2000-2001, Aaron D. Gifford 9 * All rights reserved. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. Neither the name of the copyright holder nor the names of contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTOR(S) ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTOR(S) BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 * 35 * Id: sha2.h,v 1.1 2001/11/08 00:02:01 adg Exp adg 36 */ 37 38 #ifndef __SHA2_H__ 39 #define __SHA2_H__ 40 41 #include "portable.h" 42 43 #ifdef HAVE_INTTYPES_H 44 # define SHA2_USE_INTTYPES_H 1 45 #endif 46 47 #ifndef LITTLE_ENDIAN 48 # define LITTLE_ENDIAN 1234 49 #endif 50 #ifndef BIG_ENDIAN 51 # define BIG_ENDIAN 4321 52 #endif 53 #ifndef BYTE_ORDER 54 # ifdef WORDS_BIGENDIAN 55 # define BYTE_ORDER BIG_ENDIAN 56 # else 57 # define BYTE_ORDER LITTLE_ENDIAN 58 # endif 59 #endif 60 61 #ifdef __cplusplus 62 extern "C" { 63 #endif 64 65 /* 66 * Import u_intXX_t size_t type definitions from system headers. You 67 * may need to change this, or define these things yourself in this 68 * file. 69 */ 70 #include <sys/types.h> 71 72 #ifdef SHA2_USE_INTTYPES_H 73 74 #include <inttypes.h> 75 76 #endif /* SHA2_USE_INTTYPES_H */ 77 78 79 /*** SHA-256/384/512 Various Length Definitions ***********************/ 80 #define SHA256_BLOCK_LENGTH 64 81 #define SHA256_DIGEST_LENGTH 32 82 #define SHA256_DIGEST_STRING_LENGTH (SHA256_DIGEST_LENGTH * 2 + 1) 83 #define SHA384_BLOCK_LENGTH 128 84 #define SHA384_DIGEST_LENGTH 48 85 #define SHA384_DIGEST_STRING_LENGTH (SHA384_DIGEST_LENGTH * 2 + 1) 86 #define SHA512_BLOCK_LENGTH 128 87 #define SHA512_DIGEST_LENGTH 64 88 #define SHA512_DIGEST_STRING_LENGTH (SHA512_DIGEST_LENGTH * 2 + 1) 89 90 91 /*** SHA-256/384/512 Context Structures *******************************/ 92 /* NOTE: If your architecture does not define either u_intXX_t types or 93 * uintXX_t (from inttypes.h), you may need to define things by hand 94 * for your system: 95 */ 96 #if 0 97 typedef unsigned char u_int8_t; /* 1-byte (8-bits) */ 98 typedef unsigned int u_int32_t; /* 4-bytes (32-bits) */ 99 typedef unsigned long long u_int64_t; /* 8-bytes (64-bits) */ 100 #endif 101 /* 102 * Most BSD systems already define u_intXX_t types, as does Linux. 103 * Some systems, however, like Compaq's Tru64 Unix instead can use 104 * uintXX_t types defined by very recent ANSI C standards and included 105 * in the file: 106 * 107 * #include <inttypes.h> 108 * 109 * If you choose to use <inttypes.h> then please define: 110 * 111 * #define SHA2_USE_INTTYPES_H 112 * 113 * Or on the command line during compile: 114 * 115 * cc -DSHA2_USE_INTTYPES_H ... 116 */ 117 #ifdef SHA2_USE_INTTYPES_H 118 119 typedef struct _SHA256_CTX { 120 uint32_t state[8]; 121 uint64_t bitcount; 122 uint8_t buffer[SHA256_BLOCK_LENGTH]; 123 } SHA256_CTX; 124 typedef struct _SHA512_CTX { 125 uint64_t state[8]; 126 uint64_t bitcount[2]; 127 uint8_t buffer[SHA512_BLOCK_LENGTH]; 128 } SHA512_CTX; 129 130 #else /* SHA2_USE_INTTYPES_H */ 131 132 typedef struct _SHA256_CTX { 133 u_int32_t state[8]; 134 u_int64_t bitcount; 135 u_int8_t buffer[SHA256_BLOCK_LENGTH]; 136 } SHA256_CTX; 137 typedef struct _SHA512_CTX { 138 u_int64_t state[8]; 139 u_int64_t bitcount[2]; 140 u_int8_t buffer[SHA512_BLOCK_LENGTH]; 141 } SHA512_CTX; 142 143 #endif /* SHA2_USE_INTTYPES_H */ 144 145 typedef SHA512_CTX SHA384_CTX; 146 147 148 /*** SHA-256/384/512 Function Prototypes ******************************/ 149 #ifndef NOPROTO 150 #ifdef SHA2_USE_INTTYPES_H 151 152 void SHA256_Init(SHA256_CTX *); 153 void SHA256_Update(SHA256_CTX*, const uint8_t*, size_t); 154 void SHA256_Final(uint8_t[SHA256_DIGEST_LENGTH], SHA256_CTX*); 155 char* SHA256_End(SHA256_CTX*, char[SHA256_DIGEST_STRING_LENGTH]); 156 char* SHA256_Data(const uint8_t*, size_t, char[SHA256_DIGEST_STRING_LENGTH]); 157 158 void SHA384_Init(SHA384_CTX*); 159 void SHA384_Update(SHA384_CTX*, const uint8_t*, size_t); 160 void SHA384_Final(uint8_t[SHA384_DIGEST_LENGTH], SHA384_CTX*); 161 char* SHA384_End(SHA384_CTX*, char[SHA384_DIGEST_STRING_LENGTH]); 162 char* SHA384_Data(const uint8_t*, size_t, char[SHA384_DIGEST_STRING_LENGTH]); 163 164 void SHA512_Init(SHA512_CTX*); 165 void SHA512_Update(SHA512_CTX*, const uint8_t*, size_t); 166 void SHA512_Final(uint8_t[SHA512_DIGEST_LENGTH], SHA512_CTX*); 167 char* SHA512_End(SHA512_CTX*, char[SHA512_DIGEST_STRING_LENGTH]); 168 char* SHA512_Data(const uint8_t*, size_t, char[SHA512_DIGEST_STRING_LENGTH]); 169 170 #else /* SHA2_USE_INTTYPES_H */ 171 172 void SHA256_Init(SHA256_CTX *); 173 void SHA256_Update(SHA256_CTX*, const u_int8_t*, size_t); 174 void SHA256_Final(u_int8_t[SHA256_DIGEST_LENGTH], SHA256_CTX*); 175 char* SHA256_End(SHA256_CTX*, char[SHA256_DIGEST_STRING_LENGTH]); 176 char* SHA256_Data(const u_int8_t*, size_t, char[SHA256_DIGEST_STRING_LENGTH]); 177 178 void SHA384_Init(SHA384_CTX*); 179 void SHA384_Update(SHA384_CTX*, const u_int8_t*, size_t); 180 void SHA384_Final(u_int8_t[SHA384_DIGEST_LENGTH], SHA384_CTX*); 181 char* SHA384_End(SHA384_CTX*, char[SHA384_DIGEST_STRING_LENGTH]); 182 char* SHA384_Data(const u_int8_t*, size_t, char[SHA384_DIGEST_STRING_LENGTH]); 183 184 void SHA512_Init(SHA512_CTX*); 185 void SHA512_Update(SHA512_CTX*, const u_int8_t*, size_t); 186 void SHA512_Final(u_int8_t[SHA512_DIGEST_LENGTH], SHA512_CTX*); 187 char* SHA512_End(SHA512_CTX*, char[SHA512_DIGEST_STRING_LENGTH]); 188 char* SHA512_Data(const u_int8_t*, size_t, char[SHA512_DIGEST_STRING_LENGTH]); 189 190 #endif /* SHA2_USE_INTTYPES_H */ 191 192 #else /* NOPROTO */ 193 194 void SHA256_Init(); 195 void SHA256_Update(); 196 void SHA256_Final(); 197 char* SHA256_End(); 198 char* SHA256_Data(); 199 200 void SHA384_Init(); 201 void SHA384_Update(); 202 void SHA384_Final(); 203 char* SHA384_End(); 204 char* SHA384_Data(); 205 206 void SHA512_Init(); 207 void SHA512_Update(); 208 void SHA512_Final(); 209 char* SHA512_End(); 210 char* SHA512_Data(); 211 212 #endif /* NOPROTO */ 213 214 #ifdef __cplusplus 215 } 216 #endif /* __cplusplus */ 217 218 #endif /* __SHA2_H__ */ 219 220