-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathcst_signer.c
More file actions
1729 lines (1505 loc) · 57.5 KB
/
Copy pathcst_signer.c
File metadata and controls
1729 lines (1505 loc) · 57.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2022-2024 NXP
* SPDX-License-Identifier: GPL-2.0-only
*
*/
#include <limits.h>
#include <cst_signer.h>
#include <cfg_parser.h>
#include <mkimage_helper.h>
#include <fdt.h>
#define RSIZE 256
uint32_t g_image_offset = 0;
typedef struct {
int cntr_num;
uint32_t cntr_offset;
uint32_t sig_offset;
} __attribute__((packed)) csf_params_t;
image_block_t g_images[NUM_IMGS];
/*
* @brief Search for pattern in buffer
*
* @param[in] buff : Input buffer to search into
* @param[in] pattern : Input pattern
* @param[in] buff_len : Input buffer length
* @param[in] patt_len : Input pattern length
* @param[in] pos : Position where to start the search from
* @param[in] order : Search order from @pos: ascending or descending
* @param[in] mask : Mask bytes in @pattern
* @param[in] step : Use step bytes to increment from position for the next
* search iteration.
*
* @retval Return offset in buffer for the pattern. If pattern not found,
* return buff_len + 1.
*/
unsigned long search_pattern(const unsigned char *buff, unsigned char *pattern,
size_t buff_len, size_t patt_len, unsigned short order,
unsigned long pos, unsigned char *mask, unsigned long step)
{
unsigned long off;
short found = 0;
char temp[patt_len];
buff += pos;
memset(temp, 0, patt_len);
if (mask) {
for (int j = 0; j < patt_len; j++)
pattern[j] = pattern[j] & mask[j];
}
/*search in ascending order */
if (order == ASCENDING) {
/* no search optimization */
for (off = pos; off < (buff_len - patt_len + 1); off += step) {
if (mask) {/* some values can be masked, e.g. length inside IVT tag */
memcpy(temp, buff, patt_len);
for (int j = 0; j < patt_len; j++) {
temp[j] = temp[j] & mask[j];
}
if (!memcmp(pattern, temp, patt_len)) {
found = 1;
break;
}
} else {
if (!memcmp(pattern, buff, patt_len)) {
found = 1;
break;
}
}
buff += step;
}
} else {/*search in descending order */
for (off = pos; off >= (patt_len - 1) ;off -= step) {
if (mask) {
memcpy(temp, (buff - patt_len + 1), patt_len);
for (int j = 0; j < patt_len; j++) {
temp[j] = temp[j] & mask[j];
}
if (!memcmp(pattern, temp, patt_len)) {
found = 1;
off = off - patt_len + 1;
break;
}
} else {
if (!memcmp(pattern, (buff - patt_len + 1),
patt_len)) {
found = 1;
off = off - patt_len + 1;
break;
}
}
buff -= step;
}
}
if (!found)
off = buff_len + 1;
return off;
}
/*
* @brief Common function to call CST to sign the generated CSF file
*
* @param[in] ifname : Input CSF filename
* @param[out] ofname : Output signed filename
*
* @retval -E_FAILURE : Failure
* E_OK : Success
*/
int sign_csf(char *ifname, char *ofname)
{
ASSERT(ifname, -1);
ASSERT(ofname, -1);
char sys_cmd[SYS_CMD_LEN] = {0};
/* Checking if processor is available */
if (!(system(NULL))) {
fprintf(stderr, "ERROR: Command processor is not available. Exiting.\n");
return -E_FAILURE;
}
#if defined(__linux__)
const char *sz_bin_path="/linux64/bin/cst";
const char *sz_extra_venv="";
char tmp_sz_buf[128];
if ( g_pkcs11 ) { // pkcs#11 - extend with ENV variable and modify to compiled cst
#define PKCS11_MODULE_PATH_VAR "PKCS11_MODULE_PATH"
sprintf( tmp_sz_buf, "%s=%s && ", PKCS11_MODULE_PATH_VAR, getenv(PKCS11_MODULE_PATH_VAR)); // The last space is important!
sz_extra_venv=tmp_sz_buf;
sz_bin_path="/code/build/cst";
}
if (0 > (snprintf(sys_cmd, SYS_CMD_LEN, "%s%s%s ", sz_extra_venv, g_cst_path, sz_bin_path))) {
fprintf(stderr, "ERROR: System command build unsuccessful. Exiting.\n");
return -E_FAILURE;
}
#elif defined(_WIN32) || defined(_WIN64)
if (0 > (snprintf(sys_cmd, SYS_CMD_LEN, "%s/mingw32/bin/cst.exe ", g_cst_path))) {
fprintf(stderr, "ERROR: System command build unsuccessful. Exiting.\n");
return -E_FAILURE;
}
#else
#error Unsupported OS
#endif
const char *sz_cmd_format = "--i %s --o %s";
if ( g_pkcs11 ) {
sz_cmd_format = "-b pkcs11 --i %s --o %s"; // use pkcs11
}
if (0 > (snprintf(sys_cmd + strlen(sys_cmd), (SYS_CMD_LEN - strlen(sys_cmd)), sz_cmd_format, ifname, ofname))) {
fprintf(stderr, "ERROR: System command build unsuccessful. Exiting.\n");
return -E_FAILURE;
}
/* Execute command */
printf("Executing command: %s\n", sys_cmd);
return(system(sys_cmd));
}
/*
* @brief Clone input file to output file
*
* @param[in] ifname : Input file
* @param[out] ofname : Output file
*
* @retval -E_FAILURE : Success
* E_OK : Failure
*/
int copy_files(char *ifname, char *ofname)
{
ASSERT(ifname, -1);
ASSERT(ofname, -1);
long int ifname_size = 0;
unsigned char *buf = NULL;
size_t result_size = 0;
/* Open input file */
FILE *fp_ifname = fopen(ifname, "rb");
if (NULL == fp_ifname) {
fprintf(stderr, "ERROR: Couldn't open file: %s; %s\n", ifname, strerror(errno));
goto err;
}
ifname_size = get_file_size(fp_ifname, ifname);
if (0 > ifname_size) {
fprintf(stderr, "ERROR: Invalid file size %ld of file: %s\n", ifname_size, ifname);
goto err;
}
/* Open output file */
FILE *fp_ofname = fopen(ofname, "wb");
if (NULL == fp_ofname) {
fprintf(stderr, "ERROR: Couldn't open file: %s; %s\n", ofname, strerror(errno));
goto err;
}
/* Allocate memory to the buffer */
buf = malloc(ifname_size);
if (NULL == buf || 0 == buf) {
fprintf(stderr, "ERROR: Error allocating memory; %s\n", strerror(errno));
goto err;
}
/* Copy input file to output file */
result_size = fread(buf, 1, ifname_size, fp_ifname);
if (result_size != ifname_size) {
fprintf(stderr, "ERROR: File read error; %s\n", strerror(errno));
goto err;
} else {
result_size = fwrite(buf, 1, ifname_size, fp_ofname);
if (result_size != ifname_size) {
fprintf(stderr, "ERROR: File write error; %s\n", strerror(errno));
goto err;
}
}
/* Cleanup */
FREE(buf);
FCLOSE(fp_ifname);
FCLOSE(fp_ofname);
return E_OK;
err:
FREE(buf);
FCLOSE(fp_ifname);
FCLOSE(fp_ofname);
return -E_FAILURE;
}
/*
* @brief Create CSF source file for IVT type v1
*
* @param[in] blocks : Data blocks that will be authenticated
* idx : CSF file has a standard naming csf_image%d.txt
* idx represents the index of the CSF file and will
* be appended in the name
* @param[out] ofname : The name of the generated CSF source file
*
* @retval -E_FAILURE : Failure
* E_OK : Success
*/
static int create_csf_file_v1(image_block_t *blocks, int idx, char *ofname)
{
char csf_filename[100UL] = {0};
char rvalue[RSIZE] = {0};
bool fast_auth = false;
if (0 > (snprintf(csf_filename, sizeof(csf_filename), "csf_image%d.txt", idx))) {
fprintf(stderr, "ERROR: Cannot populate CSF file name.\n");
goto err;
}
/* Create CSF file with CSF parameters */
FILE *fp_csf_file = fopen(csf_filename, "w");
if (NULL == fp_csf_file ) {
fprintf(stderr, "ERROR: Couldn't open file: %s; %s\n", csf_filename, strerror(errno));
goto err;
}
/* Open CSF config file */
FILE *fp_cfg = fopen(g_csf_cfgfilename, "r");
if (NULL == fp_cfg) {
fprintf(stderr, "ERROR: Couldn't open file: %s; %s\n", g_csf_cfgfilename, strerror(errno));
goto err;
}
/* Populate CSF file with appropriate parameters */
/* Header */
fprintf(fp_csf_file, "[Header]\n");
cfg_parser(fp_cfg, rvalue, RSIZE, "header_version");
if ('\0' == rvalue[0])
fprintf(fp_csf_file, "\tVersion = 4.3\n");
else
fprintf(fp_csf_file, "\tVersion = %s\n", rvalue);
fprintf(fp_csf_file, "\tHash Algorithm = sha256\n");
cfg_parser(fp_cfg, rvalue, RSIZE, "header_eng");
if ('\0' == rvalue[0])
fprintf(fp_csf_file, "\tEngine = ANY\n");
else
fprintf(fp_csf_file, "\tEngine = %s\n", rvalue);
cfg_parser(fp_cfg, rvalue, RSIZE, "header_eng_config");
if ('\0' == rvalue[0])
fprintf(fp_csf_file, "\tEngine Configuration = 0\n");
else
fprintf(fp_csf_file, "\tEngine Configuration = %s\n", rvalue);
fprintf(fp_csf_file, "\tCertificate Format = X509\n");
fprintf(fp_csf_file, "\tSignature Format = CMS\n");
/* Install SRK */
fprintf(fp_csf_file, "[Install SRK]\n");
cfg_parser(fp_cfg, rvalue, RSIZE, "srktable_file");
if ('\0' == rvalue[0])
fprintf(fp_csf_file, "\tFile = \"%s/crts/SRK_1_2_3_4_table.bin\"\n", g_cst_path);
else
fprintf(fp_csf_file, "\tFile = \"%s/crts/%s\"\n", g_cst_path, rvalue);
cfg_parser(fp_cfg, rvalue, RSIZE, "srk_source_index");
if ('\0' == rvalue[0])
fprintf(fp_csf_file, "\tSource index = 0\n");
else
fprintf(fp_csf_file, "\tSource index = %s\n", rvalue);
/* Choose between fast authentication and normal authentication */
cfg_parser(fp_cfg, rvalue, RSIZE, "nocak_file");
if ('\0' != rvalue[0]) {
/* Prepare fast authentication parameters */
fast_auth = true;
/* Install NOCAK */
fprintf(fp_csf_file, "[Install NOCAK]\n");
fprintf(fp_csf_file, "\tFile = \"%s/crts/%s\"\n", g_cst_path, rvalue);
} else {
/* Prepare normal authentication parameters */
/* Install CSFK */
fprintf(fp_csf_file, "[Install CSFK]\n");
cfg_parser(fp_cfg, rvalue, RSIZE, "csfk_file");
if ('\0' == rvalue[0])
fprintf(fp_csf_file, "\tFile = \"%s/crts/CSF1_1_sha256_2048_65537_v3_usr_crt.pem\"\n", g_cst_path);
else
fprintf(fp_csf_file, "\tFile = \"%s/crts/%s\"\n", g_cst_path, rvalue);
}
fprintf(fp_csf_file, "[Authenticate CSF]\n");
/* Unlock */
#define NUM_ENGINES 4
#define NUM_FEATURES 10
#define MAX_ENGINE_LEN 6 // OCOTP
#define MAX_FEATURE_LEN 13 // FIELD RETURN
char *key = NULL;
int i = 0;
char engines[NUM_ENGINES][MAX_ENGINE_LEN] = { };
char features[NUM_FEATURES][MAX_FEATURE_LEN] = { };
char uid[100] = { };
/* Engines */
cfg_parser(fp_cfg, rvalue, RSIZE, "unlock_engine");
if ('\0' != rvalue[0]) {
for (key = strtok(rvalue, ","); key != NULL; key = strtok(NULL, ",")) {
strncpy((char *)engines[i++], key, MAX_ENGINE_LEN - 1);
}
key = NULL;
i = 0;
/* Features */
cfg_parser(fp_cfg, rvalue, RSIZE, "unlock_features");
if ('\0' != rvalue[0]) {
for (key = strtok(rvalue, ","); key != NULL; key = strtok(NULL, ",")) {
strncpy((char *)features[i++], key, MAX_FEATURE_LEN - 1);
}
}
/* UID */
cfg_parser(fp_cfg, rvalue, RSIZE, "unlock_uid");
if ('\0' != rvalue[0])
strncpy(uid, rvalue, sizeof(uid)/sizeof(uid[0]));
}
#define PRINT_UNLOCK_CMD do { \
fprintf(fp_csf_file, "[Unlock]\n"); \
fprintf(fp_csf_file, "\tEngine = %s\n", engines[i]); \
fprintf(fp_csf_file, "\tFeatures = %s\n", features[j]); \
} while(0)
/* Parse engines and features*/
if (strlen(engines[0]) && strlen(features[0])) {
for (int i = 0; i < NUM_ENGINES; i++) {
if (!strncmp(engines[i], "SRTC", 4)) {
fprintf(fp_csf_file, "[Unlock]\n");
fprintf(fp_csf_file, "\tEngine = %s\n", engines[i]);
continue;
} else if (!strncmp(engines[i], "CAAM", 4)) {
for (int j = 0; j < NUM_FEATURES; j++) {
if (!strncmp(features[j], "MID", 3) || \
!strncmp(features[j], "RNG", 3) || \
!strncmp(features[j], "MFG", 3)) {
PRINT_UNLOCK_CMD;
}
}
} else if (!strncmp(engines[i], "SNVS", 4)) {
for (int j = 0; j < NUM_FEATURES; j++) {
if (!strncmp(features[j], "LP SWR", 6) || \
!strncmp(features[j], "ZMK WRITE", 9)) {
PRINT_UNLOCK_CMD;
}
}
} else if (!strncmp(engines[i], "OCOTP", 5)) {
for (int j = 0; j < NUM_FEATURES; j++) {
if ((!strncmp(features[j], "FIELD RETURN", 12) || \
!strncmp(features[j], "SCS", 3) || \
!strncmp(features[j], "JTAG", 4)) \
&& strlen(uid) > 0) {
PRINT_UNLOCK_CMD;
fprintf(fp_csf_file, "\tUID = %s\n", uid);
} else if (!strncmp(features[j], "SRK REVOKE", 10)) {
PRINT_UNLOCK_CMD;
}
}
}
}
}
/* Choose between fast authentication and normal authentication */
if (!fast_auth) {
/* Install Key */
fprintf(fp_csf_file, "[Install Key]\n");
cfg_parser(fp_cfg, rvalue, RSIZE, "img_verification_index");
if ('\0' == rvalue[0])
fprintf(fp_csf_file, "\tVerification index = 0\n");
else
fprintf(fp_csf_file, "\tVerification index = %s\n", rvalue);
cfg_parser(fp_cfg, rvalue, RSIZE, "img_target_index");
if ('\0' == rvalue[0])
fprintf(fp_csf_file, "\tTarget index = 0\n");
else
fprintf(fp_csf_file, "\tTarget index = %s\n", rvalue);
cfg_parser(fp_cfg, rvalue, RSIZE, "img_file");
if ('\0' == rvalue[0])
fprintf(fp_csf_file, "\tFile = \"%s/crts/IMG1_1_sha256_2048_65537_v3_usr_crt.pem\"\n", g_cst_path);
else
fprintf(fp_csf_file, "\tFile = \"%s/crts/%s\"\n", g_cst_path, rvalue);
}
/* Authenticate Data */
fprintf(fp_csf_file, "[Authenticate Data]\n");
/* Choose between fast authentication and normal authentication */
if (!fast_auth) {
cfg_parser(fp_cfg, rvalue, RSIZE, "auth_verification_index");
if ('\0' == rvalue[0])
fprintf(fp_csf_file, "\tVerification index = 2\n");
else
fprintf(fp_csf_file, "\tVerification index = %s\n", rvalue);
} else {
fprintf(fp_csf_file, "\tVerification index = 0\n");
}
fprintf(fp_csf_file, "\tBlocks = ");
for (int cnt = 0; cnt < NUM_IMGS; cnt++) {
if (!blocks[cnt].valid)
continue;
if (cnt) {
fprintf(fp_csf_file, "\t ");
}
fprintf(fp_csf_file, "0x%08lX 0x%08lX 0x%08lX \"%s\"",
blocks[cnt].load_addr,
blocks[cnt].offset,
blocks[cnt].size, ofname);
if (cnt != (NUM_IMGS - 1) && blocks[cnt + 1].valid)
fprintf(fp_csf_file, ", \\");
fprintf(fp_csf_file, "\n");
}
printf("INFO: %s generated\n", csf_filename);
FCLOSE(fp_csf_file);
FCLOSE(fp_cfg);
return E_OK;
err:
FCLOSE(fp_csf_file);
FCLOSE(fp_cfg);
return -E_FAILURE;
}
/*
* @brief Create CSF file for IVT type v3
*
* @param[in] csf_filename : CSF filename to be populated
* ifname : Input filename
* csf_param : CSF parameters config file
*
* @retval -E_FAILURE : Failure
* E_OK : Success
*/
static int create_csf_file_v3(char *csf_filename, char *ifname, csf_params_t *csf_param)
{
ASSERT(csf_filename, -1);
ASSERT(ifname, -1);
ASSERT(csf_param, -1);
char rvalue[RSIZE] = {0};
/* Create CSF file with CSF parameters */
FILE *fp_csf_file = fopen(csf_filename, "w");
if (NULL == fp_csf_file ) {
fprintf(stderr, "ERROR: Couldn't open file: %s; %s\n", csf_filename, strerror(errno));
goto err;
}
/* Open CSF config file */
FILE *fp_cfg = fopen(g_csf_cfgfilename, "r");
if (NULL == fp_cfg) {
fprintf(stderr, "ERROR: Couldn't open file: %s; %s\n", g_csf_cfgfilename, strerror(errno));
goto err;
}
/* Populate CSF file with appropriate parameters */
/* Header */
fprintf(fp_csf_file, "[Header]\n");
fprintf(fp_csf_file, "\tTarget = AHAB\n");
cfg_parser(fp_cfg, rvalue, RSIZE,"header_version");
if ('\0' == rvalue[0])
fprintf(fp_csf_file, "\tVersion = 1.0\n");
else
fprintf(fp_csf_file, "\tVersion = %s\n", rvalue);
/* Install SRK */
fprintf(fp_csf_file, "[Install SRK]\n");
cfg_parser(fp_cfg, rvalue, RSIZE, "srktable_file");
if ('\0' == rvalue[0])
fprintf(fp_csf_file, "\tFile = \"%s/crts/SRK_1_2_3_4_table.bin\"\n", g_cst_path);
else
fprintf(fp_csf_file, "\tFile = \"%s/crts/%s\"\n", g_cst_path, rvalue);
cfg_parser(fp_cfg, rvalue, RSIZE, "srk_source");
if ('\0' == rvalue[0])
fprintf(fp_csf_file, "\tSource = \"%s/crts/SRK1_sha256_prime256v1_v3_ca_crt.pem\"\n", g_cst_path);
else {
if ( 0 == g_pkcs11 ) {
DEBUG("Source = \"%s/crts/%s\"\n", g_cst_path, rvalue);
fprintf(fp_csf_file, "\tSource = \"%s/crts/%s\"\n", g_cst_path, rvalue);
} else { // use PKCS#11
DEBUG("PKCS11:\n");
DEBUG("Source = %s\n", rvalue);
fprintf(fp_csf_file, "\tSource = \"%s\"\n", rvalue);
}
}
cfg_parser(fp_cfg, rvalue, RSIZE, "srk_source_index");
if ('\0' == rvalue[0])
fprintf(fp_csf_file, "\tSource index = 0\n");
else
fprintf(fp_csf_file, "\tSource index = %s\n", rvalue);
cfg_parser(fp_cfg, rvalue, RSIZE, "srk_source_set");
if ('\0' == rvalue[0])
fprintf(fp_csf_file, "\tSource set = OEM\n");
else
fprintf(fp_csf_file, "\tSource set = %s\n", rvalue);
cfg_parser(fp_cfg, rvalue, RSIZE, "srk_revocations");
if ('\0' == rvalue[0])
fprintf(fp_csf_file, "\tRevocations = 0x0\n");
else
fprintf(fp_csf_file, "\tRevocations = %s\n", rvalue);
/* Install SGK */
/* Add SGK info only if its value is populated in config file */
cfg_parser(fp_cfg, rvalue, RSIZE, "sgk_file");
if ('\0' != rvalue[0]) {
fprintf(fp_csf_file, "[Install Certificate]\n");
fprintf(fp_csf_file, "\tFile = \"%s/crts/%s\"\n", g_cst_path, rvalue);
cfg_parser(fp_cfg, rvalue, RSIZE, "sgk_permissions");
if ('\0' == rvalue[0])
fprintf(fp_csf_file, "\tPermissions = 0x1\n");
else
fprintf(fp_csf_file, "\tPermissions = %s\n", rvalue);
}
/* Authenticate Data */
fprintf(fp_csf_file, "[Authenticate Data]\n");
fprintf(fp_csf_file, "\tFile = \"%s\"\n", ifname);
fprintf(fp_csf_file, "\tOffsets = 0x%08X\t0x%08X\n", csf_param->cntr_offset,
csf_param->sig_offset);
/* DONE */
printf("INFO: %s generated\n", csf_filename);
FCLOSE(fp_csf_file);
FCLOSE(fp_cfg);
return E_OK;
err:
FCLOSE(fp_csf_file);
FCLOSE(fp_cfg);
return -E_FAILURE;
}
/*
* @brief Sign image of IVT type v3
*
* @param[in] infile_buf : Input file buffer
* infile_size : Input file size
* ifname_full : Input filename
* @param[out] ofname : Ouput filename
*
* @retval E_OK : Success
* -E_FAILURE : Failure
*/
static int sign_container(const uint8_t *infile_buf, long int infile_size, char *ifname_full, char *ofname)
{
ASSERT(infile_buf, -1);
ASSERT(ifname_full, -1);
ASSERT(ofname, -1);
int cntr_num = 0; /* number of containers in binary */
uint32_t file_off = 0; /* offset within container binary */
uint32_t img_array_size = 0; /* number of images in container */
flash_header_v3_t container_headers[MAX_NUM_OF_CONTAINER];
flash_header_v3_t app_container_header;
int app_cntr_off;
csf_params_t csf_param[MAX_NUM_OF_CONTAINER] = {0};
/* initialize region of memory where flash header will be stored */
memset((void *)container_headers, 0, sizeof(container_headers));
while (cntr_num < MAX_NUM_OF_CONTAINER && \
((file_off + g_image_offset) < infile_size)) {
/* read in next container header up to the image array */
memcpy((void *)&container_headers[cntr_num], \
(file_off + g_image_offset + infile_buf), \
16);
/* check that the current container has a valid tag */
if (container_headers[cntr_num].tag != IVT_HEADER_TAG_B0 && \
container_headers[cntr_num].tag != IVT_HEADER_TAG_MSG)
break;
/* Validate number of images */
if (container_headers[cntr_num].num_images > MAX_NUM_IMGS) {
fprintf(stderr, "ERROR: This container includes %d images, beyond max %d images\n", container_headers[cntr_num].num_images, MAX_NUM_IMGS);
break;
/* Message container does not have images */
} else if (container_headers[cntr_num].tag == IVT_HEADER_TAG_MSG && \
0 != container_headers[cntr_num].num_images) {
fprintf(stderr, "ERROR: Messages cannot contain images\n");
break;
}
/* compute the size of the image array */
img_array_size = container_headers[cntr_num].num_images * sizeof(boot_img_t);
if (img_array_size) {
/* read in the full image array */
memcpy((void *)&container_headers[cntr_num].img, \
&((flash_header_v3_t *)(file_off + g_image_offset + infile_buf))->img, \
img_array_size);
/* determine the type of image */
boot_img_t img = container_headers[cntr_num].img[0];
img_flags_t img_flags;
img_flags.type = img.hab_flags & IMAGE_TYPE_MASK;
DEBUG("Image Flag type: 0x%X\n", img_flags.type);
/* If container contains signed images provided by NXP like SECO/SENTINEL/V2X, then no need to sign this container. Skip to next one. */
if (img_flags.type == 0x6 || \
img_flags.type == 0xB || \
img_flags.type == 0xC) {
DEBUG("Container %d already signed\n", cntr_num);
/* calculate next container offset in binary */
file_off += ALIGN(container_headers[cntr_num].length, CONTAINER_ALIGNMENT);
DEBUG("file_off = 0x%08X\n", file_off);
/* increment current container count */
cntr_num++;
continue;
}
}
/* Fill CSF parameters */
csf_param[cntr_num].cntr_num = cntr_num + 1;
csf_param[cntr_num].cntr_offset = file_off + g_image_offset;
csf_param[cntr_num].sig_offset = csf_param[cntr_num].cntr_offset + container_headers[cntr_num].sig_blk_offset;
/* calculate next container offset in binary */
file_off += ALIGN(container_headers[cntr_num].length, CONTAINER_ALIGNMENT);
DEBUG("file_off = 0x%08X\n", file_off);
/* increment current container count */
cntr_num++;
}
app_cntr_off = search_app_container(container_headers, cntr_num, &app_container_header, infile_buf, infile_size);
if(0 < app_cntr_off) {
DEBUG("APP container offset = 0x%08X\n", app_cntr_off);
DEBUG("APP container signature offset = 0x%08X\n", app_cntr_off + app_container_header.sig_blk_offset);
/* Fill CSF parameters with APP container parameters */
csf_param[cntr_num].cntr_num = cntr_num + 1;
csf_param[cntr_num].cntr_offset = app_cntr_off;
csf_param[cntr_num].sig_offset = csf_param[cntr_num].cntr_offset + app_container_header.sig_blk_offset;
} else {
/* Reduce the total containers by 1 if no APP container is found */
cntr_num--;
}
for(int i=0; i<MAX_NUM_OF_CONTAINER; i++) {
DEBUG("CSF Container:\t Container Number : %d\n", csf_param[i].cntr_num);
DEBUG("Container Offset : 0x%08X\n", csf_param[i].cntr_offset);
DEBUG("Signature Offset : 0x%08X\n", csf_param[i].sig_offset);
}
/* TODO: Signing logic would be simpler when multiple blocks can be signed in the same CSF file */
char *csf_filename = NULL;
char *itemp_fname = "itemp.bin";
char *otemp_fname = "otemp.bin";
/* Copy file to be signed to a temporary file */
if(copy_files(ifname_full, itemp_fname)) {
fprintf(stderr, "ERROR: Failed to copy files: %s and %s\n", ifname_full, itemp_fname);
return -E_FAILURE;
}
/* Create CSF files & sign them */
for (int i = 0; i <= cntr_num; i++) {
/* Skip creating CSF for NXP signed images */
if (0 == csf_param[i].cntr_offset && 0 == csf_param[i].sig_offset)
continue;
csf_filename = calloc(FILENAME_MAX_LEN, sizeof(char));
if (NULL == csf_filename) {
fprintf(stderr, "ERROR: Error allocating memory; %s\n", strerror(errno));
goto err;
}
/* Prepare CSF filename */
if (0 > (snprintf(csf_filename, FILENAME_MAX_LEN, "container_%d.csf", i + 1))) {
fprintf(stderr, "ERROR: CSF filename build unsuccessful. Exiting.\n");
goto err;
}
DEBUG("CSF filename = %s\n", csf_filename);
/* Create CSF file with input filename */
if (create_csf_file_v3(csf_filename, itemp_fname, &csf_param[i])) {
fprintf(stderr, "ERROR: Failed to create CSF file properly: %s\n", csf_filename);
goto err;
}
DEBUG("CSF filename created = %s\n", csf_filename);
if (i != cntr_num) {
/* Sign into a temp output file */
if (sign_csf(csf_filename, otemp_fname)) {
fprintf(stderr, "ERROR: Failed to sign the image using: %s\n", csf_filename);
goto err;
}
/* Remove input temp file */
if (remove(itemp_fname)) {
fprintf(stderr, "ERROR: Couldn't remove file: %s; %s\n", itemp_fname, strerror(errno));
goto err;
}
/* Rename output temp file to input temp file to be signed again */
if (rename(otemp_fname, itemp_fname)) {
fprintf(stderr, "ERROR: Couldn't rename file: %s to %s; %s\n", otemp_fname, itemp_fname, strerror(errno));
goto err;
}
} else {
/* If last container is being signed then create final signed image */
if (sign_csf(csf_filename, ofname)) {
fprintf(stderr, "ERROR: Failed to sign the image using: %s\n", csf_filename);
goto err;
}
/* Remove input temporary file */
if (remove(itemp_fname)) {
fprintf(stderr, "ERROR: Couldn't remove file: %s; %s\n", itemp_fname, strerror(errno));
goto err;
}
}
FREE(csf_filename);
}
return E_OK;
err:
FREE(csf_filename);
return -E_FAILURE;
}
/*
* @brief This function reads the inputs file and returns size
*
* @param[in] fp - Input file pointer
* input_file - Input file name
*
* @retval Return file size
*/
long int get_file_size(FILE *fp, char *input_file)
{
int ret = -1;
if (NULL == input_file) {
fprintf(stderr, "ERROR: Invalid file: %s\n", input_file);
return -E_FAILURE;
}
/* Open file */
fp = fopen(input_file, "rb");
if (NULL == fp) {
fprintf(stderr, "ERROR: Couldn't open file: %s; %s\n", input_file, strerror(errno));
return -E_FAILURE;
}
/* Seek to the end of file to calculate size */
if (fseek(fp , 0 , SEEK_END)) {
errno = ENOENT;
fprintf(stderr, "ERROR: Couldn't seek to end of file: %s; %s\n", input_file, strerror(errno));
FCLOSE(fp);
return -E_FAILURE;
}
/* Get size and go back to start of the file */
ret = ftell(fp);
rewind(fp);
FCLOSE(fp);
return ret;
}
/*
* @brief This function allocates buffer with size from input file
*
* @param[in] fp - Input file pointer
* input_file - Input file name
*
* @retval return buffer pointer
*/
unsigned char *alloc_buffer(FILE *fp, char *input_file)
{
long int file_size = 0;
unsigned char *buf = NULL;
file_size = get_file_size(fp, input_file);
if (0 > file_size) {
fprintf(stderr, "ERROR: Invalid file size %ld of file: %s\n", file_size, input_file);
return NULL;
}
/* Open file */
fp = fopen(input_file, "rb");
if (NULL == fp) {
fprintf(stderr, "ERROR: Couldn't open file: %s; %s\n", input_file, strerror(errno));
return NULL;
}
/* Allocate memory to the buffer */
buf = malloc(file_size);
if (NULL == buf || 0 == buf) {
fprintf(stderr, "ERROR: Error allocating memory; %s\n", strerror(errno));
FCLOSE(fp);
return NULL;
}
/* Copy the file into the buffer */
size_t result = fread(buf, 1, file_size, fp);
if (result != file_size) {
fprintf(stderr, "ERROR: File read error; %s\n", strerror(errno));
FCLOSE(fp);
FREE(buf);
return NULL;
}
FCLOSE(fp);
return buf;
}
/*
* @brief Insert CSF data from ifile1 into ifile2 starting at offset
*
* @param[in] ifile1 : Input file that contains the Command Sequence File
* offset : Offset of the CSF in ifile2
*
* @param[out] ifile2 : Output file that contains the flash image.
*
* @retval -E_FAILURE : Failure
* E_OK : Success
*/
static int insert_csf(char *ifile1, char *ifile2, uint32_t offset)
{
FILE *fp1 = NULL;
FILE *fp2 = NULL;
long int ifile1_size, result_size;
unsigned char *buf = NULL;
ifile1_size = get_file_size(fp1, ifile1);
if (0 > ifile1_size) {
fprintf(stderr, "ERROR: Invalid file size %ld of file: %s\n", ifile1_size, ifile1);
goto err;
}
/* Allocate memory to the buffer */
buf = malloc(ifile1_size);
if (buf == NULL) {
fprintf(stderr, "ERROR: Error allocating memory; %s\n", strerror(errno));
goto err;
}
fp1 = fopen(ifile1, "rb");
fp2 = fopen(ifile2, "r+b");
if (NULL == fp1 || NULL == fp2) {
fprintf(stderr, "ERROR: Couldn't open one of the files : %s or %s %s\n",
ifile1, ifile2, strerror(errno));
goto err;
}
result_size = fread(buf, 1, ifile1_size, fp1);
/* Copy input file to output file */
if (result_size != ifile1_size) {
fprintf(stderr, "ERROR: File read error; %s\n", strerror(errno));
goto err;
}
if (fseek (fp2, offset, SEEK_SET)) {
fprintf(stderr, "ERROR: Cannot set pointer to %x offset; %s\n",offset, strerror(errno));
goto err;
}
result_size = fwrite(buf, 1, ifile1_size, fp2);
if (result_size != ifile1_size) {
fprintf(stderr, "ERROR: File write error; %s\n", strerror(errno));
goto err;
}
FCLOSE(fp1);
FCLOSE(fp2);
FREE(buf);
return E_OK;
err:
FCLOSE(fp1);
FCLOSE(fp2);
FREE(buf);
return -E_FAILURE;
}
/*
* @brief Concatenate two files and put the result in the first file
*
* @param[in] ifile1 : Input file1
* ifile2 : Input file2
*
* @retval -E_FAILURE : Failure
* E_OK : Success
*/
int concat_files(char *ifname1, char *ifname2)
{
char tmp_file[100UL] = {0};
FILE *fp1 = NULL;
FILE *fp2 = NULL;
FILE *fp3 = NULL;
unsigned char *buf = NULL;
long int ifile1_size, ifile2_size, result_size;
if (0 > (snprintf(tmp_file, sizeof(tmp_file), "temp.bin"))) {
fprintf(stderr, "ERROR: Cannot populate temp file name.\n");
return -E_FAILURE;
}
ifile1_size = get_file_size(fp1, ifname1);
if (0 > ifile1_size) {
fprintf(stderr, "ERROR: Invalid file size %ld of file: %s\n", ifile1_size, ifname1);
goto err;
}
ifile2_size = get_file_size(fp2, ifname2);
if (0 > ifile2_size) {
fprintf(stderr, "ERROR: Invalid file size %ld of file: %s\n", ifile2_size, ifname2);