| [1] | 1 | #include <stdio.h>
|
|---|
| 2 | #include "aes.h"
|
|---|
| 3 | #include "crc.h"
|
|---|
| 4 | #include <string.h>
|
|---|
| 5 |
|
|---|
| 6 | #define MAX_LENGH (2 * 1024 * 1024) // muss durch 16 teilbar. Da AES auf 16Bit Buffer eingestellt, eventuell �nderbar
|
|---|
| 7 |
|
|---|
| 8 | #define MAX_PATH 255
|
|---|
| 9 |
|
|---|
| 10 | uint8_t input[MAX_LENGH];
|
|---|
| 11 | uint8_t output[MAX_LENGH];
|
|---|
| 12 |
|
|---|
| 13 | uint8_t key[16] = { 0x04, 0x60, 0x62, 0x06, 0x3e, 0x0b, 0x5c, 0x4e, 0x04, 0x06, 0x20, 0x50, 0x4f, 0x34, 0x41, 0x4b };
|
|---|
| 14 |
|
|---|
| 15 | int main(int argc, char *argv[])
|
|---|
| 16 | {
|
|---|
| 17 |
|
|---|
| 18 | int i=0;
|
|---|
| 19 | FILE *fp= NULL;
|
|---|
| 20 | int c=0;
|
|---|
| 21 | int filePos=0;
|
|---|
| 22 | uint32_t fileSize=0;
|
|---|
| 23 | char outputFilename[MAX_PATH] = "";
|
|---|
| 24 | //uint16_t crc=0;
|
|---|
| 25 |
|
|---|
| 26 | //uint16_t hw_id;
|
|---|
| 27 | //uint16_t fw_major;
|
|---|
| 28 | //uint16_t fw_minor;
|
|---|
| 29 | //uint16_t fw_build;
|
|---|
| 30 |
|
|---|
| 31 | printf("Starting file %s.\n", argv[0]);
|
|---|
| 32 |
|
|---|
| 33 | memset(input,0xFF,MAX_LENGH);
|
|---|
| 34 | memset(output,0xFF,MAX_LENGH);
|
|---|
| 35 |
|
|---|
| 36 | if (argc < 2)
|
|---|
| 37 | {
|
|---|
| 38 | printf("Was soll ich den verschluesseln?\nDu hast vergessen einen Dateinahmen anzugeben!\n");
|
|---|
| 39 | printf("Bitte gib es z.B. so ein: FileEncrypt abc.bin\n");
|
|---|
| 40 | printf("LG Falko\n");
|
|---|
| 41 | return 0;
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| 44 | //printf ("Bitte geben Sie die Hardware ID ein: ");
|
|---|
| 45 | //scanf ("%hu", &hw_id);
|
|---|
| 46 |
|
|---|
| 47 | //printf ("Bitte geben Sie die Firmware Version ein, Major: ");
|
|---|
| 48 | //scanf ("%hu", &fw_major);
|
|---|
| 49 |
|
|---|
| 50 | //printf ("Bitte geben Sie die Firmware Version ein, Minor: ");
|
|---|
| 51 | //scanf ("%hu", &fw_minor);
|
|---|
| 52 |
|
|---|
| 53 | //printf ("Bitte geben Sie die Firmware Version ein, Build: ");
|
|---|
| 54 | //scanf ("%hu", &fw_build);
|
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 | // oeffnen im Lesemodus
|
|---|
| 58 | fp = fopen(argv[1], "rb");
|
|---|
| 59 |
|
|---|
| 60 | if(fp == NULL)
|
|---|
| 61 | {
|
|---|
| 62 | printf("Cannot open firmware file %s!\n", argv[1]);
|
|---|
| 63 | fflush(stdout);
|
|---|
| 64 | return -1;
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | printf("Loading file %s\n", argv[1]);
|
|---|
| 68 | fflush(stdout);
|
|---|
| 69 | do
|
|---|
| 70 | {
|
|---|
| 71 | c = fgetc (fp);
|
|---|
| 72 | if (c == EOF) break;
|
|---|
| 73 | input[filePos] = c;
|
|---|
| 74 | //crc= update_crc_16(crc, c );
|
|---|
| 75 | filePos++;
|
|---|
| 76 | if (filePos > (MAX_LENGH-1))
|
|---|
| 77 | {
|
|---|
| 78 | printf("Datei zu gro� f�r aktuellen Buffer\n");fflush(stdout);
|
|---|
| 79 | return -2;
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | } while (1);
|
|---|
| 83 |
|
|---|
| 84 |
|
|---|
| 85 | fclose (fp);
|
|---|
| 86 |
|
|---|
| 87 | fileSize = filePos;
|
|---|
| 88 |
|
|---|
| 89 |
|
|---|
| 90 | // --- ENDE DATEI EINLESEN ----
|
|---|
| 91 |
|
|---|
| 92 |
|
|---|
| 93 | fflush(stdout);
|
|---|
| 94 |
|
|---|
| 95 | // print the resulting cipher as 4 x 16 byte strings
|
|---|
| 96 | printf("Encrypting file...\n");
|
|---|
| 97 | fflush(stdout);
|
|---|
| 98 |
|
|---|
| 99 | for(i = 0; i < (MAX_LENGH / 16); ++i)
|
|---|
| 100 | {
|
|---|
| 101 | AES128_ECB_encrypt(input + (i*16), key, output+(i*16));
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | //Datei schreiben - verschl�sselt
|
|---|
| 105 | //oeffnen im schreibmodus, bin�r
|
|---|
| 106 | //strcat (outputFilename, "encrypted_");
|
|---|
| 107 | strcat(outputFilename, argv[1]);
|
|---|
| 108 | // Let's find last '/' or '\' in path
|
|---|
| 109 | char* fn = NULL;
|
|---|
| 110 | if (!((fn = strrchr(outputFilename, '/')) || (fn = strrchr(outputFilename, '\\'))))
|
|---|
| 111 | {
|
|---|
| 112 | printf("Cannot find filename in %s", argv[1]);
|
|---|
| 113 | return -4;
|
|---|
| 114 | }
|
|---|
| 115 | // Moving pointer to the beginning of the real file name
|
|---|
| 116 | fn++;
|
|---|
| 117 | char filename[MAX_PATH] = "";
|
|---|
| 118 | strcpy(filename, fn);
|
|---|
| 119 | char dir[MAX_PATH] = "";
|
|---|
| 120 | strncat(dir, outputFilename, fn - outputFilename);
|
|---|
| 121 | char new_filename[255] = "";
|
|---|
| 122 | strcpy(new_filename, "encrypted_");
|
|---|
| 123 | strcat(new_filename, filename);
|
|---|
| 124 | strcat(dir, new_filename);
|
|---|
| 125 |
|
|---|
| 126 | printf("Writing file %s\n", dir);
|
|---|
| 127 | fflush(stdout);
|
|---|
| 128 |
|
|---|
| 129 | fp = fopen(dir, "wb");
|
|---|
| 130 |
|
|---|
| 131 | if(fp == NULL)
|
|---|
| 132 | {
|
|---|
| 133 | printf("File %s cannot be written!\n", dir);
|
|---|
| 134 | return -3;
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| 137 | filePos=0;
|
|---|
| 138 | do
|
|---|
| 139 | {
|
|---|
| 140 | c = output[filePos];
|
|---|
| 141 | fputc (c, fp);
|
|---|
| 142 | filePos++;
|
|---|
| 143 | } while (filePos < fileSize);
|
|---|
| 144 | fclose (fp);
|
|---|
| 145 |
|
|---|
| 146 |
|
|---|
| 147 | printf ("Bootloader file generated!\n");
|
|---|
| 148 | printf ("Bin file size:\t\t\t\t %d\n", fileSize);
|
|---|
| 149 | //printf ("HW Id:\t\t\t\t\t %d\n", hw_id );
|
|---|
| 150 | //printf ("FW Version:\t\t\t\t %d.%d.%d\n", fw_major, fw_minor, fw_build );
|
|---|
| 151 | printf ("Used Key for AES 128 Bit encryption:\t ");
|
|---|
| 152 | phex(key, 16);
|
|---|
| 153 | //printf ("Calculated CRC:\t\t\t\t %x\n", crc);
|
|---|
| 154 | printf("Finished!\n");
|
|---|
| 155 | fflush(stdout);
|
|---|
| 156 |
|
|---|
| 157 | return 0;
|
|---|
| 158 | }
|
|---|