Remove xchacha20 streaming API for now, as they are not available on apple builds

This commit is contained in:
Ugljesa Jovanovic 2024-04-06 18:56:26 +02:00
parent 59c5f958ca
commit 1424db0336
No known key found for this signature in database
GPG Key ID: 5884AC34A0EC67DB
6 changed files with 177 additions and 174 deletions

View File

@ -2,6 +2,7 @@
(All dates are DD.MM.YYYY) (All dates are DD.MM.YYYY)
#### 0.9.1-SNAPSHOT - current development snapshot #### 0.9.1-SNAPSHOT - current development snapshot
- Fix #42, return values from libsodium calls are now checked
#### 0.9.0 - 23.9.2023 #### 0.9.0 - 23.9.2023
- Breaking changes: - Breaking changes:

View File

@ -23,8 +23,9 @@ expect object Stream {
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: UInt, key: UByteArray) : UByteArray fun chacha20IetfXorIc(message: UByteArray, nonce: UByteArray, initialCounter: UInt, key: UByteArray) : UByteArray
fun xChacha20Keygen() : UByteArray // Not available on apple builds
// fun xChacha20Keygen() : UByteArray
fun xChacha20Xor(message : UByteArray, nonce: UByteArray, key: UByteArray) : UByteArray //
fun xChacha20XorIc(message : UByteArray, nonce: UByteArray, initialCounter: ULong, key: UByteArray) : UByteArray // fun xChacha20Xor(message : UByteArray, nonce: UByteArray, key: UByteArray) : UByteArray
// fun xChacha20XorIc(message : UByteArray, nonce: UByteArray, initialCounter: ULong, key: UByteArray) : UByteArray
} }

View File

@ -67,25 +67,26 @@ class StreamTest {
} }
} }
@Test // Not available on apple builds
fun testXChaCha20IetfStream() = runTest { // @Test
LibsodiumInitializer.initializeWithCallback { // fun testXChaCha20IetfStream() = runTest {
val message = "This is a cha cha message".encodeToUByteArray() // LibsodiumInitializer.initializeWithCallback {
val nonce = LibsodiumRandom.bufDeterministic(crypto_stream_xchacha20_NONCEBYTES, seed) // val message = "This is a cha cha message".encodeToUByteArray()
val key = Stream.xChacha20Keygen() // val nonce = LibsodiumRandom.bufDeterministic(crypto_stream_xchacha20_NONCEBYTES, seed)
val encryptedUsingLibsodium = Stream.xChacha20Xor(message, nonce, key) // val key = Stream.xChacha20Keygen()
val encryptedUsingLibsodiumWithInitialCounter = Stream.xChacha20XorIc(message, nonce, 0U, key) // val encryptedUsingLibsodium = Stream.xChacha20Xor(message, nonce, key)
println(encryptedUsingLibsodium.toHexString()) // val encryptedUsingLibsodiumWithInitialCounter = Stream.xChacha20XorIc(message, nonce, 0U, key)
println(encryptedUsingLibsodiumWithInitialCounter.toHexString()) // println(encryptedUsingLibsodium.toHexString())
assertTrue { // println(encryptedUsingLibsodiumWithInitialCounter.toHexString())
encryptedUsingLibsodium.contentEquals(encryptedUsingLibsodiumWithInitialCounter) // assertTrue {
} // encryptedUsingLibsodium.contentEquals(encryptedUsingLibsodiumWithInitialCounter)
val decryptedUsingLibsodium = Stream.xChacha20Xor(encryptedUsingLibsodium, nonce, key) // }
println(message.toHexString()) // val decryptedUsingLibsodium = Stream.xChacha20Xor(encryptedUsingLibsodium, nonce, key)
println(decryptedUsingLibsodium.toHexString()) // println(message.toHexString())
assertTrue { // println(decryptedUsingLibsodium.toHexString())
decryptedUsingLibsodium.contentEquals(message) // assertTrue {
} // decryptedUsingLibsodium.contentEquals(message)
} // }
} // }
// }
} }

View File

