md5.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * MD5 hash in C
  3. *
  4. * Copyright (c) 2016 Project Nayuki. (MIT License)
  5. * https://www.nayuki.io/page/fast-md5-hash-implementation-in-x86-assembly
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  8. * this software and associated documentation files (the "Software"), to deal in
  9. * the Software without restriction, including without limitation the rights to
  10. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  11. * the Software, and to permit persons to whom the Software is furnished to do so,
  12. * subject to the following conditions:
  13. * - The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. * - The Software is provided "as is", without warranty of any kind, express or
  16. * implied, including but not limited to the warranties of merchantability,
  17. * fitness for a particular purpose and noninfringement. In no event shall the
  18. * authors or copyright holders be liable for any claim, damages or other
  19. * liability, whether in an action of contract, tort or otherwise, arising from,
  20. * out of or in connection with the Software or the use or other dealings in the
  21. * Software.
  22. */
  23. #include "md5.h"
  24. void md5_compress(uint32_t state[4], const uint8_t block[64]) {
  25. #define LOADSCHEDULE(i) \
  26. schedule[i] = (uint32_t)block[i * 4 + 0] << 0 \
  27. | (uint32_t)block[i * 4 + 1] << 8 \
  28. | (uint32_t)block[i * 4 + 2] << 16 \
  29. | (uint32_t)block[i * 4 + 3] << 24;
  30. uint32_t schedule[16];
  31. LOADSCHEDULE( 0)
  32. LOADSCHEDULE( 1)
  33. LOADSCHEDULE( 2)
  34. LOADSCHEDULE( 3)
  35. LOADSCHEDULE( 4)
  36. LOADSCHEDULE( 5)
  37. LOADSCHEDULE( 6)
  38. LOADSCHEDULE( 7)
  39. LOADSCHEDULE( 8)
  40. LOADSCHEDULE( 9)
  41. LOADSCHEDULE(10)
  42. LOADSCHEDULE(11)
  43. LOADSCHEDULE(12)
  44. LOADSCHEDULE(13)
  45. LOADSCHEDULE(14)
  46. LOADSCHEDULE(15)
  47. #define ROTL32(x, n) (((0U + (x)) << (n)) | ((x) >> (32 - (n)))) // Assumes that x is uint32_t and 0 < n < 32
  48. #define ROUND0(a, b, c, d, k, s, t) ROUND_TAIL(a, b, d ^ (b & (c ^ d)), k, s, t)
  49. #define ROUND1(a, b, c, d, k, s, t) ROUND_TAIL(a, b, c ^ (d & (b ^ c)), k, s, t)
  50. #define ROUND2(a, b, c, d, k, s, t) ROUND_TAIL(a, b, b ^ c ^ d , k, s, t)
  51. #define ROUND3(a, b, c, d, k, s, t) ROUND_TAIL(a, b, c ^ (b | ~d) , k, s, t)
  52. #define ROUND_TAIL(a, b, expr, k, s, t) \
  53. a = 0U + a + (expr) + UINT32_C(t) + schedule[k]; \
  54. a = 0U + b + ROTL32(a, s);
  55. uint32_t a = state[0];
  56. uint32_t b = state[1];
  57. uint32_t c = state[2];
  58. uint32_t d = state[3];
  59. ROUND0(a, b, c, d, 0, 7, 0xD76AA478)
  60. ROUND0(d, a, b, c, 1, 12, 0xE8C7B756)
  61. ROUND0(c, d, a, b, 2, 17, 0x242070DB)
  62. ROUND0(b, c, d, a, 3, 22, 0xC1BDCEEE)
  63. ROUND0(a, b, c, d, 4, 7, 0xF57C0FAF)
  64. ROUND0(d, a, b, c, 5, 12, 0x4787C62A)
  65. ROUND0(c, d, a, b, 6, 17, 0xA8304613)
  66. ROUND0(b, c, d, a, 7, 22, 0xFD469501)
  67. ROUND0(a, b, c, d, 8, 7, 0x698098D8)
  68. ROUND0(d, a, b, c, 9, 12, 0x8B44F7AF)
  69. ROUND0(c, d, a, b, 10, 17, 0xFFFF5BB1)
  70. ROUND0(b, c, d, a, 11, 22, 0x895CD7BE)
  71. ROUND0(a, b, c, d, 12, 7, 0x6B901122)
  72. ROUND0(d, a, b, c, 13, 12, 0xFD987193)
  73. ROUND0(c, d, a, b, 14, 17, 0xA679438E)
  74. ROUND0(b, c, d, a, 15, 22, 0x49B40821)
  75. ROUND1(a, b, c, d, 1, 5, 0xF61E2562)
  76. ROUND1(d, a, b, c, 6, 9, 0xC040B340)
  77. ROUND1(c, d, a, b, 11, 14, 0x265E5A51)
  78. ROUND1(b, c, d, a, 0, 20, 0xE9B6C7AA)
  79. ROUND1(a, b, c, d, 5, 5, 0xD62F105D)
  80. ROUND1(d, a, b, c, 10, 9, 0x02441453)
  81. ROUND1(c, d, a, b, 15, 14, 0xD8A1E681)
  82. ROUND1(b, c, d, a, 4, 20, 0xE7D3FBC8)
  83. ROUND1(a, b, c, d, 9, 5, 0x21E1CDE6)
  84. ROUND1(d, a, b, c, 14, 9, 0xC33707D6)
  85. ROUND1(c, d, a, b, 3, 14, 0xF4D50D87)
  86. ROUND1(b, c, d, a, 8, 20, 0x455A14ED)
  87. ROUND1(a, b, c, d, 13, 5, 0xA9E3E905)
  88. ROUND1(d, a, b, c, 2, 9, 0xFCEFA3F8)
  89. ROUND1(c, d, a, b, 7, 14, 0x676F02D9)
  90. ROUND1(b, c, d, a, 12, 20, 0x8D2A4C8A)
  91. ROUND2(a, b, c, d, 5, 4, 0xFFFA3942)
  92. ROUND2(d, a, b, c, 8, 11, 0x8771F681)
  93. ROUND2(c, d, a, b, 11, 16, 0x6D9D6122)
  94. ROUND2(b, c, d, a, 14, 23, 0xFDE5380C)
  95. ROUND2(a, b, c, d, 1, 4, 0xA4BEEA44)
  96. ROUND2(d, a, b, c, 4, 11, 0x4BDECFA9)
  97. ROUND2(c, d, a, b, 7, 16, 0xF6BB4B60)
  98. ROUND2(b, c, d, a, 10, 23, 0xBEBFBC70)
  99. ROUND2(a, b, c, d, 13, 4, 0x289B7EC6)
  100. ROUND2(d, a, b, c, 0, 11, 0xEAA127FA)
  101. ROUND2(c, d, a, b, 3, 16, 0xD4EF3085)
  102. ROUND2(b, c, d, a, 6, 23, 0x04881D05)
  103. ROUND2(a, b, c, d, 9, 4, 0xD9D4D039)
  104. ROUND2(d, a, b, c, 12, 11, 0xE6DB99E5)
  105. ROUND2(c, d, a, b, 15, 16, 0x1FA27CF8)
  106. ROUND2(b, c, d, a, 2, 23, 0xC4AC5665)
  107. ROUND3(a, b, c, d, 0, 6, 0xF4292244)
  108. ROUND3(d, a, b, c, 7, 10, 0x432AFF97)
  109. ROUND3(c, d, a, b, 14, 15, 0xAB9423A7)
  110. ROUND3(b, c, d, a, 5, 21, 0xFC93A039)
  111. ROUND3(a, b, c, d, 12, 6, 0x655B59C3)
  112. ROUND3(d, a, b, c, 3, 10, 0x8F0CCC92)
  113. ROUND3(c, d, a, b, 10, 15, 0xFFEFF47D)
  114. ROUND3(b, c, d, a, 1, 21, 0x85845DD1)
  115. ROUND3(a, b, c, d, 8, 6, 0x6FA87E4F)
  116. ROUND3(d, a, b, c, 15, 10, 0xFE2CE6E0)
  117. ROUND3(c, d, a, b, 6, 15, 0xA3014314)
  118. ROUND3(b, c, d, a, 13, 21, 0x4E0811A1)
  119. ROUND3(a, b, c, d, 4, 6, 0xF7537E82)
  120. ROUND3(d, a, b, c, 11, 10, 0xBD3AF235)
  121. ROUND3(c, d, a, b, 2, 15, 0x2AD7D2BB)
  122. ROUND3(b, c, d, a, 9, 21, 0xEB86D391)
  123. state[0] = 0U + state[0] + a;
  124. state[1] = 0U + state[1] + b;
  125. state[2] = 0U + state[2] + c;
  126. state[3] = 0U + state[3] + d;
  127. }