BadVPN – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 /**
2 * @file bencryption_bench.c
3 * @author Ambroz Bizjak <ambrop7@gmail.com>
4 *
5 * @section LICENSE
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of the author nor the
15 * names of its contributors may be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29  
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <stdint.h>
34 #include <limits.h>
35  
36 #include <misc/balloc.h>
37 #include <security/BRandom.h>
38 #include <security/BEncryption.h>
39 #include <base/DebugObject.h>
40  
41 static void usage (char *name)
42 {
43 printf(
44 "Usage: %s <enc/dec> <ciper> <num_blocks> <num_ops>\n"
45 " <cipher> is one of (blowfish, aes).\n",
46 name
47 );
48  
49 exit(1);
50 }
51  
52 int main (int argc, char **argv)
53 {
54 if (argc <= 0) {
55 return 1;
56 }
57  
58 if (argc != 5) {
59 usage(argv[0]);
60 }
61  
62 char *mode_str = argv[1];
63 char *cipher_str = argv[2];
64  
65 int mode;
66 int cipher = 0; // silence warning
67 int num_blocks = atoi(argv[3]);
68 int num_ops = atoi(argv[4]);
69  
70 if (!strcmp(mode_str, "enc")) {
71 mode = BENCRYPTION_MODE_ENCRYPT;
72 }
73 else if (!strcmp(mode_str, "dec")) {
74 mode = BENCRYPTION_MODE_DECRYPT;
75 }
76 else {
77 usage(argv[0]);
78 }
79  
80 if (!strcmp(cipher_str, "blowfish")) {
81 cipher = BENCRYPTION_CIPHER_BLOWFISH;
82 }
83 else if (!strcmp(cipher_str, "aes")) {
84 cipher = BENCRYPTION_CIPHER_AES;
85 }
86 else {
87 usage(argv[0]);
88 }
89  
90 if (num_blocks < 0 || num_ops < 0) {
91 usage(argv[0]);
92 }
93  
94 int key_size = BEncryption_cipher_key_size(cipher);
95 int block_size = BEncryption_cipher_block_size(cipher);
96  
97 uint8_t key[BENCRYPTION_MAX_KEY_SIZE];
98 BRandom_randomize(key, key_size);
99  
100 uint8_t iv[BENCRYPTION_MAX_BLOCK_SIZE];
101 BRandom_randomize(iv, block_size);
102  
103 if (num_blocks > INT_MAX / block_size) {
104 printf("too much");
105 goto fail0;
106 }
107 int unit_size = num_blocks * block_size;
108  
109 printf("unit size %d\n", unit_size);
110  
111 uint8_t *buf1 = (uint8_t *)BAlloc(unit_size);
112 if (!buf1) {
113 printf("BAlloc failed");
114 goto fail0;
115 }
116  
117 uint8_t *buf2 = (uint8_t *)BAlloc(unit_size);
118 if (!buf2) {
119 printf("BAlloc failed");
120 goto fail1;
121 }
122  
123 BEncryption enc;
124 BEncryption_Init(&enc, mode, cipher, key);
125  
126 uint8_t *in = buf1;
127 uint8_t *out = buf2;
128 BRandom_randomize(in, unit_size);
129  
130 for (int i = 0; i < num_ops; i++) {
131 BEncryption_Encrypt(&enc, in, out, unit_size, iv);
132  
133 uint8_t *t = in;
134 in = out;
135 out = t;
136 }
137  
138 BEncryption_Free(&enc);
139 BFree(buf2);
140 fail1:
141 BFree(buf1);
142 fail0:
143 DebugObjectGlobal_Finish();
144  
145 return 0;
146 }