crypt-burn: Add a encryption buffer command line argument
[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 static int burn_crypter(const proposal_token_t *token, u_int limit, u_int len)
20 {
21 chunk_t iv, key, data;
22 crypter_t *crypter;
23 int i = 0;
24 bool ok;
25
26 crypter = lib->crypto->create_crypter(lib->crypto, token->algorithm,
27 token->keysize / 8);
28 if (!crypter)
29 {
30 fprintf(stderr, "%N-%zu not supported\n",
31 encryption_algorithm_names, token->algorithm, token->keysize);
32 return FALSE;
33 }
34
35 iv = chunk_alloc(crypter->get_iv_size(crypter));
36 memset(iv.ptr, 0xFF, iv.len);
37 data = chunk_alloc(round_up(len, crypter->get_block_size(crypter)));
38 memset(data.ptr, 0xDD, data.len);
39 key = chunk_alloc(crypter->get_key_size(crypter));
40 memset(key.ptr, 0xAA, key.len);
41
42 ok = crypter->set_key(crypter, key);
43 while (ok)
44 {
45 if (!crypter->encrypt(crypter, data, iv, NULL))
46 {
47 fprintf(stderr, "encryption failed!\n");
48 ok = FALSE;
49 break;
50 }
51 if (!crypter->decrypt(crypter, data, iv, NULL))
52 {
53 fprintf(stderr, "decryption failed!\n");
54 ok = FALSE;
55 break;
56 }
57 if (limit && ++i == limit)
58 {
59 break;
60 }
61 }
62 crypter->destroy(crypter);
63
64 free(iv.ptr);
65 free(data.ptr);
66 free(key.ptr);
67
68 return ok;
69 }
70
71 static bool burn_aead(const proposal_token_t *token, u_int limit, u_int len)
72 {
73 chunk_t iv, key, data, dataicv, assoc;
74 aead_t *aead;
75 int i = 0;
76 bool ok;
77
78 aead = lib->crypto->create_aead(lib->crypto, token->algorithm,
79 token->keysize / 8, 0);
80 if (!aead)
81 {
82 fprintf(stderr, "%N-%zu not supported\n",
83 encryption_algorithm_names, token->algorithm, token->keysize);
84 return FALSE;
85 }
86
87 iv = chunk_alloc(aead->get_iv_size(aead));
88 memset(iv.ptr, 0xFF, iv.len);
89 dataicv = chunk_alloc(round_up(len, aead->get_block_size(aead)) +
90 aead->get_icv_size(aead));
91 data = chunk_create(dataicv.ptr, dataicv.len - aead->get_icv_size(aead));
92 memset(data.ptr, 0xDD, data.len);
93 assoc = chunk_alloc(13);
94 memset(assoc.ptr, 0xCC, assoc.len);
95 key = chunk_alloc(aead->get_key_size(aead));
96 memset(key.ptr, 0xAA, key.len);
97
98 ok = aead->set_key(aead, key);
99 while (ok)
100 {
101 if (!aead->encrypt(aead, data, assoc, iv, NULL))
102 {
103 fprintf(stderr, "aead encryption failed!\n");
104 ok = FALSE;
105 break;
106 }
107 if (!aead->decrypt(aead, dataicv, assoc, iv, NULL))
108 {
109 fprintf(stderr, "aead integrity check failed!\n");
110 ok = FALSE;
111 break;
112 }
113 if (limit && ++i == limit)
114 {
115 break;
116 }
117 }
118 aead->destroy(aead);
119
120 free(iv.ptr);
121 free(data.ptr);
122 free(key.ptr);
123
124 return ok;
125 }
126
127 int main(int argc, char *argv[])
128 {
129 const proposal_token_t *token;
130 u_int limit = 0, len = 1024;
131 bool ok;
132
133 library_init(NULL, "crypt_burn");
134 lib->plugins->load(lib->plugins, getenv("PLUGINS") ?: PLUGINS);
135 atexit(library_deinit);
136
137 fprintf(stderr, "loaded: %s\n", lib->plugins->loaded_plugins(lib->plugins));
138
139 if (argc < 2)
140 {
141 fprintf(stderr, "usage: %s <algorithm> [buflen=%u] [rounds=%u]\n",
142 argv[0], len, limit);
143 return 1;
144 }
145 if (argc > 2)
146 {
147 len = atoi(argv[2]);
148 }
149 if (argc > 3)
150 {
151 limit = atoi(argv[3]);
152 }
153
154 token = lib->proposal->get_token(lib->proposal, argv[1]);
155 if (!token)
156 {
157 fprintf(stderr, "algorithm '%s' unknown!\n", argv[1]);
158 return 1;
159 }
160
161 switch (token->type)
162 {
163 case ENCRYPTION_ALGORITHM:
164 if (encryption_algorithm_is_aead(token->algorithm))
165 {
166 ok = burn_aead(token, limit, len);
167 }
168 else
169 {
170 ok = burn_crypter(token, limit, len);
171 }
172 break;
173 default:
174 fprintf(stderr, "'%s' is not a crypter/aead algorithm!\n", argv[1]);
175 ok = FALSE;
176 break;
177 }
178 return !ok;
179 }