Fixed native (i forgot to account for tag length. Added debug list of js functions, can be usefull when defining interface
This commit is contained in:
1 parent
9e10677165
commit
89e5ae62e4
9 files changed
+543
-97
No files matched your search
+89
-76
@@ -1,76 +1,89 @@
|
||||
//package com.ionspin.kotlin.crypto.secretstream
|
||||
//
|
||||
//import com.ionspin.kotlin.bignum.integer.util.hexColumsPrint
|
||||
//import com.ionspin.kotlin.crypto.Initializer
|
||||
//import com.ionspin.kotlin.crypto.util.encodeToUByteArray
|
||||
//import com.ionspin.kotlin.crypto.util.testBlocking
|
||||
//import debug.test.Crypto
|
||||
//import kotlin.math.exp
|
||||
//import kotlin.test.Test
|
||||
//import kotlin.test.assertTrue
|
||||
//
|
||||
///**
|
||||
// * Created by Ugljesa Jovanovic
|
||||
// * ugljesa.jovanovic@ionspin.com
|
||||
// * on 15-Aug-2020
|
||||
// */
|
||||
//class SecretStreamTest {
|
||||
// @Test
|
||||
// fun testSecretStream() = testBlocking {
|
||||
// Initializer.initializeWithCallback {
|
||||
// assertTrue {
|
||||
// val message = ("Ladies and Gentlemen of the class of '99: If I could offer you " +
|
||||
// "only one tip for the future, sunscreen would be it.").encodeToUByteArray()
|
||||
//
|
||||
// val additionalData = ubyteArrayOf(
|
||||
// 0x50U, 0x51U, 0x52U, 0x53U, 0xc0U, 0xc1U, 0xc2U, 0xc3U, 0xc4U, 0xc5U, 0xc6U, 0xc7U
|
||||
// )
|
||||
// val key = ubyteArrayOf(
|
||||
// 0x80U, 0x81U, 0x82U, 0x83U, 0x84U, 0x85U, 0x86U, 0x87U,
|
||||
// 0x88U, 0x89U, 0x8aU, 0x8bU, 0x8cU, 0x8dU, 0x8eU, 0x8fU,
|
||||
// 0x90U, 0x91U, 0x92U, 0x93U, 0x94U, 0x95U, 0x96U, 0x97U,
|
||||
// 0x98U, 0x99U, 0x9aU, 0x9bU, 0x9cU, 0x9dU, 0x9eU, 0x9fU,
|
||||
// )
|
||||
//
|
||||
// val nonce = ubyteArrayOf(
|
||||
// 0x40U, 0x41U, 0x42U, 0x43U, 0x44U, 0x45U, 0x46U, 0x47U,
|
||||
// 0x48U, 0x49U, 0x4aU, 0x4bU, 0x4cU, 0x4dU, 0x4eU, 0x4fU,
|
||||
// 0x50U, 0x51U, 0x52U, 0x53U, 0x54U, 0x55U, 0x56U, 0x57U,
|
||||
// )
|
||||
//
|
||||
// val expected = ubyteArrayOf(
|
||||
// 0xbdU, 0x6dU, 0x17U, 0x9dU, 0x3eU, 0x83U, 0xd4U, 0x3bU,
|
||||
// 0x95U, 0x76U, 0x57U, 0x94U, 0x93U, 0xc0U, 0xe9U, 0x39U,
|
||||
// 0x57U, 0x2aU, 0x17U, 0x00U, 0x25U, 0x2bU, 0xfaU, 0xccU,
|
||||
// 0xbeU, 0xd2U, 0x90U, 0x2cU, 0x21U, 0x39U, 0x6cU, 0xbbU,
|
||||
// 0x73U, 0x1cU, 0x7fU, 0x1bU, 0x0bU, 0x4aU, 0xa6U, 0x44U,
|
||||
// 0x0bU, 0xf3U, 0xa8U, 0x2fU, 0x4eU, 0xdaU, 0x7eU, 0x39U,
|
||||
// 0xaeU, 0x64U, 0xc6U, 0x70U, 0x8cU, 0x54U, 0xc2U, 0x16U,
|
||||
// 0xcbU, 0x96U, 0xb7U, 0x2eU, 0x12U, 0x13U, 0xb4U, 0x52U,
|
||||
// 0x2fU, 0x8cU, 0x9bU, 0xa4U, 0x0dU, 0xb5U, 0xd9U, 0x45U,
|
||||
// 0xb1U, 0x1bU, 0x69U, 0xb9U, 0x82U, 0xc1U, 0xbbU, 0x9eU,
|
||||
// 0x3fU, 0x3fU, 0xacU, 0x2bU, 0xc3U, 0x69U, 0x48U, 0x8fU,
|
||||
// 0x76U, 0xb2U, 0x38U, 0x35U, 0x65U, 0xd3U, 0xffU, 0xf9U,
|
||||
// 0x21U, 0xf9U, 0x66U, 0x4cU, 0x97U, 0x63U, 0x7dU, 0xa9U,
|
||||
// 0x76U, 0x88U, 0x12U, 0xf6U, 0x15U, 0xc6U, 0x8bU, 0x13U,
|
||||
// 0xb5U, 0x2eU, 0xc0U, 0x87U, 0x59U, 0x24U, 0xc1U, 0xc7U,
|
||||
// 0x98U, 0x79U, 0x47U, 0xdeU, 0xafU, 0xd8U, 0x78U, 0x0aU,
|
||||
// 0xcfU, 0x49U
|
||||
// )
|
||||
// message.hexColumsPrint()
|
||||
// val crypto = Crypto()
|
||||
// val stateAndHeader = crypto.crypto_secretstream_xchacha20poly1305_init_push(key)
|
||||
// val encrypted =
|
||||
// crypto.crypto_secretstream_xchacha20poly1305_push(stateAndHeader.state, message, ubyteArrayOf(), 0U)
|
||||
// encrypted.hexColumsPrint()
|
||||
// val decryptState = crypto.crypto_secretstream_xchacha20poly1305_init_pull(stateAndHeader.header, key)
|
||||
// val decrypted =
|
||||
// crypto.crypto_secretstream_xchacha20poly1305_pull(decryptState, encrypted, ubyteArrayOf())
|
||||
// decrypted.hexColumsPrint()
|
||||
// decrypted.contentEquals(message)
|
||||
//
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
//}
|
||||
package com.ionspin.kotlin.crypto.secretstream
|
||||
|
||||
import com.ionspin.kotlin.bignum.integer.util.hexColumsPrint
|
||||
import com.ionspin.kotlin.crypto.LibsodiumInitializer
|
||||
import com.ionspin.kotlin.crypto.util.encodeToUByteArray
|
||||
import com.ionspin.kotlin.crypto.util.testBlocking
|
||||
import kotlin.test.BeforeTest
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
/**
|
||||
* Created by Ugljesa Jovanovic
|
||||
* ugljesa.jovanovic@ionspin.com
|
||||
* on 15-Aug-2020
|
||||
*/
|
||||
class SecretStreamTest {
|
||||
|
||||
@BeforeTest
|
||||
fun initialize() = testBlocking {
|
||||
LibsodiumInitializer.initialize()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testSecretStream() = testBlocking {
|
||||
val message = ("Ladies and Gentlemen of the class of '99: If I could offer you " +
|
||||
"only one tip for the future, sunscreen would be it.").encodeToUByteArray()
|
||||
|
||||
val additionalData = ubyteArrayOf(
|
||||
0x50U, 0x51U, 0x52U, 0x53U, 0xc0U, 0xc1U, 0xc2U, 0xc3U, 0xc4U, 0xc5U, 0xc6U, 0xc7U
|
||||
)
|
||||
val key = ubyteArrayOf(
|
||||
0x80U, 0x81U, 0x82U, 0x83U, 0x84U, 0x85U, 0x86U, 0x87U,
|
||||
0x88U, 0x89U, 0x8aU, 0x8bU, 0x8cU, 0x8dU, 0x8eU, 0x8fU,
|
||||
0x90U, 0x91U, 0x92U, 0x93U, 0x94U, 0x95U, 0x96U, 0x97U,
|
||||
0x98U, 0x99U, 0x9aU, 0x9bU, 0x9cU, 0x9dU, 0x9eU, 0x9fU,
|
||||
)
|
||||
|
||||
val nonce = ubyteArrayOf(
|
||||
0x40U, 0x41U, 0x42U, 0x43U, 0x44U, 0x45U, 0x46U, 0x47U,
|
||||
0x48U, 0x49U, 0x4aU, 0x4bU, 0x4cU, 0x4dU, 0x4eU, 0x4fU,
|
||||
0x50U, 0x51U, 0x52U, 0x53U, 0x54U, 0x55U, 0x56U, 0x57U,
|
||||
)
|
||||
|
||||
val expected = ubyteArrayOf(
|
||||
0xbdU, 0x6dU, 0x17U, 0x9dU, 0x3eU, 0x83U, 0xd4U, 0x3bU,
|
||||
0x95U, 0x76U, 0x57U, 0x94U, 0x93U, 0xc0U, 0xe9U, 0x39U,
|
||||
0x57U, 0x2aU, 0x17U, 0x00U, 0x25U, 0x2bU, 0xfaU, 0xccU,
|
||||
0xbeU, 0xd2U, 0x90U, 0x2cU, 0x21U, 0x39U, 0x6cU, 0xbbU,
|
||||
0x73U, 0x1cU, 0x7fU, 0x1bU, 0x0bU, 0x4aU, 0xa6U, 0x44U,
|
||||
0x0bU, 0xf3U, 0xa8U, 0x2fU, 0x4eU, 0xdaU, 0x7eU, 0x39U,
|
||||
0xaeU, 0x64U, 0xc6U, 0x70U, 0x8cU, 0x54U, 0xc2U, 0x16U,
|
||||
0xcbU, 0x96U, 0xb7U, 0x2eU, 0x12U, 0x13U, 0xb4U, 0x52U,
|
||||
0x2fU, 0x8cU, 0x9bU, 0xa4U, 0x0dU, 0xb5U, 0xd9U, 0x45U,
|
||||
0xb1U, 0x1bU, 0x69U, 0xb9U, 0x82U, 0xc1U, 0xbbU, 0x9eU,
|
||||
0x3fU, 0x3fU, 0xacU, 0x2bU, 0xc3U, 0x69U, 0x48U, 0x8fU,
|
||||
0x76U, 0xb2U, 0x38U, 0x35U, 0x65U, 0xd3U, 0xffU, 0xf9U,
|
||||
0x21U, 0xf9U, 0x66U, 0x4cU, 0x97U, 0x63U, 0x7dU, 0xa9U,
|
||||
0x76U, 0x88U, 0x12U, 0xf6U, 0x15U, 0xc6U, 0x8bU, 0x13U,
|
||||
0xb5U, 0x2eU, 0xc0U, 0x87U, 0x59U, 0x24U, 0xc1U, 0xc7U,
|
||||
0x98U, 0x79U, 0x47U, 0xdeU, 0xafU, 0xd8U, 0x78U, 0x0aU,
|
||||
0xcfU, 0x49U
|
||||
)
|
||||
message.hexColumsPrint()
|
||||
println("---- init enc ----")
|
||||
val stateAndHeader = SecretStream.xChaCha20Poly1305InitPush(key)
|
||||
println("---- encrypt ----")
|
||||
val encrypted =
|
||||
SecretStream.xChaCha20Poly1305Push(stateAndHeader.state, message, ubyteArrayOf(), 0U)
|
||||
encrypted.hexColumsPrint()
|
||||
println("---- init dec ----")
|
||||
val decryptState = SecretStream.xChaCha20Poly1305InitPull(key, stateAndHeader.header)
|
||||
println("---- decrypt ----")
|
||||
val decrypted =
|
||||
SecretStream.xChaCha20Poly1305Pull(decryptState.state, encrypted, ubyteArrayOf())
|
||||
decrypted.decryptedData.hexColumsPrint()
|
||||
assertTrue {
|
||||
decrypted.decryptedData.contentEquals(message)
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
expect fun modifyState(state: SecretStreamState, forceNonce: UByteArray)
|
||||
|
||||
|
||||
+2
-2
@@ -45,12 +45,12 @@ interface JsSodiumInterface {
|
||||
|
||||
//XChaCha20Poly1305
|
||||
//encrypt
|
||||
fun crypto_secretstream_xchacha20poly1305_init_push(header: Uint8Array) : dynamic
|
||||
fun crypto_secretstream_xchacha20poly1305_init_push(key: Uint8Array) : dynamic
|
||||
fun crypto_secretstream_xchacha20poly1305_push(state: dynamic, message: Uint8Array, additionalData: Uint8Array, tag: UByte) : Uint8Array
|
||||
|
||||
//decrypt
|
||||
fun crypto_secretstream_xchacha20poly1305_init_pull(header: Uint8Array, key: Uint8Array) : dynamic
|
||||
fun crypto_secretstream_xchacha20poly1305_pull(state: dynamic, ciphertext: Uint8Array, additionalData: Uint8Array) : Uint8Array
|
||||
fun crypto_secretstream_xchacha20poly1305_pull(state: dynamic, ciphertext: Uint8Array, additionalData: Uint8Array) : dynamic
|
||||
|
||||
//util
|
||||
fun memzero(array: Uint8Array)
|
||||
|
||||
+17
-4
@@ -1,10 +1,16 @@
|
||||
package com.ionspin.kotlin.crypto.secretstream
|
||||
|
||||
import com.ionspin.kotlin.crypto.getSodium
|
||||
import ext.libsodium.com.ionspin.kotlin.crypto.toUByteArray
|
||||
import ext.libsodium.com.ionspin.kotlin.crypto.toUInt8Array
|
||||
import org.khronos.webgl.Uint8Array
|
||||
|
||||
actual typealias SecretStreamState = Any
|
||||
|
||||
actual object SecretStream {
|
||||
actual fun xChaCha20Poly1305InitPush(key: UByteArray): SecretStreamStateAndHeader {
|
||||
TODO("not implemented yet")
|
||||
val state = getSodium().crypto_secretstream_xchacha20poly1305_init_push(key.toUInt8Array())
|
||||
return SecretStreamStateAndHeader(state.state, (state.header as Uint8Array).toUByteArray())
|
||||
}
|
||||
|
||||
actual fun xChaCha20Poly1305Push(
|
||||
@@ -13,14 +19,17 @@ actual object SecretStream {
|
||||
additionalData: UByteArray,
|
||||
tag: UByte
|
||||
): UByteArray {
|
||||
TODO("not implemented yet")
|
||||
return getSodium().crypto_secretstream_xchacha20poly1305_push(
|
||||
state, message.toUInt8Array(), additionalData.toUInt8Array(), tag
|
||||
).toUByteArray()
|
||||
}
|
||||
|
||||
actual fun xChaCha20Poly1305InitPull(
|
||||
key: UByteArray,
|
||||
header: UByteArray
|
||||
): SecretStreamStateAndHeader {
|
||||
TODO("not implemented yet")
|
||||
val state = getSodium().crypto_secretstream_xchacha20poly1305_init_pull(header.toUInt8Array(), key.toUInt8Array())
|
||||
return SecretStreamStateAndHeader(state, header)
|
||||
}
|
||||
|
||||
actual fun xChaCha20Poly1305Pull(
|
||||
@@ -28,7 +37,11 @@ actual object SecretStream {
|
||||
ciphertext: UByteArray,
|
||||
additionalData: UByteArray
|
||||
): DecryptedDataAndTag {
|
||||
TODO("not implemented yet")
|
||||
val dataAndTag = getSodium().crypto_secretstream_xchacha20poly1305_pull(
|
||||
state, ciphertext.toUInt8Array(), additionalData.toUInt8Array()
|
||||
)
|
||||
return DecryptedDataAndTag((dataAndTag.message as Uint8Array).toUByteArray(), dataAndTag.tag)
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
package com.ionspin.kotlin.crypto.secretstream
|
||||
|
||||
actual fun modifyState(state: SecretStreamState, forceNonce: UByteArray) {
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
package com.ionspin.kotlin.crypto.secretstream
|
||||
|
||||
actual fun modifyState(state: SecretStreamState, forceNonce: UByteArray) {
|
||||
state.nonce = forceNonce.sliceArray(12 until 24).asByteArray()
|
||||
println("Nonce modified ${state.nonce}")
|
||||
}
|
||||
+24
-14
@@ -1,13 +1,12 @@
|
||||
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_ABYTES
|
||||
import libsodium.crypto_secretstream_xchacha20poly1305_headerbytes
|
||||
import libsodium.crypto_secretstream_xchacha20poly1305_init_pull
|
||||
import libsodium.crypto_secretstream_xchacha20poly1305_init_push
|
||||
@@ -42,23 +41,28 @@ actual object SecretStream {
|
||||
additionalData: UByteArray,
|
||||
tag: UByte
|
||||
): UByteArray {
|
||||
val ciphertext = UByteArray(message.size)
|
||||
val ciphertext = UByteArray(message.size + crypto_secretstream_xchacha20poly1305_ABYTES.toInt()) { 0U }
|
||||
val ciphertextPinned = ciphertext.pin()
|
||||
val messagePinned = message.pin()
|
||||
val additionalDataPinned = additionalData.pin()
|
||||
val additionalDataPinned = if (additionalData.isNotEmpty()) {
|
||||
additionalData.pin()
|
||||
} else {
|
||||
null
|
||||
}
|
||||
crypto_secretstream_xchacha20poly1305_push(
|
||||
state.ptr,
|
||||
ciphertextPinned.toPtr(),
|
||||
null,
|
||||
messagePinned.toPtr(),
|
||||
message.size.convert(),
|
||||
additionalDataPinned.toPtr(),
|
||||
additionalDataPinned?.toPtr(),
|
||||
additionalData.size.convert(),
|
||||
tag
|
||||
)
|
||||
|
||||
ciphertextPinned.unpin()
|
||||
messagePinned.unpin()
|
||||
additionalDataPinned.unpin()
|
||||
additionalDataPinned?.unpin()
|
||||
return ciphertext
|
||||
}
|
||||
|
||||
@@ -85,27 +89,33 @@ actual object SecretStream {
|
||||
ciphertext: UByteArray,
|
||||
additionalData: UByteArray
|
||||
): DecryptedDataAndTag {
|
||||
val message = UByteArray(ciphertext.size)
|
||||
val message = UByteArray(ciphertext.size - crypto_secretstream_xchacha20poly1305_ABYTES.toInt())
|
||||
val messagePinned = message.pin()
|
||||
val ciphertextPinned = ciphertext.pin()
|
||||
val additionalDataPinned = additionalData.pin()
|
||||
val additionalDataPinned = if (additionalData.isNotEmpty()) {
|
||||
additionalData.pin()
|
||||
} else {
|
||||
null
|
||||
}
|
||||
val tag = UByteArray(1) { 0U }
|
||||
val tagPinned = tag.pin()
|
||||
crypto_secretstream_xchacha20poly1305_pull(
|
||||
val validTag = crypto_secretstream_xchacha20poly1305_pull(
|
||||
state.ptr,
|
||||
messagePinned.toPtr(),
|
||||
null,
|
||||
tagPinned.toPtr(),
|
||||
ciphertextPinned.toPtr(),
|
||||
ciphertext.size.convert(),
|
||||
additionalDataPinned.toPtr(),
|
||||
additionalData.size.convert(),
|
||||
|
||||
)
|
||||
additionalDataPinned?.toPtr(),
|
||||
additionalData.size.convert()
|
||||
)
|
||||
ciphertextPinned.unpin()
|
||||
messagePinned.unpin()
|
||||
additionalDataPinned.unpin()
|
||||
additionalDataPinned?.unpin()
|
||||
tagPinned.unpin()
|
||||
if (validTag != 0) {
|
||||
throw RuntimeException("Invalid tag")
|
||||
}
|
||||
return DecryptedDataAndTag(message, tag[0])
|
||||
}
|
||||
|
||||
|
||||
+2
-1
@@ -4,10 +4,11 @@ import kotlinx.cinterop.CPointer
|
||||
import kotlinx.cinterop.Pinned
|
||||
import kotlinx.cinterop.UByteVar
|
||||
import kotlinx.cinterop.addressOf
|
||||
import kotlinx.cinterop.toCPointer
|
||||
|
||||
/**
|
||||
* Created by Ugljesa Jovanovic
|
||||
* ugljesa.jovanovic@ionspin.com
|
||||
* on 27-Aug-2020
|
||||
*/
|
||||
fun Pinned<UByteArray>.toPtr() : CPointer<UByteVar> = addressOf(0)
|
||||
fun Pinned<UByteArray>.toPtr() : CPointer<UByteVar>? = addressOf(0)
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
package com.ionspin.kotlin.crypto.secretstream
|
||||
|
||||
actual fun modifyState(state: SecretStreamState, forceNonce: UByteArray) {
|
||||
}
|
||||
Reference in new issue
Block a user