Skip to content

Commit 7336d2b

Browse files
authored
Merge branch 'main' into nano-base-key
2 parents e853f99 + 95c20b3 commit 7336d2b

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

.github/workflows/codeql.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ on:
44
schedule:
55
- cron: '0 13 * * 1' # At 1:00 PM UTC every Monday
66
pull_request:
7-
paths:
8-
- '.github/workflows/codeql.yaml'
97

108
jobs:
119
analyze:

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929

3030
public class ECKeyPair {
3131

32+
private static final int SHA256_BYTES = 32;
33+
3234
static {
3335
Security.addProvider(new BouncyCastleProvider());
3436
}
@@ -232,8 +234,12 @@ public static byte[] computeECDHKey(ECPublicKey publicKey, ECPrivateKey privateK
232234
}
233235
}
234236

237+
/**
238+
* Returns a HKDF key derived from the provided salt and secret
239+
* that is 32 bytes (256 bits) long.
240+
*/
235241
public static byte[] calculateHKDF(byte[] salt, byte[] secret) {
236-
byte[] key = new byte[secret.length];
242+
byte[] key = new byte[SHA256_BYTES];
237243
HKDFParameters params = new HKDFParameters(secret, salt, null);
238244

239245
HKDFBytesGenerator hkdf = new HKDFBytesGenerator(SHA256Digest.newInstance());

sdk/src/test/java/io/opentdf/platform/sdk/ECKeyPairTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import java.util.Base64;
1414

1515
import static io.opentdf.platform.sdk.NanoTDFType.ECCurve.SECP256R1;
16+
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
1617
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
1718
import static org.junit.jupiter.api.Assertions.assertEquals;
1819

@@ -117,6 +118,19 @@ void extractPemPubKeyFromX509() throws CertificateException, IOException, NoSuch
117118
byte[] key = ECKeyPair.calculateHKDF(ECKeys.salt.getBytes(StandardCharsets.UTF_8), symmetricKey);
118119
System.out.println(Arrays.toString(key));
119120
System.out.println(key.length);
121+
122+
assertThat(key.length).isEqualTo(32); // SHA-256 produces a 32-byte key
123+
}
124+
125+
@Test
126+
void createSymmetricKeysWithOtherCurves() {
127+
ECKeyPair pubPair = new ECKeyPair(NanoTDFType.ECCurve.SECP384R1, ECKeyPair.ECAlgorithm.ECDH);
128+
ECKeyPair keyPair = new ECKeyPair(NanoTDFType.ECCurve.SECP384R1, ECKeyPair.ECAlgorithm.ECDH);
129+
130+
byte[] sharedSecret = ECKeyPair.computeECDHKey(pubPair.getPublicKey(), keyPair.getPrivateKey());
131+
byte[] encryptionKey = ECKeyPair.calculateHKDF(ECKeys.salt.getBytes(StandardCharsets.UTF_8), sharedSecret);
132+
133+
assertThat(encryptionKey).hasSize(32); // SHA-256 produces a 32-byte key
120134
}
121135

122136
@Test

0 commit comments

Comments
 (0)