Added a crypto transform stress test for profiling
[strongswan.git] / scripts / crypt_burn.c
1
2 #include <stdio.h>
3 #include <library.h>
4 #include <crypto/proposal/proposal_keywords.h>
5
6 int main(int argc, char *argv[])
7 {
8 const proposal_token_t *token;
9 aead_t *aead;
10 crypter_t *crypter;
11 char buffer[1024], assoc[8], iv[32];
12 size_t bs;
13 int i = 0, limit = 0;
14
15
16 library_init(NULL);
17 lib->plugins->load(lib->plugins, NULL, PLUGINS);
18 atexit(library_deinit);
19
20 printf("loaded: %s\n", PLUGINS);
21
22 memset(buffer, 0x12, sizeof(buffer));
23 memset(assoc, 0x34, sizeof(assoc));
24 memset(iv, 0x56, sizeof(iv));
25
26 if (argc < 2)
27 {
28 fprintf(stderr, "usage: %s <algorithm>!\n", argv[0]);
29 return 1;
30 }
31 if (argc > 2)
32 {
33 limit = atoi(argv[2]);
34 }
35
36 token = proposal_get_token(argv[1], strlen(argv[1]));
37 if (!token)
38 {
39 fprintf(stderr, "algorithm '%s' unknown!\n", argv[1]);
40 return 1;
41 }
42 if (token->type != ENCRYPTION_ALGORITHM)
43 {
44 fprintf(stderr, "'%s' is not an encryption/aead algorithm!\n", argv[1]);
45 return 1;
46 }
47
48 if (encryption_algorithm_is_aead(token->algorithm))
49 {
50 aead = lib->crypto->create_aead(lib->crypto,
51 token->algorithm, token->keysize / 8);
52 if (!aead)
53 {
54 fprintf(stderr, "aead '%s' not supported!\n", argv[1]);
55 return 1;
56 }
57 while (TRUE)
58 {
59 aead->encrypt(aead,
60 chunk_create(buffer, sizeof(buffer) - aead->get_icv_size(aead)),
61 chunk_from_thing(assoc),
62 chunk_create(iv, aead->get_iv_size(aead)), NULL);
63 if (!aead->decrypt(aead, chunk_create(buffer, sizeof(buffer)),
64 chunk_from_thing(assoc),
65 chunk_create(iv, aead->get_iv_size(aead)), NULL))
66 {
67 fprintf(stderr, "aead integrity check failed!\n");
68 return FALSE;
69 }
70 if (limit && ++i == limit)
71 {
72 break;
73 }
74 }
75 }
76 else
77 {
78 crypter = lib->crypto->create_crypter(lib->crypto,
79 token->algorithm, token->keysize / 8);
80 if (!crypter)
81 {
82 fprintf(stderr, "crypter '%s' not supported!\n", argv[1]);
83 return 1;
84 }
85 bs = crypter->get_block_size(crypter);
86
87 while (i--)
88 {
89 crypter->encrypt(crypter,
90 chunk_create(buffer, sizeof(buffer) / bs * bs),
91 chunk_create(iv, crypter->get_iv_size(crypter)), NULL);
92 crypter->decrypt(crypter,
93 chunk_create(buffer, sizeof(buffer) / bs * bs),
94 chunk_create(iv, crypter->get_iv_size(crypter)), NULL);
95 if (limit && ++i == limit)
96 {
97 break;
98 }
99 }
100 }
101 return 0;
102 }