1# !/usr/bin/env python3 2 3import json 4 5from pathlib import Path 6 7# Can only process base mode 0 8# https://www.rfc-editor.org/rfc/rfc9180.html#name-hybrid-public-key-encryptio 9MODE_BASE = 0x00 10 11# Can only process KEM DHKEM(X25519, HKDF-SHA256) = 0x0020 12# https://www.rfc-editor.org/rfc/rfc9180.html#name-key-encapsulation-mechanism 13KEM_DHKEM_X25519_SHA256 = 0x0020 14 15# Can only process KDF HKDF-SHA256 16# https://www.rfc-editor.org/rfc/rfc9180.html#name-key-derivation-functions-kd 17KDF_HKDF_SHA256 = 0x0001 18 19# Can process all AEADs except EXPORT-only as this will be generating 20# a file to test encryption/decryption only, not secret exports 21# https://www.rfc-editor.org/rfc/rfc9180.html#name-authenticated-encryption-wi 22AEAD_EXPORT_ONLY = 0xffff 23 24 25def parse_and_format(file_in_path: str, file_out_path: str) -> None: 26 """ 27 Parse and formats test-vectors.txt into Conscrypt's format. 28 A copy of the JSON file mentioned in RFC 9180 must be placed right next to 29 this script. A file will be created named "hpke-encryption.csv" 30 31 Parameters: 32 file_in_path: Absolute path to test-vectors.txt. 33 file_out_path: Absolute path to output file. 34 """ 35 36 with open(file_in_path) as input: 37 payload = json.load(input) 38 39 records = ["# Data is in the format:", 40 "# kem_id,kdf_id,aead_id,info,skRm,skEm,pkRm,pkEm,exporter_context,L,exported_value"] 41 42 for key in payload: 43 # Skip these to test only capabilities exposed by BoringSSL 44 if (key["mode"] != MODE_BASE or 45 key["kem_id"] != KEM_DHKEM_X25519_SHA256 or 46 key["kdf_id"] != KDF_HKDF_SHA256 or 47 key["aead_id"] == AEAD_EXPORT_ONLY): 48 continue 49 50 for exportKey in key["exports"]: 51 records.append("{},{},{},{},{},{},{},{},{},{},{}" 52 .format(str(key["kem_id"]), 53 str(key["kdf_id"]), 54 str(key["aead_id"]), 55 str(key["info"]), 56 str(key["skRm"]), 57 str(key["skEm"]), 58 str(key["pkRm"]), 59 str(key["pkEm"]), 60 str(exportKey["exporter_context"]), 61 str(exportKey["L"]), 62 str(exportKey["exported_value"]))) 63 64 65 with open(file_out_path, "w") as output: 66 output.write("\n".join(records)) 67 68 69def main(): 70 path = Path(__file__).parent.absolute() 71 parse_and_format(path / "test-vectors.txt", path / "hpke-export.csv") 72 73 74if __name__ == "__main__": 75 main() 76