Cryptographic library for Python with Mbed TLS back end status

python-mbedtls is a free cryptographic library for Python that uses mbed TLS for back end.

mbed TLS (formerly known as PolarSSL) makes it trivially easy for developers to include cryptographic and SSL/TLS capabilities in their (embedded) products, facilitating this functionality with a minimal coding footprint.

python-mbedtls API follows the recommendations from:

and therefore plays well with the cryptographic services from the Python standard library and many other cryptography libraries as well.


python-mbedtls is licensed under the MIT License (see LICENSE.txt). This enables the use of python-mbedtls in both open source and closed source projects. The MIT License is compatible with both GPL and Apache 2.0 license under which mbed TLS is distributed.

API documentation


The bindings are tested with mbedTLS 2.28.1 for Python 3.7, 3.8, 3.9, 3.10, and 3.11 on Linux, macOS, and Windows.

manylinux wheels are available for 64-bit Linux systems. Install with pip install python-mbedtls.

Usage and examples

Now, let us see examples using the various parts of the library.

Check which version of mbed TLS is being used by python-mbedtls

The mbedtls.version module shows the run-time version information to mbed TLS.

>>> from mbedtls import version
>>> _ = version.version  # "mbed TLS 2.28.1"
>>> _ = version.version_info  # (2, 28, 1)

Message digest

The mbedtls.hashlib module supports MD2, MD4, MD5, SHA-1, SHA-2 (in 224, 256, 384, and 512-bits), and RIPEMD-160 secure hashes and message digests. Note that MD2 and MD4 are not included by default and are only present if they are compiled in mbedtls.

Here are the examples from (standard) hashlib ported to python-mbedtls:

>>> from mbedtls import hashlib
>>> m = hashlib.md5()
>>> m.update(b"Nobody inspects")
>>> m.update(b" the spammish repetition")
>>> m.digest()
>>> m.digest_size
>>> m.block_size

More condensed:

>>> hashlib.sha224(b"Nobody inspects the spammish repetition").hexdigest()

Using new():

>>> h ='ripemd160')
>>> h.update(b"Nobody inspects the spammish repetition")
>>> h.hexdigest()

HMAC algorithm

The mbedtls.hmac module computes HMAC.


>>> from mbedtls import hmac
>>> m ="This is my secret key", digestmod="md5")
>>> m.update(b"Nobody inspects")
>>> m.update(b" the spammish repetition")
>>> m.digest()


The message is cleared after calculation of the digest. Only call mbedtls.hmac.Hmac.digest() or mbedtls.hmac.Hmac.hexdigest() once per message.

HMAC-based key derivation function (HKDF)

The mbedtls.hkdf module exposes extract-and-expand key derivation functions. The main function is hkdf() but extract() and expand() may be used as well.


>>> from mbedtls import hkdf
>>> hkdf.hkdf(
...     b"my secret key",
...     length=42,
...     info=b"my cool app",
...     salt=b"and pepper",
...     digestmod=hmac.sha256
... )

where info, salt, and digestmod are optional, although providing (at least) info is highly recommended.

Symmetric cipher

The mbedtls.cipher module provides symmetric encryption. The API follows the recommendations from PEP 272 so that it can be used as a drop-in replacement to other libraries.

python-mbedtls provides the following algorithms:

  • AES encryption/decryption (128, 192, and 256 bits) in ECB, CBC, CFB128, CTR, OFB, or XTS mode;

  • AES AEAD (128, 192, and 256 bits) in GCM or CCM mode;

  • ARC4 encryption/decryption;

  • ARIA encryption/decryption (128, 192, and 256 bits) in ECB, CBC, CTR, or GCM modes;

  • Blowfish encryption/decryption in ECB, CBC, CFB64, or CTR mode;

  • Camellia encryption/decryption (128, 192, and 256 bits) in ECB, CBC, CFB128, CTR, or GCM mode;

  • DES, DES3, and double DES3 encryption/decryption in ECB or CBC mode;

  • CHACHA20 and CHACHA20/POLY1305 encryption/decryption.


>>> from mbedtls import cipher
>>> c ="My 16-bytes key.", cipher.MODE_CBC, b"CBC needs an IV.")
>>> enc = c.encrypt(b"This is a super-secret message!!")
>>> enc
>>> c.decrypt(enc)
b'This is a super-secret message!!'

RSA public key

The module provides the RSA cryptosystem. This includes:

  • Public-private key generation and key import/export in PEM and DER formats;

  • asymmetric encryption and decryption;

  • message signature and verification.

Key generation, the default size is 2048 bits:

>>> from mbedtls import pk
>>> rsa = pk.RSA()
>>> prv = rsa.generate()
>>> rsa.key_size

Message encryption and decryption:

>>> enc = rsa.encrypt(b"secret message")
>>> rsa.decrypt(enc)
b'secret message'

Message signature and verification:

>>> sig = rsa.sign(b"Please sign here.")
>>> rsa.verify(b"Please sign here.", sig)
>>> rsa.verify(b"Sorry, wrong message.", sig)
>>> pub = rsa.export_public_key(format="DER")
>>> other = pk.RSA.from_buffer(pub)
>>> other.verify(b"Please sign here.", sig)

Static and ephemeral elliptic curve Diffie-Hellman

