diff --git a/palm-client/Makefile b/palm-client/Makefile index 50e2328..3fd39dd 100644 --- a/palm-client/Makefile +++ b/palm-client/Makefile @@ -10,7 +10,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/convos.c src/protocol.c src/transport_srm.c src/session.c src/settings.c src/presets.c src/contacts.c src/contacts_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/fontmetrics.c src/ports.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/session.c src/settings.c src/presets.c src/contacts.c src/contacts_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/fontmetrics.c src/ports.c OBJS = $(SRCS:src/%.c=build/%.o) CC = m68k-palmos-gcc @@ -61,7 +61,7 @@ build/$(APP).prc: build/$(APP) build/res.stamp build-prc -o $@ $(APP).def build/$(APP) build/res/*.bin test: | build - gcc -std=c99 -Wall -Wextra -o build/test_protocol tests/test_protocol.c src/protocol.c + gcc -std=c99 -Wall -Wextra -o build/test_protocol tests/test_protocol.c src/protocol.c src/sha256.c build/test_protocol gcc -std=c99 -Wall -Wextra -o build/test_text tests/test_text.c src/ui/text.c src/ui/fontmetrics.c src/protocol.c build/test_text diff --git a/palm-client/resources/visormesh.rcp b/palm-client/resources/visormesh.rcp index e6297c6..fa78d29 100644 --- a/palm-client/resources/visormesh.rcp +++ b/palm-client/resources/visormesh.rcp @@ -17,6 +17,9 @@ BEGIN MENUITEM "Contacts" ID MainMenuContacts "C" MENUITEM "Node" ID MainMenuNode "N" MENUITEM "-" AUTOID + MENUITEM "Add Channel..." ID MainMenuAddChannel "A" + MENUITEM "Leave Channel" ID MainMenuLeaveChannel "L" + MENUITEM "-" AUTOID MENUITEM "Serial Ports..." ID MainMenuPorts "P" MENUITEM "Font Preview" ID MainMenuFontPreview "F" END @@ -201,3 +204,32 @@ BEGIN TITLE "Font Preview" BUTTON "Done" ID FontPreviewDoneButton AT (CENTER 147 AUTO AUTO) END + +/* Add a #hashtag channel, or join a private one with its key */ +FORM ID ChannelForm AT (0 0 160 160) +NOFRAME +USABLE +BEGIN + TITLE "Add Channel" + LABEL "Name:" AUTOID AT (5 20) FONT 1 + FIELD ID ChannelNameField AT (40 20 115 12) UNDERLINED SINGLELINE EDITABLE MAXCHARS 30 + LABEL "Type:" AUTOID AT (5 38) FONT 1 + 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 + FIELD ID ChannelKeyField AT (5 72 150 24) UNDERLINED MULTIPLELINES EDITABLE MAXCHARS 39 + LABEL "A #hashtag channel's key comes from its name:" AUTOID AT (5 104) + LABEL "anyone who knows the name can read it." AUTOID AT (5 115) + BUTTON "Add" ID ChannelAddButton AT (5 145 AUTO AUTO) + BUTTON "Cancel" ID ChannelCancelButton AT (PREVRIGHT+6 PREVTOP AUTO AUTO) + GRAFFITISTATEINDICATOR AT (145 147) +END + +ALERT ID LeaveChannelAlert +CONFIRMATION +DEFAULTBUTTON 1 +BEGIN + TITLE "Leave Channel" + MESSAGE "Remove ^1 from the radio? Its messages stay on the Visor." + BUTTONS "Leave" "Cancel" +END diff --git a/palm-client/src/channel_form.c b/palm-client/src/channel_form.c new file mode 100644 index 0000000..54b795b --- /dev/null +++ b/palm-client/src/channel_form.c @@ -0,0 +1,164 @@ +/* + * Add Channel: a #hashtag channel (key = first 16 bytes of SHA-256 of the + * name, so anyone who knows the name can read it) or a private channel joined + * with its 32-hex-digit key. Creating a new private channel isn't offered: + * the Visor has no good source of randomness for the key. + */ +#include +#include "resources.h" +#include "protocol.h" +#include "sha256.h" +#include "session.h" +#include "channels.h" +#include "app.h" +#include "ui/text.h" +#include "channel_form.h" + +static void *GetObject(FormType *form, UInt16 id) +{ + return FrmGetObjectPtr(form, FrmGetObjectIndex(form, id)); +} + +static const Char *FieldText(FormType *form, UInt16 id) +{ + const Char *t = FldGetTextPtr(GetObject(form, id)); + + return t ? t : ""; +} + +static Boolean IsPrivate(FormType *form) +{ + return FrmGetObjectId(form, FrmGetControlGroupSelection(form, ChannelTypeGroup)) == ChannelPrivatePush; +} + +/* The key field only matters for a private channel. */ +static void ShowKeyField(FormType *form) +{ + UInt16 label = FrmGetObjectIndex(form, ChannelKeyLabel), field = FrmGetObjectIndex(form, ChannelKeyField); + + if (IsPrivate(form)) { + FrmShowObject(form, label); + FrmShowObject(form, field); + } else { + FrmHideObject(form, label); + FrmHideObject(form, field); + } +} + +static void Error(const Char *msg) +{ + FrmCustomAlert(SettingsErrorAlert, msg, "", ""); +} + +static Boolean Add(FormType *form) +{ + const Char *typed = FieldText(form, ChannelNameField); + Char name[VM_NAME_SIZE + 1], msg[48]; + UInt8 key[VM_CHANNEL_SECRET_SIZE], utf8[VM_NAME_SIZE], hash[32]; + UInt8 maxChannels = SessionDevice()->maxChannels; + UInt16 n; + Int16 slot; + VmConv conv; + Err err; + + if (!SessionIsConnected()) { + Error("Connect to the radio first."); + return false; + } + if (!*typed) { + Error("Enter a channel name."); + return false; + } + if (IsPrivate(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)."); + return false; + } + } else { + /* hashtag: the key is derived from the exact name, '#' included */ + StrPrintF(name, "%s%s", typed[0] == '#' ? "" : "#", typed); + n = TxtPalmToUtf8(name, StrLen(name), utf8, VM_NAME_SIZE - 1); + if (n == 0) { + Error("That name is too long."); + return false; + } + VmSha256(utf8, n, hash); + MemMove(key, hash, sizeof(key)); + } + slot = ChannelsFreeSlot(maxChannels); + if (slot < 0) { + StrPrintF(msg, "All %u channel slots are in use.", maxChannels); + Error(msg); + return false; + } + err = ChannelsSet((UInt8)slot, name, key, maxChannels); + if (err != errNone) { + AppErrorText(err, msg); + Error(msg); + return false; + } + MemSet(&conv, sizeof(conv), 0); + conv.type = VM_CONV_CHANNEL; + conv.channel = (UInt8)slot; + AppOpenConversation(&conv); + return true; +} + +Boolean ChannelFormHandleEvent(EventType *event) +{ + FormType *form = FrmGetActiveForm(); + + switch (event->eType) { + case frmOpenEvent: + FrmSetControlGroupSelection(form, ChannelTypeGroup, ChannelHashtagPush); + FrmDrawForm(form); + ShowKeyField(form); + FrmSetFocus(form, FrmGetObjectIndex(form, ChannelNameField)); + return true; + case ctlSelectEvent: + switch (event->data.ctlSelect.controlID) { + case ChannelHashtagPush: + case ChannelPrivatePush: + ShowKeyField(form); + return true; + case ChannelAddButton: + Add(form); + return true; + case ChannelCancelButton: + FrmGotoForm(HomeForm); + return true; + } + break; + default: + break; + } + return false; +} + +void ChannelLeaveCurrent(void) +{ + VmChannel c; + Char name[VM_NAME_SIZE + 1], msg[48]; + Err err; + + if (FrmGetActiveFormID() != ConvoForm || gAppConv.type != VM_CONV_CHANNEL) { + Error("Open the channel you want to leave first."); + return; + } + if (!SessionIsConnected()) { + Error("Connect to the radio first."); + return; + } + StrCopy(name, ChannelsFindSlot(gAppConv.channel, &c) ? c.name : "this channel"); + if (FrmCustomAlert(LeaveChannelAlert, name, "", "") != 0) + return; + err = ChannelsRemove(gAppConv.channel, SessionDevice()->maxChannels); + if (err != errNone) { + AppErrorText(err, msg); + Error(msg); + return; + } + FrmGotoForm(HomeForm); +} diff --git a/palm-client/src/channel_form.h b/palm-client/src/channel_form.h new file mode 100644 index 0000000..2e81b44 --- /dev/null +++ b/palm-client/src/channel_form.h @@ -0,0 +1,10 @@ +#ifndef VM_CHANNEL_FORM_H +#define VM_CHANNEL_FORM_H + +#include + +Boolean ChannelFormHandleEvent(EventType *event); +/* Options > Leave Channel: asks, then empties the open channel's slot. */ +void ChannelLeaveCurrent(void); + +#endif diff --git a/palm-client/src/channels.c b/palm-client/src/channels.c index c374e05..d64b897 100644 --- a/palm-client/src/channels.c +++ b/palm-client/src/channels.c @@ -116,3 +116,39 @@ fail: DmRemoveRecord(gDb, i - 1); return err; } + +Int16 ChannelsFreeSlot(UInt8 maxChannels) +{ + VmChannel c; + UInt8 slot; + + for (slot = 0; slot < maxChannels; slot++) + if (!ChannelsFindSlot(slot, &c)) + return slot; + return -1; +} + +Err ChannelsSet(UInt8 slot, const Char *name, const UInt8 *secret, UInt8 maxChannels) +{ + UInt8 frame[VM_HEADER_LEN + 2 + VM_NAME_SIZE + VM_CHANNEL_SECRET_SIZE]; + UInt8 utf8[VM_NAME_SIZE]; + UInt16 n = TxtPalmToUtf8(name, StrLen(name), utf8, VM_NAME_SIZE - 1); + UInt16 len; + Err err; + + if (*name && n == 0) + return vmErrTooLong; + len = VmBuildSetChannel(frame, sizeof(frame), slot, utf8, n, secret); + err = SessionCommand(frame, len); + if (err == errNone) + err = ChannelsRefresh(maxChannels); + return err; +} + +Err ChannelsRemove(UInt8 slot, UInt8 maxChannels) +{ + UInt8 zero[VM_CHANNEL_SECRET_SIZE]; + + MemSet(zero, sizeof(zero), 0); + return ChannelsSet(slot, "", zero, maxChannels); +} diff --git a/palm-client/src/channels.h b/palm-client/src/channels.h index 88aa2df..da5b575 100644 --- a/palm-client/src/channels.h +++ b/palm-client/src/channels.h @@ -14,6 +14,13 @@ UInt16 ChannelsCount(void); Boolean ChannelsGet(UInt16 i, VmChannel *out); Boolean ChannelsFindSlot(UInt8 slot, VmChannel *out); +/* The first slot not in use (from the cache), or -1 if all maxChannels are used. */ +Int16 ChannelsFreeSlot(UInt8 maxChannels); +/* Stores a channel in a slot on the radio (name in the Visor's charset), then refreshes the cache. */ +Err ChannelsSet(UInt8 slot, const Char *name, const UInt8 *secret, UInt8 maxChannels); +/* Empties a slot on the radio (empty name, zero secret), then refreshes the cache. */ +Err ChannelsRemove(UInt8 slot, UInt8 maxChannels); + /* Asks the radio for slots 0..maxChannels-1 and replaces the cache with the ones in use. */ Err ChannelsRefresh(UInt8 maxChannels); diff --git a/palm-client/src/main.c b/palm-client/src/main.c index ed3d70c..d8de0a8 100644 --- a/palm-client/src/main.c +++ b/palm-client/src/main.c @@ -12,6 +12,7 @@ #include "settings.h" #include "ports.h" #include "preview_form.h" +#include "channel_form.h" #include "ui/draw.h" #define VM_MIN_ROM_VERSION sysMakeROMVersion(3, 5, 0, sysROMStageRelease, 0) @@ -40,6 +41,8 @@ static Boolean HandleMenu(UInt16 itemID) case MainMenuNode: FrmGotoForm(MainForm); return true; case MainMenuPorts: FrmGotoForm(PortsForm); return true; case MainMenuFontPreview: FrmGotoForm(FontPreviewForm); return true; + case MainMenuAddChannel: FrmGotoForm(ChannelForm); return true; + case MainMenuLeaveChannel: ChannelLeaveCurrent(); return true; } return false; } @@ -64,6 +67,7 @@ static Boolean AppHandleEvent(EventType *event) case SettingsForm: FrmSetEventHandler(form, SettingsFormHandleEvent); break; case PortsForm: FrmSetEventHandler(form, PortsFormHandleEvent); break; case FontPreviewForm: FrmSetEventHandler(form, FontPreviewFormHandleEvent); break; + case ChannelForm: FrmSetEventHandler(form, ChannelFormHandleEvent); break; } return true; } diff --git a/palm-client/src/protocol.c b/palm-client/src/protocol.c index c0ec6f9..03398a4 100644 --- a/palm-client/src/protocol.c +++ b/palm-client/src/protocol.c @@ -118,6 +118,33 @@ Boolean VmParseInt(const Char *s, Int32 *out) return 1; } +Boolean VmParseHex(const Char *s, UInt8 *out, UInt16 n) +{ + UInt16 got = 0; + UInt8 v = 0; + + for (; *s; s++) { + UInt8 d; + + if (*s == ' ') + continue; + if (*s >= '0' && *s <= '9') + d = (UInt8)(*s - '0'); + else if (*s >= 'a' && *s <= 'f') + d = (UInt8)(*s - 'a' + 10); + else if (*s >= 'A' && *s <= 'F') + d = (UInt8)(*s - 'A' + 10); + else + return 0; + if (got >= 2 * n) + return 0; + v = (UInt8)((v << 4) | d); + if (got++ & 1) + out[got / 2 - 1] = v; + } + return got == 2 * n; +} + UInt16 VmEncodeFrame(UInt8 *out, UInt16 outSize, const UInt8 *payload, UInt16 len) { if (len > VM_MAX_FRAME || outSize < (UInt16)(len + VM_HEADER_LEN)) @@ -293,6 +320,23 @@ UInt16 VmBuildGetChannel(UInt8 *out, UInt16 outSize, UInt8 idx) return VmEncodeFrame(out, outSize, p, 2); } +/* CMD_SET_CHANNEL: code, idx, name[32] (NUL-padded), secret[16]. */ +UInt16 VmBuildSetChannel(UInt8 *out, UInt16 outSize, UInt8 idx, const UInt8 *name, UInt16 nameLen, + const UInt8 *secret) +{ + UInt8 p[2 + VM_NAME_SIZE + VM_CHANNEL_SECRET_SIZE]; + UInt16 i; + + if (nameLen > VM_NAME_SIZE - 1) + return 0; + p[0] = VM_CMD_SET_CHANNEL; + p[1] = idx; + for (i = 0; i < VM_NAME_SIZE; i++) + p[2 + i] = i < nameLen ? name[i] : 0; + CopyBytes(p + 2 + VM_NAME_SIZE, secret, VM_CHANNEL_SECRET_SIZE); + return VmEncodeFrame(out, outSize, p, sizeof(p)); +} + /* CMD_SEND_TXT_MSG: code, txt_type, attempt, timestamp, recipient key prefix[6], text. */ UInt16 VmBuildSendTxt(UInt8 *out, UInt16 outSize, const UInt8 *prefix6, UInt32 timestamp, UInt8 attempt, const UInt8 *text, UInt16 textLen) diff --git a/palm-client/src/protocol.h b/palm-client/src/protocol.h index 33ea32e..d541a19 100644 --- a/palm-client/src/protocol.h +++ b/palm-client/src/protocol.h @@ -59,6 +59,7 @@ typedef char Char; #define VM_CMD_REBOOT 19 #define VM_CMD_DEVICE_QUERY 22 #define VM_CMD_GET_CHANNEL 31 +#define VM_CMD_SET_CHANNEL 32 #define VM_CMD_SET_PATH_HASH_MODE 61 /* Replies (radio -> app) */ @@ -197,6 +198,8 @@ Boolean VmParseDecimal(const Char *s, UInt16 scale, UInt32 *out) VM_SECT2; void VmFormatDecimal(Char *out, UInt32 value, UInt16 scale) VM_SECT2; /* Optional leading '-', then digits. */ Boolean VmParseInt(const Char *s, Int32 *out) VM_SECT2; +/* Exactly 2*n hex digits (spaces ignored) into n bytes. */ +Boolean VmParseHex(const Char *s, UInt8 *out, UInt16 n) VM_SECT2; /* Framing */ UInt16 VmEncodeFrame(UInt8 *out, UInt16 outSize, const UInt8 *payload, UInt16 len) VM_SECT2; @@ -214,6 +217,9 @@ UInt16 VmBuildSetRadioParams(UInt8 *out, UInt16 outSize, UInt32 freqKHz, UInt32 UInt16 VmBuildSetTxPower(UInt8 *out, UInt16 outSize, Int8 dbm) VM_SECT2; UInt16 VmBuildSetPathHashMode(UInt8 *out, UInt16 outSize, UInt8 mode) VM_SECT2; UInt16 VmBuildGetChannel(UInt8 *out, UInt16 outSize, UInt8 idx) VM_SECT2; +/* CMD_SET_CHANNEL: name (UTF-8, up to 31 bytes) and 16-byte secret; empty name + zero secret deletes. */ +UInt16 VmBuildSetChannel(UInt8 *out, UInt16 outSize, UInt8 idx, const UInt8 *name, UInt16 nameLen, + const UInt8 *secret) VM_SECT2; /* Direct message: text is UTF-8 bytes, prefix the first 6 bytes of the recipient's key. */ UInt16 VmBuildSendTxt(UInt8 *out, UInt16 outSize, const UInt8 *prefix6, UInt32 timestamp, UInt8 attempt, const UInt8 *text, UInt16 textLen) VM_SECT2; diff --git a/palm-client/src/resources.h b/palm-client/src/resources.h index 52b2366..7de4472 100644 --- a/palm-client/src/resources.h +++ b/palm-client/src/resources.h @@ -19,6 +19,8 @@ #define MainMenuHome 1003 #define MainMenuContacts 1004 #define MainMenuNode 1005 +#define MainMenuAddChannel 1006 +#define MainMenuLeaveChannel 1007 #define MainLinkGroup 1 #define SettingsForm 1200 @@ -64,6 +66,17 @@ #define AdvertAlert 2004 #define RetryAlert 2005 +#define LeaveChannelAlert 2006 + +#define ChannelForm 1700 +#define ChannelNameField 1701 +#define ChannelHashtagPush 1702 +#define ChannelPrivatePush 1703 +#define ChannelKeyLabel 1704 +#define ChannelKeyField 1705 +#define ChannelAddButton 1706 +#define ChannelCancelButton 1707 +#define ChannelTypeGroup 3 #define PortsForm 1100 #define PortsDoneButton 1101 diff --git a/palm-client/src/sections.h b/palm-client/src/sections.h index 2307344..f6b57c2 100644 --- a/palm-client/src/sections.h +++ b/palm-client/src/sections.h @@ -8,12 +8,12 @@ * - No static const tables and no string literals: those stay in the first * segment, and code in this one can't reach them PC-relative. Use plain * (non-const) data, which is A5-relative. - * On the host (tests) the attribute compiles away. + * On the host (tests) and in the editor's clang checker the attribute compiles away. */ #ifndef VM_SECTIONS_H #define VM_SECTIONS_H -#ifdef __palmos__ +#if defined(__palmos__) && !defined(__clang__) /* clang: the editor's checker, which rejects the section name */ #define VM_SECT2 __attribute__ ((section ("vmcode2"))) #else #define VM_SECT2 diff --git a/palm-client/src/sha256.c b/palm-client/src/sha256.c new file mode 100644 index 0000000..26c667e --- /dev/null +++ b/palm-client/src/sha256.c @@ -0,0 +1,74 @@ +#include "sha256.h" + +/* Not const: code in the second segment can't reach const data (sections.h). */ +static UInt32 K[64] = { + 0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL, 0x3956c25bUL, 0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL, + 0xd807aa98UL, 0x12835b01UL, 0x243185beUL, 0x550c7dc3UL, 0x72be5d74UL, 0x80deb1feUL, 0x9bdc06a7UL, 0xc19bf174UL, + 0xe49b69c1UL, 0xefbe4786UL, 0x0fc19dc6UL, 0x240ca1ccUL, 0x2de92c6fUL, 0x4a7484aaUL, 0x5cb0a9dcUL, 0x76f988daUL, + 0x983e5152UL, 0xa831c66dUL, 0xb00327c8UL, 0xbf597fc7UL, 0xc6e00bf3UL, 0xd5a79147UL, 0x06ca6351UL, 0x14292967UL, + 0x27b70a85UL, 0x2e1b2138UL, 0x4d2c6dfcUL, 0x53380d13UL, 0x650a7354UL, 0x766a0abbUL, 0x81c2c92eUL, 0x92722c85UL, + 0xa2bfe8a1UL, 0xa81a664bUL, 0xc24b8b70UL, 0xc76c51a3UL, 0xd192e819UL, 0xd6990624UL, 0xf40e3585UL, 0x106aa070UL, + 0x19a4c116UL, 0x1e376c08UL, 0x2748774cUL, 0x34b0bcb5UL, 0x391c0cb3UL, 0x4ed8aa4aUL, 0x5b9cca4fUL, 0x682e6ff3UL, + 0x748f82eeUL, 0x78a5636fUL, 0x84c87814UL, 0x8cc70208UL, 0x90befffaUL, 0xa4506cebUL, 0xbef9a3f7UL, 0xc67178f2UL +}; + +static UInt32 Rotr(UInt32 x, UInt16 n) VM_SECT2; +static void Block(UInt32 h[8], const UInt8 *p) VM_SECT2; + +static UInt32 Rotr(UInt32 x, UInt16 n) +{ + return (x >> n) | (x << (32 - n)); +} + +static void Block(UInt32 h[8], const UInt8 *p) +{ + UInt32 w[64], a, b, c, d, e, f, g, hh, t1, t2; + UInt16 i; + + for (i = 0; i < 16; i++) + w[i] = ((UInt32)p[4 * i] << 24) | ((UInt32)p[4 * i + 1] << 16) | ((UInt32)p[4 * i + 2] << 8) | p[4 * i + 3]; + for (i = 16; i < 64; i++) { + UInt32 s0 = Rotr(w[i - 15], 7) ^ Rotr(w[i - 15], 18) ^ (w[i - 15] >> 3); + UInt32 s1 = Rotr(w[i - 2], 17) ^ Rotr(w[i - 2], 19) ^ (w[i - 2] >> 10); + w[i] = w[i - 16] + s0 + w[i - 7] + s1; + } + a = h[0]; b = h[1]; c = h[2]; d = h[3]; e = h[4]; f = h[5]; g = h[6]; hh = h[7]; + for (i = 0; i < 64; i++) { + t1 = hh + (Rotr(e, 6) ^ Rotr(e, 11) ^ Rotr(e, 25)) + ((e & f) ^ (~e & g)) + K[i] + w[i]; + t2 = (Rotr(a, 2) ^ Rotr(a, 13) ^ Rotr(a, 22)) + ((a & b) ^ (a & c) ^ (b & c)); + hh = g; g = f; f = e; e = d + t1; d = c; c = b; b = a; a = t1 + t2; + } + h[0] += a; h[1] += b; h[2] += c; h[3] += d; h[4] += e; h[5] += f; h[6] += g; h[7] += hh; +} + +void VmSha256(const UInt8 *data, UInt32 len, UInt8 out[32]) +{ + UInt32 h[8] = { 0x6a09e667UL, 0xbb67ae85UL, 0x3c6ef372UL, 0xa54ff53aUL, + 0x510e527fUL, 0x9b05688cUL, 0x1f83d9abUL, 0x5be0cd19UL }; + UInt8 tail[128]; + UInt32 i, done = 0, bits = len * 8, n; + + for (; done + 64 <= len; done += 64) + Block(h, data + done); + /* last bytes, the 0x80 marker, zero padding and the 64-bit length (up to 2 blocks) */ + n = len - done; + for (i = 0; i < sizeof(tail); i++) + tail[i] = 0; + for (i = 0; i < n; i++) + tail[i] = data[done + i]; + tail[n] = 0x80; + n = (n + 9 <= 64) ? 64 : 128; + tail[n - 4] = (UInt8)(bits >> 24); + tail[n - 3] = (UInt8)(bits >> 16); + tail[n - 2] = (UInt8)(bits >> 8); + tail[n - 1] = (UInt8)bits; + Block(h, tail); + if (n == 128) + Block(h, tail + 64); + for (i = 0; i < 8; i++) { + out[4 * i] = (UInt8)(h[i] >> 24); + out[4 * i + 1] = (UInt8)(h[i] >> 16); + out[4 * i + 2] = (UInt8)(h[i] >> 8); + out[4 * i + 3] = (UInt8)h[i]; + } +} diff --git a/palm-client/src/sha256.h b/palm-client/src/sha256.h new file mode 100644 index 0000000..eacfa59 --- /dev/null +++ b/palm-client/src/sha256.h @@ -0,0 +1,13 @@ +/* + * SHA-256 (FIPS 180-4), for deriving hashtag channel keys: a "#name" + * channel's secret is the first 16 bytes of SHA-256("#name"). + * Plain C, tested on the host. In the second code segment (sections.h). + */ +#ifndef VM_SHA256_H +#define VM_SHA256_H + +#include "protocol.h" + +void VmSha256(const UInt8 *data, UInt32 len, UInt8 out[32]) VM_SECT2; + +#endif diff --git a/palm-client/tests/test_protocol.c b/palm-client/tests/test_protocol.c index 9148dec..a7af0d6 100644 --- a/palm-client/tests/test_protocol.c +++ b/palm-client/tests/test_protocol.c @@ -3,6 +3,7 @@ #include #include "../src/protocol.h" #include "fixtures.h" +#include "../src/sha256.h" static int failures; @@ -184,6 +185,61 @@ static void test_settings_builders(void) } } +static void test_sha256_and_channels(void) +{ + UInt8 h[32], out[64]; + static const UInt8 abc[32] = { + 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea, 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23, + 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c, 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad + }; + /* companion_protocol.md: hashtag channel #test has key 9cd8fcf22a47333b591d96a2b848b73f */ + static const UInt8 testKey[16] = { + 0x9c, 0xd8, 0xfc, 0xf2, 0x2a, 0x47, 0x33, 0x3b, 0x59, 0x1d, 0x96, 0xa2, 0xb8, 0x48, 0xb7, 0x3f + }; + UInt8 longMsg[200]; + static const UInt8 longDigest[4] = { 0xc2, 0xa9, 0x08, 0xd9 }; /* sha256(200 x 'a') starts c2a908d9 (Python hashlib) */ + + VmSha256((const UInt8 *)"abc", 3, h); + CHECK(memcmp(h, abc, 32) == 0); + VmSha256((const UInt8 *)"#test", 5, h); + CHECK(memcmp(h, testKey, 16) == 0); + memset(longMsg, 'a', sizeof(longMsg)); /* several blocks + a 2-block tail */ + VmSha256(longMsg, sizeof(longMsg), h); + CHECK(memcmp(h, longDigest, 4) == 0); + + { + /* padding boundaries: digest prefixes of n x 'x' from Python hashlib */ + static const struct { UInt16 n; UInt8 d[4]; } v[] = { + { 55, { 0xd5, 0xe2, 0x85, 0x68 } }, { 56, { 0x04, 0xc2, 0x62, 0x61 } }, + { 63, { 0x75, 0x22, 0x0b, 0x47 } }, { 64, { 0x7c, 0xe1, 0x00, 0x97 } }, + { 119, { 0x00, 0x0b, 0x48, 0xd4 } }, { 120, { 0x13, 0xf0, 0x5a, 0x0b } } + }; + UInt16 k; + + memset(longMsg, 'x', sizeof(longMsg)); + for (k = 0; k < sizeof(v) / sizeof(v[0]); k++) { + VmSha256(longMsg, v[k].n, h); + CHECK(memcmp(h, v[k].d, 4) == 0); + } + } + + { + UInt8 k[16]; + + CHECK(VmParseHex("9cd8fcf22a47333b591d96a2b848b73f", k, 16) && memcmp(k, testKey, 16) == 0); + CHECK(VmParseHex("9CD8 FCF2 2A47 333B 591D 96A2 B848 B73F", k, 16) && memcmp(k, testKey, 16) == 0); + CHECK(!VmParseHex("9cd8fcf22a47333b591d96a2b848b73", k, 16)); /* one digit short */ + CHECK(!VmParseHex("9cd8fcf22a47333b591d96a2b848b73f00", k, 16)); /* too long */ + CHECK(!VmParseHex("9cd8fcf22a47333b591d96a2b848b73g", k, 16)); /* not hex */ + } + + CHECK(VmBuildSetChannel(out, sizeof(out), 2, (const UInt8 *)"#test", 5, testKey) == 3 + 2 + 32 + 16); + CHECK(out[3] == 0x20 && out[4] == 2 && memcmp(out + 5, "#test", 5) == 0 && out[10] == 0); + CHECK(memcmp(out + 5 + 32, testKey, 16) == 0); + CHECK(VmBuildSetChannel(out, sizeof(out), 2, (const UInt8 *)"0123456789012345678901234567890123", 32, + testKey) == 0); +} + static void test_path_len(void) { CHECK(VM_PATH_HOPS(0x00) == 0 && VM_PATH_HASH_BYTES(0x00) == 1); @@ -379,6 +435,7 @@ int main(void) test_settings_builders(); test_decimals(); test_path_len(); + test_sha256_and_channels(); test_messaging(); test_parse_self_info(); test_parse_contacts();