-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbuild.rs
364 lines (323 loc) · 11.7 KB
/
build.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/*!
* Contains the build process for WolfSSL
*/
extern crate bindgen;
use autotools::Config;
use std::collections::HashSet;
use std::env;
use std::fs::File;
use std::path::{Path, PathBuf};
use std::process::Command;
/**
* Work around for bindgen creating duplicate values.
*/
#[derive(Debug)]
struct IgnoreMacros(HashSet<String>);
impl bindgen::callbacks::ParseCallbacks for IgnoreMacros {
fn will_parse_macro(&self, name: &str) -> bindgen::callbacks::MacroParsingBehavior {
if self.0.contains(name) {
bindgen::callbacks::MacroParsingBehavior::Ignore
} else {
bindgen::callbacks::MacroParsingBehavior::Default
}
}
}
/**
* Copy WolfSSL
*/
fn copy_wolfssl(dest: &Path) -> std::io::Result<PathBuf> {
println!("cargo:rerun-if-changed=wolfssl-src");
Command::new("cp")
.arg("-rf")
.arg("wolfssl-src")
.arg(dest)
.status()
.unwrap();
Ok(dest.join("wolfssl-src"))
}
const PATCH_DIR: &str = "patches";
const PATCHES: &[&str] = &[
"fix-poly1305-aarch64-asm.patch",
"include-private-key-fields-for-kyber.patch",
"make-kyber-mlkem-available.patch",
"fix-kyber-mlkem-benchmark.patch",
"fix-mlkem-get-curve-name.patch",
"fix-kyber-get-curve-name.patch",
"fix-kyber-prf-non-avx2.patch",
"CVPN-1945-Lower-max-mtu-for-DTLS-1.3-handshake-message.patch",
];
/**
* Apply patch to wolfssl-src
*/
fn apply_patch(wolfssl_path: &Path, patch: impl AsRef<Path>) {
let full_patch = Path::new(PATCH_DIR).join(patch.as_ref());
println!("cargo:rerun-if-changed={}", full_patch.display());
let patch_buffer = File::open(full_patch).unwrap();
let status = Command::new("patch")
.arg("-d")
.arg(wolfssl_path)
.arg("-p1")
.stdin(patch_buffer)
.status()
.unwrap();
assert!(
status.success(),
"Failed to apply {}",
patch.as_ref().display()
);
}
/**
Builds WolfSSL
*/
fn build_wolfssl(wolfssl_src: &Path) -> PathBuf {
// Create the config
let mut conf = Config::new(wolfssl_src);
// Configure it
conf.reconf("-ivf")
// Disable benchmarks
.disable("benchmark", None)
// Disable DH key exchanges
.disable("dh", None)
// Disable examples
.disable("examples", None)
// Disable old TLS versions
.disable("oldtls", None)
// Disable SHA3
.disable("sha3", None)
// Disable dynamic library
.disable_shared()
// Disable sys ca certificate store
.disable("sys-ca-certs", None)
// Disable dilithium
.disable("dilithium", None)
// Enable AES bitsliced implementation (cache attack safe)
.enable("aes-bitsliced", None)
// Enable Curve25519
.enable("curve25519", None)
// Enable D/TLS
.enable("dtls", None)
// Enable DTLS/1.3
.enable("dtls13", None)
// Enable DTLS1.3 ClientHello fragmentation
.enable("dtls-frag-ch", None)
// Enable setting the D/TLS MTU size
.enable("dtls-mtu", None)
// Enable Secure Renegotiation
.enable("secure-renegotiation", None)
// Enable single threaded mode
.enable("singlethreaded", None)
// Enable SNI
.enable("sni", None)
// Enable single precision
.enable("sp", None)
// Enable single precision ASM
.enable("sp-asm", None)
// Only build the static library
.enable_static()
// Enable elliptic curve exchanges
.enable("supportedcurves", None)
// Enable TLS/1.3
.enable("tls13", None)
// Enable kyber, etc
.enable("experimental", None)
// CFLAGS
.cflag("-g")
.cflag("-fPIC")
.cflag("-DWOLFSSL_DTLS_ALLOW_FUTURE")
.cflag("-DWOLFSSL_MIN_RSA_BITS=2048")
.cflag("-DWOLFSSL_MIN_ECC_BITS=256")
.cflag("-DUSE_CERT_BUFFERS_4096")
.cflag("-DUSE_CERT_BUFFERS_256")
.cflag("-DWOLFSSL_NO_SPHINCS")
.cflag("-DWOLFSSL_TLS13_MIDDLEBOX_COMPAT");
if cfg!(feature = "debug") {
conf.enable("debug", None);
conf.cflag("-DHAVE_SECRET_CALLBACK");
}
if cfg!(feature = "postquantum") {
let flags = if cfg!(feature = "kyber_only") {
"all,original"
} else {
"all,original,ml-kem"
};
// Enable Kyber
conf.enable("kyber", Some(flags))
// SHA3 is needed for using WolfSSL's implementation of Kyber/ML-KEM
.enable("sha3", None);
}
match build_target::target_arch().unwrap() {
build_target::Arch::AARCH64 => {
// Enable ARM ASM optimisations
conf.enable("armasm", None);
}
build_target::Arch::ARM => {
// Enable ARM ASM optimisations, except for android armeabi-v7a
if build_target::target_os().unwrap() != build_target::Os::Android {
conf.enable("armasm", None);
}
}
build_target::Arch::X86 => {
// Disable sp asm optmisations which has been enabled earlier
conf.disable("sp-asm", None);
}
build_target::Arch::X86_64 => {
// We don't need these build flag for iOS simulator
if build_target::target_os().unwrap() != build_target::Os::iOs {
// Enable Intel ASM optmisations
conf.enable("intelasm", None);
// Enable AES hardware acceleration
conf.enable("aesni", None);
}
}
_ => {}
}
if build_target::target_os().unwrap() == build_target::Os::Android {
// Build options for Android
let (chost, arch_flags, arch, configure_platform) =
match build_target::target_arch().unwrap() {
build_target::Arch::ARM => (
"armv7a-linux-androideabi",
"-march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16 -O3",
"armeabi-v7a",
"android-arm",
),
build_target::Arch::AARCH64 => (
"aarch64-linux-android",
"-march=armv8-a+crypto -O3",
"arm64-v8a",
"android-arm64",
),
build_target::Arch::X86 => (
"i686-linux-android",
"-march=i686 -msse3 -m32 -O3",
"x86",
"android-x86",
),
build_target::Arch::X86_64 => (
"x86_64-linux-android",
"-march=x86-64 -msse4.2 -mpopcnt -m64 -O3",
"x86_64",
"android64-x86_64",
),
_ => panic!("Unsupported build_target for Android"),
};
// Per arch configurations
conf.config_option("host", Some(chost));
conf.cflag(arch_flags);
conf.env("ARCH", arch);
conf.env("CONFIGURE_PLATFORM", configure_platform);
// General Android specific configurations
conf.disable("crypttests", None);
conf.cflag("-DFP_MAX_BITS=8192");
conf.cflag("-fomit-frame-pointer");
conf.env("LIBS", "-llog -landroid");
}
if build_target::target_os().unwrap() == build_target::Os::iOs {
// Check whether we have set IPHONEOS_DEPLOYMENT_TARGET to ensure we support older iOS
let ios_target = env::var("IPHONEOS_DEPLOYMENT_TARGET")
.expect("Must have set minimum supported iOS version");
if ios_target.is_empty() {
panic!("IPHONEOS_DEPLOYMENT_TARGET is empty")
}
// Build options for iOS
let (chost, arch_flags, arch) = match build_target::target_arch().unwrap() {
build_target::Arch::AARCH64 => ("arm64-apple-ios", "-O3", "arm64"),
build_target::Arch::X86_64 => ("x86_64-apple-darwin", "-O3", "x86_64"),
_ => panic!("Unsupported build_target for iOS"),
};
// Per arch configurations
conf.config_option("host", Some(chost));
conf.cflag(arch_flags);
conf.cxxflag(arch_flags);
conf.env("ARCH", arch);
// General iOS specific configurations
conf.disable("crypttests", None);
conf.cflag("-D_FORTIFY_SOURCE=2");
}
if build_target::target_os().unwrap() == build_target::Os::from_str("tvos") {
// Check whether we have set TVOS_DEPLOYMENT_TARGET to ensure we support older tvOS
let ios_target = env::var("TVOS_DEPLOYMENT_TARGET")
.expect("Must have set minimum supported tvOS version");
if ios_target.is_empty() {
panic!("TVOS_DEPLOYMENT_TARGET is empty")
}
// Build options for tvos
let (chost, arch_flags, arch) = match build_target::target_arch().unwrap() {
build_target::Arch::AARCH64 => ("arm64-apple-ios", "-O3", "arm64"),
build_target::Arch::X86_64 => ("x86_64-apple-darwin", "-O3", "x86_64"), // for tvOS simulator
_ => panic!("Unsupported build_target for tvos"),
};
// Per arch configurations
conf.config_option("host", Some(chost));
conf.cflag(arch_flags);
conf.cxxflag(arch_flags);
conf.env("ARCH", arch);
// General tvOS specific configurations
conf.disable("crypttests", None);
conf.cflag("-D_FORTIFY_SOURCE=2");
}
// Build and return the config
conf.build()
}
fn main() -> std::io::Result<()> {
// Get the build directory
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
// Extract WolfSSL
let wolfssl_src = copy_wolfssl(&out_dir)?;
// Apply patches
PATCHES.iter().for_each(|&f| apply_patch(&wolfssl_src, f));
println!("cargo:rerun-if-changed={}", PATCH_DIR);
// Configure and build WolfSSL
let wolfssl_install_dir = build_wolfssl(&wolfssl_src);
// We want to block some macros as they are incorrectly creating duplicate values
// https://github.com/rust-lang/rust-bindgen/issues/687
// TODO: Reach out to tlspuffin and ask if we can incorporate this code and credit them
let mut hash_ignored_macros = HashSet::new();
for i in &[
"IPPORT_RESERVED",
"EVP_PKEY_DH",
"BIO_CLOSE",
"BIO_NOCLOSE",
"CRYPTO_LOCK",
"ASN1_STRFLGS_ESC_MSB",
"SSL_MODE_RELEASE_BUFFERS",
// Wolfssl 4.3.0
"GEN_IPADD",
"EVP_PKEY_RSA",
] {
hash_ignored_macros.insert(i.to_string());
}
let ignored_macros = IgnoreMacros(hash_ignored_macros);
let wolfssl_include_dir = wolfssl_install_dir.join("include");
// Build the Rust binding
let builder = bindgen::Builder::default()
.header("wrapper.h")
.clang_arg(format!("-I{}/", wolfssl_include_dir.to_str().unwrap()))
.parse_callbacks(Box::new(ignored_macros))
.formatter(bindgen::Formatter::Rustfmt);
let builder = [
"wolfssl/.*.h",
"wolfssl/wolfcrypt/.*.h",
"wolfssl/openssl/compat_types.h",
]
.iter()
.fold(builder, |b, p| {
b.allowlist_file(wolfssl_include_dir.join(p).to_str().unwrap())
});
let builder = builder.blocklist_function("wolfSSL_BIO_vprintf");
let bindings: bindgen::Bindings = builder.generate().expect("Unable to generate bindings");
// Write out the bindings
bindings
.write_to_file(out_dir.join("bindings.rs"))
.expect("Couldn't write bindings!");
// Tell cargo to tell rustc to link in WolfSSL
println!("cargo:rustc-link-lib=static=wolfssl");
println!(
"cargo:rustc-link-search=native={}",
wolfssl_install_dir.join("lib").to_str().unwrap()
);
// Invalidate the built crate whenever the wrapper changes
println!("cargo:rerun-if-changed=wrapper.h");
// That should do it...
Ok(())
}