Skip to content

Commit 68b9f5e

Browse files
committed
TLS: GCM nonce mechanism now needs custom JcaTlsCrypto
1 parent 6971a97 commit 68b9f5e

18 files changed

+221
-94
lines changed

tls/src/main/java/org/bouncycastle/tls/crypto/impl/GcmTls12NonceGeneratorUtil.java

Lines changed: 0 additions & 21 deletions
This file was deleted.

tls/src/main/java/org/bouncycastle/tls/crypto/impl/TlsAEADCipher.java

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,18 @@ public final class TlsAEADCipher
4545

4646
private final boolean isTLSv13;
4747
private final int nonceMode;
48-
private final AEADNonceGenerator gcmFipsNonceGenerator;
48+
private final AEADNonceGenerator nonceGenerator;
4949

50-
public TlsAEADCipher(TlsCryptoParameters cryptoParams, TlsAEADCipherImpl encryptCipher, TlsAEADCipherImpl decryptCipher,
51-
int keySize, int macSize, int aeadType) throws IOException
50+
/** @deprecated Use version with extra 'nonceGeneratorFactory' parameter */
51+
public TlsAEADCipher(TlsCryptoParameters cryptoParams, TlsAEADCipherImpl encryptCipher,
52+
TlsAEADCipherImpl decryptCipher, int keySize, int macSize, int aeadType) throws IOException
53+
{
54+
this(cryptoParams, encryptCipher, decryptCipher, keySize, macSize, aeadType, null);
55+
}
56+
57+
public TlsAEADCipher(TlsCryptoParameters cryptoParams, TlsAEADCipherImpl encryptCipher,
58+
TlsAEADCipherImpl decryptCipher, int keySize, int macSize, int aeadType,
59+
AEADNonceGeneratorFactory nonceGeneratorFactory) throws IOException
5260
{
5361
final SecurityParameters securityParameters = cryptoParams.getSecurityParametersHandshake();
5462
final ProtocolVersion negotiatedVersion = securityParameters.getNegotiatedVersion();
@@ -94,7 +102,7 @@ public TlsAEADCipher(TlsCryptoParameters cryptoParams, TlsAEADCipherImpl encrypt
94102
final boolean isServer = cryptoParams.isServer();
95103
if (isTLSv13)
96104
{
97-
gcmFipsNonceGenerator = null;
105+
nonceGenerator = null;
98106
rekeyCipher(securityParameters, decryptCipher, decryptNonce, !isServer);
99107
rekeyCipher(securityParameters, encryptCipher, encryptNonce, isServer);
100108
return;
@@ -126,7 +134,7 @@ public TlsAEADCipher(TlsCryptoParameters cryptoParams, TlsAEADCipherImpl encrypt
126134
throw new TlsFatalAlert(AlertDescription.internal_error);
127135
}
128136

129-
if (AEAD_GCM == aeadType && GcmTls12NonceGeneratorUtil.isGcmFipsNonceGeneratorFactorySet())
137+
if (AEAD_GCM == aeadType && nonceGeneratorFactory != null)
130138
{
131139
int nonceLength = fixed_iv_length + record_iv_length;
132140
byte[] baseNonce = Arrays.copyOf(encryptNonce, nonceLength);
@@ -141,12 +149,11 @@ public TlsAEADCipher(TlsCryptoParameters cryptoParams, TlsAEADCipherImpl encrypt
141149
{
142150
counterSizeInBits = record_iv_length * 8; // 64
143151
}
144-
gcmFipsNonceGenerator = GcmTls12NonceGeneratorUtil.createGcmFipsNonceGenerator(baseNonce,
145-
counterSizeInBits);
152+
nonceGenerator = nonceGeneratorFactory.create(baseNonce, counterSizeInBits);
146153
}
147154
else
148155
{
149-
gcmFipsNonceGenerator = null;
156+
nonceGenerator = null;
150157
}
151158
}
152159

@@ -183,9 +190,9 @@ public TlsEncodeResult encodePlaintext(long seqNo, short contentType, ProtocolVe
183190
{
184191
byte[] nonce = new byte[encryptNonce.length + record_iv_length];
185192

186-
if (null != gcmFipsNonceGenerator)
193+
if (null != nonceGenerator)
187194
{
188-
gcmFipsNonceGenerator.generateNonce(nonce);
195+
nonceGenerator.generateNonce(nonce);
189196
}
190197
else
191198
{

tls/src/main/java/org/bouncycastle/tls/crypto/impl/bc/BcTlsCrypto.java

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
import org.bouncycastle.tls.crypto.TlsSRP6VerifierGenerator;
6464
import org.bouncycastle.tls.crypto.TlsSRPConfig;
6565
import org.bouncycastle.tls.crypto.TlsSecret;
66+
import org.bouncycastle.tls.crypto.impl.AEADNonceGeneratorFactory;
6667
import org.bouncycastle.tls.crypto.impl.AbstractTlsCrypto;
6768
import org.bouncycastle.tls.crypto.impl.TlsAEADCipher;
6869
import org.bouncycastle.tls.crypto.impl.TlsBlockCipher;
@@ -594,7 +595,7 @@ protected BlockCipher createCBCBlockCipher(int encryptionAlgorithm)
594595
protected TlsCipher createChaCha20Poly1305(TlsCryptoParameters cryptoParams) throws IOException
595596
{
596597
return new TlsAEADCipher(cryptoParams, new BcChaCha20Poly1305(true), new BcChaCha20Poly1305(false), 32, 16,
597-
TlsAEADCipher.AEAD_CHACHA20_POLY1305);
598+
TlsAEADCipher.AEAD_CHACHA20_POLY1305, null);
598599
}
599600

600601
protected TlsAEADCipher createCipher_AES_CCM(TlsCryptoParameters cryptoParams, int cipherKeySize, int macSize)
@@ -603,7 +604,8 @@ protected TlsAEADCipher createCipher_AES_CCM(TlsCryptoParameters cryptoParams, i
603604
BcTlsAEADCipherImpl encrypt = new BcTlsAEADCipherImpl(createAEADBlockCipher_AES_CCM(), true);
604605
BcTlsAEADCipherImpl decrypt = new BcTlsAEADCipherImpl(createAEADBlockCipher_AES_CCM(), false);
605606

606-
return new TlsAEADCipher(cryptoParams, encrypt, decrypt, cipherKeySize, macSize, TlsAEADCipher.AEAD_CCM);
607+
return new TlsAEADCipher(cryptoParams, encrypt, decrypt, cipherKeySize, macSize, TlsAEADCipher.AEAD_CCM,
608+
null);
607609
}
608610

609611
protected TlsAEADCipher createCipher_AES_GCM(TlsCryptoParameters cryptoParams, int cipherKeySize, int macSize)
@@ -612,7 +614,8 @@ protected TlsAEADCipher createCipher_AES_GCM(TlsCryptoParameters cryptoParams, i
612614
BcTlsAEADCipherImpl encrypt = new BcTlsAEADCipherImpl(createAEADBlockCipher_AES_GCM(), true);
613615
BcTlsAEADCipherImpl decrypt = new BcTlsAEADCipherImpl(createAEADBlockCipher_AES_GCM(), false);
614616

615-
return new TlsAEADCipher(cryptoParams, encrypt, decrypt, cipherKeySize, macSize, TlsAEADCipher.AEAD_GCM);
617+
return new TlsAEADCipher(cryptoParams, encrypt, decrypt, cipherKeySize, macSize, TlsAEADCipher.AEAD_GCM,
618+
getGCMNonceGeneratorFactory());
616619
}
617620

618621
protected TlsAEADCipher createCipher_ARIA_GCM(TlsCryptoParameters cryptoParams, int cipherKeySize, int macSize)
@@ -621,7 +624,8 @@ protected TlsAEADCipher createCipher_ARIA_GCM(TlsCryptoParameters cryptoParams,
621624
BcTlsAEADCipherImpl encrypt = new BcTlsAEADCipherImpl(createAEADBlockCipher_ARIA_GCM(), true);
622625
BcTlsAEADCipherImpl decrypt = new BcTlsAEADCipherImpl(createAEADBlockCipher_ARIA_GCM(), false);
623626

624-
return new TlsAEADCipher(cryptoParams, encrypt, decrypt, cipherKeySize, macSize, TlsAEADCipher.AEAD_GCM);
627+
return new TlsAEADCipher(cryptoParams, encrypt, decrypt, cipherKeySize, macSize, TlsAEADCipher.AEAD_GCM,
628+
getGCMNonceGeneratorFactory());
625629
}
626630

627631
protected TlsAEADCipher createCipher_Camellia_GCM(TlsCryptoParameters cryptoParams, int cipherKeySize, int macSize)
@@ -630,7 +634,8 @@ protected TlsAEADCipher createCipher_Camellia_GCM(TlsCryptoParameters cryptoPara
630634
BcTlsAEADCipherImpl encrypt = new BcTlsAEADCipherImpl(createAEADBlockCipher_Camellia_GCM(), true);
631635
BcTlsAEADCipherImpl decrypt = new BcTlsAEADCipherImpl(createAEADBlockCipher_Camellia_GCM(), false);
632636

633-
return new TlsAEADCipher(cryptoParams, encrypt, decrypt, cipherKeySize, macSize, TlsAEADCipher.AEAD_GCM);
637+
return new TlsAEADCipher(cryptoParams, encrypt, decrypt, cipherKeySize, macSize, TlsAEADCipher.AEAD_GCM,
638+
getGCMNonceGeneratorFactory());
634639
}
635640

636641
protected TlsCipher createCipher_CBC(TlsCryptoParameters cryptoParams, int encryptionAlgorithm, int cipherKeySize,
@@ -651,7 +656,7 @@ protected TlsAEADCipher createCipher_SM4_CCM(TlsCryptoParameters cryptoParams)
651656
BcTlsAEADCipherImpl encrypt = new BcTlsAEADCipherImpl(createAEADBlockCipher_SM4_CCM(), true);
652657
BcTlsAEADCipherImpl decrypt = new BcTlsAEADCipherImpl(createAEADBlockCipher_SM4_CCM(), false);
653658

654-
return new TlsAEADCipher(cryptoParams, encrypt, decrypt, 16, 16, TlsAEADCipher.AEAD_CCM);
659+
return new TlsAEADCipher(cryptoParams, encrypt, decrypt, 16, 16, TlsAEADCipher.AEAD_CCM, null);
655660
}
656661

657662
protected TlsAEADCipher createCipher_SM4_GCM(TlsCryptoParameters cryptoParams)
@@ -660,7 +665,8 @@ protected TlsAEADCipher createCipher_SM4_GCM(TlsCryptoParameters cryptoParams)
660665
BcTlsAEADCipherImpl encrypt = new BcTlsAEADCipherImpl(createAEADBlockCipher_SM4_GCM(), true);
661666
BcTlsAEADCipherImpl decrypt = new BcTlsAEADCipherImpl(createAEADBlockCipher_SM4_GCM(), false);
662667

663-
return new TlsAEADCipher(cryptoParams, encrypt, decrypt, 16, 16, TlsAEADCipher.AEAD_GCM);
668+
return new TlsAEADCipher(cryptoParams, encrypt, decrypt, 16, 16, TlsAEADCipher.AEAD_GCM,
669+
getGCMNonceGeneratorFactory());
664670
}
665671

666672
protected TlsNullCipher createNullCipher(TlsCryptoParameters cryptoParams, int macAlgorithm)
@@ -741,6 +747,11 @@ protected AEADBlockCipher createAEADBlockCipher_SM4_GCM()
741747
return createGCMMode(createSM4Engine());
742748
}
743749

750+
protected AEADNonceGeneratorFactory getGCMNonceGeneratorFactory()
751+
{
752+
return null;
753+
}
754+
744755
public TlsHMAC createHMAC(int macAlgorithm)
745756
{
746757
switch (macAlgorithm)

tls/src/main/java/org/bouncycastle/tls/crypto/impl/jcajce/GCMUtil.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.security.PrivilegedExceptionAction;
77
import java.security.spec.AlgorithmParameterSpec;
88

9+
import org.bouncycastle.tls.crypto.impl.AEADNonceGeneratorFactory;
910
import org.bouncycastle.util.Integers;
1011

1112
class GCMUtil
@@ -30,6 +31,11 @@ public AlgorithmParameterSpec run()
3031
});
3132
}
3233

34+
static AEADNonceGeneratorFactory getDefaultNonceGeneratorFactory()
35+
{
36+
return null;
37+
}
38+
3339
static boolean isGCMParameterSpecAvailable()
3440
{
3541
return gcmParameterSpec != null;

tls/src/main/java/org/bouncycastle/tls/crypto/impl/jcajce/JcaTlsCrypto.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
import org.bouncycastle.tls.crypto.TlsSecret;
5959
import org.bouncycastle.tls.crypto.TlsStreamSigner;
6060
import org.bouncycastle.tls.crypto.TlsStreamVerifier;
61+
import org.bouncycastle.tls.crypto.impl.AEADNonceGeneratorFactory;
6162
import org.bouncycastle.tls.crypto.impl.AbstractTlsCrypto;
6263
import org.bouncycastle.tls.crypto.impl.TlsAEADCipher;
6364
import org.bouncycastle.tls.crypto.impl.TlsAEADCipherImpl;
@@ -1226,31 +1227,31 @@ private TlsCipher createChaCha20Poly1305(TlsCryptoParameters cryptoParams)
12261227
throws IOException, GeneralSecurityException
12271228
{
12281229
return new TlsAEADCipher(cryptoParams, new JceChaCha20Poly1305(this, helper, true),
1229-
new JceChaCha20Poly1305(this, helper, false), 32, 16, TlsAEADCipher.AEAD_CHACHA20_POLY1305);
1230+
new JceChaCha20Poly1305(this, helper, false), 32, 16, TlsAEADCipher.AEAD_CHACHA20_POLY1305, null);
12301231
}
12311232

12321233
private TlsAEADCipher createCipher_AES_CCM(TlsCryptoParameters cryptoParams, int cipherKeySize, int macSize)
12331234
throws IOException, GeneralSecurityException
12341235
{
12351236
return new TlsAEADCipher(cryptoParams, createAEADCipher("AES/CCM/NoPadding", "AES", cipherKeySize, true),
12361237
createAEADCipher("AES/CCM/NoPadding", "AES", cipherKeySize, false), cipherKeySize, macSize,
1237-
TlsAEADCipher.AEAD_CCM);
1238+
TlsAEADCipher.AEAD_CCM, null);
12381239
}
12391240

