]> git.ipfire.org Git - people/ms/strongswan.git/blame - scripts/crypt_burn.c
crypt-burn: Accept a PLUGINS env var to configure plugins to load
[people/ms/strongswan.git] / scripts / crypt_burn.c
CommitLineData
f3af4969
TB
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 */
37e52c3f
MW
15
16#include <stdio.h>
17#include <library.h>
37e52c3f
MW
18
19int 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
34d3bfcf 28 library_init(NULL, "crypt_burn");
d5ce572d 29 lib->plugins->load(lib->plugins, getenv("PLUGINS") ?: PLUGINS);
37e52c3f
MW
30 atexit(library_deinit);
31
d5ce572d 32 fprintf(stderr, "loaded: %s\n", lib->plugins->loaded_plugins(lib->plugins));
37e52c3f
MW
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
4c57c630 48 token = lib->proposal->get_token(lib->proposal, argv[1]);
37e52c3f
MW
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,
e5d73b0d 63 token->algorithm, token->keysize / 8, 0);
37e52c3f
MW
64 if (!aead)
65 {
66 fprintf(stderr, "aead '%s' not supported!\n", argv[1]);
67 return 1;
68 }
69 while (TRUE)
70 {
e2ed7bfd 71 if (!aead->encrypt(aead,
37e52c3f
MW
72 chunk_create(buffer, sizeof(buffer) - aead->get_icv_size(aead)),
73 chunk_from_thing(assoc),
e2ed7bfd
MW
74 chunk_create(iv, aead->get_iv_size(aead)), NULL))
75 {
76 fprintf(stderr, "aead encryption failed!\n");
77 return 1;
78 }
37e52c3f
MW
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");
79d5c4f0 84 return 1;
37e52c3f
MW
85 }
86 if (limit && ++i == limit)
87 {
88 break;
89 }
90 }
51caeeb1 91 aead->destroy(aead);
37e52c3f
MW
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
d4f2f3dd 104 while (TRUE)
37e52c3f 105 {
e35abbe5
MW
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 }
3b96189a
MW
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 }
37e52c3f
MW
118 if (limit && ++i == limit)
119 {
120 break;
121 }
122 }
51caeeb1 123 crypter->destroy(crypter);
37e52c3f
MW
124 }
125 return 0;
126}