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>
36 #define DES_KEY "H@L9K*(3"
40 ({ __typeof__ (a) _a = (a); \
41 __typeof__ (b) _b = (b); \
45 #define IH_MAGIC 0x27051956
47 typedef struct image_header
{
48 uint32_t ih_magic
; /* Image Header Magic Number */
49 uint32_t ih_hcrc
; /* Image Header CRC Checksum */
50 uint32_t ih_time
; /* Image Creation Timestamp */
51 uint32_t ih_size
; /* Image Data Size */
52 uint32_t ih_load
; /* Data Load Address */
53 uint32_t ih_ep
; /* Entry Point Address */
54 uint32_t ih_dcrc
; /* Image Data CRC Checksum */
55 uint8_t ih_os
; /* Operating System */
56 uint8_t ih_arch
; /* CPU architecture */
57 uint8_t ih_type
; /* Image Type */
58 uint8_t ih_comp
; /* Compression Type */
59 uint8_t ih_name
[IH_NMLEN
]; /* Image Name */
62 static int temp_fd
= -1;
63 static DES_key_schedule schedule
;
65 static void show_usage(const char *arg0
);
66 static void exit_cleanup(void);
67 static void copy_file(int src
, int dst
);
68 static void encrypt(void *p
, off_t len
);
69 static void decrypt(void *p
, off_t len
);
72 int main(int argc
, char **argv
)
78 char *input_filename
= NULL
;
79 char *output_filename
= NULL
;
85 char buf
[sizeof(image_header_t
) + 3];
86 image_header_t
*header
;
89 static struct option long_options
[] = {
90 {"encrypt", no_argument
, 0, 'e'},
91 {"decrypt", no_argument
, 0, 'd'},
92 {"input", required_argument
, 0, 'i'},
93 {"output", required_argument
, 0, 'o'},
97 int c
= getopt_long(argc
, argv
, "dei:o:",
98 long_options
, &option_index
);
105 if (decrypt_opt
> 1) {
106 fprintf(stderr
, "%s: decrypt may only be specified once\n",
114 if (encrypt_opt
> 1) {
115 fprintf(stderr
, "%s: encrypt may only be specified once\n",
124 fprintf(stderr
, "%s: only one input file may be specified\n",
128 if (strcmp("-", optarg
) != 0) {
129 input_filename
= optarg
;
135 if (output_opt
> 1) {
136 fprintf(stderr
, "%s: only one output file may be specified\n",
140 if (strcmp("-", optarg
) != 0) {
141 output_filename
= optarg
;
153 if (decrypt_opt
&& encrypt_opt
) {
154 fprintf(stderr
, "%s: decrypt and encrypt may not be used together\n",
159 if (!decrypt_opt
&& !encrypt_opt
) {
160 fprintf(stderr
, "%s: neither decrypt or encrypt were specified\n",
165 temp_fd
= fileno(tmpfile());
167 fprintf(stderr
, "Can't create temporary file\n");
171 atexit(exit_cleanup
);
172 DES_set_key_unchecked((const_DES_cblock
*)DES_KEY
, &schedule
);
174 if (input_filename
) {
175 input_fd
= open(input_filename
, O_RDONLY
);
177 fprintf(stderr
, "Can't open %s for reading: %s\n", input_filename
,
181 copy_file(input_fd
, temp_fd
);
185 copy_file(STDIN_FILENO
, temp_fd
);
188 file_len
= lseek(temp_fd
, 0, SEEK_CUR
);
190 fprintf(stderr
, "Not enough data\n");
194 p
= mmap(0, file_len
, PROT_READ
|PROT_WRITE
, MAP_SHARED
, temp_fd
, 0);
195 if (p
== MAP_FAILED
) {
196 fprintf(stderr
, "mmap failed: %s\n", strerror(errno
));
201 header
= (image_header_t
*)p
;
202 off_t len
= min(file_len
,
203 ntohl(header
->ih_size
) + sizeof(image_header_t
));
204 if (ntohl(header
->ih_magic
) != IH_MAGIC
) {
205 fprintf(stderr
, "Header magic incorrect: "
206 "expected 0x%08X, got 0x%08X\n",
207 IH_MAGIC
, ntohl(header
->ih_magic
));
213 if (len
!= file_len
) {
214 if (ftruncate(temp_fd
, len
) < 0) {
215 fprintf(stderr
, "ftruncate failed: %s\n", strerror(errno
));
222 off_t header_len
= min(file_len
, sizeof(image_header_t
) + 3);
223 memcpy(buf
, p
, header_len
);
224 decrypt(buf
, header_len
);
225 header
= (image_header_t
*)buf
;
226 if (ntohl(header
->ih_magic
) != IH_MAGIC
) {
227 fprintf(stderr
, "Header magic incorrect: "
228 "expected 0x%08X, got 0x%08X\n",
229 IH_MAGIC
, ntohl(header
->ih_magic
));
232 decrypt(p
, file_len
);
236 lseek(temp_fd
, 0, SEEK_SET
);
237 if (output_filename
) {
238 output_fd
= creat(output_filename
, S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IROTH
);
240 fprintf(stderr
, "Can't open %s for writing: %s\n",
241 output_filename
, strerror(errno
));
244 copy_file(temp_fd
, output_fd
);
248 copy_file(temp_fd
, STDOUT_FILENO
);
255 static void show_usage(const char *arg0
)
257 fprintf(stderr
, "usage: %s -d|-e [-i FILE] [-o FILE]\n\n", arg0
);
258 fprintf(stderr
, "%-15s %s\n", "-d, --decrypt", "decrypt data");
259 fprintf(stderr
, "%-15s %s\n", "-e, --encrypt", "encrypt data");
260 fprintf(stderr
, "%-15s %s\n", "-i, --input", "intput file (defaults to stdin)");
261 fprintf(stderr
, "%-15s %s\n", "-o, --output", "output file (defaults to stdout)");
265 static void exit_cleanup(void)
272 static void copy_file(int src
, int dst
)
277 while ((size
= read(src
, buf
, 4096)) > 0) {
278 write(dst
, buf
, size
);
282 static void encrypt(void *p
, off_t len
)
287 num_blocks
= len
/ 8;
288 pblock
= (DES_cblock
*) p
;
289 while (num_blocks
--) {
290 DES_ecb_encrypt(pblock
, pblock
, &schedule
, DES_ENCRYPT
);
294 num_blocks
= (len
- 3) / 8;
295 pblock
= (DES_cblock
*) (p
+ 3);
296 while (num_blocks
--) {
297 DES_ecb_encrypt(pblock
, pblock
, &schedule
, DES_ENCRYPT
);
302 static void decrypt(void *p
, off_t len
)
307 num_blocks
= (len
- 3) / 8;
308 pblock
= (DES_cblock
*) (p
+ 3);
309 while (num_blocks
--) {
310 DES_ecb_encrypt(pblock
, pblock
, &schedule
, DES_DECRYPT
);
314 num_blocks
= len
/ 8;
315 pblock
= (DES_cblock
*) p
;
316 while (num_blocks
--) {
317 DES_ecb_encrypt(pblock
, pblock
, &schedule
, DES_DECRYPT
);