Align the Python and EJS ldap tests.
[Samba.git] / source / lib / crypto / sha1.c
blob1b91f8a9494ec2d92266405d976ea1c8d435d712
1 /*
2 This file contains the reference implementation of SHA-1
3 from http://www.ietf.org/rfc/rfc3174.txt
4 */
5 /*
6 * sha1.c
8 * Description:
9 * This file implements the Secure Hashing Algorithm 1 as
10 * defined in FIPS PUB 180-1 published April 17, 1995.
12 * The SHA-1, produces a 160-bit message digest for a given
13 * data stream. It should take about 2**n steps to find a
14 * message with the same digest as a given message and
15 * 2**(n/2) to find any two messages with the same digest,
16 * when n is the digest size in bits. Therefore, this
17 * algorithm can serve as a means of providing a
18 * "fingerprint" for a message.
20 * Portability Issues:
21 * SHA-1 is defined in terms of 32-bit "words". This code
22 * uses <stdint.h> (included via "sha1.h" to define 32 and 8
23 * bit unsigned integer types. If your C compiler does not
24 * support 32 bit unsigned integers, this code is not
25 * appropriate.
27 * Caveats:
28 * SHA-1 is designed to work with messages less than 2^64 bits
29 * long. Although SHA-1 allows a message digest to be generated
30 * for messages of any number of bits less than 2^64, this
31 * implementation only works with messages with a length that is
32 * a multiple of the size of an 8-bit character.
36 #include "includes.h"
38 #include "sha1.h"
41 * Define the SHA1 circular left shift macro
43 #define SHA1CircularShift(bits,word) \
44 (((word) << (bits)) | ((word) >> (32-(bits))))
46 /* Local Function Prototyptes */
47 static void SHA1PadMessage(struct SHA1Context *);
48 static void SHA1ProcessMessageBlock(struct SHA1Context *);
51 * SHA1Init (SHA1Reset in the rfc)
53 * Description:
54 * This function will initialize the SHA1Context in preparation
55 * for computing a new SHA1 message digest.
57 * Parameters:
58 * context: [in/out]
59 * The context to reset.
61 * Returns:
62 * sha Error Code.
65 int SHA1Init(struct SHA1Context *context)
67 if (!context)
69 return shaNull;
72 context->Length_Low = 0;
73 context->Length_High = 0;
74 context->Message_Block_Index = 0;
76 context->Intermediate_Hash[0] = 0x67452301;
77 context->Intermediate_Hash[1] = 0xEFCDAB89;
78 context->Intermediate_Hash[2] = 0x98BADCFE;
79 context->Intermediate_Hash[3] = 0x10325476;
80 context->Intermediate_Hash[4] = 0xC3D2E1F0;
82 context->Computed = 0;
83 context->Corrupted = 0;
85 return shaSuccess;
89 * SHA1Final (SHA1Result in the rfc)
91 * Description:
92 * This function will return the 160-bit message digest into the
93 * Message_Digest array provided by the caller.
94 * NOTE: The first octet of hash is stored in the 0th element,
95 * the last octet of hash in the 19th element.
97 * Parameters:
98 * context: [in/out]
99 * The context to use to calculate the SHA-1 hash.
100 * Message_Digest: [out]
101 * Where the digest is returned.
103 * Returns:
104 * sha Error Code.
107 int SHA1Final(uint8_t Message_Digest[SHA1HashSize],
108 struct SHA1Context *context)
110 int i;
112 if (!context || !Message_Digest)
114 return shaNull;
117 if (context->Corrupted)
119 return context->Corrupted;
122 if (!context->Computed)
124 SHA1PadMessage(context);
125 for(i=0; i<64; ++i)
127 /* message may be sensitive, clear it out */
128 context->Message_Block[i] = 0;
130 context->Length_Low = 0; /* and clear length */
131 context->Length_High = 0;
132 context->Computed = 1;
135 for(i = 0; i < SHA1HashSize; ++i)
137 Message_Digest[i] = context->Intermediate_Hash[i>>2]
138 >> 8 * ( 3 - ( i & 0x03 ) );
141 return shaSuccess;
145 * SHA1Update (SHA1Input in the rfc)
147 * Description:
148 * This function accepts an array of octets as the next portion
149 * of the message.
151 * Parameters:
152 * context: [in/out]
153 * The SHA context to update
154 * message_array: [in]
155 * An array of characters representing the next portion of
156 * the message.
157 * length: [in]
158 * The length of the message in message_array
160 * Returns:
161 * sha Error Code.
164 int SHA1Update(struct SHA1Context *context,
165 const uint8_t *message_array,
166 size_t length)
168 if (!length)
170 return shaSuccess;
173 if (!context || !message_array)
175 return shaNull;
178 if (context->Computed)
180 context->Corrupted = shaStateError;
181 return shaStateError;
184 if (context->Corrupted)
186 return context->Corrupted;
188 while(length-- && !context->Corrupted)
190 context->Message_Block[context->Message_Block_Index++] =
191 (*message_array & 0xFF);
193 context->Length_Low += 8;
194 if (context->Length_Low == 0)
196 context->Length_High++;
197 if (context->Length_High == 0)
199 /* Message is too long */
200 context->Corrupted = 1;
204 if (context->Message_Block_Index == 64)
206 SHA1ProcessMessageBlock(context);
209 message_array++;
212 return shaSuccess;
216 * SHA1ProcessMessageBlock
218 * Description:
219 * This function will process the next 512 bits of the message
220 * stored in the Message_Block array.
222 * Parameters:
223 * None.
225 * Returns:
226 * Nothing.
228 * Comments:
229 * Many of the variable names in this code, especially the
230 * single character names, were used because those were the
231 * names used in the publication.
235 static void SHA1ProcessMessageBlock(struct SHA1Context *context)
237 const uint32_t K[] = { /* Constants defined in SHA-1 */
238 0x5A827999,
239 0x6ED9EBA1,
240 0x8F1BBCDC,
241 0xCA62C1D6
243 int t; /* Loop counter */
244 uint32_t temp; /* Temporary word value */
245 uint32_t W[80]; /* Word sequence */
246 uint32_t A, B, C, D, E; /* Word buffers */
249 * Initialize the first 16 words in the array W
251 for(t = 0; t < 16; t++)
253 W[t] = context->Message_Block[t * 4] << 24;
254 W[t] |= context->Message_Block[t * 4 + 1] << 16;
255 W[t] |= context->Message_Block[t * 4 + 2] << 8;
256 W[t] |= context->Message_Block[t * 4 + 3];
259 for(t = 16; t < 80; t++)
261 W[t] = SHA1CircularShift(1,W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16]);
264 A = context->Intermediate_Hash[0];
265 B = context->Intermediate_Hash[1];
266 C = context->Intermediate_Hash[2];
267 D = context->Intermediate_Hash[3];
268 E = context->Intermediate_Hash[4];
270 for(t = 0; t < 20; t++)
272 temp = SHA1CircularShift(5,A) +
273 ((B & C) | ((~B) & D)) + E + W[t] + K[0];
274 E = D;
275 D = C;
276 C = SHA1CircularShift(30,B);
277 B = A;
278 A = temp;
281 for(t = 20; t < 40; t++)
283 temp = SHA1CircularShift(5,A) + (B ^ C ^ D) + E + W[t] + K[1];
284 E = D;
285 D = C;
286 C = SHA1CircularShift(30,B);
287 B = A;
288 A = temp;
291 for(t = 40; t < 60; t++)
293 temp = SHA1CircularShift(5,A) +
294 ((B & C) | (B & D) | (C & D)) + E + W[t] + K[2];
295 E = D;
296 D = C;
297 C = SHA1CircularShift(30,B);
298 B = A;
299 A = temp;
302 for(t = 60; t < 80; t++)
304 temp = SHA1CircularShift(5,A) + (B ^ C ^ D) + E + W[t] + K[3];
305 E = D;
306 D = C;
307 C = SHA1CircularShift(30,B);
308 B = A;
309 A = temp;
312 context->Intermediate_Hash[0] += A;
313 context->Intermediate_Hash[1] += B;
314 context->Intermediate_Hash[2] += C;
315 context->Intermediate_Hash[3] += D;
316 context->Intermediate_Hash[4] += E;
318 context->Message_Block_Index = 0;
323 * SHA1PadMessage
325 * Description:
326 * According to the standard, the message must be padded to an even
327 * 512 bits. The first padding bit must be a '1'. The last 64
328 * bits represent the length of the original message. All bits in
329 * between should be 0. This function will pad the message
330 * according to those rules by filling the Message_Block array
331 * accordingly. It will also call the ProcessMessageBlock function
332 * provided appropriately. When it returns, it can be assumed that
333 * the message digest has been computed.
335 * Parameters:
336 * context: [in/out]
337 * The context to pad
338 * ProcessMessageBlock: [in]
339 * The appropriate SHA*ProcessMessageBlock function
340 * Returns:
341 * Nothing.
345 static void SHA1PadMessage(struct SHA1Context *context)
348 * Check to see if the current message block is too small to hold
349 * the initial padding bits and length. If so, we will pad the
350 * block, process it, and then continue padding into a second
351 * block.
353 if (context->Message_Block_Index > 55)
355 context->Message_Block[context->Message_Block_Index++] = 0x80;
356 while(context->Message_Block_Index < 64)
358 context->Message_Block[context->Message_Block_Index++] = 0;
361 SHA1ProcessMessageBlock(context);
363 while(context->Message_Block_Index < 56)
365 context->Message_Block[context->Message_Block_Index++] = 0;
368 else
370 context->Message_Block[context->Message_Block_Index++] = 0x80;
371 while(context->Message_Block_Index < 56)
373 context->Message_Block[context->Message_Block_Index++] = 0;
378 * Store the message length as the last 8 octets
380 context->Message_Block[56] = context->Length_High >> 24;
381 context->Message_Block[57] = context->Length_High >> 16;
382 context->Message_Block[58] = context->Length_High >> 8;
383 context->Message_Block[59] = context->Length_High;
384 context->Message_Block[60] = context->Length_Low >> 24;
385 context->Message_Block[61] = context->Length_Low >> 16;
386 context->Message_Block[62] = context->Length_Low >> 8;
387 context->Message_Block[63] = context->Length_Low;
389 SHA1ProcessMessageBlock(context);