Botan 3.0.0-alpha0
Crypto and TLS for C&
Public Member Functions | List of all members
Botan::Roughtime::Chain Class Referencefinal

#include <roughtime.h>

Public Member Functions

void append (const Link &new_link, size_t max_chain_size)
 
 Chain ()=default
 
 Chain (const std::string &str)
 
const std::vector< Link > & links () const
 
Nonce next_nonce (const Nonce &blind) const
 
std::vector< Responseresponses () const
 
std::string to_string () const
 

Detailed Description

Definition at line 109 of file roughtime.h.

Constructor & Destructor Documentation

◆ Chain() [1/2]

Botan::Roughtime::Chain::Chain ( )
default

◆ Chain() [2/2]

Botan::Roughtime::Chain::Chain ( const std::string &  str)

Definition at line 252 of file roughtime.cpp.

253 {
254 std::stringstream ss(str);
255 const std::string ERROR_MESSAGE = "Line does not have 4 space separated fields";
256 for(std::string s; std::getline(ss, s);)
257 {
258 size_t start = 0, end = 0;
259 end = s.find(' ', start);
260 if(end == std::string::npos)
261 {
262 throw Decoding_Error(ERROR_MESSAGE);
263 }
264 const auto publicKeyType = s.substr(start, end-start);
265 if(publicKeyType != "ed25519")
266 { throw Not_Implemented("Only ed25519 publicKeyType is implemented"); }
267
268 start = end + 1;
269 end = s.find(' ', start);
270 if(end == std::string::npos)
271 {
272 throw Decoding_Error(ERROR_MESSAGE);
273 }
274 const auto serverPublicKey = Botan::Ed25519_PublicKey(Botan::base64_decode(s.substr(start, end-start)));
275
276 start = end + 1;
277 end = s.find(' ', start);
278 if(end == std::string::npos)
279 {
280 throw Decoding_Error(ERROR_MESSAGE);
281 }
282 if((end - start) != 88)
283 {
284 throw Decoding_Error("Nonce has invalid length");
285 }
286 const auto vec = Botan::base64_decode(s.substr(start, end-start));
287 const auto nonceOrBlind = Nonce(vector_to_array<64>(Botan::base64_decode(s.substr(start, end-start))));
288
289 start = end + 1;
290 end = s.find(' ', start);
291 if(end != std::string::npos)
292 {
293 throw Decoding_Error(ERROR_MESSAGE);
294 }
295 const auto response = Botan::unlock(Botan::base64_decode(s.substr(start)));
296
297 m_links.push_back({response, serverPublicKey, nonceOrBlind});
298 }
299 }
size_t base64_decode(uint8_t out[], const char in[], size_t input_length, size_t &input_consumed, bool final_inputs, bool ignore_ws)
Definition: base64.cpp:200
std::vector< T > unlock(const secure_vector< T > &in)
Definition: secmem.h:72

References Botan::base64_decode(), and Botan::unlock().

Member Function Documentation

◆ append()

void Botan::Roughtime::Chain::append ( const Link new_link,
size_t  max_chain_size 
)

Definition at line 320 of file roughtime.cpp.

321 {
322 if(max_chain_size <= 0)
323 { throw Invalid_Argument("Max chain size must be positive"); }
324
325 while(m_links.size() >= max_chain_size)
326 {
327 if(m_links.size() == 1)
328 {
329 auto new_link_updated = new_link;
330 new_link_updated.nonce_or_blind() =
331 nonce_from_blind(m_links[0].response(), new_link.nonce_or_blind()); //we need to convert blind to nonce
332 m_links.clear();
333 m_links.push_back(new_link_updated);
334 return;
335 }
336 if(m_links.size() >= 2)
337 {
338 m_links[1].nonce_or_blind() =
339 nonce_from_blind(m_links[0].response(), m_links[1].nonce_or_blind()); //we need to convert blind to nonce
340 }
341 m_links.erase(m_links.begin());
342 }
343 m_links.push_back(new_link);
344 }
Nonce nonce_from_blind(const std::vector< uint8_t > &previous_response, const Nonce &blind)
Definition: roughtime.cpp:238

References Botan::Roughtime::nonce_from_blind(), and Botan::Roughtime::Link::nonce_or_blind().

◆ links()

const std::vector< Link > & Botan::Roughtime::Chain::links ( ) const
inline

Definition at line 114 of file roughtime.h.

114{ return m_links; }

◆ next_nonce()

Nonce Botan::Roughtime::Chain::next_nonce ( const Nonce blind) const

Definition at line 314 of file roughtime.cpp.

315 {
316 return m_links.empty()
317 ? blind
318 : nonce_from_blind(m_links.back().response(), blind);
319 }

References Botan::Roughtime::nonce_from_blind().

◆ responses()

std::vector< Response > Botan::Roughtime::Chain::responses ( ) const

Definition at line 300 of file roughtime.cpp.

301 {
302 std::vector<Response> responses;
303 for(unsigned i = 0; i < m_links.size(); ++i)
304 {
305 const auto& l = m_links[i];
306 const auto nonce = i ? nonce_from_blind(m_links[i-1].response(), l.nonce_or_blind()) : l.nonce_or_blind();
307 const auto response = Response::from_bits(l.response(), nonce);
308 if(!response.validate(l.public_key()))
309 { throw Roughtime_Error("Invalid signature or public key"); }
310 responses.push_back(response);
311 }
312 return responses;
313 }
std::vector< Response > responses() const
Definition: roughtime.cpp:300
static Response from_bits(const std::vector< uint8_t > &response, const Nonce &nonce)
Definition: roughtime.cpp:178

References Botan::Roughtime::Response::from_bits(), Botan::Roughtime::nonce_from_blind(), and responses().

Referenced by responses().

◆ to_string()

std::string Botan::Roughtime::Chain::to_string ( ) const

Definition at line 346 of file roughtime.cpp.

347 {
348 std::string s;
349 s.reserve((7+1 + 88+1 + 44+1 + 480)*m_links.size());
350 for(const auto& link : m_links)
351 {
352 s += "ed25519";
353 s += ' ';
354 s += Botan::base64_encode(link.public_key().get_public_key());
355 s += ' ';
356 s += Botan::base64_encode(link.nonce_or_blind().get_nonce().data(), link.nonce_or_blind().get_nonce().size());
357 s += ' ';
358 s += Botan::base64_encode(link.response());
359 s += '\n';
360 }
361 return s;
362 }
size_t base64_encode(char out[], const uint8_t in[], size_t input_length, size_t &input_consumed, bool final_inputs)
Definition: base64.cpp:185

References Botan::base64_encode().


The documentation for this class was generated from the following files: