Skip to content

Commit b12d3e6

Browse files
committed
refactor
1 parent 14288d5 commit b12d3e6

14 files changed

Lines changed: 84 additions & 164 deletions

File tree

sdk/src/main/java/io/opentdf/platform/sdk/ECCMode.java

Lines changed: 2 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public ECCMode() {
1313
public ECCMode(byte value) {
1414
data = new ECCModeStruct();
1515
int curveMode = value & 0x07; // first 3 bits
16-
setEllipticCurve(NanoTDFType.ECCurve.values()[curveMode]);
16+
setEllipticCurve(NanoTDFType.ECCurve.fromCurveMode(curveMode));
1717
int useECDSABinding = (value >> 7) & 0x01; // most significant bit
1818
data.useECDSABinding = useECDSABinding;
1919
}
@@ -52,69 +52,16 @@ public boolean isECDSABindingEnabled() {
5252
return data.useECDSABinding == 1;
5353
}
5454

55-
public String getCurveName() {
56-
return getEllipticCurveName(NanoTDFType.ECCurve.values()[data.curveMode]);
57-
}
58-
5955
public byte getECCModeAsByte() {
6056
int value = (data.useECDSABinding << 7) | data.curveMode;
6157
return (byte) value;
6258
}
6359

64-
public static String getEllipticCurveName(NanoTDFType.ECCurve curve) {
65-
switch (curve) {
66-
case SECP256R1:
67-
return "secp256r1";
68-
case SECP384R1:
69-
return "secp384r1";
70-
case SECP521R1:
71-
return "secp521r1";
72-
case SECP256K1:
73-
throw new RuntimeException("SDK doesn't support 'secp256k1' curve");
74-
default:
75-
throw new RuntimeException("Unsupported ECC algorithm.");
76-
}
77-
}
78-
79-
public static int getECKeySize(NanoTDFType.ECCurve curve) {
80-
switch (curve) {
81-
case SECP256K1:
82-
throw new RuntimeException("SDK doesn't support 'secp256k1' curve");
83-
case SECP256R1:
84-
return 32;
85-
case SECP384R1:
86-
return 48;
87-
case SECP521R1:
88-
return 66;
89-
default:
90-
throw new RuntimeException("Unsupported ECC algorithm.");
91-
}
92-
}
93-
9460
public static int getECDSASignatureStructSize(NanoTDFType.ECCurve curve) {
95-
int keySize = getECKeySize(curve);
61+
int keySize = curve.compressedPubKeySize;
9662
return (1 + keySize + 1 + keySize);
9763
}
9864

99-
public static int getECKeySize(String curveName) {
100-
return ECKeyPair.getECKeySize(curveName);
101-
}
102-
103-
public static int getECCompressedPubKeySize(NanoTDFType.ECCurve curve) {
104-
switch (curve) {
105-
case SECP256K1:
106-
throw new RuntimeException("SDK doesn't support 'secp256k1' curve");
107-
case SECP256R1:
108-
return 33;
109-
case SECP384R1:
110-
return 49;
111-
case SECP521R1:
112-
return 67;
113-
default:
114-
throw new RuntimeException("Unsupported ECC algorithm.");
115-
}
116-
}
117-
11865
private class ECCModeStruct {
11966
int curveMode;
12067
int unused;

sdk/src/main/java/io/opentdf/platform/sdk/ECKeyPair.java

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -39,30 +39,6 @@ public enum ECAlgorithm {
3939

4040
private static final BouncyCastleProvider BOUNCY_CASTLE_PROVIDER = new BouncyCastleProvider();
4141

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-
6642
private KeyPair keyPair;
6743
private String curveName;
6844

@@ -108,19 +84,6 @@ public ECPrivateKey getPrivateKey() {
10884
return (ECPrivateKey) this.keyPair.getPrivate();
10985
}
11086

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-
}
122-
}
123-
12487
public String publicKeyInPEMFormat() {
12588
StringWriter writer = new StringWriter();
12689
PemWriter pemWriter = new PemWriter(writer);

sdk/src/main/java/io/opentdf/platform/sdk/Header.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public Header(ByteBuffer buffer) {
2626
this.payloadConfig = new SymmetricAndPayloadConfig(buffer.get());
2727
this.policyInfo = new PolicyInfo(buffer, this.eccMode);
2828

29-
int compressedPubKeySize = ECCMode.getECCompressedPubKeySize(this.eccMode.getEllipticCurveType());
29+
int compressedPubKeySize = this.eccMode.getEllipticCurveType().keySize;
3030
this.ephemeralKey = new byte[compressedPubKeySize];
3131
buffer.get(this.ephemeralKey);
3232
}
@@ -79,10 +79,10 @@ public PolicyInfo getPolicyInfo() {
7979
}
8080

8181
public void setEphemeralKey(byte[] bytes) {
82-
if (bytes.length < eccMode.getECCompressedPubKeySize(eccMode.getEllipticCurveType())) {
82+
if (bytes.length < eccMode.getEllipticCurveType().keySize) {
8383
throw new IllegalArgumentException("Failed to read ephemeral key - invalid buffer size.");
8484
}
85-
ephemeralKey = Arrays.copyOf(bytes, eccMode.getECCompressedPubKeySize(eccMode.getEllipticCurveType()));
85+
ephemeralKey = Arrays.copyOf(bytes, eccMode.getEllipticCurveType().keySize);
8686
}
8787

8888
public byte[] getEphemeralKey() {

sdk/src/main/java/io/opentdf/platform/sdk/KASClient.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,8 @@ public byte[] unwrap(Manifest.KeyAccess keyAccess, String policy, KeyType sessi
149149
ECKeyPair ecKeyPair = null;
150150

151151
if (sessionKeyType.isEc()) {
152-
var curveName = sessionKeyType.getCurveName();
153-
ecKeyPair = new ECKeyPair(curveName, ECKeyPair.ECAlgorithm.ECDH);
152+
var curve = sessionKeyType.getECEcurve().get();
153+
ecKeyPair = new ECKeyPair(curve.curveName, ECKeyPair.ECAlgorithm.ECDH);
154154
clientPublicKey = ecKeyPair.publicKeyInPEMFormat();
155155
} else {
156156
// Initialize the RSA key pair only once and reuse it for future unwrap operations
Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,38 @@
11
package io.opentdf.platform.sdk;
22

3+
import java.util.Optional;
4+
5+
import static io.opentdf.platform.sdk.NanoTDFType.ECCurve.SECP256R1;
6+
import static io.opentdf.platform.sdk.NanoTDFType.ECCurve.SECP384R1;
7+
import static io.opentdf.platform.sdk.NanoTDFType.ECCurve.SECP521R1;
8+
39
public enum KeyType {
410
RSA2048Key("rsa:2048"),
5-
EC256Key("ec:secp256r1"),
6-
EC384Key("ec:secp384r1"),
7-
EC521Key("ec:secp521r1");
11+
EC256Key("ec:secp256r1", SECP256R1),
12+
EC384Key("ec:secp384r1", SECP384R1),
13+
EC521Key("ec:secp521r1", SECP521R1);
814

915
private final String keyType;
16+
private final NanoTDFType.ECCurve curve;
1017

11-
KeyType(String keyType) {
18+
KeyType(String keyType, NanoTDFType.ECCurve ecCurve) {
1219
this.keyType = keyType;
20+
this.curve = ecCurve;
21+
}
22+
23+
KeyType(String keyType) {
24+
this(keyType, null);
25+
}
26+
27+
public Optional<NanoTDFType.ECCurve> getECEcurve() {
28+
return Optional.ofNullable(curve);
1329
}
1430

1531
@Override
1632
public String toString() {
1733
return keyType;
1834
}
1935

20-
public String getCurveName() {
21-
switch (this) {
22-
case EC256Key:
23-
return "secp256r1";
24-
case EC384Key:
25-
return "secp384r1";
26-
case EC521Key:
27-
return "secp521r1";
28-
default:
29-
throw new IllegalArgumentException("Unsupported key type: " + this);
30-
}
31-
}
3236

3337
public static KeyType fromString(String keyType) {
3438
for (KeyType type : KeyType.values()) {
@@ -40,6 +44,6 @@ public static KeyType fromString(String keyType) {
4044
}
4145

4246
public boolean isEc() {
43-
return this != RSA2048Key;
47+
return this.curve != null;
4448
}
4549
}

sdk/src/main/java/io/opentdf/platform/sdk/NanoTDF.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ private Config.HeaderInfo getHeaderInfo(Config.NanoTDFConfig nanoTDFConfig) thro
8989
ResourceLocator kasURL = new ResourceLocator(nanoTDFConfig.kasInfoList.get(0).URL, kasInfo.KID);
9090
assert kasURL.getIdentifier() != null : "Identifier in ResourceLocator cannot be null";
9191

92-
ECKeyPair keyPair = new ECKeyPair(nanoTDFConfig.eccMode.getCurveName(), ECKeyPair.ECAlgorithm.ECDSA);
92+
ECKeyPair keyPair = new ECKeyPair(nanoTDFConfig.eccMode.getEllipticCurveType().curveName, ECKeyPair.ECAlgorithm.ECDSA);
9393

9494
// Generate symmetric key
9595
ECPublicKey kasPublicKey = ECKeyPair.publicKeyFromPem(kasInfo.PublicKey);

sdk/src/main/java/io/opentdf/platform/sdk/NanoTDFType.java

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,38 @@
11
package io.opentdf.platform.sdk;
22

33
public class NanoTDFType {
4-
public enum ECCurve {
5-
SECP256R1("secp256r1"),
6-
SECP384R1("secp384r1"),
7-
SECP521R1("secp384r1"),
8-
SECP256K1("secp256k1");
4+
enum ECCurve {
5+
SECP256R1("secp256r1", 32, 33, 0x00),
6+
SECP384R1("secp384r1", 48, 49, 0x01),
7+
SECP521R1("secp512r1", 66, 67, 0x02),
8+
SECP256K1("secp256k1",-1, -1, -1, false); // Note: SECP256K1 is not supported by the SDK
99

10-
private final String name;
10+
final int curveMode;
11+
final int keySize;
12+
final int compressedPubKeySize;
13+
final String curveName;
14+
final boolean isSupported;
1115

12-
ECCurve(String curveName) {
13-
this.name = curveName;
16+
17+
ECCurve(String curveName, int compressedPubKeySize, int keySize, int curveMode) {
18+
this(curveName, compressedPubKeySize, keySize, curveMode, true);
19+
}
20+
21+
ECCurve(String curveName, int compressedPubKeySize, int keySize, int curveMode, boolean isSupported) {
22+
this.compressedPubKeySize = compressedPubKeySize;
23+
this.keySize = keySize;
24+
this.curveMode = curveMode;
25+
this.curveName = curveName ;
26+
this.isSupported = isSupported;
1427
}
1528

16-
@Override
17-
public String toString() {
18-
return name;
29+
static ECCurve fromCurveMode(int curveMode) {
30+
for (ECCurve curve : ECCurve.values()) {
31+
if (curve.curveMode == curveMode) {
32+
return curve;
33+
}
34+
}
35+
throw new IllegalArgumentException("No enum constant for curve mode: " + curveMode);
1936
}
2037
}
2138
// ResourceLocator Protocol

sdk/src/main/java/io/opentdf/platform/sdk/SymmetricAndPayloadConfig.java

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package io.opentdf.platform.sdk;
22

3+
import io.opentdf.platform.policy.Key;
4+
35
public class SymmetricAndPayloadConfig {
46
private Data data;
57

@@ -17,7 +19,7 @@ public SymmetricAndPayloadConfig(byte value) {
1719
setSymmetricCipherType(NanoTDFType.Cipher.values()[cipherType]);
1820

1921
int signatureECCMode = (value >> 4) & 0x07;
20-
setSignatureECCMode(NanoTDFType.ECCurve.values()[signatureECCMode]);
22+
data.signatureECCMode = signatureECCMode;
2123

2224
int hasSignature = (value >> 7) & 0x01; // most significant bit
2325
data.hasSignature = hasSignature;
@@ -27,22 +29,11 @@ public void setHasSignature(boolean flag) {
2729
data.hasSignature = flag ? 1 : 0;
2830
}
2931

30-
public void setSignatureECCMode(NanoTDFType.ECCurve curve) {
31-
switch (curve) {
32-
case SECP256R1:
33-
data.signatureECCMode = 0x00;
34-
break;
35-
case SECP384R1:
36-
data.signatureECCMode = 0x01;
37-
break;
38-
case SECP521R1:
39-
data.signatureECCMode = 0x02;
40-
break;
41-
case SECP256K1:
42-
throw new RuntimeException("SDK doesn't support 'secp256k1' curve");
43-
default:
44-
throw new RuntimeException("Unsupported ECC algorithm.");
32+
public void setSignatureECCMode(NanoTDFType.ECCurve eccCurve) {
33+
if (!eccCurve.isSupported) {
34+
throw new RuntimeException(String.format("Unsupported ECC algorithm: %s", eccCurve.curveName));
4535
}
36+
data.signatureECCMode = eccCurve.curveMode;
4637
}
4738

4839
public void setSymmetricCipherType(NanoTDFType.Cipher cipherType) {
@@ -77,10 +68,6 @@ public boolean hasSignature() {
7768
return data.hasSignature == 1;
7869
}
7970

80-
public NanoTDFType.ECCurve getSignatureECCMode() {
81-
return NanoTDFType.ECCurve.values()[data.signatureECCMode];
82-
}
83-
8471
public NanoTDFType.Cipher getCipherType() {
8572
return NanoTDFType.Cipher.values()[data.symmetricCipherEnum];
8673
}
@@ -111,6 +98,10 @@ static public int sizeOfAuthTagForCipher(NanoTDFType.Cipher cipherType) {
11198
}
11299
}
113100

101+
public NanoTDFType.ECCurve getSignatureECCMode() {
102+
return NanoTDFType.ECCurve.fromCurveMode(this.data.signatureECCMode);
103+
}
104+
114105
private static class Data {
115106
int symmetricCipherEnum;
116107
int signatureECCMode;

sdk/src/main/java/io/opentdf/platform/sdk/TDF.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,8 +277,9 @@ private Manifest.KeyAccess createKeyAccess(Config.TDFConfig tdfConfig, Config.KA
277277
}
278278

279279
private ECKeyWrappedKeyInfo createECWrappedKey(Config.TDFConfig tdfConfig, Config.KASInfo kasInfo, byte[] symKey) {
280-
var curveName = tdfConfig.wrappingKeyType.getCurveName();
281-
var keyPair = new ECKeyPair(curveName, ECKeyPair.ECAlgorithm.ECDH);
280+
var curve = tdfConfig.wrappingKeyType.getECEcurve().get();
281+
assert curve != null : "Wrapping key type must be an EC key type";
282+
var keyPair = new ECKeyPair(curve.curveName, ECKeyPair.ECAlgorithm.ECDH);
282283

283284
ECPublicKey kasPubKey = ECKeyPair.publicKeyFromPem(kasInfo.PublicKey);
284285
byte[] symmetricKey = ECKeyPair.computeECDHKey(kasPubKey, keyPair.getPrivateKey());

0 commit comments

Comments
 (0)