blob: 69f2dcb26e1cd62a6cdb42dc571ab19036a814c0 [file] [log] [blame] [view]
Ron Eldorc7acb912017-10-30 17:03:57 +02001README for Mbed TLS
Manuel Pégourié-Gonnardf851f142015-09-03 13:29:45 +02002===================
3
Ronald Crond196cbd2025-09-30 09:58:08 +02004Mbed TLS is a C library that implements X.509 certificate manipulation and the TLS and DTLS protocols. Its small code footprint makes it suitable for embedded systems.
5Mbed TLS includes the [TF-PSA-Crypto repository](https://github.com/Mbed-TLS/TF-PSA-Crypto) that provides an implementation of the [PSA Cryptography API](https://arm-software.github.io/psa-api).
Gilles Peskineda5abbf2020-03-09 18:51:37 +01006
Manuel Pégourié-Gonnardf851f142015-09-03 13:29:45 +02007Configuration
8-------------
Ronald Cron0f2ef4a2025-09-30 18:30:32 +02009Configuration options related to X.509 and TLS are available in `include/mbedtls/mbedtls_config.h`, while cryptography and platform options are located in the TF-PSA-Crypto configuration file `tf-psa-crypto/include/psa/crypto_config.h`.
Manuel Pégourié-Gonnardf851f142015-09-03 13:29:45 +020010
Ronald Cron0f2ef4a2025-09-30 18:30:32 +020011With the default platform options, Mbed TLS should build out of the box on most systems.
Manuel Pégourié-Gonnardf851f142015-09-03 13:29:45 +020012
Ronald Cronb9063012025-10-05 16:47:45 +020013These configuration files can be edited manually, or programmatically using the Python script `scripts/config.py` (run with --help for usage instructions).
Manuel Pégourié-Gonnardf851f142015-09-03 13:29:45 +020014
Ronald Cron0f2ef4a2025-09-30 18:30:32 +020015We provide some non-standard configurations focused on specific use cases in the `configs/` directory. You can read more about those in `configs/README.txt`.
Gilles Peskineb05d89d2020-03-09 19:23:51 +010016
Gilles Peskinea10cbda2020-03-09 19:21:51 +010017Documentation
18-------------
19
Dave Rodgman7c195162022-10-12 16:27:14 +010020The main Mbed TLS documentation is available via [ReadTheDocs](https://mbed-tls.readthedocs.io/).
21
Gilles Peskinea10cbda2020-03-09 19:21:51 +010022To generate a local copy of the library documentation in HTML format, tailored to your compile-time configuration:
23
Dave Rodgman2f458d32021-06-03 17:58:13 +0100241. Make sure that [Doxygen](http://www.doxygen.nl/) is installed.
Ronald Cron200b89b2025-10-01 10:05:34 +0200251. Run `cmake -B /path/to/build_dir /path/to/mbedtls/source`
261. Run `cmake --build /path/to/build_dir --target mbedtls-apidoc`
Ronald Cron82671962025-10-05 16:58:41 +0200271. Open one of the main generated HTML files:
28 * `apidoc/index.html`
29 * `apidoc/modules.html` or `apidoc/topics.html`
Gilles Peskinea10cbda2020-03-09 19:21:51 +010030
Manuel Pégourié-Gonnard80c02af2021-02-25 12:34:58 +010031For other sources of documentation, see the [SUPPORT](SUPPORT.md) document.
32
Manuel Pégourié-Gonnardf851f142015-09-03 13:29:45 +020033Compiling
34---------
35
Ronald Cron7cf78b42025-10-01 10:28:17 +020036We use CMake to configure and drive our build process. Three libraries are built: `libtfpsacrypto`, `libmbedx509`, and `libmbedtls`. Note that `libmbedtls` depends on `libmbedx509` and `libtfpsacrypto`, and `libmbedx509` depends on `libtfpsacrypto`. As a result, some linkers will expect flags to be in a specific order, for example the GNU linker wants `-lmbedtls -lmbedx509 -ltfpsacrypto`. The cryptographic library `libtfpsacrypto` is also provided under its legacy name, `libmbedcrypto`.
Manuel Pégourié-Gonnardf851f142015-09-03 13:29:45 +020037
Gilles Peskine67698702020-03-09 19:30:08 +010038### Tool versions
39
Ronald Cron4ccdaf12025-10-01 12:40:27 +020040You need the following tools to build the library from the main branch with the provided CMake files. Mbed TLS minimum tool version requirements are set based on the versions shipped in the latest or penultimate (depending on the release cadence) long-term support releases of major Linux distributions, namely at time of writing: Ubuntu 22.04, RHEL 9, and SLES 15 SP4.
Gilles Peskine67698702020-03-09 19:30:08 +010041
Ronald Cron63180eb2025-10-05 17:41:01 +020042* CMake 3.20.2 or later.
Ronald Cronc9d79ff2025-10-02 19:14:14 +020043* A build system like Make or Ninja for which CMake can generate build files.
Ronald Cron864c31a2025-10-05 17:28:11 +020044* A C99 toolchain (compiler, linker, archiver). We actively test with GCC 5.4, Clang 3.8, Arm Compiler 6, and Visual Studio 2017 Compiler. More recent versions should work. Slightly older versions may work.
Ronald Cronb9063012025-10-05 16:47:45 +020045* Python 3.8 or later to generate the test code. Python is also needed to build the development branch (see next section).
Gilles Peskined05a5882021-05-17 23:57:42 +020046* Perl to run the tests, and to generate some source files in the development branch.
Ronald Cron4ccdaf12025-10-01 12:40:27 +020047* Doxygen 1.8.14 or later (if building the documentation; slightly older versions should work).
Gilles Peskined05a5882021-05-17 23:57:42 +020048
Gilles Peskine0c3f0e92024-03-04 15:54:54 +010049### Git usage
50
Ronald Cronc9998d32025-10-03 10:03:20 +020051The supported branches (see [`BRANCHES.md`](BRANCHES.md)) use [Git submodules](https://git-scm.com/book/en/v2/Git-Tools-Submodules#_cloning_submodules). They contain two submodules: the [framework](https://github.com/Mbed-TLS/mbedtls-framework) submodule and the [tf-psa-crypto](https://github.com/Mbed-TLS/TF-PSA-Crypto) submodule, except for the 3.6 LTS branch, which contains only the framework submodule. Release tags also use Git submodules.
52
53After cloning or checking out a branch or tag, run:
54 ```
55 git submodule update --init --recursive
56 ```
57 to initialize and update the submodules before building.
58
59However, the official source release tarballs (e.g. [mbedtls-4.0.0-beta.tar.bz2](https://github.com/Mbed-TLS/mbedtls/releases/download/mbedtls-4.0.0-beta/mbedtls-4.0.0-beta.tar.bz2)) include the contents of the submodules.
Gilles Peskine0c3f0e92024-03-04 15:54:54 +010060
Gilles Peskined05a5882021-05-17 23:57:42 +020061### Generated source files in the development branch
62
63The source code of Mbed TLS includes some files that are automatically generated by scripts and whose content depends only on the Mbed TLS source, not on the platform or on the library configuration. These files are not included in the development branch of Mbed TLS, but the generated files are included in official releases. This section explains how to generate the missing files in the development branch.
64
65The following tools are required:
66
Ronald Croneef87b32025-09-30 13:06:32 +020067* Perl, for some library source files.
Ronald Cron4ccdaf12025-10-01 12:40:27 +020068* Python 3 and some Python packages, for some library source files, sample programs and test data. To install the necessary packages, run:
Gilles Peskine87485a32021-11-17 19:17:03 +010069 ```
Gilles Peskine429e9012023-03-07 20:40:04 +010070 python3 -m pip install --user -r scripts/basic.requirements.txt
Gilles Peskine87485a32021-11-17 19:17:03 +010071 ```
Ronald Cronb9063012025-10-05 16:47:45 +020072 Depending on your Python installation, you may need to invoke `python` instead of `python3`. To install the packages system-wide or in a virtual environment, omit the `--user` option.
Gilles Peskined05a5882021-05-17 23:57:42 +020073* A C compiler for the host platform, for some test data.
74
David Horstmann24e33882025-08-19 16:56:25 +010075The scripts that generate the configuration-independent files will look for a host C compiler in the following places (in order of preference):
76
771. The `HOSTCC` environment variable. This can be used if `CC` is pointing to a cross-compiler.
782. The `CC` environment variable.
793. An executable called `cc` in the current path.
80
81Note: If you have multiple toolchains installed, it is recommended to set `CC` or `HOSTCC` to the intended host compiler before generating the files.
Gilles Peskined05a5882021-05-17 23:57:42 +020082
83Any of the following methods are available to generate the configuration-independent files:
84
Ronald Cronee63b642025-08-29 16:14:19 +020085* On non-Windows systems, when not cross-compiling, CMake generates the required files automatically.
86* Run `framework/scripts/make_generated_files.py` to generate all the configuration-independent files.
Manuel Pégourié-Gonnardf851f142015-09-03 13:29:45 +020087
88### CMake
89
Ronald Cronee63b642025-08-29 16:14:19 +020090In order to build the libraries using CMake in a separate directory (recommended), just enter at the command line:
Manuel Pégourié-Gonnardf851f142015-09-03 13:29:45 +020091
Manuel Pégourié-Gonnardb89c4722017-12-26 12:52:53 +010092 mkdir /path/to/build_dir && cd /path/to/build_dir
93 cmake /path/to/mbedtls_source
Carlos Gomes Martinho227a9db2020-04-03 09:42:57 +020094 cmake --build .
Manuel Pégourié-Gonnardf851f142015-09-03 13:29:45 +020095
96In order to run the tests, enter:
97
Carlos Gomes Martinho227a9db2020-04-03 09:42:57 +020098 ctest
Manuel Pégourié-Gonnardf851f142015-09-03 13:29:45 +020099
Ronald Cron74a49842025-10-03 11:13:44 +0200100The test suites need Python to be built. If you don't have Python installed, you'll want to disable the test suites with:
Manuel Pégourié-Gonnardf851f142015-09-03 13:29:45 +0200101
Manuel Pégourié-Gonnardb89c4722017-12-26 12:52:53 +0100102 cmake -DENABLE_TESTING=Off /path/to/mbedtls_source
Manuel Pégourié-Gonnardf851f142015-09-03 13:29:45 +0200103
Manuel Pégourié-Gonnardf851f142015-09-03 13:29:45 +0200104To configure CMake for building shared libraries, use:
105
Manuel Pégourié-Gonnardb89c4722017-12-26 12:52:53 +0100106 cmake -DUSE_SHARED_MBEDTLS_LIBRARY=On /path/to/mbedtls_source
Manuel Pégourié-Gonnardf851f142015-09-03 13:29:45 +0200107
Ronald Cronee63b642025-08-29 16:14:19 +0200108There are many different build types available with CMake. Most of them are available for gcc and clang, though some are compiler-specific:
Manuel Pégourié-Gonnardf851f142015-09-03 13:29:45 +0200109
Gilles Peskineb21a0852018-03-09 14:24:36 +0100110- `Release`. This generates the default code without any unnecessary information in the binary files.
111- `Debug`. This generates debug information and disables optimization of the code.
112- `Coverage`. This generates code coverage information in addition to debug information.
113- `ASan`. This instruments the code with AddressSanitizer to check for memory errors. (This includes LeakSanitizer, with recent version of gcc and clang.) (With recent version of clang, this mode also instruments the code with UndefinedSanitizer to check for undefined behaviour.)
114- `ASanDbg`. Same as ASan but slower, with debug information and better stack traces.
Ronald Cron7cf78b42025-10-01 10:28:17 +0200115- `MemSan`. This instruments the code with MemorySanitizer to check for uninitialised memory reads.
Gilles Peskineb21a0852018-03-09 14:24:36 +0100116- `MemSanDbg`. Same as MemSan but slower, with debug information, better stack traces and origin tracking.
117- `Check`. This activates the compiler warnings that depend on optimization and treats all warnings as errors.
Ronald Cron7cf78b42025-10-01 10:28:17 +0200118- `TSan`. This instruments the code with ThreadSanitizer to detect data races and other threading-related concurrency issues at runtime.
119- `TSanDbg`. Same as TSan but slower, with debug information, better stack traces and origin tracking.
Manuel Pégourié-Gonnardf851f142015-09-03 13:29:45 +0200120
Ronald Cronee63b642025-08-29 16:14:19 +0200121Switching build types in CMake is simple. For debug mode, enter at the command line:
Manuel Pégourié-Gonnardf851f142015-09-03 13:29:45 +0200122
Manuel Pégourié-Gonnardb89c4722017-12-26 12:52:53 +0100123 cmake -D CMAKE_BUILD_TYPE=Debug /path/to/mbedtls_source
Manuel Pégourié-Gonnardf851f142015-09-03 13:29:45 +0200124
125To list other available CMake options, use:
126
127 cmake -LH
128
Manuel Pégourié-Gonnard976dd162018-01-02 10:49:46 +0100129Note that, with CMake, you can't adjust the compiler or its flags after the
Manuel Pégourié-Gonnardb89c4722017-12-26 12:52:53 +0100130initial invocation of cmake. This means that `CC=your_cc make` and `make
131CC=your_cc` will *not* work (similarly with `CFLAGS` and other variables).
132These variables need to be adjusted when invoking cmake for the first time,
133for example:
134
135 CC=your_cc cmake /path/to/mbedtls_source
136
137If you already invoked cmake and want to change those settings, you need to
Ronald Cron74a49842025-10-03 11:13:44 +0200138invoke the configuration phase of CMake again with the new settings.
Manuel Pégourié-Gonnardb89c4722017-12-26 12:52:53 +0100139
140Note that it is possible to build in-place; this will however overwrite the
Ronald Cronee63b642025-08-29 16:14:19 +0200141legacy Makefiles still used for testing purposes (see
142`scripts/tmp_ignore_makefiles.sh` if you want to prevent `git status` from
143showing them as modified). In order to do so, from the Mbed TLS source
144directory, use:
Manuel Pégourié-Gonnardb89c4722017-12-26 12:52:53 +0100145
146 cmake .
Ronald Cronc9d79ff2025-10-02 19:14:14 +0200147 cmake --build .
Manuel Pégourié-Gonnardb89c4722017-12-26 12:52:53 +0100148
149If you want to change `CC` or `CFLAGS` afterwards, you will need to remove the
150CMake cache. This can be done with the following command using GNU find:
Manuel Pégourié-Gonnardf851f142015-09-03 13:29:45 +0200151
152 find . -iname '*cmake*' -not -name CMakeLists.txt -exec rm -rf {} +
Manuel Pégourié-Gonnardb89c4722017-12-26 12:52:53 +0100153
Manuel Pégourié-Gonnard976dd162018-01-02 10:49:46 +0100154You can now make the desired change:
Manuel Pégourié-Gonnardb89c4722017-12-26 12:52:53 +0100155
156 CC=your_cc cmake .
Ronald Cronc9d79ff2025-10-02 19:14:14 +0200157 cmake --build .
Manuel Pégourié-Gonnardf851f142015-09-03 13:29:45 +0200158
Manuel Pégourié-Gonnard05c92712017-12-28 09:14:47 +0100159Regarding variables, also note that if you set CFLAGS when invoking cmake,
Ronald Cronc9d79ff2025-10-02 19:14:14 +0200160your value of CFLAGS doesn't override the content provided by CMake (depending
Manuel Pégourié-Gonnard05c92712017-12-28 09:14:47 +0100161on the build mode as seen above), it's merely prepended to it.
162
Chris Kayd259e342021-03-25 16:03:25 +0000163#### Consuming Mbed TLS
164
Ronald Cron74a49842025-10-03 11:13:44 +0200165Mbed TLS provides a CMake package configuration file for consumption as a
166dependency in other CMake projects. You can load its CMake targets with:
Chris Kayd259e342021-03-25 16:03:25 +0000167
Ronald Cron74a49842025-10-03 11:13:44 +0200168 find_package(MbedTLS REQUIRED)
Chris Kayd259e342021-03-25 16:03:25 +0000169
Ronald Cron74a49842025-10-03 11:13:44 +0200170You can help CMake find the package:
Chris Kayd259e342021-03-25 16:03:25 +0000171
Ronald Cron74a49842025-10-03 11:13:44 +0200172- By setting the variable `MbedTLS_DIR` to `${YOUR_MBEDTLS_BUILD_DIR}/cmake`,
173 as shown in `programs/test/cmake_package/CMakeLists.txt`, or
174- By adding the Mbed TLS installation prefix to `CMAKE_PREFIX_PATH`,
175 as shown in `programs/test/cmake_package_install/CMakeLists.txt`.
176
177After a successful `find_package(MbedTLS)`, the following imported targets are available:
178
179- `MbedTLS::tfpsacrypto`, the crypto library
180- `MbedTLS::mbedtls`, the TLS library
181- `MbedTLS::mbedx509`, the X.509 library
Chris Kayd259e342021-03-25 16:03:25 +0000182
183You can then use these directly through `target_link_libraries()`:
184
185 add_executable(xyz)
186
187 target_link_libraries(xyz
188 PUBLIC MbedTLS::mbedtls
Ronald Cron8126a682024-10-25 17:34:23 +0200189 MbedTLS::tfpsacrypto
Chris Kayd259e342021-03-25 16:03:25 +0000190 MbedTLS::mbedx509)
191
192This will link the Mbed TLS libraries to your library or application, and add
193its include directories to your target (transitively, in the case of `PUBLIC` or
194`INTERFACE` link libraries).
195
Jaeden Amero41421c42019-06-20 17:26:29 +0100196#### Mbed TLS as a subproject
197
Manuel Pégourié-Gonnarda4b99a22020-03-19 12:36:02 +0100198Mbed TLS supports being built as a CMake subproject. One can
Jaeden Amero41421c42019-06-20 17:26:29 +0100199use `add_subdirectory()` from a parent CMake project to include Mbed TLS as a
200subproject.
201
Manuel Pégourié-Gonnardf851f142015-09-03 13:29:45 +0200202Example programs
203----------------
204
Gilles Peskinecf63f592020-03-09 19:24:18 +0100205We've included example programs for a lot of different features and uses in [`programs/`](programs/README.md).
206Please note that the goal of these sample programs is to demonstrate specific features of the library, and the code may need to be adapted to build a real-world application.
Manuel Pégourié-Gonnardf851f142015-09-03 13:29:45 +0200207
208Tests
209-----
210
Ronald Cronc9d79ff2025-10-02 19:14:14 +0200211Mbed TLS includes an elaborate test suite in `tests/` that initially requires Python to generate the tests files (e.g. `test_suite_ssl.c`). These files are generated from a `function file` (e.g. `suites/test_suite_ssl.function`) and a `data file` (e.g. `suites/test_suite_ssl.data`). The `function file` contains the test functions. The `data file` contains the test cases, specified as parameters that will be passed to the test function.
Manuel Pégourié-Gonnardf851f142015-09-03 13:29:45 +0200212
213For machines with a Unix shell and OpenSSL (and optionally GnuTLS) installed, additional test scripts are available:
214
215- `tests/ssl-opt.sh` runs integration tests for various TLS options (renegotiation, resumption, etc.) and tests interoperability of these options with other implementations.
216- `tests/compat.sh` tests interoperability of every ciphersuite with other implementations.
Ronald Cronc9d79ff2025-10-02 19:14:14 +0200217- `tests/scripts/depends.py` tests builds in configurations with a single curve, key exchange, hash, cipher, or pkalg on.
Bence Szépkútibb0cfeb2021-05-28 09:42:25 +0200218- `tests/scripts/all.sh` runs a combination of the above tests, plus some more, with various build options (such as ASan, full `mbedtls_config.h`, etc).
Manuel Pégourié-Gonnardf851f142015-09-03 13:29:45 +0200219
Dave Rodgman0da8c512024-03-18 15:25:53 +0000220Instead of manually installing the required versions of all tools required for testing, it is possible to use the Docker images from our CI systems, as explained in [our testing infrastructure repository](https://github.com/Mbed-TLS/mbedtls-test/blob/main/README.md#quick-start).
Manuel Pégourié-Gonnard59626b62022-12-15 10:08:26 +0100221
Ron Eldorc7acb912017-10-30 17:03:57 +0200222Porting Mbed TLS
Andres AG1a6e9c32016-12-28 15:38:05 +0000223----------------
224
Simon Butcher6965f772018-07-23 23:57:07 +0100225Mbed TLS can be ported to many different architectures, OS's and platforms. Before starting a port, you may find the following Knowledge Base articles useful:
Andres AG1a6e9c32016-12-28 15:38:05 +0000226
Dave Rodgman7c195162022-10-12 16:27:14 +0100227- [Porting Mbed TLS to a new environment or OS](https://mbed-tls.readthedocs.io/en/latest/kb/how-to/how-do-i-port-mbed-tls-to-a-new-environment-OS/)
228- [What external dependencies does Mbed TLS rely on?](https://mbed-tls.readthedocs.io/en/latest/kb/development/what-external-dependencies-does-mbedtls-rely-on/)
229- [How do I configure Mbed TLS](https://mbed-tls.readthedocs.io/en/latest/kb/compiling-and-building/how-do-i-configure-mbedtls/)
Andres AG1a6e9c32016-12-28 15:38:05 +0000230
Minos Galanakisc42cadb2021-12-09 13:16:54 +0000231Mbed TLS is mostly written in portable C99; however, it has a few platform requirements that go beyond the standard, but are met by most modern architectures:
232
Minos Galanakisd7547fc2021-12-09 15:06:16 +0000233- Bytes must be 8 bits.
minosgalanakis0f2a46c2021-12-09 15:38:39 +0000234- All-bits-zero must be a valid representation of a null pointer.
Minos Galanakisd7547fc2021-12-09 15:06:16 +0000235- Signed integers must be represented using two's complement.
236- `int` and `size_t` must be at least 32 bits wide.
237- The types `uint8_t`, `uint16_t`, `uint32_t` and their signed equivalents must be available.
Dave Rodgman28f424f2022-12-01 09:49:44 +0000238- Mixed-endian platforms are not supported.
Dave Rodgman37296a42023-02-10 15:39:22 +0000239- SIZE_MAX must be at least as big as INT_MAX and UINT_MAX.
Minos Galanakisc42cadb2021-12-09 13:16:54 +0000240
Dan Handleyc76a5452020-02-18 17:58:20 +0000241License
242-------
243
Dave Rodgman8ce51702023-11-02 17:36:49 +0000244Unless specifically indicated otherwise in a file, Mbed TLS files are provided under a dual [Apache-2.0](https://spdx.org/licenses/Apache-2.0.html) OR [GPL-2.0-or-later](https://spdx.org/licenses/GPL-2.0-or-later.html) license. See the [LICENSE](LICENSE) file for the full text of these licenses, and [the 'License and Copyright' section in the contributing guidelines](CONTRIBUTING.md#License-and-Copyright) for more information.
Dan Handleyc76a5452020-02-18 17:58:20 +0000245
Manuel Pégourié-Gonnardf851f142015-09-03 13:29:45 +0200246Contributing
247------------
248
Dan Handleyc76a5452020-02-18 17:58:20 +0000249We gratefully accept bug reports and contributions from the community. Please see the [contributing guidelines](CONTRIBUTING.md) for details on how to do this.
Gilles Peskineb6b15b22021-09-30 14:10:06 +0200250
251Contact
252-------
253
254* To report a security vulnerability in Mbed TLS, please email <mbed-tls-security@lists.trustedfirmware.org>. For more information, see [`SECURITY.md`](SECURITY.md).
Dave Rodgman017a1992022-03-31 14:07:01 +0100255* To report a bug or request a feature in Mbed TLS, please [file an issue on GitHub](https://github.com/Mbed-TLS/mbedtls/issues/new/choose).
Gilles Peskineb6b15b22021-09-30 14:10:06 +0200256* Please see [`SUPPORT.md`](SUPPORT.md) for other channels for discussion and support about Mbed TLS.