Fix ByteContainer.setBytes to reallocate data when the incoming length differs from the currently stored length. Upstream licel/jcardsim master drops `this.length != length` from the reallocation guard; the DT fork kept it. Without the check, calling setKey(buf13, 0, 13) then setKey(buf64, 32, 32) on the same key reuses the already-sized-13 internal data[] and triggers ArrayIndexOutOfBoundsException in the subsequent Util.arrayCopy. This regression hits HKDF-Extract-then-Expand on the same HMACKey, which is exactly the shape Aliro §8.3.1.4 / §8.3.1.13 require. diff --git a/src/main/java/com/licel/jcardsim/crypto/ByteContainer.java b/src/main/java/com/licel/jcardsim/crypto/ByteContainer.java --- a/src/main/java/com/licel/jcardsim/crypto/ByteContainer.java +++ b/src/main/java/com/licel/jcardsim/crypto/ByteContainer.java @@ -106,7 +106,7 @@ public final class ByteContainer { * @param length length of data in byte array */ public void setBytes(byte[] buff, short offset, short length) { - if (data == null) { + if (data == null || this.length != length) { switch (memoryType) { case JCSystem.MEMORY_TYPE_TRANSIENT_DESELECT: data = JCSystem.makeTransientByteArray(length, JCSystem.CLEAR_ON_DESELECT);