ProtoCore v0.0.1
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
Totp - TOTP two-factor authentication (RFC 6238)

Layer: L7 Application ยท Build flags: PC_ENABLE_TOTP

What this example teaches

TOTP is the six-digit code an authenticator app (Google Authenticator, Authy) shows. This decodes a base32 shared secret, computes the current code, and verifies a submitted one within a +/-1 step window - so you can add a second factor to a protected route. GET /totp shows the current code (demo only) and GET /totp/verify?code=NNNNNN checks one.

Decode the secret once, then compute / verify against the clock:

int n = pc_base32_decode(SECRET_B32, g_secret, sizeof(g_secret)); // base32 -> bytes
g_secret_len = (n > 0) ? (size_t)n : 0;
uint32_t code = pc_totp(g_secret, g_secret_len, now_unix(), 30, 6); // 30s step, 6 digits
bool ok = pc_totp_verify(g_secret, g_secret_len, now_unix(), code, 30, 6, 1); // +/-1 step tolerance

The last argument to pc_totp_verify is the allowed step skew, which absorbs clock drift between the device and the authenticator. For codes that match a real app, sync the device clock to real time via NTP (SNTP); this example uses a fixed clock base so it is self-contained.

Security note. The /totp endpoint that reveals the live code exists only to make the demo testable - never expose the current code in production.

Build and run

pio ci --board=esp32dev --project-option="framework=arduino" \
--project-option="build_flags=-DPC_ENABLE_TOTP=1" \
--lib="." examples/L7-Application/Totp/Totp.ino
code=$(curl -s http://<ip>/totp)
curl "http://<ip>/totp/verify?code=$code" # {"ok":true}

Annotated source

The complete sketch (Totp.ino), reproduced verbatim with added explanatory comments:

// Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
// SPDX-License-Identifier: AGPL-3.0-or-later
#define PC_ENABLE_TOTP 1
#include "protocore.h"
static const char *SSID = "YOUR_SSID";
static const char *PASSWORD = "YOUR_PASSWORD";
// The shared secret, base32 (share this with the authenticator app at enrolment).
static const char *SECRET_B32 = "JBSWY3DPEHPK3PXP";
static uint8_t g_secret[32];
static size_t g_secret_len = 0;
PC server;
static uint64_t now_unix()
{
// Self-contained demo clock; replace with real (NTP) time for app-matching codes.
return 1700000000ull + millis() / 1000;
}
void setup()
{
Serial.begin(115200);
init_wifi_physical(SSID, PASSWORD);
while (!wifi_ready())
delay(250);
Serial.print("IP: ");
uint32_t ip = pc_net_egress_ip(); // library egress IP (network byte order), no Arduino WiFi
Serial.printf("IP: %u.%u.%u.%u\n", (unsigned)(ip & 0xFF), (unsigned)((ip >> 8) & 0xFF),
(unsigned)((ip >> 16) & 0xFF), (unsigned)((ip >> 24) & 0xFF));
int n = pc_base32_decode(SECRET_B32, g_secret, sizeof(g_secret));
g_secret_len = (n > 0) ? (size_t)n : 0;
server.on("/totp", HttpMethod::HTTP_GET, [](uint8_t id, HttpReq *) {
uint32_t code = pc_totp(g_secret, g_secret_len, now_unix(), 30, 6);
char b[16];
snprintf(b, sizeof(b), "%06u", code); // zero-pad to 6 digits
server.send(id, 200, "text/plain", b);
});
server.on("/totp/verify", HttpMethod::HTTP_GET, [](uint8_t id, HttpReq *req) {
const char *code_s = http_get_query(req, "code");
uint32_t code = code_s ? (uint32_t)strtoul(code_s, nullptr, 10) : 0;
bool ok = pc_totp_verify(g_secret, g_secret_len, now_unix(), code, 30, 6, 1);
server.send(id, 200, "application/json", ok ? "{\"ok\":true}" : "{\"ok\":false}");
});
server.begin(80);
}
void loop()
{
server.handle();
}
Single-port HTTP server with deterministic, zero-allocation execution.
Definition protocore.h:348
void send(uint8_t slot_id, int code, const char *content_type, const char *payload)
Send an HTTP response with a body and close the connection.
int32_t begin(const WebServerConfig *cfg=nullptr)
Initialize all connection slots and open all registered listeners.
void on(const char *path, HttpMethod method, Handler callback)
Register a route handler.
void handle()
Drive the server - call every Arduino loop() iteration.
const char * http_get_query(const HttpReq *req, const char *key)
Look up a query parameter value by name (case-sensitive).
bool init_wifi_physical(const char *, const char *)
Connect to a WiFi access point.
Definition physical.cpp:41
uint32_t pc_net_egress_ip(void)
IPv4 (network byte order) of the current egress interface, or 0 if none.
Definition physical.cpp:77
bool wifi_ready()
True if the WiFi station link is up (associated + an IP is assigned).
Definition physical.cpp:45
Layer 1 (Physical) - link bring-up and live egress-interface reporting.
Layer 7 (Application) - public HTTP routing API.
@ HTTP_GET
Safe, idempotent read.
Fully-parsed HTTP/1.1 request.
TOTP two-factor auth (RFC 6238) (PC_ENABLE_TOTP).