Asterisk - The Open Source Telephony Project GIT-master-27fb039
Loading...
Searching...
No Matches
sha1.h
Go to the documentation of this file.
1/**************************** sha.h ****************************/
2/***************** See RFC 6234 for details. *******************/
3/*
4 Copyright (c) 2011 IETF Trust and the persons identified as
5 authors of the code. All rights reserved.
6
7 Redistribution and use in source and binary forms, with or
8 without modification, are permitted provided that the following
9 conditions are met:
10
11 - Redistributions of source code must retain the above
12 copyright notice, this list of conditions and
13 the following disclaimer.
14
15 - Redistributions in binary form must reproduce the above
16 copyright notice, this list of conditions and the following
17 disclaimer in the documentation and/or other materials provided
18 with the distribution.
19
20 - Neither the name of Internet Society, IETF or IETF Trust, nor
21 the names of specific contributors, may be used to endorse or
22 promote products derived from this software without specific
23 prior written permission.
24
25 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
26 CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
27 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
28 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
29 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
30 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
32 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
33 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
36 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
37 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38*/
39#ifndef _SHA1_H_
40#define _SHA1_H_
41
42/*
43 * Description:
44 * This file implements the Secure Hash Algorithms
45 * as defined in the U.S. National Institute of Standards
46 * and Technology Federal Information Processing Standards
47 * Publication (FIPS PUB) 180-3 published in October 2008
48 * and formerly defined in its predecessors, FIPS PUB 180-1
49 * and FIP PUB 180-2.
50 *
51 * A combined document showing all algorithms is available at
52 * http://csrc.nist.gov/publications/fips/
53 * fips180-3/fips180-3_final.pdf
54 *
55 * The five hashes are defined in these sizes:
56 * SHA-1 20 byte / 160 bit
57 * SHA-224 28 byte / 224 bit
58 * SHA-256 32 byte / 256 bit
59 * SHA-384 48 byte / 384 bit
60 * SHA-512 64 byte / 512 bit
61 *
62 * Compilation Note:
63 * These files may be compiled with two options:
64 * USE_32BIT_ONLY - use 32-bit arithmetic only, for systems
65 * without 64-bit integers
66 *
67 * USE_MODIFIED_MACROS - use alternate form of the SHA_Ch()
68 * and SHA_Maj() macros that are equivalent
69 * and potentially faster on many systems
70 *
71 */
72
73#include <stdint.h>
74/*
75 * If you do not have the ISO standard stdint.h header file, then you
76 * must typedef the following:
77 * name meaning
78 * uint64_t unsigned 64-bit integer
79 * uint32_t unsigned 32-bit integer
80 * uint8_t unsigned 8-bit integer (i.e., unsigned char)
81 * int_least16_t integer of >= 16 bits
82 *
83 * See stdint-example.h
84 */
85
86#ifndef _SHA_enum_
87#define _SHA_enum_
88/*
89 * All SHA functions return one of these values.
90 */
91enum {
93 shaNull, /* Null pointer parameter */
94 shaInputTooLong, /* input data too long */
95 shaStateError, /* called Input after FinalBits or Result */
96 shaBadParam /* passed a bad parameter */
97};
98#endif /* _SHA_enum_ */
99
100/*
101 * These constants hold size information for each of the SHA
102 * hashing operations
103 */
104enum {
112
117
118/*
119 * These constants are used in the USHA (Unified SHA) functions.
120 */
124
125/*
126 * This structure will hold context information for the SHA-1
127 * hashing operation.
128 */
129typedef struct SHA1Context {
130 uint32_t Intermediate_Hash[SHA1HashSize/4]; /* Message Digest */
131
132 uint32_t Length_High; /* Message length in bits */
133 uint32_t Length_Low; /* Message length in bits */
134
135 int_least16_t Message_Block_Index; /* Message_Block array index */
136 /* 512-bit message blocks */
138
139 int Computed; /* Is the hash computed? */
140 int Corrupted; /* Cumulative corruption code */
142
143/*
144 * This structure will hold context information for the SHA-256
145 * hashing operation.
146 */
147typedef struct SHA256Context {
148 uint32_t Intermediate_Hash[SHA256HashSize/4]; /* Message Digest */
149
150 uint32_t Length_High; /* Message length in bits */
151 uint32_t Length_Low; /* Message length in bits */
152
153 int_least16_t Message_Block_Index; /* Message_Block array index */
154 /* 512-bit message blocks */
156 int Computed; /* Is the hash computed? */
157 int Corrupted; /* Cumulative corruption code */
159
160/*
161 * This structure will hold context information for the SHA-512
162 * hashing operation.
163 */
164typedef struct SHA512Context {
165#ifdef USE_32BIT_ONLY
166 uint32_t Intermediate_Hash[SHA512HashSize/4]; /* Message Digest */
167 uint32_t Length[4]; /* Message length in bits */
168#else /* !USE_32BIT_ONLY */
169 uint64_t Intermediate_Hash[SHA512HashSize/8]; /* Message Digest */
170 uint64_t Length_High, Length_Low; /* Message length in bits */
171#endif /* USE_32BIT_ONLY */
172
173 int_least16_t Message_Block_Index; /* Message_Block array index */
174 /* 1024-bit message blocks */
176
177 int Computed; /* Is the hash computed?*/
178 int Corrupted; /* Cumulative corruption code */
180
181/*
182 * This structure will hold context information for the SHA-224
183 * hashing operation. It uses the SHA-256 structure for computation.
184 */
186
187/*
188 * This structure will hold context information for the SHA-384
189 * hashing operation. It uses the SHA-512 structure for computation.
190 */
192
193/*
194 * This structure holds context information for all SHA
195 * hashing operations.
196 */
205
206/*
207 * This structure will hold context information for the HMAC
208 * keyed-hashing operation.
209 */
210typedef struct HMACContext {
211 int whichSha; /* which SHA is being used */
212 int hashSize; /* hash size of SHA being used */
213 int blockSize; /* block size of SHA being used */
214 USHAContext shaContext; /* SHA context */
216 /* outer padding - key XORd with opad */
217 int Computed; /* Is the MAC computed? */
218 int Corrupted; /* Cumulative corruption code */
219
221
222/*
223 * This structure will hold context information for the HKDF
224 * extract-and-expand Key Derivation Functions.
225 */
226typedef struct HKDFContext {
227 int whichSha; /* which SHA is being used */
229 int hashSize; /* hash size of SHA being used */
230 unsigned char prk[USHAMaxHashSize];
231 /* pseudo-random key - output of hkdfInput */
232 int Computed; /* Is the key material computed? */
233 int Corrupted; /* Cumulative corruption code */
235
236/*
237 * Function Prototypes
238 */
239
240/* SHA-1 */
241extern int SHA1Reset(SHA1Context *);
242extern int SHA1Input(SHA1Context *, const uint8_t *bytes,
243 unsigned int bytecount);
244extern int SHA1FinalBits(SHA1Context *, uint8_t bits,
245 unsigned int bit_count);
246extern int SHA1Result(SHA1Context *,
247 uint8_t Message_Digest[SHA1HashSize]);
248/* SHA-224 */
250extern int SHA224Input(SHA224Context *, const uint8_t *bytes,
251 unsigned int bytecount);
252extern int SHA224FinalBits(SHA224Context *, uint8_t bits,
253 unsigned int bit_count);
255 uint8_t Message_Digest[SHA224HashSize]);
256
257/* SHA-256 */
259extern int SHA256Input(SHA256Context *, const uint8_t *bytes,
260 unsigned int bytecount);
261extern int SHA256FinalBits(SHA256Context *, uint8_t bits,
262 unsigned int bit_count);
264 uint8_t Message_Digest[SHA256HashSize]);
265
266/* SHA-384 */
268extern int SHA384Input(SHA384Context *, const uint8_t *bytes,
269 unsigned int bytecount);
270extern int SHA384FinalBits(SHA384Context *, uint8_t bits,
271 unsigned int bit_count);
273 uint8_t Message_Digest[SHA384HashSize]);
274
275/* SHA-512 */
277extern int SHA512Input(SHA512Context *, const uint8_t *bytes,
278 unsigned int bytecount);
279extern int SHA512FinalBits(SHA512Context *, uint8_t bits,
280 unsigned int bit_count);
282 uint8_t Message_Digest[SHA512HashSize]);
283
284/* Unified SHA functions, chosen by whichSha */
285extern int USHAReset(USHAContext *context, SHAversion whichSha);
286extern int USHAInput(USHAContext *context,
287 const uint8_t *bytes, unsigned int bytecount);
288extern int USHAFinalBits(USHAContext *context,
289 uint8_t bits, unsigned int bit_count);
290extern int USHAResult(USHAContext *context,
291 uint8_t Message_Digest[USHAMaxHashSize]);
292extern int USHABlockSize(enum SHAversion whichSha);
293extern int USHAHashSize(enum SHAversion whichSha);
294extern int USHAHashSizeBits(enum SHAversion whichSha);
295extern const char *USHAHashName(enum SHAversion whichSha);
296
297/************************ sha-private.h ************************/
298/***************** See RFC 6234 for details. *******************/
299/*
300 * These definitions are defined in FIPS 180-3, section 4.1.
301 * Ch() and Maj() are defined identically in sections 4.1.1,
302 * 4.1.2, and 4.1.3.
303 *
304 * The definitions used in FIPS 180-3 are as follows:
305 */
306
307#ifndef USE_MODIFIED_MACROS
308#define SHA_Ch(x,y,z) (((x) & (y)) ^ ((~(x)) & (z)))
309#define SHA_Maj(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
310#else /* USE_MODIFIED_MACROS */
311/*
312 * The following definitions are equivalent and potentially faster.
313 */
314
315#define SHA_Ch(x, y, z) (((x) & ((y) ^ (z))) ^ (z))
316#define SHA_Maj(x, y, z) (((x) & ((y) | (z))) | ((y) & (z)))
317
318#endif /* USE_MODIFIED_MACROS */
319
320#define SHA_Parity(x, y, z) ((x) ^ (y) ^ (z))
321
322#endif /* _SHA1_H_ */
int SHA512Result(SHA512Context *, uint8_t Message_Digest[SHA512HashSize])
int SHA384Result(SHA384Context *, uint8_t Message_Digest[SHA384HashSize])
int SHA512FinalBits(SHA512Context *, uint8_t bits, unsigned int bit_count)
SHAversion
Definition sha1.h:121
@ SHA512
Definition sha1.h:122
@ SHA1
Definition sha1.h:122
@ SHA384
Definition sha1.h:122
@ SHA224
Definition sha1.h:122
@ SHA256
Definition sha1.h:122
int SHA1Result(SHA1Context *, uint8_t Message_Digest[SHA1HashSize])
SHA1Result Returns the resulting 160-bit digest.
Definition sha1.c:226
int USHAHashSize(enum SHAversion whichSha)
int SHA256FinalBits(SHA256Context *, uint8_t bits, unsigned int bit_count)
int USHAInput(USHAContext *context, const uint8_t *bytes, unsigned int bytecount)
int SHA1Input(SHA1Context *, const uint8_t *bytes, unsigned int bytecount)
@ shaInputTooLong
Definition sha1.h:94
@ shaSuccess
Definition sha1.h:92
@ shaBadParam
Definition sha1.h:96
@ shaNull
Definition sha1.h:93
@ shaStateError
Definition sha1.h:95
int SHA224Input(SHA224Context *, const uint8_t *bytes, unsigned int bytecount)
int SHA384Reset(SHA384Context *)
int SHA256Input(SHA256Context *, const uint8_t *bytes, unsigned int bytecount)
int USHAHashSizeBits(enum SHAversion whichSha)
int SHA512Input(SHA512Context *, const uint8_t *bytes, unsigned int bytecount)
int SHA512Reset(SHA512Context *)
int SHA224FinalBits(SHA224Context *, uint8_t bits, unsigned int bit_count)
@ USHA_Max_Message_Block_Size
Definition sha1.h:108
@ SHA224HashSizeBits
Definition sha1.h:113
@ SHA384HashSize
Definition sha1.h:110
@ SHA384HashSizeBits
Definition sha1.h:114
@ SHA1_Message_Block_Size
Definition sha1.h:105
@ SHA256HashSize
Definition sha1.h:109
@ SHA1HashSizeBits
Definition sha1.h:113
@ SHA512HashSizeBits
Definition sha1.h:115
@ SHA384_Message_Block_Size
Definition sha1.h:106
@ SHA224HashSize
Definition sha1.h:109
@ SHA512HashSize
Definition sha1.h:110
@ USHAMaxHashSize
Definition sha1.h:111
@ SHA512_Message_Block_Size
Definition sha1.h:107
@ SHA1HashSize
Definition sha1.h:109
@ USHAMaxHashSizeBits
Definition sha1.h:115
@ SHA256_Message_Block_Size
Definition sha1.h:106
@ SHA256HashSizeBits
Definition sha1.h:114
@ SHA224_Message_Block_Size
Definition sha1.h:105
int SHA384Input(SHA384Context *, const uint8_t *bytes, unsigned int bytecount)
const char * USHAHashName(enum SHAversion whichSha)
int USHABlockSize(enum SHAversion whichSha)
int USHAResult(USHAContext *context, uint8_t Message_Digest[USHAMaxHashSize])
int USHAReset(USHAContext *context, SHAversion whichSha)
int SHA1Reset(SHA1Context *)
SHA1Reset.
Definition sha1.c:101
int USHAFinalBits(USHAContext *context, uint8_t bits, unsigned int bit_count)
int SHA224Result(SHA224Context *, uint8_t Message_Digest[SHA224HashSize])
int SHA256Result(SHA256Context *, uint8_t Message_Digest[SHA256HashSize])
int SHA224Reset(SHA224Context *)
int SHA1FinalBits(SHA1Context *, uint8_t bits, unsigned int bit_count)
SHA1FinalBits Add in any final bits of the message.
Definition sha1.c:179
int SHA256Reset(SHA256Context *)
int SHA384FinalBits(SHA384Context *, uint8_t bits, unsigned int bit_count)
int hashSize
Definition sha1.h:229
int Corrupted
Definition sha1.h:233
HMACContext hmacContext
Definition sha1.h:228
int whichSha
Definition sha1.h:227
unsigned char prk[USHAMaxHashSize]
Definition sha1.h:230
int Computed
Definition sha1.h:232
int hashSize
Definition sha1.h:212
int blockSize
Definition sha1.h:213
int Corrupted
Definition sha1.h:218
unsigned char k_opad[USHA_Max_Message_Block_Size]
Definition sha1.h:215
USHAContext shaContext
Definition sha1.h:214
int whichSha
Definition sha1.h:211
int Computed
Definition sha1.h:217
uint32_t Intermediate_Hash[SHA1HashSize/4]
Definition sha1.h:130
uint8_t Message_Block[SHA1_Message_Block_Size]
Definition sha1.h:137
uint32_t Length_Low
Definition sha1.h:133
int Corrupted
Definition sha1.h:140
uint32_t Length_High
Definition sha1.h:132
int_least16_t Message_Block_Index
Definition sha1.h:135
int Computed
Definition sha1.h:139
uint32_t Length_Low
Definition sha1.h:151
int Corrupted
Definition sha1.h:157
uint32_t Intermediate_Hash[SHA256HashSize/4]
Definition sha1.h:148
uint32_t Length_High
Definition sha1.h:150
uint8_t Message_Block[SHA256_Message_Block_Size]
Definition sha1.h:155
int_least16_t Message_Block_Index
Definition sha1.h:153
int Computed
Definition sha1.h:156
int Corrupted
Definition sha1.h:178
uint64_t Intermediate_Hash[SHA512HashSize/8]
Definition sha1.h:169
uint64_t Length_High
Definition sha1.h:170
uint64_t Length_Low
Definition sha1.h:170
uint8_t Message_Block[SHA512_Message_Block_Size]
Definition sha1.h:175
int_least16_t Message_Block_Index
Definition sha1.h:173
int Computed
Definition sha1.h:177
union USHAContext::@302 ctx
SHA384Context sha384Context
Definition sha1.h:202
SHA512Context sha512Context
Definition sha1.h:202
SHA256Context sha256Context
Definition sha1.h:201
SHA224Context sha224Context
Definition sha1.h:201
int whichSha
Definition sha1.h:198
SHA1Context sha1Context
Definition sha1.h:200