Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 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 | |
| 62 | #if defined(__cplusplus) |
| 63 | extern "C" { |
| 64 | #endif |
| 65 | |
| 66 | |
David Benjamin | 5b082e8 | 2014-12-26 00:54:52 -0500 | [diff] [blame] | 67 | /* The SHA family of hash functions (SHA-1 and SHA-2). */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 68 | |
| 69 | |
| 70 | /* SHA_CBLOCK is the block size of SHA-1. */ |
| 71 | #define SHA_CBLOCK 64 |
| 72 | |
| 73 | /* SHA_DIGEST_LENGTH is the length of a SHA-1 digest. */ |
| 74 | #define SHA_DIGEST_LENGTH 20 |
| 75 | |
| 76 | /* TODO(fork): remove */ |
| 77 | #define SHA_LBLOCK 16 |
| 78 | #define SHA_LONG uint32_t |
| 79 | |
| 80 | /* SHA1_Init initialises |sha| and returns one. */ |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 81 | OPENSSL_EXPORT int SHA1_Init(SHA_CTX *sha); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 82 | |
| 83 | /* SHA1_Update adds |len| bytes from |data| to |sha| and returns one. */ |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 84 | OPENSSL_EXPORT int SHA1_Update(SHA_CTX *sha, const void *data, size_t len); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 85 | |
| 86 | /* SHA1_Final adds the final padding to |sha| and writes the resulting digest |
| 87 | * to |md|, which must have at least |SHA_DIGEST_LENGTH| bytes of space. It |
| 88 | * returns one. */ |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 89 | OPENSSL_EXPORT int SHA1_Final(uint8_t *md, SHA_CTX *sha); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 90 | |
| 91 | /* SHA1 writes the digest of |len| bytes from |data| to |out| and returns |
| 92 | * |out|. There must be at least |SHA_DIGEST_LENGTH| bytes of space in |
| 93 | * |out|. */ |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 94 | OPENSSL_EXPORT uint8_t *SHA1(const uint8_t *data, size_t len, uint8_t *out); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 95 | |
| 96 | /* SHA1_Transform is a low-level function that performs a single, SHA-1 block |
| 97 | * transformation using the state from |sha| and 64 bytes from |block|. */ |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 98 | OPENSSL_EXPORT void SHA1_Transform(SHA_CTX *sha, const uint8_t *block); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 99 | |
| 100 | struct sha_state_st { |
Adam Langley | 902870e | 2015-12-04 13:17:57 -0800 | [diff] [blame] | 101 | #if defined(OPENSSL_WINDOWS) |
| 102 | uint32_t h[5]; |
| 103 | #else |
Brian Smith | ac9404c | 2015-11-01 10:13:24 -1000 | [diff] [blame] | 104 | /* wpa_supplicant accesses |h0|..|h4| so we must support those names |
Adam Langley | 902870e | 2015-12-04 13:17:57 -0800 | [diff] [blame] | 105 | * for compatibility with it until it can be updated. */ |
Brian Smith | ac9404c | 2015-11-01 10:13:24 -1000 | [diff] [blame] | 106 | union { |
| 107 | uint32_t h[5]; |
| 108 | struct { |
| 109 | uint32_t h0; |
| 110 | uint32_t h1; |
| 111 | uint32_t h2; |
| 112 | uint32_t h3; |
| 113 | uint32_t h4; |
| 114 | }; |
| 115 | }; |
Adam Langley | 902870e | 2015-12-04 13:17:57 -0800 | [diff] [blame] | 116 | #endif |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 117 | uint32_t Nl, Nh; |
David Benjamin | 0aff3ff | 2015-12-07 13:17:03 -0500 | [diff] [blame] | 118 | uint8_t data[SHA_CBLOCK]; |
David Benjamin | 78fefbf | 2015-12-06 21:48:45 -0500 | [diff] [blame] | 119 | unsigned num; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 120 | }; |
| 121 | |
| 122 | |
| 123 | /* SHA-224. */ |
| 124 | |
| 125 | /* SHA224_CBLOCK is the block size of SHA-224. */ |
| 126 | #define SHA224_CBLOCK 64 |
| 127 | |
| 128 | /* SHA224_DIGEST_LENGTH is the length of a SHA-224 digest. */ |
| 129 | #define SHA224_DIGEST_LENGTH 28 |
| 130 | |
David Benjamin | 862c0aa | 2016-04-27 14:59:12 -0400 | [diff] [blame] | 131 | /* SHA224_Init initialises |sha| and returns 1. */ |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 132 | OPENSSL_EXPORT int SHA224_Init(SHA256_CTX *sha); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 133 | |
David Benjamin | 862c0aa | 2016-04-27 14:59:12 -0400 | [diff] [blame] | 134 | /* SHA224_Update adds |len| bytes from |data| to |sha| and returns 1. */ |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 135 | OPENSSL_EXPORT int SHA224_Update(SHA256_CTX *sha, const void *data, size_t len); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 136 | |
| 137 | /* SHA224_Final adds the final padding to |sha| and writes the resulting digest |
David Benjamin | e2375e1 | 2015-05-27 01:32:26 -0400 | [diff] [blame] | 138 | * to |md|, which must have at least |SHA224_DIGEST_LENGTH| bytes of space. It |
David Benjamin | 862c0aa | 2016-04-27 14:59:12 -0400 | [diff] [blame] | 139 | * returns one on success and zero on programmer error. */ |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 140 | OPENSSL_EXPORT int SHA224_Final(uint8_t *md, SHA256_CTX *sha); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 141 | |
| 142 | /* SHA224 writes the digest of |len| bytes from |data| to |out| and returns |
David Benjamin | 622a6db | 2014-10-06 19:12:30 -0400 | [diff] [blame] | 143 | * |out|. There must be at least |SHA224_DIGEST_LENGTH| bytes of space in |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 144 | * |out|. */ |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 145 | OPENSSL_EXPORT uint8_t *SHA224(const uint8_t *data, size_t len, uint8_t *out); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 146 | |
| 147 | |
| 148 | /* SHA-256. */ |
| 149 | |
| 150 | /* SHA256_CBLOCK is the block size of SHA-256. */ |
| 151 | #define SHA256_CBLOCK 64 |
| 152 | |
| 153 | /* SHA256_DIGEST_LENGTH is the length of a SHA-256 digest. */ |
| 154 | #define SHA256_DIGEST_LENGTH 32 |
| 155 | |
David Benjamin | 862c0aa | 2016-04-27 14:59:12 -0400 | [diff] [blame] | 156 | /* SHA256_Init initialises |sha| and returns 1. */ |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 157 | OPENSSL_EXPORT int SHA256_Init(SHA256_CTX *sha); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 158 | |
David Benjamin | 862c0aa | 2016-04-27 14:59:12 -0400 | [diff] [blame] | 159 | /* SHA256_Update adds |len| bytes from |data| to |sha| and returns 1. */ |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 160 | OPENSSL_EXPORT int SHA256_Update(SHA256_CTX *sha, const void *data, size_t len); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 161 | |
| 162 | /* SHA256_Final adds the final padding to |sha| and writes the resulting digest |
David Benjamin | e2375e1 | 2015-05-27 01:32:26 -0400 | [diff] [blame] | 163 | * to |md|, which must have at least |SHA256_DIGEST_LENGTH| bytes of space. It |
David Benjamin | 862c0aa | 2016-04-27 14:59:12 -0400 | [diff] [blame] | 164 | * returns one on success and zero on programmer error. */ |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 165 | OPENSSL_EXPORT int SHA256_Final(uint8_t *md, SHA256_CTX *sha); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 166 | |
| 167 | /* SHA256 writes the digest of |len| bytes from |data| to |out| and returns |
David Benjamin | 622a6db | 2014-10-06 19:12:30 -0400 | [diff] [blame] | 168 | * |out|. There must be at least |SHA256_DIGEST_LENGTH| bytes of space in |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 169 | * |out|. */ |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 170 | OPENSSL_EXPORT uint8_t *SHA256(const uint8_t *data, size_t len, uint8_t *out); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 171 | |
| 172 | /* SHA256_Transform is a low-level function that performs a single, SHA-1 block |
| 173 | * transformation using the state from |sha| and 64 bytes from |block|. */ |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 174 | OPENSSL_EXPORT void SHA256_Transform(SHA256_CTX *sha, const uint8_t *data); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 175 | |
| 176 | struct sha256_state_st { |
| 177 | uint32_t h[8]; |
| 178 | uint32_t Nl, Nh; |
David Benjamin | 0aff3ff | 2015-12-07 13:17:03 -0500 | [diff] [blame] | 179 | uint8_t data[SHA256_CBLOCK]; |
David Benjamin | 862c0aa | 2016-04-27 14:59:12 -0400 | [diff] [blame] | 180 | unsigned num, md_len; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 181 | }; |
| 182 | |
| 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 | |
David Benjamin | 862c0aa | 2016-04-27 14:59:12 -0400 | [diff] [blame] | 192 | /* SHA384_Init initialises |sha| and returns 1. */ |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 193 | OPENSSL_EXPORT int SHA384_Init(SHA512_CTX *sha); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 194 | |
David Benjamin | 862c0aa | 2016-04-27 14:59:12 -0400 | [diff] [blame] | 195 | /* SHA384_Update adds |len| bytes from |data| to |sha| and returns 1. */ |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 196 | OPENSSL_EXPORT int SHA384_Update(SHA512_CTX *sha, const void *data, size_t len); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 197 | |
| 198 | /* SHA384_Final adds the final padding to |sha| and writes the resulting digest |
David Benjamin | e2375e1 | 2015-05-27 01:32:26 -0400 | [diff] [blame] | 199 | * to |md|, which must have at least |SHA384_DIGEST_LENGTH| bytes of space. It |
David Benjamin | 862c0aa | 2016-04-27 14:59:12 -0400 | [diff] [blame] | 200 | * returns one on success and zero on programmer error. */ |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 201 | OPENSSL_EXPORT int SHA384_Final(uint8_t *md, SHA512_CTX *sha); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 202 | |
| 203 | /* SHA384 writes the digest of |len| bytes from |data| to |out| and returns |
David Benjamin | 622a6db | 2014-10-06 19:12:30 -0400 | [diff] [blame] | 204 | * |out|. There must be at least |SHA384_DIGEST_LENGTH| bytes of space in |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 205 | * |out|. */ |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 206 | OPENSSL_EXPORT uint8_t *SHA384(const uint8_t *data, size_t len, uint8_t *out); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 207 | |
| 208 | /* SHA384_Transform is a low-level function that performs a single, SHA-1 block |
| 209 | * transformation using the state from |sha| and 64 bytes from |block|. */ |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 210 | OPENSSL_EXPORT void SHA384_Transform(SHA512_CTX *sha, const uint8_t *data); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 211 | |
| 212 | |
| 213 | /* SHA-512. */ |
| 214 | |
| 215 | /* SHA512_CBLOCK is the block size of SHA-512. */ |
| 216 | #define SHA512_CBLOCK 128 |
| 217 | |
| 218 | /* SHA512_DIGEST_LENGTH is the length of a SHA-512 digest. */ |
| 219 | #define SHA512_DIGEST_LENGTH 64 |
| 220 | |
David Benjamin | 862c0aa | 2016-04-27 14:59:12 -0400 | [diff] [blame] | 221 | /* SHA512_Init initialises |sha| and returns 1. */ |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 222 | OPENSSL_EXPORT int SHA512_Init(SHA512_CTX *sha); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 223 | |
David Benjamin | 862c0aa | 2016-04-27 14:59:12 -0400 | [diff] [blame] | 224 | /* SHA512_Update adds |len| bytes from |data| to |sha| and returns 1. */ |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 225 | OPENSSL_EXPORT int SHA512_Update(SHA512_CTX *sha, const void *data, size_t len); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 226 | |
| 227 | /* SHA512_Final adds the final padding to |sha| and writes the resulting digest |
David Benjamin | e2375e1 | 2015-05-27 01:32:26 -0400 | [diff] [blame] | 228 | * to |md|, which must have at least |SHA512_DIGEST_LENGTH| bytes of space. It |
David Benjamin | 862c0aa | 2016-04-27 14:59:12 -0400 | [diff] [blame] | 229 | * returns one on success and zero on programmer error. */ |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 230 | OPENSSL_EXPORT int SHA512_Final(uint8_t *md, SHA512_CTX *sha); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 231 | |
| 232 | /* SHA512 writes the digest of |len| bytes from |data| to |out| and returns |
David Benjamin | 622a6db | 2014-10-06 19:12:30 -0400 | [diff] [blame] | 233 | * |out|. There must be at least |SHA512_DIGEST_LENGTH| bytes of space in |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 234 | * |out|. */ |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 235 | OPENSSL_EXPORT uint8_t *SHA512(const uint8_t *data, size_t len, uint8_t *out); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 236 | |
| 237 | /* SHA512_Transform is a low-level function that performs a single, SHA-1 block |
| 238 | * transformation using the state from |sha| and 64 bytes from |block|. */ |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 239 | OPENSSL_EXPORT void SHA512_Transform(SHA512_CTX *sha, const uint8_t *data); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 240 | |
| 241 | struct sha512_state_st { |
| 242 | uint64_t h[8]; |
| 243 | uint64_t Nl, Nh; |
| 244 | union { |
| 245 | uint64_t d[16]; |
Adam Langley | e023ad2 | 2015-03-02 14:55:56 -0800 | [diff] [blame] | 246 | uint8_t p[128]; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 247 | } u; |
David Benjamin | 862c0aa | 2016-04-27 14:59:12 -0400 | [diff] [blame] | 248 | unsigned num, md_len; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 249 | }; |
| 250 | |
| 251 | |
| 252 | #if defined(__cplusplus) |
| 253 | } /* extern C */ |
| 254 | #endif |
| 255 | |
| 256 | #endif /* OPENSSL_HEADER_SHA_H */ |