Skip to content

Commit e2a6872

Browse files
committed
feat: patch mbedtls to compile for baremetal targets
1 parent 4d51c5d commit e2a6872

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

psa-crypto-sys/build.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,11 @@ mod common {
6363
use std::env;
6464
use std::io::{Error, ErrorKind, Result};
6565
use std::path::{Path, PathBuf};
66+
use std::process::Command;
6667

6768
pub fn configure_mbed_crypto() -> Result<()> {
6869
let mbedtls_dir = String::from("./vendor");
69-
let mbedtls_config = mbedtls_dir + "/scripts/config.py";
70+
let mbedtls_config = mbedtls_dir.clone() + "/scripts/config.py";
7071

7172
println!("cargo:rerun-if-changed=src/c/shim.c");
7273
println!("cargo:rerun-if-changed=src/c/shim.h");
@@ -87,6 +88,19 @@ mod common {
8788
"crypto"
8889
};
8990

91+
if mbedtls_mode == "crypto_baremetal" {
92+
// Apply patch to MbedTLS
93+
let patch_path = Path::new("../patches/0001-Update-config-for-baremetal-targets.patch"); // relative to ./vendor folder
94+
let status = Command::new("git")
95+
.current_dir(&mbedtls_dir)
96+
.args(&["apply", patch_path.to_str().unwrap()])
97+
.status()?;
98+
99+
if !status.success() {
100+
println!("cargo:warning=Could not apply patch to mbedtls: {:?}", patch_path);
101+
}
102+
}
103+
90104
// Configure the MbedTLS build for making Mbed Crypto
91105
if !::std::process::Command::new(mbedtls_config)
92106
.arg("--write")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
From 035aca2948c136e76ec7acfa739e4f0264d55c39 Mon Sep 17 00:00:00 2001
2+
From: Geovane Fedrecheski <[email protected]>
3+
Date: Wed, 29 Nov 2023 11:09:44 +0100
4+
Subject: [PATCH] Update config for baremetal targets
5+
6+
Signed-off-by: Geovane Fedrecheski <[email protected]>
7+
---
8+
scripts/config.py | 15 +++++++++++++++
9+
1 file changed, 15 insertions(+)
10+
11+
diff --git a/scripts/config.py b/scripts/config.py
12+
index 6d5edc7c0..36312df04 100755
13+
--- a/scripts/config.py
14+
+++ b/scripts/config.py
15+
@@ -241,6 +241,7 @@ def full_adapter(name, active, section):
16+
# need to be repeated here.
17+
EXCLUDE_FROM_BAREMETAL = frozenset([
18+
#pylint: disable=line-too-long
19+
+ 'MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS',
20+
'MBEDTLS_ENTROPY_NV_SEED', # requires a filesystem and FS_IO or alternate NV seed hooks
21+
'MBEDTLS_FS_IO', # requires a filesystem
22+
'MBEDTLS_HAVE_TIME', # requires a clock
23+
@@ -270,6 +271,20 @@ def baremetal_adapter(name, active, section):
24+
if name == 'MBEDTLS_NO_PLATFORM_ENTROPY':
25+
# No OS-provided entropy source
26+
return True
27+
+ if name == 'MBEDTLS_ENTROPY_HARDWARE_ALT':
28+
+ # Custom entropy source provided
29+
+ return True
30+
+ if name == 'MBEDTLS_ENTROPY_FORCE_SHA256':
31+
+ # Force SHA-256 accumulator
32+
+ return True
33+
+ if name == 'MBEDTLS_MEMORY_BUFFER_ALLOC_C':
34+
+ return True
35+
+ if name == 'MBEDTLS_PLATFORM_C':
36+
+ return True
37+
+ if name == 'MBEDTLS_PLATFORM_MEMORY':
38+
+ return True
39+
+ if name == 'MBEDTLS_PLATFORM_NO_STD_FUNCTIONS':
40+
+ return True
41+
return include_in_full(name) and keep_in_baremetal(name)
42+
43+
def include_in_crypto(name):
44+
--
45+
2.34.1
46+

0 commit comments

Comments
 (0)