ADded helper extension toPtr instead of addressOf(0), implemented jvm and native secret stream

This commit is contained in:
Ugljesa Jovanovic authored and Ugljesa Jovanovic committed 2020-08-27 19:27:46 +02:00
1 parent 2c92a8142f
commit 9e10677165
10 files changed
+165 -28

No files matched your search

@@ -7,8 +7,10 @@ actual typealias SecretStreamState = SecretStream.State
actual object SecretStream {
actual fun xChaCha20Poly1305InitPush(key: UByteArray): SecretStreamStateAndHeader {
TODO("not implemented yet")
// sodium.crypto_secretstream_xchacha20poly1305_init_push()
val state = SecretStreamState()
val header = UByteArray(sodium.crypto_secretstream_xchacha20poly1305_headerbytes())
sodium.crypto_secretstream_xchacha20poly1305_init_push(state, header.asByteArray(), key.asByteArray())
return SecretStreamStateAndHeader(state, header)
}
actual fun xChaCha20Poly1305Push(
@@ -17,14 +19,27 @@ actual object SecretStream {
additionalData: UByteArray,
tag: UByte
): UByteArray {
TODO("not implemented yet")
val ciphertext = UByteArray(message.size)
sodium.crypto_secretstream_xchacha20poly1305_push(
state,
ciphertext.asByteArray(),
null,
message.asByteArray(),
message.size.toLong(),
additionalData.asByteArray(),
additionalData.size.toLong(),
tag.toByte()
)
return ciphertext
}
actual fun xChaCha20Poly1305InitPull(
key: UByteArray,
header: UByteArray
): SecretStreamStateAndHeader {
TODO("not implemented yet")
val state = SecretStreamState()
sodium.crypto_secretstream_xchacha20poly1305_init_pull(state, header.asByteArray(), key.asByteArray())
return SecretStreamStateAndHeader(state, header)
}
actual fun xChaCha20Poly1305Pull(
@@ -32,7 +47,19 @@ actual object SecretStream {
ciphertext: UByteArray,
additionalData: UByteArray
): DecryptedDataAndTag {
TODO("not implemented yet")
val result = UByteArray(ciphertext.size)
val tagArray = UByteArray(1) { 0U }
sodium.crypto_secretstream_xchacha20poly1305_pull(
state,
result.asByteArray(),
null,
tagArray.asByteArray(),
ciphertext.asByteArray(),
ciphertext.size.toLong(),
additionalData.asByteArray(),
additionalData.size.toLong()
)
return DecryptedDataAndTag(result, tagArray[0])
}
}
@@ -1,5 +1,6 @@
package com.ionspin.kotlin.crypto.generichash
import com.ionspin.kotlin.crypto.util.toPtr
import kotlinx.cinterop.addressOf
import kotlinx.cinterop.convert
import kotlinx.cinterop.pin
@@ -32,11 +33,11 @@ actual object GenericHash {
val pinnedKey = key?.pin()
val pinnedMessage = message.pin()
crypto_generichash(
pinnedHash.addressOf(0),
pinnedHash.toPtr(),
requestedHashLength.convert(),
pinnedMessage.addressOf(0),
pinnedMessage.toPtr(),
message.size.convert(),
pinnedKey?.addressOf(0),
pinnedKey?.toPtr(),
(key?.size ?: 0).convert()
)
pinnedHash.unpin()
@@ -54,7 +55,7 @@ actual object GenericHash {
val pinnedKey = key?.pin()
crypto_generichash_init(
statePointed.ptr,
pinnedKey?.addressOf(0),
pinnedKey?.toPtr(),
(key?.size ?: 0).convert(),
requestedHashLength.convert()
)
@@ -69,7 +70,7 @@ actual object GenericHash {
val pinnedMessage = messagePart.pin()
crypto_generichash_update(
state.internalState.ptr,
pinnedMessage.addressOf(0),
pinnedMessage.toPtr(),
messagePart.size.convert()
)
pinnedMessage.unpin()
@@ -80,7 +81,7 @@ actual object GenericHash {
val hashResultPinned = hashResult.pin()
crypto_generichash_final(
state.internalState.ptr,
hashResultPinned.addressOf(0),
hashResultPinned.toPtr(),
state.hashLength.convert()
)
hashResultPinned.unpin()
@@ -1,10 +1,39 @@
package com.ionspin.kotlin.crypto.secretstream
import com.ionspin.kotlin.crypto.util.toPtr
import kotlinx.cinterop.UByteVar
import kotlinx.cinterop.convert
import kotlinx.cinterop.pin
import kotlinx.cinterop.pointed
import kotlinx.cinterop.ptr
import kotlinx.cinterop.reinterpret
import kotlinx.cinterop.toCPointer
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_pull
import libsodium.crypto_secretstream_xchacha20poly1305_push
import platform.posix.malloc
actual typealias SecretStreamState = libsodium.crypto_secretstream_xchacha20poly1305_state
actual object SecretStream {
actual fun xChaCha20Poly1305InitPush(key: UByteArray): SecretStreamStateAndHeader {
TODO("not implemented yet")
val stateAllocated = malloc(SecretStreamState.size.convert())
val statePointed = stateAllocated!!.reinterpret<SecretStreamState>().pointed
val header = UByteArray(crypto_secretstream_xchacha20poly1305_headerbytes().convert()) { 0U }
val headerPinned = header.pin()
val keyPinned = key.pin()
crypto_secretstream_xchacha20poly1305_init_push(
statePointed.ptr,
headerPinned.toPtr(),
keyPinned.toPtr()
)
headerPinned.unpin()
keyPinned.unpin()
return SecretStreamStateAndHeader(statePointed, header)
}
actual fun xChaCha20Poly1305Push(
@@ -13,14 +42,42 @@ actual object SecretStream {
additionalData: UByteArray,
tag: UByte
): UByteArray {
TODO("not implemented yet")
val ciphertext = UByteArray(message.size)
val ciphertextPinned = ciphertext.pin()
val messagePinned = message.pin()
val additionalDataPinned = additionalData.pin()
crypto_secretstream_xchacha20poly1305_push(
state.ptr,
ciphertextPinned.toPtr(),
null,
messagePinned.toPtr(),
message.size.convert(),
additionalDataPinned.toPtr(),
additionalData.size.convert(),
tag
)
ciphertextPinned.unpin()
messagePinned.unpin()
additionalDataPinned.unpin()
return ciphertext
}
actual fun xChaCha20Poly1305InitPull(
key: UByteArray,
header: UByteArray
): SecretStreamStateAndHeader {
TODO("not implemented yet")
val stateAllocated = malloc(SecretStreamState.size.convert())
val statePointed = stateAllocated!!.reinterpret<SecretStreamState>().pointed
val keyPinned = key.pin()
val headerPinned = header.pin()
crypto_secretstream_xchacha20poly1305_init_pull(
statePointed.ptr,
headerPinned.toPtr(),
keyPinned.toPtr()
)
headerPinned.unpin()
keyPinned.unpin()
return SecretStreamStateAndHeader(statePointed, header)
}
actual fun xChaCha20Poly1305Pull(
@@ -28,7 +85,28 @@ actual object SecretStream {
ciphertext: UByteArray,
additionalData: UByteArray
): DecryptedDataAndTag {
TODO("not implemented yet")
val message = UByteArray(ciphertext.size)
val messagePinned = message.pin()
val ciphertextPinned = ciphertext.pin()
val additionalDataPinned = additionalData.pin()
val tag = UByteArray(1) { 0U }
val tagPinned = tag.pin()
crypto_secretstream_xchacha20poly1305_pull(
state.ptr,
messagePinned.toPtr(),
null,
tagPinned.toPtr(),
ciphertextPinned.toPtr(),
ciphertext.size.convert(),
additionalDataPinned.toPtr(),
additionalData.size.convert(),
)
ciphertextPinned.unpin()
messagePinned.unpin()
additionalDataPinned.unpin()
tagPinned.unpin()
return DecryptedDataAndTag(message, tag[0])
}
}
@@ -0,0 +1,13 @@
package com.ionspin.kotlin.crypto.util
import kotlinx.cinterop.CPointer
import kotlinx.cinterop.Pinned
import kotlinx.cinterop.UByteVar
import kotlinx.cinterop.addressOf
/**
* Created by Ugljesa Jovanovic
* ugljesa.jovanovic@ionspin.com
* on 27-Aug-2020
*/
fun Pinned<UByteArray>.toPtr() : CPointer<UByteVar> = addressOf(0)