diff --git a/.gitignore b/.gitignore index 36ccec6..c1ba8d3 100644 --- a/.gitignore +++ b/.gitignore @@ -17,3 +17,6 @@ build/ /sodiumWrapper/lib/ /sodiumWrapper/ios-include/ /sodiumWrapper/ios-lib/ +/sodiumWrapper/static-arm64/ +/sodiumWrapper/static-arm32/ +/sodiumWrapper/static-ios/ diff --git a/buildSrc/src/main/kotlin/Deps.kt b/buildSrc/src/main/kotlin/Deps.kt index 4965bea..f695578 100644 --- a/buildSrc/src/main/kotlin/Deps.kt +++ b/buildSrc/src/main/kotlin/Deps.kt @@ -21,6 +21,7 @@ object Versions { val atomicfu = "0.14.3-SNAPSHOT" //NOTE: my linux arm32 and arm64 build val nodePlugin = "1.3.0" val dokkaPlugin = "0.9.18" + val taskTreePlugin = "1.5" val kotlinBigNumVersion = "0.1.6-SNAPSHOT" @@ -99,5 +100,6 @@ object PluginsDeps { val mavenPublish = "maven-publish" val signing = "signing" val dokka = "org.jetbrains.dokka" + val taskTree = "com.dorongold.task-tree" } diff --git a/multiplatform-crypto-api/build.gradle.kts b/multiplatform-crypto-api/build.gradle.kts index b251aa8..096e9d8 100644 --- a/multiplatform-crypto-api/build.gradle.kts +++ b/multiplatform-crypto-api/build.gradle.kts @@ -82,10 +82,6 @@ kotlin { } } - - } - - runningOnLinuxArm64 { //Not supported in OFFICIAL coroutines at the moment linuxArm64() { binaries { @@ -93,6 +89,12 @@ kotlin { } } } + + + } + + runningOnLinuxArm64 { + } runningOnLinuxArm32 { diff --git a/multiplatform-crypto-delegated/build.gradle.kts b/multiplatform-crypto-delegated/build.gradle.kts index 35f2dd2..b6fa5cf 100644 --- a/multiplatform-crypto-delegated/build.gradle.kts +++ b/multiplatform-crypto-delegated/build.gradle.kts @@ -27,7 +27,7 @@ plugins { id(PluginsDeps.signing) id(PluginsDeps.node) version Versions.nodePlugin id(PluginsDeps.dokka) version Versions.dokkaPlugin - id("com.dorongold.task-tree") version "1.5" + id(PluginsDeps.taskTree) version Versions.taskTreePlugin } val sonatypeStaging = "https://oss.sonatype.org/service/local/staging/deploy/maven2/" @@ -84,12 +84,6 @@ kotlin { } - } - - - //Not supported in OFFICIAL coroutines at the moment (we're running a custom build) - runningOnLinuxArm64 { - println("Configuring Linux Arm 64 targets") linuxArm64() { binaries { staticLib { @@ -97,6 +91,14 @@ kotlin { } } + + } + + + //Not supported in OFFICIAL coroutines at the moment (we're running a custom build) + runningOnLinuxArm64 { + println("Configuring Linux Arm 64 targets") + } runningOnLinuxArm32 { @@ -235,13 +237,23 @@ kotlin { if ((this@withType.name.contains("ios") || this@withType.name.contains("tvos") || this@withType.name.contains("watchos")).not()) { defaultSourceSet.dependsOn(createWorkaroundNativeMainSourceSet(this@withType.name, nativeDependencies)) println("Setting cinterop for $this") - val libsodiumCinterop by cinterops.creating { - defFile(project.file("src/nativeInterop/cinterop/libsodium.def")) - compilerOpts.add("-I${project.rootDir}/sodiumWrapper/include/") + if (this@withType.name.contains("arm64")) { + val libsodiumCinterop by cinterops.creating { + defFile(project.file("src/nativeInterop/cinterop/libsodium.def")) + compilerOpts.add("-I${project.rootDir}/sodiumWrapper/libsodium-arm64/include/") + } + kotlinOptions.freeCompilerArgs = listOf( + "-include-binary", "${project.rootDir}/sodiumWrapper/libsodium-arm64/lib/libsodium.a" + ) + } else { + val libsodiumCinterop by cinterops.creating { + defFile(project.file("src/nativeInterop/cinterop/libsodium.def")) + compilerOpts.add("-I${project.rootDir}/sodiumWrapper/include/") + } + kotlinOptions.freeCompilerArgs = listOf( + "-include-binary", "${project.rootDir}/sodiumWrapper/lib/libsodium.a" + ) } - kotlinOptions.freeCompilerArgs = listOf( - "-include-binary", "${project.rootDir}/sodiumWrapper/lib/libsodium.a" - ) } if (this@withType.name.contains("ios") || this@withType.name.contains("tvos") || this@withType.name.contains("watchos")) { defaultSourceSet.dependsOn(createWorkaroundNativeMainSourceSet(this@withType.name, nativeDependencies)) 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 new file mode 100644 index 0000000..c537533 --- /dev/null +++ b/multiplatform-crypto-delegated/src/native32Main/kotlin/com/ionspin/kotlin/crypto/Initializer.kt @@ -0,0 +1,19 @@ +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 new file mode 100644 index 0000000..b8c3f28 --- /dev/null +++ b/multiplatform-crypto-delegated/src/native32Main/kotlin/com/ionspin/kotlin/crypto/SRNG.kt @@ -0,0 +1,40 @@ +/* + * 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.toULong()) + 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 new file mode 100644 index 0000000..619ac9e --- /dev/null +++ b/multiplatform-crypto-delegated/src/native32Main/kotlin/com/ionspin/kotlin/crypto/hash/blake2b/Blake2bDelegated.kt @@ -0,0 +1,70 @@ +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 + */ + +@ExperimentalUnsignedTypes +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.toULong(), + inputString.encodeToByteArray().toUByteArray().toCValues(), + inputString.length.toULong(), + key?.run { this.encodeToByteArray().toUByteArray().toCValues() }, + key?.length?.toULong() ?: 0UL + ) + 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.toULong(), + inputMessage.toCValues(), + inputMessage.size.toULong(), + key.toCValues(), + key.size.toULong() ?: 0UL + ) + 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 new file mode 100644 index 0000000..9a71168 --- /dev/null +++ b/multiplatform-crypto-delegated/src/native32Test/kotlin/com/ionspin/kotlin/crypto/hash/blake2b/Blake2bLinuxTest.kt @@ -0,0 +1,30 @@ +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 new file mode 100644 index 0000000..8fb509a --- /dev/null +++ b/multiplatform-crypto-delegated/src/native32Test/kotlin/com/ionspin/kotlin/crypto/util/testBlocking.kt @@ -0,0 +1,27 @@ +/* + * 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/sample/build.gradle.kts b/sample/build.gradle.kts index d6f45dc..5f7eb2a 100644 --- a/sample/build.gradle.kts +++ b/sample/build.gradle.kts @@ -104,19 +104,19 @@ kotlin { } } //Not supported in coroutines at the moment -// linuxArm32Hfp() { -// binaries { -// staticLib { -// } -// } -// } + linuxArm32Hfp() { + binaries { + staticLib { + } + } + } //Not supported in coroutines at the moment -// linuxArm64() { -// binaries { -// staticLib { -// } -// } -// } + linuxArm64() { + binaries { + staticLib { + } + } + } } diff --git a/settings.gradle.kts b/settings.gradle.kts index 31a58a2..fa503b5 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -37,5 +37,5 @@ rootProject.name = "KotlinMultiplatformCrypto" include("multiplatform-crypto-api") include("multiplatform-crypto") include("multiplatform-crypto-delegated") -//include("sample") +include("sample") diff --git a/sodiumWrapper/configureLinuxArm64.sh b/sodiumWrapper/configureLinuxArm64.sh new file mode 100755 index 0000000..3232ffc --- /dev/null +++ b/sodiumWrapper/configureLinuxArm64.sh @@ -0,0 +1,8 @@ +#! /bin/sh +export PREFIX="$(pwd)/libsodium-ios" +export CC=/usr/bin/aarch64-linux-gnu-gcc +export TARGET_ARCH=armv8-a +export CFLAGS="-Os -march=${TARGET_ARCH}" +cd libsodium +./configure --prefix=$PREFIX --host=aarch64-linux-gnu-gcc --with-sysroot=/home/ionspin/.konan/dependencies/target-sysroot-1-linux-glibc-arm64/ "$@" +