|
7 | 7 | use Exception;
|
8 | 8 | use Mdanter\Ecc\Crypto\Key\PrivateKey;
|
9 | 9 | use Mdanter\Ecc\Crypto\Signature\SchnorrSigner;
|
| 10 | +use Mdanter\Ecc\Crypto\Signature\Signature; |
| 11 | +use Mdanter\Ecc\Crypto\Signature\Signer; |
| 12 | +use Mdanter\Ecc\Crypto\Signature\SignHasher; |
10 | 13 | use Mdanter\Ecc\Curves\SecureCurveFactory;
|
| 14 | +use Mdanter\Ecc\Exception\IncorrectAlgorithmException; |
11 | 15 | use Mdanter\Ecc\Exception\InsecureCurveException;
|
12 | 16 | use Mdanter\Ecc\Math\ConstantTimeMath;
|
| 17 | +use Mdanter\Ecc\Math\GmpMath; |
13 | 18 | use Mdanter\Ecc\Tests\AbstractTestCase;
|
14 | 19 |
|
15 | 20 | /**
|
@@ -99,6 +104,30 @@ public function testSchnorrVerificationAndSigning(
|
99 | 104 |
|
100 | 105 | // Ensure the same verification result occurs:
|
101 | 106 | self::assertSame($signer->formatSignature($pkObject, $signResult2), $signResult['signature']);
|
| 107 | + |
| 108 | + // First, we make a fake "ECDSA" signature out of this Schnorr signature and ensure it's not accepted: |
| 109 | + $ecdsaSig = new Signature($signResult2->getR(), $signResult2->getS()); |
| 110 | + |
| 111 | + $thrown = false; |
| 112 | + try { |
| 113 | + $signer->verifyWithKey($pkObject, $ecdsaSig, $message); |
| 114 | + } catch (IncorrectAlgorithmException $ex) { |
| 115 | + $thrown = true; |
| 116 | + } |
| 117 | + self::assertTrue($thrown, 'ECDSA / Schnorr signatures can be swapped'); |
| 118 | + |
| 119 | + // Next, we try the inverse attack: Feeding a Schnorr sig into the ECDSA class: |
| 120 | + $math = new GmpMath(); |
| 121 | + $ecdsaSigner = new Signer($math, true); |
| 122 | + $hash = (new SignHasher('sha256', $math))->makeHash($message, $generator); |
| 123 | + |
| 124 | + $thrown = false; |
| 125 | + try { |
| 126 | + $ecdsaSigner->verify($pkObject, $signResult2, $hash); |
| 127 | + } catch (IncorrectAlgorithmException $ex) { |
| 128 | + $thrown = true; |
| 129 | + } |
| 130 | + self::assertTrue($thrown, 'ECDSA / Schnorr signatures can be swapped'); |
102 | 131 | }
|
103 | 132 |
|
104 | 133 | /**
|
|
0 commit comments