12401241
private TlsAEADCipher createCipher_AES_GCM(TlsCryptoParameters cryptoParams, int cipherKeySize, int macSize)
12411242
throws IOException, GeneralSecurityException
12421243
{
12431244
return new TlsAEADCipher(cryptoParams, createAEADCipher("AES/GCM/NoPadding", "AES", cipherKeySize, true),
12441245
createAEADCipher("AES/GCM/NoPadding", "AES", cipherKeySize, false), cipherKeySize, macSize,
1245-
TlsAEADCipher.AEAD_GCM);
1246+
TlsAEADCipher.AEAD_GCM, getGCMNonceGeneratorFactory());
12461247
}
12471248

12481249
private TlsAEADCipher createCipher_ARIA_GCM(TlsCryptoParameters cryptoParams, int cipherKeySize, int macSize)
12491250
throws IOException, GeneralSecurityException
12501251
{
12511252
return new TlsAEADCipher(cryptoParams, createAEADCipher("ARIA/GCM/NoPadding", "ARIA", cipherKeySize, true),
12521253
createAEADCipher("ARIA/GCM/NoPadding", "ARIA", cipherKeySize, false), cipherKeySize, macSize,
1253-
TlsAEADCipher.AEAD_GCM);
1254+
TlsAEADCipher.AEAD_GCM, getGCMNonceGeneratorFactory());
12541255
}
12551256

