- DelphiTools - https://www.delphitools.info -

Nonces and Tokens

padlockI recently committed an extension to dwsCrypto lib module that exposes direct support for cryptographic nonces and tokens.

What are those nonce things?

A cryptographic nonce [1] is a number that is going to be used only once, it is usually a huge number, which will usually be encoded in various, non-decimal ways.

A nonce should be meaningless, impossible to guess, expiring and never reused.

The extension is centered around a Nonces static class that is dedicated to managing “expiring” nonces, or tokens, with a “short” life expectancy. Each nonce has an associated data string.

By default nonces are generated as a string of 20 characters you can place in URLs or anywhere (in more detailed terms, each nonce is 120 bits of cryptographic-strength random, encoded with an URI-safe base 64 scheme).

What are nonces useful for?

owasp [2]There are several uses for nonces. They typically involve a server generating a nonce and passing it to the client, the client then has a limited time to “do something” with the nonce, then has to send back a request alongside the nonce to the server.

Finally another use for nonces can be as “light-weight” session tokens, when sessions are going to be short-lived or are going to be renewed continuously, or do not need to be persisted in a database. In those cases, you will however have to manage the nonce lifetime carefully.

Available methods

Nonces is exposed as a static class, with only static methods:

Less commonly used methods

Previously you could use DWScript’s expiring Global Storages [6] alongside functions like CryptographicToken to implement nonces with very limited code, but you still had to delete the nonces manually, and Global Storages being enumerable, a misbehaving script could end up leaking information it should not have.

Like Global Storages, the Nonces are process-wide, thread-safe and shared across all scripts, but they are not enumerable (and not guessable, so a script only check the nonces it already knows, and cannot leak nonces from another context).