@@ -114,7 +114,7 @@ void run_darknet_detector(image im, image im_sized, float thresh,
114114 if (outfile) {
115115 printf (" Saving prediction to %s.jpg...\n " , outfile);
116116 time = what_time_is_it_now ();
117- save_image (im, outfile);
117+ save_image (im, outfile);
118118 printf (" Write duration: %lf seconds\n " ,
119119 what_time_is_it_now () - time);
120120 }
@@ -138,7 +138,7 @@ void on_frame_ready(SBufferInfo *bufInfo)
138138 double time;
139139 const char *outfile_prefix = " output/prediction" ;
140140 char outfile[strlen (outfile_prefix) + 12 ];
141- outfile[0 ] = ' \0 ' ;
141+ outfile[0 ] = ' \0 ' ;
142142 char frame_number_suffix[12 ];
143143
144144 printf (" Image %d ===========================\n " , frames_processed);
@@ -171,86 +171,117 @@ int decrypt_video(char *encrypted_video_path, char *decrypted_video_path, char *
171171 long input_file_size;
172172 unsigned char key[KEY_LENGTH / 8 ];
173173 unsigned char iv[BLOCK_SIZE / 8 ];
174- unsigned char *input_buffer, *output_buffer;
174+ unsigned char *input_buffer = NULL , *output_buffer = NULL ;
175175 size_t output_len;
176+ mbedtls_cipher_context_t ctx;
177+ mbedtls_cipher_type_t type;
178+ int ret = 1 , mbedtls_ret = 1 ;
176179
177- // Read key
180+ // Read key
178181 f = fopen (key_path, " r" );
179182 if (f == NULL ) {
180183 printf (" Couldn't open %s\n " , key_path);
181- return 1 ;
184+ goto exit ;
182185 }
183186 n = fread (key, sizeof (key), 1 , f);
187+ fclose (f);
184188 if (n != 1 ) {
185189 printf (" Invalid key length. Should be %d bits long\n " , KEY_LENGTH);
186- return 1 ;
190+ goto exit ;
187191 }
188- fclose (f);
189192
190- // Read IV
193+ // Read IV
191194 f = fopen (iv_path, " r" );
192195 if (f == NULL ) {
193196 printf (" Couldn't open %s\n " , iv_path);
194- return 1 ;
197+ goto exit ;
195198 }
196199 n = fread (iv, sizeof (iv), 1 , f);
200+ fclose (f);
197201 if (n != 1 ) {
198202 printf (" Invalid IV length. Should be %d bits long\n " , BLOCK_SIZE);
199- return 1 ;
203+ goto exit ;
200204 }
201- fclose (f);
202205
203206 // Determine input file size
204207 f = fopen (encrypted_video_path, " r" );
205208 if (f == NULL ) {
206209 printf (" Couldn't open %s\n " , encrypted_video_path);
207- return 1 ;
210+ goto exit ;
208211 }
209212 fseek (f, 0L , SEEK_END);
210213 input_file_size = ftell (f);
211214 rewind (f);
212215
213216 // Allocate input buffer the size of the input file
214217 input_buffer = (unsigned char *) malloc (input_file_size);
218+ if (!input_buffer) {
219+ printf (" Couldn't allocate input buffer\n " );
220+ goto free_buffers;
221+ }
222+
223+ // Allocate output buffer the size of the input buffer (can't be longer than
224+ // that due to padding)
225+ output_buffer = (unsigned char *) malloc (input_file_size);
226+ if (!output_buffer) {
227+ printf (" Couldn't allocate output buffer\n " );
228+ goto free_buffers;
229+ }
215230
216231 // Read input file
217232 n = fread (input_buffer, input_file_size, 1 , f);
233+ fclose (f);
218234 if (n != 1 ) {
219235 printf (" Failure reading %s\n " , encrypted_video_path);
220- return 1 ;
236+ goto free_buffers ;
221237 }
222- fclose (f);
223-
224- // Allocate output buffer the size of the input buffer (can't be longer than
225- // that due to padding)
226- output_buffer = (unsigned char *) malloc (input_file_size);
227238
228- // Initialize decryption context and decrypt buffer
229- mbedtls_cipher_context_t ctx;
230- mbedtls_cipher_type_t type = MBEDTLS_CIPHER_AES_128_CTR;
239+ // Initialize decryption context
240+ type = MBEDTLS_CIPHER_AES_128_CTR;
231241 mbedtls_cipher_init (&ctx);
232- mbedtls_cipher_setup (&ctx, mbedtls_cipher_info_from_type (type));
233- mbedtls_cipher_setkey (&ctx, key, KEY_LENGTH, MBEDTLS_DECRYPT);
234- mbedtls_cipher_crypt (&ctx, iv, BLOCK_SIZE / 8 , input_buffer, input_file_size, output_buffer, &output_len);
242+ if ((mbedtls_ret = mbedtls_cipher_setup (&ctx, mbedtls_cipher_info_from_type (type))) != 0 ) {
243+ printf (" mbedtls_cipher_setup failed: %d\n " , mbedtls_ret);
244+ goto mbedtls_exit;
245+ }
246+ if ((mbedtls_ret = mbedtls_cipher_setkey (&ctx, key, KEY_LENGTH, MBEDTLS_DECRYPT)) != 0 ) {
247+ printf (" mbedtls_cipher_setkey failed: %d\n " , mbedtls_ret);
248+ goto mbedtls_exit;
249+ }
235250
236- free (input_buffer);
251+ // Decrypt buffer
252+ if ((mbedtls_ret = mbedtls_cipher_crypt (&ctx, iv, BLOCK_SIZE / 8 , input_buffer, input_file_size, output_buffer, &output_len)) != 0 ) {
253+ printf (" mbedtls_cipher_crypt failed: %d\n " , mbedtls_ret);
254+ goto mbedtls_exit;
255+ }
237256
238257 // Write result to `decrypted_video_path`
239258 f = fopen (decrypted_video_path, " w" );
240259 if (f == NULL ) {
241260 printf (" Couldn't open %s\n " , decrypted_video_path);
242- return 1 ;
261+ goto mbedtls_exit ;
243262 }
244263 n = fwrite (output_buffer, output_len, 1 , f);
264+ fclose (f);
245265 if (n != 1 ) {
246266 printf (" Failure writing %s\n " , decrypted_video_path);
247- return 1 ;
267+ goto mbedtls_exit ;
248268 }
249- fclose (f);
250269
270+ ret = 0 ;
271+
272+ mbedtls_exit:
273+ mbedtls_cipher_free (&ctx);
274+ mbedtls_platform_zeroize (input_buffer, input_file_size);
275+ mbedtls_platform_zeroize (output_buffer, input_file_size);
276+ mbedtls_platform_zeroize (key, sizeof (key));
277+ mbedtls_platform_zeroize (iv, sizeof (iv));
278+
279+ free_buffers:
280+ free (input_buffer);
251281 free (output_buffer);
252282
253- return 0 ;
283+ exit:
284+ return ret;
254285}
255286
256287/* Run the object detection model on each decoded frame */
@@ -261,6 +292,12 @@ int main(int argc, char **argv)
261292 char *decrypted_video_path = " program_internal/in.h264" ;
262293 char *key_path = " user_input/key" ;
263294 char *iv_path = " user_input/iv" ;
295+ char *name_list_file = " program_data/coco.names" ;
296+ char *cfgfile = " program_data/yolov3.cfg" ;
297+ char *weightfile = " program_data/yolov3.weights" ;
298+ // XXX: Box annotation is temporarily disabled until we find a way to
299+ // efficiently provision a batch of files to the enclave (file archive?)
300+ bool annotate_boxes = false ;
264301
265302 // Decrypt input video
266303 printf (" Decrypting video...\n " );
@@ -269,13 +306,10 @@ int main(int argc, char **argv)
269306 return 1 ;
270307 }
271308
272- // Initialize Darknet
309+ // Initialize Darknet
273310 printf (" Initializing detector...\n " );
274311 time = what_time_is_it_now ();
275- // XXX: Box annotation is temporarily disabled until we find a way to
276- // efficiently provision a batch of files to the enclave (file archive?)
277- init_darknet_detector (" program_data/coco.names" , " program_data/yolov3.cfg" ,
278- " program_data/yolov3.weights" , false );
312+ init_darknet_detector (name_list_file, cfgfile, weightfile, annotate_boxes);
279313 printf (" Arguments loaded and network parsed: %lf seconds\n " ,
280314 what_time_is_it_now () - time);
281315
@@ -285,6 +319,8 @@ int main(int argc, char **argv)
285319 int x = h264_decode (decrypted_video_path, " " , false , &on_frame_ready);
286320 printf (" Finished decoding: %lf seconds\n " ,
287321 what_time_is_it_now () - time);
322+ if (frames_processed == 0 )
323+ printf (" No frames were processed. The input video was whether empty or not an H.264 video\n " );
288324
289325 return x;
290326}
0 commit comments