From 651aeb48f9b1c6fef369f692f8adee2bf998e8ad Mon Sep 17 00:00:00 2001 From: Thomas <3169-thomas@users.noreply.code.briarproject.org> Date: Tue, 8 Nov 2022 19:49:29 +0100 Subject: [PATCH 1/2] Clarifying comments --- .../bramble/crypto/StreamEncrypterImpl.java | 11 +++++++---- .../bramble/sync/SyncRecordWriterImpl.java | 4 ++++ .../bramble/transport/StreamWriterImpl.java | 5 +++-- .../bramble/transport/TransportKeyManagerImpl.java | 3 +++ 4 files changed, 17 insertions(+), 6 deletions(-) diff --git a/bramble-core/src/main/java/org/briarproject/bramble/crypto/StreamEncrypterImpl.java b/bramble-core/src/main/java/org/briarproject/bramble/crypto/StreamEncrypterImpl.java index 55224c729..55a4d0c6f 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/crypto/StreamEncrypterImpl.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/crypto/StreamEncrypterImpl.java @@ -25,6 +25,9 @@ import static org.briarproject.bramble.api.transport.TransportConstants.STREAM_H import static org.briarproject.bramble.util.ByteUtils.INT_16_BYTES; import static org.briarproject.bramble.util.ByteUtils.INT_64_BYTES; +/** + * See the BTP-Specs to understand whats going on here + */ @NotThreadSafe @NotNullByDefault class StreamEncrypterImpl implements StreamEncrypter { @@ -81,9 +84,9 @@ class StreamEncrypterImpl implements StreamEncrypter { FrameEncoder.encodeNonce(frameNonce, frameNumber, true); try { cipher.init(true, frameKey, frameNonce); - int encrypted = cipher.process(frameHeader, 0, + int encryptedLength = cipher.process(frameHeader, 0, FRAME_HEADER_PLAINTEXT_LENGTH, frameCiphertext, 0); - if (encrypted != FRAME_HEADER_LENGTH) throw new RuntimeException(); + if (encryptedLength != FRAME_HEADER_LENGTH) throw new RuntimeException(); } catch (GeneralSecurityException badCipher) { throw new RuntimeException(badCipher); } @@ -129,10 +132,10 @@ class StreamEncrypterImpl implements StreamEncrypter { // Encrypt and authenticate the stream header key try { cipher.init(true, streamHeaderKey, streamHeaderNonce); - int encrypted = cipher.process(streamHeaderPlaintext, 0, + int encryptedLength = cipher.process(streamHeaderPlaintext, 0, STREAM_HEADER_PLAINTEXT_LENGTH, streamHeaderCiphertext, STREAM_HEADER_NONCE_LENGTH); - if (encrypted != STREAM_HEADER_PLAINTEXT_LENGTH + MAC_LENGTH) + if (encryptedLength != STREAM_HEADER_PLAINTEXT_LENGTH + MAC_LENGTH) throw new RuntimeException(); } catch (GeneralSecurityException badCipher) { throw new RuntimeException(badCipher); diff --git a/bramble-core/src/main/java/org/briarproject/bramble/sync/SyncRecordWriterImpl.java b/bramble-core/src/main/java/org/briarproject/bramble/sync/SyncRecordWriterImpl.java index 2079198b3..7b228e9f4 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/sync/SyncRecordWriterImpl.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/sync/SyncRecordWriterImpl.java @@ -26,6 +26,10 @@ import static org.briarproject.bramble.api.sync.RecordTypes.REQUEST; import static org.briarproject.bramble.api.sync.RecordTypes.VERSIONS; import static org.briarproject.bramble.api.sync.SyncConstants.PROTOCOL_VERSION; +/** + * see the BSP-Specs + */ + @NotThreadSafe @NotNullByDefault class SyncRecordWriterImpl implements SyncRecordWriter { diff --git a/bramble-core/src/main/java/org/briarproject/bramble/transport/StreamWriterImpl.java b/bramble-core/src/main/java/org/briarproject/bramble/transport/StreamWriterImpl.java index 0b4e3be8f..66bf8a568 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/transport/StreamWriterImpl.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/transport/StreamWriterImpl.java @@ -22,7 +22,7 @@ class StreamWriterImpl extends OutputStream implements StreamWriter { private final StreamEncrypter encrypter; private final byte[] payload; - + ///@var length Stores how long the content that is currently in the buffer "payload" is. private int length = 0; StreamWriterImpl(StreamEncrypter encrypter) { @@ -68,7 +68,8 @@ class StreamWriterImpl extends OutputStream implements StreamWriter { @Override public void write(byte[] b, int off, int len) throws IOException { - int available = payload.length - length; + //Frame Segmentation + int available = payload.length - length; // available = How much free space ther is at the end of the buffer "payload" while (available <= len) { System.arraycopy(b, off, payload, length, available); length += available; diff --git a/bramble-core/src/main/java/org/briarproject/bramble/transport/TransportKeyManagerImpl.java b/bramble-core/src/main/java/org/briarproject/bramble/transport/TransportKeyManagerImpl.java index 788d51d3c..e5d304e0a 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/transport/TransportKeyManagerImpl.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/transport/TransportKeyManagerImpl.java @@ -63,6 +63,9 @@ class TransportKeyManagerImpl implements TransportKeyManager { @GuardedBy("lock") private final Map keys = new HashMap<>(); + /** + * List of all Tags + */ @GuardedBy("lock") private final Map inContexts = new HashMap<>(); @GuardedBy("lock") -- GitLab From e248419dcaff17a18b7047137352a35ae9c44d8a Mon Sep 17 00:00:00 2001 From: Thomas <3169-thomas@users.noreply.code.briarproject.org> Date: Tue, 8 Nov 2022 19:18:05 +0000 Subject: [PATCH 2/2] Address comments --- .../org/briarproject/bramble/crypto/StreamEncrypterImpl.java | 3 ++- .../org/briarproject/bramble/sync/SyncRecordWriterImpl.java | 1 + .../org/briarproject/bramble/transport/StreamWriterImpl.java | 5 +++-- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/bramble-core/src/main/java/org/briarproject/bramble/crypto/StreamEncrypterImpl.java b/bramble-core/src/main/java/org/briarproject/bramble/crypto/StreamEncrypterImpl.java index 55a4d0c6f..5425c9486 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/crypto/StreamEncrypterImpl.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/crypto/StreamEncrypterImpl.java @@ -27,6 +27,7 @@ import static org.briarproject.bramble.util.ByteUtils.INT_64_BYTES; /** * See the BTP-Specs to understand whats going on here + * https://code.briarproject.org/briar/briar-spec/blob/master/protocols/BTP.md */ @NotThreadSafe @NotNullByDefault @@ -152,4 +153,4 @@ class StreamEncrypterImpl implements StreamEncrypter { if (writeStreamHeader) writeStreamHeader(); out.flush(); } -} \ No newline at end of file +} diff --git a/bramble-core/src/main/java/org/briarproject/bramble/sync/SyncRecordWriterImpl.java b/bramble-core/src/main/java/org/briarproject/bramble/sync/SyncRecordWriterImpl.java index 7b228e9f4..4e665d216 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/sync/SyncRecordWriterImpl.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/sync/SyncRecordWriterImpl.java @@ -28,6 +28,7 @@ import static org.briarproject.bramble.api.sync.SyncConstants.PROTOCOL_VERSION; /** * see the BSP-Specs + * https://code.briarproject.org/briar/briar-spec/blob/master/protocols/BSP.md */ @NotThreadSafe diff --git a/bramble-core/src/main/java/org/briarproject/bramble/transport/StreamWriterImpl.java b/bramble-core/src/main/java/org/briarproject/bramble/transport/StreamWriterImpl.java index 66bf8a568..a88a76e5d 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/transport/StreamWriterImpl.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/transport/StreamWriterImpl.java @@ -22,7 +22,7 @@ class StreamWriterImpl extends OutputStream implements StreamWriter { private final StreamEncrypter encrypter; private final byte[] payload; - ///@var length Stores how long the content that is currently in the buffer "payload" is. + /// length Stores how long the content that is currently in the buffer "payload" is. private int length = 0; StreamWriterImpl(StreamEncrypter encrypter) { @@ -69,7 +69,8 @@ class StreamWriterImpl extends OutputStream implements StreamWriter { @Override public void write(byte[] b, int off, int len) throws IOException { //Frame Segmentation - int available = payload.length - length; // available = How much free space ther is at the end of the buffer "payload" + // available = How much free space ther is at the end of the buffer "payload" + int available = payload.length - length; while (available <= len) { System.arraycopy(b, off, payload, length, available); length += available; -- GitLab