Add delegated xchacha20poly1305 decrypt

This commit is contained in:
Ugljesa Jovanovic authored and Ugljesa Jovanovic committed 2020-07-07 23:11:12 +02:00
1 parent 5d3c14de2f
commit 579c44fcc7
5 files changed
+62 -11

No files matched your search

@@ -50,6 +50,7 @@ interface JsSodiumInterface {
//decrypt
fun crypto_secretstream_xchacha20poly1305_init_pull(header: Uint8Array, key: Uint8Array) : dynamic
fun crypto_secretstream_xchacha20poly1305_pull(state: dynamic, ciphertext: Uint8Array, additionalData: Uint8Array) : dynamic
}
@@ -1,6 +1,8 @@
package com.ionspin.kotlin.crypto.authenticated
import com.ionspin.kotlin.crypto.InvalidTagException
import com.ionspin.kotlin.crypto.getSodium
import com.ionspin.kotlin.crypto.util.hexColumsPrint
import ext.libsodium.com.ionspin.kotlin.crypto.toUByteArray
import ext.libsodium.com.ionspin.kotlin.crypto.toUInt8Array
import org.khronos.webgl.Uint8Array
@@ -84,7 +86,15 @@ actual class XChaCha20Poly1305Delegated internal actual constructor() {
}
actual fun decrypt(data: UByteArray, additionalData: UByteArray): UByteArray {
TODO("not implemented yet")
val decryptedWithTag = getSodium().crypto_secretstream_xchacha20poly1305_pull(state, data.toUInt8Array(), additionalData.toUInt8Array())
val decrypted = decryptedWithTag.message as Uint8Array
val validTag = decryptedWithTag.tag as UInt
if (validTag != 0U) {
println("Tag validation failed")
throw InvalidTagException()
}
return decrypted.toUByteArray()
}