You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

dovecot_hmacmd5.php 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. /**
  3. *
  4. * dovecot_hmacmd5.php V1.01
  5. *
  6. * Generates HMAC-MD5 'contexts' for Dovecot's password files.
  7. *
  8. * (C) 2008 Hajo Noerenberg
  9. *
  10. * http://www.noerenberg.de/hajo/pub/dovecot_hmacmd5.php.txt
  11. *
  12. * Most of the code has been shamelessly stolen from various sources:
  13. *
  14. * (C) Paul Johnston 1999 - 2000 / http://pajhome.org.uk/crypt/md5/
  15. * (C) William K. Cole 2008 / http://www.scconsult.com/bill/crampass.pl
  16. * (C) Borfast 2002 / http://www.zend.com/code/codex.php?ozid=962&single=1
  17. * (C) Thomas Weber / http://pajhome.org.uk/crypt/md5/contrib/md5.java.txt
  18. *
  19. *
  20. * This program is free software: you can redistribute it and/or modify
  21. * it under the terms of the GNU General Public License version 3.0 as
  22. * published by the Free Software Foundation.
  23. *
  24. * This program is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. * GNU General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU General Public License along
  30. * with this program. If not, see <http://www.gnu.org/licenses/gpl-3.0.txt>.
  31. *
  32. */
  33. /* Convert a 32-bit number to a hex string with ls-byte first
  34. */
  35. function rhex($n) {
  36. $hex_chr = "0123456789abcdef"; $r = '';
  37. for($j = 0; $j <= 3; $j++)
  38. $r .= $hex_chr[($n >> ($j * 8 + 4)) & 0x0F] . $hex_chr[($n >> ($j * 8)) & 0x0F];
  39. return $r;
  40. }
  41. /* zeroFill() is needed because PHP doesn't have a zero-fill
  42. * right shift operator like JavaScript's >>>
  43. */
  44. function zeroFill($a, $b) {
  45. $z = hexdec(80000000);
  46. if ($z & $a) {
  47. $a >>= 1;
  48. $a &= (~$z);
  49. $a |= 0x40000000;
  50. $a >>= ($b-1);
  51. } else {
  52. $a >>= $b;
  53. }
  54. return $a;
  55. }
  56. /* Bitwise rotate a 32-bit number to the left
  57. */
  58. function bit_rol($num, $cnt) {
  59. return ($num << $cnt) | (zeroFill($num, (32 - $cnt)));
  60. }
  61. /* Add integers, wrapping at 2^32
  62. */
  63. function safe_add($x, $y) {
  64. return (($x&0x7FFFFFFF) + ($y&0x7FFFFFFF)) ^ ($x&0x80000000) ^ ($y&0x80000000);
  65. }
  66. /* These functions implement the four basic operations the algorithm uses.
  67. */
  68. function md5_cmn($q, $a, $b, $x, $s, $t) {
  69. return safe_add(bit_rol(safe_add(safe_add($a, $q), safe_add($x, $t)), $s), $b);
  70. }
  71. function md5_ff($a, $b, $c, $d, $x, $s, $t) {
  72. return md5_cmn(($b & $c) | ((~$b) & $d), $a, $b, $x, $s, $t);
  73. }
  74. function md5_gg($a, $b, $c, $d, $x, $s, $t) {
  75. return md5_cmn(($b & $d) | ($c & (~$d)), $a, $b, $x, $s, $t);
  76. }
  77. function md5_hh($a, $b, $c, $d, $x, $s, $t) {
  78. return md5_cmn($b ^ $c ^ $d, $a, $b, $x, $s, $t);
  79. }
  80. function md5_ii($a, $b, $c, $d, $x, $s, $t) {
  81. return md5_cmn($c ^ ($b | (~$d)), $a, $b, $x, $s, $t);
  82. }
  83. /* Calculate the first round of the MD5 algorithm
  84. */
  85. function md5_oneround($s, $io) {
  86. $s = str_pad($s, 64, chr(0x00));
  87. $x = array_fill(0, 16, 0);
  88. for($i = 0; $i < 64; $i++)
  89. $x[$i >> 2] |= (($io ? 0x36 : 0x5c) ^ ord($s[$i])) << (($i % 4) * 8);
  90. $a = $olda = 1732584193;
  91. $b = $oldb = -271733879;
  92. $c = $oldc = -1732584194;
  93. $d = $oldd = 271733878;
  94. $a = md5_ff($a, $b, $c, $d, $x[ 0], 7 , -680876936);
  95. $d = md5_ff($d, $a, $b, $c, $x[ 1], 12, -389564586);
  96. $c = md5_ff($c, $d, $a, $b, $x[ 2], 17, 606105819);
  97. $b = md5_ff($b, $c, $d, $a, $x[ 3], 22, -1044525330);
  98. $a = md5_ff($a, $b, $c, $d, $x[ 4], 7 , -176418897);
  99. $d = md5_ff($d, $a, $b, $c, $x[ 5], 12, 1200080426);
  100. $c = md5_ff($c, $d, $a, $b, $x[ 6], 17, -1473231341);
  101. $b = md5_ff($b, $c, $d, $a, $x[ 7], 22, -45705983);
  102. $a = md5_ff($a, $b, $c, $d, $x[ 8], 7 , 1770035416);
  103. $d = md5_ff($d, $a, $b, $c, $x[ 9], 12, -1958414417);
  104. $c = md5_ff($c, $d, $a, $b, $x[10], 17, -42063);
  105. $b = md5_ff($b, $c, $d, $a, $x[11], 22, -1990404162);
  106. $a = md5_ff($a, $b, $c, $d, $x[12], 7 , 1804603682);
  107. $d = md5_ff($d, $a, $b, $c, $x[13], 12, -40341101);
  108. $c = md5_ff($c, $d, $a, $b, $x[14], 17, -1502002290);
  109. $b = md5_ff($b, $c, $d, $a, $x[15], 22, 1236535329);
  110. $a = md5_gg($a, $b, $c, $d, $x[ 1], 5 , -165796510);
  111. $d = md5_gg($d, $a, $b, $c, $x[ 6], 9 , -1069501632);
  112. $c = md5_gg($c, $d, $a, $b, $x[11], 14, 643717713);
  113. $b = md5_gg($b, $c, $d, $a, $x[ 0], 20, -373897302);
  114. $a = md5_gg($a, $b, $c, $d, $x[ 5], 5 , -701558691);
  115. $d = md5_gg($d, $a, $b, $c, $x[10], 9 , 38016083);
  116. $c = md5_gg($c, $d, $a, $b, $x[15], 14, -660478335);
  117. $b = md5_gg($b, $c, $d, $a, $x[ 4], 20, -405537848);
  118. $a = md5_gg($a, $b, $c, $d, $x[ 9], 5 , 568446438);
  119. $d = md5_gg($d, $a, $b, $c, $x[14], 9 , -1019803690);
  120. $c = md5_gg($c, $d, $a, $b, $x[ 3], 14, -187363961);
  121. $b = md5_gg($b, $c, $d, $a, $x[ 8], 20, 1163531501);
  122. $a = md5_gg($a, $b, $c, $d, $x[13], 5 , -1444681467);
  123. $d = md5_gg($d, $a, $b, $c, $x[ 2], 9 , -51403784);
  124. $c = md5_gg($c, $d, $a, $b, $x[ 7], 14, 1735328473);
  125. $b = md5_gg($b, $c, $d, $a, $x[12], 20, -1926607734);
  126. $a = md5_hh($a, $b, $c, $d, $x[ 5], 4 , -378558);
  127. $d = md5_hh($d, $a, $b, $c, $x[ 8], 11, -2022574463);
  128. $c = md5_hh($c, $d, $a, $b, $x[11], 16, 1839030562);
  129. $b = md5_hh($b, $c, $d, $a, $x[14], 23, -35309556);
  130. $a = md5_hh($a, $b, $c, $d, $x[ 1], 4 , -1530992060);
  131. $d = md5_hh($d, $a, $b, $c, $x[ 4], 11, 1272893353);
  132. $c = md5_hh($c, $d, $a, $b, $x[ 7], 16, -155497632);
  133. $b = md5_hh($b, $c, $d, $a, $x[10], 23, -1094730640);
  134. $a = md5_hh($a, $b, $c, $d, $x[13], 4 , 681279174);
  135. $d = md5_hh($d, $a, $b, $c, $x[ 0], 11, -358537222);
  136. $c = md5_hh($c, $d, $a, $b, $x[ 3], 16, -722521979);
  137. $b = md5_hh($b, $c, $d, $a, $x[ 6], 23, 76029189);
  138. $a = md5_hh($a, $b, $c, $d, $x[ 9], 4 , -640364487);
  139. $d = md5_hh($d, $a, $b, $c, $x[12], 11, -421815835);
  140. $c = md5_hh($c, $d, $a, $b, $x[15], 16, 530742520);
  141. $b = md5_hh($b, $c, $d, $a, $x[ 2], 23, -995338651);
  142. $a = md5_ii($a, $b, $c, $d, $x[ 0], 6 , -198630844);
  143. $d = md5_ii($d, $a, $b, $c, $x[ 7], 10, 1126891415);
  144. $c = md5_ii($c, $d, $a, $b, $x[14], 15, -1416354905);
  145. $b = md5_ii($b, $c, $d, $a, $x[ 5], 21, -57434055);
  146. $a = md5_ii($a, $b, $c, $d, $x[12], 6 , 1700485571);
  147. $d = md5_ii($d, $a, $b, $c, $x[ 3], 10, -1894986606);
  148. $c = md5_ii($c, $d, $a, $b, $x[10], 15, -1051523);
  149. $b = md5_ii($b, $c, $d, $a, $x[ 1], 21, -2054922799);
  150. $a = md5_ii($a, $b, $c, $d, $x[ 8], 6 , 1873313359);
  151. $d = md5_ii($d, $a, $b, $c, $x[15], 10, -30611744);
  152. $c = md5_ii($c, $d, $a, $b, $x[ 6], 15, -1560198380);
  153. $b = md5_ii($b, $c, $d, $a, $x[13], 21, 1309151649);
  154. $a = md5_ii($a, $b, $c, $d, $x[ 4], 6 , -145523070);
  155. $d = md5_ii($d, $a, $b, $c, $x[11], 10, -1120210379);
  156. $c = md5_ii($c, $d, $a, $b, $x[ 2], 15, 718787259);
  157. $b = md5_ii($b, $c, $d, $a, $x[ 9], 21, -343485551);
  158. $a = safe_add($a, $olda);
  159. $b = safe_add($b, $oldb);
  160. $c = safe_add($c, $oldc);
  161. $d = safe_add($d, $oldd);
  162. return rhex($a) . rhex($b) . rhex($c) . rhex($d);
  163. }
  164. function dovecot_hmacmd5 ($s) {
  165. if (strlen($s) > 64) $s=pack("H*", md5($s));
  166. return md5_oneround($s, 0) . md5_oneround($s, 1);
  167. }