Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
6 changes: 4 additions & 2 deletions TESTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@
- [BitString concatenation with shifting](https://github.com/andreypfau/ton-kotlin/blob/363504ec96e821d4178dc09a2234377fd02808e9/ton-bitstring/src/commonTest/kotlin/org/ton/bitstring/BitStringTest.kt#L57)
- [BitString concatenation with double-shifting](https://github.com/andreypfau/ton-kotlin/blob/363504ec96e821d4178dc09a2234377fd02808e9/ton-bitstring/src/commonTest/kotlin/org/ton/bitstring/BitStringTest.kt#L68)
- [BitString.toString() on a zero number](https://github.com/andreypfau/ton-kotlin/blob/363504ec96e821d4178dc09a2234377fd02808e9/ton-bitstring/src/commonTest/kotlin/org/ton/bitstring/BitStringTest.kt#L107)
- [check BitString.size in `0..1023`](https://github.com/andreypfau/ton-kotlin/blob/3b02ad6c729e14fff8c023801062f9505cc6ed4a/ton-bitstring/src/commonMain/kotlin/org/ton/bitstring/ByteBackedBitString.kt#L167)
- [check BitString.size in
`0..1023`](https://github.com/andreypfau/ton-kotlin/blob/3b02ad6c729e14fff8c023801062f9505cc6ed4a/ton-bitstring/src/commonMain/kotlin/org/ton/bitstring/ByteBackedBitString.kt#L167)
- [BitString assertions for all tests](https://github.com/andreypfau/ton-kotlin/blob/363504ec96e821d4178dc09a2234377fd02808e9/ton-bitstring/src/commonTest/kotlin/org/ton/bitstring/BitStringTest.kt#L114)
- [from binary == from hex](https://github.com/andreypfau/ton-kotlin/blob/363504ec96e821d4178dc09a2234377fd02808e9/ton-bitstring/src/commonTest/kotlin/org/ton/bitstring/BitStringTest.kt#L119)
- [from binary toString() == from hex toString()](https://github.com/andreypfau/ton-kotlin/blob/363504ec96e821d4178dc09a2234377fd02808e9/ton-bitstring/src/commonTest/kotlin/org/ton/bitstring/BitStringTest.kt#L121)
Expand Down Expand Up @@ -99,7 +100,8 @@
- [check Bag-of-Cells (de)serialization equals](https://github.com/andreypfau/ton-kotlin/blob/addf79aeed62e87da74049aa1e720f96a791edde/ton-boc/src/jvmTest/kotlin/BagOfCellsTest.kt#L12)
- check Bag-of-Cells crc32c checksum calculation
- [check that cell offset indexes greater than previous](https://github.com/andreypfau/ton-kotlin/blob/addf79aeed62e87da74049aa1e720f96a791edde/ton-boc/src/commonMain/kotlin/org/ton/boc/BagOfCellsUtils.kt#L71)
- [check cell references count in `0..4`](https://github.com/andreypfau/ton-kotlin/blob/addf79aeed62e87da74049aa1e720f96a791edde/ton-boc/src/commonMain/kotlin/org/ton/boc/BagOfCellsUtils.kt#L95)
- [check cell references count in
`0..4`](https://github.com/andreypfau/ton-kotlin/blob/addf79aeed62e87da74049aa1e720f96a791edde/ton-boc/src/commonMain/kotlin/org/ton/boc/BagOfCellsUtils.kt#L95)
- [check invalid cell reference indexes](https://github.com/andreypfau/ton-kotlin/blob/addf79aeed62e87da74049aa1e720f96a791edde/ton-boc/src/commonMain/kotlin/org/ton/boc/BagOfCellsUtils.kt#L121)
- [check cell depth less than 1024](https://github.com/andreypfau/ton-kotlin/blob/6e2f83fc80f19466c84289c40e6de396b7320752/ton-boc/src/commonMain/kotlin/org/ton/boc/CachedBagOfCells.kt#L62)
- [check cell order algorithm. TODO: make unit-tests for illegal cell order](https://github.com/andreypfau/ton-kotlin/blob/6e2f83fc80f19466c84289c40e6de396b7320752/ton-boc/src/commonMain/kotlin/org/ton/boc/CachedBagOfCells.kt#L89)
Expand Down
13 changes: 13 additions & 0 deletions adnl/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,18 @@ kotlin {
implementation(libs.ktor.network)
}
}

all {
if (name.endsWith("Main")) {
val suffix = if (name.startsWith("common")) "" else "@${name.removeSuffix("Main")}"
kotlin.srcDir("src$suffix")
resources.srcDir("resources$suffix")
}
if (name.endsWith("Test")) {
val suffix = if (name.startsWith("common")) "" else "@${name.removeSuffix("Test")}"
kotlin.srcDir("test$suffix")
resources.srcDir("testResources$suffix")
}
}
}
}
28 changes: 14 additions & 14 deletions adnl/src/connection/AdnlConnection.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package org.ton.adnl.connection

import io.github.andreypfau.kotlinx.crypto.aes.AES
import io.github.andreypfau.kotlinx.crypto.cipher.CTRBlockCipher
import io.github.andreypfau.kotlinx.crypto.sha2.SHA256
import io.ktor.utils.io.*
import io.ktor.utils.io.core.*
import io.ktor.utils.io.errors.*
Expand All @@ -13,7 +10,10 @@ import kotlinx.datetime.Clock
import kotlinx.datetime.Instant
import org.ton.adnl.network.TcpClient
import org.ton.api.liteserver.LiteServerDesc
import org.ton.crypto.SecureRandom
import org.ton.kotlin.crypto.AesCtr
import org.ton.kotlin.crypto.SecureRandom
import org.ton.kotlin.crypto.Sha256
import org.ton.kotlin.crypto.sha256
import kotlin.coroutines.CoroutineContext
import kotlin.time.Duration
import kotlin.time.Duration.Companion.ZERO
Expand Down Expand Up @@ -67,7 +67,7 @@ public class AdnlConnection(

connection.output.writePacket {
writeFully(liteServerDesc.id.toAdnlIdShort().id.toByteArray())
writeFully(liteServerDesc.id.encrypt(nonce))
writeFully(liteServerDesc.id.encryptToByteArray(nonce))
}
connection.output.flush()

Expand Down Expand Up @@ -113,7 +113,7 @@ public class AdnlConnection(
request: AdnlRequestData,
callContext: CoroutineContext,
output: ByteWriteChannel,
cipher: CTRBlockCipher,
cipher: AesCtr,
closeChannel: Boolean = true
) = withContext(callContext) {
val scope = CoroutineScope(callContext + CoroutineName("Request body writer"))
Expand All @@ -135,7 +135,7 @@ public class AdnlConnection(
private suspend fun readResponse(
requestTime: Instant,
input: ByteReadChannel,
cipher: CTRBlockCipher,
cipher: AesCtr,
callContext: CoroutineContext
) = withContext(callContext) {
val packet = readRaw(input, cipher)
Expand All @@ -148,7 +148,7 @@ public class AdnlConnection(

private suspend fun readRaw(
input: ByteReadChannel,
cipher: CTRBlockCipher
cipher: AesCtr
): ByteReadPacket {
val encryptedLength = input.readPacket(4).readBytes()
val plainLength = ByteArray(4)
Expand All @@ -164,7 +164,7 @@ public class AdnlConnection(
val payload = data.readBytes((data.remaining - 32).toInt())
val hash = data.readBytes(32)

require(io.github.andreypfau.kotlinx.crypto.sha2.sha256(payload).contentEquals(hash)) {
require(sha256(payload).contentEquals(hash)) {
"sha256 mismatch"
}

Expand All @@ -175,15 +175,15 @@ public class AdnlConnection(

private suspend fun writeRaw(
output: ByteWriteChannel,
cipher: CTRBlockCipher,
cipher: AesCtr,
packet: ByteReadPacket
) {
val dataSize = (packet.remaining + 32 + 32).toInt()
require(dataSize in 32..(1 shl 24)) { "Invalid packet size: $dataSize" }
val nonce = SecureRandom.nextBytes(32)
val payload = packet.readBytes()

val hash = SHA256().apply {
val hash = Sha256().apply {
update(nonce)
update(payload)
}.digest()
Expand All @@ -201,12 +201,12 @@ public class AdnlConnection(
}

private class ChannelCipher(
val input: CTRBlockCipher,
val output: CTRBlockCipher
val input: AesCtr,
val output: AesCtr
) {
constructor(
s1: ByteArray, s2: ByteArray, v1: ByteArray, v2: ByteArray
) : this(CTRBlockCipher(AES(s1), v1), CTRBlockCipher(AES(s2), v2))
) : this(AesCtr(s1, v1), AesCtr(s2, v2))

constructor(
nonce: ByteArray
Expand Down
10 changes: 9 additions & 1 deletion adnl/src/network/TcpClientImpl.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
package org.ton.adnl.network

public expect class TcpClientImpl() : TcpClient
import io.ktor.utils.io.*

public expect class TcpClientImpl() : TcpClient {
override val input: ByteReadChannel
override val output: ByteWriteChannel
override suspend fun connect(host: String, port: Int)
override fun close(cause: Throwable?)
override fun close()
}
23 changes: 0 additions & 23 deletions adnl/src/network/UdpServer.kt

This file was deleted.

11 changes: 0 additions & 11 deletions adnl/src/network/UdpServerImpl.kt

This file was deleted.

10 changes: 5 additions & 5 deletions adnl/src@apple/network/TcpClientImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@ public actual class TcpClientImpl actual constructor(
private var isClosed by atomic(false)
private lateinit var socket: Socket
private lateinit var connection: Connection
override val input: ByteReadChannel
actual override val input: ByteReadChannel
get() = connection.input
override val output: ByteWriteChannel
actual override val output: ByteWriteChannel
get() = connection.output

override suspend fun connect(host: String, port: Int) {
actual override suspend fun connect(host: String, port: Int) {
socket = aSocket(selectorManager).tcpNoDelay().tcp().connect(host, port)
connection = socket.connection()
isClosed = false
}

override fun close() {
actual override fun close() {
close(null)
}

override fun close(cause: Throwable?) {
actual override fun close(cause: Throwable?) {
if (isClosed) return
connection.input.cancel(cause)
connection.output.close(cause)
Expand Down
34 changes: 0 additions & 34 deletions adnl/src@apple/network/UdpServerImpl.kt

This file was deleted.

10 changes: 5 additions & 5 deletions adnl/src@jvm/network/TcpClientImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@ public actual class TcpClientImpl actual constructor(
private var isClosed by atomic(false)
private lateinit var socket: Socket
private lateinit var connection: Connection
override val input: ByteReadChannel
actual override val input: ByteReadChannel
get() = connection.input
override val output: ByteWriteChannel
actual override val output: ByteWriteChannel
get() = connection.output

override suspend fun connect(host: String, port: Int) {
actual override suspend fun connect(host: String, port: Int) {
socket = aSocket(selectorManager).tcpNoDelay().tcp().connect(host, port)
connection = socket.connection()
isClosed = false
}

override fun close() {
actual override fun close() {
close(null)
}

override fun close(cause: Throwable?) {
actual override fun close(cause: Throwable?) {
if (isClosed) return
connection.input.cancel(cause)
connection.output.close(cause)
Expand Down
34 changes: 0 additions & 34 deletions adnl/src@jvm/network/UdpServerImpl.kt

This file was deleted.

10 changes: 5 additions & 5 deletions adnl/src@linux/network/TcpClientImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@ public actual class TcpClientImpl actual constructor(
private var isClosed by atomic(false)
private lateinit var socket: Socket
private lateinit var connection: Connection
override val input: ByteReadChannel
actual override val input: ByteReadChannel
get() = connection.input
override val output: ByteWriteChannel
actual override val output: ByteWriteChannel
get() = connection.output

override suspend fun connect(host: String, port: Int) {
actual override suspend fun connect(host: String, port: Int) {
socket = aSocket(selectorManager).tcpNoDelay().tcp().connect(host, port)
connection = socket.connection()
isClosed = false
}

override fun close() {
actual override fun close() {
close(null)
}

override fun close(cause: Throwable?) {
actual override fun close(cause: Throwable?) {
if (isClosed) return
connection.input.cancel(cause)
connection.output.close(cause)
Expand Down
40 changes: 0 additions & 40 deletions adnl/src@linux/network/UdpServerImpl.kt

This file was deleted.

Loading