summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authortailor <cygnus@janrain.com>2005-12-29 18:57:44 +0000
committertailor <cygnus@janrain.com>2005-12-29 18:57:44 +0000
commit8528b291e2b8de89282b65b82b58768b0804ccf6 (patch)
treeec13dbe09e8d62a37ea1ced20732140cd9f4af7c
parent74d6c3a384c4ab8df09f72c034e6f173f44dcfc8 (diff)
downloadphp-openid-8528b291e2b8de89282b65b82b58768b0804ccf6.zip
php-openid-8528b291e2b8de89282b65b82b58768b0804ccf6.tar.gz
php-openid-8528b291e2b8de89282b65b82b58768b0804ccf6.tar.bz2
[project @ Refactored DiffieHellman to use Net_OpenID_MathLibrary]
-rw-r--r--Net/OpenID/CryptUtil.php2
-rw-r--r--Net/OpenID/DiffieHellman.php161
2 files changed, 50 insertions, 113 deletions
diff --git a/Net/OpenID/CryptUtil.php b/Net/OpenID/CryptUtil.php
index 8d8f4cb..ec83df4 100644
--- a/Net/OpenID/CryptUtil.php
+++ b/Net/OpenID/CryptUtil.php
@@ -17,7 +17,7 @@
/**
* Require the HMAC/SHA-1 implementation for creating such hashes.
*/
-require('HMACSHA1.php');
+require_once('HMACSHA1.php');
if (!defined('Net_OpenID_RAND_SOURCE')) {
/**
diff --git a/Net/OpenID/DiffieHellman.php b/Net/OpenID/DiffieHellman.php
index 8c1ca77..d56c69e 100644
--- a/Net/OpenID/DiffieHellman.php
+++ b/Net/OpenID/DiffieHellman.php
@@ -14,135 +14,72 @@
*/
/**
- * Check to see if GMP or Bcmath are available and supply the
- * appropriate implementation.
+ * Require CryptUtil because we need to get a math library wrapper
+ * object.
*/
-if (extension_loaded('gmp') || @dl('gmp.' . PHP_SHLIB_SUFFIX) ||
- @dl('php_gmp.' . PHP_SHLIB_SUFFIX)) {
-
- define('Net_OpenID_math_type', 'gmp');
-
- /**
- * The Diffie-Hellman key exchange class.
- *
- * @package OpenID
- */
- class Net_OpenID_DiffieHellman {
- var $DEFAULT_MOD = '155172898181473697471232257763715539915724801966915404479707795314057629378541917580651227423698188993727816152646631438561595825688188889951272158842675419950341258706556549803580104870537681476726513255747040765857479291291572334510643245094715007229621094194349783925984760375594985848253359305585439638443';
-
- var $DEFAULT_GEN = '2';
-
- var $mod;
- var $gen;
- var $private;
-
- function generateRandom() {
- // XXX: not cryptographically secure (potentially predictable)
- $limb_cnt = 31;
- do {
- $rdm = gmp_random($limb_cnt--);
- } while (gmp_cmp( $minval, $rdm) > 0);
- return $rdm;
- }
+require_once('CryptUtil.php');
- function Net_OpenID_DiffieHellman($mod=NULL, $gen=NULL, $private=NULL) {
- if ($mod === NULL) {
- $this->mod = gmp_init($this->DEFAULT_MOD, 10);
- } else {
- $this->mod = $mod;
- }
+/**
+ * The Diffie-Hellman key exchange class. This class relies on
+ * Net_OpenID_MathLibrary to perform large number operations.
+ *
+ * @package OpenID
+ */
+class Net_OpenID_DiffieHellman {
+ var $DEFAULT_MOD = '155172898181473697471232257763715539915724801966915404479707795314057629378541917580651227423698188993727816152646631438561595825688188889951272158842675419950341258706556549803580104870537681476726513255747040765857479291291572334510643245094715007229621094194349783925984760375594985848253359305585439638443';
- if ($gen === NULL) {
- $this->gen = gmp_init($this->DEFAULT_GEN, 10);
- } else {
- $this->gen = $gen;
- }
+ var $DEFAULT_GEN = '2';
- $this->private =
- $private === NULL ? $this->generateRandom() : $private;
+ var $mod;
+ var $gen;
+ var $private;
+ var $lib = null;
- $this->public = user_error("not implemented", E_USER_ERROR);
- }
+ function Net_OpenID_DiffieHellman($mod = NULL, $gen = NULL, $private = NULL) {
- function createKeyExchange( ) {
- return Net_OpenID_BigInt::powm( $this->g, $this->x, $this->p);
- }
+ $this->lib =& Net_OpenID_MathLibrary::getLibWrapper();
- function decryptKeyExchange( $keyEx ) {
- return Net_OpenID_BigInt::powm( $keyEx, $this->x, $this->p );
+ if (!$this->lib) {
+ // This should NEVER occur, but if there's a bug in
+ // Net_OpenID_MathLibrary::getLibWrapper, it might.
+ trigger_error("Big integer fallback implementation unavailable.", E_USER_ERROR);
}
- }
-} elseif (extension_loaded('bcmath') || @dl('bcmath.' . PHP_SHLIB_SUFFIX) ||
- @dl('php_bcmath.' . PHP_SHLIB_SUFFIX)) {
-
- /**
- * @ignore
- */
- define('Net_OpenID_math_type', 'bcmath');
-
- if (!function_exists('bcpowmod')) {
- // PHP4 does not expose bcpowmod, so we have to implement it here
- /**
- * (base ^ exponent) % modulus
- */
- function bcpowmod($base, $exponent, $modulus) {
- $square = bcmod($base, $modulus);
- $result = '1';
- while( bccomp( $exponent, 0 ) > 0 ) {
- if (bcmod($exponent, 2)) {
- // result = (result * square) % modulus
- $result = bcmod(bcmul($result, $square), $modulus);
- }
- $square = bcmod(bcmul($square, $square), $modulus);
- $exponent = bcdiv($exponent, 2);
- }
- return $result;
+ if ($mod === NULL) {
+ $this->mod = $this->lib->init($this->DEFAULT_MOD);
+ } else {
+ $this->mod = $mod;
}
- }
-
- /**
- * @ignore
- * @package OpenID
- */
- class Net_OpenID_DiffieHellman {
- var $DEFAULT_MOD = '155172898181473697471232257763715539915724801966915404479707795314057629378541917580651227423698188993727816152646631438561595825688188889951272158842675419950341258706556549803580104870537681476726513255747040765857479291291572334510643245094715007229621094194349783925984760375594985848253359305585439638443';
-
- var $DEFAULT_GEN = '2';
- var $mod;
- var $gen;
- var $private;
- var $public;
+ if ($gen === NULL) {
+ $this->gen = $this->lib->init($this->DEFAULT_GEN);
+ } else {
+ $this->gen = $gen;
+ }
- function Net_OpenID_DiffieHellman($mod=NULL, $gen=NULL, $private=NULL) {
- $this->mod = $mod === NULL ? $this->DEFAULT_MOD : $mod;
- $this->gen = $gen === NULL ? $this->DEFAULT_GEN : $gen;
- $this->private =
- $private === NULL ? $this->generateRandom() : $private;
+ $this->private =
+ ($private === NULL) ? $this->generateRandom() : $private;
- $this->public = bcpowmod($this->gen, $this->private, $this->mod);
- }
+ $this->public = $this->lib->powmod($this->gen, $this->private, $this->mod);
+ }
- function generateRandom() {
- // XXX: not cryptographically secure (predictable!!!)
- // XXX: also, way too small (usually)
- // FIXME
- return mt_rand(1, $this->mod);
- }
+ function generateRandom() {
+ return $this->lib->random(1, $this->mod);
+ }
- function getSharedSecret($composite) {
- return bcpowmod($composite, $this->private, $this->mod);
- }
+ function createKeyExchange() {
+ return $this->lib->powmod($this->g, $this->x, $this->p);
+ }
- function getPublicKey() {
- return $this->public;
- }
+ function decryptKeyExchange($keyEx) {
+ return $this->lib->powmod($keyEx, $this->x, $this->p);
+ }
+ function getSharedSecret($composite) {
+ return $this->lib->powmod($composite, $this->private, $this->mod);
}
-} else {
- trigger_error("No usable big int library present (gmp or bcmath). " .
- "Only dumb mode OpenID is available.",
- E_USER_NOTICE);
+ function getPublicKey() {
+ return $this->public;
+ }
}