Skip to content

Commit a456e7b

Browse files
fixed findings from code review
[deploy]
1 parent 4546454 commit a456e7b

File tree

3 files changed

+8
-7
lines changed

3 files changed

+8
-7
lines changed

src/main/java/org/cryptomator/siv/CMac.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ protected int engineGetMacLength() {
4545

4646
@Override
4747
protected void engineInit(Key key, AlgorithmParameterSpec params) throws InvalidKeyException {
48+
engineReset();
4849
try {
4950
this.cipher = Cipher.getInstance(AES_ECB_NO_PADDING);
5051
} catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
@@ -63,9 +64,6 @@ protected void engineInit(Key key, AlgorithmParameterSpec params) throws Invalid
6364
} finally {
6465
Arrays.fill(L, (byte) 0);
6566
}
66-
67-
// reset state
68-
engineReset();
6967
}
7068

7169
@Override

src/main/java/org/cryptomator/siv/SivCipher.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ protected int engineGetBlockSize() {
5454
@Override
5555
protected int engineGetOutputSize(int inputLen) {
5656
if (opmode == Cipher.ENCRYPT_MODE || opmode == Cipher.WRAP_MODE) {
57-
return 16 + inputLen;
57+
return 16 + inputBuffer.length + inputLen;
5858
} else if (opmode == Cipher.DECRYPT_MODE || opmode == Cipher.UNWRAP_MODE) {
59-
return inputLen - 16;
59+
return inputBuffer.length + inputLen - 16;
6060
} else {
6161
throw new IllegalStateException("Invalid opmode " + this.opmode);
6262
}
@@ -123,7 +123,7 @@ protected int engineUpdate(byte[] input, int inputOffset, int inputLen, byte[] o
123123

124124
@Override
125125
protected byte[] engineDoFinal(byte[] input, int inputOffset, int inputLen) throws IllegalBlockSizeException, BadPaddingException {
126-
int outputSize = engineGetOutputSize(inputBuffer.length + inputLen);
126+
int outputSize = engineGetOutputSize(inputLen);
127127
if (outputSize < 0) {
128128
throw new IllegalBlockSizeException("Ciphertext too short (must be at least 16 bytes including SIV tag)");
129129
}
@@ -139,7 +139,7 @@ protected byte[] engineDoFinal(byte[] input, int inputOffset, int inputLen) thro
139139

140140
@Override
141141
protected int engineDoFinal(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset) throws ShortBufferException, IllegalBlockSizeException, BadPaddingException {
142-
int outputSize = engineGetOutputSize(inputBuffer.length + inputLen);
142+
int outputSize = engineGetOutputSize(inputLen);
143143
if (outputSize < 0) {
144144
throw new IllegalBlockSizeException("Ciphertext too short (must be at least 16 bytes including SIV tag)");
145145
}

src/main/java/org/cryptomator/siv/Utils.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ public class Utils {
1313

1414
// First bit 1, following bits 0.
1515
static byte[] pad(byte[] in, int desiredLength) {
16+
if (in.length >= desiredLength) {
17+
throw new IllegalArgumentException("pad() expects input shorter than desiredLength");
18+
}
1619
final byte[] result = Arrays.copyOf(in, desiredLength);
1720
result[in.length] = (byte) 0x80;
1821
return result;

0 commit comments

Comments
 (0)