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