Mbedtls vs Wolfssl
Detailed comparison of features, pros, cons, and usage
Mbed TLS and wolfSSL are both popular open-source SSL/TLS libraries, with Mbed TLS offering a more modular and customizable approach suitable for embedded systems, while wolfSSL focuses on high performance and a smaller footprint, making it ideal for resource-constrained environments, though it may have a steeper learning curve.
An open source, portable, easy to use, readable and flexible TLS library, and reference implementation of the PSA Cryptography API. Releases are on a varying cadence, typically around 3 - 6 months between releases.
The wolfSSL library is a small, fast, portable implementation of TLS/SSL for embedded devices to the cloud. wolfSSL supports up to TLS 1.3 and DTLS 1.3!
Mbedtls Pros and Cons
Pros
-
Lightweight and Modular: MbedTLS is designed to be compact and modular, making it suitable for embedded systems and IoT devices with limited resources.
-
Cross-Platform Compatibility: The library supports a wide range of platforms and operating systems, including various embedded systems, making it versatile for different projects.
-
Extensive Documentation: MbedTLS provides comprehensive documentation, including API references, example code, and tutorials, which helps developers integrate and use the library effectively.
-
Active Development and Support: Maintained by Arm, the project receives regular updates, security patches, and has an active community for support and contributions.
Cons
-
Performance: While optimized for embedded systems, MbedTLS may not offer the same level of performance as some other SSL/TLS libraries when used in high-throughput server environments.
-
Limited Advanced Features: Compared to some larger SSL/TLS libraries, MbedTLS may lack certain advanced features or cutting-edge cryptographic algorithms.
-
Learning Curve: For developers new to cryptography or those used to other SSL/TLS libraries, there might be a learning curve to effectively utilize MbedTLS's API and features.
-
Potential Resource Constraints: Although designed for embedded systems, implementing full SSL/TLS functionality may still be challenging on extremely resource-constrained devices.
Wolfssl Pros and Cons
Pros
-
Lightweight and Efficient: wolfSSL is designed to be a small, fast, and portable SSL/TLS library, making it ideal for embedded systems and IoT devices.
-
Wide Platform Support: It supports a broad range of operating systems and platforms, including embedded systems, RTOS, and desktop environments.
-
Strong Security Features: wolfSSL offers robust security features, including support for the latest TLS 1.3 protocol and various cryptographic algorithms.
-
Open Source with Commercial Support: Available under both open-source (GPLv2) and commercial licenses, providing flexibility for different use cases and access to professional support if needed.
Cons
-
Learning Curve: For developers accustomed to OpenSSL, there may be a learning curve when switching to wolfSSL's API and configuration options.
-
Limited Ecosystem: Compared to more established libraries like OpenSSL, wolfSSL has a smaller ecosystem of tools, extensions, and third-party integrations.
-
Documentation Challenges: While improving, some users report that the documentation could be more comprehensive and user-friendly, especially for advanced features.
-
Performance Trade-offs: In some scenarios, the focus on small footprint and embedded systems might lead to performance trade-offs compared to larger, more feature-rich SSL/TLS libraries.
Mbedtls Code Examples
SSL/TLS Client Connection
This snippet demonstrates how to establish a secure SSL/TLS connection as a client:
#include "mbedtls/ssl.h"
mbedtls_ssl_context ssl;
mbedtls_ssl_config conf;
mbedtls_ssl_init(&ssl);
mbedtls_ssl_config_init(&conf);
mbedtls_ssl_config_defaults(&conf, MBEDTLS_SSL_IS_CLIENT, MBEDTLS_SSL_TRANSPORT_STREAM, MBEDTLS_SSL_PRESET_DEFAULT);
mbedtls_ssl_setup(&ssl, &conf);
mbedtls_ssl_set_hostname(&ssl, "example.com");
Cryptographic Operations
Here's an example of using mbedTLS for AES encryption:
#include "mbedtls/aes.h"
mbedtls_aes_context aes;
unsigned char key[32];
unsigned char input[16], output[16];
mbedtls_aes_init(&aes);
mbedtls_aes_setkey_enc(&aes, key, 256);
mbedtls_aes_crypt_ecb(&aes, MBEDTLS_AES_ENCRYPT, input, output);
mbedtls_aes_free(&aes);
X.509 Certificate Parsing
This snippet shows how to parse an X.509 certificate:
#include "mbedtls/x509_crt.h"
mbedtls_x509_crt cert;
mbedtls_x509_crt_init(&cert);
int ret = mbedtls_x509_crt_parse_file(&cert, "certificate.pem");
if (ret != 0) {
// Handle error
}
mbedtls_x509_crt_free(&cert);
Wolfssl Code Examples
TLS Client Connection
This snippet demonstrates how to create a TLS client connection using wolfSSL:
WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
WOLFSSL* ssl = wolfSSL_new(ctx);
wolfSSL_set_fd(ssl, sockfd);
int ret = wolfSSL_connect(ssl);
if (ret != SSL_SUCCESS) {
// Handle error
}
// Use wolfSSL_read() and wolfSSL_write() for secure communication
Generating RSA Keys
Here's an example of generating RSA keys using wolfSSL:
RsaKey key;
WC_RNG rng;
wc_InitRng(&rng);
wc_InitRsaKey(&key, NULL);
int ret = wc_MakeRsaKey(&key, 2048, 65537, &rng);
if (ret != 0) {
// Handle error
}
// Use the generated key for encryption/decryption or signing/verification
SHA-256 Hashing
This snippet shows how to perform SHA-256 hashing with wolfSSL:
Sha256 sha;
byte hash[SHA256_DIGEST_SIZE];
byte data[] = "Hello, wolfSSL!";
wc_InitSha256(&sha);
wc_Sha256Update(&sha, data, sizeof(data));
wc_Sha256Final(&sha, hash);
// hash now contains the SHA-256 digest of the input data
Mbedtls Quick Start
Installation
To get started with Mbed TLS, follow these steps:
-
Clone the repository:
git clone https://github.com/Mbed-TLS/mbedtls.git
-
Change to the project directory:
cd mbedtls
-
Configure and build the library:
mkdir build cd build cmake .. make
-
(Optional) Install the library system-wide:
sudo make install
Basic Usage Example
Here's a simple example of how to use Mbed TLS to generate a random number:
-
Create a new C file named
random_example.c
with the following content:#include <stdio.h> #include <stdlib.h> #include "mbedtls/entropy.h" #include "mbedtls/ctr_drbg.h" int main() { mbedtls_entropy_context entropy; mbedtls_ctr_drbg_context ctr_drbg; unsigned char random_bytes[32]; mbedtls_entropy_init(&entropy); mbedtls_ctr_drbg_init(&ctr_drbg); if (mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, NULL, 0) != 0) { printf("Failed to seed random number generator\n"); return 1; } if (mbedtls_ctr_drbg_random(&ctr_drbg, random_bytes, sizeof(random_bytes)) != 0) { printf("Failed to generate random bytes\n"); return 1; } printf("Random bytes: "); for (int i = 0; i < sizeof(random_bytes); i++) { printf("%02x", random_bytes[i]); } printf("\n"); mbedtls_ctr_drbg_free(&ctr_drbg); mbedtls_entropy_free(&entropy); return 0; }
-
Compile the example:
gcc random_example.c -o random_example -lmbedtls -lmbedcrypto
-
Run the example:
./random_example
This example demonstrates how to initialize the entropy source and random number generator, generate random bytes, and clean up resources when finished.
Wolfssl Quick Start
Installation
To get started with wolfSSL, follow these steps:
-
Clone the repository:
git clone https://github.com/wolfSSL/wolfssl.git
-
Change to the wolfSSL directory:
cd wolfssl
-
Configure and build the library:
./autogen.sh ./configure make
-
(Optional) Install the library system-wide:
sudo make install
Basic Usage Example
Here's a simple example of how to use wolfSSL in a C program:
- Create a new C file, e.g.,
example.c
:
#include <wolfssl/ssl.h>
#include <wolfssl/wolfcrypt/settings.h>
int main() {
WOLFSSL_CTX* ctx;
WOLFSSL* ssl;
wolfSSL_Init();
ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
if (ctx == NULL) {
// Handle error
return -1;
}
ssl = wolfSSL_new(ctx);
if (ssl == NULL) {
// Handle error
wolfSSL_CTX_free(ctx);
return -1;
}
// Use ssl for secure communication...
wolfSSL_free(ssl);
wolfSSL_CTX_free(ctx);
wolfSSL_Cleanup();
return 0;
}
-
Compile the program:
gcc -o example example.c -lwolfssl
-
Run the compiled program:
./example
This example demonstrates how to initialize wolfSSL, create a context and an SSL object, and properly clean up resources. In a real-world application, you would add more code to establish a connection, perform I/O operations, and handle errors.
Top Related Projects
Arm Mbed OS is a platform operating system designed for the internet of things
Pros of mbed-os
- Comprehensive IoT development platform with RTOS, drivers, and libraries
- Supports a wide range of ARM Cortex-M based hardware
- Integrated development environment with Mbed CLI and online compiler
Cons of mbed-os
- Larger footprint compared to standalone cryptographic libraries
- May include unnecessary components for projects focused solely on security
- Steeper learning curve for developers new to the Mbed ecosystem
Code Comparison
mbed-os (main.cpp):
#include "mbed.h"
int main() {
DigitalOut led(LED1);
while (true) {
led = !led;
ThisThread::sleep_for(500ms);
}
}
mbedtls (example.c):
#include "mbedtls/ssl.h"
int main() {
mbedtls_ssl_context ssl;
mbedtls_ssl_init(&ssl);
// SSL configuration and connection setup
mbedtls_ssl_free(&ssl);
return 0;
}
wolfssl (example.c):
#include <wolfssl/ssl.h>
int main() {
WOLFSSL* ssl;
WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
ssl = wolfSSL_new(ctx);
// SSL configuration and connection setup
wolfSSL_free(ssl);
wolfSSL_CTX_free(ctx);
return 0;
}
TLS/SSL and crypto library
Pros of OpenSSL
- Extensive feature set and wide protocol support
- Large community and extensive documentation
- Robust security with frequent updates and patches
Cons of OpenSSL
- Large codebase and memory footprint
- Complex API and steep learning curve
- Slower performance compared to lightweight alternatives
Code Comparison
OpenSSL:
SSL_CTX *ctx = SSL_CTX_new(TLS_method());
SSL *ssl = SSL_new(ctx);
SSL_set_fd(ssl, socket);
SSL_connect(ssl);
MbedTLS:
mbedtls_ssl_context ssl;
mbedtls_ssl_init(&ssl);
mbedtls_ssl_setup(&ssl, &conf);
mbedtls_ssl_set_bio(&ssl, &server_fd, mbedtls_net_send, mbedtls_net_recv, NULL);
mbedtls_ssl_handshake(&ssl);
WolfSSL:
WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
WOLFSSL* ssl = wolfSSL_new(ctx);
wolfSSL_set_fd(ssl, sockfd);
wolfSSL_connect(ssl);
OpenSSL provides a comprehensive set of cryptographic functions and protocols, making it suitable for a wide range of applications. However, its large codebase and complex API can be challenging for beginners and resource-constrained environments. MbedTLS and WolfSSL offer more lightweight alternatives with simpler APIs, making them better suited for embedded systems and IoT devices. The code comparison shows similar basic usage patterns across all three libraries, with slight variations in function names and initialization processes.
LibreSSL Portable itself. This includes the build scaffold and compatibility layer that builds portable LibreSSL from the OpenBSD source code. Pull requests or patches sent to tech@openbsd.org are welcome.
Pros of LibreSSL
- Focuses on security and code cleanliness, with regular audits and removal of obsolete features
- Maintains better compatibility with OpenSSL, making it easier to adopt in existing projects
- Provides a more conservative approach to cryptography, prioritizing well-established algorithms
Cons of LibreSSL
- Slower release cycle compared to mbedTLS and wolfSSL
- Less optimized for embedded systems and resource-constrained environments
- Smaller community and fewer commercial support options
Code Comparison
LibreSSL (simplified TLS handshake):
SSL_CTX *ctx = SSL_CTX_new(TLS_method());
SSL *ssl = SSL_new(ctx);
SSL_set_fd(ssl, socket);
SSL_connect(ssl);
mbedTLS (simplified TLS handshake):
mbedtls_ssl_context ssl;
mbedtls_ssl_init(&ssl);
mbedtls_ssl_setup(&ssl, &conf);
mbedtls_ssl_set_bio(&ssl, &server_fd, mbedtls_net_send, mbedtls_net_recv, NULL);
mbedtls_ssl_handshake(&ssl);
wolfSSL (simplified TLS handshake):
WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
WOLFSSL* ssl = wolfSSL_new(ctx);
wolfSSL_set_fd(ssl, sockfd);
wolfSSL_connect(ssl);
An implementation of the TLS/SSL protocols
Pros of s2n-tls
- Designed for high performance and low latency
- Focused on simplicity and minimal codebase
- Backed by Amazon Web Services, ensuring regular updates and support
Cons of s2n-tls
- Limited to TLS 1.2 and 1.3 protocols
- Fewer cryptographic algorithms and features compared to mbedtls and wolfssl
- Less flexibility for customization in non-AWS environments
Code Comparison
mbedtls:
#include "mbedtls/ssl.h"
mbedtls_ssl_context ssl;
mbedtls_ssl_init(&ssl);
mbedtls_ssl_setup(&ssl, &conf);
mbedtls_ssl_handshake(&ssl);
wolfssl:
#include <wolfssl/ssl.h>
WOLFSSL* ssl;
WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
ssl = wolfSSL_new(ctx);
wolfSSL_connect(ssl);
s2n-tls:
#include <s2n.h>
struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT);
s2n_connection_set_config(conn, config);
s2n_negotiate(conn, &blocked);
The code snippets demonstrate that s2n-tls has a simpler API compared to mbedtls and wolfssl, with fewer function calls required to establish a connection. However, this simplicity may come at the cost of reduced flexibility for advanced use cases.
Mirror of BoringSSL
Pros of BoringSSL
- Developed and maintained by Google, benefiting from their security expertise
- Optimized for performance in Google's infrastructure
- Regularly updated with modern cryptographic standards and algorithms
Cons of BoringSSL
- Not designed as a general-purpose cryptographic library for external use
- May lack some features and compatibility options found in mbedTLS and wolfSSL
- Documentation and support might be less comprehensive for non-Google users
Code Comparison
mbedTLS:
#include "mbedtls/ssl.h"
mbedtls_ssl_context ssl;
mbedtls_ssl_init(&ssl);
wolfSSL:
#include <wolfssl/ssl.h>
WOLFSSL* ssl;
ssl = wolfSSL_new(ctx);
BoringSSL:
#include <openssl/ssl.h>
SSL* ssl;
ssl = SSL_new(ctx);
All three libraries provide similar APIs for SSL/TLS functionality, but BoringSSL's API is more closely aligned with OpenSSL. mbedTLS and wolfSSL offer more flexibility for embedded systems and resource-constrained environments, while BoringSSL focuses on performance and security for Google's specific use cases. The choice between these libraries depends on the project requirements, target platform, and desired level of support and documentation.