sha.h (9371B)
1 // Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #ifndef OPENSSL_HEADER_SHA_H 16 #define OPENSSL_HEADER_SHA_H 17 18 #include <openssl/base.h> // IWYU pragma: export 19 #include <openssl/bcm_public.h> // IWYU pragma: export 20 21 #if defined(__cplusplus) 22 extern "C" { 23 #endif 24 25 26 // The SHA family of hash functions (SHA-1 and SHA-2). 27 28 29 // SHA-1. 30 31 // SHA_CBLOCK is the block size of SHA-1. 32 #define SHA_CBLOCK 64 33 34 // SHA_DIGEST_LENGTH is the length of a SHA-1 digest. 35 #define SHA_DIGEST_LENGTH 20 36 37 // SHA1_Init initialises |sha| and returns one. 38 OPENSSL_EXPORT int SHA1_Init(SHA_CTX *sha); 39 40 // SHA1_Update adds |len| bytes from |data| to |sha| and returns one. 41 OPENSSL_EXPORT int SHA1_Update(SHA_CTX *sha, const void *data, size_t len); 42 43 // SHA1_Final adds the final padding to |sha| and writes the resulting digest to 44 // |out|, which must have at least |SHA_DIGEST_LENGTH| bytes of space. It 45 // returns one. 46 OPENSSL_EXPORT int SHA1_Final(uint8_t out[SHA_DIGEST_LENGTH], SHA_CTX *sha); 47 48 // SHA1 writes the digest of |len| bytes from |data| to |out| and returns 49 // |out|. There must be at least |SHA_DIGEST_LENGTH| bytes of space in 50 // |out|. 51 OPENSSL_EXPORT uint8_t *SHA1(const uint8_t *data, size_t len, 52 uint8_t out[SHA_DIGEST_LENGTH]); 53 54 // SHA1_Transform is a low-level function that performs a single, SHA-1 block 55 // transformation using the state from |sha| and |SHA_CBLOCK| bytes from 56 // |block|. 57 OPENSSL_EXPORT void SHA1_Transform(SHA_CTX *sha, 58 const uint8_t block[SHA_CBLOCK]); 59 60 // CRYPTO_fips_186_2_prf derives |out_len| bytes from |xkey| using the PRF 61 // defined in FIPS 186-2, Appendix 3.1, with change notice 1 applied. The b 62 // parameter is 160 and seed, XKEY, is also 160 bits. The optional XSEED user 63 // input is all zeros. 64 // 65 // The PRF generates a sequence of 320-bit numbers. Each number is encoded as a 66 // 40-byte string in big-endian and then concatenated to form |out|. If 67 // |out_len| is not a multiple of 40, the result is truncated. This matches the 68 // construction used in Section 7 of RFC 4186 and Section 7 of RFC 4187. 69 // 70 // This PRF is based on SHA-1, a weak hash function, and should not be used 71 // in new protocols. It is provided for compatibility with some legacy EAP 72 // methods. 73 OPENSSL_EXPORT void CRYPTO_fips_186_2_prf( 74 uint8_t *out, size_t out_len, const uint8_t xkey[SHA_DIGEST_LENGTH]); 75 76 77 // SHA-224. 78 79 // SHA224_CBLOCK is the block size of SHA-224. 80 #define SHA224_CBLOCK 64 81 82 // SHA224_DIGEST_LENGTH is the length of a SHA-224 digest. 83 #define SHA224_DIGEST_LENGTH 28 84 85 // SHA224_Init initialises |sha| and returns 1. 86 OPENSSL_EXPORT int SHA224_Init(SHA256_CTX *sha); 87 88 // SHA224_Update adds |len| bytes from |data| to |sha| and returns 1. 89 OPENSSL_EXPORT int SHA224_Update(SHA256_CTX *sha, const void *data, size_t len); 90 91 // SHA224_Final adds the final padding to |sha| and writes the resulting digest 92 // to |out|, which must have at least |SHA224_DIGEST_LENGTH| bytes of space. It 93 // returns 1. 94 OPENSSL_EXPORT int SHA224_Final(uint8_t out[SHA224_DIGEST_LENGTH], 95 SHA256_CTX *sha); 96 97 // SHA224 writes the digest of |len| bytes from |data| to |out| and returns 98 // |out|. There must be at least |SHA224_DIGEST_LENGTH| bytes of space in 99 // |out|. 100 OPENSSL_EXPORT uint8_t *SHA224(const uint8_t *data, size_t len, 101 uint8_t out[SHA224_DIGEST_LENGTH]); 102 103 104 // SHA-256. 105 106 // SHA256_CBLOCK is the block size of SHA-256. 107 #define SHA256_CBLOCK 64 108 109 // SHA256_DIGEST_LENGTH is the length of a SHA-256 digest. 110 #define SHA256_DIGEST_LENGTH 32 111 112 // SHA256_Init initialises |sha| and returns 1. 113 OPENSSL_EXPORT int SHA256_Init(SHA256_CTX *sha); 114 115 // SHA256_Update adds |len| bytes from |data| to |sha| and returns 1. 116 OPENSSL_EXPORT int SHA256_Update(SHA256_CTX *sha, const void *data, size_t len); 117 118 // SHA256_Final adds the final padding to |sha| and writes the resulting digest 119 // to |out|, which must have at least |SHA256_DIGEST_LENGTH| bytes of space. It 120 // returns one on success and zero on programmer error. 121 OPENSSL_EXPORT int SHA256_Final(uint8_t out[SHA256_DIGEST_LENGTH], 122 SHA256_CTX *sha); 123 124 // SHA256 writes the digest of |len| bytes from |data| to |out| and returns 125 // |out|. There must be at least |SHA256_DIGEST_LENGTH| bytes of space in 126 // |out|. 127 OPENSSL_EXPORT uint8_t *SHA256(const uint8_t *data, size_t len, 128 uint8_t out[SHA256_DIGEST_LENGTH]); 129 130 // SHA256_Transform is a low-level function that performs a single, SHA-256 131 // block transformation using the state from |sha| and |SHA256_CBLOCK| bytes 132 // from |block|. 133 OPENSSL_EXPORT void SHA256_Transform(SHA256_CTX *sha, 134 const uint8_t block[SHA256_CBLOCK]); 135 136 // SHA256_TransformBlocks is a low-level function that takes |num_blocks| * 137 // |SHA256_CBLOCK| bytes of data and performs SHA-256 transforms on it to update 138 // |state|. You should not use this function unless you are implementing a 139 // derivative of SHA-256. 140 OPENSSL_EXPORT void SHA256_TransformBlocks(uint32_t state[8], 141 const uint8_t *data, 142 size_t num_blocks); 143 144 145 // SHA-384. 146 147 // SHA384_CBLOCK is the block size of SHA-384. 148 #define SHA384_CBLOCK 128 149 150 // SHA384_DIGEST_LENGTH is the length of a SHA-384 digest. 151 #define SHA384_DIGEST_LENGTH 48 152 153 // SHA384_Init initialises |sha| and returns 1. 154 OPENSSL_EXPORT int SHA384_Init(SHA512_CTX *sha); 155 156 // SHA384_Update adds |len| bytes from |data| to |sha| and returns 1. 157 OPENSSL_EXPORT int SHA384_Update(SHA512_CTX *sha, const void *data, size_t len); 158 159 // SHA384_Final adds the final padding to |sha| and writes the resulting digest 160 // to |out|, which must have at least |SHA384_DIGEST_LENGTH| bytes of space. It 161 // returns one on success and zero on programmer error. 162 OPENSSL_EXPORT int SHA384_Final(uint8_t out[SHA384_DIGEST_LENGTH], 163 SHA512_CTX *sha); 164 165 // SHA384 writes the digest of |len| bytes from |data| to |out| and returns 166 // |out|. There must be at least |SHA384_DIGEST_LENGTH| bytes of space in 167 // |out|. 168 OPENSSL_EXPORT uint8_t *SHA384(const uint8_t *data, size_t len, 169 uint8_t out[SHA384_DIGEST_LENGTH]); 170 171 172 // SHA-512. 173 174 // SHA512_CBLOCK is the block size of SHA-512. 175 #define SHA512_CBLOCK 128 176 177 // SHA512_DIGEST_LENGTH is the length of a SHA-512 digest. 178 #define SHA512_DIGEST_LENGTH 64 179 180 // SHA512_Init initialises |sha| and returns 1. 181 OPENSSL_EXPORT int SHA512_Init(SHA512_CTX *sha); 182 183 // SHA512_Update adds |len| bytes from |data| to |sha| and returns 1. 184 OPENSSL_EXPORT int SHA512_Update(SHA512_CTX *sha, const void *data, size_t len); 185 186 // SHA512_Final adds the final padding to |sha| and writes the resulting digest 187 // to |out|, which must have at least |SHA512_DIGEST_LENGTH| bytes of space. It 188 // returns one on success and zero on programmer error. 189 OPENSSL_EXPORT int SHA512_Final(uint8_t out[SHA512_DIGEST_LENGTH], 190 SHA512_CTX *sha); 191 192 // SHA512 writes the digest of |len| bytes from |data| to |out| and returns 193 // |out|. There must be at least |SHA512_DIGEST_LENGTH| bytes of space in 194 // |out|. 195 OPENSSL_EXPORT uint8_t *SHA512(const uint8_t *data, size_t len, 196 uint8_t out[SHA512_DIGEST_LENGTH]); 197 198 // SHA512_Transform is a low-level function that performs a single, SHA-512 199 // block transformation using the state from |sha| and |SHA512_CBLOCK| bytes 200 // from |block|. 201 OPENSSL_EXPORT void SHA512_Transform(SHA512_CTX *sha, 202 const uint8_t block[SHA512_CBLOCK]); 203 204 205 // SHA-512-256 206 // 207 // See https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf section 5.3.6 208 209 #define SHA512_256_DIGEST_LENGTH 32 210 211 // SHA512_256_Init initialises |sha| and returns 1. 212 OPENSSL_EXPORT int SHA512_256_Init(SHA512_CTX *sha); 213 214 // SHA512_256_Update adds |len| bytes from |data| to |sha| and returns 1. 215 OPENSSL_EXPORT int SHA512_256_Update(SHA512_CTX *sha, const void *data, 216 size_t len); 217 218 // SHA512_256_Final adds the final padding to |sha| and writes the resulting 219 // digest to |out|, which must have at least |SHA512_256_DIGEST_LENGTH| bytes of 220 // space. It returns one on success and zero on programmer error. 221 OPENSSL_EXPORT int SHA512_256_Final(uint8_t out[SHA512_256_DIGEST_LENGTH], 222 SHA512_CTX *sha); 223 224 // SHA512_256 writes the digest of |len| bytes from |data| to |out| and returns 225 // |out|. There must be at least |SHA512_256_DIGEST_LENGTH| bytes of space in 226 // |out|. 227 OPENSSL_EXPORT uint8_t *SHA512_256(const uint8_t *data, size_t len, 228 uint8_t out[SHA512_256_DIGEST_LENGTH]); 229 230 231 #if defined(__cplusplus) 232 } // extern C 233 #endif 234 235 #endif // OPENSSL_HEADER_SHA_H