Skip to content

Commit 33e9e85

Browse files
committed
Merge the shared KEM secret-key derivation helper (KdfUtil.makeSecretKey) and javax.crypto.KEM support for NTRU LPRime, NTRU+ and SMAUG-T, completing the BCPQC KEM services.
2 parents 58fb839 + d915c39 commit 33e9e85

54 files changed

Lines changed: 2835 additions & 374 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎core/src/main/java/org/bouncycastle/pqc/crypto/ntruplus/NTRUPlusKEMExtractor.java‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@
22

33
import org.bouncycastle.crypto.EncapsulatedSecretExtractor;
44

5+
/**
6+
* NTRU+ decapsulation.
7+
* <p>
8+
* <b>Not thread safe.</b> The NTRUPlusEngine built here keeps one SHAKE instance in a field, so two
9+
* threads sharing an extractor interleave its absorb and squeeze phases: the secrets they recover do
10+
* not match the sender's, and an IllegalStateException from the digest is also possible. Give each
11+
* thread its own extractor - construction is cheap, roughly a thousandth of a decapsulation. The
12+
* KEM.Decapsulator that javax.crypto.KEM builds does exactly that, since that API requires
13+
* decapsulate to be safe for concurrent use.
14+
*/
515
public class NTRUPlusKEMExtractor
616
implements EncapsulatedSecretExtractor
717
{

‎core/src/main/java/org/bouncycastle/pqc/crypto/ntruplus/NTRUPlusKEMGenerator.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.security.SecureRandom;
44

5+
import org.bouncycastle.crypto.CryptoServicesRegistrar;
56
import org.bouncycastle.crypto.EncapsulatedSecretGenerator;
67
import org.bouncycastle.crypto.SecretWithEncapsulation;
78
import org.bouncycastle.crypto.params.AsymmetricKeyParameter;
@@ -14,7 +15,7 @@ public class NTRUPlusKEMGenerator
1415

1516
public NTRUPlusKEMGenerator(SecureRandom random)
1617
{
17-
this.sr = random;
18+
this.sr = CryptoServicesRegistrar.getSecureRandom(random);
1819
}
1920

2021
@Override

‎core/src/main/java/org/bouncycastle/pqc/crypto/ntruprime/NTRULPRimeKEMExtractor.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,6 @@ public byte[] extractSecret(byte[] encapsulation)
182182

183183
public int getEncapsulationLength()
184184
{
185-
return privateKey.getParameters().getRoundedPolynomialBytes() + 128 + 32;
185+
return privateKey.getParameters().getEncapsulationLength();
186186
}
187187
}

‎core/src/main/java/org/bouncycastle/pqc/crypto/ntruprime/NTRULPRimeKEMGenerator.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.security.SecureRandom;
44

5+
import org.bouncycastle.crypto.CryptoServicesRegistrar;
56
import org.bouncycastle.crypto.EncapsulatedSecretGenerator;
67
import org.bouncycastle.crypto.SecretWithEncapsulation;
78
import org.bouncycastle.crypto.params.AsymmetricKeyParameter;
@@ -15,7 +16,7 @@ public class NTRULPRimeKEMGenerator
1516

1617
public NTRULPRimeKEMGenerator(SecureRandom random)
1718
{
18-
this.random = random;
19+
this.random = CryptoServicesRegistrar.getSecureRandom(random);
1920
}
2021

2122
@Override

‎core/src/main/java/org/bouncycastle/pqc/crypto/ntruprime/NTRULPRimeParameters.java‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,4 +139,13 @@ public int getSessionKeySize()
139139
return sharedKeyBytes * 8;
140140
}
141141

142+
/**
143+
* Return the size in bytes of an encapsulation produced for this parameter set. Kept here so
144+
* that a caller holding only a public key - the encapsulating side - derives it from the same
145+
* expression NTRULPRimeKEMExtractor uses, rather than repeating it.
146+
*/
147+
public int getEncapsulationLength()
148+
{
149+
return roundedPolynomialBytes + 128 + 32;
150+
}
142151
}

