59 #include "pedigree/kernel/utilities/sha1/sha1.h" 77 : H(), Length_Low(0), Length_High(0), Message_Block(),
78 Message_Block_Index(0), Computed(false), Corrupted(false)
123 Message_Block_Index = 0;
153 bool SHA1::Result(
unsigned *message_digest_array)
168 for (i = 0; i < 5; i++)
170 message_digest_array[i] = H[i];
194 void SHA1::Input(
const unsigned char *message_array,
unsigned length)
201 if (Computed || Corrupted)
207 while (length-- && !Corrupted)
209 Message_Block[Message_Block_Index++] = (*message_array & 0xFF);
212 Length_Low &= 0xFFFFFFFF;
216 Length_High &= 0xFFFFFFFF;
217 if (Length_High == 0)
223 if (Message_Block_Index == 64)
225 ProcessMessageBlock();
252 void SHA1::Input(
const char *message_array,
unsigned length)
255 reinterpret_cast<unsigned char *>(const_cast<char *>(message_array)),
275 void SHA1::Input(
unsigned char message_element)
277 Input(&message_element, 1);
296 void SHA1::Input(
char message_element)
298 Input(reinterpret_cast<unsigned char *>(&message_element), 1);
319 SHA1 &SHA1::operator<<(
const char *message_array)
321 const char *p = message_array;
350 SHA1 &SHA1::operator<<(
const unsigned char *message_array)
352 const unsigned char *p = message_array;
380 SHA1 &SHA1::operator<<(
const char message_element)
383 reinterpret_cast<unsigned char *>(const_cast<char *>(&message_element)),
406 SHA1 &SHA1::operator<<(
const unsigned char message_element)
408 Input(&message_element, 1);
432 void SHA1::ProcessMessageBlock()
434 const unsigned K[] = {
435 0x5A827999, 0x6ED9EBA1, 0x8F1BBCDC, 0xCA62C1D6};
439 unsigned A, B, C, D, E;
444 for (t = 0; t < 16; t++)
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];
452 for (t = 16; t < 80; t++)
454 W[t] = CircularShift(1, W[t - 3] ^ W[t - 8] ^ W[t - 14] ^ W[t - 16]);
463 for (t = 0; t < 20; t++)
465 temp = CircularShift(5, A) + ((B & C) | ((~B) & D)) + E + W[t] + K[0];
469 C = CircularShift(30, B);
474 for (t = 20; t < 40; t++)
476 temp = CircularShift(5, A) + (B ^ C ^ D) + E + W[t] + K[1];
480 C = CircularShift(30, B);
485 for (t = 40; t < 60; t++)
487 temp = CircularShift(5, A) + ((B & C) | (B & D) | (C & D)) + E + W[t] +
492 C = CircularShift(30, B);
497 for (t = 60; t < 80; t++)
499 temp = CircularShift(5, A) + (B ^ C ^ D) + E + W[t] + K[3];
503 C = CircularShift(30, B);
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;
514 Message_Block_Index = 0;
538 void SHA1::PadMessage()
545 if (Message_Block_Index > 55)
547 Message_Block[Message_Block_Index++] = 0x80;
548 while (Message_Block_Index < 64)
550 Message_Block[Message_Block_Index++] = 0;
553 ProcessMessageBlock();
555 while (Message_Block_Index < 56)
557 Message_Block[Message_Block_Index++] = 0;
562 Message_Block[Message_Block_Index++] = 0x80;
563 while (Message_Block_Index < 56)
565 Message_Block[Message_Block_Index++] = 0;
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;
581 ProcessMessageBlock();
602 unsigned SHA1::CircularShift(
int bits,
unsigned word)
604 return ((word << bits) & 0xFFFFFFFF) | ((word & 0xFFFFFFFF) >> (32 - bits));