1 #ifndef NDNPH_PORT_EC_MBED_HPP
2 #define NDNPH_PORT_EC_MBED_HPP
4 #include "../mbed-common.hpp"
5 #include <mbedtls/ecdsa.h>
7 #if MBEDTLS_VERSION_MAJOR >= 3
8 #define NDNPH_MBEDTLS_PVT3(mem) MBEDTLS_PRIVATE(mem)
10 #define NDNPH_MBEDTLS_PVT3(mem) mem
14 namespace port_ec_mbed {
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;
24 mbedtls_ecdsa_free(&m_ctx);
27 mbedtls_ecdsa_context*
get() {
28 return m_hasError ? nullptr : &m_ctx;
32 mbedtls_ecdsa_context m_ctx;
33 bool m_hasError =
false;
39 mbedtls_ecp_keypair_init(&
keypair);
44 mbedtls_ecp_keypair_free(&
keypair);
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;
64 ssize_t
sign(
const uint8_t* digest, uint8_t* sig)
const {
66 auto ctx = context.
get();
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,
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;
92 bool verify(
const uint8_t* digest,
const uint8_t* sig,
size_t sigLen)
const {
94 auto ctx = context.
get();
99 return mbedtls_ecdsa_read_signature(ctx, digest,
NDNPH_SHA256_LEN, sig, sigLen) == 0;
105 bool generate(uint8_t* pvtBits, uint8_t* pubBits) {
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;
132 #ifdef NDNPH_PORT_EC_MBED
134 using Ec = port_ec_mbed::Ec;
EC curve P256.
Definition: mbed-common.hpp:233
static mbedtls_ecp_group * group()
Definition: mbed-common.hpp:240
EcContext(const mbedtls_ecp_keypair &key)
Definition: mbed.hpp:18
mbedtls_ecdsa_context * get()
Definition: mbed.hpp:27
~EcContext()
Definition: mbed.hpp:23
mbedtls_ecp_keypair keypair
Definition: mbed.hpp:52
~EcKeyBase()
Definition: mbed.hpp:43
EcKeyBase()
Definition: mbed.hpp:38
bool generate(uint8_t *pvtBits, uint8_t *pubBits)
Definition: mbed.hpp:105
bool verify(const uint8_t *digest, const uint8_t *sig, size_t sigLen) const
Definition: mbed.hpp:92
ssize_t sign(const uint8_t *digest, uint8_t *sig) const
Definition: mbed.hpp:64
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