2 * Copyright (C) 2009-2011 Gabor Juhos <juhosg@openwrt.org>
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.
15 #include <getopt.h> /* for getopt() */
18 #include "buffalo-lib.h"
20 #define ERR(fmt, args...) do { \
22 fprintf(stderr, "[%s] *** error: " fmt "\n", \
23 progname, ## args ); \
26 static char *progname;
29 static char *crypt_key = "Buffalo";
30 static char *magic = "start";
32 static unsigned char seed = 'O';
36 static int do_decrypt;
38 void usage(int status)
40 FILE *stream = (status != EXIT_SUCCESS) ? stderr : stdout;
42 fprintf(stream, "Usage: %s [OPTIONS...]\n", progname);
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"
60 static int decrypt_file(void)
64 unsigned char *buf = NULL;
68 src_len = get_file_size(ifname);
70 ERR("unable to get size of '%s'", ifname);
74 buf = malloc(src_len);
76 ERR("no memory for the buffer");
80 err = read_file_to_buf(ifname, buf, src_len);
82 ERR("unable to read from file '%s'", ifname);
86 memset(&ep, '\0', sizeof(ep));
87 ep.key = (unsigned char *) crypt_key;
89 err = decrypt_buf(&ep, buf, src_len);
91 ERR("unable to decrypt '%s'", ifname);
95 printf("Magic\t\t: '%s'\n", ep.magic);
96 printf("Seed\t\t: 0x%02x\n", ep.seed);
97 printf("Product\t\t: '%s'\n", ep.product);
98 printf("Version\t\t: '%s'\n", ep.version);
99 printf("Data len\t: %u\n", ep.datalen);
100 printf("Checksum\t: 0x%08x\n", ep.csum);
102 err = write_buf_to_file(ofname, buf, ep.datalen);
104 ERR("unable to write to file '%s'", ofname);
115 static int encrypt_file(void)
125 src_len = get_file_size(ifname);
127 ERR("unable to get size of '%s'", ifname);
131 totlen = enc_compute_buf_len(product, version, src_len);
132 hdrlen = enc_compute_header_len(product, version);
134 buf = malloc(totlen);
136 ERR("no memory for the buffer");
140 err = read_file_to_buf(ifname, &buf[hdrlen], src_len);
142 ERR("unable to read from file '%s'", ofname);
146 memset(&ep, '\0', sizeof(ep));
147 ep.key = (unsigned char *) crypt_key;
149 ep.longstate = longstate;
150 ep.csum = buffalo_csum(src_len, &buf[hdrlen], src_len);
151 ep.datalen = src_len;
152 strcpy((char *) ep.magic, magic);
153 strcpy((char *) ep.product, product);
154 strcpy((char *) ep.version, version);
156 err = encrypt_buf(&ep, buf, &buf[hdrlen]);
158 ERR("invalid input file");
162 err = write_buf_to_file(ofname, buf, totlen);
164 ERR("unable to write to file '%s'", ofname);
176 static int check_params(void)
180 if (ifname == NULL) {
181 ERR("no input file specified");
185 if (ofname == NULL) {
186 ERR("no output file specified");
190 if (crypt_key == NULL) {
191 ERR("no key specified");
193 } else if (strlen(crypt_key) > BCRYPT_MAX_KEYLEN) {
194 ERR("key '%s' is too long", crypt_key);
198 if (strlen(magic) != (ENC_MAGIC_LEN - 1)) {
199 ERR("length of magic must be %d", ENC_MAGIC_LEN - 1);
204 if (product == NULL) {
205 ERR("no product specified");
209 if (version == NULL) {
210 ERR("no version specified");
214 if (strlen(product) > (ENC_PRODUCT_LEN - 1)) {
215 ERR("product name '%s' is too long", product);
219 if (strlen(version) > (ENC_VERSION_LEN - 1)) {
220 ERR("version '%s' is too long", version);
231 int main(int argc, char *argv[])
233 int res = EXIT_FAILURE;
236 progname = basename(argv[0]);
241 c = getopt(argc, argv, "adi:m:o:hp:v:k:r:s:");
271 seed = strtoul(optarg, NULL, 16);
282 err = check_params();
287 err = decrypt_file();
289 err = encrypt_file();