From 042749d34080c7c768eb8ab9550a60f16ab95c5a Mon Sep 17 00:00:00 2001 From: Nguyen Van Nguyen Date: Fri, 23 Aug 2024 11:17:19 +0700 Subject: [PATCH] WIP Signed-off-by: Nguyen Van Nguyen --- src/Packet/Key/ECDHSecretKeyMaterial.php | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/Packet/Key/ECDHSecretKeyMaterial.php b/src/Packet/Key/ECDHSecretKeyMaterial.php index d0723ddc..02b45a14 100644 --- a/src/Packet/Key/ECDHSecretKeyMaterial.php +++ b/src/Packet/Key/ECDHSecretKeyMaterial.php @@ -25,6 +25,8 @@ */ class ECDHSecretKeyMaterial extends ECSecretKeyMaterial { + const CURVE25519_KEY_LENGTH = 32; + /** * Read key material from bytes * @@ -50,16 +52,19 @@ public static function fromBytes( public static function generate(CurveOid $curveOid): self { if ($curveOid !== CurveOid::Ed25519) { - $privateKey = EC::createKey($curveOid->name); if ($curveOid === CurveOid::Curve25519) { - $d = Helper::bin2BigInt( - strrev($privateKey->toString('MontgomeryPrivate')) - ); + do { + $privateKey = EC::createKey($curveOid->name); + $d = Helper::bin2BigInt( + strrev($privateKey->toString('MontgomeryPrivate')) + ); + } while ($d->getLengthInBytes() !== self::CURVE25519_KEY_LENGTH); $q = Helper::bin2BigInt( "\x40" . $privateKey->getEncodedCoordinates() ); } else { + $privateKey = EC::createKey($curveOid->name); $params = PKCS8::load($privateKey->toString('PKCS8')); $d = $params['dA']; $q = Helper::bin2BigInt(