1*72c33676SMaxim Ag /* $OpenBSD: sha.h,v 1.21 2015/09/13 21:09:56 doug Exp $ */ 2f5b1c8a1SJohn Marino /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 3f5b1c8a1SJohn Marino * All rights reserved. 4f5b1c8a1SJohn Marino * 5f5b1c8a1SJohn Marino * This package is an SSL implementation written 6f5b1c8a1SJohn Marino * by Eric Young (eay@cryptsoft.com). 7f5b1c8a1SJohn Marino * The implementation was written so as to conform with Netscapes SSL. 8f5b1c8a1SJohn Marino * 9f5b1c8a1SJohn Marino * This library is free for commercial and non-commercial use as long as 10f5b1c8a1SJohn Marino * the following conditions are aheared to. The following conditions 11f5b1c8a1SJohn Marino * apply to all code found in this distribution, be it the RC4, RSA, 12f5b1c8a1SJohn Marino * lhash, DES, etc., code; not just the SSL code. The SSL documentation 13f5b1c8a1SJohn Marino * included with this distribution is covered by the same copyright terms 14f5b1c8a1SJohn Marino * except that the holder is Tim Hudson (tjh@cryptsoft.com). 15f5b1c8a1SJohn Marino * 16f5b1c8a1SJohn Marino * Copyright remains Eric Young's, and as such any Copyright notices in 17f5b1c8a1SJohn Marino * the code are not to be removed. 18f5b1c8a1SJohn Marino * If this package is used in a product, Eric Young should be given attribution 19f5b1c8a1SJohn Marino * as the author of the parts of the library used. 20f5b1c8a1SJohn Marino * This can be in the form of a textual message at program startup or 21f5b1c8a1SJohn Marino * in documentation (online or textual) provided with the package. 22f5b1c8a1SJohn Marino * 23f5b1c8a1SJohn Marino * Redistribution and use in source and binary forms, with or without 24f5b1c8a1SJohn Marino * modification, are permitted provided that the following conditions 25f5b1c8a1SJohn Marino * are met: 26f5b1c8a1SJohn Marino * 1. Redistributions of source code must retain the copyright 27f5b1c8a1SJohn Marino * notice, this list of conditions and the following disclaimer. 28f5b1c8a1SJohn Marino * 2. Redistributions in binary form must reproduce the above copyright 29f5b1c8a1SJohn Marino * notice, this list of conditions and the following disclaimer in the 30f5b1c8a1SJohn Marino * documentation and/or other materials provided with the distribution. 31f5b1c8a1SJohn Marino * 3. All advertising materials mentioning features or use of this software 32f5b1c8a1SJohn Marino * must display the following acknowledgement: 33f5b1c8a1SJohn Marino * "This product includes cryptographic software written by 34f5b1c8a1SJohn Marino * Eric Young (eay@cryptsoft.com)" 35f5b1c8a1SJohn Marino * The word 'cryptographic' can be left out if the rouines from the library 36f5b1c8a1SJohn Marino * being used are not cryptographic related :-). 37f5b1c8a1SJohn Marino * 4. If you include any Windows specific code (or a derivative thereof) from 38f5b1c8a1SJohn Marino * the apps directory (application code) you must include an acknowledgement: 39f5b1c8a1SJohn Marino * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 40f5b1c8a1SJohn Marino * 41f5b1c8a1SJohn Marino * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 42f5b1c8a1SJohn Marino * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 43f5b1c8a1SJohn Marino * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 44f5b1c8a1SJohn Marino * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 45f5b1c8a1SJohn Marino * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 46f5b1c8a1SJohn Marino * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 47f5b1c8a1SJohn Marino * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 48f5b1c8a1SJohn Marino * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 49f5b1c8a1SJohn Marino * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 50f5b1c8a1SJohn Marino * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 51f5b1c8a1SJohn Marino * SUCH DAMAGE. 52f5b1c8a1SJohn Marino * 53f5b1c8a1SJohn Marino * The licence and distribution terms for any publically available version or 54f5b1c8a1SJohn Marino * derivative of this code cannot be changed. i.e. this code cannot simply be 55f5b1c8a1SJohn Marino * copied and put under another distribution licence 56f5b1c8a1SJohn Marino * [including the GNU Public Licence.] 57f5b1c8a1SJohn Marino */ 58f5b1c8a1SJohn Marino 59f5b1c8a1SJohn Marino #include <stddef.h> 60f5b1c8a1SJohn Marino 61f5b1c8a1SJohn Marino #ifndef HEADER_SHA_H 62f5b1c8a1SJohn Marino #define HEADER_SHA_H 63f5b1c8a1SJohn Marino #if !defined(HAVE_ATTRIBUTE__BOUNDED__) && !defined(__OpenBSD__) 64f5b1c8a1SJohn Marino #define __bounded__(x, y, z) 65f5b1c8a1SJohn Marino #endif 66f5b1c8a1SJohn Marino 67f5b1c8a1SJohn Marino #include <openssl/opensslconf.h> 68f5b1c8a1SJohn Marino 69f5b1c8a1SJohn Marino #ifdef __cplusplus 70f5b1c8a1SJohn Marino extern "C" { 71f5b1c8a1SJohn Marino #endif 72f5b1c8a1SJohn Marino 73f5b1c8a1SJohn Marino #if defined(OPENSSL_NO_SHA) || defined(OPENSSL_NO_SHA1) 74f5b1c8a1SJohn Marino #error SHA is disabled. 75f5b1c8a1SJohn Marino #endif 76f5b1c8a1SJohn Marino 77f5b1c8a1SJohn Marino /* 78f5b1c8a1SJohn Marino * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 79f5b1c8a1SJohn Marino * ! SHA_LONG has to be at least 32 bits wide. ! 80f5b1c8a1SJohn Marino * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 81f5b1c8a1SJohn Marino */ 82f5b1c8a1SJohn Marino 83f5b1c8a1SJohn Marino #define SHA_LONG unsigned int 84f5b1c8a1SJohn Marino 85f5b1c8a1SJohn Marino #define SHA_LBLOCK 16 86f5b1c8a1SJohn Marino #define SHA_CBLOCK (SHA_LBLOCK*4) /* SHA treats input data as a 87f5b1c8a1SJohn Marino * contiguous array of 32 bit 88f5b1c8a1SJohn Marino * wide big-endian values. */ 89f5b1c8a1SJohn Marino #define SHA_LAST_BLOCK (SHA_CBLOCK-8) 90f5b1c8a1SJohn Marino #define SHA_DIGEST_LENGTH 20 91f5b1c8a1SJohn Marino 92f5b1c8a1SJohn Marino typedef struct SHAstate_st 93f5b1c8a1SJohn Marino { 94f5b1c8a1SJohn Marino SHA_LONG h0,h1,h2,h3,h4; 95f5b1c8a1SJohn Marino SHA_LONG Nl,Nh; 96f5b1c8a1SJohn Marino SHA_LONG data[SHA_LBLOCK]; 97f5b1c8a1SJohn Marino unsigned int num; 98f5b1c8a1SJohn Marino } SHA_CTX; 99f5b1c8a1SJohn Marino 100f5b1c8a1SJohn Marino #ifndef OPENSSL_NO_SHA1 101f5b1c8a1SJohn Marino int SHA1_Init(SHA_CTX *c); 102f5b1c8a1SJohn Marino int SHA1_Update(SHA_CTX *c, const void *data, size_t len) 103f5b1c8a1SJohn Marino __attribute__ ((__bounded__(__buffer__,2,3))); 104f5b1c8a1SJohn Marino int SHA1_Final(unsigned char *md, SHA_CTX *c); 105f5b1c8a1SJohn Marino unsigned char *SHA1(const unsigned char *d, size_t n, unsigned char *md) 106f5b1c8a1SJohn Marino __attribute__ ((__bounded__(__buffer__,1,2))); 107f5b1c8a1SJohn Marino void SHA1_Transform(SHA_CTX *c, const unsigned char *data); 108f5b1c8a1SJohn Marino #endif 109f5b1c8a1SJohn Marino 110f5b1c8a1SJohn Marino #define SHA256_CBLOCK (SHA_LBLOCK*4) /* SHA-256 treats input data as a 111f5b1c8a1SJohn Marino * contiguous array of 32 bit 112f5b1c8a1SJohn Marino * wide big-endian values. */ 113f5b1c8a1SJohn Marino #define SHA224_DIGEST_LENGTH 28 114f5b1c8a1SJohn Marino #define SHA256_DIGEST_LENGTH 32 115f5b1c8a1SJohn Marino 116f5b1c8a1SJohn Marino typedef struct SHA256state_st 117f5b1c8a1SJohn Marino { 118f5b1c8a1SJohn Marino SHA_LONG h[8]; 119f5b1c8a1SJohn Marino SHA_LONG Nl,Nh; 120f5b1c8a1SJohn Marino SHA_LONG data[SHA_LBLOCK]; 121f5b1c8a1SJohn Marino unsigned int num,md_len; 122f5b1c8a1SJohn Marino } SHA256_CTX; 123f5b1c8a1SJohn Marino 124f5b1c8a1SJohn Marino #ifndef OPENSSL_NO_SHA256 125f5b1c8a1SJohn Marino int SHA224_Init(SHA256_CTX *c); 126f5b1c8a1SJohn Marino int SHA224_Update(SHA256_CTX *c, const void *data, size_t len) 127f5b1c8a1SJohn Marino __attribute__ ((__bounded__(__buffer__,2,3))); 128f5b1c8a1SJohn Marino int SHA224_Final(unsigned char *md, SHA256_CTX *c); 129f5b1c8a1SJohn Marino unsigned char *SHA224(const unsigned char *d, size_t n,unsigned char *md) 130f5b1c8a1SJohn Marino __attribute__ ((__bounded__(__buffer__,1,2))); 131f5b1c8a1SJohn Marino int SHA256_Init(SHA256_CTX *c); 132f5b1c8a1SJohn Marino int SHA256_Update(SHA256_CTX *c, const void *data, size_t len) 133f5b1c8a1SJohn Marino __attribute__ ((__bounded__(__buffer__,2,3))); 134f5b1c8a1SJohn Marino int SHA256_Final(unsigned char *md, SHA256_CTX *c); 135f5b1c8a1SJohn Marino unsigned char *SHA256(const unsigned char *d, size_t n,unsigned char *md) 136f5b1c8a1SJohn Marino __attribute__ ((__bounded__(__buffer__,1,2))); 137f5b1c8a1SJohn Marino void SHA256_Transform(SHA256_CTX *c, const unsigned char *data); 138f5b1c8a1SJohn Marino #endif 139f5b1c8a1SJohn Marino 140f5b1c8a1SJohn Marino #define SHA384_DIGEST_LENGTH 48 141f5b1c8a1SJohn Marino #define SHA512_DIGEST_LENGTH 64 142f5b1c8a1SJohn Marino 143f5b1c8a1SJohn Marino #ifndef OPENSSL_NO_SHA512 144f5b1c8a1SJohn Marino /* 145f5b1c8a1SJohn Marino * Unlike 32-bit digest algorithms, SHA-512 *relies* on SHA_LONG64 146f5b1c8a1SJohn Marino * being exactly 64-bit wide. See Implementation Notes in sha512.c 147f5b1c8a1SJohn Marino * for further details. 148f5b1c8a1SJohn Marino */ 149f5b1c8a1SJohn Marino #define SHA512_CBLOCK (SHA_LBLOCK*8) /* SHA-512 treats input data as a 150f5b1c8a1SJohn Marino * contiguous array of 64 bit 151f5b1c8a1SJohn Marino * wide big-endian values. */ 152f5b1c8a1SJohn Marino #if defined(_LP64) 153f5b1c8a1SJohn Marino #define SHA_LONG64 unsigned long 154f5b1c8a1SJohn Marino #define U64(C) C##UL 155f5b1c8a1SJohn Marino #else 156f5b1c8a1SJohn Marino #define SHA_LONG64 unsigned long long 157f5b1c8a1SJohn Marino #define U64(C) C##ULL 158f5b1c8a1SJohn Marino #endif 159f5b1c8a1SJohn Marino 160f5b1c8a1SJohn Marino typedef struct SHA512state_st 161f5b1c8a1SJohn Marino { 162f5b1c8a1SJohn Marino SHA_LONG64 h[8]; 163f5b1c8a1SJohn Marino SHA_LONG64 Nl,Nh; 164f5b1c8a1SJohn Marino union { 165f5b1c8a1SJohn Marino SHA_LONG64 d[SHA_LBLOCK]; 166f5b1c8a1SJohn Marino unsigned char p[SHA512_CBLOCK]; 167f5b1c8a1SJohn Marino } u; 168f5b1c8a1SJohn Marino unsigned int num,md_len; 169f5b1c8a1SJohn Marino } SHA512_CTX; 170f5b1c8a1SJohn Marino #endif 171f5b1c8a1SJohn Marino 172f5b1c8a1SJohn Marino #ifndef OPENSSL_NO_SHA512 173f5b1c8a1SJohn Marino int SHA384_Init(SHA512_CTX *c); 174f5b1c8a1SJohn Marino int SHA384_Update(SHA512_CTX *c, const void *data, size_t len) 175f5b1c8a1SJohn Marino __attribute__ ((__bounded__(__buffer__,2,3))); 176f5b1c8a1SJohn Marino int SHA384_Final(unsigned char *md, SHA512_CTX *c); 177f5b1c8a1SJohn Marino unsigned char *SHA384(const unsigned char *d, size_t n,unsigned char *md) 178f5b1c8a1SJohn Marino __attribute__ ((__bounded__(__buffer__,1,2))); 179f5b1c8a1SJohn Marino int SHA512_Init(SHA512_CTX *c); 180f5b1c8a1SJohn Marino int SHA512_Update(SHA512_CTX *c, const void *data, size_t len) 181f5b1c8a1SJohn Marino __attribute__ ((__bounded__(__buffer__,2,3))); 182f5b1c8a1SJohn Marino int SHA512_Final(unsigned char *md, SHA512_CTX *c); 183f5b1c8a1SJohn Marino unsigned char *SHA512(const unsigned char *d, size_t n,unsigned char *md) 184f5b1c8a1SJohn Marino __attribute__ ((__bounded__(__buffer__,1,2))); 185f5b1c8a1SJohn Marino void SHA512_Transform(SHA512_CTX *c, const unsigned char *data); 186f5b1c8a1SJohn Marino #endif 187f5b1c8a1SJohn Marino 188f5b1c8a1SJohn Marino #ifdef __cplusplus 189f5b1c8a1SJohn Marino } 190f5b1c8a1SJohn Marino #endif 191f5b1c8a1SJohn Marino 192f5b1c8a1SJohn Marino #endif 193