コード例 #1
0
ファイル: test_silctime.c プロジェクト: TabTwo/silc-toolkit
int main(int argc, char **argv)
{
  SilcBool success = FALSE;
  SilcTimeStruct curtime;

  if (argc > 1 && !strcmp(argv[1], "-d")) {
    silc_log_debug(TRUE);
    silc_log_quick(TRUE);
    silc_log_debug_hexdump(TRUE);
    silc_log_set_debug_string("*time*");
  }

  SILC_LOG_DEBUG(("Get current time"));
  if (!silc_time_value(0, &curtime))
    goto err;
  SILC_LOG_DEBUG(("year      : %d", curtime.year));
  SILC_LOG_DEBUG(("month     : %d", curtime.month));
  SILC_LOG_DEBUG(("day       : %d", curtime.day));
  SILC_LOG_DEBUG(("hour      : %d", curtime.hour));
  SILC_LOG_DEBUG(("minute    : %d", curtime.minute));
  SILC_LOG_DEBUG(("second    : %d", curtime.second));
  SILC_LOG_DEBUG(("msecond   : %d", curtime.msecond));
  SILC_LOG_DEBUG(("utc_hour  : %d", curtime.utc_hour));
  SILC_LOG_DEBUG(("utc_min   : %d", curtime.utc_minute));
  SILC_LOG_DEBUG(("utc_east  : %d", curtime.utc_east));
  SILC_LOG_DEBUG(("dst       : %d", curtime.dst));

  success = TRUE;

 err:
  SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE"));
  fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE");

  return success;
}
コード例 #2
0
ファイル: sftp_client.c プロジェクト: TabTwo/silc-toolkit
int main(int argc, char **argv)
{
  Client client = silc_calloc(1, sizeof(*client));

  gclient = client;

  if (argc > 1) {
    if (!strcmp(argv[1], "-d"))
      silc_log_debug(TRUE);
    if (argc > 2 && !strcmp(argv[2], "-x"))
      silc_log_debug_hexdump(TRUE);
    silc_log_set_debug_string("*");
  }

  client->schedule = silc_schedule_init(0, NULL);
  if (!client->schedule)
    return -1;

  /* Connecto to server */
  silc_net_tcp_connect(NULL, "127.0.0.1", 5000, client->schedule,
		       connect_callback, client);

  silc_schedule(client->schedule);
  return 0;
}
コード例 #3
0
ファイル: test_silcfsm.c プロジェクト: TabTwo/silc-toolkit
int main(int argc, char **argv)
{
  SilcBool success = FALSE;
  SilcSchedule schedule;
  SilcFSM fsm;
  Foo f;

  if (argc > 1 && !strcmp(argv[1], "-d")) {
    silc_log_debug(TRUE);
    silc_log_debug_hexdump(TRUE);
    silc_log_quick(TRUE);
    silc_log_set_debug_string("*fsm*,*async*");
  }

  SILC_LOG_DEBUG(("Allocating scheduler"));
  schedule = silc_schedule_init(0, NULL);

  f = silc_calloc(1, sizeof(*f));
  if (!f)
    goto err;
  f->schedule = schedule;

  SILC_LOG_DEBUG(("Allocating FSM context"));
  f->fsm = fsm = silc_fsm_alloc(f, destructor, f, schedule);
  if (!fsm)
    goto err;
  silc_fsm_start(fsm, test_st_start);

  SILC_LOG_DEBUG(("Running scheduler"));
  silc_schedule(schedule);

  if (f->error)
    goto err;

  silc_schedule_uninit(schedule);
  silc_free(f);

  success = TRUE;

 err:
  SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE"));
  fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE");

  return success;
}
コード例 #4
0
int main(int argc, char **argv)
{
  SilcFSM fsm;

  if (argc > 1 && !strcmp(argv[1], "-d")) {
    silc_log_debug(TRUE);
    silc_log_debug_hexdump(TRUE);
    silc_log_set_debug_string("*fdstream*");
  }

  SILC_LOG_DEBUG(("Allocating scheduler"));
  schedule = silc_schedule_init(0, NULL);
  if (!schedule)
    goto err;

  SILC_LOG_DEBUG(("Allocating FSM"));
  fsm = silc_fsm_alloc(NULL, fsm_dest, NULL, schedule);
  if (!fsm)
    goto err;

  silc_fsm_start(fsm, st_write);

  SILC_LOG_DEBUG(("Running scheduler"));
  silc_schedule(schedule);

  if (!success)
    goto err;

  silc_schedule_uninit(schedule);
  success = TRUE;

 err:
  SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE"));
  fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE");

  return success;
}
コード例 #5
0
int main(int argc, char **argv)
{
  SilcFSM fsm;

  silc_runtime_init();

  if (argc > 1 && !strcmp(argv[1], "-d")) {
    silc_log_debug(TRUE);
    silc_log_debug_hexdump(TRUE);
    silc_log_set_debug_string("*thread*");
  }

  SILC_LOG_DEBUG(("Allocating scheduler"));
  schedule = silc_schedule_init(0, NULL, NULL, NULL);
  if (!schedule)
    goto err;

  SILC_LOG_DEBUG(("Allocating FSM context"));
  fsm = silc_fsm_alloc(NULL, destructor, NULL, schedule);
  if (!fsm)
    goto err;
  silc_fsm_start(fsm, test_st_start);

  SILC_LOG_DEBUG(("Running scheduler"));
  silc_schedule(schedule);

  silc_schedule_uninit(schedule);

 err:
  SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE"));
  fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE");

  silc_runtime_uninit();

  return !success;
}
コード例 #6
0
ファイル: test_silcskr.c プロジェクト: FabrizioFabbe/crypto
int main(int argc, char **argv)
{
  SilcBool success = FALSE;
  SilcSchedule schedule;
  SilcSKR skr;
  SilcSKRFind find;
  SilcPublicKey pk;

  if (argc > 1 && !strcmp(argv[1], "-d")) {
    silc_log_debug(TRUE);
    silc_log_debug_hexdump(TRUE);
    silc_log_set_debug_string("*skr*");
  }

  SILC_LOG_DEBUG(("Allocating scheduler"));
  schedule = silc_schedule_init(0, NULL, NULL);

  SILC_LOG_DEBUG(("Allocating SKR"));
  skr = silc_skr_alloc();
  if (!skr)
    goto err;

  SILC_LOG_DEBUG(("Adding public key to SKR"));
  pk = silc_calloc(1, sizeof(*pk));
  pk->len = 1;
  pk->pk_type = SILC_PKCS_SILC;
  pk->name = strdup("rsa");
  pk->pk = strdup("  ");
  pk->pk_len = 2;
  pk->identifier = silc_pkcs_encode_identifier("foo", "foo.com",
					       "Foo T. Bar", "*****@*****.**",
					       "ORG", "FI");
  silc_skr_add_public_key(skr, pk, 0, NULL);

  SILC_LOG_DEBUG(("Adding public key to SKR"));
  pk = silc_calloc(1, sizeof(*pk));
  pk->len = 1;
  pk->pk_type = SILC_PKCS_SILC;
  pk->name = strdup("rsa");
  pk->pk = strdup("  ");
  pk->pk_len = 2;
  pk->identifier = silc_pkcs_encode_identifier("bar", "bar.com",
					       "Bar T. Bar", "*****@*****.**",
					       "ORG", "FI");
  silc_skr_add_public_key(skr, pk, SILC_SKR_USAGE_IDENTIFICATION |
			  SILC_SKR_USAGE_AUTH, NULL);

  SILC_LOG_DEBUG(("Attempting to add key twice"));
  if (silc_skr_add_public_key(skr, pk, 0, NULL) == SILC_SKR_OK) {
    SILC_LOG_DEBUG(("Adding key twice not detected"));
    goto err;
  }

  SILC_LOG_DEBUG(("Finding public key by email"));
  find = silc_skr_find_alloc();
  silc_skr_find_set_email(find, "*****@*****.**");
  silc_skr_find(skr, schedule, find, skr_found, NULL);
  silc_skr_find_free(find);
  if (!found)
    goto err;

  SILC_LOG_DEBUG(("Finding public key by country"));
  find = silc_skr_find_alloc();
  silc_skr_find_set_country(find, "FI");
  silc_skr_find(skr, schedule, find, skr_found, NULL);
  silc_skr_find_free(find);
  if (!found)
    goto err;

  SILC_LOG_DEBUG(("Finding public key by country, ORG and hostname"));
  find = silc_skr_find_alloc();
  silc_skr_find_set_country(find, "FI");
  silc_skr_find_set_org(find, "ORG");
  silc_skr_find_set_host(find, "foo.com");
  silc_skr_find(skr, schedule, find, skr_found, NULL);
  silc_skr_find_free(find);
  if (!found)
    goto err;

  SILC_LOG_DEBUG(("Finding public key by SILC public key"));
  silc_skr_find_silc(skr, pk, skr_found, NULL);
  if (!found)
    goto err;

  SILC_LOG_DEBUG(("Finding public key by country and usage (must not find)"));
  find = silc_skr_find_alloc();
  silc_skr_find_set_country(find, "FI");
  silc_skr_find_set_usage(find, SILC_SKR_USAGE_ENC);
  silc_skr_find(skr, schedule, find, skr_found, NULL);
  silc_skr_find_free(find);
  if (found)
    goto err;

  SILC_LOG_DEBUG(("Finding public key by country and usage"));
  find = silc_skr_find_alloc();
  silc_skr_find_set_country(find, "FI");
  silc_skr_find_set_usage(find, SILC_SKR_USAGE_IDENTIFICATION);
  silc_skr_find(skr, schedule, find, skr_found, NULL);
  silc_skr_find_free(find);
  if (!found)
    goto err;

  silc_skr_free(skr);
  silc_schedule_uninit(schedule);

  success = TRUE;

 err:
  SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE"));
  fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE");

  return success;
}
コード例 #7
0
int main(int argc, char **argv)
{
  SilcBool success = FALSE;
  SilcStack stack, child, child2;
  void *ptr, *ptr2;
  int i;

  if (argc > 1 && !strcmp(argv[1], "-d")) {
    silc_log_debug(TRUE);
    silc_log_debug_hexdump(TRUE);
    silc_log_quick(TRUE);
    silc_log_set_debug_string("*stack*,*errno*");
  }

  SILC_LOG_DEBUG(("Allocating stack of default size (1024 bytes)"));
  stack = silc_stack_alloc(0, NULL);
  if (!stack)
    goto err;
#ifdef SILC_DIST_INPLACE
  silc_stack_stats(stack);
#endif /* SILC_DIST_INPLACE */

  SILC_LOG_DEBUG(("Allocating 2048 bytes from stack"));
  ptr = silc_smalloc(stack, 2048);
  if (!ptr)
    goto err;
#ifdef SILC_DIST_INPLACE
  silc_stack_stats(stack);
#endif /* SILC_DIST_INPLACE */

  SILC_LOG_DEBUG(("Freeing the stack"));
  silc_stack_free(stack);

  SILC_LOG_DEBUG(("Allocating stack of default size (1024 bytes)"));
  stack = silc_stack_alloc(0, NULL);
  if (!stack)
    goto err;
#ifdef SILC_DIST_INPLACE
  silc_stack_stats(stack);
#endif /* SILC_DIST_INPLACE */

  SILC_LOG_DEBUG(("Pushing and allocating %d times", NUM_ALLS));
  if (!silc_stack_push(stack, NULL))
    goto err;
  for (i = 0; i < NUM_ALLS; i++) {
    ptr2 = silc_smalloc(stack, (i + 1) * 7);
    if (!ptr2)
      goto err;
  }
#ifdef SILC_DIST_INPLACE
  silc_stack_stats(stack);
#endif /* SILC_DIST_INPLACE */
  silc_stack_pop(stack);
  SILC_LOG_DEBUG(("Popping"));
#ifdef SILC_DIST_INPLACE
  silc_stack_stats(stack);
#endif /* SILC_DIST_INPLACE */

  SILC_LOG_DEBUG(("Pushing and allocating %d times", NUM_ALLS));
  if (!silc_stack_push(stack, NULL))
    goto err;
  for (i = 0; i < NUM_ALLS; i++) {
    ptr2 = silc_smalloc(stack, (i + 1) * 7);
    if (!ptr2)
      goto err;
  }
#ifdef SILC_DIST_INPLACE
  silc_stack_stats(stack);
#endif /* SILC_DIST_INPLACE */
  silc_stack_pop(stack);
  SILC_LOG_DEBUG(("Popping"));
#ifdef SILC_DIST_INPLACE
  silc_stack_stats(stack);
#endif /* SILC_DIST_INPLACE */

  SILC_LOG_DEBUG(("Pushing %d times", NUM_ALLS / 2));
  for (i = 0; i < NUM_ALLS / 2; i++) {
    if (!silc_stack_push(stack, NULL))
      goto err;
    ptr2 = silc_smalloc(stack, (i + 1) * 7);
    if (!ptr2)
      goto err;
  }
#ifdef SILC_DIST_INPLACE
  silc_stack_stats(stack);
#endif /* SILC_DIST_INPLACE */
  SILC_LOG_DEBUG(("Popping %d times", NUM_ALLS / 2));
  for (i = 0; i < NUM_ALLS / 2; i++)
    silc_stack_pop(stack);
#ifdef SILC_DIST_INPLACE
  silc_stack_stats(stack);
#endif /* SILC_DIST_INPLACE */

  SILC_LOG_DEBUG(("Pushing and reallocating %d times", NUM_ALLS / 10));
  ptr2 = NULL;
  if (!silc_stack_push(stack, NULL))
    goto err;
  for (i = 0; i < NUM_ALLS / 10; i++) {
    ptr2 = silc_srealloc(stack, (i * 7), ptr2, (i + 1) * 7);
    if (!ptr2)
      goto err;
  }
#ifdef SILC_DIST_INPLACE
  silc_stack_stats(stack);
#endif /* SILC_DIST_INPLACE */
  silc_stack_pop(stack);
  SILC_LOG_DEBUG(("Popping"));
#ifdef SILC_DIST_INPLACE
  silc_stack_stats(stack);
#endif /* SILC_DIST_INPLACE */

  SILC_LOG_DEBUG(("Creating child stack"));
  child = silc_stack_alloc(8190, stack);
  if (!child)
    goto err;
  SILC_LOG_DEBUG(("Pushing %d times", NUM_ALLS / 2));
  for (i = 0; i < NUM_ALLS / 2; i++) {
    if (!silc_stack_push(child, NULL))
      goto err;
    ptr2 = silc_smalloc(child, (i + 1) * 7);
    if (!ptr2)
      goto err;
  }
#ifdef SILC_DIST_INPLACE
  silc_stack_stats(child);
#endif /* SILC_DIST_INPLACE */
  SILC_LOG_DEBUG(("Popping %d times", NUM_ALLS / 2));
  for (i = 0; i < NUM_ALLS / 2; i++)
    silc_stack_pop(child);
#ifdef SILC_DIST_INPLACE
  silc_stack_stats(child);
#endif /* SILC_DIST_INPLACE */

  SILC_LOG_DEBUG(("Pushing and reallocating %d times", NUM_ALLS / 10));
  ptr2 = NULL;
  if (!silc_stack_push(child, NULL))
    goto err;
  for (i = 0; i < NUM_ALLS / 10; i++) {
    ptr2 = silc_srealloc(child, (i * 7), ptr2, (i + 1) * 7);
    if (!ptr2)
      goto err;
  }
  ptr = silc_smalloc(child, 100000);
#ifdef SILC_DIST_INPLACE
  silc_stack_stats(child);
#endif /* SILC_DIST_INPLACE */
  silc_stack_pop(child);
  SILC_LOG_DEBUG(("Popping"));
#ifdef SILC_DIST_INPLACE
  silc_stack_stats(child);
#endif /* SILC_DIST_INPLACE */
#ifdef SILC_DIST_INPLACE
  silc_stack_stats(stack);
#endif /* SILC_DIST_INPLACE */
  silc_stack_free(child);
#ifdef SILC_DIST_INPLACE
  silc_stack_stats(stack);
#endif /* SILC_DIST_INPLACE */

  SILC_LOG_DEBUG(("Creating child stack"));
  child = silc_stack_alloc(8192, stack);
  if (!child)
    goto err;
  SILC_LOG_DEBUG(("Pushing %d times", NUM_ALLS / 10));
  for (i = 0; i < NUM_ALLS / 10; i++) {
    if (!silc_stack_push(child, NULL))
      goto err;
    ptr2 = silc_smalloc(child, (i + 1) * 7);
    if (!ptr2)
      goto err;
  }
#ifdef SILC_DIST_INPLACE
  silc_stack_stats(child);
#endif /* SILC_DIST_INPLACE */
  SILC_LOG_DEBUG(("Popping %d times", NUM_ALLS / 10));
  for (i = 0; i < NUM_ALLS / 10; i++)
    silc_stack_pop(child);
#ifdef SILC_DIST_INPLACE
  silc_stack_stats(child);
#endif /* SILC_DIST_INPLACE */

  SILC_LOG_DEBUG(("Pushing and reallocating %d times", NUM_ALLS / 10));
  ptr2 = NULL;
  if (!silc_stack_push(child, NULL))
    goto err;
  for (i = 0; i < NUM_ALLS / 10; i++) {
    ptr2 = silc_srealloc(child, (i * 7), ptr2, (i + 1) * 7);
    if (!ptr2)
      goto err;
  }
  SILC_LOG_DEBUG(("Allocate child from child"));
  child2 = silc_stack_alloc(0, child);
  ptr = silc_smalloc(child2, 500000);
#ifdef SILC_DIST_INPLACE
  silc_stack_stats(child2);
#endif /* SILC_DIST_INPLACE */
  silc_stack_free(child2);
#ifdef SILC_DIST_INPLACE
  silc_stack_stats(child2);
#endif /* SILC_DIST_INPLACE */
  silc_stack_pop(child);
  SILC_LOG_DEBUG(("Popping"));
#ifdef SILC_DIST_INPLACE
  silc_stack_stats(child);
#endif /* SILC_DIST_INPLACE */
#ifdef SILC_DIST_INPLACE
  silc_stack_stats(stack);
#endif /* SILC_DIST_INPLACE */
  silc_stack_free(child);
#ifdef SILC_DIST_INPLACE
  silc_stack_stats(stack);
#endif /* SILC_DIST_INPLACE */

  SILC_LOG_DEBUG(("Purge stack"));
  silc_stack_purge(stack);
#ifdef SILC_DIST_INPLACE
  silc_stack_stats(stack);
#endif /* SILC_DIST_INPLACE */

  SILC_LOG_DEBUG(("Current alignment: %d", silc_stack_get_alignment(stack)));
  SILC_LOG_DEBUG(("Set alignemtn to 16"));
  silc_stack_set_alignment(stack, 16);
  SILC_LOG_DEBUG(("Current alignment: %d", silc_stack_get_alignment(stack)));
  SILC_LOG_DEBUG(("Allocate 1 byte"));
  ptr = silc_smalloc(stack, 1);
  SILC_LOG_DEBUG(("Allocate 1 byte, check alignment"));
  ptr2 = silc_smalloc(stack, 1);
  if (ptr2 - ptr < 16) {
    SILC_LOG_DEBUG(("Bad alignment"));
    goto err;
  }
  SILC_LOG_DEBUG(("Alignment (ptr, ptr2) is %d", ptr2 - ptr));
  SILC_LOG_DEBUG(("Allocate 1 byte, check alignment"));
  ptr2 = silc_smalloc(stack, 1);
  if (ptr2 - ptr < 32) {
    SILC_LOG_DEBUG(("Bad alignment"));
    goto err;
  }
  SILC_LOG_DEBUG(("Alignment (ptr, ptr2) is %d", ptr2 - ptr));

  SILC_LOG_DEBUG(("Freeing the stack"));
  silc_stack_free(stack);

  success = TRUE;

 err:
  SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE"));
  fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE");

  return !success;
}
コード例 #8
0
int main(int argc, char **argv)
{
  SilcUInt64 sec;
  SilcUInt32 usec;
  double totsec;
  unsigned char *data;
  SilcUInt32 rounds;
  SilcUInt32 i, k;

  silc_runtime_init();
  silc_crypto_init(NULL);

#if 0
  silc_log_debug(TRUE);
  silc_log_quick(TRUE);
  silc_log_debug_hexdump(TRUE);
  silc_log_set_debug_string("*acc*,*thread*");
#endif

  if (!silc_acc_init(SILC_SOFTACC, (void *)0x01, "min_threads", 2,
		     "max_threads", 8, NULL))
    exit(1);

  data = malloc(ENC_LEN * sizeof(*data));
  if (!data)
    exit(1);

  for (i = 0; i < ENC_LEN; i++)
    data[i] = i % 255;

  silc_timer_synchronize(&timer);

  for (i = 0; silc_default_ciphers[i].name; i++) {
    if (!silc_cipher_alloc(silc_default_ciphers[i].name, &cipher)) {
      fprintf(stderr, "Error allocating %s\n", silc_default_ciphers[i].name);
      exit(1);
    }

    acc_cipher = silc_acc_cipher(SILC_SOFTACC, cipher);
    if (!acc_cipher)
      continue;

    silc_cipher_set_iv(acc_cipher, data);
    silc_cipher_set_key(acc_cipher, data, silc_cipher_get_key_len(cipher),
			TRUE);
    sleep(1);

    rounds = ENC_ROUND;

  retry:
    silc_timer_start(&timer);
    for (k = 0; k < rounds; k++)
      silc_cipher_encrypt(acc_cipher, data, data, ENC_LEN, NULL);
    silc_timer_stop(&timer);

    silc_timer_value(&timer, &sec, &usec);
    totsec = (double)sec;
    totsec += ((double)usec / (double)((double)1000 * (double)1000));
    if (totsec < ENC_MIN_TIME) {
      rounds += rounds;
      goto retry;
    }

    silc_cipher_free(acc_cipher);
    silc_cipher_free(cipher);

    sleep(1);
    printf("%s:\t%.2f KB (%.2f MB, %.2f Mbit) / sec (total %.3f secs)\n",
	   silc_default_ciphers[i].name,
	   (((double)((double)ENC_LEN * (double)rounds) / 1024.0) / totsec),
	   (((double)((double)ENC_LEN * (double)rounds) / (1024.0 *
							   1024.0)) / totsec),
	   ((((double)((double)ENC_LEN * (double)rounds) / 1024.0)
	     / 128.0) / totsec),
	   totsec);
  }

  silc_acc_uninit(SILC_SOFTACC);

  silc_crypto_uninit();
  silc_runtime_uninit();

  return 0;
}
コード例 #9
0
ファイル: test_silctree.c プロジェクト: FabrizioFabbe/runtime
int main(int argc, char **argv)
{
  SilcBool success = FALSE;
  int i;

  silc_runtime_init();

  if (argc > 1 && !strcmp(argv[1], "-d")) {
    silc_log_debug(TRUE);
    silc_log_quick(TRUE);
    silc_log_debug_hexdump(TRUE);
    silc_log_set_debug_string("*tree*");
  }

  for (i = 0; i < NUM; i++) {
    foo[i].id = i + 1;
    foo[i].od = i + 10;
  }

  for (i = 0; i < NUM; i++) {
    foo2[i].id = (i + 1) % (NUM / 4);
    foo2[i].od = (i + 10) % (NUM / 4);
  }

  /* AVL */
  SILC_LOG_DEBUG(("Create AVL tree"));
  if (!silc_tree_init(tree, SILC_TREE_AVL, compare, NULL,
		      silc_offsetof(Foo, header), TRUE))
    goto err;

  /* Populate tree */
  SILC_LOG_DEBUG(("Populate tree, %d entries", NUM));
  for (i = 0; i < NUM; i++)
    if (!silc_tree_add(tree, &foo[i]))
      goto err;

  /* Add duplicates */
  SILC_LOG_DEBUG(("Add duplicates"));
  for (i = 0; i < NUM; i++)
    if (!silc_tree_add(tree, &foo2[i]))
      goto err;

  SILC_LOG_DEBUG(("Tree has %d entries", silc_tree_count(tree)));
  if (silc_tree_count(tree) != NUM + NUM)
    goto err;

  /* Find random */
  for (i = 0; i < NUM; i++) {
    tmp.id = (silc_rand() % NUM) + 1;
    SILC_LOG_DEBUG(("Find entry %d", tmp.id));
    if ((entry = silc_tree_find(tree, &tmp)) == NULL)
      goto err;
    SILC_LOG_DEBUG(("Found entry %p %d", entry, entry->id));
  }

  /* Find non-existing */
  for (i = 0; i < 5; i++) {
    tmp.id = (silc_rand() % NUM) + (i % 2 ? -NUM - 1 : NUM + 1);
    SILC_LOG_DEBUG(("Find entry %d", tmp.id));
    if (silc_tree_find(tree, &tmp))
      goto err;
  }

  /* Enumerate in order */
  for (entry = silc_tree_enumerate(tree, NULL);
       entry;
       entry = silc_tree_enumerate(tree, entry)) {
    SILC_LOG_DEBUG(("Enum entry %d, %p", entry->id, entry));
  }

  /* Delete all */
  for (i = 0; i < NUM; i++) {
    memset(&tmp, 0, sizeof(tmp));
    tmp.id = i + 1;
    SILC_LOG_DEBUG(("Delete entry %d", tmp.id));
    if (!silc_tree_del(tree, &tmp))
      goto err;
  }

  /* Delete remaining duplicates in loop */
  while ((entry = silc_tree_enumerate(tree, NULL))) {
    if (!silc_tree_del(tree, entry))
      goto err;
  }

  SILC_LOG_DEBUG(("Tree has %d entries", silc_tree_count(tree)));
  if (silc_tree_count(tree) != 0)
    goto err;

  success = TRUE;

 err:
  SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE"));
  fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE");

  silc_runtime_uninit();

  return !success;
}
コード例 #10
0
int main(int argc, char **argv)
{
  SilcBool success = FALSE;
  unsigned char *vbuf, *vbuf2;
  unsigned char init[20];
  SilcUInt32 gint, *gintptr;

  if (argc > 1 && !strcmp(argv[1], "-d")) {
    silc_log_debug(TRUE);
    silc_log_quick(TRUE);
    silc_log_debug_hexdump(TRUE);
    silc_log_set_debug_string("*global*");
  }

  SILC_LOG_DEBUG(("Set global var"));
  gint = 100;
  if (!silc_global_set_var("gint", 4, &gint, FALSE))
    goto err;

  SILC_LOG_DEBUG(("Retrieve var"));
  gintptr = silc_global_get_var("gint", FALSE);
  if (!gintptr)
    goto err;
  if (*gintptr != 100)
    goto err;

  SILC_LOG_DEBUG(("Set global var"));
  if (!silc_global_set_var("vbuf", 10, NULL, FALSE))
    goto err;

  SILC_LOG_DEBUG(("Retrieve var"));
  vbuf = silc_global_get_var("vbuf", FALSE);
  if (!vbuf)
    goto err;

  SILC_LOG_DEBUG(("Change value"));
  memset(vbuf, 'F', 10);

  SILC_LOG_DEBUG(("Retrieve var"));
  vbuf = silc_global_get_var("vbuf", FALSE);
  if (!vbuf)
    goto err;
  if (vbuf[0] != 'F')
    goto err;

  SILC_LOG_DEBUG(("Retrieve var (must not find)"));
  vbuf = silc_global_get_var("vbuf2", FALSE);
  if (vbuf)
    goto err;
  SILC_LOG_DEBUG(("Retrieve var (must not find)"));
  vbuf = silc_global_get_var("VBUF", FALSE);
  if (vbuf)
    goto err;
  SILC_LOG_DEBUG(("Retrieve var (must not find)"));
  vbuf = silc_global_get_var("vbuf", TRUE);
  if (vbuf)
    goto err;

  SILC_LOG_DEBUG(("Reset same var"));
  if (!silc_global_set_var("vbuf", 20, NULL, FALSE))
    goto err;
  
  SILC_LOG_DEBUG(("Retrieve var"));
  vbuf = silc_global_get_var("vbuf", FALSE);
  if (!vbuf)
    goto err;

  SILC_LOG_DEBUG(("Change value"));
  memset(vbuf, 'F', 20);

  SILC_LOG_DEBUG(("Retrieve var"));
  vbuf = silc_global_get_var("vbuf", FALSE);
  if (!vbuf)
    goto err;
  if (vbuf[19] != 'F')
    goto err;

  SILC_LOG_DEBUG(("Reset Tls var with initial value"));
  memset(init, 'D', 20);
  if (!silc_global_set_var("vbuf", 20, init, TRUE))
    goto err;
  
  SILC_LOG_DEBUG(("Retrieve var"));
  vbuf2 = silc_global_get_var("vbuf", TRUE);
  if (vbuf == vbuf2)
    goto err;
  if (!vbuf2)
    goto err;
  if (vbuf2[19] != 'D')
    goto err;

  SILC_LOG_DEBUG(("Change value"));
  memset(vbuf2, 'T', 20);

  SILC_LOG_DEBUG(("Retrieve Tls var"));
  vbuf = silc_global_get_var("vbuf", TRUE);
  if (!vbuf)
    goto err;
  if (vbuf[0] != 'T')
    goto err;
 
  SILC_LOG_DEBUG(("Retrieve global var"));
  vbuf = silc_global_get_var("vbuf", FALSE);
  if (!vbuf)
    goto err;
  if (vbuf[19] != 'F')
    goto err;

  SILC_LOG_DEBUG(("Delete global var"));
  silc_global_del_var("vbuf", FALSE);
  SILC_LOG_DEBUG(("Retrieve var (must not find)"));
  vbuf = silc_global_get_var("vbuf", FALSE);
  if (vbuf)
    goto err;

  SILC_LOG_DEBUG(("Delete Tls var"));
  silc_global_del_var("vbuf", TRUE);
  SILC_LOG_DEBUG(("Retrieve var (must not find)"));
  vbuf = silc_global_get_var("vbuf", TRUE);
  if (vbuf)
    goto err;

  success = TRUE;

 err:
  SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE"));
  fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE");

  return !success;
}
コード例 #11
0
ファイル: test_cast5.c プロジェクト: FabrizioFabbe/crypto
int main(int argc, char **argv)
{
  SilcBool success = FALSE;
  SilcCipher cipher, cipher2;
  unsigned char dst[256], pdst[256];
  int i;

  if (argc > 1 && !strcmp(argv[1], "-d")) {
    silc_log_debug(TRUE);
    silc_log_debug_hexdump(TRUE);
    silc_log_set_debug_string("*crypt*,*cast*,*cipher*");
  }

  SILC_LOG_DEBUG(("Registering builtin hash functions"));
  silc_cipher_register_default();

  SILC_LOG_DEBUG(("Allocating cast5-CBC cipher"));
  if (!silc_cipher_alloc("cast5-128-cbc", &cipher)) {
    SILC_LOG_DEBUG(("Allocating cas5-CBC cipher failed"));
    goto err;
  }
  if (!silc_cipher_alloc("cast5-128-cbc", &cipher2)) {
    SILC_LOG_DEBUG(("Allocating cast5-CBC cipher failed"));
    goto err;
  }

  /* First test vector */
  SILC_LOG_DEBUG(("First test vector"));
  memset(dst, 0, sizeof(dst));
  memset(pdst, 0, sizeof(pdst));
  silc_cipher_set_iv(cipher, iv1);
  assert(silc_cipher_set_key(cipher, key1, key1_len, TRUE));
  assert(silc_cipher_set_key(cipher2, key1, key1_len, FALSE));
  assert(silc_cipher_encrypt(cipher, p1, dst, p1_len, NULL));
  SILC_LOG_DEBUG(("block len %d, key len %d, name %s",
		 silc_cipher_get_block_len(cipher),
		 silc_cipher_get_key_len(cipher),
		 silc_cipher_get_name(cipher)));
  SILC_LOG_HEXDUMP(("Plaintext"), (unsigned char *)p1, p1_len);
  SILC_LOG_HEXDUMP(("Ciphertext"), (unsigned char *)dst, p1_len);
  SILC_LOG_HEXDUMP(("Expected ciphertext"), (unsigned char *)c1, p1_len);
  if (memcmp(dst, c1, p1_len)) {
    SILC_LOG_DEBUG(("Encrypt failed"));
    goto err;
  }
  SILC_LOG_DEBUG(("Encrypt is successful"));
  silc_cipher_set_iv(cipher2, iv1);
  assert(silc_cipher_decrypt(cipher2, dst, pdst, p1_len, NULL));
  SILC_LOG_HEXDUMP(("Decrypted plaintext"), (unsigned char *)pdst, p1_len);
  SILC_LOG_HEXDUMP(("Expected plaintext"), (unsigned char *)p1, p1_len);
  if (memcmp(pdst, p1, p1_len)) {
    SILC_LOG_DEBUG(("Decrypt failed"));
    goto err;
  }
  SILC_LOG_DEBUG(("Decrypt is successful"));


  /* Second test vector */
  SILC_LOG_DEBUG(("Second test vector"));
  memset(dst, 0, sizeof(dst));
  memset(pdst, 0, sizeof(pdst));
  silc_cipher_set_iv(cipher, iv2);
  assert(silc_cipher_set_key(cipher, key2, key2_len, TRUE));
  assert(silc_cipher_set_key(cipher2, key2, key2_len, FALSE));
  assert(silc_cipher_encrypt(cipher, p2, dst, p2_len, NULL));
  SILC_LOG_DEBUG(("block len %d, key len %d, name %s",
		 silc_cipher_get_block_len(cipher),
		 silc_cipher_get_key_len(cipher),
		 silc_cipher_get_name(cipher)));
  SILC_LOG_HEXDUMP(("Plaintext"), (unsigned char *)p2, p2_len);
  SILC_LOG_HEXDUMP(("Ciphertext"), (unsigned char *)dst, p2_len);
  SILC_LOG_HEXDUMP(("Expected ciphertext"), (unsigned char *)c2, p2_len);
  if (memcmp(dst, c2, p2_len)) {
    SILC_LOG_DEBUG(("Encrypt failed"));
    goto err;
  }
  SILC_LOG_DEBUG(("Encrypt is successful"));
  silc_cipher_set_iv(cipher2, iv2);
  assert(silc_cipher_decrypt(cipher2, dst, pdst, p2_len, NULL));
  SILC_LOG_HEXDUMP(("Decrypted plaintext"), (unsigned char *)pdst, p2_len);
  SILC_LOG_HEXDUMP(("Expected plaintext"), (unsigned char *)p2, p2_len);
  if (memcmp(pdst, p2, p2_len)) {
    SILC_LOG_DEBUG(("Decrypt failed"));
    goto err;
  }
  SILC_LOG_DEBUG(("Decrypt is successful"));
  silc_cipher_free(cipher);
  silc_cipher_free(cipher2);

  SILC_LOG_DEBUG(("Allocating cast5-128-ctr cipher"));
  if (!silc_cipher_alloc("cast5-128-ctr", &cipher)) {
    SILC_LOG_DEBUG(("Allocating cast5-128-ctr cipher failed"));
    goto err;
  }

  /* Fourth test vector */
  SILC_LOG_DEBUG(("Fourth test vector"));
  memset(dst, 0, sizeof(dst));
  memset(pdst, 0, sizeof(pdst));
  silc_cipher_set_iv(cipher, iv4);
  assert(silc_cipher_set_key(cipher, key4, key4_len, TRUE));
  assert(silc_cipher_encrypt(cipher, p4, dst, p4_len, NULL));
  SILC_LOG_DEBUG(("block len %d, key len %d, name %s",
		 silc_cipher_get_block_len(cipher),
		 silc_cipher_get_key_len(cipher),
		 silc_cipher_get_name(cipher)));
  SILC_LOG_HEXDUMP(("Plaintext"), (unsigned char *)p4, p4_len);
  SILC_LOG_HEXDUMP(("Ciphertext"), (unsigned char *)dst, p4_len);
  SILC_LOG_HEXDUMP(("Expected ciphertext"), (unsigned char *)c4, p4_len);
  if (memcmp(dst, c4, p4_len)) {
    SILC_LOG_DEBUG(("Encrypt failed"));
    goto err;
  }
  SILC_LOG_DEBUG(("Encrypt is successful"));
  silc_cipher_set_iv(cipher, iv4);
  assert(silc_cipher_decrypt(cipher, dst, pdst, p4_len, NULL));
  SILC_LOG_HEXDUMP(("Decrypted plaintext"), (unsigned char *)pdst, p4_len);
  SILC_LOG_HEXDUMP(("Expected plaintext"), (unsigned char *)p4, p4_len);
  if (memcmp(pdst, p4, p4_len)) {
    SILC_LOG_DEBUG(("Decrypt failed"));
    goto err;
  }
  SILC_LOG_DEBUG(("Decrypt is successful"));
  silc_cipher_free(cipher);


  SILC_LOG_DEBUG(("Allocating cast5-128-cfb cipher"));
  if (!silc_cipher_alloc("cast5-128-cfb", &cipher)) {
    SILC_LOG_DEBUG(("Allocating cast5-128-cfb cipher failed"));
    goto err;
  }
  if (!silc_cipher_alloc("cast5-128-cfb", &cipher2)) {
    SILC_LOG_DEBUG(("Allocating cast5-128-cfb cipher failed"));
    goto err;
  }

  SILC_LOG_DEBUG(("CFB test vector"));
  memset(dst, 0, sizeof(dst));
  memset(pdst, 0, sizeof(pdst));
  silc_cipher_set_iv(cipher, iv6);
  assert(silc_cipher_set_key(cipher, key6, key6_len, TRUE));
  assert(silc_cipher_set_key(cipher2, key6, key6_len, FALSE));
  assert(silc_cipher_encrypt(cipher, p6, dst, p6_len, NULL));
  SILC_LOG_DEBUG(("block len %d, key len %d, name %s",
		 silc_cipher_get_block_len(cipher),
		 silc_cipher_get_key_len(cipher),
		 silc_cipher_get_name(cipher)));
  SILC_LOG_HEXDUMP(("Plaintext"), (unsigned char *)p6, p6_len);
  SILC_LOG_HEXDUMP(("Ciphertext"), (unsigned char *)dst, p6_len);
  SILC_LOG_HEXDUMP(("Expected ciphertext"), (unsigned char *)c6, p6_len);
  if (memcmp(dst, c6, p6_len)) {
    SILC_LOG_DEBUG(("Encrypt failed"));
    goto err;
  }
  SILC_LOG_DEBUG(("Encrypt is successful"));
  silc_cipher_set_iv(cipher2, iv6);
  assert(silc_cipher_decrypt(cipher2, dst, pdst, p6_len, NULL));
  SILC_LOG_HEXDUMP(("Decrypted plaintext"), (unsigned char *)pdst, p6_len);
  SILC_LOG_HEXDUMP(("Expected plaintext"), (unsigned char *)p6, p6_len);
  if (memcmp(pdst, p6, p6_len)) {
    SILC_LOG_DEBUG(("Decrypt failed"));
    goto err;
  }
  SILC_LOG_DEBUG(("Decrypt is successful"));
  silc_cipher_free(cipher2);

  success = TRUE;

 err:
  SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE"));
  fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE");

  silc_cipher_unregister_all();
  return success;
}
コード例 #12
0
int main(int argc, char **argv)
{
  SilcBool success = FALSE;
  SilcAtomic32 ref32;
  SilcAtomic16 ref16;
  SilcAtomic8 ref8;
  SilcAtomicPointer refptr;
  SilcUInt8 ret8;
  SilcUInt16 ret16;
  SilcUInt32 ret32;

  if (argc > 1 && !strcmp(argv[1], "-d")) {
    silc_log_debug(TRUE);
    silc_log_debug_hexdump(TRUE);
    silc_log_set_debug_string("*atomic*,*errno*");
  }

  silc_atomic_init8(&ref8, 1);
  silc_atomic_init16(&ref16, 1);
  silc_atomic_init32(&ref32, 1);
  silc_atomic_init_pointer(&refptr, SILC_32_TO_PTR(0xdeadbeef));

  ret8 = silc_atomic_add_int8(&ref8, 7);
  SILC_LOG_DEBUG(("ref8: 1 + 7 = %d (8)", ret8));
  ret8 = silc_atomic_add_int8(&ref8, 3);
  SILC_LOG_DEBUG(("ref8: 8 + 3 = %d (11)", ret8));
  ret8 = silc_atomic_sub_int8(&ref8, 10);
  SILC_LOG_DEBUG(("ref8: 11 - 10 = %d (1)", ret8));

  ret16 = silc_atomic_add_int16(&ref16, 1);
  SILC_LOG_DEBUG(("ref16: 1 + 1 = %d (2)", ret16));
  ret16 = silc_atomic_add_int16(&ref16, 31020);
  SILC_LOG_DEBUG(("ref16: 2 + 31020 = %d (31022)", ret16));
  ret16 = silc_atomic_add_int16(&ref16, 34000);
  SILC_LOG_DEBUG(("ref16: 31022 + 34000 = %d (65022)", ret16));
  ret16 = silc_atomic_sub_int16(&ref16, 0);
  SILC_LOG_DEBUG(("ref16: 65022 - 0 = %d (65022)", ret16));
  ret16 = silc_atomic_sub_int16(&ref16, (SilcInt16)0xffff);
  SILC_LOG_DEBUG(("ref16: 65022 - 0xffff = %d (65023) (underflow)", ret16));

  SILC_LOG_DEBUG(("Current value: %d (-513)",
		  (SilcInt16)silc_atomic_get_int16(&ref16)));

  SILC_LOG_DEBUG(("Swapping -513 with 57392"));
  if (!silc_atomic_cas16(&ref16, silc_atomic_get_int16(&ref16), 57392))
    goto err;
  SILC_LOG_DEBUG(("Current value: %d (57392)",
		  silc_atomic_get_int16(&ref16)));
  SILC_LOG_DEBUG(("Swapping 57392 with -500"));
  if (!silc_atomic_cas16(&ref16, silc_atomic_get_int16(&ref16), -500))
    goto err;
  SILC_LOG_DEBUG(("Current value: %d (-500)",
		  (SilcInt16)silc_atomic_get_int16(&ref16)));

  ret32 = silc_atomic_add_int32(&ref32, 1);
  SILC_LOG_DEBUG(("ref32: 1 + 1 = %d (2)", ret32));
  ret32 = silc_atomic_add_int32(&ref32, 310200);
  SILC_LOG_DEBUG(("ref32: 2 + 310200 = %d (310202)", ret32));
  ret32 = silc_atomic_add_int32(&ref32, 34000000);
  SILC_LOG_DEBUG(("ref32: 310202 + 34000000 = %d (34310202)", ret32));
  ret32 = silc_atomic_sub_int32(&ref32, 0);
  SILC_LOG_DEBUG(("ref32: 34310202 - 0 = %d (34310202)", ret32));
  ret32 = silc_atomic_sub_int32(&ref32, 0xfffffff);
  SILC_LOG_DEBUG(("ref32: 34310202 - 0xfffffff = %d (-234125253) "
		  "(underflow)", ret32));

  SILC_LOG_DEBUG(("Current value: %d (-234125253)",
		  silc_atomic_get_int32(&ref32)));

  SILC_LOG_DEBUG(("Swapping -234125253 with 76327681"));
  if (!silc_atomic_cas32(&ref32, silc_atomic_get_int32(&ref32), 76327681))
    goto err;
  SILC_LOG_DEBUG(("Current value: %d (76327681)",
		  silc_atomic_get_int32(&ref32)));

  SILC_LOG_DEBUG(("Current ptr: %p (0xdeadbeef)",
		  silc_atomic_get_pointer(&refptr)));
  SILC_LOG_DEBUG(("Swapping %p with NULL", silc_atomic_get_pointer(&refptr)));
  if (!silc_atomic_cas_pointer(&refptr,
			       silc_atomic_get_pointer(&refptr), NULL))
    goto err;
  SILC_LOG_DEBUG(("Current ptr: %p (NULL)",
		  silc_atomic_get_pointer(&refptr)));

  SILC_LOG_DEBUG(("Setting val 34322111 (32-bit)"));
  silc_atomic_set_int32(&ref32, 34322111);
  if (silc_atomic_get_int32(&ref32) != 34322111)
    goto err;
  SILC_LOG_DEBUG(("Setting val 1432211119 (32-bit)"));
  silc_atomic_set_int32(&ref32, 1432211119);
  if (silc_atomic_get_int32(&ref32) != 1432211119)
    goto err;
  SILC_LOG_DEBUG(("Setting val 23422 (16-bit)"));
  silc_atomic_set_int16(&ref16, 23422);
  if (silc_atomic_get_int16(&ref16) != 23422)
    goto err;
  SILC_LOG_DEBUG(("Setting val 124 (8-bit)"));
  silc_atomic_set_int8(&ref8, 124);
  if (silc_atomic_get_int8(&ref8) != 124)
    goto err;

  silc_atomic_uninit8(&ref8);
  silc_atomic_uninit16(&ref16);
  silc_atomic_uninit32(&ref32);
  silc_atomic_uninit_pointer(&refptr);

  success = TRUE;

 err:
  SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE"));
  fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE");

  return !success;
}
コード例 #13
0
int main(int argc, char **argv)
{
  SilcBool success = FALSE;
  unsigned char *data, iv[SILC_CIPHER_MAX_IV_SIZE];
  SilcUInt32 i, k;

  silc_runtime_init();
  silc_crypto_init(NULL);

  if (argc > 1 && !strcmp(argv[1], "-d")) {
    silc_log_debug(TRUE);
    silc_log_debug_hexdump(TRUE);
    silc_log_set_debug_string("*acc*,*cipher*,*twofish*");
  }

  if (!silc_acc_init(SILC_SOFTACC, (void *)0x01, "min_threads", 2,
		     "max_threads", 8, NULL))
    exit(1);

  data = malloc(ENC_LEN * sizeof(*data));
  if (!data)
    exit(1);

  /* Plaintext */
  for (i = 0; i < ENC_LEN; i++)
    data[i] = i % 255;
  SILC_LOG_HEXDUMP(("data"), data, ENC_LEN);

  /* IV */
  for (i = 0; i < SILC_CIPHER_MAX_IV_SIZE; i++)
    iv[i] = i % 255;

  SILC_LOG_HEXDUMP(("IV"), iv, SILC_CIPHER_MAX_IV_SIZE);

  for (i = 0; silc_default_ciphers[i].name; i++) {
    if (!silc_cipher_alloc(silc_default_ciphers[i].name, &enc_cipher)) {
      fprintf(stderr, "Error allocating %s\n", silc_default_ciphers[i].name);
      exit(1);
    }
    if (!silc_cipher_alloc(silc_default_ciphers[i].name, &dec_cipher)) {
      fprintf(stderr, "Error allocating %s\n", silc_default_ciphers[i].name);
      exit(1);
    }

    enc_acc_cipher = silc_acc_cipher(SILC_SOFTACC, enc_cipher);
    if (!enc_acc_cipher)
      continue;
    dec_acc_cipher = silc_acc_cipher(SILC_SOFTACC, dec_cipher);
    if (!dec_acc_cipher)
      continue;

    SILC_LOG_DEBUG(("Allocated cipher %s", silc_default_ciphers[i].name));

    SILC_LOG_DEBUG(("Set key"));
    silc_cipher_set_key(enc_acc_cipher, data,
			silc_cipher_get_key_len(enc_cipher),
			TRUE);
    silc_cipher_set_key(dec_acc_cipher, data,
			silc_cipher_get_key_len(dec_cipher),
			FALSE);


    SILC_LOG_DEBUG(("Set IV"));
    silc_cipher_set_iv(enc_acc_cipher, iv);

    SILC_LOG_DEBUG(("Encrypt with accelerated cipher"));
    for (k = 0; k < ENC_ROUND; k++)
      silc_cipher_encrypt(enc_acc_cipher, data, data, ENC_LEN, NULL);
    SILC_LOG_HEXDUMP(("data"), data, ENC_LEN);

    SILC_LOG_DEBUG(("Set IV"));
    silc_cipher_set_iv(dec_cipher, iv);

    SILC_LOG_DEBUG(("Decrypt with associated cipher"));
    for (k = 0; k < ENC_ROUND; k++)
      silc_cipher_decrypt(dec_cipher, data, data, ENC_LEN, NULL);
    SILC_LOG_HEXDUMP(("data"), data, ENC_LEN);

    /* Verify */
    SILC_LOG_DEBUG(("Verify"));
    for (k = 0; k < ENC_LEN; k++)
      if (data[k] != k % 255)
	goto err;
    SILC_LOG_DEBUG(("Ok"));


    SILC_LOG_DEBUG(("Set IV"));
    silc_cipher_set_iv(enc_cipher, iv);

    SILC_LOG_DEBUG(("Encrypt with associated cipher"));
    for (k = 0; k < ENC_ROUND; k++)
      silc_cipher_encrypt(enc_cipher, data, data, ENC_LEN, NULL);
    SILC_LOG_HEXDUMP(("data"), data, ENC_LEN);

    SILC_LOG_DEBUG(("Set IV"));
    silc_cipher_set_iv(dec_acc_cipher, iv);

    SILC_LOG_DEBUG(("Decrypt with accelerated cipher"));
    for (k = 0; k < ENC_ROUND; k++)
      silc_cipher_decrypt(dec_acc_cipher, data, data, ENC_LEN, NULL);
    SILC_LOG_HEXDUMP(("data"), data, ENC_LEN);

    /* Verify */
    SILC_LOG_DEBUG(("Verify"));
    for (k = 0; k < ENC_LEN; k++)
      if (data[k] != k % 255)
	goto err;
    SILC_LOG_DEBUG(("Ok"));


    SILC_LOG_DEBUG(("Set IV"));
    silc_cipher_set_iv(enc_acc_cipher, iv);

    SILC_LOG_DEBUG(("Encrypt with accelerated cipher"));
    for (k = 0; k < ENC_ROUND; k++)
      silc_cipher_encrypt(enc_acc_cipher, data, data, ENC_LEN, NULL);
    SILC_LOG_HEXDUMP(("data"), data, ENC_LEN);

    SILC_LOG_DEBUG(("Set IV"));
    silc_cipher_set_iv(dec_acc_cipher, iv);

    SILC_LOG_DEBUG(("Decrypt with accelerated cipher"));
    for (k = 0; k < ENC_ROUND; k++)
      silc_cipher_decrypt(dec_acc_cipher, data, data, ENC_LEN, NULL);
    SILC_LOG_HEXDUMP(("data"), data, ENC_LEN);

    /* Verify */
    SILC_LOG_DEBUG(("Verify"));
    for (k = 0; k < ENC_LEN; k++)
      if (data[k] != k % 255)
	goto err;
    SILC_LOG_DEBUG(("Ok"));


    SILC_LOG_DEBUG(("Set IV"));
    silc_cipher_set_iv(enc_cipher, iv);

    SILC_LOG_DEBUG(("Encrypt with associated cipher"));
    for (k = 0; k < ENC_ROUND; k++)
      silc_cipher_encrypt(enc_cipher, data, data, ENC_LEN, NULL);
    SILC_LOG_HEXDUMP(("data"), data, ENC_LEN);

    SILC_LOG_DEBUG(("Set IV"));
    silc_cipher_set_iv(dec_cipher, iv);

    SILC_LOG_DEBUG(("Decrypt with associated cipher"));
    for (k = 0; k < ENC_ROUND; k++)
      silc_cipher_decrypt(dec_cipher, data, data, ENC_LEN, NULL);
    SILC_LOG_HEXDUMP(("data"), data, ENC_LEN);

    /* Verify */
    SILC_LOG_DEBUG(("Verify"));
    for (k = 0; k < ENC_LEN; k++)
      if (data[k] != k % 255)
	goto err;
    SILC_LOG_DEBUG(("Ok"));


    silc_cipher_free(enc_acc_cipher);
    silc_cipher_free(enc_cipher);
    silc_cipher_free(dec_acc_cipher);
    silc_cipher_free(dec_cipher);
  }

  silc_acc_uninit(SILC_SOFTACC);

  success = TRUE;

 err:
  SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE"));
  fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE");

  silc_crypto_uninit();
  silc_runtime_uninit();

  return !success;
}
コード例 #14
0
ファイル: test_hmacsha1.c プロジェクト: FabrizioFabbe/crypto
int main(int argc, char **argv)
{
  SilcBool success = FALSE;
  unsigned char digest[20];
  SilcUInt32 len;
  SilcHmac hmac;
  
  if (argc > 1 && !strcmp(argv[1], "-d")) {
    silc_log_debug(TRUE);
    silc_log_debug_hexdump(TRUE);
    silc_log_set_debug_string("*crypt*,*hash*,*sha1*,*hmac*");
  }

  SILC_LOG_DEBUG(("Registering builtin hash functions"));
  silc_hash_register_default();
  silc_hmac_register_default();

  SILC_LOG_DEBUG(("Allocating sha1 HMAC"));
  if (!silc_hmac_alloc("hmac-sha1", NULL, &hmac)) {
    SILC_LOG_DEBUG(("Allocating sha1 HMAC failed"));
    goto err;
  }

  /* First test vector */
  SILC_LOG_DEBUG(("First test vector"));
  silc_hmac_init_with_key(hmac, key1, key1_len);
  silc_hmac_update(hmac, data1, strlen(data1));
  silc_hmac_final(hmac, digest, &len);
  SILC_LOG_HEXDUMP(("Key"), (unsigned char *)key1, key1_len);
  SILC_LOG_HEXDUMP(("Message"), (unsigned char *)data1, strlen(data1));
  SILC_LOG_HEXDUMP(("Digest"), digest, len);
  SILC_LOG_HEXDUMP(("Expected digest"), (unsigned char *)data1_digest, len);
  if (memcmp(digest, data1_digest, len)) {
    SILC_LOG_DEBUG(("HMAC failed"));
    goto err;
  }
  SILC_LOG_DEBUG(("HMAC is successful"));
  
  /* Second test vector */
  SILC_LOG_DEBUG(("Second test vector"));
  silc_hmac_init_with_key(hmac, key2, key2_len);
  silc_hmac_update(hmac, data2, strlen(data2));
  silc_hmac_final(hmac, digest, &len);
  SILC_LOG_HEXDUMP(("Key"), (unsigned char *)key2, key2_len);
  SILC_LOG_HEXDUMP(("Message"), (unsigned char *)data2, strlen(data2));
  SILC_LOG_HEXDUMP(("Digest"), digest, len);
  SILC_LOG_HEXDUMP(("Expected digest"), (unsigned char *)data2_digest, len);
  if (memcmp(digest, data2_digest, len)) {
    SILC_LOG_DEBUG(("HMAC failed"));
    goto err;
  }
  SILC_LOG_DEBUG(("HMAC is successful"));
  
  /* Third test vector */
  SILC_LOG_DEBUG(("Third test vector"));
  silc_hmac_init_with_key(hmac, key3, key3_len);
  memset(data3, '\xdd', sizeof(data3));
  silc_hmac_update(hmac, data3, sizeof(data3));
  silc_hmac_final(hmac, digest, &len);
  SILC_LOG_HEXDUMP(("Key"), (unsigned char *)key3, key3_len);
  SILC_LOG_HEXDUMP(("Message"), (unsigned char *)data3, sizeof(data3));
  SILC_LOG_HEXDUMP(("Digest"), digest, len);
  SILC_LOG_HEXDUMP(("Expected digest"), (unsigned char *)data3_digest, len);
  if (memcmp(digest, data3_digest, len)) {
    SILC_LOG_DEBUG(("HMAC failed"));
    goto err;
  }
  SILC_LOG_DEBUG(("HMAC is successful"));
  
  /* Fourth test vector */
  SILC_LOG_DEBUG(("Fourth test vector"));
  memset(key4, '\xaa', key4_len);
  silc_hmac_init_with_key(hmac, key4, key4_len);
  silc_hmac_update(hmac, data4, strlen(data4));
  silc_hmac_final(hmac, digest, &len);
  SILC_LOG_HEXDUMP(("Key"), (unsigned char *)key4, key4_len);
  SILC_LOG_HEXDUMP(("Message"), (unsigned char *)data4, sizeof(data4));
  SILC_LOG_HEXDUMP(("Digest"), digest, len);
  SILC_LOG_HEXDUMP(("Expected digest"), (unsigned char *)data4_digest, len);
  if (memcmp(digest, data4_digest, len)) {
    SILC_LOG_DEBUG(("HMAC failed"));
    goto err;
  }
  SILC_LOG_DEBUG(("HMAC is successful"));
  
  success = TRUE;
  
 err:
  SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE"));
  fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE");

  silc_hmac_free(hmac);
  silc_hash_unregister_all();
  silc_hmac_unregister_all();
  return success;
}
コード例 #15
0
ファイル: test_silcmessage.c プロジェクト: FabrizioFabbe/silc
int main(int argc, char **argv)
{
  SilcBool success = FALSE;
  SilcMessagePayload message;
  SilcBuffer buf;
  const char *msg = "FOOBAR MESSAGE";
  unsigned char *data, tmp[1023], *tmp2;
  SilcUInt32 data_len;
  SilcUInt16 flags;
  int i, n;

  if (argc > 1 && !strcmp(argv[1], "-d")) {
    silc_log_debug(TRUE);
    silc_log_debug_hexdump(TRUE);
    silc_log_set_debug_string("*message*");
  }

  silc_cipher_register_default();
  silc_hash_register_default();
  silc_hmac_register_default();
  silc_pkcs_register_default();

  SILC_LOG_DEBUG(("Load keypair"));
  if (!silc_load_key_pair("pubkey.pub", "privkey.prv", "",
			  &public_key, &private_key)) {
    SILC_LOG_DEBUG(("Create keypair"));
    if (!silc_create_key_pair("rsa", 2048, "pubkey.pub", "privkey.prv",
			      NULL, "", &public_key, &private_key, FALSE))
      goto err;
  }

  SILC_LOG_DEBUG(("Alloc RNG"));
  rng = silc_rng_alloc();
  silc_rng_init(rng);

  SILC_LOG_DEBUG(("Alloc AES"));
  if (!silc_cipher_alloc("aes-128-cbc", &key))
    goto err;

  SILC_LOG_DEBUG(("Alloc SHA-256"));
  if (!silc_hash_alloc("sha256", &hash))
    goto err;

  SILC_LOG_DEBUG(("Alloc HMAC"));
  if (!silc_hmac_alloc("hmac-sha256-96", hash, &hmac))
    goto err;

  SILC_LOG_DEBUG(("Set static key: '1234567890123456'"));
  if (!silc_cipher_set_key(key, "1234567890123456", 16 * 8))
    goto err;
  SILC_LOG_DEBUG(("Set HMAC key: '1234567890123456'"));
  silc_hmac_set_key(hmac, "1234567890123456", 16);

  /* Simple private message */
  SILC_LOG_DEBUG(("Encoding private message len %d (static key)",
		  strlen(msg)));
  buf = silc_message_payload_encode(SILC_MESSAGE_FLAG_ACTION |
				    SILC_MESSAGE_FLAG_UTF8 |
				    SILC_MESSAGE_FLAG_ACK,
				    msg, strlen(msg), TRUE, TRUE,
				    key, hmac, rng, NULL, NULL, NULL, NULL);
  if (!buf)
    goto err;
  SILC_LOG_HEXDUMP(("message"), buf->data, silc_buffer_len(buf));
  SILC_LOG_DEBUG(("Parsing private messsage (static key)"));
  message = silc_message_payload_parse(silc_buffer_data(buf),
				       silc_buffer_len(buf), TRUE, TRUE,
				       key, hmac, NULL, FALSE, NULL);
  if (!message)
    goto err;
  flags = silc_message_get_flags(message);
  SILC_LOG_DEBUG(("Flags: %x", flags));
  if (!(flags & SILC_MESSAGE_FLAG_ACTION))
    goto err;
  if (!(flags & SILC_MESSAGE_FLAG_UTF8))
    goto err;
  if (!(flags & SILC_MESSAGE_FLAG_ACK))
    goto err;
  data = silc_message_get_data(message, &data_len);
  SILC_LOG_HEXDUMP(("Data"), data, data_len);
  if (data_len != strlen(msg) || memcmp(data, msg, strlen(msg)))
    goto err;
  SILC_LOG_HEXDUMP(("MAC"), silc_message_get_mac(message),
		   silc_hmac_len(hmac));
  silc_message_payload_free(message);

  /* Simple private message */
  n = 10;
  SILC_LOG_DEBUG(("Encoding private message len %d (static key)", n));
  buf = silc_message_payload_encode(SILC_MESSAGE_FLAG_ACTION |
				    SILC_MESSAGE_FLAG_UTF8 |
				    SILC_MESSAGE_FLAG_ACK,
				    msg, n, TRUE, TRUE,
				    key, hmac, rng, NULL, NULL, NULL, buf);
  if (!buf)
    goto err;
  SILC_LOG_HEXDUMP(("message"), buf->data, silc_buffer_len(buf));
  SILC_LOG_DEBUG(("Parsing private messsage (static key)"));
  message = silc_message_payload_parse(silc_buffer_data(buf),
				       silc_buffer_len(buf), TRUE, TRUE,
				       key, hmac, NULL, FALSE, NULL);
  if (!message)
    goto err;
  flags = silc_message_get_flags(message);
  SILC_LOG_DEBUG(("Flags: %x", flags));
  if (!(flags & SILC_MESSAGE_FLAG_ACTION))
    goto err;
  if (!(flags & SILC_MESSAGE_FLAG_UTF8))
    goto err;
  if (!(flags & SILC_MESSAGE_FLAG_ACK))
    goto err;
  data = silc_message_get_data(message, &data_len);
  SILC_LOG_HEXDUMP(("Data"), data, data_len);
  if (data_len != n || memcmp(data, msg, n))
    goto err;
  SILC_LOG_HEXDUMP(("MAC"), silc_message_get_mac(message),
		   silc_hmac_len(hmac));
  silc_message_payload_free(message);

  /* Simple private message */
  n = 1;
  SILC_LOG_DEBUG(("Encoding private message len %d (static key)", n));
  buf = silc_message_payload_encode(SILC_MESSAGE_FLAG_ACTION |
				    SILC_MESSAGE_FLAG_UTF8 |
				    SILC_MESSAGE_FLAG_ACK,
				    msg, n, TRUE, TRUE,
				    key, hmac, rng, NULL, NULL, NULL, buf);
  if (!buf)
    goto err;
  SILC_LOG_HEXDUMP(("message"), buf->data, silc_buffer_len(buf));
  SILC_LOG_DEBUG(("Parsing private messsage (static key)"));
  message = silc_message_payload_parse(silc_buffer_data(buf),
				       silc_buffer_len(buf), TRUE, TRUE,
				       key, hmac, NULL, FALSE, NULL);
  if (!message)
    goto err;
  flags = silc_message_get_flags(message);
  SILC_LOG_DEBUG(("Flags: %x", flags));
  if (!(flags & SILC_MESSAGE_FLAG_ACTION))
    goto err;
  if (!(flags & SILC_MESSAGE_FLAG_UTF8))
    goto err;
  if (!(flags & SILC_MESSAGE_FLAG_ACK))
    goto err;
  data = silc_message_get_data(message, &data_len);
  SILC_LOG_HEXDUMP(("Data"), data, data_len);
  if (data_len != n || memcmp(data, msg, n))
    goto err;
  SILC_LOG_HEXDUMP(("MAC"), silc_message_get_mac(message),
		   silc_hmac_len(hmac));
  silc_message_payload_free(message);

  /* Simple private message */
  for (i = 0; i < sizeof(tmp); i++)
    tmp[i] = (32 + i) & 127;
  SILC_LOG_DEBUG(("Encoding private message len %d (static key)",
		  sizeof(tmp)));
  buf = silc_message_payload_encode(SILC_MESSAGE_FLAG_ACTION |
				    SILC_MESSAGE_FLAG_UTF8 |
				    SILC_MESSAGE_FLAG_ACK,
				    tmp, sizeof(tmp), TRUE, TRUE,
				    key, hmac, rng, NULL, NULL, NULL, buf);
  if (!buf)
    goto err;
  SILC_LOG_HEXDUMP(("message"), buf->data, silc_buffer_len(buf));
  SILC_LOG_DEBUG(("Parsing private messsage (static key)"));
  message = silc_message_payload_parse(silc_buffer_data(buf),
				       silc_buffer_len(buf), TRUE, TRUE,
				       key, hmac, NULL, FALSE, NULL);
  if (!message)
    goto err;
  flags = silc_message_get_flags(message);
  SILC_LOG_DEBUG(("Flags: %x", flags));
  if (!(flags & SILC_MESSAGE_FLAG_ACTION))
    goto err;
  if (!(flags & SILC_MESSAGE_FLAG_UTF8))
    goto err;
  if (!(flags & SILC_MESSAGE_FLAG_ACK))
    goto err;
  data = silc_message_get_data(message, &data_len);
  SILC_LOG_HEXDUMP(("Data"), data, data_len);
  if (data_len != sizeof(tmp) || memcmp(data, tmp, sizeof(tmp)))
    goto err;
  SILC_LOG_HEXDUMP(("MAC"), silc_message_get_mac(message),
		   silc_hmac_len(hmac));
  silc_message_payload_free(message);

  /* Digitally signed private message */
  for (i = 0; i < sizeof(tmp); i++)
    tmp[i] = (32 + i) & 127;
  SILC_LOG_DEBUG(("Encoding private message len %d (static key) SIGNED",
		  sizeof(tmp)));
  buf = silc_message_payload_encode(SILC_MESSAGE_FLAG_ACTION |
				    SILC_MESSAGE_FLAG_UTF8 |
				    SILC_MESSAGE_FLAG_ACK |
				    SILC_MESSAGE_FLAG_SIGNED,
				    tmp, sizeof(tmp), TRUE, TRUE,
				    key, hmac, rng,
				    public_key, private_key, hash, buf);
  if (!buf)
    goto err;
  SILC_LOG_HEXDUMP(("message"), buf->data, silc_buffer_len(buf));
  SILC_LOG_DEBUG(("Parsing private messsage (static key)"));
  message = silc_message_payload_parse(silc_buffer_data(buf),
				       silc_buffer_len(buf), TRUE, TRUE,
				       key, hmac, NULL, FALSE, NULL);
  if (!message)
    goto err;
  flags = silc_message_get_flags(message);
  SILC_LOG_DEBUG(("Flags: %x", flags));
  if (!(flags & SILC_MESSAGE_FLAG_ACTION))
    goto err;
  if (!(flags & SILC_MESSAGE_FLAG_UTF8))
    goto err;
  if (!(flags & SILC_MESSAGE_FLAG_ACK))
    goto err;
  if (!(flags & SILC_MESSAGE_FLAG_SIGNED))
    goto err;
  data = silc_message_get_data(message, &data_len);
  SILC_LOG_HEXDUMP(("Data"), data, data_len);
  if (data_len != sizeof(tmp) || memcmp(data, tmp, sizeof(tmp)))
    goto err;
  SILC_LOG_HEXDUMP(("MAC"), silc_message_get_mac(message),
		   silc_hmac_len(hmac));
  SILC_LOG_DEBUG(("Verifying signature"));
  if (silc_message_signed_verify(message, public_key, hash) !=
      SILC_AUTH_OK)
    goto err;
  SILC_LOG_DEBUG(("Signature Ok"));
  SILC_LOG_DEBUG(("Get public key"));
  pk2 = silc_message_signed_get_public_key(message, NULL, NULL);
  if (!pk2)
    goto err;
  SILC_LOG_DEBUG(("Verify public key"));
  if (!silc_pkcs_public_key_compare(public_key, pk2))
    goto err;
  SILC_LOG_DEBUG(("Public key Ok"));
  silc_pkcs_public_key_free(pk2);
  silc_message_payload_free(message);

  /* Digitally signed channel message */
  for (i = 0; i < sizeof(tmp) / 2; i++)
    tmp[i] = (32 + i) & 127;
  SILC_LOG_DEBUG(("Encoding channel message len %d (static key) SIGNED",
		  sizeof(tmp) / 2));
  buf = silc_message_payload_encode(SILC_MESSAGE_FLAG_ACTION |
				    SILC_MESSAGE_FLAG_UTF8 |
				    SILC_MESSAGE_FLAG_ACK |
				    SILC_MESSAGE_FLAG_SIGNED,
				    tmp, sizeof(tmp) / 2, TRUE, FALSE,
				    key, hmac, rng,
				    public_key, private_key, hash, buf);
  if (!buf)
    goto err;
  SILC_LOG_HEXDUMP(("message"), buf->data, silc_buffer_len(buf));
  SILC_LOG_DEBUG(("Parsing channel messsage (static key)"));
  message = silc_message_payload_parse(silc_buffer_data(buf),
				       silc_buffer_len(buf), FALSE, TRUE,
				       key, hmac, NULL, FALSE, NULL);
  if (!message)
    goto err;
  flags = silc_message_get_flags(message);
  SILC_LOG_DEBUG(("Flags: %x", flags));
  if (!(flags & SILC_MESSAGE_FLAG_ACTION))
    goto err;
  if (!(flags & SILC_MESSAGE_FLAG_UTF8))
    goto err;
  if (!(flags & SILC_MESSAGE_FLAG_ACK))
    goto err;
  if (!(flags & SILC_MESSAGE_FLAG_SIGNED))
    goto err;
  data = silc_message_get_data(message, &data_len);
  SILC_LOG_HEXDUMP(("Data"), data, data_len);
  if (data_len != sizeof(tmp) / 2 || memcmp(data, tmp, sizeof(tmp) / 2))
    goto err;
  SILC_LOG_HEXDUMP(("MAC"), silc_message_get_mac(message),
		   silc_hmac_len(hmac));
  SILC_LOG_DEBUG(("Verifying signature"));
  if (silc_message_signed_verify(message, public_key, hash) !=
      SILC_AUTH_OK)
    goto err;
  SILC_LOG_DEBUG(("Signature Ok"));
  SILC_LOG_DEBUG(("Get public key"));
  pk2 = silc_message_signed_get_public_key(message, NULL, NULL);
  if (!pk2)
    goto err;
  SILC_LOG_DEBUG(("Verify public key"));
  if (!silc_pkcs_public_key_compare(public_key, pk2))
    goto err;
  SILC_LOG_DEBUG(("Public key Ok"));
  silc_pkcs_public_key_free(pk2);
  silc_message_payload_free(message);

  /* Digitally signed private message (no encryption) */
  for (i = 0; i < sizeof(tmp) / 2; i++)
    tmp[i] = (32 + i) & 127;
  SILC_LOG_DEBUG(("Encoding private message len %d SIGNED",
		  sizeof(tmp) / 2));
  buf = silc_message_payload_encode(SILC_MESSAGE_FLAG_ACTION |
				    SILC_MESSAGE_FLAG_UTF8 |
				    SILC_MESSAGE_FLAG_ACK |
				    SILC_MESSAGE_FLAG_SIGNED,
				    tmp, sizeof(tmp) / 2, FALSE, TRUE,
				    NULL, NULL, rng,
				    public_key, private_key, hash, buf);
  if (!buf)
    goto err;
  SILC_LOG_HEXDUMP(("message"), buf->data, silc_buffer_len(buf));
  SILC_LOG_DEBUG(("Parsing private messsage (static key)"));
  message = silc_message_payload_parse(silc_buffer_data(buf),
				       silc_buffer_len(buf), TRUE, FALSE,
				       NULL, NULL, NULL, FALSE, NULL);
  if (!message)
    goto err;
  flags = silc_message_get_flags(message);
  SILC_LOG_DEBUG(("Flags: %x", flags));
  if (!(flags & SILC_MESSAGE_FLAG_ACTION))
    goto err;
  if (!(flags & SILC_MESSAGE_FLAG_UTF8))
    goto err;
  if (!(flags & SILC_MESSAGE_FLAG_ACK))
    goto err;
  if (!(flags & SILC_MESSAGE_FLAG_SIGNED))
    goto err;
  data = silc_message_get_data(message, &data_len);
  SILC_LOG_HEXDUMP(("Data"), data, data_len);
  if (data_len != sizeof(tmp) / 2 || memcmp(data, tmp, sizeof(tmp) / 2))
    goto err;
  SILC_LOG_DEBUG(("Verifying signature"));
  if (silc_message_signed_verify(message, public_key, hash) !=
      SILC_AUTH_OK)
    goto err;
  SILC_LOG_DEBUG(("Signature Ok"));
  SILC_LOG_DEBUG(("Get public key"));
  pk2 = silc_message_signed_get_public_key(message, NULL, NULL);
  if (!pk2)
    goto err;
  SILC_LOG_DEBUG(("Verify public key"));
  if (!silc_pkcs_public_key_compare(public_key, pk2))
    goto err;
  SILC_LOG_DEBUG(("Public key Ok"));
  silc_pkcs_public_key_free(pk2);
  silc_message_payload_free(message);

  /* Digitally signed channel message (LARGE) */
  n = 65550;
  tmp2 = silc_malloc(n);
  if (!tmp2)
    goto err;
  SILC_LOG_DEBUG(("Encoding channel message len %d (static key) SIGNED LARGE",
		  n));
  buf = silc_message_payload_encode(SILC_MESSAGE_FLAG_ACTION |
				    SILC_MESSAGE_FLAG_UTF8 |
				    SILC_MESSAGE_FLAG_ACK |
				    SILC_MESSAGE_FLAG_SIGNED,
				    tmp2, n, TRUE, FALSE,
				    key, hmac, rng,
				    public_key, private_key, hash, buf);
  if (!buf)
    goto err;
  SILC_LOG_DEBUG(("Message length: %d", silc_buffer_len(buf)));
  if (silc_buffer_len(buf) > SILC_PACKET_MAX_LEN)
    goto err;
  SILC_LOG_DEBUG(("Parsing channel messsage (static key)"));
  message = silc_message_payload_parse(silc_buffer_data(buf),
				       silc_buffer_len(buf), FALSE, TRUE,
				       key, hmac, NULL, FALSE, NULL);
  if (!message)
    goto err;
  flags = silc_message_get_flags(message);
  SILC_LOG_DEBUG(("Flags: %x", flags));
  if (!(flags & SILC_MESSAGE_FLAG_ACTION))
    goto err;
  if (!(flags & SILC_MESSAGE_FLAG_UTF8))
    goto err;
  if (!(flags & SILC_MESSAGE_FLAG_ACK))
    goto err;
  if (!(flags & SILC_MESSAGE_FLAG_SIGNED))
    goto err;
  data = silc_message_get_data(message, &data_len);
  SILC_LOG_DEBUG(("Data len: %d", data_len));
  if (silc_buffer_len(buf) > SILC_PACKET_MAX_LEN)
    goto err;
  SILC_LOG_HEXDUMP(("MAC"), silc_message_get_mac(message),
		   silc_hmac_len(hmac));
  SILC_LOG_DEBUG(("Verifying signature"));
  if (silc_message_signed_verify(message, public_key, hash) !=
      SILC_AUTH_OK)
    goto err;
  SILC_LOG_DEBUG(("Signature Ok"));
  SILC_LOG_DEBUG(("Get public key"));
  pk2 = silc_message_signed_get_public_key(message, NULL, NULL);
  if (!pk2)
    goto err;
  SILC_LOG_DEBUG(("Verify public key"));
  if (!silc_pkcs_public_key_compare(public_key, pk2))
    goto err;
  SILC_LOG_DEBUG(("Public key Ok"));
  silc_pkcs_public_key_free(pk2);
  silc_message_payload_free(message);
  silc_free(tmp2);


  success = TRUE;
  SILC_LOG_DEBUG(("Cleanup"));
  silc_pkcs_public_key_free(public_key);
  silc_pkcs_private_key_free(private_key);
  silc_cipher_free(key);
  silc_hash_free(hash);
  silc_rng_free(rng);

 err:
  silc_cipher_unregister_all();
  silc_hash_unregister_all();
  silc_hmac_unregister_all();
  silc_pkcs_unregister_all();

  SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE"));
  fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE");

  return success;
}
コード例 #16
0
ファイル: test_sha256.c プロジェクト: FabrizioFabbe/crypto
int main(int argc, char **argv)
{
  SilcBool success = FALSE;
  unsigned char digest[32], tmp[4096], digest2[32];
  SilcUInt32 tmp_len;
  SilcHash sha256;
  
  if (argc > 1 && !strcmp(argv[1], "-d")) {
    silc_log_debug(TRUE);
    silc_log_debug_hexdump(TRUE);
    silc_log_set_debug_string("*crypt*,*hash*,*sha256*");
  }

  SILC_LOG_DEBUG(("Registering builtin hash functions"));
  silc_hash_register_default();

  SILC_LOG_DEBUG(("Allocating sha256 hash function"));
  if (!silc_hash_alloc("sha256", &sha256)) {
    SILC_LOG_DEBUG(("Allocating sha256 hash function failed"));
    goto err;
  }

  SilcUInt64 t1, t2;
  silc_timer_synchronize(&timer);

  /* First test vector */
  SILC_LOG_DEBUG(("First test vector"));
  silc_hash_init(sha256);
  silc_hash_update(sha256, data1, strlen(data1));
  memset(digest, 0, sizeof(digest));
  t1 = silc_timer_tick(&timer, FALSE);
  silc_hash_final(sha256, digest);
  t2 = silc_timer_tick(&timer, TRUE);
  SILC_LOG_DEBUG(("cycles: %d", t2 - t1));
  SILC_LOG_HEXDUMP(("Message"), (unsigned char *)data1, strlen(data1));
  SILC_LOG_HEXDUMP(("Digest"), digest, sizeof(digest));
  SILC_LOG_HEXDUMP(("Expected digest"), (unsigned char *)data1_digest,
		   sizeof(digest));
  if (memcmp(digest, data1_digest, sizeof(digest))) {
    SILC_LOG_DEBUG(("Hash failed"));
    goto err;
  }
  SILC_LOG_DEBUG(("Hash is successful"));
  
  /* Second test vector */
  SILC_LOG_DEBUG(("Second test vector"));
  silc_hash_init(sha256);
  silc_hash_update(sha256, data2, strlen(data2));
  silc_hash_final(sha256, digest);
  SILC_LOG_HEXDUMP(("Message"), (unsigned char *)data2, strlen(data2));
  SILC_LOG_HEXDUMP(("Digest"), digest, sizeof(digest));
  SILC_LOG_HEXDUMP(("Expected digest"), (unsigned char *)data2_digest,
		   sizeof(digest));
  if (memcmp(digest, data2_digest, sizeof(digest))) {
    SILC_LOG_DEBUG(("Hash failed"));
    goto err;
  }
  SILC_LOG_DEBUG(("Hash is successful"));
  
  /* Third test vector */
  SILC_LOG_DEBUG(("Third test vector"));
  silc_hash_init(sha256);
  silc_hex2data(data3, tmp, sizeof(tmp), &tmp_len);
  silc_hash_update(sha256, tmp, tmp_len);
  silc_hash_final(sha256, digest);
  SILC_LOG_HEXDUMP(("Message"), tmp, tmp_len);
  SILC_LOG_HEXDUMP(("Digest"), digest, sizeof(digest));
  silc_hex2data(data3_digest, digest2, sizeof(digest2), NULL);
  SILC_LOG_HEXDUMP(("Expected digest"), (unsigned char *)digest2,
		   sizeof(digest));
  if (memcmp(digest, digest2, sizeof(digest))) {
    SILC_LOG_DEBUG(("Hash failed"));
    goto err;
  }
  SILC_LOG_DEBUG(("Hash is successful"));
  
  success = TRUE;
  
 err:
  SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE"));
  fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE");

  silc_hash_free(sha256);
  silc_hash_unregister_all();
  return success;
}
コード例 #17
0
int main(int argc, char **argv)
{
  SilcBool success = FALSE;
  unsigned char *s1, *s2, *s3, *s4;
  unsigned char t[16];
  char h[32 + 1], str[40];
  int l, opt, i;
  SilcUInt32 len;

  while ((opt = getopt(argc, argv, "hVd:")) != EOF) {
      switch(opt) {
        case 'h':
          printf("usage: test_silcstrutil\n");
	  exit(0);
          break;
        case 'V':
          printf("Secure Internet Live Conferencing\n");
          exit(0);
          break;
        case 'd':
          silc_log_debug(TRUE);
	  silc_log_debug_hexdump(TRUE);
	  silc_log_quick(TRUE);
          if (optarg)
            silc_log_set_debug_string(optarg);
	  else
	    silc_log_set_debug_string("*strutil*,*errno*");
          break;
	default:
	  exit(1);
	  break;
      }
  }

  /* Failure tests */
  utf8failc(1);  utf8failc(2);
  utf8failc(3);  utf8failc(4);
  utf8failc(5);  utf8failc(6);
  utf8failc(7);  utf8failc(8);
  utf8failc(9);  utf8failc(10);
  utf8failc(11);  utf8failc(12);
  utf8failc(13);  utf8failc(14);
  utf8failc(15);  utf8failc(16);
  utf8failc(17);  utf8failc(18);
  utf8failc(19);  utf8failc(20);
  utf8failc(21);  utf8failc(22);
  utf8failc(23);  utf8failc(24);
  utf8failc(25);  utf8failc(26);
  utf8failc(27);  utf8failc(28);
  utf8failc(29);  utf8failc(30);

  /* LDAP DN simple test */
  s1 = "#&?*Pekka, \\Riikonen, <*****@*****.**>\xc4\x8d\\ ";
  SILC_LOG_DEBUG(("s1 = %s", s1));

  /* To LDAP DN */
  l = silc_utf8_decoded_len(s1, strlen(s1), SILC_STRING_LDAP_DN);
  if (!l)
    goto err;
  s3 = silc_calloc(l + 1, sizeof(*s3));
  silc_utf8_decode(s1, strlen(s1), SILC_STRING_LDAP_DN, s3, l);
  SILC_LOG_DEBUG(("ldapdn = %s", s3));

  /* To UTF-8 */
  l = silc_utf8_encoded_len(s3, strlen(s3), SILC_STRING_LDAP_DN);
  if (!l)
    goto err;
  s4 = silc_calloc(l + 1, sizeof(*s4));
  silc_utf8_encode(s3, strlen(s3), SILC_STRING_LDAP_DN, s4, l);
  SILC_LOG_DEBUG(("utf8 = %s", s4));

  if (memcmp(s4, s1, strlen(s4))) {
    SILC_LOG_DEBUG(("UTF-8 mismatch"));
    goto err;
  }
  silc_free(s3);
  silc_free(s4);

  /* UTF-8 strcasecmp test */
  SILC_LOG_DEBUG(("silc_utf8_strcasecmp test"));
  s1 = "Päivää vuan Yrjö";
  s2 = "PÄIVÄÄ VUAN YRJÖ";
  l = silc_utf8_encoded_len(s1, strlen(s1), SILC_STRING_LOCALE);
  if (!l)
    goto err;
  s3 = silc_calloc(l + 1, sizeof(*s3));
  silc_utf8_encode(s1, strlen(s1), SILC_STRING_LOCALE, s3, l);

  l = silc_utf8_encoded_len(s2, strlen(s2), SILC_STRING_LOCALE);
  if (!l)
    goto err;
  s4 = silc_calloc(l + 1, sizeof(*s4));
  silc_utf8_encode(s2, strlen(s2), SILC_STRING_LOCALE, s4, l);

  SILC_LOG_DEBUG(("%s == %s", s3, s4));
  if (!silc_utf8_strcasecmp(s3, s4)) {
    SILC_LOG_DEBUG(("mismatch"));
    goto err;
  }
  SILC_LOG_DEBUG(("match"));

  silc_free(s3);
  silc_free(s4);

  /* Regex test */
  SILC_LOG_DEBUG(("Simple regex test"));
  s1 = "foo,bar,silc,com";
  SILC_LOG_DEBUG(("Find 'silc' from %s", s1));
  if (!silc_string_match(s1, "silc"))
    goto err;
  SILC_LOG_DEBUG(("Regex match"));
  SILC_LOG_DEBUG(("Find 'foobar' from %s", s1));
  if (silc_string_match(s1, "foobar"))
    goto err;
  SILC_LOG_DEBUG(("Regex not found (Ok)"));

  /* HEX to data, data to HEX tests */
  for (i = 0; i < sizeof(t); i++)
    t[i] = i;
  silc_data2hex(t, sizeof(t), h, sizeof(h));
  silc_hex2data(h, t, sizeof(t), &len);
  silc_snprintf(h, sizeof(h), "010203ffabdef9ab");
  silc_hex2data(h, t, sizeof(t), &len);
  silc_data2hex(t, sizeof(t), h, sizeof(h));

  /* snprintf test */
  silc_snprintf(str, sizeof(str), "This is %@ rendered\n",
		render, "automatically");
  SILC_LOG_DEBUG((str));
  SILC_LOG_DEBUG(("This too %@ rendered", render, "is automatically"));

  success = TRUE;

 err:
  SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE"));
  fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE");

  return !success;
}