From eabe6eb622b4f422175fbcbeb28c91639a79e02a Mon Sep 17 00:00:00 2001 From: yet Date: Fri, 15 May 2026 17:58:05 +0400 Subject: [PATCH] refactor(noise/protocol): migrate ChaChaPolyCipherState to Kotlin MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Override CipherState properties (`cipherName`, `keyLength`, `macLength`) instead of Java-style getters. - Initialize `val` fields inline and drop the now-empty constructor `init` block. - Replace `Arrays.fill(arr, 0.toByte())` with `arr.fill(0)` and `Arrays.fill(intArr, 0)` with `intArr.fill(0)`. - Tighten counter increments (`++input[12]`) — Kotlin no longer needs the parenthesized form the IDE emitted. - Crypto path (ChaCha20 keystream, Poly1305 MAC, AD padding) is unchanged byte-for-byte. Co-Authored-By: Claude Opus 4.7 --- .../protocol/ChaChaPolyCipherState.kt | 42 ++++++------------- 1 file changed, 13 insertions(+), 29 deletions(-) diff --git a/app/src/main/java/com/bitchat/android/noise/southernstorm/protocol/ChaChaPolyCipherState.kt b/app/src/main/java/com/bitchat/android/noise/southernstorm/protocol/ChaChaPolyCipherState.kt index 8e988b39..10e64d94 100644 --- a/app/src/main/java/com/bitchat/android/noise/southernstorm/protocol/ChaChaPolyCipherState.kt +++ b/app/src/main/java/com/bitchat/android/noise/southernstorm/protocol/ChaChaPolyCipherState.kt @@ -27,7 +27,6 @@ import com.bitchat.android.noise.southernstorm.crypto.ChaChaCore.initKey256 import com.bitchat.android.noise.southernstorm.crypto.Poly1305 import com.bitchat.android.noise.southernstorm.protocol.Noise.destroy import com.bitchat.android.noise.southernstorm.protocol.Noise.throwBadTagException -import java.util.Arrays import javax.crypto.BadPaddingException import javax.crypto.ShortBufferException @@ -35,38 +34,23 @@ import javax.crypto.ShortBufferException * Implements the ChaChaPoly cipher for Noise. */ internal class ChaChaPolyCipherState : CipherState { - private val poly: Poly1305 - private val input: IntArray - private val output: IntArray - private val polyKey: ByteArray - var n: Long = 0 + private val poly: Poly1305 = Poly1305() + private val input: IntArray = IntArray(16) + private val output: IntArray = IntArray(16) + private val polyKey: ByteArray = ByteArray(32) + private var n: Long = 0 private var haskey = false - /** - * Constructs a new cipher state for the "ChaChaPoly" algorithm. - */ - init { - poly = Poly1305() - input = IntArray(16) - output = IntArray(16) - polyKey = ByteArray(32) - } - override fun destroy() { poly.destroy() - Arrays.fill(input, 0) - Arrays.fill(output, 0) + input.fill(0) + output.fill(0) destroy(polyKey) } - val cipherName: String - get() = "ChaChaPoly" - - val keyLength: Int - get() = 32 - - val mACLength: Int - get() = if (haskey) 16 else 0 + override val cipherName: String get() = "ChaChaPoly" + override val keyLength: Int get() = 32 + override val macLength: Int get() = if (haskey) 16 else 0 override fun initializeKey(key: ByteArray, offset: Int) { initKey256(input, key, offset) @@ -87,14 +71,14 @@ internal class ChaChaPolyCipherState : CipherState { check(n != -1L) { "Nonce has wrapped around" } initIV(input, n++) hash(output, input) - Arrays.fill(polyKey, 0.toByte()) + polyKey.fill(0) xorBlock(polyKey, 0, polyKey, 0, 32, output) poly.reset(polyKey, 0) if (ad != null) { poly.update(ad, 0, ad.size) poly.pad() } - if (++(input[12]) == 0) ++(input[13]) + if (++input[12] == 0) ++input[13] } /** @@ -132,7 +116,7 @@ internal class ChaChaPolyCipherState : CipherState { if (tempLen > length) tempLen = length hash(output, input) xorBlock(plaintext, plaintextOffset, ciphertext, ciphertextOffset, tempLen, output) - if (++(input[12]) == 0) ++(input[13]) + if (++input[12] == 0) ++input[13] plaintextOffset += tempLen ciphertextOffset += tempLen length -= tempLen