| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Cose\Algorithm\Signature\ECDSA; |
| 6 |
|
| 7 |
use function bin2hex; |
| 8 |
use function dechex; |
| 9 |
use function hex2bin; |
| 10 |
use function hexdec; |
| 11 |
use InvalidArgumentException; |
| 12 |
use function str_pad; |
| 13 |
use const STR_PAD_LEFT; |
| 14 |
use function strlen; |
| 15 |
use function substr; |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
final class ECSignature |
| 21 |
{ |
| 22 |
private const ASN1_SEQUENCE = '30'; |
| 23 |
|
| 24 |
private const ASN1_INTEGER = '02'; |
| 25 |
|
| 26 |
private const ASN1_MAX_SINGLE_BYTE = 128; |
| 27 |
|
| 28 |
private const ASN1_LENGTH_2BYTES = '81'; |
| 29 |
|
| 30 |
private const ASN1_BIG_INTEGER_LIMIT = '7f'; |
| 31 |
|
| 32 |
private const ASN1_NEGATIVE_INTEGER = '00'; |
| 33 |
|
| 34 |
private const BYTE_SIZE = 2; |
| 35 |
|
| 36 |
public static function toAsn1(string $signature, int $length): string |
| 37 |
{ |
| 38 |
$signature = bin2hex($signature); |
| 39 |
|
| 40 |
if (self::octetLength($signature) !== $length) { |
| 41 |
throw new InvalidArgumentException('Invalid signature length.'); |
| 42 |
} |
| 43 |
|
| 44 |
$pointR = self::preparePositiveInteger(substr($signature, 0, $length)); |
| 45 |
$pointS = self::preparePositiveInteger(substr($signature, $length)); |
| 46 |
|
| 47 |
$lengthR = self::octetLength($pointR); |
| 48 |
$lengthS = self::octetLength($pointS); |
| 49 |
|
| 50 |
$totalLength = $lengthR + $lengthS + self::BYTE_SIZE + self::BYTE_SIZE; |
| 51 |
$lengthPrefix = $totalLength > self::ASN1_MAX_SINGLE_BYTE ? self::ASN1_LENGTH_2BYTES : ''; |
| 52 |
|
| 53 |
return hex2bin( |
| 54 |
self::ASN1_SEQUENCE |
| 55 |
. $lengthPrefix . dechex($totalLength) |
| 56 |
. self::ASN1_INTEGER . dechex($lengthR) . $pointR |
| 57 |
. self::ASN1_INTEGER . dechex($lengthS) . $pointS |
| 58 |
); |
| 59 |
} |
| 60 |
|
| 61 |
public static function fromAsn1(string $signature, int $length): string |
| 62 |
{ |
| 63 |
$message = bin2hex($signature); |
| 64 |
$position = 0; |
| 65 |
|
| 66 |
if (self::readAsn1Content($message, $position, self::BYTE_SIZE) !== self::ASN1_SEQUENCE) { |
| 67 |
throw new InvalidArgumentException('Invalid data. Should start with a sequence.'); |
| 68 |
} |
| 69 |
|
| 70 |
if (self::readAsn1Content($message, $position, self::BYTE_SIZE) === self::ASN1_LENGTH_2BYTES) { |
| 71 |
$position += self::BYTE_SIZE; |
| 72 |
} |
| 73 |
|
| 74 |
$pointR = self::retrievePositiveInteger(self::readAsn1Integer($message, $position)); |
| 75 |
$pointS = self::retrievePositiveInteger(self::readAsn1Integer($message, $position)); |
| 76 |
|
| 77 |
return hex2bin(str_pad($pointR, $length, '0', STR_PAD_LEFT) . str_pad($pointS, $length, '0', STR_PAD_LEFT)); |
| 78 |
} |
| 79 |
|
| 80 |
private static function octetLength(string $data): int |
| 81 |
{ |
| 82 |
return intdiv(strlen($data), self::BYTE_SIZE); |
| 83 |
} |
| 84 |
|
| 85 |
private static function preparePositiveInteger(string $data): string |
| 86 |
{ |
| 87 |
if (substr($data, 0, self::BYTE_SIZE) > self::ASN1_BIG_INTEGER_LIMIT) { |
| 88 |
return self::ASN1_NEGATIVE_INTEGER . $data; |
| 89 |
} |
| 90 |
|
| 91 |
while ( |
| 92 |
str_starts_with($data, self::ASN1_NEGATIVE_INTEGER) |
| 93 |
&& substr($data, 2, self::BYTE_SIZE) <= self::ASN1_BIG_INTEGER_LIMIT |
| 94 |
) { |
| 95 |
$data = substr($data, 2); |
| 96 |
} |
| 97 |
|
| 98 |
return $data; |
| 99 |
} |
| 100 |
|
| 101 |
private static function readAsn1Content(string $message, int &$position, int $length): string |
| 102 |
{ |
| 103 |
$content = substr($message, $position, $length); |
| 104 |
$position += $length; |
| 105 |
|
| 106 |
return $content; |
| 107 |
} |
| 108 |
|
| 109 |
private static function readAsn1Integer(string $message, int &$position): string |
| 110 |
{ |
| 111 |
if (self::readAsn1Content($message, $position, self::BYTE_SIZE) !== self::ASN1_INTEGER) { |
| 112 |
throw new InvalidArgumentException('Invalid data. Should contain an integer.'); |
| 113 |
} |
| 114 |
|
| 115 |
$length = (int) hexdec(self::readAsn1Content($message, $position, self::BYTE_SIZE)); |
| 116 |
|
| 117 |
return self::readAsn1Content($message, $position, $length * self::BYTE_SIZE); |
| 118 |
} |
| 119 |
|
| 120 |
private static function retrievePositiveInteger(string $data): string |
| 121 |
{ |
| 122 |
while ( |
| 123 |
str_starts_with($data, self::ASN1_NEGATIVE_INTEGER) |
| 124 |
&& substr($data, 2, self::BYTE_SIZE) > self::ASN1_BIG_INTEGER_LIMIT |
| 125 |
) { |
| 126 |
$data = substr($data, 2); |
| 127 |
} |
| 128 |
|
| 129 |
return $data; |
| 130 |
} |
| 131 |
} |