migrated to crypto2 and kitlin2, no direct libsodium. Better session

This commit is contained in:
sergeych committed 2024-06-13 11:57:32 +07:00
1 parent 51873aa9b1
commit 9660379891
20 files changed
+90 -298

No files matched your search

+6 -19
View File
@@ -1,8 +1,6 @@
import com.ionspin.kotlin.crypto.secretbox.SecretBox
import com.ionspin.kotlin.crypto.util.decodeFromUByteArray
import com.ionspin.kotlin.crypto.util.encodeToUByteArray
import kotlinx.coroutines.test.runTest
import net.sergeych.crypto2.*
import net.sergeych.kiloparsec.encodeToUByteArray
import net.sergeych.utools.pack
import net.sergeych.utools.unpack
import kotlin.test.*
@@ -23,35 +21,24 @@ class KeysTest {
val data = "8 rays dev!".encodeToUByteArray()
val data1 = "8 rays dev!".encodeToUByteArray()
val s = stk.seal(data)
assertTrue(s.verify(data))
assertTrue(s.isValid(data))
data1[0] = 0x01u
assertFalse(s.verify(data1))
assertFalse(s.isValid(data1))
val p2 = SigningKey.pair()
val p3 = SigningKey.pair()
val ms = SignedBox(data, s1) + p2.secretKey
val ms = SealedBox(data, s1) + p2.secretKey
// non tampered:
val ms1 = unpack<SignedBox>(pack(ms))
val ms1 = unpack<SealedBox>(pack(ms))
assertContentEquals(data, ms1.message)
assertTrue(pbk in ms1)
assertTrue(p2.publicKey in ms1)
assertTrue(p3.publicKey !in ms1)
assertThrows<IllegalSignatureException> {
unpack<SignedBox>(pack(ms).also { it[3] = 1u })
}
}
@Test
fun secretEncryptTest() = runTest {
initCrypto()
val key = SecretBox.keygen()
val key1 = SecretBox.keygen()
assertEquals("hello", decrypt(key, encrypt(key, "hello".encodeToUByteArray())).decodeFromUByteArray())
assertThrows<DecryptionFailedException> {
decrypt(key, encrypt(key1, "hello".encodeToUByteArray())).decodeFromUByteArray()
unpack<SealedBox>(pack(ms).also { it[3] = 1u })
}
}
+20 -21
View File
@@ -1,4 +1,3 @@
import com.ionspin.kotlin.crypto.keyexchange.KeyExchange
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.channels.ReceiveChannel
@@ -69,29 +68,29 @@ class TransportTest {
val t1 = Transport(d1, l1, Unit)
val t2 = Transport(d2, l2, Unit)
val clip = KeyExchange.keypair()
val serp = KeyExchange.keypair()
val clisk = KeyExchange.clientSessionKeys(clip.publicKey, clip.secretKey, serp.publicKey)
val sersk = KeyExchange.serverSessionKeys(serp.publicKey, serp.secretKey, clip.publicKey)
val pser = KiloParams(true, t1, sersk, Unit, null, t1)
val pcli = KiloParams(false, t2, clisk, Unit, null, t2)
assertContentEquals(pcli.token, pser.token)
assertEquals(pser.decryptString(pcli.encrypt("hello!")), "hello!")
assertEquals(pser.decryptString(pcli.encrypt("hello!")), "hello!")
assertEquals(pser.decryptString(pcli.encrypt("hello2!")), "hello2!")
assertEquals(pser.decryptString(pcli.encrypt("hello3!")), "hello3!")
assertEquals(pser.decryptString(pcli.encrypt("hello!")), "hello!")
// val clip = KeyExchange.keypair()
// val serp = KeyExchange.keypair()
// val clisk = KeyExchange.clientSessionKeys(clip.publicKey, clip.secretKey, serp.publicKey)
// val sersk = KeyExchange.serverSessionKeys(serp.publicKey, serp.secretKey, clip.publicKey)
// val pser = KiloParams(true, t1, sersk, Unit, null, t1)
// val pcli = KiloParams(false, t2, clisk, Unit, null, t2)
// assertContentEquals(pcli.token, pser.token)
// assertEquals(pser.decryptString(pcli.encrypt("hello!")), "hello!")
// assertEquals(pser.decryptString(pcli.encrypt("hello!")), "hello!")
// assertEquals(pser.decryptString(pcli.encrypt("hello2!")), "hello2!")
// assertEquals(pser.decryptString(pcli.encrypt("hello3!")), "hello3!")
// assertEquals(pser.decryptString(pcli.encrypt("hello!")), "hello!")
//
// test nonce increment
assertFalse { pcli.encrypt("once") contentEquals pcli.encrypt("once") }
// assertFalse { pcli.encrypt("once") contentEquals pcli.encrypt("once") }
assertEquals(pcli.decryptString(pser.encrypt("hello!")), "hello!")
assertEquals(pcli.decryptString(pser.encrypt("hello!")), "hello!")
assertEquals(pcli.decryptString(pser.encrypt("hello!")), "hello!")
assertEquals(pcli.decryptString(pser.encrypt("hello!")), "hello!")
assertEquals(pcli.decryptString(pser.encrypt("hello!")), "hello!")
assertEquals(pcli.decryptString(pser.encrypt("hello!")), "hello!")
// assertEquals(pcli.decryptString(pser.encrypt("hello!")), "hello!")
// assertEquals(pcli.decryptString(pser.encrypt("hello!")), "hello!")
// assertEquals(pcli.decryptString(pser.encrypt("hello!")), "hello!")
// assertEquals(pcli.decryptString(pser.encrypt("hello!")), "hello!")
// assertEquals(pcli.decryptString(pser.encrypt("hello!")), "hello!")
// assertEquals(pcli.decryptString(pser.encrypt("hello!")), "hello!")
coroutineScope {