Added cipher acceleration to SILC Accelerator. Added cipher softacc.
[crypto.git] / lib / silcacc / tests / test_softacc_cipher.c
1 /*
2
3   test_softacc_cipher.c
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 2008 Pekka Riikonen
8
9   This program is free software; you can redistribute it and/or modify
10   it under the terms of the GNU General Public License as published by
11   the Free Software Foundation; version 2 of the License.
12
13   This program is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   GNU General Public License for more details.
17
18 */
19
20 #include "silccrypto.h"
21 #include "softacc.h"
22
23 #define ENC_LEN 0x00100000      /* enc data len (at least) */
24 #define ENC_ROUND 512           /* enc rounds (at least) */
25 #define ENC_MIN_TIME 15.0        /* seconds to run the test (at least) */
26
27 SilcTimerStruct timer;
28 SilcCipher cipher, acc_cipher;
29
30 int main(int argc, char **argv)
31 {
32   SilcUInt64 sec;
33   SilcUInt32 usec;
34   double totsec;
35   unsigned char *data;
36   SilcUInt32 rounds;
37   SilcUInt32 i, k;
38
39   silc_runtime_init();
40   silc_crypto_init(NULL);
41
42 #if 0
43   silc_log_debug(TRUE);
44   silc_log_quick(TRUE);
45   silc_log_debug_hexdump(TRUE);
46   silc_log_set_debug_string("*acc*,*thread*");
47 #endif
48
49   if (!silc_acc_init(SILC_SOFTACC, (void *)0x01, "min_threads", 2,
50                      "max_threads", 8, NULL))
51     exit(1);
52
53   data = malloc(ENC_LEN * sizeof(*data));
54   if (!data)
55     exit(1);
56
57   for (i = 0; i < ENC_LEN; i++)
58     data[i] = i % 255;
59
60   silc_timer_synchronize(&timer);
61
62   for (i = 0; silc_default_ciphers[i].name; i++) {
63     if (!silc_cipher_alloc(silc_default_ciphers[i].name, &cipher)) {
64       fprintf(stderr, "Error allocating %s\n", silc_default_ciphers[i].name);
65       exit(1);
66     }
67
68     acc_cipher = silc_acc_cipher(SILC_SOFTACC, cipher);
69     if (!acc_cipher)
70       continue;
71
72     silc_cipher_set_iv(acc_cipher, data);
73     silc_cipher_set_key(acc_cipher, data, silc_cipher_get_key_len(cipher),
74                         TRUE);
75     sleep(1);
76
77     rounds = ENC_ROUND;
78
79   retry:
80     silc_timer_start(&timer);
81     for (k = 0; k < rounds; k++)
82       silc_cipher_encrypt(acc_cipher, data, data, ENC_LEN, NULL);
83     silc_timer_stop(&timer);
84
85     silc_timer_value(&timer, &sec, &usec);
86     totsec = (double)sec;
87     totsec += ((double)usec / (double)((double)1000 * (double)1000));
88     if (totsec < ENC_MIN_TIME) {
89       rounds += rounds;
90       goto retry;
91     }
92
93     silc_cipher_free(acc_cipher);
94     silc_cipher_free(cipher);
95
96     sleep(1);
97     printf("%s:\t%.2f KB (%.2f MB, %.2f Mbit) / sec (total %.3f secs)\n",
98            silc_default_ciphers[i].name,
99            (((double)((double)ENC_LEN * (double)rounds) / 1024.0) / totsec),
100            (((double)((double)ENC_LEN * (double)rounds) / (1024.0 *
101                                                            1024.0)) / totsec),
102            ((((double)((double)ENC_LEN * (double)rounds) / 1024.0)
103              / 128.0) / totsec),
104            totsec);
105   }
106
107   silc_acc_uninit(SILC_SOFTACC);
108
109   silc_crypto_uninit();
110   silc_runtime_uninit();
111
112   return 0;
113 }