diff --git a/comms.c b/comms.c index 614d0dd..67f2410 100644 --- a/comms.c +++ b/comms.c @@ -13,7 +13,7 @@ _strobe_state_size() } void -comms_init(struct comms_state *cs, process_msgfunc_t pmf) +comms_init(struct comms_state *cs, process_msgfunc_t pmf, struct pktbuf *shared) { *cs = (struct comms_state){ @@ -23,9 +23,11 @@ comms_init(struct comms_state *cs, process_msgfunc_t pmf) strobe_init(&cs->cs_start, domain, sizeof domain - 1); + if (shared != NULL) + strobe_key(&cs->cs_start, SYM_KEY, shared->pkt, shared->pktlen); + /* copy starting state over to initial state */ cs->cs_state = cs->cs_start; - } #define CONFIRMED_STR_BASE "confirmed" diff --git a/comms.h b/comms.h index e53062d..b9e44fb 100644 --- a/comms.h +++ b/comms.h @@ -27,5 +27,5 @@ struct comms_state { size_t _strobe_state_size(); -void comms_init(struct comms_state *, process_msgfunc_t); +void comms_init(struct comms_state *, process_msgfunc_t, struct pktbuf *); void comms_process(struct comms_state *, struct pktbuf, struct pktbuf *); diff --git a/lora.py b/lora.py index 2a2e045..a05eadd 100644 --- a/lora.py +++ b/lora.py @@ -22,9 +22,11 @@ CMD_RUNFOR = 3 # arg: (chan, length): turns on chan for length seconds class LORANode(object): '''Implement a LORANode initiator.''' - def __init__(self, syncdatagram): + def __init__(self, syncdatagram, shared=None): self.sd = syncdatagram self.st = Strobe(domain, F=KeccakF(800)) + if shared is not None: + self.st.key(shared) async def start(self): msg = self.st.send_enc(os.urandom(16) + b'reqreset') + \ @@ -177,10 +179,14 @@ def timeout(timeout): class TestLORANode(unittest.IsolatedAsyncioTestCase): @timeout(2) async def test_lora(self): + shared_key = os.urandom(32) + class TestSD(MockSyncDatagram): async def runner(self): l = Strobe(domain, F=KeccakF(800)) + l.key(shared_key) + # start handshake r = await self.get() @@ -234,7 +240,7 @@ class TestLORANode(unittest.IsolatedAsyncioTestCase): l.send_mac(8)) tsd = TestSD() - l = LORANode(tsd) + l = LORANode(tsd, shared=shared_key) await l.start() @@ -308,12 +314,15 @@ class TestLORANode(unittest.IsolatedAsyncioTestCase): # pass the reply back await self.put(outbytes[:outbuf.pktlen]) + # Generate shared key + shared_key = os.urandom(32) + # Initialize everything - lora_comms.comms_init(commstate, cb) + lora_comms.comms_init(commstate, cb, make_pktbuf(shared_key)) # Create test fixture tsd = CCodeSD() - l = LORANode(tsd) + l = LORANode(tsd, shared=shared_key) # Send various messages await l.start() diff --git a/lora_comms.py b/lora_comms.py index b2830b6..605591f 100644 --- a/lora_comms.py +++ b/lora_comms.py @@ -49,7 +49,7 @@ class CommsState(Structure): ] for func, ret, args in [ - ('comms_init', None, (POINTER(CommsState), process_msgfunc_t)), + ('comms_init', None, (POINTER(CommsState), process_msgfunc_t, POINTER(PktBuf))), ('comms_process', None, (POINTER(CommsState), PktBuf, POINTER(PktBuf))), ('strobe_seed_prng', None, (POINTER(c_uint8), c_ssize_t)), ]: