1 /* Copyright (C) 1995-1998 Eric Young ([email protected]) 2 * All rights reserved. 3 * 4 * This package is an SSL implementation written 5 * by Eric Young ([email protected]). 6 * The implementation was written so as to conform with Netscapes SSL. 7 * 8 * This library is free for commercial and non-commercial use as long as 9 * the following conditions are aheared to. The following conditions 10 * apply to all code found in this distribution, be it the RC4, RSA, 11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation 12 * included with this distribution is covered by the same copyright terms 13 * except that the holder is Tim Hudson ([email protected]). 14 * 15 * Copyright remains Eric Young's, and as such any Copyright notices in 16 * the code are not to be removed. 17 * If this package is used in a product, Eric Young should be given attribution 18 * as the author of the parts of the library used. 19 * This can be in the form of a textual message at program startup or 20 * in documentation (online or textual) provided with the package. 21 * 22 * Redistribution and use in source and binary forms, with or without 23 * modification, are permitted provided that the following conditions 24 * are met: 25 * 1. Redistributions of source code must retain the copyright 26 * notice, this list of conditions and the following disclaimer. 27 * 2. Redistributions in binary form must reproduce the above copyright 28 * notice, this list of conditions and the following disclaimer in the 29 * documentation and/or other materials provided with the distribution. 30 * 3. All advertising materials mentioning features or use of this software 31 * must display the following acknowledgement: 32 * "This product includes cryptographic software written by 33 * Eric Young ([email protected])" 34 * The word 'cryptographic' can be left out if the rouines from the library 35 * being used are not cryptographic related :-). 36 * 4. If you include any Windows specific code (or a derivative thereof) from 37 * the apps directory (application code) you must include an acknowledgement: 38 * "This product includes software written by Tim Hudson ([email protected])" 39 * 40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 50 * SUCH DAMAGE. 51 * 52 * The licence and distribution terms for any publically available version or 53 * derivative of this code cannot be changed. i.e. this code cannot simply be 54 * copied and put under another distribution licence 55 * [including the GNU Public Licence.] */ 56 57 #ifndef OPENSSL_HEADER_SHA_H 58 #define OPENSSL_HEADER_SHA_H 59 60 #include <openssl/base.h> 61 62 #if defined(__cplusplus) 63 extern "C" { 64 #endif 65 66 67 // The SHA family of hash functions (SHA-1 and SHA-2). 68 69 70 // SHA_CBLOCK is the block size of SHA-1. 71 #define SHA_CBLOCK 64 72 73 // SHA_DIGEST_LENGTH is the length of a SHA-1 digest. 74 #define SHA_DIGEST_LENGTH 20 75 76 // SHA1_Init initialises |sha| and returns one. 77 OPENSSL_EXPORT int SHA1_Init(SHA_CTX *sha); 78 79 // SHA1_Update adds |len| bytes from |data| to |sha| and returns one. 80 OPENSSL_EXPORT int SHA1_Update(SHA_CTX *sha, const void *data, size_t len); 81 82 // SHA1_Final adds the final padding to |sha| and writes the resulting digest to 83 // |out|, which must have at least |SHA_DIGEST_LENGTH| bytes of space. It 84 // returns one. 85 OPENSSL_EXPORT int SHA1_Final(uint8_t out[SHA_DIGEST_LENGTH], SHA_CTX *sha); 86 87 // SHA1 writes the digest of |len| bytes from |data| to |out| and returns 88 // |out|. There must be at least |SHA_DIGEST_LENGTH| bytes of space in 89 // |out|. 90 OPENSSL_EXPORT uint8_t *SHA1(const uint8_t *data, size_t len, 91 uint8_t out[SHA_DIGEST_LENGTH]); 92 93 // SHA1_Transform is a low-level function that performs a single, SHA-1 block 94 // transformation using the state from |sha| and |SHA_CBLOCK| bytes from 95 // |block|. 96 OPENSSL_EXPORT void SHA1_Transform(SHA_CTX *sha, 97 const uint8_t block[SHA_CBLOCK]); 98 99 // CRYPTO_fips_186_2_prf derives |out_len| bytes from |xkey| using the PRF 100 // defined in FIPS 186-2, Appendix 3.1, with change notice 1 applied. The b 101 // parameter is 160 and seed, XKEY, is also 160 bits. The optional XSEED user 102 // input is all zeros. 103 // 104 // The PRF generates a sequence of 320-bit numbers. Each number is encoded as a 105 // 40-byte string in big-endian and then concatenated to form |out|. If 106 // |out_len| is not a multiple of 40, the result is truncated. This matches the 107 // construction used in Section 7 of RFC 4186 and Section 7 of RFC 4187. 108 // 109 // This PRF is based on SHA-1, a weak hash function, and should not be used 110 // in new protocols. It is provided for compatibility with some legacy EAP 111 // methods. 112 OPENSSL_EXPORT void CRYPTO_fips_186_2_prf( 113 uint8_t *out, size_t out_len, const uint8_t xkey[SHA_DIGEST_LENGTH]); 114 115 struct sha_state_st { 116 #if defined(__cplusplus) || defined(OPENSSL_WINDOWS) 117 uint32_t h[5]; 118 #else 119 // wpa_supplicant accesses |h0|..|h4| so we must support those names for 120 // compatibility with it until it can be updated. Anonymous unions are only 121 // standard in C11, so disable this workaround in C++. 122 union { 123 uint32_t h[5]; 124 struct { 125 uint32_t h0; 126 uint32_t h1; 127 uint32_t h2; 128 uint32_t h3; 129 uint32_t h4; 130 }; 131 }; 132 #endif 133 uint32_t Nl, Nh; 134 uint8_t data[SHA_CBLOCK]; 135 unsigned num; 136 }; 137 138 139 // SHA-224. 140 141 // SHA224_CBLOCK is the block size of SHA-224. 142 #define SHA224_CBLOCK 64 143 144 // SHA224_DIGEST_LENGTH is the length of a SHA-224 digest. 145 #define SHA224_DIGEST_LENGTH 28 146 147 // SHA224_Init initialises |sha| and returns 1. 148 OPENSSL_EXPORT int SHA224_Init(SHA256_CTX *sha); 149 150 // SHA224_Update adds |len| bytes from |data| to |sha| and returns 1. 151 OPENSSL_EXPORT int SHA224_Update(SHA256_CTX *sha, const void *data, size_t len); 152 153 // SHA224_Final adds the final padding to |sha| and writes the resulting digest 154 // to |out|, which must have at least |SHA224_DIGEST_LENGTH| bytes of space. It 155 // returns one on success and zero on programmer error. 156 OPENSSL_EXPORT int SHA224_Final(uint8_t out[SHA224_DIGEST_LENGTH], 157 SHA256_CTX *sha); 158 159 // SHA224 writes the digest of |len| bytes from |data| to |out| and returns 160 // |out|. There must be at least |SHA224_DIGEST_LENGTH| bytes of space in 161 // |out|. 162 OPENSSL_EXPORT uint8_t *SHA224(const uint8_t *data, size_t len, 163 uint8_t out[SHA224_DIGEST_LENGTH]); 164 165 166 // SHA-256. 167 168 // SHA256_CBLOCK is the block size of SHA-256. 169 #define SHA256_CBLOCK 64 170 171 // SHA256_DIGEST_LENGTH is the length of a SHA-256 digest. 172 #define SHA256_DIGEST_LENGTH 32 173 174 // SHA256_Init initialises |sha| and returns 1. 175 OPENSSL_EXPORT int SHA256_Init(SHA256_CTX *sha); 176 177 // SHA256_Update adds |len| bytes from |data| to |sha| and returns 1. 178 OPENSSL_EXPORT int SHA256_Update(SHA256_CTX *sha, const void *data, size_t len); 179 180 // SHA256_Final adds the final padding to |sha| and writes the resulting digest 181 // to |out|, which must have at least |SHA256_DIGEST_LENGTH| bytes of space. It 182 // returns one on success and zero on programmer error. 183 OPENSSL_EXPORT int SHA256_Final(uint8_t out[SHA256_DIGEST_LENGTH], 184 SHA256_CTX *sha); 185 186 // SHA256 writes the digest of |len| bytes from |data| to |out| and returns 187 // |out|. There must be at least |SHA256_DIGEST_LENGTH| bytes of space in 188 // |out|. 189 OPENSSL_EXPORT uint8_t *SHA256(const uint8_t *data, size_t len, 190 uint8_t out[SHA256_DIGEST_LENGTH]); 191 192 // SHA256_Transform is a low-level function that performs a single, SHA-256 193 // block transformation using the state from |sha| and |SHA256_CBLOCK| bytes 194 // from |block|. 195 OPENSSL_EXPORT void SHA256_Transform(SHA256_CTX *sha, 196 const uint8_t block[SHA256_CBLOCK]); 197 198 // SHA256_TransformBlocks is a low-level function that takes |num_blocks| * 199 // |SHA256_CBLOCK| bytes of data and performs SHA-256 transforms on it to update 200 // |state|. You should not use this function unless you are implementing a 201 // derivative of SHA-256. 202 OPENSSL_EXPORT void SHA256_TransformBlocks(uint32_t state[8], 203 const uint8_t *data, 204 size_t num_blocks); 205 206 struct sha256_state_st { 207 uint32_t h[8]; 208 uint32_t Nl, Nh; 209 uint8_t data[SHA256_CBLOCK]; 210 unsigned num, md_len; 211 }; 212 213 214 // SHA-384. 215 216 // SHA384_CBLOCK is the block size of SHA-384. 217 #define SHA384_CBLOCK 128 218 219 // SHA384_DIGEST_LENGTH is the length of a SHA-384 digest. 220 #define SHA384_DIGEST_LENGTH 48 221 222 // SHA384_Init initialises |sha| and returns 1. 223 OPENSSL_EXPORT int SHA384_Init(SHA512_CTX *sha); 224 225 // SHA384_Update adds |len| bytes from |data| to |sha| and returns 1. 226 OPENSSL_EXPORT int SHA384_Update(SHA512_CTX *sha, const void *data, size_t len); 227 228 // SHA384_Final adds the final padding to |sha| and writes the resulting digest 229 // to |out|, which must have at least |SHA384_DIGEST_LENGTH| bytes of space. It 230 // returns one on success and zero on programmer error. 231 OPENSSL_EXPORT int SHA384_Final(uint8_t out[SHA384_DIGEST_LENGTH], 232 SHA512_CTX *sha); 233 234 // SHA384 writes the digest of |len| bytes from |data| to |out| and returns 235 // |out|. There must be at least |SHA384_DIGEST_LENGTH| bytes of space in 236 // |out|. 237 OPENSSL_EXPORT uint8_t *SHA384(const uint8_t *data, size_t len, 238 uint8_t out[SHA384_DIGEST_LENGTH]); 239 240 241 // SHA-512. 242 243 // SHA512_CBLOCK is the block size of SHA-512. 244 #define SHA512_CBLOCK 128 245 246 // SHA512_DIGEST_LENGTH is the length of a SHA-512 digest. 247 #define SHA512_DIGEST_LENGTH 64 248 249 // SHA512_Init initialises |sha| and returns 1. 250 OPENSSL_EXPORT int SHA512_Init(SHA512_CTX *sha); 251 252 // SHA512_Update adds |len| bytes from |data| to |sha| and returns 1. 253 OPENSSL_EXPORT int SHA512_Update(SHA512_CTX *sha, const void *data, size_t len); 254 255 // SHA512_Final adds the final padding to |sha| and writes the resulting digest 256 // to |out|, which must have at least |SHA512_DIGEST_LENGTH| bytes of space. It 257 // returns one on success and zero on programmer error. 258 OPENSSL_EXPORT int SHA512_Final(uint8_t out[SHA512_DIGEST_LENGTH], 259 SHA512_CTX *sha); 260 261 // SHA512 writes the digest of |len| bytes from |data| to |out| and returns 262 // |out|. There must be at least |SHA512_DIGEST_LENGTH| bytes of space in 263 // |out|. 264 OPENSSL_EXPORT uint8_t *SHA512(const uint8_t *data, size_t len, 265 uint8_t out[SHA512_DIGEST_LENGTH]); 266 267 // SHA512_Transform is a low-level function that performs a single, SHA-512 268 // block transformation using the state from |sha| and |SHA512_CBLOCK| bytes 269 // from |block|. 270 OPENSSL_EXPORT void SHA512_Transform(SHA512_CTX *sha, 271 const uint8_t block[SHA512_CBLOCK]); 272 273 struct sha512_state_st { 274 uint64_t h[8]; 275 uint64_t Nl, Nh; 276 uint8_t p[128]; 277 unsigned num, md_len; 278 }; 279 280 281 // SHA-512-256 282 // 283 // See https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf section 5.3.6 284 285 #define SHA512_256_DIGEST_LENGTH 32 286 287 // SHA512_256_Init initialises |sha| and returns 1. 288 OPENSSL_EXPORT int SHA512_256_Init(SHA512_CTX *sha); 289 290 // SHA512_256_Update adds |len| bytes from |data| to |sha| and returns 1. 291 OPENSSL_EXPORT int SHA512_256_Update(SHA512_CTX *sha, const void *data, 292 size_t len); 293 294 // SHA512_256_Final adds the final padding to |sha| and writes the resulting 295 // digest to |out|, which must have at least |SHA512_256_DIGEST_LENGTH| bytes of 296 // space. It returns one on success and zero on programmer error. 297 OPENSSL_EXPORT int SHA512_256_Final(uint8_t out[SHA512_256_DIGEST_LENGTH], 298 SHA512_CTX *sha); 299 300 // SHA512_256 writes the digest of |len| bytes from |data| to |out| and returns 301 // |out|. There must be at least |SHA512_256_DIGEST_LENGTH| bytes of space in 302 // |out|. 303 OPENSSL_EXPORT uint8_t *SHA512_256(const uint8_t *data, size_t len, 304 uint8_t out[SHA512_256_DIGEST_LENGTH]); 305 306 307 #if defined(__cplusplus) 308 } // extern C 309 #endif 310 311 #endif // OPENSSL_HEADER_SHA_H 312