Added SILC Accelerator Library.
[silc.git] / lib / silcacc / tests / test_softacc.c
1 /* Software accelerator tests */
2
3 #include "silc.h"
4
5 SilcSchedule schedule;
6 SilcPublicKey public_key, accpub;
7 SilcPrivateKey private_key, accprv;
8 SilcHash hash;
9 unsigned char data[] = "Single block msg";
10 int data_len = 16;
11 int s = 100;
12
13 void sign_compl(SilcBool success, const unsigned char *signature,
14                 SilcUInt32 signature_len, void *context)
15 {
16   SILC_LOG_DEBUG(("Sign compl %s", success ? "Ok" : "failed"));
17 }
18
19 SILC_TASK_CALLBACK(quit)
20 {
21   silc_schedule_stop(schedule);
22 }
23
24 SILC_TASK_CALLBACK(sign)
25 {
26   silc_pkcs_sign(accprv, data, data_len, TRUE, hash, sign_compl, NULL);
27   if (--s > 0)
28     silc_schedule_task_add_timeout(schedule, sign, NULL, 0, 70000);
29 }
30
31 int main(int argc, char **argv)
32 {
33   SilcBool success = FALSE;
34   SilcAccelerator softacc;
35
36   if (argc > 1 && !strcmp(argv[1], "-d")) {
37     silc_log_debug(TRUE);
38     silc_log_quick(TRUE);
39     silc_log_debug_hexdump(TRUE);
40     silc_log_set_debug_string("*acc*");
41   }
42
43   silc_crypto_init(NULL);
44   if (!silc_hash_alloc("sha1", &hash))
45     goto err;
46
47   if (!silc_create_key_pair("rsa", 2048, "pubkey.pub", "privkey.prv", NULL,
48                             "", &public_key, &private_key, FALSE))
49     goto err;
50
51   schedule = silc_schedule_init(0, NULL, NULL);
52
53   softacc = silc_acc_find("softacc");
54   if (!softacc)
55     goto err;
56
57   if (!silc_acc_init(softacc, schedule, "min_threads", 2, "max_threads", 
58                      8, NULL))
59     goto err;
60
61   accpub = silc_acc_public_key(softacc, public_key);
62   if (!accpub)
63     goto err;
64   accprv = silc_acc_private_key(softacc, private_key);
65   if (!accprv)
66     goto err;
67
68   if (silc_acc_get_public_key(softacc, accpub) != public_key)
69     goto err;
70   if (silc_acc_get_private_key(softacc, accprv) != private_key)
71     goto err;
72
73   silc_schedule_task_add_timeout(schedule, sign, NULL, 0, 1);
74   silc_schedule_task_add_timeout(schedule, quit, NULL, 15, 0);
75   silc_schedule(schedule);
76
77   silc_acc_uninit(softacc);
78   silc_schedule_uninit(schedule);
79   silc_crypto_uninit();
80
81   success = TRUE;
82
83  err:
84   SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE"));
85   fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE");
86
87   return success;
88 }