3

I am generating a nonce to validate the wizard steps to secure it one after another.

I know how to create the nonce in nodejs and store the same in database to make sure it can be used once.

But I was wondering, whether there is an idea to generate and validate a nonce as above like to be used only once and if possible, can be used within a time limit (expiry) without storing the same in the database but simply returning it to the client in one wizard step and validate the same on the next step.

I normally use the below method to generate the nonce, normalize it and store it in a mongodb with a time to expire so that mongodb will delete it after a particular time if its not been used.

var crypto = require('crypto');

crypto.randomBytes(32, function (err, bytes) {
   if (err) {
       next(err);
    } else {
       next(null, normalize(bytes));
    }
});

Please suggest if any good/optimized way of generating the nonce than this and a possibility to take care of the one time usage and expiry without a database call.

3 Answers 3

3

It's always better practise to have a database to store and validate the nonce. To restrict with time, either you can use a mongodb with expiry or you can generate the timestamp, then generate a hmac with timestamp, nonce & private key. Then send the nonce, timestamp & hmac to the client. This way you can secure the timestamp as well you can restrict the nonce with particular time if your database is not supporting the document expiry as mongodb. Hope it explains.

4
  • Database? Why? The probability of repetition is almost close to zero within thousands of years, if you use powerful cryptographic random number generators. Apr 29, 2018 at 0:25
  • @JoãoPimentelFerreira I would prefer not having any data stored in the node.js process at all, as it would become difficult for scaling. Having two or more server nodes, where the generated nonce is stored in the memory of another node than the node the client is connected to, would probably cause some errors. That's why stored data should always be stored outside of Node.js. Personally, I'd have the nonce storage on a cache server, such as Redis. Sep 18, 2018 at 10:00
  • check my answer. My point is not where to store, but simply that you don't need to store anywhere. Sep 20, 2018 at 15:32
  • I think it depends on the problem you are working on, nonces are normally stored in some authentication schemes like Hawk to compare to avoid replay attacks. Sep 22, 2020 at 13:22
1

It is possible to have a time expired nonce without a database call, using something like a hash map on the server to store and retrieve the nonce and check the time. If there is a session id or static user id, that would help. However, this idea of having the server generate, hold, fetch, and maintain a nonce seems difficult in comparison to a tested solution such as MongoDB. There is another wrinkle that would happen in the case of multiple servers for a service, where traffic would have to be kept consistent for sessions start to finish. Using a database for a single source of truth on nonces fixes that.

If the reason for keeping the nonce on the node server is for speed / lag, it could be worth the extra testing and code. In that case the timeout for the nonces would need to be scanned with regularity for expired ones, to maintain fast searching.

-3

EDIT: This answer does not prevent replay attacks and for that you need databases.

The reason for storing a nonce is to ensure that it can only be used once. This protects against reply attacks, where a malicious party could potentially intercept a valid request and then retransmit it. The extra step of validating the nonce prevents the replayed request from being accepted.

To prevent certain types of 'replay' attack, it is important to know not only that the nonce is unique but that it has not been used in a previous request. Real world example: A bank issues a chequebook, where each cheque has a unique number. If I write you a check and sign it you can then take it to the bank and get the money. The bank needs to record the number of every check that has been cashed, otherwise you could simply photocopy the original check I gave you and claim that money again

Old Answer (I don't delete it because it is mathematically elegant :)

If you use a cryptographic random number with 128 bits, you have 2^(128) possibilities of numbers. Considering an even, equally distributed random number generator, and that you call such function once per second, the probability of repetition would be almost zero.

You may use something like this in node (conversion here to base64)

const crypto = require('crypto');
var nonce = crypto.randomBytes(16).toString('base64');

128 bits are 16 bytes.

Mathematical demonstration

The probability mass function of the binomial distribution, which is the one applicable, is

enter image description here

This functions returns the probability of getting exactly k successes within n trials, with p being the probability of success for each trial.

In our case p=1/(2^128).

The cumulative function is

enter image description here

where k on the sum is the "floor" under k, i.e. the greatest integer less than or equal to k. This cumulative function gives, the probability of having the number of successes between 0 and k.

But we need the probability of at least one successful trial, since we don't want to have repetitions. And considering that

enter image description here

thus

enter image description here

which means for k=1 that

enter image description here

in our case, if we call our nonce once per second during 100 years, we get n=1*60*60*24*365.25*100=3155760000

Therefore

p=1/(2^128);
n=3155760000;

Applying the formula

enter image description here

Conclusion

If you use 128 bits nonce and you call the nonce once per second during 100 years, the probability of repeating said nonce during that period of 100 years is almost zero, which means almost impossible. That means that you don't need databases.

13
  • 8
    The reason for storing a nonce is to ensure that it can only be used once. This protects against reply attacks, where a malicious party could potentially intercept a valid request and then retransmit it. The extra step of validating the nonce prevents the replayed request from being accepted. Aug 25, 2018 at 19:45
  • @JordanBurnett if you read the math you understand that the probability of repetition even without DB validation or storing is so ridiculously low, that you need the age of the Universe to even have the slightest chance to repeat that nonce. I suppose you don't have any idea of what 10^(-30) means in terms of probability. Sep 10, 2018 at 10:08
  • 6
    I think you've misunderstood. To prevent certain types of 'replay' attack, it is important to know not only that the nonce is unique but that it has not been used in a previous request. Real world example: A bank issues a chequebook, where each cheque has a unique number. If I write you a check and sign it you can then take it to the bank and get the money. The bank needs to record the number of every check that has been cashed, otherwise you could simply photocopy the original check I gave you and claim that money again. Sep 11, 2018 at 14:54
  • @JordanBurnett I perfectly understood your example. But what's the purpose of the check if the world ends tomorrow? And the probability of that is higher than the nonce being repeated in a lifetime through this number generation. Sep 11, 2018 at 19:49
  • 4
    @JoaoPimentalFerreria - Read my real world example. The generated nonce can be as unique as you like, but for some applications it's crucial that you shouldn't be able to make the same (signed) request twice. Sep 12, 2018 at 20:03

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.