23 return Error::Success;
28 return Error::BufferTooSmall;
30 std::memcpy(content_.data(), data, size);
32 return Error::Success;
36 return setContent(
reinterpret_cast<const uint8_t*
>(str.data()), str.size());
40 freshnessPeriod_ = periodMs;
45 finalBlockId_ = segmentNum;
50 finalBlockId_.reset();
60 signatureType_ = type;
66 hasKeyLocator_ =
true;
72 hasKeyLocator_ =
false;
76Error Data::encodeSignedPortion(uint8_t* buf,
size_t bufSize,
size_t& encodedLen)
const {
81 Error err = name_.
encode(encoder.current(), encoder.remaining(), nameLen);
82 if (err != Error::Success) {
85 encoder.setPosition(encoder.position() + nameLen);
88 if (contentType_ != ContentType::Blob || freshnessPeriod_ || finalBlockId_) {
89 uint8_t metaInfoBuf[48];
90 TlvEncoder metaEncoder(metaInfoBuf,
sizeof(metaInfoBuf));
93 if (contentType_ != ContentType::Blob) {
95 static_cast<uint8_t
>(contentType_));
96 if (err != Error::Success) {
102 if (freshnessPeriod_) {
104 if (err != Error::Success) {
112 uint8_t segNumBuf[9];
113 TlvEncoder segNumEncoder(segNumBuf,
sizeof(segNumBuf));
114 segNumEncoder.writeNonNegativeInteger(*finalBlockId_);
118 TlvEncoder compEncoder(compBuf,
sizeof(compBuf));
122 if (err != Error::Success) {
127 err = encoder.writeTlv(
tlv::MetaInfo, metaInfoBuf, metaEncoder.size());
128 if (err != Error::Success) {
134 if (contentSize_ > 0) {
135 err = encoder.writeTlv(
tlv::Content, content_.data(), contentSize_);
136 if (err != Error::Success) {
143 TlvEncoder sigInfoEncoder(sigInfoBuf,
sizeof(sigInfoBuf));
145 static_cast<uint8_t
>(signatureType_));
148 if (hasKeyLocator_) {
150 TlvEncoder keyLocEncoder(keyLocBuf,
sizeof(keyLocBuf));
151 size_t keyLocNameLen = 0;
152 err = keyLocator_.
encode(keyLocEncoder.current(), keyLocEncoder.remaining(), keyLocNameLen);
153 if (err != Error::Success) {
156 keyLocEncoder.setPosition(keyLocEncoder.position() + keyLocNameLen);
157 sigInfoEncoder.writeTlv(
tlv::KeyLocator, keyLocBuf, keyLocEncoder.size());
161 if (err != Error::Success) {
165 encodedLen = encoder.size();
166 return Error::Success;
170 signatureType_ = SignatureType::DigestSha256;
174 size_t signedLen = 0;
175 Error err = encodeSignedPortion(signedBuf,
sizeof(signedBuf), signedLen);
176 if (err != Error::Success) {
181 err = crypto::sha256(signedBuf, signedLen, signatureValue_.data());
182 if (err != Error::Success) {
187 return Error::Success;
191 if (key ==
nullptr || keyLen == 0) {
192 return Error::InvalidParam;
195 signatureType_ = SignatureType::SignatureHmacWithSha256;
199 size_t signedLen = 0;
200 Error err = encodeSignedPortion(signedBuf,
sizeof(signedBuf), signedLen);
201 if (err != Error::Success) {
206 err = crypto::hmacSha256(key, keyLen, signedBuf, signedLen, signatureValue_.data());
207 if (err != Error::Success) {
212 return Error::Success;
216 if (signatureType_ != SignatureType::DigestSha256) {
225 size_t signedLen = 0;
226 Error err = encodeSignedPortion(signedBuf,
sizeof(signedBuf), signedLen);
227 if (err != Error::Success) {
233 err = crypto::sha256(signedBuf, signedLen, computed);
234 if (err != Error::Success) {
239 return crypto::constantTimeCompare(computed, signatureValue_.data(),
SHA256_DIGEST_SIZE);
243 if (key ==
nullptr || keyLen == 0) {
246 if (signatureType_ != SignatureType::SignatureHmacWithSha256) {
255 size_t signedLen = 0;
256 Error err = encodeSignedPortion(signedBuf,
sizeof(signedBuf), signedLen);
257 if (err != Error::Success) {
263 err = crypto::hmacSha256(key, keyLen, signedBuf, signedLen, computed);
264 if (err != Error::Success) {
269 return crypto::constantTimeCompare(computed, signatureValue_.data(),
HMAC_SHA256_SIZE);
273 if (privKey ==
nullptr) {
274 return Error::InvalidParam;
277 signatureType_ = SignatureType::SignatureSha256WithEcdsa;
281 size_t signedLen = 0;
282 Error err = encodeSignedPortion(signedBuf,
sizeof(signedBuf), signedLen);
283 if (err != Error::Success) {
288 err = crypto::ecdsaP256Sign(privKey, signedBuf, signedLen, signatureValue_.data(),
294 if (pubKey ==
nullptr) {
297 if (signatureType_ != SignatureType::SignatureSha256WithEcdsa) {
306 size_t signedLen = 0;
307 Error err = encodeSignedPortion(signedBuf,
sizeof(signedBuf), signedLen);
308 if (err != Error::Success) {
313 return crypto::ecdsaP256Verify(pubKey, signedBuf, signedLen, signatureValue_.data(),
319 TlvEncoder valueEncoder(valueBuf,
sizeof(valueBuf));
322 size_t signedLen = 0;
324 if (err != Error::Success) {
331 if (err != Error::Success) {
336 const size_t valueLen = valueEncoder.
size();
338 const size_t totalSize = headerSize + valueLen;
340 if (bufSize < totalSize) {
341 return Error::BufferTooSmall;
346 if (err != Error::Success) {
350 if (err != Error::Success) {
354 if (err != Error::Success) {
358 encodedLen = encoder.
size();
359 return Error::Success;
368 if (!headerResult.ok()) {
369 return {.value =
Data{}, .error = headerResult.error};
372 if (headerResult.value.type !=
tlv::Data) {
373 return {.value =
Data{}, .error = Error::InvalidPacket};
376 const size_t dataValueLen = headerResult.value.length;
377 const size_t dataValueEnd = decoder.
position() + dataValueLen;
379 if (decoder.
remaining() < dataValueLen) {
380 return {.value =
Data{}, .error = Error::DecodeFailed};
384 bool hasName =
false;
386 while (decoder.
position() < dataValueEnd) {
387 const size_t elementStart = decoder.
position();
390 if (!elemHeader.ok()) {
391 return {.value =
Data{}, .error = elemHeader.error};
394 const uint32_t elemType = elemHeader.value.type;
395 const size_t elemLen = elemHeader.value.length;
398 return {.value =
Data{}, .error = Error::DecodeFailed};
404 size_t bytesRead = 0;
407 if (!nameResult.ok()) {
408 return {.value =
Data{}, .error = nameResult.error};
410 data.name_ = nameResult.value;
411 decoder.
skip(bytesRead);
418 const size_t metaEnd = decoder.
position() + elemLen;
419 while (decoder.
position() < metaEnd) {
421 if (!metaElemHeader.ok()) {
422 return {.value =
Data{}, .error = metaElemHeader.error};
427 if (!ctResult.ok()) {
428 return {.value =
Data{}, .error = ctResult.error};
430 data.contentType_ =
static_cast<ContentType>(ctResult.value);
433 if (!fpResult.ok()) {
434 return {.value =
Data{}, .error = fpResult.error};
436 data.freshnessPeriod_ =
static_cast<uint32_t
>(fpResult.value);
440 if (!compHeader.ok()) {
441 return {.value =
Data{}, .error = compHeader.error};
445 if (!segNumResult.ok()) {
446 return {.value =
Data{}, .error = segNumResult.error};
448 data.finalBlockId_ = segNumResult.value;
450 decoder.
skip(metaElemHeader.value.length);
458 return {.value =
Data{}, .error = Error::BufferTooSmall};
460 const Error err = decoder.
readBytes(data.content_.data(), elemLen);
461 if (err != Error::Success) {
462 return {.value =
Data{}, .error = err};
464 data.contentSize_ = elemLen;
470 const size_t sigInfoEnd = decoder.
position() + elemLen;
471 while (decoder.
position() < sigInfoEnd) {
473 if (!sigInfoElemHeader.ok()) {
474 return {.value =
Data{}, .error = sigInfoElemHeader.error};
480 if (!stResult.ok()) {
481 return {.value =
Data{}, .error = stResult.error};
483 data.signatureType_ =
static_cast<SignatureType>(stResult.value);
486 const size_t keyLocEnd =
487 decoder.
position() + sigInfoElemHeader.value.length;
488 if (decoder.
position() < keyLocEnd) {
489 size_t bytesRead = 0;
492 if (nameResult.ok()) {
493 data.keyLocator_ = nameResult.value;
494 data.hasKeyLocator_ =
true;
500 decoder.
skip(sigInfoElemHeader.value.length);
508 return {.value =
Data{}, .error = Error::BufferTooSmall};
510 const Error err = decoder.
readBytes(data.signatureValue_.data(), elemLen);
511 if (err != Error::Success) {
512 return {.value =
Data{}, .error = err};
514 data.signatureSize_ = elemLen;
520 decoder.
skip(elemLen);
526 return {.value =
Data{}, .error = Error::InvalidPacket};
529 return {.value = data, .error = Error::Success};
Data & setFreshnessPeriod(uint32_t periodMs)
Set the FreshnessPeriod.
Data & setKeyLocator(const Name &name)
Set the KeyLocator.
Data & clearKeyLocator()
Clear the KeyLocator.
Error signWithEcdsa(const uint8_t *privKey)
Sign with ECDSA P-256.
const Name & name() const
Get the Name (const reference)
Error signWithDigestSha256()
Sign with DigestSha256.
Data & setContentType(ContentType type)
Set the content type.
Data & setFinalBlockId(uint64_t segmentNum)
Set the FinalBlockId.
bool verifyHmac(const uint8_t *key, size_t keyLen) const
Verify an HMAC-SHA256 signature.
Error setContent(const uint8_t *data, size_t size)
Set binary data as content.
bool verifyDigestSha256() const
Verify a DigestSha256 signature.
Error encode(uint8_t *buf, size_t bufSize, size_t &encodedLen) const
Encode the Data packet to TLV wire format.
Data & clearFinalBlockId()
Clear the FinalBlockId.
Data & setSignatureType(SignatureType type)
Set the signature type.
bool verifyEcdsa(const uint8_t *pubKey) const
Verify an ECDSA P-256 signature.
static Result< Data > fromWire(const uint8_t *buf, size_t len)
Decode a Data packet from TLV wire format.
Data()=default
Default constructor.
Data & setName(const Name &name)
Set the Name (supports method chaining)
Error signWithHmac(const uint8_t *key, size_t keyLen)
Sign with HMAC-SHA256.
Error encode(uint8_t *buf, size_t bufSize, size_t &encodedLen) const
Encode the Name to TLV wire format.
static Result< Name > fromWire(const uint8_t *buf, size_t len, size_t *bytesRead=nullptr)
Decode a Name from TLV wire format.
static Result< Name > fromUri(std::string_view uri)
Create a Name from a URI string.
size_t position() const
Get current position.
void setPosition(size_t pos)
Set current position.
const uint8_t * current() const
Pointer to current position.
Error readBytes(uint8_t *out, size_t len)
Read a specified number of bytes.
Result< uint64_t > readNonNegativeInteger(size_t numBytes)
Read a non-negative integer of specified byte count.
Result< TlvHeader > readTlvHeader()
Read a TLV header (Type and Length) at once.
Error skip(size_t len)
Skip a specified number of bytes.
size_t remaining() const
Remaining readable bytes.
size_t position() const
Get current position.
Error writeLength(size_t length)
Write a TLV Length.
void setPosition(size_t pos)
Set current position.
Error writeTlv(uint32_t type, const uint8_t *value, size_t valueLen)
Write a complete TLV structure.
Error writeBytes(const uint8_t *data, size_t len)
Write a byte sequence.
size_t size() const
Current write position (= number of bytes written)
size_t remaining() const
Remaining writable bytes.
Error writeType(uint32_t type)
Write a TLV Type.
uint8_t * current()
Pointer to current position.
constexpr size_t NAME_MAX_LENGTH
Maximum Name length (bytes)
constexpr size_t DATA_MAX_CONTENT_SIZE
Maximum content size of a Data packet (bytes) ESP-NOW v2.0: max 1470 bytes - TLV overhead (approx.
constexpr size_t PACKET_MAX_SIZE
Maximum packet size (ESP-NOW v2.0 compatible)
NDN cryptographic utilities.
constexpr uint32_t KeyLocator
Key locator.
constexpr uint32_t MetaInfo
Meta information.
constexpr uint32_t SignatureInfo
Signature info.
constexpr uint32_t FinalBlockId
Final block ID.
constexpr uint32_t FreshnessPeriod
Freshness period.
constexpr uint32_t Content
Content.
constexpr uint32_t SignatureValue
Signature value.
constexpr uint32_t ContentType
Content type.
constexpr uint32_t Name
Name.
constexpr uint32_t Data
Data packet.
constexpr uint32_t SignatureType
Signature type.
constexpr uint32_t GenericNameComponent
Generic Name component.
constexpr size_t SIGNATURE_MAX_SIZE
Maximum signature size (for ECDSA P-256, embedded)
constexpr size_t ECDSA_P256_SIG_MAX_SIZE
ECDSA P-256 signature max size (DER format)
constexpr size_t HMAC_SHA256_SIZE
HMAC-SHA256 size (bytes)
SignatureType
Signature type.
constexpr size_t SHA256_DIGEST_SIZE
SHA-256 digest size (bytes)
NDN TLV (Type-Length-Value) encoding.
constexpr size_t varNumberSize(uint64_t value)
Calculate the encoded size of a VAR-NUMBER.