Adding secretstream push definition and neccessary changes

This commit is contained in:
Ugljesa Jovanovic committed 2020-08-14 16:00:30 +02:00
1 parent efe9661c30
commit 28995c065f
9 files changed
+195 -31

No files matched your search

@@ -1,6 +1,7 @@
package debug.test
import kotlin.Int
import kotlin.UByte
import kotlin.UByteArray
expect class Sha256State
@@ -35,5 +36,19 @@ expect class Crypto internal constructor() {
fun crypto_generichash_init(key: UByteArray, outlen: Int): GenericHashState
fun crypto_secretstream_xchacha20poly1305_init_push(key: UByteArray): UByteArray
/**
* Initialize a state and generate a random header. Both are returned inside
* `SecretStreamStateAndHeader` object
*/
fun crypto_secretstream_xchacha20poly1305_init_push(key: UByteArray): SecretStreamStateAndHeader
/**
* Encrypt next block of data using the previously initialized state. Returns encrypted block.
*/
fun crypto_secretstream_xchacha20poly1305_push(
state: SecretStreamState,
m: UByteArray,
ad: UByteArray,
tag: UByte
): UByteArray
}
@@ -5,6 +5,7 @@ import ext.libsodium.com.ionspin.kotlin.crypto.toUByteArray
import ext.libsodium.com.ionspin.kotlin.crypto.toUInt8Array
import kotlin.Any
import kotlin.Int
import kotlin.UByte
import kotlin.UByteArray
import org.khronos.webgl.Uint8Array
@@ -58,6 +59,10 @@ actual class Crypto internal actual constructor() {
return getSodium().crypto_generichash_init(key.toUInt8Array(), outlen)
}
/**
* Initialize a state and generate a random header. Both are returned inside
* `SecretStreamStateAndHeader` object
*/
actual fun crypto_secretstream_xchacha20poly1305_init_push(key: UByteArray): dynamic {
println("Debug crypto_secretstream_xchacha20poly1305_init_push")
val stateAndHeader =
@@ -66,4 +71,18 @@ actual class Crypto internal actual constructor() {
val header = (stateAndHeader.header as Uint8Array).toUByteArray()
return SecretStreamStateAndHeader(state, header)
}
/**
* Encrypt next block of data using the previously initialized state. Returns encrypted block.
*/
actual fun crypto_secretstream_xchacha20poly1305_push(
state: SecretStreamState,
m: UByteArray,
ad: UByteArray,
tag: UByte
): UByteArray {
println("Debug crypto_secretstream_xchacha20poly1305_push")
return getSodium().crypto_secretstream_xchacha20poly1305_push(state, m.toUInt8Array(), ,
ad.toUInt8Array(), , tag).toUByteArray()
}
}
@@ -5,6 +5,7 @@ import com.goterl.lazycode.lazysodium.interfaces.Hash
import com.goterl.lazycode.lazysodium.interfaces.SecretStream
import kotlin.ByteArray
import kotlin.Int
import kotlin.UByte
import kotlin.UByteArray
val sodium: SodiumJava = SodiumJava()
@@ -67,7 +68,12 @@ actual class Crypto internal actual constructor() {
return state
}
actual fun crypto_secretstream_xchacha20poly1305_init_push(key: UByteArray): UByteArray {
/**
* Initialize a state and generate a random header. Both are returned inside
* `SecretStreamStateAndHeader` object
*/
actual fun crypto_secretstream_xchacha20poly1305_init_push(key: UByteArray):
SecretStreamStateAndHeader {
println("Debug crypto_secretstream_xchacha20poly1305_init_push")
val header = UByteArray(24)
val state = SecretStream.State()
@@ -75,4 +81,20 @@ actual class Crypto internal actual constructor() {
key.asByteArray())
return SecretStreamStateAndHeader(state, header)
}
/**
* Encrypt next block of data using the previously initialized state. Returns encrypted block.
*/
actual fun crypto_secretstream_xchacha20poly1305_push(
state: SecretStreamState,
m: UByteArray,
ad: UByteArray,
tag: UByte
): UByteArray {
val c = UByteArray(m.size)
println("Debug crypto_secretstream_xchacha20poly1305_push")
sodium.crypto_secretstream_xchacha20poly1305_push(state, c.asByteArray(), c.size.toLong(),
m.asByteArray(), m.size.toLong(), ad.asByteArray(), ad.size.toLong(), tag)
return c
}
}
@@ -3,6 +3,7 @@ package debug.test
import kotlin.Byte
import kotlin.ByteArray
import kotlin.Int
import kotlin.UByte
import kotlin.UByteArray
import kotlinx.cinterop.addressOf
import kotlinx.cinterop.convert
@@ -14,6 +15,7 @@ import kotlinx.cinterop.toCValues
import libsodium.crypto_generichash_blake2b_state
import libsodium.crypto_hash_sha256_state
import libsodium.crypto_hash_sha512_state
import libsodium.crypto_secretstream_xchacha20poly1305_state
import libsodium.sodium_malloc
actual typealias Sha256State = crypto_hash_sha256_state
@@ -22,7 +24,7 @@ actual typealias Sha512State = crypto_hash_sha512_state
actual typealias GenericHashState = crypto_generichash_blake2b_state
actual typealias SecretStreamState = crypto_hash_sha256_state
actual typealias SecretStreamState = crypto_secretstream_xchacha20poly1305_state
actual class Crypto internal actual constructor() {
val _emitByte: Byte = 0
@@ -92,18 +94,48 @@ actual class Crypto internal actual constructor() {
return state
}
actual fun crypto_secretstream_xchacha20poly1305_init_push(key: UByteArray): UByteArray {
/**
* Initialize a state and generate a random header. Both are returned inside
* `SecretStreamStateAndHeader` object
*/
actual fun crypto_secretstream_xchacha20poly1305_init_push(key: UByteArray):
SecretStreamStateAndHeader {
println("Debug crypto_secretstream_xchacha20poly1305_init_push")
val pinnedKey = key.pin()
val state =
libsodium.sodium_malloc(libsodium.crypto_secretstream_xchacha20poly1305_state.size.convert())!!
val state =
sodium_malloc(libsodium.crypto_secretstream_xchacha20poly1305_state.size.convert())!!
.reinterpret<libsodium.crypto_secretstream_xchacha20poly1305_state>()
.pointed
val header = UByteArray(crypto_secretstream_xchacha20poly1305_HEADERBYTES.toInt()) { 0U }
val header = UByteArray(libsodium.crypto_secretstream_xchacha20poly1305_HEADERBYTES.toInt())
{ 0U }
val pinnedHeader = header.pin()
libsodium.crypto_secretstream_xchacha20poly1305_init_push(state.ptr,
pinnedHeader.addressOf(0), key.toCValues())
pinnedHeader.addressOf(0), pinnedKey.addressOf(0))
pinnedHeader.unpin()
pinnedKey.unpin()
return SecretStreamStateAndHeader(state, header)
}
/**
* Encrypt next block of data using the previously initialized state. Returns encrypted block.
*/
actual fun crypto_secretstream_xchacha20poly1305_push(
state: SecretStreamState,
m: UByteArray,
ad: UByteArray,
tag: UByte
): UByteArray {
val c = UByteArray(m.size)
println("Debug crypto_secretstream_xchacha20poly1305_push")
val pinnedC = c.pin()
val pinnedM = m.pin()
val pinnedAd = ad.pin()
libsodium.crypto_secretstream_xchacha20poly1305_push(state.ptr, pinnedC.addressOf(0),
c.size.convert(), pinnedM.addressOf(0), m.size.convert(), pinnedAd.addressOf(0),
ad.size.convert(), tag)
pinnedC.unpin()
pinnedM.unpin()
pinnedAd.unpin()
return c
}
}