2424import java .io .*;
2525import java .security .*;
2626import java .security .spec .*;
27+ import java .util .Objects ;
2728// https://www.bouncycastle.org/latest_releases.html
2829
2930public class ECKeyPair {
@@ -32,45 +33,23 @@ public class ECKeyPair {
3233 Security .addProvider (new BouncyCastleProvider ());
3334 }
3435
36+ private final NanoTDFType .ECCurve curve ;
37+
3538 public enum ECAlgorithm {
3639 ECDH ,
3740 ECDSA
3841 }
3942
4043 private static final BouncyCastleProvider BOUNCY_CASTLE_PROVIDER = new BouncyCastleProvider ();
4144
42- public enum NanoTDFECCurve {
43- SECP256R1 ("secp256r1" , KeyType .EC256Key ),
44- PRIME256V1 ("prime256v1" , KeyType .EC256Key ),
45- SECP384R1 ("secp384r1" , KeyType .EC384Key ),
46- SECP521R1 ("secp521r1" , KeyType .EC521Key );
47-
48- private String name ;
49- private KeyType keyType ;
50-
51- NanoTDFECCurve (String curveName , KeyType keyType ) {
52- this .name = curveName ;
53- this .keyType = keyType ;
54- }
55-
56- @ Override
57- public String toString () {
58- return name ;
59- }
60-
61- public KeyType getKeyType () {
62- return keyType ;
63- }
64- }
65-
6645 private KeyPair keyPair ;
67- private String curveName ;
6846
6947 public ECKeyPair () {
70- this ("secp256r1" , ECAlgorithm .ECDH );
48+ this (NanoTDFType . ECCurve . SECP256R1 , ECAlgorithm .ECDH );
7149 }
7250
73- public ECKeyPair (String curveName , ECAlgorithm algorithm ) {
51+ public ECKeyPair (NanoTDFType .ECCurve curve , ECAlgorithm algorithm ) {
52+ this .curve = Objects .requireNonNull (curve );
7453 KeyPairGenerator generator ;
7554
7655 try {
@@ -85,19 +64,13 @@ public ECKeyPair(String curveName, ECAlgorithm algorithm) {
8564 throw new RuntimeException (e );
8665 }
8766
88- ECGenParameterSpec spec = new ECGenParameterSpec (curveName );
67+ ECGenParameterSpec spec = new ECGenParameterSpec (this . curve . getCurveName () );
8968 try {
9069 generator .initialize (spec );
9170 } catch (InvalidAlgorithmParameterException e ) {
9271 throw new RuntimeException (e );
9372 }
9473 this .keyPair = generator .generateKeyPair ();
95- this .curveName = curveName ;
96- }
97-
98- public ECKeyPair (ECPublicKey publicKey , ECPrivateKey privateKey , String curveName ) {
99- this .keyPair = new KeyPair (publicKey , privateKey );
100- this .curveName = curveName ;
10174 }
10275
10376 public ECPublicKey getPublicKey () {
@@ -108,17 +81,8 @@ public ECPrivateKey getPrivateKey() {
10881 return (ECPrivateKey ) this .keyPair .getPrivate ();
10982 }
11083
111- public static int getECKeySize (String curveName ) {
112- if (curveName .equalsIgnoreCase (NanoTDFECCurve .SECP256R1 .toString ()) ||
113- curveName .equalsIgnoreCase (NanoTDFECCurve .PRIME256V1 .toString ())) {
114- return 32 ;
115- } else if (curveName .equalsIgnoreCase (NanoTDFECCurve .SECP384R1 .toString ())) {
116- return 48 ;
117- } else if (curveName .equalsIgnoreCase (NanoTDFECCurve .SECP521R1 .toString ())) {
118- return 66 ;
119- } else {
120- throw new IllegalArgumentException ("Unsupported ECC algorithm." );
121- }
84+ NanoTDFType .ECCurve getCurve () {
85+ return this .curve ;
12286 }
12387
12488 public String publicKeyInPEMFormat () {
@@ -155,10 +119,6 @@ public int keySize() {
155119 return this .keyPair .getPrivate ().getEncoded ().length * 8 ;
156120 }
157121
158- public String curveName () {
159- return this .curveName ;
160- }
161-
162122 public byte [] compressECPublickey () {
163123 return ((ECPublicKey ) this .keyPair .getPublic ()).getQ ().getEncoded (true );
164124 }
0 commit comments