excrypt_sha.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include "excrypt.h"
  4. // SHA1 code based on https://github.com/mohaps/TinySHA1
  5. void sha1_process_block(EXCRYPT_SHA_STATE* state)
  6. {
  7. uint32_t w[80];
  8. for (size_t i = 0; i < 16; i++) {
  9. w[i] = ((uint32_t)state->buffer[i * 4 + 0] << 24);
  10. w[i] |= ((uint32_t)state->buffer[i * 4 + 1] << 16);
  11. w[i] |= ((uint32_t)state->buffer[i * 4 + 2] << 8);
  12. w[i] |= ((uint32_t)state->buffer[i * 4 + 3]);
  13. }
  14. for (size_t i = 16; i < 80; i++) {
  15. w[i] = ROTL32((w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16]), 1);
  16. }
  17. uint32_t a = state->state[0];
  18. uint32_t b = state->state[1];
  19. uint32_t c = state->state[2];
  20. uint32_t d = state->state[3];
  21. uint32_t e = state->state[4];
  22. for (int i = 0; i < 80; ++i) {
  23. uint32_t f = 0;
  24. uint32_t k = 0;
  25. if (i < 20) {
  26. f = (b & c) | (~b & d);
  27. k = 0x5A827999;
  28. }
  29. else if (i < 40) {
  30. f = b ^ c ^ d;
  31. k = 0x6ED9EBA1;
  32. }
  33. else if (i < 60) {
  34. f = (b & c) | (b & d) | (c & d);
  35. k = 0x8F1BBCDC;
  36. }
  37. else {
  38. f = b ^ c ^ d;
  39. k = 0xCA62C1D6;
  40. }
  41. uint32_t temp = ROTL32(a, 5) + f + e + k + w[i];
  42. e = d;
  43. d = c;
  44. c = ROTL32(b, 30);
  45. b = a;
  46. a = temp;
  47. }
  48. state->state[0] += a;
  49. state->state[1] += b;
  50. state->state[2] += c;
  51. state->state[3] += d;
  52. state->state[4] += e;
  53. }
  54. void sha1_process_byte(EXCRYPT_SHA_STATE* state, uint8_t octet)
  55. {
  56. uint32_t offset = state->count++ & 0x3F;
  57. state->buffer[offset] = octet;
  58. if ((state->count & 0x3F) == 0)
  59. {
  60. sha1_process_block(state);
  61. }
  62. }
  63. void ExCryptShaInit(EXCRYPT_SHA_STATE* state)
  64. {
  65. state->count = 0;
  66. state->state[0] = 0x67452301;
  67. state->state[1] = 0xEFCDAB89;
  68. state->state[2] = 0x98BADCFE;
  69. state->state[3] = 0x10325476;
  70. state->state[4] = 0xC3D2E1F0;
  71. }
  72. void ExCryptShaUpdate(EXCRYPT_SHA_STATE* state, const uint8_t* input, uint32_t input_size)
  73. {
  74. for (uint32_t i = 0; i < input_size; i++)
  75. {
  76. sha1_process_byte(state, input[i]);
  77. }
  78. }
  79. void ExCryptShaFinal(EXCRYPT_SHA_STATE* state, uint8_t* output, uint32_t output_size)
  80. {
  81. (void)output_size;
  82. uint64_t bit_count = (uint64_t)state->count * 8;
  83. sha1_process_byte(state, 0x80);
  84. if ((state->count & 0x3F) > 56)
  85. {
  86. while ((state->count & 0x3F) != 0)
  87. {
  88. sha1_process_byte(state, 0);
  89. }
  90. while ((state->count & 0x3F) < 56)
  91. {
  92. sha1_process_byte(state, 0);
  93. }
  94. }
  95. else
  96. {
  97. while ((state->count & 0x3F) < 56)
  98. {
  99. sha1_process_byte(state, 0);
  100. }
  101. }
  102. sha1_process_byte(state, 0);
  103. sha1_process_byte(state, 0);
  104. sha1_process_byte(state, 0);
  105. sha1_process_byte(state, 0);
  106. sha1_process_byte(state, (uint8_t)((bit_count >> 24) & 0xFF));
  107. sha1_process_byte(state, (uint8_t)((bit_count >> 16) & 0xFF));
  108. sha1_process_byte(state, (uint8_t)((bit_count >> 8) & 0xFF));
  109. sha1_process_byte(state, (uint8_t)((bit_count) & 0xFF));
  110. //sha1_process_block(state);
  111. uint32_t result[5];
  112. result[0] = SWAP32(state->state[0]);
  113. result[1] = SWAP32(state->state[1]);
  114. result[2] = SWAP32(state->state[2]);
  115. result[3] = SWAP32(state->state[3]);
  116. result[4] = SWAP32(state->state[4]);
  117. memcpy(output, result, 0x14);
  118. }
  119. void ExCryptSha(const uint8_t* input1, uint32_t input1_size, const uint8_t* input2, uint32_t input2_size,
  120. const uint8_t* input3, uint32_t input3_size, uint8_t* output, uint32_t output_size)
  121. {
  122. EXCRYPT_SHA_STATE state[1];
  123. ExCryptShaInit(state);
  124. if (input1 && input1_size)
  125. {
  126. ExCryptShaUpdate(state, input1, input1_size);
  127. }
  128. if (input2 && input2_size)
  129. {
  130. ExCryptShaUpdate(state, input2, input2_size);
  131. }
  132. if (input3 && input3_size)
  133. {
  134. ExCryptShaUpdate(state, input3, input3_size);
  135. }
  136. ExCryptShaFinal(state, output, output_size);
  137. }