Chapter 19 — Identity and cryptography
Lesson map
Lesson 19 published. Map, example, EX055.
flowchart TD
N0["Learning outcomes"]
N1["19.1 Device identity"]
N2["Lab — Message authentication"]
N3["19.2 Reading the HMAC run"]
N4["19.3 People and revocation"]
N5["Synthesis"]
N0 --> N1 --> N2 --> N3 --> N4 --> N5
N5 --> EX["Worked example"]
EX --> Q["Exercise EX055"]
Logical drawing
flowchart LR
N0["Learning outcomes"]
N1["19.1 Device identity"]
N2["Lab — Message authentication"]
N3["19.2 Reading the HMAC run"]
N4["19.3 People and revocation"]
N5["Synthesis"]
N0 --> N1 --> N2 --> N3 --> N4 --> N5 --> Q["EX055"]
[Learning outcomes]
|
v
[19.1 Device identity]
|
v
[Lab — Message authentication]
|
v
[19.2 Reading the HMAC run]
|
v
[19.3 People and revocation]
|
v
[Synthesis]
|
v
[Worked example] --> [Exercise EX055]
Learning outcomes
You will (1) authenticate devices and people with keys and certificates; (2) use HMAC for message authentication; (3) relate identities to certificates and revocation.
19.1 Device identity
Each Ridgeway meter needs an identity that others can verify. Aiyana compares two approaches. Symmetric identity uses a per-meter secret key shared with the key service; it is cheap but the service must protect every key. Asymmetric identity gives each meter a key pair and a certificate signed by Ridgeway's device authority, following the X.509 profile in RFC 5280; verifiers need only public keys. She recommends symmetric HMAC keys for readings, where volume is high, and certificates for collectors, which authenticate to the gateway over TLS.
Lab — Message authentication
"""Authenticate synthetic meter readings with HMAC-SHA256 (RFC 2104) and constant-time compare."""
import hmac, hashlib
KEY = bytes(range(32)) # demo key; real keys come from the utility's key service
def tag(msg): return hmac.new(KEY, msg, hashlib.sha256).hexdigest()
def main():
msg = b"meter=M-0417;litres=18342;ts=2026-04-01T06:00Z"
t = tag(msg)
print("tag", t[:24], "...")
print("genuine verifies:", hmac.compare_digest(t, tag(msg)))
forged = msg.replace(b"18342", b"08342")
print("edited reading verifies:", hmac.compare_digest(t, tag(forged)))
if __name__ == "__main__":
main()
Run with python3 lab_hmac.py. Verified output:
tag bc7de0101afc1c79bfc23747 ...
genuine verifies: True
edited reading verifies: False
19.2 Reading the HMAC run
The lab tags a canonical reading with HMAC-SHA256 and verifies it. The genuine reading verifies and the edited reading, with one digit changed, fails, because without the key nobody can compute a valid tag for a modified message. Verification uses hmac.compare_digest to avoid timing leaks. The demonstration key is a fixed byte sequence only so the output is reproducible; real keys come from the key service. Aiyana stresses that HMAC gives integrity and origin authentication between parties sharing the key, but not non-repudiation, because either party could have produced the tag.
19.3 People and revocation
People at Ridgeway authenticate with hardware security keys and short-lived certificates for administrative access. When someone leaves, their access must end quickly. Aiyana documents revocation for each identity type: meter keys are disabled in the key service; collector certificates are added to a revocation list or simply allowed to expire, since they are short-lived; staff certificates are revoked and their tokens disabled. She notes that short lifetimes reduce dependence on revocation checking, which clients perform unevenly. Revocation steps are rehearsed twice a year so that staff know them under pressure.
Worked example
Aiyana chooses HMAC per-meter keys for readings and certificates for collectors and staff, demonstrates tag verification, and documents revocation per identity type.
Synthetic case. Identities and the demo key are invented; RFC 5280 and RFC 2104 are real.
Errors and limits
HMAC cannot prove to a third party which side created a message. Long-lived certificates make revocation essential and hard.
Exercises
- EX055 — Symmetric or asymmetric. Why use HMAC keys for readings but certificates for collectors? Rubric: 3 = high-volume readings suit cheap symmetric tags; collectors use TLS with public verification;; 2 = reasons partial;; 1 = random;; 0 = none.
- EX056 — Edited reading. What did the lab print for the edited reading, and why? Rubric: 3 = False, since a valid tag cannot be computed without the key;; 2 = False;; 1 = True;; 0 = none.
- EX057 — Non-repudiation. Why does HMAC not give non-repudiation? Rubric: 3 = both parties share the key, so either could have made the tag;; 2 = shared key;; 1 = it does;; 0 = none.
Synthesis
Identity combines keys, certificates and revocation; HMAC authenticates high-volume messages between key holders, while certificates scale verification.
Continue studying — chapter 19 of 50
← Chapter 18 (draft) · Course chapter index · Chapter 20 (draft) →