59b66ab930cc22ac42ccbe8709e46b850d760c10
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
;
88 ep
.longstate
= longstate
;
90 err
= decrypt_buf(&ep
, buf
, src_len
);
92 ERR("unable to decrypt '%s'", ifname
);
96 printf("Magic\t\t: '%s'\n", ep
.magic
);
97 printf("Seed\t\t: 0x%02x\n", ep
.seed
);
98 printf("Product\t\t: '%s'\n", ep
.product
);
99 printf("Version\t\t: '%s'\n", ep
.version
);
100 printf("Data len\t: %u\n", ep
.datalen
);
101 printf("Checksum\t: 0x%08x\n", ep
.csum
);
103 err
= write_buf_to_file(ofname
, buf
, ep
.datalen
);
105 ERR("unable to write to file '%s'", ofname
);
116 static int encrypt_file(void)
126 src_len
= get_file_size(ifname
);
128 ERR("unable to get size of '%s'", ifname
);
132 totlen
= enc_compute_buf_len(product
, version
, src_len
);
133 hdrlen
= enc_compute_header_len(product
, version
);
135 buf
= malloc(totlen
);
137 ERR("no memory for the buffer");
141 err
= read_file_to_buf(ifname
, &buf
[hdrlen
], src_len
);
143 ERR("unable to read from file '%s'", ofname
);
147 memset(&ep
, '\0', sizeof(ep
));
148 ep
.key
= (unsigned char *) crypt_key
;
150 ep
.longstate
= longstate
;
151 ep
.csum
= buffalo_csum(src_len
, &buf
[hdrlen
], src_len
);
152 ep
.datalen
= src_len
;
153 strcpy((char *) ep
.magic
, magic
);
154 strcpy((char *) ep
.product
, product
);
155 strcpy((char *) ep
.version
, version
);
157 err
= encrypt_buf(&ep
, buf
, &buf
[hdrlen
]);
159 ERR("invalid input file");
163 err
= write_buf_to_file(ofname
, buf
, totlen
);
165 ERR("unable to write to file '%s'", ofname
);
177 static int check_params(void)
181 if (ifname
== NULL
) {
182 ERR("no input file specified");
186 if (ofname
== NULL
) {
187 ERR("no output file specified");
191 if (crypt_key
== NULL
) {
192 ERR("no key specified");
194 } else if (strlen(crypt_key
) > BCRYPT_MAX_KEYLEN
) {
195 ERR("key '%s' is too long", crypt_key
);
199 if (strlen(magic
) != (ENC_MAGIC_LEN
- 1)) {
200 ERR("length of magic must be %d", ENC_MAGIC_LEN
- 1);
205 if (product
== NULL
) {
206 ERR("no product specified");
210 if (version
== NULL
) {
211 ERR("no version specified");
215 if (strlen(product
) > (ENC_PRODUCT_LEN
- 1)) {
216 ERR("product name '%s' is too long", product
);
220 if (strlen(version
) > (ENC_VERSION_LEN
- 1)) {
221 ERR("version '%s' is too long", version
);
232 int main(int argc
, char *argv
[])
234 int res
= EXIT_FAILURE
;
237 progname
= basename(argv
[0]);
242 c
= getopt(argc
, argv
, "adi:m:o:hlp:v:k:r:s:");
272 seed
= strtoul(optarg
, NULL
, 16);
283 err
= check_params();
288 err
= decrypt_file();
290 err
= encrypt_file();