Final adjustments on the use of XChaCha

This commit is contained in:
Vitor Pamplona
2023-07-29 16:00:13 -04:00
parent 16a7fdffcf
commit e240cd04d4
5 changed files with 81 additions and 27 deletions
@@ -93,12 +93,12 @@ object CryptoUtils {
)
}
fun decryptXChaCha(encryptedInfo: EncryptedInfo, privateKey: ByteArray, pubKey: ByteArray): String {
fun decryptXChaCha(encryptedInfo: EncryptedInfo, privateKey: ByteArray, pubKey: ByteArray): String? {
val sharedSecret = getSharedSecretXChaCha(privateKey, pubKey)
return decryptXChaCha(encryptedInfo, sharedSecret)
}
fun decryptXChaCha(encryptedInfo: EncryptedInfo, sharedSecret: ByteArray): String {
fun decryptXChaCha(encryptedInfo: EncryptedInfo, sharedSecret: ByteArray): String? {
val lazySodium = LazySodiumAndroid(SodiumAndroid(), Base64MessageEncoder())
val key = Key.fromBytes(sharedSecret)
@@ -111,7 +111,7 @@ object CryptoUtils {
key = key
)
return cipher.decodeToString()
return cipher?.decodeToString()
}
fun verifySignature(
@@ -221,9 +221,9 @@ private fun LazySodium.cryptoStreamXChaCha20Xor(
messageBytes: ByteArray,
nonce: ByteArray,
key: Key
): ByteArray {
): ByteArray? {
val mLen = messageBytes.size
val cipher = ByteArray(mLen)
cryptoStreamXChaCha20Xor(cipher, messageBytes, mLen.toLong(), nonce, key.asBytes)
return cipher
val sucessful = cryptoStreamXChaCha20Xor(cipher, messageBytes, mLen.toLong(), nonce, key.asBytes)
return if (sucessful) cipher else null
}
@@ -32,7 +32,7 @@ class GiftWrapEvent(
fun unwrap(privKey: ByteArray) = try {
plainContent(privKey)?.let { fromJson(it, Client.lenient) }
} catch (e: Exception) {
Log.e("UnwrapError", "Couldn't Decrypt the content", e)
// Log.e("UnwrapError", "Couldn't Decrypt the content", e)
null
}
@@ -38,7 +38,7 @@ class SealedGossipEvent(
if (content.isBlank()) return null
return try {
val sharedSecret = CryptoUtils.getSharedSecret(privKey, pubKey.hexToByteArray())
val sharedSecret = CryptoUtils.getSharedSecretXChaCha(privKey, pubKey.hexToByteArray())
val toDecrypt = gson.fromJson<EncryptedInfo>(
content,
@@ -71,7 +71,7 @@ class SealedGossipEvent(
privateKey: ByteArray,
createdAt: Long = TimeUtils.now()
): SealedGossipEvent {
val sharedSecret = CryptoUtils.getSharedSecret(privateKey, encryptTo.hexToByteArray())
val sharedSecret = CryptoUtils.getSharedSecretXChaCha(privateKey, encryptTo.hexToByteArray())
val content = gson.toJson(
CryptoUtils.encryptXChaCha(