cqcallaw
cqcallaw

Reputation: 1513

Is UUID.randomUUID() suitable for use as a one-time password?

As previous discussed, confirmation emails should have a unique, (practically) un-guessable code--essentially a one-time password--in the confirmation link.

The UUID.randomUUID() docs say:

The UUID is generated using a cryptographically strong pseudo random number generator.

Does this imply that the the UUID random generator in a properly implemented JVM is suitable for use as the unique, (practically) un-guessable OTP?

Upvotes: 36

Views: 29462

Answers (8)

Thiyanesh
Thiyanesh

Reputation: 2360

Password strength can be quantified based on the required entropy(higher the better).

For a binary computer,

entropy = password length * log2(symbol space)
symbol space is the total unique symbols(characters) available for selection

For a normal english speaking user with qwerty keyboard,

  1. symbols are selected from 52 characters(26 * 2 for both cases) + 10 numbers + maybe 15 other characters like (*, + -, ...), the general symbol space is around 75.
  2. if we expect a minimum password length of 8:
entropy = 8 * log275 ~= 8 * 6.x ~= 50

To achieve an entropy(50) for autogenerated one-time passwords with only hexadecimal (16 symbol space = 0-9,a-f),

password length = 50 / log216 = 50 / 4 ~= 12

If the application can be relaxed to consider the complete case sensitive english alphabets and numbers, the sample space will be 62 (26 * 2 + 10),

password length = 50 / log262 = 50 / 6 ~= 8

This has reduced the number of characters to be typed by the user to 8 (from 12 with hexadecimal).

With UUID.randomUUID(), two main concerns are

  1. user has to enter 32 characters (not user friendly)
  2. implementations has to ensure uniqueness criteria (close coupling with library versions and language dependencies)

I understand this is not a direct answer and its really up to the application owner to choose the best strategy considering the security and usability constraints.

Personally, i will not use UUID.randomUUID() as one-time password.

Upvotes: 1

jchook
jchook

Reputation: 7220

No. According to the UUID spec:

Do not assume that UUIDs are hard to guess; they should not be used as security capabilities (identifiers whose mere possession grants access), for example. A predictable random number source will exacerbate the situation.

Also, UUIDs only have 16 possible characters (0 through F). You can generate a much more compact and explicitly secure random password using SecureRandom (thanks to @erickson).

import java.security.SecureRandom;
import java.math.BigInteger;

public final class PasswordGenerator {
    private SecureRandom random = new SecureRandom();

    public String nextPassword() {
        return new BigInteger(130, random).toString(32);
    }
}

P.S.

I want to give a clear example of how using UUID as a security token may lead to issues:

In uuid-random we discovered an enormous speed-boost by internally re-using random bytes in a clever way, leading to predictable UUIDs. Though we did not release the change, the RFC allows it and such optimizations could sneak into your UUID library unnoticed.

Upvotes: 20

Alex Lockwood
Alex Lockwood

Reputation: 83303

Yes, using a java.util.UUID is fine, randomUUID methods generates from a cryptographically secure source. There's not much more that needs to be said.

Here's my suggestion:

  1. Send the user a link with a huge password in it as the URL argument.
  2. When user clicks the link, write your backend so that it will determine whether or not the argument is correct and that the user is logged in.
  3. Invalidate the UUID 24 hours after it has been issued.

This will take some work, but it's necessary if you really care about writing a robust, secure system.

Upvotes: 11

Mercury
Mercury

Reputation: 33

I think java.util.UUID should be fine. You can find more information from this article:

Upvotes: -4

andrew cooke
andrew cooke

Reputation: 46882

if you read the RFC that defines UUIDs, and which is linked to from the API docs, you'll see that not all bits of the UUID are actually random (the "variant" and the "version" are not random). so a type 4 UUID (the kind that you intend to use), if implemented correctly, should have 122 bits of (secure, for this implementation) random information, out of a total size of 128 bits.

so yes, it will work as well as a 122 bit random number from a "secure" generator. but a shorter value may contain a sufficient amount of randomness and might be easier for a user (maybe i am the only old-fashioned person who still reads email in a terminal, but confirmation URLs that wrap across lines are annoying....).

Upvotes: 24

Fang-Pen Lin
Fang-Pen Lin

Reputation: 14406

The point of the random code for a confirmation link is that the attacker should not be able to guess nor predict the value. As you can see, to find the correct code to your confirmation link, a 128bits length UUID yields 2^128 different possible codes, namely, 340,282,366,920,938,463,463,374,607,431,768,211,456 possible codes to try. I think your confirmation link is not for launching a nuclear weapon, right? This is difficult enough for attacker to guess. It's secure.

-- update --

If you don't trust the cryptographically strong random number generator provided, you can put some more unpredictable parameters with the UUID code and hash them. For example,

code = SHA1(UUID, Process PID, Thread ID, Local connection port number, CPU temperature)

This make it even harder to predict.

Upvotes: -2

wattostudios
wattostudios

Reputation: 8764

I think this should be suitable, as it is generated randomly rather than from any specific input (ie you're not feeding it with a username or something like that) - so multiple calls to this code will give different results. It states that its a 128-bit key, so its long enough to be impractical to break.

Are you then going to use this key to encrypt a value, or are you expecting to use this as the actual password? Regardless, you'll need to re-interpret the key into a format that can be entered by a keyboard. For example, do a Base64 or Hex conversion, or somehow map the values to alpha-numerics, otherwise the user will be trying to enter byte values that don't exist on the keyboard.

Upvotes: -3

Anuj Balan
Anuj Balan

Reputation: 7729

It is perfect as one time password, as even I had implemented the same for application on which am working. Moreover, the link which you've shared says it all.

Upvotes: -4

Related Questions