From c18c0ebc6da674328b15a39d441c89dd0e35ea77 Mon Sep 17 00:00:00 2001 From: goncalo-frade-iohk <87179681+goncalo-frade-iohk@users.noreply.github.com> Date: Sun, 16 Jul 2023 15:47:26 +0100 Subject: [PATCH] feat(x25519): add x25519 implementation for ios, macos (#80) --- .../prism/apollo/utils/KMMX25519KeyPair.kt | 7 ++--- .../prism/apollo/utils/KMMX25519PrivateKey.kt | 29 +++++++++++++++++-- .../prism/apollo/utils/KMMX25519PublicKey.kt | 6 ++-- 3 files changed, 33 insertions(+), 9 deletions(-) diff --git a/base-asymmetric-encryption/src/iosMain/kotlin/io/iohk/atala/prism/apollo/utils/KMMX25519KeyPair.kt b/base-asymmetric-encryption/src/iosMain/kotlin/io/iohk/atala/prism/apollo/utils/KMMX25519KeyPair.kt index 5bb07c177..f1bfdafdb 100644 --- a/base-asymmetric-encryption/src/iosMain/kotlin/io/iohk/atala/prism/apollo/utils/KMMX25519KeyPair.kt +++ b/base-asymmetric-encryption/src/iosMain/kotlin/io/iohk/atala/prism/apollo/utils/KMMX25519KeyPair.kt @@ -4,13 +4,10 @@ actual class KMMX25519KeyPair actual constructor( actual val privateKey: KMMX25519PrivateKey, actual val publicKey: KMMX25519PublicKey ) { - init { - throw NotImplementedError("X25519 is yet to be implemented in iOS") - } - actual companion object : X25519KeyPairGeneration { override fun generateX25519KeyPair(): KMMX25519KeyPair { - throw NotImplementedError("X25519 is yet to be implemented in iOS") + val privateKey = KMMX25519PrivateKey() + return KMMX25519KeyPair(privateKey, privateKey.publicKey()) } } } diff --git a/base-asymmetric-encryption/src/iosMain/kotlin/io/iohk/atala/prism/apollo/utils/KMMX25519PrivateKey.kt b/base-asymmetric-encryption/src/iosMain/kotlin/io/iohk/atala/prism/apollo/utils/KMMX25519PrivateKey.kt index 78279ce82..54ad3ed7b 100644 --- a/base-asymmetric-encryption/src/iosMain/kotlin/io/iohk/atala/prism/apollo/utils/KMMX25519PrivateKey.kt +++ b/base-asymmetric-encryption/src/iosMain/kotlin/io/iohk/atala/prism/apollo/utils/KMMX25519PrivateKey.kt @@ -1,7 +1,32 @@ package io.iohk.atala.prism.apollo.utils +import cocoapods.IOHKCryptoKit.X25519 +import kotlinx.cinterop.ObjCObjectVar +import kotlinx.cinterop.alloc +import kotlinx.cinterop.memScoped +import kotlinx.cinterop.ptr +import kotlinx.cinterop.value +import platform.Foundation.NSError + actual class KMMX25519PrivateKey { - init { - throw NotImplementedError("X25519 is yet to be implemented in iOS") + public val raw: ByteArray + + constructor(raw: ByteArray) { + this.raw = raw + } + + constructor() { + this.raw = X25519.createPrivateKey().toByteArray() + } + + @Throws(RuntimeException::class) + public fun publicKey(): KMMX25519PublicKey { + memScoped { + val errorRef = alloc>() + val result = X25519.publicKeyWithPrivateKey(raw.toNSData(), errorRef.ptr) + errorRef.value?.let { throw RuntimeException(it.localizedDescription()) } + val publicRaw = result?.toByteArray() ?: throw RuntimeException("Null result") + return KMMX25519PublicKey(publicRaw) + } } } diff --git a/base-asymmetric-encryption/src/iosMain/kotlin/io/iohk/atala/prism/apollo/utils/KMMX25519PublicKey.kt b/base-asymmetric-encryption/src/iosMain/kotlin/io/iohk/atala/prism/apollo/utils/KMMX25519PublicKey.kt index 36bf0d6d0..628fa15b1 100644 --- a/base-asymmetric-encryption/src/iosMain/kotlin/io/iohk/atala/prism/apollo/utils/KMMX25519PublicKey.kt +++ b/base-asymmetric-encryption/src/iosMain/kotlin/io/iohk/atala/prism/apollo/utils/KMMX25519PublicKey.kt @@ -1,7 +1,9 @@ package io.iohk.atala.prism.apollo.utils actual class KMMX25519PublicKey { - init { - throw NotImplementedError("X25519 is yet to be implemented in iOS") + public val raw: ByteArray + + constructor(raw: ByteArray) { + this.raw = raw } }