blob: fc4644bff2a377b38b5174b57754a2225de9d511 [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* 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)
63extern "C" {
64#endif
65
66
David Benjamin4512b792017-08-18 19:21:50 -040067// The SHA family of hash functions (SHA-1 and SHA-2).
Adam Langley95c29f32014-06-20 12:00:00 -070068
69
David Benjamin4512b792017-08-18 19:21:50 -040070// SHA_CBLOCK is the block size of SHA-1.
Adam Langley95c29f32014-06-20 12:00:00 -070071#define SHA_CBLOCK 64
72
David Benjamin4512b792017-08-18 19:21:50 -040073// SHA_DIGEST_LENGTH is the length of a SHA-1 digest.
Adam Langley95c29f32014-06-20 12:00:00 -070074#define SHA_DIGEST_LENGTH 20
75
David Benjamin4512b792017-08-18 19:21:50 -040076// SHA1_Init initialises |sha| and returns one.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -070077OPENSSL_EXPORT int SHA1_Init(SHA_CTX *sha);
Adam Langley95c29f32014-06-20 12:00:00 -070078
David Benjamin4512b792017-08-18 19:21:50 -040079// SHA1_Update adds |len| bytes from |data| to |sha| and returns one.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -070080OPENSSL_EXPORT int SHA1_Update(SHA_CTX *sha, const void *data, size_t len);
Adam Langley95c29f32014-06-20 12:00:00 -070081
David Benjamin4512b792017-08-18 19:21:50 -040082// SHA1_Final adds the final padding to |sha| and writes the resulting digest
83// to |md|, which must have at least |SHA_DIGEST_LENGTH| bytes of space. It
84// returns one.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -070085OPENSSL_EXPORT int SHA1_Final(uint8_t *md, SHA_CTX *sha);
Adam Langley95c29f32014-06-20 12:00:00 -070086
David Benjamin4512b792017-08-18 19:21:50 -040087// SHA1 writes the digest of |len| bytes from |data| to |out| and returns
88// |out|. There must be at least |SHA_DIGEST_LENGTH| bytes of space in
89// |out|.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -070090OPENSSL_EXPORT uint8_t *SHA1(const uint8_t *data, size_t len, uint8_t *out);
Adam Langley95c29f32014-06-20 12:00:00 -070091
David Benjamin4512b792017-08-18 19:21:50 -040092// SHA1_Transform is a low-level function that performs a single, SHA-1 block
93// transformation using the state from |sha| and |SHA_CBLOCK| bytes from
94// |block|.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -070095OPENSSL_EXPORT void SHA1_Transform(SHA_CTX *sha, const uint8_t *block);
Adam Langley95c29f32014-06-20 12:00:00 -070096
97struct sha_state_st {
Adam Langley902870e2015-12-04 13:17:57 -080098#if defined(OPENSSL_WINDOWS)
99 uint32_t h[5];
100#else
David Benjamin4512b792017-08-18 19:21:50 -0400101 // wpa_supplicant accesses |h0|..|h4| so we must support those names
102 // for compatibility with it until it can be updated.
Brian Smithac9404c2015-11-01 10:13:24 -1000103 union {
104 uint32_t h[5];
105 struct {
106 uint32_t h0;
107 uint32_t h1;
108 uint32_t h2;
109 uint32_t h3;
110 uint32_t h4;
111 };
112 };
Adam Langley902870e2015-12-04 13:17:57 -0800113#endif
Adam Langley95c29f32014-06-20 12:00:00 -0700114 uint32_t Nl, Nh;
David Benjamin0aff3ff2015-12-07 13:17:03 -0500115 uint8_t data[SHA_CBLOCK];
David Benjamin78fefbf2015-12-06 21:48:45 -0500116 unsigned num;
Adam Langley95c29f32014-06-20 12:00:00 -0700117};
118
119
David Benjamin4512b792017-08-18 19:21:50 -0400120// SHA-224.
Adam Langley95c29f32014-06-20 12:00:00 -0700121
David Benjamin4512b792017-08-18 19:21:50 -0400122// SHA224_CBLOCK is the block size of SHA-224.
Adam Langley95c29f32014-06-20 12:00:00 -0700123#define SHA224_CBLOCK 64
124
David Benjamin4512b792017-08-18 19:21:50 -0400125// SHA224_DIGEST_LENGTH is the length of a SHA-224 digest.
Adam Langley95c29f32014-06-20 12:00:00 -0700126#define SHA224_DIGEST_LENGTH 28
127
David Benjamin4512b792017-08-18 19:21:50 -0400128// SHA224_Init initialises |sha| and returns 1.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700129OPENSSL_EXPORT int SHA224_Init(SHA256_CTX *sha);
Adam Langley95c29f32014-06-20 12:00:00 -0700130
David Benjamin4512b792017-08-18 19:21:50 -0400131// SHA224_Update adds |len| bytes from |data| to |sha| and returns 1.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700132OPENSSL_EXPORT int SHA224_Update(SHA256_CTX *sha, const void *data, size_t len);
Adam Langley95c29f32014-06-20 12:00:00 -0700133
David Benjamin4512b792017-08-18 19:21:50 -0400134// SHA224_Final adds the final padding to |sha| and writes the resulting digest
135// to |md|, which must have at least |SHA224_DIGEST_LENGTH| bytes of space. It
136// returns one on success and zero on programmer error.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700137OPENSSL_EXPORT int SHA224_Final(uint8_t *md, SHA256_CTX *sha);
Adam Langley95c29f32014-06-20 12:00:00 -0700138
David Benjamin4512b792017-08-18 19:21:50 -0400139// SHA224 writes the digest of |len| bytes from |data| to |out| and returns
140// |out|. There must be at least |SHA224_DIGEST_LENGTH| bytes of space in
141// |out|.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700142OPENSSL_EXPORT uint8_t *SHA224(const uint8_t *data, size_t len, uint8_t *out);
Adam Langley95c29f32014-06-20 12:00:00 -0700143
144
David Benjamin4512b792017-08-18 19:21:50 -0400145// SHA-256.
Adam Langley95c29f32014-06-20 12:00:00 -0700146
David Benjamin4512b792017-08-18 19:21:50 -0400147// SHA256_CBLOCK is the block size of SHA-256.
Adam Langley95c29f32014-06-20 12:00:00 -0700148#define SHA256_CBLOCK 64
149
David Benjamin4512b792017-08-18 19:21:50 -0400150// SHA256_DIGEST_LENGTH is the length of a SHA-256 digest.
Adam Langley95c29f32014-06-20 12:00:00 -0700151#define SHA256_DIGEST_LENGTH 32
152
David Benjamin4512b792017-08-18 19:21:50 -0400153// SHA256_Init initialises |sha| and returns 1.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700154OPENSSL_EXPORT int SHA256_Init(SHA256_CTX *sha);
Adam Langley95c29f32014-06-20 12:00:00 -0700155
David Benjamin4512b792017-08-18 19:21:50 -0400156// SHA256_Update adds |len| bytes from |data| to |sha| and returns 1.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700157OPENSSL_EXPORT int SHA256_Update(SHA256_CTX *sha, const void *data, size_t len);
Adam Langley95c29f32014-06-20 12:00:00 -0700158
David Benjamin4512b792017-08-18 19:21:50 -0400159// SHA256_Final adds the final padding to |sha| and writes the resulting digest
160// to |md|, which must have at least |SHA256_DIGEST_LENGTH| bytes of space. It
161// returns one on success and zero on programmer error.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700162OPENSSL_EXPORT int SHA256_Final(uint8_t *md, SHA256_CTX *sha);
Adam Langley95c29f32014-06-20 12:00:00 -0700163
David Benjamin4512b792017-08-18 19:21:50 -0400164// SHA256 writes the digest of |len| bytes from |data| to |out| and returns
165// |out|. There must be at least |SHA256_DIGEST_LENGTH| bytes of space in
166// |out|.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700167OPENSSL_EXPORT uint8_t *SHA256(const uint8_t *data, size_t len, uint8_t *out);
Adam Langley95c29f32014-06-20 12:00:00 -0700168
David Benjamin4512b792017-08-18 19:21:50 -0400169// SHA256_Transform is a low-level function that performs a single, SHA-256
170// block transformation using the state from |sha| and |SHA256_CBLOCK| bytes
171// from |block|.
Adam Langley5a8d48e2016-07-28 14:16:45 -0700172OPENSSL_EXPORT void SHA256_Transform(SHA256_CTX *sha, const uint8_t *block);
Adam Langley95c29f32014-06-20 12:00:00 -0700173
174struct sha256_state_st {
175 uint32_t h[8];
176 uint32_t Nl, Nh;
David Benjamin0aff3ff2015-12-07 13:17:03 -0500177 uint8_t data[SHA256_CBLOCK];
David Benjamin862c0aa2016-04-27 14:59:12 -0400178 unsigned num, md_len;
Adam Langley95c29f32014-06-20 12:00:00 -0700179};
180
181
David Benjamin4512b792017-08-18 19:21:50 -0400182// SHA-384.
Adam Langley95c29f32014-06-20 12:00:00 -0700183
David Benjamin4512b792017-08-18 19:21:50 -0400184// SHA384_CBLOCK is the block size of SHA-384.
Adam Langley95c29f32014-06-20 12:00:00 -0700185#define SHA384_CBLOCK 128
186
David Benjamin4512b792017-08-18 19:21:50 -0400187// SHA384_DIGEST_LENGTH is the length of a SHA-384 digest.
Adam Langley95c29f32014-06-20 12:00:00 -0700188#define SHA384_DIGEST_LENGTH 48
189
David Benjamin4512b792017-08-18 19:21:50 -0400190// SHA384_Init initialises |sha| and returns 1.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700191OPENSSL_EXPORT int SHA384_Init(SHA512_CTX *sha);
Adam Langley95c29f32014-06-20 12:00:00 -0700192
David Benjamin4512b792017-08-18 19:21:50 -0400193// SHA384_Update adds |len| bytes from |data| to |sha| and returns 1.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700194OPENSSL_EXPORT int SHA384_Update(SHA512_CTX *sha, const void *data, size_t len);
Adam Langley95c29f32014-06-20 12:00:00 -0700195
David Benjamin4512b792017-08-18 19:21:50 -0400196// SHA384_Final adds the final padding to |sha| and writes the resulting digest
197// to |md|, which must have at least |SHA384_DIGEST_LENGTH| bytes of space. It
198// returns one on success and zero on programmer error.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700199OPENSSL_EXPORT int SHA384_Final(uint8_t *md, SHA512_CTX *sha);
Adam Langley95c29f32014-06-20 12:00:00 -0700200
David Benjamin4512b792017-08-18 19:21:50 -0400201// SHA384 writes the digest of |len| bytes from |data| to |out| and returns
202// |out|. There must be at least |SHA384_DIGEST_LENGTH| bytes of space in
203// |out|.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700204OPENSSL_EXPORT uint8_t *SHA384(const uint8_t *data, size_t len, uint8_t *out);
Adam Langley95c29f32014-06-20 12:00:00 -0700205
David Benjamin4512b792017-08-18 19:21:50 -0400206// SHA384_Transform is a low-level function that performs a single, SHA-384
207// block transformation using the state from |sha| and |SHA384_CBLOCK| bytes
208// from |block|.
Adam Langley5a8d48e2016-07-28 14:16:45 -0700209OPENSSL_EXPORT void SHA384_Transform(SHA512_CTX *sha, const uint8_t *block);
Adam Langley95c29f32014-06-20 12:00:00 -0700210
211
David Benjamin4512b792017-08-18 19:21:50 -0400212// SHA-512.
Adam Langley95c29f32014-06-20 12:00:00 -0700213
David Benjamin4512b792017-08-18 19:21:50 -0400214// SHA512_CBLOCK is the block size of SHA-512.
Adam Langley95c29f32014-06-20 12:00:00 -0700215#define SHA512_CBLOCK 128
216
David Benjamin4512b792017-08-18 19:21:50 -0400217// SHA512_DIGEST_LENGTH is the length of a SHA-512 digest.
Adam Langley95c29f32014-06-20 12:00:00 -0700218#define SHA512_DIGEST_LENGTH 64
219
David Benjamin4512b792017-08-18 19:21:50 -0400220// SHA512_Init initialises |sha| and returns 1.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700221OPENSSL_EXPORT int SHA512_Init(SHA512_CTX *sha);
Adam Langley95c29f32014-06-20 12:00:00 -0700222
David Benjamin4512b792017-08-18 19:21:50 -0400223// SHA512_Update adds |len| bytes from |data| to |sha| and returns 1.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700224OPENSSL_EXPORT int SHA512_Update(SHA512_CTX *sha, const void *data, size_t len);
Adam Langley95c29f32014-06-20 12:00:00 -0700225
David Benjamin4512b792017-08-18 19:21:50 -0400226// SHA512_Final adds the final padding to |sha| and writes the resulting digest
227// to |md|, which must have at least |SHA512_DIGEST_LENGTH| bytes of space. It
228// returns one on success and zero on programmer error.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700229OPENSSL_EXPORT int SHA512_Final(uint8_t *md, SHA512_CTX *sha);
Adam Langley95c29f32014-06-20 12:00:00 -0700230
David Benjamin4512b792017-08-18 19:21:50 -0400231// SHA512 writes the digest of |len| bytes from |data| to |out| and returns
232// |out|. There must be at least |SHA512_DIGEST_LENGTH| bytes of space in
233// |out|.
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700234OPENSSL_EXPORT uint8_t *SHA512(const uint8_t *data, size_t len, uint8_t *out);
Adam Langley95c29f32014-06-20 12:00:00 -0700235
David Benjamin4512b792017-08-18 19:21:50 -0400236// SHA512_Transform is a low-level function that performs a single, SHA-512
237// block transformation using the state from |sha| and |SHA512_CBLOCK| bytes
238// from |block|.
Adam Langley5a8d48e2016-07-28 14:16:45 -0700239OPENSSL_EXPORT void SHA512_Transform(SHA512_CTX *sha, const uint8_t *block);
Adam Langley95c29f32014-06-20 12:00:00 -0700240
241struct sha512_state_st {
242 uint64_t h[8];
243 uint64_t Nl, Nh;
244 union {
245 uint64_t d[16];
Adam Langleye023ad22015-03-02 14:55:56 -0800246 uint8_t p[128];
Adam Langley95c29f32014-06-20 12:00:00 -0700247 } u;
David Benjamin862c0aa2016-04-27 14:59:12 -0400248 unsigned num, md_len;
Adam Langley95c29f32014-06-20 12:00:00 -0700249};
250
251
252#if defined(__cplusplus)
David Benjamin4512b792017-08-18 19:21:50 -0400253} // extern C
Adam Langley95c29f32014-06-20 12:00:00 -0700254#endif
255
David Benjamin4512b792017-08-18 19:21:50 -0400256#endif // OPENSSL_HEADER_SHA_H