示例#1
0
/** Generate bytes using the Intel RDRAND instruction. */
static int
ottery_get_entropy_rdrand(const struct ottery_entropy_config *cfg,
                          struct ottery_entropy_state *state,
                           uint8_t *out, size_t outlen)
{
  int err;
  uint32_t *up = (uint32_t *) out;
  (void) cfg;
  (void) state;
  if (! (ottery_get_cpu_capabilities_() & OTTERY_CPUCAP_RAND) || ottery_valgrind_)
    return OTTERY_ERR_INIT_STRONG_RNG;
  while (outlen >= 4) {
    if ((err = rdrand(up)))
      return err;
    up += 1;
    outlen -= 4;
  }
  if (outlen) {
    uint32_t tmp;
    if ((err = rdrand(&tmp)))
      return err;
    memcpy(up, &tmp, outlen);
  }
  return 0;
}
示例#2
0
static const struct ottery_prf *
ottery_get_impl(const char *impl)
{
  int i;
  const struct ottery_prf *ALL_PRFS[] = {
#ifdef HAVE_SIMD_CHACHA_2
    &ottery_prf_chacha20_krovetz_2_,
    &ottery_prf_chacha12_krovetz_2_,
    &ottery_prf_chacha8_krovetz_2_,
#endif
#ifdef HAVE_SIMD_CHACHA
    &ottery_prf_chacha20_krovetz_1_,
    &ottery_prf_chacha12_krovetz_1_,
    &ottery_prf_chacha8_krovetz_1_,
#endif
    &ottery_prf_chacha20_merged_,
    &ottery_prf_chacha12_merged_,
    &ottery_prf_chacha8_merged_,

    NULL,
  };
  const uint32_t cap = ottery_get_cpu_capabilities_();

  for (i = 0; ALL_PRFS[i]; ++i) {
    const struct ottery_prf *prf = ALL_PRFS[i];
    if ((prf->required_cpucap & cap) != prf->required_cpucap)
      continue;
    if (impl == NULL)
      return prf;
    if (!strcmp(impl, prf->name))
      return prf;
    if (!strcmp(impl, prf->impl))
      return prf;
    if (!strcmp(impl, prf->flav))
      return prf;
  }
  return NULL;
}
示例#3
0
int
main(int argc, char **argv)
{
  (void) argc;
  (void) argv;
#ifndef NO_OPENSSL
  RAND_poll();
#endif
  struct ottery_config cfg_chacha8;
  struct ottery_config cfg_chacha12;
  struct ottery_config cfg_chacha20;
  ottery_config_init(&cfg_chacha8);
  ottery_config_force_implementation(&cfg_chacha8, OTTERY_PRF_CHACHA8);
  ottery_config_init(&cfg_chacha12);
  ottery_config_force_implementation(&cfg_chacha12, OTTERY_PRF_CHACHA12);
  ottery_config_init(&cfg_chacha20);
  ottery_config_force_implementation(&cfg_chacha20, OTTERY_PRF_CHACHA20);

  ottery_st_init(&s8, &cfg_chacha8);
  ottery_st_init(&s12, &cfg_chacha12);
  ottery_st_init(&s20, &cfg_chacha20);
  ottery_st_init_nolock(&s8nl, &cfg_chacha8);
  ottery_st_init_nolock(&s12nl, &cfg_chacha12);
  ottery_st_init_nolock(&s20nl, &cfg_chacha20);

  time_chacharand8();
  time_chacharand8_u64();
  time_chacharand8_onebyte();
  time_chacharand8_buf16();
  time_chacharand8_buf1024();

  time_chacharand12();
  time_chacharand12_u64();
  time_chacharand12_onebyte();
  time_chacharand12_buf16();
  time_chacharand12_buf1024();

  time_chacharand20();
  time_chacharand20_u64();
  time_chacharand20_onebyte();
  time_chacharand20_buf16();
  time_chacharand20_buf1024();

  time_chacharand8nl();
  time_chacharand8nl_u64();
  time_chacharand8nl_onebyte();
  time_chacharand8nl_buf16();
  time_chacharand8nl_buf1024();

  time_chacharand12nl();
  time_chacharand12nl_u64();
  time_chacharand12nl_onebyte();
  time_chacharand12nl_buf16();
  time_chacharand12nl_buf1024();

  time_chacharand20nl();
  time_chacharand20nl_u64();
  time_chacharand20nl_onebyte();
  time_chacharand20nl_buf16();
  time_chacharand20nl_buf1024();

  time_arc4random();
  time_arc4random_u64();
  time_arc4random_onebyte();
  time_arc4random_buf16();
  time_arc4random_buf1024();

  if (ottery_get_cpu_capabilities_() & OTTERY_CPUCAP_RAND) {
    time_rdrandom();
    time_rdrandom_buf16();
    time_rdrandom_buf1024();
  }

#ifndef NO_URANDOM
  urandom_fd = open("/dev/urandom", O_RDONLY);
  time_urandom();
  time_urandom_u64();
  time_urandom_buf16();
  time_urandom_buf1024();
#endif

  time_libc_random();
  time_libc_random_u64();
  time_libc_onebyte();
  time_libcrandom_buf16();
  time_libcrandom_buf1024();

  time_openssl_random();
  time_opensslrandom_buf16();
  time_opensslrandom_buf1024();

  return 0;
}