From e3fe276e4c7103bb515acf0e2058cd736b74ee63 Mon Sep 17 00:00:00 2001 From: Ugljesa Jovanovic Date: Tue, 23 Jun 2020 22:19:19 +0200 Subject: [PATCH] Starting delegated implementation --- .../com/ionspin/kotlin/crypto/Crypto.kt | 11 ++-- .../DelegatedXChaCha20Poly1305.kt | 15 +++++ .../XChaCha20Poly1305Delegated.kt | 63 +++++++++++++++++++ .../com/ionspin/kotlin/crypto/Crypto.kt | 3 +- .../authenticated/XChaCha20Poly1305Pure.kt | 2 +- .../authenticated/XChaCha20Poly1305Test.kt | 6 +- 6 files changed, 89 insertions(+), 11 deletions(-) create mode 100644 multiplatform-crypto-delegated/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/authenticated/XChaCha20Poly1305Delegated.kt diff --git a/multiplatform-crypto-delegated/src/commonMain/kotlin/com/ionspin/kotlin/crypto/Crypto.kt b/multiplatform-crypto-delegated/src/commonMain/kotlin/com/ionspin/kotlin/crypto/Crypto.kt index a7a0ec6..aab902c 100644 --- a/multiplatform-crypto-delegated/src/commonMain/kotlin/com/ionspin/kotlin/crypto/Crypto.kt +++ b/multiplatform-crypto-delegated/src/commonMain/kotlin/com/ionspin/kotlin/crypto/Crypto.kt @@ -1,5 +1,6 @@ package com.ionspin.kotlin.crypto +import com.ionspin.kotlin.crypto.authenticated.XChaCha20Poly1305Delegated import com.ionspin.kotlin.crypto.hash.blake2b.Blake2bProperties import com.ionspin.kotlin.crypto.hash.blake2b.Blake2bDelegated import com.ionspin.kotlin.crypto.hash.blake2b.Blake2bDelegatedStateless @@ -152,12 +153,12 @@ object Crypto { throw RuntimeException("Invalid key size! Required 32, supplied ${key.value.size}") } val nonce = SRNG.getRandomBytes(24) - return EncryptedData(XChaCha20Poly1305Pure.encrypt(key.value, nonce, data.toEncryptableForm(), additionalData), nonce) + return EncryptedData(XChaCha20Poly1305Delegated.encrypt(key.value, nonce, data.toEncryptableForm(), additionalData), nonce) } override fun > decrypt(key: SymmetricKey, encryptedData : EncryptedData, additionalData: UByteArray, byteArrayDeserializer : (UByteArray) -> T) : T { - return byteArrayDeserializer(XChaCha20Poly1305Pure.decrypt(key.value, encryptedData.nonce, encryptedData.ciphertext, additionalData)) + return byteArrayDeserializer(XChaCha20Poly1305Delegated.decrypt(key.value, encryptedData.nonce, encryptedData.ciphertext, additionalData)) } @@ -172,7 +173,7 @@ object Crypto { } class MultipartAuthenticatedEncryptor internal constructor(val key : SymmetricKey, additionalData: UByteArray) : MultipartAuthenticatedEncryption { - val primitive = XChaCha20Poly1305Pure(key.value, additionalData) + val primitive = XChaCha20Poly1305Delegated(key.value, additionalData) override fun encryptPartialData(data: UByteArray): EncryptedDataPart { return EncryptedDataPart(primitive.encryptPartialData(data)) } @@ -185,7 +186,7 @@ class MultipartAuthenticatedEncryptor internal constructor(val key : SymmetricKe } class MultiplatformAuthenticatedVerificator internal constructor(key: SymmetricKey, multipartEncryptedDataDescriptor: MultipartEncryptedDataDescriptor, additionalData: UByteArray) : MultipartAuthenticatedVerification { - val primitive = XChaCha20Poly1305Pure(key.value, additionalData) + val primitive = XChaCha20Poly1305Delegated(key.value, additionalData) val tag = multipartEncryptedDataDescriptor.data.sliceArray( multipartEncryptedDataDescriptor.data.size - 16 until multipartEncryptedDataDescriptor.data.size ) @@ -200,7 +201,7 @@ class MultiplatformAuthenticatedVerificator internal constructor(key: SymmetricK } -class MultipartAuthenticatedDecryptor internal constructor(val encryptor: XChaCha20Poly1305Pure) : MultipartAuthenticatedDecryption { +class MultipartAuthenticatedDecryptor internal constructor(val encryptor: XChaCha20Poly1305Delegated) : MultipartAuthenticatedDecryption { override fun decryptPartialData(data: EncryptedDataPart): DecryptedDataPart { return DecryptedDataPart(encryptor.decrypt(data.data)) } diff --git a/multiplatform-crypto-delegated/src/commonMain/kotlin/com/ionspin/kotlin/crypto/authenticated/DelegatedXChaCha20Poly1305.kt b/multiplatform-crypto-delegated/src/commonMain/kotlin/com/ionspin/kotlin/crypto/authenticated/DelegatedXChaCha20Poly1305.kt index 055b82f..4b70cc9 100644 --- a/multiplatform-crypto-delegated/src/commonMain/kotlin/com/ionspin/kotlin/crypto/authenticated/DelegatedXChaCha20Poly1305.kt +++ b/multiplatform-crypto-delegated/src/commonMain/kotlin/com/ionspin/kotlin/crypto/authenticated/DelegatedXChaCha20Poly1305.kt @@ -5,3 +5,18 @@ package com.ionspin.kotlin.crypto.authenticated * ugljesa.jovanovic@ionspin.com * on 14-Jun-2020 */ +expect class XChaCha20Poly1305Delegated { + companion object { + fun encrypt(key: UByteArray, nonce: UByteArray, message: UByteArray, additionalData: UByteArray) : UByteArray + fun decrypt(key: UByteArray, nonce: UByteArray, cipherText: UByteArray, additionalData: UByteArray) : UByteArray + } + + fun encryptPartialData(data: UByteArray) : UByteArray + fun verifyPartialData(data: UByteArray) + fun checkTag(expectedTag: UByteArray) + fun decrypt(data: UByteArray) : UByteArray + fun finishEncryption() : Pair + +} + + diff --git a/multiplatform-crypto-delegated/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/authenticated/XChaCha20Poly1305Delegated.kt b/multiplatform-crypto-delegated/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/authenticated/XChaCha20Poly1305Delegated.kt new file mode 100644 index 0000000..f2767cc --- /dev/null +++ b/multiplatform-crypto-delegated/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/authenticated/XChaCha20Poly1305Delegated.kt @@ -0,0 +1,63 @@ +package com.ionspin.kotlin.crypto.authenticated + +import com.goterl.lazycode.lazysodium.SodiumJava +import com.ionspin.kotlin.crypto.Initializer.sodium + +/** + * Created by Ugljesa Jovanovic + * ugljesa.jovanovic@ionspin.com + * on 14-Jun-2020 + */ +actual class XChaCha20Poly1305Delegated { + actual companion object { + actual fun encrypt( + key: UByteArray, + nonce: UByteArray, + message: UByteArray, + additionalData: UByteArray + ): UByteArray { + val ciphertext = ByteArray(message.size + sodium.crypto_secretstream_xchacha20poly1305_abytes()) + SodiumJava().crypto_aead_xchacha20poly1305_ietf_encrypt( + ciphertext, + longArrayOf(ciphertext.size.toLong()), + message.toByteArray(), + message.size.toLong(), + additionalData.toByteArray(), + additionalData.size.toLong(), + null, + nonce.toByteArray(), + key.toByteArray() + + ) + return ciphertext.toUByteArray() + } + + actual fun decrypt( + key: UByteArray, + nonce: UByteArray, + cipherText: UByteArray, + additionalData: UByteArray + ): UByteArray { + TODO("not implemented yet") + } + } + + actual fun encryptPartialData(data: UByteArray): UByteArray { + TODO("not implemented yet") + } + + actual fun verifyPartialData(data: UByteArray) { + } + + actual fun checkTag(expectedTag: UByteArray) { + } + + actual fun decrypt(data: UByteArray): UByteArray { + TODO("not implemented yet") + } + + actual fun finishEncryption(): Pair { + TODO("not implemented yet") + } + +} diff --git a/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/Crypto.kt b/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/Crypto.kt index e1caffb..f34fdb8 100644 --- a/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/Crypto.kt +++ b/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/Crypto.kt @@ -1,7 +1,6 @@ package com.ionspin.kotlin.crypto import com.ionspin.kotlin.crypto.authenticated.* -import com.ionspin.kotlin.crypto.hash.blake2b.Blake2b import com.ionspin.kotlin.crypto.hash.blake2b.Blake2bMultipart import com.ionspin.kotlin.crypto.hash.blake2b.Blake2bPure import com.ionspin.kotlin.crypto.hash.sha.Sha256Pure @@ -134,7 +133,7 @@ class MultipartAuthenticatedEncryptor internal constructor(val key : SymmetricKe } override fun finish(): MultipartEncryptedDataDescriptor { - val finished = primitive.finish() + val finished = primitive.finishEncryption() return MultipartEncryptedDataDescriptor(finished.first, finished.second) } diff --git a/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/authenticated/XChaCha20Poly1305Pure.kt b/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/authenticated/XChaCha20Poly1305Pure.kt index 8184fdc..2382247 100644 --- a/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/authenticated/XChaCha20Poly1305Pure.kt +++ b/multiplatform-crypto/src/commonMain/kotlin/com/ionspin/kotlin/crypto/authenticated/XChaCha20Poly1305Pure.kt @@ -168,7 +168,7 @@ class XChaCha20Poly1305Pure(val key: UByteArray, val additionalData: UByteArray) } } - fun finish() : Pair { + fun finishEncryption() : Pair { val cipherTextPad = UByteArray(16 - processedBytes % 16) { 0U } val macData = cipherTextPad + diff --git a/multiplatform-crypto/src/commonTest/kotlin/com/ionspin/kotlin/crypto/authenticated/XChaCha20Poly1305Test.kt b/multiplatform-crypto/src/commonTest/kotlin/com/ionspin/kotlin/crypto/authenticated/XChaCha20Poly1305Test.kt index cda5e1b..05e0be4 100644 --- a/multiplatform-crypto/src/commonTest/kotlin/com/ionspin/kotlin/crypto/authenticated/XChaCha20Poly1305Test.kt +++ b/multiplatform-crypto/src/commonTest/kotlin/com/ionspin/kotlin/crypto/authenticated/XChaCha20Poly1305Test.kt @@ -134,7 +134,7 @@ class XChaCha20Poly1305Test { ) val xChaChaPoly = XChaCha20Poly1305Pure(key, nonce, additionalData) val firstChunk = xChaChaPoly.encryptPartialData(message) - val finalChunk = xChaChaPoly.finish() + val finalChunk = xChaChaPoly.finishEncryption() val result = firstChunk + finalChunk result.contentEquals(expected) @@ -165,11 +165,11 @@ class XChaCha20Poly1305Test { ) val xChaChaPoly = XChaCha20Poly1305Pure(key, nonce, additionalData) val firstChunk = xChaChaPoly.encryptPartialData(message) - val finalChunk = xChaChaPoly.finish() + val finalChunk = xChaChaPoly.finishEncryption() val result = firstChunk + finalChunk result.contentEquals(expected) } } -} \ No newline at end of file +}