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