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

This commit is contained in:
Ugljesa Jovanovic 2022-03-25 12:47:22 +01:00
parent ce868fc598
commit cd3ec109a3
No known key found for this signature in database
GPG Key ID: 178E6DFCECCB0E0F
8 changed files with 27 additions and 9 deletions

View File

@ -21,7 +21,7 @@ expect object Stream {
fun chacha20XorIc(message : UByteArray, nonce: UByteArray, initialCounter: ULong, key: UByteArray) : UByteArray fun chacha20XorIc(message : UByteArray, nonce: UByteArray, initialCounter: ULong, key: UByteArray) : UByteArray
fun chacha20IetfXor(message : UByteArray, nonce: UByteArray, 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 // fun xChacha20Keygen() : UByteArray
// //

View File

@ -64,4 +64,13 @@ class PasswordHashTest {
//TODO strNeedsRehash -1 case? //TODO strNeedsRehash -1 case?
} }
} }
@Test
fun testJsNarrowing() = runTest {
val someValue = ULong.MAX_VALUE
println(someValue)
val narrowed = someValue.toInt()
println(narrowed)
}
} }

View File

@ -5,8 +5,6 @@ import com.ionspin.kotlin.crypto.util.LibsodiumRandom
import com.ionspin.kotlin.crypto.util.encodeToUByteArray import com.ionspin.kotlin.crypto.util.encodeToUByteArray
import com.ionspin.kotlin.crypto.util.randombytes_SEEDBYTES import com.ionspin.kotlin.crypto.util.randombytes_SEEDBYTES
import com.ionspin.kotlin.crypto.util.toHexString import com.ionspin.kotlin.crypto.util.toHexString
import kotlin.random.Random
import kotlin.random.nextUBytes
import com.ionspin.kotlin.crypto.util.runTest import com.ionspin.kotlin.crypto.util.runTest
import kotlin.test.Test import kotlin.test.Test
import kotlin.test.assertTrue import kotlin.test.assertTrue

View File

@ -22,6 +22,9 @@ actual object PasswordHash {
memLimit: Int, memLimit: Int,
algorithm: Int algorithm: Int
): UByteArray { ): UByteArray {
if (opsLimit > UInt.MAX_VALUE) {
throw RuntimeException("Javascript doesnt support more than ${UInt.MAX_VALUE} for opslimit")
}
return getSodium().crypto_pwhash( return getSodium().crypto_pwhash(
outputLength, outputLength,
password.encodeToUByteArray().toUInt8Array(), password.encodeToUByteArray().toUInt8Array(),
@ -43,6 +46,9 @@ actual object PasswordHash {
* The function returns 0 on success and -1 if it didn't complete successfully. * The function returns 0 on success and -1 if it didn't complete successfully.
*/ */
actual fun str(password: String, opslimit: ULong, memlimit: Int): UByteArray { 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( return getSodium().crypto_pwhash_str(
password.encodeToUByteArray().toUInt8Array(), password.encodeToUByteArray().toUInt8Array(),
opslimit.toInt(), // TODO check this narrowing opslimit.toInt(), // TODO check this narrowing
@ -61,6 +67,9 @@ actual object PasswordHash {
opslimit: ULong, opslimit: ULong,
memlimit: Int memlimit: Int
): Int { ): Int {
if (opslimit > UInt.MAX_VALUE) {
throw RuntimeException("Javascript doesnt support more than ${UInt.MAX_VALUE} for opslimit")
}
return if ( return if (
getSodium().crypto_pwhash_str_needs_rehash( getSodium().crypto_pwhash_str_needs_rehash(
passwordHash.asByteArray().decodeToString(), passwordHash.asByteArray().decodeToString(),

View File

@ -29,10 +29,9 @@ actual object Stream {
actual fun chacha20IetfXorIc( actual fun chacha20IetfXorIc(
message: UByteArray, message: UByteArray,
nonce: UByteArray, nonce: UByteArray,
initialCounter: ULong, initialCounter: UInt,
key: UByteArray key: UByteArray
): UByteArray { ): UByteArray {
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(),
@ -69,6 +68,9 @@ actual object Stream {
initialCounter: ULong, initialCounter: ULong,
key: UByteArray key: UByteArray
): 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( val result = getSodium().crypto_stream_chacha20_xor_ic(
message.toUInt8Array(), message.toUInt8Array(),
nonce.toUInt8Array(), nonce.toUInt8Array(),

View File

@ -1236,7 +1236,7 @@ interface JnaLibsodiumInterface : Library {
message: ByteArray, message: ByteArray,
messageLength: Long, messageLength: Long,
nonce: ByteArray, nonce: ByteArray,
initialCounter : Long, initialCounter : Int,
key: ByteArray key: ByteArray
) : Int ) : Int

View File

@ -32,7 +32,7 @@ actual object Stream {
actual fun chacha20IetfXorIc( actual fun chacha20IetfXorIc(
message: UByteArray, message: UByteArray,
nonce: UByteArray, nonce: UByteArray,
initialCounter: ULong, initialCounter: UInt,
key: UByteArray key: UByteArray
): UByteArray { ): UByteArray {
val result = UByteArray(message.size) val result = UByteArray(message.size)
@ -42,7 +42,7 @@ actual object Stream {
message.asByteArray(), message.asByteArray(),
message.size.toLong(), message.size.toLong(),
nonce.asByteArray(), nonce.asByteArray(),
initialCounter.toLong(), initialCounter.toInt(),
key.asByteArray() key.asByteArray()
) )

View File

@ -60,7 +60,7 @@ actual object Stream {
actual fun chacha20IetfXorIc( actual fun chacha20IetfXorIc(
message: UByteArray, message: UByteArray,
nonce: UByteArray, nonce: UByteArray,
initialCounter: ULong, initialCounter: UInt,
key: UByteArray key: UByteArray
): UByteArray { ): UByteArray {
val result = UByteArray(message.size) val result = UByteArray(message.size)