Database security: authentication, authorisation and encryption
Clear, exam-focused notes on database security covering authentication, authorization and encryption, with definitions, exact statements, formulas (SQL and crypto), worked examples (RSA, Diffie–Hellman, GRANT/REVOKE, password storage), common exam mistakes and quick checks.
Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.
Why it matters
Database security prevents unauthorised reading, writing or deletion of data. Engineers use authentication to verify who a user is, authorisation to decide what an authenticated user may do, and encryption to protect data when stored or transmitted. These three together reduce data breaches, meet regulatory requirements and form the basis for secure DBMS design and operations.
Key ideas
Define terms first, then build precise models and rules.
Authentication: the process of proving the identity of an entity (user, service or machine). Typical factors: something you know (password), something you have (token, smart card), something you are (biometric). Exact goal: after authentication, the system has reason to believe requests come from the claimed identity.
Authorisation (access control): the decision process that determines whether an authenticated identity may perform an operation on a database object. It answers "can X do action Y on object Z?".
Encryption: transformation of data using an algorithm and a key so that only holders of the correct key can recover the original. Two families: symmetric encryption (same key for encrypt/decrypt) and asymmetric encryption (public/private key pair).
Authentication models and mechanisms
- Password-based authentication: store a hashed password (never plaintext). Use a slow, salted hash (bcrypt, scrypt, Argon2). Define salt: a random per-password value concatenated to the password before hashing to prevent precomputed dictionary attacks.
- Multi-factor authentication (MFA): require two or more different factor types.
- Kerberos: a network authentication protocol using symmetric keys and trusted third-party (Key Distribution Center). Precise flow to learn: AS (Authentication Service) gives Ticket Granting Ticket (TGT); client uses TGT to request service tickets from TGS (Ticket Granting Service); client presents service ticket to server.
- Public Key Infrastructure (PKI) and X.509 certificates: bind an identity to a public key via a certificate signed by a Certificate Authority (CA).
Authorisation models (exact statements)
- Discretionary Access Control (DAC): the owner of an object (table, file) controls access. Formal property: if user U owns object O, U may grant or revoke rights to other users at U's discretion.
- Mandatory Access Control (MAC): access decisions are based on fixed labels (security levels) and system-enforced rules. Example policy: Bell–LaPadula (confidentiality) — "no read up, no write down" formally: subject at level Ls can read object at level Lo only if Ls >= Lo; subject can write only if Ls <= Lo. (Assumes hierarchical security levels and trusted label assignment.)
- Role-Based Access Control (RBAC): permissions are assigned to roles; users are assigned roles. Exact statement: user U has permission P iff there exists a role R such that U is assigned to R and R includes P. Additions: role hierarchies (role inheritance) and constraints (separation of duty).
- Principle of Least Privilege (PoLP): each user/process should have only the privileges necessary to perform its tasks; formally: minimize the set of permissions granted, P(U), such that workload requirements are satisfied.
- Attribute-Based Access Control (ABAC): decisions use attributes of subjects, objects, actions, and context (e.g., user.department = 'Sales' and row.owner_id = user.id); policies are Boolean rules evaluated at request time; ABAC generalizes RBAC and underlies many RLS policies.
Database privileges and enforcement
- SQL privileges (SELECT, INSERT, UPDATE, DELETE, EXECUTE, etc.) are mapped to objects (databases, schemas, tables, views, columns). Enforce at connection/session level: DBMS checks user identity and roles on each request.
- Fine-grained access control: row-level security (RLS) and column-level privileges or policy-based views (optionally with dynamic data masking) allow per-row and per-column rules.
- Audit and logging: record authentication attempts, privilege changes, and sensitive actions. Auditing supports accountability.
Encryption types and key management
- Encryption at rest: protects stored data. Implementations: Transparent Data Encryption (TDE) encrypts database files; column-level encryption encrypts specific columns before storage.
- Encryption in transit: TLS between client and server (SSL is deprecated); prefer mutual TLS (client certificates) where appropriate.
- Field/column encryption: encrypt selected sensitive columns; application or DBMS performs encryption/decryption with keys. Use AEAD modes (e.g., AES-GCM or ChaCha20-Poly1305) to provide integrity as well as confidentiality, and ensure nonces/IVs are unique per key.
- End-to-end and homomorphic encryption: end-to-end ensures data is encrypted at source and only decrypted at destination. Homomorphic encryption allows computation on encrypted data but is expensive; specify trade-offs.
- Key management: keys must be stored and rotated securely. Use a Key Management Service (KMS), Hardware Security Module (HSM) or trusted vault. If a key is compromised, all data encrypted by that key is compromised; therefore proper rotation and separation of duties for key access are essential.
Security design rules (exact statements)
- Authenticate before authorising: identity must be established first.
- Apply PoLP and separation of duties: no single role should have all powers required to perform and hide fraud.
- Defence in depth: combine network, host, DBMS, and application controls.
Diagram to draw (exam question): Draw the Kerberos protocol flow. Boxes left to right: Client, AS, TGS, Server. Arrows: 1) Client -> AS: auth request with ID; 2) AS -> Client: TGT (encrypted with TGS key) + session key; 3) Client -> TGS: TGT + service request; 4) TGS -> Client: service ticket + session key; 5) Client -> Server: service ticket + authenticator (proves freshness and client identity); 6) Server -> Client: response (optionally AP-REP to prove server identity). Label tickets, session keys, timestamps and encryption keys.
Formulas
n = p * q— RSA modulus, product of two primes p and q.φ(n) = (p-1)*(q-1)— Euler's totient for RSA (if p, q prime).e · d ≡ 1 (mod φ(n))— e and d are modular inverses.A = g^a mod pandB = g^b mod pands = A^b mod p = B^a mod p— Diffie–Hellman shared secret. p prime, g generator.GRANT SELECT, INSERT ON table_name TO role_name;— SQL syntax to grant privileges.REVOKE INSERT ON table_name FROM role_name;— SQL syntax to revoke privileges.CREATE ROLE role_name;andGRANT role_name TO user_name;— RBAC role creation and assignment.- Password storage (concept):
stored = Hash(salt || password)where salt is random per-user; use slow hash (bcrypt/Argon2) — no SI unit.
Worked examples
RSA small numeric example (full steps):
- Choose primes p=61, q=53. Compute
n = 61*53 = 3233. - Compute φ(n) = (61-1)(53-1) = 6052 = 3120.
- Choose e = 17 such that gcd(e, φ(n)) = 1.
- Compute d = modular inverse of 17 mod 3120 => d = 2753 (because 17*2753 mod 3120 = 1).
- Public key = (e=17, n=3233). Private key = (d=2753, n=3233).
- Encrypt message m = 65:
c = 65^17 mod 3233 = 2790. - Decrypt c:
m = 2790^2753 mod 3233 = 65. Final answer: m = 65.
- Choose primes p=61, q=53. Compute
Diffie–Hellman numeric trace (small prime):
- Public parameters: p = 23, g = 5.
- Alice chooses a = 6 -> A = 5^6 mod 23 = 8.
- Bob chooses b = 15 -> B = 5^15 mod 23 = 19.
- Alice computes secret s = B^a mod 23 = 19^6 mod 23 = 2.
- Bob computes s = A^b mod 23 = 8^15 mod 23 = 2.
- Shared secret s = 2.
SQL RBAC example and privilege change (trace):
- Create role and assign privileges:
CREATE ROLE analyst;GRANT SELECT ON sales TO analyst;GRANT analyst TO alice;(now alice inherits SELECT on sales)
- Revoke a privilege:
REVOKE SELECT ON sales FROM analyst;=> alice loses SELECT. - To allow column-level only:
GRANT SELECT(sale_amount) ON sales TO analyst;(if DBMS supports column-level grants)
- Create role and assign privileges:
Password storage example (conceptual steps):
- Generate random salt, e.g.
salt = 0x9b1f3a2d4c6e7f01d3c2b4a59687e4f2(16 bytes). - Compute
stored_hash = Argon2(password, salt, workparams). - Store (username, salt, stored_hash). On login, recompute Argon2 with stored salt and compare.
- Generate random salt, e.g.
Common mistakes
- Believing encryption alone solves access control: encrypted data still needs correct key management and authorisation of decrypted data.
- Incorrectly assuming TDE protects backups automatically — many DBMSs do, but configuration and key backup matter.
- Using RSA to encrypt large blobs directly instead of using hybrid encryption (use RSA to encrypt a random symmetric key, then use AES for the data).
- Forgetting to log and audit privilege GRANT/REVOKE operations.
For your exam
Kinds of questions:
- Definitions and short-notes: define authentication/authorisation/encryption, explain PoLP, list types of encryption.
- Derivations/algorithms: show RSA key generation and encryption/decryption steps, explain Kerberos flow with a diagram.
- SQL and RBAC tasks: write GRANT/REVOKE, create roles and assign privileges, explain separation of duty constraints.
- Numerical crypto problems: compute RSA encryption/decryption for small primes or compute Diffie–Hellman shared key for given small p, g, a, b.
- Design/short essay: propose a secure design for a DB-backed web application combining authentication, RBAC and encryption, and explain key management.
What a full-marks answer includes:
- Clear definitions of terms (bold them if asked), step-by-step process, any diagrams drawn and labelled (for Kerberos or key flows), exact SQL syntax when required, numeric computations with intermediate steps, and a short note on practical considerations (key management, logging, rotation).
Quick check
- What is the difference between DAC and MAC?
- Show the RSA decryption formula and explain each symbol.
- Name two ways to protect data in transit.
Answers: (1) DAC: owner-controlled grants; MAC: system-enforced based on labels and rules. (2) m = c^d mod n where c = ciphertext, d = private exponent (modular inverse of e), n = RSA modulus (product of primes). (3) TLS/SSL and VPN or TLS + client certificates.
Finished this topic? Mark it so your progress, study plan and readiness keep up.
Stuck on something here?