|
12 | 12 | namespace Minishlink\WebPush;
|
13 | 13 |
|
14 | 14 | use Base64Url\Base64Url;
|
15 |
| -use Mdanter\Ecc\Crypto\Key\PublicKey; |
16 | 15 | use Mdanter\Ecc\EccFactory;
|
17 | 16 | use Mdanter\Ecc\Serializer\Point\UncompressedPointSerializer;
|
18 | 17 |
|
@@ -41,21 +40,22 @@ public static function encrypt($payload, $userPublicKey, $userAuthToken, $native
|
41 | 40 | {
|
42 | 41 | $userPublicKey = base64_decode($userPublicKey);
|
43 | 42 | $userAuthToken = base64_decode($userAuthToken);
|
44 |
| - $plaintext = chr(0).chr(0).utf8_decode($payload); |
| 43 | + $plaintext = chr(0).chr(0).$payload; |
45 | 44 |
|
46 | 45 | // initialize utilities
|
47 | 46 | $math = EccFactory::getAdapter();
|
48 |
| - $keySerializer = new UncompressedPointSerializer($math); |
49 |
| - $curveGenerator = EccFactory::getNistCurves()->generator256(); |
| 47 | + $pointSerializer = new UncompressedPointSerializer($math); |
| 48 | + $generator = EccFactory::getNistCurves()->generator256(); |
50 | 49 | $curve = EccFactory::getNistCurves()->curve256();
|
51 | 50 |
|
52 | 51 | // get local key pair
|
53 |
| - $localPrivateKeyObject = $curveGenerator->createPrivateKey(); |
| 52 | + $localPrivateKeyObject = $generator->createPrivateKey(); |
54 | 53 | $localPublicKeyObject = $localPrivateKeyObject->getPublicKey();
|
55 |
| - $localPublicKey = hex2bin($keySerializer->serialize($localPublicKeyObject->getPoint())); |
| 54 | + $localPublicKey = hex2bin($pointSerializer->serialize($localPublicKeyObject->getPoint())); |
56 | 55 |
|
57 | 56 | // get user public key object
|
58 |
| - $userPublicKeyObject = new PublicKey($math, $curveGenerator, $keySerializer->unserialize($curve, bin2hex($userPublicKey))); |
| 57 | + $pointUserPublicKey = $pointSerializer->unserialize($curve, bin2hex($userPublicKey)); |
| 58 | + $userPublicKeyObject = $generator->getPublicKeyFrom($pointUserPublicKey->getX(), $pointUserPublicKey->getY(), $generator->getOrder()); |
59 | 59 |
|
60 | 60 | // get shared secret from user public key and local private key
|
61 | 61 | $sharedSecret = hex2bin($math->decHex($userPublicKeyObject->getPoint()->mul($localPrivateKeyObject->getSecret())->getX()));
|
@@ -145,7 +145,9 @@ private static function createContext($clientPublicKey, $serverPublicKey)
|
145 | 145 | throw new \ErrorException('Invalid server public key length');
|
146 | 146 | }
|
147 | 147 |
|
148 |
| - return chr(0).strlen($clientPublicKey).$clientPublicKey.strlen($serverPublicKey).$serverPublicKey; |
| 148 | + $len = chr(0).'A'; // 65 as Uint16BE |
| 149 | + |
| 150 | + return chr(0).$len.$clientPublicKey.$len.$serverPublicKey; |
149 | 151 | }
|
150 | 152 |
|
151 | 153 | /**
|
|
0 commit comments