1 | // Copyright (c) Microsoft. All rights reserved.
|
---|
2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
---|
3 |
|
---|
4 | /**************************** sha1.c ****************************/
|
---|
5 | /******************** See RFC 4634 for details ******************/
|
---|
6 | /*
|
---|
7 | * Description:
|
---|
8 | * This file implements the Secure Hash Signature Standard
|
---|
9 | * algorithms as defined in the National Institute of Standards
|
---|
10 | * and Technology Federal Information Processing Standards
|
---|
11 | * Publication (FIPS PUB) 180-1 published on April 17, 1995, 180-2
|
---|
12 | * published on August 1, 2002, and the FIPS PUB 180-2 Change
|
---|
13 | * Notice published on February 28, 2004.
|
---|
14 | *
|
---|
15 | * A combined document showing all algorithms is available at
|
---|
16 | * http://csrc.nist.gov/publications/fips/
|
---|
17 | * fips180-2/fips180-2withchangenotice.pdf
|
---|
18 | *
|
---|
19 | * The SHA-1 algorithm produces a 160-bit message digest for a
|
---|
20 | * given data stream. It should take about 2**n steps to find a
|
---|
21 | * message with the same digest as a given message and
|
---|
22 | * 2**(n/2) to find any two messages with the same digest,
|
---|
23 | * when n is the digest size in bits. Therefore, this
|
---|
24 | * algorithm can serve as a means of providing a
|
---|
25 | * "fingerprint" for a message.
|
---|
26 | *
|
---|
27 | * Portability Issues:
|
---|
28 | * SHA-1 is defined in terms of 32-bit "words". This code
|
---|
29 | * uses <stdint.h> (included via "sha.h") to define 32 and 8
|
---|
30 | * bit unsigned integer types. If your C compiler does not
|
---|
31 | * support 32 bit unsigned integers, this code is not
|
---|
32 | * appropriate.
|
---|
33 | *
|
---|
34 | * Caveats:
|
---|
35 | * SHA-1 is designed to work with messages less than 2^64 bits
|
---|
36 | * long. This implementation uses SHA1Input() to hash the bits
|
---|
37 | * that are a multiple of the size of an 8-bit character, and then
|
---|
38 | * uses SHA1FinalBits() to hash the final few bits of the input.
|
---|
39 | */
|
---|
40 |
|
---|
41 | #include <stdlib.h>
|
---|
42 | #include "azure_c_shared_utility/gballoc.h"
|
---|
43 |
|
---|
44 | #include "azure_c_shared_utility/sha.h"
|
---|
45 | #include "azure_c_shared_utility/sha-private.h"
|
---|
46 |
|
---|
47 | /*
|
---|
48 | * Define the SHA1 circular left shift macro
|
---|
49 | */
|
---|
50 | #define SHA1_ROTL(bits,word) \
|
---|
51 | (((word) << (bits)) | ((word) >> (32-(bits))))
|
---|
52 |
|
---|
53 | /*
|
---|
54 | * add "length" to the length
|
---|
55 | */
|
---|
56 | #define SHA1AddLength(context, length) \
|
---|
57 | (addTemp = (context)->Length_Low, \
|
---|
58 | (context)->Corrupted = \
|
---|
59 | (((context)->Length_Low += (length)) < addTemp) && \
|
---|
60 | (++(context)->Length_High == 0) ? 1 : 0)
|
---|
61 |
|
---|
62 | /* Local Function Prototypes */
|
---|
63 | static void SHA1Finalize(SHA1Context *context, uint8_t Pad_Byte);
|
---|
64 | static void SHA1PadMessage(SHA1Context *, uint8_t Pad_Byte);
|
---|
65 | static void SHA1ProcessMessageBlock(SHA1Context *);
|
---|
66 |
|
---|
67 | /*
|
---|
68 | * SHA1Reset
|
---|
69 | *
|
---|
70 | * Description:
|
---|
71 | * This function will initialize the SHA1Context in preparation
|
---|
72 | * for computing a new SHA1 message digest.
|
---|
73 | *
|
---|
74 | * Parameters:
|
---|
75 | * context: [in/out]
|
---|
76 | * The context to reset.
|
---|
77 | *
|
---|
78 | * Returns:
|
---|
79 | * sha Error Code.
|
---|
80 | *
|
---|
81 | */
|
---|
82 | int SHA1Reset(SHA1Context *context)
|
---|
83 | {
|
---|
84 | if (!context)
|
---|
85 | return shaNull;
|
---|
86 |
|
---|
87 | context->Length_Low = 0;
|
---|
88 | context->Length_High = 0;
|
---|
89 | context->Message_Block_Index = 0;
|
---|
90 |
|
---|
91 |
|
---|
92 | /* Initial Hash Values: FIPS-180-2 section 5.3.1 */
|
---|
93 | context->Intermediate_Hash[0] = 0x67452301;
|
---|
94 | context->Intermediate_Hash[1] = 0xEFCDAB89;
|
---|
95 | context->Intermediate_Hash[2] = 0x98BADCFE;
|
---|
96 | context->Intermediate_Hash[3] = 0x10325476;
|
---|
97 | context->Intermediate_Hash[4] = 0xC3D2E1F0;
|
---|
98 |
|
---|
99 | context->Computed = 0;
|
---|
100 | context->Corrupted = 0;
|
---|
101 |
|
---|
102 | return shaSuccess;
|
---|
103 | }
|
---|
104 |
|
---|
105 | /*
|
---|
106 | * SHA1Input
|
---|
107 | *
|
---|
108 | * Description:
|
---|
109 | * This function accepts an array of octets as the next portion
|
---|
110 | * of the message.
|
---|
111 | *
|
---|
112 | * Parameters:
|
---|
113 | * context: [in/out]
|
---|
114 | * The SHA context to update
|
---|
115 | * message_array: [in]
|
---|
116 | * An array of characters representing the next portion of
|
---|
117 | * the message.
|
---|
118 | * length: [in]
|
---|
119 | * The length of the message in message_array
|
---|
120 | *
|
---|
121 | * Returns:
|
---|
122 | * sha Error Code.
|
---|
123 | *
|
---|
124 | */
|
---|
125 | int SHA1Input(SHA1Context *context, const uint8_t *message_array, unsigned int length)
|
---|
126 | {
|
---|
127 | int result;
|
---|
128 | uint32_t addTemp;
|
---|
129 | if (!length)
|
---|
130 | {
|
---|
131 | result = shaSuccess;
|
---|
132 | }
|
---|
133 | else if (!context || !message_array)
|
---|
134 | {
|
---|
135 | result = shaNull;
|
---|
136 | }
|
---|
137 | else if (context->Computed)
|
---|
138 | {
|
---|
139 | result = context->Corrupted = shaStateError;
|
---|
140 | }
|
---|
141 | else if (context->Corrupted)
|
---|
142 | {
|
---|
143 | result = context->Corrupted;
|
---|
144 | }
|
---|
145 | else
|
---|
146 | {
|
---|
147 | while (length-- && !context->Corrupted)
|
---|
148 | {
|
---|
149 | context->Message_Block[context->Message_Block_Index++] = (*message_array & 0xFF);
|
---|
150 |
|
---|
151 | if (!SHA1AddLength(context, 8) && (context->Message_Block_Index == SHA1_Message_Block_Size))
|
---|
152 | {
|
---|
153 | SHA1ProcessMessageBlock(context);
|
---|
154 | }
|
---|
155 | message_array++;
|
---|
156 | }
|
---|
157 | result = shaSuccess;
|
---|
158 | }
|
---|
159 | return result;
|
---|
160 | }
|
---|
161 |
|
---|
162 | /*
|
---|
163 | * SHA1FinalBits
|
---|
164 | *
|
---|
165 | * Description:
|
---|
166 | * This function will add in any final bits of the message.
|
---|
167 | *
|
---|
168 | * Parameters:
|
---|
169 | * context: [in/out]
|
---|
170 | * The SHA context to update
|
---|
171 | * message_bits: [in]
|
---|
172 | * The final bits of the message, in the upper portion of the
|
---|
173 | * byte. (Use 0b###00000 instead of 0b00000### to input the
|
---|
174 | * three bits ###.)
|
---|
175 | * length: [in]
|
---|
176 | * The number of bits in message_bits, between 1 and 7.
|
---|
177 | *
|
---|
178 | * Returns:
|
---|
179 | * sha Error Code.
|
---|
180 | */
|
---|
181 | int SHA1FinalBits(SHA1Context *context, const uint8_t message_bits,
|
---|
182 | unsigned int length)
|
---|
183 | {
|
---|
184 | uint32_t addTemp;
|
---|
185 |
|
---|
186 | uint8_t masks[8] = {
|
---|
187 | /* 0 0b00000000 */ 0x00, /* 1 0b10000000 */ 0x80,
|
---|
188 | /* 2 0b11000000 */ 0xC0, /* 3 0b11100000 */ 0xE0,
|
---|
189 | /* 4 0b11110000 */ 0xF0, /* 5 0b11111000 */ 0xF8,
|
---|
190 | /* 6 0b11111100 */ 0xFC, /* 7 0b11111110 */ 0xFE
|
---|
191 | };
|
---|
192 | uint8_t markbit[8] = {
|
---|
193 | /* 0 0b10000000 */ 0x80, /* 1 0b01000000 */ 0x40,
|
---|
194 | /* 2 0b00100000 */ 0x20, /* 3 0b00010000 */ 0x10,
|
---|
195 | /* 4 0b00001000 */ 0x08, /* 5 0b00000100 */ 0x04,
|
---|
196 | /* 6 0b00000010 */ 0x02, /* 7 0b00000001 */ 0x01
|
---|
197 | };
|
---|
198 |
|
---|
199 | if (!length)
|
---|
200 | return shaSuccess;
|
---|
201 |
|
---|
202 | if (!context)
|
---|
203 | return shaNull;
|
---|
204 |
|
---|
205 | if (context->Computed || (length >= 8) || (length == 0)) {
|
---|
206 | context->Corrupted = shaStateError;
|
---|
207 | return shaStateError;
|
---|
208 | }
|
---|
209 |
|
---|
210 | if (context->Corrupted)
|
---|
211 | return context->Corrupted;
|
---|
212 |
|
---|
213 | SHA1AddLength(context, length);
|
---|
214 | SHA1Finalize(context,
|
---|
215 | (uint8_t)((message_bits & masks[length]) | markbit[length]));
|
---|
216 |
|
---|
217 | return shaSuccess;
|
---|
218 | }
|
---|
219 |
|
---|
220 | /*
|
---|
221 | * SHA1Result
|
---|
222 | *
|
---|
223 | * Description:
|
---|
224 | * This function will return the 160-bit message digest into the
|
---|
225 | * Message_Digest array provided by the caller.
|
---|
226 | * NOTE: The first octet of hash is stored in the 0th element,
|
---|
227 | * the last octet of hash in the 19th element.
|
---|
228 | *
|
---|
229 | * Parameters:
|
---|
230 | * context: [in/out]
|
---|
231 | * The context to use to calculate the SHA-1 hash.
|
---|
232 | * Message_Digest: [out]
|
---|
233 | * Where the digest is returned.
|
---|
234 | *
|
---|
235 | * Returns:
|
---|
236 | * sha Error Code.
|
---|
237 | *
|
---|
238 | */
|
---|
239 | int SHA1Result(SHA1Context *context,
|
---|
240 | uint8_t Message_Digest[SHA1HashSize])
|
---|
241 | {
|
---|
242 | int i;
|
---|
243 |
|
---|
244 | if (!context || !Message_Digest)
|
---|
245 | return shaNull;
|
---|
246 |
|
---|
247 | if (context->Corrupted)
|
---|
248 | return context->Corrupted;
|
---|
249 |
|
---|
250 | if (!context->Computed)
|
---|
251 | SHA1Finalize(context, 0x80);
|
---|
252 |
|
---|
253 | for (i = 0; i < SHA1HashSize; ++i)
|
---|
254 | Message_Digest[i] = (uint8_t)(context->Intermediate_Hash[i >> 2]
|
---|
255 | >> 8 * (3 - (i & 0x03)));
|
---|
256 |
|
---|
257 | return shaSuccess;
|
---|
258 | }
|
---|
259 |
|
---|
260 | /*
|
---|
261 | * SHA1Finalize
|
---|
262 | *
|
---|
263 | * Description:
|
---|
264 | * This helper function finishes off the digest calculations.
|
---|
265 | *
|
---|
266 | * Parameters:
|
---|
267 | * context: [in/out]
|
---|
268 | * The SHA context to update
|
---|
269 | * Pad_Byte: [in]
|
---|
270 | * The last byte to add to the digest before the 0-padding
|
---|
271 | * and length. This will contain the last bits of the message
|
---|
272 | * followed by another single bit. If the message was an
|
---|
273 | * exact multiple of 8-bits long, Pad_Byte will be 0x80.
|
---|
274 | *
|
---|
275 | * Returns:
|
---|
276 | * sha Error Code.
|
---|
277 | *
|
---|
278 | */
|
---|
279 | static void SHA1Finalize(SHA1Context *context, uint8_t Pad_Byte)
|
---|
280 | {
|
---|
281 | int i;
|
---|
282 | SHA1PadMessage(context, Pad_Byte);
|
---|
283 | /* message may be sensitive, clear it out */
|
---|
284 | for (i = 0; i < SHA1_Message_Block_Size; ++i)
|
---|
285 | context->Message_Block[i] = 0;
|
---|
286 | context->Length_Low = 0; /* and clear length */
|
---|
287 | context->Length_High = 0;
|
---|
288 | context->Computed = 1;
|
---|
289 | }
|
---|
290 |
|
---|
291 | /*
|
---|
292 | * SHA1PadMessage
|
---|
293 | *
|
---|
294 | * Description:
|
---|
295 | * According to the standard, the message must be padded to an
|
---|
296 | * even 512 bits. The first padding bit must be a '1'. The last
|
---|
297 | * 64 bits represent the length of the original message. All bits
|
---|
298 | * in between should be 0. This helper function will pad the
|
---|
299 | * message according to those rules by filling the Message_Block
|
---|
300 | * array accordingly. When it returns, it can be assumed that the
|
---|
301 | * message digest has been computed.
|
---|
302 | *
|
---|
303 | * Parameters:
|
---|
304 | * context: [in/out]
|
---|
305 | * The context to pad
|
---|
306 | * Pad_Byte: [in]
|
---|
307 | * The last byte to add to the digest before the 0-padding
|
---|
308 | * and length. This will contain the last bits of the message
|
---|
309 | * followed by another single bit. If the message was an
|
---|
310 | * exact multiple of 8-bits long, Pad_Byte will be 0x80.
|
---|
311 | *
|
---|
312 | * Returns:
|
---|
313 | * Nothing.
|
---|
314 | */
|
---|
315 | static void SHA1PadMessage(SHA1Context *context, uint8_t Pad_Byte)
|
---|
316 | {
|
---|
317 | /*
|
---|
318 | * Check to see if the current message block is too small to hold
|
---|
319 | * the initial padding bits and length. If so, we will pad the
|
---|
320 | * block, process it, and then continue padding into a second
|
---|
321 | * block.
|
---|
322 | */
|
---|
323 | if (context->Message_Block_Index >= (SHA1_Message_Block_Size - 8)) {
|
---|
324 | context->Message_Block[context->Message_Block_Index++] = Pad_Byte;
|
---|
325 | while (context->Message_Block_Index < SHA1_Message_Block_Size)
|
---|
326 | context->Message_Block[context->Message_Block_Index++] = 0;
|
---|
327 |
|
---|
328 | SHA1ProcessMessageBlock(context);
|
---|
329 | }
|
---|
330 | else
|
---|
331 | context->Message_Block[context->Message_Block_Index++] = Pad_Byte;
|
---|
332 |
|
---|
333 | while (context->Message_Block_Index < (SHA1_Message_Block_Size - 8))
|
---|
334 | context->Message_Block[context->Message_Block_Index++] = 0;
|
---|
335 |
|
---|
336 | /*
|
---|
337 | * Store the message length as the last 8 octets
|
---|
338 | */
|
---|
339 | context->Message_Block[56] = (uint8_t)(context->Length_High >> 24);
|
---|
340 | context->Message_Block[57] = (uint8_t)(context->Length_High >> 16);
|
---|
341 |
|
---|
342 | context->Message_Block[58] = (uint8_t)(context->Length_High >> 8);
|
---|
343 | context->Message_Block[59] = (uint8_t)(context->Length_High);
|
---|
344 | context->Message_Block[60] = (uint8_t)(context->Length_Low >> 24);
|
---|
345 | context->Message_Block[61] = (uint8_t)(context->Length_Low >> 16);
|
---|
346 | context->Message_Block[62] = (uint8_t)(context->Length_Low >> 8);
|
---|
347 | context->Message_Block[63] = (uint8_t)(context->Length_Low);
|
---|
348 |
|
---|
349 | SHA1ProcessMessageBlock(context);
|
---|
350 | }
|
---|
351 |
|
---|
352 | /*
|
---|
353 | * SHA1ProcessMessageBlock
|
---|
354 | *
|
---|
355 | * Description:
|
---|
356 | * This helper function will process the next 512 bits of the
|
---|
357 | * message stored in the Message_Block array.
|
---|
358 | *
|
---|
359 | * Parameters:
|
---|
360 | * None.
|
---|
361 | *
|
---|
362 | * Returns:
|
---|
363 | * Nothing.
|
---|
364 | *
|
---|
365 | * Comments:
|
---|
366 | * Many of the variable names in this code, especially the
|
---|
367 | * single character names, were used because those were the
|
---|
368 | * names used in the publication.
|
---|
369 | */
|
---|
370 | static void SHA1ProcessMessageBlock(SHA1Context *context)
|
---|
371 | {
|
---|
372 | /* Constants defined in FIPS-180-2, section 4.2.1 */
|
---|
373 | const uint32_t K[4] = {
|
---|
374 | 0x5A827999, 0x6ED9EBA1, 0x8F1BBCDC, 0xCA62C1D6
|
---|
375 | };
|
---|
376 | int t; /* Loop counter */
|
---|
377 | uint32_t temp; /* Temporary word value */
|
---|
378 | uint32_t W[80]; /* Word sequence */
|
---|
379 | uint32_t A, B, C, D, E; /* Word buffers */
|
---|
380 |
|
---|
381 | /*
|
---|
382 | * Initialize the first 16 words in the array W
|
---|
383 | */
|
---|
384 | for (t = 0; t < 16; t++) {
|
---|
385 | W[t] = ((uint32_t)context->Message_Block[t * 4]) << 24;
|
---|
386 | W[t] |= ((uint32_t)context->Message_Block[t * 4 + 1]) << 16;
|
---|
387 | W[t] |= ((uint32_t)context->Message_Block[t * 4 + 2]) << 8;
|
---|
388 | W[t] |= ((uint32_t)context->Message_Block[t * 4 + 3]);
|
---|
389 | }
|
---|
390 |
|
---|
391 | for (t = 16; t < 80; t++)
|
---|
392 | W[t] = SHA1_ROTL(1, W[t - 3] ^ W[t - 8] ^ W[t - 14] ^ W[t - 16]);
|
---|
393 |
|
---|
394 | A = context->Intermediate_Hash[0];
|
---|
395 | B = context->Intermediate_Hash[1];
|
---|
396 | C = context->Intermediate_Hash[2];
|
---|
397 | D = context->Intermediate_Hash[3];
|
---|
398 | E = context->Intermediate_Hash[4];
|
---|
399 |
|
---|
400 | for (t = 0; t < 20; t++) {
|
---|
401 | temp = SHA1_ROTL(5, A) + SHA_Ch(B, C, D) + E + W[t] + K[0];
|
---|
402 | E = D;
|
---|
403 | D = C;
|
---|
404 | C = SHA1_ROTL(30, B);
|
---|
405 | B = A;
|
---|
406 | A = temp;
|
---|
407 | }
|
---|
408 |
|
---|
409 | for (t = 20; t < 40; t++) {
|
---|
410 | temp = SHA1_ROTL(5, A) + SHA_Parity(B, C, D) + E + W[t] + K[1];
|
---|
411 | E = D;
|
---|
412 | D = C;
|
---|
413 | C = SHA1_ROTL(30, B);
|
---|
414 | B = A;
|
---|
415 | A = temp;
|
---|
416 | }
|
---|
417 |
|
---|
418 | for (t = 40; t < 60; t++) {
|
---|
419 | temp = SHA1_ROTL(5, A) + SHA_Maj(B, C, D) + E + W[t] + K[2];
|
---|
420 | E = D;
|
---|
421 | D = C;
|
---|
422 | C = SHA1_ROTL(30, B);
|
---|
423 | B = A;
|
---|
424 | A = temp;
|
---|
425 | }
|
---|
426 |
|
---|
427 | for (t = 60; t < 80; t++) {
|
---|
428 | temp = SHA1_ROTL(5, A) + SHA_Parity(B, C, D) + E + W[t] + K[3];
|
---|
429 | E = D;
|
---|
430 | D = C;
|
---|
431 | C = SHA1_ROTL(30, B);
|
---|
432 | B = A;
|
---|
433 | A = temp;
|
---|
434 | }
|
---|
435 |
|
---|
436 | context->Intermediate_Hash[0] += A;
|
---|
437 | context->Intermediate_Hash[1] += B;
|
---|
438 | context->Intermediate_Hash[2] += C;
|
---|
439 |
|
---|
440 | context->Intermediate_Hash[3] += D;
|
---|
441 | context->Intermediate_Hash[4] += E;
|
---|
442 |
|
---|
443 | context->Message_Block_Index = 0;
|
---|
444 | }
|
---|
445 |
|
---|