sha1.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * SHA transform algorithm, originally taken from code written by
  3. * Peter Gutmann, and placed in the public domain.
  4. */
  5. static uint32_t
  6. rol32(uint32_t word, int shift)
  7. {
  8. return (word << shift) | (word >> (32 - shift));
  9. }
  10. /* The SHA f()-functions. */
  11. #define f1(x,y,z) (z ^ (x & (y ^ z))) /* x ? y : z */
  12. #define f2(x,y,z) (x ^ y ^ z) /* XOR */
  13. #define f3(x,y,z) ((x & y) + (z & (x ^ y))) /* majority */
  14. /* The SHA Mysterious Constants */
  15. #define K1 0x5A827999L /* Rounds 0-19: sqrt(2) * 2^30 */
  16. #define K2 0x6ED9EBA1L /* Rounds 20-39: sqrt(3) * 2^30 */
  17. #define K3 0x8F1BBCDCL /* Rounds 40-59: sqrt(5) * 2^30 */
  18. #define K4 0xCA62C1D6L /* Rounds 60-79: sqrt(10) * 2^30 */
  19. /**
  20. * sha_transform - single block SHA1 transform
  21. *
  22. * @digest: 160 bit digest to update
  23. * @data: 512 bits of data to hash
  24. * @W: 80 words of workspace (see note)
  25. *
  26. * This function generates a SHA1 digest for a single 512-bit block.
  27. * Be warned, it does not handle padding and message digest, do not
  28. * confuse it with the full FIPS 180-1 digest algorithm for variable
  29. * length messages.
  30. *
  31. * Note: If the hash is security sensitive, the caller should be sure
  32. * to clear the workspace. This is left to the caller to avoid
  33. * unnecessary clears between chained hashing operations.
  34. */
  35. static void sha_transform(uint32_t *digest, const unsigned char *in, uint32_t *W)
  36. {
  37. uint32_t a, b, c, d, e, t, i;
  38. for (i = 0; i < 16; i++) {
  39. int ofs = 4 * i;
  40. /* word load/store may be unaligned here, so use bytes instead */
  41. W[i] =
  42. (in[ofs+0] << 24) |
  43. (in[ofs+1] << 16) |
  44. (in[ofs+2] << 8) |
  45. in[ofs+3];
  46. }
  47. for (i = 0; i < 64; i++)
  48. W[i+16] = rol32(W[i+13] ^ W[i+8] ^ W[i+2] ^ W[i], 1);
  49. a = digest[0];
  50. b = digest[1];
  51. c = digest[2];
  52. d = digest[3];
  53. e = digest[4];
  54. for (i = 0; i < 20; i++) {
  55. t = f1(b, c, d) + K1 + rol32(a, 5) + e + W[i];
  56. e = d; d = c; c = rol32(b, 30); b = a; a = t;
  57. }
  58. for (; i < 40; i ++) {
  59. t = f2(b, c, d) + K2 + rol32(a, 5) + e + W[i];
  60. e = d; d = c; c = rol32(b, 30); b = a; a = t;
  61. }
  62. for (; i < 60; i ++) {
  63. t = f3(b, c, d) + K3 + rol32(a, 5) + e + W[i];
  64. e = d; d = c; c = rol32(b, 30); b = a; a = t;
  65. }
  66. for (; i < 80; i ++) {
  67. t = f2(b, c, d) + K4 + rol32(a, 5) + e + W[i];
  68. e = d; d = c; c = rol32(b, 30); b = a; a = t;
  69. }
  70. digest[0] += a;
  71. digest[1] += b;
  72. digest[2] += c;
  73. digest[3] += d;
  74. digest[4] += e;
  75. }
  76. /**
  77. * sha_init - initialize the vectors for a SHA1 digest
  78. * @buf: vector to initialize
  79. */
  80. static void sha_init(uint32_t *buf)
  81. {
  82. buf[0] = 0x67452301;
  83. buf[1] = 0xefcdab89;
  84. buf[2] = 0x98badcfe;
  85. buf[3] = 0x10325476;
  86. buf[4] = 0xc3d2e1f0;
  87. }