mirror of
https://github.com/permissionlesstech/bitchat-android.git
synced 2026-11-01 07:51:15 +00:00
refactor(noise/protocol): migrate Curve25519DHState to Kotlin
- Override DHState properties (`dhName`, `publicKeyLength`, `privateKeyLength`, `sharedKeyLength`) instead of Java-style getters. - Replace `Arrays.fill(arr, 0.toByte())` with `arr.fill(0)`. - Drop redundant `val dh = other` alias after the `is` smart cast. - Preserve bit-identical crypto behavior (Curve25519.eval ordering, null-public-key constant-time scan, mode bitfield semantics). Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
parent
2655aa7636
commit
e7c0f28f76
@ -24,43 +24,23 @@ package com.bitchat.android.noise.southernstorm.protocol
|
|||||||
import com.bitchat.android.noise.southernstorm.crypto.Curve25519
|
import com.bitchat.android.noise.southernstorm.crypto.Curve25519
|
||||||
import com.bitchat.android.noise.southernstorm.protocol.Noise.destroy
|
import com.bitchat.android.noise.southernstorm.protocol.Noise.destroy
|
||||||
import com.bitchat.android.noise.southernstorm.protocol.Noise.random
|
import com.bitchat.android.noise.southernstorm.protocol.Noise.random
|
||||||
import java.util.Arrays
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Implementation of the Curve25519 algorithm for the Noise protocol.
|
* Implementation of the Curve25519 algorithm for the Noise protocol.
|
||||||
*/
|
*/
|
||||||
internal class Curve25519DHState : DHState {
|
internal class Curve25519DHState : DHState {
|
||||||
private val publicKey: ByteArray
|
private val publicKey: ByteArray = ByteArray(32)
|
||||||
private val privateKey: ByteArray
|
private val privateKey: ByteArray = ByteArray(32)
|
||||||
private var mode = 0
|
private var mode = 0
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructs a new Diffie-Hellman object for Curve25519.
|
|
||||||
*/
|
|
||||||
init {
|
|
||||||
publicKey = ByteArray(32)
|
|
||||||
privateKey = ByteArray(32)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun destroy() {
|
override fun destroy() {
|
||||||
clearKey()
|
clearKey()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getDHName(): String {
|
override val dhName: String get() = "25519"
|
||||||
return "25519"
|
override val publicKeyLength: Int get() = 32
|
||||||
}
|
override val privateKeyLength: Int get() = 32
|
||||||
|
override val sharedKeyLength: Int get() = 32
|
||||||
override fun getPublicKeyLength(): Int {
|
|
||||||
return 32
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun getPrivateKeyLength(): Int {
|
|
||||||
return 32
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun getSharedKeyLength(): Int {
|
|
||||||
return 32
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun generateKeyPair() {
|
override fun generateKeyPair() {
|
||||||
random(privateKey)
|
random(privateKey)
|
||||||
@ -74,7 +54,7 @@ internal class Curve25519DHState : DHState {
|
|||||||
|
|
||||||
override fun setPublicKey(key: ByteArray, offset: Int) {
|
override fun setPublicKey(key: ByteArray, offset: Int) {
|
||||||
System.arraycopy(key, offset, publicKey, 0, 32)
|
System.arraycopy(key, offset, publicKey, 0, 32)
|
||||||
Arrays.fill(privateKey, 0.toByte())
|
privateKey.fill(0)
|
||||||
mode = 0x01
|
mode = 0x01
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -89,8 +69,8 @@ internal class Curve25519DHState : DHState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun setToNullPublicKey() {
|
override fun setToNullPublicKey() {
|
||||||
Arrays.fill(publicKey, 0.toByte())
|
publicKey.fill(0)
|
||||||
Arrays.fill(privateKey, 0.toByte())
|
privateKey.fill(0)
|
||||||
mode = 0x01
|
mode = 0x01
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -100,13 +80,9 @@ internal class Curve25519DHState : DHState {
|
|||||||
mode = 0
|
mode = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun hasPublicKey(): Boolean {
|
override fun hasPublicKey(): Boolean = (mode and 0x01) != 0
|
||||||
return (mode and 0x01) != 0
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun hasPrivateKey(): Boolean {
|
override fun hasPrivateKey(): Boolean = (mode and 0x02) != 0
|
||||||
return (mode and 0x02) != 0
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun isNullPublicKey(): Boolean {
|
override fun isNullPublicKey(): Boolean {
|
||||||
if ((mode and 0x01) == 0) return false
|
if ((mode and 0x01) == 0) return false
|
||||||
@ -115,17 +91,16 @@ internal class Curve25519DHState : DHState {
|
|||||||
return temp == 0
|
return temp == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun calculate(sharedKey: ByteArray, offset: Int, publicDH: DHState?) {
|
override fun calculate(sharedKey: ByteArray, offset: Int, publicDH: DHState) {
|
||||||
require(publicDH is Curve25519DHState) { "Incompatible DH algorithms" }
|
require(publicDH is Curve25519DHState) { "Incompatible DH algorithms" }
|
||||||
Curve25519.eval(sharedKey, offset, privateKey, publicDH.publicKey)
|
Curve25519.eval(sharedKey, offset, privateKey, publicDH.publicKey)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun copyFrom(other: DHState?) {
|
override fun copyFrom(other: DHState) {
|
||||||
check(other is Curve25519DHState) { "Mismatched DH key objects" }
|
check(other is Curve25519DHState) { "Mismatched DH key objects" }
|
||||||
if (other === this) return
|
if (other === this) return
|
||||||
val dh = other
|
System.arraycopy(other.privateKey, 0, privateKey, 0, 32)
|
||||||
System.arraycopy(dh.privateKey, 0, privateKey, 0, 32)
|
System.arraycopy(other.publicKey, 0, publicKey, 0, 32)
|
||||||
System.arraycopy(dh.publicKey, 0, publicKey, 0, 32)
|
mode = other.mode
|
||||||
mode = dh.mode
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user