Skip to content

Commit c4717b8

Browse files
Update interfaces
This will matter a lot when we implement ML-KEM support
1 parent 8963af5 commit c4717b8

File tree

3 files changed

+49
-11
lines changed

3 files changed

+49
-11
lines changed

src/Interfaces/KemInterface.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ public function getHeaderLength(): int;
1010

1111
public function getKemId(): string;
1212

13+
public function getPublicKeyLength(): int;
14+
15+
public function getSecretLength(): int;
16+
17+
public function getSecretKeyLength(): int;
18+
1319
public function encapsulate(
1420
EncapsKeyInterface $encapsKey
1521
): array;

src/KEM/DHKEM/Curve.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,6 @@ public function decapsKeyLength(): int
2828
};
2929
}
3030

31-
public function secretLength(): int
32-
{
33-
return match($this) {
34-
self::X25519, self::Secp256k1, self::NistP256 => 32,
35-
self::NistP384 => 48,
36-
self::NistP521 => 64,
37-
};
38-
}
39-
4031
public function encapsKeyLength(): int
4132
{
4233
return match($this) {

src/KEM/DiffieHellmanKEM.php

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,47 @@ public function __construct(
3838
) {}
3939

4040
/**
41+
* This is called Npk in the HPKE spec..
42+
*
43+
* @return int
44+
*/
45+
public function getPublicKeyLength(): int
46+
{
47+
return match ($this->curve) {
48+
Curve::Secp256k1, Curve::NistP256 => 65,
49+
Curve::NistP384 => 97,
50+
Curve::NistP521 => 133,
51+
Curve::X25519 => 32
52+
};
53+
}
54+
55+
/**
56+
* Thisi s called Nsec in the HPKE spec.
57+
*/
58+
public function getSecretLength(): int
59+
{
60+
return match ($this->curve) {
61+
Curve::Secp256k1, Curve::NistP256, Curve::X25519 => 32,
62+
Curve::NistP384 => 48,
63+
Curve::NistP521 => 64
64+
};
65+
}
66+
67+
/**
68+
* Thisi s called Nsk in the HPKE spec.
69+
*/
70+
public function getSecretKeyLength(): int
71+
{
72+
return match ($this->curve) {
73+
Curve::Secp256k1, Curve::NistP256, Curve::X25519 => 32,
74+
Curve::NistP384 => 48,
75+
Curve::NistP521 => 66
76+
};
77+
}
78+
79+
/**
80+
* This is called Nenc in the HPKE spec.
81+
*
4182
* @return int
4283
*/
4384
public function getHeaderLength(): int
@@ -137,7 +178,7 @@ public function encapsulate(EncapsKeyInterface $encapsKey): array
137178
$dh = $this->scalarMult($ephSecret, $encapsKey);
138179
$enc = $ephPublic->serializeForHeader();
139180
$kem_context = $enc . $encapsKey->serializeForHeader();
140-
$secret_length = $this->curve->secretLength();
181+
$secret_length = $this->getSecretLength();
141182
$shared_secret = new SymmetricKey(
142183
$this->kdf->extractAndExpand(
143184
suiteId: $this->getSuiteId(),
@@ -171,7 +212,7 @@ public function decapsulate(
171212
$ephPublic = new EncapsKey($decapsKey->curve, $enc);
172213
$dh = $this->scalarMult($decapsKey, $ephPublic);
173214
$kem_context = $enc . $decapsKey->getEncapsKey()->bytes;
174-
$secret_length = $this->curve->secretLength();
215+
$secret_length = $this->getSecretLength();
175216
return new SymmetricKey(
176217
$this->kdf->extractAndExpand($this->getSuiteId(), $dh, $kem_context, $secret_length)
177218
);

0 commit comments

Comments
 (0)