From 5c12dc8e1515b6fb67439acd5e722349d435f06c Mon Sep 17 00:00:00 2001 From: Tim203 Date: Thu, 1 Apr 2021 00:37:58 +0200 Subject: [PATCH] Don't use a blocking algorithm for generating keys on unix-like systems This should fix GeyserMC/Floodgate#125 --- .../geysermc/floodgate/crypto/AesKeyProducer.java | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/common/src/main/java/org/geysermc/floodgate/crypto/AesKeyProducer.java b/common/src/main/java/org/geysermc/floodgate/crypto/AesKeyProducer.java index 5217b4cf7..bb2be89f8 100644 --- a/common/src/main/java/org/geysermc/floodgate/crypto/AesKeyProducer.java +++ b/common/src/main/java/org/geysermc/floodgate/crypto/AesKeyProducer.java @@ -29,7 +29,9 @@ package org.geysermc.floodgate.crypto; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; +import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; +import java.util.Locale; public final class AesKeyProducer implements KeyProducer { public static int KEY_SIZE = 128; @@ -38,7 +40,7 @@ public final class AesKeyProducer implements KeyProducer { public SecretKey produce() { try { KeyGenerator keyGenerator = KeyGenerator.getInstance("AES"); - keyGenerator.init(KEY_SIZE, SecureRandom.getInstanceStrong()); + keyGenerator.init(KEY_SIZE, getSecureRandom()); return keyGenerator.generateKey(); } catch (Exception exception) { throw new RuntimeException(exception); @@ -53,4 +55,14 @@ public final class AesKeyProducer implements KeyProducer { throw new RuntimeException(exception); } } + + private SecureRandom getSecureRandom() throws NoSuchAlgorithmException { + // use Windows-PRNG for windows (default impl is SHA1PRNG) + // default impl for unix-like systems is NativePRNG. + if (System.getProperty("os.name").toLowerCase(Locale.ROOT).contains("win")) { + return SecureRandom.getInstance("Windows-PRNG"); + } else { + return new SecureRandom(); + } + } }