Add delegated xchacha20poly1305 decrypt
This commit is contained in:
parent
5d3c14de2f
commit
579c44fcc7
@ -7,6 +7,7 @@ import com.ionspin.kotlin.crypto.util.hexColumsPrint
|
|||||||
import com.ionspin.kotlin.crypto.util.testBlocking
|
import com.ionspin.kotlin.crypto.util.testBlocking
|
||||||
import kotlin.test.Ignore
|
import kotlin.test.Ignore
|
||||||
import kotlin.test.Test
|
import kotlin.test.Test
|
||||||
|
import kotlin.test.assertFails
|
||||||
import kotlin.test.assertTrue
|
import kotlin.test.assertTrue
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -210,12 +211,28 @@ class XChaCha20Poly1305Test {
|
|||||||
0xDEU, 0xFBU, 0x5CU, 0x7FU, 0x1CU, 0x26U, 0x32U, 0x2CU, 0x51U, 0xF6U, 0xEFU, 0xC6U, 0x34U, 0xC4U, 0xACU, 0x6CU,
|
0xDEU, 0xFBU, 0x5CU, 0x7FU, 0x1CU, 0x26U, 0x32U, 0x2CU, 0x51U, 0xF6U, 0xEFU, 0xC6U, 0x34U, 0xC4U, 0xACU, 0x6CU,
|
||||||
0xE8U, 0xF9U, 0x4BU, 0xABU, 0xA3U,
|
0xE8U, 0xF9U, 0x4BU, 0xABU, 0xA3U,
|
||||||
)
|
)
|
||||||
val xcha = XChaCha20Poly1305Delegated(key, state, header)
|
val encryptor = XChaCha20Poly1305Delegated(key, state, header)
|
||||||
|
val decryptor = XChaCha20Poly1305Delegated(key, state, header)
|
||||||
val data = UByteArray(100) { 0U }
|
val data = UByteArray(100) { 0U }
|
||||||
val result = xcha.encrypt(data)
|
val result = encryptor.encrypt(data)
|
||||||
|
val decrypted = decryptor.decrypt(result)
|
||||||
|
println("Encrypted -----------")
|
||||||
|
result.hexColumsPrint()
|
||||||
|
println("Encrypted end -----------")
|
||||||
|
println("Decrypted -----------")
|
||||||
|
decrypted.hexColumsPrint()
|
||||||
|
println("Decrypted end -----------")
|
||||||
assertTrue {
|
assertTrue {
|
||||||
expected.contentEquals(result)
|
expected.contentEquals(result) && decrypted.contentEquals(data)
|
||||||
|
}
|
||||||
|
val messedUpTag = result.copyOf()
|
||||||
|
messedUpTag[messedUpTag.size - 2] = 0U
|
||||||
|
assertFails {
|
||||||
|
val decryptorForWrongTag = XChaCha20Poly1305Delegated(key, state, header)
|
||||||
|
val plaintext = decryptorForWrongTag.decrypt(messedUpTag)
|
||||||
|
println("Decrypted with wrong tag -----------")
|
||||||
|
plaintext.hexColumsPrint()
|
||||||
|
println("Decrypted with wrong tag end -----------")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -50,6 +50,7 @@ interface JsSodiumInterface {
|
|||||||
|
|
||||||
//decrypt
|
//decrypt
|
||||||
fun crypto_secretstream_xchacha20poly1305_init_pull(header: Uint8Array, key: Uint8Array) : dynamic
|
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
|
package com.ionspin.kotlin.crypto.authenticated
|
||||||
|
|
||||||
|
import com.ionspin.kotlin.crypto.InvalidTagException
|
||||||
import com.ionspin.kotlin.crypto.getSodium
|
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.toUByteArray
|
||||||
import ext.libsodium.com.ionspin.kotlin.crypto.toUInt8Array
|
import ext.libsodium.com.ionspin.kotlin.crypto.toUInt8Array
|
||||||
import org.khronos.webgl.Uint8Array
|
import org.khronos.webgl.Uint8Array
|
||||||
@ -84,7 +86,15 @@ actual class XChaCha20Poly1305Delegated internal actual constructor() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
actual fun decrypt(data: UByteArray, additionalData: UByteArray): UByteArray {
|
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()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -2,6 +2,7 @@ package com.ionspin.kotlin.crypto.authenticated
|
|||||||
|
|
||||||
import com.goterl.lazycode.lazysodium.SodiumJava
|
import com.goterl.lazycode.lazysodium.SodiumJava
|
||||||
import com.goterl.lazycode.lazysodium.interfaces.SecretStream
|
import com.goterl.lazycode.lazysodium.interfaces.SecretStream
|
||||||
|
import com.ionspin.kotlin.crypto.InvalidTagException
|
||||||
import com.ionspin.kotlin.crypto.util.hexColumsPrint
|
import com.ionspin.kotlin.crypto.util.hexColumsPrint
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -89,14 +90,18 @@ actual class XChaCha20Poly1305Delegated internal actual constructor() {
|
|||||||
actual fun decrypt(data: UByteArray, additionalData: UByteArray): UByteArray {
|
actual fun decrypt(data: UByteArray, additionalData: UByteArray): UByteArray {
|
||||||
val plaintext = ByteArray(data.size - 17)
|
val plaintext = ByteArray(data.size - 17)
|
||||||
|
|
||||||
sodium.crypto_secretstream_xchacha20poly1305_pull(
|
val validTag = sodium.crypto_secretstream_xchacha20poly1305_pull(
|
||||||
state, plaintext, null,
|
state, plaintext, null,
|
||||||
data.sliceArray(0 until 1).asByteArray(),
|
null,
|
||||||
data.sliceArray(1 until data.size).asByteArray(),
|
data.asByteArray(),
|
||||||
(data.size - 17).toLong(),
|
(data.size).toLong(),
|
||||||
additionalData.asByteArray(),
|
additionalData.asByteArray(),
|
||||||
additionalData.size.toLong()
|
additionalData.size.toLong()
|
||||||
)
|
)
|
||||||
|
if (validTag != 0) {
|
||||||
|
println("Tag validation failed")
|
||||||
|
throw InvalidTagException()
|
||||||
|
}
|
||||||
return plaintext.asUByteArray()
|
return plaintext.asUByteArray()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package com.ionspin.kotlin.crypto.authenticated
|
package com.ionspin.kotlin.crypto.authenticated
|
||||||
|
|
||||||
import com.ionspin.kotlin.bignum.integer.util.hexColumsPrint
|
import com.ionspin.kotlin.bignum.integer.util.hexColumsPrint
|
||||||
|
import com.ionspin.kotlin.crypto.InvalidTagException
|
||||||
import kotlinx.cinterop.*
|
import kotlinx.cinterop.*
|
||||||
import libsodium.*
|
import libsodium.*
|
||||||
import platform.posix.malloc
|
import platform.posix.malloc
|
||||||
@ -127,7 +128,24 @@ actual class XChaCha20Poly1305Delegated internal actual constructor() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
actual fun decrypt(data: UByteArray, additionalData: UByteArray): UByteArray {
|
actual fun decrypt(data: UByteArray, additionalData: UByteArray): UByteArray {
|
||||||
TODO("not implemented yet")
|
val plaintext = UByteArray(data.size - crypto_secretstream_xchacha20poly1305_ABYTES.toInt())
|
||||||
|
val plaintextPinned = plaintext.pin()
|
||||||
|
val validTag = crypto_secretstream_xchacha20poly1305_pull(
|
||||||
|
state.ptr,
|
||||||
|
plaintextPinned.addressOf(0),
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
data.toCValues(),
|
||||||
|
data.size.convert(),
|
||||||
|
additionalData.toCValues(),
|
||||||
|
additionalData.size.convert()
|
||||||
|
)
|
||||||
|
plaintextPinned.unpin()
|
||||||
|
if (validTag != 0) {
|
||||||
|
println("Tag validation failed")
|
||||||
|
throw InvalidTagException()
|
||||||
|
}
|
||||||
|
return plaintext
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user