Skip to content

Commit 3accf84

Browse files
Merge pull request Mbed-TLS#17 from ronald-cron-arm/various-fixes
Fix config_psa.h extension and main update process
2 parents 01c6192 + 9f28fc5 commit 3accf84

File tree

2 files changed

+76
-42
lines changed

2 files changed

+76
-42
lines changed

docs/architecture/psa-crypto-repository.md

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -177,24 +177,51 @@ library though.
177177
The PSA-Crypto repository provides a reference implementation of the
178178
PSA cryptography API through its main branch.
179179

180-
The main branch is updated against the head of the Mbed TLS development branch
181-
according to the following sequence where \<mbedtls-commit-id> is the identifier
182-
of the head of the Mbed TLS development branch, \<mbedtls-pr\> is the number
183-
of the last PR merged into the Mbed TLS development branch and
184-
\<psa-crypto-commit-id\> is the identifier of the head of the development
185-
branch of this repository used for the update. Just the first nine characters
186-
of the commit identifiers are used.
187-
188-
* Checkout the Mbed TLS branch https://github.com/ronald-cron-arm/mbedtls/tree/psa-crypto-repository.
189-
This branch should have been rebased beforehand on top of the head of the
190-
Mbed TLS development branch we want to update against.
180+
The main branch head is built from a commit of the PSA-Crypto development
181+
branch and a commit of the Mbed TLS development branch. Updating the main
182+
branch consists in moving its head to be based on more recent commits of the
183+
PSA-Crypto and Mbed TLS development branches. In the following,
184+
\<mbedtls-commit-id\> is the identifier of the commit of the Mbed TLS
185+
development branch used to update the main branch, \<mbedtls-pr\> is
186+
the number of the last PR merged into this commit, \<psa-crypto-commit-id\> is
187+
the identifier of the commit of the development branch of this repository used
188+
for the update and \<psa-crypto-pr\> the number of the last PR merged into that
189+
commit. Just the first nine characters of the commit identifiers are used.
190+
191+
An update follows the following flow:
192+
193+
* A base-for-psa-crypto-PR\<psa-crypto-pr\> branch is created in
194+
https://github.com/ronald-cron-arm/mbedtls/tree/psa-crypto-repository. The
195+
branch is the Mbed TLS commit we want to update against plus a few additional
196+
commits. This specific branch is created to keep track of those few
197+
additional commits.
198+
* Checkout locally the base-for-psa-crypto-PR\<psa-crypto-pr\> branch.
199+
200+
Build what we want to become the new head of the main branch:
191201
* cd path/to/my/psa/crypto/repo
192-
* git checkout -b update-against-\<mbedtls-commit-id\>-PR\<mbedtls-pr\>-with-\<psa-crypto-commit-id\>
193-
development
194-
* ./scripts/psa_crypto.py --mbedlts path/to/the/mbedtls/branch
202+
* git checkout -b new-main development
203+
* git clean -fdx
204+
* ./scripts/psa_crypto.py --mbedtls path/to/the/mbedtls/branch/checked/out/above
195205
* git add --all
196-
* git commit -s -m"Update against \<mbedtls-commit-id\>(PR \<mbedtls-pr\>) with \<psa-crypto-commit-id\>"
197-
* Create a PR against the main branch with the branch that has just been created.
206+
* git commit -s -m"New main head"
207+
208+
Create the branch for the update pull request from current main head, merge
209+
into it the PSA-Crypto development branch to get its last version (not necessary
210+
if the PSA-Crypto development branch has not changed since the last update)
211+
and then update the PSA cryptography implementation by applying the patch to
212+
end up with the same tree as the new-main branch.
213+
* git checkout -b update-against-\<mbedtls-commit-id\>-PR\<mbedtls-pr\>-with-\<psa-crypto-commit-id\>-PR\<psa-crypto-pr\> main
214+
* git merge development -m"Merge \<psa-crypto-commit-id\>-PR\<psa-crypto-pr\>"
215+
* git diff HEAD new-main > patch.file
216+
* git apply patch.file
217+
* rm patch.file
218+
* git add --all
219+
* git commit -s -m"Update against \<mbedtls-commit-id\>(PR \<mbedtls-pr\>)"
220+
221+
Clean-up
222+
* git branch -D new-main
223+
224+
* Create a PR against the main branch with the update branch created above.
198225
* Merge the PR which completes the update.
199226

