Skip to content

Commit 489ff7c

Browse files
committed
added some additional work from TLS fips implementation to nonce setting - relates to github #1950
1 parent c35119b commit 489ff7c

File tree

10 files changed

+78
-70
lines changed

10 files changed

+78
-70
lines changed

tls/src/main/java/org/bouncycastle/jsse/provider/GcmTls12NonceGeneratorUtil.java

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

tls/src/main/java/org/bouncycastle/jsse/provider/TlsNonceGeneratorFactory.java

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package org.bouncycastle.tls.crypto.impl;
2+
3+
import org.bouncycastle.tls.TlsFatalAlert;
4+
5+
public interface AEADNonceGenerator
6+
{
7+
public void generateNonce(byte[] nonce)
8+
throws TlsFatalAlert;
9+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package org.bouncycastle.tls.crypto.impl;
2+
3+
import org.bouncycastle.tls.crypto.TlsNonceGenerator;
4+
5+
public interface AEADNonceGeneratorFactory
6+
{
7+
AEADNonceGenerator create(byte[] baseNonce, int counterSizeInBits);
8+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.bouncycastle.tls.crypto.impl;
2+
3+
import java.security.AccessController;
4+
import java.security.PrivilegedAction;
5+
6+
final public class GcmTls12NonceGeneratorUtil
7+
{
8+
private static AEADNonceGeneratorFactory tlsNonceGeneratorFactory = null;
9+
10+
public static void setGcmTlsNonceGeneratorFactory(final AEADNonceGeneratorFactory factory)
11+
{
12+
tlsNonceGeneratorFactory = factory;
13+
}
14+
15+
public static boolean isGcmFipsNonceGeneratorFactorySet()
16+
{
17+
return tlsNonceGeneratorFactory != null;
18+
}
19+
20+
public static AEADNonceGenerator createGcmFipsNonceGenerator(final byte[] baseNonce, final int counterSizeInBits)
21+
{
22+
return tlsNonceGeneratorFactory != null
23+
? tlsNonceGeneratorFactory.create(baseNonce, counterSizeInBits)
24+
: null;
25+
}
26+
}

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

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@
1717
import org.bouncycastle.tls.crypto.TlsSecret;
1818
import org.bouncycastle.util.Arrays;
1919

20-
import static org.bouncycastle.jsse.provider.GcmTls12NonceGeneratorUtil.createGcmFipsNonceGenerator;
21-
import static org.bouncycastle.jsse.provider.GcmTls12NonceGeneratorUtil.isGcmFipsNonceGeneratorFactorySet;
22-
2320
/**
2421
* A generic TLS 1.2 AEAD cipher.
2522
*/
@@ -49,7 +46,7 @@ public final class TlsAEADCipher
4946

5047
private final boolean isTLSv13;
5148
private final int nonceMode;
52-
private final TlsNonceGenerator gcmFipsNonceGenerator;
49+
private final AEADNonceGenerator gcmFipsNonceGenerator;
5350

5451
public TlsAEADCipher(TlsCryptoParameters cryptoParams, TlsAEADCipherImpl encryptCipher, TlsAEADCipherImpl decryptCipher,
5552
int keySize, int macSize, int aeadType) throws IOException
@@ -130,7 +127,7 @@ public TlsAEADCipher(TlsCryptoParameters cryptoParams, TlsAEADCipherImpl encrypt
130127
throw new TlsFatalAlert(AlertDescription.internal_error);
131128
}
132129

133-
if (AEAD_GCM == aeadType && isGcmFipsNonceGeneratorFactorySet())
130+
if (AEAD_GCM == aeadType && GcmTls12NonceGeneratorUtil.isGcmFipsNonceGeneratorFactorySet())
134131
{
135132
final int nonceLength = fixed_iv_length + record_iv_length;
136133
final byte[] baseNonce = Arrays.copyOf(encryptNonce, nonceLength);
@@ -145,7 +142,7 @@ public TlsAEADCipher(TlsCryptoParameters cryptoParams, TlsAEADCipherImpl encrypt
145142
{
146143
counterSizeInBits = record_iv_length * 8; // 64
147144
}
148-
gcmFipsNonceGenerator = createGcmFipsNonceGenerator(baseNonce, counterSizeInBits);
145+
gcmFipsNonceGenerator = GcmTls12NonceGeneratorUtil.createGcmFipsNonceGenerator(baseNonce, counterSizeInBits);
149146
}
150147
else
151148
{
@@ -185,15 +182,14 @@ public TlsEncodeResult encodePlaintext(long seqNo, short contentType, ProtocolVe
185182
int headerAllocation, byte[] plaintext, int plaintextOffset, int plaintextLength) throws IOException
186183
{
187184
final int nonceSize = encryptNonce.length + record_iv_length;
188-
final byte[] nonce;
185+
final byte[] nonce = new byte[nonceSize];
189186

190187
if (null != gcmFipsNonceGenerator)
191188
{
192-
nonce = gcmFipsNonceGenerator.generateNonce(nonceSize);
189+
gcmFipsNonceGenerator.generateNonce(nonce);
193190
}
194191
else
195192
{
196-
nonce = new byte[nonceSize];
197193
switch (nonceMode)
198194
{
199195
case NONCE_RFC5288:

tls/src/test/java/org/bouncycastle/tls/test/AllTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package org.bouncycastle.tls.test;
22

3-
import org.bouncycastle.jsse.provider.GcmTls12NonceGeneratorUtil;
3+
import org.bouncycastle.tls.crypto.impl.GcmTls12NonceGeneratorUtil;
44
import org.bouncycastle.test.PrintTestResult;
55

66
import junit.extensions.TestSetup;
@@ -20,7 +20,7 @@ public static void main(String[] args)
2020

2121
public static Test suiteWithCustomNonceGeneratorForTls12() throws Exception
2222
{
23-
GcmTls12NonceGeneratorUtil.setGcmTlsNonceGeneratorFactory(TestTlsNonceGeneratorFactory.INSTANCE);
23+
GcmTls12NonceGeneratorUtil.setGcmTlsNonceGeneratorFactory(TestAEADGeneratorFactory.INSTANCE);
2424
return suite();
2525
}
2626

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.bouncycastle.tls.test;
2+
3+
import org.bouncycastle.tls.crypto.impl.AEADNonceGenerator;
4+
import org.bouncycastle.tls.crypto.impl.AEADNonceGeneratorFactory;
5+
6+
class TestAEADGeneratorFactory
7+
implements AEADNonceGeneratorFactory
8+
{
9+
public static final AEADNonceGeneratorFactory INSTANCE = new TestAEADGeneratorFactory();
10+
11+
private TestAEADGeneratorFactory()
12+
{
13+
// no op
14+
}
15+
16+
@Override
17+
public AEADNonceGenerator create(final byte[] baseNonce, final int counterSizeInBits)
18+
{
19+
return new TestAEADNonceGenerator(baseNonce, counterSizeInBits);
20+
}
21+
}

tls/src/test/java/org/bouncycastle/tls/test/TestNonceGenerator.java renamed to tls/src/test/java/org/bouncycastle/tls/test/TestAEADNonceGenerator.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package org.bouncycastle.tls.test;
22

33
import org.bouncycastle.tls.crypto.TlsNonceGenerator;
4+
import org.bouncycastle.tls.crypto.impl.AEADNonceGenerator;
45

56
import java.util.Arrays;
67

7-
class TestNonceGenerator implements TlsNonceGenerator
8+
class TestAEADNonceGenerator
9+
implements AEADNonceGenerator
810
{
911
private final byte[] baseNonce;
1012
private final long counterMask;
@@ -13,7 +15,7 @@ class TestNonceGenerator implements TlsNonceGenerator
1315
private long counterValue;
1416
private boolean counterExhausted;
1517

16-
TestNonceGenerator(final byte[] baseNonce, final int counterBits)
18+
TestAEADNonceGenerator(final byte[] baseNonce, final int counterBits)
1719
{
1820
this.baseNonce = Arrays.copyOf(baseNonce, baseNonce.length);
1921
this.counterMask = -1L >>> (64 - counterBits);
@@ -24,9 +26,9 @@ class TestNonceGenerator implements TlsNonceGenerator
2426
}
2527

2628
@Override
27-
public byte[] generateNonce(final int size)
29+
public void generateNonce(byte[] nonce)
2830
{
29-
if (size != baseNonce.length)
31+
if (nonce.length != baseNonce.length)
3032
{
3133
throw new IllegalArgumentException("requested length is not equal to the length of the base nonce.");
3234
}
@@ -36,7 +38,7 @@ public byte[] generateNonce(final int size)
3638
throw new IllegalStateException("TLS nonce generator exhausted");
3739
}
3840

39-
final byte[] nonce = Arrays.copyOf(baseNonce, baseNonce.length);
41+
System.arraycopy(baseNonce, 0, nonce, 0, baseNonce.length);
4042
final int offset = baseNonce.length - counterBytes;
4143

4244
for (int i = 0; i < counterBytes; i++)
@@ -45,7 +47,5 @@ public byte[] generateNonce(final int size)
4547
}
4648

4749
counterExhausted |= ((++counterValue & counterMask) == 0);
48-
49-
return nonce;
5050
}
5151
}

tls/src/test/java/org/bouncycastle/tls/test/TestTlsNonceGeneratorFactory.java

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

0 commit comments

Comments
 (0)