diff --git a/multiplatform-crypto-libsodium-bindings/src/wasmJsMain/kotlin/com/ionspin/kotlin/crypto/JsSodiumInterface.kt b/multiplatform-crypto-libsodium-bindings/src/wasmJsMain/kotlin/com/ionspin/kotlin/crypto/JsSodiumInterface.kt index 3c34d40..13fa4b1 100644 --- a/multiplatform-crypto-libsodium-bindings/src/wasmJsMain/kotlin/com/ionspin/kotlin/crypto/JsSodiumInterface.kt +++ b/multiplatform-crypto-libsodium-bindings/src/wasmJsMain/kotlin/com/ionspin/kotlin/crypto/JsSodiumInterface.kt @@ -48,6 +48,23 @@ external object CryptoKxServerSessionKeysResult: JsAny { val sharedTx: Uint8Array } +external object CryptoSecretboxDetachedResult: JsAny { + val cipher: Uint8Array + val mac: Uint8Array +} + +external object CryptoSecretstreamXchacha20poly1305InitPushResult: JsAny { + val state: Uint8Array + val header: Uint8Array +} + +external object CryptoSecretstreamXchacha20poly1305PullResult: JsAny { + val message: Uint8Array + val tag: UByte +} + + + @JsModule("libsodium-wrappers-sumo") external object JsSodiumInterface { @@ -87,7 +104,8 @@ external object JsSodiumInterface { @JsName("crypto_generichash_blake2b_update") fun crypto_generichash_blake2b_update(state: JsAny, inputMessage: Uint8Array) - + // TODO: строка ниже просто висела без ничего, я ее закомментила +//crypto_secretstream_xchacha20poly1305_init_push @JsName("crypto_generichash_blake2b_final") fun crypto_generichash_blake2b_final(state: JsAny, hashLength: Int) : Uint8Array @@ -130,7 +148,7 @@ external object JsSodiumInterface { //XChaCha20Poly1305 //encrypt @JsName("crypto_secretstream_xchacha20poly1305_init_push") - fun crypto_secretstream_xchacha20poly1305_init_push(key: Uint8Array) : JsAny + fun crypto_secretstream_xchacha20poly1305_init_push(key: Uint8Array) : CryptoSecretstreamXchacha20poly1305InitPushResult @JsName("crypto_secretstream_xchacha20poly1305_push") // TODO: два варианта: \/ // 1. Меняем юбайт на байт и юинт на инт \/ @@ -141,7 +159,7 @@ external object JsSodiumInterface { @JsName("crypto_secretstream_xchacha20poly1305_init_pull") fun crypto_secretstream_xchacha20poly1305_init_pull(header: Uint8Array, key: Uint8Array) : JsAny @JsName("crypto_secretstream_xchacha20poly1305_pull") - fun crypto_secretstream_xchacha20poly1305_pull(state: JsAny, ciphertext: Uint8Array, associatedData: Uint8Array) : JsAny + fun crypto_secretstream_xchacha20poly1305_pull(state: JsAny, ciphertext: Uint8Array, associatedData: Uint8Array) : CryptoSecretstreamXchacha20poly1305PullResult //keygen and rekey @JsName("crypto_secretstream_xchacha20poly1305_keygen") @@ -151,7 +169,7 @@ external object JsSodiumInterface { // ---- SecretBox ---- @JsName("crypto_secretbox_detached") - fun crypto_secretbox_detached(message: Uint8Array, nonce: Uint8Array, key: Uint8Array) : JsAny + fun crypto_secretbox_detached(message: Uint8Array, nonce: Uint8Array, key: Uint8Array) : CryptoSecretboxDetachedResult @JsName("crypto_secretbox_easy") fun crypto_secretbox_easy(message: Uint8Array, nonce: Uint8Array, key: Uint8Array) : Uint8Array @JsName("crypto_secretbox_keygen") diff --git a/multiplatform-crypto-libsodium-bindings/src/wasmJsMain/kotlin/com/ionspin/kotlin/crypto/pwhash/PasswordHash.kt b/multiplatform-crypto-libsodium-bindings/src/wasmJsMain/kotlin/com/ionspin/kotlin/crypto/pwhash/PasswordHash.kt index 82bf6c4..e678013 100644 --- a/multiplatform-crypto-libsodium-bindings/src/wasmJsMain/kotlin/com/ionspin/kotlin/crypto/pwhash/PasswordHash.kt +++ b/multiplatform-crypto-libsodium-bindings/src/wasmJsMain/kotlin/com/ionspin/kotlin/crypto/pwhash/PasswordHash.kt @@ -26,12 +26,12 @@ actual object PasswordHash { throw RuntimeException("Javascript doesnt support more than ${UInt.MAX_VALUE} for opslimit") } return getSodium().crypto_pwhash( - outputLength.toUInt(), + outputLength.toLong(), password.encodeToUByteArray().toUInt8Array(), salt.toUInt8Array(), - opsLimit.toUInt(), - memLimit.toUInt(), - algorithm.toUInt() + opsLimit.toLong(), + memLimit.toLong(), + algorithm.toLong() ).toUByteArray() } @@ -51,8 +51,8 @@ actual object PasswordHash { } return getSodium().crypto_pwhash_str( password.encodeToUByteArray().toUInt8Array(), - opslimit.toUInt(), - memlimit.toUInt() + opslimit.toLong(), + memlimit.toLong() ) } @@ -73,8 +73,8 @@ actual object PasswordHash { return if ( getSodium().crypto_pwhash_str_needs_rehash( passwordHash, - opslimit.toUInt(), - memlimit.toUInt() + opslimit.toLong(), + memlimit.toLong() ) ) { 1 diff --git a/multiplatform-crypto-libsodium-bindings/src/wasmJsMain/kotlin/com/ionspin/kotlin/crypto/secretbox/SecretBox.kt b/multiplatform-crypto-libsodium-bindings/src/wasmJsMain/kotlin/com/ionspin/kotlin/crypto/secretbox/SecretBox.kt index 4ff9188..e61d14f 100644 --- a/multiplatform-crypto-libsodium-bindings/src/wasmJsMain/kotlin/com/ionspin/kotlin/crypto/secretbox/SecretBox.kt +++ b/multiplatform-crypto-libsodium-bindings/src/wasmJsMain/kotlin/com/ionspin/kotlin/crypto/secretbox/SecretBox.kt @@ -42,8 +42,8 @@ actual object SecretBox { key.toUInt8Array() ) return SecretBoxEncryptedDataAndTag( - (result.cipher as Uint8Array).toUByteArray(), - (result.mac as Uint8Array).toUByteArray() + result.cipher.toUByteArray(), + result.mac.toUByteArray() ) } diff --git a/multiplatform-crypto-libsodium-bindings/src/wasmJsMain/kotlin/com/ionspin/kotlin/crypto/secretstream/SecretStream.kt b/multiplatform-crypto-libsodium-bindings/src/wasmJsMain/kotlin/com/ionspin/kotlin/crypto/secretstream/SecretStream.kt index 27856e8..6cb67fd 100644 --- a/multiplatform-crypto-libsodium-bindings/src/wasmJsMain/kotlin/com/ionspin/kotlin/crypto/secretstream/SecretStream.kt +++ b/multiplatform-crypto-libsodium-bindings/src/wasmJsMain/kotlin/com/ionspin/kotlin/crypto/secretstream/SecretStream.kt @@ -5,12 +5,14 @@ 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 +external object SecretStreamStateType: JsAny + +actual typealias SecretStreamState = SecretStreamStateType actual object SecretStream { actual fun xChaCha20Poly1305InitPush(key: UByteArray): SecretStreamStateAndHeader { val state = getSodium().crypto_secretstream_xchacha20poly1305_init_push(key.toUInt8Array()) - return SecretStreamStateAndHeader(state.state, (state.header as Uint8Array).toUByteArray()) + return SecretStreamStateAndHeader(state.state as SecretStreamState, state.header.toUByteArray()) } actual fun xChaCha20Poly1305Push( @@ -20,7 +22,7 @@ actual object SecretStream { tag: UByte ): UByteArray { return getSodium().crypto_secretstream_xchacha20poly1305_push( - state, message.toUInt8Array(), associatedData.toUInt8Array(), tag + state, message.toUInt8Array(), associatedData.toUInt8Array(), tag.toInt() ).toUByteArray() } @@ -29,7 +31,7 @@ actual object SecretStream { header: UByteArray ): SecretStreamStateAndHeader { val state = getSodium().crypto_secretstream_xchacha20poly1305_init_pull(header.toUInt8Array(), key.toUInt8Array()) - return SecretStreamStateAndHeader(state, header) + return SecretStreamStateAndHeader(state as SecretStreamState, header) } actual fun xChaCha20Poly1305Pull( @@ -40,10 +42,12 @@ actual object SecretStream { val dataAndTag = getSodium().crypto_secretstream_xchacha20poly1305_pull( state, ciphertext.toUInt8Array(), associatedData.toUInt8Array() ) - if (dataAndTag == false) { + // TODO: cast definitely will succeed (i hope), + // but it needs to be checked \/ i'm not sure about this move + if (dataAndTag as Boolean == false) { throw SecretStreamCorruptedOrTamperedDataException() } - return DecryptedDataAndTag((dataAndTag.message as Uint8Array).toUByteArray(), dataAndTag.tag) + return DecryptedDataAndTag(dataAndTag.message.toUByteArray(), dataAndTag.tag.toUByte()) }