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