sha512-block.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #include <stdint.h>
  2. /* Process LEN bytes of BUFFER, accumulating context into CTX.
  3. It is assumed that LEN % 128 == 0. */
  4. void
  5. __sha512_process_block (const void *buffer, size_t len, struct sha512_ctx *ctx)
  6. {
  7. const uint64_t *words = buffer;
  8. size_t nwords = len / sizeof (uint64_t);
  9. uint64_t a = ctx->H[0];
  10. uint64_t b = ctx->H[1];
  11. uint64_t c = ctx->H[2];
  12. uint64_t d = ctx->H[3];
  13. uint64_t e = ctx->H[4];
  14. uint64_t f = ctx->H[5];
  15. uint64_t g = ctx->H[6];
  16. uint64_t h = ctx->H[7];
  17. /* First increment the byte count. FIPS 180-2 specifies the possible
  18. length of the file up to 2^128 bits. Here we only compute the
  19. number of bytes. Do a double word increment. */
  20. #ifdef USE_TOTAL128
  21. ctx->total128 += len;
  22. #else
  23. uint64_t lolen = len;
  24. ctx->total[TOTAL128_low] += lolen;
  25. ctx->total[TOTAL128_high] += ((len >> 31 >> 31 >> 2)
  26. + (ctx->total[TOTAL128_low] < lolen));
  27. #endif
  28. /* Process all bytes in the buffer with 128 bytes in each round of
  29. the loop. */
  30. while (nwords > 0)
  31. {
  32. uint64_t W[80];
  33. uint64_t a_save = a;
  34. uint64_t b_save = b;
  35. uint64_t c_save = c;
  36. uint64_t d_save = d;
  37. uint64_t e_save = e;
  38. uint64_t f_save = f;
  39. uint64_t g_save = g;
  40. uint64_t h_save = h;
  41. /* Operators defined in FIPS 180-2:4.1.2. */
  42. #define Ch(x, y, z) ((x & y) ^ (~x & z))
  43. #define Maj(x, y, z) ((x & y) ^ (x & z) ^ (y & z))
  44. #define S0(x) (CYCLIC (x, 28) ^ CYCLIC (x, 34) ^ CYCLIC (x, 39))
  45. #define S1(x) (CYCLIC (x, 14) ^ CYCLIC (x, 18) ^ CYCLIC (x, 41))
  46. #define R0(x) (CYCLIC (x, 1) ^ CYCLIC (x, 8) ^ (x >> 7))
  47. #define R1(x) (CYCLIC (x, 19) ^ CYCLIC (x, 61) ^ (x >> 6))
  48. /* It is unfortunate that C does not provide an operator for
  49. cyclic rotation. Hope the C compiler is smart enough. */
  50. #define CYCLIC(w, s) ((w >> s) | (w << (64 - s)))
  51. /* Compute the message schedule according to FIPS 180-2:6.3.2 step 2. */
  52. for (unsigned int t = 0; t < 16; ++t)
  53. {
  54. W[t] = SWAP (*words);
  55. ++words;
  56. }
  57. for (unsigned int t = 16; t < 80; ++t)
  58. W[t] = R1 (W[t - 2]) + W[t - 7] + R0 (W[t - 15]) + W[t - 16];
  59. /* The actual computation according to FIPS 180-2:6.3.2 step 3. */
  60. for (unsigned int t = 0; t < 80; ++t)
  61. {
  62. uint64_t T1 = h + S1 (e) + Ch (e, f, g) + K[t] + W[t];
  63. uint64_t T2 = S0 (a) + Maj (a, b, c);
  64. h = g;
  65. g = f;
  66. f = e;
  67. e = d + T1;
  68. d = c;
  69. c = b;
  70. b = a;
  71. a = T1 + T2;
  72. }
  73. /* Add the starting values of the context according to FIPS 180-2:6.3.2
  74. step 4. */
  75. a += a_save;
  76. b += b_save;
  77. c += c_save;
  78. d += d_save;
  79. e += e_save;
  80. f += f_save;
  81. g += g_save;
  82. h += h_save;
  83. /* Prepare for the next round. */
  84. nwords -= 16;
  85. }
  86. /* Put checksum in context given as argument. */
  87. ctx->H[0] = a;
  88. ctx->H[1] = b;
  89. ctx->H[2] = c;
  90. ctx->H[3] = d;
  91. ctx->H[4] = e;
  92. ctx->H[5] = f;
  93. ctx->H[6] = g;
  94. ctx->H[7] = h;
  95. }