268ac9344a3fdbb93c5612f71c43fe114d85e249
[strongswan.git] / scripts / crypt_burn.c
1 /*
2 * Copyright (C) 2010 Martin Willi
3 * Copyright (C) 2010 revosec AG
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include <stdio.h>
17 #include <library.h>
18
19 int main(int argc, char *argv[])
20 {
21 const proposal_token_t *token;
22 aead_t *aead;
23 crypter_t *crypter;
24 char buffer[1024], assoc[8], iv[32];
25 size_t bs;
26 int i = 0, limit = 0;
27
28 library_init(NULL, "crypt_burn");
29 lib->plugins->load(lib->plugins, getenv("PLUGINS") ?: PLUGINS);
30 atexit(library_deinit);
31
32 fprintf(stderr, "loaded: %s\n", lib->plugins->loaded_plugins(lib->plugins));
33
34 memset(buffer, 0x12, sizeof(buffer));
35 memset(assoc, 0x34, sizeof(assoc));
36 memset(iv, 0x56, sizeof(iv));
37
38 if (argc < 2)
39 {
40 fprintf(stderr, "usage: %s <algorithm>!\n", argv[0]);
41 return 1;
42 }
43 if (argc > 2)
44 {
45 limit = atoi(argv[2]);
46 }
47
48 token = lib->proposal->get_token(lib->proposal, argv[1]);
49 if (!token)
50 {
51 fprintf(stderr, "algorithm '%s' unknown!\n", argv[1]);
52 return 1;
53 }
54 if (token->type != ENCRYPTION_ALGORITHM)
55 {
56 fprintf(stderr, "'%s' is not an encryption/aead algorithm!\n", argv[1]);
57 return 1;
58 }
59
60 if (encryption_algorithm_is_aead(token->algorithm))
61 {
62 aead = lib->crypto->create_aead(lib->crypto,
63 token->algorithm, token->keysize / 8, 0);
64 if (!aead)
65 {
66 fprintf(stderr, "aead '%s' not supported!\n", argv[1]);
67 return 1;
68 }
69 while (TRUE)
70 {
71 if (!aead->encrypt(aead,
72 chunk_create(buffer, sizeof(buffer) - aead->get_icv_size(aead)),
73 chunk_from_thing(assoc),
74 chunk_create(iv, aead->get_iv_size(aead)), NULL))
75 {
76 fprintf(stderr, "aead encryption failed!\n");
77 return 1;
78 }
79 if (!aead->decrypt(aead, chunk_create(buffer, sizeof(buffer)),
80 chunk_from_thing(assoc),
81 chunk_create(iv, aead->get_iv_size(aead)), NULL))
82 {
83 fprintf(stderr, "aead integrity check failed!\n");
84 return 1;
85 }
86 if (limit && ++i == limit)
87 {
88 break;
89 }
90 }
91 aead->destroy(aead);
92 }
93 else
94 {
95 crypter = lib->crypto->create_crypter(lib->crypto,
96 token->algorithm, token->keysize / 8);
97 if (!crypter)
98 {
99 fprintf(stderr, "crypter '%s' not supported!\n", argv[1]);
100 return 1;
101 }
102 bs = crypter->get_block_size(crypter);
103
104 while (TRUE)
105 {
106 if (!crypter->encrypt(crypter,
107 chunk_create(buffer, sizeof(buffer) / bs * bs),
108 chunk_create(iv, crypter->get_iv_size(crypter)), NULL))
109 {
110 continue;
111 }
112 if (!crypter->decrypt(crypter,
113 chunk_create(buffer, sizeof(buffer) / bs * bs),
114 chunk_create(iv, crypter->get_iv_size(crypter)), NULL))
115 {
116 continue;
117 }
118 if (limit && ++i == limit)
119 {
120 break;
121 }
122 }
123 crypter->destroy(crypter);
124 }
125 return 0;
126 }