12561257
private TlsAEADCipher createCipher_Camellia_GCM(TlsCryptoParameters cryptoParams, int cipherKeySize, int macSize)
@@ -1259,7 +1260,7 @@ private TlsAEADCipher createCipher_Camellia_GCM(TlsCryptoParameters cryptoParams
12591260
return new TlsAEADCipher(cryptoParams,
12601261
createAEADCipher("Camellia/GCM/NoPadding", "Camellia", cipherKeySize, true),
12611262
createAEADCipher("Camellia/GCM/NoPadding", "Camellia", cipherKeySize, false), cipherKeySize, macSize,
1262-
TlsAEADCipher.AEAD_GCM);
1263+
TlsAEADCipher.AEAD_GCM, getGCMNonceGeneratorFactory());
12631264
}
12641265

12651266
protected TlsCipher createCipher_CBC(TlsCryptoParameters cryptoParams, String algorithm, int cipherKeySize,
@@ -1280,7 +1281,7 @@ private TlsAEADCipher createCipher_SM4_CCM(TlsCryptoParameters cryptoParams)
12801281
int cipherKeySize = 16, macSize = 16;
12811282
return new TlsAEADCipher(cryptoParams, createAEADCipher("SM4/CCM/NoPadding", "SM4", cipherKeySize, true),
12821283
createAEADCipher("SM4/CCM/NoPadding", "SM4", cipherKeySize, false), cipherKeySize, macSize,
1283-
TlsAEADCipher.AEAD_CCM);
1284+
TlsAEADCipher.AEAD_CCM, null);
12841285
}
12851286