@ -82,41 +82,41 @@ actual object Stream {
return result.toUByteArray() return result.toUByteArray()
} }
actual fun xChacha20Keygen(): UByteArray { // actual fun xChacha20Keygen(): UByteArray {
val result = getSodium().crypto_stream_xchacha20_keygen() // val result = getSodium().crypto_stream_xchacha20_keygen()
//
return result.toUByteArray() // return result.toUByteArray()
} // }
//
actual fun xChacha20Xor( // actual fun xChacha20Xor(
message: UByteArray, // message: UByteArray,
nonce: UByteArray, // nonce: UByteArray,
key: UByteArray // key: UByteArray
): UByteArray { // ): UByteArray {
val result = getSodium().crypto_stream_xchacha20_xor( // val result = getSodium().crypto_stream_xchacha20_xor(
message.toUInt8Array(), // message.toUInt8Array(),
nonce.toUInt8Array(), // nonce.toUInt8Array(),
key.toUInt8Array() // key.toUInt8Array()
) // )
//
return result.toUByteArray() // return result.toUByteArray()
} // }
//
actual fun xChacha20XorIc( // actual fun xChacha20XorIc(
message: UByteArray, // message: UByteArray,
nonce: UByteArray, // nonce: UByteArray,
initialCounter: ULong, // initialCounter: ULong,
key: UByteArray // key: UByteArray
): UByteArray { // ): UByteArray {
val result = getSodium().crypto_stream_xchacha20_xor_ic( // val result = getSodium().crypto_stream_xchacha20_xor_ic(
message.toUInt8Array(), // message.toUInt8Array(),
nonce.toUInt8Array(), // nonce.toUInt8Array(),
initialCounter.toUInt(), // initialCounter.toUInt(),
key.toUInt8Array() // key.toUInt8Array()
) // )
//
return result.toUByteArray() // return result.toUByteArray()
} // }
} }

View File

@ -97,49 +97,49 @@ actual object Stream {
return result return result
} }
actual fun xChacha20Keygen(): UByteArray { // actual fun xChacha20Keygen(): UByteArray {
val result = UByteArray(crypto_stream_chacha20_KEYBYTES) // val result = UByteArray(crypto_stream_chacha20_KEYBYTES)
//
sodiumJna.crypto_stream_xchacha20_keygen(result.asByteArray()) // sodiumJna.crypto_stream_xchacha20_keygen(result.asByteArray())
//
return result // return result
} // }
//
actual fun xChacha20Xor( // actual fun xChacha20Xor(
message: UByteArray, // message: UByteArray,
nonce: UByteArray, // nonce: UByteArray,
key: UByteArray // key: UByteArray
): UByteArray { // ): UByteArray {
val result = UByteArray(message.size) // val result = UByteArray(message.size)
//
sodiumJna.crypto_stream_xchacha20_xor( // sodiumJna.crypto_stream_xchacha20_xor(
result.asByteArray(), // result.asByteArray(),
message.asByteArray(), // message.asByteArray(),
message.size.toLong(), // message.size.toLong(),
nonce.asByteArray(), // nonce.asByteArray(),
key.asByteArray() // key.asByteArray()
).ensureLibsodiumSuccess() // ).ensureLibsodiumSuccess()
//
return result // return result
} // }
//
actual fun xChacha20XorIc( // actual fun xChacha20XorIc(
message: UByteArray, // message: UByteArray,
nonce: UByteArray, // nonce: UByteArray,
initialCounter: ULong, // initialCounter: ULong,
key: UByteArray // key: UByteArray
): UByteArray { // ): UByteArray {
val result = UByteArray(message.size) // val result = UByteArray(message.size)
//
sodiumJna.crypto_stream_xchacha20_xor_ic( // sodiumJna.crypto_stream_xchacha20_xor_ic(
result.asByteArray(), // result.asByteArray(),
message.asByteArray(), // message.asByteArray(),
message.size.toLong(), // message.size.toLong(),
nonce.asByteArray(), // nonce.asByteArray(),
initialCounter.toLong(), // initialCounter.toLong(),
key.asByteArray() // key.asByteArray()
).ensureLibsodiumSuccess() // ).ensureLibsodiumSuccess()
//
return result // return result
} // }
} }

View File

