halfmd4.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #include <linux/kernel.h>
  2. #include <linux/module.h>
  3. #include <linux/cryptohash.h>
  4. /* F, G and H are basic MD4 functions: selection, majority, parity */
  5. #define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
  6. #define G(x, y, z) (((x) & (y)) + (((x) ^ (y)) & (z)))
  7. #define H(x, y, z) ((x) ^ (y) ^ (z))
  8. /*
  9. * The generic round function. The application is so specific that
  10. * we don't bother protecting all the arguments with parens, as is generally
  11. * good macro practice, in favor of extra legibility.
  12. * Rotation is separate from addition to prevent recomputation
  13. */
  14. #define ROUND(f, a, b, c, d, x, s) \
  15. (a += f(b, c, d) + x, a = (a << s) | (a >> (32 - s)))
  16. #define K1 0
  17. #define K2 013240474631UL
  18. #define K3 015666365641UL
  19. /*
  20. * Basic cut-down MD4 transform. Returns only 32 bits of result.
  21. */
  22. __u32 half_md4_transform(__u32 buf[4], __u32 const in[8])
  23. {
  24. __u32 a = buf[0], b = buf[1], c = buf[2], d = buf[3];
  25. /* Round 1 */
  26. ROUND(F, a, b, c, d, in[0] + K1, 3);
  27. ROUND(F, d, a, b, c, in[1] + K1, 7);
  28. ROUND(F, c, d, a, b, in[2] + K1, 11);
  29. ROUND(F, b, c, d, a, in[3] + K1, 19);
  30. ROUND(F, a, b, c, d, in[4] + K1, 3);
  31. ROUND(F, d, a, b, c, in[5] + K1, 7);
  32. ROUND(F, c, d, a, b, in[6] + K1, 11);
  33. ROUND(F, b, c, d, a, in[7] + K1, 19);
  34. /* Round 2 */
  35. ROUND(G, a, b, c, d, in[1] + K2, 3);
  36. ROUND(G, d, a, b, c, in[3] + K2, 5);
  37. ROUND(G, c, d, a, b, in[5] + K2, 9);
  38. ROUND(G, b, c, d, a, in[7] + K2, 13);
  39. ROUND(G, a, b, c, d, in[0] + K2, 3);
  40. ROUND(G, d, a, b, c, in[2] + K2, 5);
  41. ROUND(G, c, d, a, b, in[4] + K2, 9);
  42. ROUND(G, b, c, d, a, in[6] + K2, 13);
  43. /* Round 3 */
  44. ROUND(H, a, b, c, d, in[3] + K3, 3);
  45. ROUND(H, d, a, b, c, in[7] + K3, 9);
  46. ROUND(H, c, d, a, b, in[2] + K3, 11);
  47. ROUND(H, b, c, d, a, in[6] + K3, 15);
  48. ROUND(H, a, b, c, d, in[1] + K3, 3);
  49. ROUND(H, d, a, b, c, in[5] + K3, 9);
  50. ROUND(H, c, d, a, b, in[0] + K3, 11);
  51. ROUND(H, b, c, d, a, in[4] + K3, 15);
  52. buf[0] += a;
  53. buf[1] += b;
  54. buf[2] += c;
  55. buf[3] += d;
  56. return buf[1]; /* "most hashed" word */
  57. }
  58. EXPORT_SYMBOL(half_md4_transform);