Added rekey and keygen to secretstream

This commit is contained in:
Ugljesa Jovanovic 2020-08-29 17:25:59 +02:00 committed by Ugljesa Jovanovic
parent 91cd41c8c1
commit d5b1e7f7f3
No known key found for this signature in database
GPG Key ID: 178E6DFCECCB0E0F
5 changed files with 49 additions and 2 deletions

View File

@ -11,11 +11,22 @@ data class SecretStreamStateAndHeader(val state: SecretStreamState, val header :
data class DecryptedDataAndTag(val decryptedData : UByteArray, val tag : UByte) 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 { expect object SecretStream {
fun xChaCha20Poly1305InitPush(key: UByteArray) : SecretStreamStateAndHeader fun xChaCha20Poly1305InitPush(key: UByteArray) : SecretStreamStateAndHeader
fun xChaCha20Poly1305Push(state : SecretStreamState, message: UByteArray, additionalData : UByteArray = ubyteArrayOf(), tag: UByte) : UByteArray fun xChaCha20Poly1305Push(state : SecretStreamState, message: UByteArray, additionalData : UByteArray = ubyteArrayOf(), tag: UByte) : UByteArray
fun xChaCha20Poly1305InitPull(key: UByteArray, header: UByteArray) : SecretStreamStateAndHeader fun xChaCha20Poly1305InitPull(key: UByteArray, header: UByteArray) : SecretStreamStateAndHeader
fun xChaCha20Poly1305Pull(state : SecretStreamState, ciphertext: UByteArray, additionalData : UByteArray = ubyteArrayOf()) : DecryptedDataAndTag fun xChaCha20Poly1305Pull(state : SecretStreamState, ciphertext: UByteArray, additionalData : UByteArray = ubyteArrayOf()) : DecryptedDataAndTag
fun xChaCha20Poly1305Keygen() : UByteArray
fun xChaCha20Poly1305Rekey(state: SecretStreamState)
} }

View File

@ -57,6 +57,10 @@ interface JsSodiumInterface {
fun crypto_secretstream_xchacha20poly1305_init_pull(header: Uint8Array, key: Uint8Array) : dynamic fun crypto_secretstream_xchacha20poly1305_init_pull(header: Uint8Array, key: Uint8Array) : dynamic
fun crypto_secretstream_xchacha20poly1305_pull(state: dynamic, ciphertext: Uint8Array, additionalData: 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 //util
fun memzero(array: Uint8Array) fun memzero(array: Uint8Array)

View File

@ -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)
}
} }

View File

@ -19,7 +19,7 @@ actual object SecretStream {
additionalData: UByteArray, additionalData: UByteArray,
tag: UByte tag: UByte
): UByteArray { ): UByteArray {
val ciphertext = UByteArray(message.size + 17) val ciphertext = UByteArray(message.size + crypto_secretstream_xchacha20poly1305_ABYTES)
sodium.crypto_secretstream_xchacha20poly1305_push( sodium.crypto_secretstream_xchacha20poly1305_push(
state, state,
ciphertext.asByteArray(), ciphertext.asByteArray(),
@ -47,7 +47,7 @@ actual object SecretStream {
ciphertext: UByteArray, ciphertext: UByteArray,
additionalData: UByteArray additionalData: UByteArray
): DecryptedDataAndTag { ): DecryptedDataAndTag {
val result = UByteArray(ciphertext.size - 17) val result = UByteArray(ciphertext.size - crypto_secretstream_xchacha20poly1305_ABYTES)
val tagArray = UByteArray(1) { 0U } val tagArray = UByteArray(1) { 0U }
sodium.crypto_secretstream_xchacha20poly1305_pull( sodium.crypto_secretstream_xchacha20poly1305_pull(
state, state,
@ -62,4 +62,14 @@ actual object SecretStream {
return DecryptedDataAndTag(result, tagArray[0]) 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)
}
} }

View File

@ -10,8 +10,10 @@ import libsodium.crypto_secretstream_xchacha20poly1305_ABYTES
import libsodium.crypto_secretstream_xchacha20poly1305_headerbytes import libsodium.crypto_secretstream_xchacha20poly1305_headerbytes
import libsodium.crypto_secretstream_xchacha20poly1305_init_pull import libsodium.crypto_secretstream_xchacha20poly1305_init_pull
import libsodium.crypto_secretstream_xchacha20poly1305_init_push import libsodium.crypto_secretstream_xchacha20poly1305_init_push
import libsodium.crypto_secretstream_xchacha20poly1305_keygen
import libsodium.crypto_secretstream_xchacha20poly1305_pull import libsodium.crypto_secretstream_xchacha20poly1305_pull
import libsodium.crypto_secretstream_xchacha20poly1305_push import libsodium.crypto_secretstream_xchacha20poly1305_push
import libsodium.crypto_secretstream_xchacha20poly1305_rekey
import platform.posix.malloc import platform.posix.malloc
actual typealias SecretStreamState = libsodium.crypto_secretstream_xchacha20poly1305_state actual typealias SecretStreamState = libsodium.crypto_secretstream_xchacha20poly1305_state
@ -119,4 +121,16 @@ actual object SecretStream {
return DecryptedDataAndTag(message, tag[0]) 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)
}
} }