@@ -114,7 +114,7 @@ void run_darknet_detector(image im, image im_sized, float thresh,
114
114
if (outfile) {
115
115
printf (" Saving prediction to %s.jpg...\n " , outfile);
116
116
time = what_time_is_it_now ();
117
- save_image (im, outfile);
117
+ save_image (im, outfile);
118
118
printf (" Write duration: %lf seconds\n " ,
119
119
what_time_is_it_now () - time );
120
120
}
@@ -138,7 +138,7 @@ void on_frame_ready(SBufferInfo *bufInfo)
138
138
double time ;
139
139
const char *outfile_prefix = " output/prediction" ;
140
140
char outfile[strlen (outfile_prefix) + 12 ];
141
- outfile[0 ] = ' \0 ' ;
141
+ outfile[0 ] = ' \0 ' ;
142
142
char frame_number_suffix[12 ];
143
143
144
144
printf (" Image %d ===========================\n " , frames_processed);
@@ -171,86 +171,117 @@ int decrypt_video(char *encrypted_video_path, char *decrypted_video_path, char *
171
171
long input_file_size;
172
172
unsigned char key[KEY_LENGTH / 8 ];
173
173
unsigned char iv[BLOCK_SIZE / 8 ];
174
- unsigned char *input_buffer, *output_buffer;
174
+ unsigned char *input_buffer = NULL , *output_buffer = NULL ;
175
175
size_t output_len;
176
+ mbedtls_cipher_context_t ctx;
177
+ mbedtls_cipher_type_t type;
178
+ int ret = 1 , mbedtls_ret = 1 ;
176
179
177
- // Read key
180
+ // Read key
178
181
f = fopen (key_path, " r" );
179
182
if (f == NULL ) {
180
183
printf (" Couldn't open %s\n " , key_path);
181
- return 1 ;
184
+ goto exit ;
182
185
}
183
186
n = fread (key, sizeof (key), 1 , f);
187
+ fclose (f);
184
188
if (n != 1 ) {
185
189
printf (" Invalid key length. Should be %d bits long\n " , KEY_LENGTH);
186
- return 1 ;
190
+ goto exit ;
187
191
}
188
- fclose (f);
189
192
190
- // Read IV
193
+ // Read IV
191
194
f = fopen (iv_path, " r" );
192
195
if (f == NULL ) {
193
196
printf (" Couldn't open %s\n " , iv_path);
194
- return 1 ;
197
+ goto exit ;
195
198
}
196
199
n = fread (iv, sizeof (iv), 1 , f);
200
+ fclose (f);
197
201
if (n != 1 ) {
198
202
printf (" Invalid IV length. Should be %d bits long\n " , BLOCK_SIZE);
199
- return 1 ;
203
+ goto exit ;
200
204
}
201
- fclose (f);
202
205
203
206
// Determine input file size
204
207
f = fopen (encrypted_video_path, " r" );
205
208
if (f == NULL ) {
206
209
printf (" Couldn't open %s\n " , encrypted_video_path);
207
- return 1 ;
210
+ goto exit ;
208
211
}
209
212
fseek (f, 0L , SEEK_END);
210
213
input_file_size = ftell (f);
211
214
rewind (f);
212
215
213
216
// Allocate input buffer the size of the input file
214
217
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
+ }
215
230
216
231
// Read input file
217
232
n = fread (input_buffer, input_file_size, 1 , f);
233
+ fclose (f);
218
234
if (n != 1 ) {
219
235
printf (" Failure reading %s\n " , encrypted_video_path);
220
- return 1 ;
236
+ goto free_buffers ;
221
237
}
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);
227
238
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;
231
241
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
+ }
235
250
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
+ }
237
256
238
257
// Write result to `decrypted_video_path`
239
258
f = fopen (decrypted_video_path, " w" );
240
259
if (f == NULL ) {
241
260
printf (" Couldn't open %s\n " , decrypted_video_path);
242
- return 1 ;
261
+ goto mbedtls_exit ;
243
262
}
244
263
n = fwrite (output_buffer, output_len, 1 , f);
264
+ fclose (f);
245
265
if (n != 1 ) {
246
266
printf (" Failure writing %s\n " , decrypted_video_path);
247
- return 1 ;
267
+ goto mbedtls_exit ;
248
268
}
249
- fclose (f);
250
269
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);
251
281
free (output_buffer);
252
282
253
- return 0 ;
283
+ exit :
284
+ return ret;
254
285
}
255
286
256
287
/* Run the object detection model on each decoded frame */
@@ -261,6 +292,12 @@ int main(int argc, char **argv)
261
292
char *decrypted_video_path = " program_internal/in.h264" ;
262
293
char *key_path = " user_input/key" ;
263
294
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 ;
264
301
265
302
// Decrypt input video
266
303
printf (" Decrypting video...\n " );
@@ -269,13 +306,10 @@ int main(int argc, char **argv)
269
306
return 1 ;
270
307
}
271
308
272
- // Initialize Darknet
309
+ // Initialize Darknet
273
310
printf (" Initializing detector...\n " );
274
311
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);
279
313
printf (" Arguments loaded and network parsed: %lf seconds\n " ,
280
314
what_time_is_it_now () - time );
281
315
@@ -285,6 +319,8 @@ int main(int argc, char **argv)
285
319
int x = h264_decode (decrypted_video_path, " " , false , &on_frame_ready);
286
320
printf (" Finished decoding: %lf seconds\n " ,
287
321
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 " );
288
324
289
325
return x;
290
326
}
0 commit comments