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:
43 files changed
+388
-339
No files matched your search
+13
-11
@@ -82,6 +82,8 @@ interface JnaLibsodiumInterface : Library {
|
||||
// ---- Utils ----
|
||||
fun sodium_version_string(): String
|
||||
|
||||
// void
|
||||
// randombytes_buf(void * const buf, const size_t size)
|
||||
fun randombytes_buf(buffer: ByteArray, bufferSize: Int)
|
||||
|
||||
// void randombytes_buf_deterministic(void * const buf, const size_t size,
|
||||
@@ -147,7 +149,7 @@ interface JnaLibsodiumInterface : Library {
|
||||
buffer: ByteArray,
|
||||
paddedBufferLength: Int,
|
||||
blockSize: Int
|
||||
)
|
||||
) : Int
|
||||
|
||||
|
||||
// char *sodium_bin2base64(char * const b64, const size_t b64_maxlen,
|
||||
@@ -431,7 +433,7 @@ interface JnaLibsodiumInterface : Library {
|
||||
nsec: ByteArray?,
|
||||
npub: ByteArray,
|
||||
key: ByteArray
|
||||
)
|
||||
) : Int
|
||||
|
||||
// int crypto_aead_chacha20poly1305_ietf_decrypt(
|
||||
// unsigned char *m,
|
||||
@@ -809,7 +811,7 @@ interface JnaLibsodiumInterface : Library {
|
||||
// // ---- Box ----
|
||||
//
|
||||
// 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,
|
||||
// const unsigned char *seed)
|
||||
@@ -1037,7 +1039,7 @@ interface JnaLibsodiumInterface : Library {
|
||||
// int crypto_sign_keypair(unsigned char *pk, unsigned char *sk)
|
||||
fun crypto_sign_keypair(
|
||||
publicKey: ByteArray, secretKey: ByteArray
|
||||
)
|
||||
): Int
|
||||
|
||||
// int crypto_sign_seed_keypair(unsigned char *pk, unsigned char *sk,
|
||||
// const unsigned char *seed)
|
||||
@@ -1066,7 +1068,7 @@ interface JnaLibsodiumInterface : Library {
|
||||
subkeyId: Long,
|
||||
context: ByteArray,
|
||||
key: ByteArray
|
||||
)
|
||||
): Int
|
||||
|
||||
// void crypto_kdf_keygen(unsigned char k[crypto_kdf_KEYBYTES])
|
||||
fun crypto_kdf_keygen(
|
||||
@@ -1135,7 +1137,7 @@ interface JnaLibsodiumInterface : Library {
|
||||
fun crypto_kx_keypair(
|
||||
publicKey: ByteArray,
|
||||
secretKey: ByteArray
|
||||
)
|
||||
): Int
|
||||
|
||||
|
||||
// int crypto_kx_seed_keypair(unsigned char pk[crypto_kx_PUBLICKEYBYTES],
|
||||
@@ -1145,7 +1147,7 @@ interface JnaLibsodiumInterface : Library {
|
||||
publicKey: ByteArray,
|
||||
secretKey: ByteArray,
|
||||
seed: ByteArray
|
||||
)
|
||||
): Int
|
||||
// int crypto_kx_client_session_keys(unsigned char rx[crypto_kx_SESSIONKEYBYTES],
|
||||
// unsigned char tx[crypto_kx_SESSIONKEYBYTES],
|
||||
// const unsigned char client_pk[crypto_kx_PUBLICKEYBYTES],
|
||||
@@ -1157,7 +1159,7 @@ interface JnaLibsodiumInterface : Library {
|
||||
clientPublicKey: ByteArray,
|
||||
clientSecretKey: ByteArray,
|
||||
serverPublicKey: ByteArray
|
||||
)
|
||||
): Int
|
||||
// int crypto_kx_server_session_keys(unsigned char rx[crypto_kx_SESSIONKEYBYTES],
|
||||
// unsigned char tx[crypto_kx_SESSIONKEYBYTES],
|
||||
// const unsigned char server_pk[crypto_kx_PUBLICKEYBYTES],
|
||||
@@ -1169,7 +1171,7 @@ interface JnaLibsodiumInterface : Library {
|
||||
serverPublicKey: ByteArray,
|
||||
serverSecretKey: ByteArray,
|
||||
clientPublicKey: ByteArray
|
||||
)
|
||||
): Int
|
||||
|
||||
//
|
||||
// // ---- Key exchange end ----
|
||||
@@ -1286,9 +1288,9 @@ interface JnaLibsodiumInterface : Library {
|
||||
//
|
||||
// int crypto_scalarmult(unsigned char *q, const unsigned char *n,
|
||||
// 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)
|
||||
fun crypto_scalarmult_base(q: ByteArray, b: ByteArray)
|
||||
fun crypto_scalarmult_base(q: ByteArray, b: ByteArray): Int
|
||||
//
|
||||
// // ---- Scalar multiplication end ----
|
||||
}
|
||||
+3
-2
@@ -1,6 +1,7 @@
|
||||
package com.ionspin.kotlin.crypto
|
||||
|
||||
import com.goterl.resourceloader.SharedLibraryLoader
|
||||
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
|
||||
import com.sun.jna.Native
|
||||
import com.sun.jna.Platform
|
||||
import java.io.File
|
||||
@@ -49,13 +50,13 @@ actual object LibsodiumInitializer {
|
||||
lateinit var sodiumJna : JnaLibsodiumInterface
|
||||
actual suspend fun initialize() {
|
||||
sodiumJna = loadLibrary()
|
||||
sodiumJna.sodium_init()
|
||||
sodiumJna.sodium_init().ensureLibsodiumSuccess()
|
||||
isPlatformInitialized = true
|
||||
}
|
||||
|
||||
actual fun initializeWithCallback(done: () -> Unit) {
|
||||
sodiumJna = loadLibrary()
|
||||
sodiumJna.sodium_init()
|
||||
sodiumJna.sodium_init().ensureLibsodiumSuccess()
|
||||
isPlatformInitialized = true
|
||||
done()
|
||||
}
|
||||
|
||||
+7
-6
@@ -1,5 +1,6 @@
|
||||
package com.ionspin.kotlin.crypto.aead
|
||||
|
||||
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
|
||||
import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodiumJna
|
||||
|
||||
actual object AuthenticatedEncryptionWithAssociatedData {
|
||||
@@ -24,7 +25,7 @@ actual object AuthenticatedEncryptionWithAssociatedData {
|
||||
null,
|
||||
nonce.asByteArray(),
|
||||
key.asByteArray(),
|
||||
)
|
||||
).ensureLibsodiumSuccess()
|
||||
return ciphertext
|
||||
}
|
||||
|
||||
@@ -71,7 +72,7 @@ actual object AuthenticatedEncryptionWithAssociatedData {
|
||||
null,
|
||||
nonce.asByteArray(),
|
||||
key.asByteArray(),
|
||||
)
|
||||
).ensureLibsodiumSuccess()
|
||||
return AeadEncryptedDataAndTag(ciphertext, authenticationTag)
|
||||
}
|
||||
|
||||
@@ -117,7 +118,7 @@ actual object AuthenticatedEncryptionWithAssociatedData {
|
||||
null,
|
||||
nonce.asByteArray(),
|
||||
key.asByteArray(),
|
||||
)
|
||||
).ensureLibsodiumSuccess()
|
||||
return ciphertext
|
||||
}
|
||||
|
||||
@@ -164,7 +165,7 @@ actual object AuthenticatedEncryptionWithAssociatedData {
|
||||
null,
|
||||
nonce.asByteArray(),
|
||||
key.asByteArray(),
|
||||
)
|
||||
).ensureLibsodiumSuccess()
|
||||
return AeadEncryptedDataAndTag(ciphertext, authenticationTag)
|
||||
}
|
||||
|
||||
@@ -210,7 +211,7 @@ actual object AuthenticatedEncryptionWithAssociatedData {
|
||||
null,
|
||||
nonce.asByteArray(),
|
||||
key.asByteArray(),
|
||||
)
|
||||
).ensureLibsodiumSuccess()
|
||||
return ciphertext
|
||||
}
|
||||
|
||||
@@ -257,7 +258,7 @@ actual object AuthenticatedEncryptionWithAssociatedData {
|
||||
null,
|
||||
nonce.asByteArray(),
|
||||
key.asByteArray(),
|
||||
)
|
||||
).ensureLibsodiumSuccess()
|
||||
return AeadEncryptedDataAndTag(ciphertext, authenticationTag)
|
||||
}
|
||||
|
||||
|
||||
+4
-3
@@ -1,5 +1,6 @@
|
||||
package com.ionspin.kotlin.crypto.auth
|
||||
|
||||
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
|
||||
import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodiumJna
|
||||
|
||||
actual object Auth {
|
||||
@@ -16,7 +17,7 @@ actual object Auth {
|
||||
message.asByteArray(),
|
||||
message.size.toLong(),
|
||||
key.asByteArray()
|
||||
)
|
||||
).ensureLibsodiumSuccess()
|
||||
return mac
|
||||
}
|
||||
|
||||
@@ -42,7 +43,7 @@ actual object Auth {
|
||||
message.asByteArray(),
|
||||
message.size.toLong(),
|
||||
key.asByteArray()
|
||||
)
|
||||
).ensureLibsodiumSuccess()
|
||||
return mac
|
||||
}
|
||||
|
||||
@@ -72,7 +73,7 @@ actual object Auth {
|
||||
message.asByteArray(),
|
||||
message.size.toLong(),
|
||||
key.asByteArray()
|
||||
)
|
||||
).ensureLibsodiumSuccess()
|
||||
return mac
|
||||
}
|
||||
|
||||
|
||||
+8
-7
@@ -1,5 +1,6 @@
|
||||
package com.ionspin.kotlin.crypto.box
|
||||
|
||||
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
|
||||
import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodiumJna
|
||||
|
||||
actual object Box {
|
||||
@@ -11,7 +12,7 @@ actual object Box {
|
||||
actual fun keypair(): BoxKeyPair {
|
||||
val publicKey = UByteArray(crypto_box_PUBLICKEYBYTES)
|
||||
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)
|
||||
}
|
||||
|
||||
@@ -21,7 +22,7 @@ actual object Box {
|
||||
actual fun seedKeypair(seed: UByteArray): BoxKeyPair {
|
||||
val publicKey = UByteArray(crypto_box_PUBLICKEYBYTES)
|
||||
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)
|
||||
}
|
||||
|
||||
@@ -46,7 +47,7 @@ actual object Box {
|
||||
nonce.asByteArray(),
|
||||
recipientsPublicKey.asByteArray(),
|
||||
sendersSecretKey.asByteArray()
|
||||
)
|
||||
).ensureLibsodiumSuccess()
|
||||
return ciphertext
|
||||
}
|
||||
|
||||
@@ -85,7 +86,7 @@ actual object Box {
|
||||
*/
|
||||
actual fun beforeNM(publicKey: UByteArray, secretKey: UByteArray): UByteArray {
|
||||
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
|
||||
}
|
||||
|
||||
@@ -105,7 +106,7 @@ actual object Box {
|
||||
message.size.toLong(),
|
||||
nonce.asByteArray(),
|
||||
precomputedKey.asByteArray()
|
||||
)
|
||||
).ensureLibsodiumSuccess()
|
||||
|
||||
return ciphertext
|
||||
}
|
||||
@@ -157,7 +158,7 @@ actual object Box {
|
||||
nonce.asByteArray(),
|
||||
recipientsPublicKey.asByteArray(),
|
||||
sendersSecretKey.asByteArray()
|
||||
)
|
||||
).ensureLibsodiumSuccess()
|
||||
|
||||
return BoxEncryptedDataAndTag(ciphertext, tag)
|
||||
}
|
||||
@@ -201,7 +202,7 @@ actual object Box {
|
||||
message.asByteArray(),
|
||||
message.size.toLong(),
|
||||
recipientsPublicKey.asByteArray()
|
||||
)
|
||||
).ensureLibsodiumSuccess()
|
||||
return ciphertextWithPublicKey
|
||||
}
|
||||
|
||||
|
||||
+5
-4
@@ -1,6 +1,7 @@
|
||||
package com.ionspin.kotlin.crypto.generichash
|
||||
|
||||
import com.ionspin.kotlin.crypto.Blake2bState
|
||||
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
|
||||
import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodiumJna
|
||||
|
||||
/**
|
||||
@@ -24,7 +25,7 @@ actual object GenericHash {
|
||||
message.size.toLong(),
|
||||
key?.asByteArray() ?: ByteArray(0),
|
||||
(key?.size ?: 0)
|
||||
)
|
||||
).ensureLibsodiumSuccess()
|
||||
return hash
|
||||
}
|
||||
|
||||
@@ -33,7 +34,7 @@ actual object GenericHash {
|
||||
key: UByteArray?
|
||||
): GenericHashState {
|
||||
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)
|
||||
}
|
||||
|
||||
@@ -41,12 +42,12 @@ actual object GenericHash {
|
||||
state: GenericHashState,
|
||||
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 {
|
||||
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()
|
||||
}
|
||||
|
||||
|
||||
+9
-8
@@ -1,5 +1,6 @@
|
||||
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.Hash512State
|
||||
import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodiumJna
|
||||
@@ -11,45 +12,45 @@ actual object Hash {
|
||||
|
||||
actual fun sha256(data: UByteArray): UByteArray {
|
||||
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
|
||||
}
|
||||
|
||||
actual fun sha256Init(): Sha256State {
|
||||
val state = Hash256State()
|
||||
sodiumJna.crypto_hash_sha256_init(state)
|
||||
sodiumJna.crypto_hash_sha256_init(state).ensureLibsodiumSuccess()
|
||||
return state
|
||||
}
|
||||
|
||||
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 {
|
||||
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
|
||||
}
|
||||
|
||||
actual fun sha512(data: UByteArray): UByteArray {
|
||||
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
|
||||
}
|
||||
|
||||
actual fun sha512Init(): Sha512State {
|
||||
val state = Hash512State()
|
||||
sodiumJna.crypto_hash_sha512_init(state)
|
||||
sodiumJna.crypto_hash_sha512_init(state).ensureLibsodiumSuccess()
|
||||
return state
|
||||
}
|
||||
|
||||
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 {
|
||||
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
|
||||
}
|
||||
|
||||
|
||||
+2
-1
@@ -1,5 +1,6 @@
|
||||
package com.ionspin.kotlin.crypto.kdf
|
||||
|
||||
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
|
||||
import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodiumJna
|
||||
|
||||
actual object Kdf {
|
||||
@@ -32,7 +33,7 @@ actual object Kdf {
|
||||
subkeyId.toLong(),
|
||||
contextEncoded,
|
||||
masterKey.asByteArray()
|
||||
)
|
||||
).ensureLibsodiumSuccess()
|
||||
|
||||
return subkey
|
||||
}
|
||||
|
||||
+5
-4
@@ -1,5 +1,6 @@
|
||||
package com.ionspin.kotlin.crypto.keyexchange
|
||||
|
||||
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
|
||||
import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodiumJna
|
||||
|
||||
actual object KeyExchange {
|
||||
@@ -13,7 +14,7 @@ actual object KeyExchange {
|
||||
clientPublicKey.asByteArray(),
|
||||
clientSecretKey.asByteArray(),
|
||||
serverPublicKey.asByteArray()
|
||||
)
|
||||
).ensureLibsodiumSuccess()
|
||||
|
||||
|
||||
|
||||
@@ -25,7 +26,7 @@ actual object KeyExchange {
|
||||
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)
|
||||
@@ -35,7 +36,7 @@ actual object KeyExchange {
|
||||
val publicKey = UByteArray(crypto_kx_PUBLICKEYBYTES)
|
||||
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)
|
||||
}
|
||||
@@ -50,7 +51,7 @@ actual object KeyExchange {
|
||||
serverPublicKey.asByteArray(),
|
||||
serverSecretKey.asByteArray(),
|
||||
clientPublicKey.asByteArray()
|
||||
)
|
||||
).ensureLibsodiumSuccess()
|
||||
|
||||
return KeyExchangeSessionKeyPair(receiveKey, sendKey)
|
||||
}
|
||||
|
||||
+3
-2
@@ -1,5 +1,6 @@
|
||||
package com.ionspin.kotlin.crypto.pwhash
|
||||
|
||||
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
|
||||
import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodiumJna
|
||||
|
||||
actual object PasswordHash {
|
||||
@@ -30,7 +31,7 @@ actual object PasswordHash {
|
||||
opsLimit.toLong(),
|
||||
memLimit.toLong(),
|
||||
algorithm
|
||||
)
|
||||
).ensureLibsodiumSuccess()
|
||||
|
||||
return hashedPassword
|
||||
}
|
||||
@@ -53,7 +54,7 @@ actual object PasswordHash {
|
||||
password.length.toLong(),
|
||||
opslimit.toLong(),
|
||||
memlimit.toLong()
|
||||
)
|
||||
).ensureLibsodiumSuccess()
|
||||
return output.decodeToString()
|
||||
}
|
||||
|
||||
|
||||
+3
-2
@@ -1,5 +1,6 @@
|
||||
package com.ionspin.kotlin.crypto.scalarmult
|
||||
|
||||
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
|
||||
import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodiumJna
|
||||
|
||||
actual object ScalarMultiplication {
|
||||
@@ -18,7 +19,7 @@ actual object ScalarMultiplication {
|
||||
actual fun scalarMultiplication(secretKeyN: UByteArray, publicKeyP: UByteArray): UByteArray {
|
||||
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
|
||||
@@ -35,7 +36,7 @@ actual object ScalarMultiplication {
|
||||
): UByteArray {
|
||||
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
|
||||
}
|
||||
|
||||
+3
-2
@@ -1,5 +1,6 @@
|
||||
package com.ionspin.kotlin.crypto.secretbox
|
||||
|
||||
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
|
||||
import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodiumJna
|
||||
|
||||
actual object SecretBox {
|
||||
@@ -11,7 +12,7 @@ actual object SecretBox {
|
||||
message.size.toLong(),
|
||||
nonce.asByteArray(),
|
||||
key.asByteArray()
|
||||
)
|
||||
).ensureLibsodiumSuccess()
|
||||
return ciphertext
|
||||
}
|
||||
|
||||
@@ -48,7 +49,7 @@ actual object SecretBox {
|
||||
message.size.toLong(),
|
||||
nonce.asByteArray(),
|
||||
key.asByteArray()
|
||||
)
|
||||
).ensureLibsodiumSuccess()
|
||||
return SecretBoxEncryptedDataAndTag(ciphertext, authenticationTag)
|
||||
}
|
||||
|
||||
|
||||
+4
-3
@@ -1,5 +1,6 @@
|
||||
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.SecretStreamXChaCha20Poly1305State
|
||||
|
||||
@@ -9,7 +10,7 @@ actual object SecretStream {
|
||||
actual fun xChaCha20Poly1305InitPush(key: UByteArray): SecretStreamStateAndHeader {
|
||||
val state = SecretStreamState()
|
||||
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)
|
||||
}
|
||||
|
||||
@@ -29,7 +30,7 @@ actual object SecretStream {
|
||||
associatedData.asByteArray(),
|
||||
associatedData.size.toLong(),
|
||||
tag.toByte()
|
||||
)
|
||||
).ensureLibsodiumSuccess()
|
||||
return ciphertext
|
||||
}
|
||||
|
||||
@@ -38,7 +39,7 @@ actual object SecretStream {
|
||||
header: UByteArray
|
||||
): SecretStreamStateAndHeader {
|
||||
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)
|
||||
}
|
||||
|
||||
|
||||
+3
-2
@@ -1,5 +1,6 @@
|
||||
package com.ionspin.kotlin.crypto.shortinputhash
|
||||
|
||||
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
|
||||
import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodiumJna
|
||||
import com.sun.jna.NativeLong
|
||||
|
||||
@@ -13,13 +14,13 @@ import com.sun.jna.NativeLong
|
||||
actual object ShortHash {
|
||||
actual fun shortHash(data: UByteArray, key: UByteArray): UByteArray {
|
||||
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
|
||||
}
|
||||
|
||||
actual fun shortHashKeygen(): UByteArray {
|
||||
val key = UByteArray(crypto_shorthash_KEYBYTES)
|
||||
sodiumJna.crypto_shorthash_keygen(key.asByteArray())
|
||||
sodiumJna.crypto_shorthash_keygen(key.asByteArray()).ensureLibsodiumSuccess()
|
||||
return key
|
||||
}
|
||||
|
||||
|
||||
+11
-10
@@ -1,6 +1,7 @@
|
||||
package com.ionspin.kotlin.crypto.signature
|
||||
|
||||
import com.ionspin.kotlin.crypto.Ed25519SignatureState
|
||||
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
|
||||
import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodiumJna
|
||||
|
||||
actual typealias SignatureState = Ed25519SignatureState
|
||||
@@ -11,7 +12,7 @@ actual object Signature {
|
||||
}
|
||||
|
||||
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(
|
||||
@@ -24,7 +25,7 @@ actual object Signature {
|
||||
signature.asByteArray(),
|
||||
null,
|
||||
secretKey.asByteArray()
|
||||
)
|
||||
).ensureLibsodiumSuccess()
|
||||
return signature
|
||||
}
|
||||
|
||||
@@ -53,7 +54,7 @@ actual object Signature {
|
||||
sodiumJna.crypto_sign_keypair(
|
||||
publicKey.asByteArray(),
|
||||
secretKey.asByteArray(),
|
||||
)
|
||||
).ensureLibsodiumSuccess()
|
||||
return SignatureKeyPair(publicKey, secretKey)
|
||||
}
|
||||
|
||||
@@ -71,7 +72,7 @@ actual object Signature {
|
||||
publicKey.asByteArray(),
|
||||
secretKey.asByteArray(),
|
||||
seed.asByteArray()
|
||||
)
|
||||
).ensureLibsodiumSuccess()
|
||||
return SignatureKeyPair(publicKey, secretKey)
|
||||
}
|
||||
|
||||
@@ -88,7 +89,7 @@ actual object Signature {
|
||||
message.asByteArray(),
|
||||
message.size.toLong(),
|
||||
secretKey.asByteArray()
|
||||
)
|
||||
).ensureLibsodiumSuccess()
|
||||
|
||||
return signedMessage
|
||||
}
|
||||
@@ -127,7 +128,7 @@ actual object Signature {
|
||||
message.asByteArray(),
|
||||
message.size.toLong(),
|
||||
secretKey.asByteArray()
|
||||
)
|
||||
).ensureLibsodiumSuccess()
|
||||
|
||||
return signature
|
||||
}
|
||||
@@ -162,7 +163,7 @@ actual object Signature {
|
||||
sodiumJna.crypto_sign_ed25519_pk_to_curve25519(
|
||||
x25519PublicKey.asByteArray(),
|
||||
ed25519PublicKey.asByteArray()
|
||||
)
|
||||
).ensureLibsodiumSuccess()
|
||||
return x25519PublicKey
|
||||
}
|
||||
|
||||
@@ -171,7 +172,7 @@ actual object Signature {
|
||||
sodiumJna.crypto_sign_ed25519_sk_to_curve25519(
|
||||
x25519SecretKey.asByteArray(),
|
||||
ed25519SecretKey.asByteArray()
|
||||
)
|
||||
).ensureLibsodiumSuccess()
|
||||
return x25519SecretKey
|
||||
}
|
||||
|
||||
@@ -185,7 +186,7 @@ actual object Signature {
|
||||
sodiumJna.crypto_sign_ed25519_sk_to_seed(
|
||||
seed.asByteArray(),
|
||||
secretKey.asByteArray()
|
||||
)
|
||||
).ensureLibsodiumSuccess()
|
||||
|
||||
return seed
|
||||
|
||||
@@ -201,7 +202,7 @@ actual object Signature {
|
||||
sodiumJna.crypto_sign_ed25519_sk_to_pk(
|
||||
publicKey.asByteArray(),
|
||||
secretKey.asByteArray()
|
||||
)
|
||||
).ensureLibsodiumSuccess()
|
||||
|
||||
return publicKey
|
||||
}
|
||||
|
||||
+51
-50
@@ -1,12 +1,13 @@
|
||||
package com.ionspin.kotlin.crypto.stream
|
||||
|
||||
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
|
||||
import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodiumJna
|
||||
|
||||
actual object Stream {
|
||||
actual fun chacha20(clen: Int, nonce: UByteArray, key: UByteArray): UByteArray {
|
||||
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
|
||||
}
|
||||
@@ -24,7 +25,7 @@ actual object Stream {
|
||||
message.size.toLong(),
|
||||
nonce.asByteArray(),
|
||||
key.asByteArray()
|
||||
)
|
||||
).ensureLibsodiumSuccess()
|
||||
|
||||
return result
|
||||
}
|
||||
@@ -44,7 +45,7 @@ actual object Stream {
|
||||
nonce.asByteArray(),
|
||||
initialCounter.toInt(),
|
||||
key.asByteArray()
|
||||
)
|
||||
).ensureLibsodiumSuccess()
|
||||
|
||||
return result
|
||||
}
|
||||
@@ -70,7 +71,7 @@ actual object Stream {
|
||||
message.size.toLong(),
|
||||
nonce.asByteArray(),
|
||||
key.asByteArray()
|
||||
)
|
||||
).ensureLibsodiumSuccess()
|
||||
|
||||
return result
|
||||
}
|
||||
@@ -90,55 +91,55 @@ actual object Stream {
|
||||
nonce.asByteArray(),
|
||||
initialCounter.toLong(),
|
||||
key.asByteArray()
|
||||
)
|
||||
).ensureLibsodiumSuccess()
|
||||
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// actual fun xChacha20Keygen(): UByteArray {
|
||||
// val result = UByteArray(crypto_stream_chacha20_KEYBYTES)
|
||||
//
|
||||
// sodiumJna.crypto_stream_xchacha20_keygen(result.asByteArray())
|
||||
//
|
||||
// return result
|
||||
// }
|
||||
//
|
||||
// actual fun xChacha20Xor(
|
||||
// message: UByteArray,
|
||||
// nonce: UByteArray,
|
||||
// key: UByteArray
|
||||
// ): UByteArray {
|
||||
// val result = UByteArray(message.size)
|
||||
//
|
||||
// sodiumJna.crypto_stream_xchacha20_xor(
|
||||
// result.asByteArray(),
|
||||
// message.asByteArray(),
|
||||
// message.size.toLong(),
|
||||
// nonce.asByteArray(),
|
||||
// key.asByteArray()
|
||||
// )
|
||||
//
|
||||
// return result
|
||||
// }
|
||||
//
|
||||
// actual fun xChacha20XorIc(
|
||||
// message: UByteArray,
|
||||
// nonce: UByteArray,
|
||||
// initialCounter: ULong,
|
||||
// key: UByteArray
|
||||
// ): UByteArray {
|
||||
// val result = UByteArray(message.size)
|
||||
//
|
||||
// sodiumJna.crypto_stream_xchacha20_xor_ic(
|
||||
// result.asByteArray(),
|
||||
// message.asByteArray(),
|
||||
// message.size.toLong(),
|
||||
// nonce.asByteArray(),
|
||||
// initialCounter.toLong(),
|
||||
// key.asByteArray()
|
||||
// )
|
||||
//
|
||||
// return result
|
||||
// }
|
||||
actual fun xChacha20Keygen(): UByteArray {
|
||||
val result = UByteArray(crypto_stream_chacha20_KEYBYTES)
|
||||
|
||||
sodiumJna.crypto_stream_xchacha20_keygen(result.asByteArray())
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
actual fun xChacha20Xor(
|
||||
message: UByteArray,
|
||||
nonce: UByteArray,
|
||||
key: UByteArray
|
||||
): UByteArray {
|
||||
val result = UByteArray(message.size)
|
||||
|
||||
sodiumJna.crypto_stream_xchacha20_xor(
|
||||
result.asByteArray(),
|
||||
message.asByteArray(),
|
||||
message.size.toLong(),
|
||||
nonce.asByteArray(),
|
||||
key.asByteArray()
|
||||
).ensureLibsodiumSuccess()
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
actual fun xChacha20XorIc(
|
||||
message: UByteArray,
|
||||
nonce: UByteArray,
|
||||
initialCounter: ULong,
|
||||
key: UByteArray
|
||||
): UByteArray {
|
||||
val result = UByteArray(message.size)
|
||||
|
||||
sodiumJna.crypto_stream_xchacha20_xor_ic(
|
||||
result.asByteArray(),
|
||||
message.asByteArray(),
|
||||
message.size.toLong(),
|
||||
nonce.asByteArray(),
|
||||
initialCounter.toLong(),
|
||||
key.asByteArray()
|
||||
).ensureLibsodiumSuccess()
|
||||
|
||||
return result
|
||||
}
|
||||
}
|
||||
+3
-4
@@ -1,9 +1,8 @@
|
||||
package com.ionspin.kotlin.crypto.util
|
||||
|
||||
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
|
||||
import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodiumJna
|
||||
import com.sun.jna.ptr.IntByReference
|
||||
import java.lang.RuntimeException
|
||||
import java.util.*
|
||||
|
||||
actual object LibsodiumUtil {
|
||||
actual fun memcmp(first: UByteArray, second: UByteArray): Boolean {
|
||||
@@ -53,7 +52,7 @@ actual object LibsodiumUtil {
|
||||
paddedDataCopy,
|
||||
paddedData.size,
|
||||
blocksize
|
||||
)
|
||||
).ensureLibsodiumSuccess()
|
||||
|
||||
val unpadded = paddedDataCopy.sliceArray(0 until unpaddedSize.value).asUByteArray()
|
||||
|
||||
@@ -72,7 +71,7 @@ actual object LibsodiumUtil {
|
||||
data.asByteArray(),
|
||||
data.size,
|
||||
variant.value
|
||||
)
|
||||
).ensureLibsodiumSuccess()
|
||||
//Drop terminating char \0
|
||||
return String(result.sliceArray(0 until result.size - 1))
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user