Skip to content

Commit

Permalink
feat: Implement X25519 - Keypair generation (#53)
Browse files Browse the repository at this point in the history
* feat: Implement X25519 - Keypair generation

* Fix linting issues
  • Loading branch information
cristianIOHK authored Apr 25, 2023
1 parent 6c4eab1 commit be0aa7e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
)
}
Expand Down

0 comments on commit be0aa7e

Please sign in to comment.