1*4882a593Smuzhiyun /* 2*4882a593Smuzhiyun --------------------------------------------------------------------------- 3*4882a593Smuzhiyun Copyright (c) 2002, Dr Brian Gladman <brg@gladman.me.uk>, Worcester, UK. 4*4882a593Smuzhiyun All rights reserved. 5*4882a593Smuzhiyun 6*4882a593Smuzhiyun LICENSE TERMS 7*4882a593Smuzhiyun 8*4882a593Smuzhiyun The free distribution and use of this software in both source and binary 9*4882a593Smuzhiyun form is allowed (with or without changes) provided that: 10*4882a593Smuzhiyun 11*4882a593Smuzhiyun 1. distributions of this source code include the above copyright 12*4882a593Smuzhiyun notice, this list of conditions and the following disclaimer; 13*4882a593Smuzhiyun 14*4882a593Smuzhiyun 2. distributions in binary form include the above copyright 15*4882a593Smuzhiyun notice, this list of conditions and the following disclaimer 16*4882a593Smuzhiyun in the documentation and/or other associated materials; 17*4882a593Smuzhiyun 18*4882a593Smuzhiyun 3. the copyright holder's name is not used to endorse products 19*4882a593Smuzhiyun built using this software without specific written permission. 20*4882a593Smuzhiyun 21*4882a593Smuzhiyun ALTERNATIVELY, provided that this notice is retained in full, this product 22*4882a593Smuzhiyun may be distributed under the terms of the GNU General Public License (GPL), 23*4882a593Smuzhiyun in which case the provisions of the GPL apply INSTEAD OF those given above. 24*4882a593Smuzhiyun 25*4882a593Smuzhiyun DISCLAIMER 26*4882a593Smuzhiyun 27*4882a593Smuzhiyun This software is provided 'as is' with no explicit or implied warranties 28*4882a593Smuzhiyun in respect of its properties, including, but not limited to, correctness 29*4882a593Smuzhiyun and/or fitness for purpose. 30*4882a593Smuzhiyun --------------------------------------------------------------------------- 31*4882a593Smuzhiyun Issue Date: 30/11/2002 32*4882a593Smuzhiyun */ 33*4882a593Smuzhiyun 34*4882a593Smuzhiyun #ifndef _SHA2_H 35*4882a593Smuzhiyun #define _SHA2_H 36*4882a593Smuzhiyun 37*4882a593Smuzhiyun #ifdef USE_HOSTCC 38*4882a593Smuzhiyun #include <limits.h> 39*4882a593Smuzhiyun #endif 40*4882a593Smuzhiyun 41*4882a593Smuzhiyun /* Defines for suffixes to 32 and 64 bit unsigned numeric values */ 42*4882a593Smuzhiyun 43*4882a593Smuzhiyun #define sfx_lo(x,y) x##y 44*4882a593Smuzhiyun #define sfx_hi(x,y) sfx_lo(x,y) 45*4882a593Smuzhiyun #define n_u32(p) sfx_hi(0x##p,s_u32) 46*4882a593Smuzhiyun #define n_u64(p) sfx_hi(0x##p,s_u64) 47*4882a593Smuzhiyun 48*4882a593Smuzhiyun #ifdef USE_HOSTCC 49*4882a593Smuzhiyun /* define an unsigned 32-bit type */ 50*4882a593Smuzhiyun 51*4882a593Smuzhiyun #if UINT_MAX == 0xffffffff 52*4882a593Smuzhiyun typedef unsigned int sha2_32t; 53*4882a593Smuzhiyun #define s_u32 u 54*4882a593Smuzhiyun #elif ULONG_MAX == 0xffffffff 55*4882a593Smuzhiyun typedef unsigned long sha2_32t; 56*4882a593Smuzhiyun #define s_u32 ul 57*4882a593Smuzhiyun #else 58*4882a593Smuzhiyun #error Please define sha2_32t as an unsigned 32 bit type in sha2.h 59*4882a593Smuzhiyun #endif 60*4882a593Smuzhiyun 61*4882a593Smuzhiyun /* define an unsigned 64-bit type */ 62*4882a593Smuzhiyun 63*4882a593Smuzhiyun #if defined( _MSC_VER ) 64*4882a593Smuzhiyun typedef unsigned __int64 sha2_64t; 65*4882a593Smuzhiyun #define s_u64 ui64 66*4882a593Smuzhiyun #elif ULONG_MAX == 0xffffffffffffffff 67*4882a593Smuzhiyun typedef unsigned long sha2_64t; 68*4882a593Smuzhiyun #define s_u64 ul 69*4882a593Smuzhiyun #elif ULONG_MAX == 0xffffffff 70*4882a593Smuzhiyun typedef unsigned long long sha2_64t; /* a somewhat dangerous guess */ 71*4882a593Smuzhiyun #define s_u64 ull 72*4882a593Smuzhiyun #else 73*4882a593Smuzhiyun #error Please define sha2_64t as an unsigned 64 bit type in sha2.h 74*4882a593Smuzhiyun #endif 75*4882a593Smuzhiyun 76*4882a593Smuzhiyun #else 77*4882a593Smuzhiyun #include <common.h> 78*4882a593Smuzhiyun 79*4882a593Smuzhiyun typedef uint32_t sha2_32t; 80*4882a593Smuzhiyun #define s_u32 u 81*4882a593Smuzhiyun 82*4882a593Smuzhiyun typedef uint64_t sha2_64t; /* a somewhat dangerous guess */ 83*4882a593Smuzhiyun #define s_u64 ull 84*4882a593Smuzhiyun #endif /* USE_HOSTCC */ 85*4882a593Smuzhiyun 86*4882a593Smuzhiyun #if defined(__cplusplus) 87*4882a593Smuzhiyun extern "C" 88*4882a593Smuzhiyun { 89*4882a593Smuzhiyun #endif 90*4882a593Smuzhiyun 91*4882a593Smuzhiyun #define SHA256_DIGEST_SIZE 32 92*4882a593Smuzhiyun #define SHA384_DIGEST_SIZE 48 93*4882a593Smuzhiyun #define SHA512_DIGEST_SIZE 64 94*4882a593Smuzhiyun 95*4882a593Smuzhiyun #define SHA256_BLOCK_SIZE 64 96*4882a593Smuzhiyun #define SHA384_BLOCK_SIZE 128 97*4882a593Smuzhiyun #define SHA512_BLOCK_SIZE 128 98*4882a593Smuzhiyun 99*4882a593Smuzhiyun #define SHA2_DIGEST_SIZE SHA256_DIGEST_SIZE 100*4882a593Smuzhiyun #define SHA2_MAX_DIGEST_SIZE SHA512_DIGEST_SIZE 101*4882a593Smuzhiyun 102*4882a593Smuzhiyun #define SHA2_GOOD 0 103*4882a593Smuzhiyun #define SHA2_BAD 1 104*4882a593Smuzhiyun 105*4882a593Smuzhiyun /* type to hold the SHA256 context */ 106*4882a593Smuzhiyun 107*4882a593Smuzhiyun typedef struct { 108*4882a593Smuzhiyun sha2_32t count[2]; 109*4882a593Smuzhiyun sha2_32t hash[8]; 110*4882a593Smuzhiyun sha2_32t wbuf[16]; 111*4882a593Smuzhiyun } sha256_ctx; 112*4882a593Smuzhiyun 113*4882a593Smuzhiyun /* type to hold the SHA384/512 context */ 114*4882a593Smuzhiyun 115*4882a593Smuzhiyun typedef struct { 116*4882a593Smuzhiyun sha2_64t count[2]; 117*4882a593Smuzhiyun sha2_64t hash[8]; 118*4882a593Smuzhiyun sha2_64t wbuf[16]; 119*4882a593Smuzhiyun } sha512_ctx; 120*4882a593Smuzhiyun 121*4882a593Smuzhiyun typedef sha512_ctx sha384_ctx; 122*4882a593Smuzhiyun 123*4882a593Smuzhiyun /* type to hold a SHA2 context (256/384/512) */ 124*4882a593Smuzhiyun 125*4882a593Smuzhiyun typedef struct { 126*4882a593Smuzhiyun union { 127*4882a593Smuzhiyun sha256_ctx ctx256[1]; 128*4882a593Smuzhiyun sha512_ctx ctx512[1]; 129*4882a593Smuzhiyun } uu[1]; 130*4882a593Smuzhiyun sha2_32t sha2_len; 131*4882a593Smuzhiyun } sha2_ctx; 132*4882a593Smuzhiyun 133*4882a593Smuzhiyun void sha256_compile(sha256_ctx ctx[1]); 134*4882a593Smuzhiyun void sha512_compile(sha512_ctx ctx[1]); 135*4882a593Smuzhiyun 136*4882a593Smuzhiyun void sha256_begin(sha256_ctx ctx[1]); 137*4882a593Smuzhiyun void sha256_hash(sha256_ctx ctx[1], const unsigned char data[], unsigned long len); 138*4882a593Smuzhiyun void sha256_end(sha256_ctx ctx[1], unsigned char hval[]); 139*4882a593Smuzhiyun void sha256(unsigned char hval[], const unsigned char data[], unsigned long len); 140*4882a593Smuzhiyun 141*4882a593Smuzhiyun void sha384_begin(sha384_ctx ctx[1]); 142*4882a593Smuzhiyun #define sha384_hash sha512_hash 143*4882a593Smuzhiyun void sha384_end(sha384_ctx ctx[1], unsigned char hval[]); 144*4882a593Smuzhiyun void sha384(unsigned char hval[], const unsigned char data[], unsigned long len); 145*4882a593Smuzhiyun 146*4882a593Smuzhiyun void sha512_begin(sha512_ctx ctx[1]); 147*4882a593Smuzhiyun void sha512_hash(sha512_ctx ctx[1], const unsigned char data[], unsigned long len); 148*4882a593Smuzhiyun void sha512_end(sha512_ctx ctx[1], unsigned char hval[]); 149*4882a593Smuzhiyun void sha512(unsigned char hval[], const unsigned char data[], unsigned long len); 150*4882a593Smuzhiyun 151*4882a593Smuzhiyun int sha2_begin(sha2_ctx ctx[1], unsigned long size); 152*4882a593Smuzhiyun void sha2_hash(sha2_ctx ctx[1], const unsigned char data[], unsigned long len); 153*4882a593Smuzhiyun void sha2_end(sha2_ctx ctx[1], unsigned char hval[]); 154*4882a593Smuzhiyun int sha2(unsigned char hval[], unsigned long size, const unsigned char data[], unsigned long len); 155*4882a593Smuzhiyun 156*4882a593Smuzhiyun #if defined(__cplusplus) 157*4882a593Smuzhiyun } 158*4882a593Smuzhiyun #endif 159*4882a593Smuzhiyun 160*4882a593Smuzhiyun 161*4882a593Smuzhiyun #endif 162