08fad4ee61b4b18f6bd5666ad26224e5e43541c0
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
;
40 void usage(int status
)
42 FILE *stream
= (status
!= EXIT_SUCCESS
) ? stderr
: stdout
;
44 fprintf(stream
, "Usage: %s [OPTIONS...]\n", progname
);
48 " -d decrypt instead of encrypt\n"
49 " -i <file> read input from the file <file>\n"
50 " -o <file> write output to the file <file>\n"
51 " -l use longstate {en,de}cryption method\n"
52 " -k <key> use <key> for encryption (default: Buffalo)\n"
53 " -m <magic> set magic to <magic>\n"
54 " -p <product> set product name to <product>\n"
55 " -v <version> set version to <version>\n"
56 " -h show this screen\n"
57 " -O Offset of encrypted data in file (decryption)\n"
58 " -S Size of unencrypted data in file (encryption)\n"
64 static int decrypt_file(void)
68 unsigned char *buf
= NULL
;
72 src_len
= get_file_size(ifname
);
74 ERR("unable to get size of '%s'", ifname
);
78 buf
= malloc(src_len
);
80 ERR("no memory for the buffer");
84 err
= read_file_to_buf(ifname
, buf
, src_len
);
86 ERR("unable to read from file '%s'", ifname
);
90 memset(&ep
, '\0', sizeof(ep
));
91 ep
.key
= (unsigned char *) crypt_key
;
92 ep
.longstate
= longstate
;
94 err
= decrypt_buf(&ep
, buf
+ offset
, src_len
- offset
);
96 ERR("unable to decrypt '%s'", ifname
);
100 printf("Magic\t\t: '%s'\n", ep
.magic
);
101 printf("Seed\t\t: 0x%02x\n", ep
.seed
);
102 printf("Product\t\t: '%s'\n", ep
.product
);
103 printf("Version\t\t: '%s'\n", ep
.version
);
104 printf("Data len\t: %u\n", ep
.datalen
);
105 printf("Checksum\t: 0x%08x\n", ep
.csum
);
107 err
= write_buf_to_file(ofname
, buf
+ offset
, ep
.datalen
);
109 ERR("unable to write to file '%s'", ofname
);
120 static int encrypt_file(void)
123 ssize_t src_len
, tail_dst
, tail_len
, tail_src
;
130 src_len
= get_file_size(ifname
);
132 ERR("unable to get size of '%s'", ifname
);
137 tail_dst
= enc_compute_buf_len(product
, version
, size
);
138 tail_len
= src_len
- size
;
139 totlen
= tail_dst
+ tail_len
;
141 totlen
= enc_compute_buf_len(product
, version
, src_len
);
143 buf
= malloc(totlen
);
145 ERR("no memory for the buffer");
149 hdrlen
= enc_compute_header_len(product
, version
);
151 err
= read_file_to_buf(ifname
, &buf
[hdrlen
], src_len
);
153 ERR("unable to read from file '%s'", ofname
);
158 tail_src
= hdrlen
+ size
;
159 memmove(&buf
[tail_dst
], &buf
[tail_src
], tail_len
);
160 memset(&buf
[tail_src
], 0, tail_dst
- tail_src
);
164 memset(&ep
, '\0', sizeof(ep
));
165 ep
.key
= (unsigned char *) crypt_key
;
167 ep
.longstate
= longstate
;
168 ep
.csum
= buffalo_csum(src_len
, &buf
[hdrlen
], src_len
);
169 ep
.datalen
= src_len
;
170 strcpy((char *) ep
.magic
, magic
);
171 strcpy((char *) ep
.product
, product
);
172 strcpy((char *) ep
.version
, version
);
174 err
= encrypt_buf(&ep
, buf
, &buf
[hdrlen
]);
176 ERR("invalid input file");
180 err
= write_buf_to_file(ofname
, buf
, totlen
);
182 ERR("unable to write to file '%s'", ofname
);
194 static int check_params(void)
198 if (ifname
== NULL
) {
199 ERR("no input file specified");
203 if (ofname
== NULL
) {
204 ERR("no output file specified");
208 if (crypt_key
== NULL
) {
209 ERR("no key specified");
211 } else if (strlen(crypt_key
) > BCRYPT_MAX_KEYLEN
) {
212 ERR("key '%s' is too long", crypt_key
);
216 if (strlen(magic
) != (ENC_MAGIC_LEN
- 1)) {
217 ERR("length of magic must be %d", ENC_MAGIC_LEN
- 1);
222 if (product
== NULL
) {
223 ERR("no product specified");
227 if (version
== NULL
) {
228 ERR("no version specified");
232 if (strlen(product
) > (ENC_PRODUCT_LEN
- 1)) {
233 ERR("product name '%s' is too long", product
);
237 if (strlen(version
) > (ENC_VERSION_LEN
- 1)) {
238 ERR("version '%s' is too long", version
);
249 int main(int argc
, char *argv
[])
251 int res
= EXIT_FAILURE
;
254 progname
= basename(argv
[0]);
259 c
= getopt(argc
, argv
, "adi:m:o:hlp:v:k:O:r:s:S:");
289 seed
= strtoul(optarg
, NULL
, 16);
292 offset
= strtoul(optarg
, NULL
, 0);
295 size
= strtoul(optarg
, NULL
, 0);
306 err
= check_params();
311 err
= decrypt_file();
313 err
= encrypt_file();