Esempio n. 1
0
bool tr_rand_buffer(void* buffer, size_t length)
{
    bool ret;
    tr_lock* rng_lock = get_rng_lock();

    TR_ASSERT(buffer != NULL);

    tr_lockLock(rng_lock);
    ret = check_result(API(RNG_GenerateBlock)(get_rng(), buffer, length));
    tr_lockUnlock(rng_lock);

    return ret;
}
Esempio n. 2
0
bool
tr_dh_make_key (tr_dh_ctx_t   raw_handle,
                size_t        private_key_length UNUSED,
                uint8_t     * public_key,
                size_t      * public_key_length)
{
  struct tr_dh_ctx * handle = raw_handle;
  word32 my_private_key_length, my_public_key_length;
  tr_lock * rng_lock = get_rng_lock ();

  assert (handle != NULL);
  assert (public_key != NULL);

  if (handle->private_key == NULL)
    handle->private_key = tr_malloc (handle->key_length);

  tr_lockLock (rng_lock);

  if (!check_result (DhGenerateKeyPair (&handle->dh, get_rng (),
                                        handle->private_key, &my_private_key_length,
                                        public_key, &my_public_key_length)))
    {
      tr_lockUnlock (rng_lock);
      return false;
    }

  tr_lockUnlock (rng_lock);

  tr_dh_align_key (public_key, my_public_key_length, handle->key_length);

  handle->private_key_length = my_private_key_length;

  if (public_key_length != NULL)
    *public_key_length = handle->key_length;

  return true;
}