sha1.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #include "sha1.h"
  2. #include <memory.h>
  3. #if defined(_MSC_VER)
  4. #include <intrin.h>
  5. #define _bswap _byteswap_ulong
  6. #define _bswap64 _byteswap_uint64
  7. #elif defined(__GNUC__)
  8. #include <x86intrin.h>
  9. #endif
  10. #define SHA1_BLOCKSIZE 64
  11. void accelc_SHA1_init(SHA1_BUFFER* HashBuffer) {
  12. HashBuffer->dword[0] = 0x67452301;
  13. HashBuffer->dword[1] = 0xEFCDAB89;
  14. HashBuffer->dword[2] = 0x98BADCFE;
  15. HashBuffer->dword[3] = 0x10325476;
  16. HashBuffer->dword[4] = 0xC3D2E1F0;
  17. }
  18. void accelc_SHA1_update(const void* __restrict srcBytes, size_t srcBytesLength,
  19. SHA1_BUFFER* __restrict HashBuffer) {
  20. uint32_t Buffer[80] = { 0 };
  21. uint32_t a, b, c, d, e;
  22. const uint32_t (*MessageBlock)[16] = srcBytes;
  23. size_t RoundsOfMainCycle = srcBytesLength / SHA1_BLOCKSIZE;
  24. for (size_t i = 0; i < RoundsOfMainCycle; ++i) {
  25. for (int j = 0; j < 16; ++j)
  26. Buffer[j] = _bswap(MessageBlock[i][j]);
  27. for (int j = 16; j < 80; ++j) {
  28. uint32_t temp = Buffer[j - 3] ^ Buffer[j - 8] ^ Buffer[j - 14] ^ Buffer[j - 16];
  29. Buffer[j] = _rotl(temp, 1);
  30. }
  31. a = HashBuffer->dword[0];
  32. b = HashBuffer->dword[1];
  33. c = HashBuffer->dword[2];
  34. d = HashBuffer->dword[3];
  35. e = HashBuffer->dword[4];
  36. for (int j = 0; j < 20; ++j) {
  37. uint32_t T = _rotl(a, 5);
  38. T += ((b & c) ^ (~b & d)) + e + 0x5A827999 + Buffer[j];
  39. e = d;
  40. d = c;
  41. c = _rotl(b, 30);
  42. b = a;
  43. a = T;
  44. }
  45. for (int j = 20; j < 40; ++j) {
  46. uint32_t T = _rotl(a, 5);
  47. T += (b ^ c ^ d) + e + 0x6ED9EBA1 + Buffer[j];
  48. e = d;
  49. d = c;
  50. c = _rotl(b, 30);
  51. b = a;
  52. a = T;
  53. }
  54. for (int j = 40; j < 60; ++j) {
  55. uint32_t T = _rotl(a, 5);
  56. T += ((b & c) ^ (b & d) ^ (c & d)) + e + 0x8F1BBCDC + Buffer[j];
  57. e = d;
  58. d = c;
  59. c = _rotl(b, 30);
  60. b = a;
  61. a = T;
  62. }
  63. for (int j = 60; j < 80; ++j) {
  64. uint32_t T = _rotl(a, 5);
  65. T += (b ^ c ^ d) + e + 0xCA62C1D6 + Buffer[j];
  66. e = d;
  67. d = c;
  68. c = _rotl(b, 30);
  69. b = a;
  70. a = T;
  71. }
  72. HashBuffer->dword[0] += a;
  73. HashBuffer->dword[1] += b;
  74. HashBuffer->dword[2] += c;
  75. HashBuffer->dword[3] += d;
  76. HashBuffer->dword[4] += e;
  77. }
  78. }
  79. void accelc_SHA1_final(const void* __restrict LeftBytes, size_t LeftBytesLength, uint64_t TotalBytesLength,
  80. const SHA1_BUFFER* HashBuffer, SHA1_DIGEST* Hash) {
  81. if (HashBuffer != Hash)
  82. memcpy(Hash, HashBuffer, sizeof(SHA1_BUFFER));
  83. if (LeftBytesLength >= SHA1_BLOCKSIZE) {
  84. accelc_SHA1_update(LeftBytes, LeftBytesLength, Hash);
  85. LeftBytes = (const uint8_t*)LeftBytes + (LeftBytesLength / SHA1_BLOCKSIZE) * SHA1_BLOCKSIZE;
  86. LeftBytesLength %= SHA1_BLOCKSIZE;
  87. }
  88. uint8_t Extra[128] = { 0 };
  89. for (size_t i = 0; i < LeftBytesLength; ++i)
  90. Extra[i] = ((const uint8_t*)LeftBytes)[i];
  91. Extra[LeftBytesLength] = 0x80;
  92. *(uint64_t*)(Extra + (LeftBytesLength >= 64 - 8 ? 128 - 8 : 64 - 8)) = _bswap64(TotalBytesLength * 8);
  93. accelc_SHA1_update(Extra, LeftBytesLength >= 56 ? 128 : 64, Hash);
  94. Hash->dword[0] = _bswap(Hash->dword[0]);
  95. Hash->dword[1] = _bswap(Hash->dword[1]);
  96. Hash->dword[2] = _bswap(Hash->dword[2]);
  97. Hash->dword[3] = _bswap(Hash->dword[3]);
  98. Hash->dword[4] = _bswap(Hash->dword[4]);
  99. }
  100. void accelc_SHA1(const void* __restrict srcBytes, size_t srclen,
  101. SHA1_DIGEST* __restrict Hash) {
  102. accelc_SHA1_init(Hash);
  103. accelc_SHA1_update(srcBytes, srclen, Hash);
  104. accelc_SHA1_final((uint8_t*)srcBytes + (srclen / SHA1_BLOCKSIZE) * SHA1_BLOCKSIZE, srclen % SHA1_BLOCKSIZE, srclen, Hash, Hash);
  105. }