【问题标题】:secureRandom : Which urandom config is right non-blocking algorith for Linux in Java 8 and Java 11secureRandom :哪个 urandom 配置是 Java 8 和 Java 11 中 Linux 的正确非阻塞算法
【发布时间】:2022-01-07 16:04:44
【问题描述】:

最近我一直致力于在 CentOS-7 中使用 Java 8 和 Java 11 将安全随机非阻塞设置设置为默认设置。所以非阻塞配置是使用 securerandom.source=/dev/./urandom-Djava.security.egd=file:/dev/./urandom。但是我发现使用/dev/./urandom/dev/urandom 显示使用不同的算法。

我编写了我的第一个 java 程序 myConfigOut 在运行时将其吐出,并使用它来进行以下测试:

import java.security.*;
import java.util.*;

public class myConfigOut {
    public static void main(String[] argv) {

        try {
            // Trying to see which secureRandom provider we are using
            System.out.println("Trying to output RNG source");
            SecureRandom secureRandom = new SecureRandom();
            System.out.println("Secure random source: " + Security.getProperty("securerandom.source"));
            System.out.println("java.security.egd: " + System.getProperty("java.security.egd"));
            System.out.println("Algorithm: " + secureRandom.getAlgorithm());
        } finally {
            System.out.println("I'm done here");
        }
    }
}

CentOS 7 + Java 11:

# java -version
openjdk version "11.0.13" 2021-10-19 LTS
OpenJDK Runtime Environment 18.9 (build 11.0.13+8-LTS)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.13+8-LTS, mixed mode, sharing)

# java -Djava.security.egd=file:/dev/./urandom myConfigOut
Trying to output RNG source
Secure random source: file:/dev/random
java.security.egd: file:/dev/./urandom
Algorithm: DRBG
I'm done here

# java -Djava.security.egd=file:/dev/urandom myConfigOut
Trying to output RNG source
Secure random source: file:/dev/random
java.security.egd: file:/dev/urandom
Algorithm: NativePRNG
I'm done here

以上结果将算法从DRBG 切换到NativePRNG。 注意:DRBG 是 JDK9+ 的默认算法

CentOS 7 + Java 8

# java -version
openjdk version "1.8.0_312"
OpenJDK Runtime Environment (build 1.8.0_312-b07)
OpenJDK 64-Bit Server VM (build 25.312-b07, mixed mode)

# java -Djava.security.egd=file:/dev/./urandom myConfigOut
Trying to output RNG source
Secure random source: file:/dev/random
java.security.egd: file:/dev/./urandom
Algorithm: SHA1PRNG
I'm done here

# java -Djava.security.egd=file:/dev/urandom myConfigOut
Trying to output RNG source
Secure random source: file:/dev/random
java.security.egd: file:/dev/urandom
Algorithm: NativePRNG
I'm done here

在上面的测试算法中从SHA1PRNG切换到NativePRNG

所以我的问题是为什么在 CentOS 7 中从 /dev/./urandom/dev/urandom 切换以及要配置为具有非阻塞随机性的文件会有所不同。

【问题讨论】:

    标签: java java-8 java-11


    【解决方案1】:

    /dev/random 是阻塞的,过去被认为比 urandom 更安全。

    直到 Java 8 /dev/random 成为默认值,/dev/urandom 被硬编码为黑名单。

    所以-Djava.security.egd=file:/dev/urandom 被故意忽略并产生没有效果。

    /dev/./urandom 是克服这个后备名单的一种技巧。

    从 Java 11 开始使用新的内部随机算法,默认使用 /dev/urandom

    PS:这里也有说明:

    https://stackoverflow.com/a/59097932/836215

    https://stackoverflow.com/a/20315239/836215

    【讨论】:

    • 对于大多数用例 urandom is the correct choice,这使得 JDK 中的旧 hack 问题加倍。
    • @GenerousBadger 你是对的。从 Java8 的最后一个版本和 Java11 开始,这个旧的 hack 应该没有必要了。
    猜你喜欢
    • 1970-01-01
    • 2021-01-03
    • 2018-01-04
    • 2017-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多