Workaround inline class not allowed in external interfaces in JS https://youtrack.jetbrains.com/issue/KT-43224 still need to verify there is no narrowing or truncation when not using unsinged inline classes

This commit is contained in:
Ugljesa Jovanovic 2022-03-25 11:51:23 +01:00
parent a77ba4cc8f
commit ce868fc598
No known key found for this signature in database
GPG Key ID: 178E6DFCECCB0E0F
7 changed files with 37 additions and 31 deletions

View File

@ -94,7 +94,7 @@ external object JsSodiumInterface {
@JsName("crypto_secretstream_xchacha20poly1305_init_push")
fun crypto_secretstream_xchacha20poly1305_init_push(key: Uint8Array) : dynamic
@JsName("crypto_secretstream_xchacha20poly1305_push")
fun crypto_secretstream_xchacha20poly1305_push(state: dynamic, message: Uint8Array, associatedData: Uint8Array, tag: UByte) : Uint8Array
fun crypto_secretstream_xchacha20poly1305_push(state: dynamic, message: Uint8Array, associatedData: Uint8Array, tag: Byte) : Uint8Array
//decrypt
@JsName("crypto_secretstream_xchacha20poly1305_init_pull")
@ -262,7 +262,7 @@ external object JsSodiumInterface {
// ---- KDF ----
@JsName("crypto_kdf_derive_from_key")
fun crypto_kdf_derive_from_key(subkey_len: UInt, subkeyId : UInt, ctx: String, key: Uint8Array) : Uint8Array
fun crypto_kdf_derive_from_key(subkey_len: Int, subkeyId : Int, ctx: String, key: Uint8Array) : Uint8Array
@JsName("crypto_kdf_keygen")
fun crypto_kdf_keygen() : Uint8Array
@ -271,11 +271,11 @@ external object JsSodiumInterface {
// ---- Password hashing ----
@JsName("crypto_pwhash")
fun crypto_pwhash(keyLength : UInt, password : Uint8Array, salt: Uint8Array, opsLimit: UInt, memLimit: UInt, algorithm: UInt) : Uint8Array
fun crypto_pwhash(keyLength : Int, password : Uint8Array, salt: Uint8Array, opsLimit: Int, memLimit: Int, algorithm: Int) : Uint8Array
@JsName("crypto_pwhash_str")
fun crypto_pwhash_str(password: Uint8Array, opsLimit: UInt, memLimit: UInt) : String
fun crypto_pwhash_str(password: Uint8Array, opsLimit: Int, memLimit: Int) : String
@JsName("crypto_pwhash_str_needs_rehash")
fun crypto_pwhash_str_needs_rehash(hashedPassword: String, opsLimit: UInt, memLimit: UInt) : Boolean
fun crypto_pwhash_str_needs_rehash(hashedPassword: String, opsLimit: Int, memLimit: Int) : Boolean
@JsName("crypto_pwhash_str_verify")
fun crypto_pwhash_str_verify(hashedPassword: String, password: Uint8Array) : Boolean
@ -310,11 +310,11 @@ external object JsSodiumInterface {
@JsName("randombytes_buf")
fun randombytes_buf(length: Int) : Uint8Array
@JsName("randombytes_buf_deterministic")
fun randombytes_buf_deterministic(length: UInt, seed : Uint8Array) : Uint8Array
fun randombytes_buf_deterministic(length: Int, seed : Uint8Array) : Uint8Array
@JsName("randombytes_random")
fun randombytes_random() : UInt
fun randombytes_random() : Int
@JsName("randombytes_uniform")
fun randombytes_uniform(upper_bound: UInt) : UInt
fun randombytes_uniform(upper_bound: Int) : Int
// ---- Utils end ----
@ -332,24 +332,24 @@ external object JsSodiumInterface {
// -- Stream ----
@JsName("crypto_stream_chacha20")
fun crypto_stream_chacha20(outLength: UInt, key: Uint8Array, nonce: Uint8Array) : Uint8Array
fun crypto_stream_chacha20(outLength: Int, key: Uint8Array, nonce: Uint8Array) : Uint8Array
@JsName("crypto_stream_chacha20_ietf_xor")
fun crypto_stream_chacha20_ietf_xor(message : Uint8Array, nonce: Uint8Array, key: Uint8Array) : Uint8Array
@JsName("crypto_stream_chacha20_ietf_xor_ic")
fun crypto_stream_chacha20_ietf_xor_ic(message : Uint8Array, nonce: Uint8Array, initialCounter: UInt, key: Uint8Array) : Uint8Array
fun crypto_stream_chacha20_ietf_xor_ic(message : Uint8Array, nonce: Uint8Array, initialCounter: Int, key: Uint8Array) : Uint8Array
@JsName("crypto_stream_chacha20_keygen")
fun crypto_stream_chacha20_keygen() : Uint8Array
@JsName("crypto_stream_chacha20_xor")
fun crypto_stream_chacha20_xor(message : Uint8Array, nonce: Uint8Array, key: Uint8Array) : Uint8Array
@JsName("crypto_stream_chacha20_xor_ic")
fun crypto_stream_chacha20_xor_ic(message : Uint8Array, nonce: Uint8Array, initialCounter: UInt, key: Uint8Array) : Uint8Array
fun crypto_stream_chacha20_xor_ic(message : Uint8Array, nonce: Uint8Array, initialCounter: Int, key: Uint8Array) : Uint8Array
@JsName("crypto_stream_xchacha20_keygen")
fun crypto_stream_xchacha20_keygen() : Uint8Array
@JsName("crypto_stream_xchacha20_xor")
fun crypto_stream_xchacha20_xor(message : Uint8Array, nonce: Uint8Array, key: Uint8Array) : Uint8Array
@JsName("crypto_stream_xchacha20_xor_ic")
fun crypto_stream_xchacha20_xor_ic(message : Uint8Array, nonce: Uint8Array, initialCounter: UInt, key: Uint8Array) : Uint8Array
fun crypto_stream_xchacha20_xor_ic(message : Uint8Array, nonce: Uint8Array, initialCounter: Int, key: Uint8Array) : Uint8Array
// ---- Stream end ----

View File

@ -1,7 +1,13 @@
package ext.libsodium.com.ionspin.kotlin.crypto
import com.ionspin.kotlin.crypto.*
import ext.libsodium.*
import com.ionspin.kotlin.crypto.getSodiumLoaded
import com.ionspin.kotlin.crypto.sodiumLoaded
import ext.libsodium._libsodiumPromise
import ext.libsodium.crypto_generichash
import ext.libsodium.crypto_hash_sha256
import ext.libsodium.crypto_hash_sha256_init
import ext.libsodium.crypto_hash_sha512
import ext.libsodium.sodium_init
import kotlin.coroutines.suspendCoroutine
/**

View File

@ -26,8 +26,8 @@ actual object Kdf {
masterKey: UByteArray
): UByteArray {
return getSodium().crypto_kdf_derive_from_key(
subkeyLength.toUInt(),
subkeyId.toUInt(),
subkeyLength,
subkeyId.toInt(),
context,
masterKey.toUInt8Array()
).toUByteArray()

View File

@ -23,12 +23,12 @@ actual object PasswordHash {
algorithm: Int
): UByteArray {
return getSodium().crypto_pwhash(
outputLength.toUInt(),
outputLength,
password.encodeToUByteArray().toUInt8Array(),
salt.toUInt8Array(),
opsLimit.toUInt(),
memLimit.toUInt(),
algorithm.toUInt()
opsLimit.toInt(), // TODO check this narrowing
memLimit,
algorithm
).toUByteArray()
}
@ -45,8 +45,8 @@ actual object PasswordHash {
actual fun str(password: String, opslimit: ULong, memlimit: Int): UByteArray {
return getSodium().crypto_pwhash_str(
password.encodeToUByteArray().toUInt8Array(),
opslimit.toUInt(),
memlimit.toUInt()
opslimit.toInt(), // TODO check this narrowing
memlimit
).encodeToUByteArray()
}
@ -64,8 +64,8 @@ actual object PasswordHash {
return if (
getSodium().crypto_pwhash_str_needs_rehash(
passwordHash.asByteArray().decodeToString(),
opslimit.toUInt(),
memlimit.toUInt()
opslimit.toInt(), // TODO check this narrowing
memlimit
)
) {
1

View File

@ -20,7 +20,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.toByte()
).toUByteArray()
}

View File

@ -7,7 +7,7 @@ import ext.libsodium.com.ionspin.kotlin.crypto.toUInt8Array
actual object Stream {
actual fun chacha20(clen: Int, nonce: UByteArray, key: UByteArray): UByteArray {
//Note, unlike the other ones, here the positions of key and nonce are reversed.
val result = getSodium().crypto_stream_chacha20(clen.toUInt(), key.toUInt8Array(), nonce.toUInt8Array())
val result = getSodium().crypto_stream_chacha20(clen, key.toUInt8Array(), nonce.toUInt8Array())
return result.toUByteArray()
}
@ -36,7 +36,7 @@ actual object Stream {
val result = getSodium().crypto_stream_chacha20_ietf_xor_ic(
message.toUInt8Array(),
nonce.toUInt8Array(),
initialCounter.toUInt(),
initialCounter.toInt(),
key.toUInt8Array()
)
@ -72,7 +72,7 @@ actual object Stream {
val result = getSodium().crypto_stream_chacha20_xor_ic(
message.toUInt8Array(),
nonce.toUInt8Array(),
initialCounter.toUInt(),
initialCounter.toInt(),
key.toUInt8Array()
)

View File

@ -26,14 +26,14 @@ actual object LibsodiumRandom {
*
*/
actual fun bufDeterministic(size: Int, seed: UByteArray): UByteArray {
return getSodium().randombytes_buf_deterministic(size.toUInt(), seed.toUInt8Array()).toUByteArray()
return getSodium().randombytes_buf_deterministic(size, seed.toUInt8Array()).toUByteArray()
}
/**
* The randombytes_random() function returns an unpredictable value between 0 and 0xffffffff (included).
*/
actual fun random(): UInt {
return getSodium().randombytes_random()
return getSodium().randombytes_random().toUInt()
}
/**
@ -42,7 +42,7 @@ actual object LibsodiumRandom {
* upper_bound is not a power of 2. Note that an upper_bound < 2 leaves only a single element to be chosen, namely 0
*/
actual fun uniform(upperBound: UInt): UInt {
return getSodium().randombytes_uniform(upperBound)
return getSodium().randombytes_uniform(upperBound.toInt()).toUInt()
}
}