Skip to content

Commit b85dedd

Browse files
Add mbedtls files and dependency.
I had to include some auto-generated files because Zig does not allow me to download the .tar.bz2 file which contains these (see ziglang/zig#26050)
1 parent 703f1dd commit b85dedd

19 files changed

Lines changed: 12320 additions & 0 deletions

build.zig

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,118 @@ const freetypeSources = [_][]const u8{
8484
"src/winfonts/winfnt.c",
8585
};
8686

87+
const tfPsaCryptoCoreSources: []const []const u8 = &.{
88+
"psa_crypto.c",
89+
"psa_crypto_client.c",
90+
"psa_crypto_slot_management.c",
91+
"psa_crypto_storage.c",
92+
"psa_its_file.c",
93+
"tf_psa_crypto_config.c",
94+
"tf_psa_crypto_version.c",
95+
};
96+
97+
const tfPsaCryptoDriverSources: []const []const u8 = &.{
98+
"aes.c",
99+
"aesce.c",
100+
"aesni.c",
101+
"aria.c",
102+
"asn1parse.c",
103+
"asn1write.c",
104+
"base64.c",
105+
"bignum.c",
106+
"bignum_core.c",
107+
"bignum_mod.c",
108+
"bignum_mod_raw.c",
109+
"block_cipher.c",
110+
"camellia.c",
111+
"ccm.c",
112+
"chacha20.c",
113+
"chachapoly.c",
114+
"cipher.c",
115+
"cipher_wrap.c",
116+
"cmac.c",
117+
"constant_time.c",
118+
"ctr_drbg.c",
119+
"ecdh.c",
120+
"ecdsa.c",
121+
"ecjpake.c",
122+
"ecp.c",
123+
"ecp_curves.c",
124+
"ecp_curves_new.c",
125+
"entropy.c",
126+
"entropy_poll.c",
127+
"gcm.c",
128+
"hmac_drbg.c",
129+
"lmots.c",
130+
"lms.c",
131+
"md.c",
132+
"md5.c",
133+
"memory_buffer_alloc.c",
134+
"nist_kw.c",
135+
"oid.c",
136+
"pem.c",
137+
"pk.c",
138+
"pk_ecc.c",
139+
"pk_rsa.c",
140+
"pk_wrap.c",
141+
"pkcs5.c",
142+
"pkparse.c",
143+
"pkwrite.c",
144+
"platform.c",
145+
"platform_util.c",
146+
"poly1305.c",
147+
"psa_crypto_aead.c",
148+
"psa_crypto_cipher.c",
149+
"psa_crypto_ecp.c",
150+
"psa_crypto_ffdh.c",
151+
"psa_crypto_hash.c",
152+
"psa_crypto_mac.c",
153+
"psa_crypto_pake.c",
154+
"psa_crypto_rsa.c",
155+
"psa_util.c",
156+
"ripemd160.c",
157+
"rsa.c",
158+
"rsa_alt_helpers.c",
159+
"sha1.c",
160+
"sha256.c",
161+
"sha3.c",
162+
"sha512.c",
163+
"threading.c",
164+
};
165+
166+
const mbedTlsSources: []const []const u8 = &.{
167+
"mbedtls_config.c",
168+
"pkcs7.c",
169+
"x509.c",
170+
"x509_create.c",
171+
"x509_crl.c",
172+
"x509_crt.c",
173+
"x509_csr.c",
174+
"x509_oid.c",
175+
"x509write.c",
176+
"x509write_crt.c",
177+
"x509write_csr.c",
178+
"debug.c",
179+
"mps_reader.c",
180+
"mps_trace.c",
181+
"net_sockets.c",
182+
"ssl_cache.c",
183+
"ssl_ciphersuites.c",
184+
"ssl_client.c",
185+
"ssl_cookie.c",
186+
"ssl_msg.c",
187+
"ssl_ticket.c",
188+
"ssl_tls.c",
189+
"ssl_tls12_client.c",
190+
"ssl_tls12_server.c",
191+
"ssl_tls13_keys.c",
192+
"ssl_tls13_server.c",
193+
"ssl_tls13_client.c",
194+
"ssl_tls13_generic.c",
195+
"timing.c",
196+
"version.c",
197+
};
198+
87199
pub fn addVulkanApple(b: *std.Build, step: *std.Build.Step, c_lib: *std.Build.Step.Compile, name: []const u8, target: std.Build.ResolvedTarget, flags: []const []const u8, replace_tool: *std.Build.Step.Compile) !void {
88200
std.debug.assert(target.result.os.tag.isDarwin());
89201

@@ -545,6 +657,45 @@ pub fn addMiniaudioAndStbVorbis(b: *std.Build, c_lib: *std.Build.Step.Compile, f
545657
c_lib.addCSourceFile(.{.file = b.path("lib/miniaudio_stbvorbis.c"), .flags = flags});
546658
}
547659

660+
pub fn addMbedTls(b: *std.Build, c_lib: *std.Build.Step.Compile, flags: []const []const u8) void {
661+
const mbedtls = b.dependency("mbedtls", .{});
662+
const tfPsaCrypto = b.dependency("tf_psa_crypto", .{});
663+
c_lib.addCSourceFiles(.{
664+
.root = mbedtls.path("library"),
665+
.files = mbedTlsSources,
666+
.flags = flags,
667+
});
668+
c_lib.addCSourceFiles(.{
669+
.root = tfPsaCrypto.path("core"),
670+
.files = tfPsaCryptoCoreSources,
671+
.flags = flags,
672+
});
673+
c_lib.addCSourceFiles(.{
674+
.root = tfPsaCrypto.path("drivers/builtin/src"),
675+
.files = tfPsaCryptoDriverSources,
676+
.flags = flags,
677+
});
678+
c_lib.addCSourceFile(.{
679+
.file = b.path("lib/tf_psa_crypto/psa_crypto_driver_wrappers_no_static.c"), // Generated file
680+
.flags = flags,
681+
});
682+
c_lib.addCSourceFiles(.{
683+
.root = b.path("lib/mbedtls"),
684+
.files = &.{"error.c", "ssl_debug_helpers_generated.c", "version_features.c"}, // Generated files
685+
.flags = flags,
686+
});
687+
c_lib.addIncludePath(b.path("lib/tf_psa_crypto")); // Contains generated files
688+
c_lib.addIncludePath(b.path("lib/mbedtls")); // Contains generated files
689+
c_lib.addIncludePath(tfPsaCrypto.path("core"));
690+
c_lib.addIncludePath(tfPsaCrypto.path("drivers/builtin/src"));
691+
c_lib.addIncludePath(tfPsaCrypto.path("include"));
692+
c_lib.addIncludePath(tfPsaCrypto.path("drivers/builtin/include"));
693+
c_lib.addIncludePath(mbedtls.path("include"));
694+
c_lib.installHeadersDirectory(mbedtls.path("include"), "", .{});
695+
c_lib.installHeadersDirectory(tfPsaCrypto.path("include"), "", .{});
696+
c_lib.installHeadersDirectory(tfPsaCrypto.path("drivers/builtin/include"), "", .{});
697+
}
698+
548699
pub inline fn addHeaderOnlyLibs(b: *std.Build, c_lib: *std.Build.Step.Compile, flags: []const []const u8) void {
549700
const cgltf = b.dependency("cgltf", .{});
550701

@@ -589,6 +740,7 @@ pub inline fn makeCubyzLibs(b: *std.Build, step: *std.Build.Step, name: []const
589740
try addVulkanApple(b, step, c_lib, name, target, flags, replace_tool);
590741
}
591742
try addGLFWSources(b, c_lib, target, flags);
743+
addMbedTls(b, c_lib, flags);
592744
c_lib.addCSourceFile(.{.file = b.path("lib/gl.c"), .flags = flags});
593745

594746
// NOTE(blackedout): See the above glad comment

build.zig.zon

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,13 @@
5252
.url = "https://github.com/mackron/miniaudio/archive/refs/tags/0.11.23.tar.gz",
5353
.hash = "N-V-__8AAPu4vwC_P5AHlL_kEON58uGgrDcJSjGq9Hc5yadh",
5454
},
55+
.mbedtls = .{
56+
.url = "https://github.com/Mbed-TLS/mbedtls/archive/refs/tags/mbedtls-4.0.0.tar.gz",
57+
.hash = "N-V-__8AAKIwiADtY89ifMh8C6X0PHvyBULclU19OYMsiDg_",
58+
},
59+
.tf_psa_crypto = .{
60+
.url = "https://github.com/Mbed-TLS/TF-PSA-Crypto/archive/refs/tags/tf-psa-crypto-1.0.0.tar.gz",
61+
.hash = "N-V-__8AAOuqOgGAlxfU32chMWurMwBPZACZk-9MdknRMDIM",
62+
},
5563
},
5664
}

