Fix for #42, add check for values returned by libsodium that signal failure, reintroduce xChaCha20 stream API since iosArm32 is not supported any more, use forked resource loader that deletes tmp folders properly

This commit is contained in:
Ugljesa Jovanovic 2024-04-06 11:46:28 +02:00
parent bd988cc493
commit 59c5f958ca
No known key found for this signature in database
GPG Key ID: 5884AC34A0EC67DB
43 changed files with 388 additions and 339 deletions

View File

@ -27,7 +27,9 @@ object Versions {
val ktor = "1.3.2" val ktor = "1.3.2"
val timber = "4.7.1" val timber = "4.7.1"
val kodeinVersion = "7.1.0" val kodeinVersion = "7.1.0"
val resourceLoader = "2.0.2" // Fork that deletes temporary folders resource loader creates
// https://github.com/ionspin/resource-loader/tree/delete-temp-folder
val resourceLoader = "2.0.3-SNAPSHOT"

View File

@ -45,6 +45,7 @@ val sonatypeUsernameEnv: String? = System.getenv()["SONATYPE_USERNAME"]
repositories { repositories {
mavenCentral() mavenCentral()
mavenLocal()
maven { maven {
url = uri("https://oss.sonatype.org/content/repositories/snapshots") url = uri("https://oss.sonatype.org/content/repositories/snapshots")
} }

View File

@ -0,0 +1,11 @@
package com.ionspin.kotlin.crypto
class GeneralLibsodiumException : RuntimeException("Libsodium reported error! Returned value was -1") {
companion object {
fun Int.ensureLibsodiumSuccess() {
if (this == -1) {
throw GeneralLibsodiumException()
}
}
}
}

View File

@ -15,9 +15,6 @@ expect class Sha512State
expect object Hash { expect object Hash {
//Not present in LazySodium
//fun hash(data: UByteArray) : UByteArray
fun sha256(data: UByteArray) : UByteArray fun sha256(data: UByteArray) : UByteArray
fun sha256Init() : Sha256State fun sha256Init() : Sha256State
fun sha256Update(state: Sha256State, data : UByteArray) fun sha256Update(state: Sha256State, data : UByteArray)

View File

@ -23,8 +23,8 @@ expect object Stream {
fun chacha20IetfXor(message : UByteArray, nonce: UByteArray, key: UByteArray) : UByteArray fun chacha20IetfXor(message : UByteArray, nonce: UByteArray, key: UByteArray) : UByteArray
fun chacha20IetfXorIc(message: UByteArray, nonce: UByteArray, initialCounter: UInt, key: UByteArray) : UByteArray fun chacha20IetfXorIc(message: UByteArray, nonce: UByteArray, initialCounter: UInt, key: UByteArray) : UByteArray
// fun xChacha20Keygen() : UByteArray fun xChacha20Keygen() : UByteArray
//
// fun xChacha20Xor(message : UByteArray, nonce: UByteArray, key: UByteArray) : UByteArray fun xChacha20Xor(message : UByteArray, nonce: UByteArray, key: UByteArray) : UByteArray
// fun xChacha20XorIc(message : UByteArray, nonce: UByteArray, initialCounter: ULong, key: UByteArray) : UByteArray fun xChacha20XorIc(message : UByteArray, nonce: UByteArray, initialCounter: ULong, key: UByteArray) : UByteArray
} }

View File

@ -67,25 +67,25 @@ class StreamTest {
} }
} }
// @Test @Test
// fun testXChaCha20IetfStream() = runTest { fun testXChaCha20IetfStream() = runTest {
// LibsodiumInitializer.initializeWithCallback { LibsodiumInitializer.initializeWithCallback {
// val message = "This is a cha cha message".encodeToUByteArray() val message = "This is a cha cha message".encodeToUByteArray()
// val nonce = LibsodiumRandom.bufDeterministic(crypto_stream_xchacha20_NONCEBYTES, seed) val nonce = LibsodiumRandom.bufDeterministic(crypto_stream_xchacha20_NONCEBYTES, seed)
// val key = Stream.xChacha20Keygen() val key = Stream.xChacha20Keygen()
// val encryptedUsingLibsodium = Stream.xChacha20Xor(message, nonce, key) val encryptedUsingLibsodium = Stream.xChacha20Xor(message, nonce, key)
// val encryptedUsingLibsodiumWithInitialCounter = Stream.xChacha20XorIc(message, nonce, 0U, key) val encryptedUsingLibsodiumWithInitialCounter = Stream.xChacha20XorIc(message, nonce, 0U, key)
// println(encryptedUsingLibsodium.toHexString()) println(encryptedUsingLibsodium.toHexString())
// println(encryptedUsingLibsodiumWithInitialCounter.toHexString()) println(encryptedUsingLibsodiumWithInitialCounter.toHexString())
// assertTrue { assertTrue {
// encryptedUsingLibsodium.contentEquals(encryptedUsingLibsodiumWithInitialCounter) encryptedUsingLibsodium.contentEquals(encryptedUsingLibsodiumWithInitialCounter)
// } }
// val decryptedUsingLibsodium = Stream.xChacha20Xor(encryptedUsingLibsodium, nonce, key) val decryptedUsingLibsodium = Stream.xChacha20Xor(encryptedUsingLibsodium, nonce, key)
// println(message.toHexString()) println(message.toHexString())
// println(decryptedUsingLibsodium.toHexString()) println(decryptedUsingLibsodium.toHexString())
// assertTrue { assertTrue {
// decryptedUsingLibsodium.contentEquals(message) decryptedUsingLibsodium.contentEquals(message)
// } }
// } }
// } }
} }

View File