12861287
private TlsAEADCipher createCipher_SM4_GCM(TlsCryptoParameters cryptoParams)
@@ -1289,7 +1290,12 @@ private TlsAEADCipher createCipher_SM4_GCM(TlsCryptoParameters cryptoParams)
12891290
int cipherKeySize = 16, macSize = 16;
12901291
return new TlsAEADCipher(cryptoParams, createAEADCipher("SM4/GCM/NoPadding", "SM4", cipherKeySize, true),
12911292
createAEADCipher("SM4/GCM/NoPadding", "SM4", cipherKeySize, false), cipherKeySize, macSize,
1292-
TlsAEADCipher.AEAD_GCM);
1293+
TlsAEADCipher.AEAD_GCM, getGCMNonceGeneratorFactory());
1294+
}
1295+
1296+
protected AEADNonceGeneratorFactory getGCMNonceGeneratorFactory()
1297+
{
1298+
return GCMUtil.getDefaultNonceGeneratorFactory();
12931299
}
12941300

12951301
String getDigestName(int cryptoHashAlgorithm)

tls/src/test/java/org/bouncycastle/jsse/provider/test/CipherSuitesEngineTestCase.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,6 @@ public CipherSuitesEngineTestCase(CipherSuitesTestConfig config)
4444
this.config = config;
4545
}
4646

