Files
led-controller/desktop/port/src/desktop_crypto.c
T
jimmyandClaude Opus 5.5 581b751904 feat: browser simulator served by the controller, desktop build, browser bridge
- led-simulator: the rig runs the C++ led-driver as WebAssembly (sim-driver/),
  one single-threaded worker per bar, no cross-origin isolation needed. Rooms
  (one JSON per room) with an on-page editor, and a pattern gallery. The
  Python server, Pyodide boot and ring buffer are removed.
- sim-controller sim_rig.c: serves /sim/ from an embedded pack, the page's
  WebSocket and the rooms/flash API. The desktop build embeds it too.
- bridge.c: a "browser" bridge kind that carries ESP-NOW frames to the page;
  devices announced through it are marked sim.
- UI: Simulator in the Menu, split view with resize and pop-out, swipe
  between controller and rig on phones; SIM/REAL badges in Devices.
- Fades: preset pushes and Wi-Fi driver bodies always carry fade and
  beat_time, so "off" no longer inherits an earlier fade.
- Also the sequences-v2 work in progress: desktop port, bridge firmware,
  serial bridge, Wi-Fi drivers, sequence timeline, audio and API changes.
- led-driver: ESP-NOW link, beat sync, control messages, sim hooks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 19:32:46 +13:00

92 lines
2.8 KiB
C

/* SHA-1 (FIPS 180-1) and base64 (RFC 4648) for the WebSocket handshake: no TLS library on the desktop */
#include "desktop_crypto.h"
#include <string.h>
static uint32_t rol(uint32_t v, int n)
{
return (v << n) | (v >> (32 - n));
}
static void sha1_block(uint32_t h[5], const uint8_t blk[64])
{
uint32_t w[80];
for (int i = 0; i < 16; i++) {
w[i] = ((uint32_t)blk[i * 4] << 24) | ((uint32_t)blk[i * 4 + 1] << 16) |
((uint32_t)blk[i * 4 + 2] << 8) | blk[i * 4 + 3];
}
for (int i = 16; i < 80; i++) {
w[i] = rol(w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16], 1);
}
uint32_t a = h[0], b = h[1], c = h[2], d = h[3], e = h[4];
for (int i = 0; i < 80; i++) {
uint32_t f, k;
if (i < 20) { f = (b & c) | (~b & d); k = 0x5A827999; }
else if (i < 40) { f = b ^ c ^ d; k = 0x6ED9EBA1; }
else if (i < 60) { f = (b & c) | (b & d) | (c & d); k = 0x8F1BBCDC; }
else { f = b ^ c ^ d; k = 0xCA62C1D6; }
uint32_t t = rol(a, 5) + f + e + k + w[i];
e = d;
d = c;
c = rol(b, 30);
b = a;
a = t;
}
h[0] += a;
h[1] += b;
h[2] += c;
h[3] += d;
h[4] += e;
}
void desktop_sha1(const uint8_t *data, size_t len, uint8_t out[20])
{
uint32_t h[5] = { 0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0 };
size_t i = 0;
for (; i + 64 <= len; i += 64) {
sha1_block(h, data + i);
}
// the tail: the rest, a 0x80 byte, zeros, and the message length in bits, in one or two blocks
uint8_t tail[128];
memset(tail, 0, sizeof(tail));
size_t rest = len - i;
memcpy(tail, data + i, rest);
tail[rest] = 0x80;
size_t tail_len = rest < 56 ? 64 : 128;
uint64_t bits = (uint64_t)len * 8;
for (int k = 0; k < 8; k++) {
tail[tail_len - 1 - k] = (uint8_t)(bits >> (8 * k));
}
sha1_block(h, tail);
if (tail_len == 128) {
sha1_block(h, tail + 64);
}
for (int k = 0; k < 5; k++) {
out[k * 4] = (uint8_t)(h[k] >> 24);
out[k * 4 + 1] = (uint8_t)(h[k] >> 16);
out[k * 4 + 2] = (uint8_t)(h[k] >> 8);
out[k * 4 + 3] = (uint8_t)h[k];
}
}
size_t desktop_base64_encode(char *out, size_t cap, const uint8_t *in, size_t n)
{
static const char tbl[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
size_t need = ((n + 2) / 3) * 4;
if (cap < need + 1) {
return 0;
}
size_t o = 0;
for (size_t i = 0; i < n; i += 3) {
uint32_t v = (uint32_t)in[i] << 16;
if (i + 1 < n) v |= (uint32_t)in[i + 1] << 8;
if (i + 2 < n) v |= in[i + 2];
out[o++] = tbl[(v >> 18) & 63];
out[o++] = tbl[(v >> 12) & 63];
out[o++] = (i + 1 < n) ? tbl[(v >> 6) & 63] : '=';
out[o++] = (i + 2 < n) ? tbl[v & 63] : '=';
}
out[o] = '\0';
return o;
}