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