translate_test_vectors.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 # coding=utf-8
3 # Copyright (c) 2020, Google Inc.
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 
19 Usage: translate_test_vectors.py TEST_VECTORS_JSON_FILE
20 
21 The TEST_VECTORS_JSON_FILE is expected to come from the JSON copy of
22 draft-irtf-cfrg-hpke-12's test vectors, linked from its [TestVectors] citation.
23 The output is written to "hpke_test_vectors.txt".
24 """
25 
26 import collections
27 import json
28 import sys
29 
30 HPKE_MODE_BASE = 0
31 HPKE_MODE_PSK = 1
32 HPKE_DHKEM_X25519_SHA256 = 0x0020
33 HPKE_HKDF_SHA256 = 0x0001
34 HPKE_AEAD_EXPORT_ONLY = 0xffff
35 
36 
37 def read_test_vectors_and_generate_code(json_file_in_path, test_file_out_path):
38  """Translates JSON test vectors into BoringSSL's FileTest language.
39 
40  Args:
41  json_file_in_path: Path to the JSON test vectors file.
42  test_file_out_path: Path to output file.
43  """
44 
45  # Load the JSON file into |test_vecs|.
46  with open(json_file_in_path) as file_in:
47  test_vecs = json.load(file_in)
48 
49  lines = []
50  for test in test_vecs:
51  # Filter out test cases that we don't use.
52  if (test["mode"] != HPKE_MODE_BASE or
53  test["kem_id"] != HPKE_DHKEM_X25519_SHA256 or
54  test["aead_id"] == HPKE_AEAD_EXPORT_ONLY or
55  test["kdf_id"] != HPKE_HKDF_SHA256):
56  continue
57 
58  keys = ["mode", "kdf_id", "aead_id", "info", "skRm", "skEm", "pkRm", "pkEm"]
59 
60  if test["mode"] == HPKE_MODE_PSK:
61  keys.append("psk")
62  keys.append("psk_id")
63 
64  for key in keys:
65  lines.append("{} = {}".format(key, str(test[key])))
66 
67  for i, enc in enumerate(test["encryptions"]):
68  lines.append("# encryptions[{}]".format(i))
69  for key in ("aad", "ct", "pt"):
70  lines.append("{} = {}".format(key, str(enc[key])))
71 
72  for i, exp in enumerate(test["exports"]):
73  lines.append("# exports[{}]".format(i))
74  for key in ("exporter_context", "L", "exported_value"):
75  lines.append("{} = {}".format(key, str(exp[key])))
76 
77  lines.append("")
78 
79  with open(test_file_out_path, "w") as file_out:
80  file_out.write("\n".join(lines))
81 
82 
83 def main(argv):
84  if len(argv) != 2:
85  print(__doc__)
86  sys.exit(1)
87 
88  read_test_vectors_and_generate_code(argv[1], "hpke_test_vectors.txt")
89 
90 
91 if __name__ == "__main__":
92  main(sys.argv)
xds_interop_client.str
str
Definition: xds_interop_client.py:487
http2_test_server.format
format
Definition: http2_test_server.py:118
translate_test_vectors.main
def main(argv)
Definition: translate_test_vectors.py:83
main
Definition: main.py:1
open
#define open
Definition: test-fs.c:46
translate_test_vectors.read_test_vectors_and_generate_code
def read_test_vectors_and_generate_code(json_file_in_path, test_file_out_path)
Definition: translate_test_vectors.py:37
len
int len
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:46


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:40