WIP: clean up signal protocol
parent
735cceafdb
commit
bbd067873a
@ -1,39 +0,0 @@
|
|||||||
package org.session.libsignal.libsignal.loki
|
|
||||||
|
|
||||||
import com.google.protobuf.ByteString
|
|
||||||
import org.session.libsignal.utilities.logging.Log
|
|
||||||
import org.session.libsignal.libsignal.protocol.CiphertextMessage
|
|
||||||
import org.session.libsignal.libsignal.protocol.SignalProtos
|
|
||||||
|
|
||||||
class ClosedGroupCiphertextMessage(val ivAndCiphertext: ByteArray, val senderPublicKey: ByteArray, val keyIndex: Int) : CiphertextMessage {
|
|
||||||
private val serialized: ByteArray
|
|
||||||
|
|
||||||
companion object {
|
|
||||||
|
|
||||||
fun from(serialized: ByteArray): ClosedGroupCiphertextMessage? {
|
|
||||||
try {
|
|
||||||
val proto = SignalProtos.ClosedGroupCiphertextMessage.parseFrom(serialized)
|
|
||||||
return ClosedGroupCiphertextMessage(proto.ciphertext.toByteArray(), proto.senderPublicKey.toByteArray(), proto.keyIndex)
|
|
||||||
} catch (exception: Exception) {
|
|
||||||
Log.d("Loki", "Couldn't parse proto due to error: $exception.")
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
init {
|
|
||||||
val builder = SignalProtos.ClosedGroupCiphertextMessage.newBuilder()
|
|
||||||
builder.ciphertext = ByteString.copyFrom(ivAndCiphertext)
|
|
||||||
builder.senderPublicKey = ByteString.copyFrom(senderPublicKey)
|
|
||||||
builder.keyIndex = keyIndex
|
|
||||||
serialized = builder.build().toByteArray()
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun getType(): Int {
|
|
||||||
return CiphertextMessage.CLOSED_GROUP_CIPHERTEXT
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun serialize(): ByteArray {
|
|
||||||
return serialized
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,14 +0,0 @@
|
|||||||
package org.session.libsignal.libsignal.loki
|
|
||||||
|
|
||||||
import org.session.libsignal.libsignal.protocol.CiphertextMessage
|
|
||||||
|
|
||||||
class FallbackMessage(private val paddedMessageBody: ByteArray) : CiphertextMessage {
|
|
||||||
|
|
||||||
override fun serialize(): ByteArray {
|
|
||||||
return paddedMessageBody
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun getType(): Int {
|
|
||||||
return CiphertextMessage.FALLBACK_MESSAGE_TYPE
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,51 +0,0 @@
|
|||||||
package org.session.libsignal.libsignal.loki
|
|
||||||
|
|
||||||
import org.whispersystems.curve25519.Curve25519
|
|
||||||
import org.session.libsignal.utilities.Hex
|
|
||||||
import org.session.libsignal.service.loki.utilities.removing05PrefixIfNeeded
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A session cipher that uses the current user's private key along with a contact's public key to encrypt data.
|
|
||||||
*/
|
|
||||||
class FallbackSessionCipher(private val userPrivateKey: ByteArray, private val hexEncodedContactPublicKey: String) {
|
|
||||||
|
|
||||||
private val contactPublicKey by lazy {
|
|
||||||
val hexEncodedContactPublicKey = hexEncodedContactPublicKey.removing05PrefixIfNeeded()
|
|
||||||
Hex.fromStringCondensed(hexEncodedContactPublicKey)
|
|
||||||
}
|
|
||||||
|
|
||||||
private val symmetricKey: ByteArray?
|
|
||||||
get() {
|
|
||||||
try {
|
|
||||||
val curve = Curve25519.getInstance(Curve25519.BEST)
|
|
||||||
return curve.calculateAgreement(contactPublicKey, userPrivateKey)
|
|
||||||
} catch (e: Exception) {
|
|
||||||
e.printStackTrace()
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
companion object {
|
|
||||||
@JvmStatic val sessionVersion = 3
|
|
||||||
}
|
|
||||||
|
|
||||||
fun encrypt(paddedMessageBody: ByteArray): ByteArray? {
|
|
||||||
val symmetricKey = symmetricKey ?: return null
|
|
||||||
try {
|
|
||||||
return DiffieHellman.encrypt(paddedMessageBody, symmetricKey)
|
|
||||||
} catch (e: Exception) {
|
|
||||||
e.printStackTrace()
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun decrypt(bytes: ByteArray): ByteArray? {
|
|
||||||
val symmetricKey = symmetricKey ?: return null
|
|
||||||
try {
|
|
||||||
return DiffieHellman.decrypt(bytes, symmetricKey)
|
|
||||||
} catch (e: Exception) {
|
|
||||||
e.printStackTrace()
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,4 +1,4 @@
|
|||||||
package org.session.libsignal.libsignal.loki
|
package org.session.libsignal.utilities
|
||||||
|
|
||||||
import org.whispersystems.curve25519.Curve25519
|
import org.whispersystems.curve25519.Curve25519
|
||||||
import org.session.libsignal.service.internal.util.Util
|
import org.session.libsignal.service.internal.util.Util
|
Loading…
Reference in New Issue