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.
 
 
 
 
 
 

245 lines
6.5 KiB

  1. /*-
  2. * Copyright 2021 John-Mark Gurney.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  14. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  16. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  17. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  19. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  20. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  21. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  22. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  23. * SUCH DAMAGE.
  24. *
  25. */
  26. #include <comms.h>
  27. #include <strobe_rng_init.h>
  28. static const size_t MAC_LEN = 8;
  29. static const size_t CHALLENGE_LEN = 16;
  30. static const uint8_t domain[] = "com.funkthat.lora.irrigation.shared.v0.0.1";
  31. static int comms_pktbuf_equal(struct pktbuf a, struct pktbuf b);
  32. /* returns 1 if equal, 0 if not equal */
  33. static int
  34. comms_pktbuf_equal(struct pktbuf a, struct pktbuf b)
  35. {
  36. if (a.pktlen != b.pktlen)
  37. return 0;
  38. return memcmp(a.pkt, b.pkt, a.pktlen) == 0;
  39. }
  40. size_t
  41. _comms_state_size()
  42. {
  43. return sizeof(struct comms_state);
  44. }
  45. size_t
  46. _strobe_state_size()
  47. {
  48. return sizeof(strobe_s);
  49. }
  50. void
  51. comms_init(struct comms_state *cs, process_msgfunc_t pmf, struct pktbuf *shared)
  52. {
  53. *cs = (struct comms_state){
  54. .cs_procmsg = pmf,
  55. };
  56. strobe_init(&cs->cs_start, domain, sizeof domain - 1);
  57. if (shared != NULL)
  58. strobe_key(&cs->cs_start, SYM_KEY, shared->pkt, shared->pktlen);
  59. /* copy starting state over to initial state */
  60. cs->cs_active = (struct comms_session){
  61. .cs_crypto = cs->cs_start,
  62. .cs_state = COMMS_WAIT_REQUEST,
  63. };
  64. cs->cs_pending = cs->cs_active;
  65. }
  66. #define CONFIRMED_STR_BASE "confirmed"
  67. #define CONFIRMED_STR ((const uint8_t *)CONFIRMED_STR_BASE)
  68. #define CONFIRMED_STR_LEN (sizeof(CONFIRMED_STR_BASE) - 1)
  69. static void
  70. _comms_process_session(struct comms_state *cs, struct comms_session *sess, struct pktbuf pbin, struct pktbuf *pbout)
  71. {
  72. strobe_s tmp;
  73. uint8_t buf[64] = {};
  74. struct pktbuf pbmsg, pbrep;
  75. ssize_t cnt, ret, msglen;
  76. /* save the state incase the message is bad */
  77. tmp = sess->cs_crypto;
  78. strobe_attach_buffer(&sess->cs_crypto, pbin.pkt, pbin.pktlen);
  79. cnt = strobe_get(&sess->cs_crypto, APP_CIPHERTEXT, buf, pbin.pktlen -
  80. MAC_LEN);
  81. msglen = cnt;
  82. cnt = strobe_get(&sess->cs_crypto, MAC, pbin.pkt +
  83. (pbin.pktlen - MAC_LEN), MAC_LEN);
  84. /* MAC check failed */
  85. if (cnt == -1) {
  86. /* restore the previous state */
  87. sess->cs_crypto = tmp;
  88. pbout->pktlen = 0;
  89. return;
  90. }
  91. /*
  92. * if we have arrived here, MAC has been verified, and buf now
  93. * contains the data to operate upon.
  94. */
  95. /* attach the buffer for output */
  96. strobe_attach_buffer(&sess->cs_crypto, pbout->pkt, pbout->pktlen);
  97. ret = 0;
  98. switch (sess->cs_state) {
  99. case COMMS_WAIT_REQUEST:
  100. /* XXX - reqreset check */
  101. bare_strobe_randomize(buf, CHALLENGE_LEN);
  102. ret = strobe_put(&sess->cs_crypto, APP_CIPHERTEXT, buf,
  103. CHALLENGE_LEN);
  104. ret += strobe_put(&sess->cs_crypto, MAC, NULL, MAC_LEN);
  105. strobe_operate(&sess->cs_crypto, RATCHET, NULL, 32);
  106. sess->cs_state = COMMS_WAIT_CONFIRM;
  107. break;
  108. case COMMS_WAIT_CONFIRM:
  109. /* XXX - confirm check */
  110. ret = strobe_put(&sess->cs_crypto, APP_CIPHERTEXT, CONFIRMED_STR,
  111. CONFIRMED_STR_LEN);
  112. ret += strobe_put(&sess->cs_crypto, MAC, NULL, MAC_LEN);
  113. sess->cs_state = COMMS_PROCESS_MSGS;
  114. break;
  115. case COMMS_PROCESS_MSGS: {
  116. uint8_t repbuf[pbout->pktlen - MAC_LEN];
  117. memset(repbuf, '\x00', sizeof repbuf);
  118. pbmsg.pkt = buf;
  119. pbmsg.pktlen = msglen;
  120. pbrep.pkt = repbuf;
  121. pbrep.pktlen = sizeof repbuf;
  122. cs->cs_procmsg(pbmsg, &pbrep);
  123. ret = strobe_put(&sess->cs_crypto, APP_CIPHERTEXT, repbuf,
  124. pbrep.pktlen);
  125. ret += strobe_put(&sess->cs_crypto, MAC, NULL, MAC_LEN);
  126. break;
  127. }
  128. }
  129. /* set the output buffer length */
  130. pbout->pktlen = ret;
  131. }
  132. /*
  133. * encrypted data to be processed is passed in via pbin.
  134. *
  135. * The pktbuf pointed to by pbout contains the buffer that a [encrypted]
  136. * response will be written to. The length needs to be updated, where 0
  137. * means no reply.
  138. */
  139. void
  140. comms_process(struct comms_state *cs, struct pktbuf pbin, struct pktbuf *pbout)
  141. {
  142. struct pktbuf pbouttmp;
  143. /* if the current msg matches the previous */
  144. if (comms_pktbuf_equal(pbin, cs->cs_prevmsg)) {
  145. /* send the previous response */
  146. pbout->pktlen = cs->cs_prevmsgresp.pktlen;
  147. memcpy(pbout->pkt, cs->cs_prevmsgresp.pkt, pbout->pktlen);
  148. return;
  149. }
  150. /* try to use the active session */
  151. pbouttmp = *pbout;
  152. _comms_process_session(cs, &cs->cs_active, pbin, &pbouttmp);
  153. if (pbouttmp.pktlen != 0) {
  154. retmsg:
  155. /* we accepted a new message store it */
  156. *pbout = pbouttmp;
  157. /* store the req */
  158. cs->cs_prevmsg.pkt = cs->cs_prevmsgbuf;
  159. cs->cs_prevmsg.pktlen = pbin.pktlen;
  160. memcpy(cs->cs_prevmsg.pkt, pbin.pkt, pbin.pktlen);
  161. /* store the response */
  162. cs->cs_prevmsgresp.pkt = cs->cs_prevmsgrespbuf;
  163. cs->cs_prevmsgresp.pktlen = pbout->pktlen;
  164. memcpy(cs->cs_prevmsgresp.pkt, pbout->pkt, pbout->pktlen);
  165. } else {
  166. /* active session didn't work, try cs_pending */
  167. pbouttmp = *pbout;
  168. _comms_process_session(cs, &cs->cs_pending, pbin, &pbouttmp);
  169. if (cs->cs_pending.cs_state == COMMS_PROCESS_MSGS) {
  170. /* new active state */
  171. cs->cs_active = cs->cs_pending;
  172. cs->cs_pending = (struct comms_session){
  173. .cs_crypto = cs->cs_start,
  174. .cs_state = COMMS_WAIT_REQUEST,
  175. };
  176. goto retmsg;
  177. }
  178. /* pending session didn't work, maybe new */
  179. struct comms_session tmpsess;
  180. tmpsess = (struct comms_session){
  181. .cs_crypto = cs->cs_start,
  182. .cs_state = COMMS_WAIT_REQUEST,
  183. };
  184. pbouttmp = *pbout;
  185. _comms_process_session(cs, &tmpsess, pbin, &pbouttmp);
  186. if (tmpsess.cs_state == COMMS_WAIT_CONFIRM) {
  187. /* new request for session */
  188. cs->cs_pending = tmpsess;
  189. *pbout = pbouttmp;
  190. } else {
  191. /* no packet to reply with */
  192. pbout->pktlen = 0;
  193. }
  194. }
  195. }