sshsha.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /*
  2. * SHA core transform algorithm, used here solely as a `stirring'
  3. * function for the PuTTY random number pool. Implemented directly
  4. * from the specification by Simon Tatham.
  5. */
  6. #include "ssh.h"
  7. #define rol(x,y) ( ((x) << (y)) | (((word32)x) >> (32-y)) )
  8. void SHATransform(word32 *digest, word32 *block) {
  9. word32 w[80];
  10. word32 a,b,c,d,e;
  11. int t;
  12. for (t = 0; t < 16; t++)
  13. w[t] = block[t];
  14. for (t = 16; t < 80; t++) {
  15. word32 tmp = w[t-3] ^ w[t-8] ^ w[t-14] ^ w[t-16];
  16. w[t] = rol(tmp, 1);
  17. }
  18. a = digest[0];
  19. b = digest[1];
  20. c = digest[2];
  21. d = digest[3];
  22. e = digest[4];
  23. for (t = 0; t < 20; t++) {
  24. word32 tmp = rol(a, 5) + ( (b&c) | (d&~b) ) + e + w[t] + 0x5a827999;
  25. e = d; d = c; c = rol(b, 30); b = a; a = tmp;
  26. }
  27. for (t = 20; t < 40; t++) {
  28. word32 tmp = rol(a, 5) + (b^c^d) + e + w[t] + 0x6ed9eba1;
  29. e = d; d = c; c = rol(b, 30); b = a; a = tmp;
  30. }
  31. for (t = 40; t < 60; t++) {
  32. word32 tmp = rol(a, 5) + ( (b&c) | (b&d) | (c&d) ) + e + w[t] + 0x8f1bbcdc;
  33. e = d; d = c; c = rol(b, 30); b = a; a = tmp;
  34. }
  35. for (t = 60; t < 80; t++) {
  36. word32 tmp = rol(a, 5) + (b^c^d) + e + w[t] + 0xca62c1d6;
  37. e = d; d = c; c = rol(b, 30); b = a; a = tmp;
  38. }
  39. digest[0] += a;
  40. digest[1] += b;
  41. digest[2] += c;
  42. digest[3] += d;
  43. digest[4] += e;
  44. }