esp8266ndn
NDN Arduino library for ESP8266 and more
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
iv.hpp
Go to the documentation of this file.
1 #ifndef NDNPH_KEYCHAIN_IV_HPP
2 #define NDNPH_KEYCHAIN_IV_HPP
3 
4 #include "../port/random/port.hpp"
5 #include "../tlv/nni.hpp"
6 #include "../tlv/value.hpp"
7 
8 namespace ndnph {
9 
19 public:
21  using IvLen = std::integral_constant<size_t, 12>;
22 
24  using BlockSize = std::integral_constant<size_t, 16>;
25 
27  bool randomize() {
28  m_ok = port::RandomSource::generate(reinterpret_cast<uint8_t*>(&random), sizeof(random));
29  return m_ok;
30  }
31 
33  bool write(uint8_t room[12]) {
35  tlv::NNI4::writeValue(room + 8, counter);
36  return true;
37  }
38 
43  bool advance(size_t size) {
44  uint64_t nBlocks = divCeil(size, BlockSize::value);
45  uint64_t cnt = static_cast<uint64_t>(counter) + nBlocks;
46  if (cnt > std::numeric_limits<uint32_t>::max()) {
47  m_ok = false;
48  }
49  counter = static_cast<uint32_t>(cnt);
50  return m_ok;
51  }
52 
59  bool check(const uint8_t* iv, size_t size) {
60  uint64_t rand = tlv::NNI8::readValue(iv);
61  uint32_t cnt = tlv::NNI4::readValue(iv + sizeof(rand));
62 
63  if (counter == 0) {
64  random = rand;
65  } else if (random != rand) {
66  return false;
67  }
68 
69  if (cnt < counter) {
70  return false;
71  }
72  counter = cnt;
73  return advance(size);
74  }
75 
76 public:
77  uint64_t random = 0;
78  uint32_t counter = 0;
79 
80 private:
81  bool m_ok = true;
82 };
83 
84 } // namespace ndnph
85 
86 #endif // NDNPH_KEYCHAIN_IV_HPP
static bool generate(uint8_t *output, size_t count)
AES-GCM Initialization Vector generator and checker.
Definition: iv.hpp:18
bool check(const uint8_t *iv, size_t size)
Check received IV.
Definition: iv.hpp:59
std::integral_constant< size_t, 12 > IvLen
IV length.
Definition: iv.hpp:21
bool write(uint8_t room[12])
Write IV to room .
Definition: iv.hpp:33
uint64_t random
Definition: iv.hpp:77
std::integral_constant< size_t, 16 > BlockSize
AES-GCM block size.
Definition: iv.hpp:24
bool randomize()
Randomize the random number portion.
Definition: iv.hpp:27
bool advance(size_t size)
Advance the counter portion.
Definition: iv.hpp:43
uint32_t counter
Definition: iv.hpp:78
static void writeValue(uint8_t *room, T n)
static T readValue(const uint8_t *input)
Definition: fs.hpp:33
std::enable_if< std::is_integral< I >::value, I >::type divCeil(const I &a, const I &b)
Compute ceil( a / b ).
Definition: operators.hpp:25