|
| 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.crypto.generators.FrodoKEMKeyPairGenerator; |
| 9 | +import org.bouncycastle.crypto.kems.FrodoKEMExtractor; |
| 10 | +import org.bouncycastle.crypto.kems.FrodoKEMGenerator; |
| 11 | +import org.bouncycastle.crypto.params.FrodoKEMKeyGenerationParameters; |
| 12 | +import org.bouncycastle.crypto.params.FrodoKEMParameters; |
| 13 | +import org.bouncycastle.crypto.params.FrodoKEMPrivateKeyParameters; |
| 14 | +import org.bouncycastle.crypto.params.FrodoKEMPublicKeyParameters; |
| 15 | +import org.bouncycastle.util.Arrays; |
| 16 | + |
| 17 | +/** |
| 18 | + * A FrodoKEMExtractor holds one FrodoKEMEngine 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 while squeezing}. The |
| 22 | + * digest is now built per call, as CMCEEngine's already was. |
| 23 | + * <p> |
| 24 | + * The generator side is exercised too, since FrodoKEMGenerator builds an engine per call but its |
| 25 | + * KEM keys and parameters are shared. |
| 26 | + */ |
| 27 | +public class FrodoKEMConcurrencyTest |
| 28 | + extends TestCase |
| 29 | +{ |
| 30 | + private static final int VECTORS = 16; |
| 31 | + private static final int THREADS = 4; |
| 32 | + |
| 33 | + public void testConcurrentExtraction() |
| 34 | + throws Exception |
| 35 | + { |
| 36 | + SecureRandom random = new SecureRandom(); |
| 37 | + |
| 38 | + FrodoKEMKeyPairGenerator kpg = new FrodoKEMKeyPairGenerator(); |
| 39 | + kpg.init(new FrodoKEMKeyGenerationParameters(random, FrodoKEMParameters.frodokem976shake)); |
| 40 | + AsymmetricCipherKeyPair kp = kpg.generateKeyPair(); |
| 41 | + |
| 42 | + final FrodoKEMPublicKeyParameters pub = (FrodoKEMPublicKeyParameters)kp.getPublic(); |
| 43 | + FrodoKEMPrivateKeyParameters priv = (FrodoKEMPrivateKeyParameters)kp.getPrivate(); |
| 44 | + |
| 45 | + final byte[][] encapsulations = new byte[VECTORS][]; |
| 46 | + final byte[][] expected = new byte[VECTORS][]; |
| 47 | + |
| 48 | + FrodoKEMGenerator gen = new FrodoKEMGenerator(random); |
| 49 | + for (int i = 0; i != VECTORS; i++) |
| 50 | + { |
| 51 | + SecretWithEncapsulation enc = gen.generateEncapsulated(pub); |
| 52 | + encapsulations[i] = enc.getEncapsulation(); |
| 53 | + expected[i] = enc.getSecret(); |
| 54 | + } |
| 55 | + |
| 56 | + // one extractor, shared across threads |
| 57 | + final FrodoKEMExtractor extractor = new FrodoKEMExtractor(priv); |
| 58 | + final Throwable[] failure = new Throwable[1]; |
| 59 | + final int[] mismatches = new int[1]; |
| 60 | + |
| 61 | + Thread[] threads = new Thread[THREADS]; |
| 62 | + for (int t = 0; t != THREADS; t++) |
| 63 | + { |
| 64 | + threads[t] = new Thread() |
| 65 | + { |
| 66 | + public void run() |
| 67 | + { |
| 68 | + for (int i = 0; i != VECTORS; i++) |
| 69 | + { |
| 70 | + try |
| 71 | + { |
| 72 | + byte[] secret = extractor.extractSecret(encapsulations[i]); |
| 73 | + if (!Arrays.areEqual(secret, expected[i])) |
| 74 | + { |
| 75 | + synchronized (mismatches) |
| 76 | + { |
| 77 | + mismatches[0]++; |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + catch (Throwable th) |
| 82 | + { |
| 83 | + failure[0] = th; |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + }; |
| 88 | + threads[t].start(); |
| 89 | + } |
| 90 | + for (int t = 0; t != THREADS; t++) |
| 91 | + { |
| 92 | + threads[t].join(); |
| 93 | + } |
| 94 | + |
| 95 | + assertNull("shared extractor threw " + failure[0], failure[0]); |
| 96 | + assertEquals("shared extractor produced wrong secrets", 0, mismatches[0]); |
| 97 | + } |
| 98 | + |
| 99 | + public void testConcurrentEncapsulation() |
| 100 | + throws Exception |
| 101 | + { |
| 102 | + SecureRandom random = new SecureRandom(); |
| 103 | + |
| 104 | + FrodoKEMKeyPairGenerator kpg = new FrodoKEMKeyPairGenerator(); |
| 105 | + kpg.init(new FrodoKEMKeyGenerationParameters(random, FrodoKEMParameters.frodokem976shake)); |
| 106 | + AsymmetricCipherKeyPair kp = kpg.generateKeyPair(); |
| 107 | + |
| 108 | + final FrodoKEMPublicKeyParameters pub = (FrodoKEMPublicKeyParameters)kp.getPublic(); |
| 109 | + final FrodoKEMExtractor extractor = |
| 110 | + new FrodoKEMExtractor((FrodoKEMPrivateKeyParameters)kp.getPrivate()); |
| 111 | + |
| 112 | + // one generator, shared across threads - each encapsulation must round trip |
| 113 | + final FrodoKEMGenerator gen = new FrodoKEMGenerator(random); |
| 114 | + final Throwable[] failure = new Throwable[1]; |
| 115 | + final int[] mismatches = new int[1]; |
| 116 | + |
| 117 | + Thread[] threads = new Thread[THREADS]; |
| 118 | + for (int t = 0; t != THREADS; t++) |
| 119 | + { |
| 120 | + threads[t] = new Thread() |
| 121 | + { |
| 122 | + public void run() |
| 123 | + { |
| 124 | + for (int i = 0; i != VECTORS; i++) |
| 125 | + { |
| 126 | + try |
| 127 | + { |
| 128 | + SecretWithEncapsulation enc = gen.generateEncapsulated(pub); |
| 129 | + byte[] secret; |
| 130 | + synchronized (extractor) |
| 131 | + { |
| 132 | + secret = extractor.extractSecret(enc.getEncapsulation()); |
| 133 | + } |
| 134 | + if (!Arrays.areEqual(secret, enc.getSecret())) |
| 135 | + { |
| 136 | + synchronized (mismatches) |
| 137 | + { |
| 138 | + mismatches[0]++; |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + catch (Throwable th) |
| 143 | + { |
| 144 | + failure[0] = th; |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + }; |
| 149 | + threads[t].start(); |
| 150 | + } |
| 151 | + for (int t = 0; t != THREADS; t++) |
| 152 | + { |
| 153 | + threads[t].join(); |
| 154 | + } |
| 155 | + |
| 156 | + assertNull("shared generator threw " + failure[0], failure[0]); |
| 157 | + assertEquals("shared generator produced unrecoverable encapsulations", 0, mismatches[0]); |
| 158 | + } |
| 159 | +} |
0 commit comments