Implement a secure ICS protocol targeting LoRa Node151 microcontroller for controlling irrigation.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

111 lines
2.6 KiB

  1. #include <comms.h>
  2. #include <strobe_rng_init.h>
  3. static const size_t MAC_LEN = 8;
  4. static const size_t CHALLENGE_LEN = 16;
  5. static const uint8_t domain[] = "com.funkthat.lora.irrigation.shared.v0.0.1";
  6. size_t
  7. _strobe_state_size()
  8. {
  9. return sizeof(strobe_s);
  10. }
  11. void
  12. comms_init(struct comms_state *cs, process_msgfunc_t pmf)
  13. {
  14. *cs = (struct comms_state){
  15. .cs_comm_state = COMMS_WAIT_REQUEST,
  16. .cs_procmsg = pmf,
  17. };
  18. strobe_init(&cs->cs_start, domain, sizeof domain - 1);
  19. /* copy starting state over to initial state */
  20. cs->cs_state = cs->cs_start;
  21. }
  22. #define CONFIRMED_STR_BASE "confirmed"
  23. #define CONFIRMED_STR ((const uint8_t *)CONFIRMED_STR_BASE)
  24. #define CONFIRMED_STR_LEN (sizeof(CONFIRMED_STR_BASE) - 1)
  25. /*
  26. * encrypted data to be processed is passed in via pbin.
  27. *
  28. * The pktbuf pointed to by pbout contains the buffer that a [encrypted]
  29. * response will be written to. The length needs to be updated, where 0
  30. * means no reply.
  31. */
  32. void
  33. comms_process(struct comms_state *cs, struct pktbuf pbin, struct pktbuf *pbout)
  34. {
  35. uint8_t buf[64] = {};
  36. struct pktbuf pbmsg, pbrep;
  37. ssize_t cnt, ret, msglen;
  38. strobe_attach_buffer(&cs->cs_state, pbin.pkt, pbin.pktlen);
  39. cnt = strobe_get(&cs->cs_state, APP_CIPHERTEXT, buf, pbin.pktlen -
  40. MAC_LEN);
  41. msglen = cnt;
  42. cnt = strobe_get(&cs->cs_state, MAC, pbin.pkt +
  43. (pbin.pktlen - MAC_LEN), MAC_LEN);
  44. /* XXX - cnt != MAC_LEN test case */
  45. /*
  46. * if we have arrived here, MAC has been verified, and buf now
  47. * contains the data to operate upon.
  48. */
  49. /* attach the buffer for output */
  50. strobe_attach_buffer(&cs->cs_state, pbout->pkt, pbout->pktlen);
  51. ret = 0;
  52. switch (cs->cs_comm_state) {
  53. case COMMS_WAIT_REQUEST:
  54. /* XXX - reqreset check */
  55. bare_strobe_randomize(buf, CHALLENGE_LEN);
  56. ret = strobe_put(&cs->cs_state, APP_CIPHERTEXT, buf,
  57. CHALLENGE_LEN);
  58. ret += strobe_put(&cs->cs_state, MAC, NULL, MAC_LEN);
  59. cs->cs_comm_state = COMMS_WAIT_CONFIRM;
  60. break;
  61. case COMMS_WAIT_CONFIRM:
  62. /* XXX - confirm check */
  63. ret = strobe_put(&cs->cs_state, APP_CIPHERTEXT, CONFIRMED_STR,
  64. CONFIRMED_STR_LEN);
  65. ret += strobe_put(&cs->cs_state, MAC, NULL, MAC_LEN);
  66. cs->cs_comm_state = COMMS_PROCESS_MSGS;
  67. break;
  68. case COMMS_PROCESS_MSGS: {
  69. uint8_t repbuf[pbout->pktlen - MAC_LEN];
  70. memset(repbuf, '\x00', sizeof repbuf);
  71. pbmsg.pkt = buf;
  72. pbmsg.pktlen = msglen;
  73. pbrep.pkt = repbuf;
  74. pbrep.pktlen = sizeof repbuf;
  75. cs->cs_procmsg(pbmsg, &pbrep);
  76. ret = strobe_put(&cs->cs_state, APP_CIPHERTEXT, repbuf,
  77. pbrep.pktlen);
  78. ret += strobe_put(&cs->cs_state, MAC, NULL, MAC_LEN);
  79. break;
  80. }
  81. }
  82. pbout->pktlen = ret;
  83. }