Commit ec182544 authored by johan's avatar johan
Browse files

Use C++ API for bctoolbox RNG

improved RNG internal error management
parent 3bb879d1
......@@ -19,6 +19,7 @@
#include "lime_crypto_primitives.hpp"
#include "bctoolbox/crypto.h"
#include "bctoolbox/crypto.hh"
#include "bctoolbox/exception.hh"
namespace lime {
......@@ -52,42 +53,22 @@ namespace lime {
*/
class bctbx_RNG : public RNG {
private :
bctbx_rng_context_t *m_context; // the bctoolbox RNG context
/* only bctbx_EDDSA and bctbx_ECDH needs a direct access to the actual RNG context */
template <typename Curve> friend class bctbx_EDDSA;
template <typename Curve> friend class bctbx_ECDH;
/**
* @brief access internal RNG context
* Used internally by the bctoolbox wrapper, is not exposed to the lime_crypto_primitive API.
*
* @return a pointer to the RNG context
*/
bctbx_rng_context_t *get_context(void) {
return m_context;
}
bctoolbox::RNG m_context; // the bctoolbox RNG context
public:
void randomize(sBuffer<lime::settings::DRrandomSeedSize> &buffer) override {
bctbx_rng_get(m_context, buffer.data(), buffer.size());
m_context.randomize(buffer.data(), buffer.size());
};
uint32_t randomize() override {
std::array<uint8_t, 4> buffer;
bctbx_rng_get(m_context, buffer.data(), buffer.size());
uint32_t ret = m_context.randomize();
// we are on 31 bits: keep the uint32_t MSb set to 0 (see RNG interface definition)
return (static_cast<uint32_t>(buffer[0]&0x7F)<<24 | static_cast<uint32_t>(buffer[1])<<16 | static_cast<uint32_t>(buffer[2])<<8 | static_cast<uint32_t>(buffer[3]));
return (ret & 0x7FFFFFFF);
};
bctbx_RNG() {
m_context = bctbx_rng_context_new();
}
~bctbx_RNG() {
bctbx_rng_context_free(m_context);
m_context = nullptr;
void randomize(uint8_t *buffer, const size_t size) override {
m_context.randomize(buffer, size);
}
}; // class bctbx_RNG
......@@ -162,8 +143,13 @@ class bctbx_EDDSA : public Signature<Curve> {
}
void createKeyPair(std::shared_ptr<lime::RNG> rng) override {
// the dynamic cast will generate an exception if RNG is not actually a bctbx_RNG
bctbx_EDDSACreateKeyPair(m_context, (int (*)(void *, uint8_t *, size_t))bctbx_rng_get, dynamic_cast<lime::bctbx_RNG&>(*rng).get_context());
// Generate a random secret key
DSA<Curve, lime::DSAtype::privateKey> secret;
rng->randomize(secret.data(), secret.size());
// set it in the context
set_secret(secret);
// and generate the public value
derivePublic();
}
void derivePublic(void) override {
......@@ -329,8 +315,13 @@ class bctbx_ECDH : public keyExchange<Curve> {
}
void createKeyPair(std::shared_ptr<lime::RNG> rng) override {
// the dynamic cast will generate an exception if RNG is not actually a bctbx_RNG
bctbx_ECDHCreateKeyPair(m_context, (int (*)(void *, uint8_t *, size_t))bctbx_rng_get, dynamic_cast<lime::bctbx_RNG&>(*rng).get_context());
// Generate a random secret key
X<Curve, lime::Xtype::privateKey> secret;
rng->randomize(secret.data(), secret.size());
// set it in the context
set_secret(secret);
// and generate the public value
deriveSelfPublic();
}
void deriveSelfPublic(void) override {
......
......@@ -133,10 +133,11 @@ namespace lime {
/**
* @brief Random number generator interface
*
* This abstract class is used to hold a RNG object which then is passed to internal crypto primitives
* who may need it (and dynamically cast to their need which it must fit)
* The only "external" use of RNG is to generate a random seed and some 32 bits id, provide explicit
* functions for that.
* This abstract class is used to hold a RNG object
* It provides explicit functions to generate:
* - random key for Double Ratchet
* - random keys Id on 31 bits.
* It also expose a generic function to generate random in a buffer
*/
class RNG {
public:
......@@ -156,6 +157,13 @@ class RNG {
*/
virtual uint32_t randomize() = 0;
/**
* fill a buffer with random numbers
* @param[in,out] buffer The buffer to be filled with random (callers responsability to allocate memory)
* @param[in] size size in bytes of the random generated, buffer must be at least of this size
**/
virtual void randomize(uint8_t *buffer, const size_t size) = 0;
virtual ~RNG() = default;
}; //class RNG
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment