1a8e1175bSopenharmony_ci#!/usr/bin/env python3
2a8e1175bSopenharmony_ci
3a8e1175bSopenharmony_ci"""Generate psa_constant_names_generated.c
4a8e1175bSopenharmony_ciwhich is included by programs/psa/psa_constant_names.c.
5a8e1175bSopenharmony_ciThe code generated by this module is only meant to be used in the context
6a8e1175bSopenharmony_ciof that program.
7a8e1175bSopenharmony_ci
8a8e1175bSopenharmony_ciAn argument passed to this script will modify the output directory where the
9a8e1175bSopenharmony_cifile is written:
10a8e1175bSopenharmony_ci* by default (no arguments passed): writes to programs/psa/
11a8e1175bSopenharmony_ci* OUTPUT_FILE_DIR passed: writes to OUTPUT_FILE_DIR/
12a8e1175bSopenharmony_ci"""
13a8e1175bSopenharmony_ci
14a8e1175bSopenharmony_ci# Copyright The Mbed TLS Contributors
15a8e1175bSopenharmony_ci# SPDX-License-Identifier: Apache-2.0
16a8e1175bSopenharmony_ci#
17a8e1175bSopenharmony_ci# Licensed under the Apache License, Version 2.0 (the "License"); you may
18a8e1175bSopenharmony_ci# not use this file except in compliance with the License.
19a8e1175bSopenharmony_ci# You may obtain a copy of the License at
20a8e1175bSopenharmony_ci#
21a8e1175bSopenharmony_ci# http://www.apache.org/licenses/LICENSE-2.0
22a8e1175bSopenharmony_ci#
23a8e1175bSopenharmony_ci# Unless required by applicable law or agreed to in writing, software
24a8e1175bSopenharmony_ci# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
25a8e1175bSopenharmony_ci# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
26a8e1175bSopenharmony_ci# See the License for the specific language governing permissions and
27a8e1175bSopenharmony_ci# limitations under the License.
28a8e1175bSopenharmony_ci
29a8e1175bSopenharmony_ciimport os
30a8e1175bSopenharmony_ciimport sys
31a8e1175bSopenharmony_ci
32a8e1175bSopenharmony_cifrom mbedtls_dev import build_tree
33a8e1175bSopenharmony_cifrom mbedtls_dev import macro_collector
34a8e1175bSopenharmony_ci
35a8e1175bSopenharmony_ciOUTPUT_TEMPLATE = '''\
36a8e1175bSopenharmony_ci/* Automatically generated by generate_psa_constant.py. DO NOT EDIT. */
37a8e1175bSopenharmony_ci
38a8e1175bSopenharmony_cistatic const char *psa_strerror(psa_status_t status)
39a8e1175bSopenharmony_ci{
40a8e1175bSopenharmony_ci    switch (status) {
41a8e1175bSopenharmony_ci    %(status_cases)s
42a8e1175bSopenharmony_ci    default: return NULL;
43a8e1175bSopenharmony_ci    }
44a8e1175bSopenharmony_ci}
45a8e1175bSopenharmony_ci
46a8e1175bSopenharmony_cistatic const char *psa_ecc_family_name(psa_ecc_family_t curve)
47a8e1175bSopenharmony_ci{
48a8e1175bSopenharmony_ci    switch (curve) {
49a8e1175bSopenharmony_ci    %(ecc_curve_cases)s
50a8e1175bSopenharmony_ci    default: return NULL;
51a8e1175bSopenharmony_ci    }
52a8e1175bSopenharmony_ci}
53a8e1175bSopenharmony_ci
54a8e1175bSopenharmony_cistatic const char *psa_dh_family_name(psa_dh_family_t group)
55a8e1175bSopenharmony_ci{
56a8e1175bSopenharmony_ci    switch (group) {
57a8e1175bSopenharmony_ci    %(dh_group_cases)s
58a8e1175bSopenharmony_ci    default: return NULL;
59a8e1175bSopenharmony_ci    }
60a8e1175bSopenharmony_ci}
61a8e1175bSopenharmony_ci
62a8e1175bSopenharmony_cistatic const char *psa_hash_algorithm_name(psa_algorithm_t hash_alg)
63a8e1175bSopenharmony_ci{
64a8e1175bSopenharmony_ci    switch (hash_alg) {
65a8e1175bSopenharmony_ci    %(hash_algorithm_cases)s
66a8e1175bSopenharmony_ci    default: return NULL;
67a8e1175bSopenharmony_ci    }
68a8e1175bSopenharmony_ci}
69a8e1175bSopenharmony_ci
70a8e1175bSopenharmony_cistatic const char *psa_ka_algorithm_name(psa_algorithm_t ka_alg)
71a8e1175bSopenharmony_ci{
72a8e1175bSopenharmony_ci    switch (ka_alg) {
73a8e1175bSopenharmony_ci    %(ka_algorithm_cases)s
74a8e1175bSopenharmony_ci    default: return NULL;
75a8e1175bSopenharmony_ci    }
76a8e1175bSopenharmony_ci}
77a8e1175bSopenharmony_ci
78a8e1175bSopenharmony_cistatic int psa_snprint_key_type(char *buffer, size_t buffer_size,
79a8e1175bSopenharmony_ci                                psa_key_type_t type)
80a8e1175bSopenharmony_ci{
81a8e1175bSopenharmony_ci    size_t required_size = 0;
82a8e1175bSopenharmony_ci    switch (type) {
83a8e1175bSopenharmony_ci    %(key_type_cases)s
84a8e1175bSopenharmony_ci    default:
85a8e1175bSopenharmony_ci        %(key_type_code)s{
86a8e1175bSopenharmony_ci            return snprintf(buffer, buffer_size,
87a8e1175bSopenharmony_ci                            "0x%%04x", (unsigned) type);
88a8e1175bSopenharmony_ci        }
89a8e1175bSopenharmony_ci        break;
90a8e1175bSopenharmony_ci    }
91a8e1175bSopenharmony_ci    buffer[0] = 0;
92a8e1175bSopenharmony_ci    return (int) required_size;
93a8e1175bSopenharmony_ci}
94a8e1175bSopenharmony_ci
95a8e1175bSopenharmony_ci#define NO_LENGTH_MODIFIER 0xfffffffflu
96a8e1175bSopenharmony_cistatic int psa_snprint_algorithm(char *buffer, size_t buffer_size,
97a8e1175bSopenharmony_ci                                 psa_algorithm_t alg)
98a8e1175bSopenharmony_ci{
99a8e1175bSopenharmony_ci    size_t required_size = 0;
100a8e1175bSopenharmony_ci    psa_algorithm_t core_alg = alg;
101a8e1175bSopenharmony_ci    unsigned long length_modifier = NO_LENGTH_MODIFIER;
102a8e1175bSopenharmony_ci    if (PSA_ALG_IS_MAC(alg)) {
103a8e1175bSopenharmony_ci        core_alg = PSA_ALG_TRUNCATED_MAC(alg, 0);
104a8e1175bSopenharmony_ci        if (alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG) {
105a8e1175bSopenharmony_ci            append(&buffer, buffer_size, &required_size,
106a8e1175bSopenharmony_ci                   "PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(", 33);
107a8e1175bSopenharmony_ci            length_modifier = PSA_MAC_TRUNCATED_LENGTH(alg);
108a8e1175bSopenharmony_ci        } else if (core_alg != alg) {
109a8e1175bSopenharmony_ci            append(&buffer, buffer_size, &required_size,
110a8e1175bSopenharmony_ci                   "PSA_ALG_TRUNCATED_MAC(", 22);
111a8e1175bSopenharmony_ci            length_modifier = PSA_MAC_TRUNCATED_LENGTH(alg);
112a8e1175bSopenharmony_ci        }
113a8e1175bSopenharmony_ci    } else if (PSA_ALG_IS_AEAD(alg)) {
114a8e1175bSopenharmony_ci        core_alg = PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg);
115a8e1175bSopenharmony_ci        if (core_alg == 0) {
116a8e1175bSopenharmony_ci            /* For unknown AEAD algorithms, there is no "default tag length". */
117a8e1175bSopenharmony_ci            core_alg = alg;
118a8e1175bSopenharmony_ci        } else if (alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG) {
119a8e1175bSopenharmony_ci            append(&buffer, buffer_size, &required_size,
120a8e1175bSopenharmony_ci                   "PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(", 43);
121a8e1175bSopenharmony_ci            length_modifier = PSA_ALG_AEAD_GET_TAG_LENGTH(alg);
122a8e1175bSopenharmony_ci        } else if (core_alg != alg) {
123a8e1175bSopenharmony_ci            append(&buffer, buffer_size, &required_size,
124a8e1175bSopenharmony_ci                   "PSA_ALG_AEAD_WITH_SHORTENED_TAG(", 32);
125a8e1175bSopenharmony_ci            length_modifier = PSA_ALG_AEAD_GET_TAG_LENGTH(alg);
126a8e1175bSopenharmony_ci        }
127a8e1175bSopenharmony_ci    } else if (PSA_ALG_IS_KEY_AGREEMENT(alg) &&
128a8e1175bSopenharmony_ci               !PSA_ALG_IS_RAW_KEY_AGREEMENT(alg)) {
129a8e1175bSopenharmony_ci        core_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
130a8e1175bSopenharmony_ci        append(&buffer, buffer_size, &required_size,
131a8e1175bSopenharmony_ci               "PSA_ALG_KEY_AGREEMENT(", 22);
132a8e1175bSopenharmony_ci        append_with_alg(&buffer, buffer_size, &required_size,
133a8e1175bSopenharmony_ci                        psa_ka_algorithm_name,
134a8e1175bSopenharmony_ci                        PSA_ALG_KEY_AGREEMENT_GET_BASE(alg));
135a8e1175bSopenharmony_ci        append(&buffer, buffer_size, &required_size, ", ", 2);
136a8e1175bSopenharmony_ci    }
137a8e1175bSopenharmony_ci    switch (core_alg) {
138a8e1175bSopenharmony_ci    %(algorithm_cases)s
139a8e1175bSopenharmony_ci    default:
140a8e1175bSopenharmony_ci        %(algorithm_code)s{
141a8e1175bSopenharmony_ci            append_integer(&buffer, buffer_size, &required_size,
142a8e1175bSopenharmony_ci                           "0x%%08lx", (unsigned long) core_alg);
143a8e1175bSopenharmony_ci        }
144a8e1175bSopenharmony_ci        break;
145a8e1175bSopenharmony_ci    }
146a8e1175bSopenharmony_ci    if (core_alg != alg) {
147a8e1175bSopenharmony_ci        if (length_modifier != NO_LENGTH_MODIFIER) {
148a8e1175bSopenharmony_ci            append(&buffer, buffer_size, &required_size, ", ", 2);
149a8e1175bSopenharmony_ci            append_integer(&buffer, buffer_size, &required_size,
150a8e1175bSopenharmony_ci                           "%%lu", length_modifier);
151a8e1175bSopenharmony_ci        }
152a8e1175bSopenharmony_ci        append(&buffer, buffer_size, &required_size, ")", 1);
153a8e1175bSopenharmony_ci    }
154a8e1175bSopenharmony_ci    buffer[0] = 0;
155a8e1175bSopenharmony_ci    return (int) required_size;
156a8e1175bSopenharmony_ci}
157a8e1175bSopenharmony_ci
158a8e1175bSopenharmony_cistatic int psa_snprint_key_usage(char *buffer, size_t buffer_size,
159a8e1175bSopenharmony_ci                                 psa_key_usage_t usage)
160a8e1175bSopenharmony_ci{
161a8e1175bSopenharmony_ci    size_t required_size = 0;
162a8e1175bSopenharmony_ci    if (usage == 0) {
163a8e1175bSopenharmony_ci        if (buffer_size > 1) {
164a8e1175bSopenharmony_ci            buffer[0] = '0';
165a8e1175bSopenharmony_ci            buffer[1] = 0;
166a8e1175bSopenharmony_ci        } else if (buffer_size == 1) {
167a8e1175bSopenharmony_ci            buffer[0] = 0;
168a8e1175bSopenharmony_ci        }
169a8e1175bSopenharmony_ci        return 1;
170a8e1175bSopenharmony_ci    }
171a8e1175bSopenharmony_ci%(key_usage_code)s
172a8e1175bSopenharmony_ci    if (usage != 0) {
173a8e1175bSopenharmony_ci        if (required_size != 0) {
174a8e1175bSopenharmony_ci            append(&buffer, buffer_size, &required_size, " | ", 3);
175a8e1175bSopenharmony_ci        }
176a8e1175bSopenharmony_ci        append_integer(&buffer, buffer_size, &required_size,
177a8e1175bSopenharmony_ci                       "0x%%08lx", (unsigned long) usage);
178a8e1175bSopenharmony_ci    } else {
179a8e1175bSopenharmony_ci        buffer[0] = 0;
180a8e1175bSopenharmony_ci    }
181a8e1175bSopenharmony_ci    return (int) required_size;
182a8e1175bSopenharmony_ci}
183a8e1175bSopenharmony_ci
184a8e1175bSopenharmony_ci/* End of automatically generated file. */
185a8e1175bSopenharmony_ci'''
186a8e1175bSopenharmony_ci
187a8e1175bSopenharmony_ciKEY_TYPE_FROM_CURVE_TEMPLATE = '''if (%(tester)s(type)) {
188a8e1175bSopenharmony_ci            append_with_curve(&buffer, buffer_size, &required_size,
189a8e1175bSopenharmony_ci                              "%(builder)s", %(builder_length)s,
190a8e1175bSopenharmony_ci                              PSA_KEY_TYPE_ECC_GET_FAMILY(type));
191a8e1175bSopenharmony_ci        } else '''
192a8e1175bSopenharmony_ci
193a8e1175bSopenharmony_ciKEY_TYPE_FROM_GROUP_TEMPLATE = '''if (%(tester)s(type)) {
194a8e1175bSopenharmony_ci            append_with_group(&buffer, buffer_size, &required_size,
195a8e1175bSopenharmony_ci                              "%(builder)s", %(builder_length)s,
196a8e1175bSopenharmony_ci                              PSA_KEY_TYPE_DH_GET_FAMILY(type));
197a8e1175bSopenharmony_ci        } else '''
198a8e1175bSopenharmony_ci
199a8e1175bSopenharmony_ciALGORITHM_FROM_HASH_TEMPLATE = '''if (%(tester)s(core_alg)) {
200a8e1175bSopenharmony_ci            append(&buffer, buffer_size, &required_size,
201a8e1175bSopenharmony_ci                   "%(builder)s(", %(builder_length)s + 1);
202a8e1175bSopenharmony_ci            append_with_alg(&buffer, buffer_size, &required_size,
203a8e1175bSopenharmony_ci                            psa_hash_algorithm_name,
204a8e1175bSopenharmony_ci                            PSA_ALG_GET_HASH(core_alg));
205a8e1175bSopenharmony_ci            append(&buffer, buffer_size, &required_size, ")", 1);
206a8e1175bSopenharmony_ci        } else '''
207a8e1175bSopenharmony_ci
208a8e1175bSopenharmony_ciBIT_TEST_TEMPLATE = '''\
209a8e1175bSopenharmony_ci    if (%(var)s & %(flag)s) {
210a8e1175bSopenharmony_ci        if (required_size != 0) {
211a8e1175bSopenharmony_ci            append(&buffer, buffer_size, &required_size, " | ", 3);
212a8e1175bSopenharmony_ci        }
213a8e1175bSopenharmony_ci        append(&buffer, buffer_size, &required_size, "%(flag)s", %(length)d);
214a8e1175bSopenharmony_ci        %(var)s ^= %(flag)s;
215a8e1175bSopenharmony_ci    }\
216a8e1175bSopenharmony_ci'''
217a8e1175bSopenharmony_ci
218a8e1175bSopenharmony_ciclass CaseBuilder(macro_collector.PSAMacroCollector):
219a8e1175bSopenharmony_ci    """Collect PSA crypto macro definitions and write value recognition functions.
220a8e1175bSopenharmony_ci
221a8e1175bSopenharmony_ci    1. Call `read_file` on the input header file(s).
222a8e1175bSopenharmony_ci    2. Call `write_file` to write ``psa_constant_names_generated.c``.
223a8e1175bSopenharmony_ci    """
224a8e1175bSopenharmony_ci
225a8e1175bSopenharmony_ci    def __init__(self):
226a8e1175bSopenharmony_ci        super().__init__(include_intermediate=True)
227a8e1175bSopenharmony_ci
228a8e1175bSopenharmony_ci    @staticmethod
229a8e1175bSopenharmony_ci    def _make_return_case(name):
230a8e1175bSopenharmony_ci        return 'case %(name)s: return "%(name)s";' % {'name': name}
231a8e1175bSopenharmony_ci
232a8e1175bSopenharmony_ci    @staticmethod
233a8e1175bSopenharmony_ci    def _make_append_case(name):
234a8e1175bSopenharmony_ci        template = ('case %(name)s: '
235a8e1175bSopenharmony_ci                    'append(&buffer, buffer_size, &required_size, "%(name)s", %(length)d); '
236a8e1175bSopenharmony_ci                    'break;')
237a8e1175bSopenharmony_ci        return template % {'name': name, 'length': len(name)}
238a8e1175bSopenharmony_ci
239a8e1175bSopenharmony_ci    @staticmethod
240a8e1175bSopenharmony_ci    def _make_bit_test(var, flag):
241a8e1175bSopenharmony_ci        return BIT_TEST_TEMPLATE % {'var': var,
242a8e1175bSopenharmony_ci                                    'flag': flag,
243a8e1175bSopenharmony_ci                                    'length': len(flag)}
244a8e1175bSopenharmony_ci
245a8e1175bSopenharmony_ci    def _make_status_cases(self):
246a8e1175bSopenharmony_ci        return '\n    '.join(map(self._make_return_case,
247a8e1175bSopenharmony_ci                                 sorted(self.statuses)))
248a8e1175bSopenharmony_ci
249a8e1175bSopenharmony_ci    def _make_ecc_curve_cases(self):
250a8e1175bSopenharmony_ci        return '\n    '.join(map(self._make_return_case,
251a8e1175bSopenharmony_ci                                 sorted(self.ecc_curves)))
252a8e1175bSopenharmony_ci
253a8e1175bSopenharmony_ci    def _make_dh_group_cases(self):
254a8e1175bSopenharmony_ci        return '\n    '.join(map(self._make_return_case,
255a8e1175bSopenharmony_ci                                 sorted(self.dh_groups)))
256a8e1175bSopenharmony_ci
257a8e1175bSopenharmony_ci    def _make_key_type_cases(self):
258a8e1175bSopenharmony_ci        return '\n    '.join(map(self._make_append_case,
259a8e1175bSopenharmony_ci                                 sorted(self.key_types)))
260a8e1175bSopenharmony_ci
261a8e1175bSopenharmony_ci    @staticmethod
262a8e1175bSopenharmony_ci    def _make_key_type_from_curve_code(builder, tester):
263a8e1175bSopenharmony_ci        return KEY_TYPE_FROM_CURVE_TEMPLATE % {'builder': builder,
264a8e1175bSopenharmony_ci                                               'builder_length': len(builder),
265a8e1175bSopenharmony_ci                                               'tester': tester}
266a8e1175bSopenharmony_ci
267a8e1175bSopenharmony_ci    @staticmethod
268a8e1175bSopenharmony_ci    def _make_key_type_from_group_code(builder, tester):
269a8e1175bSopenharmony_ci        return KEY_TYPE_FROM_GROUP_TEMPLATE % {'builder': builder,
270a8e1175bSopenharmony_ci                                               'builder_length': len(builder),
271a8e1175bSopenharmony_ci                                               'tester': tester}
272a8e1175bSopenharmony_ci
273a8e1175bSopenharmony_ci    def _make_ecc_key_type_code(self):
274a8e1175bSopenharmony_ci        d = self.key_types_from_curve
275a8e1175bSopenharmony_ci        make = self._make_key_type_from_curve_code
276a8e1175bSopenharmony_ci        return ''.join([make(k, d[k]) for k in sorted(d.keys())])
277a8e1175bSopenharmony_ci
278a8e1175bSopenharmony_ci    def _make_dh_key_type_code(self):
279a8e1175bSopenharmony_ci        d = self.key_types_from_group
280a8e1175bSopenharmony_ci        make = self._make_key_type_from_group_code
281a8e1175bSopenharmony_ci        return ''.join([make(k, d[k]) for k in sorted(d.keys())])
282a8e1175bSopenharmony_ci
283a8e1175bSopenharmony_ci    def _make_hash_algorithm_cases(self):
284a8e1175bSopenharmony_ci        return '\n    '.join(map(self._make_return_case,
285a8e1175bSopenharmony_ci                                 sorted(self.hash_algorithms)))
286a8e1175bSopenharmony_ci
287a8e1175bSopenharmony_ci    def _make_ka_algorithm_cases(self):
288a8e1175bSopenharmony_ci        return '\n    '.join(map(self._make_return_case,
289a8e1175bSopenharmony_ci                                 sorted(self.ka_algorithms)))
290a8e1175bSopenharmony_ci
291a8e1175bSopenharmony_ci    def _make_algorithm_cases(self):
292a8e1175bSopenharmony_ci        return '\n    '.join(map(self._make_append_case,
293a8e1175bSopenharmony_ci                                 sorted(self.algorithms)))
294a8e1175bSopenharmony_ci
295a8e1175bSopenharmony_ci    @staticmethod
296a8e1175bSopenharmony_ci    def _make_algorithm_from_hash_code(builder, tester):
297a8e1175bSopenharmony_ci        return ALGORITHM_FROM_HASH_TEMPLATE % {'builder': builder,
298a8e1175bSopenharmony_ci                                               'builder_length': len(builder),
299a8e1175bSopenharmony_ci                                               'tester': tester}
300a8e1175bSopenharmony_ci
301a8e1175bSopenharmony_ci    def _make_algorithm_code(self):
302a8e1175bSopenharmony_ci        d = self.algorithms_from_hash
303a8e1175bSopenharmony_ci        make = self._make_algorithm_from_hash_code
304a8e1175bSopenharmony_ci        return ''.join([make(k, d[k]) for k in sorted(d.keys())])
305a8e1175bSopenharmony_ci
306a8e1175bSopenharmony_ci    def _make_key_usage_code(self):
307a8e1175bSopenharmony_ci        return '\n'.join([self._make_bit_test('usage', bit)
308a8e1175bSopenharmony_ci                          for bit in sorted(self.key_usage_flags)])
309a8e1175bSopenharmony_ci
310a8e1175bSopenharmony_ci    def write_file(self, output_file):
311a8e1175bSopenharmony_ci        """Generate the pretty-printer function code from the gathered
312a8e1175bSopenharmony_ci        constant definitions.
313a8e1175bSopenharmony_ci        """
314a8e1175bSopenharmony_ci        data = {}
315a8e1175bSopenharmony_ci        data['status_cases'] = self._make_status_cases()
316a8e1175bSopenharmony_ci        data['ecc_curve_cases'] = self._make_ecc_curve_cases()
317a8e1175bSopenharmony_ci        data['dh_group_cases'] = self._make_dh_group_cases()
318a8e1175bSopenharmony_ci        data['key_type_cases'] = self._make_key_type_cases()
319a8e1175bSopenharmony_ci        data['key_type_code'] = (self._make_ecc_key_type_code() +
320a8e1175bSopenharmony_ci                                 self._make_dh_key_type_code())
321a8e1175bSopenharmony_ci        data['hash_algorithm_cases'] = self._make_hash_algorithm_cases()
322a8e1175bSopenharmony_ci        data['ka_algorithm_cases'] = self._make_ka_algorithm_cases()
323a8e1175bSopenharmony_ci        data['algorithm_cases'] = self._make_algorithm_cases()
324a8e1175bSopenharmony_ci        data['algorithm_code'] = self._make_algorithm_code()
325a8e1175bSopenharmony_ci        data['key_usage_code'] = self._make_key_usage_code()
326a8e1175bSopenharmony_ci        output_file.write(OUTPUT_TEMPLATE % data)
327a8e1175bSopenharmony_ci
328a8e1175bSopenharmony_cidef generate_psa_constants(header_file_names, output_file_name):
329a8e1175bSopenharmony_ci    collector = CaseBuilder()
330a8e1175bSopenharmony_ci    for header_file_name in header_file_names:
331a8e1175bSopenharmony_ci        with open(header_file_name, 'rb') as header_file:
332a8e1175bSopenharmony_ci            collector.read_file(header_file)
333a8e1175bSopenharmony_ci    temp_file_name = output_file_name + '.tmp'
334a8e1175bSopenharmony_ci    with open(temp_file_name, 'w') as output_file:
335a8e1175bSopenharmony_ci        collector.write_file(output_file)
336a8e1175bSopenharmony_ci    os.replace(temp_file_name, output_file_name)
337a8e1175bSopenharmony_ci
338a8e1175bSopenharmony_ciif __name__ == '__main__':
339a8e1175bSopenharmony_ci    build_tree.chdir_to_root()
340a8e1175bSopenharmony_ci    # Allow to change the directory where psa_constant_names_generated.c is written to.
341a8e1175bSopenharmony_ci    OUTPUT_FILE_DIR = sys.argv[1] if len(sys.argv) == 2 else "programs/psa"
342a8e1175bSopenharmony_ci    generate_psa_constants(['include/psa/crypto_values.h',
343a8e1175bSopenharmony_ci                            'include/psa/crypto_extra.h'],
344a8e1175bSopenharmony_ci                           OUTPUT_FILE_DIR + '/psa_constant_names_generated.c')
345