4
$\begingroup$

Given SecureRandom class is considered suitable for use in cryptography, I consider new SecureRandom() to be secure (funny term, isn't it?).

If new SecureRandom() already is secure, what would be the benefit of using SecureRandom.getInstanceStrong() instead?

Is this same kind of difference as between /dev/urandom and /dev/random?

I'm debating this in the following scenario, where I'm mostly concerned about making IV random (for use with AES-GCM):

private final SecureRandom secureRandom = new SecureRandom();

[...]

private byte[] getIv() {
    int ivLength = 12;
    byte[] iv = new byte[ivLength];
    secureRandom.nextBytes(iv);
    return iv;
}
$\endgroup$
6
  • 2
    $\begingroup$ See the footnote: Depends on the OS, in IOS they are same $\endgroup$
    – kelalaka
    Dec 24, 2021 at 20:22
  • 1
    $\begingroup$ This is library call to get instance... SecureRandom.getInstanceStrong() See Maarten answer on so $\endgroup$
    – kelalaka
    Dec 24, 2021 at 20:24
  • 1
    $\begingroup$ A GCM IV doesn't need to be random. It only needs to be unique. So for this specific case, it doesn't matter. The only reason to use a random IV for GCM is if you can't remember all the IVs that have been previously used, so you instead rely on statistical uniqueness. For that, it's enough to have a statistically random value, it doesn't even need to be cryptographically random (which is a stronger property, including impredictability and independence from all other random values). $\endgroup$ Dec 24, 2021 at 23:22
  • 1
    $\begingroup$ @kelalaka This is a bit of a Java mess actually. securerandom.source=file:/dev/random which block(ed) of course. So if you spun up an app server, it took like five minutes to return a http request. Even a non https daft one like my site which is pretty surprising (probably cookie generation). $\endgroup$
    – Paul Uszak
    Dec 25, 2021 at 3:30
  • 1
    $\begingroup$ What @Gilles said is true, If possible use AES-GCM-SIV to mitigate this issue... $\endgroup$
    – kelalaka
    Dec 25, 2021 at 22:38

1 Answer 1

2
$\begingroup$

SecureRandom.getInstanceStrong() will ensure that a strong algorithm (securerandom.strongAlgorithms) will is used.

  • It is available since Java version 8. Check your version before starting to use.

  • If no such algorithm is available in running VM, it will throw NoSuchAlgorithmException.

  • This failure is a better practice instead of defaulting into weak security.

$\endgroup$
5
  • 1
    $\begingroup$ Welcome to Cryptography. A link to this claim or code line from the source will make this answer much better. Otherwise, this is a self claim. For example one could also say, it there is no strong ( what is the meaning of strong) algorithm then it will use the default one; java.util.Random. There is need for reference and in Java case, even versions.. $\endgroup$
    – kelalaka
    Dec 27, 2021 at 21:51
  • $\begingroup$ Thank your for your feedback kelalaka, I've added a bit more clarifications, as you suggested. $\endgroup$ Dec 27, 2021 at 23:56
  • $\begingroup$ Is there a list of strongAlgorithms? $\endgroup$
    – kelalaka
    Dec 28, 2021 at 6:33
  • $\begingroup$ @kelalaka 'Strong' is a common term used by cryptographers, agencies and totalitarian governments to label an encryption can can't be defeated in real time. There's a list here. I don't know if AES should be added as there is no mathematical evidence that it's still secure. $\endgroup$
    – Paul Uszak
    Dec 28, 2021 at 16:48
  • $\begingroup$ @PaulUszak I'm talking about in the context of the Java Security and specially on the random number generatos. $\endgroup$
    – kelalaka
    Dec 28, 2021 at 16:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.