@ -11,9 +11,9 @@ import libsodium.crypto_stream_chacha20_keygen
import libsodium.crypto_stream_chacha20_xor import libsodium.crypto_stream_chacha20_xor
import libsodium.crypto_stream_chacha20_xor_ic import libsodium.crypto_stream_chacha20_xor_ic
import libsodium.crypto_stream_xchacha20_keygen //import libsodium.crypto_stream_xchacha20_keygen
import libsodium.crypto_stream_xchacha20_xor //import libsodium.crypto_stream_xchacha20_xor
import libsodium.crypto_stream_xchacha20_xor_ic //import libsodium.crypto_stream_xchacha20_xor_ic
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 {
@ -154,70 +154,70 @@ actual object Stream {
return result return result
} }
actual fun xChacha20Keygen(): UByteArray { // actual fun xChacha20Keygen(): UByteArray {
val result = UByteArray(crypto_stream_xchacha20_KEYBYTES) // val result = UByteArray(crypto_stream_xchacha20_KEYBYTES)
val resultPinned = result.pin() // val resultPinned = result.pin()
//
crypto_stream_xchacha20_keygen(resultPinned.toPtr()) // crypto_stream_xchacha20_keygen(resultPinned.toPtr())
//
resultPinned.unpin() // resultPinned.unpin()
//
return result // return result
} // }
//
actual fun xChacha20Xor( // actual fun xChacha20Xor(
message: UByteArray, // message: UByteArray,
nonce: UByteArray, // nonce: UByteArray,
key: UByteArray // key: UByteArray
): UByteArray { // ): UByteArray {
val result = UByteArray(message.size) // val result = UByteArray(message.size)
val messagePinned = message.pin() // val messagePinned = message.pin()
val resultPinned = result.pin() // val resultPinned = result.pin()
val noncePinned = nonce.pin() // val noncePinned = nonce.pin()
val keyPinned = key.pin() // val keyPinned = key.pin()
//
crypto_stream_xchacha20_xor( // crypto_stream_xchacha20_xor(
resultPinned.toPtr(), // resultPinned.toPtr(),
messagePinned.toPtr(), // messagePinned.toPtr(),
message.size.convert(), // message.size.convert(),
noncePinned.toPtr(), // noncePinned.toPtr(),
keyPinned.toPtr() // keyPinned.toPtr()
).ensureLibsodiumSuccess() // ).ensureLibsodiumSuccess()
//
messagePinned.unpin() // messagePinned.unpin()
resultPinned.unpin() // resultPinned.unpin()
noncePinned.unpin() // noncePinned.unpin()
keyPinned.unpin() // keyPinned.unpin()
//
return result // return result
} // }
//
actual fun xChacha20XorIc( // actual fun xChacha20XorIc(
message: UByteArray, // message: UByteArray,
nonce: UByteArray, // nonce: UByteArray,
initialCounter: ULong, // initialCounter: ULong,
key: UByteArray // key: UByteArray
): UByteArray { // ): UByteArray {
val result = UByteArray(message.size) // val result = UByteArray(message.size)
val messagePinned = message.pin() // val messagePinned = message.pin()
val resultPinned = result.pin() // val resultPinned = result.pin()
val noncePinned = nonce.pin() // val noncePinned = nonce.pin()
val keyPinned = key.pin() // val keyPinned = key.pin()
//
crypto_stream_xchacha20_xor_ic( // crypto_stream_xchacha20_xor_ic(
resultPinned.toPtr(), // resultPinned.toPtr(),
messagePinned.toPtr(), // messagePinned.toPtr(),
message.size.convert(), // message.size.convert(),
noncePinned.toPtr(), // noncePinned.toPtr(),
initialCounter.convert(), // initialCounter.convert(),
keyPinned.toPtr() // keyPinned.toPtr()
).ensureLibsodiumSuccess() // ).ensureLibsodiumSuccess()
//
messagePinned.unpin() // messagePinned.unpin()
resultPinned.unpin() // resultPinned.unpin()
noncePinned.unpin() // noncePinned.unpin()
keyPinned.unpin() // keyPinned.unpin()
//
return result // return result
} // }
} }