blob: bb514f43d5924dc857934623588f3317fa367709 [file] [log] [blame]
Ali Zhang6a23acf2021-06-21 15:55:48 -07001.. _module-pw_crypto:
2
3---------
4pw_crypto
5---------
6A set of safe (read: easy to use, hard to misuse) crypto APIs.
7
8.. attention::
9
10 This module is under construction.
11
12The following services are coming soon:
13
141. Digesting a message with SHA256.
152. Verifying a digital signature signed with ECDSA over the NIST P256 curve.
16
17=====
18Usage
19=====
20
21Here are some examples for a taste of what is coming up.
22
231. 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
332. 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
493. 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
654. 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);