Update kotlin version, need to fix secretstream pull, at least in js if not everywhere

This commit is contained in:
Ugljesa Jovanovic 2020-08-18 23:32:50 +02:00 committed by Ugljesa Jovanovic
parent 0c098e57db
commit cd90f964ab
No known key found for this signature in database
GPG Key ID: 178E6DFCECCB0E0F
9 changed files with 74 additions and 57 deletions

View File

@ -15,12 +15,12 @@
*/ */
object Versions { object Versions {
val kotlinCoroutines = "1.3.8-1.4.0-rc" val kotlinCoroutines = "1.3.9"
val kotlin = "1.4.0-rc" val kotlin = "1.4.0"
val kotlinSerialization = "1.0-M1-1.4.0-rc" val kotlinSerialization = "1.0.0-RC"
val atomicfu = "0.14.3-M2-2-SNAPSHOT" //NOTE: my linux arm32 and arm64 build val atomicfu = "0.14.3-M2-2-SNAPSHOT" //NOTE: my linux arm32 and arm64 build
val nodePlugin = "1.3.0" val nodePlugin = "1.3.0"
val dokkaPlugin = "1.4.0-M3-dev-92" val dokkaPlugin = "1.4.0-rc"
val taskTreePlugin = "1.5" val taskTreePlugin = "1.5"
val kotlinBigNumVersion = "0.1.6-1.4.0-rc-SNAPSHOT" val kotlinBigNumVersion = "0.1.6-1.4.0-rc-SNAPSHOT"

View File

@ -69,7 +69,7 @@ fun ClassDefinition.defineSecretStreamFunctions() {
Initialize a state and generate a random header. Both are returned inside `SecretStreamStateAndHeader` object. Initialize a state and generate a random header. Both are returned inside `SecretStreamStateAndHeader` object.
""".trimIndent(), """.trimIndent(),
returnType = CustomTypeDefinition(withPackageName("SecretStreamStateAndHeader")), returnType = CustomTypeDefinition(withPackageName("SecretStreamStateAndHeader")),
dynamicJsReturn = true, dynamicJsReturn = false,
isStateCreationFunction = true, isStateCreationFunction = true,
customCodeBlockReplacesFunctionBody = listOf(jsSecretStreamInit, jvmSecretStreamInit, nativeSecretStreamInit) customCodeBlockReplacesFunctionBody = listOf(jsSecretStreamInit, jvmSecretStreamInit, nativeSecretStreamInit)
) { ) {

View File

@ -45,6 +45,10 @@ fun generateDataClass(fileBuilder: FileSpec.Builder, dataClassDefinition: DataCl
val dataClassConstructor = FunSpec.constructorBuilder() val dataClassConstructor = FunSpec.constructorBuilder()
for (parameter in dataClassDefinition.parameters) { for (parameter in dataClassDefinition.parameters) {
val parameterBuilder = ParameterSpec.builder(parameter.parameterName, parameter.parameterType.typeName) 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()) dataClassConstructor.addParameter(parameterBuilder.build())
} }
dataClassBuilder.primaryConstructor(dataClassConstructor.build()) dataClassBuilder.primaryConstructor(dataClassConstructor.build())

View File

@ -50,7 +50,7 @@ interface JsSodiumInterface {
//decrypt //decrypt
fun crypto_secretstream_xchacha20poly1305_init_pull(header: Uint8Array, key: Uint8Array) : dynamic 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 //util
fun memzero(array: Uint8Array) fun memzero(array: Uint8Array)

View File

@ -3,6 +3,7 @@ package debug.test
import kotlin.Int import kotlin.Int
import kotlin.UByte import kotlin.UByte
import kotlin.UByteArray import kotlin.UByteArray
import kotlin.js.JsName
expect class Sha256State expect class Sha256State
@ -13,7 +14,9 @@ expect class GenericHashState
expect class SecretStreamState expect class SecretStreamState
data class SecretStreamStateAndHeader( data class SecretStreamStateAndHeader(
@JsName("state")
val state: SecretStreamState, val state: SecretStreamState,
@JsName("header")
val header: UByteArray val header: UByteArray
) )

View File

@ -1,7 +1,9 @@
package com.ionspin.kotlin.crypto.secretstream package com.ionspin.kotlin.crypto.secretstream
import com.ionspin.kotlin.bignum.integer.util.hexColumsPrint 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.encodeToUByteArray
import com.ionspin.kotlin.crypto.util.testBlocking
import debug.test.Crypto import debug.test.Crypto
import kotlin.math.exp import kotlin.math.exp
import kotlin.test.Test import kotlin.test.Test
@ -14,56 +16,60 @@ import kotlin.test.assertTrue
*/ */
class SecretStreamTest { class SecretStreamTest {
@Test @Test
fun testSecretStream() { fun testSecretStream() = testBlocking {
assertTrue { Initializer.initializeWithCallback {
val message = ("Ladies and Gentlemen of the class of '99: If I could offer you " + assertTrue {
"only one tip for the future, sunscreen would be it.").encodeToUByteArray() 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( val additionalData = ubyteArrayOf(
0x50U, 0x51U, 0x52U, 0x53U, 0xc0U, 0xc1U, 0xc2U, 0xc3U, 0xc4U, 0xc5U, 0xc6U, 0xc7U 0x50U, 0x51U, 0x52U, 0x53U, 0xc0U, 0xc1U, 0xc2U, 0xc3U, 0xc4U, 0xc5U, 0xc6U, 0xc7U
) )
val key = ubyteArrayOf( val key = ubyteArrayOf(
0x80U, 0x81U, 0x82U, 0x83U, 0x84U, 0x85U, 0x86U, 0x87U, 0x80U, 0x81U, 0x82U, 0x83U, 0x84U, 0x85U, 0x86U, 0x87U,
0x88U, 0x89U, 0x8aU, 0x8bU, 0x8cU, 0x8dU, 0x8eU, 0x8fU, 0x88U, 0x89U, 0x8aU, 0x8bU, 0x8cU, 0x8dU, 0x8eU, 0x8fU,
0x90U, 0x91U, 0x92U, 0x93U, 0x94U, 0x95U, 0x96U, 0x97U, 0x90U, 0x91U, 0x92U, 0x93U, 0x94U, 0x95U, 0x96U, 0x97U,
0x98U, 0x99U, 0x9aU, 0x9bU, 0x9cU, 0x9dU, 0x9eU, 0x9fU, 0x98U, 0x99U, 0x9aU, 0x9bU, 0x9cU, 0x9dU, 0x9eU, 0x9fU,
) )
val nonce = ubyteArrayOf( val nonce = ubyteArrayOf(
0x40U, 0x41U, 0x42U, 0x43U, 0x44U, 0x45U, 0x46U, 0x47U, 0x40U, 0x41U, 0x42U, 0x43U, 0x44U, 0x45U, 0x46U, 0x47U,
0x48U, 0x49U, 0x4aU, 0x4bU, 0x4cU, 0x4dU, 0x4eU, 0x4fU, 0x48U, 0x49U, 0x4aU, 0x4bU, 0x4cU, 0x4dU, 0x4eU, 0x4fU,
0x50U, 0x51U, 0x52U, 0x53U, 0x54U, 0x55U, 0x56U, 0x57U, 0x50U, 0x51U, 0x52U, 0x53U, 0x54U, 0x55U, 0x56U, 0x57U,
) )
val expected = ubyteArrayOf( val expected = ubyteArrayOf(
0xbdU, 0x6dU, 0x17U, 0x9dU, 0x3eU, 0x83U, 0xd4U, 0x3bU, 0xbdU, 0x6dU, 0x17U, 0x9dU, 0x3eU, 0x83U, 0xd4U, 0x3bU,
0x95U, 0x76U, 0x57U, 0x94U, 0x93U, 0xc0U, 0xe9U, 0x39U, 0x95U, 0x76U, 0x57U, 0x94U, 0x93U, 0xc0U, 0xe9U, 0x39U,
0x57U, 0x2aU, 0x17U, 0x00U, 0x25U, 0x2bU, 0xfaU, 0xccU, 0x57U, 0x2aU, 0x17U, 0x00U, 0x25U, 0x2bU, 0xfaU, 0xccU,
0xbeU, 0xd2U, 0x90U, 0x2cU, 0x21U, 0x39U, 0x6cU, 0xbbU, 0xbeU, 0xd2U, 0x90U, 0x2cU, 0x21U, 0x39U, 0x6cU, 0xbbU,
0x73U, 0x1cU, 0x7fU, 0x1bU, 0x0bU, 0x4aU, 0xa6U, 0x44U, 0x73U, 0x1cU, 0x7fU, 0x1bU, 0x0bU, 0x4aU, 0xa6U, 0x44U,
0x0bU, 0xf3U, 0xa8U, 0x2fU, 0x4eU, 0xdaU, 0x7eU, 0x39U, 0x0bU, 0xf3U, 0xa8U, 0x2fU, 0x4e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, 0x79U, 0x47U, 0xdeU, 0xafU, 0xd8U, 0x78U, 0x0aU,
0xcfU, 0x49U 0xcfU, 0x49U
) )
message.hexColumsPrint() message.hexColumsPrint()
val crypto = Crypto() val crypto = Crypto()
val stateAndHeader = crypto.crypto_secretstream_xchacha20poly1305_init_push(key) val stateAndHeader = crypto.crypto_secretstream_xchacha20poly1305_init_push(key)
val encrypted = crypto.crypto_secretstream_xchacha20poly1305_push(stateAndHeader.state, message, ubyteArrayOf(), 0U) val encrypted =
encrypted.hexColumsPrint() crypto.crypto_secretstream_xchacha20poly1305_push(stateAndHeader.state, message, ubyteArrayOf(), 0U)
val decryptState = crypto.crypto_secretstream_xchacha20poly1305_init_pull(stateAndHeader.header, key) encrypted.hexColumsPrint()
val decrypted = crypto.crypto_secretstream_xchacha20poly1305_pull(decryptState, encrypted, ubyteArrayOf()) val decryptState = crypto.crypto_secretstream_xchacha20poly1305_init_pull(stateAndHeader.header, key)
decrypted.hexColumsPrint() val decrypted =
decrypted.contentEquals(message) crypto.crypto_secretstream_xchacha20poly1305_pull(decryptState, encrypted, ubyteArrayOf()) //TODO JS pull returns a tag and a message!!!
decrypted.hexColumsPrint()
decrypted.contentEquals(message)
}
} }
} }

View File

@ -50,7 +50,7 @@ interface JsSodiumInterface {
//decrypt //decrypt
fun crypto_secretstream_xchacha20poly1305_init_pull(header: Uint8Array, key: Uint8Array) : dynamic 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 //util
fun memzero(array: Uint8Array) fun memzero(array: Uint8Array)

View File

@ -15,6 +15,9 @@ fun UByteArray.toUInt8Array() : Uint8Array {
fun Uint8Array.toUByteArray() : UByteArray { fun Uint8Array.toUByteArray() : UByteArray {
if (length.asDynamic() == undefined) {
println("Error")
}
val result = UByteArray(length) val result = UByteArray(length)
for (i in 0 until length) { for (i in 0 until length) {
result[i] = get(i).toUByte() result[i] = get(i).toUByte()

View File

@ -63,23 +63,24 @@ actual class Crypto internal actual constructor() {
* Initialize a state and generate a random header. Both are returned inside * 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 { actual fun crypto_secretstream_xchacha20poly1305_init_push(key: UByteArray):
SecretStreamStateAndHeader {
println("Debug crypto_secretstream_xchacha20poly1305_init_push") println("Debug crypto_secretstream_xchacha20poly1305_init_push")
val stateAndHeader = val stateAndHeader =
getSodium().crypto_secretstream_xchacha20poly1305_init_push(key.toUInt8Array()) getSodium().crypto_secretstream_xchacha20poly1305_init_push(key.toUInt8Array())
val state = stateAndHeader.state val state = stateAndHeader.state
val header = (stateAndHeader.header as Uint8Array).toUByteArray() 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. * 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): actual fun crypto_secretstream_xchacha20poly1305_init_pull(header: UByteArray, key: UByteArray):
dynamic { SecretStreamState {
println("Debug crypto_secretstream_xchacha20poly1305_init_pull") println("Debug crypto_secretstream_xchacha20poly1305_init_pull")
return getSodium().crypto_secretstream_xchacha20poly1305_init_pull(header.toUInt8Array(), return getSodium().crypto_secretstream_xchacha20poly1305_init_pull(header.toUInt8Array(),
key.toUInt8Array()) key.toUInt8Array()) as SecretStreamState
} }
/** /**