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.
 
 
 
 
 
 

270 lines
7.6 KiB

  1. # Copyright 2021 John-Mark Gurney.
  2. #
  3. # Redistribution and use in source and binary forms, with or without
  4. # modification, are permitted provided that the following conditions
  5. # are met:
  6. # 1. Redistributions of source code must retain the above copyright
  7. # notice, this list of conditions and the following disclaimer.
  8. # 2. Redistributions in binary form must reproduce the above copyright
  9. # notice, this list of conditions and the following disclaimer in the
  10. # documentation and/or other materials provided with the distribution.
  11. #
  12. # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  13. # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  14. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  15. # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  16. # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  17. # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  18. # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  19. # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  20. # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  21. # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  22. # SUCH DAMAGE.
  23. #
  24. import os
  25. import unittest
  26. from ctypes import Structure, POINTER, CFUNCTYPE, pointer, sizeof
  27. from ctypes import c_uint8, c_uint16, c_ssize_t, c_size_t, c_uint64, c_int
  28. from ctypes import CDLL
  29. class StructureRepr(object):
  30. def __repr__(self): #pragma: no cover
  31. return '%s(%s)' % (self.__class__.__name__, ', '.join('%s=%s' %
  32. (k, getattr(self, k)) for k, v in self._fields_))
  33. class PktBuf(Structure):
  34. _fields_ = [
  35. ('pkt', POINTER(c_uint8)),
  36. ('pktlen', c_uint16),
  37. ]
  38. def _from(self):
  39. return bytes(self.pkt[:self.pktlen])
  40. def __repr__(self): #pragma: no cover
  41. return 'PktBuf(pkt=%s, pktlen=%s)' % (repr(self._from()),
  42. self.pktlen)
  43. def make_pktbuf(s):
  44. pb = PktBuf()
  45. if isinstance(s, bytearray):
  46. obj = s
  47. pb.pkt = pointer(c_uint8.from_buffer(s))
  48. else:
  49. obj = (c_uint8 * len(s))(*s)
  50. pb.pkt = obj
  51. pb.pktlen = len(s)
  52. pb._make_pktbuf_ref = (obj, s)
  53. return pb
  54. process_msgfunc_t = CFUNCTYPE(None, PktBuf, POINTER(PktBuf))
  55. try:
  56. _lib = CDLL('libsyote_test.dylib')
  57. except OSError:
  58. _lib = None
  59. if _lib is not None:
  60. _lib._strobe_state_size.restype = c_size_t
  61. _lib._strobe_state_size.argtypes = ()
  62. _strobe_state_u64_cnt = (_lib._strobe_state_size() + 7) // 8
  63. else:
  64. _strobe_state_u64_cnt = 1
  65. class CommsSession(Structure,StructureRepr):
  66. _fields_ = [
  67. ('cs_crypto', c_uint64 * _strobe_state_u64_cnt),
  68. ('cs_state', c_int),
  69. ]
  70. EC_PUBLIC_BYTES = 32
  71. EC_PRIVATE_BYTES = 32
  72. class CommsState(Structure,StructureRepr):
  73. _fields_ = [
  74. # The alignment of these may be off
  75. ('cs_active', CommsSession),
  76. ('cs_pending', CommsSession),
  77. ('cs_respkey', c_uint8 * EC_PRIVATE_BYTES),
  78. ('cs_resppubkey', c_uint8 * EC_PUBLIC_BYTES),
  79. ('cs_initpubkey', c_uint8 * EC_PUBLIC_BYTES),
  80. ('cs_start', CommsSession),
  81. ('cs_procmsg', process_msgfunc_t),
  82. ('cs_prevmsg', PktBuf),
  83. ('cs_prevmsgresp', PktBuf),
  84. ('cs_prevmsgbuf', c_uint8 * 64),
  85. ('cs_prevmsgrespbuf', c_uint8 * 64),
  86. ]
  87. if _lib is not None:
  88. _lib._comms_state_size.restype = c_size_t
  89. _lib._comms_state_size.argtypes = ()
  90. if _lib._comms_state_size() != sizeof(CommsState): # pragma: no cover
  91. raise RuntimeError('CommsState structure size mismatch!')
  92. X25519_BASE_POINT = (c_uint8 * (256//8)).in_dll(_lib, 'X25519_BASE_POINT')
  93. for func, ret, args in [
  94. ('comms_init', c_int, (POINTER(CommsState), process_msgfunc_t,
  95. POINTER(PktBuf), POINTER(PktBuf), POINTER(PktBuf))),
  96. ('comms_process', None, (POINTER(CommsState), PktBuf, POINTER(PktBuf))),
  97. ('strobe_seed_prng', None, (POINTER(c_uint8), c_ssize_t)),
  98. ('x25519', c_int, (c_uint8 * EC_PUBLIC_BYTES, c_uint8 * EC_PRIVATE_BYTES, c_uint8 * EC_PUBLIC_BYTES, c_int)),
  99. ]:
  100. f = getattr(_lib, func)
  101. f.restype = ret
  102. f.argtypes = args
  103. locals()[func] = f
  104. def x25519_wrap(out, scalar, base, clamp):
  105. outptr = (c_uint8 * EC_PUBLIC_BYTES).from_buffer_copy(out)
  106. scalarptr = (c_uint8 * EC_PRIVATE_BYTES).from_buffer_copy(scalar)
  107. baseptr = (c_uint8 * EC_PRIVATE_BYTES).from_buffer_copy(base)
  108. r = x25519(outptr, scalarptr, baseptr, clamp)
  109. if r != 0:
  110. raise RuntimeError('x25519 failed')
  111. return bytes(outptr)
  112. def x25519_genkey():
  113. return os.urandom(EC_PRIVATE_BYTES)
  114. def x25519_base(scalar, clamp):
  115. out = bytearray(EC_PUBLIC_BYTES)
  116. outptr = (c_uint8 * EC_PUBLIC_BYTES).from_buffer(out)
  117. scalarptr = (c_uint8 * EC_PRIVATE_BYTES).from_buffer_copy(scalar)
  118. r = x25519(outptr, scalarptr, X25519_BASE_POINT, clamp)
  119. if r != 0:
  120. raise RuntimeError('x25519 failed')
  121. return bytes(out)
  122. class X25519:
  123. '''Class to wrap the x25519 functions into something a bit more
  124. usable. This provides better key ingestion and better support
  125. for other key formats.
  126. Use either the gen method to generate a random key, or the frombytes
  127. method.
  128. a = X25519.gen()
  129. b = X25519.gen()
  130. a.dh(b.getpub()) == b.dh(a.getpub())
  131. That is, each party generates a key, sends their public part to the
  132. other party, and then uses their received public part as an argument
  133. to the dh method. The resulting value will be shared between the
  134. two parties.
  135. '''
  136. def __init__(self, key):
  137. self.privkey = key
  138. self.pubkey = x25519_base(key, 1)
  139. def dh(self, pub):
  140. '''Perform a DH operation using the public part pub.'''
  141. return x25519_wrap(self.pubkey, self.privkey, pub, 1)
  142. def getpub(self):
  143. '''Get the public part of the key. This is to be sent
  144. to the other party for key exchange.'''
  145. return self.pubkey
  146. def getpriv(self):
  147. return self.privkey
  148. @classmethod
  149. def gen(cls):
  150. '''Generate a random X25519 key.'''
  151. return cls(x25519_genkey())
  152. @classmethod
  153. def frombytes(cls, key):
  154. '''Generate an X25519 key from 32 bytes.'''
  155. return cls(key)
  156. def comms_process_wrap(state, input):
  157. '''A wrapper around comms_process that converts the argument
  158. into the buffer, and the returns the message as a bytes string.
  159. '''
  160. inpkt = make_pktbuf(input)
  161. outbytes = bytearray(64)
  162. outbuf = make_pktbuf(outbytes)
  163. comms_process(state, inpkt, outbuf)
  164. return outbuf._from()
  165. class TestX25519(unittest.TestCase):
  166. PUBLIC_BYTES = EC_PUBLIC_BYTES
  167. PRIVATE_BYTES = EC_PRIVATE_BYTES
  168. def test_class(self):
  169. key = X25519.gen()
  170. pubkey = key.getpub()
  171. privkey = key.getpriv()
  172. apubkey = x25519_base(privkey, 1)
  173. self.assertEqual(apubkey, pubkey)
  174. self.assertEqual(X25519.frombytes(privkey).getpub(), pubkey)
  175. with self.assertRaises(ValueError):
  176. X25519(b'0'*31)
  177. def test_rfc7748_6_1(self):
  178. # KAT from https://datatracker.ietf.org/doc/html/rfc7748#section-6.1
  179. apriv = bytes.fromhex('77076d0a7318a57d3c16c17251b26645df4c2f87ebc0992ab177fba51db92c2a')
  180. akey = X25519(apriv)
  181. self.assertEqual(akey.getpub(), bytes.fromhex('8520f0098930a754748b7ddcb43ef75a0dbf3a0d26381af4eba4a98eaa9b4e6a'))
  182. bpriv = bytes.fromhex('5dab087e624a8a4b79e17f8b83800ee66f3bb1292618b6fd1c2f8b27ff88e0eb')
  183. bkey = X25519(bpriv)
  184. self.assertEqual(bkey.getpub(), bytes.fromhex('de9edb7d7b7dc1b4d35b61c2ece435373f8343c85b78674dadfc7e146f882b4f'))
  185. ss = bytes.fromhex('4a5d9d5ba4ce2de1728e3bf480350f25e07e21c947d19e3376f09b3c1e161742')
  186. self.assertEqual(akey.dh(bkey.getpub()), ss)
  187. self.assertEqual(bkey.dh(akey.getpub()), ss)
  188. def test_basic_ops(self):
  189. aprivkey = x25519_genkey()
  190. apubkey = x25519_base(aprivkey, 1)
  191. bprivkey = x25519_genkey()
  192. bpubkey = x25519_base(bprivkey, 1)
  193. self.assertNotEqual(aprivkey, bprivkey)
  194. self.assertNotEqual(apubkey, bpubkey)
  195. ra = x25519_wrap(apubkey, aprivkey, bpubkey, 1)
  196. rb = x25519_wrap(bpubkey, bprivkey, apubkey, 1)
  197. self.assertEqual(ra, rb)