Major restructuring of the internals of SILC Cipher API
[crypto.git] / lib / silccrypt / tests / test_cipher.c
1 /*
2
3   test_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
22 #define ENC_LEN 0x00100000      /* enc data len (at least) */
23 #define ENC_ROUND 512           /* enc rounds (at least) */
24 #define ENC_MIN_TIME 3.0        /* seconds to run the test (at least) */
25
26 SilcTimerStruct timer;
27 SilcCipher cipher;
28
29 int main(int argc, char **argv)
30 {
31   SilcUInt64 sec;
32   SilcUInt32 usec;
33   double totsec;
34   unsigned char *data;
35   SilcUInt32 rounds;
36   SilcUInt32 i, k;
37
38   data = malloc(ENC_LEN * sizeof(*data));
39   if (!data)
40     exit(1);
41
42   for (i = 0; i < ENC_LEN; i++)
43     data[i] = i % 255;
44
45   silc_timer_synchronize(&timer);
46
47   for (i = 0; silc_default_ciphers[i].name; i++) {
48     if (!silc_cipher_alloc(silc_default_ciphers[i].name, &cipher)) {
49       fprintf(stderr, "Error allocating %s\n", silc_default_ciphers[i].name);
50       exit(1);
51     }
52
53     silc_cipher_set_key(cipher, data, silc_cipher_get_key_len(cipher), TRUE);
54     silc_cipher_set_iv(cipher, data);
55
56     rounds = ENC_ROUND;
57
58   retry:
59     silc_timer_start(&timer);
60     for (k = 0; k < rounds; k++)
61       silc_cipher_encrypt(cipher, data, data, ENC_LEN, NULL);
62     silc_timer_stop(&timer);
63
64     silc_timer_value(&timer, &sec, &usec);
65     totsec = (double)sec;
66     totsec += ((double)usec / (double)(1000 * 1000));
67     if (totsec < ENC_MIN_TIME) {
68       rounds += rounds;
69       goto retry;
70     }
71
72     printf("%s:\t%.2f KB (%.2f MB) / sec (total test time %.2f secs)\n",
73            silc_default_ciphers[i].name,
74            (((double)(ENC_LEN * rounds) / 1024.0) / totsec),
75            (((double)(ENC_LEN * rounds) / (1024.0 * 1024.0)) / totsec),
76            totsec);
77
78     silc_cipher_free(cipher);
79   }
80
81   return 0;
82 }