The Pedigree Project  0.1
sha1.cc
1 /*
2  * Copyright (c) 2008-2014, Pedigree Developers
3  *
4  * Please see the CONTRIB file in the root of the source tree for a full
5  * list of contributors.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 /*
21  * sha1.cpp
22  *
23  * Copyright (C) 1998, 2009
24  * Paul E. Jones <paulej@packetizer.com>
25  * All Rights Reserved.
26  *
27  *****************************************************************************
28  * $Id: sha1.cpp 12 2009-06-22 19:34:25Z paulej $
29  *****************************************************************************
30  *
31  * Description:
32  * This class implements the Secure Hashing Standard as defined
33  * in FIPS PUB 180-1 published April 17, 1995.
34  *
35  * The Secure Hashing Standard, which uses the Secure Hashing
36  * Algorithm (SHA), produces a 160-bit message digest for a
37  * given data stream. In theory, it is highly improbable that
38  * two messages will produce the same message digest. Therefore,
39  * this algorithm can serve as a means of providing a "fingerprint"
40  * for a message.
41  *
42  * Portability Issues:
43  * SHA-1 is defined in terms of 32-bit "words". This code was
44  * written with the expectation that the processor has at least
45  * a 32-bit machine word size. If the machine word size is larger,
46  * the code should still function properly. One caveat to that
47  * is that the input functions taking characters and character arrays
48  * assume that only 8 bits of information are stored in each character.
49  *
50  * Caveats:
51  * SHA-1 is designed to work with messages less than 2^64 bits long.
52  * Although SHA-1 allows a message digest to be generated for
53  * messages of any number of bits less than 2^64, this implementation
54  * only works with messages with a length that is a multiple of 8
55  * bits.
56  *
57  */
58 
59 #include "pedigree/kernel/utilities/sha1/sha1.h"
60 
61 /*
62  * SHA1
63  *
64  * Description:
65  * This is the constructor for the sha1 class.
66  *
67  * Parameters:
68  * None.
69  *
70  * Returns:
71  * Nothing.
72  *
73  * Comments:
74  *
75  */
76 SHA1::SHA1()
77  : H(), Length_Low(0), Length_High(0), Message_Block(),
78  Message_Block_Index(0), Computed(false), Corrupted(false)
79 {
80  Reset();
81 }
82 
83 /*
84  * ~SHA1
85  *
86  * Description:
87  * This is the destructor for the sha1 class
88  *
89  * Parameters:
90  * None.
91  *
92  * Returns:
93  * Nothing.
94  *
95  * Comments:
96  *
97  */
98 SHA1::~SHA1()
99 {
100  // The destructor does nothing
101 }
102 
103 /*
104  * Reset
105  *
106  * Description:
107  * This function will initialize the sha1 class member variables
108  * in preparation for computing a new message digest.
109  *
110  * Parameters:
111  * None.
112  *
113  * Returns:
114  * Nothing.
115  *
116  * Comments:
117  *
118  */
119 void SHA1::Reset()
120 {
121  Length_Low = 0;
122  Length_High = 0;
123  Message_Block_Index = 0;
124 
125  H[0] = 0x67452301;
126  H[1] = 0xEFCDAB89;
127  H[2] = 0x98BADCFE;
128  H[3] = 0x10325476;
129  H[4] = 0xC3D2E1F0;
130 
131  Computed = false;
132  Corrupted = false;
133 }
134 
135 /*
136  * Result
137  *
138  * Description:
139  * This function will return the 160-bit message digest into the
140  * array provided.
141  *
142  * Parameters:
143  * message_digest_array: [out]
144  * This is an array of five unsigned integers which will be filled
145  * with the message digest that has been computed.
146  *
147  * Returns:
148  * True if successful, false if it failed.
149  *
150  * Comments:
151  *
152  */
153 bool SHA1::Result(unsigned *message_digest_array)
154 {
155  int i; // Counter
156 
157  if (Corrupted)
158  {
159  return false;
160  }
161 
162  if (!Computed)
163  {
164  PadMessage();
165  Computed = true;
166  }
167 
168  for (i = 0; i < 5; i++)
169  {
170  message_digest_array[i] = H[i];
171  }
172 
173  return true;
174 }
175 
176 /*
177  * Input
178  *
179  * Description:
180  * This function accepts an array of octets as the next portion of
181  * the message.
182  *
183  * Parameters:
184  * message_array: [in]
185  * An array of characters representing the next portion of the
186  * message.
187  *
188  * Returns:
189  * Nothing.
190  *
191  * Comments:
192  *
193  */
194 void SHA1::Input(const unsigned char *message_array, unsigned length)
195 {
196  if (!length)
197  {
198  return;
199  }
200 
201  if (Computed || Corrupted)
202  {
203  Corrupted = true;
204  return;
205  }
206 
207  while (length-- && !Corrupted)
208  {
209  Message_Block[Message_Block_Index++] = (*message_array & 0xFF);
210 
211  Length_Low += 8;
212  Length_Low &= 0xFFFFFFFF; // Force it to 32 bits
213  if (Length_Low == 0)
214  {
215  Length_High++;
216  Length_High &= 0xFFFFFFFF; // Force it to 32 bits
217  if (Length_High == 0)
218  {
219  Corrupted = true; // Message is too long
220  }
221  }
222 
223  if (Message_Block_Index == 64)
224  {
225  ProcessMessageBlock();
226  }
227 
228  message_array++;
229  }
230 }
231 
232 /*
233  * Input
234  *
235  * Description:
236  * This function accepts an array of octets as the next portion of
237  * the message.
238  *
239  * Parameters:
240  * message_array: [in]
241  * An array of characters representing the next portion of the
242  * message.
243  * length: [in]
244  * The length of the message_array
245  *
246  * Returns:
247  * Nothing.
248  *
249  * Comments:
250  *
251  */
252 void SHA1::Input(const char *message_array, unsigned length)
253 {
254  Input(
255  reinterpret_cast<unsigned char *>(const_cast<char *>(message_array)),
256  length);
257 }
258 
259 /*
260  * Input
261  *
262  * Description:
263  * This function accepts a single octets as the next message element.
264  *
265  * Parameters:
266  * message_element: [in]
267  * The next octet in the message.
268  *
269  * Returns:
270  * Nothing.
271  *
272  * Comments:
273  *
274  */
275 void SHA1::Input(unsigned char message_element)
276 {
277  Input(&message_element, 1);
278 }
279 
280 /*
281  * Input
282  *
283  * Description:
284  * This function accepts a single octet as the next message element.
285  *
286  * Parameters:
287  * message_element: [in]
288  * The next octet in the message.
289  *
290  * Returns:
291  * Nothing.
292  *
293  * Comments:
294  *
295  */
296 void SHA1::Input(char message_element)
297 {
298  Input(reinterpret_cast<unsigned char *>(&message_element), 1);
299 }
300 
301 /*
302  * operator<<
303  *
304  * Description:
305  * This operator makes it convenient to provide character strings to
306  * the SHA1 object for processing.
307  *
308  * Parameters:
309  * message_array: [in]
310  * The character array to take as input.
311  *
312  * Returns:
313  * A reference to the SHA1 object.
314  *
315  * Comments:
316  * Each character is assumed to hold 8 bits of information.
317  *
318  */
319 SHA1 &SHA1::operator<<(const char *message_array)
320 {
321  const char *p = message_array;
322 
323  while (*p)
324  {
325  Input(*p);
326  p++;
327  }
328 
329  return *this;
330 }
331 
332 /*
333  * operator<<
334  *
335  * Description:
336  * This operator makes it convenient to provide character strings to
337  * the SHA1 object for processing.
338  *
339  * Parameters:
340  * message_array: [in]
341  * The character array to take as input.
342  *
343  * Returns:
344  * A reference to the SHA1 object.
345  *
346  * Comments:
347  * Each character is assumed to hold 8 bits of information.
348  *
349  */
350 SHA1 &SHA1::operator<<(const unsigned char *message_array)
351 {
352  const unsigned char *p = message_array;
353 
354  while (*p)
355  {
356  Input(*p);
357  p++;
358  }
359 
360  return *this;
361 }
362 
363 /*
364  * operator<<
365  *
366  * Description:
367  * This function provides the next octet in the message.
368  *
369  * Parameters:
370  * message_element: [in]
371  * The next octet in the message
372  *
373  * Returns:
374  * A reference to the SHA1 object.
375  *
376  * Comments:
377  * The character is assumed to hold 8 bits of information.
378  *
379  */
380 SHA1 &SHA1::operator<<(const char message_element)
381 {
382  Input(
383  reinterpret_cast<unsigned char *>(const_cast<char *>(&message_element)),
384  1);
385 
386  return *this;
387 }
388 
389 /*
390  * operator<<
391  *
392  * Description:
393  * This function provides the next octet in the message.
394  *
395  * Parameters:
396  * message_element: [in]
397  * The next octet in the message
398  *
399  * Returns:
400  * A reference to the SHA1 object.
401  *
402  * Comments:
403  * The character is assumed to hold 8 bits of information.
404  *
405  */
406 SHA1 &SHA1::operator<<(const unsigned char message_element)
407 {
408  Input(&message_element, 1);
409 
410  return *this;
411 }
412 
413 /*
414  * ProcessMessageBlock
415  *
416  * Description:
417  * This function will process the next 512 bits of the message
418  * stored in the Message_Block array.
419  *
420  * Parameters:
421  * None.
422  *
423  * Returns:
424  * Nothing.
425  *
426  * Comments:
427  * Many of the variable names in this function, especially the single
428  * character names, were used because those were the names used
429  * in the publication.
430  *
431  */
432 void SHA1::ProcessMessageBlock()
433 {
434  const unsigned K[] = {// Constants defined for SHA-1
435  0x5A827999, 0x6ED9EBA1, 0x8F1BBCDC, 0xCA62C1D6};
436  int t; // Loop counter
437  unsigned temp; // Temporary word value
438  unsigned W[80]; // Word sequence
439  unsigned A, B, C, D, E; // Word buffers
440 
441  /*
442  * Initialize the first 16 words in the array W
443  */
444  for (t = 0; t < 16; t++)
445  {
446  W[t] = Message_Block[t * 4] << 24;
447  W[t] |= Message_Block[t * 4 + 1] << 16;
448  W[t] |= Message_Block[t * 4 + 2] << 8;
449  W[t] |= Message_Block[t * 4 + 3];
450  }
451 
452  for (t = 16; t < 80; t++)
453  {
454  W[t] = CircularShift(1, W[t - 3] ^ W[t - 8] ^ W[t - 14] ^ W[t - 16]);
455  }
456 
457  A = H[0];
458  B = H[1];
459  C = H[2];
460  D = H[3];
461  E = H[4];
462 
463  for (t = 0; t < 20; t++)
464  {
465  temp = CircularShift(5, A) + ((B & C) | ((~B) & D)) + E + W[t] + K[0];
466  temp &= 0xFFFFFFFF;
467  E = D;
468  D = C;
469  C = CircularShift(30, B);
470  B = A;
471  A = temp;
472  }
473 
474  for (t = 20; t < 40; t++)
475  {
476  temp = CircularShift(5, A) + (B ^ C ^ D) + E + W[t] + K[1];
477  temp &= 0xFFFFFFFF;
478  E = D;
479  D = C;
480  C = CircularShift(30, B);
481  B = A;
482  A = temp;
483  }
484 
485  for (t = 40; t < 60; t++)
486  {
487  temp = CircularShift(5, A) + ((B & C) | (B & D) | (C & D)) + E + W[t] +
488  K[2];
489  temp &= 0xFFFFFFFF;
490  E = D;
491  D = C;
492  C = CircularShift(30, B);
493  B = A;
494  A = temp;
495  }
496 
497  for (t = 60; t < 80; t++)
498  {
499  temp = CircularShift(5, A) + (B ^ C ^ D) + E + W[t] + K[3];
500  temp &= 0xFFFFFFFF;
501  E = D;
502  D = C;
503  C = CircularShift(30, B);
504  B = A;
505  A = temp;
506  }
507 
508  H[0] = (H[0] + A) & 0xFFFFFFFF;
509  H[1] = (H[1] + B) & 0xFFFFFFFF;
510  H[2] = (H[2] + C) & 0xFFFFFFFF;
511  H[3] = (H[3] + D) & 0xFFFFFFFF;
512  H[4] = (H[4] + E) & 0xFFFFFFFF;
513 
514  Message_Block_Index = 0;
515 }
516 
517 /*
518  * PadMessage
519  *
520  * Description:
521  * According to the standard, the message must be padded to an even
522  * 512 bits. The first padding bit must be a '1'. The last 64 bits
523  * represent the length of the original message. All bits in between
524  * should be 0. This function will pad the message according to those
525  * rules by filling the message_block array accordingly. It will also
526  * call ProcessMessageBlock() appropriately. When it returns, it
527  * can be assumed that the message digest has been computed.
528  *
529  * Parameters:
530  * None.
531  *
532  * Returns:
533  * Nothing.
534  *
535  * Comments:
536  *
537  */
538 void SHA1::PadMessage()
539 {
540  /*
541  * Check to see if the current message block is too small to hold
542  * the initial padding bits and length. If so, we will pad the
543  * block, process it, and then continue padding into a second block.
544  */
545  if (Message_Block_Index > 55)
546  {
547  Message_Block[Message_Block_Index++] = 0x80;
548  while (Message_Block_Index < 64)
549  {
550  Message_Block[Message_Block_Index++] = 0;
551  }
552 
553  ProcessMessageBlock();
554 
555  while (Message_Block_Index < 56)
556  {
557  Message_Block[Message_Block_Index++] = 0;
558  }
559  }
560  else
561  {
562  Message_Block[Message_Block_Index++] = 0x80;
563  while (Message_Block_Index < 56)
564  {
565  Message_Block[Message_Block_Index++] = 0;
566  }
567  }
568 
569  /*
570  * Store the message length as the last 8 octets
571  */
572  Message_Block[56] = (Length_High >> 24) & 0xFF;
573  Message_Block[57] = (Length_High >> 16) & 0xFF;
574  Message_Block[58] = (Length_High >> 8) & 0xFF;
575  Message_Block[59] = (Length_High) &0xFF;
576  Message_Block[60] = (Length_Low >> 24) & 0xFF;
577  Message_Block[61] = (Length_Low >> 16) & 0xFF;
578  Message_Block[62] = (Length_Low >> 8) & 0xFF;
579  Message_Block[63] = (Length_Low) &0xFF;
580 
581  ProcessMessageBlock();
582 }
583 
584 /*
585  * CircularShift
586  *
587  * Description:
588  * This member function will perform a circular shifting operation.
589  *
590  * Parameters:
591  * bits: [in]
592  * The number of bits to shift (1-31)
593  * word: [in]
594  * The value to shift (assumes a 32-bit integer)
595  *
596  * Returns:
597  * The shifted value.
598  *
599  * Comments:
600  *
601  */
602 unsigned SHA1::CircularShift(int bits, unsigned word)
603 {
604  return ((word << bits) & 0xFFFFFFFF) | ((word & 0xFFFFFFFF) >> (32 - bits));
605 }
Definition: Input.h:26