esp8266ndn
NDN Arduino library for ESP8266 and more
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
encrypted-message.hpp
Go to the documentation of this file.
1#ifndef NDNPH_PACKET_ENCRYPTED_MESSAGE_HPP
2#define NDNPH_PACKET_ENCRYPTED_MESSAGE_HPP
3
4#include "../tlv/value.hpp"
5
6namespace ndnph {
7
23template<uint32_t ivType, size_t ivLen, uint32_t tagType, size_t tagLen, uint32_t epType>
25public:
26 using IvLen = std::integral_constant<size_t, ivLen>;
27 using TagLen = std::integral_constant<size_t, tagLen>;
28
29 struct InPlace {
30 uint8_t* iv = nullptr;
31 uint8_t* tag = nullptr;
32 uint8_t* ciphertext = nullptr;
33 };
34
35 static InPlace prependInPlace(Encoder& encoder, size_t ciphertextLen) {
36 InPlace result;
37 result.ciphertext = encoder.prependRoom(ciphertextLen);
38 encoder.prependTypeLength(epType, ciphertextLen);
39 if (tagLen > 0) {
40 result.tag = encoder.prependRoom(tagLen);
41 encoder.prependTypeLength(tagType, tagLen);
42 }
43 if (ivLen > 0) {
44 result.iv = encoder.prependRoom(ivLen);
45 encoder.prependTypeLength(ivType, ivLen);
46 }
47 return result;
48 }
49
50 void encodeTo(Encoder& encoder) const {
51 auto place = prependInPlace(encoder, ciphertext.size());
52 if (!encoder) {
53 return;
54 }
55
56 if (ivLen > 0) {
57 std::copy(iv.begin(), iv.end(), place.iv);
58 }
59 if (tagLen > 0) {
60 std::copy(tag.begin(), tag.end(), place.tag);
61 }
62 std::copy(ciphertext.begin(), ciphertext.end(), place.ciphertext);
63 }
64
65 bool decodeFrom(const Decoder::Tlv& d) {
66 switch (d.type) {
67 case ivType: {
68 if (d.length == ivLen) {
69 std::copy_n(d.value, ivLen, iv.begin());
70 return true;
71 }
72 return false;
73 }
74 case tagType: {
75 if (d.length == tagLen) {
76 std::copy_n(d.value, tagLen, tag.begin());
77 return true;
78 }
79 return false;
80 }
81 case epType: {
83 return true;
84 }
85 }
86 return false;
87 }
88
89public:
90 std::array<uint8_t, ivLen> iv;
91 std::array<uint8_t, tagLen> tag;
93};
94
95} // namespace ndnph
96
97#endif // NDNPH_PACKET_ENCRYPTED_MESSAGE_HPP
Decoded TLV.
Definition decoder.hpp:13
uint32_t type
Definition decoder.hpp:38
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 prependTypeLength(uint32_t type, size_t length)
Prepend TLV-TYPE and TLV-LENGTH.
Definition encoder.hpp:103
uint8_t * prependRoom(size_t size)
Make room to prepend an object.
Definition encoder.hpp:90
Encrypted message structure.
Definition encrypted-message.hpp:24
tlv::Value ciphertext
Definition encrypted-message.hpp:92
std::integral_constant< size_t, ivLen > IvLen
Definition encrypted-message.hpp:26
std::array< uint8_t, tagLen > tag
Definition encrypted-message.hpp:91
void encodeTo(Encoder &encoder) const
Definition encrypted-message.hpp:50
bool decodeFrom(const Decoder::Tlv &d)
Definition encrypted-message.hpp:65
static InPlace prependInPlace(Encoder &encoder, size_t ciphertextLen)
Definition encrypted-message.hpp:35
std::integral_constant< size_t, tagLen > TagLen
Definition encrypted-message.hpp:27
std::array< uint8_t, ivLen > iv
Definition encrypted-message.hpp:90
A sequence of bytes, usually TLV-VALUE.
Definition value.hpp:11
const uint8_t * end() const
Definition value.hpp:42
const uint8_t * begin() const
Definition value.hpp:38
size_t size() const
Definition value.hpp:46
Definition fs.hpp:33
Definition encrypted-message.hpp:29
uint8_t * ciphertext
Definition encrypted-message.hpp:32
uint8_t * iv
Definition encrypted-message.hpp:30
uint8_t * tag
Definition encrypted-message.hpp:31