1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2009-2011 Gabor Juhos <juhosg@openwrt.org>
11 #include <getopt.h> /* for getopt() */
14 #include "buffalo-lib.h"
16 #define ERR(fmt, args...) do { \
18 fprintf(stderr, "[%s] *** error: " fmt "\n", \
19 progname, ## args ); \
22 static char *progname
;
25 static char *crypt_key
= "Buffalo";
26 static char *magic
= "start";
28 static unsigned char seed
= 'O';
32 static int do_decrypt
;
36 void usage(int status
)
38 FILE *stream
= (status
!= EXIT_SUCCESS
) ? stderr
: stdout
;
40 fprintf(stream
, "Usage: %s [OPTIONS...]\n", progname
);
44 " -d decrypt instead of encrypt\n"
45 " -i <file> read input from the file <file>\n"
46 " -o <file> write output to the file <file>\n"
47 " -l use longstate {en,de}cryption method\n"
48 " -k <key> use <key> for encryption (default: Buffalo)\n"
49 " -m <magic> set magic to <magic>\n"
50 " -p <product> set product name to <product>\n"
51 " -v <version> set version to <version>\n"
52 " -h show this screen\n"
53 " -O Offset of encrypted data in file (decryption)\n"
54 " -S Size of unencrypted data in file (encryption)\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
+ offset
, src_len
- offset
);
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
+ offset
, ep
.datalen
);
105 ERR("unable to write to file '%s'", ofname
);
116 static int encrypt_file(void)
119 ssize_t src_len
, tail_dst
, tail_len
, tail_src
;
126 src_len
= get_file_size(ifname
);
128 ERR("unable to get size of '%s'", ifname
);
133 tail_dst
= enc_compute_buf_len(product
, version
, size
);
134 tail_len
= src_len
- size
;
135 totlen
= tail_dst
+ tail_len
;
137 totlen
= enc_compute_buf_len(product
, version
, src_len
);
139 buf
= malloc(totlen
);
141 ERR("no memory for the buffer");
145 hdrlen
= enc_compute_header_len(product
, version
);
147 err
= read_file_to_buf(ifname
, &buf
[hdrlen
], src_len
);
149 ERR("unable to read from file '%s'", ofname
);
154 tail_src
= hdrlen
+ size
;
155 memmove(&buf
[tail_dst
], &buf
[tail_src
], tail_len
);
156 memset(&buf
[tail_src
], 0, tail_dst
- tail_src
);
160 memset(&ep
, '\0', sizeof(ep
));
161 ep
.key
= (unsigned char *) crypt_key
;
163 ep
.longstate
= longstate
;
164 ep
.csum
= buffalo_csum(src_len
, &buf
[hdrlen
], src_len
);
165 ep
.datalen
= src_len
;
166 strcpy((char *) ep
.magic
, magic
);
167 strcpy((char *) ep
.product
, product
);
168 strcpy((char *) ep
.version
, version
);
170 err
= encrypt_buf(&ep
, buf
, &buf
[hdrlen
]);
172 ERR("invalid input file");
176 err
= write_buf_to_file(ofname
, buf
, totlen
);
178 ERR("unable to write to file '%s'", ofname
);
190 static int check_params(void)
194 if (ifname
== NULL
) {
195 ERR("no input file specified");
199 if (ofname
== NULL
) {
200 ERR("no output file specified");
204 if (crypt_key
== NULL
) {
205 ERR("no key specified");
207 } else if (strlen(crypt_key
) > BCRYPT_MAX_KEYLEN
) {
208 ERR("key '%s' is too long", crypt_key
);
212 if (strlen(magic
) != (ENC_MAGIC_LEN
- 1)) {
213 ERR("length of magic must be %d", ENC_MAGIC_LEN
- 1);
218 if (product
== NULL
) {
219 ERR("no product specified");
223 if (version
== NULL
) {
224 ERR("no version specified");
228 if (strlen(product
) > (ENC_PRODUCT_LEN
- 1)) {
229 ERR("product name '%s' is too long", product
);
233 if (strlen(version
) > (ENC_VERSION_LEN
- 1)) {
234 ERR("version '%s' is too long", version
);
245 int main(int argc
, char *argv
[])
247 int res
= EXIT_FAILURE
;
250 progname
= basename(argv
[0]);
255 c
= getopt(argc
, argv
, "adi:m:o:hlp:v:k:O:r:s:S:");
285 seed
= strtoul(optarg
, NULL
, 16);
288 offset
= strtoul(optarg
, NULL
, 0);
291 size
= strtoul(optarg
, NULL
, 0);
302 err
= check_params();
307 err
= decrypt_file();
309 err
= encrypt_file();