Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
Signed-off-by: Nguyen Van Nguyen <[email protected]>
  • Loading branch information
nguyennv committed Aug 23, 2024
1 parent 27858e1 commit 042749d
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/Packet/Key/ECDHSecretKeyMaterial.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
*/
class ECDHSecretKeyMaterial extends ECSecretKeyMaterial
{
const CURVE25519_KEY_LENGTH = 32;

/**
* Read key material from bytes
*
Expand All @@ -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(
Expand Down

0 comments on commit 042749d

Please sign in to comment.