Commit acbb60cd authored by Nick Mathewson's avatar Nick Mathewson 🦀
Browse files

Move unittests' RSA pregen code into a new file, and improve.

This patch moves the pregenerated RSA key logic into a new
testing_rsakeys.c.

Also, it adds support for RSA2048, since the link handshake tests
want that.

Also, it includes pregenerated keys, rather than trying to actually
generate the keys at startup, since generating even a small handful
of RSA2048 keys makes for an annoying delay.
parent af2459f0
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -128,6 +128,7 @@ src_test_test_SOURCES = \
	src/test/test_helpers.c \
	src/test/test_dns.c \
	src/test/testing_common.c \
	src/test/testing_rsakeys.c \
	src/ext/tinytest.c

src_test_test_slow_SOURCES = \
@@ -135,6 +136,7 @@ src_test_test_slow_SOURCES = \
	src/test/test_crypto_slow.c \
	src/test/test_util_slow.c \
	src/test/testing_common.c \
	src/test/testing_rsakeys.c \
	src/ext/tinytest.c

src_test_test_memwipe_SOURCES = \
+2 −0
Original line number Diff line number Diff line
@@ -74,6 +74,8 @@

const char *get_fname(const char *name);
struct crypto_pk_t *pk_generate(int idx);
void init_pregenerated_keys(void);
void free_pregenerated_keys(void);

#define US2_CONCAT_2__(a, b) a ## __ ## b
#define US_CONCAT_2__(a, b) a ## _ ## b
+1 −68
Original line number Diff line number Diff line
@@ -155,65 +155,6 @@ remove_directory(void)
  rm_rf(temp_dir);
}

/** Define this if unit tests spend too much time generating public keys*/
#define CACHE_GENERATED_KEYS

#define N_PREGEN_KEYS 11
static crypto_pk_t *pregen_keys[N_PREGEN_KEYS];
static int next_key_idx;

/** Generate and return a new keypair for use in unit tests.  If we're using
 * the key cache optimization, we might reuse keys. "idx" is ignored.
 * Our only guarantee is that we won't reuse a key till this function has been
 * called several times. The order in which keys are returned is slightly
 * randomized, so that tests that depend on a particular order will not be
 * reliable. */
crypto_pk_t *
pk_generate(int idx)
{
  (void) idx;
#ifdef CACHE_GENERATED_KEYS
  /* Either skip 1 or 2 keys. */
  next_key_idx += crypto_rand_int_range(1,3);
  next_key_idx %= N_PREGEN_KEYS;
  return crypto_pk_dup_key(pregen_keys[next_key_idx]);
#else
  crypto_pk_t *result;
  int res;
  result = crypto_pk_new();
  res = crypto_pk_generate_key__real(result);
  tor_assert(!res);
  return result;
#endif
}

#ifdef CACHE_GENERATED_KEYS
static int
crypto_pk_generate_key_with_bits__get_cached(crypto_pk_t *env, int bits)
{
  if (bits != 1024)
    return crypto_pk_generate_key_with_bits__real(env, bits);

  crypto_pk_t *newkey = pk_generate(0);
  crypto_pk_assign_(env, newkey);
  crypto_pk_free(newkey);
  return 0;
}
#endif

/** Free all storage used for the cached key optimization. */
static void
free_pregenerated_keys(void)
{
  unsigned idx;
  for (idx = 0; idx < N_PREGEN_KEYS; ++idx) {
    if (pregen_keys[idx]) {
      crypto_pk_free(pregen_keys[idx]);
      pregen_keys[idx] = NULL;
    }
  }
}

static void *
passthrough_test_setup(const struct testcase_t *testcase)
{
@@ -339,15 +280,7 @@ main(int c, const char **v)
  }
  tor_set_failed_assertion_callback(an_assertion_failed);

#ifdef CACHE_GENERATED_KEYS
  for (i = 0; i < N_PREGEN_KEYS; ++i) {
    pregen_keys[i] = crypto_pk_new();
    int r = crypto_pk_generate_key(pregen_keys[i]);
    tor_assert(r == 0);
  }
  MOCK(crypto_pk_generate_key_with_bits,
       crypto_pk_generate_key_with_bits__get_cached);
#endif
  init_pregenerated_keys();

  atexit(remove_directory);

+545 −0

File added.

Preview size limit exceeded, changes collapsed.