loader.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. # ===================================================================
  2. #
  3. # Copyright (c) 2016, Legrandin <helderijs@gmail.com>
  4. # All rights reserved.
  5. #
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions
  8. # are met:
  9. #
  10. # 1. Redistributions of source code must retain the above copyright
  11. # notice, this list of conditions and the following disclaimer.
  12. # 2. Redistributions in binary form must reproduce the above copyright
  13. # notice, this list of conditions and the following disclaimer in
  14. # the documentation and/or other materials provided with the
  15. # distribution.
  16. #
  17. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  20. # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  21. # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  25. # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  27. # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. # POSSIBILITY OF SUCH DAMAGE.
  29. # ===================================================================
  30. import re
  31. import sys
  32. import binascii
  33. from tls.Crypto.Util._file_system import pycryptodome_filename
  34. def _load_tests(dir_comps, file_in, description, conversions):
  35. """Load and parse a test vector file
  36. Return a list of objects, one per group of adjacent
  37. KV lines or for a single line in the form "[.*]".
  38. For a group of lines, the object has one attribute per line.
  39. """
  40. line_number = 0
  41. results = []
  42. class TestVector(object):
  43. def __init__(self, description, count):
  44. self.desc = description
  45. self.count = count
  46. self.others = []
  47. test_vector = None
  48. count = 0
  49. new_group = True
  50. while True:
  51. line_number += 1
  52. line = file_in.readline()
  53. if not line:
  54. if test_vector is not None:
  55. results.append(test_vector)
  56. break
  57. line = line.strip()
  58. # Skip comments and empty lines
  59. if line.startswith('#') or not line:
  60. new_group = True
  61. continue
  62. if line.startswith("["):
  63. if test_vector is not None:
  64. results.append(test_vector)
  65. test_vector = None
  66. results.append(line)
  67. continue
  68. if new_group:
  69. count += 1
  70. new_group = False
  71. if test_vector is not None:
  72. results.append(test_vector)
  73. test_vector = TestVector("%s (#%d)" % (description, count), count)
  74. res = re.match("([A-Za-z0-9]+) = ?(.*)", line)
  75. if not res:
  76. test_vector.others += [line]
  77. else:
  78. token = res.group(1).lower()
  79. data = res.group(2).lower()
  80. conversion = conversions.get(token, None)
  81. if conversion is None:
  82. if len(data) % 2 != 0:
  83. data = "0" + data
  84. setattr(test_vector, token, binascii.unhexlify(data))
  85. else:
  86. setattr(test_vector, token, conversion(data))
  87. # This line is ignored
  88. return results
  89. def load_tests(dir_comps, file_name, description, conversions):
  90. """Load and parse a test vector file
  91. This function returnis a list of objects, one per group of adjacent
  92. KV lines or for a single line in the form "[.*]".
  93. For a group of lines, the object has one attribute per line.
  94. """
  95. description = "%s test (%s)" % (description, file_name)
  96. with open(pycryptodome_filename(dir_comps, file_name)) as file_in:
  97. results = _load_tests(dir_comps, file_in, description, conversions)
  98. return results