Skip to content

Commit 6301d32

Browse files
authored
fix: NanoTDF secure key from debug logging and iv conflict risk (#208)
This change is motivated from the CodeQL result: https://github.com/opentdf/java-sdk/security/code-scanning/1 Although that use of a static IV is deliberate, it helped highlight that we should ensure that there is no reuse of the IV when encrypting the data. In addition it was found that there were two places the key was logged, due to the sensitivity of the key this has been removed.
1 parent b4f95e6 commit 6301d32

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ public int createNanoTDF(ByteBuffer data, OutputStream outputStream,
8888
MessageDigest digest = MessageDigest.getInstance("SHA-256");
8989
byte[] hashOfSalt = digest.digest(MAGIC_NUMBER_AND_VERSION);
9090
byte[] key = ECKeyPair.calculateHKDF(hashOfSalt, symmetricKey);
91-
logger.debug("createNanoTDF key is - {}", Base64.getEncoder().encodeToString(key));
9291

9392
// Encrypt policy
9493
PolicyObject policyObject = createPolicyObject(nanoTDFConfig.attributes);
@@ -135,9 +134,11 @@ public int createNanoTDF(ByteBuffer data, OutputStream outputStream,
135134

136135
// Encrypt the data
137136
byte[] actualIV = new byte[kIvPadding + kNanoTDFIvSize];
138-
byte[] iv = new byte[kNanoTDFIvSize];
139-
SecureRandom.getInstanceStrong().nextBytes(iv);
140-
System.arraycopy(iv, 0, actualIV, kIvPadding, iv.length);
137+
do {
138+
byte[] iv = new byte[kNanoTDFIvSize];
139+
SecureRandom.getInstanceStrong().nextBytes(iv);
140+
System.arraycopy(iv, 0, actualIV, kIvPadding, iv.length);
141+
} while (Arrays.equals(actualIV, kEmptyIV)); // if match, we need to retry to prevent key + iv reuse with the policy
141142

142143
byte[] cipherData = gcm.encrypt(actualIV, authTagSize, data.array(), 0, dataSize);
143144

@@ -173,7 +174,6 @@ public void readNanoTDF(ByteBuffer nanoTDF, OutputStream outputStream,
173174
byte[] key = kas.unwrapNanoTDF(header.getECCMode().getEllipticCurveType(),
174175
base64HeaderData,
175176
kasUrl);
176-
logger.debug("readNanoTDF key is {}", Base64.getEncoder().encodeToString(key));
177177

178178
byte[] payloadLengthBuf = new byte[4];
179179
nanoTDF.get(payloadLengthBuf, 1, 3);

0 commit comments

Comments
 (0)