summaryrefslogtreecommitdiff
path: root/utils/imxtools/sbtools/sb.c
diff options
context:
space:
mode:
Diffstat (limited to 'utils/imxtools/sbtools/sb.c')
-rw-r--r--utils/imxtools/sbtools/sb.c68
1 files changed, 34 insertions, 34 deletions
diff --git a/utils/imxtools/sbtools/sb.c b/utils/imxtools/sbtools/sb.c
index cbeacf9c3f..04acc2a6ad 100644
--- a/utils/imxtools/sbtools/sb.c
+++ b/utils/imxtools/sbtools/sb.c
@@ -223,7 +223,7 @@ static void produce_sb_section_header(struct sb_section_t *sec,
223static uint8_t instruction_checksum(struct sb_instruction_header_t *hdr) 223static uint8_t instruction_checksum(struct sb_instruction_header_t *hdr)
224{ 224{
225 uint8_t sum = 90; 225 uint8_t sum = 90;
226 byte *ptr = (byte *)hdr; 226 uint8_t *ptr = (uint8_t *)hdr;
227 for(int i = 1; i < 16; i++) 227 for(int i = 1; i < 16; i++)
228 sum += ptr[i]; 228 sum += ptr[i];
229 return sum; 229 return sum;
@@ -287,8 +287,8 @@ enum sb_error_t sb_write_file(struct sb_file_t *sb, const char *filename, void *
287 #define printf(c, ...) cprintf(u, false, c, __VA_ARGS__) 287 #define printf(c, ...) cprintf(u, false, c, __VA_ARGS__)
288 struct crypto_key_t real_key; 288 struct crypto_key_t real_key;
289 real_key.method = CRYPTO_KEY; 289 real_key.method = CRYPTO_KEY;
290 byte crypto_iv[16]; 290 uint8_t crypto_iv[16];
291 byte (*cbc_macs)[16] = xmalloc(16 * g_nr_keys); 291 uint8_t (*cbc_macs)[16] = xmalloc(16 * g_nr_keys);
292 /* init CBC-MACs */ 292 /* init CBC-MACs */
293 for(int i = 0; i < g_nr_keys; i++) 293 for(int i = 0; i < g_nr_keys; i++)
294 memset(cbc_macs[i], 0, 16); 294 memset(cbc_macs[i], 0, 16);
@@ -319,8 +319,8 @@ enum sb_error_t sb_write_file(struct sb_file_t *sb, const char *filename, void *
319 struct sb_header_t sb_hdr; 319 struct sb_header_t sb_hdr;
320 produce_sb_header(sb, &sb_hdr); 320 produce_sb_header(sb, &sb_hdr);
321 /* allocate image */ 321 /* allocate image */
322 byte *buf = xmalloc(sb_hdr.image_size * BLOCK_SIZE); 322 uint8_t *buf = xmalloc(sb_hdr.image_size * BLOCK_SIZE);
323 byte *buf_p = buf; 323 uint8_t *buf_p = buf;
324 #define write(p, sz) do { memcpy(buf_p, p, sz); buf_p += sz; } while(0) 324 #define write(p, sz) do { memcpy(buf_p, p, sz); buf_p += sz; } while(0)
325 #define check_crypto(expr) \ 325 #define check_crypto(expr) \
326 do { int err = expr; \ 326 do { int err = expr; \
@@ -329,7 +329,7 @@ enum sb_error_t sb_write_file(struct sb_file_t *sb, const char *filename, void *
329 cprintf(u, true, GREY, "Crypto error: %d\n", err); \ 329 cprintf(u, true, GREY, "Crypto error: %d\n", err); \
330 return SB_CRYPTO_ERROR; } } while(0) 330 return SB_CRYPTO_ERROR; } } while(0)
331 331
332 sha_1_update(&file_sha1, (byte *)&sb_hdr, sizeof(sb_hdr)); 332 sha_1_update(&file_sha1, (uint8_t *)&sb_hdr, sizeof(sb_hdr));
333 write(&sb_hdr, sizeof(sb_hdr)); 333 write(&sb_hdr, sizeof(sb_hdr));
334 334
335 memcpy(crypto_iv, &sb_hdr, 16); 335 memcpy(crypto_iv, &sb_hdr, 16);
@@ -338,7 +338,7 @@ enum sb_error_t sb_write_file(struct sb_file_t *sb, const char *filename, void *
338 for(int i = 0; i < g_nr_keys; i++) 338 for(int i = 0; i < g_nr_keys; i++)
339 { 339 {
340 check_crypto(crypto_setup(&g_key_array[i])); 340 check_crypto(crypto_setup(&g_key_array[i]));
341 check_crypto(crypto_apply((byte *)&sb_hdr, NULL, sizeof(sb_hdr) / BLOCK_SIZE, 341 check_crypto(crypto_apply((uint8_t *)&sb_hdr, NULL, sizeof(sb_hdr) / BLOCK_SIZE,
342 cbc_macs[i], &cbc_macs[i], true)); 342 cbc_macs[i], &cbc_macs[i], true));
343 } 343 }
344 344
@@ -347,13 +347,13 @@ enum sb_error_t sb_write_file(struct sb_file_t *sb, const char *filename, void *
347 { 347 {
348 struct sb_section_header_t sb_sec_hdr; 348 struct sb_section_header_t sb_sec_hdr;
349 produce_sb_section_header(&sb->sections[i], &sb_sec_hdr); 349 produce_sb_section_header(&sb->sections[i], &sb_sec_hdr);
350 sha_1_update(&file_sha1, (byte *)&sb_sec_hdr, sizeof(sb_sec_hdr)); 350 sha_1_update(&file_sha1, (uint8_t *)&sb_sec_hdr, sizeof(sb_sec_hdr));
351 write(&sb_sec_hdr, sizeof(sb_sec_hdr)); 351 write(&sb_sec_hdr, sizeof(sb_sec_hdr));
352 /* update CBC-MACs */ 352 /* update CBC-MACs */
353 for(int j = 0; j < g_nr_keys; j++) 353 for(int j = 0; j < g_nr_keys; j++)
354 { 354 {
355 check_crypto(crypto_setup(&g_key_array[j])); 355 check_crypto(crypto_setup(&g_key_array[j]));
356 check_crypto(crypto_apply((byte *)&sb_sec_hdr, NULL, 356 check_crypto(crypto_apply((uint8_t *)&sb_sec_hdr, NULL,
357 sizeof(sb_sec_hdr) / BLOCK_SIZE, cbc_macs[j], &cbc_macs[j], true)); 357 sizeof(sb_sec_hdr) / BLOCK_SIZE, cbc_macs[j], &cbc_macs[j], true));
358 } 358 }
359 } 359 }
@@ -365,7 +365,7 @@ enum sb_error_t sb_write_file(struct sb_file_t *sb, const char *filename, void *
365 check_crypto(crypto_setup(&g_key_array[i])); 365 check_crypto(crypto_setup(&g_key_array[i]));
366 check_crypto(crypto_apply(real_key.u.key, entry.key, 1, crypto_iv, NULL, true)); 366 check_crypto(crypto_apply(real_key.u.key, entry.key, 1, crypto_iv, NULL, true));
367 write(&entry, sizeof(entry)); 367 write(&entry, sizeof(entry));
368 sha_1_update(&file_sha1, (byte *)&entry, sizeof(entry)); 368 sha_1_update(&file_sha1, (uint8_t *)&entry, sizeof(entry));
369 } 369 }
370 370
371 /* HACK HACK HACK HACK HACK HACK HACK HACK HACK HACK HACK HACK HACK HACK */ 371 /* HACK HACK HACK HACK HACK HACK HACK HACK HACK HACK HACK HACK HACK HACK */
@@ -391,7 +391,7 @@ enum sb_error_t sb_write_file(struct sb_file_t *sb, const char *filename, void *
391 unsigned init_gap = (sb->sections[0].file_offset - 1) * BLOCK_SIZE - (buf_p - buf); 391 unsigned init_gap = (sb->sections[0].file_offset - 1) * BLOCK_SIZE - (buf_p - buf);
392 if(init_gap > 0) 392 if(init_gap > 0)
393 { 393 {
394 byte *data = xmalloc(init_gap); 394 uint8_t *data = xmalloc(init_gap);
395 generate_random_data(data, init_gap); 395 generate_random_data(data, init_gap);
396 sha_1_update(&file_sha1, data, init_gap); 396 sha_1_update(&file_sha1, data, init_gap);
397 write(data, init_gap); 397 write(data, init_gap);
@@ -407,13 +407,13 @@ enum sb_error_t sb_write_file(struct sb_file_t *sb, const char *filename, void *
407 produce_section_tag_cmd(&sb->sections[i], &tag_cmd, (i + 1) == sb_hdr.nr_sections); 407 produce_section_tag_cmd(&sb->sections[i], &tag_cmd, (i + 1) == sb_hdr.nr_sections);
408 if(g_nr_keys > 0) 408 if(g_nr_keys > 0)
409 { 409 {
410 check_crypto(crypto_apply((byte *)&tag_cmd, (byte *)&tag_cmd, 410 check_crypto(crypto_apply((uint8_t *)&tag_cmd, (uint8_t *)&tag_cmd,
411 sizeof(tag_cmd) / BLOCK_SIZE, crypto_iv, NULL, true)); 411 sizeof(tag_cmd) / BLOCK_SIZE, crypto_iv, NULL, true));
412 } 412 }
413 sha_1_update(&file_sha1, (byte *)&tag_cmd, sizeof(tag_cmd)); 413 sha_1_update(&file_sha1, (uint8_t *)&tag_cmd, sizeof(tag_cmd));
414 write(&tag_cmd, sizeof(tag_cmd)); 414 write(&tag_cmd, sizeof(tag_cmd));
415 /* produce other commands */ 415 /* produce other commands */
416 byte cur_cbc_mac[16]; 416 uint8_t cur_cbc_mac[16];
417 memcpy(cur_cbc_mac, crypto_iv, 16); 417 memcpy(cur_cbc_mac, crypto_iv, 16);
418 for(int j = 0; j < sb->sections[i].nr_insts; j++) 418 for(int j = 0; j < sb->sections[i].nr_insts; j++)
419 { 419 {
@@ -425,17 +425,17 @@ enum sb_error_t sb_write_file(struct sb_file_t *sb, const char *filename, void *
425 produce_sb_instruction(inst, &cmd, u, cprintf); 425 produce_sb_instruction(inst, &cmd, u, cprintf);
426 if(g_nr_keys > 0 && !sb->sections[i].is_cleartext) 426 if(g_nr_keys > 0 && !sb->sections[i].is_cleartext)
427 { 427 {
428 check_crypto(crypto_apply((byte *)&cmd, (byte *)&cmd, 428 check_crypto(crypto_apply((uint8_t *)&cmd, (uint8_t *)&cmd,
429 sizeof(cmd) / BLOCK_SIZE, cur_cbc_mac, &cur_cbc_mac, true)); 429 sizeof(cmd) / BLOCK_SIZE, cur_cbc_mac, &cur_cbc_mac, true));
430 } 430 }
431 sha_1_update(&file_sha1, (byte *)&cmd, sizeof(cmd)); 431 sha_1_update(&file_sha1, (uint8_t *)&cmd, sizeof(cmd));
432 write(&cmd, sizeof(cmd)); 432 write(&cmd, sizeof(cmd));
433 } 433 }
434 /* data */ 434 /* data */
435 if(inst->inst == SB_INST_LOAD || inst->inst == SB_INST_DATA) 435 if(inst->inst == SB_INST_LOAD || inst->inst == SB_INST_DATA)
436 { 436 {
437 uint32_t sz = inst->size + inst->padding_size; 437 uint32_t sz = inst->size + inst->padding_size;
438 byte *data = xmalloc(sz); 438 uint8_t *data = xmalloc(sz);
439 memcpy(data, inst->data, inst->size); 439 memcpy(data, inst->data, inst->size);
440 memcpy(data + inst->size, inst->padding, inst->padding_size); 440 memcpy(data + inst->size, inst->padding, inst->padding_size);
441 if(g_nr_keys > 0 && !sb->sections[i].is_cleartext) 441 if(g_nr_keys > 0 && !sb->sections[i].is_cleartext)
@@ -452,7 +452,7 @@ enum sb_error_t sb_write_file(struct sb_file_t *sb, const char *filename, void *
452 uint32_t pad_size = sb->sections[i].pad_size; 452 uint32_t pad_size = sb->sections[i].pad_size;
453 if(sb->sections[i].is_data) 453 if(sb->sections[i].is_data)
454 { 454 {
455 byte *data = xmalloc(pad_size * BLOCK_SIZE); 455 uint8_t *data = xmalloc(pad_size * BLOCK_SIZE);
456 generate_random_data(data, pad_size * BLOCK_SIZE); 456 generate_random_data(data, pad_size * BLOCK_SIZE);
457 sha_1_update(&file_sha1, data, pad_size * BLOCK_SIZE); 457 sha_1_update(&file_sha1, data, pad_size * BLOCK_SIZE);
458 write(data, pad_size * BLOCK_SIZE); 458 write(data, pad_size * BLOCK_SIZE);
@@ -468,16 +468,16 @@ enum sb_error_t sb_write_file(struct sb_file_t *sb, const char *filename, void *
468 cmd.hdr.checksum = instruction_checksum(&cmd.hdr); 468 cmd.hdr.checksum = instruction_checksum(&cmd.hdr);
469 if(g_nr_keys > 0 && !sb->sections[i].is_cleartext) 469 if(g_nr_keys > 0 && !sb->sections[i].is_cleartext)
470 { 470 {
471 check_crypto(crypto_apply((byte *)&cmd, (byte *)&cmd, 471 check_crypto(crypto_apply((uint8_t *)&cmd, (uint8_t *)&cmd,
472 sizeof(cmd) / BLOCK_SIZE, cur_cbc_mac, &cur_cbc_mac, true)); 472 sizeof(cmd) / BLOCK_SIZE, cur_cbc_mac, &cur_cbc_mac, true));
473 } 473 }
474 sha_1_update(&file_sha1, (byte *)&cmd, sizeof(cmd)); 474 sha_1_update(&file_sha1, (uint8_t *)&cmd, sizeof(cmd));
475 write(&cmd, sizeof(cmd)); 475 write(&cmd, sizeof(cmd));
476 } 476 }
477 } 477 }
478 } 478 }
479 /* write file SHA-1 */ 479 /* write file SHA-1 */
480 byte final_sig[32]; 480 uint8_t final_sig[32];
481 sha_1_finish(&file_sha1); 481 sha_1_finish(&file_sha1);
482 sha_1_output(&file_sha1, final_sig); 482 sha_1_output(&file_sha1, final_sig);
483 generate_random_data(final_sig + 20, 12); 483 generate_random_data(final_sig + 20, 12);
@@ -513,7 +513,7 @@ enum sb_error_t sb_write_file(struct sb_file_t *sb, const char *filename, void *
513 #undef printf 513 #undef printf
514} 514}
515 515
516static struct sb_section_t *read_section(bool data_sec, uint32_t id, byte *buf, 516static struct sb_section_t *read_section(bool data_sec, uint32_t id, uint8_t *buf,
517 int size, const char *indent, void *u, generic_printf_t cprintf, enum sb_error_t *err) 517 int size, const char *indent, void *u, generic_printf_t cprintf, enum sb_error_t *err)
518{ 518{
519 #define printf(c, ...) cprintf(u, false, c, __VA_ARGS__) 519 #define printf(c, ...) cprintf(u, false, c, __VA_ARGS__)
@@ -758,7 +758,7 @@ struct sb_file_t *sb_read_memory(void *_buf, size_t filesize, unsigned flags, vo
758 fatal(SB_CRYPTO_ERROR, "Crypto error: %d\n", err); } while(0) 758 fatal(SB_CRYPTO_ERROR, "Crypto error: %d\n", err); } while(0)
759 759
760 struct sha_1_params_t sha_1_params; 760 struct sha_1_params_t sha_1_params;
761 byte (*cbcmacs)[16] = xmalloc(16 * g_nr_keys); 761 uint8_t (*cbcmacs)[16] = xmalloc(16 * g_nr_keys);
762 sb_file = xmalloc(sizeof(struct sb_file_t)); 762 sb_file = xmalloc(sizeof(struct sb_file_t));
763 memset(sb_file, 0, sizeof(struct sb_file_t)); 763 memset(sb_file, 0, sizeof(struct sb_file_t));
764 struct sb_header_t *sb_header = (struct sb_header_t *)buf; 764 struct sb_header_t *sb_header = (struct sb_header_t *)buf;
@@ -790,10 +790,10 @@ struct sb_file_t *sb_read_memory(void *_buf, size_t filesize, unsigned flags, vo
790 printf(GREEN, " SB version: "); 790 printf(GREEN, " SB version: ");
791 printf(YELLOW, "%d.%d\n", sb_header->major_ver, sb_header->minor_ver); 791 printf(YELLOW, "%d.%d\n", sb_header->major_ver, sb_header->minor_ver);
792 printf(GREEN, " Header SHA-1: "); 792 printf(GREEN, " Header SHA-1: ");
793 byte *hdr_sha1 = sb_header->sha1_header; 793 uint8_t *hdr_sha1 = sb_header->sha1_header;
794 print_hex(YELLOW, hdr_sha1, 20, false); 794 print_hex(YELLOW, hdr_sha1, 20, false);
795 /* Check SHA1 sum */ 795 /* Check SHA1 sum */
796 byte computed_sha1[20]; 796 uint8_t computed_sha1[20];
797 sha_1_init(&sha_1_params); 797 sha_1_init(&sha_1_params);
798 sha_1_update(&sha_1_params, &sb_header->signature[0], 798 sha_1_update(&sha_1_params, &sb_header->signature[0],
799 sizeof(struct sb_header_t) - sizeof(sb_header->sha1_header)); 799 sizeof(struct sb_header_t) - sizeof(sb_header->sha1_header));
@@ -876,7 +876,7 @@ struct sb_file_t *sb_read_memory(void *_buf, size_t filesize, unsigned flags, vo
876 print_key(&printer, sb_printer, &g_key_array[i], true); 876 print_key(&printer, sb_printer, &g_key_array[i], true);
877 printf(GREEN, " CBC-MAC: "); 877 printf(GREEN, " CBC-MAC: ");
878 /* check it */ 878 /* check it */
879 byte zero[16]; 879 uint8_t zero[16];
880 memset(zero, 0, 16); 880 memset(zero, 0, 16);
881 check_crypto(crypto_setup(&g_key_array[i])); 881 check_crypto(crypto_setup(&g_key_array[i]));
882 check_crypto(crypto_apply(buf, NULL, sb_header->header_size + 882 check_crypto(crypto_apply(buf, NULL, sb_header->header_size +
@@ -906,8 +906,8 @@ struct sb_file_t *sb_read_memory(void *_buf, size_t filesize, unsigned flags, vo
906 { 906 {
907 printf(RED, " Match\n"); 907 printf(RED, " Match\n");
908 /* decrypt */ 908 /* decrypt */
909 byte decrypted_key[16]; 909 uint8_t decrypted_key[16];
910 byte iv[16]; 910 uint8_t iv[16];
911 memcpy(iv, buf, 16); /* uses the first 16-bytes of SHA-1 sig as IV */ 911 memcpy(iv, buf, 16); /* uses the first 16-bytes of SHA-1 sig as IV */
912 check_crypto(crypto_setup(&g_key_array[idx])); 912 check_crypto(crypto_setup(&g_key_array[idx]));
913 check_crypto(crypto_apply(dict_entry->key, decrypted_key, 1, iv, NULL, false)); 913 check_crypto(crypto_apply(dict_entry->key, decrypted_key, 1, iv, NULL, false));
@@ -1008,7 +1008,7 @@ struct sb_file_t *sb_read_memory(void *_buf, size_t filesize, unsigned flags, vo
1008 } 1008 }
1009 1009
1010 /* save it */ 1010 /* save it */
1011 byte *sec = xmalloc(size); 1011 uint8_t *sec = xmalloc(size);
1012 if(encrypted) 1012 if(encrypted)
1013 check_crypto(crypto_apply(buf + pos, sec, size / BLOCK_SIZE, buf, NULL, false)); 1013 check_crypto(crypto_apply(buf + pos, sec, size / BLOCK_SIZE, buf, NULL, false));
1014 else 1014 else
@@ -1034,13 +1034,13 @@ struct sb_file_t *sb_read_memory(void *_buf, size_t filesize, unsigned flags, vo
1034 /* advanced raw mode */ 1034 /* advanced raw mode */
1035 printf(BLUE, "Commands\n"); 1035 printf(BLUE, "Commands\n");
1036 uint32_t offset = sb_header->first_boot_tag_off * BLOCK_SIZE; 1036 uint32_t offset = sb_header->first_boot_tag_off * BLOCK_SIZE;
1037 byte iv[16]; 1037 uint8_t iv[16];
1038 const char *indent = " "; 1038 const char *indent = " ";
1039 while(true) 1039 while(true)
1040 { 1040 {
1041 /* restart with IV */ 1041 /* restart with IV */
1042 memcpy(iv, buf, 16); 1042 memcpy(iv, buf, 16);
1043 byte cmd[BLOCK_SIZE]; 1043 uint8_t cmd[BLOCK_SIZE];
1044 if(sb_header->nr_keys > 0) 1044 if(sb_header->nr_keys > 0)
1045 check_crypto(crypto_apply(buf + offset, cmd, 1, iv, &iv, false)); 1045 check_crypto(crypto_apply(buf + offset, cmd, 1, iv, &iv, false));
1046 else 1046 else
@@ -1106,7 +1106,7 @@ struct sb_file_t *sb_read_memory(void *_buf, size_t filesize, unsigned flags, vo
1106 } 1106 }
1107 1107
1108 /* save it */ 1108 /* save it */
1109 byte *sec = xmalloc(size); 1109 uint8_t *sec = xmalloc(size);
1110 if(encrypted) 1110 if(encrypted)
1111 check_crypto(crypto_apply(buf + pos, sec, size / BLOCK_SIZE, buf, NULL, false)); 1111 check_crypto(crypto_apply(buf + pos, sec, size / BLOCK_SIZE, buf, NULL, false));
1112 else 1112 else
@@ -1147,11 +1147,11 @@ struct sb_file_t *sb_read_memory(void *_buf, size_t filesize, unsigned flags, vo
1147 1147
1148 /* final signature */ 1148 /* final signature */
1149 printf(BLUE, "Final signature:\n"); 1149 printf(BLUE, "Final signature:\n");
1150 byte decrypted_block[32]; 1150 uint8_t decrypted_block[32];
1151 if(sb_header->nr_keys > 0) 1151 if(sb_header->nr_keys > 0)
1152 { 1152 {
1153 printf(GREEN, " Encrypted SHA-1:\n"); 1153 printf(GREEN, " Encrypted SHA-1:\n");
1154 byte *encrypted_block = &buf[filesize - 32]; 1154 uint8_t *encrypted_block = &buf[filesize - 32];
1155 printf(OFF, " "); 1155 printf(OFF, " ");
1156 print_hex(YELLOW, encrypted_block, 16, true); 1156 print_hex(YELLOW, encrypted_block, 16, true);
1157 printf(OFF, " "); 1157 printf(OFF, " ");