Hardware Security Module

Hardware Security Modules for iGaming

Protecting Keys, RNG, and Player Data with YubiHSM 2

Real-money gambling is an adversarial environment. RNG seeds that can be predicted mean manipulated outcomes. Payment signing keys that can be exported mean stolen funds. The book's reference platform stores all sensitive cryptographic material in a YubiHSM 2 — a USB hardware security module where private keys are created, used, and destroyed without ever leaving the hardware.

YubiHSM 2 FIPS 140-2 Level 3 XMLDSig AES-256-GCM RSA-4096 Python yubihsm
Why HSM

Regulatory Requirement, Not Optional

MGA, UKGC, and GLI-GSF certification all require that RNG entropy sources and payment signing keys are protected by tamper-resistant hardware. An HSM is not a security nicety — it's a licensing prerequisite.

casino

RNG Certification

GLI-19 and UKGC require that random number generation uses certified entropy. Hardware entropy from the YubiHSM is mixed with OS entropy (Python secrets) for defense in depth.

payments

Payment Key Custody

PIX payment signing keys are generated inside the HSM and marked non-exportable. The key never leaves the hardware — not in memory, not on disk, not across the network.

fact_check

Audit Trail Signing

Each game round result hash is signed by the HSM, creating a cryptographic chain of non-repudiable audit records. Regulators can verify the integrity of every game outcome years after the fact.

memory

Tamper-Proof

FIPS 140-2 Level 3 certification means the device physically destroys key material if tampered with. Even if an attacker has physical access to the server, the keys are unrecoverable.

The Hardware

What is the YubiHSM 2?

The YubiHSM 2 is a nano-form USB security module designed for server deployment. At the size of a USB-A flash drive, it provides HSM functionality at a fraction of the cost of traditional rack-mounted HSMs — making hardware-backed cryptography accessible to iGaming startups, not just Tier-1 operators.

FIPS 140-2 Level 3
Physical tamper detection and response. Meets MGA, UKGC, and PCI-DSS key custody requirements.
Supported Algorithms
RSA-4096, Ed25519, ECDSA, AES-256-CCM, AES-256-GCM, HMAC-SHA256, HMAC-SHA512
True Hardware RNG
On-chip TRNG for entropy generation. Output can seed platform CSPRNGs or be used directly for high-stakes operations.
Role-Based Access Control
Each application gets a session with minimum required capabilities. The RNG service can't access payment keys, and vice versa.
Python & CLI SDKs
The yubihsm Python library and yubihsm-shell CLI integrate naturally into the book's Python microservices.
Use Cases

Cryptography in iGaming

casino
RNG Seeding
HSM provides 32 bytes of hardware entropy combined with OS entropy via SHA-256. The resulting seed is cryptographically sound per GLI-19 and NIST SP 800-90B.
payments
PIX Payment Signing
RSA-2048 or ECDSA P-256 keys (ICP-Brasil X.509 certificates) for signing PIX transactions via XMLDSig/ISO 20.022 are generated inside the HSM and marked non-exportable. The payment data travels to the HSM, the signature comes back. The key never leaves.
verified_user
mTLS Certificates
RSA-4096 private keys for server-to-server mTLS authentication are stored in the HSM. The public key is exported for CSR signing; the private key never leaves hardware.
key
JWT Signing Keys
HMAC-SHA256 keys for JWT token signing are stored in the HSM. Compromising the application server doesn't expose the signing key — tokens can't be forged.
Real Examples from the Book

Production HSM Integration

01

CSPRNG Seeding with Hardware Entropy

The book's RNG module fetches 32 bytes of true random numbers from the YubiHSM's on-chip TRNG, then mixes it with OS-level entropy from Python's secrets module via SHA-256. The resulting seed meets GLI-19's requirement for a certified entropy source combined with a CSPRNG.

services/rng/hsm_seed.py
# YubiHSM 2: Generate CSPRNG seed for casino RNG
import yubihsm
from yubihsm.objects import HmacKey

session = yubihsm.YubiHsm.connect("yhusb://")
auth = session.create_session_derived(1, "password")

# Generate 32 bytes of hardware entropy
entropy = auth.get_pseudo_random(32)

# Use as seed for Python secrets module
import secrets
import hashlib

seed = hashlib.sha256(entropy + secrets.token_bytes(32)).digest()
print(f"RNG Seed (HSM + OS entropy): {seed.hex()}")
02

