Skip to content

Commit bb3153d

Browse files
bugfix: Fix slow AES encryption (#746)
* bugfix: Fix slow AES encryption * Fix copyright headers
1 parent b1ee22c commit bb3153d

File tree

10 files changed

+51
-17
lines changed

10 files changed

+51
-17
lines changed

edc-controlplane/edc-controlplane-memory-hashicorp-vault/src/main/docker/Dockerfile

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#
2+
# Copyright (c) 2023 ZF Friedrichshafen AG
23
# Copyright (c) 2022 Mercedes-Benz Tech Innovation GmbH
34
# Copyright (c) 2021,2022 Contributors to the Eclipse Foundation
45
#
@@ -64,6 +65,6 @@ CMD ["java", \
6465
"-Dotel.javaagent.configuration-file=/app/opentelemetry.properties", \
6566
"-Dotel.metrics.exporter=prometheus", \
6667
"-Dotel.exporter.prometheus.port=9090", \
67-
"-Djava.security.edg=file:/dev/.urandom", \
68+
"-Djava.security.egd=file:/dev/urandom", \
6869
"-jar", \
6970
"edc-controlplane.jar"]

edc-controlplane/edc-controlplane-memory/src/main/docker/Dockerfile

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#
2+
# Copyright (c) 2023 ZF Friedrichshafen AG
23
# Copyright (c) 2022 Mercedes-Benz Tech Innovation GmbH
34
# Copyright (c) 2021,2022 Contributors to the Eclipse Foundation
45
#
@@ -64,6 +65,6 @@ CMD ["java", \
6465
"-Dotel.javaagent.configuration-file=/app/opentelemetry.properties", \
6566
"-Dotel.metrics.exporter=prometheus", \
6667
"-Dotel.exporter.prometheus.port=9090", \
67-
"-Djava.security.edg=file:/dev/.urandom", \
68+
"-Djava.security.egd=file:/dev/urandom", \
6869
"-jar", \
6970
"edc-controlplane.jar"]

edc-controlplane/edc-controlplane-postgresql-hashicorp-vault/src/main/docker/Dockerfile

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#
2+
# Copyright (c) 2023 ZF Friedrichshafen AG
23
# Copyright (c) 2022 Mercedes-Benz Tech Innovation GmbH
34
# Copyright (c) 2021,2022 Contributors to the Eclipse Foundation
45
#
@@ -64,6 +65,6 @@ CMD ["java", \
6465
"-Dotel.javaagent.configuration-file=/app/opentelemetry.properties", \
6566
"-Dotel.metrics.exporter=prometheus", \
6667
"-Dotel.exporter.prometheus.port=9090", \
67-
"-Djava.security.edg=file:/dev/.urandom", \
68+
"-Djava.security.egd=file:/dev/urandom", \
6869
"-jar", \
6970
"edc-controlplane.jar"]

edc-controlplane/edc-controlplane-postgresql/src/main/docker/Dockerfile

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#
2+
# Copyright (c) 2023 ZF Friedrichshafen AG
23
# Copyright (c) 2022 Mercedes-Benz Tech Innovation GmbH
34
# Copyright (c) 2021,2022 Contributors to the Eclipse Foundation
45
#
@@ -64,6 +65,6 @@ CMD ["java", \
6465
"-Dotel.javaagent.configuration-file=/app/opentelemetry.properties", \
6566
"-Dotel.metrics.exporter=prometheus", \
6667
"-Dotel.exporter.prometheus.port=9090", \
67-
"-Djava.security.edg=file:/dev/.urandom", \
68+
"-Djava.security.egd=file:/dev/urandom", \
6869
"-jar", \
6970
"edc-controlplane.jar"]

edc-dataplane/edc-dataplane-azure-vault/src/main/docker/Dockerfile

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#
2+
# Copyright (c) 2023 ZF Friedrichshafen AG
23
# Copyright (c) 2022 Mercedes-Benz Tech Innovation GmbH
34
# Copyright (c) 2021,2022 Contributors to the Eclipse Foundation
45
#
@@ -64,6 +65,6 @@ CMD ["java", \
6465
"-Dotel.javaagent.configuration-file=/app/opentelemetry.properties", \
6566
"-Dotel.metrics.exporter=prometheus", \
6667
"-Dotel.exporter.prometheus.port=9090", \
67-
"-Djava.security.edg=file:/dev/.urandom", \
68+
"-Djava.security.egd=file:/dev/urandom", \
6869
"-jar", \
6970
"edc-dataplane.jar"]

edc-dataplane/edc-dataplane-hashicorp-vault/src/main/docker/Dockerfile

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#
2+
# Copyright (c) 2023 ZF Friedrichshafen AG
23
# Copyright (c) 2022 Mercedes-Benz Tech Innovation GmbH
34
# Copyright (c) 2021,2022 Contributors to the Eclipse Foundation
45
#
@@ -64,6 +65,6 @@ CMD ["java", \
6465
"-Dotel.javaagent.configuration-file=/app/opentelemetry.properties", \
6566
"-Dotel.metrics.exporter=prometheus", \
6667
"-Dotel.exporter.prometheus.port=9090", \
67-
"-Djava.security.edg=file:/dev/.urandom", \
68+
"-Djava.security.egd=file:/dev/urandom", \
6869
"-jar", \
6970
"edc-dataplane.jar"]

edc-extensions/data-encryption/src/main/java/org/eclipse/tractusx/edc/data/encryption/algorithms/aes/AesAlgorithm.java

+20-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/*
2+
* Copyright (c) 2023 ZF Friedrichshafen AG
23
* Copyright (c) 2022 Mercedes-Benz Tech Innovation GmbH
34
* Copyright (c) 2021,2022 Contributors to the Eclipse Foundation
45
*
@@ -22,33 +23,45 @@
2223
import java.security.InvalidAlgorithmParameterException;
2324
import java.security.InvalidKeyException;
2425
import java.security.NoSuchAlgorithmException;
26+
import java.security.SecureRandom;
2527
import javax.crypto.BadPaddingException;
2628
import javax.crypto.Cipher;
2729
import javax.crypto.IllegalBlockSizeException;
2830
import javax.crypto.NoSuchPaddingException;
2931
import javax.crypto.spec.GCMParameterSpec;
3032
import javax.crypto.spec.SecretKeySpec;
3133
import lombok.NonNull;
34+
import lombok.SneakyThrows;
3235
import org.bouncycastle.jce.provider.BouncyCastleProvider;
3336
import org.eclipse.tractusx.edc.data.encryption.algorithms.CryptoAlgorithm;
3437
import org.eclipse.tractusx.edc.data.encryption.data.CryptoDataFactory;
3538
import org.eclipse.tractusx.edc.data.encryption.data.DecryptedData;
3639
import org.eclipse.tractusx.edc.data.encryption.data.EncryptedData;
3740
import org.eclipse.tractusx.edc.data.encryption.key.AesKey;
3841
import org.eclipse.tractusx.edc.data.encryption.util.ArrayUtil;
42+
import org.jetbrains.annotations.NotNull;
3943

4044
public class AesAlgorithm implements CryptoAlgorithm<AesKey> {
4145

4246
private static final String AES_GCM = "AES/GCM/NoPadding";
4347
private static final String AES = "AES";
4448
private static final Object MONITOR = new Object();
4549

50+
private final SecureRandom secureRandom;
51+
4652
@NonNull private final CryptoDataFactory cryptoDataFactory;
4753
private AesInitializationVectorIterator initializationVectorIterator;
4854

49-
public AesAlgorithm(CryptoDataFactory cryptoDataFactory) {
55+
@SneakyThrows
56+
public AesAlgorithm(@NotNull CryptoDataFactory cryptoDataFactory) {
5057
this.cryptoDataFactory = cryptoDataFactory;
51-
this.initializationVectorIterator = new AesInitializationVectorIterator();
58+
59+
// We use new SecureRandom() and not SecureRandom.getInstanceStrong(), as the second one
60+
// would use a blocking algorithm, which leads to an increased encryption time of up to 3
61+
// minutes. Since we have already used /dev/urandom, which only provides pseudo-randomness and
62+
// is also non-blocking, switching to a non-blocking algorithm should not matter here either.
63+
this.secureRandom = new SecureRandom();
64+
this.initializationVectorIterator = new AesInitializationVectorIterator(this.secureRandom);
5265
}
5366

5467
@Override
@@ -59,7 +72,7 @@ public synchronized EncryptedData encrypt(DecryptedData data, AesKey key)
5972
final byte[] initializationVector;
6073
synchronized (MONITOR) {
6174
if (!initializationVectorIterator.hasNext()) {
62-
initializationVectorIterator = new AesInitializationVectorIterator();
75+
initializationVectorIterator = new AesInitializationVectorIterator(this.secureRandom);
6376
}
6477

6578
initializationVector = initializationVectorIterator.next();
@@ -92,4 +105,8 @@ public DecryptedData decrypt(EncryptedData data, AesKey key)
92105
byte[] decryptedData = cipher.doFinal(encrypted);
93106
return cryptoDataFactory.decryptedFromBytes(decryptedData);
94107
}
108+
109+
public String getAlgorithm() {
110+
return this.secureRandom.getAlgorithm();
111+
}
95112
}

edc-extensions/data-encryption/src/main/java/org/eclipse/tractusx/edc/data/encryption/algorithms/aes/AesInitializationVectorIterator.java

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/*
2+
* Copyright (c) 2023 ZF Friedrichshafen AG
23
* Copyright (c) 2022 Mercedes-Benz Tech Innovation GmbH
34
* Copyright (c) 2021,2022 Contributors to the Eclipse Foundation
45
*
@@ -29,12 +30,14 @@ public class AesInitializationVectorIterator implements Iterator<byte[]> {
2930

3031
public static final int RANDOM_SIZE = 12;
3132
public static final int COUNTER_SIZE = 4;
32-
public static final int VECTOR_SIZE = RANDOM_SIZE + COUNTER_SIZE;
3333

3434
private final ByteCounter counter;
3535

36-
public AesInitializationVectorIterator() {
37-
counter = new ByteCounter(COUNTER_SIZE);
36+
private SecureRandom secureRandom;
37+
38+
public AesInitializationVectorIterator(SecureRandom secureRandom) {
39+
this.counter = new ByteCounter(COUNTER_SIZE);
40+
this.secureRandom = secureRandom;
3841
}
3942

4043
public AesInitializationVectorIterator(ByteCounter byteCounter) {
@@ -60,9 +63,8 @@ public byte[] next() {
6063

6164
@SneakyThrows
6265
public byte[] getNextRandom() {
63-
SecureRandom random = SecureRandom.getInstanceStrong();
6466
byte[] newVector = new byte[RANDOM_SIZE];
65-
random.nextBytes(newVector);
67+
secureRandom.nextBytes(newVector);
6668
return newVector;
6769
}
6870
}

edc-extensions/data-encryption/src/main/java/org/eclipse/tractusx/edc/data/encryption/encrypter/DataEncrypterFactory.java

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/*
2+
* Copyright (c) 2023 ZF Friedrichshafen AG
23
* Copyright (c) 2022 Mercedes-Benz Tech Innovation GmbH
34
* Copyright (c) 2021,2022 Contributors to the Eclipse Foundation
45
*
@@ -20,11 +21,12 @@
2021

2122
package org.eclipse.tractusx.edc.data.encryption.encrypter;
2223

24+
import static java.lang.String.format;
25+
2326
import lombok.RequiredArgsConstructor;
2427
import org.eclipse.edc.connector.transfer.dataplane.spi.security.DataEncrypter;
2528
import org.eclipse.edc.spi.monitor.Monitor;
2629
import org.eclipse.edc.spi.security.Vault;
27-
import org.eclipse.tractusx.edc.data.encryption.algorithms.CryptoAlgorithm;
2830
import org.eclipse.tractusx.edc.data.encryption.algorithms.aes.AesAlgorithm;
2931
import org.eclipse.tractusx.edc.data.encryption.data.CryptoDataFactory;
3032
import org.eclipse.tractusx.edc.data.encryption.data.CryptoDataFactoryImpl;
@@ -67,8 +69,12 @@ public DataEncrypter createAesEncrypter(AesDataEncrypterConfiguration configurat
6769
}
6870

6971
final CryptoDataFactory cryptoDataFactory = new CryptoDataFactoryImpl();
70-
final CryptoAlgorithm<AesKey> algorithm = new AesAlgorithm(cryptoDataFactory);
72+
final AesAlgorithm algorithm = new AesAlgorithm(cryptoDataFactory);
7173

74+
monitor.debug(
75+
format(
76+
"AES algorithm was initialised with SecureRandom algorithm '%s'",
77+
algorithm.getAlgorithm()));
7278
return new AesDataEncrypterImpl(algorithm, monitor, keyProvider, algorithm, cryptoDataFactory);
7379
}
7480
}

edc-extensions/data-encryption/src/test/java/org/eclipse/tractusx/edc/data/encryption/algorithms/aes/AesInitializationVectorIteratorTest.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/*
2+
* Copyright (c) 2023 ZF Friedrichshafen AG
23
* Copyright (c) 2022 Mercedes-Benz Tech Innovation GmbH
34
* Copyright (c) 2021,2022 Contributors to the Eclipse Foundation
45
*
@@ -19,6 +20,7 @@
1920
*/
2021
package org.eclipse.tractusx.edc.data.encryption.algorithms.aes;
2122

23+
import java.security.SecureRandom;
2224
import java.util.ArrayList;
2325
import java.util.List;
2426
import java.util.NoSuchElementException;
@@ -34,7 +36,8 @@ class AesInitializationVectorIteratorTest {
3436
@SneakyThrows
3537
void testDistinctVectors() {
3638
final int vectorCount = 100;
37-
AesInitializationVectorIterator iterator = new AesInitializationVectorIterator();
39+
final SecureRandom secureRandom = new SecureRandom();
40+
AesInitializationVectorIterator iterator = new AesInitializationVectorIterator(secureRandom);
3841

3942
List<byte[]> vectors = new ArrayList<>();
4043
for (var i = 0; i < vectorCount; i++) {

0 commit comments

Comments
 (0)