diff --git a/multiplatform-crypto-libsodium-bindings/src/commonMain/kotlin/com.ionspin.kotlin.crypto/secretstream/SecretStream.kt b/multiplatform-crypto-libsodium-bindings/src/commonMain/kotlin/com.ionspin.kotlin.crypto/secretstream/SecretStream.kt index 847298f..24811d4 100644 --- a/multiplatform-crypto-libsodium-bindings/src/commonMain/kotlin/com.ionspin.kotlin.crypto/secretstream/SecretStream.kt +++ b/multiplatform-crypto-libsodium-bindings/src/commonMain/kotlin/com.ionspin.kotlin.crypto/secretstream/SecretStream.kt @@ -11,11 +11,22 @@ data class SecretStreamStateAndHeader(val state: SecretStreamState, val header : data class DecryptedDataAndTag(val decryptedData : UByteArray, val tag : UByte) +val crypto_secretstream_xchacha20poly1305_TAG_MESSAGE = 0 +val crypto_secretstream_xchacha20poly1305_TAG_PUSH = 1 +val crypto_secretstream_xchacha20poly1305_TAG_REKEY = 2 +val crypto_secretstream_xchacha20poly1305_TAG_FINAL = 3 + +val crypto_secretstream_xchacha20poly1305_HEADERBYTES = 24 +val crypto_secretstream_xchacha20poly1305_KEYBYTES = 32 +val crypto_secretstream_xchacha20poly1305_ABYTES = 17 + expect object SecretStream { fun xChaCha20Poly1305InitPush(key: UByteArray) : SecretStreamStateAndHeader fun xChaCha20Poly1305Push(state : SecretStreamState, message: UByteArray, additionalData : UByteArray = ubyteArrayOf(), tag: UByte) : UByteArray fun xChaCha20Poly1305InitPull(key: UByteArray, header: UByteArray) : SecretStreamStateAndHeader fun xChaCha20Poly1305Pull(state : SecretStreamState, ciphertext: UByteArray, additionalData : UByteArray = ubyteArrayOf()) : DecryptedDataAndTag + fun xChaCha20Poly1305Keygen() : UByteArray + fun xChaCha20Poly1305Rekey(state: SecretStreamState) } diff --git a/multiplatform-crypto-libsodium-bindings/src/jsMain/kotlin/com/ionspin/kotlin/crypto/JsSodiumInterface.kt b/multiplatform-crypto-libsodium-bindings/src/jsMain/kotlin/com/ionspin/kotlin/crypto/JsSodiumInterface.kt index a811559..aa1711f 100644 --- a/multiplatform-crypto-libsodium-bindings/src/jsMain/kotlin/com/ionspin/kotlin/crypto/JsSodiumInterface.kt +++ b/multiplatform-crypto-libsodium-bindings/src/jsMain/kotlin/com/ionspin/kotlin/crypto/JsSodiumInterface.kt @@ -57,6 +57,10 @@ interface JsSodiumInterface { fun crypto_secretstream_xchacha20poly1305_init_pull(header: Uint8Array, key: Uint8Array) : dynamic fun crypto_secretstream_xchacha20poly1305_pull(state: dynamic, ciphertext: Uint8Array, additionalData: Uint8Array) : dynamic + //keygen and rekey + fun crypto_secretstream_xchacha20poly1305_keygen() : Uint8Array + fun crypto_secretstream_xchacha20poly1305_rekey(state: dynamic) + //util fun memzero(array: Uint8Array) diff --git a/multiplatform-crypto-libsodium-bindings/src/jsMain/kotlin/com/ionspin/kotlin/crypto/secretstream/SecretStream.kt b/multiplatform-crypto-libsodium-bindings/src/jsMain/kotlin/com/ionspin/kotlin/crypto/secretstream/SecretStream.kt index b1daf7e..82b0a7c 100644 --- a/multiplatform-crypto-libsodium-bindings/src/jsMain/kotlin/com/ionspin/kotlin/crypto/secretstream/SecretStream.kt +++ b/multiplatform-crypto-libsodium-bindings/src/jsMain/kotlin/com/ionspin/kotlin/crypto/secretstream/SecretStream.kt @@ -44,4 +44,12 @@ actual object SecretStream { } + actual fun xChaCha20Poly1305Keygen(): UByteArray { + return getSodium().crypto_shorthash_keygen().toUByteArray() + } + + actual fun xChaCha20Poly1305Rekey(state: SecretStreamState) { + getSodium().crypto_secretstream_xchacha20poly1305_rekey(state) + } + } diff --git a/multiplatform-crypto-libsodium-bindings/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/secretstream/SecretStream.kt b/multiplatform-crypto-libsodium-bindings/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/secretstream/SecretStream.kt index 96e1e65..ab32a6e 100644 --- a/multiplatform-crypto-libsodium-bindings/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/secretstream/SecretStream.kt +++ b/multiplatform-crypto-libsodium-bindings/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/secretstream/SecretStream.kt @@ -19,7 +19,7 @@ actual object SecretStream { additionalData: UByteArray, tag: UByte ): UByteArray { - val ciphertext = UByteArray(message.size + 17) + val ciphertext = UByteArray(message.size + crypto_secretstream_xchacha20poly1305_ABYTES) sodium.crypto_secretstream_xchacha20poly1305_push( state, ciphertext.asByteArray(), @@ -47,7 +47,7 @@ actual object SecretStream { ciphertext: UByteArray, additionalData: UByteArray ): DecryptedDataAndTag { - val result = UByteArray(ciphertext.size - 17) + val result = UByteArray(ciphertext.size - crypto_secretstream_xchacha20poly1305_ABYTES) val tagArray = UByteArray(1) { 0U } sodium.crypto_secretstream_xchacha20poly1305_pull( state, @@ -62,4 +62,14 @@ actual object SecretStream { return DecryptedDataAndTag(result, tagArray[0]) } + actual fun xChaCha20Poly1305Keygen(): UByteArray { + val generatedKey = UByteArray(crypto_aead_xchacha20poly1305_ietf_KEYBYTES) + sodium.crypto_secretstream_xchacha20poly1305_keygen(generatedKey.asByteArray()) + return generatedKey + } + + actual fun xChaCha20Poly1305Rekey(state: SecretStreamState) { + sodium.crypto_secretstream_xchacha20poly1305_rekey(state) + } + } diff --git a/multiplatform-crypto-libsodium-bindings/src/nativeMain/kotlin/com/ionspin/kotlin/crypto/secretstream/SecretStream.kt b/multiplatform-crypto-libsodium-bindings/src/nativeMain/kotlin/com/ionspin/kotlin/crypto/secretstream/SecretStream.kt index af9f2e5..529c1be 100644 --- a/multiplatform-crypto-libsodium-bindings/src/nativeMain/kotlin/com/ionspin/kotlin/crypto/secretstream/SecretStream.kt +++ b/multiplatform-crypto-libsodium-bindings/src/nativeMain/kotlin/com/ionspin/kotlin/crypto/secretstream/SecretStream.kt @@ -10,8 +10,10 @@ import libsodium.crypto_secretstream_xchacha20poly1305_ABYTES import libsodium.crypto_secretstream_xchacha20poly1305_headerbytes import libsodium.crypto_secretstream_xchacha20poly1305_init_pull import libsodium.crypto_secretstream_xchacha20poly1305_init_push +import libsodium.crypto_secretstream_xchacha20poly1305_keygen import libsodium.crypto_secretstream_xchacha20poly1305_pull import libsodium.crypto_secretstream_xchacha20poly1305_push +import libsodium.crypto_secretstream_xchacha20poly1305_rekey import platform.posix.malloc actual typealias SecretStreamState = libsodium.crypto_secretstream_xchacha20poly1305_state @@ -119,4 +121,16 @@ actual object SecretStream { return DecryptedDataAndTag(message, tag[0]) } + actual fun xChaCha20Poly1305Keygen(): UByteArray { + val generatedKey = UByteArray(crypto_secretstream_xchacha20poly1305_KEYBYTES) + val generatedKeyPinned = generatedKey.pin() + crypto_secretstream_xchacha20poly1305_keygen(generatedKeyPinned.toPtr()) + generatedKeyPinned.unpin() + return generatedKey + } + + actual fun xChaCha20Poly1305Rekey(state: SecretStreamState) { + crypto_secretstream_xchacha20poly1305_rekey(state.ptr) + } + }