Ali Zhang | 6a23acf | 2021-06-21 15:55:48 -0700 | [diff] [blame] | 1 | .. _module-pw_crypto: |
| 2 | |
Ali Zhang | 6a23acf | 2021-06-21 15:55:48 -0700 | [diff] [blame] | 3 | pw_crypto |
Ali Zhang | ef68dc6 | 2021-06-25 16:25:44 -0700 | [diff] [blame] | 4 | ========= |
Ali Zhang | 6a23acf | 2021-06-21 15:55:48 -0700 | [diff] [blame] | 5 | A set of safe (read: easy to use, hard to misuse) crypto APIs. |
| 6 | |
| 7 | .. attention:: |
| 8 | |
| 9 | This module is under construction. |
| 10 | |
Ali Zhang | ef68dc6 | 2021-06-25 16:25:44 -0700 | [diff] [blame] | 11 | The following crypto services are provided by this module. |
Ali Zhang | 6a23acf | 2021-06-21 15:55:48 -0700 | [diff] [blame] | 12 | |
Ali Zhang | 2c44c22 | 2021-07-20 09:44:18 -0700 | [diff] [blame] | 13 | 1. Hashing a message with `SHA256`_. |
Ali Zhang | ef68dc6 | 2021-06-25 16:25:44 -0700 | [diff] [blame] | 14 | 2. Verifying a digital signature signed with `ECDSA`_ over the NIST P256 curve. |
| 15 | 3. Many more to come ... |
Ali Zhang | 6a23acf | 2021-06-21 15:55:48 -0700 | [diff] [blame] | 16 | |
Ali Zhang | ef68dc6 | 2021-06-25 16:25:44 -0700 | [diff] [blame] | 17 | SHA256 |
| 18 | ------ |
| 19 | |
Ali Zhang | ef68dc6 | 2021-06-25 16:25:44 -0700 | [diff] [blame] | 20 | 1. Obtaining a oneshot digest. |
Ali Zhang | 6a23acf | 2021-06-21 15:55:48 -0700 | [diff] [blame] | 21 | |
| 22 | .. code-block:: cpp |
| 23 | |
| 24 | #include "pw_crypto/sha256.h" |
| 25 | |
| 26 | std::byte digest[32]; |
Ali Zhang | 2c44c22 | 2021-07-20 09:44:18 -0700 | [diff] [blame] | 27 | Status status = pw::crypto::sha256::Hash(message, digest); |
Ali Zhang | 6a23acf | 2021-06-21 15:55:48 -0700 | [diff] [blame] | 28 | |
Ali Zhang | 2c44c22 | 2021-07-20 09:44:18 -0700 | [diff] [blame] | 29 | 2. Hashing a long, potentially non-contiguous message. |
Ali Zhang | 6a23acf | 2021-06-21 15:55:48 -0700 | [diff] [blame] | 30 | |
| 31 | .. code-block:: cpp |
| 32 | |
| 33 | #include "pw_crypto/sha256.h" |
| 34 | |
| 35 | std::byte digest[32]; |
| 36 | auto h = pw::crypto::sha256::Sha256(); |
| 37 | |
| 38 | while (/* chunk ← Get next chunk of message */) { |
| 39 | h.Update(chunk); |
| 40 | } |
| 41 | |
| 42 | Status status = h.Final(digest); |
| 43 | |
Ali Zhang | ef68dc6 | 2021-06-25 16:25:44 -0700 | [diff] [blame] | 44 | ECDSA |
| 45 | ----- |
| 46 | |
Ali Zhang | ef68dc6 | 2021-06-25 16:25:44 -0700 | [diff] [blame] | 47 | 1. Verifying a digital signature signed with ECDSA over the NIST P256 curve. |
Ali Zhang | 6a23acf | 2021-06-21 15:55:48 -0700 | [diff] [blame] | 48 | |
| 49 | .. code-block:: cpp |
| 50 | |
| 51 | #include "pw_crypto/sha256.h" |
| 52 | |
| 53 | std::byte digest[32]; |
Ali Zhang | 2c44c22 | 2021-07-20 09:44:18 -0700 | [diff] [blame] | 54 | auto status = pw::crypto::sha256::Hash(message, digest); |
Ali Zhang | 6a23acf | 2021-06-21 15:55:48 -0700 | [diff] [blame] | 55 | |
| 56 | if (!status.ok()) { |
| 57 | // handle errors. |
| 58 | } |
| 59 | |
Ali Zhang | b7b38c2 | 2021-07-07 11:39:50 -0700 | [diff] [blame] | 60 | bool valid = pw::crypto::ecdsa::VerifyP256Signature(public_key, digest, signature).ok(); |
Ali Zhang | 6a23acf | 2021-06-21 15:55:48 -0700 | [diff] [blame] | 61 | |
Ali Zhang | ef68dc6 | 2021-06-25 16:25:44 -0700 | [diff] [blame] | 62 | 2. Verifying a digital signature signed with ECDSA over the NIST P256 curve, with a long and/or non-contiguous message. |
Ali Zhang | 6a23acf | 2021-06-21 15:55:48 -0700 | [diff] [blame] | 63 | |
| 64 | .. code-block:: cpp |
| 65 | |
| 66 | #include "pw_crypto/sha256.h" |
| 67 | |
| 68 | std::byte digest[32]; |
| 69 | auto h = pw::crypto::sha256::Sha256(); |
| 70 | |
| 71 | while (/* chunk ← Get the next chunk of message */) { |
| 72 | h.Update(chunk); |
| 73 | } |
| 74 | |
| 75 | auto status = h.Final(digest); |
Ali Zhang | cc6101e | 2021-07-08 14:16:52 -0700 | [diff] [blame] | 76 | bool valid = status.ok() && pw::crypto::ecdsa::VerifyP256Signature(public_key, digest, signature).ok(); |
| 77 | |
| 78 | Configuration |
| 79 | ------------- |
| 80 | |
| 81 | The crypto services offered by pw_crypto can be backed by different backend crypto libraries. For now only Mbed TLS is supported, others are under construction. |
| 82 | |
| 83 | Mbed TLS |
Ali Zhang | 442e487 | 2021-07-19 17:10:33 -0700 | [diff] [blame] | 84 | ^^^^^^^^ |
| 85 | |
| 86 | To select the Mbed TLS backend, the MbedTLS library needs to be installed and configured. |
Ali Zhang | cc6101e | 2021-07-08 14:16:52 -0700 | [diff] [blame] | 87 | |
| 88 | .. code-block:: sh |
| 89 | |
| 90 | # Install and configure MbedTLS |
| 91 | pw package install mbedtls |
| 92 | gn gen out --args='dir_pw_third_party_mbedtls="//.environment/packages/mbedtls" pw_crypto_SHA256_BACKEND="//pw_crypto:sha256_mbedtls" pw_crypto_ECDSA_BACKEND="//pw_crypto:ecdsa_mbedtls"' |
| 93 | |
| 94 | ninja -C out |
Ali Zhang | 5f15bfd | 2021-07-09 21:02:30 -0700 | [diff] [blame] | 95 | |
Ali Zhang | 442e487 | 2021-07-19 17:10:33 -0700 | [diff] [blame] | 96 | For optimal code size and/or performance, the Mbed TLS library needs to be configured per product. Mbed TLS configuration is achieved by turning on and off MBEDTLS_* options in a config.h file. See //third_party/mbedtls for how this is done. |
| 97 | |
| 98 | ``pw::crypto::sha256`` does not need any special configuration as it uses the mbedtls_sha256_* APIs directly. However you can optionally turn on ``MBEDTLS_SHA256_SMALLER`` to further reduce the code size to from 3KiB to ~1.8KiB at a ~30% slowdown cost (Cortex-M4). |
| 99 | |
| 100 | .. code-block:: c |
| 101 | |
| 102 | #define MBEDTLS_SHA256_SMALLER |
| 103 | |
| 104 | ``pw::crypto::ecdsa`` requires the following minimum configurations which yields a code size of ~12KiB. |
| 105 | |
| 106 | .. code-block:: c |
| 107 | |
| 108 | #define MBEDTLS_BIGNUM_C |
| 109 | #define MBEDTLS_ECP_C |
| 110 | #define MBEDTLS_ECDSA_C |
| 111 | // The ASN1 options are needed only because mbedtls considers and verifies |
| 112 | // them (in check_config.h) as dependencies of MBEDTLS_ECDSA_C. |
| 113 | #define MBEDTLS_ASN1_WRITE_C |
| 114 | #define MBEDTLS_ASN1_PARSE_C |
| 115 | #define MBEDTLS_ECP_NO_INTERNAL_RNG |
| 116 | #define MBEDTLS_ECP_DP_SECP256R1_ENABLED |
Ali Zhang | 5f15bfd | 2021-07-09 21:02:30 -0700 | [diff] [blame] | 117 | |
Ali Zhang | dd1cbe7 | 2021-07-20 15:48:55 -0700 | [diff] [blame] | 118 | BoringSSL |
| 119 | ^^^^^^^^^ |
| 120 | |
| 121 | To select the BoringSSL backend, the BoringSSL library needs to be installed and configured. |
| 122 | |
| 123 | .. code-block:: sh |
| 124 | |
| 125 | # Install and configure BoringSSL |
| 126 | pw package install boringssl |
| 127 | gn gen out --args='dir_pw_third_party_boringssl="//.environment/packages/boringssl" pw_crypto_SHA256_BACKEND="//pw_crypto:sha256_boringssl"' |
| 128 | |
| 129 | ninja -C out |
| 130 | |
Ali Zhang | 5f15bfd | 2021-07-09 21:02:30 -0700 | [diff] [blame] | 131 | Size Reports |
| 132 | ------------ |
| 133 | |
Ali Zhang | 442e487 | 2021-07-19 17:10:33 -0700 | [diff] [blame] | 134 | Below are size reports for each crypto service. These vary across configurations. |
Ali Zhang | 5f15bfd | 2021-07-09 21:02:30 -0700 | [diff] [blame] | 135 | |
| 136 | .. include:: size_report |