Keys, salts, nonces and session identifiers have to be unpredictable, which System.Random is not. The library generates them with an SP 800-90A DRBG, seeded and reseeded from its own SP 800-90B entropy source.
uses System.SysUtils, stSecUtils, // iRawRandomEx stRandom, // GetRawRandom stDrbgAesCtr, // installs the library's SP 800-90A DRBG as the generator stAESNI, // AES in hardware, on CPUs that have it stAESCT; // constant-time software AES on every other CPU
stDrbgAesCtr installs the generator when it is linked. The TLS, SSH and mail components link it themselves, so a program that uses one of them already has a generator; a program that only does cryptography of its own names the unit.
The generator is AES-256 in counter mode (the CTR_DRBG of SP 800-90A), and it finds its AES implementation at run time among the units linked into the program. Name both of these, and each computer uses the right one:
stAESNI - AES in hardware. It registers where the CPU has the instructions: AES-NI on a Delphi Win32, Win64 or Linux64 build. On Linux64 the instructions come from the object file stAESNIFunc.o, which must be on the object path (-O, or DCC_ObjPath) for the program to link.stAESCT - constant-time software AES. It registers on exactly the CPUs stAESNI does not: an x86 CPU without AES-NI, and every target a Delphi build has no hardware AES for - macOS, iOS and Android.Neither covers for the other, so link both. Do not name stAES instead: its table lookups depend on the key and the data, so their timing can give both away; with the two units above linked it is never used.
With no AES unit linked, the generator cannot start, and GetRawRandom raises eRandom ("Unable to create instance"). The TLS, SSH and mail components link both units themselves.
function NewKey(aBytes: Integer): TBytes; begin { Key material comes from the PRIMARY generator: the shared DRBG seeded and reseeded from the entropy source. } SetLength(Result, aBytes); GetRawRandom(False).RawRandom(Result); end;
NewKey(32) is a 256-bit key, for example for Encrypt data with AES-GCM or an HMAC (Compute a SHA-256 hash and an HMAC).
threadvar gSessionRandom: iRawRandomEx; function NewSessionId: string; var lId: TBytes; I: Integer; begin { Values that must be unpredictable but are not key material - session ids, nonces - come from a SECONDARY generator: an independent DRBG seeded from the primary. Take one per thread (or per connection) and keep it; a new one per call costs a reseed from the primary. } if gSessionRandom = nil then gSessionRandom := GetRawRandom(True); SetLength(lId, 16); gSessionRandom.RawRandom(lId); Result := ''; for I := 0 to High(lId) do Result := Result + LowerCase(IntToHex(lId[I], 2)); end;
GetRawRandom(False): long-term keys, key-encryption keys, seeds. One shared instance behind a lock, reseeded from the entropy source, which is rate-limited by design - do not draw bulk data from it.GetRawRandom(True): per connection, per thread or per session - nonces, IVs, session ids, salts. Just as strong for these purposes, and fast.The DRBG and entropy source notes describe the generators, the entropy source and its health tests, and when a platform or hardware generator is the better choice.