예제 #1
0
void ssh_busy_wait_usec(const SshUInt64 time_us)
{
  SshTimeMeasure timer;

  timer = ssh_time_measure_allocate();
  ssh_time_measure_start(timer);
  while (ssh_time_measure_stamp(timer,
                                SSH_TIME_GRANULARITY_MICROSECOND) <
         time_us)
    /*NOTHING*/;
  ssh_time_measure_stop(timer);
  ssh_time_measure_free(timer);
}
예제 #2
0
void acc_encrypt_done(SshCryptoStatus status,
                      const unsigned char *data,
                      size_t length,
                      void *context)
{
  SshEKTestOp tc = context, ntc;
  SshExternalKeyTestCtx ctx = tc->test_ctx;
  SshPrivateKey key;
  SshUInt32 nanos;
  SshUInt64 secs;
  SshUInt32 s;

  ctx->operations_done++;
  ssh_time_measure_get_value(tc->timer,
                             &secs, &nanos);

  s = (SshUInt32)secs;
  key = accelerator_test ? ctx->acc_prv_key : ctx->prv_key;

  ctx->accelerated_encrypts_pending--;
  SSH_DEBUG(2, ("Completed encrypt %d", tc->op_id));
  SSH_DEBUG(3, ("Time %ds %dns", s, nanos));
  ssh_time_measure_free(tc->timer);
  ssh_xfree(tc);
  SSH_DEBUG_HEXDUMP(7, ("Encrypted data of len %d:", length), data, length);


  if (status == SSH_CRYPTO_OK)
    {
      SSH_DEBUG(10, ("Got the data of len %d", length));

      SSH_DEBUG(10, ("Accelerated encrypt succesfull"));
      ctx->accelerated_decrypts_pending++;
      ntc = ssh_xcalloc(1, sizeof(*ntc));

      ntc->op_id = next_op_id++;
      ntc->test_ctx = ctx;
      ssh_private_key_decrypt_async(key,
                                    data, length,
                                    acc_decrypt_done, ntc);
    }
  else
    {
      SSH_DEBUG(1, ("Accelerated encrypt unsuccesfull"));
      ctx->operations_failed++;
    }

}
예제 #3
0
void cipher_speed_test(char *cipher_name,
                       char *passphrase,
                       unsigned char *key, size_t key_len,
                       unsigned char *iv, size_t iv_len,
                       Boolean encrypt_mode)
{
  SshCryptoStatus cs;
  SshCipher cipher;
  SshTimeMeasure timer;
  unsigned char *data;
  unsigned char *data_dest;
  size_t block_len, data_len, tot, n;
  int i, len_mul;
  double sec;

  if (!passphrase && !key)
    {
      passphrase = "This is a test key!";
    }
  if (passphrase)
    {
      SSH_DEBUG(5, ("Allocating %s context with passphrase.", cipher_name));
      cs = ssh_cipher_allocate_with_passphrase(cipher_name,
                                               passphrase,
                                               encrypt_mode,
                                               &cipher);
    }
  else
    {
      SSH_DEBUG(5, ("Allocating %s context with key vector.", cipher_name));
      cs = ssh_cipher_allocate(cipher_name,
                               key,
                               key_len,
                               encrypt_mode,
                               &cipher);
    }
  if (cs != SSH_CRYPTO_OK)
    {
      switch (cs)
        {
        case SSH_CRYPTO_UNSUPPORTED:
          fprintf(stderr, "%s: Unsupported cipher \"%s\".\n", 
                  av0, cipher_name);
          exit(-1);
        case SSH_CRYPTO_KEY_TOO_SHORT:
          fprintf(stderr, "%s: Key too short for \"%s\".\n", av0, cipher_name);
          exit(-1);
        default:
          fprintf(stderr, "%s: Cipher allocate failed.\n", av0);
          exit(-1);
        }
      /*NOTREACHED*/
    }
  if (iv != NULL)
    {
      if (ssh_cipher_get_iv_length(ssh_cipher_name(cipher)) == iv_len)
        ssh_cipher_set_iv(cipher, iv);
      else
        {
          fprintf(stderr, "%s: Weird IV length.\n", av0);
          exit(1);
        }
    }
  block_len = ssh_cipher_get_block_length(ssh_cipher_name(cipher));
  data_len = CIPHER_TEST_DATA_LEN;
  while (data_len % block_len != 0)
    {
      SSH_DEBUG(5, ("Growing test data len to match with cipher block size."));
      data_len++;
    }
  data = ssh_xmalloc(data_len);
  data_dest = ssh_xmalloc(data_len);
  for (i = 0; i < data_len; i++)
    data[i] = (unsigned char)(i % 0x100);
  tot = data_len * 2;
  timer = ssh_time_measure_allocate();
 timer_retry:
  n = tot;
  ssh_time_measure_reset(timer);
  ssh_time_measure_start(timer);
  while (n > 0)
    {
      if (n > CIPHER_TEST_DATA_LEN)
        {
          cs = ssh_cipher_transform(cipher,
                                    data_dest,
                                    data,
                                    CIPHER_TEST_DATA_LEN);
          n -= CIPHER_TEST_DATA_LEN;
        }
      else
        {
          cs = ssh_cipher_transform(cipher,
                                    data_dest,
                                    data,
                                    n);
          n = 0;
        }
      if (cs != SSH_CRYPTO_OK)
          ssh_fatal("%s: ssh_cipher_transform failed (%d).", av0, (int)cs);

    }
  ssh_time_measure_stop(timer);
  sec = (double)ssh_time_measure_get(timer, SSH_TIME_GRANULARITY_SECOND);

  if (sec < CIPHER_TEST_MIN_TIME)
    {
      if (sec < 0.1)
        {
          /* This doesn't directly aim to sufficient length.
             Let's retry with 10 times longer data to get
             hash code into the cache for the `real test'. */
          len_mul = 10;
        }
      else
        {
          /* Let's try to heuristically adjust the next test
             loop so that it takes about CIPHER_TEST_MIN_TIME
             seconds to complete. */
          len_mul = (int)((CIPHER_TEST_MIN_TIME + 1.0) / sec) + 1;
        }
      SSH_DEBUG(5, ("Test completes too fast (%.2f sec).", sec));
      SSH_DEBUG(5, ("Multiply test len by %d.", len_mul));
      tot *= len_mul;
      goto timer_retry;
    }
  else
    {
      SSH_DEBUG(5, ("Test completes OK (%.2f sec).", sec));
    }
  printf("Speed[\"%s\"] = %.2f kB (%.2f megabits) / sec\n",
         cipher_name,
         ((((double)tot) / 1024.0) / sec),
         ((((double)tot) / 131072.0) / sec));
  ssh_time_measure_free(timer);
  ssh_cipher_free(cipher);
  ssh_xfree(data);
  ssh_xfree(data_dest);
  return;
}
예제 #4
0
파일: t-timemeasure.c 프로젝트: AnthraX1/rk
int main()
{
  SshTimeMeasure total_timer;
  SshTimeMeasure timer_1;
  SshTimeMeasure timer_2;
  SshTimeMeasure timer_3;
  SshTimeMeasure timer_4;
  SshTimeMeasure timer_5;
  static struct SshTimeMeasureRec timer_6_rec = SSH_TIME_MEASURE_INITIALIZER;
  SshTimeMeasure timer_6;
  int i;
  double rv = 0.0;
  int ev = 0;
#ifdef HAVE_GETTIMEOFDAY      
  struct timeval tv;
#endif /* HAVE_GETTIMEOFDAY */
  SshUInt64 seconds;
  SshUInt32 nanoseconds;

  total_timer = ssh_time_measure_allocate();
  timer_1 = ssh_time_measure_allocate();
  timer_2 = ssh_time_measure_allocate();
  timer_3 = ssh_time_measure_allocate();
  timer_4 = ssh_time_measure_allocate();
  timer_5 = ssh_time_measure_allocate();
  timer_6 = &timer_6_rec;

  if (ssh_time_measure_get(timer_5, SSH_TIME_GRANULARITY_NANOSECOND) != 0)
    {
      ssh_warning("Weird initial stamp value.\n");
      ev++;
    }
  if (ssh_time_measure_get(timer_6, SSH_TIME_GRANULARITY_NANOSECOND) != 0)
    {
      ssh_warning("Weird initial (static) stamp value.\n");
      ev++;
    }
  rv = (double)ssh_time_measure_get(total_timer, SSH_TIME_GRANULARITY_SECOND); 
  if ((rv < 0.0) || (rv > 0.0))
    {
      ssh_warning("Weird initial value.\n");
      ev++;
    }

  ssh_time_measure_granularity(&seconds, &nanoseconds);
  if ((seconds == 0) && (nanoseconds == 0))
    {
      ssh_warning("Weird granularity.\n");
      ev++;
    }
  else
    {
      printf("granularity is %lu sec %lu nsec\n", 
             (unsigned long)seconds,
             (unsigned long)nanoseconds);
    }

  START(total_timer);
  START(timer_1);
  START(timer_3);
  START(timer_4);
  START(timer_5);

  STAMP(total_timer);

  printf("testing stamps\n");
  NANOSTAMP(timer_1);
  MICROSTAMP(timer_1);
  MILLISTAMP(timer_1);
  STAMP(timer_1);
  USLEEP(1000000);
  NANOSTAMP(timer_1);
  MICROSTAMP(timer_1);
  MILLISTAMP(timer_1);
  STAMP(timer_1);
  USLEEP(1000000);
  NANOSTAMP(timer_1);
  MICROSTAMP(timer_1);
  MILLISTAMP(timer_1);
  STAMP(timer_1);
  USLEEP(1000000);
  NANOSTAMP(timer_1);
  MICROSTAMP(timer_1);
  MILLISTAMP(timer_1);
  STAMP(timer_1);
  CHECKNANOSTAMP(timer_1);
  USLEEP(1000000);
  NANOSTAMP(timer_1);
  MICROSTAMP(timer_1);
  MILLISTAMP(timer_1);
  STAMP(timer_1);
  CHECKNANOSTAMP(timer_1);
  USLEEP(1000000);
  NANOSTAMP(timer_1);
  MICROSTAMP(timer_1);
  MILLISTAMP(timer_1);
  STAMP(timer_1);
  CHECKNANOSTAMP(timer_1);
  USLEEP(1000000);
  NANOSTAMP(timer_1);
  MICROSTAMP(timer_1);
  MILLISTAMP(timer_1);
  STAMP(timer_1);
  CHECKNANOSTAMP(timer_1);
  
  USLEEP(2000000);
  STAMP(total_timer);

  SET(timer_5, 12345, 12345678);
  INTERMEDIATE(timer_5);
  if ((rv < 12345.0) || (rv > 12350.0))
    {
      ssh_warning("Weird intermediate after running set.\n");
      ev++;
    }

  INTERMEDIATE(timer_1);
  if (rv < 1.0)
    {
      ssh_warning("Weird intermediate.\n");
      ev++;
    }
  STOP(timer_3);
  if (rv < 1.0)
    {
      ssh_warning("Weird stop value.\n");
      ev++;
    }
  START(timer_2);
  RESET(timer_4);

  USLEEP(3000000);
  STAMP(total_timer);

  INTERMEDIATE(timer_2);
  INTERMEDIATE(timer_5);
  START(timer_3);
  if (rv < 1.0)
    {
      ssh_warning("Weird restart value.\n");
      ev++;
    }
  RESET(timer_4);
  STOP(timer_1);


  USLEEP(4000000);
  STAMP(total_timer);


  STOP(timer_5);

#ifdef SSHUINT64_IS_64BITS
  printf("Setting timer_5 to big value.\n");
  ssh_time_measure_set_value(timer_5, 
                             ((SshUInt64)0xffffffff) * ((SshUInt64)30), 
                             987654321);
  INTERMEDIATE(timer_5);
  if ((rv < 128849018000.0) || (rv > 128849019000.0))
    {
      ssh_warning("Weird intermediate after stopped set.\n");
      ev++;
    }
#else
  SET(timer_5, 1234567890, 987654321);
  INTERMEDIATE(timer_5);
  if ((rv < 1234567890.0) || (rv > 1234567900.0))
    {
      ssh_warning("Weird intermediate after stopped set.\n");
      ev++;
    }
#endif

  STOP(timer_4);
  STOP(timer_3);
  STOP(timer_2);
  STOP(timer_1);

#define TIMESTAMPS 1000000

  ssh_time_measure_reset(timer_1);
  ssh_time_measure_reset(timer_2);
  printf("\nGenerating %d timestamps.\n", TIMESTAMPS);
  START(timer_2);
  START(timer_1);
  for (i = 1; i < TIMESTAMPS; i++)
    {
      ssh_time_measure_stamp(timer_2, SSH_TIME_GRANULARITY_MICROSECOND);
    }
  STOP(timer_1);
  STOP(timer_2);
  printf("Time elapsed %.12f seconds (%.12f seconds/timestamp", 
         (double)ssh_time_measure_get(timer_1, SSH_TIME_GRANULARITY_SECOND),
         (double)ssh_time_measure_get(timer_1, SSH_TIME_GRANULARITY_SECOND) / (double)TIMESTAMPS);
  if ((double)ssh_time_measure_get(timer_1, SSH_TIME_GRANULARITY_SECOND) > 0.0)
    printf(", %d timestamps/second",
           (int)((double)TIMESTAMPS / (double)ssh_time_measure_get(timer_1, SSH_TIME_GRANULARITY_SECOND)));
  printf(")\n");

  ssh_time_measure_reset(timer_3);
  ssh_time_measure_reset(timer_4);
  printf("\nFor reference generating %d timestamps with time(3).\n", 
         TIMESTAMPS);
  START(timer_4);
  START(timer_3);
  for (i = 1; i < TIMESTAMPS; i++)
    {
      ssh_time();
    }
  STOP(timer_3);
  STOP(timer_4);
  printf("Time elapsed %.12f seconds (%.12f seconds/timestamp", 
         (double)ssh_time_measure_get(timer_3, SSH_TIME_GRANULARITY_SECOND),
         (double)ssh_time_measure_get(timer_3, SSH_TIME_GRANULARITY_SECOND) / (double)TIMESTAMPS);
  if ((double)ssh_time_measure_get(timer_3, SSH_TIME_GRANULARITY_SECOND) > 0.0)
    printf(", %d timestamps/second",
           (int)((double)TIMESTAMPS / (double)ssh_time_measure_get(timer_3, SSH_TIME_GRANULARITY_SECOND)));
  printf(")\n");

  if (((double)ssh_time_measure_get(timer_1, SSH_TIME_GRANULARITY_SECOND) > 0.0) &&
      ((double)ssh_time_measure_get(timer_3, SSH_TIME_GRANULARITY_SECOND) > 0.0))
    printf("Using time(3) is %2.1f%% faster than ssh_..._stamp.\n", 
           (((double)ssh_time_measure_get(timer_1, SSH_TIME_GRANULARITY_SECOND) - 
             (double)ssh_time_measure_get(timer_3, SSH_TIME_GRANULARITY_SECOND)) /
            (double)ssh_time_measure_get(timer_1, SSH_TIME_GRANULARITY_SECOND)) * 100.0);

#ifdef HAVE_GETTIMEOFDAY
  ssh_time_measure_reset(timer_3);
  ssh_time_measure_reset(timer_4);
  printf("\nFor reference generating %d timestamps with gettimeofday.\n", 
         TIMESTAMPS);
  START(timer_4);
  START(timer_3);
  for (i = 1; i < TIMESTAMPS; i++)
    {
      gettimeofday(&tv, NULL);
    }
  STOP(timer_3);
  STOP(timer_4);
  printf("Time elapsed %.12f seconds (%.12f seconds/timestamp", 
         (double)ssh_time_measure_get(timer_3, SSH_TIME_GRANULARITY_SECOND),
         (double)ssh_time_measure_get(timer_3, SSH_TIME_GRANULARITY_SECOND) / (double)TIMESTAMPS);
  if ((double)ssh_time_measure_get(timer_3, SSH_TIME_GRANULARITY_SECOND) > 0.0)
    printf(", %d timestamps/second",
           (int)((double)TIMESTAMPS / (double)ssh_time_measure_get(timer_3, SSH_TIME_GRANULARITY_SECOND)));
  printf(")\n");

  if (((double)ssh_time_measure_get(timer_1, SSH_TIME_GRANULARITY_SECOND) > 0.0) &&
      ((double)ssh_time_measure_get(timer_3, SSH_TIME_GRANULARITY_SECOND) > 0.0))
    printf("Using gettimeofday(3) is %2.1f%% faster than ssh_..._stamp.\n", 
           (((double)ssh_time_measure_get(timer_1, SSH_TIME_GRANULARITY_SECOND) - 
             (double)ssh_time_measure_get(timer_3, SSH_TIME_GRANULARITY_SECOND)) /
            (double)ssh_time_measure_get(timer_1, SSH_TIME_GRANULARITY_SECOND)) * 100.0);
#endif /* HAVE_GETTIMEOFDAY */

  printf("making start stop test. timers are silently started and stopped.\n");
  printf("timer_3 runs while timer_4 is started and stopped in loop.\n");
  ssh_time_measure_stop(timer_3);
  ssh_time_measure_stop(timer_4);
  ssh_time_measure_reset(timer_3);
  ssh_time_measure_reset(timer_4);
  ssh_time_measure_start(timer_3);
  for (i = 0; i < 1000000; i++)
    {
      ssh_time_measure_start(timer_4);
      ssh_time_measure_stop(timer_4);
    }
  ssh_time_measure_stop(timer_3);
  INTERMEDIATE(timer_4);
  INTERMEDIATE(timer_3);
  

  STOP(total_timer);
  GET_INT(timer_1);
  INTERMEDIATE(timer_1);
  GET_INT(timer_2);
  INTERMEDIATE(timer_2);
  GET_INT(timer_3);
  INTERMEDIATE(timer_3);
  GET_INT(timer_4);
  INTERMEDIATE(timer_4);
  GET_INT(timer_5);
  INTERMEDIATE(timer_5);
  GET_INT(total_timer);
  INTERMEDIATE(total_timer);
  printf("Testing granularities\n");
  GET_NANOSECONDS(total_timer);
  GET_MICROSECONDS(total_timer);
  GET_MILLISECONDS(total_timer);
  GET_SECONDS(total_timer);
  GET_MINUTES(total_timer);
  GET_HOURS(total_timer);
  GET_DAYS(total_timer);
  GET_WEEKS(total_timer);
  GET_MONTHS(total_timer);
  GET_YEARS(total_timer);
  GET_NANOSECONDS(timer_5);
  GET_MICROSECONDS(timer_5);
  GET_MILLISECONDS(timer_5);
  GET_SECONDS(timer_5);
  GET_MINUTES(timer_5);
  GET_HOURS(timer_5);
  GET_DAYS(timer_5);
  GET_WEEKS(timer_5);
  GET_MONTHS(timer_5);
  GET_MONTHS_2(timer_5);
  GET_YEARS(timer_5);
  GET_YEARS_2(timer_5);
  GET_YEARS_3(timer_5);

  ssh_time_measure_free(timer_5);
  ssh_time_measure_free(timer_4);
  ssh_time_measure_free(timer_3);
  ssh_time_measure_free(timer_2);
  ssh_time_measure_free(timer_1);
  ssh_time_measure_free(total_timer);

  exit(ev);
}