-
Notifications
You must be signed in to change notification settings - Fork 781
Add double-ratchet encryption for Nostr relay DMs #697
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mmalmi
wants to merge
5
commits into
permissionlesstech:main
Choose a base branch
from
mmalmi:codex/nostr-double-ratchet
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
fdc491c
Add Nostr double-ratchet DMs
68167a1
Update Android NdrFfi artifacts to 0.0.100
e857710
Update ndr-ffi Android artifacts to 0.0.104
9a1710b
Update ndr-ffi Android artifacts to 0.0.124
a5e4052
Update ndr-ffi Android artifacts to 0.0.135
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
app/src/main/java/com/bitchat/android/mesh/BlePacketBudget.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package com.bitchat.android.mesh | ||
|
|
||
| object BlePacketBudget { | ||
| private const val ATT_PAYLOAD_OVERHEAD_BYTES = 3 | ||
| private const val DEFAULT_PACKET_LIMIT_BYTES = 182 | ||
| private const val MIN_PACKET_LIMIT_BYTES = 20 | ||
|
|
||
| fun packetLimitBytesForMtu(mtu: Int?): Int { | ||
| val payloadBytes = (mtu ?: (DEFAULT_PACKET_LIMIT_BYTES + ATT_PAYLOAD_OVERHEAD_BYTES)) - | ||
| ATT_PAYLOAD_OVERHEAD_BYTES | ||
| return payloadBytes.coerceAtLeast(MIN_PACKET_LIMIT_BYTES) | ||
| } | ||
| } |
94 changes: 94 additions & 0 deletions
94
app/src/main/java/com/bitchat/android/mesh/BleWriteAccumulator.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| package com.bitchat.android.mesh | ||
|
|
||
| import com.bitchat.android.protocol.BitchatPacket | ||
| import java.util.concurrent.ConcurrentHashMap | ||
|
|
||
| /** | ||
| * Reassembles characteristic writes that arrive in multiple offset chunks. | ||
| * | ||
| * CoreBluetooth may split a single packet across multiple writes when acting as | ||
| * the central. Android's GATT server callback receives those chunks one by one, | ||
| * so we keep a per-device sparse buffer and only hand the packet upstream once | ||
| * the accumulated bytes decode successfully. | ||
| */ | ||
| class BleWriteAccumulator { | ||
|
|
||
| private data class PendingWrite( | ||
| val buffer: ByteArray, | ||
| val receivedRanges: List<IntRange> | ||
| ) | ||
|
|
||
| private val pendingWrites = ConcurrentHashMap<String, PendingWrite>() | ||
|
|
||
| fun append(deviceAddress: String, offset: Int, chunk: ByteArray): BitchatPacket? { | ||
| if (chunk.isEmpty()) { | ||
| return null | ||
| } | ||
|
|
||
| val current = pendingWrites[deviceAddress] | ||
| val existing = if (offset == 0 && current?.receivedRanges?.any { it.first == 0 } == true) { | ||
| null | ||
| } else { | ||
| current | ||
| } | ||
| val end = offset + chunk.size | ||
| val existingBuffer = existing?.buffer ?: ByteArray(0) | ||
| val combined = if (existingBuffer.size >= end) { | ||
| existingBuffer.copyOf() | ||
| } else { | ||
| existingBuffer.copyOf(end) | ||
| } | ||
| chunk.copyInto(combined, destinationOffset = offset) | ||
| val mergedRanges = mergeRanges(existing?.receivedRanges.orEmpty(), IntRange(offset, end - 1)) | ||
| val pendingWrite = PendingWrite(combined, mergedRanges) | ||
| pendingWrites[deviceAddress] = pendingWrite | ||
|
|
||
| if (!isContiguousFromStart(pendingWrite)) { | ||
| return null | ||
| } | ||
|
|
||
| val packet = BitchatPacket.fromBinaryData(combined) ?: return null | ||
| val canonicalEncoding = packet.toBinaryData() ?: return null | ||
| if (!canonicalEncoding.contentEquals(combined)) { | ||
| return null | ||
| } | ||
| pendingWrites.remove(deviceAddress) | ||
| return packet | ||
| } | ||
|
|
||
| fun clear(deviceAddress: String) { | ||
| pendingWrites.remove(deviceAddress) | ||
| } | ||
|
|
||
| fun clearAll() { | ||
| pendingWrites.clear() | ||
| } | ||
|
|
||
| private fun mergeRanges(existing: List<IntRange>, next: IntRange): List<IntRange> { | ||
| val sorted = buildList { | ||
| addAll(existing) | ||
| add(next) | ||
| }.sortedBy { it.first } | ||
| if (sorted.isEmpty()) { | ||
| return emptyList() | ||
| } | ||
|
|
||
| val merged = mutableListOf<IntRange>() | ||
| var current = sorted.first() | ||
| for (candidate in sorted.drop(1)) { | ||
| current = if (candidate.first <= current.last + 1) { | ||
| current.first..maxOf(current.last, candidate.last) | ||
| } else { | ||
| merged.add(current) | ||
| candidate | ||
| } | ||
| } | ||
| merged.add(current) | ||
| return merged | ||
| } | ||
|
|
||
| private fun isContiguousFromStart(pendingWrite: PendingWrite): Boolean { | ||
| val onlyRange = pendingWrite.receivedRanges.singleOrNull() ?: return false | ||
| return onlyRange.first == 0 && onlyRange.last + 1 == pendingWrite.buffer.size | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The accumulator keeps the previous buffer length even when a fresh write starts at
offset == 0, so after any truncated larger packet, later smaller packets from the same device can never satisfyisContiguousFromStart(onlyRange.last + 1will stay smaller than the stale buffer size). In practice this can permanently drop subsequent messages on that connection until disconnect/clear, because the pending state is never reinitialized for a new packet boundary.Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 16d7dd8. A fresh write at offset 0 now starts a new accumulator when the previous pending write already had a zero-based range, and there is a regression test for a truncated packet followed by a smaller complete packet.