Support for withheld or missing CID string
[asterisk-bristuff.git] / main / sha1.c
blob16ddd6aa39bf3ade18b8aa97221dc20a44d463e8
1 /*
3 * Based on the RFC 3174
4 *
5 * Full Copyright Statement
7 * Copyright (C) The Internet Society (2001). All Rights Reserved.
9 * This document and translations of it may be copied and furnished to
10 * others, and derivative works that comment on or otherwise explain it
11 * or assist in its implementation may be prepared, copied, published
12 * and distributed, in whole or in part, without restriction of any
13 * kind, provided that the above copyright notice and this paragraph are
14 * included on all such copies and derivative works. However, this
15 * document itself may not be modified in any way, such as by removing
16 * the copyright notice or references to the Internet Society or other
17 * Internet organizations, except as needed for the purpose of
18 * developing Internet standards in which case the procedures for
19 * copyrights defined in the Internet Standards process must be
20 * followed, or as required to translate it into languages other than
21 * English.
23 * The limited permissions granted above are perpetual and will not be
24 * revoked by the Internet Society or its successors or assigns.
26 * This document and the information contained herein is provided on an
27 * "AS IS" basis and THE INTERNET SOCIETY AND THE INTERNET ENGINEERING
28 * TASK FORCE DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED, INCLUDING
29 * BUT NOT LIMITED TO ANY WARRANTY THAT THE USE OF THE INFORMATION
30 * HEREIN WILL NOT INFRINGE ANY RIGHTS OR ANY IMPLIED WARRANTIES OF
31 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
35 * Description:
36 * This file implements the Secure Hashing Algorithm 1 as
37 * defined in FIPS PUB 180-1 published April 17, 1995.
39 * The SHA-1, produces a 160-bit message digest for a given
40 * data stream. It should take about 2**n steps to find a
41 * message with the same digest as a given message and
42 * 2**(n/2) to find any two messages with the same digest,
43 * when n is the digest size in bits. Therefore, this
44 * algorithm can serve as a means of providing a
45 * "fingerprint" for a message.
47 * Portability Issues:
48 * SHA-1 is defined in terms of 32-bit "words". This code
49 * uses <stdint.h> (included via "sha1.h" to define 32 and 8
50 * bit unsigned integer types. If your C compiler does not
51 * support 32 bit unsigned integers, this code is not
52 * appropriate.
54 * Caveats:
55 * SHA-1 is designed to work with messages less than 2^64 bits
56 * long. Although SHA-1 allows a message digest to be generated
57 * for messages of any number of bits less than 2^64, this
58 * implementation only works with messages with a length that is
59 * a multiple of the size of an 8-bit character.
64 #include "asterisk/sha1.h"
67 * Define the SHA1 circular left shift macro
69 #define SHA1CircularShift(bits,word) \
70 (((word) << (bits)) | ((word) >> (32-(bits))))
72 /* Local Function Prototyptes */
73 void SHA1PadMessage(SHA1Context *);
74 void SHA1ProcessMessageBlock(SHA1Context *);
77 * SHA1Reset
79 * Description:
80 * This function will initialize the SHA1Context in preparation
81 * for computing a new SHA1 message digest.
83 * Parameters:
84 * context: [in/out]
85 * The context to reset.
87 * Returns:
88 * sha Error Code.
91 int SHA1Reset(SHA1Context *context)
93 if (!context) {
94 return shaNull;
97 context->Length_Low = 0;
98 context->Length_High = 0;
99 context->Message_Block_Index = 0;
101 context->Intermediate_Hash[0] = 0x67452301;
102 context->Intermediate_Hash[1] = 0xEFCDAB89;
103 context->Intermediate_Hash[2] = 0x98BADCFE;
104 context->Intermediate_Hash[3] = 0x10325476;
105 context->Intermediate_Hash[4] = 0xC3D2E1F0;
107 context->Computed = 0;
108 context->Corrupted = 0;
110 return shaSuccess;
114 * SHA1Result
116 * Description:
117 * This function will return the 160-bit message digest into the
118 * Message_Digest array provided by the caller.
119 * NOTE: The first octet of hash is stored in the 0th element,
120 * the last octet of hash in the 19th element.
122 * Parameters:
123 * context: [in/out]
124 * The context to use to calculate the SHA-1 hash.
125 * Message_Digest: [out]
126 * Where the digest is returned.
128 * Returns:
129 * sha Error Code.
132 int SHA1Result( SHA1Context *context,
133 uint8_t Message_Digest[SHA1HashSize])
135 int i;
137 if (!context || !Message_Digest) {
138 return shaNull;
141 if (context->Corrupted) {
142 return context->Corrupted;
145 if (!context->Computed) {
146 SHA1PadMessage(context);
147 for (i = 0; i < 64; ++i) {
148 /* message may be sensitive, clear it out */
149 context->Message_Block[i] = 0;
151 context->Length_Low = 0; /* and clear length */
152 context->Length_High = 0;
153 context->Computed = 1;
156 for (i = 0; i < SHA1HashSize; ++i) {
157 Message_Digest[i] = context->Intermediate_Hash[i >> 2] >> 8 * ( 3 - ( i & 0x03 ) );
160 return shaSuccess;
164 * SHA1Input
166 * Description:
167 * This function accepts an array of octets as the next portion
168 * of the message.
170 * Parameters:
171 * context: [in/out]
172 * The SHA context to update
173 * message_array: [in]
174 * An array of characters representing the next portion of
175 * the message.
176 * length: [in]
177 * The length of the message in message_array
179 * Returns:
180 * sha Error Code.
183 int SHA1Input(SHA1Context *context, const uint8_t *message_array, unsigned length)
185 if (!length) {
186 return shaSuccess;
189 if (!context || !message_array) {
190 return shaNull;
193 if (context->Computed) {
194 context->Corrupted = shaStateError;
195 return shaStateError;
198 if (context->Corrupted) {
199 return context->Corrupted;
202 while (length-- && !context->Corrupted) {
203 context->Message_Block[context->Message_Block_Index++] = (*message_array & 0xFF);
205 context->Length_Low += 8;
206 if (context->Length_Low == 0) {
207 context->Length_High++;
208 if (context->Length_High == 0) {
209 /* Message is too long */
210 context->Corrupted = 1;
214 if (context->Message_Block_Index == 64) {
215 SHA1ProcessMessageBlock(context);
218 message_array++;
221 return shaSuccess;
225 * SHA1ProcessMessageBlock
227 * Description:
228 * This function will process the next 512 bits of the message
229 * stored in the Message_Block array.
231 * Parameters:
232 * None.
234 * Returns:
235 * Nothing.
237 * Comments:
238 * Many of the variable names in this code, especially the
239 * single character names, were used because those were the
240 * names used in the publication.
244 void SHA1ProcessMessageBlock(SHA1Context *context)
246 const uint32_t K[] = { /* Constants defined in SHA-1 */
247 0x5A827999,
248 0x6ED9EBA1,
249 0x8F1BBCDC,
250 0xCA62C1D6
252 int t; /* Loop counter */
253 uint32_t temp; /* Temporary word value */
254 uint32_t W[80]; /* Word sequence */
255 uint32_t A, B, C, D, E; /* Word buffers */
258 * Initialize the first 16 words in the array W
260 for (t = 0; t < 16; t++) {
261 W[t] = context->Message_Block[t * 4] << 24;
262 W[t] |= context->Message_Block[t * 4 + 1] << 16;
263 W[t] |= context->Message_Block[t * 4 + 2] << 8;
264 W[t] |= context->Message_Block[t * 4 + 3];
267 for (t = 16; t < 80; t++) {
268 W[t] = SHA1CircularShift(1,W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16]);
271 A = context->Intermediate_Hash[0];
272 B = context->Intermediate_Hash[1];
273 C = context->Intermediate_Hash[2];
274 D = context->Intermediate_Hash[3];
275 E = context->Intermediate_Hash[4];
277 for (t = 0; t < 20; t++) {
278 temp = SHA1CircularShift(5,A) + ((B & C) | ((~B) & D)) + E + W[t] + K[0];
279 E = D;
280 D = C;
281 C = SHA1CircularShift(30,B);
282 B = A;
283 A = temp;
286 for (t = 20; t < 40; t++) {
287 temp = SHA1CircularShift(5,A) + (B ^ C ^ D) + E + W[t] + K[1];
288 E = D;
289 D = C;
290 C = SHA1CircularShift(30,B);
291 B = A;
292 A = temp;
295 for (t = 40; t < 60; t++) {
296 temp = SHA1CircularShift(5,A) + ((B & C) | (B & D) | (C & D)) + E + W[t] + K[2];
297 E = D;
298 D = C;
299 C = SHA1CircularShift(30,B);
300 B = A;
301 A = temp;
304 for (t = 60; t < 80; t++) {
305 temp = SHA1CircularShift(5,A) + (B ^ C ^ D) + E + W[t] + K[3];
306 E = D;
307 D = C;
308 C = SHA1CircularShift(30,B);
309 B = A;
310 A = temp;
313 context->Intermediate_Hash[0] += A;
314 context->Intermediate_Hash[1] += B;
315 context->Intermediate_Hash[2] += C;
316 context->Intermediate_Hash[3] += D;
317 context->Intermediate_Hash[4] += E;
319 context->Message_Block_Index = 0;
324 * SHA1PadMessage
326 * Description:
327 * According to the standard, the message must be padded to an even
328 * 512 bits. The first padding bit must be a '1'. The last 64
329 * bits represent the length of the original message. All bits in
330 * between should be 0. This function will pad the message
331 * according to those rules by filling the Message_Block array
332 * accordingly. It will also call the ProcessMessageBlock function
333 * provided appropriately. When it returns, it can be assumed that
334 * the message digest has been computed.
336 * Parameters:
337 * context: [in/out]
338 * The context to pad
339 * ProcessMessageBlock: [in]
340 * The appropriate SHA*ProcessMessageBlock function
341 * Returns:
342 * Nothing.
346 void SHA1PadMessage(SHA1Context *context)
349 * Check to see if the current message block is too small to hold
350 * the initial padding bits and length. If so, we will pad the
351 * block, process it, and then continue padding into a second
352 * block.
354 if (context->Message_Block_Index > 55) {
355 context->Message_Block[context->Message_Block_Index++] = 0x80;
356 while (context->Message_Block_Index < 64) {
357 context->Message_Block[context->Message_Block_Index++] = 0;
360 SHA1ProcessMessageBlock(context);
362 while (context->Message_Block_Index < 56) {
363 context->Message_Block[context->Message_Block_Index++] = 0;
365 } else {
366 context->Message_Block[context->Message_Block_Index++] = 0x80;
367 while (context->Message_Block_Index < 56) {
368 context->Message_Block[context->Message_Block_Index++] = 0;
373 * Store the message length as the last 8 octets
375 context->Message_Block[56] = context->Length_High >> 24;
376 context->Message_Block[57] = context->Length_High >> 16;
377 context->Message_Block[58] = context->Length_High >> 8;
378 context->Message_Block[59] = context->Length_High;
379 context->Message_Block[60] = context->Length_Low >> 24;
380 context->Message_Block[61] = context->Length_Low >> 16;
381 context->Message_Block[62] = context->Length_Low >> 8;
382 context->Message_Block[63] = context->Length_Low;
384 SHA1ProcessMessageBlock(context);