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.
 
 
 
 
 
 

109 lines
5.0 KiB

  1. from collections import namedtuple
  2. # Control flags
  3. FLAG_R = 1<<0
  4. FLAG_I = 1<<0
  5. DIR_CLIENT = 0
  6. DIR_SERVER = FLAG_I
  7. FLAG_A = 1<<1
  8. FLAG_C = 1<<2
  9. FLAG_T = 1<<3
  10. FLAG_M = 1<<4
  11. FLAG_K = 1<<5
  12. # Record defining a STROBE control word
  13. class ControlWord(namedtuple("ControlWord",("name",
  14. "bytes","dmode","cmode",
  15. "length_bytes","length","max_length","min_length"))):
  16. """
  17. Control word for STROBE.
  18. TODO: write more docs
  19. """
  20. def __new__(cls,name,
  21. bytes,dmode,cmode=None,
  22. length_bytes=0,length=None,max_length=None,min_length=None,explicit=None):
  23. if explicit is None:
  24. explicit = (len(bytes) or length_bytes) and (dmode & FLAG_T)
  25. if cmode is None:
  26. # Default: Don't send unless there are length bytes and transport
  27. if explicit: cmode = FLAG_A | FLAG_T | FLAG_M
  28. else: cmode = FLAG_A | FLAG_M
  29. bytes = bytearray(bytes)
  30. if dmode & (FLAG_T | FLAG_A) == 0 and length_bytes == 0 and length is None:
  31. length = 0
  32. return super(ControlWord,cls).__new__(cls,name,
  33. bytes,dmode,cmode,
  34. length_bytes,length,max_length,min_length)
  35. def __str__(self): return self.name
  36. TYPE_META = 0
  37. TYPE_ABSORB = FLAG_A
  38. TYPE_PLAINTEXT = FLAG_A | FLAG_T
  39. TYPE_ENCRYPT = FLAG_A | FLAG_T | FLAG_C
  40. TYPE_MAC = FLAG_T | FLAG_C
  41. TYPE_PRNG = FLAG_A | FLAG_C
  42. TYPE_RATCHET = FLAG_C # to be used with extract
  43. TYPE_KEY = FLAG_A | FLAG_C
  44. ################################################################################
  45. # Example control words.
  46. #
  47. # The STROBE lite framework is not tied to any of these definitions.
  48. # These are just some examples / recommendations of what you can use.
  49. #
  50. # These code words span the gamut from offline encrypted and/or signed messages,
  51. # to full TLS-like protocols.
  52. #
  53. # ***
  54. # The assumption is that most protocols will use a VERY SMALL SUBSET of these tags.
  55. # They are comprehensive just to demonstrate that you could replace TLS with a
  56. # protocol like this.
  57. # ***
  58. ################################################################################
  59. # 0x00-0x0F: symmetric cryptography
  60. SYM_SCHEME = ControlWord("SYM_SCHEME", [0x00], TYPE_PLAINTEXT , length_bytes=2)
  61. SYM_KEY = ControlWord("SYM_KEY", [0x01], TYPE_KEY )
  62. APP_PLAINTEXT = ControlWord("APP_PLAINTEXT", [0x02], TYPE_PLAINTEXT , length_bytes=2)
  63. APP_CIPHERTEXT = ControlWord("APP_CIPHERTEXT", [0x03], TYPE_ENCRYPT , length_bytes=2)
  64. AUTH_DATA = ControlWord("NONCE", [0x04], TYPE_PLAINTEXT , length_bytes=2)
  65. AUTH_DATA = ControlWord("AUTH_DATA", [0x05], TYPE_PLAINTEXT , length_bytes=2)
  66. MAC = ControlWord("MAC", [0x06], TYPE_MAC , length_bytes=2, length=16, explicit=False )
  67. STEG_MAC = ControlWord("STEG_MAC", [0x06], TYPE_MAC , length_bytes=2, min_length=16, cmode=TYPE_ENCRYPT|FLAG_M)
  68. SIV_MAC_INNER = ControlWord("SIV_MAC_INNER", [0x06], TYPE_MAC , length_bytes=2, length=16, explicit=False )
  69. HASH = ControlWord("HASH", [0x07], TYPE_PRNG , length_bytes=2, explicit=False )
  70. SIV_PT_INNER = ControlWord("SIV_PT_INNER", [0x0D], TYPE_PLAINTEXT , explicit=False)
  71. SIV_MAC_OUTER = ControlWord("SIV_MAC_OUTER", [0x0E], TYPE_PLAINTEXT , length=16)
  72. RATCHET = ControlWord("RATCHET", [0x0F], TYPE_RATCHET , length=32)
  73. # 0x10-0x1F: Asymmetric key exchange and encryption */
  74. KEM_SCHEME = ControlWord("KEM_SCHEME", [0x10], TYPE_PLAINTEXT , length_bytes=2)
  75. PUBLIC_KEY = ControlWord("PUBLIC_KEY", [0x11], TYPE_PLAINTEXT , length_bytes=2)
  76. KEM_EPH = ControlWord("KEM_EPH", [0x12], TYPE_PLAINTEXT , length_bytes=2)
  77. KEM_RESULT = ControlWord("KEM_RESULT", [0x13], TYPE_KEY )
  78. # 0x18-0x1F: Signatures */
  79. SIG_SCHEME = ControlWord("SIG_SCHEME", [0x18], TYPE_PLAINTEXT , length_bytes=2)
  80. SIG_EPH = ControlWord("SIG_EPH", [0x19], TYPE_PLAINTEXT , length_bytes=2)
  81. SIG_CHALLENGE = ControlWord("SIG_CHALLENGE", [0x1A], TYPE_PRNG , length_bytes=2, explicit=False)
  82. SIG_RESPONSE = ControlWord("SIG_RESPONSE", [0x1B], TYPE_ENCRYPT , length_bytes=2)
  83. # 0x00-0x0F: header and other metadata */
  84. HANDSHAKE = ControlWord("HANDSHAKE", [0x20], TYPE_PLAINTEXT , length_bytes=2)
  85. VERSION = ControlWord("VERSION", [0x21], TYPE_PLAINTEXT , length_bytes=2)
  86. CIPHERSUITE = ControlWord("CIPHERSUITE", [0x22], TYPE_PLAINTEXT , length_bytes=2)
  87. META_PLAINTEXT = ControlWord("META_PLAINTEXT", [0x24], TYPE_PLAINTEXT , length_bytes=2)
  88. META_CIPHERTEXT= ControlWord("META_CIPHERTEXT", [0x25], TYPE_PLAINTEXT , length_bytes=2)
  89. CERTIFICATE = ControlWord("CERTIFICATE", [0x26], TYPE_PLAINTEXT , length_bytes=2)
  90. ENCRYPTED_CERT = ControlWord("ENCRYPTED_CERT", [0x27], TYPE_ENCRYPT , length_bytes=2)
  91. OVER = ControlWord("OVER", [0x2E], TYPE_MAC , length_bytes=2)
  92. CLOSE = ControlWord("CLOSE", [0x2F], TYPE_MAC , length_bytes=2)