Ali Zhang | 6a23acf | 2021-06-21 15:55:48 -0700 | [diff] [blame] | 1 | .. _module-pw_crypto: |
| 2 | |
| 3 | --------- |
| 4 | pw_crypto |
| 5 | --------- |
| 6 | A set of safe (read: easy to use, hard to misuse) crypto APIs. |
| 7 | |
| 8 | .. attention:: |
| 9 | |
| 10 | This module is under construction. |
| 11 | |
| 12 | The following services are coming soon: |
| 13 | |
| 14 | 1. Digesting a message with SHA256. |
| 15 | 2. Verifying a digital signature signed with ECDSA over the NIST P256 curve. |
| 16 | |
| 17 | ===== |
| 18 | Usage |
| 19 | ===== |
| 20 | |
| 21 | Here are some examples for a taste of what is coming up. |
| 22 | |
| 23 | 1. SHA256: Obtaining a oneshot digest. |
| 24 | -------------------------------------- |
| 25 | |
| 26 | .. code-block:: cpp |
| 27 | |
| 28 | #include "pw_crypto/sha256.h" |
| 29 | |
| 30 | std::byte digest[32]; |
| 31 | Status status = pw::crypto::sha256::Digest(message, digest); |
| 32 | |
| 33 | 2. SHA256: Digesting a long, potentially non-contiguous message. |
| 34 | ---------------------------------------------------------------- |
| 35 | |
| 36 | .. code-block:: cpp |
| 37 | |
| 38 | #include "pw_crypto/sha256.h" |
| 39 | |
| 40 | std::byte digest[32]; |
| 41 | auto h = pw::crypto::sha256::Sha256(); |
| 42 | |
| 43 | while (/* chunk ← Get next chunk of message */) { |
| 44 | h.Update(chunk); |
| 45 | } |
| 46 | |
| 47 | Status status = h.Final(digest); |
| 48 | |
| 49 | 3. ECDSA P256: Verifying a digital signature. |
| 50 | --------------------------------------------- |
| 51 | |
| 52 | .. code-block:: cpp |
| 53 | |
| 54 | #include "pw_crypto/sha256.h" |
| 55 | |
| 56 | std::byte digest[32]; |
| 57 | auto status = pw::crypto::sha256::Digest(message, digest); |
| 58 | |
| 59 | if (!status.ok()) { |
| 60 | // handle errors. |
| 61 | } |
| 62 | |
| 63 | bool valid = pw::crypto::ecdsa::VerifyP256Signature(public_key, digest, signature); |
| 64 | |
| 65 | 4. ECDSA: Verifying a digital signature signed with ECDSA over the NIST P256 curve, with a long and/or non-contiguous message. |
| 66 | ------------------------------------------------------------------------------------------------------------------------------ |
| 67 | |
| 68 | .. code-block:: cpp |
| 69 | |
| 70 | #include "pw_crypto/sha256.h" |
| 71 | |
| 72 | std::byte digest[32]; |
| 73 | auto h = pw::crypto::sha256::Sha256(); |
| 74 | |
| 75 | while (/* chunk ← Get the next chunk of message */) { |
| 76 | h.Update(chunk); |
| 77 | } |
| 78 | |
| 79 | auto status = h.Final(digest); |
| 80 | bool valid = status.ok() && pw::crypto::ecdsa::VerifyP256Signature(public_key, digest, signature); |