1#ifndef NDNPH_APP_NDNCERT_COMMON_HPP
2#define NDNPH_APP_NDNCERT_COMMON_HPP
5#include "../../core/log.hpp"
6#include "../../keychain/ec.hpp"
7#include "../../packet/encrypted-message.hpp"
8#include "../../port/clock/port.hpp"
9#include "../../port/mbed-common.hpp"
10#include <mbedtls/hkdf.h>
12#ifdef NDNPH_NDNCERT_DEBUG
13#define NDNPH_NDNCERT_LOG(...) NDNPH_LOG_LINE("ndncert", ##__VA_ARGS__)
15#define NDNPH_NDNCERT_LOG(...) \
15#define NDNPH_NDNCERT_LOG(...) \ …
28using SaltLen = std::integral_constant<size_t, 32>;
37 const uint8_t* salt,
const uint8_t* requestId) {
41 mbedtls_hkdf(mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), salt, SaltLen::value,
42 ikm.data(), ikm.size(), requestId, RequestIdLen::value, okm.data(),
49 return m_aes.
encrypt<
Encrypted>(region, plaintext, requestId, RequestIdLen::value);
56 EvDecoder::def<TT::InitializationVector>(&encrypted),
57 EvDecoder::def<TT::AuthenticationTag>(&encrypted),
58 EvDecoder::def<TT::EncryptedPayload>(&encrypted));
62 return m_aes.
decrypt(region, encrypted, requestId, RequestIdLen::value);
81namespace packet_struct {
93 if (m_pos >= detail::MaxChallengeParams::value) {
101 if (m_pos >= detail::MaxChallengeParams::value) {
104 auto key = m_target.m_kv[m_pos].first;
111 ParameterKV& m_target;
117 for (
const auto& p : m_kv) {
118 if (p.first == key) {
128 for (
auto& p : m_kv) {
130 p = std::make_pair(key, value);
143 void encodeTo(
Encoder& encoder)
const {
144 for (
auto it = m_kv.rbegin(); it != m_kv.rend(); ++it) {
154 std::array<std::pair<tlv::Value, tlv::Value>, detail::MaxChallengeParams::value> m_kv;
162 uint32_t maxValidityPeriod = 0;
181 uint8_t salt[detail::SaltLen::value];
184 uint8_t requestId[detail::RequestIdLen::value];
187template<
typename ChallengeT>
188struct ChallengeRequest {
190 ChallengeT* challenge =
nullptr;
196struct ChallengeResponse {
204 uint16_t remainingTries = 0;
81namespace packet_struct {
…}
Data packet.
Definition data.hpp:136
Decoded TLV.
Definition decoder.hpp:13
size_t length
Definition decoder.hpp:39
const uint8_t * value
Definition decoder.hpp:40
TLV encoder that accepts items in reverse order.
Definition encoder.hpp:10
bool prependTlv(uint32_t type, OmitEmptyTag omitEmpty, const Arg &... arg)
Prepend TLV, measuring TLV-LENGTH automatically.
Definition encoder.hpp:143
Encrypted message structure.
Definition encrypted-message.hpp:24
static bool decodeValue(const Decoder &input, const E &... defs)
Decode input TLV-VALUE with a sequence of element definitions.
Definition ev-decoder.hpp:126
Name.
Definition name.hpp:14
Region-based memory allocator thats owns memory of NDNph objects.
Definition region.hpp:9
Require SigNonce field in Interest SigInfo.
Definition sig-info.hpp:114
Require SigTime field in Interest SigInfo.
Definition sig-info.hpp:192
AES-GCM secret key.
Definition mbed-common.hpp:270
tlv::Value encrypt(Region ®ion, tlv::Value plaintext, const uint8_t *aad=nullptr, size_t aadLen=0)
Encrypt to encrypted-message.
Definition mbed-common.hpp:309
tlv::Value decrypt(Region ®ion, const Encrypted &encrypted, const uint8_t *aad=nullptr, size_t aadLen=0)
Decrypt from encrypted-message.
Definition mbed-common.hpp:344
bool import(const Key &key)
Import raw AES key.
Definition mbed-common.hpp:292
std::array< uint8_t, keyBits/8 > Key
Definition mbed-common.hpp:273
EC point associated with a curve.
Definition mbed-common.hpp:213
Multi-Precision Integer.
Definition mbed-common.hpp:102
std::array< uint8_t, PvtLen::value > SharedSecret
ECDH shared secret buffer.
Definition mbed-common.hpp:252
static bool ecdh(const mbedtls_mpi *pvt, const mbedtls_ecp_point *pub, SharedSecret &shared)
Compute ECDH shared secret.
Definition mbed-common.hpp:255
Symmetric key used in CHALLENGE step.
Definition common.hpp:33
tlv::Value decrypt(Region ®ion, tlv::Value message, const uint8_t *requestId)
Decrypt from encrypted-message.
Definition common.hpp:53
tlv::Value encrypt(Region ®ion, tlv::Value plaintext, const uint8_t *requestId)
Encrypt to encrypted-message.
Definition common.hpp:48
bool makeKey(const mbedtls::Mpi &ecdhPvt, const mbedtls::P256::Point &ecdhPub, const uint8_t *salt, const uint8_t *requestId)
Derive the key.
Definition common.hpp:36
bool parseValue(const Decoder::Tlv &d)
Definition common.hpp:100
bool parseKey(const Decoder::Tlv &d)
Definition common.hpp:92
Parser(ParameterKV &target)
Definition common.hpp:87
A sequence of bytes, usually TLV-VALUE.
Definition value.hpp:11
Decoder makeDecoder() const
Create a Decoder over this value buffer.
Definition value.hpp:64
#define NDNPH_ASSERT(x)
Definition common.hpp:30
Definition input-iterator-pointer-proxy.hpp:5
Policy< R... > makePolicy(R &&... rule)
Create Interest SigInfo validation policy.
Definition sig-info.hpp:312
@ BEFORE_CHALLENGE
Definition an.hpp:86
@ EncryptedPayload
Definition an.hpp:28
@ ParameterValue
Definition an.hpp:16
@ InitializationVector
Definition an.hpp:27
@ AuthenticationTag
Definition an.hpp:36
@ ParameterKey
Definition an.hpp:15
std::integral_constant< int, 2 > MaxChallengeParams
Definition common.hpp:26
std::integral_constant< size_t, 8 > RequestIdLen
Definition common.hpp:29
std::integral_constant< size_t, 16 > AuthenticationTagLen
Definition common.hpp:30
std::integral_constant< int, 4 > MaxChallenges
Definition common.hpp:25
ISigPolicy makeISigPolicy()
Definition common.hpp:76
std::integral_constant< size_t, 32 > SaltLen
Definition common.hpp:28