1 /* 2 --------------------------------------------------------------------------- 3 Copyright (c) 1998-2010, Brian Gladman, Worcester, UK. All rights reserved. 4 5 The redistribution and use of this software (with or without changes) 6 is allowed without the payment of fees or royalties provided that: 7 8 source code distributions include the above copyright notice, this 9 list of conditions and the following disclaimer; 10 11 binary distributions include the above copyright notice, this list 12 of conditions and the following disclaimer in their documentation. 13 14 This software is provided 'as is' with no explicit or implied warranties 15 in respect of its operation, including, but not limited to, correctness 16 and fitness for purpose. 17 --------------------------------------------------------------------------- 18 Issue Date: 20/12/2007 19 */ 20 21 #ifndef _SHA2_H 22 #define _SHA2_H 23 24 #include <stdlib.h> 25 26 /* define for bit or byte oriented SHA */ 27 #if 1 28 # define SHA2_BITS 0 /* byte oriented */ 29 #else 30 # define SHA2_BITS 1 /* bit oriented */ 31 #endif 32 33 /* define the hash functions that you need */ 34 /* define for 64-bit SHA384 and SHA512 */ 35 #define SHA_64BIT 36 #define SHA_2 /* for dynamic hash length */ 37 #define SHA_224 38 #define SHA_256 39 #ifdef SHA_64BIT 40 # define SHA_384 41 # define SHA_512 42 # define NEED_uint64_t 43 #endif 44 45 #define SHA2_MAX_DIGEST_SIZE 64 46 #define SHA2_MAX_BLOCK_SIZE 128 47 48 #include "brg_types.h" 49 50 #if defined(__cplusplus) 51 extern "C" 52 { 53 #endif 54 55 /* Note that the following function prototypes are the same */ 56 /* for both the bit and byte oriented implementations. But */ 57 /* the length fields are in bytes or bits as is appropriate */ 58 /* for the version used. Bit sequences are arrays of bytes */ 59 /* in which bit sequence indexes increase from the most to */ 60 /* the least significant end of each byte. The value 'len' */ 61 /* in sha<nnn>_hash for the byte oriented versions of SHA2 */ 62 /* is limited to 2^29 bytes, but multiple calls will handle */ 63 /* longer data blocks. */ 64 65 #define SHA224_DIGEST_SIZE 28 66 #define SHA224_BLOCK_SIZE 64 67 68 #define SHA256_DIGEST_SIZE 32 69 #define SHA256_BLOCK_SIZE 64 70 71 /* type to hold the SHA256 (and SHA224) context */ 72 73 typedef struct 74 { uint32_t count[2]; 75 uint32_t hash[SHA256_DIGEST_SIZE >> 2]; 76 uint32_t wbuf[SHA256_BLOCK_SIZE >> 2]; 77 } sha256_ctx; 78 79 typedef sha256_ctx sha224_ctx; 80 81 VOID_RETURN sha256_compile(sha256_ctx ctx[1]); 82 83 VOID_RETURN sha224_begin(sha224_ctx ctx[1]); 84 #define sha224_hash sha256_hash 85 VOID_RETURN sha224_end(unsigned char hval[], sha224_ctx ctx[1]); 86 VOID_RETURN sha224(unsigned char hval[], const unsigned char data[], unsigned long len); 87 88 VOID_RETURN sha256_begin(sha256_ctx ctx[1]); 89 VOID_RETURN sha256_hash(const unsigned char data[], unsigned long len, sha256_ctx ctx[1]); 90 VOID_RETURN sha256_end(unsigned char hval[], sha256_ctx ctx[1]); 91 VOID_RETURN sha256(unsigned char hval[], const unsigned char data[], unsigned long len); 92 93 #ifndef SHA_64BIT 94 95 typedef struct 96 { union 97 { sha256_ctx ctx256[1]; 98 } uu[1]; 99 uint32_t sha2_len; 100 } sha2_ctx; 101 102 #else 103 104 #define SHA384_DIGEST_SIZE 48 105 #define SHA384_BLOCK_SIZE 128 106 107 #define SHA512_DIGEST_SIZE 64 108 #define SHA512_BLOCK_SIZE 128 109 110 #define SHA512_128_DIGEST_SIZE 16 111 #define SHA512_128_BLOCK_SIZE SHA512_BLOCK_SIZE 112 113 #define SHA512_192_DIGEST_SIZE 24 114 #define SHA512_192_BLOCK_SIZE SHA512_BLOCK_SIZE 115 116 #define SHA512_224_DIGEST_SIZE 28 117 #define SHA512_224_BLOCK_SIZE SHA512_BLOCK_SIZE 118 119 #define SHA512_256_DIGEST_SIZE 32 120 #define SHA512_256_BLOCK_SIZE SHA512_BLOCK_SIZE 121 122 /* type to hold the SHA384 (and SHA512) context */ 123 124 typedef struct 125 { uint64_t count[2]; 126 uint64_t hash[SHA512_DIGEST_SIZE >> 3]; 127 uint64_t wbuf[SHA512_BLOCK_SIZE >> 3]; 128 } sha512_ctx; 129 130 typedef sha512_ctx sha384_ctx; 131 132 typedef struct 133 { union 134 { sha256_ctx ctx256[1]; 135 sha512_ctx ctx512[1]; 136 } uu[1]; 137 uint32_t sha2_len; 138 } sha2_ctx; 139 140 VOID_RETURN sha512_compile(sha512_ctx ctx[1]); 141 142 VOID_RETURN sha384_begin(sha384_ctx ctx[1]); 143 #define sha384_hash sha512_hash 144 VOID_RETURN sha384_end(unsigned char hval[], sha384_ctx ctx[1]); 145 VOID_RETURN sha384(unsigned char hval[], const unsigned char data[], unsigned long len); 146 147 VOID_RETURN sha512_begin(sha512_ctx ctx[1]); 148 VOID_RETURN sha512_hash(const unsigned char data[], unsigned long len, sha512_ctx ctx[1]); 149 VOID_RETURN sha512_end(unsigned char hval[], sha512_ctx ctx[1]); 150 VOID_RETURN sha512(unsigned char hval[], const unsigned char data[], unsigned long len); 151 152 VOID_RETURN sha512_256_begin(sha512_ctx ctx[1]); 153 #define sha512_256_hash sha512_hash 154 VOID_RETURN sha512_256_end(unsigned char hval[], sha512_ctx ctx[1]); 155 VOID_RETURN sha512_256(unsigned char hval[], const unsigned char data[], unsigned long len); 156 157 VOID_RETURN sha512_224_begin(sha512_ctx ctx[1]); 158 #define sha512_224_hash sha512_hash 159 VOID_RETURN sha512_224_end(unsigned char hval[], sha512_ctx ctx[1]); 160 VOID_RETURN sha512_224(unsigned char hval[], const unsigned char data[], unsigned long len); 161 162 VOID_RETURN sha512_192_begin(sha512_ctx ctx[1]); 163 #define sha512_192_hash sha512_hash 164 VOID_RETURN sha512_192_end(unsigned char hval[], sha512_ctx ctx[1]); 165 VOID_RETURN sha512_192(unsigned char hval[], const unsigned char data[], unsigned long len); 166 167 VOID_RETURN sha512_128_begin(sha512_ctx ctx[1]); 168 #define sha512_128_hash sha512_hash 169 VOID_RETURN sha512_128_end(unsigned char hval[], sha512_ctx ctx[1]); 170 VOID_RETURN sha512_128(unsigned char hval[], const unsigned char data[], unsigned long len); 171 172 INT_RETURN sha2_begin(unsigned long size, sha2_ctx ctx[1]); 173 VOID_RETURN sha2_hash(const unsigned char data[], unsigned long len, sha2_ctx ctx[1]); 174 VOID_RETURN sha2_end(unsigned char hval[], sha2_ctx ctx[1]); 175 INT_RETURN sha2(unsigned char hval[], unsigned long size, const unsigned char data[], unsigned long len); 176 177 #endif 178 179 #if defined(__cplusplus) 180 } 181 #endif 182 183 #endif 184