2018-10-25 03:00:37 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
2019-06-28 02:10:40 +02:00
|
|
|
* Pure-PHP implementation of EC.
|
2018-10-25 03:00:37 +02:00
|
|
|
*
|
|
|
|
* PHP version 5
|
|
|
|
*
|
|
|
|
* Here's an example of how to create signatures and verify signatures with this library:
|
|
|
|
* <code>
|
|
|
|
* <?php
|
|
|
|
* include 'vendor/autoload.php';
|
|
|
|
*
|
2019-06-28 02:10:40 +02:00
|
|
|
* $private = \phpseclib\Crypt\EC::createKey('secp256k1');
|
2019-05-19 22:35:29 +02:00
|
|
|
* $public = $private->getPublicKey();
|
2018-10-25 03:00:37 +02:00
|
|
|
*
|
|
|
|
* $plaintext = 'terrafrost';
|
|
|
|
*
|
2019-05-19 22:35:29 +02:00
|
|
|
* $signature = $private->sign($plaintext);
|
2018-10-25 03:00:37 +02:00
|
|
|
*
|
2019-05-19 22:35:29 +02:00
|
|
|
* echo $public->verify($plaintext, $signature) ? 'verified' : 'unverified';
|
2018-10-25 03:00:37 +02:00
|
|
|
* ?>
|
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
* @category Crypt
|
2019-06-28 02:10:40 +02:00
|
|
|
* @package EC
|
2018-10-25 03:00:37 +02:00
|
|
|
* @author Jim Wigginton <terrafrost@php.net>
|
|
|
|
* @copyright 2016 Jim Wigginton
|
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
|
|
|
* @link http://phpseclib.sourceforge.net
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace phpseclib\Crypt;
|
|
|
|
|
|
|
|
use phpseclib\Crypt\Common\AsymmetricKey;
|
2019-06-28 02:10:40 +02:00
|
|
|
use phpseclib\Crypt\EC\PrivateKey;
|
|
|
|
use phpseclib\Crypt\EC\PublicKey;
|
|
|
|
use phpseclib\Crypt\EC\Parameters;
|
|
|
|
use phpseclib\Crypt\EC\BaseCurves\TwistedEdwards as TwistedEdwardsCurve;
|
|
|
|
use phpseclib\Crypt\EC\Curves\Ed25519;
|
|
|
|
use phpseclib\Crypt\EC\Curves\Ed448;
|
|
|
|
use phpseclib\Crypt\EC\Formats\Keys\PKCS1;
|
2019-05-19 22:35:29 +02:00
|
|
|
use phpseclib\File\ASN1\Maps\ECParameters;
|
|
|
|
use phpseclib\File\ASN1;
|
|
|
|
use phpseclib\Exception\UnsupportedCurveException;
|
|
|
|
use phpseclib\Exception\UnsupportedAlgorithmException;
|
2018-10-25 03:00:37 +02:00
|
|
|
|
|
|
|
/**
|
2019-06-28 02:10:40 +02:00
|
|
|
* Pure-PHP implementation of EC.
|
2018-10-25 03:00:37 +02:00
|
|
|
*
|
2019-06-28 02:10:40 +02:00
|
|
|
* @package EC
|
2018-10-25 03:00:37 +02:00
|
|
|
* @author Jim Wigginton <terrafrost@php.net>
|
|
|
|
* @access public
|
|
|
|
*/
|
2019-06-28 02:10:40 +02:00
|
|
|
abstract class EC extends AsymmetricKey
|
2018-10-25 03:00:37 +02:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Algorithm Name
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
* @access private
|
|
|
|
*/
|
2019-06-28 02:10:40 +02:00
|
|
|
const ALGORITHM = 'EC';
|
2018-10-25 03:00:37 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Public Key QA
|
|
|
|
*
|
|
|
|
* @var object[]
|
|
|
|
*/
|
2019-05-19 22:35:29 +02:00
|
|
|
protected $QA;
|
2018-10-25 03:00:37 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Curve
|
|
|
|
*
|
2019-06-28 02:10:40 +02:00
|
|
|
* @var \phpseclib\Crypt\EC\BaseCurves\Base
|
2018-10-25 03:00:37 +02:00
|
|
|
*/
|
2019-05-19 22:35:29 +02:00
|
|
|
protected $curve;
|
2018-10-25 03:00:37 +02:00
|
|
|
|
|
|
|
/**
|
2019-05-19 22:35:29 +02:00
|
|
|
* Signature Format
|
2018-10-25 03:00:37 +02:00
|
|
|
*
|
|
|
|
* @var string
|
2019-05-19 22:35:29 +02:00
|
|
|
* @access private
|
2018-10-25 03:00:37 +02:00
|
|
|
*/
|
2019-05-19 22:35:29 +02:00
|
|
|
protected $format;
|
2018-10-25 03:00:37 +02:00
|
|
|
|
|
|
|
/**
|
2019-05-19 22:35:29 +02:00
|
|
|
* Signature Format (Short)
|
2018-10-25 03:00:37 +02:00
|
|
|
*
|
2019-05-19 22:35:29 +02:00
|
|
|
* @var string
|
|
|
|
* @access private
|
2018-10-25 03:00:37 +02:00
|
|
|
*/
|
2019-05-19 22:35:29 +02:00
|
|
|
protected $shortFormat;
|
2018-10-25 03:00:37 +02:00
|
|
|
|
|
|
|
/**
|
2019-05-19 22:35:29 +02:00
|
|
|
* Curve Name
|
2018-10-25 03:00:37 +02:00
|
|
|
*
|
2019-05-19 22:35:29 +02:00
|
|
|
* @var string
|
2018-10-25 03:00:37 +02:00
|
|
|
*/
|
2019-05-19 22:35:29 +02:00
|
|
|
private $curveName;
|
2018-10-25 03:00:37 +02:00
|
|
|
|
2019-05-21 07:03:21 +02:00
|
|
|
/**
|
|
|
|
* Curve Order
|
|
|
|
*
|
|
|
|
* Used for deterministic ECDSA
|
|
|
|
*
|
|
|
|
* @var \phpseclib\Math\BigInteger
|
|
|
|
*/
|
|
|
|
protected $q;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Alias for the private key
|
|
|
|
*
|
|
|
|
* Used for deterministic ECDSA. AsymmetricKey expects $x. I don't like x because
|
|
|
|
* with x you have x * the base point yielding an (x, y)-coordinate that is the
|
|
|
|
* public key. But the x is different depending on which side of the equal sign
|
|
|
|
* you're on. It's less ambiguous if you do dA * base point = (x, y)-coordinate.
|
|
|
|
*
|
|
|
|
* @var \phpseclib\Math\BigInteger
|
|
|
|
*/
|
|
|
|
protected $x;
|
|
|
|
|
2018-10-25 03:00:37 +02:00
|
|
|
/**
|
2019-05-19 22:35:29 +02:00
|
|
|
* Context
|
2018-10-25 03:00:37 +02:00
|
|
|
*
|
2019-05-19 22:35:29 +02:00
|
|
|
* @var string
|
2018-10-25 03:00:37 +02:00
|
|
|
*/
|
2019-05-19 22:35:29 +02:00
|
|
|
protected $context;
|
2018-10-25 03:00:37 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create public / private key pair.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param string $curve
|
2019-06-28 02:10:40 +02:00
|
|
|
* @return \phpseclib\Crypt\EC\PrivateKey
|
2018-10-25 03:00:37 +02:00
|
|
|
*/
|
|
|
|
public static function createKey($curve)
|
|
|
|
{
|
|
|
|
self::initialize_static_variables();
|
|
|
|
|
|
|
|
if (!isset(self::$engines['PHP'])) {
|
|
|
|
self::useBestEngine();
|
|
|
|
}
|
|
|
|
|
2019-05-19 22:35:29 +02:00
|
|
|
$curve = strtolower($curve);
|
|
|
|
if (self::$engines['libsodium'] && $curve == 'ed25519' && function_exists('sodium_crypto_sign_keypair')) {
|
2018-10-25 03:00:37 +02:00
|
|
|
$kp = sodium_crypto_sign_keypair();
|
|
|
|
|
2019-06-28 02:10:40 +02:00
|
|
|
$privatekey = EC::load(sodium_crypto_sign_secretkey($kp), 'libsodium');
|
|
|
|
//$publickey = EC::load(sodium_crypto_sign_publickey($kp), 'libsodium');
|
2018-10-25 03:00:37 +02:00
|
|
|
|
2019-05-19 22:35:29 +02:00
|
|
|
$privatekey->curveName = 'Ed25519';
|
|
|
|
//$publickey->curveName = $curve;
|
2018-10-25 03:00:37 +02:00
|
|
|
|
2019-05-19 22:35:29 +02:00
|
|
|
return $privatekey;
|
2018-10-25 03:00:37 +02:00
|
|
|
}
|
|
|
|
|
2019-05-19 22:35:29 +02:00
|
|
|
$privatekey = new PrivateKey;
|
2018-10-25 03:00:37 +02:00
|
|
|
|
|
|
|
$curveName = $curve;
|
2019-06-28 02:10:40 +02:00
|
|
|
$curve = '\phpseclib\Crypt\EC\Curves\\' . $curve;
|
2018-10-25 03:00:37 +02:00
|
|
|
if (!class_exists($curve)) {
|
2019-05-19 22:35:29 +02:00
|
|
|
throw new UnsupportedCurveException('Named Curve of ' . $curveName . ' is not supported');
|
2018-10-25 03:00:37 +02:00
|
|
|
}
|
2019-05-19 22:35:29 +02:00
|
|
|
|
|
|
|
$reflect = new \ReflectionClass($curve);
|
|
|
|
$curveName = $reflect->isFinal() ?
|
|
|
|
$reflect->getParentClass()->getShortName() :
|
|
|
|
$reflect->getShortName();
|
|
|
|
|
2018-10-25 03:00:37 +02:00
|
|
|
$curve = new $curve();
|
|
|
|
$privatekey->dA = $dA = $curve->createRandomMultiplier();
|
|
|
|
$privatekey->QA = $curve->multiplyPoint($curve->getBasePoint(), $dA);
|
|
|
|
$privatekey->curve = $curve;
|
|
|
|
|
2019-05-19 22:35:29 +02:00
|
|
|
//$publickey = clone $privatekey;
|
|
|
|
//unset($publickey->dA);
|
|
|
|
//unset($publickey->x);
|
2018-10-25 03:00:37 +02:00
|
|
|
|
2019-05-19 22:35:29 +02:00
|
|
|
$privatekey->curveName = $curveName;
|
|
|
|
//$publickey->curveName = $curveName;
|
|
|
|
|
|
|
|
if ($privatekey->curve instanceof TwistedEdwardsCurve) {
|
|
|
|
return $privatekey->withHash($curve::HASH);
|
|
|
|
}
|
2018-10-25 03:00:37 +02:00
|
|
|
|
2019-05-19 22:35:29 +02:00
|
|
|
return $privatekey;
|
2018-10-25 03:00:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-06-28 12:32:38 +02:00
|
|
|
* OnLoad Handler
|
2018-10-25 03:00:37 +02:00
|
|
|
*
|
2019-05-19 22:35:29 +02:00
|
|
|
* @return bool
|
2019-06-28 12:32:38 +02:00
|
|
|
* @access protected
|
|
|
|
* @param array $components
|
2018-10-25 03:00:37 +02:00
|
|
|
*/
|
2019-06-28 12:32:38 +02:00
|
|
|
protected static function onLoad($components)
|
2018-10-25 03:00:37 +02:00
|
|
|
{
|
|
|
|
if (!isset(self::$engines['PHP'])) {
|
|
|
|
self::useBestEngine();
|
|
|
|
}
|
|
|
|
|
2019-05-19 22:35:29 +02:00
|
|
|
if (!isset($components['dA']) && !isset($components['QA'])) {
|
|
|
|
$new = new Parameters;
|
|
|
|
$new->curve = $components['curve'];
|
|
|
|
return $new;
|
2018-10-25 03:00:37 +02:00
|
|
|
}
|
|
|
|
|
2019-05-19 22:35:29 +02:00
|
|
|
$new = isset($components['dA']) ?
|
|
|
|
new PrivateKey :
|
|
|
|
new PublicKey;
|
|
|
|
$new->curve = $components['curve'];
|
|
|
|
$new->QA = $components['QA'];
|
2018-10-25 03:00:37 +02:00
|
|
|
|
2019-05-19 22:35:29 +02:00
|
|
|
if (isset($components['dA'])) {
|
|
|
|
$new->dA = $components['dA'];
|
2018-11-29 17:05:28 +01:00
|
|
|
}
|
|
|
|
|
2019-05-19 22:35:29 +02:00
|
|
|
if ($new->curve instanceof TwistedEdwardsCurve) {
|
|
|
|
return $new->withHash($components['curve']::HASH);
|
|
|
|
}
|
2018-10-25 03:00:37 +02:00
|
|
|
|
2019-05-19 22:35:29 +02:00
|
|
|
return $new;
|
2018-10-25 03:00:37 +02:00
|
|
|
}
|
|
|
|
|
2018-11-29 17:05:28 +01:00
|
|
|
/**
|
2019-05-19 22:35:29 +02:00
|
|
|
* Constructor
|
2018-11-29 17:05:28 +01:00
|
|
|
*
|
2019-05-19 22:35:29 +02:00
|
|
|
* PublicKey and PrivateKey objects can only be created from abstract RSA class
|
2018-11-29 17:05:28 +01:00
|
|
|
*/
|
2019-05-19 22:35:29 +02:00
|
|
|
protected function __construct()
|
2018-11-29 17:05:28 +01:00
|
|
|
{
|
2019-05-19 22:35:29 +02:00
|
|
|
$this->format = self::validatePlugin('Signature', 'ASN1');
|
|
|
|
$this->shortFormat = 'ASN1';
|
|
|
|
|
|
|
|
parent::__construct();
|
2018-11-29 17:05:28 +01:00
|
|
|
}
|
|
|
|
|
2018-10-25 03:00:37 +02:00
|
|
|
/**
|
|
|
|
* Returns the curve
|
|
|
|
*
|
|
|
|
* Returns a string if it's a named curve, an array if not
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return string|array
|
|
|
|
*/
|
|
|
|
public function getCurve()
|
|
|
|
{
|
|
|
|
if ($this->curveName) {
|
|
|
|
return $this->curveName;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->curve instanceof TwistedEdwardsCurve) {
|
|
|
|
$this->curveName = $this->curve instanceof Ed25519 ? 'Ed25519' : 'Ed448';
|
|
|
|
return $this->curveName;
|
|
|
|
}
|
|
|
|
|
2019-06-03 15:16:13 +02:00
|
|
|
$params = $this->getParameters()->toString('PKCS8', ['namedCurve' => true]);
|
2018-10-25 03:00:37 +02:00
|
|
|
$decoded = ASN1::extractBER($params);
|
|
|
|
$decoded = ASN1::decodeBER($decoded);
|
|
|
|
$decoded = ASN1::asn1map($decoded[0], ECParameters::MAP);
|
|
|
|
if (isset($decoded['namedCurve'])) {
|
|
|
|
$this->curveName = $decoded['namedCurve'];
|
|
|
|
return $decoded['namedCurve'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$namedCurves) {
|
|
|
|
PKCS1::useSpecifiedCurve();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $decoded;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the key size
|
|
|
|
*
|
|
|
|
* Quoting https://tools.ietf.org/html/rfc5656#section-2,
|
|
|
|
*
|
|
|
|
* "The size of a set of elliptic curve domain parameters on a prime
|
|
|
|
* curve is defined as the number of bits in the binary representation
|
|
|
|
* of the field order, commonly denoted by p. Size on a
|
|
|
|
* characteristic-2 curve is defined as the number of bits in the binary
|
|
|
|
* representation of the field, commonly denoted by m. A set of
|
|
|
|
* elliptic curve domain parameters defines a group of order n generated
|
|
|
|
* by a base point P"
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getLength()
|
|
|
|
{
|
|
|
|
return $this->curve->getLength();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-05-19 22:35:29 +02:00
|
|
|
* Returns the current engine being used
|
2018-10-25 03:00:37 +02:00
|
|
|
*
|
2019-05-19 22:35:29 +02:00
|
|
|
* @see self::useInternalEngine()
|
|
|
|
* @see self::useBestEngine()
|
2018-10-25 03:00:37 +02:00
|
|
|
* @access public
|
2019-05-19 22:35:29 +02:00
|
|
|
* @return string
|
2018-10-25 03:00:37 +02:00
|
|
|
*/
|
2019-05-19 22:35:29 +02:00
|
|
|
public function getEngine()
|
2018-10-25 03:00:37 +02:00
|
|
|
{
|
2019-05-19 22:35:29 +02:00
|
|
|
if ($this->curve instanceof TwistedEdwardsCurve) {
|
|
|
|
return $this->curve instanceof Ed25519 && self::$engines['libsodium'] && !isset($this->context) ?
|
|
|
|
'libsodium' : 'PHP';
|
2018-10-25 03:00:37 +02:00
|
|
|
}
|
|
|
|
|
2019-05-19 22:35:29 +02:00
|
|
|
return self::$engines['OpenSSL'] && in_array($this->hash->getHash(), openssl_get_md_methods()) ?
|
|
|
|
'OpenSSL' : 'PHP';
|
2018-10-25 03:00:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the parameters
|
|
|
|
*
|
|
|
|
* @see self::getPublicKey()
|
|
|
|
* @access public
|
|
|
|
* @param string $type optional
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function getParameters($type = 'PKCS1')
|
|
|
|
{
|
|
|
|
$type = self::validatePlugin('Keys', $type, 'saveParameters');
|
|
|
|
|
2019-05-19 22:35:29 +02:00
|
|
|
$key = $type::saveParameters($this->curve);
|
2018-10-25 03:00:37 +02:00
|
|
|
|
2019-06-28 02:10:40 +02:00
|
|
|
return EC::load($key, 'PKCS1')
|
2019-05-19 22:35:29 +02:00
|
|
|
->withHash($this->hash->getHash())
|
|
|
|
->withSignatureFormat($this->shortFormat);
|
2018-10-25 03:00:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-05-19 22:35:29 +02:00
|
|
|
* Determines the signature padding mode
|
|
|
|
*
|
|
|
|
* Valid values are: ASN1, SSH2, Raw
|
2018-10-25 03:00:37 +02:00
|
|
|
*
|
|
|
|
* @access public
|
2019-05-19 22:35:29 +02:00
|
|
|
* @param string $padding
|
2018-10-25 03:00:37 +02:00
|
|
|
*/
|
2019-05-19 22:35:29 +02:00
|
|
|
public function withSignatureFormat($format)
|
2018-10-25 03:00:37 +02:00
|
|
|
{
|
2019-05-19 22:35:29 +02:00
|
|
|
$new = clone $this;
|
|
|
|
$new->shortFormat = $format;
|
|
|
|
$new->format = self::validatePlugin('Signature', $format);
|
|
|
|
return $new;
|
2018-10-25 03:00:37 +02:00
|
|
|
}
|
|
|
|
|
2019-06-24 04:51:38 +02:00
|
|
|
/**
|
|
|
|
* Returns the signature format currently being used
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
public function getSignatureFormat()
|
|
|
|
{
|
|
|
|
return $this->shortFormat;
|
|
|
|
}
|
|
|
|
|
2018-10-25 03:00:37 +02:00
|
|
|
/**
|
|
|
|
* Sets the context
|
|
|
|
*
|
|
|
|
* Used by Ed25519 / Ed448.
|
|
|
|
*
|
|
|
|
* @see self::sign()
|
|
|
|
* @see self::verify()
|
|
|
|
* @access public
|
|
|
|
* @param string $context optional
|
|
|
|
*/
|
2019-05-19 22:35:29 +02:00
|
|
|
public function withContext($context = null)
|
2018-10-25 03:00:37 +02:00
|
|
|
{
|
2019-05-19 22:35:29 +02:00
|
|
|
if (!$this->curve instanceof TwistedEdwardsCurve) {
|
|
|
|
throw new UnsupportedCurveException('Only Ed25519 and Ed448 support contexts');
|
|
|
|
}
|
|
|
|
|
|
|
|
$new = clone $this;
|
2018-10-25 03:00:37 +02:00
|
|
|
if (!isset($context)) {
|
2019-05-19 22:35:29 +02:00
|
|
|
$new->context = null;
|
|
|
|
return $new;
|
2018-10-25 03:00:37 +02:00
|
|
|
}
|
|
|
|
if (!is_string($context)) {
|
2018-12-31 16:06:12 +01:00
|
|
|
throw new \InvalidArgumentException('setContext expects a string');
|
2018-10-25 03:00:37 +02:00
|
|
|
}
|
|
|
|
if (strlen($context) > 255) {
|
2018-12-31 16:06:12 +01:00
|
|
|
throw new \LengthException('The context is supposed to be, at most, 255 bytes long');
|
2018-10-25 03:00:37 +02:00
|
|
|
}
|
2019-05-19 22:35:29 +02:00
|
|
|
$new->context = $context;
|
|
|
|
return $new;
|
2018-10-25 03:00:37 +02:00
|
|
|
}
|
|
|
|
|
2019-06-24 04:51:38 +02:00
|
|
|
/**
|
|
|
|
* Returns the signature format currently being used
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
public function getContext()
|
|
|
|
{
|
|
|
|
return $this->context;
|
|
|
|
}
|
|
|
|
|
2018-11-29 17:05:28 +01:00
|
|
|
/**
|
|
|
|
* Determines which hashing function should be used
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param string $hash
|
|
|
|
*/
|
2019-05-19 22:35:29 +02:00
|
|
|
public function withHash($hash)
|
2018-11-29 17:05:28 +01:00
|
|
|
{
|
2019-03-30 19:35:16 +01:00
|
|
|
if ($this->curve instanceof Ed25519 && $hash != 'sha512') {
|
2018-12-31 16:06:12 +01:00
|
|
|
throw new UnsupportedAlgorithmException('Ed25519 only supports sha512 as a hash');
|
2018-11-29 17:05:28 +01:00
|
|
|
}
|
2019-03-30 19:35:16 +01:00
|
|
|
if ($this->curve instanceof Ed448 && $hash != 'shake256-912') {
|
2018-12-31 16:06:12 +01:00
|
|
|
throw new UnsupportedAlgorithmException('Ed448 only supports shake256 with a length of 114 bytes');
|
2018-11-29 17:05:28 +01:00
|
|
|
}
|
|
|
|
|
2019-05-19 22:35:29 +02:00
|
|
|
return parent::withHash($hash);
|
2018-10-25 03:00:37 +02:00
|
|
|
}
|
|
|
|
}
|