cryptlib  3.4.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros
sha2.h
Go to the documentation of this file.
1 /*
2  ---------------------------------------------------------------------------
3  Copyright (c) 2002, Dr Brian Gladman, Worcester, UK. All rights reserved.
4 
5  LICENSE TERMS
6 
7  The free distribution and use of this software in both source and binary
8  form is allowed (with or without changes) provided that:
9 
10  1. distributions of this source code include the above copyright
11  notice, this list of conditions and the following disclaimer;
12 
13  2. distributions in binary form include the above copyright
14  notice, this list of conditions and the following disclaimer
15  in the documentation and/or other associated materials;
16 
17  3. the copyright holder's name is not used to endorse products
18  built using this software without specific written permission.
19 
20  ALTERNATIVELY, provided that this notice is retained in full, this product
21  may be distributed under the terms of the GNU General Public License (GPL),
22  in which case the provisions of the GPL apply INSTEAD OF those given above.
23 
24  DISCLAIMER
25 
26  This software is provided 'as is' with no explicit or implied warranties
27  in respect of its properties, including, but not limited to, correctness
28  and/or fitness for purpose.
29  ---------------------------------------------------------------------------
30  Issue Date: 01/08/2005
31 */
32 
33 #ifndef _SHA2_H
34 #define _SHA2_H
35 
36 #include <stdlib.h>
37 
38 #include "crypt.h" /* For USE_SHA2_EXT define via config.h */
39 #ifdef USE_SHA2_EXT /* pcg */
40  #define SHA_64BIT
41 #endif /* USE_SHA2_EXT */
42 
43 /* define the hash functions that you need */
44 #define SHA_2 /* for dynamic hash length */
45 #define SHA_224
46 #define SHA_256
47 #ifdef SHA_64BIT
48 # define SHA_384
49 # define SHA_512
50 # define NEED_UINT_64T
51 #endif
52 
53 #if defined( INC_ALL )
54  #include "brg_types.h"
55 #else
56  #include "crypt/brg_types.h"
57 #endif
58 
59 #if defined(__cplusplus)
60 extern "C"
61 {
62 #endif
63 
64 /* Note that the following function prototypes are the same */
65 /* for both the bit and byte oriented implementations. But */
66 /* the length fields are in bytes or bits as is appropriate */
67 /* for the version used. Bit sequences are arrays of bytes */
68 /* in which bit sequence indexes increase from the most to */
69 /* the least significant end of each byte */
70 
71 #define SHA224_DIGEST_SIZE 28
72 #define SHA224_BLOCK_SIZE 64
73 #define SHA256_DIGEST_SIZE 32
74 #define SHA256_BLOCK_SIZE 64
75 
76 /* type to hold the SHA256 (and SHA224) context */
77 
78 typedef struct
79 { uint_32t count[2];
80  uint_32t hash[8];
81  uint_32t wbuf[16];
82 } sha256_ctx;
83 
85 
87 
88 VOID_RETURN sha224_begin(sha224_ctx ctx[1]);
89 #define sha224_hash sha256_hash
90 VOID_RETURN sha224_end(unsigned char hval[], sha224_ctx ctx[1]);
91 VOID_RETURN sha224(unsigned char hval[], const unsigned char data[], unsigned long len);
92 
94 VOID_RETURN sha256_hash(const unsigned char data[], unsigned long len, sha256_ctx ctx[1]);
95 VOID_RETURN sha256_end(unsigned char hval[], sha256_ctx ctx[1]);
96 VOID_RETURN sha256(unsigned char hval[], const unsigned char data[], unsigned long len);
97 
98 #ifndef SHA_64BIT
99 
100 typedef struct
101 { union
102  { sha256_ctx ctx256[1];
103  } uu[1];
104  uint_32t sha2_len;
105 } sha2_ctx;
106 
107 #define SHA2_MAX_DIGEST_SIZE SHA256_DIGEST_SIZE
108 
109 #else
110 
111 #define SHA384_DIGEST_SIZE 48
112 #define SHA384_BLOCK_SIZE 128
113 #define SHA512_DIGEST_SIZE 64
114 #define SHA512_BLOCK_SIZE 128
115 #define SHA2_MAX_DIGEST_SIZE SHA512_DIGEST_SIZE
116 
117 /* type to hold the SHA384 (and SHA512) context */
118 
119 typedef struct
120 { uint_64t count[2];
121  uint_64t hash[8];
122  uint_64t wbuf[16];
123 } sha512_ctx;
124 
125 typedef sha512_ctx sha384_ctx;
126 
127 typedef struct
128 { union
129  { sha256_ctx ctx256[1];
130  sha512_ctx ctx512[1];
131  } uu[1];
132  uint_32t sha2_len;
133 } sha2_ctx;
134 
135 VOID_RETURN sha512_compile(sha512_ctx ctx[1]);
136 
137 VOID_RETURN sha384_begin(sha384_ctx ctx[1]);
138 #define sha384_hash sha512_hash
139 VOID_RETURN sha384_end(unsigned char hval[], sha384_ctx ctx[1]);
140 VOID_RETURN sha384(unsigned char hval[], const unsigned char data[], unsigned long len);
141 
142 VOID_RETURN sha512_begin(sha512_ctx ctx[1]);
143 VOID_RETURN sha512_hash(const unsigned char data[], unsigned long len, sha512_ctx ctx[1]);
144 VOID_RETURN sha512_end(unsigned char hval[], sha512_ctx ctx[1]);
145 VOID_RETURN sha512(unsigned char hval[], const unsigned char data[], unsigned long len);
146 
147 INT_RETURN sha2_begin(unsigned long size, sha2_ctx ctx[1]);
148 VOID_RETURN sha2_hash(const unsigned char data[], unsigned long len, sha2_ctx ctx[1]);
149 VOID_RETURN sha2_end(unsigned char hval[], sha2_ctx ctx[1]);
150 INT_RETURN sha2(unsigned char hval[], unsigned long size, const unsigned char data[], unsigned long len);
151 
152 #endif
153 
154 #if defined(__cplusplus)
155 }
156 #endif
157 
158 #endif