• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2# coding=utf-8
3# Copyright 2020 The BoringSSL Authors
4#
5# Permission to use, copy, modify, and/or distribute this software for any
6# purpose with or without fee is hereby granted, provided that the above
7# copyright notice and this permission notice appear in all copies.
8#
9# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
12# SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
14# OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
15# CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16
17"""This script translates JSON test vectors to BoringSSL's "FileTest" format.
18
19Usage: translate_test_vectors.py TEST_VECTORS_JSON_FILE
20
21The TEST_VECTORS_JSON_FILE is expected to come from the JSON copy of
22RFC 9180's test vectors, linked from its [TestVectors] citation.
23The output is written to "hpke_test_vectors.txt".
24"""
25
26import collections
27import json
28import sys
29
30HPKE_MODE_BASE = 0
31HPKE_MODE_PSK = 1
32HPKE_MODE_AUTH = 2
33HPKE_DHKEM_P256_SHA256 = 0x0010
34HPKE_DHKEM_X25519_SHA256 = 0x0020
35HPKE_HKDF_SHA256 = 0x0001
36HPKE_AEAD_EXPORT_ONLY = 0xffff
37
38
39def read_test_vectors_and_generate_code(json_file_in_path, test_file_out_path):
40  """Translates JSON test vectors into BoringSSL's FileTest language.
41
42    Args:
43      json_file_in_path: Path to the JSON test vectors file.
44      test_file_out_path: Path to output file.
45  """
46
47  # Load the JSON file into |test_vecs|.
48  with open(json_file_in_path) as file_in:
49    test_vecs = json.load(file_in)
50
51  lines = []
52  for test in test_vecs:
53    # Filter out test cases that we don't use.
54    if (test["mode"] not in (HPKE_MODE_BASE, HPKE_MODE_AUTH) or
55        test["kem_id"] not in (HPKE_DHKEM_X25519_SHA256,
56                               HPKE_DHKEM_P256_SHA256) or
57        test["aead_id"] == HPKE_AEAD_EXPORT_ONLY or
58        test["kdf_id"] != HPKE_HKDF_SHA256):
59      continue
60
61    keys = ["mode", "kem_id", "kdf_id", "aead_id", "info", "skRm", "skEm", "pkRm", "pkEm", "ikmE", "ikmR"]
62
63    if test["mode"] == HPKE_MODE_AUTH:
64      keys.append("pkSm")
65      keys.append("skSm")
66
67    for key in keys:
68      lines.append("{} = {}".format(key, str(test[key])))
69
70    for i, enc in enumerate(test["encryptions"]):
71      lines.append("# encryptions[{}]".format(i))
72      for key in ("aad", "ct", "pt"):
73        lines.append("{} = {}".format(key, str(enc[key])))
74
75    for i, exp in enumerate(test["exports"]):
76      lines.append("# exports[{}]".format(i))
77      for key in ("exporter_context", "L", "exported_value"):
78        lines.append("{} = {}".format(key, str(exp[key])))
79
80    lines.append("")
81
82  with open(test_file_out_path, "w") as file_out:
83    file_out.write("\n".join(lines))
84
85
86def main(argv):
87  if len(argv) != 2:
88    print(__doc__)
89    sys.exit(1)
90
91  read_test_vectors_and_generate_code(argv[1], "hpke_test_vectors.txt")
92
93
94if __name__ == "__main__":
95  main(sys.argv)
96