Functions
C_GenerateRandom
Fills a caller-supplied buffer with random bytes.Handle of any open session on the token. The session does not need to be authenticated.
Pointer to the buffer that receives the random bytes.
Number of random bytes to generate. The buffer pointed to by
pRandomData must be at least this many bytes long.- OpenSSL backend: uses
RAND_bytes, which sources entropy from the OS (/dev/urandomon Linux). - Botan backend: uses Botan’s
AutoSeeded_RNG.
When SoftHSM is built against a FIPS-capable OpenSSL (OpenSSL 3.x with the FIPS provider enabled),
C_GenerateRandom automatically uses a FIPS 140-2 approved DRBG. No extra configuration is needed.C_SeedRandom
Mixes additional seed material into the RNG state.Handle of any open session.
Pointer to the seed bytes to mix in.
Length of the seed in bytes.
The PKCS#11 specification permits tokens to return
CKR_RANDOM_SEED_NOT_SUPPORTED if the RNG cannot be seeded externally. SoftHSM passes the seed to the underlying backend; whether it is actually used depends on the backend’s RNG implementation.Typical uses
| Use case | Details |
|---|---|
| IV / nonce generation | Generate 12–16 random bytes before initializing AES-GCM or AES-CBC |
| Key material | Derive session keys from random bytes when hardware key generation is unavailable |
| Challenge-response | Generate nonces for authentication protocols |
| Salt values | Generate random salts for password hashing or KDF inputs |
Error codes
| Return value | Meaning |
|---|---|
CKR_OK | Success |
CKR_ARGUMENTS_BAD | pRandomData is NULL_PTR or ulRandomLen is 0 |
CKR_SESSION_HANDLE_INVALID | The session handle is not valid |
CKR_RANDOM_NO_RNG | The backend RNG is unavailable |
CKR_RANDOM_SEED_NOT_SUPPORTED | The backend does not support external seeding (C_SeedRandom only) |