Gilles Peskine | ceb7b12 | 2018-01-18 23:27:47 +0100 | [diff] [blame] | 1 | Mbed TLS sample programs |
| 2 | ======================== |
| 3 | |
| 4 | This subdirectory mostly contains sample programs that illustrate specific features of the library, as well as a few test and support programs. |
| 5 | |
| 6 | ## Symmetric cryptography (AES) examples |
| 7 | |
Gilles Peskine | c2e5cdd | 2018-07-30 20:11:05 +0200 | [diff] [blame] | 8 | * [`aes/aescrypt2.c`](aes/aescrypt2.c): file encryption and authentication with a key derived from a low-entropy secret, demonstrating the low-level AES interface, the digest interface and HMAC. |
| 9 | Warning: this program illustrates how to use low-level functions in the library. It should not be taken as an example of how to build a secure encryption mechanism. To derive a key from a low-entropy secret such as a password, use a standard key stretching mechanism such as PBKDF2 (provided by the `pkcs5` module). To encrypt and authenticate data, use a standard mode such as GCM or CCM (both available as library module). |
Gilles Peskine | ceb7b12 | 2018-01-18 23:27:47 +0100 | [diff] [blame] | 10 | |
| 11 | * [`aes/crypt_and_hash.c`](aes/crypt_and_hash.c): file encryption and authentication, demonstrating the generic cipher interface and the generic hash interface. |
| 12 | |
| 13 | ## Hash (digest) examples |
| 14 | |
| 15 | * [`hash/generic_sum.c`](hash/generic_sum.c): file hash calculator and verifier, demonstrating the message digest (`md`) interface. |
| 16 | |
| 17 | * [`hash/hello.c`](hash/hello.c): hello-world program for MD5. |
| 18 | |
| 19 | ## Public-key cryptography examples |
| 20 | |
| 21 | ### Generic public-key cryptography (`pk`) examples |
| 22 | |
Gilles Peskine | 6b9cbb8 | 2018-07-30 20:06:19 +0200 | [diff] [blame] | 23 | * [`pkey/gen_key.c`](pkey/gen_key.c): generates a key for any of the supported public-key algorithms (RSA or ECC) and writes it to a file that can be used by the other pk sample programs. |
Gilles Peskine | ceb7b12 | 2018-01-18 23:27:47 +0100 | [diff] [blame] | 24 | |
Gilles Peskine | 6b9cbb8 | 2018-07-30 20:06:19 +0200 | [diff] [blame] | 25 | * [`pkey/key_app.c`](pkey/key_app.c): loads a PEM or DER public key or private key file and dumps its content. |
Gilles Peskine | ceb7b12 | 2018-01-18 23:27:47 +0100 | [diff] [blame] | 26 | |
Gilles Peskine | 6b9cbb8 | 2018-07-30 20:06:19 +0200 | [diff] [blame] | 27 | * [`pkey/key_app_writer.c`](pkey/key_app_writer.c): loads a PEM or DER public key or private key file and writes it to a new PEM or DER file. |
Gilles Peskine | ceb7b12 | 2018-01-18 23:27:47 +0100 | [diff] [blame] | 28 | |
Gilles Peskine | 27a0460 | 2018-08-06 20:09:16 +0200 | [diff] [blame] | 29 | * [`pkey/pk_encrypt.c`](pkey/pk_encrypt.c), [`pkey/pk_decrypt.c`](pkey/pk_decrypt.c): loads a PEM or DER public/private key file and uses the key to encrypt/decrypt a short string through the generic public-key interface. |
Gilles Peskine | ceb7b12 | 2018-01-18 23:27:47 +0100 | [diff] [blame] | 30 | |
Gilles Peskine | 27a0460 | 2018-08-06 20:09:16 +0200 | [diff] [blame] | 31 | * [`pkey/pk_sign.c`](pkey/pk_sign.c), [`pkey/pk_verify.c`](pkey/pk_verify.c): loads a PEM or DER private/public key file and uses the key to sign/verify a short string. |
Gilles Peskine | ceb7b12 | 2018-01-18 23:27:47 +0100 | [diff] [blame] | 32 | |
| 33 | ### ECDSA and RSA signature examples |
| 34 | |
Gilles Peskine | 0b54419 | 2018-08-10 11:32:11 +0200 | [diff] [blame] | 35 | * [`pkey/ecdsa.c`](pkey/ecdsa.c): generates an ECDSA key, signs a fixed message and verifies the signature. |
Gilles Peskine | ceb7b12 | 2018-01-18 23:27:47 +0100 | [diff] [blame] | 36 | |
Gilles Peskine | 27a0460 | 2018-08-06 20:09:16 +0200 | [diff] [blame] | 37 | * [`pkey/rsa_encrypt.c`](pkey/rsa_encrypt.c), [`pkey/rsa_decrypt.c`](pkey/rsa_decrypt.c): loads an RSA public/private key and uses it to encrypt/decrypt a short string through the low-level RSA interface. |
Gilles Peskine | ceb7b12 | 2018-01-18 23:27:47 +0100 | [diff] [blame] | 38 | |
Gilles Peskine | 27a0460 | 2018-08-06 20:09:16 +0200 | [diff] [blame] | 39 | * [`pkey/rsa_genkey.c`](pkey/rsa_genkey.c): generates an RSA key and writes it to a file that can be used with the other RSA sample programs. |
Gilles Peskine | ceb7b12 | 2018-01-18 23:27:47 +0100 | [diff] [blame] | 40 | |
Gilles Peskine | 27a0460 | 2018-08-06 20:09:16 +0200 | [diff] [blame] | 41 | * [`pkey/rsa_sign.c`](pkey/rsa_sign.c), [`pkey/rsa_verify.c`](pkey/rsa_verify.c): loads an RSA private/public key and uses it to sign/verify a short string with the RSA PKCS#1 v1.5 algorithm. |
Gilles Peskine | ceb7b12 | 2018-01-18 23:27:47 +0100 | [diff] [blame] | 42 | |
Gilles Peskine | 0b54419 | 2018-08-10 11:32:11 +0200 | [diff] [blame] | 43 | * [`pkey/rsa_sign_pss.c`](pkey/rsa_sign_pss.c), [`pkey/rsa_verify_pss.c`](pkey/rsa_verify_pss.c): loads an RSA private/public key and uses it to sign/verify a short string with the RSASSA-PSS algorithm. |
Gilles Peskine | ceb7b12 | 2018-01-18 23:27:47 +0100 | [diff] [blame] | 44 | |
| 45 | ### Diffie-Hellman key exchange examples |
| 46 | |
Gilles Peskine | 27a0460 | 2018-08-06 20:09:16 +0200 | [diff] [blame] | 47 | * [`pkey/dh_client.c`](pkey/dh_client.c), [`pkey/dh_server.c`](pkey/dh_server.c): secure channel demonstrators (client, server). This pair of programs illustrates how to set up a secure channel using RSA for authentication and Diffie-Hellman to generate a shared AES session key. |
Gilles Peskine | ceb7b12 | 2018-01-18 23:27:47 +0100 | [diff] [blame] | 48 | |
| 49 | * [`pkey/ecdh_curve25519.c`](pkey/ecdh_curve25519.c): demonstration of a elliptic curve Diffie-Hellman (ECDH) key agreement. |
| 50 | |
| 51 | ### Bignum (`mpi`) usage examples |
| 52 | |
Gilles Peskine | 6b9cbb8 | 2018-07-30 20:06:19 +0200 | [diff] [blame] | 53 | * [`pkey/dh_genprime.c`](pkey/dh_genprime.c): shows how to use the bignum (`mpi`) interface to generate Diffie-Hellman parameters. |
Gilles Peskine | ceb7b12 | 2018-01-18 23:27:47 +0100 | [diff] [blame] | 54 | |
| 55 | * [`pkey/mpi_demo.c`](pkey/mpi_demo.c): demonstrates operations on big integers. |
| 56 | |
| 57 | ## Random number generator (RNG) examples |
| 58 | |
Gilles Peskine | 6b9cbb8 | 2018-07-30 20:06:19 +0200 | [diff] [blame] | 59 | * [`random/gen_entropy.c`](random/gen_entropy.c): shows how to use the default entropy sources to generate random data. |
| 60 | Note: most applications should only use the entropy generator to seed a cryptographic pseudorandom generator, as illustrated by `random/gen_random_ctr_drbg.c`. |
Gilles Peskine | ceb7b12 | 2018-01-18 23:27:47 +0100 | [diff] [blame] | 61 | |
Gilles Peskine | aa22030 | 2018-08-06 20:19:50 +0200 | [diff] [blame] | 62 | * [`random/gen_random_ctr_drbg.c`](random/gen_random_ctr_drbg.c): shows how to use the default entropy sources to seed a pseudorandom generator, and how to use the resulting random generator to generate random data. |
Gilles Peskine | ceb7b12 | 2018-01-18 23:27:47 +0100 | [diff] [blame] | 63 | |
Gilles Peskine | 6b9cbb8 | 2018-07-30 20:06:19 +0200 | [diff] [blame] | 64 | * [`random/gen_random_havege.c`](random/gen_random_havege.c): demonstrates the HAVEGE entropy collector. |
Gilles Peskine | ceb7b12 | 2018-01-18 23:27:47 +0100 | [diff] [blame] | 65 | |
| 66 | ## SSL/TLS examples |
| 67 | |
| 68 | ### SSL/TLS sample applications |
| 69 | |
Gilles Peskine | 6b9cbb8 | 2018-07-30 20:06:19 +0200 | [diff] [blame] | 70 | * [`ssl/dtls_client.c`](ssl/dtls_client.c): a simple DTLS client program, which sends one datagram to the server and reads one datagram in response. |
Gilles Peskine | ceb7b12 | 2018-01-18 23:27:47 +0100 | [diff] [blame] | 71 | |
Gilles Peskine | 6b9cbb8 | 2018-07-30 20:06:19 +0200 | [diff] [blame] | 72 | * [`ssl/dtls_server.c`](ssl/dtls_server.c): a simple DTLS server program, which expects one datagram from the client and writes one datagram in response. This program supports DTLS cookies for hello verification. |
Gilles Peskine | ceb7b12 | 2018-01-18 23:27:47 +0100 | [diff] [blame] | 73 | |
Gilles Peskine | 6b9cbb8 | 2018-07-30 20:06:19 +0200 | [diff] [blame] | 74 | * [`ssl/mini_client.c`](ssl/mini_client.c): a minimalistic SSL client, which sends a short string and disconnects. This is primarily intended as a benchmark; for a better example of a typical TLS client, see `ssl/ssl_client1.c`. |
Gilles Peskine | ceb7b12 | 2018-01-18 23:27:47 +0100 | [diff] [blame] | 75 | |
| 76 | * [`ssl/ssl_client1.c`](ssl/ssl_client1.c): a simple HTTPS client that sends a fixed request and displays the response. |
| 77 | |
| 78 | * [`ssl/ssl_fork_server.c`](ssl/ssl_fork_server.c): a simple HTTPS server using one process per client to send a fixed response. This program requires a Unix/POSIX environment implementing the `fork` system call. |
| 79 | |
Gilles Peskine | 6b9cbb8 | 2018-07-30 20:06:19 +0200 | [diff] [blame] | 80 | * [`ssl/ssl_mail_client.c`](ssl/ssl_mail_client.c): a simple SMTP-over-TLS or SMTP-STARTTLS client. This client sends an email with fixed content. |
Gilles Peskine | ceb7b12 | 2018-01-18 23:27:47 +0100 | [diff] [blame] | 81 | |
Gilles Peskine | 6b9cbb8 | 2018-07-30 20:06:19 +0200 | [diff] [blame] | 82 | * [`ssl/ssl_pthread_server.c`](ssl/ssl_pthread_server.c): a simple HTTPS server using one thread per client to send a fixed response. This program requires the pthread library. |
Gilles Peskine | ceb7b12 | 2018-01-18 23:27:47 +0100 | [diff] [blame] | 83 | |
Gilles Peskine | aa22030 | 2018-08-06 20:19:50 +0200 | [diff] [blame] | 84 | * [`ssl/ssl_server.c`](ssl/ssl_server.c): a simple HTTPS server that sends a fixed response. It serves a single client at a time. |
Gilles Peskine | ceb7b12 | 2018-01-18 23:27:47 +0100 | [diff] [blame] | 85 | |
| 86 | ### SSL/TLS feature demonstrators |
| 87 | |
Gilles Peskine | 6b9cbb8 | 2018-07-30 20:06:19 +0200 | [diff] [blame] | 88 | Note: unlike most of the other programs under the `programs/` directory, these two programs are not intended as a basis for writing an application. They combine most of the features supported by the library, and most applications require only a few features. To write a new application, we recommended that you start with `ssl_client1.c` or `ssl_server.c`, and then look inside `ssl/ssl_client2.c` or `ssl/ssl_server2.c` to see how to use the specific features that your application needs. |
Gilles Peskine | ceb7b12 | 2018-01-18 23:27:47 +0100 | [diff] [blame] | 89 | |
| 90 | * [`ssl/ssl_client2.c`](ssl/ssl_client2.c): an HTTPS client that sends a fixed request and displays the response, with options to select TLS protocol features and Mbed TLS library features. |
| 91 | |
| 92 | * [`ssl/ssl_server2.c`](ssl/ssl_server2.c): an HTTPS server that sends a fixed response, with options to select TLS protocol features and Mbed TLS library features. |
| 93 | |
Gilles Peskine | aa22030 | 2018-08-06 20:19:50 +0200 | [diff] [blame] | 94 | In addition to providing options for testing client-side features, the `ssl_client2` program has options that allow you to trigger certain behaviors in the server. For example, there are options to select ciphersuites, or to force a renegotiation. These options are useful for testing the corresponding features in a TLS server. Likewise, `ssl_server2` has options to activate certain behaviors that are useful for testing a TLS client. |
Gilles Peskine | ceb7b12 | 2018-01-18 23:27:47 +0100 | [diff] [blame] | 95 | |
| 96 | ## Test utilities |
| 97 | |
| 98 | * [`test/benchmark.c`](test/benchmark.c): benchmark for cryptographic algorithms. |
| 99 | |
Gilles Peskine | 6b9cbb8 | 2018-07-30 20:06:19 +0200 | [diff] [blame] | 100 | * [`test/selftest.c`](test/selftest.c): runs the self-test function in each library module. |
Gilles Peskine | ceb7b12 | 2018-01-18 23:27:47 +0100 | [diff] [blame] | 101 | |
Gilles Peskine | 6b9cbb8 | 2018-07-30 20:06:19 +0200 | [diff] [blame] | 102 | * [`test/udp_proxy.c`](test/udp_proxy.c): a UDP proxy that can inject certain failures (delay, duplicate, drop). Useful for testing DTLS. |
Gilles Peskine | ceb7b12 | 2018-01-18 23:27:47 +0100 | [diff] [blame] | 103 | |
Gilles Peskine | ffbdc61 | 2018-08-10 11:48:52 +0200 | [diff] [blame] | 104 | * [`test/zeroize.c`](test/zeroize.c): a test program for `mbedtls_platform_zeroize`, used by [`tests/scripts/test_zeroize.gdb`](tests/scripts/test_zeroize.gdb). |
| 105 | |
Gilles Peskine | ceb7b12 | 2018-01-18 23:27:47 +0100 | [diff] [blame] | 106 | ## Development utilities |
| 107 | |
Gilles Peskine | 6b9cbb8 | 2018-07-30 20:06:19 +0200 | [diff] [blame] | 108 | * [`util/pem2der.c`](util/pem2der.c): a PEM to DER converter. Mbed TLS can read PEM files directly, but this utility can be useful for interacting with other tools or with minimal Mbed TLS builds that lack PEM support. |
Gilles Peskine | ceb7b12 | 2018-01-18 23:27:47 +0100 | [diff] [blame] | 109 | |
Gilles Peskine | 6b9cbb8 | 2018-07-30 20:06:19 +0200 | [diff] [blame] | 110 | * [`util/strerror.c`](util/strerror.c): prints the error description corresponding to an integer status returned by an Mbed TLS function. |
Gilles Peskine | ceb7b12 | 2018-01-18 23:27:47 +0100 | [diff] [blame] | 111 | |
| 112 | ## X.509 certificate examples |
| 113 | |
Gilles Peskine | 6b9cbb8 | 2018-07-30 20:06:19 +0200 | [diff] [blame] | 114 | * [`x509/cert_app.c`](x509/cert_app.c): connects to a TLS server and verifies its certificate chain. |
Gilles Peskine | ceb7b12 | 2018-01-18 23:27:47 +0100 | [diff] [blame] | 115 | |
Gilles Peskine | 6b9cbb8 | 2018-07-30 20:06:19 +0200 | [diff] [blame] | 116 | * [`x509/cert_req.c`](x509/cert_req.c): generates a certificate signing request (CSR) for a private key. |
Gilles Peskine | ceb7b12 | 2018-01-18 23:27:47 +0100 | [diff] [blame] | 117 | |
Gilles Peskine | 27a0460 | 2018-08-06 20:09:16 +0200 | [diff] [blame] | 118 | * [`x509/cert_write.c`](x509/cert_write.c): signs a certificate signing request, or self-signs a certificate. |
Gilles Peskine | ceb7b12 | 2018-01-18 23:27:47 +0100 | [diff] [blame] | 119 | |
Gilles Peskine | 6b9cbb8 | 2018-07-30 20:06:19 +0200 | [diff] [blame] | 120 | * [`x509/crl_app.c`](x509/crl_app.c): loads and dumps a certificate revocation list (CRL). |
Gilles Peskine | ceb7b12 | 2018-01-18 23:27:47 +0100 | [diff] [blame] | 121 | |
Gilles Peskine | 6b9cbb8 | 2018-07-30 20:06:19 +0200 | [diff] [blame] | 122 | * [`x509/req_app.c`](x509/req_app.c): loads and dumps a certificate signing request (CSR). |
Gilles Peskine | ceb7b12 | 2018-01-18 23:27:47 +0100 | [diff] [blame] | 123 | |