diff --git a/multiplatform-crypto-libsodium-bindings/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/JnaLibsodiumInterface.kt b/multiplatform-crypto-libsodium-bindings/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/JnaLibsodiumInterface.kt index ee9288f..aa2bd5f 100644 --- a/multiplatform-crypto-libsodium-bindings/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/JnaLibsodiumInterface.kt +++ b/multiplatform-crypto-libsodium-bindings/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/JnaLibsodiumInterface.kt @@ -1,66 +1,165 @@ package com.ionspin.kotlin.crypto import com.sun.jna.Library +import com.sun.jna.NativeLong +import com.sun.jna.Structure /** * Created by Ugljesa Jovanovic * ugljesa.jovanovic@ionspin.com * on 21-Feb-2021 */ +class Hash256State : Structure() { + override fun getFieldOrder(): List = listOf("state", "count", "buf") + @JvmField + val state = IntArray(8) + @JvmField + var count: Long = 0 + @JvmField + val buf = ByteArray(64) +} + +class Hash512State : Structure() { + override fun getFieldOrder(): List = listOf("state", "count", "buf") + @JvmField + val state = IntArray(8) + @JvmField + var count: LongArray = LongArray(2) + @JvmField + val buf = ByteArray(128) +} + +class Blake2bState: Structure() { + override fun getFieldOrder(): List = listOf("opaque") + @JvmField + val opaque = ByteArray(384) +} + interface JnaLibsodiumInterface : Library { - fun sodium_version_string() : String + fun sodium_version_string(): String - fun randombytes_buf(buffer: ByteArray, bufferSize : Int) + fun randombytes_buf(buffer: ByteArray, bufferSize: Int) -// fun crypto_generichash(hashLength: Int, inputMessage: Uint8Array, key: Uint8Array): Uint8Array -// -// fun crypto_hash_sha256(message: Uint8Array): Uint8Array -// -// fun crypto_hash_sha512(message: Uint8Array): Uint8Array + // int crypto_generichash(unsigned char *out, size_t outlen, + // const unsigned char *in, unsigned long long inlen, + // const unsigned char *key, size_t keylen) + fun crypto_generichash( + out: ByteArray, + outlen: Int, + input: ByteArray, + inputLength: Long, + key: ByteArray, + keylen: Int + ) + + // int crypto_hash_sha256(unsigned char *out, const unsigned char *in, + // unsigned long long inlen) + fun crypto_hash_sha256(out: ByteArray, input: ByteArray, inputLength: Long) + + // int crypto_hash_sha512(unsigned char *out, const unsigned char *in, + // unsigned long long inlen) + fun crypto_hash_sha512(out: ByteArray, input: ByteArray, inputLength: Long) // // // ---- Generic hash ---- // Updateable // -// fun crypto_generichash_init(key : Uint8Array, hashLength: Int) : dynamic -// -// fun crypto_generichash_update(state: dynamic, inputMessage: Uint8Array) -// -// fun crypto_generichash_final(state: dynamic, hashLength: Int) : Uint8Array -// -// fun crypto_generichash_keygen() : Uint8Array -// + + // int crypto_generichash_init(crypto_generichash_state *state, + // const unsigned char *key, + // const size_t keylen, const size_t outlen) + // Output cant be larger than 64 so no need to use Long to represent size_t here + fun crypto_generichash_init(state: Blake2bState, key: ByteArray, keylen: Int, outlen: Int) + + // int crypto_generichash_update(crypto_generichash_state *state, + // const unsigned char *in, + // unsigned long long inlen) + fun crypto_generichash_update(state: Blake2bState, inputMessage: ByteArray, inputLength: Long) + + // int crypto_generichash_final(crypto_generichash_state *state, + // unsigned char *out, const size_t outlen) + // Output cant be larger than 64 so no need to use Long to represent size_t here + fun crypto_generichash_final(state: Blake2bState, out: ByteArray, hashLength: Int) + + // void crypto_generichash_keygen(unsigned char k[crypto_generichash_KEYBYTES]) + fun crypto_generichash_keygen(key: ByteArray) + + fun crypto_generichash_statebytes(): Int + + // // // ---- Generic hash end ---- // Updateable // // // ---- Blake2b ---- -// -// fun crypto_generichash_blake2b(hashLength: Int, inputMessage: Uint8Array, key: Uint8Array): Uint8Array -// -// fun crypto_generichash_blake2b_init(key : Uint8Array, hashLength: Int) : dynamic -// -// fun crypto_generichash_blake2b_update(state: dynamic, inputMessage: Uint8Array) -// -// fun crypto_generichash_blake2b_final(state: dynamic, hashLength: Int) : Uint8Array -// -// fun crypto_generichash_blake2b_keygen() : Uint8Array + // int crypto_generichash_blake2b(unsigned char *out, size_t outlen, + // const unsigned char *in, + // unsigned long long inlen, + // const unsigned char *key, size_t keylen) + // Output cant be larger than 64 so no need to use Long to represent size_t here + fun crypto_generichash_blake2b( + out: ByteArray, + outlen: Int, + input: ByteArray, + inputLength: Long, + key: ByteArray, + keylen: Int + ) + + // int crypto_generichash_blake2b_init(crypto_generichash_blake2b_state *state, + // const unsigned char *key, + // const size_t keylen, const size_t outlen) + fun crypto_generichash_blake2b_init(kstate: ByteArray, key: ByteArray, keylen: Int, outlen: Int) + + // int crypto_generichash_blake2b_update(crypto_generichash_blake2b_state *state, + // const unsigned char *in, + // unsigned long long inlen) + fun crypto_generichash_blake2b_update(state: ByteArray, inputMessage: ByteArray, inputLength: Long) + + // int crypto_generichash_blake2b_final(crypto_generichash_blake2b_state *state, + // unsigned char *out, + // const size_t outlen) + fun crypto_generichash_blake2b_final(state: ByteArray, out: ByteArray, hashLength: Int) + + // void crypto_generichash_blake2b_keygen(unsigned char k[crypto_generichash_blake2b_KEYBYTES]) + fun crypto_generichash_blake2b_keygen(key: ByteArray) // // // ---- Blake2b end ---- // // // ---- Short hash ---- -// fun crypto_shorthash(data : Uint8Array, key: Uint8Array) : Uint8Array // -// fun crypto_shorthash_keygen() : Uint8Array -// // ---- Short hash end ---- + + // int crypto_shorthash(unsigned char *out, const unsigned char *in, + // unsigned long long inlen, const unsigned char *k) + fun crypto_shorthash(out: ByteArray, input: ByteArray, inlen: Long, key: ByteArray) + + // void crypto_shorthash_keygen(unsigned char k[crypto_shorthash_KEYBYTES]) + fun crypto_shorthash_keygen(key: ByteArray) + // -// fun crypto_hash_sha256_init() : dynamic +// ---- Short hash end ---- // -// fun crypto_hash_sha256_update(state: dynamic, message: Uint8Array) -// -// fun crypto_hash_sha256_final(state: dynamic): Uint8Array -// -// fun crypto_hash_sha512_init() : dynamic -// -// fun crypto_hash_sha512_update(state: dynamic, message: Uint8Array) -// -// fun crypto_hash_sha512_final(state: dynamic): Uint8Array + + // int crypto_hash_sha256_init(crypto_hash_sha256_state *state) + fun crypto_hash_sha256_init(state: Hash256State) + + // int crypto_hash_sha256_update(crypto_hash_sha256_state *state, + // const unsigned char *in, + // unsigned long long inlen) + fun crypto_hash_sha256_update(state: Hash256State, input: ByteArray, inlen: Long) + + // int crypto_hash_sha256_final(crypto_hash_sha256_state *state, + // unsigned char *out) + fun crypto_hash_sha256_final(state: Hash256State, out: ByteArray) + + + // int crypto_hash_sha512_init(crypto_hash_sha512_state *state) + fun crypto_hash_sha512_init(state: Hash512State) + + // int crypto_hash_sha512_update(crypto_hash_sha512_state *state, + // const unsigned char *in, + // unsigned long long inlen) + fun crypto_hash_sha512_update(state: Hash512State, input: ByteArray, inlen: Long) + + // int crypto_hash_sha512_final(crypto_hash_sha512_state *state, + // unsigned char *out) + fun crypto_hash_sha512_final(state: Hash512State, out: ByteArray) // // //XChaCha20Poly1305 - also in bindings // //fun crypto_aead_xchacha20poly1305_ietf_encrypt(message: Uint8Array, associatedData: Uint8Array, secretNonce: Uint8Array, nonce: Uint8Array, key: Uint8Array) : Uint8Array diff --git a/multiplatform-crypto-libsodium-bindings/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/generichash/GenericHash.kt b/multiplatform-crypto-libsodium-bindings/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/generichash/GenericHashJvm.kt similarity index 62% rename from multiplatform-crypto-libsodium-bindings/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/generichash/GenericHash.kt rename to multiplatform-crypto-libsodium-bindings/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/generichash/GenericHashJvm.kt index f674d82..d540452 100644 --- a/multiplatform-crypto-libsodium-bindings/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/generichash/GenericHash.kt +++ b/multiplatform-crypto-libsodium-bindings/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/generichash/GenericHashJvm.kt @@ -1,13 +1,14 @@ package com.ionspin.kotlin.crypto.generichash -import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodium +import com.ionspin.kotlin.crypto.Blake2bState +import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodiumJna /** * Created by Ugljesa Jovanovic * ugljesa.jovanovic@ionspin.com * on 21-Aug-2020 */ -actual class GenericHashStateInternal(internal val data: ByteArray) +actual typealias GenericHashStateInternal = Blake2bState actual object GenericHash { actual fun genericHash( @@ -16,12 +17,12 @@ actual object GenericHash { key: UByteArray? ): UByteArray { val hash = UByteArray(requestedHashLength) - sodium.crypto_generichash( + sodiumJna.crypto_generichash( hash.asByteArray(), requestedHashLength, message.asByteArray(), message.size.toLong(), - key?.asByteArray(), + key?.asByteArray() ?: ByteArray(0), (key?.size ?: 0) ) return hash @@ -31,8 +32,8 @@ actual object GenericHash { requestedHashLength: Int, key: UByteArray? ): GenericHashState { - val state = GenericHashStateInternal(ByteArray(sodium.crypto_generichash_statebytes())) - sodium.crypto_generichash_init(state.data, key?.asByteArray(), key?.size ?: 0, requestedHashLength) + val state = GenericHashStateInternal() + sodiumJna.crypto_generichash_init(state, key?.asByteArray() ?: ByteArray(0), key?.size ?: 0, requestedHashLength) return GenericHashState(requestedHashLength, state) } @@ -40,18 +41,18 @@ actual object GenericHash { state: GenericHashState, messagePart: UByteArray ) { - sodium.crypto_generichash_update(state.internalState.data, messagePart.asByteArray(), messagePart.size.toLong()) + sodiumJna.crypto_generichash_update(state.internalState, messagePart.asByteArray(), messagePart.size.toLong()) } actual fun genericHashFinal(state: GenericHashState): UByteArray { val hashResult = ByteArray(state.hashLength) - sodium.crypto_generichash_final(state.internalState.data, hashResult, state.hashLength) + sodiumJna.crypto_generichash_final(state.internalState, hashResult, state.hashLength) return hashResult.asUByteArray() } actual fun genericHashKeygen(): UByteArray { val generatedKey = UByteArray(crypto_generichash_BYTES) - sodium.crypto_generichash_keygen(generatedKey.asByteArray()) + sodiumJna.crypto_generichash_keygen(generatedKey.asByteArray()) return generatedKey } diff --git a/multiplatform-crypto-libsodium-bindings/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/hash/HashJvm.kt b/multiplatform-crypto-libsodium-bindings/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/hash/HashJvm.kt index cdaf573..283cdc8 100644 --- a/multiplatform-crypto-libsodium-bindings/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/hash/HashJvm.kt +++ b/multiplatform-crypto-libsodium-bindings/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/hash/HashJvm.kt @@ -1,54 +1,55 @@ package com.ionspin.kotlin.crypto.hash -import com.goterl.lazycode.lazysodium.interfaces.Hash -import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodium +import com.ionspin.kotlin.crypto.Hash256State +import com.ionspin.kotlin.crypto.Hash512State +import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodiumJna -actual typealias Sha256State = Hash.State256 -actual typealias Sha512State = Hash.State512 +actual typealias Sha256State = Hash256State +actual typealias Sha512State = Hash512State actual object Hash { actual fun sha256(data: UByteArray): UByteArray { val resultHash = UByteArray(crypto_hash_sha256_BYTES) - sodium.crypto_hash_sha256(resultHash.asByteArray(), data.asByteArray(), data.size.toLong()) + sodiumJna.crypto_hash_sha256(resultHash.asByteArray(), data.asByteArray(), data.size.toLong()) return resultHash } actual fun sha256Init(): Sha256State { - val state = Hash.State256() - sodium.crypto_hash_sha256_init(state) + val state = Hash256State() + sodiumJna.crypto_hash_sha256_init(state) return state } actual fun sha256Update(state: Sha256State, data: UByteArray) { - sodium.crypto_hash_sha256_update(state, data.asByteArray(), data.size.toLong()) + sodiumJna.crypto_hash_sha256_update(state, data.asByteArray(), data.size.toLong()) } actual fun sha256Final(state: Sha256State): UByteArray { val resultHash = UByteArray(crypto_hash_sha256_BYTES) - sodium.crypto_hash_sha256_final(state, resultHash.asByteArray()) + sodiumJna.crypto_hash_sha256_final(state, resultHash.asByteArray()) return resultHash } actual fun sha512(data: UByteArray): UByteArray { val resultHash = UByteArray(crypto_hash_sha512_BYTES) - sodium.crypto_hash_sha512(resultHash.asByteArray(), data.asByteArray(), data.size.toLong()) + sodiumJna.crypto_hash_sha512(resultHash.asByteArray(), data.asByteArray(), data.size.toLong()) return resultHash } actual fun sha512Init(): Sha512State { - val state = Hash.State512() - sodium.crypto_hash_sha512_init(state) + val state = Hash512State() + sodiumJna.crypto_hash_sha512_init(state) return state } actual fun sha512Update(state: Sha512State, data: UByteArray) { - sodium.crypto_hash_sha512_update(state, data.asByteArray(), data.size.toLong()) + sodiumJna.crypto_hash_sha512_update(state, data.asByteArray(), data.size.toLong()) } actual fun sha512Final(state: Sha512State): UByteArray { val resultHash = UByteArray(crypto_hash_sha512_BYTES) - sodium.crypto_hash_sha512_final(state, resultHash.asByteArray()) + sodiumJna.crypto_hash_sha512_final(state, resultHash.asByteArray()) return resultHash } diff --git a/multiplatform-crypto-libsodium-bindings/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/shortinputhash/ShortHash.kt b/multiplatform-crypto-libsodium-bindings/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/shortinputhash/ShortHash.kt index f915ef7..4c072ec 100644 --- a/multiplatform-crypto-libsodium-bindings/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/shortinputhash/ShortHash.kt +++ b/multiplatform-crypto-libsodium-bindings/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/shortinputhash/ShortHash.kt @@ -1,6 +1,7 @@ package com.ionspin.kotlin.crypto.shortinputhash -import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodium +import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodiumJna +import com.sun.jna.NativeLong /** * Created by Ugljesa Jovanovic @@ -12,13 +13,13 @@ import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodium actual object ShortHash { actual fun shortHash(data: UByteArray, key: UByteArray): UByteArray { val hashResult = UByteArray(crypto_shorthash_BYTES) - sodium.crypto_shorthash(hashResult.asByteArray(), data.asByteArray(), data.size.toLong(), key.asByteArray()) + sodiumJna.crypto_shorthash(hashResult.asByteArray(), data.asByteArray(), data.size.toLong(), key.asByteArray()) return hashResult } actual fun shortHashKeygen(): UByteArray { val key = UByteArray(crypto_shorthash_KEYBYTES) - sodium.crypto_shorthash_keygen(key.asByteArray()) + sodiumJna.crypto_shorthash_keygen(key.asByteArray()) return key } diff --git a/multiplatform-crypto-libsodium-bindings/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/util/LibsodiumRandom.kt b/multiplatform-crypto-libsodium-bindings/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/util/LibsodiumRandom.kt index fbbcae3..627b0a9 100644 --- a/multiplatform-crypto-libsodium-bindings/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/util/LibsodiumRandom.kt +++ b/multiplatform-crypto-libsodium-bindings/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/util/LibsodiumRandom.kt @@ -1,6 +1,7 @@ package com.ionspin.kotlin.crypto.util import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodium +import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodiumJna /** * Created by Ugljesa Jovanovic @@ -13,7 +14,7 @@ actual object LibsodiumRandom { */ actual fun buf(size: Int): UByteArray { val result = ByteArray(size) - sodium.randombytes_buf(result, size) + sodiumJna.randombytes_buf(result, size) return result.asUByteArray() }