Skip to content

Commit 0e67365

Browse files
author
gefeili
committed
Build NTRUPlusEngine's SHAKE instance per call rather than holding one in a field, the same change already made here for FrodoKEMEngine and which CMCEEngine had all along. It is reached that way through NTRUPlusKEMExtractor, which builds one engine in its constructor and keeps it: four threads extracting through a single extractor returned 7 of 96 shared secrets that silently did not match the sender's, and threw IllegalStateException "attempt to absorb with odd length queue". Encapsulation was unaffected, since NTRUPlusKEMGenerator builds an engine per call. The digest was already reached only through the two shake256 wrappers, whose bodies are the digest operation and nothing else, so this is one field removed and two locals added - no signature or call site changes. Single-threaded results are unchanged: the reference vectors are byte-identical, verified by reverting the change and watching NTRUPlusTest still pass while NTRUPlusConcurrencyTest fails, and likewise for FrodoKEMVectorTest against the FrodoKEM change of the previous commit. Registers NTRUPlusTest in the pqc AllTests suite while adding NTRUPlusConcurrencyTest alongside it: the only reference to NTRUPlusTest was its own main method, so the NTRU+ reference vectors had never run under the core test task at all, and they are what gates byte-identity for this change.
1 parent 500a692 commit 0e67365

3 files changed

Lines changed: 170 additions & 1 deletion

File tree

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ class NTRUPlusEngine
3131
public short polyBytes;
3232
public short[] zetas;
3333
private final NTRUPlusParameters params;
34-
private final SHAKEDigest shakeDigest = new SHAKEDigest(256);
3534