@ -1,7 +1,13 @@
package ext.libsodium.com.ionspin.kotlin.crypto package ext.libsodium.com.ionspin.kotlin.crypto
import com.ionspin.kotlin.crypto.* import com.ionspin.kotlin.crypto.getSodiumLoaded
import ext.libsodium.* import com.ionspin.kotlin.crypto.sodiumLoaded
import ext.libsodium._libsodiumPromise
import ext.libsodium.crypto_generichash
import ext.libsodium.crypto_hash_sha256
import ext.libsodium.crypto_hash_sha256_init
import ext.libsodium.crypto_hash_sha512
import ext.libsodium.sodium_init
import kotlin.coroutines.suspendCoroutine import kotlin.coroutines.suspendCoroutine
/** /**

View File

@ -9,8 +9,6 @@ actual typealias Sha512State = Any
actual object Hash { actual object Hash {
//Not present in LazySodium
//fun hash(data: UByteArray) : UByteArray
actual fun sha256(data: UByteArray): UByteArray { actual fun sha256(data: UByteArray): UByteArray {
return getSodium().crypto_hash_sha256(data.toUInt8Array()).toUByteArray() return getSodium().crypto_hash_sha256(data.toUInt8Array()).toUByteArray()
} }

View File

@ -82,41 +82,41 @@ actual object Stream {
return result.toUByteArray() return result.toUByteArray()
} }
// actual fun xChacha20Keygen(): UByteArray { actual fun xChacha20Keygen(): UByteArray {
// val result = getSodium().crypto_stream_xchacha20_keygen() val result = getSodium().crypto_stream_xchacha20_keygen()
//
// return result.toUByteArray() return result.toUByteArray()
// } }
//
// actual fun xChacha20Xor( actual fun xChacha20Xor(
// message: UByteArray, message: UByteArray,
// nonce: UByteArray, nonce: UByteArray,
// key: UByteArray key: UByteArray
// ): UByteArray { ): UByteArray {
// val result = getSodium().crypto_stream_xchacha20_xor( val result = getSodium().crypto_stream_xchacha20_xor(
// message.toUInt8Array(), message.toUInt8Array(),
// nonce.toUInt8Array(), nonce.toUInt8Array(),
// key.toUInt8Array() key.toUInt8Array()
// ) )
//
// return result.toUByteArray() return result.toUByteArray()
// } }
//
// actual fun xChacha20XorIc( actual fun xChacha20XorIc(
// message: UByteArray, message: UByteArray,
// nonce: UByteArray, nonce: UByteArray,
// initialCounter: ULong, initialCounter: ULong,
// key: UByteArray key: UByteArray
// ): UByteArray { ): UByteArray {
// val result = getSodium().crypto_stream_xchacha20_xor_ic( val result = getSodium().crypto_stream_xchacha20_xor_ic(
// message.toUInt8Array(), message.toUInt8Array(),
// nonce.toUInt8Array(), nonce.toUInt8Array(),
// initialCounter.toUInt(), initialCounter.toUInt(),
// key.toUInt8Array() key.toUInt8Array()
// ) )
//
// return result.toUByteArray() return result.toUByteArray()
// } }
} }

View File

@ -82,6 +82,8 @@ interface JnaLibsodiumInterface : Library {
// ---- Utils ---- // ---- Utils ----
fun sodium_version_string(): String fun sodium_version_string(): String
// void
// randombytes_buf(void * const buf, const size_t size)
fun randombytes_buf(buffer: ByteArray, bufferSize: Int) fun randombytes_buf(buffer: ByteArray, bufferSize: Int)
// void randombytes_buf_deterministic(void * const buf, const size_t size, // void randombytes_buf_deterministic(void * const buf, const size_t size,
@ -147,7 +149,7 @@ interface JnaLibsodiumInterface : Library {
buffer: ByteArray, buffer: ByteArray,
paddedBufferLength: Int, paddedBufferLength: Int,
blockSize: Int blockSize: Int
) ) : Int
// char *sodium_bin2base64(char * const b64, const size_t b64_maxlen, // char *sodium_bin2base64(char * const b64, const size_t b64_maxlen,
@ -431,7 +433,7 @@ interface JnaLibsodiumInterface : Library {
nsec: ByteArray?, nsec: ByteArray?,
npub: ByteArray, npub: ByteArray,
key: ByteArray key: ByteArray
) ) : Int
// int crypto_aead_chacha20poly1305_ietf_decrypt( // int crypto_aead_chacha20poly1305_ietf_decrypt(
// unsigned char *m, // unsigned char *m,
@ -809,7 +811,7 @@ interface JnaLibsodiumInterface : Library {
// // ---- Box ---- // // ---- Box ----
// //
// int crypto_box_keypair(unsigned char *pk, unsigned char *sk) // int crypto_box_keypair(unsigned char *pk, unsigned char *sk)
fun crypto_box_keypair(publicKey: ByteArray, secretKey: ByteArray) fun crypto_box_keypair(publicKey: ByteArray, secretKey: ByteArray): Int
// int crypto_box_seed_keypair(unsigned char *pk, unsigned char *sk, // int crypto_box_seed_keypair(unsigned char *pk, unsigned char *sk,
// const unsigned char *seed) // const unsigned char *seed)
@ -1037,7 +1039,7 @@ interface JnaLibsodiumInterface : Library {
// int crypto_sign_keypair(unsigned char *pk, unsigned char *sk) // int crypto_sign_keypair(unsigned char *pk, unsigned char *sk)
fun crypto_sign_keypair( fun crypto_sign_keypair(
publicKey: ByteArray, secretKey: ByteArray publicKey: ByteArray, secretKey: ByteArray
) ): Int
// int crypto_sign_seed_keypair(unsigned char *pk, unsigned char *sk, // int crypto_sign_seed_keypair(unsigned char *pk, unsigned char *sk,
// const unsigned char *seed) // const unsigned char *seed)
@ -1066,7 +1068,7 @@ interface JnaLibsodiumInterface : Library {
subkeyId: Long, subkeyId: Long,
context: ByteArray, context: ByteArray,
key: ByteArray key: ByteArray
) ): Int
// void crypto_kdf_keygen(unsigned char k[crypto_kdf_KEYBYTES]) // void crypto_kdf_keygen(unsigned char k[crypto_kdf_KEYBYTES])
fun crypto_kdf_keygen( fun crypto_kdf_keygen(
@ -1135,7 +1137,7 @@ interface JnaLibsodiumInterface : Library {
fun crypto_kx_keypair( fun crypto_kx_keypair(
publicKey: ByteArray, publicKey: ByteArray,
secretKey: ByteArray secretKey: ByteArray
) ): Int
// int crypto_kx_seed_keypair(unsigned char pk[crypto_kx_PUBLICKEYBYTES], // int crypto_kx_seed_keypair(unsigned char pk[crypto_kx_PUBLICKEYBYTES],
@ -1145,7 +1147,7 @@ interface JnaLibsodiumInterface : Library {
publicKey: ByteArray, publicKey: ByteArray,
secretKey: ByteArray, secretKey: ByteArray,
seed: ByteArray seed: ByteArray
) ): Int
// int crypto_kx_client_session_keys(unsigned char rx[crypto_kx_SESSIONKEYBYTES], // int crypto_kx_client_session_keys(unsigned char rx[crypto_kx_SESSIONKEYBYTES],
// unsigned char tx[crypto_kx_SESSIONKEYBYTES], // unsigned char tx[crypto_kx_SESSIONKEYBYTES],
// const unsigned char client_pk[crypto_kx_PUBLICKEYBYTES], // const unsigned char client_pk[crypto_kx_PUBLICKEYBYTES],
@ -1157,7 +1159,7 @@ interface JnaLibsodiumInterface : Library {
clientPublicKey: ByteArray, clientPublicKey: ByteArray,
clientSecretKey: ByteArray, clientSecretKey: ByteArray,
serverPublicKey: ByteArray serverPublicKey: ByteArray
) ): Int
// int crypto_kx_server_session_keys(unsigned char rx[crypto_kx_SESSIONKEYBYTES], // int crypto_kx_server_session_keys(unsigned char rx[crypto_kx_SESSIONKEYBYTES],
// unsigned char tx[crypto_kx_SESSIONKEYBYTES], // unsigned char tx[crypto_kx_SESSIONKEYBYTES],
// const unsigned char server_pk[crypto_kx_PUBLICKEYBYTES], // const unsigned char server_pk[crypto_kx_PUBLICKEYBYTES],
@ -1169,7 +1171,7 @@ interface JnaLibsodiumInterface : Library {
serverPublicKey: ByteArray, serverPublicKey: ByteArray,
serverSecretKey: ByteArray, serverSecretKey: ByteArray,
clientPublicKey: ByteArray clientPublicKey: ByteArray
) ): Int
// //
// // ---- Key exchange end ---- // // ---- Key exchange end ----
@ -1286,9 +1288,9 @@ interface JnaLibsodiumInterface : Library {
// //
// int crypto_scalarmult(unsigned char *q, const unsigned char *n, // int crypto_scalarmult(unsigned char *q, const unsigned char *n,
// const unsigned char *p) // const unsigned char *p)
fun crypto_scalarmult(q: ByteArray, n: ByteArray, p: ByteArray) fun crypto_scalarmult(q: ByteArray, n: ByteArray, p: ByteArray): Int
// int crypto_scalarmult_base(unsigned char *q, const unsigned char *n) // int crypto_scalarmult_base(unsigned char *q, const unsigned char *n)
fun crypto_scalarmult_base(q: ByteArray, b: ByteArray) fun crypto_scalarmult_base(q: ByteArray, b: ByteArray): Int
// //
// // ---- Scalar multiplication end ---- // // ---- Scalar multiplication end ----
} }

View File

@ -1,6 +1,7 @@
package com.ionspin.kotlin.crypto package com.ionspin.kotlin.crypto
import com.goterl.resourceloader.SharedLibraryLoader import com.goterl.resourceloader.SharedLibraryLoader
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
import com.sun.jna.Native import com.sun.jna.Native
import com.sun.jna.Platform import com.sun.jna.Platform
import java.io.File import java.io.File
@ -49,13 +50,13 @@ actual object LibsodiumInitializer {
lateinit var sodiumJna : JnaLibsodiumInterface lateinit var sodiumJna : JnaLibsodiumInterface
actual suspend fun initialize() { actual suspend fun initialize() {
sodiumJna = loadLibrary() sodiumJna = loadLibrary()
sodiumJna.sodium_init() sodiumJna.sodium_init().ensureLibsodiumSuccess()
isPlatformInitialized = true isPlatformInitialized = true
} }
actual fun initializeWithCallback(done: () -> Unit) { actual fun initializeWithCallback(done: () -> Unit) {
sodiumJna = loadLibrary() sodiumJna = loadLibrary()
sodiumJna.sodium_init() sodiumJna.sodium_init().ensureLibsodiumSuccess()
isPlatformInitialized = true isPlatformInitialized = true
done() done()
} }

View File

@ -1,5 +1,6 @@
package com.ionspin.kotlin.crypto.aead package com.ionspin.kotlin.crypto.aead
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodiumJna import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodiumJna
actual object AuthenticatedEncryptionWithAssociatedData { actual object AuthenticatedEncryptionWithAssociatedData {
@ -24,7 +25,7 @@ actual object AuthenticatedEncryptionWithAssociatedData {
null, null,
nonce.asByteArray(), nonce.asByteArray(),
key.asByteArray(), key.asByteArray(),
) ).ensureLibsodiumSuccess()
return ciphertext return ciphertext
} }
@ -71,7 +72,7 @@ actual object AuthenticatedEncryptionWithAssociatedData {
null, null,
nonce.asByteArray(), nonce.asByteArray(),
key.asByteArray(), key.asByteArray(),
) ).ensureLibsodiumSuccess()
return AeadEncryptedDataAndTag(ciphertext, authenticationTag) return AeadEncryptedDataAndTag(ciphertext, authenticationTag)
} }
@ -117,7 +118,7 @@ actual object AuthenticatedEncryptionWithAssociatedData {
null, null,
nonce.asByteArray(), nonce.asByteArray(),
key.asByteArray(), key.asByteArray(),
) ).ensureLibsodiumSuccess()
return ciphertext return ciphertext
} }
@ -164,7 +165,7 @@ actual object AuthenticatedEncryptionWithAssociatedData {
null, null,
nonce.asByteArray(), nonce.asByteArray(),
key.asByteArray(), key.asByteArray(),
) ).ensureLibsodiumSuccess()
return AeadEncryptedDataAndTag(ciphertext, authenticationTag) return AeadEncryptedDataAndTag(ciphertext, authenticationTag)
} }
@ -210,7 +211,7 @@ actual object AuthenticatedEncryptionWithAssociatedData {
null, null,
nonce.asByteArray(), nonce.asByteArray(),
key.asByteArray(), key.asByteArray(),
) ).ensureLibsodiumSuccess()
return ciphertext return ciphertext
} }
@ -257,7 +258,7 @@ actual object AuthenticatedEncryptionWithAssociatedData {
null, null,
nonce.asByteArray(), nonce.asByteArray(),
key.asByteArray(), key.asByteArray(),
) ).ensureLibsodiumSuccess()
return AeadEncryptedDataAndTag(ciphertext, authenticationTag) return AeadEncryptedDataAndTag(ciphertext, authenticationTag)
} }

View File

@ -1,5 +1,6 @@
package com.ionspin.kotlin.crypto.auth package com.ionspin.kotlin.crypto.auth
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodiumJna import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodiumJna
actual object Auth { actual object Auth {
@ -16,7 +17,7 @@ actual object Auth {
message.asByteArray(), message.asByteArray(),
message.size.toLong(), message.size.toLong(),
key.asByteArray() key.asByteArray()
) ).ensureLibsodiumSuccess()
return mac return mac
} }
@ -42,7 +43,7 @@ actual object Auth {
message.asByteArray(), message.asByteArray(),
message.size.toLong(), message.size.toLong(),
key.asByteArray() key.asByteArray()
) ).ensureLibsodiumSuccess()
return mac return mac
} }
@ -72,7 +73,7 @@ actual object Auth {
message.asByteArray(), message.asByteArray(),
message.size.toLong(), message.size.toLong(),
key.asByteArray() key.asByteArray()
) ).ensureLibsodiumSuccess()
return mac return mac
} }

View File

@ -1,5 +1,6 @@
package com.ionspin.kotlin.crypto.box package com.ionspin.kotlin.crypto.box
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodiumJna import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodiumJna
actual object Box { actual object Box {
@ -11,7 +12,7 @@ actual object Box {
actual fun keypair(): BoxKeyPair { actual fun keypair(): BoxKeyPair {
val publicKey = UByteArray(crypto_box_PUBLICKEYBYTES) val publicKey = UByteArray(crypto_box_PUBLICKEYBYTES)
val secretKey = UByteArray(crypto_box_SECRETKEYBYTES) val secretKey = UByteArray(crypto_box_SECRETKEYBYTES)
sodiumJna.crypto_box_keypair(publicKey.asByteArray(), secretKey.asByteArray()) sodiumJna.crypto_box_keypair(publicKey.asByteArray(), secretKey.asByteArray()).ensureLibsodiumSuccess()
return BoxKeyPair(publicKey, secretKey) return BoxKeyPair(publicKey, secretKey)
} }
@ -21,7 +22,7 @@ actual object Box {
actual fun seedKeypair(seed: UByteArray): BoxKeyPair { actual fun seedKeypair(seed: UByteArray): BoxKeyPair {
val publicKey = UByteArray(crypto_box_PUBLICKEYBYTES) val publicKey = UByteArray(crypto_box_PUBLICKEYBYTES)
val secretKey = UByteArray(crypto_box_SECRETKEYBYTES) val secretKey = UByteArray(crypto_box_SECRETKEYBYTES)
sodiumJna.crypto_box_seed_keypair(publicKey.asByteArray(), secretKey.asByteArray(), seed.asByteArray()) sodiumJna.crypto_box_seed_keypair(publicKey.asByteArray(), secretKey.asByteArray(), seed.asByteArray()).ensureLibsodiumSuccess()
return BoxKeyPair(publicKey, secretKey) return BoxKeyPair(publicKey, secretKey)
} }
@ -46,7 +47,7 @@ actual object Box {
nonce.asByteArray(), nonce.asByteArray(),
recipientsPublicKey.asByteArray(), recipientsPublicKey.asByteArray(),
sendersSecretKey.asByteArray() sendersSecretKey.asByteArray()
) ).ensureLibsodiumSuccess()
return ciphertext return ciphertext
} }
@ -85,7 +86,7 @@ actual object Box {
*/ */
actual fun beforeNM(publicKey: UByteArray, secretKey: UByteArray): UByteArray { actual fun beforeNM(publicKey: UByteArray, secretKey: UByteArray): UByteArray {
val sessionKey = UByteArray(crypto_box_BEFORENMBYTES) val sessionKey = UByteArray(crypto_box_BEFORENMBYTES)
sodiumJna.crypto_box_beforenm(sessionKey.asByteArray(), publicKey.asByteArray(), secretKey.asByteArray()) sodiumJna.crypto_box_beforenm(sessionKey.asByteArray(), publicKey.asByteArray(), secretKey.asByteArray()).ensureLibsodiumSuccess()
return sessionKey return sessionKey
} }
@ -105,7 +106,7 @@ actual object Box {
message.size.toLong(), message.size.toLong(),
nonce.asByteArray(), nonce.asByteArray(),
precomputedKey.asByteArray() precomputedKey.asByteArray()
) ).ensureLibsodiumSuccess()
return ciphertext return ciphertext
} }
@ -157,7 +158,7 @@ actual object Box {
nonce.asByteArray(), nonce.asByteArray(),
recipientsPublicKey.asByteArray(), recipientsPublicKey.asByteArray(),
sendersSecretKey.asByteArray() sendersSecretKey.asByteArray()
) ).ensureLibsodiumSuccess()
return BoxEncryptedDataAndTag(ciphertext, tag) return BoxEncryptedDataAndTag(ciphertext, tag)
} }
@ -201,7 +202,7 @@ actual object Box {
message.asByteArray(), message.asByteArray(),
message.size.toLong(), message.size.toLong(),
recipientsPublicKey.asByteArray() recipientsPublicKey.asByteArray()
) ).ensureLibsodiumSuccess()
return ciphertextWithPublicKey return ciphertextWithPublicKey
} }

