1 /* $NetBSD: sha2.h,v 1.2 2021/08/14 16:14:53 christos 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 <sys/cdefs.h> 42 __RCSID("$NetBSD: sha2.h,v 1.2 2021/08/14 16:14:53 christos Exp $"); 43 44 #include "portable.h" 45 46 #ifdef HAVE_INTTYPES_H 47 # define SHA2_USE_INTTYPES_H 1 48 #endif 49 50 #ifndef LITTLE_ENDIAN 51 # define LITTLE_ENDIAN 1234 52 #endif 53 #ifndef BIG_ENDIAN 54 # define BIG_ENDIAN 4321 55 #endif 56 #ifndef BYTE_ORDER 57 # ifdef WORDS_BIGENDIAN 58 # define BYTE_ORDER BIG_ENDIAN 59 # else 60 # define BYTE_ORDER LITTLE_ENDIAN 61 # endif 62 #endif 63 64 #ifdef __cplusplus 65 extern "C" { 66 #endif 67 68 /* 69 * Import u_intXX_t size_t type definitions from system headers. You 70 * may need to change this, or define these things yourself in this 71 * file. 72 */ 73 #include <sys/types.h> 74 75 #ifdef SHA2_USE_INTTYPES_H 76 77 #include <inttypes.h> 78 79 #endif /* SHA2_USE_INTTYPES_H */ 80 81 82 /*** SHA-256/384/512 Various Length Definitions ***********************/ 83 #define SHA256_BLOCK_LENGTH 64 84 #define SHA256_DIGEST_LENGTH 32 85 #define SHA256_DIGEST_STRING_LENGTH (SHA256_DIGEST_LENGTH * 2 + 1) 86 #define SHA384_BLOCK_LENGTH 128 87 #define SHA384_DIGEST_LENGTH 48 88 #define SHA384_DIGEST_STRING_LENGTH (SHA384_DIGEST_LENGTH * 2 + 1) 89 #define SHA512_BLOCK_LENGTH 128 90 #define SHA512_DIGEST_LENGTH 64 91 #define SHA512_DIGEST_STRING_LENGTH (SHA512_DIGEST_LENGTH * 2 + 1) 92 93 94 /*** SHA-256/384/512 Context Structures *******************************/ 95 /* NOTE: If your architecture does not define either u_intXX_t types or 96 * uintXX_t (from inttypes.h), you may need to define things by hand 97 * for your system: 98 */ 99 #if 0 100 typedef unsigned char u_int8_t; /* 1-byte (8-bits) */ 101 typedef unsigned int u_int32_t; /* 4-bytes (32-bits) */ 102 typedef unsigned long long u_int64_t; /* 8-bytes (64-bits) */ 103 #endif 104 /* 105 * Most BSD systems already define u_intXX_t types, as does Linux. 106 * Some systems, however, like Compaq's Tru64 Unix instead can use 107 * uintXX_t types defined by very recent ANSI C standards and included 108 * in the file: 109 * 110 * #include <inttypes.h> 111 * 112 * If you choose to use <inttypes.h> then please define: 113 * 114 * #define SHA2_USE_INTTYPES_H 115 * 116 * Or on the command line during compile: 117 * 118 * cc -DSHA2_USE_INTTYPES_H ... 119 */ 120 #ifdef SHA2_USE_INTTYPES_H 121 122 typedef struct _SHA256_CTX { 123 uint32_t state[8]; 124 uint64_t bitcount; 125 uint8_t buffer[SHA256_BLOCK_LENGTH]; 126 } SHA256_CTX; 127 typedef struct _SHA512_CTX { 128 uint64_t state[8]; 129 uint64_t bitcount[2]; 130 uint8_t buffer[SHA512_BLOCK_LENGTH]; 131 } SHA512_CTX; 132 133 #else /* SHA2_USE_INTTYPES_H */ 134 135 typedef struct _SHA256_CTX { 136 u_int32_t state[8]; 137 u_int64_t bitcount; 138 u_int8_t buffer[SHA256_BLOCK_LENGTH]; 139 } SHA256_CTX; 140 typedef struct _SHA512_CTX { 141 u_int64_t state[8]; 142 u_int64_t bitcount[2]; 143 u_int8_t buffer[SHA512_BLOCK_LENGTH]; 144 } SHA512_CTX; 145 146 #endif /* SHA2_USE_INTTYPES_H */ 147 148 typedef SHA512_CTX SHA384_CTX; 149 150 151 /*** SHA-256/384/512 Function Prototypes ******************************/ 152 /* avoid symbol clash with other crypto libs */ 153 #define SHA256_Init pw_SHA256_Init 154 #define SHA256_Update pw_SHA256_Update 155 #define SHA256_Final pw_SHA256_Final 156 #define SHA256_End pw_SHA256_End 157 #define SHA256_Data pw_SHA256_Data 158 159 #define SHA384_Init pw_SHA384_Init 160 #define SHA384_Update pw_SHA384_Update 161 #define SHA384_Final pw_SHA384_Final 162 #define SHA384_End pw_SHA384_End 163 #define SHA384_Data pw_SHA384_Data 164 165 #define SHA512_Init pw_SHA512_Init 166 #define SHA512_Update pw_SHA512_Update 167 #define SHA512_Final pw_SHA512_Final 168 #define SHA512_End pw_SHA512_End 169 #define SHA512_Data pw_SHA512_Data 170 171 #ifndef NOPROTO 172 #ifdef SHA2_USE_INTTYPES_H 173 174 void SHA256_Init(SHA256_CTX *); 175 void SHA256_Update(SHA256_CTX*, const uint8_t*, size_t); 176 void SHA256_Final(uint8_t[SHA256_DIGEST_LENGTH], SHA256_CTX*); 177 char* SHA256_End(SHA256_CTX*, char[SHA256_DIGEST_STRING_LENGTH]); 178 char* SHA256_Data(const uint8_t*, size_t, char[SHA256_DIGEST_STRING_LENGTH]); 179 180 void SHA384_Init(SHA384_CTX*); 181 void SHA384_Update(SHA384_CTX*, const uint8_t*, size_t); 182 void SHA384_Final(uint8_t[SHA384_DIGEST_LENGTH], SHA384_CTX*); 183 char* SHA384_End(SHA384_CTX*, char[SHA384_DIGEST_STRING_LENGTH]); 184 char* SHA384_Data(const uint8_t*, size_t, char[SHA384_DIGEST_STRING_LENGTH]); 185 186 void SHA512_Init(SHA512_CTX*); 187 void SHA512_Update(SHA512_CTX*, const uint8_t*, size_t); 188 void SHA512_Final(uint8_t[SHA512_DIGEST_LENGTH], SHA512_CTX*); 189 char* SHA512_End(SHA512_CTX*, char[SHA512_DIGEST_STRING_LENGTH]); 190 char* SHA512_Data(const uint8_t*, size_t, char[SHA512_DIGEST_STRING_LENGTH]); 191 192 #else /* SHA2_USE_INTTYPES_H */ 193 194 void SHA256_Init(SHA256_CTX *); 195 void SHA256_Update(SHA256_CTX*, const u_int8_t*, size_t); 196 void SHA256_Final(u_int8_t[SHA256_DIGEST_LENGTH], SHA256_CTX*); 197 char* SHA256_End(SHA256_CTX*, char[SHA256_DIGEST_STRING_LENGTH]); 198 char* SHA256_Data(const u_int8_t*, size_t, char[SHA256_DIGEST_STRING_LENGTH]); 199 200 void SHA384_Init(SHA384_CTX*); 201 void SHA384_Update(SHA384_CTX*, const u_int8_t*, size_t); 202 void SHA384_Final(u_int8_t[SHA384_DIGEST_LENGTH], SHA384_CTX*); 203 char* SHA384_End(SHA384_CTX*, char[SHA384_DIGEST_STRING_LENGTH]); 204 char* SHA384_Data(const u_int8_t*, size_t, char[SHA384_DIGEST_STRING_LENGTH]); 205 206 void SHA512_Init(SHA512_CTX*); 207 void SHA512_Update(SHA512_CTX*, const u_int8_t*, size_t); 208 void SHA512_Final(u_int8_t[SHA512_DIGEST_LENGTH], SHA512_CTX*); 209 char* SHA512_End(SHA512_CTX*, char[SHA512_DIGEST_STRING_LENGTH]); 210 char* SHA512_Data(const u_int8_t*, size_t, char[SHA512_DIGEST_STRING_LENGTH]); 211 212 #endif /* SHA2_USE_INTTYPES_H */ 213 214 #else /* NOPROTO */ 215 216 void SHA256_Init(); 217 void SHA256_Update(); 218 void SHA256_Final(); 219 char* SHA256_End(); 220 char* SHA256_Data(); 221 222 void SHA384_Init(); 223 void SHA384_Update(); 224 void SHA384_Final(); 225 char* SHA384_End(); 226 char* SHA384_Data(); 227 228 void SHA512_Init(); 229 void SHA512_Update(); 230 void SHA512_Final(); 231 char* SHA512_End(); 232 char* SHA512_Data(); 233 234 #endif /* NOPROTO */ 235 236 #ifdef __cplusplus 237 } 238 #endif /* __cplusplus */ 239 240 #endif /* __SHA2_H__ */ 241 242