94de53e91053cc2142f2f3de2da20c8b2d16a719
[openwrt/svn-archive/archive.git] / tools / firmware-utils / src / buffalo-enc.c
1 /*
2 * Copyright (C) 2009-2011 Gabor Juhos <juhosg@openwrt.org>
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 2 as published
6 * by the Free Software Foundation.
7 *
8 */
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <stdint.h>
13 #include <string.h>
14 #include <libgen.h>
15 #include <getopt.h> /* for getopt() */
16 #include <stdarg.h>
17
18 #include "buffalo-lib.h"
19
20 #define ERR(fmt, args...) do { \
21 fflush(0); \
22 fprintf(stderr, "[%s] *** error: " fmt "\n", \
23 progname, ## args ); \
24 } while (0)
25
26 static char *progname;
27 static char *ifname;
28 static char *ofname;
29 static char *crypt_key = "Buffalo";
30 static char *magic = "start";
31 static int longstate;
32 static unsigned char seed = 'O';
33
34 static char *product;
35 static char *version;
36 static int do_decrypt;
37
38 void usage(int status)
39 {
40 FILE *stream = (status != EXIT_SUCCESS) ? stderr : stdout;
41
42 fprintf(stream, "Usage: %s [OPTIONS...]\n", progname);
43 fprintf(stream,
44 "\n"
45 "Options:\n"
46 " -d decrypt instead of encrypt\n"
47 " -i <file> read input from the file <file>\n"
48 " -o <file> write output to the file <file>\n"
49 " -l use longstate {en,de}cryption method\n"
50 " -k <key> use <key> for encryption (default: Buffalo)\n"
51 " -m <magic> set magic to <magic>\n"
52 " -p <product> set product name to <product>\n"
53 " -v <version> set version to <version>\n"
54 " -h show this screen\n"
55 );
56
57 exit(status);
58 }
59
60 static int decrypt_file(void)
61 {
62 struct enc_param ep;
63 ssize_t src_len;
64 unsigned char *buf = NULL;
65 int err;
66 int ret = -1;
67
68 src_len = get_file_size(ifname);
69 if (src_len < 0) {
70 ERR("unable to get size of '%s'", ifname);
71 goto out;
72 }
73
74 buf = malloc(src_len);
75 if (buf == NULL) {
76 ERR("no memory for the buffer");
77 goto out;
78 }
79
80 err = read_file_to_buf(ifname, buf, src_len);
81 if (err) {
82 ERR("unable to read from file '%s'", ifname);
83 goto out;
84 }
85
86 memset(&ep, '\0', sizeof(ep));
87 ep.key = (unsigned char *) crypt_key;
88
89 err = decrypt_buf(&ep, buf, src_len);
90 if (err)
91 goto out;
92
93 printf("Magic\t\t: '%s'\n", ep.magic);
94 printf("Seed\t\t: 0x%02x\n", ep.seed);
95 printf("Product\t\t: '%s'\n", ep.product);
96 printf("Version\t\t: '%s'\n", ep.version);
97 printf("Data len\t: %u\n", ep.datalen);
98 printf("Checksum\t: 0x%08x\n", ep.csum);
99
100 err = write_buf_to_file(ofname, buf, ep.datalen);
101 if (err) {
102 ERR("unable to write to file '%s'", ofname);
103 goto out;
104 }
105
106 ret = 0;
107
108 out:
109 free(buf);
110 return ret;
111 }
112
113 static int encrypt_file(void)
114 {
115 struct enc_param ep;
116 ssize_t src_len;
117 unsigned char *buf;
118 uint32_t hdrlen;
119 ssize_t totlen = 0;
120 int err;
121 int ret = -1;
122
123 src_len = get_file_size(ifname);
124 if (src_len < 0) {
125 ERR("unable to get size of '%s'", ifname);
126 goto out;
127 }
128
129 totlen = enc_compute_buf_len(product, version, src_len);
130 hdrlen = enc_compute_header_len(product, version);
131
132 buf = malloc(totlen);
133 if (buf == NULL) {
134 ERR("no memory for the buffer");
135 goto out;
136 }
137
138 err = read_file_to_buf(ifname, &buf[hdrlen], src_len);
139 if (err) {
140 ERR("unable to read from file '%s'", ofname);
141 goto free_buf;
142 }
143
144 memset(&ep, '\0', sizeof(ep));
145 ep.key = (unsigned char *) crypt_key;
146 ep.seed = seed;
147 ep.longstate = longstate;
148 ep.csum = buffalo_csum(src_len, &buf[hdrlen], src_len);
149 ep.datalen = src_len;
150 strcpy((char *) ep.magic, magic);
151 strcpy((char *) ep.product, product);
152 strcpy((char *) ep.version, version);
153
154 err = encrypt_buf(&ep, buf, &buf[hdrlen]);
155 if (err) {
156 ERR("invalid input file");
157 goto free_buf;
158 }
159
160 err = write_buf_to_file(ofname, buf, totlen);
161 if (err) {
162 ERR("unable to write to file '%s'", ofname);
163 goto free_buf;
164 }
165
166 ret = 0;
167
168 free_buf:
169 free(buf);
170 out:
171 return ret;
172 }
173
174 static int check_params(void)
175 {
176 int ret = -1;
177
178 if (ifname == NULL) {
179 ERR("no input file specified");
180 goto out;
181 }
182
183 if (ofname == NULL) {
184 ERR("no output file specified");
185 goto out;
186 }
187
188 if (crypt_key == NULL) {
189 ERR("no key specified");
190 goto out;
191 } else if (strlen(crypt_key) > BCRYPT_MAX_KEYLEN) {
192 ERR("key '%s' is too long", crypt_key);
193 goto out;
194 }
195
196 if (strlen(magic) != (ENC_MAGIC_LEN - 1)) {
197 ERR("length of magic must be %d", ENC_MAGIC_LEN - 1);
198 goto out;
199 }
200
201 if (!do_decrypt) {
202 if (product == NULL) {
203 ERR("no product specified");
204 goto out;
205 }
206
207 if (version == NULL) {
208 ERR("no version specified");
209 goto out;
210 }
211
212 if (strlen(product) > (ENC_PRODUCT_LEN - 1)) {
213 ERR("product name '%s' is too long", product);
214 goto out;
215 }
216
217 if (strlen(version) > (ENC_VERSION_LEN - 1)) {
218 ERR("version '%s' is too long", version);
219 goto out;
220 }
221 }
222
223 ret = 0;
224
225 out:
226 return ret;
227 }
228
229 int main(int argc, char *argv[])
230 {
231 int res = EXIT_FAILURE;
232 int err;
233
234 progname = basename(argv[0]);
235
236 while ( 1 ) {
237 int c;
238
239 c = getopt(argc, argv, "adi:m:o:hp:v:k:r:s:");
240 if (c == -1)
241 break;
242
243 switch (c) {
244 case 'd':
245 do_decrypt = 1;
246 break;
247 case 'i':
248 ifname = optarg;
249 break;
250 case 'l':
251 longstate = 1;
252 break;
253 case 'm':
254 magic = optarg;
255 break;
256 case 'o':
257 ofname = optarg;
258 break;
259 case 'p':
260 product = optarg;
261 break;
262 case 'v':
263 version = optarg;
264 break;
265 case 'k':
266 crypt_key = optarg;
267 break;
268 case 's':
269 seed = strtoul(optarg, NULL, 16);
270 break;
271 case 'h':
272 usage(EXIT_SUCCESS);
273 break;
274 default:
275 usage(EXIT_FAILURE);
276 break;
277 }
278 }
279
280 err = check_params();
281 if (err)
282 goto out;
283
284 if (do_decrypt)
285 err = decrypt_file();
286 else
287 err = encrypt_file();
288
289 if (err)
290 goto out;
291
292 res = EXIT_SUCCESS;
293
294 out:
295 return res;
296 }