bruno-roustant commented on a change in pull request #1608:
URL: https://github.com/apache/lucene-solr/pull/1608#discussion_r448470152



##########
File path: 
lucene/core/src/java/org/apache/lucene/store/EncryptingIndexInput.java
##########
@@ -0,0 +1,299 @@
+/*
+ * 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.lucene.store;
+
+import javax.crypto.Cipher;
+import javax.crypto.ShortBufferException;
+import javax.crypto.spec.IvParameterSpec;
+import java.io.EOFException;
+import java.io.IOException;
+import java.io.UncheckedIOException;
+import java.nio.ByteBuffer;
+import java.security.InvalidAlgorithmParameterException;
+import java.security.InvalidKeyException;
+import java.security.Key;
+
+import org.apache.lucene.codecs.CodecUtil;
+
+import static org.apache.lucene.store.EncryptingIndexOutput.HEADER_LENGTH;
+import static org.apache.lucene.store.EncryptingUtil.*;
+
+public class EncryptingIndexInput extends IndexInput {
+
+  /**
+   * Must be a multiple of {@link EncryptingUtil#AES_BLOCK_SIZE}.
+   */
+  private static final int BUFFER_SIZE = 64 * AES_BLOCK_SIZE; // 1024 B
+
+  private static final long AES_BLOCK_SIZE_MOD_MASK = AES_BLOCK_SIZE - 1;
+  static final int HEADER_IV_LENGTH = HEADER_LENGTH + IV_LENGTH;
+
+  private static final byte[] EMPTY_BYTES = new byte[0];
+
+  // Some fields are not final for the clone() method.
+  private boolean isClone;
+  private final long sliceOffset;
+  private final long sliceEnd;
+  private IndexInput indexInput;
+  private final Key key;
+  private Cipher cipher;
+  private final byte[] initialIv;
+  private byte[] iv;
+  private ReusableIvParameterSpec ivParameterSpec;
+  private ByteBuffer inBuffer;
+  private ByteBuffer outBuffer;
+  private byte[] inArray;
+  private byte[] oneByteBuf;
+  private int padding;
+  private boolean closed;
+
+  public EncryptingIndexInput(IndexInput indexInput, byte[] key) throws 
IOException {
+    this("Decrypting " + indexInput.toString(),
+        HEADER_IV_LENGTH, indexInput.length() - HEADER_IV_LENGTH - 
CodecUtil.footerLength(), false,
+        indexInput, createAesKey(key), readInitialIv(indexInput));
+  }
+
+  private EncryptingIndexInput(String resourceDescription, long sliceOffset, 
long sliceLength, boolean isClone,
+                               IndexInput indexInput, Key key, byte[] 
initialIv) throws IOException {
+    super(resourceDescription);
+    assert sliceOffset >= 0 && sliceLength >= 0;
+    this.sliceOffset = sliceOffset;
+    this.sliceEnd = sliceOffset + sliceLength;
+    this.isClone = isClone;
+    this.indexInput = indexInput;
+    this.key = key;
+    this.initialIv = initialIv;
+
+    cipher = createAesCtrCipher();
+    assert cipher.getBlockSize() == AES_BLOCK_SIZE : "Invalid AES block size: 
" + cipher.getBlockSize();
+    iv = initialIv.clone();
+    ivParameterSpec = new ReusableIvParameterSpec(iv);
+    try {
+      cipher.init(Cipher.DECRYPT_MODE, this.key, ivParameterSpec);
+    } catch (InvalidKeyException | InvalidAlgorithmParameterException e) {
+      throw new IOException(e);
+    }
+
+    inBuffer = ByteBuffer.allocate(getBufferSize());
+    outBuffer = ByteBuffer.allocate(getBufferSize() + AES_BLOCK_SIZE);
+    outBuffer.limit(0);
+    assert inBuffer.hasArray() && outBuffer.hasArray();
+    assert inBuffer.arrayOffset() == 0;
+    inArray = inBuffer.array();
+    oneByteBuf = new byte[1];
+  }
+
+  /**
+   * Reads the initial IV at the beginning of the index input.
+   */
+  private static byte[] readInitialIv(IndexInput indexInput) throws 
IOException {
+    indexInput.seek(HEADER_LENGTH);
+    byte[] initialIv = new byte[IV_LENGTH];
+    indexInput.readBytes(initialIv, 0, initialIv.length, false);
+    return initialIv;
+  }
+
+  /**
+   * Gets the buffer size. It must be a multiple of {@link 
EncryptingUtil#AES_BLOCK_SIZE}.
+   */
+  protected int getBufferSize() {
+    return BUFFER_SIZE;
+  }
+
+  @Override
+  public void close() throws IOException {
+    if (!closed) {
+      closed = true;
+      if (!isClone) {
+        indexInput.close();
+      }
+    }
+  }
+
+  @Override
+  public long getFilePointer() {
+    return getPosition() - sliceOffset;
+  }
+
+  /**
+   * Gets the current internal position in the delegate {@link IndexInput}. It 
includes the initial IV length.
+   */
+  private long getPosition() {
+    return indexInput.getFilePointer() - outBuffer.remaining();
+  }
+
+  @Override
+  public void seek(long position) throws IOException {
+    if (position < 0) {
+      throw new IllegalArgumentException("Invalid position=" + position);
+    }
+    if (position > length()) {
+      throw new EOFException("Seek beyond EOF (position=" + position + ", 
length=" + length() + ") in " + this);
+    }
+    long targetPosition = position + sliceOffset;
+    long delegatePosition = indexInput.getFilePointer();
+    long currentPosition = delegatePosition - outBuffer.remaining();
+    if (targetPosition >= currentPosition && targetPosition <= 
delegatePosition) {
+      outBuffer.position(outBuffer.position() + (int) (targetPosition - 
currentPosition));

Review comment:
       The target position is within the buffered output, which has a small 
integer size. I'll add a comment here.




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

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org
For additional commands, e-mail: issues-h...@lucene.apache.org

Reply via email to