| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Cose\Algorithm\Signature\RSA; |
| 6 |
|
| 7 |
use function ceil; |
| 8 |
use function chr; |
| 9 |
use Cose\Algorithm\Signature\Signature; |
| 10 |
use Cose\BigInteger; |
| 11 |
use Cose\Hash; |
| 12 |
use Cose\Key\Key; |
| 13 |
use Cose\Key\RsaKey; |
| 14 |
use function hash_equals; |
| 15 |
use InvalidArgumentException; |
| 16 |
use function ord; |
| 17 |
use function pack; |
| 18 |
use function random_bytes; |
| 19 |
use RuntimeException; |
| 20 |
use function str_pad; |
| 21 |
use const STR_PAD_LEFT; |
| 22 |
use function str_repeat; |
| 23 |
use function strlen; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
abstract class PSSRSA implements Signature |
| 29 |
{ |
| 30 |
public function sign(string $data, Key $key): string |
| 31 |
{ |
| 32 |
$key = $this->handleKey($key); |
| 33 |
$modulusLength = strlen($key->n()); |
| 34 |
|
| 35 |
$em = $this->encodeEMSAPSS($data, 8 * $modulusLength - 1, $this->getHashAlgorithm()); |
| 36 |
$message = BigInteger::createFromBinaryString($em); |
| 37 |
$signature = $this->exponentiate($key, $message); |
| 38 |
|
| 39 |
return $this->convertIntegerToOctetString($signature, $modulusLength); |
| 40 |
} |
| 41 |
|
| 42 |
public function verify(string $data, Key $key, string $signature): bool |
| 43 |
{ |
| 44 |
$key = $this->handleKey($key); |
| 45 |
$modulusLength = strlen($key->n()); |
| 46 |
if (strlen($signature) !== $modulusLength) { |
| 47 |
throw new InvalidArgumentException('Invalid modulus length'); |
| 48 |
} |
| 49 |
$s2 = BigInteger::createFromBinaryString($signature); |
| 50 |
$m2 = $this->exponentiate($key, $s2); |
| 51 |
$em = $this->convertIntegerToOctetString($m2, $modulusLength); |
| 52 |
$modBits = 8 * $modulusLength; |
| 53 |
|
| 54 |
return $this->verifyEMSAPSS($data, $em, $modBits - 1, $this->getHashAlgorithm()); |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
public function exponentiate(RsaKey $key, BigInteger $c): BigInteger |
| 61 |
{ |
| 62 |
if ($c->compare(BigInteger::createFromDecimal(0)) < 0 || $c->compare( |
| 63 |
BigInteger::createFromBinaryString($key->n()) |
| 64 |
) > 0) { |
| 65 |
throw new RuntimeException(); |
| 66 |
} |
| 67 |
if ($key->isPublic() || ! $key->hasPrimes() || ! $key->hasExponents() || ! $key->hasCoefficient()) { |
| 68 |
return $c->modPow( |
| 69 |
BigInteger::createFromBinaryString($key->e()), |
| 70 |
BigInteger::createFromBinaryString($key->n()) |
| 71 |
); |
| 72 |
} |
| 73 |
|
| 74 |
[$pS, $qS] = $key->primes(); |
| 75 |
[$dPS, $dQS] = $key->exponents(); |
| 76 |
$qInv = BigInteger::createFromBinaryString($key->QInv()); |
| 77 |
$p = BigInteger::createFromBinaryString($pS); |
| 78 |
$q = BigInteger::createFromBinaryString($qS); |
| 79 |
$dP = BigInteger::createFromBinaryString($dPS); |
| 80 |
$dQ = BigInteger::createFromBinaryString($dQS); |
| 81 |
|
| 82 |
$m1 = $c->modPow($dP, $p); |
| 83 |
$m2 = $c->modPow($dQ, $q); |
| 84 |
$h = $qInv->multiply($m1->subtract($m2)->add($p)) |
| 85 |
->mod($p) |
| 86 |
; |
| 87 |
|
| 88 |
return $m2->add($h->multiply($q)); |
| 89 |
} |
| 90 |
|
| 91 |
abstract protected function getHashAlgorithm(): Hash; |
| 92 |
|
| 93 |
private function handleKey(Key $key): RsaKey |
| 94 |
{ |
| 95 |
return RsaKey::create($key->getData()); |
| 96 |
} |
| 97 |
|
| 98 |
private function convertIntegerToOctetString(BigInteger $x, int $xLen): string |
| 99 |
{ |
| 100 |
$xB = $x->toBytes(); |
| 101 |
if (strlen($xB) > $xLen) { |
| 102 |
throw new RuntimeException('Unable to convert the integer'); |
| 103 |
} |
| 104 |
|
| 105 |
return str_pad($xB, $xLen, chr(0), STR_PAD_LEFT); |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
private function getMGF1(string $mgfSeed, int $maskLen, Hash $mgfHash): string |
| 112 |
{ |
| 113 |
$t = ''; |
| 114 |
$count = ceil($maskLen / $mgfHash->getLength()); |
| 115 |
for ($i = 0; $i < $count; ++$i) { |
| 116 |
$c = pack('N', $i); |
| 117 |
$t .= $mgfHash->hash($mgfSeed . $c); |
| 118 |
} |
| 119 |
|
| 120 |
return substr($t, 0, $maskLen); |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
private function encodeEMSAPSS(string $message, int $modulusLength, Hash $hash): string |
| 127 |
{ |
| 128 |
$emLen = ($modulusLength + 1) >> 3; |
| 129 |
$sLen = $hash->getLength(); |
| 130 |
$mHash = $hash->hash($message); |
| 131 |
if ($emLen <= $hash->getLength() + $sLen + 2) { |
| 132 |
throw new RuntimeException(); |
| 133 |
} |
| 134 |
$salt = random_bytes($sLen); |
| 135 |
$m2 = "\0\0\0\0\0\0\0\0" . $mHash . $salt; |
| 136 |
$h = $hash->hash($m2); |
| 137 |
$ps = str_repeat(chr(0), $emLen - $sLen - $hash->getLength() - 2); |
| 138 |
$db = $ps . chr(1) . $salt; |
| 139 |
$dbMask = $this->getMGF1($h, $emLen - $hash->getLength() - 1, $hash); |
| 140 |
$maskedDB = $db ^ $dbMask; |
| 141 |
$maskedDB[0] = ~chr(0xFF << ($modulusLength & 7)) & $maskedDB[0]; |
| 142 |
|
| 143 |
return $maskedDB . $h . chr(0xBC); |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
private function verifyEMSAPSS(string $m, string $em, int $emBits, Hash $hash): bool |
| 150 |
{ |
| 151 |
$emLen = ($emBits + 1) >> 3; |
| 152 |
$sLen = $hash->getLength(); |
| 153 |
$mHash = $hash->hash($m); |
| 154 |
if ($emLen < $hash->getLength() + $sLen + 2) { |
| 155 |
throw new InvalidArgumentException(); |
| 156 |
} |
| 157 |
if ($em[strlen($em) - 1] !== chr(0xBC)) { |
| 158 |
throw new InvalidArgumentException(); |
| 159 |
} |
| 160 |
$maskedDB = substr($em, 0, -$hash->getLength() - 1); |
| 161 |
$h = substr($em, -$hash->getLength() - 1, $hash->getLength()); |
| 162 |
$temp = chr(0xFF << ($emBits & 7)); |
| 163 |
if ((~$maskedDB[0] & $temp) !== $temp) { |
| 164 |
throw new InvalidArgumentException(); |
| 165 |
} |
| 166 |
$dbMask = $this->getMGF1($h, $emLen - $hash->getLength() - 1, $hash); |
| 167 |
$db = $maskedDB ^ $dbMask; |
| 168 |
$db[0] = ~chr(0xFF << ($emBits & 7)) & $db[0]; |
| 169 |
$temp = $emLen - $hash->getLength() - $sLen - 2; |
| 170 |
if (! str_starts_with($db, str_repeat(chr(0), $temp))) { |
| 171 |
throw new InvalidArgumentException(); |
| 172 |
} |
| 173 |
if (ord($db[$temp]) !== 1) { |
| 174 |
throw new InvalidArgumentException(); |
| 175 |
} |
| 176 |
$salt = substr($db, $temp + 1); |
| 177 |
$m2 = "\0\0\0\0\0\0\0\0" . $mHash . $salt; |
| 178 |
$h2 = $hash->hash($m2); |
| 179 |
|
| 180 |
return hash_equals($h, $h2); |
| 181 |
} |
| 182 |
} |