Revert "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"

Left the checks for UInt in password hash

This reverts commit ce868fc598612f759c130715bc1caa2b21546739.
This commit is contained in:
Ugljesa Jovanovic 2022-03-25 15:00:57 +01:00
parent 10f9a3226f
commit cdb6ecb8f9
No known key found for this signature in database
GPG Key ID: 178E6DFCECCB0E0F
7 changed files with 31 additions and 37 deletions

View File

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

View File

@ -1,13 +1,7 @@
package ext.libsodium.com.ionspin.kotlin.crypto package ext.libsodium.com.ionspin.kotlin.crypto
import com.ionspin.kotlin.crypto.getSodiumLoaded import com.ionspin.kotlin.crypto.*
import com.ionspin.kotlin.crypto.sodiumLoaded import ext.libsodium.*
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 import kotlin.coroutines.suspendCoroutine
/** /**

View File

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

View File

@ -26,12 +26,12 @@ actual object PasswordHash {
throw RuntimeException("Javascript doesnt support more than ${UInt.MAX_VALUE} for opslimit") throw RuntimeException("Javascript doesnt support more than ${UInt.MAX_VALUE} for opslimit")
} }
return getSodium().crypto_pwhash( return getSodium().crypto_pwhash(
outputLength, outputLength.toUInt(),
password.encodeToUByteArray().toUInt8Array(), password.encodeToUByteArray().toUInt8Array(),
salt.toUInt8Array(), salt.toUInt8Array(),
opsLimit.toInt(), opsLimit.toUInt(),
memLimit, memLimit.toUInt(),
algorithm algorithm.toUInt()
).toUByteArray() ).toUByteArray()
} }
@ -51,8 +51,8 @@ actual object PasswordHash {
} }
return getSodium().crypto_pwhash_str( return getSodium().crypto_pwhash_str(
password.encodeToUByteArray().toUInt8Array(), password.encodeToUByteArray().toUInt8Array(),
opslimit.toInt(), opslimit.toUInt(),
memlimit memlimit.toUInt()
).encodeToUByteArray() ).encodeToUByteArray()
} }
@ -73,8 +73,8 @@ actual object PasswordHash {
return if ( return if (
getSodium().crypto_pwhash_str_needs_rehash( getSodium().crypto_pwhash_str_needs_rehash(
passwordHash.asByteArray().decodeToString(), passwordHash.asByteArray().decodeToString(),
opslimit.toInt(), opslimit.toUInt(),
memlimit memlimit.toUInt()
) )
) { ) {
1 1

View File

@ -20,7 +20,7 @@ actual object SecretStream {
tag: UByte tag: UByte
): UByteArray { ): UByteArray {
return getSodium().crypto_secretstream_xchacha20poly1305_push( return getSodium().crypto_secretstream_xchacha20poly1305_push(
state, message.toUInt8Array(), associatedData.toUInt8Array(), tag.toByte() state, message.toUInt8Array(), associatedData.toUInt8Array(), tag
).toUByteArray() ).toUByteArray()
} }

View File

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

View File

@ -26,14 +26,14 @@ actual object LibsodiumRandom {
* *
*/ */
actual fun bufDeterministic(size: Int, seed: UByteArray): UByteArray { actual fun bufDeterministic(size: Int, seed: UByteArray): UByteArray {
return getSodium().randombytes_buf_deterministic(size, seed.toUInt8Array()).toUByteArray() return getSodium().randombytes_buf_deterministic(size.toUInt(), seed.toUInt8Array()).toUByteArray()
} }
/** /**
* The randombytes_random() function returns an unpredictable value between 0 and 0xffffffff (included). * The randombytes_random() function returns an unpredictable value between 0 and 0xffffffff (included).
*/ */
actual fun random(): UInt { actual fun random(): UInt {
return getSodium().randombytes_random().toUInt() return getSodium().randombytes_random()
} }
/** /**
@ -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 * 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 { actual fun uniform(upperBound: UInt): UInt {
return getSodium().randombytes_uniform(upperBound.toInt()).toUInt() return getSodium().randombytes_uniform(upperBound)
} }
} }