‎core/src/main/java/org/bouncycastle/pqc/crypto/ntruprime/SNTRUPrimeKEMExtractor.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,6 @@ public byte[] extractSecret(byte[] encapsulation)
154154

155155
public int getEncapsulationLength()
156156
{
157-
return privateKey.getParameters().getRoundedPolynomialBytes() + 32;
157+
return privateKey.getParameters().getEncapsulationLength();
158158
}
159159
}

‎core/src/main/java/org/bouncycastle/pqc/crypto/ntruprime/SNTRUPrimeParameters.java‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,14 @@ public int getSessionKeySize()
100100
{
101101
return sharedKeyBytes * 8;
102102
}
103+
104+
/**
105+
* Return the size in bytes of an encapsulation produced for this parameter set, so that the
106+
* encapsulating side - which holds only a public key and no extractor - reads it from the same
107+
* place SNTRUPrimeKEMExtractor does.
108+
*/
109+
public int getEncapsulationLength()
110+
{
111+
return roundedPolynomialBytes + 32;
112+
}
103113
}

‎core/src/main/java/org/bouncycastle/pqc/crypto/smaugt/SmaugTEngine.java‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@
88
/**
99
* Reference implementation of SMAUG-T KEM, translated from the SMAUG-T 1.2.0 C
1010
* reference code.
11+
* <p>
12+
* <b>Must remain immutable.</b> SmaugTParameters holds one instance per parameter set for the life
13+
* of the JVM and hands it to every extractor, generator and key pair generator, so this class is
14+
* shared across threads. javax.crypto.KEM requires a Decapsulator to be safe for concurrent use and
15+
* SmaugTKEMExtractor relies on that sharing being safe. Every field here is final and set at
16+
* construction, and every operation allocates its own buffers and digests - adding a scratch buffer
17+
* or a cached digest field would silently corrupt concurrent users rather than fail. If one is ever
18+
* needed, make getEngine() return a new instance instead.
1119
*/
1220
class SmaugTEngine
1321
{

‎core/src/main/java/org/bouncycastle/pqc/crypto/smaugt/SmaugTKEMGenerator.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.security.SecureRandom;
44

5+
import org.bouncycastle.crypto.CryptoServicesRegistrar;
56
import org.bouncycastle.crypto.EncapsulatedSecretGenerator;
67
import org.bouncycastle.crypto.SecretWithEncapsulation;
78
import org.bouncycastle.crypto.params.AsymmetricKeyParameter;
@@ -14,7 +15,7 @@ public class SmaugTKEMGenerator
1415

1516
public SmaugTKEMGenerator(SecureRandom random)
1617
{
17-
this.sr = random;
18+
this.sr = CryptoServicesRegistrar.getSecureRandom(random);
1819
}
1920

2021
public SecretWithEncapsulation generateEncapsulated(AsymmetricKeyParameter recipientKey)

‎core/src/main/java/org/bouncycastle/pqc/crypto/smaugt/SmaugTParameters.java‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,25 @@ public int getSessionKeySize()
4646
return defaultKeySize;
4747
}
4848

49+
/**
50+
* Return the size in bytes of an encapsulation produced for this parameter set. The engine that
51+
* knows it is package private, so this is the only way the encapsulating side - which holds a
52+
* public key and no extractor - can obtain it.
53+
*/
54+
public int getEncapsulationLength()
55+
{
56+
return engine.getCipherTextBytes();
57+
}
58+
4959
int getMode()
5060
{
5161
return mode;
5262
}
5363

64+
/**
65+
* Return the engine for this parameter set. One instance is shared by every caller for the life
66+
* of the JVM, so SmaugTEngine must stay immutable - see its class javadoc.
67+
*/
5468
SmaugTEngine getEngine()
5569
{
5670
return engine;

0 commit comments

Comments
 (0)