palm-client: Add Channel's New Key (private channels) fills in a random key from a pool fed by what can't be predicted: the exact tick and position of every pen and key event, and the signal readings of every packet the radio reports. Samples are folded into a SHA-256 pool, a key is a hash of it, and the pool moves on after each; it's kept between launches in an unsaved preference (never in a backup). Until 256 samples have come in, New Key asks for a few seconds of stylus movement. Host-tested in tests/test_entropy.c
This commit is contained in:
1 parent
2c32033292
commit
4b05bf25d5
9 files changed
+299
-2
No files matched your search
@@ -12,7 +12,7 @@ APP = visormesh
|
||||
NAME = VisorMesh
|
||||
CREATOR = VMsh
|
||||
|
||||
SRCS = src/main.c src/app.c src/home_form.c src/convo_form.c src/node_form.c src/channel_form.c src/convos.c src/protocol.c src/sha256.c src/transport_srm.c src/transport_sb.c src/sb_spi.c src/session.c src/request.c src/settings.c src/presets.c src/contacts.c src/contacts_form.c src/contact_form.c src/heard.c src/remote_fmt.c src/remote.c src/remote_form.c src/messages.c src/channels.c src/preview_form.c src/ui/draw.c src/ui/text.c src/ui/rowlist.c src/ui/title.c src/ui/fontmetrics.c src/ports.c src/sounds.c src/mutes.c src/scopes.c src/scope_proto.c src/map_geo.c src/map_form.c src/ui/emoji.c src/bgcheck.c src/pins.c src/shots.c src/db.c src/reaction_fmt.c src/reactions.c src/rxpath.c src/msg_menu.c src/protocol2.c src/info_fmt.c src/screen_fmt.c src/stats_form.c src/sb_test.c src/packets_form.c src/trace_form.c src/blocks.c src/beam.c src/beam_rx.c src/stale.c src/convo_extra.c src/neigh.c
|
||||
SRCS = src/main.c src/app.c src/home_form.c src/convo_form.c src/node_form.c src/channel_form.c src/convos.c src/protocol.c src/sha256.c src/transport_srm.c src/transport_sb.c src/sb_spi.c src/session.c src/request.c src/settings.c src/presets.c src/contacts.c src/contacts_form.c src/contact_form.c src/heard.c src/remote_fmt.c src/remote.c src/remote_form.c src/messages.c src/channels.c src/preview_form.c src/ui/draw.c src/ui/text.c src/ui/rowlist.c src/ui/title.c src/ui/fontmetrics.c src/ports.c src/sounds.c src/mutes.c src/scopes.c src/scope_proto.c src/map_geo.c src/map_form.c src/ui/emoji.c src/bgcheck.c src/pins.c src/shots.c src/db.c src/reaction_fmt.c src/reactions.c src/rxpath.c src/msg_menu.c src/protocol2.c src/info_fmt.c src/screen_fmt.c src/entropy.c src/stats_form.c src/sb_test.c src/packets_form.c src/trace_form.c src/blocks.c src/beam.c src/beam_rx.c src/stale.c src/convo_extra.c src/neigh.c
|
||||
OBJS = $(SRCS:src/%.c=build/%.o)
|
||||
|
||||
CC = m68k-palmos-gcc
|
||||
@@ -88,6 +88,8 @@ test: | build
|
||||
build/test_text
|
||||
gcc -std=c99 -Wall -Wextra -o build/test_sblora tests/test_sblora.c src/sb_spi.c
|
||||
build/test_sblora
|
||||
gcc -std=c99 -Wall -Wextra -o build/test_entropy tests/test_entropy.c src/entropy.c src/sha256.c
|
||||
build/test_entropy
|
||||
gcc -std=c99 -Wall -Wextra -o build/test_request tests/test_request.c src/request.c
|
||||
build/test_request
|
||||
gcc -std=c99 -Wall -Wextra -Isrc -o build/test_screens tests/test_screens.c src/screen_fmt.c src/ui/text.c src/ui/fontmetrics.c src/protocol.c
|
||||
|
||||
@@ -679,6 +679,7 @@ BEGIN
|
||||
PUSHBUTTON "#hashtag" ID ChannelHashtagPush AT (40 37 50 12) GROUP ChannelTypeGroup
|
||||
PUSHBUTTON "Private" ID ChannelPrivatePush AT (PREVRIGHT+1 PREVTOP 50 12) GROUP ChannelTypeGroup
|
||||
LABEL "Key (32 hex digits):" ID ChannelKeyLabel AT (5 58) FONT 1
|
||||
BUTTON "New Key" ID ChannelNewKeyButton AT (110 57 AUTO AUTO)
|
||||
FIELD ID ChannelKeyField AT (5 72 150 24) UNDERLINED MULTIPLELINES EDITABLE MAXCHARS 39
|
||||
LABEL "A #hashtag channel's key comes" AUTOID AT (5 104)
|
||||
LABEL "from its name: anyone who knows" AUTOID AT (5 115)
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
#include "channels.h"
|
||||
#include "app.h"
|
||||
#include "ui/text.h"
|
||||
#include "entropy.h"
|
||||
#include "channel_form.h"
|
||||
|
||||
/* In the fourth code segment: see sections.h for the rules. */
|
||||
@@ -22,6 +23,7 @@ static void ShowKeyField(FormType *form) VM_SECT4;
|
||||
static void Error(const Char *msg) VM_SECT4;
|
||||
static Boolean HashtagKey(const Char *name, UInt8 *key) VM_SECT4;
|
||||
static Boolean Add(FormType *form) VM_SECT4;
|
||||
static void NewKey(FormType *form) VM_SECT4;
|
||||
|
||||
static void *GetObject(FormType *form, UInt16 id)
|
||||
{
|
||||
@@ -44,13 +46,16 @@ static Boolean IsPrivate(FormType *form)
|
||||
static void ShowKeyField(FormType *form)
|
||||
{
|
||||
UInt16 label = FrmGetObjectIndex(form, ChannelKeyLabel), field = FrmGetObjectIndex(form, ChannelKeyField);
|
||||
UInt16 button = FrmGetObjectIndex(form, ChannelNewKeyButton);
|
||||
|
||||
if (IsPrivate(form)) {
|
||||
FrmShowObject(form, label);
|
||||
FrmShowObject(form, field);
|
||||
FrmShowObject(form, button);
|
||||
} else {
|
||||
FrmHideObject(form, label);
|
||||
FrmHideObject(form, field);
|
||||
FrmHideObject(form, button);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -135,7 +140,7 @@ static Boolean Add(FormType *form)
|
||||
StrNCopy(name, typed, VM_NAME_SIZE - 1);
|
||||
name[VM_NAME_SIZE - 1] = 0;
|
||||
if (!VmParseHex(FieldText(form, ChannelKeyField), key, sizeof(key))) {
|
||||
Error("The key is 32 hex digits (0-9, a-f).");
|
||||
Error("The key is 32 hex digits (0-9, a-f). For a new channel, tap New Key.");
|
||||
return false;
|
||||
}
|
||||
return ChannelJoin(name, key);
|
||||
@@ -145,6 +150,26 @@ static Boolean Add(FormType *form)
|
||||
return ChannelJoin(name, NULL);
|
||||
}
|
||||
|
||||
/* A key from the pool (entropy.c), which needs a moment of stylus use after a fresh start. */
|
||||
static void NewKey(FormType *form)
|
||||
{
|
||||
FieldType *field = GetObject(form, ChannelKeyField);
|
||||
UInt8 key[VM_CHANNEL_SECRET_SIZE];
|
||||
Char hex[2 * VM_CHANNEL_SECRET_SIZE + 1], msg[96];
|
||||
|
||||
if (!EntropyKey(key, sizeof(key))) {
|
||||
StrPrintF(msg, "Move the stylus around the screen for a few seconds, then tap New Key again (%u%% there).",
|
||||
EntropyCount() * 100 / VM_ENTROPY_MIN);
|
||||
Error(msg);
|
||||
return;
|
||||
}
|
||||
TxtHex(key, sizeof(key), hex);
|
||||
MemSet(key, sizeof(key), 0);
|
||||
FldDelete(field, 0, FldGetTextLength(field));
|
||||
FldInsert(field, hex, StrLen(hex));
|
||||
MemSet(hex, sizeof(hex), 0);
|
||||
}
|
||||
|
||||
Boolean ChannelFormHandleEvent(EventType *event)
|
||||
{
|
||||
FormType *form = FrmGetActiveForm();
|
||||
@@ -165,6 +190,9 @@ Boolean ChannelFormHandleEvent(EventType *event)
|
||||
case ChannelAddButton:
|
||||
Add(form);
|
||||
return true;
|
||||
case ChannelNewKeyButton:
|
||||
NewKey(form);
|
||||
return true;
|
||||
case ChannelCancelButton:
|
||||
FrmGotoForm(HomeForm);
|
||||
return true;
|
||||
|
||||
@@ -0,0 +1,110 @@
|
||||
#include "entropy.h"
|
||||
#include "sha256.h"
|
||||
|
||||
static void Stir(void) VM_SECT5;
|
||||
static void Next(UInt8 tag) VM_SECT5;
|
||||
|
||||
#define BUF 64
|
||||
|
||||
static UInt8 gPool[VM_ENTROPY_STATE];
|
||||
static UInt8 gBuf[BUF];
|
||||
static UInt16 gPos = 0, gCount = 0;
|
||||
|
||||
/* pool = SHA-256(pool || buffer) */
|
||||
static void Stir(void)
|
||||
{
|
||||
UInt8 in[VM_ENTROPY_STATE + BUF];
|
||||
UInt16 i;
|
||||
|
||||
for (i = 0; i < VM_ENTROPY_STATE; i++)
|
||||
in[i] = gPool[i];
|
||||
for (i = 0; i < BUF; i++)
|
||||
in[VM_ENTROPY_STATE + i] = gBuf[i];
|
||||
VmSha256(in, sizeof(in), gPool);
|
||||
gPos = 0;
|
||||
}
|
||||
|
||||
/* pool = SHA-256(pool || tag): a one-way step, so an output never shows the pool */
|
||||
static void Next(UInt8 tag)
|
||||
{
|
||||
UInt8 in[VM_ENTROPY_STATE + 1];
|
||||
UInt16 i;
|
||||
|
||||
for (i = 0; i < VM_ENTROPY_STATE; i++)
|
||||
in[i] = gPool[i];
|
||||
in[VM_ENTROPY_STATE] = tag;
|
||||
VmSha256(in, sizeof(in), gPool);
|
||||
}
|
||||
|
||||
void EntropyAdd(const UInt8 *p, UInt16 n, Boolean credit)
|
||||
{
|
||||
while (n--) {
|
||||
gBuf[gPos] ^= *p++;
|
||||
if (++gPos == BUF)
|
||||
Stir();
|
||||
}
|
||||
if (credit && gCount < 2 * VM_ENTROPY_MIN)
|
||||
gCount++;
|
||||
}
|
||||
|
||||
void EntropyEvent(UInt32 ticks, UInt16 type, Int16 x, Int16 y, UInt16 chr, Boolean credit)
|
||||
{
|
||||
UInt8 s[12];
|
||||
|
||||
s[0] = (UInt8)ticks; /* the low bits jitter most: first */
|
||||
s[1] = (UInt8)(ticks >> 8);
|
||||
s[2] = (UInt8)(ticks >> 16);
|
||||
s[3] = (UInt8)(ticks >> 24);
|
||||
s[4] = (UInt8)type;
|
||||
s[5] = (UInt8)(type >> 8);
|
||||
s[6] = (UInt8)x;
|
||||
s[7] = (UInt8)((UInt16)x >> 8);
|
||||
s[8] = (UInt8)y;
|
||||
s[9] = (UInt8)((UInt16)y >> 8);
|
||||
s[10] = (UInt8)chr;
|
||||
s[11] = (UInt8)(chr >> 8);
|
||||
EntropyAdd(s, sizeof(s), credit);
|
||||
}
|
||||
|
||||
UInt16 EntropyCount(void)
|
||||
{
|
||||
return gCount;
|
||||
}
|
||||
|
||||
Boolean EntropyKey(UInt8 *out, UInt16 n)
|
||||
{
|
||||
UInt8 in[VM_ENTROPY_STATE + 1], h[32];
|
||||
UInt16 i;
|
||||
|
||||
if (gCount < VM_ENTROPY_MIN || n > sizeof(h))
|
||||
return 0;
|
||||
Stir();
|
||||
for (i = 0; i < VM_ENTROPY_STATE; i++)
|
||||
in[i] = gPool[i];
|
||||
in[VM_ENTROPY_STATE] = 'K';
|
||||
VmSha256(in, sizeof(in), h);
|
||||
for (i = 0; i < n; i++)
|
||||
out[i] = h[i];
|
||||
Next('N');
|
||||
return 1;
|
||||
}
|
||||
|
||||
void EntropySave(UInt8 state[VM_ENTROPY_STATE], UInt16 *count)
|
||||
{
|
||||
UInt16 i;
|
||||
|
||||
Stir();
|
||||
Next('S'); /* what's saved isn't the pool a key came from */
|
||||
for (i = 0; i < VM_ENTROPY_STATE; i++)
|
||||
state[i] = gPool[i];
|
||||
*count = gCount;
|
||||
}
|
||||
|
||||
/* Mixed in, not copied over: samples from this launch so far stay. */
|
||||
void EntropyLoad(const UInt8 state[VM_ENTROPY_STATE], UInt16 count)
|
||||
{
|
||||
EntropyAdd(state, VM_ENTROPY_STATE, 0);
|
||||
Stir();
|
||||
if (count > gCount)
|
||||
gCount = count > 2 * VM_ENTROPY_MIN ? 2 * VM_ENTROPY_MIN : count;
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* A random-number pool for new channel keys. Palm OS's SysRandom is a
|
||||
* seeded LCG, so the pool gathers what can't be predicted instead: the
|
||||
* exact tick of every pen and key event, where the pen was, and the signal
|
||||
* readings of every packet the radio reports. Samples go into a 64-byte
|
||||
* buffer that's folded into a SHA-256 pool each time it fills; a key is a
|
||||
* hash of the pool, which then moves on, so a key never shows the pool.
|
||||
*
|
||||
* The pool lives across launches in an unsaved preference (app.c), never
|
||||
* in a HotSync backup. Pure C: tests/test_entropy.c drives it on the host.
|
||||
*/
|
||||
#ifndef VM_ENTROPY_H
|
||||
#define VM_ENTROPY_H
|
||||
|
||||
#include "protocol.h"
|
||||
|
||||
#define VM_ENTROPY_STATE 32
|
||||
#define VM_ENTROPY_MIN 256 /* samples before a key: at half a bit each, 128 bits */
|
||||
|
||||
/* n bytes; credit counts it as one sample toward VM_ENTROPY_MIN. */
|
||||
void EntropyAdd(const UInt8 *p, UInt16 n, Boolean credit) VM_SECT5;
|
||||
/* An event from the loop; pen and key events count as samples. */
|
||||
void EntropyEvent(UInt32 ticks, UInt16 type, Int16 x, Int16 y, UInt16 chr, Boolean credit) VM_SECT5;
|
||||
/* Samples so far, up to VM_ENTROPY_MIN and a bit over. */
|
||||
UInt16 EntropyCount(void) VM_SECT5;
|
||||
/* n random bytes (n <= 32); false while there are too few samples. */
|
||||
Boolean EntropyKey(UInt8 *out, UInt16 n) VM_SECT5;
|
||||
|
||||
/* For keeping it between launches: the pool (folded) and its count. */
|
||||
void EntropySave(UInt8 state[VM_ENTROPY_STATE], UInt16 *count) VM_SECT5;
|
||||
void EntropyLoad(const UInt8 state[VM_ENTROPY_STATE], UInt16 count) VM_SECT5;
|
||||
|
||||
#endif
|
||||
@@ -24,6 +24,7 @@
|
||||
#include "channel_form.h"
|
||||
#include "transport.h"
|
||||
#include "bgcheck.h"
|
||||
#include "entropy.h"
|
||||
#include "shots.h"
|
||||
#include "stats_form.h"
|
||||
#include "sb_test.h"
|
||||
@@ -52,6 +53,34 @@ static Err RomVersionCompatible(UInt32 required, UInt16 launchFlags)
|
||||
return sysErrRomIncompatible;
|
||||
}
|
||||
|
||||
/* The key pool between launches: an unsaved preference, so it never goes into a backup. */
|
||||
#define VM_ENTROPY_PREFS_ID 10
|
||||
|
||||
typedef struct {
|
||||
UInt8 state[VM_ENTROPY_STATE];
|
||||
UInt16 count;
|
||||
} EntropyPrefs;
|
||||
|
||||
static void LoadEntropy(void)
|
||||
{
|
||||
EntropyPrefs p;
|
||||
UInt16 size = sizeof(p);
|
||||
|
||||
if (PrefGetAppPreferences(VM_CREATOR, VM_ENTROPY_PREFS_ID, &p, &size, false) != noPreferenceFound &&
|
||||
size == sizeof(p))
|
||||
EntropyLoad(p.state, p.count);
|
||||
MemSet(&p, sizeof(p), 0);
|
||||
}
|
||||
|
||||
static void SaveEntropy(void)
|
||||
{
|
||||
EntropyPrefs p;
|
||||
|
||||
EntropySave(p.state, &p.count);
|
||||
PrefSetAppPreferences(VM_CREATOR, VM_ENTROPY_PREFS_ID, 1, &p, sizeof(p), false);
|
||||
MemSet(&p, sizeof(p), 0);
|
||||
}
|
||||
|
||||
static void Connect(void);
|
||||
|
||||
/* Go, Channel and Options commands, whichever screen's menu they're on; Edit is the form's. */
|
||||
@@ -288,6 +317,10 @@ static void AppEventLoop(void)
|
||||
FrmUpdateForm(FrmGetActiveFormID(), VM_UPDATE_DATA);
|
||||
}
|
||||
EvtGetEvent(&event, EventTimeout());
|
||||
EntropyEvent(TimGetTicks(), event.eType, event.screenX, event.screenY,
|
||||
event.eType == keyDownEvent ? event.data.keyDown.chr : 0,
|
||||
event.eType == penDownEvent || event.eType == penMoveEvent ||
|
||||
event.eType == penUpEvent || event.eType == keyDownEvent);
|
||||
CheckWake();
|
||||
if (event.eType == nilEvent) {
|
||||
if (BeamInboxWaiting())
|
||||
@@ -342,10 +375,12 @@ UInt32 PilotMain(UInt16 cmd, MemPtr cmdPBP, UInt16 launchFlags)
|
||||
BeamInboxNote();
|
||||
UiFontInit(SoundsLargeText());
|
||||
HeardLoad();
|
||||
LoadEntropy();
|
||||
FrmGotoForm(HomeForm);
|
||||
AppEventLoop();
|
||||
SessionDisconnect();
|
||||
HeardSave();
|
||||
SaveEntropy();
|
||||
FrmCloseAllForms();
|
||||
ContactsClose();
|
||||
ChannelsClose();
|
||||
|
||||
@@ -147,6 +147,7 @@
|
||||
#define ChannelKeyField 1705
|
||||
#define ChannelAddButton 1706
|
||||
#define ChannelCancelButton 1707
|
||||
#define ChannelNewKeyButton 1708
|
||||
#define ChannelTypeGroup 3
|
||||
|
||||
#define PortsForm 1100
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
#include "ui/text.h"
|
||||
#include "session.h"
|
||||
#include "request.h"
|
||||
#include "entropy.h"
|
||||
#include "heard.h"
|
||||
#include "remote.h"
|
||||
#include "sounds.h"
|
||||
@@ -123,6 +124,8 @@ static void HandlePush(void)
|
||||
} else if (code == VM_PUSH_LOG_RX) {
|
||||
VmRxLog rx;
|
||||
|
||||
/* the signal readings and the packet as heard: one sample for new keys */
|
||||
EntropyAdd(gDecoder.payload, gDecoder.len < 24 ? gDecoder.len : 24, true);
|
||||
if (VmParseRxLog(gDecoder.payload, gDecoder.len, &rx)) {
|
||||
HeardRecord(&rx, SessionNow());
|
||||
RxPathNote(&rx, SessionNow());
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
* Host tests for the key pool (src/entropy.c). Run with `make test`.
|
||||
* They can't measure randomness; they check the plumbing: no key before
|
||||
* enough samples, uncredited input not counting, keys never repeating, and
|
||||
* a saved pool carrying over.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "../src/entropy.h"
|
||||
|
||||
static int failures;
|
||||
|
||||
#define CHECK(cond) do { \
|
||||
if (!(cond)) { printf("FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); failures++; } \
|
||||
} while (0)
|
||||
|
||||
/* n pen moves, a tick apart, from a start time. */
|
||||
static void Scribble(int n, UInt32 start)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < n; i++)
|
||||
EntropyEvent(start + (UInt32)i * 3, 2, (Int16)(i % 160), (Int16)(i * 7 % 160), 0, 1);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
UInt8 a[16], b[16], c[16], state[VM_ENTROPY_STATE], state2[VM_ENTROPY_STATE];
|
||||
UInt16 count, i, ones;
|
||||
int k;
|
||||
|
||||
/* nothing yet */
|
||||
CHECK(!EntropyKey(a, 16));
|
||||
|
||||
/* uncredited input (nil events, a reloaded pool) doesn't count */
|
||||
for (k = 0; k < 1000; k++)
|
||||
EntropyEvent((UInt32)k, 0, 0, 0, 0, 0);
|
||||
CHECK(EntropyCount() == 0 && !EntropyKey(a, 16));
|
||||
|
||||
Scribble(VM_ENTROPY_MIN - 1, 1000);
|
||||
CHECK(!EntropyKey(a, 16));
|
||||
Scribble(1, 5000);
|
||||
CHECK(EntropyCount() == VM_ENTROPY_MIN);
|
||||
CHECK(EntropyKey(a, 16));
|
||||
|
||||
/* the next key differs, with no new input */
|
||||
CHECK(EntropyKey(b, 16));
|
||||
CHECK(memcmp(a, b, 16) != 0);
|
||||
|
||||
/* more than 32 bytes can't be had in one go */
|
||||
CHECK(!EntropyKey(c, 33));
|
||||
|
||||
/* keys look like random bytes: about half their bits set over many */
|
||||
for (k = 0, ones = 0; k < 256; k++) {
|
||||
CHECK(EntropyKey(c, 16));
|
||||
for (i = 0; i < 16; i++) {
|
||||
UInt8 v = c[i];
|
||||
while (v) {
|
||||
ones += v & 1;
|
||||
v >>= 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
CHECK(ones > 256 * 128 / 2 - 600 && ones < 256 * 128 / 2 + 600);
|
||||
|
||||
/* save, then a fresh launch: the saved pool and count carry over, mixed in */
|
||||
EntropySave(state, &count);
|
||||
CHECK(count >= VM_ENTROPY_MIN);
|
||||
EntropySave(state2, &count);
|
||||
CHECK(memcmp(state, state2, VM_ENTROPY_STATE) != 0); /* saving moves the pool on */
|
||||
EntropyLoad(state, count);
|
||||
CHECK(EntropyKey(a, 16));
|
||||
|
||||
/* the count is capped, so an old count can't be inflated */
|
||||
EntropyLoad(state, 60000);
|
||||
CHECK(EntropyCount() <= 2 * VM_ENTROPY_MIN);
|
||||
|
||||
if (failures) {
|
||||
printf("%d failure(s)\n", failures);
|
||||
return 1;
|
||||
}
|
||||
printf("entropy: all tests passed\n");
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user