Skip to content

Commit b1af66e

Browse files
committed
Settle on/be clearer and more consistent about not-thread-safety
1 parent d21a726 commit b1af66e

File tree

8 files changed

+30
-41
lines changed

8 files changed

+30
-41
lines changed

src/main/java/com/eatthepath/noise/NoiseHandshake.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
* type (i.e. one-way or interactive) and pass Noise transport messages between the initiator and responder as
3333
* needed.</p>
3434
*
35+
* <p>Noise handshake instances are stateful and are <em>not</em> thread-safe.</p>
36+
*
3537
* <h2>Interactive patterns</h2>
3638
*
3739
* <p>In the most common case, Noise handshakes implement a interactive pattern in which both parties will send and

src/main/java/com/eatthepath/noise/NoiseTransport.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package com.eatthepath.noise;
22

33
/**
4-
* A Noise transport is an interactive reader and writer of Noise transport messages. In the terminology of the Noise
4+
* <p>A Noise transport is an interactive reader and writer of Noise transport messages. In the terminology of the Noise
55
* Protocol Framework specification, a {@code NoiseTransport} instance encapsulates the two "cipher states" produced by
6-
* "splitting" a {@link NoiseHandshake}.
6+
* "splitting" a {@link NoiseHandshake}.</p>
7+
*
8+
* <p>Noise transport instances are stateful and are <em>not</em> thread-safe.</p>
79
*
810
* @see NoiseHandshake#toTransport()
911
*/

src/main/java/com/eatthepath/noise/NoiseTransportReader.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
import java.nio.ByteBuffer;
66

