/application/libraries/PEAR/Crypt/RSA/Math/BigInt.php

https://github.com/grandison/budo16 · PHP · 313 lines · 97 code · 21 blank · 195 comment · 7 complexity · 0e998825b30b34878c4ca87423ee4e87 MD5 · raw file

  1. <?php
  2. /**
  3. * Crypt_RSA allows to do following operations:
  4. * - key pair generation
  5. * - encryption and decryption
  6. * - signing and sign validation
  7. *
  8. * PHP versions 4 and 5
  9. *
  10. * LICENSE: This source file is subject to version 3.0 of the PHP license
  11. * that is available through the world-wide-web at the following URI:
  12. * http://www.php.net/license/3_0.txt. If you did not receive a copy of
  13. * the PHP License and are unable to obtain it through the web, please
  14. * send a note to license@php.net so we can mail you a copy immediately.
  15. *
  16. * @category Encryption
  17. * @package Crypt_RSA
  18. * @author Alexander Valyalkin <valyala@gmail.com>
  19. * @copyright 2005, 2006 Alexander Valyalkin
  20. * @license http://www.php.net/license/3_0.txt PHP License 3.0
  21. * @version 1.2.0b
  22. * @link http://pear.php.net/package/Crypt_RSA
  23. */
  24. /**
  25. * Crypt_RSA_Math_BigInt class.
  26. *
  27. * Provides set of math functions, which are used by Crypt_RSA package
  28. * This class is a wrapper for big_int PECL extension,
  29. * which could be loaded from http://pecl.php.net/packages/big_int
  30. *
  31. * @category Encryption
  32. * @package Crypt_RSA
  33. * @author Alexander Valyalkin <valyala@gmail.com>
  34. * @copyright 2005, 2006 Alexander Valyalkin
  35. * @license http://www.php.net/license/3_0.txt PHP License 3.0
  36. * @link http://pear.php.net/package/Crypt_RSA
  37. * @version @package_version@
  38. * @access public
  39. */
  40. class Crypt_RSA_Math_BigInt
  41. {
  42. /**
  43. * error description
  44. *
  45. * @var string
  46. * @access public
  47. */
  48. var $errstr = '';
  49. /**
  50. * Crypt_RSA_Math_BigInt constructor.
  51. * Checks an existance of big_int PECL math package.
  52. * This package is available at http://pecl.php.net/packages/big_int
  53. * On failure saves error description in $this->errstr
  54. *
  55. * @access public
  56. */
  57. function Crypt_RSA_Math_BigInt()
  58. {
  59. if (!extension_loaded('big_int')) {
  60. if (!@dl('big_int.' . PHP_SHLIB_SUFFIX) && !@dl('php_big_int.' . PHP_SHLIB_SUFFIX)) {
  61. // cannot load big_int extension
  62. $this->errstr = 'Crypt_RSA package requires big_int PECL package. ' .
  63. 'It is available at http://pecl.php.net/packages/big_int';
  64. return;
  65. }
  66. }
  67. // check version of big_int extension ( Crypt_RSA requires version 1.0.2 and higher )
  68. if (!in_array('bi_info', get_extension_funcs('big_int'))) {
  69. // there is no bi_info() function in versions, older than 1.0.2
  70. $this->errstr = 'Crypt_RSA package requires big_int package version 1.0.2 and higher';
  71. }
  72. }
  73. /**
  74. * Transforms binary representation of large integer into its native form.
  75. *
  76. * Example of transformation:
  77. * $str = "\x12\x34\x56\x78\x90";
  78. * $num = 0x9078563412;
  79. *
  80. * @param string $str
  81. * @return big_int resource
  82. * @access public
  83. */
  84. function bin2int($str)
  85. {
  86. return bi_unserialize($str);
  87. }
  88. /**
  89. * Transforms large integer into binary representation.
  90. *
  91. * Example of transformation:
  92. * $num = 0x9078563412;
  93. * $str = "\x12\x34\x56\x78\x90";
  94. *
  95. * @param big_int resource $num
  96. * @return string
  97. * @access public
  98. */
  99. function int2bin($num)
  100. {
  101. return bi_serialize($num);
  102. }
  103. /**
  104. * Calculates pow($num, $pow) (mod $mod)
  105. *
  106. * @param big_int resource $num
  107. * @param big_int resource $pow
  108. * @param big_int resource $mod
  109. * @return big_int resource
  110. * @access public
  111. */
  112. function powmod($num, $pow, $mod)
  113. {
  114. return bi_powmod($num, $pow, $mod);
  115. }
  116. /**
  117. * Calculates $num1 * $num2
  118. *
  119. * @param big_int resource $num1
  120. * @param big_int resource $num2
  121. * @return big_int resource
  122. * @access public
  123. */
  124. function mul($num1, $num2)
  125. {
  126. return bi_mul($num1, $num2);
  127. }
  128. /**
  129. * Calculates $num1 % $num2
  130. *
  131. * @param string $num1
  132. * @param string $num2
  133. * @return string
  134. * @access public
  135. */
  136. function mod($num1, $num2)
  137. {
  138. return bi_mod($num1, $num2);
  139. }
  140. /**
  141. * Compares abs($num1) to abs($num2).
  142. * Returns:
  143. * -1, if abs($num1) < abs($num2)
  144. * 0, if abs($num1) == abs($num2)
  145. * 1, if abs($num1) > abs($num2)
  146. *
  147. * @param big_int resource $num1
  148. * @param big_int resource $num2
  149. * @return int
  150. * @access public
  151. */
  152. function cmpAbs($num1, $num2)
  153. {
  154. return bi_cmp_abs($num1, $num2);
  155. }
  156. /**
  157. * Tests $num on primality. Returns true, if $num is strong pseudoprime.
  158. * Else returns false.
  159. *
  160. * @param string $num
  161. * @return bool
  162. * @access private
  163. */
  164. function isPrime($num)
  165. {
  166. return bi_is_prime($num) ? true : false;
  167. }
  168. /**
  169. * Generates prime number with length $bits_cnt
  170. * using $random_generator as random generator function.
  171. *
  172. * @param int $bits_cnt
  173. * @param string $rnd_generator
  174. * @access public
  175. */
  176. function getPrime($bits_cnt, $random_generator)
  177. {
  178. $bytes_n = intval($bits_cnt / 8);
  179. $bits_n = $bits_cnt % 8;
  180. do {
  181. $str = '';
  182. for ($i = 0; $i < $bytes_n; $i++) {
  183. $str .= chr(call_user_func($random_generator) & 0xff);
  184. }
  185. $n = call_user_func($random_generator) & 0xff;
  186. $n |= 0x80;
  187. $n >>= 8 - $bits_n;
  188. $str .= chr($n);
  189. $num = $this->bin2int($str);
  190. // search for the next closest prime number after [$num]
  191. $num = bi_next_prime($num);
  192. } while ($this->bitLen($num) != $bits_cnt);
  193. return $num;
  194. }
  195. /**
  196. * Calculates $num - 1
  197. *
  198. * @param big_int resource $num
  199. * @return big_int resource
  200. * @access public
  201. */
  202. function dec($num)
  203. {
  204. return bi_dec($num);
  205. }
  206. /**
  207. * Returns true, if $num is equal to 1. Else returns false
  208. *
  209. * @param big_int resource $num
  210. * @return bool
  211. * @access public
  212. */
  213. function isOne($num)
  214. {
  215. return bi_is_one($num);
  216. }
  217. /**
  218. * Finds greatest common divider (GCD) of $num1 and $num2
  219. *
  220. * @param big_int resource $num1
  221. * @param big_int resource $num2
  222. * @return big_int resource
  223. * @access public
  224. */
  225. function GCD($num1, $num2)
  226. {
  227. return bi_gcd($num1, $num2);
  228. }
  229. /**
  230. * Finds inverse number $inv for $num by modulus $mod, such as:
  231. * $inv * $num = 1 (mod $mod)
  232. *
  233. * @param big_int resource $num
  234. * @param big_int resource $mod
  235. * @return big_int resource
  236. * @access public
  237. */
  238. function invmod($num, $mod)
  239. {
  240. return bi_invmod($num, $mod);
  241. }
  242. /**
  243. * Returns bit length of number $num
  244. *
  245. * @param big_int resource $num
  246. * @return int
  247. * @access public
  248. */
  249. function bitLen($num)
  250. {
  251. return bi_bit_len($num);
  252. }
  253. /**
  254. * Calculates bitwise or of $num1 and $num2,
  255. * starting from bit $start_pos for number $num1
  256. *
  257. * @param big_int resource $num1
  258. * @param big_int resource $num2
  259. * @param int $start_pos
  260. * @return big_int resource
  261. * @access public
  262. */
  263. function bitOr($num1, $num2, $start_pos)
  264. {
  265. return bi_or($num1, $num2, $start_pos);
  266. }
  267. /**
  268. * Returns part of number $num, starting at bit
  269. * position $start with length $length
  270. *
  271. * @param big_int resource $num
  272. * @param int start
  273. * @param int length
  274. * @return big_int resource
  275. * @access public
  276. */
  277. function subint($num, $start, $length)
  278. {
  279. return bi_subint($num, $start, $length);
  280. }
  281. /**
  282. * Returns name of current wrapper
  283. *
  284. * @return string name of current wrapper
  285. * @access public
  286. */
  287. function getWrapperName()
  288. {
  289. return 'BigInt';
  290. }
  291. }
  292. ?>