Added js and jvm implementations of _kx_
This commit is contained in:
parent
bd173f422f
commit
a19602a92f
@ -1,6 +1,6 @@
|
||||
package com.ionspin.kotlin.crypto.keyexchange
|
||||
|
||||
import com.ionspin.kotlin.crypto.util.LibsodiumRandom
|
||||
import com.ionspin.kotlin.crypto.LibsodiumInitializer
|
||||
import com.ionspin.kotlin.crypto.util.toHexString
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertTrue
|
||||
@ -13,6 +13,7 @@ import kotlin.test.assertTrue
|
||||
class KeyExchangeTest {
|
||||
@Test
|
||||
fun testKeyExchange() {
|
||||
LibsodiumInitializer.initializeWithCallback {
|
||||
val keypairClient = KeyExchange.keypair()
|
||||
val keypairServer = KeyExchange.keypair()
|
||||
val clientSessionKeyPair = KeyExchange.clientSessionKeys(
|
||||
@ -36,9 +37,11 @@ class KeyExchangeTest {
|
||||
clientSessionKeyPair.receiveKey.contentEquals(serverSessionKeyPair.sendKey)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testKeyExchangeSeeded() {
|
||||
LibsodiumInitializer.initializeWithCallback {
|
||||
val seed = UByteArray(crypto_kx_SEEDBYTES) { 7U }
|
||||
val keypairClient = KeyExchange.seedKeypair(seed)
|
||||
val keypairServer = KeyExchange.seedKeypair(seed)
|
||||
@ -72,3 +75,4 @@ class KeyExchangeTest {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -216,6 +216,12 @@ interface JsSodiumInterface {
|
||||
|
||||
// ---- Utils end ----
|
||||
|
||||
// ---- Key exchange ----
|
||||
fun crypto_kx_client_session_keys(clientPublicKey: Uint8Array, clientSecretKey: Uint8Array, serverPublicKey: Uint8Array) : dynamic
|
||||
fun crypto_kx_keypair() : dynamic
|
||||
fun crypto_kx_seed_keypair(seed: Uint8Array) : dynamic
|
||||
fun crypto_kx_server_session_keys(serverPublicKey: Uint8Array, serverSecretKey: Uint8Array, clientPublicKey: Uint8Array) : dynamic
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -0,0 +1,57 @@
|
||||
package com.ionspin.kotlin.crypto.keyexchange
|
||||
|
||||
import com.ionspin.kotlin.crypto.getSodium
|
||||
import ext.libsodium.com.ionspin.kotlin.crypto.toUByteArray
|
||||
import ext.libsodium.com.ionspin.kotlin.crypto.toUInt8Array
|
||||
import org.khronos.webgl.Uint8Array
|
||||
|
||||
actual object KeyExchange {
|
||||
actual fun clientSessionKeys(clientPublicKey: UByteArray, clientSecretKey: UByteArray, serverPublicKey: UByteArray) : KeyExchangeSessionKeyPair {
|
||||
|
||||
|
||||
val result = getSodium().crypto_kx_client_session_keys(
|
||||
clientPublicKey.toUInt8Array(),
|
||||
clientSecretKey.toUInt8Array(),
|
||||
serverPublicKey.toUInt8Array()
|
||||
)
|
||||
|
||||
val receiveKey = (result.sharedRx as Uint8Array).toUByteArray()
|
||||
val sendKey = (result.sharedTx as Uint8Array).toUByteArray()
|
||||
|
||||
|
||||
|
||||
return KeyExchangeSessionKeyPair(receiveKey, sendKey)
|
||||
}
|
||||
|
||||
actual fun keypair() : KeyExchangeKeyPair {
|
||||
val result = getSodium().crypto_kx_keypair()
|
||||
|
||||
val publicKey = (result.publicKey as Uint8Array).toUByteArray()
|
||||
val secretKey = (result.privateKey as Uint8Array).toUByteArray()
|
||||
|
||||
return KeyExchangeKeyPair(publicKey, secretKey)
|
||||
}
|
||||
|
||||
actual fun seedKeypair(seed: UByteArray) : KeyExchangeKeyPair {
|
||||
val result = getSodium().crypto_kx_seed_keypair(seed.toUInt8Array())
|
||||
|
||||
val publicKey = (result.publicKey as Uint8Array).toUByteArray()
|
||||
val secretKey = (result.privateKey as Uint8Array).toUByteArray()
|
||||
|
||||
return KeyExchangeKeyPair(publicKey, secretKey)
|
||||
}
|
||||
|
||||
actual fun serverSessionKeys(serverPublicKey: UByteArray, serverSecretKey: UByteArray, clientPublicKey: UByteArray) : KeyExchangeSessionKeyPair {
|
||||
|
||||
val result = getSodium().crypto_kx_server_session_keys(
|
||||
serverPublicKey.toUInt8Array(),
|
||||
serverSecretKey.toUInt8Array(),
|
||||
clientPublicKey.toUInt8Array()
|
||||
)
|
||||
|
||||
val receiveKey = (result.sharedRx as Uint8Array).toUByteArray()
|
||||
val sendKey = (result.sharedTx as Uint8Array).toUByteArray()
|
||||
|
||||
return KeyExchangeSessionKeyPair(receiveKey, sendKey)
|
||||
}
|
||||
}
|
@ -0,0 +1,57 @@
|
||||
package com.ionspin.kotlin.crypto.keyexchange
|
||||
|
||||
import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodium
|
||||
|
||||
actual object KeyExchange {
|
||||
actual fun clientSessionKeys(clientPublicKey: UByteArray, clientSecretKey: UByteArray, serverPublicKey: UByteArray) : KeyExchangeSessionKeyPair {
|
||||
val receiveKey = UByteArray(crypto_kx_SESSIONKEYBYTES)
|
||||
val sendKey = UByteArray(crypto_kx_SESSIONKEYBYTES)
|
||||
|
||||
sodium.crypto_kx_client_session_keys(
|
||||
receiveKey.asByteArray(),
|
||||
sendKey.asByteArray(),
|
||||
clientPublicKey.asByteArray(),
|
||||
clientSecretKey.asByteArray(),
|
||||
serverPublicKey.asByteArray()
|
||||
)
|
||||
|
||||
|
||||
|
||||
return KeyExchangeSessionKeyPair(receiveKey, sendKey)
|
||||
}
|
||||
|
||||
actual fun keypair() : KeyExchangeKeyPair {
|
||||
val publicKey = UByteArray(crypto_kx_PUBLICKEYBYTES)
|
||||
val secretKey = UByteArray(crypto_kx_SECRETKEYBYTES)
|
||||
|
||||
|
||||
sodium.crypto_kx_keypair(publicKey.asByteArray(), secretKey.asByteArray())
|
||||
|
||||
|
||||
return KeyExchangeKeyPair(publicKey, secretKey)
|
||||
}
|
||||
|
||||
actual fun seedKeypair(seed: UByteArray) : KeyExchangeKeyPair {
|
||||
val publicKey = UByteArray(crypto_kx_PUBLICKEYBYTES)
|
||||
val secretKey = UByteArray(crypto_kx_SECRETKEYBYTES)
|
||||
|
||||
sodium.crypto_kx_seed_keypair(publicKey.asByteArray(), secretKey.asByteArray(), seed.asByteArray())
|
||||
|
||||
return KeyExchangeKeyPair(publicKey, secretKey)
|
||||
}
|
||||
|
||||
actual fun serverSessionKeys(serverPublicKey: UByteArray, serverSecretKey: UByteArray, clientPublicKey: UByteArray) : KeyExchangeSessionKeyPair {
|
||||
val receiveKey = UByteArray(crypto_kx_SESSIONKEYBYTES)
|
||||
val sendKey = UByteArray(crypto_kx_SESSIONKEYBYTES)
|
||||
|
||||
sodium.crypto_kx_server_session_keys(
|
||||
receiveKey.asByteArray(),
|
||||
sendKey.asByteArray(),
|
||||
serverPublicKey.asByteArray(),
|
||||
serverSecretKey.asByteArray(),
|
||||
clientPublicKey.asByteArray()
|
||||
)
|
||||
|
||||
return KeyExchangeSessionKeyPair(receiveKey, sendKey)
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user