1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2 * All rights reserved. 3 * 4 * This package is an SSL implementation written 5 * by Eric Young (eay@cryptsoft.com). 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 (tjh@cryptsoft.com). 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 (eay@cryptsoft.com)" 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 (tjh@cryptsoft.com)" 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 #include <openssl/bcm_public.h> // IWYU pragma: export 62 63 #if defined(__cplusplus) 64 extern "C" { 65 #endif 66 67 68 // The SHA family of hash functions (SHA-1 and SHA-2). 69 70 71 // SHA_CBLOCK is the block size of SHA-1. 72 #define SHA_CBLOCK 64 73 74 // SHA_DIGEST_LENGTH is the length of a SHA-1 digest. 75 #define SHA_DIGEST_LENGTH 20 76 77 // SHA1_Init initialises |sha| and returns one. 78 OPENSSL_EXPORT int SHA1_Init(SHA_CTX *sha); 79 80 // SHA1_Update adds |len| bytes from |data| to |sha| and returns one. 81 OPENSSL_EXPORT int SHA1_Update(SHA_CTX *sha, const void *data, size_t len); 82 83 // SHA1_Final adds the final padding to |sha| and writes the resulting digest to 84 // |out|, which must have at least |SHA_DIGEST_LENGTH| bytes of space. It 85 // returns one. 86 OPENSSL_EXPORT int SHA1_Final(uint8_t out[SHA_DIGEST_LENGTH], SHA_CTX *sha); 87 88 // SHA1 writes the digest of |len| bytes from |data| to |out| and returns 89 // |out|. There must be at least |SHA_DIGEST_LENGTH| bytes of space in 90 // |out|. 91 OPENSSL_EXPORT uint8_t *SHA1(const uint8_t *data, size_t len, 92 uint8_t out[SHA_DIGEST_LENGTH]); 93 94 // SHA1_Transform is a low-level function that performs a single, SHA-1 block 95 // transformation using the state from |sha| and |SHA_CBLOCK| bytes from 96 // |block|. 97 OPENSSL_EXPORT void SHA1_Transform(SHA_CTX *sha, 98 const uint8_t block[SHA_CBLOCK]); 99 100 // CRYPTO_fips_186_2_prf derives |out_len| bytes from |xkey| using the PRF 101 // defined in FIPS 186-2, Appendix 3.1, with change notice 1 applied. The b 102 // parameter is 160 and seed, XKEY, is also 160 bits. The optional XSEED user 103 // input is all zeros. 104 // 105 // The PRF generates a sequence of 320-bit numbers. Each number is encoded as a 106 // 40-byte string in big-endian and then concatenated to form |out|. If 107 // |out_len| is not a multiple of 40, the result is truncated. This matches the 108 // construction used in Section 7 of RFC 4186 and Section 7 of RFC 4187. 109 // 110 // This PRF is based on SHA-1, a weak hash function, and should not be used 111 // in new protocols. It is provided for compatibility with some legacy EAP 112 // methods. 113 OPENSSL_EXPORT void CRYPTO_fips_186_2_prf( 114 uint8_t *out, size_t out_len, const uint8_t xkey[SHA_DIGEST_LENGTH]); 115 116 117 // SHA-224. 118 119 // SHA224_CBLOCK is the block size of SHA-224. 120 #define SHA224_CBLOCK 64 121 122 // SHA224_DIGEST_LENGTH is the length of a SHA-224 digest. 123 #define SHA224_DIGEST_LENGTH 28 124 125 // SHA224_Init initialises |sha| and returns 1. 126 OPENSSL_EXPORT int SHA224_Init(SHA256_CTX *sha); 127 128 // SHA224_Update adds |len| bytes from |data| to |sha| and returns 1. 129 OPENSSL_EXPORT int SHA224_Update(SHA256_CTX *sha, const void *data, size_t len); 130 131 // SHA224_Final adds the final padding to |sha| and writes the resulting digest 132 // to |out|, which must have at least |SHA224_DIGEST_LENGTH| bytes of space. It 133 // returns 1. 134 OPENSSL_EXPORT int SHA224_Final(uint8_t out[SHA224_DIGEST_LENGTH], 135 SHA256_CTX *sha); 136 137 // SHA224 writes the digest of |len| bytes from |data| to |out| and returns 138 // |out|. There must be at least |SHA224_DIGEST_LENGTH| bytes of space in 139 // |out|. 140 OPENSSL_EXPORT uint8_t *SHA224(const uint8_t *data, size_t len, 141 uint8_t out[SHA224_DIGEST_LENGTH]); 142 143 144 // SHA-256. 145 146 // SHA256_CBLOCK is the block size of SHA-256. 147 #define SHA256_CBLOCK 64 148 149 // SHA256_DIGEST_LENGTH is the length of a SHA-256 digest. 150 #define SHA256_DIGEST_LENGTH 32 151 152 // SHA256_Init initialises |sha| and returns 1. 153 OPENSSL_EXPORT int SHA256_Init(SHA256_CTX *sha); 154 155 // SHA256_Update adds |len| bytes from |data| to |sha| and returns 1. 156 OPENSSL_EXPORT int SHA256_Update(SHA256_CTX *sha, const void *data, size_t len); 157 158 // SHA256_Final adds the final padding to |sha| and writes the resulting digest 159 // to |out|, which must have at least |SHA256_DIGEST_LENGTH| bytes of space. It 160 // returns one on success and zero on programmer error. 161 OPENSSL_EXPORT int SHA256_Final(uint8_t out[SHA256_DIGEST_LENGTH], 162 SHA256_CTX *sha); 163 164 // SHA256 writes the digest of |len| bytes from |data| to |out| and returns 165 // |out|. There must be at least |SHA256_DIGEST_LENGTH| bytes of space in 166 // |out|. 167 OPENSSL_EXPORT uint8_t *SHA256(const uint8_t *data, size_t len, 168 uint8_t out[SHA256_DIGEST_LENGTH]); 169 170 // SHA256_Transform is a low-level function that performs a single, SHA-256 171 // block transformation using the state from |sha| and |SHA256_CBLOCK| bytes 172 // from |block|. 173 OPENSSL_EXPORT void SHA256_Transform(SHA256_CTX *sha, 174 const uint8_t block[SHA256_CBLOCK]); 175 176 // SHA256_TransformBlocks is a low-level function that takes |num_blocks| * 177 // |SHA256_CBLOCK| bytes of data and performs SHA-256 transforms on it to update 178 // |state|. You should not use this function unless you are implementing a 179 // derivative of SHA-256. 180 OPENSSL_EXPORT void SHA256_TransformBlocks(uint32_t state[8], 181 const uint8_t *data, 182 size_t num_blocks); 183 184 // SHA-384. 185 186 // SHA384_CBLOCK is the block size of SHA-384. 187 #define SHA384_CBLOCK 128 188 189 // SHA384_DIGEST_LENGTH is the length of a SHA-384 digest. 190 #define SHA384_DIGEST_LENGTH 48 191 192 // SHA384_Init initialises |sha| and returns 1. 193 OPENSSL_EXPORT int SHA384_Init(SHA512_CTX *sha); 194 195 // SHA384_Update adds |len| bytes from |data| to |sha| and returns 1. 196 OPENSSL_EXPORT int SHA384_Update(SHA512_CTX *sha, const void *data, size_t len); 197 198 // SHA384_Final adds the final padding to |sha| and writes the resulting digest 199 // to |out|, which must have at least |SHA384_DIGEST_LENGTH| bytes of space. It 200 // returns one on success and zero on programmer error. 201 OPENSSL_EXPORT int SHA384_Final(uint8_t out[SHA384_DIGEST_LENGTH], 202 SHA512_CTX *sha); 203 204 // SHA384 writes the digest of |len| bytes from |data| to |out| and returns 205 // |out|. There must be at least |SHA384_DIGEST_LENGTH| bytes of space in 206 // |out|. 207 OPENSSL_EXPORT uint8_t *SHA384(const uint8_t *data, size_t len, 208 uint8_t out[SHA384_DIGEST_LENGTH]); 209 210 211 // SHA-512. 212 213 // SHA512_CBLOCK is the block size of SHA-512. 214 #define SHA512_CBLOCK 128 215 216 // SHA512_DIGEST_LENGTH is the length of a SHA-512 digest. 217 #define SHA512_DIGEST_LENGTH 64 218 219 // SHA512_Init initialises |sha| and returns 1. 220 OPENSSL_EXPORT int SHA512_Init(SHA512_CTX *sha); 221 222 // SHA512_Update adds |len| bytes from |data| to |sha| and returns 1. 223 OPENSSL_EXPORT int SHA512_Update(SHA512_CTX *sha, const void *data, size_t len); 224 225 // SHA512_Final adds the final padding to |sha| and writes the resulting digest 226 // to |out|, which must have at least |SHA512_DIGEST_LENGTH| bytes of space. It 227 // returns one on success and zero on programmer error. 228 OPENSSL_EXPORT int SHA512_Final(uint8_t out[SHA512_DIGEST_LENGTH], 229 SHA512_CTX *sha); 230 231 // SHA512 writes the digest of |len| bytes from |data| to |out| and returns 232 // |out|. There must be at least |SHA512_DIGEST_LENGTH| bytes of space in 233 // |out|. 234 OPENSSL_EXPORT uint8_t *SHA512(const uint8_t *data, size_t len, 235 uint8_t out[SHA512_DIGEST_LENGTH]); 236 237 // SHA512_Transform is a low-level function that performs a single, SHA-512 238 // block transformation using the state from |sha| and |SHA512_CBLOCK| bytes 239 // from |block|. 240 OPENSSL_EXPORT void SHA512_Transform(SHA512_CTX *sha, 241 const uint8_t block[SHA512_CBLOCK]); 242 243 // SHA-512-256 244 // 245 // See https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf section 5.3.6 246 247 #define SHA512_256_DIGEST_LENGTH 32 248 249 // SHA512_256_Init initialises |sha| and returns 1. 250 OPENSSL_EXPORT int SHA512_256_Init(SHA512_CTX *sha); 251 252 // SHA512_256_Update adds |len| bytes from |data| to |sha| and returns 1. 253 OPENSSL_EXPORT int SHA512_256_Update(SHA512_CTX *sha, const void *data, 254 size_t len); 255 256 // SHA512_256_Final adds the final padding to |sha| and writes the resulting 257 // digest to |out|, which must have at least |SHA512_256_DIGEST_LENGTH| bytes of 258 // space. It returns one on success and zero on programmer error. 259 OPENSSL_EXPORT int SHA512_256_Final(uint8_t out[SHA512_256_DIGEST_LENGTH], 260 SHA512_CTX *sha); 261 262 // SHA512_256 writes the digest of |len| bytes from |data| to |out| and returns 263 // |out|. There must be at least |SHA512_256_DIGEST_LENGTH| bytes of space in 264 // |out|. 265 OPENSSL_EXPORT uint8_t *SHA512_256(const uint8_t *data, size_t len, 266 uint8_t out[SHA512_256_DIGEST_LENGTH]); 267 268 269 #if defined(__cplusplus) 270 } // extern C 271 #endif 272 273 #endif // OPENSSL_HEADER_SHA_H 274