From f7815d009e0a8ceba325c1edb71483d73b5c7316 Mon Sep 17 00:00:00 2001 From: Ugljesa Jovanovic Date: Mon, 22 Feb 2021 22:51:15 +0100 Subject: [PATCH] Added box: --- .../kotlin/crypto/JnaLibsodiumInterface.kt | 145 ++++++++++++++---- .../com/ionspin/kotlin/crypto/box/Box.kt | 26 ++-- 2 files changed, 129 insertions(+), 42 deletions(-) diff --git a/multiplatform-crypto-libsodium-bindings/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/JnaLibsodiumInterface.kt b/multiplatform-crypto-libsodium-bindings/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/JnaLibsodiumInterface.kt index 74f9c28..53bf760 100644 --- a/multiplatform-crypto-libsodium-bindings/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/JnaLibsodiumInterface.kt +++ b/multiplatform-crypto-libsodium-bindings/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/JnaLibsodiumInterface.kt @@ -770,39 +770,126 @@ interface JnaLibsodiumInterface : Library { inputLength: Long, key: ByteArray ): Int -// + + // // // ---- Auth end ---- // // // ---- Box ---- // -// fun crypto_box_keypair() : dynamic -// fun crypto_box_seed_keypair(seed : Uint8Array) : dynamic -// fun crypto_box_easy(message: Uint8Array, -// nonce: Uint8Array, -// recipientsPublicKey: Uint8Array, -// sendersSecretKey: Uint8Array) : Uint8Array -// fun crypto_box_open_easy(ciphertext: Uint8Array, -// nonce: Uint8Array, -// sendersPublicKey: Uint8Array, -// recipientsSecretKey: Uint8Array) : Uint8Array -// fun crypto_box_detached(message: Uint8Array, -// nonce: Uint8Array, -// recipientsPublicKey: Uint8Array, -// sendersSecretKey: Uint8Array) : dynamic -// fun crypto_box_open_detached(ciphertext: Uint8Array, -// tag: Uint8Array, -// nonce: Uint8Array, -// sendersPublicKey: Uint8Array, -// recipientsSecretKey: Uint8Array) : Uint8Array -// fun crypto_box_beforenm(publicKey: Uint8Array, secretKey: Uint8Array) : Uint8Array -// fun crypto_box_easy_afternm(message: Uint8Array, -// nonce: Uint8Array, -// precomputedKey: Uint8Array) : Uint8Array -// fun crypto_box_open_easy_afternm(ciphertext: Uint8Array, -// nonce: Uint8Array, -// precomputedKey: Uint8Array) : Uint8Array -// fun crypto_box_seal(message: Uint8Array, recipientsPublicKey: Uint8Array) : Uint8Array -// fun crypto_box_seal_open(ciphertext: Uint8Array, recipientsPublicKey: Uint8Array, recipientsSecretKey: Uint8Array) : Uint8Array + // int crypto_box_keypair(unsigned char *pk, unsigned char *sk) + fun crypto_box_keypair(publicKey: ByteArray, secretKey: ByteArray) + + // int crypto_box_seed_keypair(unsigned char *pk, unsigned char *sk, + // const unsigned char *seed) + fun crypto_box_seed_keypair( + publicKey: ByteArray, + secretKey: ByteArray, + seed: ByteArray + ) : Int + + // int crypto_box_easy(unsigned char *c, const unsigned char *m, + // unsigned long long mlen, const unsigned char *n, + // const unsigned char *pk, const unsigned char *sk) + fun crypto_box_easy( + ciphertext: ByteArray, + message: ByteArray, + messageLength: Long, + nonce: ByteArray, + recipientPublicKey: ByteArray, + senderSecretKey: ByteArray + ) : Int + + // int crypto_box_open_easy(unsigned char *m, const unsigned char *c, + // unsigned long long clen, const unsigned char *n, + // const unsigned char *pk, const unsigned char *sk) + fun crypto_box_open_easy( + message: ByteArray, + ciphertext: ByteArray, + ciphertextLength: Long, + nonce: ByteArray, + senderPublickKey: ByteArray, + recipientSecretKey: ByteArray + ) : Int + + // int crypto_box_detached(unsigned char *c, unsigned char *mac, + // const unsigned char *m, unsigned long long mlen, + // const unsigned char *n, const unsigned char *pk, + // const unsigned char *sk) + fun crypto_box_detached( + ciphertext: ByteArray, + mac: ByteArray, + message: ByteArray, + messageLength: Long, + nonce: ByteArray, + recipientPublicKey: ByteArray, + senderSecretKey: ByteArray + ) : Int + + // int crypto_box_open_detached( + // unsigned char *m, const unsigned char *c, + // const unsigned char *mac, + // unsigned long long clen, + // const unsigned char *n, + // const unsigned char *pk, + // const unsigned char *sk) + fun crypto_box_open_detached( + message: ByteArray, + ciphertext: ByteArray, + mac: ByteArray, + ciphertextLength: Long, + nonce: ByteArray, + senderPublickKey: ByteArray, + recipientSecretKey: ByteArray + ) : Int + + // int crypto_box_beforenm(unsigned char *k, const unsigned char *pk, + // const unsigned char *sk) + fun crypto_box_beforenm( + sessionKey: ByteArray, + publicKey: ByteArray, + secretKey: ByteArray + ) : Int + // int crypto_box_easy_afternm(unsigned char *c, const unsigned char *m, + // unsigned long long mlen, const unsigned char *n, + // const unsigned char *k) + fun crypto_box_easy_afternm( + ciphertext: ByteArray, + message: ByteArray, + messageLength: Long, + nonce: ByteArray, + sessionKey: ByteArray + ) : Int + + // int crypto_box_open_easy_afternm(unsigned char *m, const unsigned char *c, + // unsigned long long clen, const unsigned char *n, + // const unsigned char *k) + fun crypto_box_open_easy_afternm( + message: ByteArray, + ciphertext: ByteArray, + ciphertextLength: Long, + nonce: ByteArray, + sessionKey: ByteArray + ) : Int + // int crypto_box_seal(unsigned char *c, const unsigned char *m, + // unsigned long long mlen, const unsigned char *pk) + fun crypto_box_seal( + ciphertext: ByteArray, + message: ByteArray, + messageLength: Long, + recipientPublicKey: ByteArray + ) : Int + + + // int crypto_box_seal_open(unsigned char *m, const unsigned char *c, + // unsigned long long clen, + // const unsigned char *pk, const unsigned char *sk) + fun crypto_box_seal_open( + message: ByteArray, + ciphertext: ByteArray, + ciphertextLength: Long, + senderPublickKey: ByteArray, + recipientSecretKey: ByteArray + ) : Int // // // ---- Box end ---- // diff --git a/multiplatform-crypto-libsodium-bindings/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/box/Box.kt b/multiplatform-crypto-libsodium-bindings/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/box/Box.kt index 27b8be7..8f1f990 100644 --- a/multiplatform-crypto-libsodium-bindings/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/box/Box.kt +++ b/multiplatform-crypto-libsodium-bindings/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/box/Box.kt @@ -1,6 +1,6 @@ package com.ionspin.kotlin.crypto.box -import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodium +import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodiumJna actual object Box { /** @@ -11,7 +11,7 @@ actual object Box { actual fun keypair(): BoxKeyPair { val publicKey = UByteArray(crypto_box_PUBLICKEYBYTES) val secretKey = UByteArray(crypto_box_SECRETKEYBYTES) - sodium.crypto_box_keypair(publicKey.asByteArray(), secretKey.asByteArray()) + sodiumJna.crypto_box_keypair(publicKey.asByteArray(), secretKey.asByteArray()) return BoxKeyPair(publicKey, secretKey) } @@ -21,7 +21,7 @@ actual object Box { actual fun seedKeypair(seed: UByteArray): BoxKeyPair { val publicKey = UByteArray(crypto_box_PUBLICKEYBYTES) val secretKey = UByteArray(crypto_box_SECRETKEYBYTES) - sodium.crypto_box_seed_keypair(publicKey.asByteArray(), secretKey.asByteArray(), seed.asByteArray()) + sodiumJna.crypto_box_seed_keypair(publicKey.asByteArray(), secretKey.asByteArray(), seed.asByteArray()) return BoxKeyPair(publicKey, secretKey) } @@ -39,7 +39,7 @@ actual object Box { sendersSecretKey: UByteArray ): UByteArray { val ciphertext = UByteArray(message.size + crypto_box_MACBYTES) - sodium.crypto_box_easy( + sodiumJna.crypto_box_easy( ciphertext.asByteArray(), message.asByteArray(), message.size.toLong(), @@ -64,7 +64,7 @@ actual object Box { recipientsSecretKey: UByteArray ): UByteArray { val message = UByteArray(ciphertext.size - crypto_box_MACBYTES) - val validationResult = sodium.crypto_box_open_easy( + val validationResult = sodiumJna.crypto_box_open_easy( message.asByteArray(), ciphertext.asByteArray(), ciphertext.size.toLong(), @@ -85,7 +85,7 @@ actual object Box { */ actual fun beforeNM(publicKey: UByteArray, secretKey: UByteArray): UByteArray { val sessionKey = UByteArray(crypto_box_BEFORENMBYTES) - sodium.crypto_box_beforenm(sessionKey.asByteArray(), publicKey.asByteArray(), secretKey.asByteArray()) + sodiumJna.crypto_box_beforenm(sessionKey.asByteArray(), publicKey.asByteArray(), secretKey.asByteArray()) return sessionKey } @@ -99,7 +99,7 @@ actual object Box { ): UByteArray { val ciphertext = UByteArray(message.size + crypto_box_MACBYTES) - sodium.crypto_box_easy_afternm( + sodiumJna.crypto_box_easy_afternm( ciphertext.asByteArray(), message.asByteArray(), message.size.toLong(), @@ -119,7 +119,7 @@ actual object Box { precomputedKey: UByteArray ): UByteArray { val message = UByteArray(ciphertext.size - crypto_box_MACBYTES) - val validationResult = sodium.crypto_box_open_easy_afternm( + val validationResult = sodiumJna.crypto_box_open_easy_afternm( message.asByteArray(), ciphertext.asByteArray(), ciphertext.size.toLong(), @@ -149,7 +149,7 @@ actual object Box { val ciphertext = UByteArray(message.size) val tag = UByteArray(crypto_box_MACBYTES) - sodium.crypto_box_detached( + sodiumJna.crypto_box_detached( ciphertext.asByteArray(), tag.asByteArray(), message.asByteArray(), @@ -177,7 +177,7 @@ actual object Box { ): UByteArray { val message = UByteArray(ciphertext.size) - val validationResult = sodium.crypto_box_open_detached( + val validationResult = sodiumJna.crypto_box_open_detached( message.asByteArray(), ciphertext.asByteArray(), tag.asByteArray(), @@ -196,7 +196,7 @@ actual object Box { actual fun seal(message: UByteArray, recipientsPublicKey: UByteArray): UByteArray { val ciphertextWithPublicKey = UByteArray(message.size + crypto_box_SEALBYTES) - sodium.crypto_box_seal( + sodiumJna.crypto_box_seal( ciphertextWithPublicKey.asByteArray(), message.asByteArray(), message.size.toLong(), @@ -207,7 +207,7 @@ actual object Box { actual fun sealOpen(ciphertext: UByteArray, recipientsPublicKey: UByteArray, recipientsSecretKey: UByteArray): UByteArray { val message = UByteArray(ciphertext.size - crypto_box_SEALBYTES) - val validationResult = sodium.crypto_box_seal_open( + val validationResult = sodiumJna.crypto_box_seal_open( message.asByteArray(), ciphertext.asByteArray(), ciphertext.size.toLong(), @@ -223,4 +223,4 @@ actual object Box { } -} \ No newline at end of file +}