rdblue commented on code in PR #3231:
URL: https://github.com/apache/iceberg/pull/3231#discussion_r1279978190


##########
core/src/main/java/org/apache/iceberg/encryption/AesGcmOutputStream.java:
##########
@@ -0,0 +1,115 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.iceberg.encryption;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import org.apache.iceberg.io.PositionOutputStream;
+
+public class AesGcmOutputStream extends PositionOutputStream {
+
+  private final Ciphers.AesGcmEncryptor gcmEncryptor;
+  private final PositionOutputStream targetStream;
+  private final byte[] plainBlockBuffer;
+  private final byte[] fileAadPrefix;
+
+  private int positionInBuffer;
+  private long streamPosition;
+  private int currentBlockIndex;
+
+  AesGcmOutputStream(PositionOutputStream targetStream, byte[] aesKey, byte[] 
fileAadPrefix)
+      throws IOException {
+    this.targetStream = targetStream;
+    this.gcmEncryptor = new Ciphers.AesGcmEncryptor(aesKey);
+    this.plainBlockBuffer = new byte[Ciphers.PLAIN_BLOCK_SIZE];
+    this.positionInBuffer = 0;
+    this.streamPosition = 0;
+    this.currentBlockIndex = 0;
+    this.fileAadPrefix = fileAadPrefix;
+
+    byte[] headerBytes =
+        ByteBuffer.allocate(Ciphers.GCM_STREAM_HEADER_LENGTH)
+            .order(ByteOrder.LITTLE_ENDIAN)
+            .put(Ciphers.GCM_STREAM_MAGIC_ARRAY)
+            .putInt(Ciphers.PLAIN_BLOCK_SIZE)
+            .array();
+    targetStream.write(headerBytes);
+  }
+
+  @Override
+  public void write(int b) throws IOException {
+    throw new UnsupportedOperationException();

Review Comment:
   I think this needs to be implemented as well.



##########
core/src/main/java/org/apache/iceberg/encryption/AesGcmOutputStream.java:
##########
@@ -0,0 +1,115 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.iceberg.encryption;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import org.apache.iceberg.io.PositionOutputStream;
+
+public class AesGcmOutputStream extends PositionOutputStream {
+
+  private final Ciphers.AesGcmEncryptor gcmEncryptor;
+  private final PositionOutputStream targetStream;
+  private final byte[] plainBlockBuffer;
+  private final byte[] fileAadPrefix;
+
+  private int positionInBuffer;
+  private long streamPosition;
+  private int currentBlockIndex;
+
+  AesGcmOutputStream(PositionOutputStream targetStream, byte[] aesKey, byte[] 
fileAadPrefix)
+      throws IOException {
+    this.targetStream = targetStream;
+    this.gcmEncryptor = new Ciphers.AesGcmEncryptor(aesKey);
+    this.plainBlockBuffer = new byte[Ciphers.PLAIN_BLOCK_SIZE];
+    this.positionInBuffer = 0;
+    this.streamPosition = 0;
+    this.currentBlockIndex = 0;
+    this.fileAadPrefix = fileAadPrefix;
+
+    byte[] headerBytes =
+        ByteBuffer.allocate(Ciphers.GCM_STREAM_HEADER_LENGTH)
+            .order(ByteOrder.LITTLE_ENDIAN)
+            .put(Ciphers.GCM_STREAM_MAGIC_ARRAY)
+            .putInt(Ciphers.PLAIN_BLOCK_SIZE)
+            .array();
+    targetStream.write(headerBytes);
+  }
+
+  @Override
+  public void write(int b) throws IOException {
+    throw new UnsupportedOperationException();
+  }
+
+  @Override
+  public void write(byte[] b, int off, int len) throws IOException {
+    if (b.length - off < len) {
+      throw new IOException(
+          "Insufficient bytes in buffer: " + b.length + " - " + off + " < " + 
len);
+    }
+    int remaining = len;
+    int offset = off;
+
+    while (remaining > 0) {
+      int freeBlockBytes = Ciphers.PLAIN_BLOCK_SIZE - positionInBuffer;
+      int toWrite = freeBlockBytes <= remaining ? freeBlockBytes : remaining;
+
+      System.arraycopy(b, offset, plainBlockBuffer, positionInBuffer, toWrite);
+      positionInBuffer += toWrite;
+      if (positionInBuffer == Ciphers.PLAIN_BLOCK_SIZE) {
+        encryptAndWriteBlock();
+        positionInBuffer = 0;
+      }
+      offset += toWrite;
+      remaining -= toWrite;
+    }
+
+    streamPosition += len;
+  }
+
+  @Override
+  public long getPos() throws IOException {
+    return streamPosition;
+  }
+
+  @Override
+  public void flush() throws IOException {
+    targetStream.flush();
+  }
+
+  @Override
+  public void close() throws IOException {
+    if (positionInBuffer > 0) {
+      encryptAndWriteBlock();
+    }
+    targetStream.close();
+  }
+
+  private void encryptAndWriteBlock() throws IOException {

Review Comment:
   Do you think it would be good to have a boolean `fullBlock` to keep track of 
whether the last block was a full block? Then we could check it here and refuse 
to write another block if the last block was not full.



##########
core/src/main/java/org/apache/iceberg/encryption/AesGcmOutputStream.java:
##########
@@ -0,0 +1,115 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.iceberg.encryption;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import org.apache.iceberg.io.PositionOutputStream;
+
+public class AesGcmOutputStream extends PositionOutputStream {
+
+  private final Ciphers.AesGcmEncryptor gcmEncryptor;
+  private final PositionOutputStream targetStream;
+  private final byte[] plainBlockBuffer;
+  private final byte[] fileAadPrefix;
+
+  private int positionInBuffer;
+  private long streamPosition;
+  private int currentBlockIndex;
+
+  AesGcmOutputStream(PositionOutputStream targetStream, byte[] aesKey, byte[] 
fileAadPrefix)
+      throws IOException {
+    this.targetStream = targetStream;
+    this.gcmEncryptor = new Ciphers.AesGcmEncryptor(aesKey);
+    this.plainBlockBuffer = new byte[Ciphers.PLAIN_BLOCK_SIZE];
+    this.positionInBuffer = 0;
+    this.streamPosition = 0;
+    this.currentBlockIndex = 0;
+    this.fileAadPrefix = fileAadPrefix;
+
+    byte[] headerBytes =
+        ByteBuffer.allocate(Ciphers.GCM_STREAM_HEADER_LENGTH)
+            .order(ByteOrder.LITTLE_ENDIAN)
+            .put(Ciphers.GCM_STREAM_MAGIC_ARRAY)
+            .putInt(Ciphers.PLAIN_BLOCK_SIZE)
+            .array();
+    targetStream.write(headerBytes);
+  }
+
+  @Override
+  public void write(int b) throws IOException {
+    throw new UnsupportedOperationException();
+  }
+
+  @Override
+  public void write(byte[] b, int off, int len) throws IOException {
+    if (b.length - off < len) {
+      throw new IOException(
+          "Insufficient bytes in buffer: " + b.length + " - " + off + " < " + 
len);
+    }
+    int remaining = len;
+    int offset = off;
+
+    while (remaining > 0) {
+      int freeBlockBytes = Ciphers.PLAIN_BLOCK_SIZE - positionInBuffer;
+      int toWrite = freeBlockBytes <= remaining ? freeBlockBytes : remaining;
+
+      System.arraycopy(b, offset, plainBlockBuffer, positionInBuffer, toWrite);
+      positionInBuffer += toWrite;
+      if (positionInBuffer == Ciphers.PLAIN_BLOCK_SIZE) {

Review Comment:
   You'll probably want to move this to a method that can be called from 
`write(int)` like `flushBlock()`.



##########
core/src/main/java/org/apache/iceberg/encryption/AesGcmOutputStream.java:
##########
@@ -0,0 +1,115 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.iceberg.encryption;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import org.apache.iceberg.io.PositionOutputStream;
+
+public class AesGcmOutputStream extends PositionOutputStream {
+
+  private final Ciphers.AesGcmEncryptor gcmEncryptor;
+  private final PositionOutputStream targetStream;
+  private final byte[] plainBlockBuffer;
+  private final byte[] fileAadPrefix;
+
+  private int positionInBuffer;
+  private long streamPosition;
+  private int currentBlockIndex;
+
+  AesGcmOutputStream(PositionOutputStream targetStream, byte[] aesKey, byte[] 
fileAadPrefix)
+      throws IOException {
+    this.targetStream = targetStream;
+    this.gcmEncryptor = new Ciphers.AesGcmEncryptor(aesKey);
+    this.plainBlockBuffer = new byte[Ciphers.PLAIN_BLOCK_SIZE];
+    this.positionInBuffer = 0;
+    this.streamPosition = 0;
+    this.currentBlockIndex = 0;
+    this.fileAadPrefix = fileAadPrefix;
+
+    byte[] headerBytes =
+        ByteBuffer.allocate(Ciphers.GCM_STREAM_HEADER_LENGTH)
+            .order(ByteOrder.LITTLE_ENDIAN)
+            .put(Ciphers.GCM_STREAM_MAGIC_ARRAY)
+            .putInt(Ciphers.PLAIN_BLOCK_SIZE)
+            .array();
+    targetStream.write(headerBytes);
+  }
+
+  @Override
+  public void write(int b) throws IOException {
+    throw new UnsupportedOperationException();
+  }
+
+  @Override
+  public void write(byte[] b, int off, int len) throws IOException {
+    if (b.length - off < len) {
+      throw new IOException(
+          "Insufficient bytes in buffer: " + b.length + " - " + off + " < " + 
len);
+    }
+    int remaining = len;
+    int offset = off;
+
+    while (remaining > 0) {
+      int freeBlockBytes = Ciphers.PLAIN_BLOCK_SIZE - positionInBuffer;

Review Comment:
   While it should be the same, I think that this should use 
`plainBlockBuffer.length` instead of `Ciphers.PLAIN_BLOCK_SIZE` because that's 
the length that will avoid an `ArrayIndexOutOfBoundsException`. Otherwise, 
we're relying on the assumption that `plainBlockBuffer.length == 
Ciphers.PLAIN_BLOCK_SIZE` that may not hold later.



##########
core/src/main/java/org/apache/iceberg/encryption/AesGcmOutputStream.java:
##########
@@ -0,0 +1,115 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.iceberg.encryption;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import org.apache.iceberg.io.PositionOutputStream;
+
+public class AesGcmOutputStream extends PositionOutputStream {
+
+  private final Ciphers.AesGcmEncryptor gcmEncryptor;
+  private final PositionOutputStream targetStream;
+  private final byte[] plainBlockBuffer;
+  private final byte[] fileAadPrefix;
+
+  private int positionInBuffer;
+  private long streamPosition;
+  private int currentBlockIndex;
+
+  AesGcmOutputStream(PositionOutputStream targetStream, byte[] aesKey, byte[] 
fileAadPrefix)
+      throws IOException {
+    this.targetStream = targetStream;
+    this.gcmEncryptor = new Ciphers.AesGcmEncryptor(aesKey);
+    this.plainBlockBuffer = new byte[Ciphers.PLAIN_BLOCK_SIZE];
+    this.positionInBuffer = 0;
+    this.streamPosition = 0;
+    this.currentBlockIndex = 0;
+    this.fileAadPrefix = fileAadPrefix;
+
+    byte[] headerBytes =
+        ByteBuffer.allocate(Ciphers.GCM_STREAM_HEADER_LENGTH)
+            .order(ByteOrder.LITTLE_ENDIAN)
+            .put(Ciphers.GCM_STREAM_MAGIC_ARRAY)
+            .putInt(Ciphers.PLAIN_BLOCK_SIZE)
+            .array();
+    targetStream.write(headerBytes);
+  }
+
+  @Override
+  public void write(int b) throws IOException {
+    throw new UnsupportedOperationException();
+  }
+
+  @Override
+  public void write(byte[] b, int off, int len) throws IOException {
+    if (b.length - off < len) {
+      throw new IOException(
+          "Insufficient bytes in buffer: " + b.length + " - " + off + " < " + 
len);
+    }
+    int remaining = len;

Review Comment:
   Nit: missing newline above this.



##########
core/src/main/java/org/apache/iceberg/encryption/Ciphers.java:
##########
@@ -55,10 +71,15 @@ public AesGcmEncryptor(byte[] keyBytes) {
       this.randomGenerator = new SecureRandom();
     }
 
-    public byte[] encrypt(byte[] plainText, byte[] aad) {
+    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) {
+      Preconditions.checkArgument(plaintextLength > 0, "Wrong plaintextLength 
" + plaintextLength);
       byte[] nonce = new byte[NONCE_LENGTH];
       randomGenerator.nextBytes(nonce);
-      int cipherTextLength = NONCE_LENGTH + plainText.length + GCM_TAG_LENGTH;
+      int cipherTextLength = NONCE_LENGTH + plaintextLength + GCM_TAG_LENGTH;

Review Comment:
   Is this buffer allocation necessary? Why can't we pass this in and reuse it 
for every block? It is passed into the `doFinal` call so I think we could have 
this return the number of bytes that were encrypted and reuse a buffer from the 
caller.



##########
core/src/main/java/org/apache/iceberg/encryption/AesGcmOutputStream.java:
##########
@@ -0,0 +1,115 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.iceberg.encryption;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import org.apache.iceberg.io.PositionOutputStream;
+
+public class AesGcmOutputStream extends PositionOutputStream {
+
+  private final Ciphers.AesGcmEncryptor gcmEncryptor;
+  private final PositionOutputStream targetStream;
+  private final byte[] plainBlockBuffer;
+  private final byte[] fileAadPrefix;
+
+  private int positionInBuffer;
+  private long streamPosition;
+  private int currentBlockIndex;
+
+  AesGcmOutputStream(PositionOutputStream targetStream, byte[] aesKey, byte[] 
fileAadPrefix)
+      throws IOException {
+    this.targetStream = targetStream;
+    this.gcmEncryptor = new Ciphers.AesGcmEncryptor(aesKey);
+    this.plainBlockBuffer = new byte[Ciphers.PLAIN_BLOCK_SIZE];
+    this.positionInBuffer = 0;
+    this.streamPosition = 0;
+    this.currentBlockIndex = 0;
+    this.fileAadPrefix = fileAadPrefix;
+
+    byte[] headerBytes =
+        ByteBuffer.allocate(Ciphers.GCM_STREAM_HEADER_LENGTH)
+            .order(ByteOrder.LITTLE_ENDIAN)
+            .put(Ciphers.GCM_STREAM_MAGIC_ARRAY)
+            .putInt(Ciphers.PLAIN_BLOCK_SIZE)
+            .array();
+    targetStream.write(headerBytes);
+  }
+
+  @Override
+  public void write(int b) throws IOException {
+    throw new UnsupportedOperationException();
+  }
+
+  @Override
+  public void write(byte[] b, int off, int len) throws IOException {
+    if (b.length - off < len) {
+      throw new IOException(
+          "Insufficient bytes in buffer: " + b.length + " - " + off + " < " + 
len);
+    }
+    int remaining = len;
+    int offset = off;
+
+    while (remaining > 0) {
+      int freeBlockBytes = Ciphers.PLAIN_BLOCK_SIZE - positionInBuffer;
+      int toWrite = freeBlockBytes <= remaining ? freeBlockBytes : remaining;

Review Comment:
   I think it's better to use `Math.min(freeBlockBytes, remaining)`.



##########
core/src/main/java/org/apache/iceberg/encryption/AesGcmOutputStream.java:
##########
@@ -0,0 +1,115 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.iceberg.encryption;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import org.apache.iceberg.io.PositionOutputStream;
+
+public class AesGcmOutputStream extends PositionOutputStream {
+
+  private final Ciphers.AesGcmEncryptor gcmEncryptor;
+  private final PositionOutputStream targetStream;
+  private final byte[] plainBlockBuffer;
+  private final byte[] fileAadPrefix;
+
+  private int positionInBuffer;
+  private long streamPosition;
+  private int currentBlockIndex;
+
+  AesGcmOutputStream(PositionOutputStream targetStream, byte[] aesKey, byte[] 
fileAadPrefix)
+      throws IOException {
+    this.targetStream = targetStream;
+    this.gcmEncryptor = new Ciphers.AesGcmEncryptor(aesKey);
+    this.plainBlockBuffer = new byte[Ciphers.PLAIN_BLOCK_SIZE];
+    this.positionInBuffer = 0;
+    this.streamPosition = 0;
+    this.currentBlockIndex = 0;
+    this.fileAadPrefix = fileAadPrefix;
+
+    byte[] headerBytes =
+        ByteBuffer.allocate(Ciphers.GCM_STREAM_HEADER_LENGTH)
+            .order(ByteOrder.LITTLE_ENDIAN)
+            .put(Ciphers.GCM_STREAM_MAGIC_ARRAY)
+            .putInt(Ciphers.PLAIN_BLOCK_SIZE)
+            .array();
+    targetStream.write(headerBytes);
+  }
+
+  @Override
+  public void write(int b) throws IOException {
+    throw new UnsupportedOperationException();
+  }
+
+  @Override
+  public void write(byte[] b, int off, int len) throws IOException {
+    if (b.length - off < len) {
+      throw new IOException(
+          "Insufficient bytes in buffer: " + b.length + " - " + off + " < " + 
len);
+    }
+    int remaining = len;
+    int offset = off;
+
+    while (remaining > 0) {
+      int freeBlockBytes = Ciphers.PLAIN_BLOCK_SIZE - positionInBuffer;
+      int toWrite = freeBlockBytes <= remaining ? freeBlockBytes : remaining;
+
+      System.arraycopy(b, offset, plainBlockBuffer, positionInBuffer, toWrite);
+      positionInBuffer += toWrite;
+      if (positionInBuffer == Ciphers.PLAIN_BLOCK_SIZE) {
+        encryptAndWriteBlock();
+        positionInBuffer = 0;
+      }
+      offset += toWrite;

Review Comment:
   I think it would be better to do all of the offset accounting in the same 
place, rather than breaking it up with the check for a full block.



##########
core/src/main/java/org/apache/iceberg/encryption/AesGcmOutputStream.java:
##########
@@ -0,0 +1,115 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.iceberg.encryption;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import org.apache.iceberg.io.PositionOutputStream;
+
+public class AesGcmOutputStream extends PositionOutputStream {
+
+  private final Ciphers.AesGcmEncryptor gcmEncryptor;
+  private final PositionOutputStream targetStream;
+  private final byte[] plainBlockBuffer;
+  private final byte[] fileAadPrefix;
+
+  private int positionInBuffer;
+  private long streamPosition;
+  private int currentBlockIndex;
+
+  AesGcmOutputStream(PositionOutputStream targetStream, byte[] aesKey, byte[] 
fileAadPrefix)
+      throws IOException {
+    this.targetStream = targetStream;
+    this.gcmEncryptor = new Ciphers.AesGcmEncryptor(aesKey);
+    this.plainBlockBuffer = new byte[Ciphers.PLAIN_BLOCK_SIZE];
+    this.positionInBuffer = 0;
+    this.streamPosition = 0;
+    this.currentBlockIndex = 0;
+    this.fileAadPrefix = fileAadPrefix;
+
+    byte[] headerBytes =
+        ByteBuffer.allocate(Ciphers.GCM_STREAM_HEADER_LENGTH)
+            .order(ByteOrder.LITTLE_ENDIAN)
+            .put(Ciphers.GCM_STREAM_MAGIC_ARRAY)
+            .putInt(Ciphers.PLAIN_BLOCK_SIZE)
+            .array();
+    targetStream.write(headerBytes);
+  }
+
+  @Override
+  public void write(int b) throws IOException {
+    throw new UnsupportedOperationException();
+  }
+
+  @Override
+  public void write(byte[] b, int off, int len) throws IOException {
+    if (b.length - off < len) {
+      throw new IOException(
+          "Insufficient bytes in buffer: " + b.length + " - " + off + " < " + 
len);
+    }
+    int remaining = len;
+    int offset = off;
+
+    while (remaining > 0) {
+      int freeBlockBytes = Ciphers.PLAIN_BLOCK_SIZE - positionInBuffer;
+      int toWrite = freeBlockBytes <= remaining ? freeBlockBytes : remaining;
+
+      System.arraycopy(b, offset, plainBlockBuffer, positionInBuffer, toWrite);
+      positionInBuffer += toWrite;
+      if (positionInBuffer == Ciphers.PLAIN_BLOCK_SIZE) {
+        encryptAndWriteBlock();
+        positionInBuffer = 0;
+      }
+      offset += toWrite;
+      remaining -= toWrite;
+    }
+
+    streamPosition += len;
+  }
+
+  @Override
+  public long getPos() throws IOException {
+    return streamPosition;
+  }
+
+  @Override
+  public void flush() throws IOException {
+    targetStream.flush();
+  }
+
+  @Override
+  public void close() throws IOException {
+    if (positionInBuffer > 0) {
+      encryptAndWriteBlock();
+    }
+    targetStream.close();
+  }
+
+  private void encryptAndWriteBlock() throws IOException {
+    if (currentBlockIndex == Integer.MAX_VALUE) {
+      throw new IOException("Too many blocks - exceed Integer.MAX_VALUE");
+    }
+
+    byte[] aad = Ciphers.streamBlockAAD(fileAadPrefix, currentBlockIndex);
+    byte[] cipherBlockBuffer = gcmEncryptor.encrypt(plainBlockBuffer, 0, 
positionInBuffer, aad);
+    currentBlockIndex++;
+    targetStream.write(cipherBlockBuffer);

Review Comment:
   I think that this should reset `positionInBuffer` instead of doing it in the 
caller.



-- 
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]

Reply via email to