77
/**
8-
* A Noise transport reader decrypts Noise transport messages. In the terminology of the Noise
8+
* <p>A Noise transport reader decrypts Noise transport messages. In the terminology of the Noise
99
* Protocol Framework specification, a {@code NoiseTransportReader} instance encapsulates a "cipher state" produced by
10-
* "splitting" a {@link NoiseHandshake} instance.
10+
* "splitting" a {@link NoiseHandshake} instance.</p>
11+
*
12+
* <p>Noise transport reader instances are stateful and are <em>not</em> thread-safe.</p>
1113
*
1214
* @see NoiseHandshake#toTransportReader()
1315
* @see NoiseHandshake#toTransport()

src/main/java/com/eatthepath/noise/NoiseTransportWriter.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
import java.nio.ByteBuffer;
55

66
/**
7-
* A Noise transport writer encrypts Noise transport messages. In the terminology of the Noise Protocol Framework
7+
* <p>A Noise transport writer encrypts Noise transport messages. In the terminology of the Noise Protocol Framework
88
* specification, a {@code NoiseTransportWriter} instance encapsulates a "cipher state" produced by "splitting" a
9-
* {@link NoiseHandshake} instance.
9+
* {@link NoiseHandshake} instance.</p>
10+
*
11+
* <p>Noise transport writer instances are stateful and are <em>not</em> thread-safe.</p>
1012
*
1113
* @see NoiseHandshake#toTransportWriter()
1214
* @see NoiseHandshake#toTransport()

src/main/java/com/eatthepath/noise/component/AbstractNoiseCipher.java

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
11
package com.eatthepath.noise.component;
22

33
import javax.annotation.Nullable;
4-
import javax.annotation.concurrent.ThreadSafe;
54
import javax.crypto.*;
65
import java.nio.ByteBuffer;
76
import java.security.InvalidAlgorithmParameterException;
87
import java.security.InvalidKeyException;
98
import java.security.Key;
109
import java.security.spec.AlgorithmParameterSpec;
1110

12-
@ThreadSafe
1311
abstract class AbstractNoiseCipher implements NoiseCipher {
1412

13+
private final Cipher cipher;
14+
15+
AbstractNoiseCipher(final Cipher cipher) {
16+
this.cipher = cipher;
17+
}
18+
1519
@FunctionalInterface
1620
private interface CipherFinalizer<T> {
1721
T doFinal() throws IllegalBlockSizeException, BadPaddingException, ShortBufferException;
1822
}
1923

20-
protected abstract Cipher getCipher();
21-
2224
protected abstract AlgorithmParameterSpec getAlgorithmParameters(final long nonce);
2325

2426
@Override
@@ -28,8 +30,6 @@ public int encrypt(final Key key,
2830
final ByteBuffer plaintext,
2931
final ByteBuffer ciphertext) throws ShortBufferException {
3032

31-
final Cipher cipher = getCipher();
32-
3333
initCipher(cipher, Cipher.ENCRYPT_MODE, key, nonce);
3434

3535
if (associatedData != null) {
@@ -54,8 +54,6 @@ public int encrypt(final Key key,
5454
final byte[] ciphertext,
5555
final int ciphertextOffset) throws ShortBufferException {
5656

57-
final Cipher cipher = getCipher();
58-
5957
initCipher(cipher, Cipher.ENCRYPT_MODE, key, nonce);
6058

6159
if (associatedData != null) {
@@ -73,8 +71,6 @@ public int decrypt(final Key key,
7371
final ByteBuffer ciphertext,
7472
final ByteBuffer plaintext) throws AEADBadTagException, ShortBufferException {
7573

76-
final Cipher cipher = getCipher();
77-
7874
initCipher(cipher, Cipher.DECRYPT_MODE, key, nonce);
7975

8076
if (associatedData != null) {
@@ -99,8 +95,6 @@ public int decrypt(final Key key,
9995
final byte[] plaintext,
10096
final int plaintextOffset) throws AEADBadTagException, ShortBufferException {
10197

102-
final Cipher cipher = getCipher();
103-
10498
initCipher(cipher, Cipher.DECRYPT_MODE, key, nonce);
10599

106100
if (associatedData != null) {

src/main/java/com/eatthepath/noise/component/AesGcmCipher.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.eatthepath.noise.component;
22

3-
import javax.annotation.concurrent.ThreadSafe;
43
import javax.crypto.Cipher;
54
import javax.crypto.NoSuchPaddingException;
65
import javax.crypto.spec.GCMParameterSpec;
@@ -10,11 +9,13 @@
109
import java.security.NoSuchAlgorithmException;
1110
import java.security.spec.AlgorithmParameterSpec;
1211

13-
@ThreadSafe
1412
class AesGcmCipher extends AbstractNoiseCipher {
1513

16-
@Override
17-
protected Cipher getCipher() {
14+
AesGcmCipher() {
15+
super(getCipher());
16+
}
17+
18+
private static Cipher getCipher() {
1819
try {
1920
return Cipher.getInstance("AES/GCM/NoPadding");
2021
} catch (final NoSuchAlgorithmException | NoSuchPaddingException e) {

src/main/java/com/eatthepath/noise/component/ChaCha20Poly1305Cipher.java

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.eatthepath.noise.component;
22

3-
import javax.annotation.concurrent.ThreadSafe;
43
import javax.crypto.Cipher;
54
import javax.crypto.NoSuchPaddingException;
65
import javax.crypto.spec.IvParameterSpec;
@@ -11,23 +10,15 @@
1110
import java.security.NoSuchAlgorithmException;
1211
import java.security.spec.AlgorithmParameterSpec;
1312

14-
@ThreadSafe
1513
class ChaCha20Poly1305Cipher extends AbstractNoiseCipher {
1614

1715
private static final String ALGORITHM = "ChaCha20-Poly1305";
1816

1917
public ChaCha20Poly1305Cipher() throws NoSuchAlgorithmException {
20-
// Make sure that we can instantiate a cipher and fail fast if not
21-
try {
22-
Cipher.getInstance(ALGORITHM);
23-
} catch (final NoSuchPaddingException e) {
24-
// This should never happen since we're not specifying a padding
25-
throw new AssertionError("Padding not supported, but no padding specified", e);
26-
}
18+
super(getCipher());
2719
}
2820

29-
@Override
30-
protected Cipher getCipher() {
21+
private static Cipher getCipher() {
3122
try {
3223
return Cipher.getInstance(ALGORITHM);
3324
} catch (final NoSuchPaddingException e) {

src/main/java/com/eatthepath/noise/component/NoiseCipher.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,17 @@
11
package com.eatthepath.noise.component;
22

33
import javax.annotation.Nullable;
4-
import javax.annotation.concurrent.ThreadSafe;
54
import javax.crypto.AEADBadTagException;
65
import javax.crypto.ShortBufferException;
76
import java.nio.ByteBuffer;
87
import java.security.Key;
98
import java.security.NoSuchAlgorithmException;
109

1110
/**
12-
* <p>A Noise cipher is a stateless object that encrypts and decrypts data for use in a Noise protocol. Noise cipher
13-
* implementations must be thread-safe (i.e. calling encryption/decryption methods on different sets of data
14-
* concurrently and from different threads must have no adverse effect).</p>
15-
*
16-
* <p>Noise cipher implementations must operate in AEAD mode, produce a 16-byte AEAD tag when encrypting data, and
17-
* verify a 16-byte AEAD tag when decrypting data.</p>
11+
* A Noise cipher is a stateless object that encrypts and decrypts data for use in a Noise protocol. Noise cipher
12+
* implementations must operate in AEAD mode, produce a 16-byte AEAD tag when encrypting data, and verify a 16-byte
13+
* AEAD tag when decrypting data.
1814
*/
19-
@ThreadSafe
2015
public interface NoiseCipher {
2116

2217
/**

0 commit comments

Comments
 (0)