Guest User

Untitled

a guest
Nov 22nd, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | None | 0 0
  1. void
  2. SHA256Transform(u_int32_t *state, const u_int8_t *data)
  3. {
  4. u_int32_t a, b, c, d, e, f, g, h, s0, s1;
  5. u_int32_t T1, W256[16];
  6. int j;
  7.  
  8. /* Initialize registers with the prev. intermediate value */
  9. a = state[0];
  10. b = state[1];
  11. c = state[2];
  12. d = state[3];
  13. e = state[4];
  14. f = state[5];
  15. g = state[6];
  16. h = state[7];
  17.  
  18. j = 0;
  19. do {
  20. /* Rounds 0 to 15 (unrolled): */
  21. ROUND256_0_TO_15(a,b,c,d,e,f,g,h);
  22. ROUND256_0_TO_15(h,a,b,c,d,e,f,g);
  23. ROUND256_0_TO_15(g,h,a,b,c,d,e,f);
  24. ROUND256_0_TO_15(f,g,h,a,b,c,d,e);
  25. ROUND256_0_TO_15(e,f,g,h,a,b,c,d);
  26. ROUND256_0_TO_15(d,e,f,g,h,a,b,c);
  27. ROUND256_0_TO_15(c,d,e,f,g,h,a,b);
  28. ROUND256_0_TO_15(b,c,d,e,f,g,h,a);
  29. } while (j < 16);
  30.  
  31. /* Now for the remaining rounds to 64: */
  32. do {
  33. ROUND256(a,b,c,d,e,f,g,h);
  34. ROUND256(h,a,b,c,d,e,f,g);
  35. ROUND256(g,h,a,b,c,d,e,f);
  36. ROUND256(f,g,h,a,b,c,d,e);
  37. ROUND256(e,f,g,h,a,b,c,d);
  38. ROUND256(d,e,f,g,h,a,b,c);
  39. ROUND256(c,d,e,f,g,h,a,b);
  40. ROUND256(b,c,d,e,f,g,h,a);
  41. } while (j < 64);
  42.  
  43. /* Compute the current intermediate hash value */
  44. state[0] += a;
  45. state[1] += b;
  46. state[2] += c;
  47. state[3] += d;
  48. state[4] += e;
  49. state[5] += f;
  50. state[6] += g;
  51. state[7] += h;
  52.  
  53. /* Clean up */
  54. a = b = c = d = e = f = g = h = T1 = 0;
  55. }
  56.  
  57. /* Unrolled SHA-256 round macros: */
  58.  
  59. #define ROUND256_0_TO_15(a,b,c,d,e,f,g,h) do {
  60. W256[j] = (u_int32_t)data[3] | ((u_int32_t)data[2] << 8) |
  61. ((u_int32_t)data[1] << 16) | ((u_int32_t)data[0] << 24);
  62. data += 4;
  63. T1 = (h) + Sigma1_256((e)) + Ch((e), (f), (g)) + K256[j] + W256[j];
  64. (d) += T1;
  65. (h) = T1 + Sigma0_256((a)) + Maj((a), (b), (c));
  66. j++;
  67. } while(0)
  68.  
  69. #define ROUND256(a,b,c,d,e,f,g,h) do {
  70. s0 = W256[(j+1)&0x0f];
  71. s0 = sigma0_256(s0);
  72. s1 = W256[(j+14)&0x0f];
  73. s1 = sigma1_256(s1);
  74. T1 = (h) + Sigma1_256((e)) + Ch((e), (f), (g)) + K256[j] +
  75. (W256[j&0x0f] += s1 + W256[(j+9)&0x0f] + s0);
  76. (d) += T1;
  77. (h) = T1 + Sigma0_256((a)) + Maj((a), (b), (c));
  78. j++;
  79. } while(0)
Add Comment
Please, Sign In to add comment