1a8e1175bSopenharmony_ciMbed TLS sample programs
2a8e1175bSopenharmony_ci========================
3a8e1175bSopenharmony_ci
4a8e1175bSopenharmony_ciThis subdirectory mostly contains sample programs that illustrate specific features of the library, as well as a few test and support programs.
5a8e1175bSopenharmony_ci
6a8e1175bSopenharmony_ci## Symmetric cryptography (AES) examples
7a8e1175bSopenharmony_ci
8a8e1175bSopenharmony_ci* [`aes/crypt_and_hash.c`](aes/crypt_and_hash.c): file encryption and authentication, demonstrating the generic cipher interface and the generic hash interface.
9a8e1175bSopenharmony_ci
10a8e1175bSopenharmony_ci## Hash (digest) examples
11a8e1175bSopenharmony_ci
12a8e1175bSopenharmony_ci* [`hash/generic_sum.c`](hash/generic_sum.c): file hash calculator and verifier, demonstrating the message digest (`md`) interface.
13a8e1175bSopenharmony_ci
14a8e1175bSopenharmony_ci* [`hash/hello.c`](hash/hello.c): hello-world program for MD5.
15a8e1175bSopenharmony_ci
16a8e1175bSopenharmony_ci## Public-key cryptography examples
17a8e1175bSopenharmony_ci
18a8e1175bSopenharmony_ci### Generic public-key cryptography (`pk`) examples
19a8e1175bSopenharmony_ci
20a8e1175bSopenharmony_ci* [`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.
21a8e1175bSopenharmony_ci
22a8e1175bSopenharmony_ci* [`pkey/key_app.c`](pkey/key_app.c): loads a PEM or DER public key or private key file and dumps its content.
23a8e1175bSopenharmony_ci
24a8e1175bSopenharmony_ci* [`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.
25a8e1175bSopenharmony_ci
26a8e1175bSopenharmony_ci* [`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.
27a8e1175bSopenharmony_ci
28a8e1175bSopenharmony_ci* [`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.
29a8e1175bSopenharmony_ci
30a8e1175bSopenharmony_ci### ECDSA and RSA signature examples
31a8e1175bSopenharmony_ci
32a8e1175bSopenharmony_ci* [`pkey/ecdsa.c`](pkey/ecdsa.c): generates an ECDSA key, signs a fixed message and verifies the signature.
33a8e1175bSopenharmony_ci
34a8e1175bSopenharmony_ci* [`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.
35a8e1175bSopenharmony_ci
36a8e1175bSopenharmony_ci* [`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.
37a8e1175bSopenharmony_ci
38a8e1175bSopenharmony_ci* [`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.
39a8e1175bSopenharmony_ci
40a8e1175bSopenharmony_ci* [`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.
41a8e1175bSopenharmony_ci
42a8e1175bSopenharmony_ci### Diffie-Hellman key exchange examples
43a8e1175bSopenharmony_ci
44a8e1175bSopenharmony_ci* [`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.
45a8e1175bSopenharmony_ci
46a8e1175bSopenharmony_ci* [`pkey/ecdh_curve25519.c`](pkey/ecdh_curve25519.c): demonstration of a elliptic curve Diffie-Hellman (ECDH) key agreement.
47a8e1175bSopenharmony_ci
48a8e1175bSopenharmony_ci### Bignum (`mpi`) usage examples
49a8e1175bSopenharmony_ci
50a8e1175bSopenharmony_ci* [`pkey/dh_genprime.c`](pkey/dh_genprime.c): shows how to use the bignum (`mpi`) interface to generate Diffie-Hellman parameters.
51a8e1175bSopenharmony_ci
52a8e1175bSopenharmony_ci* [`pkey/mpi_demo.c`](pkey/mpi_demo.c): demonstrates operations on big integers.
53a8e1175bSopenharmony_ci
54a8e1175bSopenharmony_ci## Random number generator (RNG) examples
55a8e1175bSopenharmony_ci
56a8e1175bSopenharmony_ci* [`random/gen_entropy.c`](random/gen_entropy.c): shows how to use the default entropy sources to generate random data.  
57a8e1175bSopenharmony_ci  Note: most applications should only use the entropy generator to seed a cryptographic pseudorandom generator, as illustrated by `random/gen_random_ctr_drbg.c`.
58a8e1175bSopenharmony_ci
59a8e1175bSopenharmony_ci* [`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.
60a8e1175bSopenharmony_ci
61a8e1175bSopenharmony_ci## SSL/TLS examples
62a8e1175bSopenharmony_ci
63a8e1175bSopenharmony_ci### SSL/TLS sample applications
64a8e1175bSopenharmony_ci
65a8e1175bSopenharmony_ci* [`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.
66a8e1175bSopenharmony_ci
67a8e1175bSopenharmony_ci* [`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.
68a8e1175bSopenharmony_ci
69a8e1175bSopenharmony_ci* [`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`.
70a8e1175bSopenharmony_ci
71a8e1175bSopenharmony_ci* [`ssl/ssl_client1.c`](ssl/ssl_client1.c): a simple HTTPS client that sends a fixed request and displays the response.
72a8e1175bSopenharmony_ci
73a8e1175bSopenharmony_ci* [`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.
74a8e1175bSopenharmony_ci
75a8e1175bSopenharmony_ci* [`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.
76a8e1175bSopenharmony_ci
77a8e1175bSopenharmony_ci* [`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.
78a8e1175bSopenharmony_ci
79a8e1175bSopenharmony_ci* [`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.
80a8e1175bSopenharmony_ci
81a8e1175bSopenharmony_ci### SSL/TLS feature demonstrators
82a8e1175bSopenharmony_ci
83a8e1175bSopenharmony_ciNote: 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.
84a8e1175bSopenharmony_ci
85a8e1175bSopenharmony_ci* [`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.
86a8e1175bSopenharmony_ci
87a8e1175bSopenharmony_ci* [`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.
88a8e1175bSopenharmony_ci
89a8e1175bSopenharmony_ciIn 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.
90a8e1175bSopenharmony_ci
91a8e1175bSopenharmony_ci## Test utilities
92a8e1175bSopenharmony_ci
93a8e1175bSopenharmony_ci* [`test/benchmark.c`](test/benchmark.c): benchmark for cryptographic algorithms.
94a8e1175bSopenharmony_ci
95a8e1175bSopenharmony_ci* [`test/selftest.c`](test/selftest.c): runs the self-test function in each library module.
96a8e1175bSopenharmony_ci
97a8e1175bSopenharmony_ci* [`test/udp_proxy.c`](test/udp_proxy.c): a UDP proxy that can inject certain failures (delay, duplicate, drop). Useful for testing DTLS.
98a8e1175bSopenharmony_ci
99a8e1175bSopenharmony_ci* [`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).
100a8e1175bSopenharmony_ci
101a8e1175bSopenharmony_ci## Development utilities
102a8e1175bSopenharmony_ci
103a8e1175bSopenharmony_ci* [`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.
104a8e1175bSopenharmony_ci
105a8e1175bSopenharmony_ci* [`util/strerror.c`](util/strerror.c): prints the error description corresponding to an integer status returned by an Mbed TLS function.
106a8e1175bSopenharmony_ci
107a8e1175bSopenharmony_ci## X.509 certificate examples
108a8e1175bSopenharmony_ci
109a8e1175bSopenharmony_ci* [`x509/cert_app.c`](x509/cert_app.c): connects to a TLS server and verifies its certificate chain.
110a8e1175bSopenharmony_ci
111a8e1175bSopenharmony_ci* [`x509/cert_req.c`](x509/cert_req.c): generates a certificate signing request (CSR) for a private key.
112a8e1175bSopenharmony_ci
113a8e1175bSopenharmony_ci* [`x509/cert_write.c`](x509/cert_write.c): signs a certificate signing request, or self-signs a certificate.
114a8e1175bSopenharmony_ci
115a8e1175bSopenharmony_ci* [`x509/crl_app.c`](x509/crl_app.c): loads and dumps a certificate revocation list (CRL).
116a8e1175bSopenharmony_ci
117a8e1175bSopenharmony_ci* [`x509/req_app.c`](x509/req_app.c): loads and dumps a certificate signing request (CSR).
118a8e1175bSopenharmony_ci
119