sha256.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #include "rar.hpp"
  2. #include "sha256.hpp"
  3. static const uint32 K[64] =
  4. {
  5. 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
  6. 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
  7. 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
  8. 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
  9. 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
  10. 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
  11. 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
  12. 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
  13. 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
  14. 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
  15. 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
  16. 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
  17. 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
  18. 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
  19. 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
  20. 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
  21. };
  22. // SHA-256 functions. We could optimize Ch and Maj a little,
  23. // but with no visible speed benefit.
  24. #define Ch(x, y, z) ((x & y) ^ (~x & z))
  25. #define Maj(x, y, z) ((x & y) ^ (x & z) ^ (y & z))
  26. // Sigma functions.
  27. #define Sg0(x) (rotr32(x, 2) ^ rotr32(x,13) ^ rotr32(x, 22))
  28. #define Sg1(x) (rotr32(x, 6) ^ rotr32(x,11) ^ rotr32(x, 25))
  29. #define sg0(x) (rotr32(x, 7) ^ rotr32(x,18) ^ (x >> 3))
  30. #define sg1(x) (rotr32(x,17) ^ rotr32(x,19) ^ (x >> 10))
  31. void sha256_init(sha256_context *ctx)
  32. {
  33. ctx->H[0] = 0x6a09e667; // Set the initial hash value.
  34. ctx->H[1] = 0xbb67ae85;
  35. ctx->H[2] = 0x3c6ef372;
  36. ctx->H[3] = 0xa54ff53a;
  37. ctx->H[4] = 0x510e527f;
  38. ctx->H[5] = 0x9b05688c;
  39. ctx->H[6] = 0x1f83d9ab;
  40. ctx->H[7] = 0x5be0cd19;
  41. ctx->Count = 0; // Processed data counter.
  42. }
  43. static void sha256_transform(sha256_context *ctx)
  44. {
  45. uint32 W[64]; // Words of message schedule.
  46. uint32 v[8]; // FIPS a, b, c, d, e, f, g, h working variables.
  47. // Prepare message schedule.
  48. for (uint I = 0; I < 16; I++)
  49. W[I] = RawGetBE4(ctx->Buffer + I * 4);
  50. for (uint I = 16; I < 64; I++)
  51. W[I] = sg1(W[I-2]) + W[I-7] + sg0(W[I-15]) + W[I-16];
  52. uint32 *H=ctx->H;
  53. v[0]=H[0]; v[1]=H[1]; v[2]=H[2]; v[3]=H[3];
  54. v[4]=H[4]; v[5]=H[5]; v[6]=H[6]; v[7]=H[7];
  55. for (uint I = 0; I < 64; I++)
  56. {
  57. uint T1 = v[7] + Sg1(v[4]) + Ch(v[4], v[5], v[6]) + K[I] + W[I];
  58. // It is possible to eliminate variable copying if we unroll loop
  59. // and rename variables every time. But my test did not show any speed
  60. // gain on i7 for such full or partial unrolling.
  61. v[7] = v[6];
  62. v[6] = v[5];
  63. v[5] = v[4];
  64. v[4] = v[3] + T1;
  65. // It works a little faster when moved here from beginning of loop.
  66. uint T2 = Sg0(v[0]) + Maj(v[0], v[1], v[2]);
  67. v[3] = v[2];
  68. v[2] = v[1];
  69. v[1] = v[0];
  70. v[0] = T1 + T2;
  71. }
  72. H[0]+=v[0]; H[1]+=v[1]; H[2]+=v[2]; H[3]+=v[3];
  73. H[4]+=v[4]; H[5]+=v[5]; H[6]+=v[6]; H[7]+=v[7];
  74. }
  75. void sha256_process(sha256_context *ctx, const void *Data, size_t Size)
  76. {
  77. const byte *Src=(const byte *)Data;
  78. size_t BufPos = (uint)ctx->Count & 0x3f;
  79. ctx->Count+=Size;
  80. while (Size > 0)
  81. {
  82. size_t BufSpace=sizeof(ctx->Buffer)-BufPos;
  83. size_t CopySize=Size>BufSpace ? BufSpace:Size;
  84. memcpy(ctx->Buffer+BufPos,Src,CopySize);
  85. Src+=CopySize;
  86. BufPos+=CopySize;
  87. Size-=CopySize;
  88. if (BufPos == 64)
  89. {
  90. BufPos = 0;
  91. sha256_transform(ctx);
  92. }
  93. }
  94. }
  95. void sha256_done(sha256_context *ctx, byte *Digest)
  96. {
  97. uint64 BitLength = ctx->Count * 8;
  98. uint BufPos = (uint)ctx->Count & 0x3f;
  99. ctx->Buffer[BufPos++] = 0x80; // Padding the message with "1" bit.
  100. if (BufPos!=56) // We need 56 bytes block followed by 8 byte length.
  101. {
  102. if (BufPos>56)
  103. {
  104. while (BufPos<64)
  105. ctx->Buffer[BufPos++] = 0;
  106. BufPos=0;
  107. }
  108. if (BufPos==0)
  109. sha256_transform(ctx);
  110. memset(ctx->Buffer+BufPos,0,56-BufPos);
  111. }
  112. RawPutBE4((uint32)(BitLength>>32), ctx->Buffer + 56);
  113. RawPutBE4((uint32)(BitLength), ctx->Buffer + 60);
  114. sha256_transform(ctx);
  115. RawPutBE4(ctx->H[0], Digest + 0);
  116. RawPutBE4(ctx->H[1], Digest + 4);
  117. RawPutBE4(ctx->H[2], Digest + 8);
  118. RawPutBE4(ctx->H[3], Digest + 12);
  119. RawPutBE4(ctx->H[4], Digest + 16);
  120. RawPutBE4(ctx->H[5], Digest + 20);
  121. RawPutBE4(ctx->H[6], Digest + 24);
  122. RawPutBE4(ctx->H[7], Digest + 28);
  123. sha256_init(ctx);
  124. }