lib/mbedtls/debug_internal.h

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/**
2+
* \file debug_internal.h
3+
*
4+
* \brief Internal part of the public "debug.h".
5+
*/
6+
/*
7+
* Copyright The Mbed TLS Contributors
8+
* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
9+
*/
10+
#ifndef MBEDTLS_DEBUG_INTERNAL_H
11+
#define MBEDTLS_DEBUG_INTERNAL_H
12+
13+
#include "mbedtls/debug.h"
14+
15+
/**
16+
* \brief Print a message to the debug output. This function is always used
17+
* through the MBEDTLS_SSL_DEBUG_MSG() macro, which supplies the ssl
18+
* context, file and line number parameters.
19+
*
20+
* \param ssl SSL context
21+
* \param level error level of the debug message
22+
* \param file file the message has occurred in
23+
* \param line line number the message has occurred at
24+
* \param format format specifier, in printf format
25+
* \param ... variables used by the format specifier
26+
*
27+
* \attention This function is intended for INTERNAL usage within the
28+
* library only.
29+
*/
30+
void mbedtls_debug_print_msg(const mbedtls_ssl_context *ssl, int level,
31+
const char *file, int line,
32+
const char *format, ...) MBEDTLS_PRINTF_ATTRIBUTE(5, 6);
33+
34+
/**
35+
* \brief Print the return value of a function to the debug output. This
36+
* function is always used through the MBEDTLS_SSL_DEBUG_RET() macro,
37+
* which supplies the ssl context, file and line number parameters.
38+
*
39+
* \param ssl SSL context
40+
* \param level error level of the debug message
41+
* \param file file the error has occurred in
42+
* \param line line number the error has occurred in
43+
* \param text the name of the function that returned the error
44+
* \param ret the return code value
45+
*
46+
* \attention This function is intended for INTERNAL usage within the
47+
* library only.
48+
*/
49+
void mbedtls_debug_print_ret(const mbedtls_ssl_context *ssl, int level,
50+
const char *file, int line,
51+
const char *text, int ret);
52+
53+
/**
54+
* \brief Output a buffer of size len bytes to the debug output. This function
55+
* is always used through the MBEDTLS_SSL_DEBUG_BUF() macro,
56+
* which supplies the ssl context, file and line number parameters.
57+
*
58+
* \param ssl SSL context
59+
* \param level error level of the debug message
60+
* \param file file the error has occurred in
61+
* \param line line number the error has occurred in
62+
* \param text a name or label for the buffer being dumped. Normally the
63+
* variable or buffer name
64+
* \param buf the buffer to be outputted
65+
* \param len length of the buffer
66+
*
67+
* \attention This function is intended for INTERNAL usage within the
68+
* library only.
69+
*/
70+
void mbedtls_debug_print_buf(const mbedtls_ssl_context *ssl, int level,
71+
const char *file, int line, const char *text,
72+
const unsigned char *buf, size_t len);
73+
74+
#if defined(MBEDTLS_BIGNUM_C)
75+
/**
76+
* \brief Print a MPI variable to the debug output.
77+
*
78+
* \param ssl SSL context
79+
* \param level error level of the debug message
80+
* \param file file the error has occurred in
81+
* \param line line number the error has occurred in
82+
* \param text a name or label for the MPI being output. Normally the
83+
* variable name
84+
* \param X the MPI variable
85+
*
86+
* \attention This function is intended for INTERNAL usage within the
87+
* library only.
88+
*/
89+
void mbedtls_debug_print_mpi(const mbedtls_ssl_context *ssl, int level,
90+
const char *file, int line,
91+
const char *text, const mbedtls_mpi *X);
92+
#endif
93+
94+
#if defined(MBEDTLS_X509_CRT_PARSE_C) && !defined(MBEDTLS_X509_REMOVE_INFO)
95+
/**
96+
* \brief Print a X.509 certificate structure to the debug output. This
97+
* function is always used through the MBEDTLS_SSL_DEBUG_CRT() macro,
98+
* which supplies the ssl context, file and line number parameters.
99+
*
100+
* \param ssl SSL context
101+
* \param level error level of the debug message
102+
* \param file file the error has occurred in
103+
* \param line line number the error has occurred in
104+
* \param text a name or label for the certificate being output
105+
* \param crt X.509 certificate structure
106+
*
107+
* \attention This function is intended for INTERNAL usage within the
108+
* library only.
109+
*/
110+
void mbedtls_debug_print_crt(const mbedtls_ssl_context *ssl, int level,
111+
const char *file, int line,
112+
const char *text, const mbedtls_x509_crt *crt);
113+
#endif
114+
115+
#endif /* MBEDTLS_DEBUG_INTERNAL_H */

0 commit comments

Comments
 (0)