Skip to content

Commit 3501709

Browse files
committed
Share one extractor across a FrodoKEM or NTRU+ KEM decapsulator's calls now that the engines build their SHAKE instance per call, rather than constructing a new extractor on every decapsulate, matching the other KEM decapsulators.
1 parent 82dc287 commit 3501709

2 files changed

Lines changed: 12 additions & 22 deletions

File tree

‎prov/src/main/jdk17/org/bouncycastle/jcajce/provider/asymmetric/frodokem/FrodoKEMDecapsulatorSpi.java‎

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import javax.crypto.SecretKey;
88

99
import org.bouncycastle.crypto.kems.FrodoKEMExtractor;
10-
import org.bouncycastle.crypto.params.FrodoKEMPrivateKeyParameters;
1110
import org.bouncycastle.jcajce.provider.asymmetric.util.KdfUtil;
1211
import org.bouncycastle.jcajce.spec.KTSParameterSpec;
1312

@@ -19,15 +18,13 @@
1918
class FrodoKEMDecapsulatorSpi
2019
implements KEMSpi.DecapsulatorSpi
2120
{
22-
private final FrodoKEMPrivateKeyParameters privateKeyParams;
2321
private final KTSParameterSpec parameterSpec;
24-
private final int encapsulationLength;
22+
private final FrodoKEMExtractor kemExt;
2523

2624
FrodoKEMDecapsulatorSpi(BCFrodoKEMPrivateKey privateKey, KTSParameterSpec parameterSpec)
2725
{
28-
this.privateKeyParams = privateKey.getKeyParams();
2926
this.parameterSpec = parameterSpec;
30-
this.encapsulationLength = privateKeyParams.getParameters().getEncapsulationLength();
27+
this.kemExt = new FrodoKEMExtractor(privateKey.getKeyParams());
3128
}
3229

3330
@Override
@@ -45,10 +42,9 @@ public SecretKey engineDecapsulate(byte[] encapsulation, int from, int to, Strin
4542

4643
algorithm = KdfUtil.resolveAlgorithm(parameterSpec, algorithm);
4744

48-
// A FrodoKEMExtractor holds one FrodoKEMEngine, whose SHAKE digest is mutable state, so a
49-
// shared extractor is not safe for the concurrent use javax.crypto.KEM requires of a
50-
// Decapsulator - build one per call, as FrodoKEMCipherSpi.engineUnwrap does.
51-
byte[] kemSecret = new FrodoKEMExtractor(privateKeyParams).extractSecret(encapsulation);
45+
// FrodoKEMEngine builds its SHAKE instance per call, so one extractor is safe to share
46+
// across the concurrent decapsulate calls javax.crypto.KEM requires.
47+
byte[] kemSecret = kemExt.extractSecret(encapsulation);
5248

5349
return KdfUtil.makeSecretKey(parameterSpec, kemSecret, from, to, algorithm);
5450
}
@@ -62,6 +58,6 @@ public int engineSecretSize()
6258
@Override
6359
public int engineEncapsulationSize()
6460
{
65-
return encapsulationLength;
61+
return kemExt.getEncapsulationLength();
6662
}
6763
}

‎prov/src/main/jdk17/org/bouncycastle/pqc/jcajce/provider/ntruplus/NTRUPlusDecapsulatorSpi.java‎

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import org.bouncycastle.jcajce.provider.asymmetric.util.KdfUtil;
1010
import org.bouncycastle.jcajce.spec.KTSParameterSpec;
1111
import org.bouncycastle.pqc.crypto.ntruplus.NTRUPlusKEMExtractor;
12-
import org.bouncycastle.pqc.crypto.ntruplus.NTRUPlusPrivateKeyParameters;
1312

1413
/*
1514
* NOTE: Per javadoc for javax.crypto.KEM, "Encapsulator and Decapsulator objects are also immutable. It is safe to
@@ -19,15 +18,13 @@
1918
class NTRUPlusDecapsulatorSpi
2019
implements KEMSpi.DecapsulatorSpi
2120
{
22-
private final NTRUPlusPrivateKeyParameters privateKeyParams;
2321
private final KTSParameterSpec parameterSpec;
24-
private final int encapsulationLength;
22+
private final NTRUPlusKEMExtractor kemExt;
2523

2624
NTRUPlusDecapsulatorSpi(BCNTRUPlusPrivateKey privateKey, KTSParameterSpec parameterSpec)
2725
{
28-
this.privateKeyParams = privateKey.getKeyParams();
2926
this.parameterSpec = parameterSpec;
30-
this.encapsulationLength = privateKeyParams.getParameters().getCiphertextBytes();
27+
this.kemExt = new NTRUPlusKEMExtractor(privateKey.getKeyParams());
3128
}
3229

3330
@Override
@@ -45,12 +42,9 @@ public SecretKey engineDecapsulate(byte[] encapsulation, int from, int to, Strin
4542

4643
algorithm = KdfUtil.resolveAlgorithm(parameterSpec, algorithm);
4744

48-
// An NTRUPlusKEMExtractor builds an NTRUPlusEngine, which keeps one SHAKE instance in a
49-
// field, so a shared extractor is not safe for the concurrent use javax.crypto.KEM requires
50-
// of a Decapsulator - build one per call, as FrodoKEMDecapsulatorSpi does for the same
51-
// reason. Both engines still want that field made a local, which would let either
52-
// decapsulator share an extractor and would fix the lightweight extractors too.
53-
byte[] kemSecret = new NTRUPlusKEMExtractor(privateKeyParams).extractSecret(encapsulation);
45+
// NTRUPlusEngine builds its SHAKE instance per call, so one extractor is safe to share
46+
// across the concurrent decapsulate calls javax.crypto.KEM requires.
47+
byte[] kemSecret = kemExt.extractSecret(encapsulation);
5448

5549
return KdfUtil.makeSecretKey(parameterSpec, kemSecret, from, to, algorithm);
5650
}
@@ -64,6 +58,6 @@ public int engineSecretSize()
6458
@Override
6559
public int engineEncapsulationSize()
6660
{
67-
return encapsulationLength;
61+
return kemExt.getEncapsulationLength();
6862
}
6963
}

0 commit comments

Comments
 (0)