Skip to content

Commit

Permalink
0.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
andreypfau committed Dec 12, 2023
1 parent 0a4e405 commit 66b3bc6
Show file tree
Hide file tree
Showing 56 changed files with 4,211 additions and 450 deletions.
31 changes: 28 additions & 3 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,39 @@ on:
jobs:
build:
if: "!contains(github.event.commits[0].message, '[skip ci]')"

strategy:
matrix:
include:
- os: macos-latest
gradle_args: publishIosX64PublicationToSonatypeRepository publishIosArm64PublicationToSonatypeRepository publishIosSimulatorArm64PublicationToSonatypeRepository publishTvosX64PublicationToSonatypeRepository publishTvosArm64PublicationToSonatypeRepository publishTvosSimulatorArm64PublicationToSonatypeRepository publishTvosSimulatorArm64PublicationToSonatypeRepository publishWatchosArm32PublicationToSonatypeRepository publishWatchosArm64PublicationToSonatypeRepository publishWatchosX64PublicationToSonatypeRepository publishWatchosSimulatorArm64PublicationToSonatypeRepository publishMacosX64PublicationToSonatypeRepository publishMacosArm64PublicationToSonatypeRepository
gradle_args: assemble publishIosX64PublicationToSonatypeRepository
- os: macos-latest
gradle_args: assemble publishIosArm64PublicationToSonatypeRepository
- os: macos-latest
gradle_args: assemble publishIosSimulatorArm64PublicationToSonatypeRepository
- os: macos-latest
gradle_args: assemble publishIosSimulatorArm64PublicationToSonatypeRepository
- os: macos-latest
gradle_args: assemble publishTvosX64PublicationToSonatypeRepository
- os: macos-latest
gradle_args: assemble publishTvosArm64PublicationToSonatypeRepository
- os: macos-latest
gradle_args: assemble publishTvosSimulatorArm64PublicationToSonatypeRepository
- os: macos-latest
gradle_args: assemble publishTvosSimulatorArm64PublicationToSonatypeRepository
- os: macos-latest
gradle_args: assemble publishWatchosArm32PublicationToSonatypeRepository
- os: macos-latest
gradle_args: assemble publishWatchosArm64PublicationToSonatypeRepository
- os: macos-latest
gradle_args: assemble publishWatchosX64PublicationToSonatypeRepository
- os: macos-latest
gradle_args: assemble publishWatchosSimulatorArm64PublicationToSonatypeRepository
- os: macos-latest
gradle_args: assemble publishMacosX64PublicationToSonatypeRepository
- os: macos-latest
gradle_args: assemble publishMacosArm64PublicationToSonatypeRepository
- os: ubuntu-latest
gradle_args: publishKotlinMultiplatformPublicationToSonatypeRepository publishJvmPublicationToSonatypeRepository publishLinuxX64PublicationToSonatypeRepository publishLinuxArm64PublicationToSonatypeRepository publishMingwX64PublicationToSonatypeRepository
gradle_args: assemble publishKotlinMultiplatformPublicationToSonatypeRepository publishJvmPublicationToSonatypeRepository publishLinuxX64PublicationToSonatypeRepository publishLinuxArm64PublicationToSonatypeRepository publishMingwX64PublicationToSonatypeRepository

