blob: d189c166b19a4c46b1403df8327a60eee8db4373 [file] [log] [blame]
Ali Zhang6a23acf2021-06-21 15:55:48 -07001.. _module-pw_crypto:
2
Ali Zhang6a23acf2021-06-21 15:55:48 -07003pw_crypto
Ali Zhangef68dc62021-06-25 16:25:44 -07004=========
Ali Zhang6a23acf2021-06-21 15:55:48 -07005A set of safe (read: easy to use, hard to misuse) crypto APIs.
6
7.. attention::
8
9 This module is under construction.
10
Ali Zhangef68dc62021-06-25 16:25:44 -070011The following crypto services are provided by this module.
Ali Zhang6a23acf2021-06-21 15:55:48 -070012
Ali Zhang2c44c222021-07-20 09:44:18 -0700131. Hashing a message with `SHA256`_.
Ali Zhangef68dc62021-06-25 16:25:44 -0700142. Verifying a digital signature signed with `ECDSA`_ over the NIST P256 curve.
153. Many more to come ...
Ali Zhang6a23acf2021-06-21 15:55:48 -070016
Ali Zhangef68dc62021-06-25 16:25:44 -070017SHA256
18------
19
Ali Zhangef68dc62021-06-25 16:25:44 -0700201. Obtaining a oneshot digest.
Ali Zhang6a23acf2021-06-21 15:55:48 -070021
22.. code-block:: cpp
23
24 #include "pw_crypto/sha256.h"
25
26 std::byte digest[32];
Ali Zhang2c44c222021-07-20 09:44:18 -070027 Status status = pw::crypto::sha256::Hash(message, digest);
Ali Zhang6a23acf2021-06-21 15:55:48 -070028
Ali Zhang2c44c222021-07-20 09:44:18 -0700292. Hashing a long, potentially non-contiguous message.
Ali Zhang6a23acf2021-06-21 15:55:48 -070030
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 Zhangef68dc62021-06-25 16:25:44 -070044ECDSA
45-----
46
Ali Zhangef68dc62021-06-25 16:25:44 -0700471. Verifying a digital signature signed with ECDSA over the NIST P256 curve.
Ali Zhang6a23acf2021-06-21 15:55:48 -070048
49.. code-block:: cpp
50
51 #include "pw_crypto/sha256.h"
52
53 std::byte digest[32];
Ali Zhang2c44c222021-07-20 09:44:18 -070054 auto status = pw::crypto::sha256::Hash(message, digest);
Ali Zhang6a23acf2021-06-21 15:55:48 -070055
56 if (!status.ok()) {
57 // handle errors.
58 }
59
Ali Zhangb7b38c22021-07-07 11:39:50 -070060 bool valid = pw::crypto::ecdsa::VerifyP256Signature(public_key, digest, signature).ok();
Ali Zhang6a23acf2021-06-21 15:55:48 -070061
Ali Zhangef68dc62021-06-25 16:25:44 -0700622. Verifying a digital signature signed with ECDSA over the NIST P256 curve, with a long and/or non-contiguous message.
Ali Zhang6a23acf2021-06-21 15:55:48 -070063
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 Zhangcc6101e2021-07-08 14:16:52 -070076 bool valid = status.ok() && pw::crypto::ecdsa::VerifyP256Signature(public_key, digest, signature).ok();
77
78Configuration
79-------------
80
81The 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
83Mbed TLS
Ali Zhang442e4872021-07-19 17:10:33 -070084^^^^^^^^
85
86To select the Mbed TLS backend, the MbedTLS library needs to be installed and configured.
Ali Zhangcc6101e2021-07-08 14:16:52 -070087
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 Zhang5f15bfd2021-07-09 21:02:30 -070095
Ali Zhang442e4872021-07-19 17:10:33 -070096For 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 Zhang5f15bfd2021-07-09 21:02:30 -0700117
Ali Zhangdd1cbe72021-07-20 15:48:55 -0700118BoringSSL
119^^^^^^^^^
120
121To 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 Zhang5f15bfd2021-07-09 21:02:30 -0700131Size Reports
132------------
133
Ali Zhang442e4872021-07-19 17:10:33 -0700134Below are size reports for each crypto service. These vary across configurations.
Ali Zhang5f15bfd2021-07-09 21:02:30 -0700135
136.. include:: size_report