From be0aa7e809aed587dd257433e1a6aaf1952d2c35 Mon Sep 17 00:00:00 2001 From: Cristian G <113917899+cristianIOHK@users.noreply.github.com> Date: Tue, 25 Apr 2023 10:16:29 -0400 Subject: [PATCH] feat: Implement X25519 - Keypair generation (#53) * feat: Implement X25519 - Keypair generation * Fix linting issues --- .../atala/prism/walletsdk/apollo/X25519.kt | 20 ++++++++++--------- .../prism/walletsdk/apollo/ApolloTests.kt | 9 +++++++++ .../atala/prism/walletsdk/apollo/X25519.kt | 20 ++++++++++--------- 3 files changed, 31 insertions(+), 18 deletions(-) diff --git a/atala-prism-sdk/src/androidMain/kotlin/io/iohk/atala/prism/walletsdk/apollo/X25519.kt b/atala-prism-sdk/src/androidMain/kotlin/io/iohk/atala/prism/walletsdk/apollo/X25519.kt index 571de8ba8..a8424195d 100644 --- a/atala-prism-sdk/src/androidMain/kotlin/io/iohk/atala/prism/walletsdk/apollo/X25519.kt +++ b/atala-prism-sdk/src/androidMain/kotlin/io/iohk/atala/prism/walletsdk/apollo/X25519.kt @@ -5,28 +5,30 @@ import io.iohk.atala.prism.walletsdk.domain.models.KeyCurve import io.iohk.atala.prism.walletsdk.domain.models.KeyPair import io.iohk.atala.prism.walletsdk.domain.models.PrivateKey import io.iohk.atala.prism.walletsdk.domain.models.PublicKey -import org.bouncycastle.jcajce.spec.XDHParameterSpec -import org.bouncycastle.jce.provider.BouncyCastleProvider -import java.security.KeyPairGenerator +import org.bouncycastle.crypto.generators.X25519KeyPairGenerator +import org.bouncycastle.crypto.params.X25519KeyGenerationParameters +import org.bouncycastle.crypto.params.X25519PrivateKeyParameters +import org.bouncycastle.crypto.params.X25519PublicKeyParameters +import java.security.SecureRandom /** * X25519 */ actual object X25519 { actual fun createKeyPair(): KeyPair { - val provider = BouncyCastleProvider() - val kpg = KeyPairGenerator.getInstance("X25519", provider) - kpg.initialize(XDHParameterSpec(XDHParameterSpec.X25519)) - val javaKeyPair = kpg.generateKeyPair() + val generator = X25519KeyPairGenerator() + generator.init(X25519KeyGenerationParameters(SecureRandom())) + val keyPair = generator.generateKeyPair() + return KeyPair( KeyCurve(Curve.X25519), PrivateKey( KeyCurve(Curve.X25519), - javaKeyPair.private.encoded + (keyPair.private as X25519PrivateKeyParameters).encoded ), PublicKey( KeyCurve(Curve.X25519), - javaKeyPair.public.encoded + (keyPair.public as X25519PublicKeyParameters).encoded ) ) } diff --git a/atala-prism-sdk/src/commonTest/kotlin/io/iohk/atala/prism/walletsdk/apollo/ApolloTests.kt b/atala-prism-sdk/src/commonTest/kotlin/io/iohk/atala/prism/walletsdk/apollo/ApolloTests.kt index 1bcae9faf..68e7c9832 100644 --- a/atala-prism-sdk/src/commonTest/kotlin/io/iohk/atala/prism/walletsdk/apollo/ApolloTests.kt +++ b/atala-prism-sdk/src/commonTest/kotlin/io/iohk/atala/prism/walletsdk/apollo/ApolloTests.kt @@ -107,4 +107,13 @@ class ApolloTests { assertEquals(32, privateKey.value.size) assertEquals(32, publicKey.value.size) } + + @Test + fun testCreateKeyPair_whenNoSeedAndKeyCurveX25519_thenPrivateKeyLengthIsCorrect() { + val keyPair = apollo.createKeyPair(curve = KeyCurve(Curve.X25519)) + val privateKey = keyPair.privateKey + val publicKey = keyPair.publicKey + assertEquals(32, privateKey.value.size) + assertEquals(32, publicKey.value.size) + } } diff --git a/atala-prism-sdk/src/jvmMain/kotlin/io/iohk/atala/prism/walletsdk/apollo/X25519.kt b/atala-prism-sdk/src/jvmMain/kotlin/io/iohk/atala/prism/walletsdk/apollo/X25519.kt index 571de8ba8..a8424195d 100644 --- a/atala-prism-sdk/src/jvmMain/kotlin/io/iohk/atala/prism/walletsdk/apollo/X25519.kt +++ b/atala-prism-sdk/src/jvmMain/kotlin/io/iohk/atala/prism/walletsdk/apollo/X25519.kt @@ -5,28 +5,30 @@ import io.iohk.atala.prism.walletsdk.domain.models.KeyCurve import io.iohk.atala.prism.walletsdk.domain.models.KeyPair import io.iohk.atala.prism.walletsdk.domain.models.PrivateKey import io.iohk.atala.prism.walletsdk.domain.models.PublicKey -import org.bouncycastle.jcajce.spec.XDHParameterSpec -import org.bouncycastle.jce.provider.BouncyCastleProvider -import java.security.KeyPairGenerator +import org.bouncycastle.crypto.generators.X25519KeyPairGenerator +import org.bouncycastle.crypto.params.X25519KeyGenerationParameters +import org.bouncycastle.crypto.params.X25519PrivateKeyParameters +import org.bouncycastle.crypto.params.X25519PublicKeyParameters +import java.security.SecureRandom /** * X25519 */ actual object X25519 { actual fun createKeyPair(): KeyPair { - val provider = BouncyCastleProvider() - val kpg = KeyPairGenerator.getInstance("X25519", provider) - kpg.initialize(XDHParameterSpec(XDHParameterSpec.X25519)) - val javaKeyPair = kpg.generateKeyPair() + val generator = X25519KeyPairGenerator() + generator.init(X25519KeyGenerationParameters(SecureRandom())) + val keyPair = generator.generateKeyPair() + return KeyPair( KeyCurve(Curve.X25519), PrivateKey( KeyCurve(Curve.X25519), - javaKeyPair.private.encoded + (keyPair.private as X25519PrivateKeyParameters).encoded ), PublicKey( KeyCurve(Curve.X25519), - javaKeyPair.public.encoded + (keyPair.public as X25519PublicKeyParameters).encoded ) ) }