The module provides the ECC cryptosystem. This includes:

  • Public-private key generation and key import/export in the PEM and DER formats;

  • asymmetric encrypt and decryption;

  • message signature and verification;

  • ephemeral ECDH key exchange.

get_supported_curves() returns the list of supported curves.

The API of the ECC class is the same as the API of the RSA class but ciphering (encrypt() and decrypt() is not supported by Mbed TLS).

Message signature and verification using elliptic a curve digital signature algorithm (ECDSA):

>>> from mbedtls import pk
>>> ecdsa = pk.ECC()
>>> prv = ecdsa.generate()
>>> sig = ecdsa.sign(b"Please sign here.")
>>> ecdsa.verify(b"Please sign here.", sig)
>>> ecdsa.verify(b"Sorry, wrong message.", sig)
>>> pub = ecdsa.export_public_key(format="DER")
>>> other = pk.ECC.from_buffer(pub)
>>> other.verify(b"Please sign here.", sig)

The classes ECDHServer and ECDHClient may be used for ephemeral ECDH. The key exchange is as follows:

>>> ecdh_key = pk.ECC()
>>> ecdh_key.generate()
>>> ecdh_srv = pk.ECDHServer(ecdh_key)
>>> ecdh_cli = pk.ECDHClient(ecdh_key)

The server generates the ServerKeyExchange encrypted payload and passes it to the client:

>>> ske = ecdh_srv.generate()
>>> ecdh_cli.import_SKE(ske)

then the client generates the ClientKeyExchange encrypted payload and passes it back to the server:

>>> cke = ecdh_cli.generate()
>>> ecdh_srv.import_CKE(cke)

Now, client and server may generate their shared secret:

>>> secret = ecdh_srv.generate_secret()
>>> ecdh_cli.generate_secret() == secret
>>> ecdh_srv.shared_secret == ecdh_cli.shared_secret

Diffie-Hellman-Merkle key exchange

The classes DHServer and DHClient may be used for DH Key exchange. The classes have the same API as ECDHServer and ECDHClient, respectively.

The key exchange is as follow:

>>> from mbedtls.mpi import MPI
>>> from mbedtls import pk
>>> dh_srv = pk.DHServer(,
>>> dh_cli = pk.DHClient(,

The 128-bytes prime and the 96-bytes prime are the modulus P and the generator G.

The server generates the ServerKeyExchange payload:

>>> ske = dh_srv.generate()
>>> dh_cli.import_SKE(ske)

The payload ends with G^X mod P where X is the secret value of the server.

>>> cke = dh_cli.generate()
>>> dh_srv.import_CKE(cke)

cke is G^Y mod P (with Y the secret value from the client) returned as its representation in bytes so that it can be readily transported over the network.

As in ECDH, client and server may now generate their shared secret:

>>> secret = dh_srv.generate_secret()
>>> dh_cli.generate_secret() == secret
>>> dh_srv.shared_secret == dh_cli.shared_secret

X.509 certificate writing and parsing

The mbedtls.x509 module can be used to parse X.509 certificates or create and verify a certificate chain.

Here, the trusted root is a self-signed CA certificate ca0_crt signed by ca0_key.

>>> import datetime as dt
>>> from mbedtls import hashlib
>>> from mbedtls import pk
>>> from mbedtls import x509
>>> now = dt.datetime.utcnow()
>>> ca0_key = pk.RSA()
>>> _ = ca0_key.generate()
>>> ca0_csr =, "CN=Trusted CA", hashlib.sha256())
>>> ca0_crt = x509.CRT.selfsign(
...     ca0_csr, ca0_key,
...     not_before=now, not_after=now + dt.timedelta(days=90),
...     serial_number=0x123456,
...     basic_constraints=x509.BasicConstraints(True, 1))

An intermediate then issues a Certificate Singing Request (CSR) that the root CA signs:

>>> ca1_key = pk.ECC()
>>> _ = ca1_key.generate()
>>> ca1_csr =, "CN=Intermediate CA", hashlib.sha256())
>>> ca1_crt = ca0_crt.sign(
...     ca1_csr, ca0_key, now, now + dt.timedelta(days=90), 0x123456,
...     basic_constraints=x509.BasicConstraints(ca=True, max_path_length=3))

And finally, the intermediate CA signs a certificate for the End Entity on the basis of a new CSR:

>>> ee0_key = pk.ECC()
>>> _ = ee0_key.generate()
>>> ee0_csr =, "CN=End Entity", hashlib.sha256())
>>> ee0_crt = ca1_crt.sign(
...     ee0_csr, ca1_key, now, now + dt.timedelta(days=90), 0x987654)

The emitting certificate can be used to verify the next certificate in the chain:

>>> ca1_crt.verify(ee0_crt)
>>> ca0_crt.verify(ca1_crt)

Note, however, that this verification is only one step in a private key infrastructure and does not take CRLs, path length, etc. into account.

TLS and DTLS client and server

The mbedtls.tls module provides TLS clients and servers. The API follows the recommendations of PEP 543. Note, however, that the Python standard SSL library does not follow the PEP so that this library may not be a drop-in replacement.

Connectionless DTLS is supported as well.

See examples in the programs/ directory of the repository and tests/

API documentation

Indices and tables