slhdsa_parse.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #!/usr/bin/env python3
  2. #
  3. # Copyright 2025 The OpenSSL Project Authors. All Rights Reserved.
  4. #
  5. # Licensed under the Apache License 2.0 (the "License"). You may not use
  6. # this file except in compliance with the License. You can obtain a copy
  7. # in the file LICENSE in the source distribution or at
  8. # https://www.openssl.org/source/license.html
  9. # A python program written to parse (version 42) of the ACVP test vectors for
  10. # SLH_DSA. The 3 files that can be processed by this utility can be downloaded
  11. # from
  12. # https://github.com/usnistgov/ACVP-Server/blob/master/gen-val/json-files/SLH-DSA-keyGen-FIPS204/internalProjection.json
  13. # https://github.com/usnistgov/ACVP-Server/blob/master/gen-val/json-files/SLH-DSA-sigGen-FIPS204/internalProjection.json
  14. # https://github.com/usnistgov/ACVP-Server/blob/master/gen-val/json-files/SLH-DSA-sigVer-FIPS204/internalProjection.json
  15. # and output from this utility to
  16. # test/recipes/30-test_evp_data/evppkey_slh_dsa_keygen.txt
  17. # test/recipes/30-test_evp_data/evppkey_slh_dsa_siggen.txt
  18. # test/recipes/30-test_evp_data/evppkey_slh_dsa_sigver.txt
  19. #
  20. # e.g. python3 slhdsa_parse.py ~/Downloads/keygen.json > ./test/recipes/30-test_evp_data/evppkey_slh_dsa_keygen.txt
  21. #
  22. import json
  23. import argparse
  24. import datetime
  25. import re
  26. import sys
  27. def eprint(*args, **kwargs):
  28. print(*args, file=sys.stderr, **kwargs)
  29. def print_label(label, value):
  30. print(label + " = " + value)
  31. def print_hexlabel(label, tag, value):
  32. print(label + " = hex" + tag + ":" + value)
  33. def parse_slh_dsa_key_gen(groups):
  34. for grp in groups:
  35. name = grp['parameterSet'].replace('-', '_')
  36. for tst in grp['tests']:
  37. print("");
  38. print_label("FIPSversion", ">=3.5.0")
  39. print_label("KeyGen", grp['parameterSet'])
  40. print_label("KeyName", "tcId" + str(tst['tcId']))
  41. print_hexlabel("Ctrl", "seed", tst['skSeed'] + tst['skPrf'] + tst['pkSeed'])
  42. print_hexlabel("CtrlOut", "pub", tst['pk'])
  43. print_hexlabel("CtrlOut", "priv", tst['sk'])
  44. def parse_slh_dsa_sig_gen(groups):
  45. coverage = set()
  46. for grp in groups:
  47. name = grp['parameterSet'].replace('-', '_')
  48. encoding = "1" if grp['preHash'] != 'none' else "0"
  49. deterministic = "1" if grp['deterministic'] else "0"
  50. for tst in grp['tests']:
  51. if tst['hashAlg'] != 'none':
  52. continue
  53. # Check if there is a similar test already and skip if so
  54. # Signature generation tests are very expensive, so we need to
  55. # limit things substantially. By default, we only test one of
  56. # the slow flavours and one of each kind of the fast ones.
  57. if name.find('f') >= 0:
  58. context = "1" if 'context' in tst else "0"
  59. coverage_name = name + "_" + encoding + deterministic + context
  60. else:
  61. coverage_name = name
  62. extended_test = coverage_name in coverage
  63. coverage.add(coverage_name)
  64. # Emit test case
  65. testname = name + "_" + str(tst['tcId'])
  66. print("");
  67. print_label("PrivateKeyRaw", testname + ":" + grp['parameterSet'] + ":" + tst['sk'])
  68. print("");
  69. print_label("FIPSversion", ">=3.5.0")
  70. print_label("Sign-Message", grp['parameterSet'] + ":" + testname)
  71. if extended_test:
  72. print_label("Extended-Test", "1")
  73. print_label("Input", tst['message'])
  74. print_label("Output", tst['signature'])
  75. if 'additionalRandomness' in tst:
  76. print_hexlabel("Ctrl", "test-entropy", tst['additionalRandomness']);
  77. print_label("Ctrl", "deterministic:" + deterministic)
  78. print_label("Ctrl", "message-encoding:" + encoding)
  79. if 'context' in tst:
  80. print_hexlabel("Ctrl", "context-string", tst["context"])
  81. def parse_slh_dsa_sig_ver(groups):
  82. for grp in groups:
  83. name = grp['parameterSet'].replace('-', '_')
  84. encoding = "1" if grp['preHash'] != 'none' else "0"
  85. for tst in grp['tests']:
  86. if tst['hashAlg'] != 'none':
  87. continue
  88. testname = name + "_" + str(tst['tcId'])
  89. print("");
  90. print_label("PublicKeyRaw", testname + ":" + grp['parameterSet'] + ":" + tst['pk'])
  91. print("");
  92. if "reason" in tst:
  93. print("# " + tst['reason'])
  94. print_label("FIPSversion", ">=3.5.0")
  95. print_label("Verify-Message-Public", grp['parameterSet'] + ":" + testname)
  96. print_label("Input", tst['message'])
  97. print_label("Output", tst['signature'])
  98. if 'additionalRandomness' in tst:
  99. print_hexlabel("Ctrl", "test-entropy", tst['additionalRandomness']);
  100. print_label("Ctrl", "message-encoding:" + encoding)
  101. if 'context' in tst:
  102. print_hexlabel("Ctrl", "context-string", tst["context"])
  103. if not tst['testPassed']:
  104. print_label("Result", "VERIFY_ERROR")
  105. parser = argparse.ArgumentParser(description="")
  106. parser.add_argument('filename', type=str)
  107. args = parser.parse_args()
  108. # Open and read the JSON file
  109. with open(args.filename, 'r') as file:
  110. data = json.load(file)
  111. year = datetime.date.today().year
  112. version = data['vsId']
  113. algorithm = data['algorithm']
  114. mode = data['mode']
  115. print("# Copyright " + str(year) + " The OpenSSL Project Authors. All Rights Reserved.")
  116. print("#")
  117. print("# Licensed under the Apache License 2.0 (the \"License\"). You may not use")
  118. print("# this file except in compliance with the License. You can obtain a copy")
  119. print("# in the file LICENSE in the source distribution or at")
  120. print("# https://www.openssl.org/source/license.html\n")
  121. print("# ACVP test data for " + algorithm + " " + mode + " generated from")
  122. print("# https://github.com/usnistgov/ACVP-Server/blob/master/gen-val/json-files/"
  123. "SLH-DSA-" + mode + "-FIPS204/internalProjection.json")
  124. print("# [version " + str(version) + "]")
  125. if algorithm == "SLH-DSA":
  126. if mode == 'sigVer':
  127. parse_slh_dsa_sig_ver(data['testGroups'])
  128. elif mode == 'sigGen':
  129. parse_slh_dsa_sig_gen(data['testGroups'])
  130. elif mode == 'keyGen':
  131. parse_slh_dsa_key_gen(data['testGroups'])
  132. else:
  133. eprint("Unsupported mode " + mode)
  134. else:
  135. eprint("Unsupported algorithm " + algorithm)