esp8266ndn
NDN Arduino library for ESP8266 and more
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
mbed.hpp
Go to the documentation of this file.
1#ifndef NDNPH_PORT_EC_MBED_HPP
2#define NDNPH_PORT_EC_MBED_HPP
3
4#include "../mbed-common.hpp"
5#include <mbedtls/ecdsa.h>
6
7#if MBEDTLS_VERSION_MAJOR >= 3
8#define NDNPH_MBEDTLS_PVT3(mem) MBEDTLS_PRIVATE(mem)
9#else
10#define NDNPH_MBEDTLS_PVT3(mem) mem
11#endif
12
13namespace ndnph {
14namespace port_ec_mbed {
15
16class EcContext {
17public:
18 explicit EcContext(const mbedtls_ecp_keypair& key) {
19 mbedtls_ecdsa_init(&m_ctx);
20 m_hasError = mbedtls_ecdsa_from_keypair(&m_ctx, &key) != 0;
21 }
22
24 mbedtls_ecdsa_free(&m_ctx);
25 }
26
27 mbedtls_ecdsa_context* get() {
28 return m_hasError ? nullptr : &m_ctx;
29 }
30
31private:
32 mbedtls_ecdsa_context m_ctx;
33 bool m_hasError = false;
34};
35
36class EcKeyBase {
37protected:
39 mbedtls_ecp_keypair_init(&keypair);
40 mbedtls_ecp_group_copy(&keypair.NDNPH_MBEDTLS_PVT3(grp), mbedtls::P256::group());
41 }
42
44 mbedtls_ecp_keypair_free(&keypair);
45 }
46
47private:
48 EcKeyBase(EcKeyBase&) = delete;
49 EcKeyBase& operator=(EcKeyBase&) = delete;
50
51protected:
52 mbedtls_ecp_keypair keypair;
53};
54
55class EcPvt : public EcKeyBase {
56public:
57 bool import(const uint8_t* bits) {
58 return mbedtls_mpi_read_binary(&this->keypair.NDNPH_MBEDTLS_PVT3(d), bits,
59 mbedtls::P256::PvtLen::value) == 0 &&
60 mbedtls_ecp_check_privkey(&this->keypair.NDNPH_MBEDTLS_PVT3(grp),
61 &this->keypair.NDNPH_MBEDTLS_PVT3(d)) == 0;
62 }
63
64 ssize_t sign(const uint8_t* digest, uint8_t* sig) const {
65 EcContext context(this->keypair);
66 auto ctx = context.get();
67 if (ctx == nullptr) {
68 return -1;
69 }
70
71 size_t sigLen;
72 return mbedtls_ecdsa_write_signature(ctx, MBEDTLS_MD_SHA256, digest, NDNPH_SHA256_LEN, sig,
73#if MBEDTLS_VERSION_MAJOR >= 3
74 mbedtls::P256::MaxSigLen::value,
75#endif
76 &sigLen, mbedtls::rng, nullptr) == 0
77 ? sigLen
78 : -1;
79 }
80};
81
82class EcPub : public EcKeyBase {
83public:
84 bool import(const uint8_t* bits) {
85 return mbedtls_ecp_point_read_binary(&this->keypair.NDNPH_MBEDTLS_PVT3(grp),
86 &this->keypair.NDNPH_MBEDTLS_PVT3(Q), bits,
87 mbedtls::P256::PubLen::value) == 0 &&
88 mbedtls_ecp_check_pubkey(&this->keypair.NDNPH_MBEDTLS_PVT3(grp),
89 &this->keypair.NDNPH_MBEDTLS_PVT3(Q)) == 0;
90 }
91
92 bool verify(const uint8_t* digest, const uint8_t* sig, size_t sigLen) const {
93 EcContext context(this->keypair);
94 auto ctx = context.get();
95 if (ctx == nullptr) {
96 return -1;
97 }
98
99 return mbedtls_ecdsa_read_signature(ctx, digest, NDNPH_SHA256_LEN, sig, sigLen) == 0;
100 }
101};
102
103class EcKeyGen : public EcKeyBase {
104public:
105 bool generate(uint8_t* pvtBits, uint8_t* pubBits) {
106 size_t pubLen;
107 return mbedtls_ecp_gen_keypair(
108 &this->keypair.NDNPH_MBEDTLS_PVT3(grp), &this->keypair.NDNPH_MBEDTLS_PVT3(d),
109 &this->keypair.NDNPH_MBEDTLS_PVT3(Q), mbedtls::rng, nullptr) == 0 &&
110 mbedtls_mpi_write_binary(&this->keypair.NDNPH_MBEDTLS_PVT3(d), pvtBits,
111 mbedtls::P256::PvtLen::value) == 0 &&
112 mbedtls_ecp_point_write_binary(
113 &this->keypair.NDNPH_MBEDTLS_PVT3(grp), &this->keypair.NDNPH_MBEDTLS_PVT3(Q),
114 MBEDTLS_ECP_PF_UNCOMPRESSED, &pubLen, pubBits, mbedtls::P256::PubLen::value) == 0 &&
115 pubLen == mbedtls::P256::PubLen::value;
116 }
117};
118
119class Ec {
120public:
124
125 static bool generateKey(uint8_t* pvt, uint8_t* pub) {
126 return EcKeyGen().generate(pvt, pub);
127 }
128};
129
130} // namespace port_ec_mbed
131
132#ifdef NDNPH_PORT_EC_MBED
133namespace port {
135} // namespace port
136#endif
137
138} // namespace ndnph
139
140#endif // NDNPH_PORT_EC_MBED_HPP
EC curve P256.
Definition mbed-common.hpp:233
static mbedtls_ecp_group * group()
Definition mbed-common.hpp:240
Definition mbed.hpp:16
EcContext(const mbedtls_ecp_keypair &key)
Definition mbed.hpp:18
mbedtls_ecdsa_context * get()
Definition mbed.hpp:27
~EcContext()
Definition mbed.hpp:23
Definition mbed.hpp:36
mbedtls_ecp_keypair keypair
Definition mbed.hpp:52
~EcKeyBase()
Definition mbed.hpp:43
EcKeyBase()
Definition mbed.hpp:38
Definition mbed.hpp:103
bool generate(uint8_t *pvtBits, uint8_t *pubBits)
Definition mbed.hpp:105
Definition mbed.hpp:82
bool verify(const uint8_t *digest, const uint8_t *sig, size_t sigLen) const
Definition mbed.hpp:92
Definition mbed.hpp:55
ssize_t sign(const uint8_t *digest, uint8_t *sig) const
Definition mbed.hpp:64
Definition mbed.hpp:119
static bool generateKey(uint8_t *pvt, uint8_t *pub)
Definition mbed.hpp:125
#define NDNPH_SHA256_LEN
SHA256 digest length.
Definition common.hpp:34
int rng(void *, uint8_t *output, size_t count)
Random number generator for various Mbed TLS library functions.
Definition mbed-common.hpp:30
Definition fs.hpp:33