200227
## Comparison with the Mbed TLS cryptography library

scripts/psa_crypto.py

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import stat
2424
import re
2525
import shutil
26-
from distutils.dir_util import copy_tree
2726

2827
def copy_of_psa_headers(mbedtls_root_path, psa_crypto_root_path):
2928
source_path = os.path.join(mbedtls_root_path, "include", "psa")
@@ -91,33 +90,38 @@ def copy_from_scripts(mbedtls_root_path, psa_crypto_root_path):
9190
source_path = os.path.join(mbedtls_root_path, "scripts")
9291
destination_path = os.path.join(psa_crypto_root_path, "scripts")
9392

94-
copy_tree(os.path.join(source_path, "data_files", "driver_jsons"),
95-
os.path.join(destination_path, "data_files", "driver_jsons"))
96-
copy_tree(os.path.join(source_path, "data_files", "driver_templates"),
97-
os.path.join(destination_path, "data_files", "driver_templates"))
93+
shutil.copytree(os.path.join(source_path, "data_files", "driver_jsons"),
94+
os.path.join(destination_path, "data_files", "driver_jsons"),
95+
dirs_exist_ok=True)
96+
shutil.copytree(os.path.join(source_path, "data_files", "driver_templates"),
97+
os.path.join(destination_path, "data_files", "driver_templates"),
98+
dirs_exist_ok=True)
9899

99100
shutil.copy2(os.path.join(source_path, "generate_driver_wrappers.py"), destination_path)
100101
shutil.copy2(os.path.join(source_path, "generate_psa_constants.py"), destination_path)
101102
shutil.copy2(os.path.join(source_path, "output_env.sh"), destination_path)
102103
shutil.copy2(os.path.join(source_path, "config.py"), destination_path)
103104

104-
copy_tree(os.path.join(source_path, "mbedtls_dev"),
105-
os.path.join(destination_path, "mbedtls_dev"))
105+
shutil.copytree(os.path.join(source_path, "mbedtls_dev"),
106+
os.path.join(destination_path, "mbedtls_dev"), dirs_exist_ok=True)
106107

107108
def copy_from_tests(mbedtls_root_path, psa_crypto_root_path):
108109
source_path = os.path.join(mbedtls_root_path, "tests")
109110
destination_path = os.path.join(psa_crypto_root_path, "tests")
110111

111112
shutil.copy2(os.path.join(source_path, "seedfile"), destination_path)
112113

113-
copy_tree( os.path.join( source_path, "include" ),
114-
os.path.join( destination_path, "include" ) )
114+
shutil.copytree(os.path.join(source_path, "include"),
115+
os.path.join(destination_path, "include"),
116+
dirs_exist_ok=True)
115117

116-
copy_tree( os.path.join( source_path, "scripts" ),
117-
os.path.join( destination_path, "scripts" ) )
118+
shutil.copytree(os.path.join(source_path, "scripts"),
119+
os.path.join(destination_path, "scripts"),
120+
dirs_exist_ok=True)
118121

119-
copy_tree( os.path.join( source_path, "src" ),
120-
os.path.join( destination_path, "src" ) )
122+
shutil.copytree(os.path.join(source_path, "src"),
123+
os.path.join(destination_path, "src"),
124+
dirs_exist_ok=True)
121125

