├── Makefile ├── .gitignore ├── README.md ├── LICENSE └── twt.c /Makefile: -------------------------------------------------------------------------------- 1 | TWT=twt 2 | RM=rm 3 | DD=dd 4 | LIBS=-lcrypto 5 | CFLAGS=-O2 6 | SOURCES=twt.c 7 | 8 | $(TWT): $(SOURCES) 9 | $(CC) $(CFLAGS) -o $@ $^ $(LIBS) 10 | 11 | clean: 12 | $(RM) $(TWT) 13 | 14 | test: 15 | $(DD) bs=1024 count=1024 if=/dev/random of=test_a 16 | ./$(TWT) -c encrypt -s test_a -d test_b 17 | ./$(TWT) -c decrypt -s test_b -d test_c 18 | 19 | 20 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Object files 5 | *.o 6 | *.ko 7 | *.obj 8 | *.elf 9 | 10 | # Linker output 11 | *.ilk 12 | *.map 13 | *.exp 14 | 15 | # Precompiled Headers 16 | *.gch 17 | *.pch 18 | 19 | # Libraries 20 | *.lib 21 | *.a 22 | *.la 23 | *.lo 24 | 25 | # Shared objects (inc. Windows DLLs) 26 | *.dll 27 | *.so 28 | *.so.* 29 | *.dylib 30 | 31 | # Executables 32 | *.exe 33 | *.out 34 | *.app 35 | *.i*86 36 | *.x86_64 37 | *.hex 38 | 39 | # Debug files 40 | *.dSYM/ 41 | *.su 42 | *.idb 43 | *.pdb 44 | 45 | # Kernel Module Compile Results 46 | *.mod* 47 | *.cmd 48 | .tmp_versions/ 49 | modules.order 50 | Module.symvers 51 | Mkfile.old 52 | dkms.conf 53 | 54 | twt 55 | test_a 56 | test_b 57 | test_c 58 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # topwaytool 2 | Tool for managing Android OS images on TopWay car headunit 3 | 4 | This tool is used for creating and extracting "signed" image files for some TopWay headunits. 5 | I've only tested on my Allwinner T8 using firmware images found on xda-developers. 6 | 7 | See https://forum.xda-developers.com/t/tutorial-how-to-modify-allwinner-t8-firmware-and-to-be-able-to-apply-it.4054951/ 8 | 9 | ## Requirements 10 | I've only tested on Linux. The code has minimal dependencies and should only require a basic C compiling 11 | environment. 12 | 13 | In order to work with the OS images, you will need to be able to mount them in loopback mode. This also requires Linux. 14 | ie `mount -o loop system.img mnt/system`. 15 | 16 | ## How to build 17 | 18 | `make` 19 | 20 | ## Commands 21 | 22 | - verify - Check the checksum on system.img. This file is usually in 3x 400M parts and needs to be `cat`ed together. 23 | - update - Updates the checksum on system.img. 24 | - decrypt - "Decrypts" the boot.img or vendor.img files. 25 | - encrypt - "Encrypts" the boot.img or vendor.img files. 26 | 27 | This tool, combined with `unpackbootimg` and `mkbootimg` (see android source code) should give a developer nearly 28 | full control over customizing firmware for their headunit. 29 | 30 | Credit goes to the authors of cks and dcupdate.py from which this tool was inspired from. 31 | 32 | Enjoy! 33 | 34 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /twt.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | typedef enum result { OK = 0, ERROR, MEM_ERROR, IO_ERROR } result_t; 10 | 11 | #define NELEM(x) (sizeof(x)/sizeof(x[0])) 12 | #define MIN(a,b) ((akey_block, ctx->xor_block, ctx->xor_block); 120 | for(index = 0; index < hash_size; index++) { 121 | data[index] ^= ctx->xor_block[index]; 122 | } 123 | if (fwrite(data, 1, bytes_read, ctx->out) != bytes_read) { 124 | fprintf(stderr, "Unable to write output block\n"); 125 | return IO_ERROR; 126 | } 127 | return OK; 128 | } 129 | 130 | result_t decrypt_image(FILE* in, FILE* out) { 131 | size_t file_size; 132 | size_t content_size; 133 | uint8_t file_md5hash[hash_size]; 134 | uint8_t computed_md5hash[hash_size]; 135 | de_obfuscate_context_t context; 136 | context.out = out; 137 | 138 | fseek(in, 0L, SEEK_END); 139 | file_size = ftell(in); 140 | content_size = file_size - (hash_size + key_size); 141 | if (content_size < 0 || (content_size % 16) != 0) { 142 | fprintf(stderr, "Source file too small or not a multiple of %d.\n", hash_size); 143 | return ERROR; 144 | } 145 | fseek(in, content_size, SEEK_SET); 146 | 147 | if (fread(context.key_block, sizeof(uint8_t), key_size, in) != key_size) { 148 | fprintf(stderr, "Unable to read the key block.\n"); 149 | return IO_ERROR; 150 | } 151 | 152 | if (fread(file_md5hash, sizeof(uint8_t), hash_size, in) != hash_size) { 153 | fprintf(stderr, "Unable to read the MD5 hash of the source file.\n"); 154 | return IO_ERROR; 155 | } 156 | 157 | fseek(in, 0L, SEEK_SET); 158 | if (md5_hash_file(in, file_size - hash_size, computed_md5hash) != OK) { 159 | fprintf(stderr, "Error processing md5 hashing.\n"); 160 | return ERROR; 161 | } 162 | 163 | obfuscate_block(context.key_block, computed_md5hash, computed_md5hash); 164 | 165 | fprint_hex(stderr, "Key :", context.key_block); 166 | fprint_hex(stderr, "Read MD5 hash :", file_md5hash); 167 | fprint_hex(stderr, "Calculated MD5 hash:", computed_md5hash); 168 | 169 | if (memcmp(file_md5hash, computed_md5hash, hash_size) != 0) { 170 | fprintf(stderr, "Hashes don't match. Stopping.\n"); 171 | return ERROR; 172 | } 173 | 174 | fprintf(stderr, "Hashes match. Continuing.\n"); 175 | 176 | fprintf(stderr, "Working...\r"); 177 | memset(context.xor_block, 0, sizeof(context.xor_block)); 178 | fseek(in, 0L, SEEK_SET); 179 | return block_processor(in, hash_size, content_size, &de_obfuscate_block_processor, &context); 180 | } 181 | 182 | result_t obfuscate_block_processor(uint8_t* data, size_t bytes_read, void* context) { 183 | de_obfuscate_context_t* ctx = (de_obfuscate_context_t*) context; 184 | int index; 185 | 186 | obfuscate_block(ctx->key_block, ctx->xor_block, ctx->xor_block); 187 | for(index = 0; index < hash_size; index++) { 188 | data[index] ^= ctx->xor_block[index]; 189 | } 190 | if (fwrite(data, 1, bytes_read, ctx->out) != bytes_read) { 191 | fprintf(stderr, "Unable to write output block\n"); 192 | return IO_ERROR; 193 | } 194 | return OK; 195 | } 196 | 197 | result_t encrypt_image(FILE* in, FILE* out, const char *iv) { 198 | uint8_t computed_md5hash[hash_size]; 199 | size_t content_size; 200 | int index; 201 | de_obfuscate_context_t context; 202 | context.out = out; 203 | 204 | memset(context.xor_block, 0, sizeof(context.xor_block)); 205 | 206 | if (iv != NULL && strlen(iv) == hash_size * 2) { 207 | for(index = 0;index < hash_size; index++) { 208 | context.key_block[index] = hexbyte(iv + (index * 2)); 209 | } 210 | } else { 211 | for(index = 0; index < hash_size; index++) { 212 | context.key_block[index] = random() & 0xFF; 213 | } 214 | } 215 | 216 | fprint_hex(stderr, "Key :", context.key_block); 217 | fseek(in, 0L, SEEK_END); 218 | content_size = ftell(in); 219 | if (content_size % 16 != 0) { 220 | fprintf(stderr, "Input size needs to be a multiple of 16\n"); 221 | return ERROR; 222 | } 223 | fseek(in, 0L, SEEK_SET); 224 | 225 | if (block_processor(in, hash_size, content_size, &obfuscate_block_processor, &context) != OK) { 226 | fprintf(stderr, "Error processing file.\n"); 227 | return ERROR; 228 | } 229 | 230 | if (fwrite(context.key_block, key_size, 1, out) != 1) { 231 | fprintf(stderr, "Unable to write key to output.\n"); 232 | return IO_ERROR; 233 | } 234 | 235 | fseek(out, 0L, SEEK_SET); 236 | if (md5_hash_file(out, content_size + key_size, computed_md5hash) != OK) { 237 | fprintf(stderr, "Error processing md5 hashing.\n"); 238 | return ERROR; 239 | } 240 | obfuscate_block(context.key_block, computed_md5hash, computed_md5hash); 241 | 242 | fprint_hex(stderr, "Calculated MD5 hash:", computed_md5hash); 243 | if (fwrite(computed_md5hash, hash_size, 1, out) != 1) { 244 | fprintf(stderr, "Unable to write computed md5 to output.\n"); 245 | return IO_ERROR; 246 | } 247 | 248 | return OK; 249 | } 250 | 251 | result_t chksum_block_processor(uint8_t* data, size_t bytes_read, void *context) { 252 | uint32_t* checksum = (uint32_t*) context; 253 | uint32_t* cast_data = (uint32_t*) data; 254 | int index; 255 | for(index = 0; index < bytes_read / sizeof(uint32_t); index++) { 256 | *checksum += cast_data[index]; 257 | } 258 | return OK; 259 | } 260 | 261 | result_t chksum(FILE* in, size_t content_size, uint32_t* checksum) { 262 | const uint32_t block_size = 256;; 263 | result_t result; 264 | *checksum = 0xFFFFFFFF; 265 | if ((result = block_processor(in, block_size, content_size, &chksum_block_processor, checksum)) == OK) { 266 | *checksum = ~(*checksum); 267 | } 268 | return result; 269 | } 270 | 271 | result_t verify_checksum(FILE* in) { 272 | uint32_t read_checksum = 0; 273 | uint32_t computed_checksum = 0; 274 | int checksum_size = sizeof(uint32_t); 275 | 276 | size_t file_size; 277 | fseek(in, 0, SEEK_END); 278 | file_size = ftell(in); 279 | 280 | if (file_size == 0 || 281 | file_size % checksum_size != 0 || 282 | ((file_size - checksum_size) % 1024) != 0) { 283 | fprintf(stderr, "This file doesn't appear to have a %d byte checksum appended to it.\n", checksum_size); 284 | return ERROR; 285 | } 286 | 287 | fseek(in, file_size - checksum_size, SEEK_SET); 288 | if (fread(&read_checksum, checksum_size, 1, in) != 1) { 289 | fprintf(stderr, "Unable to read checksum from file.\n"); 290 | return IO_ERROR; 291 | } 292 | 293 | fseek(in, 0, SEEK_SET); 294 | fprintf(stderr, "Working...\r"); 295 | if (chksum(in, file_size - checksum_size, &computed_checksum) != OK) { 296 | fprintf(stderr, "Error computing checksum.\n"); 297 | return ERROR; 298 | } 299 | 300 | fprintf(stderr, "Read checksum is : %08X\n", read_checksum); 301 | fprintf(stderr, "Computed checksum is: %08X\n", computed_checksum); 302 | return read_checksum != computed_checksum; 303 | } 304 | 305 | result_t update_checksum(FILE* in) { 306 | size_t file_size; 307 | uint32_t computed_checksum = 0; 308 | int checksum_size = sizeof(uint32_t); 309 | uint8_t update = 1; 310 | 311 | fseek(in, 0, SEEK_END); 312 | file_size = ftell(in); 313 | update = (file_size % 1024) != 0 ; 314 | if (update) { 315 | file_size -= checksum_size; 316 | } 317 | fprintf(stderr, "File size indicates checksum needs to be %s.\n", update?"updated":"added"); 318 | fseek(in, 0, SEEK_SET); 319 | fprintf(stderr, "Working...\r"); 320 | if (chksum(in, file_size, &computed_checksum) != OK) { 321 | fprintf(stderr, "Error computing checksum.\n"); 322 | return ERROR; 323 | } 324 | fprintf(stderr, "Computed checksum is: %08X\n", computed_checksum); 325 | if (fwrite(&computed_checksum, checksum_size, 1, in) != 1) { 326 | fprintf(stderr, "Error writing checksum.\n"); 327 | return IO_ERROR; 328 | } 329 | 330 | return OK; 331 | } 332 | 333 | void help() { 334 | fprintf(stderr, "TopWay Tool Usage:\n\n"); 335 | fprintf(stderr, " -h help (this message)\n"); 336 | fprintf(stderr, " -c command. Valid commands are verify, update, encrypt, decrypt\n"); 337 | fprintf(stderr, "\n"); 338 | fprintf(stderr, " verify - verify checksum in system.img file\n"); 339 | fprintf(stderr, " update - update checksum in system.img file\n"); 340 | fprintf(stderr, " encrypt - encrypt a boot.img or vendor.img file\n"); 341 | fprintf(stderr, " decrypt - decrypt a boot.img or vendor.img file\n"); 342 | fprintf(stderr, "\n"); 343 | fprintf(stderr, " -k optional: provide key (32 char hex) for encrypt\n"); 344 | fprintf(stderr, " -s source file or - for stdin\n"); 345 | fprintf(stderr, " -d destination file or - for stdout\n"); 346 | } 347 | 348 | int main(int argc, char*argv[]) { 349 | enum command_t { verify, update, encrypt, decrypt }; 350 | int opt; 351 | int result = 0; 352 | char *src_file = NULL; 353 | char *dst_file = NULL; 354 | char *iv = NULL; 355 | char *cmd = NULL; 356 | FILE *in, *out; 357 | enum command_t command; 358 | 359 | while((opt = getopt(argc, argv, "hc:k:s:d:")) >= 0) { 360 | switch(opt) { 361 | case 'h': 362 | help(); 363 | return result; 364 | case 'c': 365 | cmd = optarg; 366 | break; 367 | case 'k': 368 | iv = optarg; 369 | break; 370 | case 's': 371 | src_file = optarg; 372 | break; 373 | case 'd': 374 | dst_file = optarg; 375 | break; 376 | } 377 | } 378 | 379 | if(cmd == NULL) { 380 | help(); 381 | return result; 382 | } else if (strcmp(cmd, "verify") == 0) { 383 | command = verify; 384 | } else if (strcmp(cmd, "update") == 0) { 385 | command = update; 386 | } else if (strcmp(cmd, "decrypt") == 0) { 387 | command = decrypt; 388 | } else if (strcmp(cmd, "encrypt") == 0) { 389 | command = encrypt; 390 | } else { 391 | fprintf(stderr, "Unknown command [%s]\n", cmd); 392 | return 1; 393 | } 394 | 395 | 396 | while(1) { 397 | if (src_file == NULL) { 398 | fprintf(stderr, "No source file specified.\n"); 399 | result = 1; 400 | break; 401 | } 402 | 403 | if (strcmp(src_file,"-") == 0) { 404 | in = stdin; 405 | } else { 406 | in = fopen(src_file, (command == update)?"r+b":"rb"); 407 | } 408 | 409 | if (in == NULL) { 410 | fprintf(stderr, "Unable to open source file [%s]\n", src_file); 411 | result = 1; 412 | break; 413 | } 414 | 415 | if(command == verify || command == update) { 416 | /* Source only commands */ 417 | switch(command) { 418 | case verify: 419 | result = verify_checksum(in); 420 | break; 421 | case update: 422 | result = update_checksum(in); 423 | break; 424 | } 425 | fclose(in); 426 | } else { 427 | /* Source and Destination commands */ 428 | if (dst_file == NULL) { 429 | fprintf(stderr, "No destination file specified.\n"); 430 | result = 1; 431 | fclose(in); 432 | break; 433 | } 434 | 435 | if (strcmp(dst_file,"-") == 0) { 436 | out = stdout; 437 | } else { 438 | out = fopen(dst_file, "w+b"); 439 | } 440 | 441 | if (out == NULL) { 442 | fprintf(stderr, "Unable to open destination file [%s]\n", dst_file); 443 | result = 1; 444 | fclose(in); 445 | break; 446 | } 447 | 448 | switch(command){ 449 | case decrypt: 450 | result = decrypt_image(in, out); 451 | break; 452 | case encrypt: 453 | result = encrypt_image(in, out, iv); 454 | break; 455 | } 456 | fclose(in); 457 | fclose(out); 458 | } 459 | break; 460 | } 461 | 462 | return result; 463 | } 464 | --------------------------------------------------------------------------------