From 1be0470745a10144cb4cf2eaeb35a1aa42609426 Mon Sep 17 00:00:00 2001 From: Ugljesa Jovanovic Date: Sun, 7 Jun 2020 00:55:46 +0200 Subject: [PATCH] Idea cinterop stopped working, again... fixed blake2b stateless segmentation --- buildSrc/src/main/kotlin/Utils.kt | 10 ---- .../build.gradle.kts | 50 ++++++++++--------- .../crypto/hash/blake2b/Blake2bDelegated.kt | 5 +- .../ionspin/kotlin/crypto/sample/Sample.kt | 3 +- 4 files changed, 32 insertions(+), 36 deletions(-) diff --git a/buildSrc/src/main/kotlin/Utils.kt b/buildSrc/src/main/kotlin/Utils.kt index 78a021f..42f3898 100644 --- a/buildSrc/src/main/kotlin/Utils.kt +++ b/buildSrc/src/main/kotlin/Utils.kt @@ -86,14 +86,4 @@ fun NamedDomainObjectContainer.createWorkaroundNativeMainSource } } -} - -fun NamedDomainObjectContainer.createWorkaround32bitNativeMainSourceSet(name : String, nativeDeps : KotlinDependencyHandler.() -> Unit) : KotlinSourceSet { - return create("${name}Workaround") { - kotlin.srcDir("src/native32Main") - dependencies { - nativeDeps.invoke(this) - } - } - } \ No newline at end of file diff --git a/multiplatform-crypto-delegated/build.gradle.kts b/multiplatform-crypto-delegated/build.gradle.kts index 9ffd7c1..d62da8d 100644 --- a/multiplatform-crypto-delegated/build.gradle.kts +++ b/multiplatform-crypto-delegated/build.gradle.kts @@ -81,16 +81,6 @@ kotlin { staticLib { } } - - compilations.getByName("main") { - val libsodiumCinterop by cinterops.creating { - defFile(project.file("src/nativeInterop/cinterop/libsodium.def")) - compilerOpts.add("-I${project.rootDir}/sodiumWrapper/static-linux-x86-64/include/") - } - kotlinOptions.freeCompilerArgs = listOf( - "-include-binary", "${project.rootDir}/sodiumWrapper/static-linux-x86-64/lib/libsodium.a" - ) - } } @@ -99,15 +89,6 @@ kotlin { staticLib { } } - compilations.getByName("main") { - val libsodiumCinterop by cinterops.creating { - defFile(project.file("src/nativeInterop/cinterop/libsodium.def")) - compilerOpts.add("-I${project.rootDir}/sodiumWrapper/static-arm64/include/") - } - kotlinOptions.freeCompilerArgs = listOf( - "-include-binary", "${project.rootDir}/sodiumWrapper/static-arm64/lib/libsodium.a" - ) - } } // Linux 32 is using target-sysroot-2-raspberrypi which is missing getrandom and explicit_bzero in stdlib // so konanc can't build klib because getrandom missing will cause sodium_misuse() @@ -296,8 +277,11 @@ kotlin { //Set up shared source sets //linux, linuxArm32Hfp, linuxArm64 val linux64Bit = setOf( - "linuxX64", "linuxArm64" + "linuxX64" ) + val linuxArm64Bit = setOf { + "linuxArm64" + } val linux32Bit = setOf( "" // "linuxArm32Hfp" ) @@ -330,9 +314,29 @@ kotlin { compilations.getByName("main") { if (linux64Bit.contains(this@withType.name)) { defaultSourceSet.dependsOn(nativeMain) + compilations.getByName("main") { + val libsodiumCinterop by cinterops.creating { + defFile(project.file("src/nativeInterop/cinterop/libsodium.def")) + compilerOpts.add("-I${project.rootDir}/sodiumWrapper/static-linux-x86-64/include/") + } + kotlinOptions.freeCompilerArgs = listOf( + "-include-binary", "${project.rootDir}/sodiumWrapper/static-linux-x86-64/lib/libsodium.a" + ) + } + } + if (linuxArm64Bit.contains(this@withType.name)) { + compilations.getByName("main") { + val libsodiumCinterop by cinterops.creating { + defFile(project.file("src/nativeInterop/cinterop/libsodium.def")) + compilerOpts.add("-I${project.rootDir}/sodiumWrapper/static-arm64/include/") + } + kotlinOptions.freeCompilerArgs = listOf( + "-include-binary", "${project.rootDir}/sodiumWrapper/static-arm64/lib/libsodium.a" + ) + } } if (linux32Bit.contains(this@withType.name)) { - defaultSourceSet.dependsOn(createWorkaround32bitNativeMainSourceSet(this@withType.name, nativeDependencies)) + defaultSourceSet.dependsOn(createWorkaroundNativeMainSourceSet(this@withType.name, nativeDependencies)) } if (macos64Bit.contains(this@withType.name)) { defaultSourceSet.dependsOn(createWorkaroundNativeMainSourceSet(this@withType.name, nativeDependencies)) @@ -351,7 +355,7 @@ kotlin { } if (ios32Bit.contains(this@withType.name)) { - defaultSourceSet.dependsOn(createWorkaround32bitNativeMainSourceSet(this@withType.name, nativeDependencies)) + defaultSourceSet.dependsOn(createWorkaroundNativeMainSourceSet(this@withType.name, nativeDependencies)) println("Setting ios cinterop for $this") val libsodiumCinterop by cinterops.creating { defFile(project.file("src/nativeInterop/cinterop/libsodium.def")) @@ -375,7 +379,7 @@ kotlin { } if (watchos32Bit.contains(this@withType.name)) { - defaultSourceSet.dependsOn(createWorkaround32bitNativeMainSourceSet(this@withType.name, nativeDependencies)) + defaultSourceSet.dependsOn(createWorkaroundNativeMainSourceSet(this@withType.name, nativeDependencies)) println("Setting ios cinterop for $this") val libsodiumCinterop by cinterops.creating { defFile(project.file("src/nativeInterop/cinterop/libsodium.def")) diff --git a/multiplatform-crypto-delegated/src/nativeMain/kotlin/com/ionspin/kotlin/crypto/hash/blake2b/Blake2bDelegated.kt b/multiplatform-crypto-delegated/src/nativeMain/kotlin/com/ionspin/kotlin/crypto/hash/blake2b/Blake2bDelegated.kt index 5273742..705f583 100644 --- a/multiplatform-crypto-delegated/src/nativeMain/kotlin/com/ionspin/kotlin/crypto/hash/blake2b/Blake2bDelegated.kt +++ b/multiplatform-crypto-delegated/src/nativeMain/kotlin/com/ionspin/kotlin/crypto/hash/blake2b/Blake2bDelegated.kt @@ -70,15 +70,16 @@ actual object Blake2bDelegatedStateless : Blake2bStateless { override fun digest(inputMessage: UByteArray, key: UByteArray, hashLength: Int): UByteArray { val hashResult = UByteArray(MAX_HASH_BYTES) - + val hashResultPinned = hashResult.pin() crypto_generichash( - StableRef.create(hashResult).asCPointer().reinterpret(), + hashResultPinned.addressOf(0), hashLength.convert(), inputMessage.toCValues(), inputMessage.size.convert(), key.toCValues(), key.size.convert() ?: 0UL ) + hashResultPinned.unpin() return hashResult } diff --git a/sample/src/commonMain/kotlin/com/ionspin/kotlin/crypto/sample/Sample.kt b/sample/src/commonMain/kotlin/com/ionspin/kotlin/crypto/sample/Sample.kt index 3aa1624..d27e1dd 100644 --- a/sample/src/commonMain/kotlin/com/ionspin/kotlin/crypto/sample/Sample.kt +++ b/sample/src/commonMain/kotlin/com/ionspin/kotlin/crypto/sample/Sample.kt @@ -23,6 +23,7 @@ object Sample { blake2bUpdateable.update("test") println(blake2bUpdateable.digest().toHexString()) println("Blake2b stateless") - println("Blake2b stateless: ${Crypto.Blake2b.stateless("test".encodeToByteArray().toUByteArray())}") + val statelessResult = Crypto.Blake2b.stateless("test".encodeToByteArray().toUByteArray()) + println("Blake2b stateless: ${statelessResult.toHexString()}") } } \ No newline at end of file