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 #define SHA_64BIT
39 
40 /* define the hash functions that you need  */
41 #define SHA_2   /* for dynamic hash length  */
42 #define SHA_224
43 #define SHA_256
44 #ifdef SHA_64BIT
45 #  define SHA_384
46 #  define SHA_512
47 #  define NEED_UINT_64T
48 #endif
49 
50 #include <cryptcommon/brg_types.h>
51 
52 #if defined(__cplusplus)
53 extern "C"
54 {
55 #endif
56 
57 /* Note that the following function prototypes are the same */
58 /* for both the bit and byte oriented implementations.  But */
59 /* the length fields are in bytes or bits as is appropriate */
60 /* for the version used.  Bit sequences are arrays of bytes */
61 /* in which bit sequence indexes increase from the most to  */
62 /* the least significant end of each byte                   */
63 
64 #define SHA224_DIGEST_SIZE  28
65 #define SHA224_BLOCK_SIZE   64
66 #define SHA256_DIGEST_SIZE  32
67 #define SHA256_BLOCK_SIZE   64
68 
69 /* type to hold the SHA256 (and SHA224) context */
70 
71 typedef struct
72 {   uint_32t count[2];
73     uint_32t hash[8];
74     uint_32t wbuf[16];
75 } sha256_ctx;
76 
77 typedef sha256_ctx  sha224_ctx;
78 
79 VOID_RETURN sha256_compile(sha256_ctx ctx[1]);
80 
81 VOID_RETURN sha224_begin(sha224_ctx ctx[1]);
82 #define sha224_hash sha256_hash
83 VOID_RETURN sha224_end(unsigned char hval[], sha224_ctx ctx[1]);
84 VOID_RETURN sha224_zrtp(unsigned char hval[], const unsigned char data[], unsigned long len);
85 
86 VOID_RETURN sha256_begin(sha256_ctx ctx[1]);
87 VOID_RETURN sha256_hash(const unsigned char data[], unsigned long len, sha256_ctx ctx[1]);
88 VOID_RETURN sha256_end(unsigned char hval[], sha256_ctx ctx[1]);
89 VOID_RETURN sha256_zrtp(unsigned char hval[], const unsigned char data[], unsigned long len);
90 
91 #ifndef SHA_64BIT
92 
93 typedef struct
94 {   union
95     { sha256_ctx  ctx256[1];
96     } uu[1];
97     uint_32t    sha2_len;
98 } sha2_ctx;
99 
100 #define SHA2_MAX_DIGEST_SIZE    SHA256_DIGEST_SIZE
101 
102 #else
103 
104 #define SHA384_DIGEST_SIZE  48
105 #define SHA384_BLOCK_SIZE  128
106 #define SHA512_DIGEST_SIZE  64
107 #define SHA512_BLOCK_SIZE  128
108 #define SHA2_MAX_DIGEST_SIZE    SHA512_DIGEST_SIZE
109 
110 /* type to hold the SHA384 (and SHA512) context */
111 
112 typedef struct
113 {   uint_64t count[2];
114     uint_64t hash[8];
115     uint_64t wbuf[16];
116 } sha512_ctx;
117 
118 typedef sha512_ctx  sha384_ctx;
119 
120 typedef struct
121 {   union
122     { sha256_ctx  ctx256[1];
123       sha512_ctx  ctx512[1];
124     } uu[1];
125     uint_32t    sha2_len;
126 } sha2_ctx;
127 
128 VOID_RETURN sha512_compile(sha512_ctx ctx[1]);
129 
130 VOID_RETURN sha384_begin(sha384_ctx ctx[1]);
131 #define sha384_hash sha512_hash
132 VOID_RETURN sha384_end(unsigned char hval[], sha384_ctx ctx[1]);
133 VOID_RETURN sha384_zrtp(unsigned char hval[], const unsigned char data[], unsigned long len);
134 
135 VOID_RETURN sha512_begin(sha512_ctx ctx[1]);
136 VOID_RETURN sha512_hash(const unsigned char data[], unsigned long len, sha512_ctx ctx[1]);
137 VOID_RETURN sha512_end(unsigned char hval[], sha512_ctx ctx[1]);
138 VOID_RETURN sha512_zrtp(unsigned char hval[], const unsigned char data[], unsigned long len);
139 
140 INT_RETURN  sha2_begin(unsigned long size, sha2_ctx ctx[1]);
141 VOID_RETURN sha2_hash(const unsigned char data[], unsigned long len, sha2_ctx ctx[1]);
142 VOID_RETURN sha2_end(unsigned char hval[], sha2_ctx ctx[1]);
143 INT_RETURN  sha2_all(unsigned char hval[], unsigned long size, const unsigned char data[], unsigned long len);
144 
145 #endif
146 
147 #if defined(__cplusplus)
148 }
149 #endif
150 
151 #endif
152