mldsa_parse.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #!/usr/bin/env python
  2. # Copyright 2025 The OpenSSL Project Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License 2.0 (the "License"). You may not use
  5. # this file except in compliance with the License. You can obtain a copy
  6. # in the file LICENSE in the source distribution or at
  7. # https://www.openssl.org/source/license.html
  8. # A python program written to parse (version 42) of the ACVP test vectors for
  9. # ML_DSA. The 3 files that can be processed by this utility can be downloaded
  10. # from
  11. # https://github.com/usnistgov/ACVP-Server/blob/master/gen-val/json-files/ML-DSA-keyGen-FIPS204/internalProjection.json
  12. # https://github.com/usnistgov/ACVP-Server/blob/master/gen-val/json-files/ML-DSA-sigGen-FIPS204/internalProjection.json
  13. # https://github.com/usnistgov/ACVP-Server/blob/master/gen-val/json-files/ML-DSA-sigVer-FIPS204/internalProjection.json
  14. # and output from this utility to
  15. # test/recipes/30-test_evp_data/evppkey_ml_dsa_keygen.txt
  16. # test/recipes/30-test_evp_data/evppkey_ml_dsa_siggen.txt
  17. # test/recipes/30-test_evp_data/evppkey_ml_dsa_sigver.txt
  18. #
  19. # e.g. python3 mldsa_parse.py ~/Downloads/keygen.json > ./test/recipes/30-test_evp_data/evppkey_ml_dsa_keygen.txt
  20. #
  21. import json
  22. import argparse
  23. import datetime
  24. def print_label(label, value):
  25. print(label + " = " + value)
  26. def print_hexlabel(label, tag, value):
  27. print(label + " = hex" + tag + ":" + value)
  28. def parse_ml_dsa_key_gen(groups):
  29. for grp in groups:
  30. for tst in grp['tests']:
  31. print("");
  32. print_label("FIPSversion", ">=3.5.0")
  33. print_label("KeyGen", grp['parameterSet'])
  34. print_label("KeyName", "tcId" + str(tst['tcId']))
  35. print_hexlabel("Ctrl", "seed", tst['seed'])
  36. print_hexlabel("CtrlOut", "pub", tst['pk'])
  37. print_hexlabel("CtrlOut", "priv", tst['sk'])
  38. def parse_ml_dsa_sig_gen(groups):
  39. for grp in groups:
  40. deter = grp['deterministic'] # Boolean
  41. externalMu = grp['externalMu'] # Boolean
  42. signInterfaceExternal = (grp['signatureInterface'] == "External")
  43. signPreHash = (grp['preHash'] == "preHash")
  44. signPure = (grp['preHash'] == "pure")
  45. includeMu = True # Flag flips to only include the Ctrl mu:0 half the time
  46. if signPreHash:
  47. continue
  48. if not externalMu and not signPure:
  49. continue
  50. name = grp['parameterSet'].replace('-', '_')
  51. for tst in grp['tests']:
  52. testname = name + "_" + str(tst['tcId'])
  53. print("");
  54. print_label("PrivateKeyRaw", testname + ":" + grp['parameterSet'] + ":" + tst['sk'])
  55. print("");
  56. print_label("FIPSversion", ">=3.5.0")
  57. print_label("Sign-Message", grp['parameterSet'] + ":" + testname)
  58. print_label("Input", tst['mu' if externalMu else 'message'])
  59. print_label("Output", tst['signature'])
  60. print_label("Ctrl", "message-encoding:1")
  61. if not externalMu:
  62. print_label("Ctrl", "hexcontext-string:" + tst["context"])
  63. includeMu = not includeMu
  64. if externalMu or includeMu:
  65. print_label("Ctrl", "mu:" + ("1" if externalMu else "0"))
  66. print_label("Ctrl", "deterministic:" + ("1" if deter else "0"))
  67. if not deter:
  68. print_label("Ctrl", "hextest-entropy:" + tst["rnd"])
  69. def parse_ml_dsa_sig_ver(groups):
  70. for grp in groups:
  71. externalMu = grp["externalMu"] # Boolean
  72. signInterfaceExternal = (grp['signatureInterface'] == "External")
  73. signPreHash = (grp['preHash'] == "preHash")
  74. signPure = (grp['preHash'] == "pure")
  75. includeMu = True # Flag flips to only include the Ctrl mu:0 half the time
  76. if signPreHash:
  77. continue
  78. if not externalMu and not signPure:
  79. continue
  80. name = grp['parameterSet'].replace('-', '_')
  81. for tst in grp['tests']:
  82. testname = name + "_" + str(tst['tcId'])
  83. print("");
  84. print_label("PublicKeyRaw", testname + ":" + grp['parameterSet'] + ":" + tst['pk'])
  85. print("");
  86. if "reason" in tst:
  87. print("# " + tst['reason'])
  88. print_label("FIPSversion", ">=3.5.0")
  89. print_label("Verify-Message-Public", grp['parameterSet'] + ":" + testname)
  90. print_label("Input", tst['mu' if externalMu else 'message'])
  91. print_label("Output", tst['signature'])
  92. print_label("Ctrl", "message-encoding:1")
  93. if not externalMu:
  94. print_label("Ctrl", "hexcontext-string:" + tst["context"])
  95. includeMu = not includeMu
  96. if externalMu or includeMu:
  97. print_label("Ctrl", "mu:" + ("1" if externalMu else "0"))
  98. if not tst['testPassed']:
  99. print_label("Result", "VERIFY_ERROR")
  100. parser = argparse.ArgumentParser(description="")
  101. parser.add_argument('filename', type=str)
  102. args = parser.parse_args()
  103. # Open and read the JSON file
  104. with open(args.filename, 'r') as file:
  105. data = json.load(file)
  106. year = datetime.date.today().year
  107. version = data['vsId']
  108. algorithm = data['algorithm']
  109. mode = data['mode']
  110. print("# Copyright " + str(year) + " The OpenSSL Project Authors. All Rights Reserved.")
  111. print("#")
  112. print("# Licensed under the Apache License 2.0 (the \"License\"). You may not use")
  113. print("# this file except in compliance with the License. You can obtain a copy")
  114. print("# in the file LICENSE in the source distribution or at")
  115. print("# https://www.openssl.org/source/license.html\n")
  116. print("# ACVP test data for " + algorithm + " " + mode + " generated from")
  117. print("# https://github.com/usnistgov/ACVP-Server/blob/master/gen-val/json-files/"
  118. "ML-DSA-" + mode + "-FIPS204/internalProjection.json")
  119. print("# [version " + str(version) + "]")
  120. if algorithm == "ML-DSA":
  121. if mode == 'sigVer':
  122. parse_ml_dsa_sig_ver(data['testGroups'])
  123. elif mode == 'sigGen':
  124. parse_ml_dsa_sig_gen(data['testGroups'])
  125. elif mode == 'keyGen':
  126. parse_ml_dsa_key_gen(data['testGroups'])
  127. else:
  128. print("Unsupported mode " + mode)
  129. else:
  130. print("Unsupported algorithm " + algorithm)