Practical Bench / IoT & Security · Advanced · 90 min
RFID access control with hashed IDs and lock-out
Build an RFID door controller that stores only salted hashes, locks out after repeated wrong cards, and measure false-accept and false-reject behaviour.
Why it matters
Access cards open offices, hostels and metro gates. The weak points are usually not the radio, but how IDs are stored and how many wrong tries are allowed.
Toy or everyday hack
Old transport and hotel cards or key-fob tags are ideal test tags
Safety
- Power the RC522 from 3.3 V only. 5 V can damage it.
- Use only your own tags and cards. Do not read or copy other peoples cards.
- This lab shows concepts. UID-only authentication is weak: many cards can be cloned. Do not use it for real security.
Parts list
| Part | Qty | Note |
|---|---|---|
| ESP32 DevKit | 1 | |
| RC522 RFID reader (13.56 MHz) | 1 | 3.3 V device |
| Tags or cards (at least 3) | 3 | Two to register, one unknown |
| Green and red LEDs + 330 Ω resistors | 2 | Green = unlocked |
| Jumper wires | 8 |
Tools and equipment
Connections
| From | To | Note |
|---|---|---|
| RC522 SDA (SS) | GPIO5 | |
| RC522 SCK / MOSI / MISO | GPIO18 / 23 / 19 | |
| RC522 RST | GPIO22 | |
| RC522 3.3V / GND | 3V3 / GND | |
| Green LED | GPIO25 via 330 Ω | Stands for the door |
| Red LED | GPIO27 via 330 Ω |
What you should see
Registered cards are granted, unknown cards are denied, and the response time is a few tens of milliseconds. A cloned UID is accepted, because the system only checks the UID: this is the lesson. Real systems use cards with cryptographic challenge-response so that a copied number is useless.
Step by step
- Wire the reader and the LEDs. Install the MFRC522 library and upload the sketch.
- Enrolment: present Card A. The Serial Monitor prints DENIED with a long hash. Copy that hash into ALLOWED[0]. Do the same for Card B.
- Change the salt in the sketch to your own secret text, then repeat the enrolment (hashes change when the salt changes).
- Re-upload and test: Card A and B must show GRANTED, an unknown card must show DENIED.
- Present the unknown card three times: the system must lock for 30 seconds and ignore everything.
- Run 10 trials: registered cards, unregistered cards and, if you have one, a card with a cloned UID. For each, write what you expected (G or D) and what happened.
- The logbook counts false accepts and false rejects.
Challenge: Replace the green LED with a 5 V relay and a 12 V solenoid lock supply. Add a flyback diode across the solenoid and explain why (see Practical 1).
Code (ESP32 DevKit, Arduino IDE)
Check pin numbers against your board. Test the sketch on your own hardware before a class demo.
// Practical 9 - RFID access control with hashed IDs and lock-out (ESP32 + RC522)
// RC522 is a 3.3 V device. Wire: SDA(SS)=5 SCK=18 MOSI=23 MISO=19 RST=22 3V3 GND
#include <SPI.h>
#include <MFRC522.h>
#include "mbedtls/sha256.h"
const uint8_t SS_PIN = 5, RST_PIN = 22, GREEN = 25, RED = 27;
MFRC522 rfid(SS_PIN, RST_PIN);
// Step 1: present each card you want to allow, copy its printed hash here, re-upload.
const char* ALLOWED[] = { "PASTE_HASH_OF_CARD_A", "PASTE_HASH_OF_CARD_B" };
const char* SALT = "change-this-salt"; // kept in firmware, makes the hash specific to YOUR lock
uint8_t fails = 0;
uint32_t lockedUntil = 0;
String hashUid(const MFRC522::Uid &u) {
uint8_t buf[64], out[32];
size_t n = strlen(SALT);
memcpy(buf, SALT, n); memcpy(buf + n, u.uidByte, u.size); n += u.size;
mbedtls_sha256_context c;
mbedtls_sha256_init(&c); mbedtls_sha256_starts(&c, 0);
mbedtls_sha256_update(&c, buf, n); mbedtls_sha256_finish(&c, out); mbedtls_sha256_free(&c);
String s; for (int i = 0; i < 32; i++) { if (out[i] < 16) s += '0'; s += String(out[i], HEX); }
return s;
}
void setup() {
Serial.begin(115200); SPI.begin(); rfid.PCD_Init();
pinMode(GREEN, OUTPUT); pinMode(RED, OUTPUT);
}
void loop() {
if (millis() < lockedUntil) return; // rate limiting
if (!rfid.PICC_IsNewCardPresent() || !rfid.PICC_ReadCardSerial()) return;
uint32_t t0 = millis();
String h = hashUid(rfid.uid);
bool ok = false;
for (auto a : ALLOWED) if (h == a) ok = true;
if (ok) {
fails = 0; digitalWrite(GREEN, HIGH);
Serial.printf("GRANTED,%lu ms\n", (unsigned long)(millis() - t0));
delay(3000); digitalWrite(GREEN, LOW); // 3 s "door open"
} else {
fails++; digitalWrite(RED, HIGH);
Serial.printf("DENIED,%lu ms,hash=%s\n", (unsigned long)(millis() - t0), h.c_str()); // lab only: remove in production
delay(500); digitalWrite(RED, LOW);
if (fails >= 3) { lockedUntil = millis() + 30000; fails = 0; Serial.println("LOCKED for 30 s"); }
}
rfid.PICC_HaltA(); rfid.PCD_StopCrypto1();
}Readings logbook
Type your measured values. Calculated columns and the analysis update as you type, and your entries are saved in this browser.
If it does not work
| Symptom | Likely cause and fix |
|---|---|
| Reader not found (version 0x00 or 0xFF) | Wiring or power issue. Check 3.3 V, SS and RST pins and that SPI wires are not crossed. |
| Card read fails randomly | Card held too briefly or at the edge of the antenna. Hold it flat for a second. |
| Hash does not match | You changed the salt after copying the hash. Re-enrol the cards. |
| Lock-out never triggers | Your code resets fails after a grant. Test with three unknown cards in a row. |