SHA1.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * Copyright (c) 2020, Ali Mohammad Pur <ali.mpfard@gmail.com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/Endian.h>
  27. #include <AK/Types.h>
  28. #include <LibCrypto/Hash/SHA1.h>
  29. namespace Crypto {
  30. namespace Hash {
  31. inline static constexpr auto ROTATE_LEFT(u32 value, size_t bits)
  32. {
  33. return (value << bits) | (value >> (32 - bits));
  34. }
  35. inline void SHA1::transform(const u8* data)
  36. {
  37. u32 blocks[80];
  38. for (size_t i = 0; i < 16; ++i)
  39. blocks[i] = AK::convert_between_host_and_network_endian(((const u32*)data)[i]);
  40. // w[i] = (w[i-3] xor w[i-8] xor w[i-14] xor w[i-16]) leftrotate 1
  41. for (size_t i = 16; i < Rounds; ++i)
  42. blocks[i] = ROTATE_LEFT(blocks[i - 3] ^ blocks[i - 8] ^ blocks[i - 14] ^ blocks[i - 16], 1);
  43. auto a = m_state[0], b = m_state[1], c = m_state[2], d = m_state[3], e = m_state[4];
  44. u32 f, k;
  45. for (size_t i = 0; i < Rounds; ++i) {
  46. if (i <= 19) {
  47. f = (b & c) | ((~b) & d);
  48. k = SHA1Constants::RoundConstants[0];
  49. } else if (i <= 39) {
  50. f = b ^ c ^ d;
  51. k = SHA1Constants::RoundConstants[1];
  52. } else if (i <= 59) {
  53. f = (b & c) | (b & d) | (c & d);
  54. k = SHA1Constants::RoundConstants[2];
  55. } else {
  56. f = b ^ c ^ d;
  57. k = SHA1Constants::RoundConstants[3];
  58. }
  59. auto temp = ROTATE_LEFT(a, 5) + f + e + k + blocks[i];
  60. e = d;
  61. d = c;
  62. c = ROTATE_LEFT(b, 30);
  63. b = a;
  64. a = temp;
  65. }
  66. m_state[0] += a;
  67. m_state[1] += b;
  68. m_state[2] += c;
  69. m_state[3] += d;
  70. m_state[4] += e;
  71. // "security" measures, as if SHA1 is secure
  72. a = 0;
  73. b = 0;
  74. c = 0;
  75. d = 0;
  76. e = 0;
  77. __builtin_memset(blocks, 0, 16 * sizeof(u32));
  78. }
  79. void SHA1::update(const u8* message, size_t length)
  80. {
  81. for (size_t i = 0; i < length; ++i) {
  82. if (m_data_length == BlockSize) {
  83. transform(m_data_buffer);
  84. m_bit_length += 512;
  85. m_data_length = 0;
  86. }
  87. m_data_buffer[m_data_length++] = message[i];
  88. }
  89. }
  90. SHA1::DigestType SHA1::digest()
  91. {
  92. auto digest = peek();
  93. reset();
  94. return digest;
  95. }
  96. SHA1::DigestType SHA1::peek()
  97. {
  98. DigestType digest;
  99. size_t i = m_data_length;
  100. // make a local copy of the data as we modify it
  101. u8 data[BlockSize];
  102. u32 state[5];
  103. __builtin_memcpy(data, m_data_buffer, m_data_length);
  104. __builtin_memcpy(state, m_state, 20);
  105. if (BlockSize == m_data_length) {
  106. transform(m_data_buffer);
  107. m_bit_length += BlockSize * 8;
  108. m_data_length = 0;
  109. i = 0;
  110. }
  111. if (m_data_length < FinalBlockDataSize) {
  112. m_data_buffer[i++] = 0x80;
  113. while (i < FinalBlockDataSize)
  114. m_data_buffer[i++] = 0x00;
  115. } else {
  116. // First, complete a block with some padding.
  117. m_data_buffer[i++] = 0x80;
  118. while (i < BlockSize)
  119. m_data_buffer[i++] = 0x00;
  120. transform(m_data_buffer);
  121. // Then start another block with BlockSize - 8 bytes of zeros
  122. __builtin_memset(m_data_buffer, 0, FinalBlockDataSize);
  123. }
  124. // append total message length
  125. m_bit_length += m_data_length * 8;
  126. m_data_buffer[BlockSize - 1] = m_bit_length;
  127. m_data_buffer[BlockSize - 2] = m_bit_length >> 8;
  128. m_data_buffer[BlockSize - 3] = m_bit_length >> 16;
  129. m_data_buffer[BlockSize - 4] = m_bit_length >> 24;
  130. m_data_buffer[BlockSize - 5] = m_bit_length >> 32;
  131. m_data_buffer[BlockSize - 6] = m_bit_length >> 40;
  132. m_data_buffer[BlockSize - 7] = m_bit_length >> 48;
  133. m_data_buffer[BlockSize - 8] = m_bit_length >> 56;
  134. transform(m_data_buffer);
  135. for (size_t i = 0; i < 4; ++i) {
  136. digest.data[i + 0] = (m_state[0] >> (24 - i * 8)) & 0x000000ff;
  137. digest.data[i + 4] = (m_state[1] >> (24 - i * 8)) & 0x000000ff;
  138. digest.data[i + 8] = (m_state[2] >> (24 - i * 8)) & 0x000000ff;
  139. digest.data[i + 12] = (m_state[3] >> (24 - i * 8)) & 0x000000ff;
  140. digest.data[i + 16] = (m_state[4] >> (24 - i * 8)) & 0x000000ff;
  141. }
  142. // restore the data
  143. __builtin_memcpy(m_data_buffer, data, m_data_length);
  144. __builtin_memcpy(m_state, state, 20);
  145. return digest;
  146. }
  147. }
  148. }