Handle ULong -> UInt conversions, update api to reflect stream_chacha20_ietf takes UInt for initial counter

This commit is contained in:
Ugljesa Jovanovic committed 2022-03-25 12:47:22 +01:00
1 parent ce868fc598
commit cd3ec109a3
8 files changed
+27 -9

No files matched your search

@@ -21,7 +21,7 @@ expect object Stream {
fun chacha20XorIc(message : UByteArray, nonce: UByteArray, initialCounter: ULong, key: UByteArray) : UByteArray
fun chacha20IetfXor(message : UByteArray, nonce: UByteArray, key: UByteArray) : UByteArray
fun chacha20IetfXorIc(message : UByteArray, nonce: UByteArray, initialCounter: ULong, key: UByteArray) : UByteArray
fun chacha20IetfXorIc(message: UByteArray, nonce: UByteArray, initialCounter: UInt, key: UByteArray) : UByteArray
// fun xChacha20Keygen() : UByteArray
//
@@ -64,4 +64,13 @@ class PasswordHashTest {
//TODO strNeedsRehash -1 case?
}
}
@Test
fun testJsNarrowing() = runTest {
val someValue = ULong.MAX_VALUE
println(someValue)
val narrowed = someValue.toInt()
println(narrowed)
}
}
@@ -5,8 +5,6 @@ import com.ionspin.kotlin.crypto.util.LibsodiumRandom
import com.ionspin.kotlin.crypto.util.encodeToUByteArray
import com.ionspin.kotlin.crypto.util.randombytes_SEEDBYTES
import com.ionspin.kotlin.crypto.util.toHexString
import kotlin.random.Random
import kotlin.random.nextUBytes
import com.ionspin.kotlin.crypto.util.runTest
import kotlin.test.Test
import kotlin.test.assertTrue
@@ -22,6 +22,9 @@ actual object PasswordHash {
memLimit: Int,
algorithm: Int
): UByteArray {
if (opsLimit > UInt.MAX_VALUE) {
throw RuntimeException("Javascript doesnt support more than ${UInt.MAX_VALUE} for opslimit")
}
return getSodium().crypto_pwhash(
outputLength,
password.encodeToUByteArray().toUInt8Array(),
@@ -43,6 +46,9 @@ actual object PasswordHash {
* The function returns 0 on success and -1 if it didn't complete successfully.
*/
actual fun str(password: String, opslimit: ULong, memlimit: Int): UByteArray {
if (opslimit > UInt.MAX_VALUE) {
throw RuntimeException("Javascript doesnt support more than ${UInt.MAX_VALUE} for opslimit")
}
return getSodium().crypto_pwhash_str(
password.encodeToUByteArray().toUInt8Array(),
opslimit.toInt(), // TODO check this narrowing
@@ -61,6 +67,9 @@ actual object PasswordHash {
opslimit: ULong,
memlimit: Int
): Int {
if (opslimit > UInt.MAX_VALUE) {
throw RuntimeException("Javascript doesnt support more than ${UInt.MAX_VALUE} for opslimit")
}
return if (
getSodium().crypto_pwhash_str_needs_rehash(
passwordHash.asByteArray().decodeToString(),
@@ -29,10 +29,9 @@ actual object Stream {
actual fun chacha20IetfXorIc(
message: UByteArray,
nonce: UByteArray,
initialCounter: ULong,
initialCounter: UInt,
key: UByteArray
): UByteArray {
val result = getSodium().crypto_stream_chacha20_ietf_xor_ic(
message.toUInt8Array(),
nonce.toUInt8Array(),
@@ -69,6 +68,9 @@ actual object Stream {
initialCounter: ULong,
key: UByteArray
): UByteArray {
if (initialCounter > UInt.MAX_VALUE) {
throw RuntimeException("Javascript doesnt support more than ${UInt.MAX_VALUE} for initial counter")
}
val result = getSodium().crypto_stream_chacha20_xor_ic(
message.toUInt8Array(),
nonce.toUInt8Array(),
@@ -1236,7 +1236,7 @@ interface JnaLibsodiumInterface : Library {
message: ByteArray,
messageLength: Long,
nonce: ByteArray,
initialCounter : Long,
initialCounter : Int,
key: ByteArray
) : Int
@@ -32,7 +32,7 @@ actual object Stream {
actual fun chacha20IetfXorIc(
message: UByteArray,
nonce: UByteArray,
initialCounter: ULong,
initialCounter: UInt,
key: UByteArray
): UByteArray {
val result = UByteArray(message.size)
@@ -42,7 +42,7 @@ actual object Stream {
message.asByteArray(),
message.size.toLong(),
nonce.asByteArray(),
initialCounter.toLong(),
initialCounter.toInt(),
key.asByteArray()
)
@@ -60,7 +60,7 @@ actual object Stream {
actual fun chacha20IetfXorIc(
message: UByteArray,
nonce: UByteArray,
initialCounter: ULong,
initialCounter: UInt,
key: UByteArray
): UByteArray {
val result = UByteArray(message.size)