@@ -15,39 +15,85 @@ static void derive_key(uint8_t *key_out) {
1515 }
1616}
1717
18- static void encrypt_file (const char * input , const char * output ) {
19- FILE * fin = fopen (input , "rb" );
18+ static void encrypt_file (const char * input , const char * output ) {
19+ // Validate input parameters
20+ if (!input || !output ) return ;
21+
22+ // Open input file
23+ FILE * fin = fopen (input , "rb" );
2024 if (!fin ) return ;
21- fseek (fin , 0 , SEEK_END );
25+
26+ // Get file size with error checking
27+ if (fseek (fin , 0 , SEEK_END )) {
28+ fclose (fin );
29+ return ;
30+ }
31+
2232 long len = ftell (fin );
2333 rewind (fin );
2434
25- uint8_t * buffer = malloc (len );
26- fread (buffer , 1 , len , fin );
35+ // Validate file length (must be positive)
36+ if (len <= 0 ) {
37+ fclose (fin );
38+ return ;
39+ }
40+
41+ // Allocate buffer for file content
42+ uint8_t * buffer = malloc ((size_t )len );
43+ if (!buffer ) {
44+ fclose (fin );
45+ return ;
46+ }
47+
48+ // Read entire file with complete verification
49+ size_t bytes_read = fread (buffer , 1 , (size_t )len , fin );
2750 fclose (fin );
51+ if (bytes_read != (size_t )len ) {
52+ free (buffer );
53+ return ;
54+ }
2855
29- // Calculate padding PKCS#7
56+ // Calculate PKCS#7 padding
3057 int padding = 16 - (len % 16 );
31- if (padding == 0 ) padding = 16 ; // Add complete padding always
58+ if (padding == 0 ) padding = 16 ; // Always add full block when aligned
59+
60+ // Check for potential integer overflow
61+ if (len > LONG_MAX - padding ) {
62+ free (buffer );
63+ return ;
64+ }
3265 long padded_len = len + padding ;
3366
34- uint8_t * buffer_padded = malloc (padded_len );
35- memcpy (buffer_padded , buffer , len );
36- memset (buffer_padded + len , padding , padding ); // Filled with value 'padding'
67+ // Allocate and prepare padded buffer
68+ uint8_t * buffer_padded = malloc ((size_t )padded_len );
69+ if (!buffer_padded ) {
70+ free (buffer );
71+ return ;
72+ }
73+
74+ // Copy original data and apply padding
75+ memcpy (buffer_padded , buffer , (size_t )len );
76+ memset (buffer_padded + len , (uint8_t )padding , (size_t )padding );
3777
78+ // Initialize AES context
3879 struct AES_ctx ctx ;
3980 uint8_t key [32 ];
4081 derive_key (key );
4182 AES_init_ctx (& ctx , key );
4283
84+ // Encrypt each 16-byte block
4385 for (long i = 0 ; i < padded_len ; i += 16 ) {
4486 AES_ECB_encrypt (& ctx , buffer_padded + i );
4587 }
4688
47- FILE * fout = fopen (output , "wb" );
48- fwrite (buffer_padded , 1 , padded_len , fout );
49- fclose (fout );
89+ // Write encrypted data to output file
90+ FILE * fout = fopen (output , "wb" );
91+ if (fout ) {
92+ fwrite (buffer_padded , 1 , (size_t )padded_len , fout );
93+ fclose (fout );
94+ }
5095
96+ // Clean up allocated memory
5197 free (buffer );
5298 free (buffer_padded );
5399}
@@ -61,7 +107,7 @@ int main(void) {
61107 const char * inputFile = "game.blend" ;
62108 const char * outputFile = "game_encrypted.block" ;
63109
64- Rectangle button = { screenWidth / 2 - 100 , screenHeight / 2 - 25 , 200 , 50 };
110+ Rectangle button = { ( float )( screenWidth / 2 - 100 ), ( float )( screenHeight / 2 - 25 ) , 200.0f , 50.0f };
65111
66112 SetTargetFPS (60 );
67113 while (!WindowShouldClose ()) {
@@ -77,7 +123,7 @@ int main(void) {
77123 } else {
78124 DrawRectangleRec (button , GRAY );
79125 }
80- DrawText ("Encrypt" , button .x + 60 , button .y + 15 , 20 , BLACK );
126+ DrawText ("Encrypt" , ( int ) button .x + 60 , ( int ) button .y + 15 , 20 , BLACK );
81127
82128 EndDrawing ();
83129 }
0 commit comments