Skip to content
Browse files

Crypt_RSA 1.1.0b

git-svn-id: http://svn.php.net/repository/pear/packages/Crypt_RSA/trunk@232767 c90b9560-bf6c-de11-be94-00142212c4b1
  • Loading branch information...
1 parent 5194199 commit 9e7045aa51758bafd56e85b426a9185b1dc0855d @cweiske cweiske committed Mar 27, 2007
Showing with 535 additions and 337 deletions.
  1. +1 −1 LICENSE
  2. +7 −11 RSA.php
  3. +2 −4 RSA/ErrorHandler.php
  4. +6 −10 RSA/Key.php
  5. +258 −38 RSA/KeyPair.php
  6. +26 −38 RSA/Math/BCMath.php
  7. +15 −28 RSA/Math/BigInt.php
  8. +26 −38 RSA/Math/GMP.php
  9. +3 −5 RSA/MathLoader.php
  10. +3 −4 docs/math_wrappers.txt
  11. +2 −4 examples/web_interface.php
  12. +145 −144 package.xml
  13. +39 −8 tests/test1.php
  14. +2 −4 tests/test2.php
View
2 LICENSE
@@ -1,4 +1,4 @@
-Copyright 2005 Alexander Valyalkin
+Copyright 2005, 2006 Alexander Valyalkin
LICENSE: This source file is subject to version 3.0 of the PHP license
that is available through the world-wide-web at the following URI:
View
18 RSA.php
@@ -1,6 +1,4 @@
<?php
-/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
-
/**
* Crypt_RSA allows to do following operations:
* - key pair generation
@@ -18,9 +16,9 @@
* @category Encryption
* @package Crypt_RSA
* @author Alexander Valyalkin <valyala@gmail.com>
- * @copyright 2005 Alexander Valyalkin
+ * @copyright 2005, 2006 Alexander Valyalkin
* @license http://www.php.net/license/3_0.txt PHP License 3.0
- * @version 1.0.0
+ * @version 1.1.0
* @link http://pear.php.net/package/Crypt_RSA
*/
@@ -62,7 +60,7 @@
* $key_pair = new Crypt_RSA_KeyPair(1024);
*
* // check consistence of Crypt_RSA_KeyPair object
- * $error_handler($rsa_obj);
+ * $error_handler($key_pair);
*
* // creating Crypt_RSA object
* $rsa_obj = new Crypt_RSA;
@@ -110,7 +108,7 @@
* @category Encryption
* @package Crypt_RSA
* @author Alexander Valyalkin <valyala@gmail.com>
- * @copyright 2005 Alexander Valyalkin
+ * @copyright 2005, 2006 Alexander Valyalkin
* @license http://www.php.net/license/3_0.txt PHP License 3.0
* @link http://pear.php.net/package/Crypt_RSA
* @version @package_version@
@@ -176,13 +174,11 @@ class Crypt_RSA extends Crypt_RSA_ErrorHandler
* enc_key, dec_key, private_key, public_key, hash_func.
* See setParams() method for more detailed description of
* these parameters.
- *
* @param string $wrapper_name
* Name of math wrapper, which will be used to
* perform different operations with big integers.
* See contents of Crypt/RSA/Math folder for examples of wrappers.
* Read docs/Crypt_RSA/docs/math_wrappers.txt for details.
- *
* @param string $error_handler name of error handler function
*
* @access public
@@ -217,19 +213,19 @@ function Crypt_RSA($params = null, $wrapper_name = 'default', $error_handler = '
* enc_key, dec_key, private_key, public_key, hash_func.
* See setParams() method for more detailed description of
* these parameters.
- *
* @param string $wrapper_name
* Name of math wrapper, which will be used to
* perform different operations with big integers.
* See contents of Crypt/RSA/Math folder for examples of wrappers.
* Read docs/Crypt_RSA/docs/math_wrappers.txt for details.
+ * @param string $error_handler name of error handler function
*
* @return object new Crypt_RSA object on success or PEAR_Error object on failure
* @access public
*/
- function &factory($params = null, $wrapper_name = 'default')
+ function &factory($params = null, $wrapper_name = 'default', $error_handler = '')
{
- $obj = &new Crypt_RSA($params, $wrapper_name);
+ $obj = &new Crypt_RSA($params, $wrapper_name, $error_handler);
if ($obj->isError()) {
// error during creating a new object. Retrurn PEAR_Error object
return $obj->getLastError();
View
6 RSA/ErrorHandler.php
@@ -1,6 +1,4 @@
<?php
-/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
-
/**
* Crypt_RSA allows to do following operations:
* - key pair generation
@@ -18,9 +16,9 @@
* @category Encryption
* @package Crypt_RSA
* @author Alexander Valyalkin <valyala@gmail.com>
- * @copyright 2005 Alexander Valyalkin
+ * @copyright 2005, 2006 Alexander Valyalkin
* @license http://www.php.net/license/3_0.txt PHP License 3.0
- * @version 1.0.0
+ * @version 1.1.0
* @link http://pear.php.net/package/Crypt_RSA
*/
View
16 RSA/Key.php
@@ -1,6 +1,4 @@
<?php
-/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
-
/**
* Crypt_RSA allows to do following operations:
* - key pair generation
@@ -18,9 +16,9 @@
* @category Encryption
* @package Crypt_RSA
* @author Alexander Valyalkin <valyala@gmail.com>
- * @copyright 2005 Alexander Valyalkin
+ * @copyright 2005, 2006 Alexander Valyalkin
* @license http://www.php.net/license/3_0.txt PHP License 3.0
- * @version 1.0.0
+ * @version 1.1.0
* @link http://pear.php.net/package/Crypt_RSA
*/
@@ -88,7 +86,7 @@
* @category Encryption
* @package Crypt_RSA
* @author Alexander Valyalkin <valyala@gmail.com>
- * @copyright 2005 Alexander Valyalkin
+ * @copyright 2005, 2006 Alexander Valyalkin
* @license http://www.php.net/license/3_0.txt PHP License 3.0
* @link http://pear.php.net/package/Crypt_RSA
* @version @package_version@
@@ -143,13 +141,11 @@ class Crypt_RSA_Key extends Crypt_RSA_ErrorHandler
* @param string $modulus key modulus
* @param string $exp key exponent
* @param string $key_type type of the key (public or private)
- *
* @param string $wrapper_name
* Name of math wrapper, which will be used to
* perform different operations with big integers.
* See contents of Crypt/RSA/Math folder for examples of wrappers.
* Read docs/Crypt_RSA/docs/math_wrappers.txt for details.
- *
* @param string $error_handler name of error handler function
*
* @access public
@@ -196,19 +192,19 @@ function Crypt_RSA_Key($modulus, $exp, $key_type, $wrapper_name = 'default', $er
* @param string $modulus key modulus
* @param string $exp key exponent
* @param string $key_type type of the key (public or private)
- *
* @param string $wrapper_name
* Name of math wrapper, which will be used to
* perform different operations with big integers.
* See contents of Crypt/RSA/Math folder for examples of wrappers.
* Read docs/Crypt_RSA/docs/math_wrappers.txt for details.
+ * @param string $error_handler name of error handler function
*
* @return object new Crypt_RSA_Key object on success or PEAR_Error object on failure
* @access public
*/
- function &factory($modulus, $exp, $key_type, $wrapper_name = 'default')
+ function &factory($modulus, $exp, $key_type, $wrapper_name = 'default', $error_handler = '')
{
- $obj = &new Crypt_RSA_Key($modulus, $exp, $key_type, $wrapper_name);
+ $obj = &new Crypt_RSA_Key($modulus, $exp, $key_type, $wrapper_name, $error_handler);
if ($obj->isError()) {
// error during creating a new object. Retrurn PEAR_Error object
return $obj->getLastError();
View
296 RSA/KeyPair.php
@@ -1,6 +1,4 @@
<?php
-/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
-
/**
* Crypt_RSA allows to do following operations:
* - key pair generation
@@ -18,9 +16,9 @@
* @category Encryption
* @package Crypt_RSA
* @author Alexander Valyalkin <valyala@gmail.com>
- * @copyright 2005 Alexander Valyalkin
+ * @copyright 2005, 2006 Alexander Valyalkin
* @license http://www.php.net/license/3_0.txt PHP License 3.0
- * @version 1.0.0
+ * @version 1.1.0
* @link http://pear.php.net/package/Crypt_RSA
*/
@@ -48,6 +46,7 @@
* - getPrivateKey() - returns private key
* - getKeyLength() - returns bit key length
* - setRandomGenerator($func_name) - sets random generator to $func_name
+ * - fromPEMString($str) - retrieves key pair from PEM-encoded string
*
* Example usage:
* // create new 1024-bit key pair
@@ -97,10 +96,20 @@
* echo "error: ", $rsa_obj->getMessage(), "\n";
* }
*
+ * // read key pair from PEM-encoded string:
+ * $str = "-----BEGIN RSA PRIVATE KEY-----"
+ * . "MCsCAQACBHr5LDkCAwEAAQIEBc6jbQIDAOCfAgMAjCcCAk3pAgJMawIDAL41"
+ * . "-----END RSA PRIVATE KEY-----";
+ * $keypair = Crypt_RSA_KeyPair::fromPEMString($str);
+ *
+ * // read key pair from .pem file 'private.pem':
+ * $str = file_get_contents('private.pem');
+ * $keypair = Crypt_RSA_KeyPair::fromPEMString($str);
+ *
* @category Encryption
* @package Crypt_RSA
* @author Alexander Valyalkin <valyala@gmail.com>
- * @copyright 2005 Alexander Valyalkin
+ * @copyright 2005, 2006 Alexander Valyalkin
* @license http://www.php.net/license/3_0.txt PHP License 3.0
* @link http://pear.php.net/package/Crypt_RSA
* @version @package_version@
@@ -150,6 +159,96 @@ class Crypt_RSA_KeyPair extends Crypt_RSA_ErrorHandler
var $_random_generator;
/**
+ * Parse ASN.1 string [$str] starting form position [$pos].
+ * Returns tag and string value of parsed object.
+ *
+ * @param string $str
+ * @param int $pos
+ * @param object $err_handler
+ *
+ * @return mixed Array('tag' => ..., 'str' => ...) on success, PEAR_Error object on error
+ * @access private
+ */
+ function _ASN1Parse($str, &$pos, $err_handler)
+ {
+ $max_pos = strlen($str);
+ if ($max_pos < 2) {
+ $err = PEAR::raiseError("ASN.1 string too short");
+ $err_handler->pushError($err);
+ return $err;
+ }
+
+ // get ASN.1 tag value
+ $tag = ord($str[$pos++]) & 0x1f;
+ if ($tag == 0x1f) {
+ $tag = 0;
+ do {
+ $n = ord($str[$pos++]);
+ $tag <<= 7;
+ $tag |= $n & 0x7f;
+ } while (($n & 0x80) && $pos < $max_pos);
+ }
+ if ($pos >= $max_pos) {
+ $err = PEAR::raiseError("ASN.1 string too short");
+ $err_handler->pushError($err);
+ return $err;
+ }
+
+ // get ASN.1 object length
+ $len = ord($str[$pos++]);
+ if ($len & 0x80) {
+ $n = $len & 0x1f;
+ $len = 0;
+ while ($n-- && $pos < $max_pos) {
+ $len <<= 8;
+ $len |= ord($in[$pos++]);
+ }
+ }
+ if ($pos >= $max_pos || $len > $max_pos - $pos) {
+ $err = PEAR::raiseError("ASN.1 string too short");
+ $err_handler->pushError($err);
+ return $err;
+ }
+
+ // get string value of ASN.1 object
+ $str = substr($str, $pos, $len);
+
+ return array(
+ 'tag' => $tag,
+ 'str' => $str,
+ );
+ }
+
+ /**
+ * Parse ASN.1 sting [$str] starting from position [$pos].
+ * Returns string representation of number, which can be passed
+ * in bin2int() function of math wrapper.
+ *
+ * @param string $str
+ * @param int $pos
+ * @param object $err_handler
+ *
+ * @return mixed string representation of parsed number on success, PEAR_Error object on error
+ * @access private
+ */
+ function _ASN1ParseInt($str, &$pos, $err_handler)
+ {
+ $tmp = Crypt_RSA_KeyPair::_ASN1Parse($str, $pos, $err_handler);
+ if (PEAR::isError($tmp)) {
+ return $tmp;
+ }
+ if ($tmp['tag'] != 0x02) {
+ $errstr = sprintf("wrong ASN tag value: 0x%02x. Expected 0x02 (INTEGER)", $tmp['tag']);
+ $err = PEAR::raiseError($errstr);
+ $err_handler->pushError($err);
+ return $err;
+ }
+ $pos += strlen($tmp['str']);
+
+ return strrev($tmp['str']);
+ }
+
+ /**
* Crypt_RSA_KeyPair constructor.
*
* @param int $key_len bit length of key pair, which will be generated in constructor
@@ -158,7 +257,6 @@ class Crypt_RSA_KeyPair extends Crypt_RSA_ErrorHandler
* perform different operations with big integers.
* See contents of Crypt/RSA/Math folder for examples of wrappers.
* Read docs/Crypt_RSA/docs/math_wrappers.txt for details.
- *
* @param string $error_handler name of error handler function
*
* @access public
@@ -182,10 +280,48 @@ function Crypt_RSA_KeyPair($key_len, $wrapper_name = 'default', $error_handler =
return;
}
- // generate key pair
- if (!$this->generate($key_len)) {
- // error during generating key pair
- return;
+ if (is_array($key_len)) {
+ // ugly BC hack - it is possible to pass array of [n, e, d] instead of key length
+ list($n, $e, $d) = $key_len;
+
+ // check 2^(e*d) = 2 (mod n)
+ $a_int = $this->_math_obj->bin2int("\x02");
+ $n_int = $this->_math_obj->bin2int($n);
+ $e_int = $this->_math_obj->bin2int($e);
+ $d_int = $this->_math_obj->bin2int($d);
+ $b_int = $this->_math_obj->powMod($a_int, $e_int, $n_int);
+ $b_int = $this->_math_obj->powMod($b_int, $d_int, $n_int);
+ if ($this->_math_obj->cmpAbs($a_int, $b_int)) {
+ $this->pushError(PEAR::raiseError("Incorrect [n, e, d] numbers"));
+ return;
+ }
+
+ // try to create public key object
+ $public_key = &new Crypt_RSA_Key($n, $e, 'public');
+ if ($public_key->isError()) {
+ // error during creating public object
+ $this->pushError($public_key->getLastError());
+ return;
+ }
+
+ // try to create private key object
+ $private_key = &new Crypt_RSA_Key($n, $d, 'private');
+ if ($private_key->isError()) {
+ // error during creating private key object
+ $this->pushError($private_key->getLastError());
+ return;
+ }
+
+ $this->_public_key = $public_key;
+ $this->_private_key = $private_key;
+ $this->_key_len = $public_key->getKeyLength();
+ }
+ else {
+ // generate key pair
+ if (!$this->generate($key_len)) {
+ // error during generating key pair
+ return;
+ }
}
}
@@ -198,13 +334,14 @@ function Crypt_RSA_KeyPair($key_len, $wrapper_name = 'default', $error_handler =
* perform different operations with big integers.
* See contents of Crypt/RSA/Math folder for examples of wrappers.
* Read docs/Crypt_RSA/docs/math_wrappers.txt for details.
+ * @param string $error_handler name of error handler function
*
* @return object new Crypt_RSA_KeyPair object on success or PEAR_Error object on failure
* @access public
*/
- function &factory($key_len, $wrapper_name = 'default')
+ function &factory($key_len, $wrapper_name = 'default', $error_handler = '')
{
- $obj = &new Crypt_RSA_KeyPair($key_len, $wrapper_name);
+ $obj = &new Crypt_RSA_KeyPair($key_len, $wrapper_name, $error_handler);
if ($obj->isError()) {
// error during creating a new object. Retrurn PEAR_Error object
return $obj->getLastError();
@@ -239,38 +376,44 @@ function generate($key_len = null)
// store key length in the _key_len property
$this->_key_len = $key_len;
- // generate two primes p and q
- $p_len = (int) ($key_len / 2) + 1;
+ // set [e] to 0x10001 (65537)
+ $e = $this->_math_obj->bin2int("\x01\x00\x01");
+
+ // generate [p], [q] and [n]
+ $p_len = intval(($key_len + 1) / 2);
$q_len = $key_len - $p_len;
- $p = $this->_math_obj->getRand($p_len, $this->_random_generator, true);
- $p = $this->_math_obj->nextPrime($p);
+ $p1 = $q1 = 0;
do {
+ // generate prime number [$p] with length [$p_len] with the following condition:
+ // GCD($e, $p - 1) = 1
do {
- $q = $this->_math_obj->getRand($q_len, $this->_random_generator, true);
- $tmp_len = $this->_math_obj->bitLen($this->_math_obj->mul($p, $q));
- if ($tmp_len < $key_len) $q_len++;
- elseif ($tmp_len > $key_len) $q_len--;
- } while ($tmp_len != $key_len);
- $q = $this->_math_obj->nextPrime($q);
- $tmp = $this->_math_obj->mul($p, $q);
- } while ($this->_math_obj->bitLen($tmp) != $key_len);
- // $n - is shared modulus
- $n = $this->_math_obj->mul($p, $q);
- // generate public ($e) and private ($d) keys
- $pq = $this->_math_obj->mul($this->_math_obj->dec($p), $this->_math_obj->dec($q));
- do {
- $e = $this->_math_obj->getRand($q_len, $this->_random_generator);
- if ($this->_math_obj->isZero($e) || $this->_math_obj->isZero($this->_math_obj->dec($e))) {
- // exponent cannot be equal to 0 or 1
- continue;
- }
- if ($this->_math_obj->isZero($this->_math_obj->dec($this->_math_obj->gcd($e, $pq)))) {
- // exponent is found
- break;
+ $p = $this->_math_obj->getPrime($p_len, $this->_random_generator);
+ $p1 = $this->_math_obj->dec($p);
+ $tmp = $this->_math_obj->GCD($e, $p1);
+ } while (!$this->_math_obj->isOne($tmp));
+ // generate prime number [$q] with length [$q_len] with the following conditions:
+ // GCD($e, $q - 1) = 1
+ // $q != $p
+ do {
+ $q = $this->_math_obj->getPrime($q_len, $this->_random_generator);
+ $q1 = $this->_math_obj->dec($q);
+ $tmp = $this->_math_obj->GCD($e, $q1);
+ } while (!$this->_math_obj->isOne($tmp) && !$this->_math_obj->cmpAbs($q, $p));
+ // if (p < q), then exchange them
+ if ($this->_math_obj->cmpAbs($p, $q) < 0) {
+ $tmp = $p;
+ $p = $q;
+ $q = $tmp;
}
- } while (true);
+ // calculate n = p * q
+ $n = $this->_math_obj->mul($p, $q);
+ } while ($this->_math_obj->bitLen($n) != $key_len);
+
+ // calculate d = 1/e mod (p - 1) * (q - 1)
+ $pq = $this->_math_obj->mul($p1, $q1);
$d = $this->_math_obj->invmod($e, $pq);
+ // convert [n], [e] and [d] into binary representation
$modulus = $this->_math_obj->int2bin($n);
$public_exp = $this->_math_obj->int2bin($e);
$private_exp = $this->_math_obj->int2bin($d);
@@ -372,6 +515,83 @@ function getKeyLength()
{
return $this->_key_len;
}
+
+ /**
+ * Retrieve RSA keypair from PEM-encoded string, containing RSA private key.
+ * Example of such string:
+ * -----BEGIN RSA PRIVATE KEY-----
+ * MCsCAQACBHtvbSECAwEAAQIEeYrk3QIDAOF3AgMAjCcCAmdnAgJMawIDALEk
+ * -----END RSA PRIVATE KEY-----
+ *
+ * @param string $str PEM-encoded string
+ * @param string $wrapper_name
+ * Name of math wrapper, which will be used to
+ * perform different operations with big integers.
+ * See contents of Crypt/RSA/Math folder for examples of wrappers.
+ * Read docs/Crypt_RSA/docs/math_wrappers.txt for details.
+ * @param string $error_handler name of error handler function
+ *
+ * @return Crypt_RSA_KeyPair object on success, PEAR_Error object on error
+ * @access public
+ */
+ function &fromPEMString($str, $wrapper_name = 'default', $error_handler = '')
+ {
+ // search for base64-encoded private key
+ $err_handler = &new Crypt_RSA_ErrorHandler;
+ $err_handler->setErrorHandler($error_handler);
+
+ if (!preg_match('/-----BEGIN RSA PRIVATE KEY-----[\\r\\n]+([^-]+)-----END RSA PRIVATE KEY-----/', $str, $matches)) {
+ $err = PEAR::raiseError("can't find RSA private key in the string [{$str}]");
+ $err_handler->pushError($err);
+ return $err;
+ }
+
+ // parse private key. It is ASN.1-encoded
+ $str = base64_decode($matches[1]);
+ $pos = 0;
+ $tmp = Crypt_RSA_KeyPair::_ASN1Parse($str, $pos, $err_handler);
+ if (PEAR::isError($tmp)) {
+ return $tmp;
+ }
+ if ($tmp['tag'] != 0x10) {
+ $errstr = sprintf("wrong ASN tag value: 0x%02x. Expected 0x10 (SEQUENCE)", $tmp['tag']);
+ $err = PEAR::raiseError($errstr);
+ $err_handler->pushError($err);
+ return $err;
+ }
+
+ // skip [version] field
+ $tmp = Crypt_RSA_KeyPair::_ASN1ParseInt($str, $pos, $err_handler);
+ if (PEAR::isError($tmp)) {
+ return $tmp;
+ }
+
+ // get [n]
+ $n = Crypt_RSA_KeyPair::_ASN1ParseInt($str, $pos, $err_handler);
+ if (PEAR::isError($n)) {
+ return $n;
+ }
+
+ // get [e]
+ $e = Crypt_RSA_KeyPair::_ASN1ParseInt($str, $pos, $err_handler);
+ if (PEAR::isError($e)) {
+ return $e;
+ }
+
+ // get [d]
+ $d = Crypt_RSA_KeyPair::_ASN1ParseInt($str, $pos, $err_handler);
+ if (PEAR::isError($d)) {
+ return $d;
+ }
+
+ // create Crypt_RSA_KeyPair object.
+ $obj = &new Crypt_RSA_KeyPair(array($n, $e, $d), $wrapper_name, $error_handler);
+ if ($obj->isError()) {
+ return $obj->getLastError();
+ }
+
+ return $obj;
+ }
}
?>
View
64 RSA/Math/BCMath.php
@@ -1,6 +1,4 @@
<?php
-/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
-
/**
* Crypt_RSA allows to do following operations:
* - key pair generation
@@ -18,9 +16,9 @@
* @category Encryption
* @package Crypt_RSA
* @author Alexander Valyalkin <valyala@gmail.com>
- * @copyright 2005 Alexander Valyalkin
+ * @copyright 2006 Alexander Valyalkin
* @license http://www.php.net/license/3_0.txt PHP License 3.0
- * @version 1.0.0
+ * @version 1.1.0
* @link http://pear.php.net/package/Crypt_RSA
*/
@@ -34,7 +32,7 @@
* @category Encryption
* @package Crypt_RSA
* @author Alexander Valyalkin <valyala@gmail.com>
- * @copyright 2005 Alexander Valyalkin
+ * @copyright 2005, 2006 Alexander Valyalkin
* @license http://www.php.net/license/3_0.txt PHP License 3.0
* @link http://pear.php.net/package/Crypt_RSA
* @version @package_version@
@@ -273,44 +271,34 @@ function cmpAbs($num1, $num2)
}
/**
- * Finds next strong pseudoprime number, following after $num
- *
- * @param string $num
- * @return string
- * @access public
- */
- function nextPrime($num)
- {
- if (!bccomp(bcmod($num, '2'), '0')) {
- $num = bcsub($num, '1');
- }
- do {
- $num = bcadd($num, '2');
- } while (!$this->_isPrime($num));
- return $num;
- }
-
- /**
- * Generates random number wich bit length $bits_cnt,
+ * Generates prime number with length $bits_cnt
* using $random_generator as random generator function.
- * If is_set_higher_bit != false, then higer bit of result
- * will be set to 1.
*
* @param int $bits_cnt
* @param string $rnd_generator
- * @return string
* @access public
*/
- function getRand($bits_cnt, $random_generator, $is_set_higher_bit = false)
+ function getPrime($bits_cnt, $random_generator)
{
$bytes_cnt = intval($bits_cnt / 8);
$bits_cnt %= 8;
- $result = $is_set_higher_bit ? 1 : (call_user_func($random_generator) & 1);
- for ($i = 0; $i <= $bytes_cnt; $i++) {
- $result = bcadd(bcmul($result, '256'), call_user_func($random_generator) & 0xff);
- }
- $result = bcdiv($result, 1 << (9 - $bits_cnt));
- return $result;
+ do {
+ // generate random number with length [$bits_cnt]
+ $num = 1;
+ for ($i = 0; $i <= $bytes_cnt; $i++) {
+ $num = bcadd(bcmul($num, '256'), call_user_func($random_generator) & 0xff);
+ }
+ $num = bcdiv($num, 1 << (8 - $bits_cnt));
+
+ // search next closest prime number after [$num]
+ if (!bccomp(bcmod($num, '2'), '0')) {
+ $num = bcadd($num, '1');
+ }
+ while (!$this->_isPrime($num)) {
+ $num = bcadd($num, '2');
+ }
+ } while ($this->bitLen($num) != $bits_cnt);
+ return $num;
}
/**
@@ -326,15 +314,15 @@ function dec($num)
}
/**
- * Returns true, if $num is equal to zero. Else returns false
+ * Returns true, if $num is equal to one. Else returns false
*
* @param string $num
* @return bool
* @access public
*/
- function isZero($num)
+ function isOne($num)
{
- return !bccomp($num, '0');
+ return !bccomp($num, '1');
}
/**
@@ -345,7 +333,7 @@ function isZero($num)
* @return string
* @access public
*/
- function gcd($num1, $num2)
+ function GCD($num1, $num2)
{
do {
$tmp = bcmod($num1, $num2);
View
43 RSA/Math/BigInt.php
@@ -1,6 +1,4 @@
<?php
-/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
-
/**
* Crypt_RSA allows to do following operations:
* - key pair generation
@@ -18,9 +16,9 @@
* @category Encryption
* @package Crypt_RSA
* @author Alexander Valyalkin <valyala@gmail.com>
- * @copyright 2005 Alexander Valyalkin
+ * @copyright 2005, 2006 Alexander Valyalkin
* @license http://www.php.net/license/3_0.txt PHP License 3.0
- * @version 1.0.0
+ * @version 1.1.0
* @link http://pear.php.net/package/Crypt_RSA
*/
@@ -34,7 +32,7 @@
* @category Encryption
* @package Crypt_RSA
* @author Alexander Valyalkin <valyala@gmail.com>
- * @copyright 2005 Alexander Valyalkin
+ * @copyright 2005, 2006 Alexander Valyalkin
* @license http://www.php.net/license/3_0.txt PHP License 3.0
* @link http://pear.php.net/package/Crypt_RSA
* @version @package_version@
@@ -153,32 +151,21 @@ function cmpAbs($num1, $num2)
}
/**
- * Finds next strong pseudoprime number, following after $num
- *
- * @param big_int resource $num
- * @return big_int resource
- * @access public
- */
- function nextPrime($num)
- {
- return bi_next_prime($num);
- }
-
- /**
- * Generates random number wich bit length $bits_cnt,
+ * Generates prime number with length $bits_cnt
* using $random_generator as random generator function.
- * If is_set_higher_bit != false, then higer bit of result
- * will be set to 1.
*
* @param int $bits_cnt
* @param string $rnd_generator
- * @return big_int resource
* @access public
*/
- function getRand($bits_cnt, $random_generator, $is_set_higher_bit = false)
+ function getPrime($bits_cnt, $random_generator)
{
- $tmp = bi_rand($bits_cnt, $random_generator);
- return $is_set_higher_bit ? bi_set_bit($tmp, $bits_cnt - 1) : $tmp;
+ do {
+ $num = bi_rand($bits_cnt, $random_generator);
+ bi_set_bit($num, $bits_cnt - 1);
+ $num = bi_next_prime($num);
+ } while (bi_bit_len($num) != $bits_cnt);
+ return $num;
}
/**
@@ -194,15 +181,15 @@ function dec($num)
}
/**
- * Returns true, if $num is equal to zero. Else returns false
+ * Returns true, if $num is equal to 1. Else returns false
*
* @param big_int resource $num
* @return bool
* @access public
*/
- function isZero($num)
+ function isOne($num)
{
- return bi_is_zero($num);
+ return bi_is_one($num);
}
/**
@@ -213,7 +200,7 @@ function isZero($num)
* @return big_int resource
* @access public
*/
- function gcd($num1, $num2)
+ function GCD($num1, $num2)
{
return bi_gcd($num1, $num2);
}
View
64 RSA/Math/GMP.php
@@ -1,6 +1,4 @@
<?php
-/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
-
/**
* Crypt_RSA allows to do following operations:
* - key pair generation
@@ -18,9 +16,9 @@
* @category Encryption
* @package Crypt_RSA
* @author Alexander Valyalkin <valyala@gmail.com>
- * @copyright 2005 Alexander Valyalkin
+ * @copyright 2005, 2006 Alexander Valyalkin
* @license http://www.php.net/license/3_0.txt PHP License 3.0
- * @version 1.0.0
+ * @version 1.1.0
* @link http://pear.php.net/package/Crypt_RSA
*/
@@ -34,7 +32,7 @@
* @category Encryption
* @package Crypt_RSA
* @author Alexander Valyalkin <valyala@gmail.com>
- * @copyright 2005 Alexander Valyalkin
+ * @copyright 2005, 2006 Alexander Valyalkin
* @license http://www.php.net/license/3_0.txt PHP License 3.0
* @link http://pear.php.net/package/Crypt_RSA
* @version @package_version@
@@ -160,44 +158,34 @@ function cmpAbs($num1, $num2)
}
/**
- * Finds next strong pseudoprime number, following after $num
- *
- * @param gmp resource $num
- * @return gmp resource
- * @access public
- */
- function nextPrime($num)
- {
- if (!gmp_cmp(gmp_mod($num, 2), 0)) {
- $num = gmp_sub($num, 1);
- }
- do {
- $num = gmp_add($num, 2);
- } while (!gmp_prob_prime($num));
- return $num;
- }
-
- /**
- * Generates random number wich bit length $bits_cnt,
+ * Generates prime number with length $bits_cnt
* using $random_generator as random generator function.
- * If is_set_higher_bit != false, then higer bit of result
- * will be set to 1.
*
* @param int $bits_cnt
* @param string $rnd_generator
- * @return gmp resource
* @access public
*/
- function getRand($bits_cnt, $random_generator, $is_set_higher_bit = false)
+ function getPrime($bits_cnt, $random_generator)
{
$bytes_cnt = intval($bits_cnt / 8);
$bits_cnt %= 8;
- $result = $is_set_higher_bit ? 1 : (call_user_func($random_generator) & 1);
- for ($i = 0; $i <= $bytes_cnt; $i++) {
- $result = gmp_add(gmp_mul($result, 256), call_user_func($random_generator) & 0xff);
- }
- $result = gmp_div($result, 1 << (9 - $bits_cnt));
- return $result;
+ do {
+ // generate random number with length [$bits_cnt]
+ $num = 1;
+ for ($i = 0; $i <= $bytes_cnt; $i++) {
+ $num = gmp_add(gmp_mul($num, '256'), call_user_func($random_generator) & 0xff);
+ }
+ $num = gmp_div($num, 1 << (8 - $bits_cnt));
+
+ // search next closest prime number after [$num]
+ if (!gmp_cmp(gmp_mod($num, '2'), '0')) {
+ $num = gmp_add($num, '1');
+ }
+ while (!gmp_prob_prime($num)) {
+ $num = gmp_add($num, '2');
+ }
+ } while ($this->bitLen($num) != $bits_cnt);
+ return $num;
}
/**
@@ -213,15 +201,15 @@ function dec($num)
}
/**
- * Returns true, if $num is equal to zero. Else returns false
+ * Returns true, if $num is equal to one. Else returns false
*
* @param gmp resource $num
* @return bool
* @access public
*/
- function isZero($num)
+ function isOne($num)
{
- return !gmp_cmp($num, 0);
+ return !gmp_cmp($num, 1);
}
/**
@@ -232,7 +220,7 @@ function isZero($num)
* @return gmp resource
* @access public
*/
- function gcd($num1, $num2)
+ function GCD($num1, $num2)
{
return gmp_gcd($num1, $num2);
}
View
8 RSA/MathLoader.php
@@ -1,6 +1,4 @@
<?php
-/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
-
/**
* Crypt_RSA allows to do following operations:
* - key pair generation
@@ -18,9 +16,9 @@
* @category Encryption
* @package Crypt_RSA
* @author Alexander Valyalkin <valyala@gmail.com>
- * @copyright 2005 Alexander Valyalkin
+ * @copyright 2005, 2006 Alexander Valyalkin
* @license http://www.php.net/license/3_0.txt PHP License 3.0
- * @version 1.0.0
+ * @version 1.1.0
* @link http://pear.php.net/package/Crypt_RSA
*/
@@ -49,7 +47,7 @@
* @category Encryption
* @package Crypt_RSA
* @author Alexander Valyalkin <valyala@gmail.com>
- * @copyright 2005 Alexander Valyalkin
+ * @copyright 2005, 2006 Alexander Valyalkin
* @license http://www.php.net/license/3_0.txt PHP License 3.0
* @link http://pear.php.net/package/Crypt_RSA
* @version @package_version@
View
7 docs/math_wrappers.txt
@@ -14,11 +14,10 @@ Math wrapper class must provide interface with following functions:
powmod($a, $b, $mod) - calculates ($a ^ $b) % $mod
mul($a, $b) - calculates $a * $b
cmpAbs($a, $b) - compares abs($a) with abs($b)
- nextPrime($num) - returns next prime, folloing by the $num
- getRand($bits_cnt, $rnd_generator, $is_set_higher_bit) - generates random number with length $bits_cnt
+ getPrime($bits_cnt, $rnd_generator) - returns prime with length $bits_cnt
dec($num) - calculates $num - 1
- isZero($num) - returns true, if $num is zero
- gcd($a, $b) - calculates greatest common divider of $an and $b
+ isOne($num) - returns true, if $num is one
+ GCD($a, $b) - calculates greatest common divider of $an and $b
invmod($num, $mod) - calculates number $num1 such as ($num1 * $num) % $mod = 1
bitOr($a, $b, $start_bit) - calculates bitwise ($a or ($b << $start_bit))
subint($num, $start, $bit_len) - returns part of number $num, starting at position $start, with length $len
View
6 examples/web_interface.php
@@ -1,6 +1,4 @@
<?php
-/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
-
/**
* Crypt_RSA allows to do following operations:
* - key pair generation
@@ -21,9 +19,9 @@
* @category Encryption
* @package Crypt_RSA
* @author Alexander Valyalkin <valyala@gmail.com>
- * @copyright 2005 Alexander Valyalkin
+ * @copyright 2005, 2006 Alexander Valyalkin
* @license http://www.php.net/license/3_0.txt PHP License 3.0
- * @version 1.0.0
+ * @version 1.1.0
* @link http://pear.php.net/package/Crypt_RSA
*/
View
289 package.xml
@@ -1,150 +1,151 @@
-<?xml version="1.0" encoding="ISO-8859-1" ?>
+<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE package SYSTEM "http://pear.php.net/dtd/package-1.0">
-<package version="1.0">
- <name>Crypt_RSA</name>
- <summary>Provides RSA-like key generation, encryption/decryption, signing and signature checking</summary>
- <description>This package allows you to use two-key strong cryptography like RSA with arbitrary key length.
+<package version="1.0" packagerversion="1.4.6">
+ <name>Crypt_RSA</name>
+ <summary>Provides RSA-like key generation, encryption/decryption, signing and signature checking</summary>
+ <description>This package allows you to use two-key strong cryptography like RSA with arbitrary key length.
It uses one of the following extensions for math calculations:
- PECL big_int extension ( http://pecl.php.net/packages/big_int ) version greater than or equal to 1.0.3
- PHP GMP extension ( http://php.net/gmp )
- - PHP BCMath extension ( http://php.net/manual/en/ref.bc.php ) for both PHP4 and PHP5</description>
- <maintainers>
- <maintainer>
- <user>valyala</user>
- <name>Alexander Valyalkin</name>
- <email>valyala@gmail.com</email>
- <role>lead</role>
- </maintainer>
+ - PHP BCMath extension ( http://php.net/manual/en/ref.bc.php ) for both PHP4 and PHP5
+ </description>
+ <maintainers>
+ <maintainer>
+ <user>valyala</user>
+ <name>Alexander Valyalkin</name>
+ <email>valyala@gmail.com</email>
+ <role>lead</role>
+ </maintainer>
</maintainers>
- <release>
- <version>1.0.0</version>
- <date>2005-05-11</date>
- <license>PHP</license>
- <state>stable</state>
- <notes>* it is the first stable release of the package
-* added optional $math_wrapper parameter to Crypt_RSA, Crypt_RSA_Key and Crypt_RSA_KeyPair constructors. It allows to catch errors by user's error handler during objects construction
-* updated documentation in docs/Crypt_RSA/docs folder. But it is still very ugly ;)</notes>
- <deps>
- <dep type="ext" rel="has" optional="yes">bcmath</dep>
- <dep type="ext" rel="has" optional="yes">gmp</dep>
- <dep type="pkg" rel="ge" version="1.0.3" optional="yes">big_int</dep>
- </deps>
- <provides type="class" name="Crypt_RSA" />
- <provides type="function" name="Crypt_RSA::factory" />
- <provides type="function" name="Crypt_RSA::setParams" />
- <provides type="function" name="Crypt_RSA::encrypt" />
- <provides type="function" name="Crypt_RSA::encryptBinary" />
- <provides type="function" name="Crypt_RSA::decrypt" />
- <provides type="function" name="Crypt_RSA::decryptBinary" />
- <provides type="function" name="Crypt_RSA::createSign" />
- <provides type="function" name="Crypt_RSA::validateSign" />
- <provides type="function" name="Crypt_RSA::isError" />
- <provides type="function" name="Crypt_RSA::getErrorList" />
- <provides type="function" name="Crypt_RSA::getLastError" />
- <provides type="function" name="Crypt_RSA::pushError" />
- <provides type="function" name="Crypt_RSA::getErrorHandler" />
- <provides type="function" name="Crypt_RSA::setErrorHandler" />
- <provides type="class" name="Crypt_RSA_Key" />
- <provides type="function" name="Crypt_RSA_Key::factory" />
- <provides type="function" name="Crypt_RSA_Key::getKeyLength" />
- <provides type="function" name="Crypt_RSA_Key::getModulus" />
- <provides type="function" name="Crypt_RSA_Key::getExponent" />
- <provides type="function" name="Crypt_RSA_Key::getKeyType" />
- <provides type="function" name="Crypt_RSA_Key::toString" />
- <provides type="function" name="Crypt_RSA_Key::fromString" />
- <provides type="function" name="Crypt_RSA_Key::isValid" />
- <provides type="function" name="Crypt_RSA_Key::isError" />
- <provides type="function" name="Crypt_RSA_Key::getErrorList" />
- <provides type="function" name="Crypt_RSA_Key::getLastError" />
- <provides type="function" name="Crypt_RSA_Key::pushError" />
- <provides type="function" name="Crypt_RSA_Key::getErrorHandler" />
- <provides type="function" name="Crypt_RSA_Key::setErrorHandler" />
- <provides type="class" name="Crypt_RSA_KeyPair" />
- <provides type="function" name="Crypt_RSA_KeyPair::factory" />
- <provides type="function" name="Crypt_RSA_KeyPair::generate" />
- <provides type="function" name="Crypt_RSA_KeyPair::getPublicKey" />
- <provides type="function" name="Crypt_RSA_KeyPair::getPrivateKey" />
- <provides type="function" name="Crypt_RSA_KeyPair::setRandomGenerator" />
- <provides type="function" name="Crypt_RSA_KeyPair::getKeyLength" />
- <provides type="function" name="Crypt_RSA_KeyPair::isError" />
- <provides type="function" name="Crypt_RSA_KeyPair::getErrorList" />
- <provides type="function" name="Crypt_RSA_KeyPair::getLastError" />
- <provides type="function" name="Crypt_RSA_KeyPair::pushError" />
- <provides type="function" name="Crypt_RSA_KeyPair::getErrorHandler" />
- <provides type="function" name="Crypt_RSA_KeyPair::setErrorHandler" />
- <provides type="class" name="Crypt_RSA_ErrorHandler" />
- <provides type="function" name="Crypt_RSA_ErrorHandler::isError" />
- <provides type="function" name="Crypt_RSA_ErrorHandler::getErrorList" />
- <provides type="function" name="Crypt_RSA_ErrorHandler::getLastError" />
- <provides type="function" name="Crypt_RSA_ErrorHandler::pushError" />
- <provides type="function" name="Crypt_RSA_ErrorHandler::getErrorHandler" />
- <provides type="function" name="Crypt_RSA_ErrorHandler::setErrorHandler" />
- <provides type="class" name="Crypt_RSA_MathLoader" />
- <provides type="function" name="Crypt_RSA_MathLoader::loadWrapper" />
- <provides type="class" name="Crypt_RSA_Math_BigInt" />
- <provides type="function" name="Crypt_RSA_Math_BigInt::bin2int" />
- <provides type="function" name="Crypt_RSA_Math_BigInt::int2bin" />
- <provides type="function" name="Crypt_RSA_Math_BigInt::powmod" />
- <provides type="function" name="Crypt_RSA_Math_BigInt::mul" />
- <provides type="function" name="Crypt_RSA_Math_BigInt::cmpAbs" />
- <provides type="function" name="Crypt_RSA_Math_BigInt::nextPrime" />
- <provides type="function" name="Crypt_RSA_Math_BigInt::getRand" />
- <provides type="function" name="Crypt_RSA_Math_BigInt::dec" />
- <provides type="function" name="Crypt_RSA_Math_BigInt::isZero" />
- <provides type="function" name="Crypt_RSA_Math_BigInt::gcd" />
- <provides type="function" name="Crypt_RSA_Math_BigInt::invmod" />
- <provides type="function" name="Crypt_RSA_Math_BigInt::bitLen" />
- <provides type="function" name="Crypt_RSA_Math_BigInt::bitOr" />
- <provides type="function" name="Crypt_RSA_Math_BigInt::subint" />
- <provides type="function" name="Crypt_RSA_Math_BigInt::getWrapperName" />
- <provides type="class" name="Crypt_RSA_Math_BCMath" />
- <provides type="function" name="Crypt_RSA_Math_BCMath::bin2int" />
- <provides type="function" name="Crypt_RSA_Math_BCMath::int2bin" />
- <provides type="function" name="Crypt_RSA_Math_BCMath::powmod" />
- <provides type="function" name="Crypt_RSA_Math_BCMath::mul" />
- <provides type="function" name="Crypt_RSA_Math_BCMath::cmpAbs" />
- <provides type="function" name="Crypt_RSA_Math_BCMath::nextPrime" />
- <provides type="function" name="Crypt_RSA_Math_BCMath::getRand" />
- <provides type="function" name="Crypt_RSA_Math_BCMath::dec" />
- <provides type="function" name="Crypt_RSA_Math_BCMath::isZero" />
- <provides type="function" name="Crypt_RSA_Math_BCMath::gcd" />
- <provides type="function" name="Crypt_RSA_Math_BCMath::invmod" />
- <provides type="function" name="Crypt_RSA_Math_BCMath::bitLen" />
- <provides type="function" name="Crypt_RSA_Math_BCMath::bitOr" />
- <provides type="function" name="Crypt_RSA_Math_BCMath::subint" />
- <provides type="function" name="Crypt_RSA_Math_BCMath::getWrapperName" />
- <provides type="class" name="Crypt_RSA_Math_GMP" />
- <provides type="function" name="Crypt_RSA_Math_GMP::bin2int" />
- <provides type="function" name="Crypt_RSA_Math_GMP::int2bin" />
- <provides type="function" name="Crypt_RSA_Math_GMP::powmod" />
- <provides type="function" name="Crypt_RSA_Math_GMP::mul" />
- <provides type="function" name="Crypt_RSA_Math_GMP::cmpAbs" />
- <provides type="function" name="Crypt_RSA_Math_GMP::nextPrime" />
- <provides type="function" name="Crypt_RSA_Math_GMP::getRand" />
- <provides type="function" name="Crypt_RSA_Math_GMP::dec" />
- <provides type="function" name="Crypt_RSA_Math_GMP::isZero" />
- <provides type="function" name="Crypt_RSA_Math_GMP::gcd" />
- <provides type="function" name="Crypt_RSA_Math_GMP::invmod" />
- <provides type="function" name="Crypt_RSA_Math_GMP::bitLen" />
- <provides type="function" name="Crypt_RSA_Math_GMP::bitOr" />
- <provides type="function" name="Crypt_RSA_Math_GMP::subint" />
- <provides type="function" name="Crypt_RSA_Math_GMP::getWrapperName" />
- <filelist>
- <file role="php" baseinstalldir="Crypt" md5sum="169de84b36724ae7b359f0015e2209df" name="RSA.php"/>
- <file role="php" baseinstalldir="Crypt" md5sum="b74b1a3b3257db7c513e2ecb95f7cbb5" name="RSA\ErrorHandler.php"/>
- <file role="php" baseinstalldir="Crypt" md5sum="518823a3c9d5d73783f7d51fc5f652a3" name="RSA\Key.php"/>
- <file role="php" baseinstalldir="Crypt" md5sum="c40b4c7ddd98f66feeb062ae2377a469" name="RSA\KeyPair.php"/>
- <file role="php" baseinstalldir="Crypt" md5sum="a927b9050e96d4ae50b10bb00cfcb91d" name="RSA\MathLoader.php"/>
- <file role="php" baseinstalldir="Crypt" md5sum="079773dbbf41b36e926deb32fda3ee24" name="RSA\Math\BCMath.php"/>
- <file role="php" baseinstalldir="Crypt" md5sum="f3706db76f00783e64ad478e9597e204" name="RSA\Math\BigInt.php"/>
- <file role="php" baseinstalldir="Crypt" md5sum="fc03fa1b507a25f4832c6730c7b11768" name="RSA\Math\GMP.php"/>
- <file role="doc" baseinstalldir="Crypt" md5sum="8e699cdffe3e4e3d1341e2f1dc81d29d" name="CREDITS"/>
- <file role="doc" baseinstalldir="Crypt" md5sum="0e51aca28890cb3d18311aa04b325184" name="LICENSE"/>
- <file role="doc" baseinstalldir="Crypt" md5sum="d772bb3eb1731824e05efd572cdd0a5f" name="docs\Crypt_RSA.txt"/>
- <file role="doc" baseinstalldir="Crypt" md5sum="db20c3324c44b51871ab25b28485359c" name="docs\math_wrappers.txt"/>
- <file role="doc" baseinstalldir="Crypt" md5sum="f95c141a26e0feed3f1db15ad89bac8d" name="examples\web_interface.php"/>
- <file role="test" baseinstalldir="Crypt" md5sum="f5aa5b61a40121e9d5fd1f96d7a7fefe" name="tests\test1.php"/>
- <file role="test" baseinstalldir="Crypt" md5sum="dab099e2d591d3c892b3d422a66f3a3c" name="tests\test2.php"/>
- </filelist>
- </release>
+ <release>
+ <version>1.1.0b</version>
+ <date>2006-02-23</date>
+ <license>PHP</license>
+ <state>beta</state>
+ <notes>* added fromPEMString() function in Crypt_RSA_KeyPair class. This function allows to retrieve RSA keypair from PEM-encoded strings. Thanks to all the people who sponsored .pem developement
+* modified key generation algorithm
+* added optional &apos;error_handler&apos; parameter to factory() methods in all classes
+* modified math wrapper interface: isZero(), nextPrime() and getRand() substituted by isOne() and getPrime() functions
+ </notes>
+ <deps>
+ <dep type="ext" rel="has" optional="yes">bcmath</dep>
+ <dep type="ext" rel="has" optional="yes">gmp</dep>
+ <dep type="pkg" rel="ge" version="1.0.3" optional="yes">big_int</dep>
+ </deps>
+ <provides type="class" name="Crypt_RSA" />
+ <provides type="function" name="Crypt_RSA::factory" />
+ <provides type="function" name="Crypt_RSA::setParams" />
+ <provides type="function" name="Crypt_RSA::encrypt" />
+ <provides type="function" name="Crypt_RSA::encryptBinary" />
+ <provides type="function" name="Crypt_RSA::decrypt" />
+ <provides type="function" name="Crypt_RSA::decryptBinary" />
+ <provides type="function" name="Crypt_RSA::createSign" />
+ <provides type="function" name="Crypt_RSA::validateSign" />
+ <provides type="function" name="Crypt_RSA::isError" />
+ <provides type="function" name="Crypt_RSA::getErrorList" />
+ <provides type="function" name="Crypt_RSA::getLastError" />
+ <provides type="function" name="Crypt_RSA::pushError" />
+ <provides type="function" name="Crypt_RSA::getErrorHandler" />
+ <provides type="function" name="Crypt_RSA::setErrorHandler" />
+ <provides type="class" name="Crypt_RSA_Key" />
+ <provides type="function" name="Crypt_RSA_Key::factory" />
+ <provides type="function" name="Crypt_RSA_Key::getKeyLength" />
+ <provides type="function" name="Crypt_RSA_Key::getModulus" />
+ <provides type="function" name="Crypt_RSA_Key::getExponent" />
+ <provides type="function" name="Crypt_RSA_Key::getKeyType" />
+ <provides type="function" name="Crypt_RSA_Key::toString" />
+ <provides type="function" name="Crypt_RSA_Key::fromString" />
+ <provides type="function" name="Crypt_RSA_Key::isValid" />
+ <provides type="function" name="Crypt_RSA_Key::isError" />
+ <provides type="function" name="Crypt_RSA_Key::getErrorList" />
+ <provides type="function" name="Crypt_RSA_Key::getLastError" />
+ <provides type="function" name="Crypt_RSA_Key::pushError" />
+ <provides type="function" name="Crypt_RSA_Key::getErrorHandler" />
+ <provides type="function" name="Crypt_RSA_Key::setErrorHandler" />
+ <provides type="class" name="Crypt_RSA_KeyPair" />
+ <provides type="function" name="Crypt_RSA_KeyPair::factory" />
+ <provides type="function" name="Crypt_RSA_KeyPair::generate" />
+ <provides type="function" name="Crypt_RSA_KeyPair::getPublicKey" />
+ <provides type="function" name="Crypt_RSA_KeyPair::getPrivateKey" />
+ <provides type="function" name="Crypt_RSA_KeyPair::setRandomGenerator" />
+ <provides type="function" name="Crypt_RSA_KeyPair::getKeyLength" />
+ <provides type="function" name="Crypt_RSA_KeyPair::fromPEMString" />
+ <provides type="function" name="Crypt_RSA_KeyPair::isError" />
+ <provides type="function" name="Crypt_RSA_KeyPair::getErrorList" />
+ <provides type="function" name="Crypt_RSA_KeyPair::getLastError" />
+ <provides type="function" name="Crypt_RSA_KeyPair::pushError" />
+ <provides type="function" name="Crypt_RSA_KeyPair::getErrorHandler" />
+ <provides type="function" name="Crypt_RSA_KeyPair::setErrorHandler" />
+ <provides type="class" name="Crypt_RSA_ErrorHandler" />
+ <provides type="function" name="Crypt_RSA_ErrorHandler::isError" />
+ <provides type="function" name="Crypt_RSA_ErrorHandler::getErrorList" />
+ <provides type="function" name="Crypt_RSA_ErrorHandler::getLastError" />
+ <provides type="function" name="Crypt_RSA_ErrorHandler::pushError" />
+ <provides type="function" name="Crypt_RSA_ErrorHandler::getErrorHandler" />
+ <provides type="function" name="Crypt_RSA_ErrorHandler::setErrorHandler" />
+ <provides type="class" name="Crypt_RSA_MathLoader" />
+ <provides type="function" name="Crypt_RSA_MathLoader::loadWrapper" />
+ <provides type="class" name="Crypt_RSA_Math_BigInt" />
+ <provides type="function" name="Crypt_RSA_Math_BigInt::bin2int" />
+ <provides type="function" name="Crypt_RSA_Math_BigInt::int2bin" />
+ <provides type="function" name="Crypt_RSA_Math_BigInt::powmod" />
+ <provides type="function" name="Crypt_RSA_Math_BigInt::mul" />
+ <provides type="function" name="Crypt_RSA_Math_BigInt::cmpAbs" />
+ <provides type="function" name="Crypt_RSA_Math_BigInt::getPrime" />
+ <provides type="function" name="Crypt_RSA_Math_BigInt::dec" />
+ <provides type="function" name="Crypt_RSA_Math_BigInt::isOne" />
+ <provides type="function" name="Crypt_RSA_Math_BigInt::GCD" />
+ <provides type="function" name="Crypt_RSA_Math_BigInt::invmod" />
+ <provides type="function" name="Crypt_RSA_Math_BigInt::bitLen" />
+ <provides type="function" name="Crypt_RSA_Math_BigInt::bitOr" />
+ <provides type="function" name="Crypt_RSA_Math_BigInt::subint" />
+ <provides type="function" name="Crypt_RSA_Math_BigInt::getWrapperName" />
+ <provides type="class" name="Crypt_RSA_Math_BCMath" />
+ <provides type="function" name="Crypt_RSA_Math_BCMath::bin2int" />
+ <provides type="function" name="Crypt_RSA_Math_BCMath::int2bin" />
+ <provides type="function" name="Crypt_RSA_Math_BCMath::powmod" />
+ <provides type="function" name="Crypt_RSA_Math_BCMath::mul" />
+ <provides type="function" name="Crypt_RSA_Math_BCMath::cmpAbs" />
+ <provides type="function" name="Crypt_RSA_Math_BCMath::getPrime" />
+ <provides type="function" name="Crypt_RSA_Math_BCMath::dec" />
+ <provides type="function" name="Crypt_RSA_Math_BCMath::isOne" />
+ <provides type="function" name="Crypt_RSA_Math_BCMath::GCD" />
+ <provides type="function" name="Crypt_RSA_Math_BCMath::invmod" />
+ <provides type="function" name="Crypt_RSA_Math_BCMath::bitLen" />
+ <provides type="function" name="Crypt_RSA_Math_BCMath::bitOr" />
+ <provides type="function" name="Crypt_RSA_Math_BCMath::subint" />
+ <provides type="function" name="Crypt_RSA_Math_BCMath::getWrapperName" />
+ <provides type="class" name="Crypt_RSA_Math_GMP" />
+ <provides type="function" name="Crypt_RSA_Math_GMP::bin2int" />
+ <provides type="function" name="Crypt_RSA_Math_GMP::int2bin" />
+ <provides type="function" name="Crypt_RSA_Math_GMP::powmod" />
+ <provides type="function" name="Crypt_RSA_Math_GMP::mul" />
+ <provides type="function" name="Crypt_RSA_Math_GMP::cmpAbs" />
+ <provides type="function" name="Crypt_RSA_Math_GMP::getPrime" />
+ <provides type="function" name="Crypt_RSA_Math_GMP::dec" />
+ <provides type="function" name="Crypt_RSA_Math_GMP::isOne" />
+ <provides type="function" name="Crypt_RSA_Math_GMP::GCD" />
+ <provides type="function" name="Crypt_RSA_Math_GMP::invmod" />
+ <provides type="function" name="Crypt_RSA_Math_GMP::bitLen" />
+ <provides type="function" name="Crypt_RSA_Math_GMP::bitOr" />
+ <provides type="function" name="Crypt_RSA_Math_GMP::subint" />
+ <provides type="function" name="Crypt_RSA_Math_GMP::getWrapperName" />
+ <filelist>
+ <file role="php" baseinstalldir="Crypt" md5sum="4dc4fb4926c0fe36966b2926b0efa840" name="RSA.php"/>
+ <file role="php" baseinstalldir="Crypt" md5sum="77076008a77505ce72a7fcc0c48f9656" name="RSA/ErrorHandler.php"/>
+ <file role="php" baseinstalldir="Crypt" md5sum="b6a063ef6c25c2589e92401c72bb9ba8" name="RSA/Key.php"/>
+ <file role="php" baseinstalldir="Crypt" md5sum="5a8c889b028febf7c5858b8d25ac6d20" name="RSA/KeyPair.php"/>
+ <file role="php" baseinstalldir="Crypt" md5sum="a1bb53e6b89e5afb9f8fddcb7327442e" name="RSA/MathLoader.php"/>
+ <file role="php" baseinstalldir="Crypt" md5sum="2c15981a896aa267b340ce34157355fc" name="RSA/Math/BCMath.php"/>
+ <file role="php" baseinstalldir="Crypt" md5sum="da034f2ea0e1acff984b3ee0490b78dd" name="RSA/Math/BigInt.php"/>
+ <file role="php" baseinstalldir="Crypt" md5sum="036f2082efdac1111d5ea668f90732f3" name="RSA/Math/GMP.php"/>
+ <file role="doc" baseinstalldir="Crypt" md5sum="8e699cdffe3e4e3d1341e2f1dc81d29d" name="CREDITS"/>
+ <file role="doc" baseinstalldir="Crypt" md5sum="f4c7bb36cd36382f0cf5fd4594f73fee" name="LICENSE"/>
+ <file role="doc" baseinstalldir="Crypt" md5sum="ca9d024c219dba927b4aee2dc1fb5737" name="docs/Crypt_RSA.txt"/>
+ <file role="doc" baseinstalldir="Crypt" md5sum="78b93d6745a22efeafea6a851c488443" name="docs/math_wrappers.txt"/>
+ <file role="doc" baseinstalldir="Crypt" md5sum="2f6ff6d854f5ca83fcaf3ce922cba3da" name="examples/web_interface.php"/>
+ <file role="test" baseinstalldir="Crypt" md5sum="5e6d9026f1907003a0af23c08f9f27cb" name="tests/test1.php"/>
+ <file role="test" baseinstalldir="Crypt" md5sum="4c5a5a7bd18ee31cb21b57bf2eb96b43" name="tests/test2.php"/>
+ </filelist>
+ </release>
</package>
View
47 tests/test1.php
@@ -1,6 +1,4 @@
<?php
-/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
-
/**
* Crypt_RSA allows to do following operations:
* - key pair generation
@@ -21,9 +19,9 @@
* @category Encryption
* @package Crypt_RSA
* @author Alexander Valyalkin <valyala@gmail.com>
- * @copyright 2005 Alexander Valyalkin
+ * @copyright 2005, 2006 Alexander Valyalkin
* @license http://www.php.net/license/3_0.txt PHP License 3.0
- * @version 1.0.0
+ * @version 1.1.0
* @link http://pear.php.net/package/Crypt_RSA
*/
@@ -46,9 +44,9 @@
See contents of /RSA/MathClasses folder for examples.
BigInt is much faster than BCMath.
*/
-//define('MATH_LIBRARY', 'BigInt');
+define('MATH_LIBRARY', 'BigInt');
//define('MATH_LIBRARY', 'GMP');
-define('MATH_LIBRARY', 'BCMath');
+//define('MATH_LIBRARY', 'BCMath');
$errors = array();
@@ -67,20 +65,53 @@
$errors[] = "wrong result returned from Crypt_RSA_KeyPair::getKeyLength() function";
}
+// check Crypt_RSA_KeyPair::fromPEMString() function
+$str = "
+-----BEGIN RSA PRIVATE KEY-----
+MIIBPAIBAAJBAKSLT0KZTXYxHr6U/9GYBbnV8vxGkIleDE4aiVMRxuofOjcHDCoI
+qsrVjgP78BrVqWMAAeQ9i0dXxz9zhy0+h7MCAwEAAQJBAI6OL1Yo0Uaj2doN5vDk
+f5l4dfMBA7ovZAPK08zHawlsLvZTzxOQJhKquN01aIJA2wpzixcC9T2PgI6XW6jx
+HkECIQDOEVpVZcE2tSnU3TwulVAC8V82akNAEH8ht6eqsEVWkwIhAMxqMc4Av7hs
+ioAs1H9NvkF01xYVhyiEc4rzgVlmjp5hAiEAi53AOYnmvd1CyWFXrCwn+MZ2/xRC
+Gj7TFBItvH0PjZcCIBi9kaGZPZsYp/qzclSmGCzb81xc5qrkvQdISZOEciaBAiEA
+vLq0MTN4jkO2DOC4qxvKc1l4383nks1g/cljSO/y0pw=
+-----END RSA PRIVATE KEY-----
+";
+
+$keypair = Crypt_RSA_KeyPair::fromPEMString($str, MATH_LIBRARY, 'check_error');
+
+$public_key = $keypair->getPublicKey();
+$private_key = $keypair->getPrivateKey();
+$key_length = $keypair->getKeyLength();
+
+if ($key_length != 512) {
+ $errors[] = "incorrect key length retrieved from PEM file";
+}
+
+$rsa_obj = new Crypt_RSA(array(), MATH_LIBRARY, 'check_error');
+
+$text = 'test text';
+$enc_text = $rsa_obj->encrypt($text, $public_key);
+$dec_text = $rsa_obj->decrypt($enc_text, $private_key);
+
+if ($dec_text != $text) {
+ $errors[] = "decrypted text differs from encrypted text in Crypt_RSA_KeyPair::fromPEMString() check";
+}
+
// try to generate 256-bit key pair
$key_pair->generate(256);
///////////////////////////////////////////////
// test all functionality of Crypt_RSA_Key class
///////////////////////////////////////////////
$rsa_obj = new Crypt_RSA(array(), MATH_LIBRARY, 'check_error');
-$key_pair = new Crypt_RSA_KeyPair(8, MATH_LIBRARY, 'check_error'); // extra small key pair (8-bit ;) )
+$key_pair = new Crypt_RSA_KeyPair(32, MATH_LIBRARY, 'check_error'); // extra small key pair (32-bit)
$public_key = $key_pair->getPublicKey();
$private_key = $key_pair->getPrivateKey();
// check the length of public key
-if ($public_key->getKeyLength() != 8) {
+if ($public_key->getKeyLength() != 32) {
$errors[] = "wrong result returned from Crypt_RSA_Key::getKeyLength() function";
}
View
6 tests/test2.php
@@ -1,6 +1,4 @@
<?php
-/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
-
/**
* Crypt_RSA allows to do following operations:
* - key pair generation
@@ -21,9 +19,9 @@
* @category Encryption
* @package Crypt_RSA
* @author Alexander Valyalkin <valyala@gmail.com>
- * @copyright 2005 Alexander Valyalkin
+ * @copyright 2005, 2006 Alexander Valyalkin
* @license http://www.php.net/license/3_0.txt PHP License 3.0
- * @version 1.0.0
+ * @version 1.1.0
* @link http://pear.php.net/package/Crypt_RSA
*/

0 comments on commit 9e7045a

Please sign in to comment.
Something went wrong with that request. Please try again.