Added sha256/512 to native, using posix malloc instead of sodium malloc

This commit is contained in:
Ugljesa Jovanovic 2020-06-09 21:05:05 +02:00 committed by Ugljesa Jovanovic
parent 197aacac33
commit 6170dc0464
No known key found for this signature in database
GPG Key ID: 178E6DFCECCB0E0F
9 changed files with 63 additions and 22 deletions

View File

@ -25,6 +25,9 @@ object Versions {
val kotlinBigNumVersion = "0.1.6-1.4-M2-SNAPSHOT" 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 serialization = "org.jetbrains.kotlinx:kotlinx-serialization-runtime:${Versions.kotlinSerialization}"
// val coroutinesTest = "org.jetbrains.kotlinx:kotlinx-coroutines-test:${Versions.kotlinCoroutines}" // val coroutinesTest = "org.jetbrains.kotlinx:kotlinx-coroutines-test:${Versions.kotlinCoroutines}"
val coroutinesTest = "com.ionspin.kotlin.coroutines: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 { object iOs {

View File

@ -421,6 +421,10 @@ kotlin {
implementation(kotlin(Deps.Jvm.test)) implementation(kotlin(Deps.Jvm.test))
implementation(kotlin(Deps.Jvm.testJUnit)) implementation(kotlin(Deps.Jvm.testJUnit))
implementation(Deps.Jvm.coroutinesCore) implementation(Deps.Jvm.coroutinesCore)
//lazysodium
implementation(Deps.Jvm.Delegated.lazysodium)
implementation(Deps.Jvm.Delegated.jna)
} }
} }
val jvmTest by getting { val jvmTest by getting {

View File

@ -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 expect object Sha256StatelessDelegated : StatelessSha256

View File

@ -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 val state : dynamic

View File

@ -11,6 +11,7 @@ import java.security.MessageDigest
actual class Sha256Delegated actual constructor(key: UByteArray?, hashLength: Int) : Sha256 { actual class Sha256Delegated actual constructor(key: UByteArray?, hashLength: Int) : Sha256 {
override fun update(data: UByteArray) { override fun update(data: UByteArray) {
TODO("not implemented yet") TODO("not implemented yet")
} }

View File

@ -2,6 +2,7 @@ package com.ionspin.kotlin.crypto.hash.blake2b
import com.ionspin.kotlin.crypto.util.toHexString import com.ionspin.kotlin.crypto.util.toHexString
import kotlinx.cinterop.* import kotlinx.cinterop.*
import libsodium.* import libsodium.*
import platform.posix.free
import platform.posix.malloc import platform.posix.malloc
/** /**
* Created by Ugljesa Jovanovic * Created by Ugljesa Jovanovic
@ -16,16 +17,10 @@ actual class Blake2bDelegated actual constructor(key: UByteArray?, hashLength: I
val requestedHashLength : Int val requestedHashLength : Int
val state : crypto_generichash_state val state : crypto_generichash_state
init { init {
println("Initializing libsodium hash")
requestedHashLength = hashLength requestedHashLength = hashLength
println("Size ${crypto_generichash_state.size}") val allocated = malloc(crypto_generichash_state.size.convert())!!
println("Align ${crypto_generichash_state.align}")
println("Using sodium malloc for state")
val allocated = sodium_malloc(crypto_generichash_state.size.convert())!!
state = allocated.reinterpret<crypto_generichash_state>().pointed state = allocated.reinterpret<crypto_generichash_state>().pointed
println("allocated state")
crypto_generichash_init(state.ptr, key?.run { this.toUByteArray().toCValues() }, key?.size?.convert() ?: 0UL.convert(), hashLength.convert()) 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) { override fun update(data: UByteArray) {
@ -36,7 +31,7 @@ actual class Blake2bDelegated actual constructor(key: UByteArray?, hashLength: I
val hashResult = UByteArray(requestedHashLength) val hashResult = UByteArray(requestedHashLength)
val hashResultPinned = hashResult.pin() val hashResultPinned = hashResult.pin()
crypto_generichash_final(state.ptr, hashResultPinned.addressOf(0), requestedHashLength.convert()) crypto_generichash_final(state.ptr, hashResultPinned.addressOf(0), requestedHashLength.convert())
sodium_free(state.ptr) free(state.ptr)
return hashResult return hashResult
} }

View File

@ -1,11 +1,10 @@
package com.ionspin.kotlin.crypto.hash.sha package com.ionspin.kotlin.crypto.hash.sha
import com.ionspin.kotlin.crypto.hash.blake2b.Blake2bDelegatedStateless import com.ionspin.kotlin.crypto.hash.blake2b.Blake2bDelegatedStateless
import kotlinx.cinterop.addressOf import kotlinx.cinterop.*
import kotlinx.cinterop.convert import libsodium.*
import kotlinx.cinterop.pin import platform.posix.free
import kotlinx.cinterop.toCValues import platform.posix.malloc
import libsodium.crypto_hash_sha256
/** /**
* Created by Ugljesa Jovanovic * 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<crypto_hash_sha256_state>().pointed
crypto_hash_sha256_init(state.ptr)
}
override fun update(data: UByteArray) { override fun update(data: UByteArray) {
TODO("not implemented yet") crypto_hash_sha256_update(state.ptr, data.toCValues(), data.size.convert())
} }
override fun digest(): UByteArray { 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
} }

View File

@ -1,5 +1,10 @@
package com.ionspin.kotlin.crypto.hash.sha package com.ionspin.kotlin.crypto.hash.sha
import kotlinx.cinterop.*
import libsodium.*
import platform.posix.free
import platform.posix.malloc
/** /**
* Created by Ugljesa Jovanovic * Created by Ugljesa Jovanovic
* ugljesa.jovanovic@ionspin.com * 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 { 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<crypto_hash_sha512_state>().pointed
crypto_hash_sha512_init(state.ptr)
}
override fun update(data: UByteArray) { override fun update(data: UByteArray) {
TODO("not implemented yet") crypto_hash_sha512_update(state.ptr, data.toCValues(), data.size.convert())
} }
override fun digest(): UByteArray { 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 { actual object Sha512StatelessDelegated : StatelessSha512 {
override fun digest(inputMessage: UByteArray): UByteArray { 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
} }
} }

View File

@ -35,6 +35,7 @@ class Sha512DelegatedLinuxTest {
@Test @Test
fun testBlake2BStateless() = testBlocking { fun testBlake2BStateless() = testBlocking {
Blake2bDelegatedStateless.digest("test".encodeToUByteArray()) val result = Blake2bDelegatedStateless.digest("test".encodeToUByteArray())
println(result.toHexString())
} }
} }