|
21 | 21 | import com.google.common.base.Suppliers; |
22 | 22 | import com.google.common.collect.ImmutableSet; |
23 | 23 | import com.google.common.collect.ImmutableSortedSet; |
| 24 | +import com.google.common.io.BaseEncoding; |
| 25 | +import com.palantir.logsafe.Preconditions; |
24 | 26 | import com.palantir.logsafe.SafeArg; |
| 27 | +import com.palantir.logsafe.UnsafeArg; |
25 | 28 | import com.palantir.logsafe.exceptions.SafeIllegalStateException; |
26 | 29 | import com.palantir.logsafe.logger.SafeLogger; |
27 | 30 | import com.palantir.logsafe.logger.SafeLoggerFactory; |
|
31 | 34 | import java.nio.file.Files; |
32 | 35 | import java.nio.file.Path; |
33 | 36 | import java.nio.file.Paths; |
| 37 | +import java.security.GeneralSecurityException; |
| 38 | +import java.security.NoSuchProviderException; |
34 | 39 | import java.util.Arrays; |
35 | 40 | import java.util.Comparator; |
36 | 41 | import java.util.Objects; |
| 42 | +import java.util.Random; |
37 | 43 | import java.util.Set; |
| 44 | +import java.util.concurrent.ThreadLocalRandom; |
38 | 45 | import java.util.function.BooleanSupplier; |
39 | 46 | import java.util.function.Supplier; |
40 | 47 | import java.util.stream.Stream; |
41 | 48 | import javax.annotation.Nullable; |
| 49 | +import javax.crypto.Cipher; |
| 50 | +import javax.crypto.spec.IvParameterSpec; |
| 51 | +import javax.crypto.spec.SecretKeySpec; |
42 | 52 |
|
43 | 53 | /** |
44 | 54 | * Determine if JVM is impacted by https://bugs.openjdk.org/browse/JDK-8292158 which can corrupt AES-CTR encryption |
@@ -100,6 +110,9 @@ static boolean isAffectedByJdkAesCtrCorruption(Version version, String architect |
100 | 110 | @SuppressWarnings("checkstyle:CyclomaticComplexity") |
101 | 111 | static boolean isAffectedByJdkAesCtrCorruption( |
102 | 112 | Version version, String architecture, Info info, BooleanSupplier cpuHasAvx512) { |
| 113 | + if (isAesCtrBroken()) { |
| 114 | + return true; |
| 115 | + } |
103 | 116 | int featureVersion = version.feature(); |
104 | 117 | if (featureVersion >= 20) { |
105 | 118 | // https://git.openjdk.org/jdk/commit/9d76ac8a4453bc51d9dca2ad6c60259cfb2c4203 in jdk-20+17 |
@@ -196,4 +209,76 @@ static boolean hasVectorizedAesCpu(Stream<String> lines) { |
196 | 209 | .collect(ImmutableSortedSet.toImmutableSortedSet(Comparator.naturalOrder())); |
197 | 210 | return flags.containsAll(jdk8292158ImpactedCpuFlags); |
198 | 211 | } |
| 212 | + |
| 213 | + @VisibleForTesting |
| 214 | + static boolean isAesCtrBroken() { |
| 215 | + try { |
| 216 | + for (int i = 8; i <= 32; i++) { |
| 217 | + testEncryptDecrypt(i); |
| 218 | + } |
| 219 | + return false; |
| 220 | + } catch (NoSuchProviderException e) { |
| 221 | + log.warn("AES-CTR test failed due to no such provider", e); |
| 222 | + return false; |
| 223 | + } catch (GeneralSecurityException | Error | RuntimeException e) { |
| 224 | + log.error("AES-CTR AES-CTR encryption/decryption round-trip failed", e); |
| 225 | + return true; |
| 226 | + } |
| 227 | + } |
| 228 | + |
| 229 | + static void testEncryptDecrypt(int length) throws GeneralSecurityException { |
| 230 | + Preconditions.checkArgument(length > 4, "length must be at least 4"); |
| 231 | + |
| 232 | + long seed = ThreadLocalRandom.current().nextLong(); |
| 233 | + if (log.isDebugEnabled()) { |
| 234 | + log.debug( |
| 235 | + "Testing AES-CTR encryption/decryption for JDK-829158", |
| 236 | + SafeArg.of("seed", seed), |
| 237 | + SafeArg.of("length", length)); |
| 238 | + } |
| 239 | + |
| 240 | + Random random = new Random(seed); |
| 241 | + |
| 242 | + byte[] key = new byte[32]; |
| 243 | + random.nextBytes(key); |
| 244 | + SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES"); |
| 245 | + |
| 246 | + byte[] iv = new byte[16]; |
| 247 | + random.nextBytes(iv); |
| 248 | + IvParameterSpec ivParameterSpec = new IvParameterSpec(iv); |
| 249 | + |
| 250 | + Cipher encrypt = Cipher.getInstance("AES/CTR/NoPadding"); |
| 251 | + encrypt.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec); |
| 252 | + |
| 253 | + Cipher decrypt = Cipher.getInstance("AES/CTR/NoPadding"); |
| 254 | + decrypt.init(Cipher.DECRYPT_MODE, secretKeySpec, ivParameterSpec); |
| 255 | + |
| 256 | + byte[] cleartext = new byte[length]; |
| 257 | + byte[] encrypted = new byte[length]; |
| 258 | + byte[] decrypted = new byte[length]; |
| 259 | + |
| 260 | + for (int i = 0; i < 10_000; i++) { |
| 261 | + random.nextBytes(cleartext); |
| 262 | + encrypt.doFinal(cleartext, 0, length, encrypted); |
| 263 | + |
| 264 | + // use decrypt cipher at least 3 times |
| 265 | + decrypt.update(encrypted, 0, 1, decrypted, 0); |
| 266 | + decrypt.update(encrypted, 1, 1, decrypted, 1); |
| 267 | + decrypt.doFinal(encrypted, 2, length - 2, decrypted, 2); |
| 268 | + |
| 269 | + if (!Arrays.equals(cleartext, decrypted)) { |
| 270 | + throw new SafeIllegalStateException( |
| 271 | + "AES-CTR encryption/decryption round trip failed", |
| 272 | + cannotEncryptAesCtrSafely(), |
| 273 | + SafeArg.of("seed", seed), |
| 274 | + SafeArg.of("length", length), |
| 275 | + SafeArg.of("iteration", i), |
| 276 | + UnsafeArg.of("cleartext", BaseEncoding.base16().encode(cleartext)), |
| 277 | + UnsafeArg.of("decrypted", BaseEncoding.base16().encode(decrypted)), |
| 278 | + UnsafeArg.of("encrypted", BaseEncoding.base16().encode(encrypted)), |
| 279 | + UnsafeArg.of("key", BaseEncoding.base16().encode(key)), |
| 280 | + UnsafeArg.of("iv", BaseEncoding.base16().encode(iv))); |
| 281 | + } |
| 282 | + } |
| 283 | + } |
199 | 284 | } |
0 commit comments