Merge branch 'ieee' into 3.0

This commit is contained in:
terrafrost 2023-11-01 19:48:32 -05:00
commit 90e3b38a29
2 changed files with 80 additions and 0 deletions

View File

@ -0,0 +1,66 @@
<?php
/**
* IEEE P1363 Signature Handler
*
* PHP version 5
*
* Handles signatures in the format described in
* https://standards.ieee.org/ieee/1363/2049/ and
* https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/sign#ecdsa
*
* @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 phpseclib3\Crypt\EC\Formats\Signature;
use phpseclib3\Math\BigInteger;
/**
* ASN1 Signature Handler
*
* @author Jim Wigginton <terrafrost@php.net>
*/
abstract class IEEE
{
/**
* Loads a signature
*
* @param string $sig
* @return array
*/
public static function load($sig)
{
if (!is_string($sig)) {
return false;
}
$len = strlen($sig);
if ($len & 1) {
return false;
}
$r = new BigInteger(substr($sig, 0, $len >> 1), 256);
$s = new BigInteger(substr($sig, $len >> 1), 256);
return compact('r', 's');
}
/**
* Returns a signature in the appropriate format
*
* @param \phpseclib3\Math\BigInteger $r
* @param \phpseclib3\Math\BigInteger $s
* @return string
*/
public static function save(BigInteger $r, BigInteger $s)
{
$r = $r->toBytes();
$s = $s->toBytes();
$len = max(strlen($r), strlen($s));
return str_pad($r, $len, "\0", STR_PAD_LEFT) . str_pad($s, $len, "\0", STR_PAD_LEFT);
}
}

View File

@ -690,4 +690,18 @@ cN6W+k8UvGf+Y/lDWNbFitQocabsDUvSN0edHH3UKP5QPTz4cOlyIPMrXQ==
$key = PublicKeyLoader::load($key);
$this->assertInstanceOf(PublicKey::class, $key);
}
/**
* @group github1956
*/
public function testIEEESignature()
{
$key = '{"alg":"ES256","crv":"P-256","ext":true,"key_ops":["verify"],"kty":"EC","x":"FKwqyGd4i2NAl8RUXCCBRCAIbcpeGyfyXwgA_AWHb8Y","y":"njxhw5O6zGVkBlcPDKYj0E-6VO1giHTUkJWBhgKNqd8"}';
$key = PublicKeyLoader::load($key)->withSignatureFormat('IEEE')->withHash('sha384');
$signature = 'a4f61518323bac50b4f87a0f766ebb10d1db25358a0a20a98dab20be4e9c3be2d77ff5a8415cfce2967999c73d2a49b2d8c01990f72c04d99ebe3c4ebf75b4e9';
$signature = pack('H*', $signature);
$this->assertTrue($key->verify('hello world!', $signature));
}
}