47-
protected void setUp()
48-
{
49-
if (config != null)
50-
{
51-
ProviderUtils.setupHighPriority(config.fips);
52-
}
53-
}
54-
5547
public void testDummy()
5648
{
5749
// Avoid "No tests found" warning from junit

tls/src/test/java/org/bouncycastle/jsse/provider/test/CipherSuitesEngineTestSuite.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import org.junit.Assert;
1111

12+
import junit.extensions.TestSetup;
1213
import junit.framework.Test;
1314
import junit.framework.TestSuite;
1415

@@ -23,7 +24,9 @@ public CipherSuitesEngineTestSuite()
2324
public static Test suite()
2425
throws Exception
2526
{
26-
return createSuite(new CipherSuitesEngineTestSuite(), null, false, new CipherSuitesFilter()
27+
ProviderUtils.setupHighPriority(false);
28+
29+
TestSuite suite = createSuite(new CipherSuitesEngineTestSuite(), null, false, new CipherSuitesFilter()
2730
{
2831
public boolean isIgnored(String cipherSuite)
2932
{
@@ -40,14 +43,20 @@ public boolean isPermitted(String cipherSuite)
4043
return true;
4144
}
4245
});
46+
47+
return new TestSetup(suite)
48+
{
49+
@Override
50+
protected void setUp() throws Exception
51+
{
52+
ProviderUtils.setupHighPriority(false);
53+
}
54+
};
4355
}
4456

45-
static Test createSuite(TestSuite testSuite, String category, boolean fips, CipherSuitesFilter filter)
57+
static TestSuite createSuite(TestSuite testSuite, String category, boolean fips, CipherSuitesFilter filter)
4658
throws Exception
4759
{
48-
// TODO Consider configuring BCJSSE with explicit crypto provider (maybe only when in fips mode?)
49-
ProviderUtils.setupHighPriority(fips);
50-
5160
char[] serverPassword = "serverPassword".toCharArray();
5261

5362
KeyPair caKeyPairDSA = TestUtils.generateDSAKeyPair();
@@ -126,7 +135,6 @@ static Test createSuite(TestSuite testSuite, String category, boolean fips, Ciph
126135
config.category = category;
127136
config.cipherSuite = cipherSuite;
128137
config.clientTrustStore = ts;
129-
config.fips = fips;
130138
config.protocol = protocol;
131139
config.serverKeyStore = ks;
132140
config.serverPassword = serverPassword;

tls/src/test/java/org/bouncycastle/jsse/provider/test/CipherSuitesTestCase.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,6 @@ public CipherSuitesTestCase(CipherSuitesTestConfig config)
4747
this.config = config;
4848
}
4949

50-
protected void setUp()
51-
{
52-
if (config != null)
53-
{
54-
ProviderUtils.setupHighPriority(config.fips);
55-
}
56-
}
57-
5850
public void testDummy()
5951
{
6052
// Avoid "No tests found" warning from junit

tls/src/test/java/org/bouncycastle/jsse/provider/test/CipherSuitesTestConfig.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ public class CipherSuitesTestConfig
77
public String category = null;
88
public String cipherSuite = null;
99
public KeyStore clientTrustStore = null;
10-
public boolean fips = false;
1110
public String protocol = null;
1211
public KeyStore serverKeyStore = null;
1312
public char[] serverPassword = null;

0 commit comments

Comments
 (0)