25 #ifndef CRYPTO3_KDF_SP800_108_HPP
26 #define CRYPTO3_KDF_SP800_108_HPP
41 template<
typename MessageAuthenticationCode>
50 template<
typename MessageAuthenticationCode>
54 constexpr
static const std::size_t
secret_bits = mac_type::key_bits;
61 const std::size_t prf_len = m_prf->output_length();
62 const uint8_t delim = 0;
63 const uint32_t length =
static_cast<uint32_t
>(key_len * 8);
67 uint8_t be_len[4] = {0};
68 std::vector<uint8_t> tmp;
70 store_be(length, be_len);
71 m_prf->set_key(secret, secret_len);
73 while (p < key + key_len &&
counter != 0) {
74 const std::size_t to_copy = std::min<std::size_t>(key + key_len - p, prf_len);
75 uint8_t be_cnt[4] = {0};
79 m_prf->update(be_cnt, 4);
80 m_prf->update(label, label_len);
82 m_prf->update(salt, salt_len);
83 m_prf->update(be_len, 4);
91 throw std::invalid_argument(
"Can't process more than 4GB");
107 template<
typename MessageAuthenticationCode>
111 constexpr
static const std::size_t
secret_bits = mac_type::key_bits;
118 const uint32_t length =
static_cast<uint32_t
>(key_len * 8);
119 const std::size_t prf_len = m_prf->output_length();
120 const std::size_t iv_len = (salt_len >= prf_len ? prf_len : 0);
121 const uint8_t delim = 0;
125 uint8_t be_len[4] = {0};
126 std::vector<uint8_t> prev(salt, salt + iv_len);
127 std::vector<uint8_t> ctx(salt + iv_len, salt + salt_len);
129 store_be(length, be_len);
130 m_prf->set_key(secret, secret_len);
132 while (p < key + key_len &&
counter != 0) {
133 const std::size_t to_copy = std::min<std::size_t>(key + key_len - p, prf_len);
134 uint8_t be_cnt[4] = {0};
139 m_prf->update(be_cnt, 4);
140 m_prf->update(label, label_len);
141 m_prf->update(delim);
143 m_prf->update(be_len, 4);
152 throw std::invalid_argument(
"Can't process more than 4GB");
168 template<
typename MessageAuthenticationCode>
172 constexpr
static const std::size_t
secret_bits = mac_type::key_bits;
179 const uint32_t length =
static_cast<uint32_t
>(key_len * 8);
180 const std::size_t prf_len = m_prf->output_length();
181 const uint8_t delim = 0;
185 uint8_t be_len[4] = {0};
186 std::vector<uint8_t> ai, ki;
188 store_be(length, be_len);
189 m_prf->set_key(secret, secret_len);
192 std::copy(label, label + label_len, std::back_inserter(ai));
193 ai.emplace_back(delim);
194 std::copy(salt, salt + salt_len, std::back_inserter(ai));
195 std::copy(be_len, be_len + 4, std::back_inserter(ai));
197 while (p < key + key_len &&
counter != 0) {
203 const std::size_t to_copy = std::min<std::size_t>(key + key_len - p, prf_len);
204 uint8_t be_cnt[4] = {0};
209 m_prf->update(be_cnt, 4);
210 m_prf->update(label, label_len);
211 m_prf->update(delim);
212 m_prf->update(salt, salt_len);
213 m_prf->update(be_len, 4);
222 throw std::invalid_argument(
"Can't process more than 4GB");
240 template<
typename MessageAuthenticationCode,
template<
typename>
class Mode>
248 constexpr
static const std::size_t
secret_bits = mode_type::secret_bits;
NIST SP 800-108 KDF.
Definition: sp800_108.hpp:241
sp800_108(const secret_type &key)
Definition: sp800_108.hpp:251
policy_type::mode_type mode_type
Definition: sp800_108.hpp:245
mode_type::secret_type secret_type
Definition: sp800_108.hpp:249
mode_type mode
Definition: sp800_108.hpp:259
constexpr static const std::size_t secret_bits
Definition: sp800_108.hpp:248
void process()
Definition: sp800_108.hpp:254
policy_type::mac_type mac_type
Definition: sp800_108.hpp:246
void copy_mem(T *out, const T *in, size_t n)
Definition: memory_operations.hpp:186
Definition: sp800_108_functions.hpp:35
policy_type::mac_type mac_type
Definition: sp800_108_functions.hpp:38
policy_type::mode_type mode_type
Definition: sp800_108_functions.hpp:37
NIST SP 800-108 KDF Counter Mode (5.1)
Definition: sp800_108.hpp:51
counter(const secret_type &key)
Definition: sp800_108.hpp:57
mac_type mac
Definition: sp800_108.hpp:99
mac_type::key_type secret_type
Definition: sp800_108.hpp:55
sp800_108_mode< MessageAuthenticationCode >::mac_type mac_type
Definition: sp800_108.hpp:52
void process()
Definition: sp800_108.hpp:60
constexpr static const std::size_t secret_bits
Definition: sp800_108.hpp:54
NIST SP 800-108 KDF Feedback Mode (5.2)
Definition: sp800_108.hpp:108
void process()
Definition: sp800_108.hpp:117
sp800_108_mode< MessageAuthenticationCode >::mac_type mac_type
Definition: sp800_108.hpp:109
mac_type::key_type secret_type
Definition: sp800_108.hpp:112
feedback(const secret_type &key)
Definition: sp800_108.hpp:114
constexpr static const std::size_t secret_bits
Definition: sp800_108.hpp:111
mac_type mac
Definition: sp800_108.hpp:160
NIST SP 800-108 KDF Double Pipeline Mode (5.3)
Definition: sp800_108.hpp:169
mac_type mac
Definition: sp800_108.hpp:230
void process()
Definition: sp800_108.hpp:178
sp800_108_mode< MessageAuthenticationCode >::mac_type mac_type
Definition: sp800_108.hpp:170
constexpr static const std::size_t secret_bits
Definition: sp800_108.hpp:172
mac_type::key_type secret_type
Definition: sp800_108.hpp:173
pipeline(const secret_type &key)
Definition: sp800_108.hpp:175
sp800_108 key derivation function policy base class
Definition: sp800_108.hpp:42
MessageAuthenticationCode mac_type
Definition: sp800_108.hpp:43