md4.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /* vim:set ts=2 sw=2 et cindent: */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. /*
  6. * "clean room" MD4 implementation (see RFC 1320)
  7. */
  8. #include <string.h>
  9. #include "md4.h"
  10. /* the "conditional" function */
  11. #define F(x,y,z) (((x) & (y)) | (~(x) & (z)))
  12. /* the "majority" function */
  13. #define G(x,y,z) (((x) & (y)) | ((x) & (z)) | ((y) & (z)))
  14. /* the "parity" function */
  15. #define H(x,y,z) ((x) ^ (y) ^ (z))
  16. /* rotate n-bits to the left */
  17. #define ROTL(x,n) (((x) << (n)) | ((x) >> (0x20 - n)))
  18. /* round 1: [abcd k s]: a = (a + F(b,c,d) + X[k]) <<< s */
  19. #define RD1(a,b,c,d,k,s) a += F(b,c,d) + X[k]; a = ROTL(a,s)
  20. /* round 2: [abcd k s]: a = (a + G(b,c,d) + X[k] + MAGIC) <<< s */
  21. #define RD2(a,b,c,d,k,s) a += G(b,c,d) + X[k] + 0x5A827999; a = ROTL(a,s)
  22. /* round 3: [abcd k s]: a = (a + H(b,c,d) + X[k] + MAGIC) <<< s */
  23. #define RD3(a,b,c,d,k,s) a += H(b,c,d) + X[k] + 0x6ED9EBA1; a = ROTL(a,s)
  24. /* converts from word array to byte array, len is number of bytes */
  25. static void w2b(uint8_t *out, const uint32_t *in, uint32_t len)
  26. {
  27. uint8_t *bp; const uint32_t *wp, *wpend;
  28. bp = out;
  29. wp = in;
  30. wpend = wp + (len >> 2);
  31. for (; wp != wpend; ++wp, bp += 4)
  32. {
  33. bp[0] = (uint8_t) ((*wp ) & 0xFF);
  34. bp[1] = (uint8_t) ((*wp >> 8) & 0xFF);
  35. bp[2] = (uint8_t) ((*wp >> 16) & 0xFF);
  36. bp[3] = (uint8_t) ((*wp >> 24) & 0xFF);
  37. }
  38. }
  39. /* converts from byte array to word array, len is number of bytes */
  40. static void b2w(uint32_t *out, const uint8_t *in, uint32_t len)
  41. {
  42. uint32_t *wp; const uint8_t *bp, *bpend;
  43. wp = out;
  44. bp = in;
  45. bpend = in + len;
  46. for (; bp != bpend; bp += 4, ++wp)
  47. {
  48. *wp = (uint32_t) (bp[0] ) |
  49. (uint32_t) (bp[1] << 8) |
  50. (uint32_t) (bp[2] << 16) |
  51. (uint32_t) (bp[3] << 24);
  52. }
  53. }
  54. /* update state: data is 64 bytes in length */
  55. static void md4step(uint32_t state[4], const uint8_t *data)
  56. {
  57. uint32_t A, B, C, D, X[16];
  58. b2w(X, data, 64);
  59. A = state[0];
  60. B = state[1];
  61. C = state[2];
  62. D = state[3];
  63. RD1(A,B,C,D, 0,3); RD1(D,A,B,C, 1,7); RD1(C,D,A,B, 2,11); RD1(B,C,D,A, 3,19);
  64. RD1(A,B,C,D, 4,3); RD1(D,A,B,C, 5,7); RD1(C,D,A,B, 6,11); RD1(B,C,D,A, 7,19);
  65. RD1(A,B,C,D, 8,3); RD1(D,A,B,C, 9,7); RD1(C,D,A,B,10,11); RD1(B,C,D,A,11,19);
  66. RD1(A,B,C,D,12,3); RD1(D,A,B,C,13,7); RD1(C,D,A,B,14,11); RD1(B,C,D,A,15,19);
  67. RD2(A,B,C,D, 0,3); RD2(D,A,B,C, 4,5); RD2(C,D,A,B, 8, 9); RD2(B,C,D,A,12,13);
  68. RD2(A,B,C,D, 1,3); RD2(D,A,B,C, 5,5); RD2(C,D,A,B, 9, 9); RD2(B,C,D,A,13,13);
  69. RD2(A,B,C,D, 2,3); RD2(D,A,B,C, 6,5); RD2(C,D,A,B,10, 9); RD2(B,C,D,A,14,13);
  70. RD2(A,B,C,D, 3,3); RD2(D,A,B,C, 7,5); RD2(C,D,A,B,11, 9); RD2(B,C,D,A,15,13);
  71. RD3(A,B,C,D, 0,3); RD3(D,A,B,C, 8,9); RD3(C,D,A,B, 4,11); RD3(B,C,D,A,12,15);
  72. RD3(A,B,C,D, 2,3); RD3(D,A,B,C,10,9); RD3(C,D,A,B, 6,11); RD3(B,C,D,A,14,15);
  73. RD3(A,B,C,D, 1,3); RD3(D,A,B,C, 9,9); RD3(C,D,A,B, 5,11); RD3(B,C,D,A,13,15);
  74. RD3(A,B,C,D, 3,3); RD3(D,A,B,C,11,9); RD3(C,D,A,B, 7,11); RD3(B,C,D,A,15,15);
  75. state[0] += A;
  76. state[1] += B;
  77. state[2] += C;
  78. state[3] += D;
  79. }
  80. void md4sum(const uint8_t *input, uint32_t inputLen, uint8_t *result)
  81. {
  82. uint8_t final[128];
  83. uint32_t i, n, m, state[4];
  84. uint64_t inputLenBits;
  85. uint32_t inputLenBitsLow;
  86. uint32_t inputLenBitsHigh;
  87. /* magic initial states */
  88. state[0] = 0x67452301;
  89. state[1] = 0xEFCDAB89;
  90. state[2] = 0x98BADCFE;
  91. state[3] = 0x10325476;
  92. /* compute number of complete 64-byte segments contained in input */
  93. m = inputLen >> 6;
  94. /* digest first m segments */
  95. for (i=0; i<m; ++i)
  96. md4step(state, (input + (i << 6)));
  97. /* build final buffer */
  98. n = inputLen % 64;
  99. memcpy(final, input + (m << 6), n);
  100. final[n] = 0x80;
  101. memset(final + n + 1, 0, 120 - (n + 1));
  102. /* Append the original input length in bits as a 64-bit number. This is done
  103. * in two 32-bit chunks, with the least-significant 32 bits first.
  104. * w2b will handle endianness. */
  105. inputLenBits = inputLen << 3;
  106. inputLenBitsLow = (uint32_t)(inputLenBits & 0xFFFFFFFF);
  107. w2b(final + (n >= 56 ? 120 : 56), &inputLenBitsLow, 4);
  108. inputLenBitsHigh = (uint32_t)((inputLenBits >> 32) & 0xFFFFFFFF);
  109. w2b(final + (n >= 56 ? 124 : 60), &inputLenBitsHigh, 4);
  110. md4step(state, final);
  111. if (n >= 56)
  112. md4step(state, final + 64);
  113. /* copy state to result */
  114. w2b(result, state, 16);
  115. }