122126
tests_suites_files = filter(lambda file_: re.match(
123127
"test_suite_psa_crypto.*|helpers\.function|"\
@@ -150,9 +154,9 @@ def replace_all_sh_components(psa_crypto_root_path):
150154
after_components = 0
151155
components_start = re.compile(r"#### Basic checks")
152156
components_end = re.compile(r"#### Termination")
153-
new_all_sh = open(os.path.join(tests_scripts_path, "all.sh"), 'x')
154157

155-
with open(os.path.join(tests_scripts_path, "all.sh.bak"), 'rt') as all_sh:
158+
with open(os.path.join(tests_scripts_path, "all.sh"), 'x') as new_all_sh, \
159+
open(os.path.join(tests_scripts_path, "all.sh.bak"), 'rt') as all_sh:
156160
for line in all_sh:
157161
if before_components:
158162
if components_start.match(line) != None:
@@ -176,7 +180,6 @@ def replace_all_sh_components(psa_crypto_root_path):
176180
if after_components:
177181
new_all_sh.write(line)
178182

179-
new_all_sh.close()
180183
os.chmod(os.path.join(tests_scripts_path, "all.sh"), stat.S_IEXEC | stat.S_IREAD | stat.S_IWRITE)
181184

182185
def extend_config_psa(psa_crypto_root_path):
@@ -187,9 +190,11 @@ def extend_config_psa(psa_crypto_root_path):
187190
if_defined_mbedtls_psa_crypto_config_file = re.compile("#if defined\(MBEDTLS_PSA_CRYPTO_CONFIG_FILE\)")
188191
include_mbedtls_psa_crypto_config_file = re.compile("#include MBEDTLS_PSA_CRYPTO_CONFIG_FILE")
189192
ext_placeholder = re.compile(".*BELOW THIS LINE - PLACEHOLDER FOR PSA-CRYPTO ADDITIONAL CONFIG OPTIONS TRANSLATION")
190-
new_config_psa = open(os.path.join(include_mbedtls_path, "config_psa.h"), 'x')
193+
endif_mbedtls_psa_crypto_config = re.compile("#endif /\* MBEDTLS_PSA_CRYPTO_CONFIG \*/")
194+
195+
with open(os.path.join(include_mbedtls_path, "config_psa.h"), 'x') as new_config_psa, \
196+
open(os.path.join(include_mbedtls_path, "config_psa.h.bak"), 'rt') as config_psa:
191197

192-
with open(os.path.join(include_mbedtls_path, "config_psa.h.bak"), 'rt') as config_psa:
193198
for line in config_psa:
194199
if if_defined_mbedtls_psa_crypto_config_file.match(line) != None:
195200
new_config_psa.write("#if defined(PSA_CRYPTO_CONFIG_FILE)\n")
@@ -200,15 +205,17 @@ def extend_config_psa(psa_crypto_root_path):
200205
else:
201206
new_config_psa.write(line)
202207

203-
config_psa.close()
204-
205-
with open(os.path.join(psa_crypto_root_path, "drivers", "builtin", "config_psa_ext.h"), 'rt') as ext:
206-
for line in ext:
207-
new_config_psa.write(line)
208+
with open(os.path.join(psa_crypto_root_path, "drivers", "builtin", "config_psa_ext.h"), 'rt') as ext:
209+
for line in ext:
210+
new_config_psa.write(line)
208211

209-
new_config_psa.write("\n#endif /* MBEDTLS_PSA_CRYPTO_CONFIG */")
210-
new_config_psa.write("\n#endif /* MBEDTLS_CONFIG_PSA_H */")
211-
new_config_psa.close()
212+
trailer = False
213+
for line in config_psa:
214+
if endif_mbedtls_psa_crypto_config.match(line) != None:
215+
new_config_psa.write("\n")
216+
trailer = True
217+
if trailer:
218+
new_config_psa.write(line)
212219

213220
def main():
214221
parser = argparse.ArgumentParser(

0 commit comments

Comments
 (0)