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.
 
 
 
 
 
 

150 lines
4.6 KiB

  1. """
  2. SHA-3 and SHAKE test vectors
  3. Copyright (c) Mike Hamburg, Cryptography Research, 2016.
  4. I will need to contact legal to get a license for this; in the mean time it is
  5. for example purposes only.
  6. """
  7. from __future__ import print_function
  8. import binascii
  9. import sys
  10. import getopt
  11. import Strobe.Keccak
  12. import fileinput
  13. def monte(hash,seed,samples=100,iterations=1000,bits=None,
  14. minoutbits=None,maxoutbits=None,**kwargs):
  15. if bits is None and hash().out_bytes is not None:
  16. bits = hash().out_bytes * 8
  17. md = binascii.unhexlify(seed)
  18. inputlen = len(md)
  19. print()
  20. if maxoutbits is None:
  21. outputlen = minoutbytes = maxoutbytes = bits//8
  22. print("[L = %d]" % bits)
  23. mdname = "MD"
  24. print()
  25. print("Seed = %s" % seed)
  26. else:
  27. minoutbytes = (minoutbits+7)//8
  28. maxoutbytes = maxoutbits//8
  29. outputlen = maxoutbytes
  30. print("[Minimum Output Length (bits) = %d]" % minoutbits)
  31. print()
  32. print("[Maximum Output Length (bits) = %d]" % maxoutbits)
  33. mdname = "Output"
  34. print()
  35. print("Msg = %s" % seed)
  36. print()
  37. for j in range(samples):
  38. for i in range(iterations):
  39. md = hash.hash((md+bytearray(inputlen))[0:inputlen],length=outputlen)
  40. randmd = bytearray(2)+md
  41. randish = randmd[-2]*256 + randmd[-1]
  42. rng = maxoutbytes-minoutbytes+1
  43. prev_outputlen = outputlen
  44. outputlen = minoutbytes + (randish % rng)
  45. print("COUNT = %d" % j)
  46. if minoutbytes != maxoutbytes: print("Outputlen = %d" % (prev_outputlen*8))
  47. print(mdname,"=", "".join(("%02x" % x for x in md)))
  48. print()
  49. sys.stdout.flush()
  50. def kat(hash,file,len=None,**kwargs):
  51. length = None
  52. outlen = None
  53. ignore = ["[Tested", "[Input Length", "COUNT = ",
  54. "[Minimum Output Length", "[Maximum Output Length"]
  55. for line in open(file,'r').readlines():
  56. line = line.rstrip()
  57. if line == "":
  58. print()
  59. elif any((line.startswith(ign) for ign in ignore)):
  60. print(line)
  61. elif line.startswith("Len = "):
  62. length = int(line.split("Len = ")[1])
  63. print(line)
  64. elif line.startswith("Msg = "):
  65. msg = line.split("Msg = ")[1]
  66. msg = binascii.unhexlify(msg)
  67. if length is not None: msg = msg[0:length//8]
  68. print(line)
  69. elif line.startswith("[L = "):
  70. outlen = int(line.split("[L = ")[1][0:-1])//8
  71. print(line)
  72. elif line.startswith("[Outputlen = "):
  73. outlen = int(line.split("[Outputlen = ")[1][0:-1])//8
  74. print(line)
  75. elif line.startswith("Outputlen = "):
  76. outlen = int(line.split("Outputlen = ")[1])//8
  77. print(line)
  78. elif line.startswith("Output = "):
  79. output = hash.hash(msg, length=outlen)
  80. print("Output =", "".join(("%02x" % x for x in output)))
  81. elif line.startswith("MD = "):
  82. output = hash.hash(msg, length=outlen)
  83. print("MD =", "".join(("%02x" % x for x in output)))
  84. if __name__ == '__main__':
  85. def usage(err=1):
  86. print("usage: TODO", file=sys.stderr)
  87. exit(err)
  88. opts,args = getopt.getopt(sys.argv[1:], "",
  89. ["test=","hash=","seed=","min-len=","max-len=","file="])
  90. if len(args) != 0 or len(opts) != len(set(opts)): usage()
  91. opts = dict(opts)
  92. hashes = {
  93. "SHA3_224":Strobe.Keccak.SHA3_224,
  94. "SHA3_256":Strobe.Keccak.SHA3_256,
  95. "SHA3_384":Strobe.Keccak.SHA3_384,
  96. "SHA3_512":Strobe.Keccak.SHA3_512,
  97. "SHAKE128":Strobe.Keccak.SHAKE128,
  98. "SHAKE256":Strobe.Keccak.SHAKE256
  99. }
  100. if "--hash" in opts and opts["--hash"] in hashes:
  101. hash = hashes[opts["--hash"]]
  102. else: usage()
  103. tests = {
  104. "Monte":monte,
  105. "Kat":kat
  106. # TODO: varlen
  107. }
  108. if "--test" in opts and opts["--test"] in tests:
  109. test = tests[opts["--test"]]
  110. else: usage()
  111. seed = None
  112. if "--seed" in opts: seed=opts["--seed"]
  113. file = None
  114. if "--file" in opts: file=opts["--file"]
  115. # parse lengths
  116. minlen = maxlen = None
  117. if "--min-len" in opts and opts["--min-len"] != "":
  118. minlen = int(opts["--min-len"])
  119. if "--max-len" in opts and opts["--max-len"] != "":
  120. maxlen = int(opts["--max-len"])
  121. if (minlen is None) != (maxlen is None): usage()
  122. if minlen is not None and (minlen+7)//8 > maxlen//8: usage()
  123. test(hash,seed=seed,file=file,minoutbits=minlen,maxoutbits=maxlen)