Skip to content

Commit

Permalink
feat(x25519): add x25519 implementation for ios, macos (#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
goncalo-frade-iohk authored Jul 16, 2023
1 parent a25798c commit c18c0eb
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}
}
Original file line number Diff line number Diff line change
@@ -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<ObjCObjectVar<NSError?>>()
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)
}
}
}
Original file line number Diff line number Diff line change
@@ -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
}
}

0 comments on commit c18c0eb

Please sign in to comment.