ggershinsky commented on code in PR #3231: URL: https://github.com/apache/iceberg/pull/3231#discussion_r1041943081
########## core/src/main/java/org/apache/iceberg/encryption/AesGcmInputStream.java: ########## @@ -0,0 +1,218 @@ +/* + * 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.io.UncheckedIOException; +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.util.Arrays; +import org.apache.iceberg.io.SeekableInputStream; +import org.apache.iceberg.relocated.com.google.common.base.Preconditions; + +public class AesGcmInputStream extends SeekableInputStream { + private final SeekableInputStream sourceStream; + private final boolean emptyCipherStream; + private final long netSourceFileSize; + private final Ciphers.AesGcmDecryptor gcmDecryptor; + private final byte[] ciphertextBlockBuffer; + private final int cipherBlockSize; + private final int plainBlockSize; + private final int numberOfBlocks; + private final int lastCipherBlockSize; + private final long plainStreamSize; + private final byte[] fileAadPrefix; + + private long plainStreamPosition; + private int currentBlockIndex; + private int currentOffsetInPlainBlock; + + AesGcmInputStream(SeekableInputStream sourceStream, long sourceLength, + byte[] aesKey, byte[] fileAadPrefix) throws IOException { + this.netSourceFileSize = sourceLength - Ciphers.GCM_STREAM_PREFIX_LENGTH; + Preconditions.checkArgument(netSourceFileSize >= 0, + "Source length " + sourceLength + " is shorter than GCM prefix. File is not encrypted"); + + this.emptyCipherStream = (0 == netSourceFileSize); + this.sourceStream = sourceStream; + byte[] prefixBytes = new byte[Ciphers.GCM_STREAM_PREFIX_LENGTH]; + int fetched = sourceStream.read(prefixBytes); + Preconditions.checkState(fetched == Ciphers.GCM_STREAM_PREFIX_LENGTH, + "Insufficient read " + fetched + + ". The stream length should be at least " + Ciphers.GCM_STREAM_PREFIX_LENGTH); + + byte[] magic = new byte[Ciphers.GCM_STREAM_MAGIC_ARRAY.length]; + System.arraycopy(prefixBytes, 0, magic, 0, Ciphers.GCM_STREAM_MAGIC_ARRAY.length); + Preconditions.checkState(Arrays.equals(Ciphers.GCM_STREAM_MAGIC_ARRAY, magic), + "Cannot open encrypted file, it does not begin with magic string " + Ciphers.GCM_STREAM_MAGIC_STRING); + + if (!emptyCipherStream) { + this.plainStreamPosition = 0; + this.fileAadPrefix = fileAadPrefix; + gcmDecryptor = new Ciphers.AesGcmDecryptor(aesKey); + plainBlockSize = ByteBuffer.wrap(prefixBytes, Ciphers.GCM_STREAM_MAGIC_ARRAY.length, 4) + .order(ByteOrder.LITTLE_ENDIAN).getInt(); + Preconditions.checkState(plainBlockSize > 0, "Wrong plainBlockSize " + plainBlockSize); + + cipherBlockSize = plainBlockSize + Ciphers.NONCE_LENGTH + Ciphers.GCM_TAG_LENGTH; + this.ciphertextBlockBuffer = new byte[cipherBlockSize]; + this.currentBlockIndex = 0; + this.currentOffsetInPlainBlock = 0; + + int numberOfFullBlocks = Math.toIntExact(netSourceFileSize / cipherBlockSize); + int cipherBytesInLastBlock = Math.toIntExact(netSourceFileSize - numberOfFullBlocks * cipherBlockSize); + boolean fullBlocksOnly = (0 == cipherBytesInLastBlock); + numberOfBlocks = fullBlocksOnly ? numberOfFullBlocks : numberOfFullBlocks + 1; + lastCipherBlockSize = fullBlocksOnly ? cipherBlockSize : cipherBytesInLastBlock; // never 0 + int plainBytesInLastBlock = fullBlocksOnly ? 0 : + (cipherBytesInLastBlock - Ciphers.NONCE_LENGTH - Ciphers.GCM_TAG_LENGTH); Review Comment: This is the last block, so its length will be variable and shorter than the block size constant. -- 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: issues-unsubscr...@iceberg.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org For additional commands, e-mail: issues-h...@iceberg.apache.org