2 * Copyright (C) 2013 Jeff Kent <jeff@jkent.net>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 * This tool encrypts and decrypts uImage formatted firmware for Hilink
18 * HLK-RM04 wireless modules. It will also truncate a dump of mtd6 and make
19 * it an image suitable for flashing via the stock firmware upgrade page.
22 * gcc -lcrypto hlkcrypt.c -o hlkcrypt
28 #include <openssl/des.h>
34 #include <sys/types.h>
38 #define DES_KEY "H@L9K*(3"
42 ({ __typeof__ (a) _a = (a); \
43 __typeof__ (b) _b = (b); \
47 #define IH_MAGIC 0x27051956
49 typedef struct image_header {
50 uint32_t ih_magic; /* Image Header Magic Number */
51 uint32_t ih_hcrc; /* Image Header CRC Checksum */
52 uint32_t ih_time; /* Image Creation Timestamp */
53 uint32_t ih_size; /* Image Data Size */
54 uint32_t ih_load; /* Data Load Address */
55 uint32_t ih_ep; /* Entry Point Address */
56 uint32_t ih_dcrc; /* Image Data CRC Checksum */
57 uint8_t ih_os; /* Operating System */
58 uint8_t ih_arch; /* CPU architecture */
59 uint8_t ih_type; /* Image Type */
60 uint8_t ih_comp; /* Compression Type */
61 uint8_t ih_name[IH_NMLEN]; /* Image Name */
64 static int temp_fd = -1;
65 static DES_key_schedule schedule;
67 static void show_usage(const char *arg0);
68 static void exit_cleanup(void);
69 static void copy_file(int src, int dst);
70 static void do_encrypt(void *p, off_t len);
71 static void do_decrypt(void *p, off_t len);
74 int main(int argc, char **argv)
80 char *input_filename = NULL;
81 char *output_filename = NULL;
87 char buf[sizeof(image_header_t) + 3];
88 image_header_t *header;
91 static struct option long_options[] = {
92 {"encrypt", no_argument, 0, 'e'},
93 {"decrypt", no_argument, 0, 'd'},
94 {"input", required_argument, 0, 'i'},
95 {"output", required_argument, 0, 'o'},
99 int c = getopt_long(argc, argv, "dei:o:",
100 long_options, &option_index);
107 if (decrypt_opt > 1) {
108 fprintf(stderr, "%s: decrypt may only be specified once\n",
116 if (encrypt_opt > 1) {
117 fprintf(stderr, "%s: encrypt may only be specified once\n",
126 fprintf(stderr, "%s: only one input file may be specified\n",
130 if (strcmp("-", optarg) != 0) {
131 input_filename = optarg;
137 if (output_opt > 1) {
138 fprintf(stderr, "%s: only one output file may be specified\n",
142 if (strcmp("-", optarg) != 0) {
143 output_filename = optarg;
155 if (decrypt_opt && encrypt_opt) {
156 fprintf(stderr, "%s: decrypt and encrypt may not be used together\n",
161 if (!decrypt_opt && !encrypt_opt) {
162 fprintf(stderr, "%s: neither decrypt or encrypt were specified\n",
167 temp_fd = fileno(tmpfile());
169 fprintf(stderr, "Can't create temporary file\n");
173 atexit(exit_cleanup);
174 DES_set_key_unchecked((const_DES_cblock *)DES_KEY, &schedule);
176 if (input_filename) {
177 input_fd = open(input_filename, O_RDONLY);
179 fprintf(stderr, "Can't open %s for reading: %s\n", input_filename,
183 copy_file(input_fd, temp_fd);
187 copy_file(STDIN_FILENO, temp_fd);
190 file_len = lseek(temp_fd, 0, SEEK_CUR);
192 fprintf(stderr, "Not enough data\n");
196 p = mmap(0, file_len, PROT_READ|PROT_WRITE, MAP_SHARED, temp_fd, 0);
197 if (p == MAP_FAILED) {
198 fprintf(stderr, "mmap failed: %s\n", strerror(errno));
203 header = (image_header_t *)p;
204 off_t len = min(file_len,
205 ntohl(header->ih_size) + sizeof(image_header_t));
206 if (ntohl(header->ih_magic) != IH_MAGIC) {
207 fprintf(stderr, "Header magic incorrect: "
208 "expected 0x%08X, got 0x%08X\n",
209 IH_MAGIC, ntohl(header->ih_magic));
215 if (len != file_len) {
216 if (ftruncate(temp_fd, len) < 0) {
217 fprintf(stderr, "ftruncate failed: %s\n", strerror(errno));
224 off_t header_len = min(file_len, sizeof(image_header_t) + 3);
225 memcpy(buf, p, header_len);
226 do_decrypt(buf, header_len);
227 header = (image_header_t *)buf;
228 if (ntohl(header->ih_magic) != IH_MAGIC) {
229 fprintf(stderr, "Header magic incorrect: "
230 "expected 0x%08X, got 0x%08X\n",
231 IH_MAGIC, ntohl(header->ih_magic));
234 do_decrypt(p, file_len);
238 lseek(temp_fd, 0, SEEK_SET);
239 if (output_filename) {
240 output_fd = creat(output_filename, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
242 fprintf(stderr, "Can't open %s for writing: %s\n",
243 output_filename, strerror(errno));
246 copy_file(temp_fd, output_fd);
250 copy_file(temp_fd, STDOUT_FILENO);
257 static void show_usage(const char *arg0)
259 fprintf(stderr, "usage: %s -d|-e [-i FILE] [-o FILE]\n\n", arg0);
260 fprintf(stderr, "%-15s %s\n", "-d, --decrypt", "decrypt data");
261 fprintf(stderr, "%-15s %s\n", "-e, --encrypt", "encrypt data");
262 fprintf(stderr, "%-15s %s\n", "-i, --input", "intput file (defaults to stdin)");
263 fprintf(stderr, "%-15s %s\n", "-o, --output", "output file (defaults to stdout)");
267 static void exit_cleanup(void)
274 static void copy_file(int src, int dst)
279 while ((size = read(src, buf, 4096)) > 0) {
280 write(dst, buf, size);
284 static void do_encrypt(void *p, off_t len)
289 num_blocks = len / 8;
290 pblock = (DES_cblock *) p;
291 while (num_blocks--) {
292 DES_ecb_encrypt(pblock, pblock, &schedule, DES_ENCRYPT);
296 num_blocks = (len - 3) / 8;
297 pblock = (DES_cblock *) (p + 3);
298 while (num_blocks--) {
299 DES_ecb_encrypt(pblock, pblock, &schedule, DES_ENCRYPT);
304 static void do_decrypt(void *p, off_t len)
309 num_blocks = (len - 3) / 8;
310 pblock = (DES_cblock *) (p + 3);
311 while (num_blocks--) {
312 DES_ecb_encrypt(pblock, pblock, &schedule, DES_DECRYPT);
316 num_blocks = len / 8;
317 pblock = (DES_cblock *) p;
318 while (num_blocks--) {
319 DES_ecb_encrypt(pblock, pblock, &schedule, DES_DECRYPT);