-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added public key derivers for eddsa and ecdsa
- Loading branch information
1 parent
b8099fc
commit 6bf5cc9
Showing
6 changed files
with
122 additions
and
32 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
src/main/kotlin/id/walt/services/key/AsymmetricPublicKeyConverter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package id.walt.services.key | ||
|
||
import org.bouncycastle.crypto.params.AsymmetricKeyParameter | ||
import org.bouncycastle.crypto.params.ECPublicKeyParameters | ||
import org.bouncycastle.crypto.params.Ed25519PublicKeyParameters | ||
import org.bouncycastle.crypto.params.RSAKeyParameters | ||
import org.bouncycastle.jcajce.provider.asymmetric.util.EC5Util | ||
import java.security.KeyFactory | ||
import java.security.PublicKey | ||
import java.security.spec.ECPoint | ||
import java.security.spec.ECPublicKeySpec | ||
import java.security.spec.RSAPublicKeySpec | ||
|
||
class AsymmetricPublicKeyConverter { | ||
|
||
fun convert(key: AsymmetricKeyParameter) = when (key) { | ||
is ECPublicKeyParameters -> ecAsymmetricKeyParameterToPublicKey(key) | ||
is Ed25519PublicKeyParameters -> edAsymmetricKeyParameterToPublicKey(key) | ||
is RSAKeyParameters -> rsaAsymmetricKeyParameterToPublicKey(key) | ||
else -> null | ||
} | ||
|
||
private fun ecAsymmetricKeyParameterToPublicKey(key: ECPublicKeyParameters): PublicKey = let { | ||
val ecParameterSpec = EC5Util.convertToSpec(key.parameters) | ||
val ecPoint: ECPoint = EC5Util.convertPoint(key.q) | ||
val ecPublicKeySpec = ECPublicKeySpec(ecPoint, ecParameterSpec) | ||
KeyFactory.getInstance("EC").generatePublic(ecPublicKeySpec) | ||
} | ||
|
||
private fun edAsymmetricKeyParameterToPublicKey(key: Ed25519PublicKeyParameters): PublicKey = let { | ||
TODO() | ||
} | ||
|
||
private fun rsaAsymmetricKeyParameterToPublicKey(key: RSAKeyParameters): PublicKey = let { | ||
val rsaPublicKeySpec = RSAPublicKeySpec(key.modulus, key.exponent) | ||
KeyFactory.getInstance("RSA").generatePublic(rsaPublicKeySpec) | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/main/kotlin/id/walt/services/key/deriver/AsymmetricPublicKeyDeriver.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package id.walt.services.key.deriver | ||
|
||
import id.walt.services.key.AsymmetricPublicKeyConverter | ||
import org.bouncycastle.crypto.params.* | ||
import java.security.PublicKey | ||
|
||
class AsymmetricPublicKeyDeriver( | ||
private val keyConverter: AsymmetricPublicKeyConverter | ||
) : PublicKeyDeriver<AsymmetricKeyParameter> { | ||
|
||
override fun derive(key: AsymmetricKeyParameter): PublicKey? = when (key) { | ||
is RSAPrivateCrtKeyParameters -> { | ||
RSAKeyParameters(false, key.modulus, key.publicExponent) | ||
} | ||
is Ed25519PrivateKeyParameters -> { | ||
key.generatePublicKey()//TODO | ||
} | ||
is ECPrivateKeyParameters -> { | ||
val q = key.parameters.g.multiply(key.d) | ||
ECPublicKeyParameters(q, key.parameters) | ||
} | ||
else -> null | ||
}?.let{ | ||
keyConverter.convert(it) | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/main/kotlin/id/walt/services/key/deriver/DefaultPublicKeyDeriver.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package id.walt.services.key.deriver | ||
|
||
import id.walt.crypto.KeyAlgorithm | ||
import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey | ||
import sun.security.ec.ed.EdDSAOperations | ||
import sun.security.ec.ed.EdDSAParameters | ||
import java.math.BigInteger | ||
import java.security.InvalidAlgorithmParameterException | ||
import java.security.KeyFactory | ||
import java.security.PrivateKey | ||
import java.security.PublicKey | ||
import java.security.spec.EdECPublicKeySpec | ||
import java.security.spec.NamedParameterSpec | ||
import java.security.spec.RSAPrivateKeySpec | ||
import java.security.spec.RSAPublicKeySpec | ||
|
||
class DefaultPublicKeyDeriver: PublicKeyDeriver<PrivateKey> { | ||
override fun derive(key: PrivateKey): PublicKey? = when (KeyAlgorithm.fromString(key.algorithm)) { | ||
KeyAlgorithm.RSA -> { | ||
val kf = KeyFactory.getInstance("RSA") | ||
val privateSpec = kf.getKeySpec(key, RSAPrivateKeySpec::class.java) | ||
val publicSpec = RSAPublicKeySpec(privateSpec.modulus, BigInteger.valueOf(65537)) | ||
kf.generatePublic(publicSpec) | ||
} | ||
KeyAlgorithm.EdDSA_Ed25519 -> { | ||
val edDsaOperations = | ||
EdDSAOperations(EdDSAParameters.get({ InvalidAlgorithmParameterException() }, NamedParameterSpec.ED25519)) | ||
val edecPublicKeyPoint = edDsaOperations.computePublic(key.encoded) | ||
val publicSpec = EdECPublicKeySpec(NamedParameterSpec.ED25519, edecPublicKeyPoint) | ||
KeyFactory.getInstance("Ed25519").generatePublic(publicSpec) | ||
} | ||
//TODO: remove BC dependency, rely purely on java.security | ||
KeyAlgorithm.ECDSA_Secp256k1, KeyAlgorithm.ECDSA_Secp256r1 -> { | ||
val definingKey = key as BCECPrivateKey | ||
val d = definingKey.d | ||
val ecSpec = definingKey.parameters | ||
val q = definingKey.parameters.g.multiply(d) | ||
val pubSpec = org.bouncycastle.jce.spec.ECPublicKeySpec(q, ecSpec) | ||
KeyFactory.getInstance("ECDSA").generatePublic(pubSpec) | ||
} | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/kotlin/id/walt/services/key/deriver/PublicKeyDeriver.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package id.walt.services.key.deriver | ||
|
||
import java.security.PublicKey | ||
|
||
interface PublicKeyDeriver<T> { | ||
fun derive(key: T): PublicKey? | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters