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 | ef68dc6 | 2021-06-25 16:25:44 -0700 | [diff] [blame] | 13 | 1. Digesting a message with `SHA256`_. |
| 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 | |
| 20 | .. attention:: |
| 21 | |
| 22 | The SHA256 crypto service is under construction. |
| 23 | |
Ali Zhang | 6a23acf | 2021-06-21 15:55:48 -0700 | [diff] [blame] | 24 | Usage |
Ali Zhang | ef68dc6 | 2021-06-25 16:25:44 -0700 | [diff] [blame] | 25 | ^^^^^ |
Ali Zhang | 6a23acf | 2021-06-21 15:55:48 -0700 | [diff] [blame] | 26 | |
Ali Zhang | ef68dc6 | 2021-06-25 16:25:44 -0700 | [diff] [blame] | 27 | 1. Obtaining a oneshot digest. |
Ali Zhang | 6a23acf | 2021-06-21 15:55:48 -0700 | [diff] [blame] | 28 | |
| 29 | .. code-block:: cpp |
| 30 | |
| 31 | #include "pw_crypto/sha256.h" |
| 32 | |
| 33 | std::byte digest[32]; |
| 34 | Status status = pw::crypto::sha256::Digest(message, digest); |
| 35 | |
Ali Zhang | ef68dc6 | 2021-06-25 16:25:44 -0700 | [diff] [blame] | 36 | 2. Digesting a long, potentially non-contiguous message. |
Ali Zhang | 6a23acf | 2021-06-21 15:55:48 -0700 | [diff] [blame] | 37 | |
| 38 | .. code-block:: cpp |
| 39 | |
| 40 | #include "pw_crypto/sha256.h" |
| 41 | |
| 42 | std::byte digest[32]; |
| 43 | auto h = pw::crypto::sha256::Sha256(); |
| 44 | |
| 45 | while (/* chunk ← Get next chunk of message */) { |
| 46 | h.Update(chunk); |
| 47 | } |
| 48 | |
| 49 | Status status = h.Final(digest); |
| 50 | |
Ali Zhang | ef68dc6 | 2021-06-25 16:25:44 -0700 | [diff] [blame] | 51 | Configuration |
| 52 | ^^^^^^^^^^^^^ |
| 53 | |
| 54 | The SHA256 crypto service can be backed by a few different crypto libraries as configured below. |
| 55 | |
| 56 | EmbedTLS |
| 57 | |
| 58 | .. code-block:: sh |
| 59 | |
| 60 | # Install and configure MbedTLS |
| 61 | pw package install mbedtls |
| 62 | gn gen out --args='dir_pw_third_party_mbedtls="//.environment/packages/mbedtls" pw_crypto_SHA256_BACKEND="//pw_crypto:sha256_mbedtls"' |
| 63 | |
| 64 | ninja -C out |
| 65 | |
| 66 | ECDSA |
| 67 | ----- |
| 68 | |
| 69 | .. attention:: |
| 70 | |
| 71 | The ECDSA crypto service is under construction. |
| 72 | |
| 73 | Usage |
| 74 | ^^^^^ |
| 75 | |
| 76 | 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] | 77 | |
| 78 | .. code-block:: cpp |
| 79 | |
| 80 | #include "pw_crypto/sha256.h" |
| 81 | |
| 82 | std::byte digest[32]; |
| 83 | auto status = pw::crypto::sha256::Digest(message, digest); |
| 84 | |
| 85 | if (!status.ok()) { |
| 86 | // handle errors. |
| 87 | } |
| 88 | |
Ali Zhang | b7b38c2 | 2021-07-07 11:39:50 -0700 | [diff] [blame^] | 89 | bool valid = pw::crypto::ecdsa::VerifyP256Signature(public_key, digest, signature).ok(); |
Ali Zhang | 6a23acf | 2021-06-21 15:55:48 -0700 | [diff] [blame] | 90 | |
Ali Zhang | ef68dc6 | 2021-06-25 16:25:44 -0700 | [diff] [blame] | 91 | 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] | 92 | |
| 93 | .. code-block:: cpp |
| 94 | |
| 95 | #include "pw_crypto/sha256.h" |
| 96 | |
| 97 | std::byte digest[32]; |
| 98 | auto h = pw::crypto::sha256::Sha256(); |
| 99 | |
| 100 | while (/* chunk ← Get the next chunk of message */) { |
| 101 | h.Update(chunk); |
| 102 | } |
| 103 | |
| 104 | auto status = h.Final(digest); |
Ali Zhang | b7b38c2 | 2021-07-07 11:39:50 -0700 | [diff] [blame^] | 105 | bool valid = status.ok() && pw::crypto::ecdsa::VerifyP256Signature(public_key, digest, signature).ok(); |