Added rekey and keygen to secretstream
This commit is contained in:
parent
91cd41c8c1
commit
d5b1e7f7f3
@ -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)
|
||||
|
||||
}
|
||||
|
@ -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)
|
||||
|
||||
|
@ -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)
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user