HSM-Backed PIX Payment Signing

The book's payment service uses RSA or ECDSA P-256 keys (ICP-Brasil X.509 certificates) stored inside the YubiHSM to sign PIX transactions via XMLDSig (ISO 20.022). The payment message is serialised in the application, sent to the HSM for signing using RSASSA-PKCS1-SHA256, and the XMLDSig signature is embedded in the BAH (Business Application Header). At no point does the private key exist in application memory.

services/payments/pix_signer.py
# HSM-backed XMLDSig signing for PIX transactions (ISO 20.022)
# PIX requires ICP-Brasil X.509 certificates with RSA or ECDSA P-256
from yubihsm.objects import AsymmetricKey

# RSA-2048 key stored in HSM (ICP-Brasil cert, non-exportable)
pix_key = AsymmetricKey(auth, key_id=0x0002)

# PIX message follows ISO 20.022 / XMLDSig format
pix_message = """<BAH>
  <Fr><FIId><FinInstnId><ISPB>12345678</ISPB></FinInstnId></FIId></Fr>
  <BizMsgIdr>M00012345620260324143200</BizMsgIdr>
  <MsgDefIdr>pacs.008.spi.1.2</MsgDefIdr>
</BAH>""".encode()

# Sign with RSASSA-PKCS1-SHA256 via HSM (key never leaves hardware)
signature = pix_key.sign_pkcs1v1_5(pix_message, hash_alg="sha256")

# XMLDSig signature embedded in the Business Application Header
print(f"PIX XMLDSig: {signature.hex()[:32]}...")
print(f"Cert: ICP-Brasil e-CNPJ A3")
03

Generating mTLS Keys in the HSM

Server-to-server communication in the platform uses mutual TLS. The RSA-4096 private key is generated inside the HSM using yubihsm-shell. Only the public key is exported for CSR submission to the internal CA. The private key will sign TLS handshakes without ever leaving the device.

scripts/mtls-keygen.sh
# Generate mTLS certificates with HSM
yubihsm-shell -a generate-asymmetric-key \
  --object-id 0x0003 \
  --algorithm rsa4096 \
  --label "casino-mtls-key" \
  --capabilities sign-pkcs

# Export public key for CSR
yubihsm-shell -a get-public-key \
  --object-id 0x0003 \
  --outformat pem \
  -o casino-mtls.pub
Cryptography Overview

Algorithm and Key Location Reference

Every cryptographic operation in the platform is documented in the book with its algorithm, key location, and the regulatory standard that mandates it. This table summarises the most critical operations.

Purpose Algorithm Key Location Regulatory Requirement
RNG Seeding SHA-256 + HMAC YubiHSM GLI-19, MGA Tech Standards
Payment Signing RSA-2048 / ECDSA P-256 (XMLDSig) YubiHSM PCI-DSS v4, BACEN, ICP-Brasil
mTLS Auth RSA-4096 YubiHSM SOC 2 Type II, ISO 27001
Data Encryption AES-256-GCM YubiHSM GDPR Art. 32, LGPD
JWT Signing HMAC-SHA256 YubiHSM Internal Policy
Audit Hashing SHA-256 Software GLI-19, SIGAP Reporting
Password Hashing Argon2id Software OWASP 2024, GLI-GSF
In the Book

Chapters Covering HSM & Cryptography

Ch. 18
RNG Module

RTC (Random) Module

Full design of the platform's random number generation subsystem: entropy collection, CSPRNG initialisation with HSM seeding, per-request seed derivation, and GLI-19 compliance documentation with test vectors.

CSPRNG GLI-19 Entropy
Ch. 20
HSM Infrastructure

HSM Infrastructure

Physical and logical setup of the YubiHSM 2: device provisioning, session authentication design, key object creation with RBAC capabilities, key backup procedures, and firmware update policies.

YubiHSM RBAC Key Backup
Ch. 27
Data & Residency

Data Residency & Backup

Encryption key management across jurisdictions: which keys live in which HSM slots, key rotation schedules, backup HSM setup for disaster recovery, and the interplay between HSM-stored keys and Ansible Vault for configuration secrets.

GDPR LGPD Key Rotation
Full Implementation Included

Keys That Never Leave Hardware. Code That Ships.

The complete HSM integration — RNG seeding, PIX signing, mTLS generation, and audit trail signing — is included with the book alongside setup guides for provisioning the YubiHSM in both development and production environments.