rdblue commented on code in PR #3231:
URL: https://github.com/apache/iceberg/pull/3231#discussion_r1292844564
##########
core/src/main/java/org/apache/iceberg/encryption/Ciphers.java:
##########
@@ -53,29 +70,61 @@ public AesGcmEncryptor(byte[] keyBytes) {
}
this.randomGenerator = new SecureRandom();
+ this.nonce = new byte[NONCE_LENGTH];
}
- public byte[] encrypt(byte[] plainText, byte[] aad) {
- byte[] nonce = new byte[NONCE_LENGTH];
- randomGenerator.nextBytes(nonce);
- int cipherTextLength = NONCE_LENGTH + plainText.length + GCM_TAG_LENGTH;
+ public byte[] encrypt(byte[] plaintext, byte[] aad) {
+ return encrypt(plaintext, 0, plaintext.length, aad);
+ }
+
+ public byte[] encrypt(byte[] plaintext, int plaintextOffset, int
plaintextLength, byte[] aad) {
+ int cipherTextLength = NONCE_LENGTH + plaintextLength + GCM_TAG_LENGTH;
byte[] cipherText = new byte[cipherTextLength];
+ encrypt(plaintext, plaintextOffset, plaintextLength, cipherText, 0, aad);
+ return cipherText;
+ }
+
+ public int encrypt(
+ byte[] plaintext,
+ int plaintextOffset,
+ int plaintextLength,
+ byte[] ciphertextBuffer,
+ int ciphertextOffset,
+ byte[] aad) {
+ Preconditions.checkArgument(plaintextLength > 0, "Wrong plaintextLength
" + plaintextLength);
+ randomGenerator.nextBytes(nonce);
+ int enciphered;
try {
GCMParameterSpec spec = new GCMParameterSpec(GCM_TAG_LENGTH_BITS,
nonce);
cipher.init(Cipher.ENCRYPT_MODE, aesKey, spec);
if (null != aad) {
cipher.updateAAD(aad);
}
- cipher.doFinal(plainText, 0, plainText.length, cipherText,
NONCE_LENGTH);
+ enciphered =
+ cipher.doFinal(
+ plaintext,
+ plaintextOffset,
+ plaintextLength,
+ ciphertextBuffer,
+ ciphertextOffset + NONCE_LENGTH);
+
+ if (enciphered != plaintextLength + GCM_TAG_LENGTH) {
+ throw new RuntimeException(
+ "Wrong number of enciphered bytes: "
+ + enciphered
+ + ". Must be "
+ + plaintextLength
+ + GCM_TAG_LENGTH);
Review Comment:
I think this can be more clear because it doesn't tell the reader that the
encryption didn't work right.
Instead, how about `Failed to encrypt block: expected %s encrypted bytes but
produced %s bytes`?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]