runs-on: ${{ matrix.os }}
steps:
Expand Down
46 changes: 28 additions & 18 deletions adnl/src/connection/AdnlConnection.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
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 @@ -10,10 +13,7 @@ import kotlinx.datetime.Clock
import kotlinx.datetime.Instant
import org.ton.adnl.network.TcpClient
import org.ton.api.liteserver.LiteServerDesc
import org.ton.crypto.AesCtr
import org.ton.crypto.SecureRandom
import org.ton.crypto.digest.Digest
import org.ton.crypto.digest.sha256
import org.ton.tl.writeByteString
import kotlin.coroutines.CoroutineContext
import kotlin.time.Duration
Expand Down Expand Up @@ -114,7 +114,7 @@ public class AdnlConnection(
request: AdnlRequestData,
callContext: CoroutineContext,
output: ByteWriteChannel,
cipher: AesCtr,
cipher: CTRBlockCipher,
closeChannel: Boolean = true
) = withContext(callContext) {
val scope = CoroutineScope(callContext + CoroutineName("Request body writer"))
Expand All @@ -136,7 +136,7 @@ public class AdnlConnection(
private suspend fun readResponse(
requestTime: Instant,
input: ByteReadChannel,
cipher: AesCtr,
cipher: CTRBlockCipher,
callContext: CoroutineContext
) = withContext(callContext) {
val packet = readRaw(input, cipher)
Expand All @@ -149,17 +149,23 @@ public class AdnlConnection(

private suspend fun readRaw(
input: ByteReadChannel,
cipher: AesCtr
cipher: CTRBlockCipher
): ByteReadPacket {
val encryptedLength = input.readPacket(4).readBytes()
val length = ByteReadPacket(cipher.update(encryptedLength)).readIntLittleEndian()
val plainLength = ByteArray(4)
cipher.processBytes(encryptedLength, plainLength)

val length = ByteReadPacket(plainLength).readIntLittleEndian()
check(length in 32..(1 shl 24)) { "Invalid length" }
val encryptedData = input.readPacket(length).readBytes()
val data = ByteReadPacket(cipher.update(encryptedData))
val plainData = ByteArray(length)
cipher.processBytes(encryptedData, plainData)

val data = ByteReadPacket(plainData)
val payload = data.readBytes((data.remaining - 32).toInt())
val hash = data.readBytes(32)

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

Expand All @@ -170,34 +176,38 @@ public class AdnlConnection(

private suspend fun writeRaw(
output: ByteWriteChannel,
cipher: AesCtr,
cipher: CTRBlockCipher,
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 = Digest.sha256().apply {
update(nonce)
update(payload)
}.build()

val hash = SHA256().apply {
write(nonce)
write(payload)
}.digest()

val data = buildPacket {
writeIntLittleEndian(dataSize)
writeFully(nonce)
writeFully(payload)
writeFully(hash)
}
val encryptedData = cipher.update(data.readBytes())

val encryptedData = ByteArray(data.remaining.toInt())
cipher.processBytes(data.readBytes(), encryptedData)
output.writeFully(encryptedData)
}

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

constructor(
nonce: ByteArray
Expand Down
3 changes: 1 addition & 2 deletions adnl/src/peer/AdnlMessagePartDecoder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package org.ton.adnl.peer

import org.ton.api.adnl.message.AdnlMessage
import org.ton.api.adnl.message.AdnlMessagePart
import org.ton.crypto.digest.sha256
import org.ton.tl.asByteString

public class AdnlMessagePartDecoder(
Expand All @@ -29,7 +28,7 @@ public class AdnlMessagePartDecoder(
val totalSize = currentMessageOffset + message.data.size
messageOffset = totalSize
if (totalSize == messagePayload.size) {
val actualMessageHash = sha256(messagePayload)
val actualMessageHash = io.github.andreypfau.kotlinx.crypto.sha2.sha256(messagePayload)
check(actualMessageHash.asByteString() == messageHash) {
"hash mismatch, expected: $messageHash, actual: $actualMessageHash"
}
Expand Down
2 changes: 1 addition & 1 deletion adnl/src/peer/AdnlMessagePartEncoder.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package org.ton.adnl.peer

import io.github.andreypfau.kotlinx.crypto.sha2.sha256
import org.ton.api.adnl.message.AdnlMessage
import org.ton.api.adnl.message.AdnlMessagePart
import org.ton.crypto.digest.sha256
import org.ton.tl.asByteString

public class AdnlMessagePartEncoder(
Expand Down
1 change: 1 addition & 0 deletions bitstring/api/ton-kotlin-bitstring.api
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public final class org/ton/bitstring/BitString$Companion {
public final fun of ([Z)Lorg/ton/bitstring/BitString;
public static synthetic fun of$default (Lorg/ton/bitstring/BitString$Companion;IILjava/lang/Object;)Lorg/ton/bitstring/BitString;
public static synthetic fun of$default (Lorg/ton/bitstring/BitString$Companion;[BIILjava/lang/Object;)Lorg/ton/bitstring/BitString;
public final fun serializer ()Lkotlinx/serialization/KSerializer;
}

public final class org/ton/bitstring/BitStringKt {
Expand Down
3 changes: 1 addition & 2 deletions bitstring/src/ByteBackedBitString.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.ton.bitstring

import org.ton.bitstring.exception.BitStringUnderflowException
import org.ton.crypto.hex
import kotlin.experimental.and
import kotlin.experimental.or
import kotlin.experimental.xor
Expand Down Expand Up @@ -83,7 +82,7 @@ public open class ByteBackedBitString protected constructor(
override fun toHex(): String {
if (size == 0) return ""
val data = appendTag(bytes, size)
val result = StringBuilder(hex(data))
val result = StringBuilder(data.toHexString())
when (size % 8) {
0 -> {
result.deleteAt(result.lastIndex)
Expand Down
Loading

0 comments on commit 66b3bc6

Please sign in to comment.