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;
39 void usage(int status)
41 FILE *stream = (status != EXIT_SUCCESS) ? stderr : stdout;
43 fprintf(stream, "Usage: %s [OPTIONS...]\n", progname);
47 " -d decrypt instead of encrypt\n"
48 " -i <file> read input from the file <file>\n"
49 " -o <file> write output to the file <file>\n"
50 " -l use longstate {en,de}cryption method\n"
51 " -k <key> use <key> for encryption (default: Buffalo)\n"
52 " -m <magic> set magic to <magic>\n"
53 " -p <product> set product name to <product>\n"
54 " -v <version> set version to <version>\n"
55 " -h show this screen\n"
56 " -O Offset of encrypted data in file (decryption)\n"
62 static int decrypt_file(void)
66 unsigned char *buf = NULL;
70 src_len = get_file_size(ifname);
72 ERR("unable to get size of '%s'", ifname);
76 buf = malloc(src_len);
78 ERR("no memory for the buffer");
82 err = read_file_to_buf(ifname, buf, src_len);
84 ERR("unable to read from file '%s'", ifname);
88 memset(&ep, '\0', sizeof(ep));
89 ep.key = (unsigned char *) crypt_key;
90 ep.longstate = longstate;
92 err = decrypt_buf(&ep, buf + offset, src_len - offset);
94 ERR("unable to decrypt '%s'", ifname);
98 printf("Magic\t\t: '%s'\n", ep.magic);
99 printf("Seed\t\t: 0x%02x\n", ep.seed);
100 printf("Product\t\t: '%s'\n", ep.product);
101 printf("Version\t\t: '%s'\n", ep.version);
102 printf("Data len\t: %u\n", ep.datalen);
103 printf("Checksum\t: 0x%08x\n", ep.csum);
105 err = write_buf_to_file(ofname, buf + offset, ep.datalen);
107 ERR("unable to write to file '%s'", ofname);
118 static int encrypt_file(void)
128 src_len = get_file_size(ifname);
130 ERR("unable to get size of '%s'", ifname);
134 totlen = enc_compute_buf_len(product, version, src_len);
135 hdrlen = enc_compute_header_len(product, version);
137 buf = malloc(totlen);
139 ERR("no memory for the buffer");
143 err = read_file_to_buf(ifname, &buf[hdrlen], src_len);
145 ERR("unable to read from file '%s'", ofname);
149 memset(&ep, '\0', sizeof(ep));
150 ep.key = (unsigned char *) crypt_key;
152 ep.longstate = longstate;
153 ep.csum = buffalo_csum(src_len, &buf[hdrlen], src_len);
154 ep.datalen = src_len;
155 strcpy((char *) ep.magic, magic);
156 strcpy((char *) ep.product, product);
157 strcpy((char *) ep.version, version);
159 err = encrypt_buf(&ep, buf, &buf[hdrlen]);
161 ERR("invalid input file");
165 err = write_buf_to_file(ofname, buf, totlen);
167 ERR("unable to write to file '%s'", ofname);
179 static int check_params(void)
183 if (ifname == NULL) {
184 ERR("no input file specified");
188 if (ofname == NULL) {
189 ERR("no output file specified");
193 if (crypt_key == NULL) {
194 ERR("no key specified");
196 } else if (strlen(crypt_key) > BCRYPT_MAX_KEYLEN) {
197 ERR("key '%s' is too long", crypt_key);
201 if (strlen(magic) != (ENC_MAGIC_LEN - 1)) {
202 ERR("length of magic must be %d", ENC_MAGIC_LEN - 1);
207 if (product == NULL) {
208 ERR("no product specified");
212 if (version == NULL) {
213 ERR("no version specified");
217 if (strlen(product) > (ENC_PRODUCT_LEN - 1)) {
218 ERR("product name '%s' is too long", product);
222 if (strlen(version) > (ENC_VERSION_LEN - 1)) {
223 ERR("version '%s' is too long", version);
234 int main(int argc, char *argv[])
236 int res = EXIT_FAILURE;
239 progname = basename(argv[0]);
244 c = getopt(argc, argv, "adi:m:o:hlp:v:k:O:r:s:");
274 seed = strtoul(optarg, NULL, 16);
277 offset = strtoul(optarg, NULL, 0);
288 err = check_params();
293 err = decrypt_file();
295 err = encrypt_file();