| 1 |
// Copyright (C) 1995-1998 Eric Young ([email protected]) All rights reserved. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
#ifndef OPENSSL_HEADER_SHA_H |
| 5 |
#define OPENSSL_HEADER_SHA_H |
| 6 |
|
| 7 |
#include <openssl/base.h> |
| 8 |
|
| 9 |
#if defined(__cplusplus) |
| 10 |
extern "C" { |
| 11 |
#endif |
| 12 |
|
| 13 |
|
| 14 |
// The SHA family of hash functions (SHA-1 and SHA-2). |
| 15 |
|
| 16 |
|
| 17 |
// SHA_CBLOCK is the block size of SHA-1. |
| 18 |
#define SHA_CBLOCK 64 |
| 19 |
|
| 20 |
// SHA_DIGEST_LENGTH is the length of a SHA-1 digest. |
| 21 |
#define SHA_DIGEST_LENGTH 20 |
| 22 |
|
| 23 |
// SHA1_Init initialises |sha| and returns one. |
| 24 |
OPENSSL_EXPORT int SHA1_Init(SHA_CTX *sha); |
| 25 |
|
| 26 |
// SHA1_Update adds |len| bytes from |data| to |sha| and returns one. |
| 27 |
OPENSSL_EXPORT int SHA1_Update(SHA_CTX *sha, const void *data, size_t len); |
| 28 |
|
| 29 |
// SHA1_Final adds the final padding to |sha| and writes the resulting digest to |
| 30 |
// |out|, which must have at least |SHA_DIGEST_LENGTH| bytes of space. It |
| 31 |
// returns one. |
| 32 |
OPENSSL_EXPORT int SHA1_Final(uint8_t out[SHA_DIGEST_LENGTH], SHA_CTX *sha); |
| 33 |
|
| 34 |
// SHA1 writes the digest of |len| bytes from |data| to |out| and returns |
| 35 |
// |out|. There must be at least |SHA_DIGEST_LENGTH| bytes of space in |
| 36 |
// |out|. |
| 37 |
OPENSSL_EXPORT uint8_t *SHA1(const uint8_t *data, size_t len, |
| 38 |
uint8_t out[SHA_DIGEST_LENGTH]); |
| 39 |
|
| 40 |
// SHA1_Transform is a low-level function that performs a single, SHA-1 block |
| 41 |
// transformation using the state from |sha| and |SHA_CBLOCK| bytes from |
| 42 |
// |block|. |
| 43 |
OPENSSL_EXPORT void SHA1_Transform(SHA_CTX *sha, |
| 44 |
const uint8_t block[SHA_CBLOCK]); |
| 45 |
|
| 46 |
struct sha_state_st { |
| 47 |
uint32_t h[5]; |
| 48 |
uint32_t Nl, Nh; |
| 49 |
uint8_t data[SHA_CBLOCK]; |
| 50 |
unsigned num; |
| 51 |
}; |
| 52 |
|
| 53 |
|
| 54 |
// SHA-224. |
| 55 |
|
| 56 |
// SHA224_CBLOCK is the block size of SHA-224. |
| 57 |
#define SHA224_CBLOCK 64 |
| 58 |
|
| 59 |
// SHA224_DIGEST_LENGTH is the length of a SHA-224 digest. |
| 60 |
#define SHA224_DIGEST_LENGTH 28 |
| 61 |
|
| 62 |
// SHA224_Init initialises |sha| and returns 1. |
| 63 |
OPENSSL_EXPORT int SHA224_Init(SHA256_CTX *sha); |
| 64 |
|
| 65 |
// SHA224_Update adds |len| bytes from |data| to |sha| and returns 1. |
| 66 |
OPENSSL_EXPORT int SHA224_Update(SHA256_CTX *sha, const void *data, size_t len); |
| 67 |
|
| 68 |
// SHA224_Final adds the final padding to |sha| and writes the resulting digest |
| 69 |
// to |out|, which must have at least |SHA224_DIGEST_LENGTH| bytes of space. It |
| 70 |
// returns one on success and zero on programmer error. |
| 71 |
OPENSSL_EXPORT int SHA224_Final(uint8_t out[SHA224_DIGEST_LENGTH], |
| 72 |
SHA256_CTX *sha); |
| 73 |
|
| 74 |
// SHA224 writes the digest of |len| bytes from |data| to |out| and returns |
| 75 |
// |out|. There must be at least |SHA224_DIGEST_LENGTH| bytes of space in |
| 76 |
// |out|. |
| 77 |
OPENSSL_EXPORT uint8_t *SHA224(const uint8_t *data, size_t len, |
| 78 |
uint8_t out[SHA224_DIGEST_LENGTH]); |
| 79 |
|
| 80 |
|
| 81 |
// SHA-256. |
| 82 |
|
| 83 |
// SHA256_CBLOCK is the block size of SHA-256. |
| 84 |
#define SHA256_CBLOCK 64 |
| 85 |
|
| 86 |
// SHA256_DIGEST_LENGTH is the length of a SHA-256 digest. |
| 87 |
#define SHA256_DIGEST_LENGTH 32 |
| 88 |
|
| 89 |
// SHA256_Init initialises |sha| and returns 1. |
| 90 |
OPENSSL_EXPORT int SHA256_Init(SHA256_CTX *sha); |
| 91 |
|
| 92 |
// SHA256_Update adds |len| bytes from |data| to |sha| and returns 1. |
| 93 |
OPENSSL_EXPORT int SHA256_Update(SHA256_CTX *sha, const void *data, size_t len); |
| 94 |
|
| 95 |
// SHA256_Final adds the final padding to |sha| and writes the resulting digest |
| 96 |
// to |out|, which must have at least |SHA256_DIGEST_LENGTH| bytes of space. It |
| 97 |
// returns one on success and zero on programmer error. |
| 98 |
OPENSSL_EXPORT int SHA256_Final(uint8_t out[SHA256_DIGEST_LENGTH], |
| 99 |
SHA256_CTX *sha); |
| 100 |
|
| 101 |
// SHA256 writes the digest of |len| bytes from |data| to |out| and returns |
| 102 |
// |out|. There must be at least |SHA256_DIGEST_LENGTH| bytes of space in |
| 103 |
// |out|. |
| 104 |
OPENSSL_EXPORT uint8_t *SHA256(const uint8_t *data, size_t len, |
| 105 |
uint8_t out[SHA256_DIGEST_LENGTH]); |
| 106 |
|
| 107 |
// SHA256_Transform is a low-level function that performs a single, SHA-256 |
| 108 |
// block transformation using the state from |sha| and |SHA256_CBLOCK| bytes |
| 109 |
// from |block|. |
| 110 |
OPENSSL_EXPORT void SHA256_Transform(SHA256_CTX *sha, |
| 111 |
const uint8_t block[SHA256_CBLOCK]); |
| 112 |
|
| 113 |
// SHA256_TransformBlocks is a low-level function that takes |num_blocks| * |
| 114 |
// |SHA256_CBLOCK| bytes of data and performs SHA-256 transforms on it to update |
| 115 |
// |state|. You should not use this function unless you are implementing a |
| 116 |
// derivative of SHA-256. |
| 117 |
OPENSSL_EXPORT void SHA256_TransformBlocks(uint32_t state[8], |
| 118 |
const uint8_t *data, |
| 119 |
size_t num_blocks); |
| 120 |
|
| 121 |
struct sha256_state_st { |
| 122 |
uint32_t h[8]; |
| 123 |
uint32_t Nl, Nh; |
| 124 |
uint8_t data[SHA256_CBLOCK]; |
| 125 |
unsigned num, md_len; |
| 126 |
}; |
| 127 |
|
| 128 |
|
| 129 |
// SHA-384. |
| 130 |
|
| 131 |
// SHA384_CBLOCK is the block size of SHA-384. |
| 132 |
#define SHA384_CBLOCK 128 |
| 133 |
|
| 134 |
// SHA384_DIGEST_LENGTH is the length of a SHA-384 digest. |
| 135 |
#define SHA384_DIGEST_LENGTH 48 |
| 136 |
|
| 137 |
// SHA384_Init initialises |sha| and returns 1. |
| 138 |
OPENSSL_EXPORT int SHA384_Init(SHA512_CTX *sha); |
| 139 |
|
| 140 |
// SHA384_Update adds |len| bytes from |data| to |sha| and returns 1. |
| 141 |
OPENSSL_EXPORT int SHA384_Update(SHA512_CTX *sha, const void *data, size_t len); |
| 142 |
|
| 143 |
// SHA384_Final adds the final padding to |sha| and writes the resulting digest |
| 144 |
// to |out|, which must have at least |SHA384_DIGEST_LENGTH| bytes of space. It |
| 145 |
// returns one on success and zero on programmer error. |
| 146 |
OPENSSL_EXPORT int SHA384_Final(uint8_t out[SHA384_DIGEST_LENGTH], |
| 147 |
SHA512_CTX *sha); |
| 148 |
|
| 149 |
// SHA384 writes the digest of |len| bytes from |data| to |out| and returns |
| 150 |
// |out|. There must be at least |SHA384_DIGEST_LENGTH| bytes of space in |
| 151 |
// |out|. |
| 152 |
OPENSSL_EXPORT uint8_t *SHA384(const uint8_t *data, size_t len, |
| 153 |
uint8_t out[SHA384_DIGEST_LENGTH]); |
| 154 |
|
| 155 |
|
| 156 |
// SHA-512. |
| 157 |
|
| 158 |
// SHA512_CBLOCK is the block size of SHA-512. |
| 159 |
#define SHA512_CBLOCK 128 |
| 160 |
|
| 161 |
// SHA512_DIGEST_LENGTH is the length of a SHA-512 digest. |
| 162 |
#define SHA512_DIGEST_LENGTH 64 |
| 163 |
|
| 164 |
// SHA512_Init initialises |sha| and returns 1. |
| 165 |
OPENSSL_EXPORT int SHA512_Init(SHA512_CTX *sha); |
| 166 |
|
| 167 |
// SHA512_Update adds |len| bytes from |data| to |sha| and returns 1. |
| 168 |
OPENSSL_EXPORT int SHA512_Update(SHA512_CTX *sha, const void *data, size_t len); |
| 169 |
|
| 170 |
// SHA512_Final adds the final padding to |sha| and writes the resulting digest |
| 171 |
// to |out|, which must have at least |SHA512_DIGEST_LENGTH| bytes of space. It |
| 172 |
// returns one on success and zero on programmer error. |
| 173 |
OPENSSL_EXPORT int SHA512_Final(uint8_t out[SHA512_DIGEST_LENGTH], |
| 174 |
SHA512_CTX *sha); |
| 175 |
|
| 176 |
// SHA512 writes the digest of |len| bytes from |data| to |out| and returns |
| 177 |
// |out|. There must be at least |SHA512_DIGEST_LENGTH| bytes of space in |
| 178 |
// |out|. |
| 179 |
OPENSSL_EXPORT uint8_t *SHA512(const uint8_t *data, size_t len, |
| 180 |
uint8_t out[SHA512_DIGEST_LENGTH]); |
| 181 |
|
| 182 |
// SHA512_Transform is a low-level function that performs a single, SHA-512 |
| 183 |
// block transformation using the state from |sha| and |SHA512_CBLOCK| bytes |
| 184 |
// from |block|. |
| 185 |
OPENSSL_EXPORT void SHA512_Transform(SHA512_CTX *sha, |
| 186 |
const uint8_t block[SHA512_CBLOCK]); |
| 187 |
|
| 188 |
struct sha512_state_st { |
| 189 |
uint64_t h[8]; |
| 190 |
uint64_t Nl, Nh; |
| 191 |
uint8_t p[128]; |
| 192 |
unsigned num, md_len; |
| 193 |
}; |
| 194 |
|
| 195 |
|
| 196 |
// SHA-512-224 and 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_224_DIGEST_LENGTH 28 |
| 201 |
|
| 202 |
// SHA512_224_Init initialises |sha| and returns 1. |
| 203 |
OPENSSL_EXPORT int SHA512_224_Init(SHA512_CTX *sha); |
| 204 |
|
| 205 |
// SHA512_224_Update adds |len| bytes from |data| to |sha| and returns 1. |
| 206 |
OPENSSL_EXPORT int SHA512_224_Update(SHA512_CTX *sha, const void *data, |
| 207 |
size_t len); |
| 208 |
|
| 209 |
// SHA512_224_Final adds the final padding to |sha| and writes the resulting |
| 210 |
// digest to |out|, which must have at least |SHA512_224_DIGEST_LENGTH| bytes of |
| 211 |
// space. It returns one on success and zero on programmer error. |
| 212 |
OPENSSL_EXPORT int SHA512_224_Final(uint8_t out[SHA512_224_DIGEST_LENGTH], |
| 213 |
SHA512_CTX *sha); |
| 214 |
|
| 215 |
// SHA512_224 writes the digest of |len| bytes from |data| to |out| and returns |
| 216 |
// |out|. There must be at least |SHA512_224_DIGEST_LENGTH| bytes of space in |
| 217 |
// |out|. |
| 218 |
OPENSSL_EXPORT uint8_t *SHA512_224(const uint8_t *data, size_t len, |
| 219 |
uint8_t out[SHA512_224_DIGEST_LENGTH]); |
| 220 |
|
| 221 |
#define SHA512_256_DIGEST_LENGTH 32 |
| 222 |
|
| 223 |
// SHA512_256_Init initialises |sha| and returns 1. |
| 224 |
OPENSSL_EXPORT int SHA512_256_Init(SHA512_CTX *sha); |
| 225 |
|
| 226 |
// SHA512_256_Update adds |len| bytes from |data| to |sha| and returns 1. |
| 227 |
OPENSSL_EXPORT int SHA512_256_Update(SHA512_CTX *sha, const void *data, |
| 228 |
size_t len); |
| 229 |
|
| 230 |
// SHA512_256_Final adds the final padding to |sha| and writes the resulting |
| 231 |
// digest to |out|, which must have at least |SHA512_256_DIGEST_LENGTH| bytes of |
| 232 |
// space. It returns one on success and zero on programmer error. |
| 233 |
OPENSSL_EXPORT int SHA512_256_Final(uint8_t out[SHA512_256_DIGEST_LENGTH], |
| 234 |
SHA512_CTX *sha); |
| 235 |
|
| 236 |
// SHA512_256 writes the digest of |len| bytes from |data| to |out| and returns |
| 237 |
// |out|. There must be at least |SHA512_256_DIGEST_LENGTH| bytes of space in |
| 238 |
// |out|. |
| 239 |
OPENSSL_EXPORT uint8_t *SHA512_256(const uint8_t *data, size_t len, |
| 240 |
uint8_t out[SHA512_256_DIGEST_LENGTH]); |
| 241 |
|
| 242 |
|
| 243 |
#if defined(__cplusplus) |
| 244 |
} // extern C |
| 245 |
#endif |
| 246 |
|
| 247 |
#endif // OPENSSL_HEADER_SHA_H |