From 54489ef6cb22d94ddd3e9e2c99d8fa7012d31619 Mon Sep 17 00:00:00 2001 From: Ugljesa Jovanovic Date: Sun, 30 Aug 2020 11:21:53 +0200 Subject: [PATCH] Start work on _aead_ --- ...thenticatedEncryptionWithAssociatedData.kt | 121 ++++++++++ .../secretbox/SecretBox.kt | 4 +- ...thenticatedEncryptionWithAssociatedData.kt | 119 ++++++++++ ...thenticatedEncryptionWithAssociatedData.kt | 119 ++++++++++ ...thenticatedEncryptionWithAssociatedData.kt | 210 ++++++++++++++++++ 5 files changed, 570 insertions(+), 3 deletions(-) create mode 100644 multiplatform-crypto-libsodium-bindings/src/commonMain/kotlin/com.ionspin.kotlin.crypto/aead/AuthenticatedEncryptionWithAssociatedData.kt create mode 100644 multiplatform-crypto-libsodium-bindings/src/jsMain/kotlin/com/ionspin/kotlin/crypto/aead/AuthenticatedEncryptionWithAssociatedData.kt create mode 100644 multiplatform-crypto-libsodium-bindings/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/aead/AuthenticatedEncryptionWithAssociatedData.kt create mode 100644 multiplatform-crypto-libsodium-bindings/src/nativeMain/kotlin/com/ionspin/kotlin/crypto/aead/AuthenticatedEncryptionWithAssociatedData.kt diff --git a/multiplatform-crypto-libsodium-bindings/src/commonMain/kotlin/com.ionspin.kotlin.crypto/aead/AuthenticatedEncryptionWithAssociatedData.kt b/multiplatform-crypto-libsodium-bindings/src/commonMain/kotlin/com.ionspin.kotlin.crypto/aead/AuthenticatedEncryptionWithAssociatedData.kt new file mode 100644 index 0000000..2108c23 --- /dev/null +++ b/multiplatform-crypto-libsodium-bindings/src/commonMain/kotlin/com.ionspin.kotlin.crypto/aead/AuthenticatedEncryptionWithAssociatedData.kt @@ -0,0 +1,121 @@ +package com.ionspin.kotlin.crypto.aead + +/** + * Created by Ugljesa Jovanovic + * ugljesa.jovanovic@ionspin.com + * on 30-Aug-2020 + */ + +//X - Ietf +val crypto_aead_xchacha20poly1305_ietf_KEYBYTES = 32 +val crypto_aead_xchacha20poly1305_ietf_NPUBBYTES = 24 +val crypto_aead_xchacha20poly1305_ietf_ABYTES = 16 + +// Ietf +val crypto_aead_chacha20poly1305_ietf_KEYBYTES = 32 +val crypto_aead_chacha20poly1305_ietf_NPUBBYTES = 12 +val crypto_aead_chacha20poly1305_ietf_ABYTES = 16 + +// original chacha20poly1305 + +val crypto_aead_chacha20poly1305_KEYBYTES = 32 +val crypto_aead_chacha20poly1305_NPUBBYTES = 8 +val crypto_aead_chacha20poly1305_ABYTES = 16 + + +data class AeadEncryptedDataAndTag(val data: UByteArray, val tag: UByteArray) + +expect object AuthenticatedEncryptionWithAssociatedData { + // X - Ietf + fun xChaCha20Poly1305IetfEncrypt( + message: UByteArray, + associatedData: UByteArray, + nonce: UByteArray, + key: UByteArray + ): UByteArray + + fun xChaCha20Poly1305IetfDecrypt( + ciphertext: UByteArray, + associatedData: UByteArray, + nonce: UByteArray, + key: UByteArray + ): UByteArray + + fun xChaCha20Poly1305IetfEncryptDetached( + message: UByteArray, + associatedData: UByteArray, + nonce: UByteArray, + key: UByteArray + ): AeadEncryptedDataAndTag + + fun xChaCha20Poly1305IetfDecryptDetached( + ciphertext: UByteArray, + tag: UByteArray, + associatedData: UByteArray, + nonce: UByteArray, + key: UByteArray + ): UByteArray + + // Ietf + + fun chaCha20Poly1305IetfEncrypt( + message: UByteArray, + associatedData: UByteArray, + nonce: UByteArray, + key: UByteArray + ): UByteArray + + fun chaCha20Poly1305IetfDecrypt( + ciphertext: UByteArray, + associatedData: UByteArray, + nonce: UByteArray, + key: UByteArray + ): UByteArray + + fun chaCha20Poly1305IetfEncryptDetached( + message: UByteArray, + associatedData: UByteArray, + nonce: UByteArray, + key: UByteArray + ): AeadEncryptedDataAndTag + + fun chaCha20Poly1305IetfDecryptDetached( + ciphertext: UByteArray, + tag: UByteArray, + associatedData: UByteArray, + nonce: UByteArray, + key: UByteArray + ): UByteArray + + // Original chacha20poly1305 + + fun chaCha20Poly1305Encrypt( + message: UByteArray, + associatedData: UByteArray, + nonce: UByteArray, + key: UByteArray + ): UByteArray + + fun chaCha20Poly1305Decrypt( + ciphertext: UByteArray, + associatedData: UByteArray, + nonce: UByteArray, + key: UByteArray + ): UByteArray + + fun chaCha20Poly1305EncryptDetached( + message: UByteArray, + associatedData: UByteArray, + nonce: UByteArray, + key: UByteArray + ): AeadEncryptedDataAndTag + + fun chaCha20Poly1305DecryptDetached( + ciphertext: UByteArray, + tag: UByteArray, + associatedData: UByteArray, + nonce: UByteArray, + key: UByteArray + ): UByteArray + +} diff --git a/multiplatform-crypto-libsodium-bindings/src/commonMain/kotlin/com.ionspin.kotlin.crypto/secretbox/SecretBox.kt b/multiplatform-crypto-libsodium-bindings/src/commonMain/kotlin/com.ionspin.kotlin.crypto/secretbox/SecretBox.kt index 23c3d86..94abe7a 100644 --- a/multiplatform-crypto-libsodium-bindings/src/commonMain/kotlin/com.ionspin.kotlin.crypto/secretbox/SecretBox.kt +++ b/multiplatform-crypto-libsodium-bindings/src/commonMain/kotlin/com.ionspin.kotlin.crypto/secretbox/SecretBox.kt @@ -14,9 +14,7 @@ val crypto_secretbox_NONCEBYTES = 24 class SecretBoxCorruptedOrTamperedDataExceptionOrInvalidKey() : RuntimeException("MAC validation failed. Data is corrupted or tampered with.") -data class SecretBoxEncryptedDataAndTag( - @JsName("data") - val data: UByteArray, val tag: UByteArray) +data class SecretBoxEncryptedDataAndTag(val data: UByteArray, val tag: UByteArray) expect object SecretBox { diff --git a/multiplatform-crypto-libsodium-bindings/src/jsMain/kotlin/com/ionspin/kotlin/crypto/aead/AuthenticatedEncryptionWithAssociatedData.kt b/multiplatform-crypto-libsodium-bindings/src/jsMain/kotlin/com/ionspin/kotlin/crypto/aead/AuthenticatedEncryptionWithAssociatedData.kt new file mode 100644 index 0000000..332f9a4 --- /dev/null +++ b/multiplatform-crypto-libsodium-bindings/src/jsMain/kotlin/com/ionspin/kotlin/crypto/aead/AuthenticatedEncryptionWithAssociatedData.kt @@ -0,0 +1,119 @@ +package com.ionspin.kotlin.crypto.aead + +actual object AuthenticatedEncryptionWithAssociatedData { + + // Ietf + + // Original chacha20poly1305 + actual fun xChaCha20Poly1305IetfEncrypt( + message: UByteArray, + associatedData: UByteArray, + nonce: UByteArray, + key: UByteArray + ): UByteArray { + TODO("not implemented yet") + } + + actual fun xChaCha20Poly1305IetfDecrypt( + ciphertext: UByteArray, + associatedData: UByteArray, + nonce: UByteArray, + key: UByteArray + ): UByteArray { + TODO("not implemented yet") + } + + actual fun xChaCha20Poly1305IetfEncryptDetached( + message: UByteArray, + associatedData: UByteArray, + nonce: UByteArray, + key: UByteArray + ): AeadEncryptedDataAndTag { + TODO("not implemented yet") + } + + actual fun xChaCha20Poly1305IetfDecryptDetached( + ciphertext: UByteArray, + tag: UByteArray, + associatedData: UByteArray, + nonce: UByteArray, + key: UByteArray + ): UByteArray { + TODO("not implemented yet") + } + + actual fun chaCha20Poly1305IetfEncrypt( + message: UByteArray, + associatedData: UByteArray, + nonce: UByteArray, + key: UByteArray + ): UByteArray { + TODO("not implemented yet") + } + + actual fun chaCha20Poly1305IetfDecrypt( + ciphertext: UByteArray, + associatedData: UByteArray, + nonce: UByteArray, + key: UByteArray + ): UByteArray { + TODO("not implemented yet") + } + + actual fun chaCha20Poly1305IetfEncryptDetached( + message: UByteArray, + associatedData: UByteArray, + nonce: UByteArray, + key: UByteArray + ): AeadEncryptedDataAndTag { + TODO("not implemented yet") + } + + actual fun chaCha20Poly1305IetfDecryptDetached( + ciphertext: UByteArray, + tag: UByteArray, + associatedData: UByteArray, + nonce: UByteArray, + key: UByteArray + ): UByteArray { + TODO("not implemented yet") + } + + actual fun chaCha20Poly1305Encrypt( + message: UByteArray, + associatedData: UByteArray, + nonce: UByteArray, + key: UByteArray + ): UByteArray { + TODO("not implemented yet") + } + + actual fun chaCha20Poly1305Decrypt( + ciphertext: UByteArray, + associatedData: UByteArray, + nonce: UByteArray, + key: UByteArray + ): UByteArray { + TODO("not implemented yet") + } + + actual fun chaCha20Poly1305EncryptDetached( + message: UByteArray, + associatedData: UByteArray, + nonce: UByteArray, + key: UByteArray + ): AeadEncryptedDataAndTag { + TODO("not implemented yet") + } + + actual fun chaCha20Poly1305DecryptDetached( + ciphertext: UByteArray, + tag: UByteArray, + associatedData: UByteArray, + nonce: UByteArray, + key: UByteArray + ): UByteArray { + TODO("not implemented yet") + } + +} diff --git a/multiplatform-crypto-libsodium-bindings/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/aead/AuthenticatedEncryptionWithAssociatedData.kt b/multiplatform-crypto-libsodium-bindings/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/aead/AuthenticatedEncryptionWithAssociatedData.kt new file mode 100644 index 0000000..332f9a4 --- /dev/null +++ b/multiplatform-crypto-libsodium-bindings/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/aead/AuthenticatedEncryptionWithAssociatedData.kt @@ -0,0 +1,119 @@ +package com.ionspin.kotlin.crypto.aead + +actual object AuthenticatedEncryptionWithAssociatedData { + + // Ietf + + // Original chacha20poly1305 + actual fun xChaCha20Poly1305IetfEncrypt( + message: UByteArray, + associatedData: UByteArray, + nonce: UByteArray, + key: UByteArray + ): UByteArray { + TODO("not implemented yet") + } + + actual fun xChaCha20Poly1305IetfDecrypt( + ciphertext: UByteArray, + associatedData: UByteArray, + nonce: UByteArray, + key: UByteArray + ): UByteArray { + TODO("not implemented yet") + } + + actual fun xChaCha20Poly1305IetfEncryptDetached( + message: UByteArray, + associatedData: UByteArray, + nonce: UByteArray, + key: UByteArray + ): AeadEncryptedDataAndTag { + TODO("not implemented yet") + } + + actual fun xChaCha20Poly1305IetfDecryptDetached( + ciphertext: UByteArray, + tag: UByteArray, + associatedData: UByteArray, + nonce: UByteArray, + key: UByteArray + ): UByteArray { + TODO("not implemented yet") + } + + actual fun chaCha20Poly1305IetfEncrypt( + message: UByteArray, + associatedData: UByteArray, + nonce: UByteArray, + key: UByteArray + ): UByteArray { + TODO("not implemented yet") + } + + actual fun chaCha20Poly1305IetfDecrypt( + ciphertext: UByteArray, + associatedData: UByteArray, + nonce: UByteArray, + key: UByteArray + ): UByteArray { + TODO("not implemented yet") + } + + actual fun chaCha20Poly1305IetfEncryptDetached( + message: UByteArray, + associatedData: UByteArray, + nonce: UByteArray, + key: UByteArray + ): AeadEncryptedDataAndTag { + TODO("not implemented yet") + } + + actual fun chaCha20Poly1305IetfDecryptDetached( + ciphertext: UByteArray, + tag: UByteArray, + associatedData: UByteArray, + nonce: UByteArray, + key: UByteArray + ): UByteArray { + TODO("not implemented yet") + } + + actual fun chaCha20Poly1305Encrypt( + message: UByteArray, + associatedData: UByteArray, + nonce: UByteArray, + key: UByteArray + ): UByteArray { + TODO("not implemented yet") + } + + actual fun chaCha20Poly1305Decrypt( + ciphertext: UByteArray, + associatedData: UByteArray, + nonce: UByteArray, + key: UByteArray + ): UByteArray { + TODO("not implemented yet") + } + + actual fun chaCha20Poly1305EncryptDetached( + message: UByteArray, + associatedData: UByteArray, + nonce: UByteArray, + key: UByteArray + ): AeadEncryptedDataAndTag { + TODO("not implemented yet") + } + + actual fun chaCha20Poly1305DecryptDetached( + ciphertext: UByteArray, + tag: UByteArray, + associatedData: UByteArray, + nonce: UByteArray, + key: UByteArray + ): UByteArray { + TODO("not implemented yet") + } + +} diff --git a/multiplatform-crypto-libsodium-bindings/src/nativeMain/kotlin/com/ionspin/kotlin/crypto/aead/AuthenticatedEncryptionWithAssociatedData.kt b/multiplatform-crypto-libsodium-bindings/src/nativeMain/kotlin/com/ionspin/kotlin/crypto/aead/AuthenticatedEncryptionWithAssociatedData.kt new file mode 100644 index 0000000..eab798d --- /dev/null +++ b/multiplatform-crypto-libsodium-bindings/src/nativeMain/kotlin/com/ionspin/kotlin/crypto/aead/AuthenticatedEncryptionWithAssociatedData.kt @@ -0,0 +1,210 @@ +package com.ionspin.kotlin.crypto.aead + +import com.ionspin.kotlin.crypto.util.toPtr +import kotlinx.cinterop.convert +import kotlinx.cinterop.pin +import libsodium.crypto_aead_chacha20poly1305_encrypt +import libsodium.crypto_aead_chacha20poly1305_ietf_encrypt +import libsodium.crypto_aead_xchacha20poly1305_ietf_encrypt + +actual object AuthenticatedEncryptionWithAssociatedData { + + // Ietf + + // Original chacha20poly1305 + actual fun xChaCha20Poly1305IetfEncrypt( + message: UByteArray, + associatedData: UByteArray, + nonce: UByteArray, + key: UByteArray + ): UByteArray { + val messagePinned = message.pin() + val associatedDataPinned = associatedData.pin() + val noncePinned = nonce.pin() + val keyPinned = key.pin() + + val ciphertext = UByteArray(message.size + crypto_aead_xchacha20poly1305_ietf_ABYTES) + val ciphertextPinned = ciphertext.pin() + + crypto_aead_xchacha20poly1305_ietf_encrypt( + ciphertextPinned.toPtr(), + null, + messagePinned.toPtr(), + message.size.convert(), + associatedDataPinned.toPtr(), + associatedData.size.convert(), + null, // nsec not used in this construct + noncePinned.toPtr(), + keyPinned.toPtr() + + ) + + ciphertextPinned.unpin() + + messagePinned.unpin() + associatedDataPinned.unpin() + noncePinned.unpin() + keyPinned.unpin() + + return ciphertext + } + + actual fun xChaCha20Poly1305IetfDecrypt( + ciphertext: UByteArray, + associatedData: UByteArray, + nonce: UByteArray, + key: UByteArray + ): UByteArray { + TODO("not implemented yet") + } + + actual fun xChaCha20Poly1305IetfEncryptDetached( + message: UByteArray, + associatedData: UByteArray, + nonce: UByteArray, + key: UByteArray + ): AeadEncryptedDataAndTag { + TODO("not implemented yet") + } + + actual fun xChaCha20Poly1305IetfDecryptDetached( + ciphertext: UByteArray, + tag: UByteArray, + associatedData: UByteArray, + nonce: UByteArray, + key: UByteArray + ): UByteArray { + TODO("not implemented yet") + } + + actual fun chaCha20Poly1305IetfEncrypt( + message: UByteArray, + associatedData: UByteArray, + nonce: UByteArray, + key: UByteArray + ): UByteArray { + val messagePinned = message.pin() + val associatedDataPinned = associatedData.pin() + val noncePinned = nonce.pin() + val keyPinned = key.pin() + + val ciphertext = UByteArray(message.size + crypto_aead_chacha20poly1305_ietf_ABYTES) + val ciphertextPinned = ciphertext.pin() + + crypto_aead_chacha20poly1305_ietf_encrypt( + ciphertextPinned.toPtr(), + null, + messagePinned.toPtr(), + message.size.convert(), + associatedDataPinned.toPtr(), + associatedData.size.convert(), + null, // nsec not used in this construct + noncePinned.toPtr(), + keyPinned.toPtr() + + ) + + ciphertextPinned.unpin() + + messagePinned.unpin() + associatedDataPinned.unpin() + noncePinned.unpin() + keyPinned.unpin() + + return ciphertext + } + + actual fun chaCha20Poly1305IetfDecrypt( + ciphertext: UByteArray, + associatedData: UByteArray, + nonce: UByteArray, + key: UByteArray + ): UByteArray { + TODO("not implemented yet") + } + + actual fun chaCha20Poly1305IetfEncryptDetached( + message: UByteArray, + associatedData: UByteArray, + nonce: UByteArray, + key: UByteArray + ): AeadEncryptedDataAndTag { + TODO("not implemented yet") + } + + actual fun chaCha20Poly1305IetfDecryptDetached( + ciphertext: UByteArray, + tag: UByteArray, + associatedData: UByteArray, + nonce: UByteArray, + key: UByteArray + ): UByteArray { + TODO("not implemented yet") + } + + actual fun chaCha20Poly1305Encrypt( + message: UByteArray, + associatedData: UByteArray, + nonce: UByteArray, + key: UByteArray + ): UByteArray { + val messagePinned = message.pin() + val associatedDataPinned = associatedData.pin() + val noncePinned = nonce.pin() + val keyPinned = key.pin() + + val ciphertext = UByteArray(message.size + crypto_aead_chacha20poly1305_ABYTES) + val ciphertextPinned = ciphertext.pin() + + crypto_aead_chacha20poly1305_encrypt( + ciphertextPinned.toPtr(), + null, + messagePinned.toPtr(), + message.size.convert(), + associatedDataPinned.toPtr(), + associatedData.size.convert(), + null, // nsec not used in this construct + noncePinned.toPtr(), + keyPinned.toPtr() + + ) + + ciphertextPinned.unpin() + + messagePinned.unpin() + associatedDataPinned.unpin() + noncePinned.unpin() + keyPinned.unpin() + + return ciphertext + } + + actual fun chaCha20Poly1305Decrypt( + ciphertext: UByteArray, + associatedData: UByteArray, + nonce: UByteArray, + key: UByteArray + ): UByteArray { + TODO("not implemented yet") + } + + actual fun chaCha20Poly1305EncryptDetached( + message: UByteArray, + associatedData: UByteArray, + nonce: UByteArray, + key: UByteArray + ): AeadEncryptedDataAndTag { + TODO("not implemented yet") + } + + actual fun chaCha20Poly1305DecryptDetached( + ciphertext: UByteArray, + tag: UByteArray, + associatedData: UByteArray, + nonce: UByteArray, + key: UByteArray + ): UByteArray { + TODO("not implemented yet") + } + +}