3635
public NTRUPlusEngine(NTRUPlusParameters params)
3736
{
@@ -325,6 +324,10 @@ public int baseinv(short[] r, int rOff, short[] a, int aOff, short zeta)
325324

326325
public void shake256(byte[] output, int outOff, int outLen, byte[] input, int inLen)
327326
{
327+
// a SHAKE instance is mutable state, so it is built per call rather than shared -
328+
// an engine may be reached concurrently through a cached extractor
329+
SHAKEDigest shakeDigest = new SHAKEDigest(256);
330+
328331
shakeDigest.update(input, 0, inLen);
329332
shakeDigest.doFinal(output, outOff, outLen);
330333
}
@@ -683,6 +686,10 @@ public void crypto_kem_enc_derand(byte[] ct, int ctPos, byte[] ss, int ssPos,
683686
*/
684687
private void shake256(byte[] output, int outOff, int outLen, byte domainSeperation, byte[] input, int inOff, int inLen)
685688
{
689+
// a SHAKE instance is mutable state, so it is built per call rather than shared -
690+
// an engine may be reached concurrently through a cached extractor
691+
SHAKEDigest shakeDigest = new SHAKEDigest(256);
692+
686693
shakeDigest.update(domainSeperation);
687694
shakeDigest.update(input, inOff, inLen);
688695
shakeDigest.doFinal(output, outOff, outLen);

‎core/src/test/java/org/bouncycastle/pqc/crypto/test/AllTests.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ public static Test suite()
2525
// suite.addTestSuite(SphincsPlusTest.class); -- now deprecated
2626
// CMCE (Classic McEliece) tests split out into AllTestsCMCE so they can run as a separate (parallel) fork.
2727
suite.addTestSuite(FrodoKEMConcurrencyTest.class);
28+
suite.addTestSuite(NTRUPlusConcurrencyTest.class);
29+
suite.addTestSuite(NTRUPlusTest.class);
2830
suite.addTestSuite(FrodoKEMVectorTest.class);
2931
suite.addTestSuite(SABERVectorTest.class);
3032
suite.addTestSuite(NTRUTest.class);
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
package org.bouncycastle.pqc.crypto.test;
2+
3+
import java.security.SecureRandom;
4+
5+
import junit.framework.TestCase;
6+
import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
7+
import org.bouncycastle.crypto.SecretWithEncapsulation;
8+
import org.bouncycastle.pqc.crypto.ntruplus.NTRUPlusKEMExtractor;
9+
import org.bouncycastle.pqc.crypto.ntruplus.NTRUPlusKEMGenerator;
10+
import org.bouncycastle.pqc.crypto.ntruplus.NTRUPlusKeyGenerationParameters;
11+
import org.bouncycastle.pqc.crypto.ntruplus.NTRUPlusKeyPairGenerator;
12+
import org.bouncycastle.pqc.crypto.ntruplus.NTRUPlusParameters;
13+
import org.bouncycastle.pqc.crypto.ntruplus.NTRUPlusPrivateKeyParameters;
14+
import org.bouncycastle.pqc.crypto.ntruplus.NTRUPlusPublicKeyParameters;
15+
import org.bouncycastle.util.Arrays;
16+
17+
/**
18+
* An NTRUPlusKEMExtractor holds one NTRUPlusEngine for its lifetime, so the engine must not carry
19+
* mutable state across a call. It used to keep a single SHAKE instance in a field: two threads
20+
* extracting through one extractor interleaved their absorb/squeeze phases, which produced wrong
21+
* shared secrets and threw {@code IllegalStateException: attempt to absorb with odd length queue}.
22+
* The digest is now built per call, as CMCEEngine's already was.
23+
* <p>
24+
* The generator side is exercised too, since NTRUPlusKEMGenerator builds an engine per call but its
25+
* KEM keys and parameters are shared - and NTRUPlusParameters hands every engine the same zetas
26+
* array, so a write to it would corrupt globally.
27+
*/
28+
public class NTRUPlusConcurrencyTest
29+
extends TestCase
30+
{
31+
private static final int VECTORS = 16;
32+
private static final int THREADS = 4;
33+
34+
public void testConcurrentExtraction()
35+
throws Exception
36+
{
37+
SecureRandom random = new SecureRandom();
38+
39+
NTRUPlusKeyPairGenerator kpg = new NTRUPlusKeyPairGenerator();
40+
kpg.init(new NTRUPlusKeyGenerationParameters(random, NTRUPlusParameters.ntruplus_kem_768));
41+
AsymmetricCipherKeyPair kp = kpg.generateKeyPair();
42+
43+
final NTRUPlusPublicKeyParameters pub = (NTRUPlusPublicKeyParameters)kp.getPublic();
44+
NTRUPlusPrivateKeyParameters priv = (NTRUPlusPrivateKeyParameters)kp.getPrivate();
45+
46+
final byte[][] encapsulations = new byte[VECTORS][];
47+
final byte[][] expected = new byte[VECTORS][];
48+
49+
NTRUPlusKEMGenerator gen = new NTRUPlusKEMGenerator(random);
50+
for (int i = 0; i != VECTORS; i++)
51+
{
52+
SecretWithEncapsulation enc = gen.generateEncapsulated(pub);
53+
encapsulations[i] = enc.getEncapsulation();
54+
expected[i] = enc.getSecret();
55+
}
56+
57+
// one extractor, shared across threads
58+
final NTRUPlusKEMExtractor extractor = new NTRUPlusKEMExtractor(priv);
59+
final Throwable[] failure = new Throwable[1];
60+
final int[] mismatches = new int[1];
61+
62+
Thread[] threads = new Thread[THREADS];
63+
for (int t = 0; t != THREADS; t++)
64+
{
65+
threads[t] = new Thread()
66+
{
67+
public void run()
68+
{
69+
for (int i = 0; i != VECTORS; i++)
70+
{
71+
try
72+
{
73+
byte[] secret = extractor.extractSecret(encapsulations[i]);
74+
if (!Arrays.areEqual(secret, expected[i]))
75+
{
76+
synchronized (mismatches)
77+
{
78+
mismatches[0]++;
79+
}
80+
}
81+
}
82+
catch (Throwable th)
83+
{
84+
failure[0] = th;
85+
}
86+
}
87+
}
88+
};
89+
threads[t].start();
90+
}
91+
for (int t = 0; t != THREADS; t++)
92+
{
93+
threads[t].join();
94+
}
95+
96+
assertNull("shared extractor threw " + failure[0], failure[0]);
97+
assertEquals("shared extractor produced wrong secrets", 0, mismatches[0]);
98+
}
99+
100+
public void testConcurrentEncapsulation()
101+
throws Exception
102+
{
103+
SecureRandom random = new SecureRandom();
104+
105+
NTRUPlusKeyPairGenerator kpg = new NTRUPlusKeyPairGenerator();
106+
kpg.init(new NTRUPlusKeyGenerationParameters(random, NTRUPlusParameters.ntruplus_kem_768));
107+
AsymmetricCipherKeyPair kp = kpg.generateKeyPair();
108+
109+
final NTRUPlusPublicKeyParameters pub = (NTRUPlusPublicKeyParameters)kp.getPublic();
110+
final NTRUPlusKEMExtractor extractor =
111+
new NTRUPlusKEMExtractor((NTRUPlusPrivateKeyParameters)kp.getPrivate());
112+
113+
// one generator, shared across threads - each encapsulation must round trip
114+
final NTRUPlusKEMGenerator gen = new NTRUPlusKEMGenerator(random);
115+
final Throwable[] failure = new Throwable[1];
116+
final int[] mismatches = new int[1];
117+
118+
Thread[] threads = new Thread[THREADS];
119+
for (int t = 0; t != THREADS; t++)
120+
{
121+
threads[t] = new Thread()
122+
{
123+
public void run()
124+
{
125+
for (int i = 0; i != VECTORS; i++)
126+
{
127+
try
128+
{
129+
SecretWithEncapsulation enc = gen.generateEncapsulated(pub);
130+
byte[] secret;
131+
synchronized (extractor)
132+
{
133+
secret = extractor.extractSecret(enc.getEncapsulation());
134+
}
135+
if (!Arrays.areEqual(secret, enc.getSecret()))
136+
{
137+
synchronized (mismatches)
138+
{
139+
mismatches[0]++;
140+
}
141+
}
142+
}
143+
catch (Throwable th)
144+
{
145+
failure[0] = th;
146+
}
147+
}
148+
}
149+
};
150+
threads[t].start();
151+
}
152+
for (int t = 0; t != THREADS; t++)
153+
{
154+
threads[t].join();
155+
}
156+
157+
assertNull("shared generator threw " + failure[0], failure[0]);
158+
assertEquals("shared generator produced unrecoverable encapsulations", 0, mismatches[0]);
159+
}
160+
}

0 commit comments

Comments
 (0)