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);
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);
100 err = write_buf_to_file(ofname, buf, ep.datalen);
102 ERR("unable to write to file '%s'", ofname);
113 static int encrypt_file(void)
123 src_len = get_file_size(ifname);
125 ERR("unable to get size of '%s'", ifname);
129 totlen = enc_compute_buf_len(product, version, src_len);
130 hdrlen = enc_compute_header_len(product, version);
132 buf = malloc(totlen);
134 ERR("no memory for the buffer");
138 err = read_file_to_buf(ifname, &buf[hdrlen], src_len);
140 ERR("unable to read from file '%s'", ofname);
144 memset(&ep, '\0', sizeof(ep));
145 ep.key = (unsigned char *) crypt_key;
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);
154 err = encrypt_buf(&ep, buf, &buf[hdrlen]);
156 ERR("invalid input file");
160 err = write_buf_to_file(ofname, buf, totlen);
162 ERR("unable to write to file '%s'", ofname);
174 static int check_params(void)
178 if (ifname == NULL) {
179 ERR("no input file specified");
183 if (ofname == NULL) {
184 ERR("no output file specified");
188 if (crypt_key == NULL) {
189 ERR("no key specified");
191 } else if (strlen(crypt_key) > BCRYPT_MAX_KEYLEN) {
192 ERR("key '%s' is too long", crypt_key);
196 if (strlen(magic) != (ENC_MAGIC_LEN - 1)) {
197 ERR("length of magic must be %d", ENC_MAGIC_LEN - 1);
202 if (product == NULL) {
203 ERR("no product specified");
207 if (version == NULL) {
208 ERR("no version specified");
212 if (strlen(product) > (ENC_PRODUCT_LEN - 1)) {
213 ERR("product name '%s' is too long", product);
217 if (strlen(version) > (ENC_VERSION_LEN - 1)) {
218 ERR("version '%s' is too long", version);
229 int main(int argc, char *argv[])
231 int res = EXIT_FAILURE;
234 progname = basename(argv[0]);
239 c = getopt(argc, argv, "adi:m:o:hp:v:k:r:s:");
269 seed = strtoul(optarg, NULL, 16);
280 err = check_params();
285 err = decrypt_file();
287 err = encrypt_file();