Adding pull secretstream definition

This commit is contained in:
Ugljesa Jovanovic authored and Ugljesa Jovanovic committed 2020-08-15 11:53:11 +02:00
1 parent 28995c065f
commit df87dae376
11 files changed
+223 -30

No files matched your search

@@ -38,10 +38,16 @@ expect class Crypto internal constructor() {
/**
* Initialize a state and generate a random header. Both are returned inside
* `SecretStreamStateAndHeader` object
* `SecretStreamStateAndHeader` object.
*/
fun crypto_secretstream_xchacha20poly1305_init_push(key: UByteArray): SecretStreamStateAndHeader
/**
* Initialize state from header and key. The state can then be used for decryption.
*/
fun crypto_secretstream_xchacha20poly1305_init_pull(header: UByteArray, key: UByteArray):
SecretStreamState
/**
* Encrypt next block of data using the previously initialized state. Returns encrypted block.
*/
@@ -51,4 +57,13 @@ expect class Crypto internal constructor() {
ad: UByteArray,
tag: UByte
): UByteArray
/**
* Decrypt next block of data using the previously initialized state. Returns decrypted block.
*/
fun crypto_secretstream_xchacha20poly1305_pull(
state: SecretStreamState,
c: UByteArray,
ad: UByteArray
): UByteArray
}
@@ -30,7 +30,7 @@ actual class Crypto internal actual constructor() {
actual fun crypto_hash_sha256_update(state: Sha256State, input: UByteArray) {
println("Debug crypto_hash_sha256_update")
getSodium().crypto_hash_sha256_update(state, input.toUInt8Array(), )
getSodium().crypto_hash_sha256_update(state, input.toUInt8Array())
}
actual fun crypto_hash_sha256_final(state: Sha256State): UByteArray {
@@ -46,7 +46,7 @@ actual class Crypto internal actual constructor() {
actual fun crypto_hash_sha512_update(state: Sha512State, input: UByteArray) {
println("Debug crypto_hash_sha512_update")
getSodium().crypto_hash_sha512_update(state, input.toUInt8Array(), )
getSodium().crypto_hash_sha512_update(state, input.toUInt8Array())
}
actual fun crypto_hash_sha512_final(state: Sha512State): UByteArray {
@@ -61,7 +61,7 @@ actual class Crypto internal actual constructor() {
/**
* Initialize a state and generate a random header. Both are returned inside
* `SecretStreamStateAndHeader` object
* `SecretStreamStateAndHeader` object.
*/
actual fun crypto_secretstream_xchacha20poly1305_init_push(key: UByteArray): dynamic {
println("Debug crypto_secretstream_xchacha20poly1305_init_push")
@@ -72,6 +72,16 @@ actual class Crypto internal actual constructor() {
return SecretStreamStateAndHeader(state, header)
}
/**
* Initialize state from header and key. The state can then be used for decryption.
*/
actual fun crypto_secretstream_xchacha20poly1305_init_pull(header: UByteArray, key: UByteArray):
dynamic {
println("Debug crypto_secretstream_xchacha20poly1305_init_pull")
return getSodium().crypto_secretstream_xchacha20poly1305_init_pull(header.toUInt8Array(),
key.toUInt8Array())
}
/**
* Encrypt next block of data using the previously initialized state. Returns encrypted block.
*/
@@ -82,7 +92,20 @@ actual class Crypto internal actual constructor() {
tag: UByte
): UByteArray {
println("Debug crypto_secretstream_xchacha20poly1305_push")
return getSodium().crypto_secretstream_xchacha20poly1305_push(state, m.toUInt8Array(), ,
ad.toUInt8Array(), , tag).toUByteArray()
return getSodium().crypto_secretstream_xchacha20poly1305_push(state, m.toUInt8Array(),
ad.toUInt8Array(), tag).toUByteArray()
}
/**
* Decrypt next block of data using the previously initialized state. Returns decrypted block.
*/
actual fun crypto_secretstream_xchacha20poly1305_pull(
state: SecretStreamState,
c: UByteArray,
ad: UByteArray
): UByteArray {
println("Debug crypto_secretstream_xchacha20poly1305_pull")
return getSodium().crypto_secretstream_xchacha20poly1305_pull(state, c.toUInt8Array(),
ad.toUInt8Array()).toUByteArray()
}
}
@@ -70,7 +70,7 @@ actual class Crypto internal actual constructor() {
/**
* Initialize a state and generate a random header. Both are returned inside
* `SecretStreamStateAndHeader` object
* `SecretStreamStateAndHeader` object.
*/
actual fun crypto_secretstream_xchacha20poly1305_init_push(key: UByteArray):
SecretStreamStateAndHeader {
@@ -82,6 +82,18 @@ actual class Crypto internal actual constructor() {
return SecretStreamStateAndHeader(state, header)
}
/**
* Initialize state from header and key. The state can then be used for decryption.
*/
actual fun crypto_secretstream_xchacha20poly1305_init_pull(header: UByteArray, key: UByteArray):
SecretStreamState {
val state = debug.test.SecretStreamState()
println("Debug crypto_secretstream_xchacha20poly1305_init_pull")
sodium.crypto_secretstream_xchacha20poly1305_init_pull(state, header.asByteArray(),
key.asByteArray())
return state
}
/**
* Encrypt next block of data using the previously initialized state. Returns encrypted block.
*/
@@ -93,8 +105,23 @@ actual class Crypto internal actual constructor() {
): 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)
sodium.crypto_secretstream_xchacha20poly1305_push(state, c.asByteArray(), null, m.asByteArray(),
m.size.toLong(), ad.asByteArray(), ad.size.toLong(), tag.toByte())
return c
}
/**
* Decrypt next block of data using the previously initialized state. Returns decrypted block.
*/
actual fun crypto_secretstream_xchacha20poly1305_pull(
state: SecretStreamState,
c: UByteArray,
ad: UByteArray
): UByteArray {
val m = UByteArray(c.size)
println("Debug crypto_secretstream_xchacha20poly1305_pull")
sodium.crypto_secretstream_xchacha20poly1305_pull(state, m.asByteArray(), null, null,
c.asByteArray(), c.size.toLong(), ad.asByteArray(), ad.size.toLong())
return m
}
}
@@ -96,7 +96,7 @@ actual class Crypto internal actual constructor() {
/**
* Initialize a state and generate a random header. Both are returned inside
* `SecretStreamStateAndHeader` object
* `SecretStreamStateAndHeader` object.
*/
actual fun crypto_secretstream_xchacha20poly1305_init_push(key: UByteArray):
SecretStreamStateAndHeader {
@@ -116,6 +116,23 @@ actual class Crypto internal actual constructor() {
return SecretStreamStateAndHeader(state, header)
}
/**
* Initialize state from header and key. The state can then be used for decryption.
*/
actual fun crypto_secretstream_xchacha20poly1305_init_pull(header: UByteArray, key: UByteArray):
SecretStreamState {
val allocated = sodium_malloc(debug.test.SecretStreamState.size.convert())!!
val state = allocated.reinterpret<debug.test.SecretStreamState>().pointed
println("Debug crypto_secretstream_xchacha20poly1305_init_pull")
val pinnedHeader = header.pin()
val pinnedKey = key.pin()
libsodium.crypto_secretstream_xchacha20poly1305_init_pull(state.ptr, pinnedHeader.addressOf(0),
pinnedKey.addressOf(0))
pinnedHeader.unpin()
pinnedKey.unpin()
return state
}
/**
* Encrypt next block of data using the previously initialized state. Returns encrypted block.
*/
@@ -130,12 +147,32 @@ actual class Crypto internal actual constructor() {
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)
libsodium.crypto_secretstream_xchacha20poly1305_push(state.ptr, pinnedC.addressOf(0), null,
pinnedM.addressOf(0), m.size.convert(), pinnedAd.addressOf(0), ad.size.convert(), tag)
pinnedC.unpin()
pinnedM.unpin()
pinnedAd.unpin()
return c
}
/**
* Decrypt next block of data using the previously initialized state. Returns decrypted block.
*/
actual fun crypto_secretstream_xchacha20poly1305_pull(
state: SecretStreamState,
c: UByteArray,
ad: UByteArray
): UByteArray {
val m = UByteArray(c.size)
println("Debug crypto_secretstream_xchacha20poly1305_pull")
val pinnedM = m.pin()
val pinnedC = c.pin()
val pinnedAd = ad.pin()
libsodium.crypto_secretstream_xchacha20poly1305_pull(state.ptr, pinnedM.addressOf(0), null,
null, pinnedC.addressOf(0), c.size.convert(), pinnedAd.addressOf(0), ad.size.convert())
pinnedM.unpin()
pinnedC.unpin()
pinnedAd.unpin()
return m
}
}