From cd90f964ab5526584960630780ed3e276849100b Mon Sep 17 00:00:00 2001 From: Ugljesa Jovanovic Date: Tue, 18 Aug 2020 23:32:50 +0200 Subject: [PATCH] Update kotlin version, need to fix secretstream pull, at least in js if not everywhere --- buildSrc/src/main/kotlin/Deps.kt | 8 +- .../LibsodiumSecretStreamDefinitions.kt | 2 +- .../libsodium/generator/SharedCreators.kt | 4 + .../kotlin/crypto/JsSodiumInterface.kt | 2 +- .../commonMain/kotlin/debug/test/DebugTest.kt | 3 + .../crypto/secretstream/SecretStreamTest.kt | 98 ++++++++++--------- .../kotlin/crypto/JsSodiumInterface.kt | 2 +- .../com/ionspin/kotlin/crypto/JsUtil.kt | 3 + .../src/jsMain/kotlin/debug/test/DebugTest.kt | 9 +- 9 files changed, 74 insertions(+), 57 deletions(-) diff --git a/buildSrc/src/main/kotlin/Deps.kt b/buildSrc/src/main/kotlin/Deps.kt index a4617e6..8c65ccd 100644 --- a/buildSrc/src/main/kotlin/Deps.kt +++ b/buildSrc/src/main/kotlin/Deps.kt @@ -15,12 +15,12 @@ */ object Versions { - val kotlinCoroutines = "1.3.8-1.4.0-rc" - val kotlin = "1.4.0-rc" - val kotlinSerialization = "1.0-M1-1.4.0-rc" + val kotlinCoroutines = "1.3.9" + val kotlin = "1.4.0" + val kotlinSerialization = "1.0.0-RC" val atomicfu = "0.14.3-M2-2-SNAPSHOT" //NOTE: my linux arm32 and arm64 build val nodePlugin = "1.3.0" - val dokkaPlugin = "1.4.0-M3-dev-92" + val dokkaPlugin = "1.4.0-rc" val taskTreePlugin = "1.5" val kotlinBigNumVersion = "0.1.6-1.4.0-rc-SNAPSHOT" diff --git a/kotlin-multiplatform-libsodium-generator/src/main/kotlin/com/ionspin/kotlin/crypto/generator/libsodium/definitions/LibsodiumSecretStreamDefinitions.kt b/kotlin-multiplatform-libsodium-generator/src/main/kotlin/com/ionspin/kotlin/crypto/generator/libsodium/definitions/LibsodiumSecretStreamDefinitions.kt index 4801ab0..76d7037 100644 --- a/kotlin-multiplatform-libsodium-generator/src/main/kotlin/com/ionspin/kotlin/crypto/generator/libsodium/definitions/LibsodiumSecretStreamDefinitions.kt +++ b/kotlin-multiplatform-libsodium-generator/src/main/kotlin/com/ionspin/kotlin/crypto/generator/libsodium/definitions/LibsodiumSecretStreamDefinitions.kt @@ -69,7 +69,7 @@ fun ClassDefinition.defineSecretStreamFunctions() { Initialize a state and generate a random header. Both are returned inside `SecretStreamStateAndHeader` object. """.trimIndent(), returnType = CustomTypeDefinition(withPackageName("SecretStreamStateAndHeader")), - dynamicJsReturn = true, + dynamicJsReturn = false, isStateCreationFunction = true, customCodeBlockReplacesFunctionBody = listOf(jsSecretStreamInit, jvmSecretStreamInit, nativeSecretStreamInit) ) { diff --git a/kotlin-multiplatform-libsodium-generator/src/main/kotlin/com/ionspin/kotlin/crypto/generator/libsodium/generator/SharedCreators.kt b/kotlin-multiplatform-libsodium-generator/src/main/kotlin/com/ionspin/kotlin/crypto/generator/libsodium/generator/SharedCreators.kt index e35df01..0fd8c24 100644 --- a/kotlin-multiplatform-libsodium-generator/src/main/kotlin/com/ionspin/kotlin/crypto/generator/libsodium/generator/SharedCreators.kt +++ b/kotlin-multiplatform-libsodium-generator/src/main/kotlin/com/ionspin/kotlin/crypto/generator/libsodium/generator/SharedCreators.kt @@ -45,6 +45,10 @@ fun generateDataClass(fileBuilder: FileSpec.Builder, dataClassDefinition: DataCl val dataClassConstructor = FunSpec.constructorBuilder() for (parameter in dataClassDefinition.parameters) { val parameterBuilder = ParameterSpec.builder(parameter.parameterName, parameter.parameterType.typeName) + val annotationBuilder = + AnnotationSpec.builder(ClassName("kotlin.js", "JsName")) + .addMember("\"${parameter.parameterName}\"") + parameterBuilder.addAnnotation(annotationBuilder.build()) dataClassConstructor.addParameter(parameterBuilder.build()) } dataClassBuilder.primaryConstructor(dataClassConstructor.build()) diff --git a/multiplatform-crypto-delegated/src/jsMain/kotlin/com/ionspin/kotlin/crypto/JsSodiumInterface.kt b/multiplatform-crypto-delegated/src/jsMain/kotlin/com/ionspin/kotlin/crypto/JsSodiumInterface.kt index 1934eb9..6694723 100644 --- a/multiplatform-crypto-delegated/src/jsMain/kotlin/com/ionspin/kotlin/crypto/JsSodiumInterface.kt +++ b/multiplatform-crypto-delegated/src/jsMain/kotlin/com/ionspin/kotlin/crypto/JsSodiumInterface.kt @@ -50,7 +50,7 @@ interface JsSodiumInterface { //decrypt 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) : Uint8Array //util fun memzero(array: Uint8Array) diff --git a/multiplatform-crypto-libsodium-bindings/src/commonMain/kotlin/debug/test/DebugTest.kt b/multiplatform-crypto-libsodium-bindings/src/commonMain/kotlin/debug/test/DebugTest.kt index e4694f6..d1acc23 100644 --- a/multiplatform-crypto-libsodium-bindings/src/commonMain/kotlin/debug/test/DebugTest.kt +++ b/multiplatform-crypto-libsodium-bindings/src/commonMain/kotlin/debug/test/DebugTest.kt @@ -3,6 +3,7 @@ package debug.test import kotlin.Int import kotlin.UByte import kotlin.UByteArray +import kotlin.js.JsName expect class Sha256State @@ -13,7 +14,9 @@ expect class GenericHashState expect class SecretStreamState data class SecretStreamStateAndHeader( + @JsName("state") val state: SecretStreamState, + @JsName("header") val header: UByteArray ) diff --git a/multiplatform-crypto-libsodium-bindings/src/commonTest/kotlin/com/ionspin/kotlin/crypto/secretstream/SecretStreamTest.kt b/multiplatform-crypto-libsodium-bindings/src/commonTest/kotlin/com/ionspin/kotlin/crypto/secretstream/SecretStreamTest.kt index 2b1cbaf..c9e3cf4 100644 --- a/multiplatform-crypto-libsodium-bindings/src/commonTest/kotlin/com/ionspin/kotlin/crypto/secretstream/SecretStreamTest.kt +++ b/multiplatform-crypto-libsodium-bindings/src/commonTest/kotlin/com/ionspin/kotlin/crypto/secretstream/SecretStreamTest.kt @@ -1,7 +1,9 @@ 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 @@ -14,56 +16,60 @@ import kotlin.test.assertTrue */ class SecretStreamTest { @Test - fun testSecretStream() { - 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() + 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 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 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) + 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()) //TODO JS pull returns a tag and a message!!! + decrypted.hexColumsPrint() + decrypted.contentEquals(message) + } } } diff --git a/multiplatform-crypto-libsodium-bindings/src/jsMain/kotlin/com/ionspin/kotlin/crypto/JsSodiumInterface.kt b/multiplatform-crypto-libsodium-bindings/src/jsMain/kotlin/com/ionspin/kotlin/crypto/JsSodiumInterface.kt index 1934eb9..6694723 100644 --- a/multiplatform-crypto-libsodium-bindings/src/jsMain/kotlin/com/ionspin/kotlin/crypto/JsSodiumInterface.kt +++ b/multiplatform-crypto-libsodium-bindings/src/jsMain/kotlin/com/ionspin/kotlin/crypto/JsSodiumInterface.kt @@ -50,7 +50,7 @@ interface JsSodiumInterface { //decrypt 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) : Uint8Array //util fun memzero(array: Uint8Array) diff --git a/multiplatform-crypto-libsodium-bindings/src/jsMain/kotlin/com/ionspin/kotlin/crypto/JsUtil.kt b/multiplatform-crypto-libsodium-bindings/src/jsMain/kotlin/com/ionspin/kotlin/crypto/JsUtil.kt index 9cecbbd..ec40078 100644 --- a/multiplatform-crypto-libsodium-bindings/src/jsMain/kotlin/com/ionspin/kotlin/crypto/JsUtil.kt +++ b/multiplatform-crypto-libsodium-bindings/src/jsMain/kotlin/com/ionspin/kotlin/crypto/JsUtil.kt @@ -15,6 +15,9 @@ fun UByteArray.toUInt8Array() : Uint8Array { fun Uint8Array.toUByteArray() : UByteArray { + if (length.asDynamic() == undefined) { + println("Error") + } val result = UByteArray(length) for (i in 0 until length) { result[i] = get(i).toUByte() diff --git a/multiplatform-crypto-libsodium-bindings/src/jsMain/kotlin/debug/test/DebugTest.kt b/multiplatform-crypto-libsodium-bindings/src/jsMain/kotlin/debug/test/DebugTest.kt index bd65e56..cb820c3 100644 --- a/multiplatform-crypto-libsodium-bindings/src/jsMain/kotlin/debug/test/DebugTest.kt +++ b/multiplatform-crypto-libsodium-bindings/src/jsMain/kotlin/debug/test/DebugTest.kt @@ -63,23 +63,24 @@ actual class Crypto internal actual constructor() { * Initialize a state and generate a random header. Both are returned inside * `SecretStreamStateAndHeader` object. */ - actual fun crypto_secretstream_xchacha20poly1305_init_push(key: UByteArray): dynamic { + actual fun crypto_secretstream_xchacha20poly1305_init_push(key: UByteArray): + SecretStreamStateAndHeader { println("Debug crypto_secretstream_xchacha20poly1305_init_push") val stateAndHeader = getSodium().crypto_secretstream_xchacha20poly1305_init_push(key.toUInt8Array()) val state = stateAndHeader.state val header = (stateAndHeader.header as Uint8Array).toUByteArray() - return SecretStreamStateAndHeader(state, header) + 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 { + SecretStreamState { println("Debug crypto_secretstream_xchacha20poly1305_init_pull") return getSodium().crypto_secretstream_xchacha20poly1305_init_pull(header.toUInt8Array(), - key.toUInt8Array()) + key.toUInt8Array()) as SecretStreamState } /**