sha1.c 2.4 KB

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