xref: /openbsd-src/lib/libcrypto/sha/sha.h (revision 515aa502ddfbfa9d16fb6d4368077c2ccf35cf3d)
1*515aa502Stb /* $OpenBSD: sha.h,v 1.26 2025/01/25 17:59:44 tb Exp $ */
25b37fcf3Sryker /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
35b37fcf3Sryker  * All rights reserved.
45b37fcf3Sryker  *
55b37fcf3Sryker  * This package is an SSL implementation written
65b37fcf3Sryker  * by Eric Young (eay@cryptsoft.com).
75b37fcf3Sryker  * The implementation was written so as to conform with Netscapes SSL.
85b37fcf3Sryker  *
95b37fcf3Sryker  * This library is free for commercial and non-commercial use as long as
105b37fcf3Sryker  * the following conditions are aheared to.  The following conditions
115b37fcf3Sryker  * apply to all code found in this distribution, be it the RC4, RSA,
125b37fcf3Sryker  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
135b37fcf3Sryker  * included with this distribution is covered by the same copyright terms
145b37fcf3Sryker  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
155b37fcf3Sryker  *
165b37fcf3Sryker  * Copyright remains Eric Young's, and as such any Copyright notices in
175b37fcf3Sryker  * the code are not to be removed.
185b37fcf3Sryker  * If this package is used in a product, Eric Young should be given attribution
195b37fcf3Sryker  * as the author of the parts of the library used.
205b37fcf3Sryker  * This can be in the form of a textual message at program startup or
215b37fcf3Sryker  * in documentation (online or textual) provided with the package.
225b37fcf3Sryker  *
235b37fcf3Sryker  * Redistribution and use in source and binary forms, with or without
245b37fcf3Sryker  * modification, are permitted provided that the following conditions
255b37fcf3Sryker  * are met:
265b37fcf3Sryker  * 1. Redistributions of source code must retain the copyright
275b37fcf3Sryker  *    notice, this list of conditions and the following disclaimer.
285b37fcf3Sryker  * 2. Redistributions in binary form must reproduce the above copyright
295b37fcf3Sryker  *    notice, this list of conditions and the following disclaimer in the
305b37fcf3Sryker  *    documentation and/or other materials provided with the distribution.
315b37fcf3Sryker  * 3. All advertising materials mentioning features or use of this software
325b37fcf3Sryker  *    must display the following acknowledgement:
335b37fcf3Sryker  *    "This product includes cryptographic software written by
345b37fcf3Sryker  *     Eric Young (eay@cryptsoft.com)"
355b37fcf3Sryker  *    The word 'cryptographic' can be left out if the rouines from the library
365b37fcf3Sryker  *    being used are not cryptographic related :-).
375b37fcf3Sryker  * 4. If you include any Windows specific code (or a derivative thereof) from
385b37fcf3Sryker  *    the apps directory (application code) you must include an acknowledgement:
395b37fcf3Sryker  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
405b37fcf3Sryker  *
415b37fcf3Sryker  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
425b37fcf3Sryker  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
435b37fcf3Sryker  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
445b37fcf3Sryker  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
455b37fcf3Sryker  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
465b37fcf3Sryker  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
475b37fcf3Sryker  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
485b37fcf3Sryker  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
495b37fcf3Sryker  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
505b37fcf3Sryker  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
515b37fcf3Sryker  * SUCH DAMAGE.
525b37fcf3Sryker  *
535b37fcf3Sryker  * The licence and distribution terms for any publically available version or
545b37fcf3Sryker  * derivative of this code cannot be changed.  i.e. this code cannot simply be
555b37fcf3Sryker  * copied and put under another distribution licence
565b37fcf3Sryker  * [including the GNU Public Licence.]
575b37fcf3Sryker  */
585b37fcf3Sryker 
598cf4d6a6Sjsing #include <stddef.h>
608cf4d6a6Sjsing 
615b37fcf3Sryker #ifndef HEADER_SHA_H
625b37fcf3Sryker #define HEADER_SHA_H
6340c1866aSbeck #if !defined(HAVE_ATTRIBUTE__BOUNDED__) && !defined(__OpenBSD__)
64361740acSbeck #define __bounded__(x, y, z)
65361740acSbeck #endif
665b37fcf3Sryker 
6720175b85Sjsing #include <openssl/opensslconf.h>
6820175b85Sjsing 
695b37fcf3Sryker #ifdef  __cplusplus
705b37fcf3Sryker extern "C" {
715b37fcf3Sryker #endif
725b37fcf3Sryker 
73913ec974Sbeck /*
74913ec974Sbeck  * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
751bcbc6d0Sbcook  * ! SHA_LONG has to be at least 32 bits wide.                    !
76913ec974Sbeck  * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
77913ec974Sbeck  */
78913ec974Sbeck 
79913ec974Sbeck #define SHA_LONG unsigned int
80913ec974Sbeck 
815b37fcf3Sryker #define SHA_LBLOCK	16
82913ec974Sbeck #define SHA_CBLOCK	(SHA_LBLOCK*4)	/* SHA treats input data as a
83913ec974Sbeck 					 * contiguous array of 32 bit
84913ec974Sbeck 					 * wide big-endian values. */
85913ec974Sbeck #define SHA_LAST_BLOCK  (SHA_CBLOCK-8)
865b37fcf3Sryker #define SHA_DIGEST_LENGTH 20
875b37fcf3Sryker 
8806b4c63bSjsing typedef struct SHAstate_st {
89913ec974Sbeck 	SHA_LONG h0, h1, h2, h3, h4;
90913ec974Sbeck 	SHA_LONG Nl, Nh;
91913ec974Sbeck 	SHA_LONG data[SHA_LBLOCK];
924fcf65c5Sdjm 	unsigned int num;
935b37fcf3Sryker } SHA_CTX;
945b37fcf3Sryker 
95da347917Sbeck #ifndef OPENSSL_NO_SHA1
96da347917Sbeck int SHA1_Init(SHA_CTX *c);
97d039a9dfSavsm int SHA1_Update(SHA_CTX *c, const void *data, size_t len)
98d039a9dfSavsm     __attribute__ ((__bounded__(__buffer__, 2, 3)));
99da347917Sbeck int SHA1_Final(unsigned char *md, SHA_CTX *c);
100d039a9dfSavsm unsigned char *SHA1(const unsigned char *d, size_t n, unsigned char *md)
101fd65fe5aStb     __attribute__ ((__bounded__(__buffer__, 1, 2)))
102fd65fe5aStb     __attribute__ ((__nonnull__(3)));
103ba5406e9Sbeck void SHA1_Transform(SHA_CTX *c, const unsigned char *data);
104913ec974Sbeck #endif
1054fcf65c5Sdjm 
1064fcf65c5Sdjm #define SHA256_CBLOCK	(SHA_LBLOCK*4)	/* SHA-256 treats input data as a
1074fcf65c5Sdjm 					 * contiguous array of 32 bit
1084fcf65c5Sdjm 					 * wide big-endian values. */
1094fcf65c5Sdjm #define SHA224_DIGEST_LENGTH	28
1104fcf65c5Sdjm #define SHA256_DIGEST_LENGTH	32
1114fcf65c5Sdjm 
11206b4c63bSjsing typedef struct SHA256state_st {
1134fcf65c5Sdjm 	SHA_LONG h[8];
1144fcf65c5Sdjm 	SHA_LONG Nl, Nh;
1154fcf65c5Sdjm 	SHA_LONG data[SHA_LBLOCK];
1164fcf65c5Sdjm 	unsigned int num, md_len;
1174fcf65c5Sdjm } SHA256_CTX;
1184fcf65c5Sdjm 
1194fcf65c5Sdjm #ifndef OPENSSL_NO_SHA256
1204fcf65c5Sdjm int SHA224_Init(SHA256_CTX *c);
121d039a9dfSavsm int SHA224_Update(SHA256_CTX *c, const void *data, size_t len)
122d039a9dfSavsm     __attribute__ ((__bounded__(__buffer__, 2, 3)));
1234fcf65c5Sdjm int SHA224_Final(unsigned char *md, SHA256_CTX *c);
124d039a9dfSavsm unsigned char *SHA224(const unsigned char *d, size_t n, unsigned char *md)
125f6fc4eafStb     __attribute__ ((__bounded__(__buffer__, 1, 2)))
126f6fc4eafStb     __attribute__ ((__nonnull__(3)));
1274fcf65c5Sdjm int SHA256_Init(SHA256_CTX *c);
128d039a9dfSavsm int SHA256_Update(SHA256_CTX *c, const void *data, size_t len)
129d039a9dfSavsm     __attribute__ ((__bounded__(__buffer__, 2, 3)));
1304fcf65c5Sdjm int SHA256_Final(unsigned char *md, SHA256_CTX *c);
131d039a9dfSavsm unsigned char *SHA256(const unsigned char *d, size_t n, unsigned char *md)
132fd65fe5aStb     __attribute__ ((__bounded__(__buffer__, 1, 2)))
133fd65fe5aStb     __attribute__ ((__nonnull__(3)));
1344fcf65c5Sdjm void SHA256_Transform(SHA256_CTX *c, const unsigned char *data);
1354fcf65c5Sdjm #endif
1364fcf65c5Sdjm 
1374fcf65c5Sdjm #define SHA384_DIGEST_LENGTH	48
1384fcf65c5Sdjm #define SHA512_DIGEST_LENGTH	64
1394fcf65c5Sdjm 
1404fcf65c5Sdjm #ifndef OPENSSL_NO_SHA512
1414fcf65c5Sdjm /*
1424fcf65c5Sdjm  * Unlike 32-bit digest algorithms, SHA-512 *relies* on SHA_LONG64
1434fcf65c5Sdjm  * being exactly 64-bit wide. See Implementation Notes in sha512.c
1444fcf65c5Sdjm  * for further details.
1454fcf65c5Sdjm  */
1464fcf65c5Sdjm #define SHA512_CBLOCK	(SHA_LBLOCK*8)	/* SHA-512 treats input data as a
1474fcf65c5Sdjm 					 * contiguous array of 64 bit
1484fcf65c5Sdjm 					 * wide big-endian values. */
149e3d5af28Smiod #if defined(_LP64)
1504fcf65c5Sdjm #define SHA_LONG64 unsigned long
1514fcf65c5Sdjm #define U64(C)     C##UL
1524fcf65c5Sdjm #else
1534fcf65c5Sdjm #define SHA_LONG64 unsigned long long
1544fcf65c5Sdjm #define U64(C)     C##ULL
1554fcf65c5Sdjm #endif
1564fcf65c5Sdjm 
15706b4c63bSjsing typedef struct SHA512state_st {
1584fcf65c5Sdjm 	SHA_LONG64 h[8];
1594fcf65c5Sdjm 	SHA_LONG64 Nl, Nh;
1604fcf65c5Sdjm 	union {
1614fcf65c5Sdjm 		SHA_LONG64	d[SHA_LBLOCK];
1624fcf65c5Sdjm 		unsigned char	p[SHA512_CBLOCK];
1634fcf65c5Sdjm 	} u;
1644fcf65c5Sdjm 	unsigned int num, md_len;
1654fcf65c5Sdjm } SHA512_CTX;
1664fcf65c5Sdjm #endif
1674fcf65c5Sdjm 
1684fcf65c5Sdjm #ifndef OPENSSL_NO_SHA512
1694fcf65c5Sdjm int SHA384_Init(SHA512_CTX *c);
170d039a9dfSavsm int SHA384_Update(SHA512_CTX *c, const void *data, size_t len)
171d039a9dfSavsm     __attribute__ ((__bounded__(__buffer__, 2, 3)));
1724fcf65c5Sdjm int SHA384_Final(unsigned char *md, SHA512_CTX *c);
173d039a9dfSavsm unsigned char *SHA384(const unsigned char *d, size_t n, unsigned char *md)
174fd65fe5aStb     __attribute__ ((__bounded__(__buffer__, 1, 2)))
175fd65fe5aStb     __attribute__ ((__nonnull__(3)));
1764fcf65c5Sdjm int SHA512_Init(SHA512_CTX *c);
177d039a9dfSavsm int SHA512_Update(SHA512_CTX *c, const void *data, size_t len)
178d039a9dfSavsm     __attribute__ ((__bounded__(__buffer__, 2, 3)));
1794fcf65c5Sdjm int SHA512_Final(unsigned char *md, SHA512_CTX *c);
180d039a9dfSavsm unsigned char *SHA512(const unsigned char *d, size_t n, unsigned char *md)
181fd65fe5aStb     __attribute__ ((__bounded__(__buffer__, 1, 2)))
182fd65fe5aStb     __attribute__ ((__nonnull__(3)));
1834fcf65c5Sdjm void SHA512_Transform(SHA512_CTX *c, const unsigned char *data);
1844fcf65c5Sdjm #endif
1854fcf65c5Sdjm 
1865b37fcf3Sryker #ifdef  __cplusplus
1875b37fcf3Sryker }
1885b37fcf3Sryker #endif
1895b37fcf3Sryker 
1905b37fcf3Sryker #endif
191