diff --git a/composer.json b/composer.json index 65f73e2..e095eac 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,6 @@ "php": ">=8.1", "ext-json": "*", "ext-openssl": "*", - "ext-mbstring": "*", "brick/math": "^0.9|^0.10|^0.11|^0.12", "spomky-labs/pki-framework": "^1.0" }, @@ -29,18 +28,18 @@ } }, "require-dev": { - "infection/infection": "^0.27", + "infection/infection": "^0.29", "phpstan/phpstan": "^1.7", "phpstan/phpstan-deprecation-rules": "^1.0", "phpstan/phpstan-phpunit": "^1.1", "phpstan/phpstan-strict-rules": "^1.2", - "phpunit/phpunit": "^10.1", - "rector/rector": "^0.19", + "phpunit/phpunit": "^10.1|^11.0", + "rector/rector": "^1.0", "symplify/easy-coding-standard": "^12.0", "symfony/phpunit-bridge": "^6.4|^7.0", "ekino/phpstan-banned-code": "^1.0", "php-parallel-lint/php-parallel-lint": "^1.3", - "qossmic/deptrac-shim": "^1.0", + "qossmic/deptrac": "^2.0", "phpstan/extension-installer": "^1.3" }, "autoload-dev": { diff --git a/deptrac.yaml b/deptrac.yaml index 9296f05..6917e63 100644 --- a/deptrac.yaml +++ b/deptrac.yaml @@ -4,13 +4,13 @@ parameters: layers: - name: 'CoseLib' collectors: - - type: 'className' - regex: '^Cose\\' + - type: 'classLike' + value: '^Cose\\' - name: 'Vendors' collectors: - - { type: className, regex: '^CBOR\\' } - - { type: className, regex: '^Brick\\' } - - { type: className, regex: '^SpomkyLabs\\Pki\\' } + - { type: 'classLike', value: '^CBOR\\' } + - { type: 'classLike', value: '^Brick\\' } + - { type: 'classLike', value: '^SpomkyLabs\\Pki\\' } ruleset: CoseLib: - Vendors diff --git a/ecs.php b/ecs.php index ab073a7..929bca3 100644 --- a/ecs.php +++ b/ecs.php @@ -2,7 +2,6 @@ declare(strict_types=1); -use PhpCsFixer\Fixer\Alias\MbStrFunctionsFixer; use PhpCsFixer\Fixer\ArrayNotation\ArraySyntaxFixer; use PhpCsFixer\Fixer\ClassNotation\ProtectedToPrivateFixer; use PhpCsFixer\Fixer\Comment\HeaderCommentFixer; @@ -25,7 +24,6 @@ use PhpCsFixer\Fixer\Strict\StrictComparisonFixer; use PhpCsFixer\Fixer\Strict\StrictParamFixer; use PhpCsFixer\Fixer\Whitespace\ArrayIndentationFixer; -use PhpCsFixer\Fixer\Whitespace\CompactNullableTypehintFixer; use PhpCsFixer\Fixer\Whitespace\MethodChainingIndentationFixer; use Symplify\EasyCodingStandard\Config\ECSConfig; use Symplify\EasyCodingStandard\ValueObject\Set\SetList; @@ -53,11 +51,9 @@ $config->rule(ProtectedToPrivateFixer::class); $config->rule(DeclareStrictTypesFixer::class); $config->rule(NativeConstantInvocationFixer::class); - $config->rule(MbStrFunctionsFixer::class); $config->rule(LinebreakAfterOpeningTagFixer::class); $config->rule(CombineConsecutiveIssetsFixer::class); $config->rule(CombineConsecutiveUnsetsFixer::class); - $config->rule(CompactNullableTypehintFixer::class); $config->rule(NoSuperfluousElseifFixer::class); $config->rule(NoSuperfluousPhpdocTagsFixer::class); $config->rule(PhpdocTrimConsecutiveBlankLineSeparationFixer::class); diff --git a/src/Algorithm/Mac/Hmac.php b/src/Algorithm/Mac/Hmac.php index 7ad8080..54ef460 100644 --- a/src/Algorithm/Mac/Hmac.php +++ b/src/Algorithm/Mac/Hmac.php @@ -18,7 +18,7 @@ public function hash(string $data, Key $key): string $this->checKey($key); $signature = hash_hmac($this->getHashAlgorithm(), $data, (string) $key->get(SymmetricKey::DATA_K), true); - return mb_substr($signature, 0, intdiv($this->getSignatureLength(), 8), '8bit'); + return substr($signature, 0, intdiv($this->getSignatureLength(), 8)); } public function verify(string $data, Key $key, string $signature): bool diff --git a/src/Algorithm/Signature/ECDSA/ECSignature.php b/src/Algorithm/Signature/ECDSA/ECSignature.php index 4354700..5995131 100644 --- a/src/Algorithm/Signature/ECDSA/ECSignature.php +++ b/src/Algorithm/Signature/ECDSA/ECSignature.php @@ -9,9 +9,9 @@ use function dechex; use function hex2bin; use function hexdec; -use function mb_strlen; -use function mb_substr; use function str_pad; +use function strlen; +use function substr; use const STR_PAD_LEFT; /** @@ -41,8 +41,8 @@ public static function toAsn1(string $signature, int $length): string throw new InvalidArgumentException('Invalid signature length.'); } - $pointR = self::preparePositiveInteger(mb_substr($signature, 0, $length, '8bit')); - $pointS = self::preparePositiveInteger(mb_substr($signature, $length, null, '8bit')); + $pointR = self::preparePositiveInteger(substr($signature, 0, $length)); + $pointS = self::preparePositiveInteger(substr($signature, $length, null)); $lengthR = self::octetLength($pointR); $lengthS = self::octetLength($pointS); @@ -80,20 +80,20 @@ public static function fromAsn1(string $signature, int $length): string private static function octetLength(string $data): int { - return intdiv(mb_strlen($data, '8bit'), self::BYTE_SIZE); + return intdiv(strlen($data), self::BYTE_SIZE); } private static function preparePositiveInteger(string $data): string { - if (mb_substr($data, 0, self::BYTE_SIZE, '8bit') > self::ASN1_BIG_INTEGER_LIMIT) { + if (substr($data, 0, self::BYTE_SIZE) > self::ASN1_BIG_INTEGER_LIMIT) { return self::ASN1_NEGATIVE_INTEGER . $data; } while ( - mb_strpos($data, self::ASN1_NEGATIVE_INTEGER, 0, '8bit') === 0 - && mb_substr($data, 2, self::BYTE_SIZE, '8bit') <= self::ASN1_BIG_INTEGER_LIMIT + str_starts_with($data, self::ASN1_NEGATIVE_INTEGER) + && substr($data, 2, self::BYTE_SIZE) <= self::ASN1_BIG_INTEGER_LIMIT ) { - $data = mb_substr($data, 2, null, '8bit'); + $data = substr($data, 2, null); } return $data; @@ -101,7 +101,7 @@ private static function preparePositiveInteger(string $data): string private static function readAsn1Content(string $message, int &$position, int $length): string { - $content = mb_substr($message, $position, $length, '8bit'); + $content = substr($message, $position, $length); $position += $length; return $content; @@ -121,10 +121,10 @@ private static function readAsn1Integer(string $message, int &$position): string private static function retrievePositiveInteger(string $data): string { while ( - mb_strpos($data, self::ASN1_NEGATIVE_INTEGER, 0, '8bit') === 0 - && mb_substr($data, 2, self::BYTE_SIZE, '8bit') > self::ASN1_BIG_INTEGER_LIMIT + str_starts_with($data, self::ASN1_NEGATIVE_INTEGER) + && substr($data, 2, self::BYTE_SIZE) > self::ASN1_BIG_INTEGER_LIMIT ) { - $data = mb_substr($data, 2, null, '8bit'); + $data = substr($data, 2, null); } return $data; diff --git a/src/Algorithm/Signature/RSA/PSSRSA.php b/src/Algorithm/Signature/RSA/PSSRSA.php index e2fa319..d383dce 100644 --- a/src/Algorithm/Signature/RSA/PSSRSA.php +++ b/src/Algorithm/Signature/RSA/PSSRSA.php @@ -14,13 +14,12 @@ use function ceil; use function chr; use function hash_equals; -use function mb_strlen; -use function mb_substr; use function ord; use function pack; use function random_bytes; use function str_pad; use function str_repeat; +use function strlen; use const STR_PAD_LEFT; /** @@ -31,7 +30,7 @@ abstract class PSSRSA implements Signature public function sign(string $data, Key $key): string { $key = $this->handleKey($key); - $modulusLength = mb_strlen($key->n(), '8bit'); + $modulusLength = strlen($key->n()); $em = $this->encodeEMSAPSS($data, 8 * $modulusLength - 1, $this->getHashAlgorithm()); $message = BigInteger::createFromBinaryString($em); @@ -43,8 +42,8 @@ public function sign(string $data, Key $key): string public function verify(string $data, Key $key, string $signature): bool { $key = $this->handleKey($key); - $modulusLength = mb_strlen($key->n(), '8bit'); - if (mb_strlen($signature, '8bit') !== $modulusLength) { + $modulusLength = strlen($key->n()); + if (strlen($signature) !== $modulusLength) { throw new InvalidArgumentException('Invalid modulus length'); } $s2 = BigInteger::createFromBinaryString($signature); @@ -99,7 +98,7 @@ private function handleKey(Key $key): RsaKey private function convertIntegerToOctetString(BigInteger $x, int $xLen): string { $xB = $x->toBytes(); - if (mb_strlen($xB, '8bit') > $xLen) { + if (strlen($xB) > $xLen) { throw new RuntimeException('Unable to convert the integer'); } @@ -118,7 +117,7 @@ private function getMGF1(string $mgfSeed, int $maskLen, Hash $mgfHash): string $t .= $mgfHash->hash($mgfSeed . $c); } - return mb_substr($t, 0, $maskLen, '8bit'); + return substr($t, 0, $maskLen); } /** @@ -155,11 +154,11 @@ private function verifyEMSAPSS(string $m, string $em, int $emBits, Hash $hash): if ($emLen < $hash->getLength() + $sLen + 2) { throw new InvalidArgumentException(); } - if ($em[mb_strlen($em, '8bit') - 1] !== chr(0xBC)) { + if ($em[strlen($em) - 1] !== chr(0xBC)) { throw new InvalidArgumentException(); } - $maskedDB = mb_substr($em, 0, -$hash->getLength() - 1, '8bit'); - $h = mb_substr($em, -$hash->getLength() - 1, $hash->getLength(), '8bit'); + $maskedDB = substr($em, 0, -$hash->getLength() - 1); + $h = substr($em, -$hash->getLength() - 1, $hash->getLength()); $temp = chr(0xFF << ($emBits & 7)); if ((~$maskedDB[0] & $temp) !== $temp) { throw new InvalidArgumentException(); @@ -168,13 +167,13 @@ private function verifyEMSAPSS(string $m, string $em, int $emBits, Hash $hash): $db = $maskedDB ^ $dbMask; $db[0] = ~chr(0xFF << ($emBits & 7)) & $db[0]; $temp = $emLen - $hash->getLength() - $sLen - 2; - if (mb_strpos($db, str_repeat(chr(0), $temp), 0, '8bit') !== 0) { + if (! str_starts_with($db, str_repeat(chr(0), $temp))) { throw new InvalidArgumentException(); } if (ord($db[$temp]) !== 1) { throw new InvalidArgumentException(); } - $salt = mb_substr($db, $temp + 1, null, '8bit'); // should be $sLen long + $salt = substr($db, $temp + 1, null); // should be $sLen long $m2 = "\0\0\0\0\0\0\0\0" . $mHash . $salt; $h2 = $hash->hash($m2); diff --git a/src/BigInteger.php b/src/BigInteger.php index e0ef082..d45fd39 100644 --- a/src/BigInteger.php +++ b/src/BigInteger.php @@ -7,6 +7,7 @@ use Brick\Math\BigInteger as BrickBigInteger; use function chr; use function hex2bin; +use function strlen; use function unpack; /** @@ -42,7 +43,7 @@ public function toBytes(): string } $temp = $this->value->toBase(16); - $temp = 0 !== (mb_strlen($temp, '8bit') & 1) ? '0' . $temp : $temp; + $temp = 0 !== (strlen($temp) & 1) ? '0' . $temp : $temp; $temp = hex2bin($temp); return ltrim($temp, chr(0)); diff --git a/src/Key/Ec2Key.php b/src/Key/Ec2Key.php index e0fa7e1..ad770be 100644 --- a/src/Key/Ec2Key.php +++ b/src/Key/Ec2Key.php @@ -14,6 +14,7 @@ use function array_key_exists; use function in_array; use function is_int; +use function strlen; /** * @final @@ -93,10 +94,10 @@ public function __construct(array $data) if (! isset($data[self::DATA_CURVE], $data[self::DATA_X], $data[self::DATA_Y])) { throw new InvalidArgumentException('Invalid EC2 key. The curve or the "x/y" coordinates are missing'); } - if (mb_strlen((string) $data[self::DATA_X], '8bit') !== self::CURVE_KEY_LENGTH[$data[self::DATA_CURVE]]) { + if (strlen((string) $data[self::DATA_X]) !== self::CURVE_KEY_LENGTH[$data[self::DATA_CURVE]]) { throw new InvalidArgumentException('Invalid length for x coordinate'); } - if (mb_strlen((string) $data[self::DATA_Y], '8bit') !== self::CURVE_KEY_LENGTH[$data[self::DATA_CURVE]]) { + if (strlen((string) $data[self::DATA_Y]) !== self::CURVE_KEY_LENGTH[$data[self::DATA_CURVE]]) { throw new InvalidArgumentException('Invalid length for y coordinate'); } if (is_int($data[self::DATA_CURVE])) { @@ -188,8 +189,8 @@ private function getCurveOid(): string private function pem(string $type, string $der): string { - return sprintf("-----BEGIN %s-----\n", mb_strtoupper($type)) . + return sprintf("-----BEGIN %s-----\n", strtoupper($type)) . chunk_split(base64_encode($der), 64, "\n") . - sprintf("-----END %s-----\n", mb_strtoupper($type)); + sprintf("-----END %s-----\n", strtoupper($type)); } } diff --git a/tests/Algorithm/Signature/ECDSA/ECDSATest.php b/tests/Algorithm/Signature/ECDSA/ECDSATest.php index 7d3834b..99a820e 100644 --- a/tests/Algorithm/Signature/ECDSA/ECDSATest.php +++ b/tests/Algorithm/Signature/ECDSA/ECDSATest.php @@ -36,7 +36,8 @@ public function aSignatureCanBeComputedAndVerified( string $d, string $x, string $y, - string $data + string $data, + string $signature, ): void { // Given $key = Ec2Key::create([ @@ -48,11 +49,13 @@ public function aSignatureCanBeComputedAndVerified( ]); // When - $hash = $algorithm->sign($data, $key); - $isValid = $algorithm->verify($data, $key, $hash); + $computedSignature = $algorithm->sign($data, $key); + $computedSignatureIsValid = $algorithm->verify($data, $key, $computedSignature); + $signatureIsValid = $algorithm->verify($data, $key, $signature); // Then - static::assertTrue($isValid); + static::assertTrue($computedSignatureIsValid); + static::assertTrue($signatureIsValid); } #[Test] @@ -134,42 +137,42 @@ public function theKeyCurveTypeIsNotValid(): void public static function getVectors(): iterable { yield [ - 'alg' => ES256::create(), - 'crv' => Ec2Key::CURVE_P256, + 'algorithm' => ES256::create(), + 'curve' => Ec2Key::CURVE_P256, 'd' => hex2bin('C9AFA9D845BA75166B5C215767B1D6934E50C3DB36E89B127B8A622B120F6721'), 'x' => hex2bin('60FED4BA255A9D31C961EB74C6356D68C049B8923B61FA6CE669622E60F29FB6'), 'y' => hex2bin('7903FE1008B8BC99A41AE9E95628BC64F2F1B20C2D7E9F5177A3C294D4462299'), 'data' => 'sample', - 'sig' => hex2bin( + 'signature' => hex2bin( 'EFD48B2AACB6A8FD1140DD9CD45E81D69D2C877B56AAF991C34D0EA84EAF3716F7CB1C942D657C41D436C7A1B6E29F65F3E900DBB9AFF4064DC4AB2F843ACDA8' ), ]; yield [ - 'alg' => ES256::create(), - 'crv' => Ec2Key::CURVE_P256, + 'algorithm' => ES256::create(), + 'curve' => Ec2Key::CURVE_P256, 'd' => hex2bin('C9AFA9D845BA75166B5C215767B1D6934E50C3DB36E89B127B8A622B120F6721'), 'x' => hex2bin('60FED4BA255A9D31C961EB74C6356D68C049B8923B61FA6CE669622E60F29FB6'), 'y' => hex2bin('7903FE1008B8BC99A41AE9E95628BC64F2F1B20C2D7E9F5177A3C294D4462299'), 'data' => 'test', - 'sig' => hex2bin( + 'signature' => hex2bin( 'F1ABB023518351CD71D881567B1EA663ED3EFCF6C5132B354F28D3B0B7D38367019F4113742A2B14BD25926B49C649155F267E60D3814B4C0CC84250E46F0083' ), ]; yield [ /** @see https://crypto.stackexchange.com/questions/41316/complete-set-of-test-vectors-for-ecdsa-secp256k1 */ - 'alg' => ES256K::create(), - 'crv' => Ec2Key::CURVE_P256K, + 'algorithm' => ES256K::create(), + 'curve' => Ec2Key::CURVE_P256K, 'd' => hex2bin('ebb2c082fd7727890a28ac82f6bdf97bad8de9f5d7c9028692de1a255cad3e0f'), 'x' => hex2bin('779dd197a5df977ed2cf6cb31d82d43328b790dc6b3b7d4437a427bd5847dfcd'), 'y' => hex2bin('e94b724a555b6d017bb7607c3e3281daf5b1699d6ef4124975c9237b917d426f'), 'data' => 'Maarten Bodewes generated this test vector on 2016-11-08', - 'sig' => hex2bin( + 'signature' => hex2bin( '241097efbf8b63bf145c8961dbdf10c310efbb3b2676bbc0f8b08505c9e2f795021006b7838609339e8b415a7f9acb1b661828131aef1ecbc7955dfb01f3ca0e' ), ]; yield [ - 'alg' => ES384::create(), - 'crv' => Ec2Key::CURVE_P384, + 'algorithm' => ES384::create(), + 'curve' => Ec2Key::CURVE_P384, 'd' => hex2bin( '6B9D3DAD2E1B8C1C05B19875B6659F4DE23C3B667BF297BA9AA47740787137D896D5724E4C70A825F872C9EA60D2EDF5' ), @@ -180,13 +183,13 @@ public static function getVectors(): iterable '8015D9B72D7D57244EA8EF9AC0C621896708A59367F9DFB9F54CA84B3F1C9DB1288B231C3AE0D4FE7344FD2533264720' ), 'data' => 'sample', - 'sig' => hex2bin( + 'signature' => hex2bin( '94EDBB92A5ECB8AAD4736E56C691916B3F88140666CE9FA73D64C4EA95AD133C81A648152E44ACF96E36DD1E80FABE4699EF4AEB15F178CEA1FE40DB2603138F130E740A19624526203B6351D0A3A94FA329C145786E679E7B82C71A38628AC8' ), ]; yield [ - 'alg' => ES384::create(), - 'crv' => Ec2Key::CURVE_P384, + 'algorithm' => ES384::create(), + 'curve' => Ec2Key::CURVE_P384, 'd' => hex2bin( '6B9D3DAD2E1B8C1C05B19875B6659F4DE23C3B667BF297BA9AA47740787137D896D5724E4C70A825F872C9EA60D2EDF5' ), @@ -197,13 +200,13 @@ public static function getVectors(): iterable '8015D9B72D7D57244EA8EF9AC0C621896708A59367F9DFB9F54CA84B3F1C9DB1288B231C3AE0D4FE7344FD2533264720' ), 'data' => 'test', - 'sig' => hex2bin( + 'signature' => hex2bin( '8203B63D3C853E8D77227FB377BCF7B7B772E97892A80F36AB775D509D7A5FEB0542A7F0812998DA8F1DD3CA3CF023DBDDD0760448D42D8A43AF45AF836FCE4DE8BE06B485E9B61B827C2F13173923E06A739F040649A667BF3B828246BAA5A5' ), ]; yield [ - 'alg' => ES512::create(), - 'crv' => Ec2Key::CURVE_P521, + 'algorithm' => ES512::create(), + 'curve' => Ec2Key::CURVE_P521, 'd' => hex2bin( '00FAD06DAA62BA3B25D2FB40133DA757205DE67F5BB0018FEE8C86E1B68C7E75CAA896EB32F1F47C70855836A6D16FCC1466F6D8FBEC67DB89EC0C08B0E996B83538' ), @@ -214,13 +217,13 @@ public static function getVectors(): iterable '00493101C962CD4D2FDDF782285E64584139C2F91B47F87FF82354D6630F746A28A0DB25741B5B34A828008B22ACC23F924FAAFBD4D33F81EA66956DFEAA2BFDFCF5' ), 'data' => 'sample', - 'sig' => hex2bin( + 'signature' => hex2bin( '00C328FAFCBD79DD77850370C46325D987CB525569FB63C5D3BC53950E6D4C5F174E25A1EE9017B5D450606ADD152B534931D7D4E8455CC91F9B15BF05EC36E377FA00617CCE7CF5064806C467F678D3B4080D6F1CC50AF26CA209417308281B68AF282623EAA63E5B5C0723D8B8C37FF0777B1A20F8CCB1DCCC43997F1EE0E44DA4A67A' ), ]; yield [ - 'alg' => ES512::create(), - 'crv' => Ec2Key::CURVE_P521, + 'algorithm' => ES512::create(), + 'curve' => Ec2Key::CURVE_P521, 'd' => hex2bin( '00FAD06DAA62BA3B25D2FB40133DA757205DE67F5BB0018FEE8C86E1B68C7E75CAA896EB32F1F47C70855836A6D16FCC1466F6D8FBEC67DB89EC0C08B0E996B83538' ), @@ -231,13 +234,13 @@ public static function getVectors(): iterable '00493101C962CD4D2FDDF782285E64584139C2F91B47F87FF82354D6630F746A28A0DB25741B5B34A828008B22ACC23F924FAAFBD4D33F81EA66956DFEAA2BFDFCF5' ), 'data' => 'test', - 'sig' => hex2bin( + 'signature' => hex2bin( '013E99020ABF5CEE7525D16B69B229652AB6BDF2AFFCAEF38773B4B7D08725F10CDB93482FDCC54EDCEE91ECA4166B2A7C6265EF0CE2BD7051B7CEF945BABD47EE6D01FBD0013C674AA79CB39849527916CE301C66EA7CE8B80682786AD60F98F7E78A19CA69EFF5C57400E3B3A0AD66CE0978214D13BAF4E9AC60752F7B155E2DE4DCE3' ), ]; yield [ - 'alg' => ES512::create(), - 'crv' => Ec2Key::CURVE_P521, + 'algorithm' => ES512::create(), + 'curve' => Ec2Key::CURVE_P521, 'd' => base64_decode( 'AAhRON2r9cqXX1hg+RoI6R1tX5p2rUAYdmpHZoC1XNM56KtscrX6zbKipQrCW9CGZH3T4ubpnoTKLDYJ/fF3/rJt', true @@ -251,7 +254,7 @@ public static function getVectors(): iterable true ), 'data' => 'eyJhbGciOiJFUzUxMiIsImtpZCI6ImJpbGJvLmJhZ2dpbnNAaG9iYml0b24uZXhhbXBsZSJ9.SXTigJlzIGEgZGFuZ2Vyb3VzIGJ1c2luZXNzLCBGcm9kbywgZ29pbmcgb3V0IHlvdXIgZG9vci4gWW91IHN0ZXAgb250byB0aGUgcm9hZCwgYW5kIGlmIHlvdSBkb24ndCBrZWVwIHlvdXIgZmVldCwgdGhlcmXigJlzIG5vIGtub3dpbmcgd2hlcmUgeW91IG1pZ2h0IGJlIHN3ZXB0IG9mZiB0by4', - 'sig' => base64_decode( + 'signature' => base64_decode( 'AE/R/YZCChjn4791jSQCrdPZCNYqHXCTZH0+JZGYNlaAjP2kqaluUIIUnC9qvbu9Plon7KRTzoNEuT4Va2cmL1eJAQy3mtPBu/u/sDDyYjnAMDxXPn7XrT0lw+kvAD890jl8e2puQens/IEKBpHABlsbEPX6sFY8OcGDqoRuBomu9xQ2', true ), diff --git a/tests/Algorithm/Signature/EdDSA/EdDSATest.php b/tests/Algorithm/Signature/EdDSA/EdDSATest.php index f306743..3f74d0a 100644 --- a/tests/Algorithm/Signature/EdDSA/EdDSATest.php +++ b/tests/Algorithm/Signature/EdDSA/EdDSATest.php @@ -31,7 +31,8 @@ public function aSignatureCanBeComputedAndVerified( int $curve, string $d, string $x, - string $data + string $data, + string $signature ): void { // Given $key = OkpKey::create([ @@ -43,10 +44,12 @@ public function aSignatureCanBeComputedAndVerified( // When $hash = $algorithm->sign($data, $key); - $isValid = $algorithm->verify($data, $key, $hash); + $hashIsValid = $algorithm->verify($data, $key, $hash); + $signatureIsValid = $algorithm->verify($data, $key, $hash); // Then - static::assertTrue($isValid); + static::assertTrue($hashIsValid); + static::assertTrue($signatureIsValid); } #[Test] @@ -80,12 +83,12 @@ public function aSignatureCanBeVerified( public static function getVectors(): iterable { yield [ - 'alg' => Ed25519::create(), - 'crv' => OkpKey::CURVE_ED25519, + 'algorithm' => Ed25519::create(), + 'curve' => OkpKey::CURVE_ED25519, 'd' => base64_decode('nWGxne/9WmC6hEr0kuwsxERJxWl7MmkZcDusAxyuf2A', true), 'x' => base64_decode('11qYAYKxCrfVS/7TyWQHOg7hcvPapiMlrwIaaPcHURo', true), 'data' => 'eyJhbGciOiJFZERTQSJ9.RXhhbXBsZSBvZiBFZDI1NTE5IHNpZ25pbmc', - 'sig' => base64_decode( + 'signature' => base64_decode( 'hgyY0il/MGCjP0JzlnLWG1PPOt7+09PGcvMg3AIbQR6dWbhijcNR4ki4iylGjg5BhVsPt9g7sVvpAr/MuM0KAg', true ), diff --git a/tests/Algorithm/Signature/RSA/RSATest.php b/tests/Algorithm/Signature/RSA/RSATest.php index 2e21e11..90acc81 100644 --- a/tests/Algorithm/Signature/RSA/RSATest.php +++ b/tests/Algorithm/Signature/RSA/RSATest.php @@ -35,16 +35,22 @@ public function theAlgorithmsHaveCorrectInnerParameters(): void #[Test] #[DataProvider('getVectors')] - public function aSignatureCanBeComputedAndVerified(RSA|PSSRSA $algorithm, RsaKey $key, string $data): void - { + public function aSignatureCanBeComputedAndVerified( + RSA|PSSRSA $algorithm, + RsaKey $key, + string $data, + string $signature + ): void { // Given // When - $signature = $algorithm->sign($data, $key); - $isValid = $algorithm->verify($data, $key, $signature); + $computedSignature = $algorithm->sign($data, $key); + $computedSignatureIsValid = $algorithm->verify($data, $key, $computedSignature); + $signatureIsValid = $algorithm->verify($data, $key, $computedSignature); // Then - static::assertTrue($isValid); + static::assertTrue($computedSignatureIsValid); + static::assertTrue($signatureIsValid); } #[Test] @@ -99,30 +105,30 @@ public static function getVectors(): iterable ]); yield [ - 'alg' => RS256::create(), + 'algorithm' => RS256::create(), 'key' => $key1, 'data' => 'eyJhbGciOiJSUzI1NiIsImp3ayI6eyJrdHkiOiJSU0EiLCJuIjoidHBTMVptZlZLVlA1S29mSWhNQlAwdFNXYzRxbGg2Zm0ybHJaU2t1S3hVakVhV2p6WlN6czcyZ0VJR3hyYVd1c01kb1J1VjU0eHNXUnlmNUtlWlQwUy1JNVBybGUzSWRpM2dJQ2lPNE53dk1rNkp3U0JjSld3bVNMRkVLeVVTbkIyQ3RmaUdjMF81clFDcGNFdF9EbjVpTS1CTm43ZnFwb0xJYmtzOHJYS1VJajgtcU1WcWtUWHNFS2VLaW5FMjN0MXlrTWxkc05hYU9ILWh2R3RpNUp0MkRNbkgxSmpvWGREWGZ4dlNQXzBnalVZYjBla3R1ZFlGWG9BNndla21ReUplSW12Z3g0TXl6MUk0aUh0a1lfQ3A3SjRNbjFlalo2SE5teXZvVEVfNE91WTF1Q2VZdjRVeVhGYzFzMXVVeVl0ajR6NTdxc0hHc1M0ZFEzQTJNSnN3IiwiZSI6IkFRQUIifX0.TGl2ZSBsb25nIGFuZCBQcm9zcGVyLg', - 'sig' => base64_decode( + 'signature' => base64_decode( 'QyRlOCcNBMvCEkJRCQA71y2bVX690g0A6wsC2YXf9/VxOYK+g9+xy+1KjghVXkDPe1gDvYSYnL9oWs1PaFKV0/+ijvvJQE6/5pheKTfIVN3Qbkzjxsm4qXTeChBI5MKeBR8z8iWLFT4xPO8NkelwbS2tSUCHrejio6lDDlWhsqSUP8NjHJhqCSZuCDGu3fMMA24cZrYev3tQRc7HHjyi3q/17NZri7feBd7w3NEDkJp7wT/ZclJrYoucHIo1ypaDPJtM+W1+W+lAVREka6Xq4Bg60zdSZ83ODRQTP/IwQrv7hrIcbrRwn1Za/ORZPRPQDP0CMgkb7TkWDZnbPsAzlQ', true ), ]; yield [ - 'alg' => RS384::create(), + 'algorithm' => RS384::create(), 'key' => $key1, 'data' => 'eyJhbGciOiJSUzM4NCIsImp3ayI6eyJrdHkiOiJSU0EiLCJuIjoidHBTMVptZlZLVlA1S29mSWhNQlAwdFNXYzRxbGg2Zm0ybHJaU2t1S3hVakVhV2p6WlN6czcyZ0VJR3hyYVd1c01kb1J1VjU0eHNXUnlmNUtlWlQwUy1JNVBybGUzSWRpM2dJQ2lPNE53dk1rNkp3U0JjSld3bVNMRkVLeVVTbkIyQ3RmaUdjMF81clFDcGNFdF9EbjVpTS1CTm43ZnFwb0xJYmtzOHJYS1VJajgtcU1WcWtUWHNFS2VLaW5FMjN0MXlrTWxkc05hYU9ILWh2R3RpNUp0MkRNbkgxSmpvWGREWGZ4dlNQXzBnalVZYjBla3R1ZFlGWG9BNndla21ReUplSW12Z3g0TXl6MUk0aUh0a1lfQ3A3SjRNbjFlalo2SE5teXZvVEVfNE91WTF1Q2VZdjRVeVhGYzFzMXVVeVl0ajR6NTdxc0hHc1M0ZFEzQTJNSnN3IiwiZSI6IkFRQUIifX0.TGl2ZSBsb25nIGFuZCBQcm9zcGVyLg', - 'sig' => base64_decode( + 'signature' => base64_decode( 'gsBhyBOEDPpHBR8OM2Xb5tybKGeijREZN+smEkvI2188pytujFevbDQJ10afbcdjh5LNKO7U/VD3hGPrC7MIkdtJw4c2d0JnVyhiZT5sFnncnCFjll+Y9GkK7a7jWJJTgF/5LmVEeJSFEEgwT1Stxb+TtZCGqc5ExYizLiuQ2IGB6Sq+hTkpWAXJfmHchE/TxV9A4iLWCMTVM6LsLV6NzDtf2a0iu9XvN1MEdzqM7FNdqNCGN43FveTA0hX8OoFfB2ZjYAjbixUCT4VVI2PuuRyu/Lr8cA73eisolBQLQemPyrCo1s560v2tKD7ICS8Teo1PCJ4HnCuO8bvufI2dKA', true ), ]; yield [ - 'alg' => RS512::create(), + 'algorithm' => RS512::create(), 'key' => $key1, 'data' => 'eyJhbGciOiJSUzUxMiIsImp3ayI6eyJrdHkiOiJSU0EiLCJuIjoidHBTMVptZlZLVlA1S29mSWhNQlAwdFNXYzRxbGg2Zm0ybHJaU2t1S3hVakVhV2p6WlN6czcyZ0VJR3hyYVd1c01kb1J1VjU0eHNXUnlmNUtlWlQwUy1JNVBybGUzSWRpM2dJQ2lPNE53dk1rNkp3U0JjSld3bVNMRkVLeVVTbkIyQ3RmaUdjMF81clFDcGNFdF9EbjVpTS1CTm43ZnFwb0xJYmtzOHJYS1VJajgtcU1WcWtUWHNFS2VLaW5FMjN0MXlrTWxkc05hYU9ILWh2R3RpNUp0MkRNbkgxSmpvWGREWGZ4dlNQXzBnalVZYjBla3R1ZFlGWG9BNndla21ReUplSW12Z3g0TXl6MUk0aUh0a1lfQ3A3SjRNbjFlalo2SE5teXZvVEVfNE91WTF1Q2VZdjRVeVhGYzFzMXVVeVl0ajR6NTdxc0hHc1M0ZFEzQTJNSnN3IiwiZSI6IkFRQUIifX0.TGl2ZSBsb25nIGFuZCBQcm9zcGVyLg', - 'sig' => base64_decode( + 'signature' => base64_decode( 'OMttEbx9fWoJl4gJwp8m249P87nNENhy5RzH84S1iR8b+upQNy8dqHoIVsQ6qINDjDL5YTl4UWvChIr5AO433LjNUimIeEp2cfiqrszTTwhv+EF3Lp3Ft9NmTb+3ZWvDo1WwwUrD0qro7bynaz5O06DxQfTROcrC6hNX05y6nW/+21exs2/w2OoOWA0Ebx9ev1ayZJh1AQ6q18Ajb0Gk1RST1PFjz0Sk/YiUIYRSVJzgv2Lf7R/Lyi5A5OkIfLOyJmKBi6m0FOLoynq/fT96wCbf5Nkhx+RiuFEcefGhgDav7Wfim3zA3ZAHeNWe58BZOf+8v1kXsV+yd6zQlVa8iw', true ), @@ -130,30 +136,30 @@ public static function getVectors(): iterable /* yield [ - 'alg' => PS256::create(), + 'algorithm' => PS256::create(), 'key' => $key1, 'data' => 'eyJhbGciOiJQUzI1NiIsImp3ayI6eyJrdHkiOiJSU0EiLCJrZXlfb3BzIjpbInZlcmlmeSJdLCJuIjoidHBTMVptZlZLVlA1S29mSWhNQlAwdFNXYzRxbGg2Zm0ybHJaU2t1S3hVakVhV2p6WlN6czcyZ0VJR3hyYVd1c01kb1J1VjU0eHNXUnlmNUtlWlQwUy1JNVBybGUzSWRpM2dJQ2lPNE53dk1rNkp3U0JjSld3bVNMRkVLeVVTbkIyQ3RmaUdjMF81clFDcGNFdF9EbjVpTS1CTm43ZnFwb0xJYmtzOHJYS1VJajgtcU1WcWtUWHNFS2VLaW5FMjN0MXlrTWxkc05hYU9ILWh2R3RpNUp0MkRNbkgxSmpvWGREWGZ4dlNQXzBnalVZYjBla3R1ZFlGWG9BNndla21ReUplSW12Z3g0TXl6MUk0aUh0a1lfQ3A3SjRNbjFlalo2SE5teXZvVEVfNE91WTF1Q2VZdjRVeVhGYzFzMXVVeVl0ajR6NTdxc0hHc1M0ZFEzQTJNSnN3IiwiZSI6IkFRQUIifX0.TGl2ZSBsb25nIGFuZCBQcm9zcGVyLg', - 'sig' => base64_decode( + 'signature' => base64_decode( 'NTHE3+OfgBuZclNFsolgYuOd+aNUB5FKQL68MwL/EGsb3hTgbiU1A/6QOdQq6DCQ36gs8nSFWpEyM77TyoDWG0t8ctZlqdrjVSSZrbzrBY0iukeAG0NqVaZlKbRiyuIwvRY4nJBCK6BWmHb4ewXOI/3m8hNVmQajcnHy+xEKm2wla0mZizPN44C/NFmbbX1MKbNRIl5wQz+ILyUOqYb3PRdJSTKCkitLYQX6qLgonlFkIHyY0TsainHJaR09SAzdk3XsDAfYBg/RXvz2lW8+IlxIy+FuLB4HrjgpAq2fRDfRtRyfnI2A1rsMJyDaMVjQniTj1fYg/0hm+7v4HLclV0UzQU3Y2zyG7zsoWDqp9b0/fZGZJydVvuPpOYIN7UlLeFbAVBmRBI09uQs3+VDh8GRtpqno7kIt5W3IiD9a6C0btKlb9yLCXdQqCQBkLX++g7B3GiPW99R/4B2WFMo8BKUbSHxrFZzyYGlGCQ/YjxKz6RPcjR2A2RPWpJfDeXzj', true ), ]; yield [ - 'alg' => PS384::create(), + 'algorithm' => PS384::create(), 'key' => $key1, 'data' => 'eyJhbGciOiJQUzM4NCIsImp3ayI6eyJrdHkiOiJSU0EiLCJuIjoidHBTMVptZlZLVlA1S29mSWhNQlAwdFNXYzRxbGg2Zm0ybHJaU2t1S3hVakVhV2p6WlN6czcyZ0VJR3hyYVd1c01kb1J1VjU0eHNXUnlmNUtlWlQwUy1JNVBybGUzSWRpM2dJQ2lPNE53dk1rNkp3U0JjSld3bVNMRkVLeVVTbkIyQ3RmaUdjMF81clFDcGNFdF9EbjVpTS1CTm43ZnFwb0xJYmtzOHJYS1VJajgtcU1WcWtUWHNFS2VLaW5FMjN0MXlrTWxkc05hYU9ILWh2R3RpNUp0MkRNbkgxSmpvWGREWGZ4dlNQXzBnalVZYjBla3R1ZFlGWG9BNndla21ReUplSW12Z3g0TXl6MUk0aUh0a1lfQ3A3SjRNbjFlalo2SE5teXZvVEVfNE91WTF1Q2VZdjRVeVhGYzFzMXVVeVl0ajR6NTdxc0hHc1M0ZFEzQTJNSnN3IiwiZSI6IkFRQUIifX0.TGl2ZSBsb25nIGFuZCBQcm9zcGVyLg', - 'sig' => base64_decode( + 'signature' => base64_decode( 'VGUibk9r/WDX/K2H4MAsN1oi5oOKWRElPFvcVtPP5hIzDqB0K3S40b+WoFplSbPTtQQKA0W9hqzdQPmpIC4yqrtKrOWF+WmyIfNl1zAnHeNJGw85L/k56BU8T1Wa5qGVf7osA8MPSvw9dnPq0DMRArqiCUipoAUzCS18dmUTH0KIMuyebxMLZHm0c0HJ2n91BxXDrET9ycYxaMPEvIvBu9dIgXwwZiPu65xz6zYgLdfbhSKjc5KJc66JLVwI6j8Q7bmlJ0ChtQtf5f65uslRoR2K3Ezn3MR074EtlCt3KjP9BtdS18Kpxu7uYT5L7OYKJutso/hPNDgUnED4QruZjA', true ), ]; yield [ - 'alg' => PS512::create(), + 'algorithm' => PS512::create(), 'key' => $key1, 'data' => 'eyJhbGciOiJQUzUxMiIsImp3ayI6eyJrdHkiOiJSU0EiLCJuIjoidHBTMVptZlZLVlA1S29mSWhNQlAwdFNXYzRxbGg2Zm0ybHJaU2t1S3hVakVhV2p6WlN6czcyZ0VJR3hyYVd1c01kb1J1VjU0eHNXUnlmNUtlWlQwUy1JNVBybGUzSWRpM2dJQ2lPNE53dk1rNkp3U0JjSld3bVNMRkVLeVVTbkIyQ3RmaUdjMF81clFDcGNFdF9EbjVpTS1CTm43ZnFwb0xJYmtzOHJYS1VJajgtcU1WcWtUWHNFS2VLaW5FMjN0MXlrTWxkc05hYU9ILWh2R3RpNUp0MkRNbkgxSmpvWGREWGZ4dlNQXzBnalVZYjBla3R1ZFlGWG9BNndla21ReUplSW12Z3g0TXl6MUk0aUh0a1lfQ3A3SjRNbjFlalo2SE5teXZvVEVfNE91WTF1Q2VZdjRVeVhGYzFzMXVVeVl0ajR6NTdxc0hHc1M0ZFEzQTJNSnN3IiwiZSI6IkFRQUIifX0.TGl2ZSBsb25nIGFuZCBQcm9zcGVyLg', - 'sig' => base64_decode( + 'signature' => base64_decode( 'bBsgpFWir0MvWmgCZ8CVCGTcHm4C9FgTty8NvtyRHAvpTlL8NCbcZ2VNJWKPpCjge/Rv29jguivUHFgudlBYY6LKJd5xUt12uZQL//Jc8Z1YCNq6BDFtH09HMKRAkePLkRXv05DdoL20eOpZGJMITn0LK5STC+c7YNjlwjppclFfEf0Arl8Er3LvPlyoBMJRd1X7osMFamdEDAoqPM/JTVMQMNI/kXv+P42iePERixvX1MDeF/KUfgWwzfYYUltrpG+JPh05iqwlKTsUchqDTdo8l2phEa5qq6MCQemzvKBMFb2u/B4+VXTD60vJVLSrionHncU1jyOwSIgAKPipxQ', true ),