RussellSpitzer commented on code in PR #3231: URL: https://github.com/apache/iceberg/pull/3231#discussion_r904337068
########## core/src/main/java/org/apache/iceberg/encryption/AesGcmInputStream.java: ########## @@ -0,0 +1,212 @@ +/* + * 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 java.security.GeneralSecurityException; +import java.util.Arrays; +import javax.crypto.Cipher; +import javax.crypto.SecretKey; +import javax.crypto.spec.GCMParameterSpec; +import javax.crypto.spec.SecretKeySpec; +import org.apache.iceberg.io.SeekableInputStream; +import org.apache.iceberg.relocated.com.google.common.base.Preconditions; + +public class AesGcmInputStream extends SeekableInputStream { + private SeekableInputStream sourceStream; + private long netSourceFileSize; + + private Cipher gcmCipher; + private SecretKey key; + private byte[] nonce; + + private byte[] ciphertextBlockBuffer; + private int cipherBlockSize; + private int plainBlockSize; + private long plainStreamPosition; + private int currentBlockIndex; + private int currentOffsetInPlainBlock; + private int numberOfBlocks; + private int lastBlockSize; + private long plainStreamSize; + private byte[] fileAadPrefix; + + AesGcmInputStream(SeekableInputStream sourceStream, long sourceLength, + byte[] aesKey, byte[] fileAadPrefix) throws IOException { + this.netSourceFileSize = sourceLength - AesGcmOutputStream.PREFIX_LENGTH; + this.sourceStream = sourceStream; + byte[] prefixBytes = new byte[AesGcmOutputStream.PREFIX_LENGTH]; + int fetched = sourceStream.read(prefixBytes); + Preconditions.checkArgument(fetched == AesGcmOutputStream.PREFIX_LENGTH, + "Insufficient read " + fetched); + this.plainStreamPosition = 0; + this.fileAadPrefix = fileAadPrefix; + + byte[] magic = new byte[AesGcmOutputStream.MAGIC_ARRAY.length]; + System.arraycopy(prefixBytes, 0, magic, 0, AesGcmOutputStream.MAGIC_ARRAY.length); + + Preconditions.checkArgument(Arrays.equals(AesGcmOutputStream.MAGIC_ARRAY, magic), + "File with wrong magic string. Should start with " + AesGcmOutputStream.MAGIC_STRING); + + plainBlockSize = ByteBuffer.wrap(prefixBytes, AesGcmOutputStream.MAGIC_ARRAY.length, 4) + .order(ByteOrder.LITTLE_ENDIAN).getInt(); + cipherBlockSize = plainBlockSize + AesGcmOutputStream.GCM_NONCE_LENGTH + AesGcmOutputStream.GCM_TAG_LENGTH; + + try { + gcmCipher = Cipher.getInstance("AES/GCM/NoPadding"); + } catch (GeneralSecurityException e) { + throw new IOException(e); + } + this.nonce = new byte[AesGcmOutputStream.GCM_NONCE_LENGTH]; + this.key = new SecretKeySpec(aesKey, "AES"); + this.ciphertextBlockBuffer = new byte[cipherBlockSize]; + this.currentBlockIndex = 0; + this.currentOffsetInPlainBlock = 0; + + numberOfBlocks = (int) (netSourceFileSize / cipherBlockSize); + lastBlockSize = (int) (netSourceFileSize % cipherBlockSize); + if (lastBlockSize == 0) { + lastBlockSize = cipherBlockSize; + } else { + numberOfBlocks += 1; + } + + plainStreamSize = (numberOfBlocks - 1L) * plainBlockSize + + (lastBlockSize - AesGcmOutputStream.GCM_NONCE_LENGTH - AesGcmOutputStream.GCM_TAG_LENGTH); + } + + public long plaintextStreamSize() { + return plainStreamSize; + } + + @Override + public int available() throws IOException { + return Math.toIntExact(plainStreamSize - plainStreamPosition); + } + + @Override + public int read(byte[] b) throws IOException { + return read(b, 0, b.length); + } + + @Override + public int read(byte[] b, int off, int len) throws IOException { + if (len <= 0) { + throw new IOException("Negative read length " + len); + } + + if (available() <= 0) { + return -1; + } + + boolean lastBlock = currentBlockIndex + 1 == numberOfBlocks; + int resultBufferOffset = off; + int remaining = len; + + sourceStream.seek(AesGcmOutputStream.PREFIX_LENGTH + currentBlockIndex * cipherBlockSize); + + while (remaining > 0) { + int toLoad = lastBlock ? lastBlockSize : cipherBlockSize; Review Comment: This is part of the code I think is kind of complicated because of the special casing of the last block logic. Let's try to make this as clear as possible. Do we actually need to keep these numbers here? I was thinking about this again, could we instead do something like ```java Math.min(cipherBlockSize, remaining) ``` -- 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]
