Skip to content

Commit 0dbb289

Browse files
committed
Add key and IV length validation for AES operations, extract common cipher logic
1 parent 488e628 commit 0dbb289

3 files changed

Lines changed: 32 additions & 16 deletions

File tree

common/src/main/java/me/alexdevs/classicPeripherals/core/Crypto.java

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import org.bouncycastle.crypto.Digest;
44
import org.bouncycastle.crypto.InvalidCipherTextException;
5-
import org.bouncycastle.crypto.digests.*;
65
import org.bouncycastle.crypto.engines.AESEngine;
76
import org.bouncycastle.crypto.digests.MD5Digest;
87
import org.bouncycastle.crypto.digests.SHA1Digest;
@@ -14,6 +13,7 @@
1413
import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher;
1514
import org.bouncycastle.crypto.params.*;
1615
import org.bouncycastle.crypto.signers.Ed25519Signer;
16+
import org.jspecify.annotations.NonNull;
1717

1818
import javax.crypto.BadPaddingException;
1919
import java.nio.charset.Charset;
@@ -33,10 +33,6 @@ public static String toStr(byte[] bytes, boolean hex) {
3333
return new String(bytes, CHARSET);
3434
}
3535

36-
public static String toStr(byte[] bytes) {
37-
return toStr(bytes, true);
38-
}
39-
4036
private static String digest(Digest digest, String data, boolean hex) {
4137
digest.update(data.getBytes(CHARSET), 0, data.length());
4238
byte[] hash = new byte[digest.getDigestSize()];
@@ -217,28 +213,25 @@ public static String encryptAes(String data, String key, String iv) throws BadPa
217213
var ivBytes = iv.getBytes(CHARSET);
218214
var dataBytes = data.getBytes(CHARSET);
219215

220-
var cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), new PKCS7Padding());
216+
var cipher = new PaddedBufferedBlockCipher(CBCBlockCipher.newInstance(AESEngine.newInstance()), new PKCS7Padding());
221217
cipher.init(true, new ParametersWithIV(new KeyParameter(keyBytes), ivBytes));
222218

223-
var output = new byte[cipher.getOutputSize(dataBytes.length)];
224-
int len = cipher.processBytes(dataBytes, 0, dataBytes.length, output, 0);
225-
try {
226-
len += cipher.doFinal(output, len);
227-
} catch (InvalidCipherTextException e) {
228-
throw new BadPaddingException(e.getMessage());
229-
}
230-
231-
return new String(output, 0, len, CHARSET);
219+
return getString(dataBytes, cipher);
232220
}
233221

234222
public static String decryptAes(String data, String key, String iv) throws BadPaddingException {
235223
var keyBytes = key.getBytes(CHARSET);
236224
var ivBytes = iv.getBytes(CHARSET);
237225
var dataBytes = data.getBytes(CHARSET);
238226

239-
var cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), new PKCS7Padding());
227+
var cipher = new PaddedBufferedBlockCipher(CBCBlockCipher.newInstance(AESEngine.newInstance()), new PKCS7Padding());
240228
cipher.init(false, new ParametersWithIV(new KeyParameter(keyBytes), ivBytes));
241229

230+
return getString(dataBytes, cipher);
231+
}
232+
233+
@NonNull
234+
private static String getString(byte[] dataBytes, PaddedBufferedBlockCipher cipher) throws BadPaddingException {
242235
var output = new byte[cipher.getOutputSize(dataBytes.length)];
243236
int len = cipher.processBytes(dataBytes, 0, dataBytes.length, output, 0);
244237
try {

common/src/main/java/me/alexdevs/classicPeripherals/peripherals/crypto/AbstractCryptographicAcceleratorPeripheral.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ public final String decodeBase64(String data) {
107107

108108
@LuaFunction
109109
public final String encryptAes(String data, String key, String iv) throws LuaException {
110+
CryptoUtils.validateLength(1, key.length(), 16, 24, 32);
111+
CryptoUtils.validateLength(2, iv.length(), 16);
112+
110113
try {
111114
return Crypto.encryptAes(data, key, iv);
112115
} catch (BadPaddingException e) {
@@ -116,6 +119,9 @@ public final String encryptAes(String data, String key, String iv) throws LuaExc
116119

117120
@LuaFunction
118121
public final String decryptAes(String data, String key, String iv) throws LuaException {
122+
CryptoUtils.validateLength(1, key.length(), 16, 24, 32);
123+
CryptoUtils.validateLength(2, iv.length(), 16);
124+
119125
try {
120126
return Crypto.decryptAes(data, key, iv);
121127
} catch (BadPaddingException e) {

common/src/main/java/me/alexdevs/classicPeripherals/utils/CryptoUtils.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
import dan200.computercraft.api.lua.LuaException;
44

5+
import java.util.stream.Collectors;
6+
import java.util.stream.IntStream;
7+
58
public class CryptoUtils {
69
public static void validateKey(String key) throws LuaException {
710
if (key != null && key.length() != 32) {
@@ -14,4 +17,18 @@ public static void validateSignature(String key) throws LuaException {
1417
throw new LuaException("Invalid signature length, expected 64 bytes.");
1518
}
1619
}
20+
21+
public static void validateLength(int index, int value, int... lengths) throws LuaException {
22+
for (int len : lengths) {
23+
if (value == len) {
24+
return;
25+
}
26+
}
27+
28+
var expectedValues = IntStream.of(lengths)
29+
.mapToObj(Integer::toString)
30+
.collect(Collectors.joining(", "));
31+
32+
throw new LuaException("bad argument length #" + (index + 1) + " (expected " + expectedValues + ", got " + value + ")");
33+
}
1734
}

0 commit comments

Comments
 (0)