View File

@ -1,6 +1,7 @@
package com.ionspin.kotlin.crypto.generichash package com.ionspin.kotlin.crypto.generichash
import com.ionspin.kotlin.crypto.Blake2bState import com.ionspin.kotlin.crypto.Blake2bState
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodiumJna import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodiumJna
/** /**
@ -24,7 +25,7 @@ actual object GenericHash {
message.size.toLong(), message.size.toLong(),
key?.asByteArray() ?: ByteArray(0), key?.asByteArray() ?: ByteArray(0),
(key?.size ?: 0) (key?.size ?: 0)
) ).ensureLibsodiumSuccess()
return hash return hash
} }
@ -33,7 +34,7 @@ actual object GenericHash {
key: UByteArray? key: UByteArray?
): GenericHashState { ): GenericHashState {
val state = GenericHashStateInternal() val state = GenericHashStateInternal()
sodiumJna.crypto_generichash_init(state, key?.asByteArray() ?: ByteArray(0), key?.size ?: 0, requestedHashLength) sodiumJna.crypto_generichash_init(state, key?.asByteArray() ?: ByteArray(0), key?.size ?: 0, requestedHashLength).ensureLibsodiumSuccess()
return GenericHashState(requestedHashLength, state) return GenericHashState(requestedHashLength, state)
} }
@ -41,12 +42,12 @@ actual object GenericHash {
state: GenericHashState, state: GenericHashState,
messagePart: UByteArray messagePart: UByteArray
) { ) {
sodiumJna.crypto_generichash_update(state.internalState, messagePart.asByteArray(), messagePart.size.toLong()) sodiumJna.crypto_generichash_update(state.internalState, messagePart.asByteArray(), messagePart.size.toLong()).ensureLibsodiumSuccess()
} }
actual fun genericHashFinal(state: GenericHashState): UByteArray { actual fun genericHashFinal(state: GenericHashState): UByteArray {
val hashResult = ByteArray(state.hashLength) val hashResult = ByteArray(state.hashLength)
sodiumJna.crypto_generichash_final(state.internalState, hashResult, state.hashLength) sodiumJna.crypto_generichash_final(state.internalState, hashResult, state.hashLength).ensureLibsodiumSuccess()
return hashResult.asUByteArray() return hashResult.asUByteArray()
} }

View File

@ -1,5 +1,6 @@
package com.ionspin.kotlin.crypto.hash package com.ionspin.kotlin.crypto.hash
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
import com.ionspin.kotlin.crypto.Hash256State import com.ionspin.kotlin.crypto.Hash256State
import com.ionspin.kotlin.crypto.Hash512State import com.ionspin.kotlin.crypto.Hash512State
import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodiumJna import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodiumJna
@ -11,45 +12,45 @@ actual object Hash {
actual fun sha256(data: UByteArray): UByteArray { actual fun sha256(data: UByteArray): UByteArray {
val resultHash = UByteArray(crypto_hash_sha256_BYTES) val resultHash = UByteArray(crypto_hash_sha256_BYTES)
sodiumJna.crypto_hash_sha256(resultHash.asByteArray(), data.asByteArray(), data.size.toLong()) sodiumJna.crypto_hash_sha256(resultHash.asByteArray(), data.asByteArray(), data.size.toLong()).ensureLibsodiumSuccess()
return resultHash return resultHash
} }
actual fun sha256Init(): Sha256State { actual fun sha256Init(): Sha256State {
val state = Hash256State() val state = Hash256State()
sodiumJna.crypto_hash_sha256_init(state) sodiumJna.crypto_hash_sha256_init(state).ensureLibsodiumSuccess()
return state return state
} }
actual fun sha256Update(state: Sha256State, data: UByteArray) { actual fun sha256Update(state: Sha256State, data: UByteArray) {
sodiumJna.crypto_hash_sha256_update(state, data.asByteArray(), data.size.toLong()) sodiumJna.crypto_hash_sha256_update(state, data.asByteArray(), data.size.toLong()).ensureLibsodiumSuccess()
} }
actual fun sha256Final(state: Sha256State): UByteArray { actual fun sha256Final(state: Sha256State): UByteArray {
val resultHash = UByteArray(crypto_hash_sha256_BYTES) val resultHash = UByteArray(crypto_hash_sha256_BYTES)
sodiumJna.crypto_hash_sha256_final(state, resultHash.asByteArray()) sodiumJna.crypto_hash_sha256_final(state, resultHash.asByteArray()).ensureLibsodiumSuccess()
return resultHash return resultHash
} }
actual fun sha512(data: UByteArray): UByteArray { actual fun sha512(data: UByteArray): UByteArray {
val resultHash = UByteArray(crypto_hash_sha512_BYTES) val resultHash = UByteArray(crypto_hash_sha512_BYTES)
sodiumJna.crypto_hash_sha512(resultHash.asByteArray(), data.asByteArray(), data.size.toLong()) sodiumJna.crypto_hash_sha512(resultHash.asByteArray(), data.asByteArray(), data.size.toLong()).ensureLibsodiumSuccess()
return resultHash return resultHash
} }
actual fun sha512Init(): Sha512State { actual fun sha512Init(): Sha512State {
val state = Hash512State() val state = Hash512State()
sodiumJna.crypto_hash_sha512_init(state) sodiumJna.crypto_hash_sha512_init(state).ensureLibsodiumSuccess()
return state return state
} }
actual fun sha512Update(state: Sha512State, data: UByteArray) { actual fun sha512Update(state: Sha512State, data: UByteArray) {
sodiumJna.crypto_hash_sha512_update(state, data.asByteArray(), data.size.toLong()) sodiumJna.crypto_hash_sha512_update(state, data.asByteArray(), data.size.toLong()).ensureLibsodiumSuccess()
} }
actual fun sha512Final(state: Sha512State): UByteArray { actual fun sha512Final(state: Sha512State): UByteArray {
val resultHash = UByteArray(crypto_hash_sha512_BYTES) val resultHash = UByteArray(crypto_hash_sha512_BYTES)
sodiumJna.crypto_hash_sha512_final(state, resultHash.asByteArray()) sodiumJna.crypto_hash_sha512_final(state, resultHash.asByteArray()).ensureLibsodiumSuccess()
return resultHash return resultHash
} }

View File

@ -1,5 +1,6 @@
package com.ionspin.kotlin.crypto.kdf package com.ionspin.kotlin.crypto.kdf
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodiumJna import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodiumJna
actual object Kdf { actual object Kdf {
@ -32,7 +33,7 @@ actual object Kdf {
subkeyId.toLong(), subkeyId.toLong(),
contextEncoded, contextEncoded,
masterKey.asByteArray() masterKey.asByteArray()
) ).ensureLibsodiumSuccess()
return subkey return subkey
} }

View File

@ -1,5 +1,6 @@
package com.ionspin.kotlin.crypto.keyexchange package com.ionspin.kotlin.crypto.keyexchange
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodiumJna import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodiumJna
actual object KeyExchange { actual object KeyExchange {
@ -13,7 +14,7 @@ actual object KeyExchange {
clientPublicKey.asByteArray(), clientPublicKey.asByteArray(),
clientSecretKey.asByteArray(), clientSecretKey.asByteArray(),
serverPublicKey.asByteArray() serverPublicKey.asByteArray()
) ).ensureLibsodiumSuccess()
@ -25,7 +26,7 @@ actual object KeyExchange {
val secretKey = UByteArray(crypto_kx_SECRETKEYBYTES) val secretKey = UByteArray(crypto_kx_SECRETKEYBYTES)
sodiumJna.crypto_kx_keypair(publicKey.asByteArray(), secretKey.asByteArray()) sodiumJna.crypto_kx_keypair(publicKey.asByteArray(), secretKey.asByteArray()).ensureLibsodiumSuccess()
return KeyExchangeKeyPair(publicKey, secretKey) return KeyExchangeKeyPair(publicKey, secretKey)
@ -35,7 +36,7 @@ actual object KeyExchange {
val publicKey = UByteArray(crypto_kx_PUBLICKEYBYTES) val publicKey = UByteArray(crypto_kx_PUBLICKEYBYTES)
val secretKey = UByteArray(crypto_kx_SECRETKEYBYTES) val secretKey = UByteArray(crypto_kx_SECRETKEYBYTES)
sodiumJna.crypto_kx_seed_keypair(publicKey.asByteArray(), secretKey.asByteArray(), seed.asByteArray()) sodiumJna.crypto_kx_seed_keypair(publicKey.asByteArray(), secretKey.asByteArray(), seed.asByteArray()).ensureLibsodiumSuccess()
return KeyExchangeKeyPair(publicKey, secretKey) return KeyExchangeKeyPair(publicKey, secretKey)
} }
@ -50,7 +51,7 @@ actual object KeyExchange {
serverPublicKey.asByteArray(), serverPublicKey.asByteArray(),
serverSecretKey.asByteArray(), serverSecretKey.asByteArray(),
clientPublicKey.asByteArray() clientPublicKey.asByteArray()
) ).ensureLibsodiumSuccess()
return KeyExchangeSessionKeyPair(receiveKey, sendKey) return KeyExchangeSessionKeyPair(receiveKey, sendKey)
} }

View File

@ -1,5 +1,6 @@
package com.ionspin.kotlin.crypto.pwhash package com.ionspin.kotlin.crypto.pwhash
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodiumJna import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodiumJna
actual object PasswordHash { actual object PasswordHash {
@ -30,7 +31,7 @@ actual object PasswordHash {
opsLimit.toLong(), opsLimit.toLong(),
memLimit.toLong(), memLimit.toLong(),
algorithm algorithm
) ).ensureLibsodiumSuccess()
return hashedPassword return hashedPassword
} }
@ -53,7 +54,7 @@ actual object PasswordHash {
password.length.toLong(), password.length.toLong(),
opslimit.toLong(), opslimit.toLong(),
memlimit.toLong() memlimit.toLong()
) ).ensureLibsodiumSuccess()
return output.decodeToString() return output.decodeToString()
} }

View File

@ -1,5 +1,6 @@
package com.ionspin.kotlin.crypto.scalarmult package com.ionspin.kotlin.crypto.scalarmult
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodiumJna import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodiumJna
actual object ScalarMultiplication { actual object ScalarMultiplication {
@ -18,7 +19,7 @@ actual object ScalarMultiplication {
actual fun scalarMultiplication(secretKeyN: UByteArray, publicKeyP: UByteArray): UByteArray { actual fun scalarMultiplication(secretKeyN: UByteArray, publicKeyP: UByteArray): UByteArray {
val result = UByteArray(crypto_scalarmult_BYTES) val result = UByteArray(crypto_scalarmult_BYTES)
sodiumJna.crypto_scalarmult(result.asByteArray(), secretKeyN.asByteArray(), publicKeyP.asByteArray()) sodiumJna.crypto_scalarmult(result.asByteArray(), secretKeyN.asByteArray(), publicKeyP.asByteArray()).ensureLibsodiumSuccess()
return result return result
@ -35,7 +36,7 @@ actual object ScalarMultiplication {
): UByteArray { ): UByteArray {
val result = UByteArray(crypto_scalarmult_BYTES) val result = UByteArray(crypto_scalarmult_BYTES)
sodiumJna.crypto_scalarmult_base(result.asByteArray(), secretKeyN.asByteArray()) sodiumJna.crypto_scalarmult_base(result.asByteArray(), secretKeyN.asByteArray()).ensureLibsodiumSuccess()
return result return result
} }

View File

@ -1,5 +1,6 @@
package com.ionspin.kotlin.crypto.secretbox package com.ionspin.kotlin.crypto.secretbox
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodiumJna import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodiumJna
actual object SecretBox { actual object SecretBox {
@ -11,7 +12,7 @@ actual object SecretBox {
message.size.toLong(), message.size.toLong(),
nonce.asByteArray(), nonce.asByteArray(),
key.asByteArray() key.asByteArray()
) ).ensureLibsodiumSuccess()
return ciphertext return ciphertext
} }
@ -48,7 +49,7 @@ actual object SecretBox {
message.size.toLong(), message.size.toLong(),
nonce.asByteArray(), nonce.asByteArray(),
key.asByteArray() key.asByteArray()
) ).ensureLibsodiumSuccess()
return SecretBoxEncryptedDataAndTag(ciphertext, authenticationTag) return SecretBoxEncryptedDataAndTag(ciphertext, authenticationTag)
} }

View File

@ -1,5 +1,6 @@
package com.ionspin.kotlin.crypto.secretstream package com.ionspin.kotlin.crypto.secretstream
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodiumJna import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodiumJna
import com.ionspin.kotlin.crypto.SecretStreamXChaCha20Poly1305State import com.ionspin.kotlin.crypto.SecretStreamXChaCha20Poly1305State
@ -9,7 +10,7 @@ actual object SecretStream {
actual fun xChaCha20Poly1305InitPush(key: UByteArray): SecretStreamStateAndHeader { actual fun xChaCha20Poly1305InitPush(key: UByteArray): SecretStreamStateAndHeader {
val state = SecretStreamState() val state = SecretStreamState()
val header = UByteArray(sodiumJna.crypto_secretstream_xchacha20poly1305_headerbytes()) val header = UByteArray(sodiumJna.crypto_secretstream_xchacha20poly1305_headerbytes())
sodiumJna.crypto_secretstream_xchacha20poly1305_init_push(state, header.asByteArray(), key.asByteArray()) sodiumJna.crypto_secretstream_xchacha20poly1305_init_push(state, header.asByteArray(), key.asByteArray()).ensureLibsodiumSuccess()
return SecretStreamStateAndHeader(state, header) return SecretStreamStateAndHeader(state, header)
} }
@ -29,7 +30,7 @@ actual object SecretStream {
associatedData.asByteArray(), associatedData.asByteArray(),
associatedData.size.toLong(), associatedData.size.toLong(),
tag.toByte() tag.toByte()
) ).ensureLibsodiumSuccess()
return ciphertext return ciphertext
} }
@ -38,7 +39,7 @@ actual object SecretStream {
header: UByteArray header: UByteArray
): SecretStreamStateAndHeader { ): SecretStreamStateAndHeader {
val state = SecretStreamState() val state = SecretStreamState()
sodiumJna.crypto_secretstream_xchacha20poly1305_init_pull(state, header.asByteArray(), key.asByteArray()) sodiumJna.crypto_secretstream_xchacha20poly1305_init_pull(state, header.asByteArray(), key.asByteArray()).ensureLibsodiumSuccess()
return SecretStreamStateAndHeader(state, header) return SecretStreamStateAndHeader(state, header)
} }

View File

@ -1,5 +1,6 @@
package com.ionspin.kotlin.crypto.shortinputhash package com.ionspin.kotlin.crypto.shortinputhash
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodiumJna import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodiumJna
import com.sun.jna.NativeLong import com.sun.jna.NativeLong
@ -13,13 +14,13 @@ import com.sun.jna.NativeLong
actual object ShortHash { actual object ShortHash {
actual fun shortHash(data: UByteArray, key: UByteArray): UByteArray { actual fun shortHash(data: UByteArray, key: UByteArray): UByteArray {
val hashResult = UByteArray(crypto_shorthash_BYTES) val hashResult = UByteArray(crypto_shorthash_BYTES)
sodiumJna.crypto_shorthash(hashResult.asByteArray(), data.asByteArray(), data.size.toLong(), key.asByteArray()) sodiumJna.crypto_shorthash(hashResult.asByteArray(), data.asByteArray(), data.size.toLong(), key.asByteArray()).ensureLibsodiumSuccess()
return hashResult return hashResult
} }
actual fun shortHashKeygen(): UByteArray { actual fun shortHashKeygen(): UByteArray {
val key = UByteArray(crypto_shorthash_KEYBYTES) val key = UByteArray(crypto_shorthash_KEYBYTES)
sodiumJna.crypto_shorthash_keygen(key.asByteArray()) sodiumJna.crypto_shorthash_keygen(key.asByteArray()).ensureLibsodiumSuccess()
return key return key
} }

View File

@ -1,6 +1,7 @@
package com.ionspin.kotlin.crypto.signature package com.ionspin.kotlin.crypto.signature
import com.ionspin.kotlin.crypto.Ed25519SignatureState import com.ionspin.kotlin.crypto.Ed25519SignatureState
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodiumJna import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodiumJna
actual typealias SignatureState = Ed25519SignatureState actual typealias SignatureState = Ed25519SignatureState
@ -11,7 +12,7 @@ actual object Signature {
} }
actual fun update(state: SignatureState, data: UByteArray) { actual fun update(state: SignatureState, data: UByteArray) {
sodiumJna.crypto_sign_update(state, data.asByteArray(), data.size.toLong()) sodiumJna.crypto_sign_update(state, data.asByteArray(), data.size.toLong()).ensureLibsodiumSuccess()
} }
actual fun finalCreate( actual fun finalCreate(
@ -24,7 +25,7 @@ actual object Signature {
signature.asByteArray(), signature.asByteArray(),
null, null,
secretKey.asByteArray() secretKey.asByteArray()
) ).ensureLibsodiumSuccess()
return signature return signature
} }
@ -53,7 +54,7 @@ actual object Signature {
sodiumJna.crypto_sign_keypair( sodiumJna.crypto_sign_keypair(
publicKey.asByteArray(), publicKey.asByteArray(),
secretKey.asByteArray(), secretKey.asByteArray(),
) ).ensureLibsodiumSuccess()
return SignatureKeyPair(publicKey, secretKey) return SignatureKeyPair(publicKey, secretKey)
} }
@ -71,7 +72,7 @@ actual object Signature {
publicKey.asByteArray(), publicKey.asByteArray(),
secretKey.asByteArray(), secretKey.asByteArray(),
seed.asByteArray() seed.asByteArray()
) ).ensureLibsodiumSuccess()
return SignatureKeyPair(publicKey, secretKey) return SignatureKeyPair(publicKey, secretKey)
} }
@ -88,7 +89,7 @@ actual object Signature {
message.asByteArray(), message.asByteArray(),
message.size.toLong(), message.size.toLong(),
secretKey.asByteArray() secretKey.asByteArray()
) ).ensureLibsodiumSuccess()
return signedMessage return signedMessage
} }
@ -127,7 +128,7 @@ actual object Signature {
message.asByteArray(), message.asByteArray(),
message.size.toLong(), message.size.toLong(),
secretKey.asByteArray() secretKey.asByteArray()
) ).ensureLibsodiumSuccess()
return signature return signature
} }
@ -162,7 +163,7 @@ actual object Signature {
sodiumJna.crypto_sign_ed25519_pk_to_curve25519( sodiumJna.crypto_sign_ed25519_pk_to_curve25519(
x25519PublicKey.asByteArray(), x25519PublicKey.asByteArray(),
ed25519PublicKey.asByteArray() ed25519PublicKey.asByteArray()
) ).ensureLibsodiumSuccess()
return x25519PublicKey return x25519PublicKey
} }
@ -171,7 +172,7 @@ actual object Signature {
sodiumJna.crypto_sign_ed25519_sk_to_curve25519( sodiumJna.crypto_sign_ed25519_sk_to_curve25519(
x25519SecretKey.asByteArray(), x25519SecretKey.asByteArray(),
ed25519SecretKey.asByteArray() ed25519SecretKey.asByteArray()
) ).ensureLibsodiumSuccess()
return x25519SecretKey return x25519SecretKey
} }
@ -185,7 +186,7 @@ actual object Signature {
sodiumJna.crypto_sign_ed25519_sk_to_seed( sodiumJna.crypto_sign_ed25519_sk_to_seed(
seed.asByteArray(), seed.asByteArray(),
secretKey.asByteArray() secretKey.asByteArray()
) ).ensureLibsodiumSuccess()
return seed return seed
@ -201,7 +202,7 @@ actual object Signature {
sodiumJna.crypto_sign_ed25519_sk_to_pk( sodiumJna.crypto_sign_ed25519_sk_to_pk(
publicKey.asByteArray(), publicKey.asByteArray(),
secretKey.asByteArray() secretKey.asByteArray()
) ).ensureLibsodiumSuccess()
return publicKey return publicKey
} }

View File

@ -1,12 +1,13 @@
package com.ionspin.kotlin.crypto.stream package com.ionspin.kotlin.crypto.stream
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodiumJna import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodiumJna
actual object Stream { actual object Stream {
actual fun chacha20(clen: Int, nonce: UByteArray, key: UByteArray): UByteArray { actual fun chacha20(clen: Int, nonce: UByteArray, key: UByteArray): UByteArray {
val result = UByteArray(clen) val result = UByteArray(clen)
sodiumJna.crypto_stream_chacha20(result.asByteArray(), clen.toLong(), nonce.asByteArray(), key.asByteArray()) sodiumJna.crypto_stream_chacha20(result.asByteArray(), clen.toLong(), nonce.asByteArray(), key.asByteArray()).ensureLibsodiumSuccess()
return result return result
} }
@ -24,7 +25,7 @@ actual object Stream {
message.size.toLong(), message.size.toLong(),
nonce.asByteArray(), nonce.asByteArray(),
key.asByteArray() key.asByteArray()
) ).ensureLibsodiumSuccess()
return result return result
} }
@ -44,7 +45,7 @@ actual object Stream {
nonce.asByteArray(), nonce.asByteArray(),
initialCounter.toInt(), initialCounter.toInt(),
key.asByteArray() key.asByteArray()
) ).ensureLibsodiumSuccess()
return result return result
} }
@ -70,7 +71,7 @@ actual object Stream {
message.size.toLong(), message.size.toLong(),
nonce.asByteArray(), nonce.asByteArray(),
key.asByteArray() key.asByteArray()
) ).ensureLibsodiumSuccess()
return result return result
} }
@ -90,55 +91,55 @@ actual object Stream {
nonce.asByteArray(), nonce.asByteArray(),
initialCounter.toLong(), initialCounter.toLong(),
key.asByteArray() key.asByteArray()
) ).ensureLibsodiumSuccess()
return result return result
} }
// actual fun xChacha20Keygen(): UByteArray { actual fun xChacha20Keygen(): UByteArray {
// val result = UByteArray(crypto_stream_chacha20_KEYBYTES) val result = UByteArray(crypto_stream_chacha20_KEYBYTES)
//
// sodiumJna.crypto_stream_xchacha20_keygen(result.asByteArray()) sodiumJna.crypto_stream_xchacha20_keygen(result.asByteArray())
//
// return result return result
// } }
//
// actual fun xChacha20Xor( actual fun xChacha20Xor(
// message: UByteArray, message: UByteArray,
// nonce: UByteArray, nonce: UByteArray,
// key: UByteArray key: UByteArray
// ): UByteArray { ): UByteArray {
// val result = UByteArray(message.size) val result = UByteArray(message.size)
//
// sodiumJna.crypto_stream_xchacha20_xor( sodiumJna.crypto_stream_xchacha20_xor(
// result.asByteArray(), result.asByteArray(),
// message.asByteArray(), message.asByteArray(),
// message.size.toLong(), message.size.toLong(),
// nonce.asByteArray(), nonce.asByteArray(),
// key.asByteArray() key.asByteArray()
// ) ).ensureLibsodiumSuccess()
//
// return result return result
// } }
//
// actual fun xChacha20XorIc( actual fun xChacha20XorIc(
// message: UByteArray, message: UByteArray,
// nonce: UByteArray, nonce: UByteArray,
// initialCounter: ULong, initialCounter: ULong,
// key: UByteArray key: UByteArray
// ): UByteArray { ): UByteArray {
// val result = UByteArray(message.size) val result = UByteArray(message.size)
//
// sodiumJna.crypto_stream_xchacha20_xor_ic( sodiumJna.crypto_stream_xchacha20_xor_ic(
// result.asByteArray(), result.asByteArray(),
// message.asByteArray(), message.asByteArray(),
// message.size.toLong(), message.size.toLong(),
// nonce.asByteArray(), nonce.asByteArray(),
// initialCounter.toLong(), initialCounter.toLong(),
// key.asByteArray() key.asByteArray()
// ) ).ensureLibsodiumSuccess()
//
// return result return result
// } }
} }

View File

@ -1,9 +1,8 @@
package com.ionspin.kotlin.crypto.util package com.ionspin.kotlin.crypto.util
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodiumJna import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodiumJna
import com.sun.jna.ptr.IntByReference import com.sun.jna.ptr.IntByReference
import java.lang.RuntimeException
import java.util.*
actual object LibsodiumUtil { actual object LibsodiumUtil {
actual fun memcmp(first: UByteArray, second: UByteArray): Boolean { actual fun memcmp(first: UByteArray, second: UByteArray): Boolean {
@ -53,7 +52,7 @@ actual object LibsodiumUtil {
paddedDataCopy, paddedDataCopy,
paddedData.size, paddedData.size,
blocksize blocksize
) ).ensureLibsodiumSuccess()
val unpadded = paddedDataCopy.sliceArray(0 until unpaddedSize.value).asUByteArray() val unpadded = paddedDataCopy.sliceArray(0 until unpaddedSize.value).asUByteArray()
@ -72,7 +71,7 @@ actual object LibsodiumUtil {
data.asByteArray(), data.asByteArray(),
data.size, data.size,
variant.value variant.value
) ).ensureLibsodiumSuccess()
//Drop terminating char \0 //Drop terminating char \0
return String(result.sliceArray(0 until result.size - 1)) return String(result.sliceArray(0 until result.size - 1))
} }

View File

@ -2,6 +2,7 @@
package com.ionspin.kotlin.crypto package com.ionspin.kotlin.crypto
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
import libsodium.sodium_init import libsodium.sodium_init
import kotlin.native.concurrent.AtomicInt import kotlin.native.concurrent.AtomicInt
@ -11,7 +12,7 @@ actual object LibsodiumInitializer {
actual suspend fun initialize() { actual suspend fun initialize() {
if (isPlatformInitialized.compareAndSet(0, 1)) { if (isPlatformInitialized.compareAndSet(0, 1)) {
sodium_init() sodium_init().ensureLibsodiumSuccess()
} }
@ -19,7 +20,7 @@ actual object LibsodiumInitializer {
actual fun initializeWithCallback(done: () -> Unit) { actual fun initializeWithCallback(done: () -> Unit) {
if (isPlatformInitialized.compareAndSet(0, 1)) { if (isPlatformInitialized.compareAndSet(0, 1)) {
sodium_init() sodium_init().ensureLibsodiumSuccess()
} }
done() done()
} }

View File

@ -1,5 +1,6 @@
package com.ionspin.kotlin.crypto.aead package com.ionspin.kotlin.crypto.aead
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
import com.ionspin.kotlin.crypto.util.toPtr import com.ionspin.kotlin.crypto.util.toPtr
import kotlinx.cinterop.convert import kotlinx.cinterop.convert
import kotlinx.cinterop.pin import kotlinx.cinterop.pin
@ -48,8 +49,7 @@ actual object AuthenticatedEncryptionWithAssociatedData {
null, // nsec not used in this construct null, // nsec not used in this construct
noncePinned.toPtr(), noncePinned.toPtr(),
keyPinned.toPtr() keyPinned.toPtr()
).ensureLibsodiumSuccess()
)
ciphertextPinned.unpin() ciphertextPinned.unpin()
@ -129,8 +129,7 @@ actual object AuthenticatedEncryptionWithAssociatedData {
null, // nsec not used in this construct null, // nsec not used in this construct
noncePinned.toPtr(), noncePinned.toPtr(),
keyPinned.toPtr() keyPinned.toPtr()
).ensureLibsodiumSuccess()
)
ciphertextPinned.unpin() ciphertextPinned.unpin()
@ -209,8 +208,7 @@ actual object AuthenticatedEncryptionWithAssociatedData {
null, // nsec not used in this construct null, // nsec not used in this construct
noncePinned.toPtr(), noncePinned.toPtr(),
keyPinned.toPtr() keyPinned.toPtr()
).ensureLibsodiumSuccess()
)
ciphertextPinned.unpin() ciphertextPinned.unpin()
@ -290,8 +288,7 @@ actual object AuthenticatedEncryptionWithAssociatedData {
null, // nsec not used in this construct null, // nsec not used in this construct
noncePinned.toPtr(), noncePinned.toPtr(),
keyPinned.toPtr() keyPinned.toPtr()
).ensureLibsodiumSuccess()
)
ciphertextPinned.unpin() ciphertextPinned.unpin()
@ -370,8 +367,7 @@ actual object AuthenticatedEncryptionWithAssociatedData {
null, // nsec not used in this construct null, // nsec not used in this construct
noncePinned.toPtr(), noncePinned.toPtr(),
keyPinned.toPtr() keyPinned.toPtr()
).ensureLibsodiumSuccess()
)
ciphertextPinned.unpin() ciphertextPinned.unpin()
@ -451,8 +447,7 @@ actual object AuthenticatedEncryptionWithAssociatedData {
null, // nsec not used in this construct null, // nsec not used in this construct
noncePinned.toPtr(), noncePinned.toPtr(),
keyPinned.toPtr() keyPinned.toPtr()
).ensureLibsodiumSuccess()
)
ciphertextPinned.unpin() ciphertextPinned.unpin()

View File

@ -1,5 +1,6 @@
package com.ionspin.kotlin.crypto.auth package com.ionspin.kotlin.crypto.auth
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
import com.ionspin.kotlin.crypto.util.toPtr import com.ionspin.kotlin.crypto.util.toPtr
import kotlinx.cinterop.convert import kotlinx.cinterop.convert
import kotlinx.cinterop.pin import kotlinx.cinterop.pin
@ -34,7 +35,7 @@ actual object Auth {
messagePinned.toPtr(), messagePinned.toPtr(),
message.size.convert(), message.size.convert(),
keyPinned.toPtr() keyPinned.toPtr()
) ).ensureLibsodiumSuccess()
macPinned.unpin() macPinned.unpin()
messagePinned.unpin() messagePinned.unpin()
@ -80,7 +81,7 @@ actual object Auth {
messagePinned.toPtr(), messagePinned.toPtr(),
message.size.convert(), message.size.convert(),
keyPinned.toPtr() keyPinned.toPtr()
) ).ensureLibsodiumSuccess()
macPinned.unpin() macPinned.unpin()
messagePinned.unpin() messagePinned.unpin()
@ -131,7 +132,7 @@ actual object Auth {
messagePinned.toPtr(), messagePinned.toPtr(),
message.size.convert(), message.size.convert(),
keyPinned.toPtr() keyPinned.toPtr()
) ).ensureLibsodiumSuccess()
macPinned.unpin() macPinned.unpin()
messagePinned.unpin() messagePinned.unpin()

View File

@ -1,5 +1,6 @@
package com.ionspin.kotlin.crypto.box package com.ionspin.kotlin.crypto.box
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
import com.ionspin.kotlin.crypto.util.toPtr import com.ionspin.kotlin.crypto.util.toPtr
import kotlinx.cinterop.convert import kotlinx.cinterop.convert
import kotlinx.cinterop.pin import kotlinx.cinterop.pin
@ -26,7 +27,7 @@ actual object Box {
val secretKey = UByteArray(crypto_box_SECRETKEYBYTES) val secretKey = UByteArray(crypto_box_SECRETKEYBYTES)
val publicKeyPinned = publicKey.pin() val publicKeyPinned = publicKey.pin()
val secretKeyPinned = secretKey.pin() val secretKeyPinned = secretKey.pin()
crypto_box_keypair(publicKeyPinned.toPtr(), secretKeyPinned.toPtr()) crypto_box_keypair(publicKeyPinned.toPtr(), secretKeyPinned.toPtr()).ensureLibsodiumSuccess()
publicKeyPinned.unpin() publicKeyPinned.unpin()
secretKeyPinned.unpin() secretKeyPinned.unpin()
return BoxKeyPair(publicKey, secretKey) return BoxKeyPair(publicKey, secretKey)
@ -41,7 +42,7 @@ actual object Box {
val publicKeyPinned = publicKey.pin() val publicKeyPinned = publicKey.pin()
val secretKeyPinned = secretKey.pin() val secretKeyPinned = secretKey.pin()
val seedPinned = seed.pin() val seedPinned = seed.pin()
crypto_box_seed_keypair(publicKeyPinned.toPtr(), secretKeyPinned.toPtr(), seedPinned.toPtr()) crypto_box_seed_keypair(publicKeyPinned.toPtr(), secretKeyPinned.toPtr(), seedPinned.toPtr()).ensureLibsodiumSuccess()
publicKeyPinned.unpin() publicKeyPinned.unpin()
secretKeyPinned.unpin() secretKeyPinned.unpin()
seedPinned.unpin() seedPinned.unpin()
@ -76,7 +77,7 @@ actual object Box {
noncePinned.toPtr(), noncePinned.toPtr(),
recipientsPublicKeyPinned.toPtr(), recipientsPublicKeyPinned.toPtr(),
sendersSecretKeyPinned.toPtr() sendersSecretKeyPinned.toPtr()
) ).ensureLibsodiumSuccess()
ciphertextPinned.unpin() ciphertextPinned.unpin()
messagePinned.unpin() messagePinned.unpin()
@ -141,7 +142,7 @@ actual object Box {
val publicKeyPinned = publicKey.pin() val publicKeyPinned = publicKey.pin()
val secretKeyPinned = secretKey.pin() val secretKeyPinned = secretKey.pin()
crypto_box_beforenm(sessionKeyPinned.toPtr(), publicKeyPinned.toPtr(), secretKeyPinned.toPtr()) crypto_box_beforenm(sessionKeyPinned.toPtr(), publicKeyPinned.toPtr(), secretKeyPinned.toPtr()).ensureLibsodiumSuccess()
sessionKeyPinned.unpin() sessionKeyPinned.unpin()
publicKeyPinned.unpin() publicKeyPinned.unpin()
@ -171,7 +172,7 @@ actual object Box {
message.size.convert(), message.size.convert(),
noncePinned.toPtr(), noncePinned.toPtr(),
precomputedKeyPinned.toPtr() precomputedKeyPinned.toPtr()
) ).ensureLibsodiumSuccess()
ciphertextPinned.unpin() ciphertextPinned.unpin()
messagePinned.unpin() messagePinned.unpin()
@ -249,7 +250,7 @@ actual object Box {
noncePinned.toPtr(), noncePinned.toPtr(),
recipientsPublicKeyPinned.toPtr(), recipientsPublicKeyPinned.toPtr(),
sendersSecretKeyPinned.toPtr() sendersSecretKeyPinned.toPtr()
) ).ensureLibsodiumSuccess()
ciphertextPinned.unpin() ciphertextPinned.unpin()
tagPinned.unpin() tagPinned.unpin()
@ -319,7 +320,7 @@ actual object Box {
messagePinned.toPtr(), messagePinned.toPtr(),
message.size.convert(), message.size.convert(),
recipientsPublicKeyPinned.toPtr() recipientsPublicKeyPinned.toPtr()
) ).ensureLibsodiumSuccess()
ciphertextWithPublicKeyPinned.unpin() ciphertextWithPublicKeyPinned.unpin()
messagePinned.unpin() messagePinned.unpin()

View File

@ -1,5 +1,6 @@
package com.ionspin.kotlin.crypto.generichash package com.ionspin.kotlin.crypto.generichash
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
import com.ionspin.kotlin.crypto.util.toPtr import com.ionspin.kotlin.crypto.util.toPtr
import kotlinx.cinterop.addressOf import kotlinx.cinterop.addressOf
import kotlinx.cinterop.convert import kotlinx.cinterop.convert
@ -40,7 +41,7 @@ actual object GenericHash {
message.size.convert(), message.size.convert(),
pinnedKey?.toPtr(), pinnedKey?.toPtr(),
(key?.size ?: 0).convert() (key?.size ?: 0).convert()
) ).ensureLibsodiumSuccess()
pinnedHash.unpin() pinnedHash.unpin()
pinnedKey?.unpin() pinnedKey?.unpin()
pinnedMessage.unpin() pinnedMessage.unpin()
@ -59,7 +60,7 @@ actual object GenericHash {
pinnedKey?.toPtr(), pinnedKey?.toPtr(),
(key?.size ?: 0).convert(), (key?.size ?: 0).convert(),
requestedHashLength.convert() requestedHashLength.convert()
) ).ensureLibsodiumSuccess()
pinnedKey?.unpin() pinnedKey?.unpin()
return GenericHashState(requestedHashLength, statePointed) return GenericHashState(requestedHashLength, statePointed)
} }
@ -73,7 +74,7 @@ actual object GenericHash {
state.internalState.ptr, state.internalState.ptr,
pinnedMessage.toPtr(), pinnedMessage.toPtr(),
messagePart.size.convert() messagePart.size.convert()
) ).ensureLibsodiumSuccess()
pinnedMessage.unpin() pinnedMessage.unpin()
} }
@ -84,7 +85,7 @@ actual object GenericHash {
state.internalState.ptr, state.internalState.ptr,
hashResultPinned.toPtr(), hashResultPinned.toPtr(),
state.hashLength.convert() state.hashLength.convert()
) ).ensureLibsodiumSuccess()
hashResultPinned.unpin() hashResultPinned.unpin()
return hashResult return hashResult
} }

View File

@ -1,5 +1,6 @@
package com.ionspin.kotlin.crypto.hash package com.ionspin.kotlin.crypto.hash
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
import com.ionspin.kotlin.crypto.util.toPtr import com.ionspin.kotlin.crypto.util.toPtr
import kotlinx.cinterop.convert import kotlinx.cinterop.convert
import kotlinx.cinterop.pin import kotlinx.cinterop.pin
@ -23,23 +24,12 @@ actual typealias Sha256State = crypto_hash_sha256_state
actual typealias Sha512State = crypto_hash_sha512_state actual typealias Sha512State = crypto_hash_sha512_state
actual object Hash { actual object Hash {
//Not present in Lazy Sodium
// actual fun hash(data: UByteArray): UByteArray {
// val hashResult = UByteArray(crypto_hash_BYTES)
// val hashResultPinned = hashResult.pin()
// val dataPinned = data.pin()
// crypto_hash(hashResultPinned.toPtr(), dataPinned.toPtr(), data.size.convert())
// hashResultPinned.unpin()
// dataPinned.unpin()
//
// return hashResult
// }
actual fun sha256(data: UByteArray): UByteArray { actual fun sha256(data: UByteArray): UByteArray {
val hashResult = UByteArray(crypto_hash_sha256_BYTES) val hashResult = UByteArray(crypto_hash_sha256_BYTES)
val hashResultPinned = hashResult.pin() val hashResultPinned = hashResult.pin()
val dataPinned = data.pin() val dataPinned = data.pin()
crypto_hash_sha256(hashResultPinned.toPtr(), dataPinned.toPtr(), data.size.convert()) crypto_hash_sha256(hashResultPinned.toPtr(), dataPinned.toPtr(), data.size.convert()).ensureLibsodiumSuccess()
hashResultPinned.unpin() hashResultPinned.unpin()
dataPinned.unpin() dataPinned.unpin()
@ -49,7 +39,7 @@ actual object Hash {
actual fun sha256Init(): Sha256State { actual fun sha256Init(): Sha256State {
val stateAllocated = malloc(Sha256State.size.convert()) val stateAllocated = malloc(Sha256State.size.convert())
val statePointed = stateAllocated!!.reinterpret<Sha256State>().pointed val statePointed = stateAllocated!!.reinterpret<Sha256State>().pointed
crypto_hash_sha256_init(statePointed.ptr) crypto_hash_sha256_init(statePointed.ptr).ensureLibsodiumSuccess()
return statePointed return statePointed
} }
@ -61,7 +51,7 @@ actual object Hash {
actual fun sha256Final(state: Sha256State): UByteArray { actual fun sha256Final(state: Sha256State): UByteArray {
val hashResult = UByteArray(crypto_hash_sha256_BYTES) val hashResult = UByteArray(crypto_hash_sha256_BYTES)
val hashResultPinned = hashResult.pin() val hashResultPinned = hashResult.pin()
crypto_hash_sha256_final(state.ptr, hashResultPinned.toPtr()) crypto_hash_sha256_final(state.ptr, hashResultPinned.toPtr()).ensureLibsodiumSuccess()
return hashResult return hashResult
} }
@ -69,7 +59,7 @@ actual object Hash {
val hashResult = UByteArray(crypto_hash_sha512_BYTES) val hashResult = UByteArray(crypto_hash_sha512_BYTES)
val hashResultPinned = hashResult.pin() val hashResultPinned = hashResult.pin()
val dataPinned = data.pin() val dataPinned = data.pin()
crypto_hash_sha512(hashResultPinned.toPtr(), dataPinned.toPtr(), data.size.convert()) crypto_hash_sha512(hashResultPinned.toPtr(), dataPinned.toPtr(), data.size.convert()).ensureLibsodiumSuccess()
hashResultPinned.unpin() hashResultPinned.unpin()
dataPinned.unpin() dataPinned.unpin()
@ -79,19 +69,19 @@ actual object Hash {
actual fun sha512Init(): Sha512State { actual fun sha512Init(): Sha512State {
val stateAllocated = malloc(Sha512State.size.convert()) val stateAllocated = malloc(Sha512State.size.convert())
val statePointed = stateAllocated!!.reinterpret<Sha512State>().pointed val statePointed = stateAllocated!!.reinterpret<Sha512State>().pointed
crypto_hash_sha512_init(statePointed.ptr) crypto_hash_sha512_init(statePointed.ptr).ensureLibsodiumSuccess()
return statePointed return statePointed
} }
actual fun sha512Update(state: Sha512State, data: UByteArray) { actual fun sha512Update(state: Sha512State, data: UByteArray) {
val dataPinned = data.pin() val dataPinned = data.pin()
crypto_hash_sha512_update(state.ptr, dataPinned.toPtr(), data.size.convert()) crypto_hash_sha512_update(state.ptr, dataPinned.toPtr(), data.size.convert()).ensureLibsodiumSuccess()
} }
actual fun sha512Final(state: Sha512State): UByteArray { actual fun sha512Final(state: Sha512State): UByteArray {
val hashResult = UByteArray(crypto_hash_sha512_BYTES) val hashResult = UByteArray(crypto_hash_sha512_BYTES)
val hashResultPinned = hashResult.pin() val hashResultPinned = hashResult.pin()
crypto_hash_sha512_final(state.ptr, hashResultPinned.toPtr()) crypto_hash_sha512_final(state.ptr, hashResultPinned.toPtr()).ensureLibsodiumSuccess()
return hashResult return hashResult
} }

View File

@ -1,5 +1,6 @@
package com.ionspin.kotlin.crypto.kdf package com.ionspin.kotlin.crypto.kdf
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
import com.ionspin.kotlin.crypto.util.encodeToUByteArray import com.ionspin.kotlin.crypto.util.encodeToUByteArray
import com.ionspin.kotlin.crypto.util.toPtr import com.ionspin.kotlin.crypto.util.toPtr
import kotlinx.cinterop.addressOf import kotlinx.cinterop.addressOf
@ -41,7 +42,7 @@ actual object Kdf {
subkeyId.convert(), subkeyId.convert(),
contextEncodedAndPinned.addressOf(0), contextEncodedAndPinned.addressOf(0),
masterKeyPinned.toPtr() masterKeyPinned.toPtr()
) ).ensureLibsodiumSuccess()
contextEncodedAndPinned.unpin() contextEncodedAndPinned.unpin()
masterKeyPinned.unpin() masterKeyPinned.unpin()

View File

@ -1,5 +1,6 @@
package com.ionspin.kotlin.crypto.keyexchange package com.ionspin.kotlin.crypto.keyexchange
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
import com.ionspin.kotlin.crypto.util.toPtr import com.ionspin.kotlin.crypto.util.toPtr
import kotlinx.cinterop.pin import kotlinx.cinterop.pin
import libsodium.crypto_kx_client_session_keys import libsodium.crypto_kx_client_session_keys
@ -29,7 +30,7 @@ actual object KeyExchange {
clientPublicKeyPinned.toPtr(), clientPublicKeyPinned.toPtr(),
clientSecretKeyPinned.toPtr(), clientSecretKeyPinned.toPtr(),
serverPublicKeyPinned.toPtr() serverPublicKeyPinned.toPtr()
) ).ensureLibsodiumSuccess()
clientPublicKeyPinned.unpin() clientPublicKeyPinned.unpin()
clientSecretKeyPinned.unpin() clientSecretKeyPinned.unpin()
@ -46,7 +47,7 @@ actual object KeyExchange {
val publicKeyPinned = publicKey.pin() val publicKeyPinned = publicKey.pin()
val secretKeyPinned = secretKey.pin() val secretKeyPinned = secretKey.pin()
crypto_kx_keypair(publicKeyPinned.toPtr(), secretKeyPinned.toPtr()) crypto_kx_keypair(publicKeyPinned.toPtr(), secretKeyPinned.toPtr()).ensureLibsodiumSuccess()
publicKeyPinned.unpin() publicKeyPinned.unpin()
secretKeyPinned.unpin() secretKeyPinned.unpin()
@ -61,7 +62,7 @@ actual object KeyExchange {
val publicKeyPinned = publicKey.pin() val publicKeyPinned = publicKey.pin()
val secretKeyPinned = secretKey.pin() val secretKeyPinned = secretKey.pin()
crypto_kx_seed_keypair(publicKeyPinned.toPtr(), secretKeyPinned.toPtr(), seedPinned.toPtr()) crypto_kx_seed_keypair(publicKeyPinned.toPtr(), secretKeyPinned.toPtr(), seedPinned.toPtr()).ensureLibsodiumSuccess()
publicKeyPinned.unpin() publicKeyPinned.unpin()
secretKeyPinned.unpin() secretKeyPinned.unpin()
@ -86,7 +87,7 @@ actual object KeyExchange {
serverPublicKeyPinned.toPtr(), serverPublicKeyPinned.toPtr(),
serverSecretKeyPinned.toPtr(), serverSecretKeyPinned.toPtr(),
clientPublicKeyPinned.toPtr() clientPublicKeyPinned.toPtr()
) ).ensureLibsodiumSuccess()
serverPublicKeyPinned.unpin() serverPublicKeyPinned.unpin()
serverSecretKeyPinned.unpin() serverSecretKeyPinned.unpin()

View File

@ -1,5 +1,6 @@
package com.ionspin.kotlin.crypto.pwhash package com.ionspin.kotlin.crypto.pwhash
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
import com.ionspin.kotlin.crypto.util.toPtr import com.ionspin.kotlin.crypto.util.toPtr
import kotlinx.cinterop.addressOf import kotlinx.cinterop.addressOf
import kotlinx.cinterop.convert import kotlinx.cinterop.convert
@ -69,7 +70,7 @@ actual object PasswordHash {
password.length.convert(), password.length.convert(),
opslimit, opslimit,
memlimit.convert() memlimit.convert()
) ).ensureLibsodiumSuccess()
outputPinned.unpin() outputPinned.unpin()
return output.decodeToString() return output.decodeToString()

View File

@ -1,5 +1,6 @@
package com.ionspin.kotlin.crypto.scalarmult package com.ionspin.kotlin.crypto.scalarmult
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
import com.ionspin.kotlin.crypto.util.toPtr import com.ionspin.kotlin.crypto.util.toPtr
import kotlinx.cinterop.pin import kotlinx.cinterop.pin
import libsodium.crypto_scalarmult import libsodium.crypto_scalarmult
@ -25,7 +26,7 @@ actual object ScalarMultiplication {
val secretKeyNPinned = secretKeyN.pin() val secretKeyNPinned = secretKeyN.pin()
val publicKeyPPinned = publicKeyP.pin() val publicKeyPPinned = publicKeyP.pin()
crypto_scalarmult(resultPinned.toPtr(), secretKeyNPinned.toPtr(), publicKeyPPinned.toPtr()) crypto_scalarmult(resultPinned.toPtr(), secretKeyNPinned.toPtr(), publicKeyPPinned.toPtr()).ensureLibsodiumSuccess()
resultPinned.unpin() resultPinned.unpin()
secretKeyNPinned.unpin() secretKeyNPinned.unpin()
@ -47,7 +48,7 @@ actual object ScalarMultiplication {
val resultPinned = result.pin() val resultPinned = result.pin()
val secretKeyNPinned = secretKeyN.pin() val secretKeyNPinned = secretKeyN.pin()
crypto_scalarmult_base(resultPinned.toPtr(), secretKeyNPinned.toPtr()) crypto_scalarmult_base(resultPinned.toPtr(), secretKeyNPinned.toPtr()).ensureLibsodiumSuccess()
resultPinned.unpin() resultPinned.unpin()
secretKeyNPinned.unpin() secretKeyNPinned.unpin()

View File

@ -1,5 +1,6 @@
package com.ionspin.kotlin.crypto.secretbox package com.ionspin.kotlin.crypto.secretbox
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
import com.ionspin.kotlin.crypto.util.toPtr import com.ionspin.kotlin.crypto.util.toPtr
import kotlinx.cinterop.convert import kotlinx.cinterop.convert
import kotlinx.cinterop.pin import kotlinx.cinterop.pin
@ -76,7 +77,7 @@ actual object SecretBox {
message.size.convert(), message.size.convert(),
noncePinned.toPtr(), noncePinned.toPtr(),
keyPinned.toPtr() keyPinned.toPtr()
) ).ensureLibsodiumSuccess()
ciphertextPinned.unpin() ciphertextPinned.unpin()
authenticationTagPinned.unpin() authenticationTagPinned.unpin()
messagePinned.unpin() messagePinned.unpin()

View File

@ -1,5 +1,6 @@
package com.ionspin.kotlin.crypto.secretstream package com.ionspin.kotlin.crypto.secretstream
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
import com.ionspin.kotlin.crypto.util.toPtr import com.ionspin.kotlin.crypto.util.toPtr
import kotlinx.cinterop.convert import kotlinx.cinterop.convert
import kotlinx.cinterop.pin import kotlinx.cinterop.pin
@ -30,7 +31,7 @@ actual object SecretStream {
statePointed.ptr, statePointed.ptr,
headerPinned.toPtr(), headerPinned.toPtr(),
keyPinned.toPtr() keyPinned.toPtr()
) ).ensureLibsodiumSuccess()
headerPinned.unpin() headerPinned.unpin()
keyPinned.unpin() keyPinned.unpin()
return SecretStreamStateAndHeader(statePointed, header) return SecretStreamStateAndHeader(statePointed, header)
@ -60,7 +61,7 @@ actual object SecretStream {
associatedDataPinned?.toPtr(), associatedDataPinned?.toPtr(),
associatedData.size.convert(), associatedData.size.convert(),
tag tag
) ).ensureLibsodiumSuccess()
ciphertextPinned.unpin() ciphertextPinned.unpin()
messagePinned.unpin() messagePinned.unpin()
@ -80,7 +81,7 @@ actual object SecretStream {
statePointed.ptr, statePointed.ptr,
headerPinned.toPtr(), headerPinned.toPtr(),
keyPinned.toPtr() keyPinned.toPtr()
) ).ensureLibsodiumSuccess()
headerPinned.unpin() headerPinned.unpin()
keyPinned.unpin() keyPinned.unpin()
return SecretStreamStateAndHeader(statePointed, header) return SecretStreamStateAndHeader(statePointed, header)

View File

@ -1,5 +1,6 @@
package com.ionspin.kotlin.crypto.shortinputhash package com.ionspin.kotlin.crypto.shortinputhash
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
import com.ionspin.kotlin.crypto.util.toPtr import com.ionspin.kotlin.crypto.util.toPtr
import kotlinx.cinterop.convert import kotlinx.cinterop.convert
import kotlinx.cinterop.pin import kotlinx.cinterop.pin
@ -17,7 +18,7 @@ actual object ShortHash {
val hashResultPinned = hashResult.pin() val hashResultPinned = hashResult.pin()
val dataPinned = data.pin() val dataPinned = data.pin()
val keyPinned = key.pin() val keyPinned = key.pin()
crypto_shorthash(hashResultPinned.toPtr(), dataPinned.toPtr(), data.size.convert(), keyPinned.toPtr()) crypto_shorthash(hashResultPinned.toPtr(), dataPinned.toPtr(), data.size.convert(), keyPinned.toPtr()).ensureLibsodiumSuccess()
hashResultPinned.unpin() hashResultPinned.unpin()
dataPinned.unpin() dataPinned.unpin()
keyPinned.unpin() keyPinned.unpin()

View File

@ -1,8 +1,27 @@
package com.ionspin.kotlin.crypto.signature package com.ionspin.kotlin.crypto.signature
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
import com.ionspin.kotlin.crypto.util.toPtr import com.ionspin.kotlin.crypto.util.toPtr
import kotlinx.cinterop.* import kotlinx.cinterop.convert
import libsodium.* import kotlinx.cinterop.pin
import kotlinx.cinterop.pointed
import kotlinx.cinterop.ptr
import kotlinx.cinterop.reinterpret
import libsodium.crypto_sign
import libsodium.crypto_sign_detached
import libsodium.crypto_sign_ed25519_pk_to_curve25519
import libsodium.crypto_sign_ed25519_sk_to_curve25519
import libsodium.crypto_sign_ed25519_sk_to_pk
import libsodium.crypto_sign_ed25519_sk_to_seed
import libsodium.crypto_sign_ed25519ph_state
import libsodium.crypto_sign_final_create
import libsodium.crypto_sign_final_verify
import libsodium.crypto_sign_init
import libsodium.crypto_sign_keypair
import libsodium.crypto_sign_open
import libsodium.crypto_sign_seed_keypair
import libsodium.crypto_sign_update
import libsodium.crypto_sign_verify_detached
import platform.posix.malloc import platform.posix.malloc
actual typealias SignatureState = crypto_sign_ed25519ph_state actual typealias SignatureState = crypto_sign_ed25519ph_state
@ -11,13 +30,13 @@ actual object Signature {
actual fun init(): SignatureState { actual fun init(): SignatureState {
val stateAllocated = malloc(SignatureState.size.convert()) val stateAllocated = malloc(SignatureState.size.convert())
val statePointed = stateAllocated!!.reinterpret<SignatureState>().pointed val statePointed = stateAllocated!!.reinterpret<SignatureState>().pointed
crypto_sign_init(statePointed.ptr) crypto_sign_init(statePointed.ptr).ensureLibsodiumSuccess()
return statePointed return statePointed
} }
actual fun update(state: SignatureState, data: UByteArray) { actual fun update(state: SignatureState, data: UByteArray) {
val dataPinned = data.pin() val dataPinned = data.pin()
crypto_sign_update(state.ptr, dataPinned.toPtr(), data.size.convert()) crypto_sign_update(state.ptr, dataPinned.toPtr(), data.size.convert()).ensureLibsodiumSuccess()
dataPinned.unpin() dataPinned.unpin()
} }
@ -33,7 +52,7 @@ actual object Signature {
signaturePinned.toPtr(), signaturePinned.toPtr(),
null, null,
secretKeyPinned.toPtr() secretKeyPinned.toPtr()
) ).ensureLibsodiumSuccess()
secretKeyPinned.unpin() secretKeyPinned.unpin()
signaturePinned.unpin() signaturePinned.unpin()
return signature return signature
@ -72,7 +91,7 @@ actual object Signature {
crypto_sign_keypair( crypto_sign_keypair(
publicKeyPinned.toPtr(), publicKeyPinned.toPtr(),
secretKeyPinned.toPtr(), secretKeyPinned.toPtr(),
) ).ensureLibsodiumSuccess()
publicKeyPinned.unpin() publicKeyPinned.unpin()
secretKeyPinned.unpin() secretKeyPinned.unpin()
return SignatureKeyPair(publicKey, secretKey) return SignatureKeyPair(publicKey, secretKey)
@ -93,7 +112,7 @@ actual object Signature {
publicKeyPinned.toPtr(), publicKeyPinned.toPtr(),
secretKeyPinned.toPtr(), secretKeyPinned.toPtr(),
seedPinned.toPtr() seedPinned.toPtr()
) ).ensureLibsodiumSuccess()
seedPinned.unpin() seedPinned.unpin()
publicKeyPinned.unpin() publicKeyPinned.unpin()
secretKeyPinned.unpin() secretKeyPinned.unpin()
@ -115,7 +134,7 @@ actual object Signature {
messagePinned.toPtr(), messagePinned.toPtr(),
message.size.convert(), message.size.convert(),
secretKeyPinned.toPtr() secretKeyPinned.toPtr()
) ).ensureLibsodiumSuccess()
signedMessagePinned.unpin() signedMessagePinned.unpin()
messagePinned.unpin() messagePinned.unpin()
secretKeyPinned.unpin() secretKeyPinned.unpin()
@ -165,7 +184,7 @@ actual object Signature {
messagePinned.toPtr(), messagePinned.toPtr(),
message.size.convert(), message.size.convert(),
secretKeyPinned.toPtr() secretKeyPinned.toPtr()
) ).ensureLibsodiumSuccess()
signaturePinned.unpin() signaturePinned.unpin()
messagePinned.unpin() messagePinned.unpin()
secretKeyPinned.unpin() secretKeyPinned.unpin()
@ -210,7 +229,7 @@ actual object Signature {
crypto_sign_ed25519_pk_to_curve25519( crypto_sign_ed25519_pk_to_curve25519(
x25519PublicKeyPinned.toPtr(), x25519PublicKeyPinned.toPtr(),
ed25519PublicKeyPinned.toPtr() ed25519PublicKeyPinned.toPtr()
) ).ensureLibsodiumSuccess()
x25519PublicKeyPinned.unpin() x25519PublicKeyPinned.unpin()
ed25519PublicKeyPinned.unpin() ed25519PublicKeyPinned.unpin()
return x25519PublicKey return x25519PublicKey
@ -223,7 +242,7 @@ actual object Signature {
crypto_sign_ed25519_sk_to_curve25519( crypto_sign_ed25519_sk_to_curve25519(
x25519SecretKeyPinned.toPtr(), x25519SecretKeyPinned.toPtr(),
ed25519SecretKeyPinned.toPtr() ed25519SecretKeyPinned.toPtr()
) ).ensureLibsodiumSuccess()
x25519SecretKeyPinned.unpin() x25519SecretKeyPinned.unpin()
ed25519SecretKeyPinned.unpin() ed25519SecretKeyPinned.unpin()
return x25519SecretKey return x25519SecretKey
@ -242,7 +261,7 @@ actual object Signature {
crypto_sign_ed25519_sk_to_seed( crypto_sign_ed25519_sk_to_seed(
seedPinned.toPtr(), seedPinned.toPtr(),
secretKeyPinned.toPtr() secretKeyPinned.toPtr()
) ).ensureLibsodiumSuccess()
secretKeyPinned.unpin() secretKeyPinned.unpin()
seedPinned.unpin() seedPinned.unpin()
@ -264,7 +283,7 @@ actual object Signature {
crypto_sign_ed25519_sk_to_pk( crypto_sign_ed25519_sk_to_pk(
publicKeyPinned.toPtr(), publicKeyPinned.toPtr(),
secretKeyPinned.toPtr() secretKeyPinned.toPtr()
) ).ensureLibsodiumSuccess()
secretKeyPinned.unpin() secretKeyPinned.unpin()
publicKeyPinned.unpin() publicKeyPinned.unpin()

View File

@ -1,5 +1,6 @@
package com.ionspin.kotlin.crypto.stream package com.ionspin.kotlin.crypto.stream
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
import com.ionspin.kotlin.crypto.util.toPtr import com.ionspin.kotlin.crypto.util.toPtr
import kotlinx.cinterop.convert import kotlinx.cinterop.convert
import kotlinx.cinterop.pin import kotlinx.cinterop.pin
@ -10,9 +11,9 @@ import libsodium.crypto_stream_chacha20_keygen
import libsodium.crypto_stream_chacha20_xor import libsodium.crypto_stream_chacha20_xor
import libsodium.crypto_stream_chacha20_xor_ic import libsodium.crypto_stream_chacha20_xor_ic
//import libsodium.crypto_stream_xchacha20_keygen import libsodium.crypto_stream_xchacha20_keygen
//import libsodium.crypto_stream_xchacha20_xor import libsodium.crypto_stream_xchacha20_xor
//import libsodium.crypto_stream_xchacha20_xor_ic import libsodium.crypto_stream_xchacha20_xor_ic
actual object Stream { actual object Stream {
actual fun chacha20(clen: Int, nonce: UByteArray, key: UByteArray): UByteArray { actual fun chacha20(clen: Int, nonce: UByteArray, key: UByteArray): UByteArray {
@ -21,7 +22,7 @@ actual object Stream {
val noncePinned = nonce.pin() val noncePinned = nonce.pin()
val keyPinned = key.pin() val keyPinned = key.pin()
crypto_stream_chacha20(resultPinned.toPtr(), clen.convert(), noncePinned.toPtr(), keyPinned.toPtr()) crypto_stream_chacha20(resultPinned.toPtr(), clen.convert(), noncePinned.toPtr(), keyPinned.toPtr()).ensureLibsodiumSuccess()
resultPinned.unpin() resultPinned.unpin()
noncePinned.unpin() noncePinned.unpin()
@ -47,7 +48,7 @@ actual object Stream {
message.size.convert(), message.size.convert(),
noncePinned.toPtr(), noncePinned.toPtr(),
keyPinned.toPtr() keyPinned.toPtr()
) ).ensureLibsodiumSuccess()
messagePinned.unpin() messagePinned.unpin()
resultPinned.unpin() resultPinned.unpin()
@ -76,7 +77,7 @@ actual object Stream {
noncePinned.toPtr(), noncePinned.toPtr(),
initialCounter.convert(), initialCounter.convert(),
keyPinned.toPtr() keyPinned.toPtr()
) ).ensureLibsodiumSuccess()
messagePinned.unpin() messagePinned.unpin()
resultPinned.unpin() resultPinned.unpin()
@ -114,7 +115,7 @@ actual object Stream {
message.size.convert(), message.size.convert(),
noncePinned.toPtr(), noncePinned.toPtr(),
keyPinned.toPtr() keyPinned.toPtr()
) ).ensureLibsodiumSuccess()
messagePinned.unpin() messagePinned.unpin()
resultPinned.unpin() resultPinned.unpin()
@ -143,7 +144,7 @@ actual object Stream {
noncePinned.toPtr(), noncePinned.toPtr(),
initialCounter.convert(), initialCounter.convert(),
keyPinned.toPtr() keyPinned.toPtr()
) ).ensureLibsodiumSuccess()
messagePinned.unpin() messagePinned.unpin()
resultPinned.unpin() resultPinned.unpin()
@ -153,70 +154,70 @@ actual object Stream {
return result return result
} }
// actual fun xChacha20Keygen(): UByteArray { actual fun xChacha20Keygen(): UByteArray {
// val result = UByteArray(crypto_stream_xchacha20_KEYBYTES) val result = UByteArray(crypto_stream_xchacha20_KEYBYTES)
// val resultPinned = result.pin() val resultPinned = result.pin()
//
// crypto_stream_xchacha20_keygen(resultPinned.toPtr()) crypto_stream_xchacha20_keygen(resultPinned.toPtr())
//
// resultPinned.unpin() resultPinned.unpin()
//
// return result return result
// } }
//
// actual fun xChacha20Xor( actual fun xChacha20Xor(
// message: UByteArray, message: UByteArray,
// nonce: UByteArray, nonce: UByteArray,
// key: UByteArray key: UByteArray
// ): UByteArray { ): UByteArray {
// val result = UByteArray(message.size) val result = UByteArray(message.size)
// val messagePinned = message.pin() val messagePinned = message.pin()
// val resultPinned = result.pin() val resultPinned = result.pin()
// val noncePinned = nonce.pin() val noncePinned = nonce.pin()
// val keyPinned = key.pin() val keyPinned = key.pin()
//
// crypto_stream_xchacha20_xor( crypto_stream_xchacha20_xor(
// resultPinned.toPtr(), resultPinned.toPtr(),
// messagePinned.toPtr(), messagePinned.toPtr(),
// message.size.convert(), message.size.convert(),
// noncePinned.toPtr(), noncePinned.toPtr(),
// keyPinned.toPtr() keyPinned.toPtr()
// ) ).ensureLibsodiumSuccess()
//
// messagePinned.unpin() messagePinned.unpin()
// resultPinned.unpin() resultPinned.unpin()
// noncePinned.unpin() noncePinned.unpin()
// keyPinned.unpin() keyPinned.unpin()
//
// return result return result
// } }
//
// actual fun xChacha20XorIc( actual fun xChacha20XorIc(
// message: UByteArray, message: UByteArray,
// nonce: UByteArray, nonce: UByteArray,
// initialCounter: ULong, initialCounter: ULong,
// key: UByteArray key: UByteArray
// ): UByteArray { ): UByteArray {
// val result = UByteArray(message.size) val result = UByteArray(message.size)
// val messagePinned = message.pin() val messagePinned = message.pin()
// val resultPinned = result.pin() val resultPinned = result.pin()
// val noncePinned = nonce.pin() val noncePinned = nonce.pin()
// val keyPinned = key.pin() val keyPinned = key.pin()
//
// crypto_stream_xchacha20_xor_ic( crypto_stream_xchacha20_xor_ic(
// resultPinned.toPtr(), resultPinned.toPtr(),
// messagePinned.toPtr(), messagePinned.toPtr(),
// message.size.convert(), message.size.convert(),
// noncePinned.toPtr(), noncePinned.toPtr(),
// initialCounter.convert(), initialCounter.convert(),
// keyPinned.toPtr() keyPinned.toPtr()
// ) ).ensureLibsodiumSuccess()
//
// messagePinned.unpin() messagePinned.unpin()
// resultPinned.unpin() resultPinned.unpin()
// noncePinned.unpin() noncePinned.unpin()
// keyPinned.unpin() keyPinned.unpin()
//
// return result return result
// } }
} }

View File

@ -1,5 +1,6 @@
package com.ionspin.kotlin.crypto.util package com.ionspin.kotlin.crypto.util
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
import kotlinx.cinterop.addressOf import kotlinx.cinterop.addressOf
import kotlinx.cinterop.convert import kotlinx.cinterop.convert
import kotlinx.cinterop.pin import kotlinx.cinterop.pin
@ -53,7 +54,7 @@ actual object LibsodiumUtil {
unpaddedData.size.convert(), unpaddedData.size.convert(),
blocksize.convert(), blocksize.convert(),
resultingSize.convert() resultingSize.convert()
) ).ensureLibsodiumSuccess()
paddedDataPinned.unpin() paddedDataPinned.unpin()
return paddedData return paddedData
} }
@ -68,7 +69,7 @@ actual object LibsodiumUtil {
paddedDataCopyPinned.toPtr(), paddedDataCopyPinned.toPtr(),
paddedData.size.convert(), paddedData.size.convert(),
blocksize.convert() blocksize.convert()
) ).ensureLibsodiumSuccess()
val unpaddedSize = newSizeULong[0] val unpaddedSize = newSizeULong[0]
if (unpaddedSize > Int.MAX_VALUE.toULong()) { if (unpaddedSize > Int.MAX_VALUE.toULong()) {
throw RuntimeException("Unsupported array size (larger than Integer max value) $unpaddedSize") throw RuntimeException("Unsupported array size (larger than Integer max value) $unpaddedSize")
@ -131,7 +132,7 @@ actual object LibsodiumUtil {
null, null,
variant.value variant.value
) ).ensureLibsodiumSuccess()
binLenPinned.unpin() binLenPinned.unpin()
intermediaryResultPinned.unpin() intermediaryResultPinned.unpin()
@ -154,7 +155,7 @@ actual object LibsodiumUtil {
null, null,
null, null,
null null
) ).ensureLibsodiumSuccess()
resultPinned.unpin() resultPinned.unpin()
return result return result
} }

View File

@ -38,6 +38,7 @@ val sonatypeSnapshots = "https://oss.sonatype.org/content/repositories/snapshots
repositories { repositories {
mavenCentral() mavenCentral()
mavenLocal()
maven("https://oss.sonatype.org/content/repositories/snapshots/") maven("https://oss.sonatype.org/content/repositories/snapshots/")
} }