From da85fefb9bcb94f6111b5f791d00c457de322b8e Mon Sep 17 00:00:00 2001 From: Ugljesa Jovanovic Date: Fri, 30 Apr 2021 22:10:02 +0200 Subject: [PATCH] Use UInt in kdf derivation, because that's the most js can support more or less. I'll try to figure out if we can use all 52bits lates. Also dont show arm build to intellij as it is checking if there is coroutines build for arm and fails with the most unusefull and misleading error I have seen so far. --- .../build.gradle.kts | 76 ++++++++++--------- .../com.ionspin.kotlin.crypto/kdf/Kdf.kt | 8 +- .../com/ionspin/kotlin/crypto/kdf/KdfTest.kt | 6 +- .../com/ionspin/kotlin/crypto/kdf/Kdf.kt | 8 +- .../com/ionspin/kotlin/crypto/kdf/Kdf.kt | 6 +- .../com/ionspin/kotlin/crypto/kdf/Kdf.kt | 8 +- 6 files changed, 60 insertions(+), 52 deletions(-) diff --git a/multiplatform-crypto-libsodium-bindings/build.gradle.kts b/multiplatform-crypto-libsodium-bindings/build.gradle.kts index 95f34c4..e7f3beb 100644 --- a/multiplatform-crypto-libsodium-bindings/build.gradle.kts +++ b/multiplatform-crypto-libsodium-bindings/build.gradle.kts @@ -71,6 +71,9 @@ android { getByName("release") { isMinifyEnabled = false } + getByName("debug") { + isMinifyEnabled = false + } } sourceSets.getByName("main") { // jniLibs.srcDir("src/androidMain/libs") @@ -122,22 +125,23 @@ kotlin { } } - - linuxArm64() { - binaries { - staticLib { + if (ideaActive.not()) { + linuxArm64() { + binaries { + staticLib { + } } } + // 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() + // ld.lld: error: undefined symbol: explicit_bzero + // >>> referenced by utils.c + // >>> libsodium_la-utils.o:(sodium_memzero) in archive /tmp/included11051337748775083797/libsodium.a + // + // ld.lld: error: undefined symbol: getrandom + // >>> referenced by randombytes_sysrandom.c + // >>> libsodium_la-randombytes_sysrandom.o:(_randombytes_linux_getrandom) in archive /tmp/included11051337748775083797/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() - // ld.lld: error: undefined symbol: explicit_bzero - // >>> referenced by utils.c - // >>> libsodium_la-utils.o:(sodium_memzero) in archive /tmp/included11051337748775083797/libsodium.a - // - // ld.lld: error: undefined symbol: getrandom - // >>> referenced by randombytes_sysrandom.c - // >>> libsodium_la-randombytes_sysrandom.o:(_randombytes_linux_getrandom) in archive /tmp/included11051337748775083797/libsodium.a } @@ -292,7 +296,11 @@ kotlin { "linuxX64" ) val linuxArm64Bit = setOf( - "linuxArm64" + if (ideaActive.not()) { + "linuxArm64" + } else { + "" + } ) val linux32Bit = setOf( "" // "linuxArm32Hfp" @@ -441,29 +449,29 @@ kotlin { } } - val androidMain by getting { - isNotRunningInIdea { - kotlin.srcDirs("src/androidMain", "src/androidSpecific", "src/jvmMain/kotlin") - } - isRunningInIdea { - kotlin.srcDirs("src/androidSpecific", "src/jvmMain/kotlin") - } - dependencies { - implementation("net.java.dev.jna:jna:5.5.0@aar") - implementation(Deps.Jvm.resourceLoader) { - exclude("net.java.dev.jna", "jna") - } + val androidMain by getting { + isNotRunningInIdea { + kotlin.srcDirs("src/androidMain", "src/androidSpecific", "src/jvmMain/kotlin") + } + isRunningInIdea { + kotlin.srcDirs("src/androidSpecific", "src/jvmMain/kotlin") + } + dependencies { + implementation("net.java.dev.jna:jna:5.5.0@aar") + implementation(Deps.Jvm.resourceLoader) { + exclude("net.java.dev.jna", "jna") } } + } - val androidTest by getting { - dependencies { - implementation(kotlin(Deps.Jvm.test)) - implementation(kotlin(Deps.Jvm.testJUnit)) - implementation("androidx.test:runner:1.2.0") - implementation("androidx.test:rules:1.2.0") - } + val androidTest by getting { + dependencies { +// implementation(kotlin(Deps.Jvm.test)) +// implementation(kotlin(Deps.Jvm.testJUnit)) +// implementation("androidx.test:runner:1.2.0") +// implementation("androidx.test:rules:1.2.0") } + } val jvmMain by getting { kotlin.srcDirs("src/jvmSpecific", "src/jvmMain/kotlin") @@ -650,7 +658,7 @@ tasks { val jsBrowserTest by getting(KotlinJsTest::class) { testLogging { events("PASSED", "FAILED", "SKIPPED") - showStandardStreams = true + showStandardStreams = true } } diff --git a/multiplatform-crypto-libsodium-bindings/src/commonMain/kotlin/com.ionspin.kotlin.crypto/kdf/Kdf.kt b/multiplatform-crypto-libsodium-bindings/src/commonMain/kotlin/com.ionspin.kotlin.crypto/kdf/Kdf.kt index 4c64812..8f5506f 100644 --- a/multiplatform-crypto-libsodium-bindings/src/commonMain/kotlin/com.ionspin.kotlin.crypto/kdf/Kdf.kt +++ b/multiplatform-crypto-libsodium-bindings/src/commonMain/kotlin/com.ionspin.kotlin.crypto/kdf/Kdf.kt @@ -11,9 +11,9 @@ const val crypto_kdf_KEYBYTES = 32 expect object Kdf { /** - * The crypto_kdf_derive_from_key() function derives a subkey_id-th subkey subkey of length subkey_len bytes using + * The deriveFromKey function derives a subkeyId-th subkey of length subkeyLenght bytes using * the master key key and the context ctx. - * subkey_id can be any value up to (2^64)-1. + * subkey_id can be any value up to (2^32) because javascript doesn't support long types. * subkey_len has to be between crypto_kdf_BYTES_MIN (inclusive) and crypto_kdf_BYTES_MAX (inclusive). * Similar to a type, the context ctx is a 8 characters string describing what the key is going to be used for. * Its purpose is to mitigate accidental bugs by separating domains. The same function used with the same key but @@ -24,10 +24,10 @@ expect object Kdf { * If more convenient, it is also fine to use a single global context for a whole application. This will still * prevent the same keys from being mistakenly used by another application. */ - fun deriveFromKey(subkeyId: Int, subkeyLength: Int, context: String, masterKey: UByteArray) : UByteArray + fun deriveFromKey(subkeyId: UInt, subkeyLength: Int, context: String, masterKey: UByteArray) : UByteArray /** * The crypto_kdf_keygen() function creates a master key. */ fun keygen() : UByteArray -} \ No newline at end of file +} diff --git a/multiplatform-crypto-libsodium-bindings/src/commonTest/kotlin/com/ionspin/kotlin/crypto/kdf/KdfTest.kt b/multiplatform-crypto-libsodium-bindings/src/commonTest/kotlin/com/ionspin/kotlin/crypto/kdf/KdfTest.kt index 3530c2d..ff66df2 100644 --- a/multiplatform-crypto-libsodium-bindings/src/commonTest/kotlin/com/ionspin/kotlin/crypto/kdf/KdfTest.kt +++ b/multiplatform-crypto-libsodium-bindings/src/commonTest/kotlin/com/ionspin/kotlin/crypto/kdf/KdfTest.kt @@ -15,15 +15,15 @@ class KdfTest { fun testKdf() = runTest { LibsodiumInitializer.initializeWithCallback { val masterKey = Kdf.keygen() - val subkey1 = Kdf.deriveFromKey(1, crypto_kdf_BYTES_MAX, "test1234", masterKey) - val subkey2 = Kdf.deriveFromKey(2, crypto_kdf_BYTES_MAX, "test1234", masterKey) + val subkey1 = Kdf.deriveFromKey(1U, crypto_kdf_BYTES_MAX, "test1234", masterKey) + val subkey2 = Kdf.deriveFromKey(2U, crypto_kdf_BYTES_MAX, "test1234", masterKey) assertTrue { subkey1.size == crypto_kdf_BYTES_MAX && subkey2.size == crypto_kdf_BYTES_MAX } - val repeatSubkey1 = Kdf.deriveFromKey(1, crypto_kdf_BYTES_MAX, "test1234", masterKey) + val repeatSubkey1 = Kdf.deriveFromKey(1U, crypto_kdf_BYTES_MAX, "test1234", masterKey) assertTrue { subkey1.contentEquals(repeatSubkey1) } diff --git a/multiplatform-crypto-libsodium-bindings/src/jsMain/kotlin/com/ionspin/kotlin/crypto/kdf/Kdf.kt b/multiplatform-crypto-libsodium-bindings/src/jsMain/kotlin/com/ionspin/kotlin/crypto/kdf/Kdf.kt index 1866909..028a19e 100644 --- a/multiplatform-crypto-libsodium-bindings/src/jsMain/kotlin/com/ionspin/kotlin/crypto/kdf/Kdf.kt +++ b/multiplatform-crypto-libsodium-bindings/src/jsMain/kotlin/com/ionspin/kotlin/crypto/kdf/Kdf.kt @@ -6,9 +6,9 @@ import ext.libsodium.com.ionspin.kotlin.crypto.toUInt8Array actual object Kdf { /** - * The crypto_kdf_derive_from_key() function derives a subkey_id-th subkey subkey of length subkey_len bytes using + * The deriveFromKey function derives a subkeyId-th subkey of length subkeyLenght bytes using * the master key key and the context ctx. - * subkey_id can be any value up to (2^64)-1. + * subkey_id can be any value up to (2^32) because javascript doesn't support long types. * subkey_len has to be between crypto_kdf_BYTES_MIN (inclusive) and crypto_kdf_BYTES_MAX (inclusive). * Similar to a type, the context ctx is a 8 characters string describing what the key is going to be used for. * Its purpose is to mitigate accidental bugs by separating domains. The same function used with the same key but @@ -20,7 +20,7 @@ actual object Kdf { * prevent the same keys from being mistakenly used by another application. */ actual fun deriveFromKey( - subkeyId: Int, + subkeyId: UInt, subkeyLength: Int, context: String, masterKey: UByteArray @@ -40,4 +40,4 @@ actual object Kdf { return getSodium().crypto_kdf_keygen().toUByteArray() } -} \ No newline at end of file +} diff --git a/multiplatform-crypto-libsodium-bindings/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/kdf/Kdf.kt b/multiplatform-crypto-libsodium-bindings/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/kdf/Kdf.kt index 8a66305..8953e45 100644 --- a/multiplatform-crypto-libsodium-bindings/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/kdf/Kdf.kt +++ b/multiplatform-crypto-libsodium-bindings/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/kdf/Kdf.kt @@ -4,9 +4,9 @@ import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodiumJna actual object Kdf { /** - * The crypto_kdf_derive_from_key() function derives a subkey_id-th subkey subkey of length subkey_len bytes using + * The deriveFromKey function derives a subkeyId-th subkey of length subkeyLenght bytes using * the master key key and the context ctx. - * subkey_id can be any value up to (2^64)-1. + * subkey_id can be any value up to (2^32) because javascript doesn't support long types * subkey_len has to be between crypto_kdf_BYTES_MIN (inclusive) and crypto_kdf_BYTES_MAX (inclusive). * Similar to a type, the context ctx is a 8 characters string describing what the key is going to be used for. * Its purpose is to mitigate accidental bugs by separating domains. The same function used with the same key but @@ -18,7 +18,7 @@ actual object Kdf { * prevent the same keys from being mistakenly used by another application. */ actual fun deriveFromKey( - subkeyId: Int, + subkeyId: UInt, subkeyLength: Int, context: String, masterKey: UByteArray diff --git a/multiplatform-crypto-libsodium-bindings/src/nativeMain/kotlin/com/ionspin/kotlin/crypto/kdf/Kdf.kt b/multiplatform-crypto-libsodium-bindings/src/nativeMain/kotlin/com/ionspin/kotlin/crypto/kdf/Kdf.kt index 972ce15..1716cc2 100644 --- a/multiplatform-crypto-libsodium-bindings/src/nativeMain/kotlin/com/ionspin/kotlin/crypto/kdf/Kdf.kt +++ b/multiplatform-crypto-libsodium-bindings/src/nativeMain/kotlin/com/ionspin/kotlin/crypto/kdf/Kdf.kt @@ -10,9 +10,9 @@ import libsodium.crypto_kdf_keygen actual object Kdf { /** - * The crypto_kdf_derive_from_key() function derives a subkey_id-th subkey subkey of length subkey_len bytes using + * The deriveFromKey function derives a subkeyId-th subkey of length subkeyLenght bytes using * the master key key and the context ctx. - * subkey_id can be any value up to (2^64)-1. + * subkey_id can be any value up to (2^32) because javascript doesn't support long types. * subkey_len has to be between crypto_kdf_BYTES_MIN (inclusive) and crypto_kdf_BYTES_MAX (inclusive). * Similar to a type, the context ctx is a 8 characters string describing what the key is going to be used for. * Its purpose is to mitigate accidental bugs by separating domains. The same function used with the same key but @@ -24,7 +24,7 @@ actual object Kdf { * prevent the same keys from being mistakenly used by another application. */ actual fun deriveFromKey( - subkeyId: Int, + subkeyId: UInt, subkeyLength: Int, context: String, masterKey: UByteArray @@ -63,4 +63,4 @@ actual object Kdf { return masterKey } -} \ No newline at end of file +}