diff --git a/buildSrc/build.gradle.kts b/buildSrc/build.gradle.kts index 1ed63da..6870178 100644 --- a/buildSrc/build.gradle.kts +++ b/buildSrc/build.gradle.kts @@ -28,7 +28,7 @@ repositories { } dependencies { - implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:1.4-M2") + implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:1.4-M3") } System.setProperty("PROJECT_PATH", project.projectDir.parentFile.toString()) diff --git a/buildSrc/src/main/kotlin/Deps.kt b/buildSrc/src/main/kotlin/Deps.kt index 5a204fc..d9c6f62 100644 --- a/buildSrc/src/main/kotlin/Deps.kt +++ b/buildSrc/src/main/kotlin/Deps.kt @@ -16,14 +16,14 @@ object Versions { val kotlinCoroutines = "1.3.5-native-mt-arm-1.4-M2-SNAPSHOT" //NOTE: my linux arm32 and arm64 build - val kotlin = "1.4-M2" - val kotlinSerialization = "0.20.0-1.4-M2" + val kotlin = "1.4-M3" + val kotlinSerialization = "0.20.0-1.4-M3" val atomicfu = "0.14.3-M2-2-SNAPSHOT" //NOTE: my linux arm32 and arm64 build val nodePlugin = "1.3.0" val dokkaPlugin = "0.11.0-dev-44" val taskTreePlugin = "1.5" - val kotlinBigNumVersion = "0.1.6-1.4-M2-3-SNAPSHOT" + val kotlinBigNumVersion = "0.1.6-1.4-M3-1-SNAPSHOT" val lazySodium = "4.2.6" val jna = "5.5.0" diff --git a/multiplatform-crypto-delegated/src/commonMain/kotlin/com/ionspin/kotlin/crypto/Crypto.kt b/multiplatform-crypto-delegated/src/commonMain/kotlin/com/ionspin/kotlin/crypto/Crypto.kt index f18e655..9f9a8d7 100644 --- a/multiplatform-crypto-delegated/src/commonMain/kotlin/com/ionspin/kotlin/crypto/Crypto.kt +++ b/multiplatform-crypto-delegated/src/commonMain/kotlin/com/ionspin/kotlin/crypto/Crypto.kt @@ -117,7 +117,7 @@ object CryptoPrimitives : PrimitivesApi { // salt, // key, // associatedData, -// parallelism, +// parallelism // tagLength, // memory, // numberOfIterations diff --git a/multiplatform-crypto-delegated/src/commonTest/kotlin/com/ionspin/kotlin/crypto/authenticated/XChaCha20Poly1305Test.kt b/multiplatform-crypto-delegated/src/commonTest/kotlin/com/ionspin/kotlin/crypto/authenticated/XChaCha20Poly1305Test.kt index 3957621..5e9ac75 100644 --- a/multiplatform-crypto-delegated/src/commonTest/kotlin/com/ionspin/kotlin/crypto/authenticated/XChaCha20Poly1305Test.kt +++ b/multiplatform-crypto-delegated/src/commonTest/kotlin/com/ionspin/kotlin/crypto/authenticated/XChaCha20Poly1305Test.kt @@ -1,6 +1,7 @@ package com.ionspin.kotlin.crypto.authenticated import com.ionspin.kotlin.crypto.CryptoInitializerDelegated +import com.ionspin.kotlin.crypto.Initializer import com.ionspin.kotlin.crypto.hash.encodeToUByteArray import com.ionspin.kotlin.crypto.util.hexColumsPrint import com.ionspin.kotlin.crypto.util.testBlocking @@ -184,10 +185,10 @@ class XChaCha20Poly1305Test { } - //Missing jvm and js impl - @Ignore + @Test - fun testStreamingImpl() { + fun testStreamingImpl() = testBlocking { + Initializer.initialize() val key = UByteArray(32) { 0U} val state = ubyteArrayOf( 0x2DU, 0xDBU, 0xC7U, 0xB2U, 0x03U, 0xBCU, 0xC3U, 0x22U, 0xBDU, 0x0CU, 0xBAU, 0x82U, 0xADU, 0x77U, 0x79U, 0x44U, @@ -212,6 +213,7 @@ class XChaCha20Poly1305Test { val xcha = XChaCha20Poly1305Delegated(key, state, header) val data = UByteArray(100) { 0U } val result = xcha.encrypt(data) + // assertTrue { // expected.contentEquals(result) // } diff --git a/multiplatform-crypto-delegated/src/commonTest/kotlin/com/ionspin/kotlin/crypto/util/TestUtil.kt b/multiplatform-crypto-delegated/src/commonTest/kotlin/com/ionspin/kotlin/crypto/util/TestUtil.kt index 65ce4a0..7d938dd 100644 --- a/multiplatform-crypto-delegated/src/commonTest/kotlin/com/ionspin/kotlin/crypto/util/TestUtil.kt +++ b/multiplatform-crypto-delegated/src/commonTest/kotlin/com/ionspin/kotlin/crypto/util/TestUtil.kt @@ -29,7 +29,9 @@ import kotlin.coroutines.startCoroutine fun testBlocking(block : suspend () -> Unit) { val continuation = Continuation(EmptyCoroutineContext) { //Do nothing - println("Done") + if (it.isFailure) { + throw it.exceptionOrNull()!! + } } block.startCoroutine(continuation) } diff --git a/multiplatform-crypto-delegated/src/jsMain/kotlin/com/ionspin/kotlin/crypto/JsSodiumInterface.kt b/multiplatform-crypto-delegated/src/jsMain/kotlin/com/ionspin/kotlin/crypto/JsSodiumInterface.kt index d0fa4c6..039fb13 100644 --- a/multiplatform-crypto-delegated/src/jsMain/kotlin/com/ionspin/kotlin/crypto/JsSodiumInterface.kt +++ b/multiplatform-crypto-delegated/src/jsMain/kotlin/com/ionspin/kotlin/crypto/JsSodiumInterface.kt @@ -46,7 +46,7 @@ interface JsSodiumInterface { //XChaCha20Poly1305 //encrypt fun crypto_secretstream_xchacha20poly1305_init_push(header: Uint8Array) : dynamic - fun crypto_secretstream_xchacha20poly1305_push(state: dynamic, message: Uint8Array, additionalData: Uint8Array, tag: Char) : Uint8Array + fun crypto_secretstream_xchacha20poly1305_push(state: dynamic, message: Uint8Array, additionalData: Uint8Array, tag: UByte) : Uint8Array //decrypt fun crypto_secretstream_xchacha20poly1305_init_pull(header: Uint8Array, key: Uint8Array) : dynamic diff --git a/multiplatform-crypto-delegated/src/jsMain/kotlin/com/ionspin/kotlin/crypto/authenticated/XChaCha20Poly1305Delegated.kt b/multiplatform-crypto-delegated/src/jsMain/kotlin/com/ionspin/kotlin/crypto/authenticated/XChaCha20Poly1305Delegated.kt index 5a0611c..abbf48a 100644 --- a/multiplatform-crypto-delegated/src/jsMain/kotlin/com/ionspin/kotlin/crypto/authenticated/XChaCha20Poly1305Delegated.kt +++ b/multiplatform-crypto-delegated/src/jsMain/kotlin/com/ionspin/kotlin/crypto/authenticated/XChaCha20Poly1305Delegated.kt @@ -54,15 +54,18 @@ actual class XChaCha20Poly1305Delegated internal actual constructor() { var state : dynamic = null actual fun initializeForEncryption(key: UByteArray) : UByteArray { + println("Initializaing for encryption") val stateAndHeader = getSodium().crypto_secretstream_xchacha20poly1305_init_push(key.toUInt8Array()) val state = stateAndHeader.state val header = stateAndHeader.header console.log(state) console.log(header) + println("Done initializaing for encryption") return header } actual fun initializeForDecryption(key: UByteArray, header: UByteArray) { + } internal actual constructor( @@ -70,12 +73,14 @@ actual class XChaCha20Poly1305Delegated internal actual constructor() { testState: UByteArray, testHeader: UByteArray ) : this() { - + state = getSodium().crypto_secretstream_xchacha20poly1305_init_pull(testHeader.toUInt8Array(), key.toUInt8Array()) + console.log(state) + println("Done initializaing test state") } actual fun encrypt(data: UByteArray, additionalData: UByteArray): UByteArray { -// val encrypted - TODO() + val encrypted = getSodium().crypto_secretstream_xchacha20poly1305_push(state, data.toUInt8Array(), additionalData.toUInt8Array(), 0U) + return encrypted.toUByteArray() } actual fun decrypt(data: UByteArray, additionalData: UByteArray): UByteArray { diff --git a/multiplatform-crypto-delegated/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/authenticated/XChaCha20Poly1305Delegated.kt b/multiplatform-crypto-delegated/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/authenticated/XChaCha20Poly1305Delegated.kt index 6125162..91e08d9 100644 --- a/multiplatform-crypto-delegated/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/authenticated/XChaCha20Poly1305Delegated.kt +++ b/multiplatform-crypto-delegated/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/authenticated/XChaCha20Poly1305Delegated.kt @@ -1,6 +1,8 @@ package com.ionspin.kotlin.crypto.authenticated import com.goterl.lazycode.lazysodium.SodiumJava +import com.goterl.lazycode.lazysodium.interfaces.SecretStream +import com.ionspin.kotlin.crypto.util.hexColumsPrint /** * Created by Ugljesa Jovanovic @@ -54,12 +56,16 @@ actual class XChaCha20Poly1305Delegated internal actual constructor() { } } + val state : SecretStream.State = SecretStream.State() + val sodium = SodiumJava() + internal actual constructor( key: UByteArray, testState: UByteArray, testHeader: UByteArray ) : this() { - + state.k = testState.sliceArray(0 until 32).toByteArray() + state.nonce = testState.sliceArray(32 until 44).toByteArray() } actual fun initializeForEncryption(key: UByteArray) : UByteArray { @@ -70,11 +76,21 @@ actual class XChaCha20Poly1305Delegated internal actual constructor() { } actual fun encrypt(data: UByteArray, additionalData: UByteArray): UByteArray { - TODO("not implemented yet") + val ciphertext = ByteArray(1 + data.size + 16) + sodium.crypto_secretstream_xchacha20poly1305_push( + state, ciphertext, null, + data.toByteArray(), data.size.toLong(), + additionalData.toByteArray(), additionalData.size.toLong(), + 0 + ) + return ciphertext.toUByteArray() } actual fun decrypt(data: UByteArray, additionalData: UByteArray): UByteArray { - TODO("not implemented yet") + val plaintext = ByteArray(data.size - 17) + + TODO() + } diff --git a/multiplatform-crypto/src/commonTest/kotlin/com/ionspin/kotlin/crypto/util/TestUtil.kt b/multiplatform-crypto/src/commonTest/kotlin/com/ionspin/kotlin/crypto/util/TestUtil.kt index 9b061e7..3cb5304 100644 --- a/multiplatform-crypto/src/commonTest/kotlin/com/ionspin/kotlin/crypto/util/TestUtil.kt +++ b/multiplatform-crypto/src/commonTest/kotlin/com/ionspin/kotlin/crypto/util/TestUtil.kt @@ -28,7 +28,9 @@ import kotlin.coroutines.startCoroutine fun testBlocking(block : suspend () -> Unit) { val continuation = Continuation(EmptyCoroutineContext) { //Do nothing - println("Done") + if (it.isFailure) { + throw it.exceptionOrNull()!! + } } block.startCoroutine(continuation) }