Lesson plan

Cryptography for builders lesson plan

Secure the things you build, in Python on a Raspberry Pi: hashes, HMAC, how ciphers work and break, why homemade crypto fails, keys and secrets, and signed commands against replay, ending in a door lock that only opens for a signed, fresh command.

4 chapters, 7 lessons, about 6 hours, on the Raspberry Pi 4 Model B. Every lesson runs in the browser, with nothing to install.

The unit

Objectives and materials

By the end, students can

  • Fingerprints for data, and tags only a key holder can make.
  • How ciphers work, and the many ways homemade ones fail.
  • Keys that cannot be guessed, and commands that cannot be replayed.
  • A door that only opens for a signed, fresh command.

Materials for the whole unit

  • Hobby servo
  • Pushbutton
  • Raspberry Pi 4 Model B
  • Traffic light module

Every part is already on the simulator’s bench, so there is nothing to buy or hand out. The counts are the most any one lesson uses, if you build it on real breadboards too.

Chapter 1. About 1 h 40 min

Trust

Fingerprints for data, and tags only a key holder can make.

LessonTimeWhat students do
HashesA hash is a fingerprint for data: SHA-256 on the Pi, the avalanche effect that flips half the bits for one changed letter, a fixed size for any input, a firmware download checked against its published hash, and why a homemade hash falls over.50 min
  1. Run it: a fingerprint
  2. The avalanche
  3. Any size in, same size out
  4. Check a download
  5. Why not a homemade hash?
HMAC signaturesA hash anyone can compute proves nothing about who sent a message. HMAC mixes in a secret key: build it from SHA-256 exactly as RFC 2104 says, check it against the RFC 4231 test vector and Python's hmac module, and light green for a genuine command and red for a forgery.50 min
  1. Run it: a tag with a key
  2. Build HMAC from SHA-256
  3. Now use the library
  4. Green for genuine, red for forged
  5. What HMAC does and does not do

Materials

  • Pushbutton
  • Raspberry Pi 4 Model B
  • Traffic light module

Check for understanding

Each student finishes "HMAC signatures" on their own, without opening the hints. The simulator checks 5 of its 5 steps as they go.

Then ask each one to explain, in a sentence or two, the last thing it asked of them: "What HMAC does and does not do".

Chapter 2. About 1 h 40 min

Secrecy

How ciphers work, and the many ways homemade ones fail.

LessonTimeWhat students do
Symmetric ciphersEncryption with a shared key, from the bottom up and for understanding only: a Caesar cipher broken in 26 tries, the one-time pad that cannot be broken, a toy stream cipher built from SHA-256, and the nonce reuse mistake that hands an attacker your plaintext.50 min
  1. Run it: Caesar
  2. Break it
  3. The one-time pad
  4. A stream cipher from a short key
  5. Reuse a nonce, lose everything
Why not roll your ownAttack homemade security the way real attackers do: recover a repeating XOR key from one known word, flip SHUT to OPEN in a properly encrypted message that has no MAC, read a PIN out of how long a comparison takes, then close every hole with encrypt-then-MAC and a constant-time compare.50 min
  1. Run it: a homemade door code
  2. Known plaintext
  3. Encryption is not integrity
  4. A timing attack
  5. Do it properly: encrypt, then MAC

Materials

  • Pushbutton
  • Raspberry Pi 4 Model B
  • Traffic light module

Check for understanding

Each student finishes "Why not roll your own" on their own, without opening the hints. The simulator checks 5 of its 5 steps as they go.

Then ask each one to explain, in a sentence or two, the last thing it asked of them: "Do it properly: encrypt, then MAC".

Chapter 3. About 1 h 40 min

In practice

Keys that cannot be guessed, and commands that cannot be replayed.

LessonTimeWhat students do
Keys and secretsThe algorithm is public; the key is everything. See a key from a seeded random generator fall to an attacker in seconds, make keys from a secure random source, stretch a passphrase into a key with PBKDF2 and a salt, measure what iterations cost an attacker, and keep keys out of your code.50 min
  1. Run it: a predictable key
  2. A real random source
  3. A key from a passphrase
  4. Slow on purpose
  5. Keep keys out of the code
Signed messagesDesign a command format the way a protocol designer does: command, counter and HMAC tag in one line, a verifier that rejects tampering, a replay attack that a valid tag cannot stop, a counter that does, and where public-key signatures fit.50 min
  1. Run it: a signed command
  2. Verify, and refuse tampering
  3. The replay attack
  4. A counter beats replay
  5. MACs and real signatures

Materials

  • Pushbutton
  • Raspberry Pi 4 Model B
  • Traffic light module

Check for understanding

Each student finishes "Signed messages" on their own, without opening the hints. The simulator checks 5 of its 5 steps as they go.

Then ask each one to explain, in a sentence or two, the last thing it asked of them: "MACs and real signatures".

Chapter 4. About 50 min

Project

A door that only opens for a signed, fresh command.

LessonTimeWhat students do
Project: a signed door commandA real lock on the Pi: a servo bolt that opens for "unlock" typed on the Serial Monitor, then only for commands signed by the phone, then only for fresh ones, with the replay counter saved to a file so a power cut cannot reopen the hole.50 min
  1. Run it: an open door
  2. Only signed commands
  3. Stop the replay
  4. Survive a power cut
  5. Review your threat model

Materials

  • Hobby servo
  • Raspberry Pi 4 Model B

Check for understanding

Each student finishes "Project: a signed door command" on their own, without opening the hints. The simulator checks 5 of its 5 steps as they go.

Then ask each one to explain, in a sentence or two, the last thing it asked of them: "Review your threat model".

Your class

Teach this unit as a class

Start a class from this course and its 7 lessons are assigned in one go, in order. Students join with a six-character code or printed login cards, and each one’s work comes back to you.

The class tools are free for 30 days for one class of up to 20 students, with no card. The lessons themselves are free either way.