diff --git a/buildSrc/src/main/kotlin/Deps.kt b/buildSrc/src/main/kotlin/Deps.kt index 5b04e3c..170b360 100644 --- a/buildSrc/src/main/kotlin/Deps.kt +++ b/buildSrc/src/main/kotlin/Deps.kt @@ -25,6 +25,9 @@ object Versions { val kotlinBigNumVersion = "0.1.6-1.4-M2-SNAPSHOT" + val lazySodium = "4.2.6" + val jna = "5.5.0" + } @@ -76,6 +79,11 @@ object Deps { val serialization = "org.jetbrains.kotlinx:kotlinx-serialization-runtime:${Versions.kotlinSerialization}" // val coroutinesTest = "org.jetbrains.kotlinx:kotlinx-coroutines-test:${Versions.kotlinCoroutines}" val coroutinesTest = "com.ionspin.kotlin.coroutines:kotlinx-coroutines-test:${Versions.kotlinCoroutines}" + + object Delegated { + val lazysodium = "com.goterl.lazycode:lazysodium-java:${Versions.lazySodium}" + val jna = "net.java.dev.jna:jna:${Versions.jna}" + } } object iOs { diff --git a/multiplatform-crypto-delegated/build.gradle.kts b/multiplatform-crypto-delegated/build.gradle.kts index 2698e6d..e4cac24 100644 --- a/multiplatform-crypto-delegated/build.gradle.kts +++ b/multiplatform-crypto-delegated/build.gradle.kts @@ -421,6 +421,10 @@ kotlin { implementation(kotlin(Deps.Jvm.test)) implementation(kotlin(Deps.Jvm.testJUnit)) implementation(Deps.Jvm.coroutinesCore) + + //lazysodium + implementation(Deps.Jvm.Delegated.lazysodium) + implementation(Deps.Jvm.Delegated.jna) } } val jvmTest by getting { diff --git a/multiplatform-crypto-delegated/src/commonMain/kotlin/com/ionspin/kotlin/crypto/hash/sha/Sha256Delegated.kt b/multiplatform-crypto-delegated/src/commonMain/kotlin/com/ionspin/kotlin/crypto/hash/sha/Sha256Delegated.kt index 31f4716..b53b3df 100644 --- a/multiplatform-crypto-delegated/src/commonMain/kotlin/com/ionspin/kotlin/crypto/hash/sha/Sha256Delegated.kt +++ b/multiplatform-crypto-delegated/src/commonMain/kotlin/com/ionspin/kotlin/crypto/hash/sha/Sha256Delegated.kt @@ -24,6 +24,6 @@ package com.ionspin.kotlin.crypto.hash.sha */ -expect class Sha256Delegated(key: UByteArray? = null, hashLength: Int = Sha256Properties.MAX_HASH_BYTES) : Sha256 +expect class Sha256Delegated() : Sha256 expect object Sha256StatelessDelegated : StatelessSha256 \ No newline at end of file diff --git a/multiplatform-crypto-delegated/src/jsMain/kotlin/com/ionspin/kotlin/crypto/hash/sha/Sha256Delegated.kt b/multiplatform-crypto-delegated/src/jsMain/kotlin/com/ionspin/kotlin/crypto/hash/sha/Sha256Delegated.kt index 79806a7..455de8e 100644 --- a/multiplatform-crypto-delegated/src/jsMain/kotlin/com/ionspin/kotlin/crypto/hash/sha/Sha256Delegated.kt +++ b/multiplatform-crypto-delegated/src/jsMain/kotlin/com/ionspin/kotlin/crypto/hash/sha/Sha256Delegated.kt @@ -15,7 +15,7 @@ import org.khronos.webgl.get */ -actual class Sha256Delegated actual constructor(key: UByteArray?, hashLength: Int) : Sha256 { +actual class Sha256Delegated actual constructor() : Sha256 { val state : dynamic diff --git a/multiplatform-crypto-delegated/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/hash/sha/Sha256StatelessDelegated.kt b/multiplatform-crypto-delegated/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/hash/sha/Sha256StatelessDelegated.kt index caf5973..24be6d4 100644 --- a/multiplatform-crypto-delegated/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/hash/sha/Sha256StatelessDelegated.kt +++ b/multiplatform-crypto-delegated/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/hash/sha/Sha256StatelessDelegated.kt @@ -11,6 +11,7 @@ import java.security.MessageDigest actual class Sha256Delegated actual constructor(key: UByteArray?, hashLength: Int) : Sha256 { + override fun update(data: UByteArray) { TODO("not implemented yet") } 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 d6fd6ef..7b49789 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 @@ -2,6 +2,7 @@ package com.ionspin.kotlin.crypto.hash.blake2b import com.ionspin.kotlin.crypto.util.toHexString import kotlinx.cinterop.* import libsodium.* +import platform.posix.free import platform.posix.malloc /** * Created by Ugljesa Jovanovic @@ -16,16 +17,10 @@ actual class Blake2bDelegated actual constructor(key: UByteArray?, hashLength: I val requestedHashLength : Int val state : crypto_generichash_state init { - println("Initializing libsodium hash") requestedHashLength = hashLength - println("Size ${crypto_generichash_state.size}") - println("Align ${crypto_generichash_state.align}") - println("Using sodium malloc for state") - val allocated = sodium_malloc(crypto_generichash_state.size.convert())!! + val allocated = malloc(crypto_generichash_state.size.convert())!! state = allocated.reinterpret().pointed - println("allocated state") crypto_generichash_init(state.ptr, key?.run { this.toUByteArray().toCValues() }, key?.size?.convert() ?: 0UL.convert(), hashLength.convert()) - println("Initialized libsodium hash") } override fun update(data: UByteArray) { @@ -36,7 +31,7 @@ actual class Blake2bDelegated actual constructor(key: UByteArray?, hashLength: I val hashResult = UByteArray(requestedHashLength) val hashResultPinned = hashResult.pin() crypto_generichash_final(state.ptr, hashResultPinned.addressOf(0), requestedHashLength.convert()) - sodium_free(state.ptr) + free(state.ptr) return hashResult } diff --git a/multiplatform-crypto-delegated/src/nativeMain/kotlin/com/ionspin/kotlin/crypto/hash/sha/Sha256Delegated.kt b/multiplatform-crypto-delegated/src/nativeMain/kotlin/com/ionspin/kotlin/crypto/hash/sha/Sha256Delegated.kt index 0b17a98..b0e5f38 100644 --- a/multiplatform-crypto-delegated/src/nativeMain/kotlin/com/ionspin/kotlin/crypto/hash/sha/Sha256Delegated.kt +++ b/multiplatform-crypto-delegated/src/nativeMain/kotlin/com/ionspin/kotlin/crypto/hash/sha/Sha256Delegated.kt @@ -1,11 +1,10 @@ package com.ionspin.kotlin.crypto.hash.sha import com.ionspin.kotlin.crypto.hash.blake2b.Blake2bDelegatedStateless -import kotlinx.cinterop.addressOf -import kotlinx.cinterop.convert -import kotlinx.cinterop.pin -import kotlinx.cinterop.toCValues -import libsodium.crypto_hash_sha256 +import kotlinx.cinterop.* +import libsodium.* +import platform.posix.free +import platform.posix.malloc /** * Created by Ugljesa Jovanovic @@ -14,16 +13,28 @@ import libsodium.crypto_hash_sha256 */ -actual class Sha256Delegated actual constructor(key: UByteArray?, hashLength: Int) : Sha256 { +actual class Sha256Delegated actual constructor() : Sha256 { + + val state : crypto_hash_sha256_state + + init { + val allocated = malloc(crypto_hash_sha256_state.size.convert())!! + state = allocated.reinterpret().pointed + crypto_hash_sha256_init(state.ptr) + } override fun update(data: UByteArray) { - TODO("not implemented yet") + crypto_hash_sha256_update(state.ptr, data.toCValues(), data.size.convert()) } override fun digest(): UByteArray { - TODO("not implemented yet") + val hashResult = UByteArray(Sha256Properties.MAX_HASH_BYTES) + val hashResultPinned = hashResult.pin() + crypto_hash_sha256_final(state.ptr, hashResultPinned.addressOf(0)) + free(state.ptr) + return hashResult } diff --git a/multiplatform-crypto-delegated/src/nativeMain/kotlin/com/ionspin/kotlin/crypto/hash/sha/Sha512Delegated.kt b/multiplatform-crypto-delegated/src/nativeMain/kotlin/com/ionspin/kotlin/crypto/hash/sha/Sha512Delegated.kt index 00b54fa..b37c6e3 100644 --- a/multiplatform-crypto-delegated/src/nativeMain/kotlin/com/ionspin/kotlin/crypto/hash/sha/Sha512Delegated.kt +++ b/multiplatform-crypto-delegated/src/nativeMain/kotlin/com/ionspin/kotlin/crypto/hash/sha/Sha512Delegated.kt @@ -1,5 +1,10 @@ package com.ionspin.kotlin.crypto.hash.sha +import kotlinx.cinterop.* +import libsodium.* +import platform.posix.free +import platform.posix.malloc + /** * Created by Ugljesa Jovanovic * ugljesa.jovanovic@ionspin.com @@ -8,14 +13,26 @@ package com.ionspin.kotlin.crypto.hash.sha actual class Sha512Delegated actual constructor(key: UByteArray?, hashLength: Int) : Sha512 { + val state : crypto_hash_sha512_state + + init { + val allocated = malloc(crypto_hash_sha512_state.size.convert())!! + state = allocated.reinterpret().pointed + crypto_hash_sha512_init(state.ptr) + } + override fun update(data: UByteArray) { - TODO("not implemented yet") + crypto_hash_sha512_update(state.ptr, data.toCValues(), data.size.convert()) } override fun digest(): UByteArray { - TODO("not implemented yet") + val hashResult = UByteArray(Sha512Properties.MAX_HASH_BYTES) + val hashResultPinned = hashResult.pin() + crypto_hash_sha512_final(state.ptr, hashResultPinned.addressOf(0)) + free(state.ptr) + return hashResult } } @@ -23,6 +40,10 @@ actual class Sha512Delegated actual constructor(key: UByteArray?, hashLength: In actual object Sha512StatelessDelegated : StatelessSha512 { override fun digest(inputMessage: UByteArray): UByteArray { - TODO("not implemented yet") + val hashResult = UByteArray(Sha512StatelessDelegated.MAX_HASH_BYTES) + val hashResultPinned = hashResult.pin() + crypto_hash_sha512(hashResultPinned.addressOf(0), inputMessage.toCValues(), inputMessage.size.convert()) + hashResultPinned.unpin() + return hashResult } } \ No newline at end of file diff --git a/multiplatform-crypto-delegated/src/nativeTest/kotlin/com/ionspin/kotlin/crypto/hash/blake2b/Sha512DelegatedLinuxTest.kt b/multiplatform-crypto-delegated/src/nativeTest/kotlin/com/ionspin/kotlin/crypto/hash/blake2b/Sha512DelegatedLinuxTest.kt index 5dabed5..685af96 100644 --- a/multiplatform-crypto-delegated/src/nativeTest/kotlin/com/ionspin/kotlin/crypto/hash/blake2b/Sha512DelegatedLinuxTest.kt +++ b/multiplatform-crypto-delegated/src/nativeTest/kotlin/com/ionspin/kotlin/crypto/hash/blake2b/Sha512DelegatedLinuxTest.kt @@ -35,6 +35,7 @@ class Sha512DelegatedLinuxTest { @Test fun testBlake2BStateless() = testBlocking { - Blake2bDelegatedStateless.digest("test".encodeToUByteArray()) + val result = Blake2bDelegatedStateless.digest("test".encodeToUByteArray()) + println(result.toHexString()) } } \ No newline at end of file