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