From 459131f08211c93f21bcd5b021aca7036bfb4767 Mon Sep 17 00:00:00 2001 From: Ugljesa Jovanovic Date: Sat, 6 Jun 2020 15:07:20 +0200 Subject: [PATCH] Remove 32bit native set, and use convert instead --- .../build.gradle.kts | 37 +--------- .../com/ionspin/kotlin/crypto/Initializer.kt | 19 ----- .../kotlin/com/ionspin/kotlin/crypto/SRNG.kt | 40 ----------- .../crypto/hash/blake2b/Blake2bDelegated.kt | 70 ------------------- .../crypto/hash/blake2b/Blake2bLinuxTest.kt | 30 -------- .../kotlin/crypto/util/testBlocking.kt | 27 ------- .../crypto/hash/blake2b/Blake2bDelegated.kt | 7 +- .../ionspin/kotlin/crypto/sample/Runner.kt | 8 +-- 8 files changed, 9 insertions(+), 229 deletions(-) delete mode 100644 multiplatform-crypto-delegated/src/native32Main/kotlin/com/ionspin/kotlin/crypto/Initializer.kt delete mode 100644 multiplatform-crypto-delegated/src/native32Main/kotlin/com/ionspin/kotlin/crypto/SRNG.kt delete mode 100644 multiplatform-crypto-delegated/src/native32Main/kotlin/com/ionspin/kotlin/crypto/hash/blake2b/Blake2bDelegated.kt delete mode 100644 multiplatform-crypto-delegated/src/native32Test/kotlin/com/ionspin/kotlin/crypto/hash/blake2b/Blake2bLinuxTest.kt delete mode 100644 multiplatform-crypto-delegated/src/native32Test/kotlin/com/ionspin/kotlin/crypto/util/testBlocking.kt diff --git a/multiplatform-crypto-delegated/build.gradle.kts b/multiplatform-crypto-delegated/build.gradle.kts index d4f365f..9ffd7c1 100644 --- a/multiplatform-crypto-delegated/build.gradle.kts +++ b/multiplatform-crypto-delegated/build.gradle.kts @@ -293,21 +293,6 @@ kotlin { } } - val native32Main by creating { - dependsOn(commonMain) - dependencies { - nativeDependencies(this) - } - } - - val native32Test by creating { - dependsOn(commonTest) - dependencies { - implementation(Deps.Native.coroutines) - } - } - - //Set up shared source sets //linux, linuxArm32Hfp, linuxArm64 val linux64Bit = setOf( @@ -406,13 +391,7 @@ kotlin { } compilations.getByName("test") { println("Setting native test dep for $this@withType.name") - if (linux64Bit.contains(this@withType.name) || - macos64Bit.contains(this@withType.name) || - mingw64Bit.contains(this@withType.name)) { - defaultSourceSet.dependsOn(nativeTest) - } else { - defaultSourceSet.dependsOn(native32Test) - } + defaultSourceSet.dependsOn(nativeTest) } @@ -465,19 +444,7 @@ kotlin { kotlin.srcDir("src/nativeTest") } } - //can still be useful for cinterop and debugging -// val linuxArm32HfpMain by getting { -// dependsOn(native32Main) -// isRunningInIdea { -// kotlin.srcDir("src/native32Main/kotlin") -// } -// } -// val linuxArm32HfpTest by getting { -// dependsOn(native32Test) -// isRunningInIdea { -// kotlin.srcDir("src/native32Test/kotlin") -// } -// } + } runningOnMacos { diff --git a/multiplatform-crypto-delegated/src/native32Main/kotlin/com/ionspin/kotlin/crypto/Initializer.kt b/multiplatform-crypto-delegated/src/native32Main/kotlin/com/ionspin/kotlin/crypto/Initializer.kt deleted file mode 100644 index c537533..0000000 --- a/multiplatform-crypto-delegated/src/native32Main/kotlin/com/ionspin/kotlin/crypto/Initializer.kt +++ /dev/null @@ -1,19 +0,0 @@ -package com.ionspin.kotlin.crypto - -import kotlinx.coroutines.GlobalScope -import kotlinx.coroutines.launch -import libsodium.sodium_init - -actual object Initializer { - actual suspend fun initialize() { - GlobalScope.launch { - sodium_init() - } - - } - - actual fun initializeWithCallback(done: () -> Unit) { - sodium_init() - done() - } -} \ No newline at end of file diff --git a/multiplatform-crypto-delegated/src/native32Main/kotlin/com/ionspin/kotlin/crypto/SRNG.kt b/multiplatform-crypto-delegated/src/native32Main/kotlin/com/ionspin/kotlin/crypto/SRNG.kt deleted file mode 100644 index a3e734f..0000000 --- a/multiplatform-crypto-delegated/src/native32Main/kotlin/com/ionspin/kotlin/crypto/SRNG.kt +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright 2019 Ugljesa Jovanovic - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.ionspin.kotlin.crypto - -import kotlinx.cinterop.* -import libsodium.randombytes_buf -import platform.posix.* -//import libsod - -/** - * Created by Ugljesa Jovanovic - * ugljesa.jovanovic@ionspin.com - * on 21-Sep-2019 - */ -actual object SRNG { - @Suppress("EXPERIMENTAL_UNSIGNED_LITERALS") - actual fun getRandomBytes(amount: Int): UByteArray { - memScoped { - val array = allocArray(amount) - randombytes_buf(array, amount.toUInt()) - return UByteArray(amount) { - array[it] - } - } - } -} \ No newline at end of file diff --git a/multiplatform-crypto-delegated/src/native32Main/kotlin/com/ionspin/kotlin/crypto/hash/blake2b/Blake2bDelegated.kt b/multiplatform-crypto-delegated/src/native32Main/kotlin/com/ionspin/kotlin/crypto/hash/blake2b/Blake2bDelegated.kt deleted file mode 100644 index 9a2c49b..0000000 --- a/multiplatform-crypto-delegated/src/native32Main/kotlin/com/ionspin/kotlin/crypto/hash/blake2b/Blake2bDelegated.kt +++ /dev/null @@ -1,70 +0,0 @@ -package com.ionspin.kotlin.crypto.hash.blake2b -import com.ionspin.kotlin.crypto.util.toHexString -import kotlinx.cinterop.* -import libsodium.* -/** - * Created by Ugljesa Jovanovic - * ugljesa.jovanovic@ionspin.com - * on 14-Jul-2019 - */ - - -actual class Blake2bDelegated actual constructor(key: UByteArray?, hashLength: Int) : Blake2b { - override val MAX_HASH_BYTES: Int = 64 - - override fun update(data: UByteArray) { - TODO("not implemented yet") - } - - override fun update(data: String) { - TODO("not implemented yet") - } - - override fun digest(): UByteArray { - val result = sodium_init() - println("Sodium init $result") - return ubyteArrayOf(0U) - } - - override fun digestString(): String { - TODO("not implemented yet") - } -} - -@Suppress("EXPERIMENTAL_API_USAGE", "EXPERIMENTAL_UNSIGNED_LITERALS") -actual object Blake2bStateless : Blake2bStatelessInterface { - override fun digest(inputString: String, key: String?, hashLength: Int): UByteArray { - val hashResult = UByteArray(MAX_HASH_BYTES) - val hashResultPinned = hashResult.pin() - crypto_generichash( - hashResultPinned.addressOf(0), - hashLength.toUInt(), - inputString.encodeToByteArray().toUByteArray().toCValues(), - inputString.length.toULong(), - key?.run { this.encodeToByteArray().toUByteArray().toCValues() }, - key?.length?.toUInt() ?: 0U - ) - println("HashPointer: ${hashResult.toHexString()}") - println(hashResult.toHexString()) - return hashResult - } - - override fun digest(inputMessage: UByteArray, key: UByteArray, hashLength: Int): UByteArray { - val hashResult = UByteArray(MAX_HASH_BYTES) - - crypto_generichash( - StableRef.create(hashResult).asCPointer().reinterpret(), - hashLength.toUInt(), - inputMessage.toCValues(), - inputMessage.size.toULong(), - key.toCValues(), - key.size.toUInt() ?: 0U - ) - println("HashPointer: ${hashResult.toHexString()}") - println(hashResult.toHexString()) - return hashResult - - } - - -} \ No newline at end of file diff --git a/multiplatform-crypto-delegated/src/native32Test/kotlin/com/ionspin/kotlin/crypto/hash/blake2b/Blake2bLinuxTest.kt b/multiplatform-crypto-delegated/src/native32Test/kotlin/com/ionspin/kotlin/crypto/hash/blake2b/Blake2bLinuxTest.kt deleted file mode 100644 index 9a71168..0000000 --- a/multiplatform-crypto-delegated/src/native32Test/kotlin/com/ionspin/kotlin/crypto/hash/blake2b/Blake2bLinuxTest.kt +++ /dev/null @@ -1,30 +0,0 @@ -package com.ionspin.kotlin.crypto.hash.blake2b - -/** - * Created by Ugljesa Jovanovic - * ugljesa.jovanovic@ionspin.com - * on 24-May-2020 - */ - -import com.ionspin.kotlin.crypto.hash.blake2b.Blake2bStateless -import com.ionspin.kotlin.crypto.util.testBlocking -import interop.* -import kotlinx.cinterop.* -import libsodium.* - -import kotlin.test.Test - -class Blake2bLinuxTest { - - @Test - fun testCinterop() { - val sodiumInitResult = sodium_init() - println("Sodium init $sodiumInitResult") - println("1") - } - - @Test - fun testBlake2BSodiumInterop() = testBlocking { - Blake2bStateless.digest("test") - } -} \ No newline at end of file diff --git a/multiplatform-crypto-delegated/src/native32Test/kotlin/com/ionspin/kotlin/crypto/util/testBlocking.kt b/multiplatform-crypto-delegated/src/native32Test/kotlin/com/ionspin/kotlin/crypto/util/testBlocking.kt deleted file mode 100644 index 8fb509a..0000000 --- a/multiplatform-crypto-delegated/src/native32Test/kotlin/com/ionspin/kotlin/crypto/util/testBlocking.kt +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Copyright 2019 Ugljesa Jovanovic - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.ionspin.kotlin.crypto.util - -import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.runBlocking - -/** - * Created by Ugljesa Jovanovic - * ugljesa.jovanovic@ionspin.com - * on 20-Jul-2019 - */ -actual fun testBlocking(block: suspend () -> Unit) = runBlocking { block() } \ No newline at end of file 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 ad36182..e2bb35c 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 @@ -18,14 +18,16 @@ actual class Blake2bDelegated actual constructor(key: UByteArray?, hashLength: I init { println("Initializing libsodium hash") requestedHashLength = hashLength + println("Size ${crypto_generichash_state.size}") + println("Align ${crypto_generichash_state.align}") state = nativeHeap.alloc() println("allocated state") - crypto_generichash_init(state.ptr, key?.run { this.toUByteArray().toCValues() }, key?.size?.toULong() ?: 0UL, hashLength.toULong()) + crypto_generichash_init(state.ptr, key?.run { this.toUByteArray().toCValues() }, key?.size?.toULong() ?: 0UL, hashLength.convert()) println("Initialized libsodium hash") } override fun update(data: UByteArray) { - crypto_generichash_update(state.ptr, data.toCValues(), data.size.toULong()) + crypto_generichash_update(state.ptr, data.toCValues(), data.size.convert()) } override fun update(data: String) { @@ -38,6 +40,7 @@ actual class Blake2bDelegated actual constructor(key: UByteArray?, hashLength: I val hashResultPinned = hashResult.pin() val result = crypto_generichash_final(state.ptr, hashResultPinned.addressOf(0), requestedHashLength.toULong()) println("HashPointer: ${hashResult.toHexString()}") + return hashResult // val inputString = "test" // val hashLength = 64 diff --git a/sample/src/linuxMain/kotlin/com/ionspin/kotlin/crypto/sample/Runner.kt b/sample/src/linuxMain/kotlin/com/ionspin/kotlin/crypto/sample/Runner.kt index 75ad5b8..19a602c 100644 --- a/sample/src/linuxMain/kotlin/com/ionspin/kotlin/crypto/sample/Runner.kt +++ b/sample/src/linuxMain/kotlin/com/ionspin/kotlin/crypto/sample/Runner.kt @@ -1,15 +1,11 @@ import com.ionspin.kotlin.crypto.hash.blake2b.Blake2bDelegated import com.ionspin.kotlin.crypto.hash.blake2b.Blake2bStateless +import com.ionspin.kotlin.crypto.sample.Sample import kotlin.time.ExperimentalTime import kotlin.time.measureTime @ExperimentalTime fun main() { - println("Test") -// Blake - val blake = Blake2bDelegated() - blake.digest() - println("Result: ${Blake2bStateless.digest("test")}") - + Sample.runSample() }