From 1424db0336987b80f16e3a7c165da07db47969cc Mon Sep 17 00:00:00 2001 From: Ugljesa Jovanovic Date: Sat, 6 Apr 2024 18:56:26 +0200 Subject: [PATCH] Remove xchacha20 streaming API for now, as they are not available on apple builds --- CHANGELOG.md | 1 + .../stream/Stream.kt | 9 +- .../kotlin/crypto/stream/StreamTest.kt | 43 +++--- .../ionspin/kotlin/crypto/stream/Stream.kt | 70 ++++----- .../ionspin/kotlin/crypto/stream/Stream.kt | 90 ++++++------ .../ionspin/kotlin/crypto/stream/Stream.kt | 138 +++++++++--------- 6 files changed, 177 insertions(+), 174 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7df26d2..a1b0aed 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ (All dates are DD.MM.YYYY) #### 0.9.1-SNAPSHOT - current development snapshot +- Fix #42, return values from libsodium calls are now checked #### 0.9.0 - 23.9.2023 - Breaking changes: diff --git a/multiplatform-crypto-libsodium-bindings/src/commonMain/kotlin/com.ionspin.kotlin.crypto/stream/Stream.kt b/multiplatform-crypto-libsodium-bindings/src/commonMain/kotlin/com.ionspin.kotlin.crypto/stream/Stream.kt index d25a2d8..b443904 100644 --- a/multiplatform-crypto-libsodium-bindings/src/commonMain/kotlin/com.ionspin.kotlin.crypto/stream/Stream.kt +++ b/multiplatform-crypto-libsodium-bindings/src/commonMain/kotlin/com.ionspin.kotlin.crypto/stream/Stream.kt @@ -23,8 +23,9 @@ expect object Stream { fun chacha20IetfXor(message : UByteArray, nonce: UByteArray, key: UByteArray) : UByteArray fun chacha20IetfXorIc(message: UByteArray, nonce: UByteArray, initialCounter: UInt, key: UByteArray) : UByteArray - fun xChacha20Keygen() : UByteArray - - fun xChacha20Xor(message : UByteArray, nonce: UByteArray, key: UByteArray) : UByteArray - fun xChacha20XorIc(message : UByteArray, nonce: UByteArray, initialCounter: ULong, key: UByteArray) : UByteArray + // Not available on apple builds +// fun xChacha20Keygen() : UByteArray +// +// fun xChacha20Xor(message : UByteArray, nonce: UByteArray, key: UByteArray) : UByteArray +// fun xChacha20XorIc(message : UByteArray, nonce: UByteArray, initialCounter: ULong, key: UByteArray) : UByteArray } diff --git a/multiplatform-crypto-libsodium-bindings/src/commonTest/kotlin/com/ionspin/kotlin/crypto/stream/StreamTest.kt b/multiplatform-crypto-libsodium-bindings/src/commonTest/kotlin/com/ionspin/kotlin/crypto/stream/StreamTest.kt index 5024812..a0105e2 100644 --- a/multiplatform-crypto-libsodium-bindings/src/commonTest/kotlin/com/ionspin/kotlin/crypto/stream/StreamTest.kt +++ b/multiplatform-crypto-libsodium-bindings/src/commonTest/kotlin/com/ionspin/kotlin/crypto/stream/StreamTest.kt @@ -67,25 +67,26 @@ class StreamTest { } } - @Test - fun testXChaCha20IetfStream() = runTest { - LibsodiumInitializer.initializeWithCallback { - val message = "This is a cha cha message".encodeToUByteArray() - val nonce = LibsodiumRandom.bufDeterministic(crypto_stream_xchacha20_NONCEBYTES, seed) - val key = Stream.xChacha20Keygen() - val encryptedUsingLibsodium = Stream.xChacha20Xor(message, nonce, key) - val encryptedUsingLibsodiumWithInitialCounter = Stream.xChacha20XorIc(message, nonce, 0U, key) - println(encryptedUsingLibsodium.toHexString()) - println(encryptedUsingLibsodiumWithInitialCounter.toHexString()) - assertTrue { - encryptedUsingLibsodium.contentEquals(encryptedUsingLibsodiumWithInitialCounter) - } - val decryptedUsingLibsodium = Stream.xChacha20Xor(encryptedUsingLibsodium, nonce, key) - println(message.toHexString()) - println(decryptedUsingLibsodium.toHexString()) - assertTrue { - decryptedUsingLibsodium.contentEquals(message) - } - } - } + // Not available on apple builds +// @Test +// fun testXChaCha20IetfStream() = runTest { +// LibsodiumInitializer.initializeWithCallback { +// val message = "This is a cha cha message".encodeToUByteArray() +// val nonce = LibsodiumRandom.bufDeterministic(crypto_stream_xchacha20_NONCEBYTES, seed) +// val key = Stream.xChacha20Keygen() +// val encryptedUsingLibsodium = Stream.xChacha20Xor(message, nonce, key) +// val encryptedUsingLibsodiumWithInitialCounter = Stream.xChacha20XorIc(message, nonce, 0U, key) +// println(encryptedUsingLibsodium.toHexString()) +// println(encryptedUsingLibsodiumWithInitialCounter.toHexString()) +// assertTrue { +// encryptedUsingLibsodium.contentEquals(encryptedUsingLibsodiumWithInitialCounter) +// } +// val decryptedUsingLibsodium = Stream.xChacha20Xor(encryptedUsingLibsodium, nonce, key) +// println(message.toHexString()) +// println(decryptedUsingLibsodium.toHexString()) +// assertTrue { +// decryptedUsingLibsodium.contentEquals(message) +// } +// } +// } } diff --git a/multiplatform-crypto-libsodium-bindings/src/jsMain/kotlin/com/ionspin/kotlin/crypto/stream/Stream.kt b/multiplatform-crypto-libsodium-bindings/src/jsMain/kotlin/com/ionspin/kotlin/crypto/stream/Stream.kt index bba836b..35a9348 100644 --- a/multiplatform-crypto-libsodium-bindings/src/jsMain/kotlin/com/ionspin/kotlin/crypto/stream/Stream.kt +++ b/multiplatform-crypto-libsodium-bindings/src/jsMain/kotlin/com/ionspin/kotlin/crypto/stream/Stream.kt @@ -82,41 +82,41 @@ actual object Stream { return result.toUByteArray() } - actual fun xChacha20Keygen(): UByteArray { - val result = getSodium().crypto_stream_xchacha20_keygen() - - return result.toUByteArray() - } - - actual fun xChacha20Xor( - message: UByteArray, - nonce: UByteArray, - key: UByteArray - ): UByteArray { - val result = getSodium().crypto_stream_xchacha20_xor( - message.toUInt8Array(), - nonce.toUInt8Array(), - key.toUInt8Array() - ) - - return result.toUByteArray() - } - - actual fun xChacha20XorIc( - message: UByteArray, - nonce: UByteArray, - initialCounter: ULong, - key: UByteArray - ): UByteArray { - val result = getSodium().crypto_stream_xchacha20_xor_ic( - message.toUInt8Array(), - nonce.toUInt8Array(), - initialCounter.toUInt(), - key.toUInt8Array() - ) - - return result.toUByteArray() - } +// actual fun xChacha20Keygen(): UByteArray { +// val result = getSodium().crypto_stream_xchacha20_keygen() +// +// return result.toUByteArray() +// } +// +// actual fun xChacha20Xor( +// message: UByteArray, +// nonce: UByteArray, +// key: UByteArray +// ): UByteArray { +// val result = getSodium().crypto_stream_xchacha20_xor( +// message.toUInt8Array(), +// nonce.toUInt8Array(), +// key.toUInt8Array() +// ) +// +// return result.toUByteArray() +// } +// +// actual fun xChacha20XorIc( +// message: UByteArray, +// nonce: UByteArray, +// initialCounter: ULong, +// key: UByteArray +// ): UByteArray { +// val result = getSodium().crypto_stream_xchacha20_xor_ic( +// message.toUInt8Array(), +// nonce.toUInt8Array(), +// initialCounter.toUInt(), +// key.toUInt8Array() +// ) +// +// return result.toUByteArray() +// } } diff --git a/multiplatform-crypto-libsodium-bindings/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/stream/Stream.kt b/multiplatform-crypto-libsodium-bindings/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/stream/Stream.kt index b22c2bd..5bc43b3 100644 --- a/multiplatform-crypto-libsodium-bindings/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/stream/Stream.kt +++ b/multiplatform-crypto-libsodium-bindings/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/stream/Stream.kt @@ -97,49 +97,49 @@ actual object Stream { 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 - } +// 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 +// } } diff --git a/multiplatform-crypto-libsodium-bindings/src/nativeMain/kotlin/com/ionspin/kotlin/crypto/stream/Stream.kt b/multiplatform-crypto-libsodium-bindings/src/nativeMain/kotlin/com/ionspin/kotlin/crypto/stream/Stream.kt index 64e3ea3..7516517 100644 --- a/multiplatform-crypto-libsodium-bindings/src/nativeMain/kotlin/com/ionspin/kotlin/crypto/stream/Stream.kt +++ b/multiplatform-crypto-libsodium-bindings/src/nativeMain/kotlin/com/ionspin/kotlin/crypto/stream/Stream.kt @@ -11,9 +11,9 @@ import libsodium.crypto_stream_chacha20_keygen import libsodium.crypto_stream_chacha20_xor import libsodium.crypto_stream_chacha20_xor_ic -import libsodium.crypto_stream_xchacha20_keygen -import libsodium.crypto_stream_xchacha20_xor -import libsodium.crypto_stream_xchacha20_xor_ic +//import libsodium.crypto_stream_xchacha20_keygen +//import libsodium.crypto_stream_xchacha20_xor +//import libsodium.crypto_stream_xchacha20_xor_ic actual object Stream { actual fun chacha20(clen: Int, nonce: UByteArray, key: UByteArray): UByteArray { @@ -154,70 +154,70 @@ actual object Stream { return result } - actual fun xChacha20Keygen(): UByteArray { - val result = UByteArray(crypto_stream_xchacha20_KEYBYTES) - val resultPinned = result.pin() - - crypto_stream_xchacha20_keygen(resultPinned.toPtr()) - - resultPinned.unpin() - - return result - } - - actual fun xChacha20Xor( - message: UByteArray, - nonce: UByteArray, - key: UByteArray - ): UByteArray { - val result = UByteArray(message.size) - val messagePinned = message.pin() - val resultPinned = result.pin() - val noncePinned = nonce.pin() - val keyPinned = key.pin() - - crypto_stream_xchacha20_xor( - resultPinned.toPtr(), - messagePinned.toPtr(), - message.size.convert(), - noncePinned.toPtr(), - keyPinned.toPtr() - ).ensureLibsodiumSuccess() - - messagePinned.unpin() - resultPinned.unpin() - noncePinned.unpin() - keyPinned.unpin() - - return result - } - - actual fun xChacha20XorIc( - message: UByteArray, - nonce: UByteArray, - initialCounter: ULong, - key: UByteArray - ): UByteArray { - val result = UByteArray(message.size) - val messagePinned = message.pin() - val resultPinned = result.pin() - val noncePinned = nonce.pin() - val keyPinned = key.pin() - - crypto_stream_xchacha20_xor_ic( - resultPinned.toPtr(), - messagePinned.toPtr(), - message.size.convert(), - noncePinned.toPtr(), - initialCounter.convert(), - keyPinned.toPtr() - ).ensureLibsodiumSuccess() - - messagePinned.unpin() - resultPinned.unpin() - noncePinned.unpin() - keyPinned.unpin() - - return result - } +// actual fun xChacha20Keygen(): UByteArray { +// val result = UByteArray(crypto_stream_xchacha20_KEYBYTES) +// val resultPinned = result.pin() +// +// crypto_stream_xchacha20_keygen(resultPinned.toPtr()) +// +// resultPinned.unpin() +// +// return result +// } +// +// actual fun xChacha20Xor( +// message: UByteArray, +// nonce: UByteArray, +// key: UByteArray +// ): UByteArray { +// val result = UByteArray(message.size) +// val messagePinned = message.pin() +// val resultPinned = result.pin() +// val noncePinned = nonce.pin() +// val keyPinned = key.pin() +// +// crypto_stream_xchacha20_xor( +// resultPinned.toPtr(), +// messagePinned.toPtr(), +// message.size.convert(), +// noncePinned.toPtr(), +// keyPinned.toPtr() +// ).ensureLibsodiumSuccess() +// +// messagePinned.unpin() +// resultPinned.unpin() +// noncePinned.unpin() +// keyPinned.unpin() +// +// return result +// } +// +// actual fun xChacha20XorIc( +// message: UByteArray, +// nonce: UByteArray, +// initialCounter: ULong, +// key: UByteArray +// ): UByteArray { +// val result = UByteArray(message.size) +// val messagePinned = message.pin() +// val resultPinned = result.pin() +// val noncePinned = nonce.pin() +// val keyPinned = key.pin() +// +// crypto_stream_xchacha20_xor_ic( +// resultPinned.toPtr(), +// messagePinned.toPtr(), +// message.size.convert(), +// noncePinned.toPtr(), +// initialCounter.convert(), +// keyPinned.toPtr() +// ).ensureLibsodiumSuccess() +// +// messagePinned.unpin() +// resultPinned.unpin() +// noncePinned.unpin() +// keyPinned.unpin() +// +// return result +// } }