Lines Matching refs:ictx
205 static int sb_aes_init(struct sb_image_ctx *ictx, uint8_t *iv, int enc) in sb_aes_init() argument
212 iv = ictx->image_key; in sb_aes_init()
215 ret = EVP_CipherInit(ctx, EVP_aes_128_cbc(), ictx->image_key, iv, enc); in sb_aes_init()
218 ictx->cipher_ctx = ctx; in sb_aes_init()
223 static int sb_aes_crypt(struct sb_image_ctx *ictx, uint8_t *in_data, in sb_aes_crypt() argument
226 EVP_CIPHER_CTX *ctx = ictx->cipher_ctx; in sb_aes_crypt()
254 static int sb_aes_reinit(struct sb_image_ctx *ictx, int enc) in sb_aes_reinit() argument
257 EVP_CIPHER_CTX *ctx = ictx->cipher_ctx; in sb_aes_reinit()
258 struct sb_boot_image_header *sb_header = &ictx->payload; in sb_aes_reinit()
264 return sb_aes_init(ictx, iv, enc); in sb_aes_reinit()
270 static void soprintf(struct sb_image_ctx *ictx, const char *fmt, ...) in soprintf() argument
274 if (ictx->silent_dump) in soprintf()
310 static void sb_encrypt_sb_header(struct sb_image_ctx *ictx) in sb_encrypt_sb_header() argument
312 EVP_MD_CTX *md_ctx = ictx->md_ctx; in sb_encrypt_sb_header()
313 struct sb_boot_image_header *sb_header = &ictx->payload; in sb_encrypt_sb_header()
317 sb_aes_crypt(ictx, sb_header_ptr, NULL, sizeof(*sb_header)); in sb_encrypt_sb_header()
321 static void sb_encrypt_sb_sections_header(struct sb_image_ctx *ictx) in sb_encrypt_sb_sections_header() argument
323 EVP_MD_CTX *md_ctx = ictx->md_ctx; in sb_encrypt_sb_sections_header()
324 struct sb_section_ctx *sctx = ictx->sect_head; in sb_encrypt_sb_sections_header()
333 sb_aes_crypt(ictx, sb_sections_header_ptr, in sb_encrypt_sb_sections_header()
334 ictx->sb_dict_key.cbc_mac, size); in sb_encrypt_sb_sections_header()
341 static void sb_encrypt_key_dictionary_key(struct sb_image_ctx *ictx) in sb_encrypt_key_dictionary_key() argument
343 EVP_MD_CTX *md_ctx = ictx->md_ctx; in sb_encrypt_key_dictionary_key()
345 sb_aes_crypt(ictx, ictx->image_key, ictx->sb_dict_key.key, in sb_encrypt_key_dictionary_key()
346 sizeof(ictx->sb_dict_key.key)); in sb_encrypt_key_dictionary_key()
347 EVP_DigestUpdate(md_ctx, &ictx->sb_dict_key, sizeof(ictx->sb_dict_key)); in sb_encrypt_key_dictionary_key()
350 static void sb_decrypt_key_dictionary_key(struct sb_image_ctx *ictx) in sb_decrypt_key_dictionary_key() argument
352 EVP_MD_CTX *md_ctx = ictx->md_ctx; in sb_decrypt_key_dictionary_key()
354 EVP_DigestUpdate(md_ctx, &ictx->sb_dict_key, sizeof(ictx->sb_dict_key)); in sb_decrypt_key_dictionary_key()
355 sb_aes_crypt(ictx, ictx->sb_dict_key.key, ictx->image_key, in sb_decrypt_key_dictionary_key()
356 sizeof(ictx->sb_dict_key.key)); in sb_decrypt_key_dictionary_key()
359 static void sb_encrypt_tag(struct sb_image_ctx *ictx, in sb_encrypt_tag() argument
362 EVP_MD_CTX *md_ctx = ictx->md_ctx; in sb_encrypt_tag()
365 sb_aes_crypt(ictx, (uint8_t *)cmd, in sb_encrypt_tag()
370 static int sb_encrypt_image(struct sb_image_ctx *ictx) in sb_encrypt_image() argument
373 ictx->md_ctx = EVP_MD_CTX_new(); in sb_encrypt_image()
374 EVP_DigestInit(ictx->md_ctx, EVP_sha1()); in sb_encrypt_image()
379 sb_aes_init(ictx, NULL, 1); in sb_encrypt_image()
380 sb_encrypt_sb_header(ictx); in sb_encrypt_image()
385 sb_encrypt_sb_sections_header(ictx); in sb_encrypt_image()
390 sb_aes_reinit(ictx, 1); in sb_encrypt_image()
391 sb_encrypt_key_dictionary_key(ictx); in sb_encrypt_image()
398 struct sb_section_ctx *sctx = ictx->sect_head; in sb_encrypt_image()
403 sb_aes_reinit(ictx, 1); in sb_encrypt_image()
408 sb_encrypt_tag(ictx, cctx); in sb_encrypt_image()
411 sb_aes_reinit(ictx, 1); in sb_encrypt_image()
413 sb_aes_crypt(ictx, cctx->data, cctx->data, in sb_encrypt_image()
415 EVP_DigestUpdate(ictx->md_ctx, cctx->data, in sb_encrypt_image()
428 sb_aes_reinit(ictx, 1); in sb_encrypt_image()
430 EVP_DigestFinal(ictx->md_ctx, ictx->digest, NULL); in sb_encrypt_image()
431 EVP_MD_CTX_free(ictx->md_ctx); in sb_encrypt_image()
432 sb_aes_crypt(ictx, ictx->digest, ictx->digest, sizeof(ictx->digest)); in sb_encrypt_image()
435 sb_aes_deinit(ictx->cipher_ctx); in sb_encrypt_image()
561 static int sb_build_dcd(struct sb_image_ctx *ictx, struct sb_cmd_list *cmd) in sb_build_dcd() argument
601 if (!ictx->dcd_head) { in sb_build_dcd()
602 ictx->dcd_head = dctx; in sb_build_dcd()
603 ictx->dcd_tail = dctx; in sb_build_dcd()
605 ictx->dcd_tail->dcd = dctx; in sb_build_dcd()
606 ictx->dcd_tail = dctx; in sb_build_dcd()
617 static int sb_build_dcd_block(struct sb_image_ctx *ictx, in sb_build_dcd_block() argument
625 struct sb_dcd_ctx *dctx = ictx->dcd_tail; in sb_build_dcd_block()
711 static int sb_build_section(struct sb_image_ctx *ictx, struct sb_cmd_list *cmd) in sb_build_section() argument
757 ictx->sect_count++; in sb_build_section()
761 if (ictx->sect_boot_found) { in sb_build_section()
766 ictx->sect_boot = id; in sb_build_section()
767 ictx->sect_boot_found = 1; in sb_build_section()
771 if (!ictx->sect_head) { in sb_build_section()
772 ictx->sect_head = sctx; in sb_build_section()
773 ictx->sect_tail = sctx; in sb_build_section()
775 ictx->sect_tail->sect = sctx; in sb_build_section()
776 ictx->sect_tail = sctx; in sb_build_section()
786 static int sb_build_command_nop(struct sb_image_ctx *ictx) in sb_build_command_nop() argument
788 struct sb_section_ctx *sctx = ictx->sect_tail; in sb_build_command_nop()
820 static int sb_build_command_tag(struct sb_image_ctx *ictx, in sb_build_command_tag() argument
823 struct sb_section_ctx *sctx = ictx->sect_tail; in sb_build_command_tag()
864 static int sb_build_command_load(struct sb_image_ctx *ictx, in sb_build_command_load() argument
867 struct sb_section_ctx *sctx = ictx->sect_tail; in sb_build_command_load()
950 struct sb_dcd_ctx *dctx = ictx->dcd_head; in sb_build_command_load()
1037 static int sb_build_command_fill(struct sb_image_ctx *ictx, in sb_build_command_fill() argument
1040 struct sb_section_ctx *sctx = ictx->sect_tail; in sb_build_command_fill()
1134 static int sb_build_command_jump_call(struct sb_image_ctx *ictx, in sb_build_command_jump_call() argument
1138 struct sb_section_ctx *sctx = ictx->sect_tail; in sb_build_command_jump_call()
1225 static int sb_build_command_jump(struct sb_image_ctx *ictx, in sb_build_command_jump() argument
1228 return sb_build_command_jump_call(ictx, cmd, 0); in sb_build_command_jump()
1231 static int sb_build_command_call(struct sb_image_ctx *ictx, in sb_build_command_call() argument
1234 return sb_build_command_jump_call(ictx, cmd, 1); in sb_build_command_call()
1237 static int sb_build_command_mode(struct sb_image_ctx *ictx, in sb_build_command_mode() argument
1240 struct sb_section_ctx *sctx = ictx->sect_tail; in sb_build_command_mode()
1315 static int sb_prefill_image_header(struct sb_image_ctx *ictx) in sb_prefill_image_header() argument
1317 struct sb_boot_image_header *hdr = &ictx->payload; in sb_prefill_image_header()
1345 hdr->flags = ictx->display_progress ? in sb_prefill_image_header()
1354 static int sb_postfill_image_header(struct sb_image_ctx *ictx) in sb_postfill_image_header() argument
1356 struct sb_boot_image_header *hdr = &ictx->payload; in sb_postfill_image_header()
1357 struct sb_section_ctx *sctx = ictx->sect_head; in sb_postfill_image_header()
1369 hdr->section_count = ictx->sect_count; in sb_postfill_image_header()
1375 if (!ictx->sect_boot_found) { in sb_postfill_image_header()
1379 hdr->first_boot_section_id = ictx->sect_boot; in sb_postfill_image_header()
1408 static int sb_fixup_sections_and_tags(struct sb_image_ctx *ictx) in sb_fixup_sections_and_tags() argument
1411 struct sb_boot_image_header *ihdr = &ictx->payload; in sb_fixup_sections_and_tags()
1412 struct sb_section_ctx *sctx = ictx->sect_head; in sb_fixup_sections_and_tags()
1450 static int sb_parse_line(struct sb_image_ctx *ictx, struct sb_cmd_list *cmd) in sb_parse_line() argument
1468 ictx->display_progress = 1; in sb_parse_line()
1474 ictx->in_section = 0; in sb_parse_line()
1475 ictx->in_dcd = 1; in sb_parse_line()
1476 sb_build_dcd(ictx, cmd); in sb_parse_line()
1482 ictx->in_section = 1; in sb_parse_line()
1483 ictx->in_dcd = 0; in sb_parse_line()
1484 sb_build_section(ictx, cmd); in sb_parse_line()
1488 if (!ictx->in_section && !ictx->in_dcd) { in sb_parse_line()
1494 if (ictx->in_section) { in sb_parse_line()
1497 ret = sb_build_command_nop(ictx); in sb_parse_line()
1499 ret = sb_build_command_tag(ictx, cmd); in sb_parse_line()
1501 ret = sb_build_command_load(ictx, cmd); in sb_parse_line()
1503 ret = sb_build_command_fill(ictx, cmd); in sb_parse_line()
1505 ret = sb_build_command_jump(ictx, cmd); in sb_parse_line()
1507 ret = sb_build_command_call(ictx, cmd); in sb_parse_line()
1509 ret = sb_build_command_mode(ictx, cmd); in sb_parse_line()
1516 } else if (ictx->in_dcd) { in sb_parse_line()
1539 ret = sb_build_dcd_block(ictx, cmd, in sb_parse_line()
1542 ret = sb_build_dcd_block(ictx, cmd, in sb_parse_line()
1545 ret = sb_build_dcd_block(ictx, cmd, in sb_parse_line()
1548 ret = sb_build_dcd_block(ictx, cmd, in sb_parse_line()
1551 ret = sb_build_dcd_block(ictx, cmd, in sb_parse_line()
1554 ret = sb_build_dcd_block(ictx, cmd, in sb_parse_line()
1557 ret = sb_build_dcd_block(ictx, cmd, in sb_parse_line()
1560 ret = sb_build_dcd_block(ictx, cmd, SB_DCD_NOOP); in sb_parse_line()
1579 if (ictx->in_section && !ret) { in sb_parse_line()
1580 ictx->sect_tail->size += ictx->sect_tail->cmd_tail->size; in sb_parse_line()
1581 ictx->sect_tail->payload.section_size = in sb_parse_line()
1582 ictx->sect_tail->size / SB_BLOCK_SIZE; in sb_parse_line()
1588 static int sb_load_cmdfile(struct sb_image_ctx *ictx) in sb_load_cmdfile() argument
1597 fp = fopen(ictx->cfg_filename, "r"); in sb_load_cmdfile()
1611 sb_parse_line(ictx, &cmd); in sb_load_cmdfile()
1623 ictx->cfg_filename); in sb_load_cmdfile()
1627 static int sb_build_tree_from_cfg(struct sb_image_ctx *ictx) in sb_build_tree_from_cfg() argument
1631 ret = sb_load_cmdfile(ictx); in sb_build_tree_from_cfg()
1635 ret = sb_prefill_image_header(ictx); in sb_build_tree_from_cfg()
1639 ret = sb_postfill_image_header(ictx); in sb_build_tree_from_cfg()
1643 ret = sb_fixup_sections_and_tags(ictx); in sb_build_tree_from_cfg()
1650 static int sb_verify_image_header(struct sb_image_ctx *ictx, in sb_verify_image_header() argument
1654 struct sb_boot_image_header *hdr = &ictx->payload; in sb_verify_image_header()
1663 ictx->md_ctx = EVP_MD_CTX_new(); in sb_verify_image_header()
1664 EVP_DigestInit(ictx->md_ctx, EVP_sha1()); in sb_verify_image_header()
1666 soprintf(ictx, "---------- Verifying SB Image Header ----------\n"); in sb_verify_image_header()
1668 size = fread(&ictx->payload, 1, sizeof(ictx->payload), fp); in sb_verify_image_header()
1669 if (size != sizeof(ictx->payload)) { in sb_verify_image_header()
1683 sb_aes_init(ictx, NULL, 1); in sb_verify_image_header()
1684 sb_encrypt_sb_header(ictx); in sb_verify_image_header()
1688 soprintf(ictx, "%s Image header checksum: %s\n", stat[!!ret], in sb_verify_image_header()
1696 soprintf(ictx, "%s Signatures: '%.4s' '%.4s'\n", in sb_verify_image_header()
1704 soprintf(ictx, "%s Image version: v%i.%i\n", stat[!!ret], in sb_verify_image_header()
1710 soprintf(ictx, in sb_verify_image_header()
1717 soprintf(ictx, "%s Product version: %x.%x.%x\n", stat[0], in sb_verify_image_header()
1721 soprintf(ictx, "%s Component version: %x.%x.%x\n", stat[0], in sb_verify_image_header()
1728 soprintf(ictx, "%s Image flags: %s\n", stat[!!ret], in sb_verify_image_header()
1736 soprintf(ictx, "%s Drive tag: %i\n", stat[!!ret], in sb_verify_image_header()
1744 soprintf(ictx, "%s Image header size (blocks): %i\n", stat[!!ret], in sb_verify_image_header()
1752 soprintf(ictx, "%s Section header size (blocks): %i\n", stat[!!ret], in sb_verify_image_header()
1757 soprintf(ictx, "%s Sections count: %i\n", stat[!!ret], in sb_verify_image_header()
1759 soprintf(ictx, "%s First bootable section %i\n", stat[!!ret], in sb_verify_image_header()
1764 soprintf(ictx, "%s Image size (blocks): %i\n", stat[!!ret], in sb_verify_image_header()
1772 soprintf(ictx, "%s Key dict offset (blocks): %i\n", stat[!!ret], in sb_verify_image_header()
1779 soprintf(ictx, "%s Number of encryption keys: %i\n", stat[!!ret], in sb_verify_image_header()
1789 soprintf(ictx, "%s First TAG block (blocks): %i\n", stat[!!ret], in sb_verify_image_header()
1797 static void sb_decrypt_tag(struct sb_image_ctx *ictx, in sb_decrypt_tag() argument
1800 EVP_MD_CTX *md_ctx = ictx->md_ctx; in sb_decrypt_tag()
1803 sb_aes_crypt(ictx, (uint8_t *)&cctx->c_payload, in sb_decrypt_tag()
1808 static int sb_verify_command(struct sb_image_ctx *ictx, in sb_verify_command() argument
1830 soprintf(ictx, " NOOP # %s\n", csum); in sb_verify_command()
1835 soprintf(ictx, " TAG %s # %s\n", flag, csum); in sb_verify_command()
1836 sb_aes_reinit(ictx, 0); in sb_verify_command()
1839 soprintf(ictx, " LOAD addr=0x%08x length=0x%08x # %s\n", in sb_verify_command()
1857 EVP_DigestUpdate(ictx->md_ctx, cctx->data, asize); in sb_verify_command()
1858 sb_aes_crypt(ictx, cctx->data, cctx->data, asize); in sb_verify_command()
1869 soprintf(ictx, in sb_verify_command()
1877 soprintf(ictx, in sb_verify_command()
1884 soprintf(ictx, in sb_verify_command()
1891 soprintf(ictx, " MODE %s # %s\n", in sb_verify_command()
1903 static int sb_verify_commands(struct sb_image_ctx *ictx, in sb_verify_commands() argument
1910 sb_aes_reinit(ictx, 0); in sb_verify_commands()
1932 sb_decrypt_tag(ictx, cctx); in sb_verify_commands()
1934 ret = sb_verify_command(ictx, cctx, fp, &tsize); in sb_verify_commands()
1942 static int sb_verify_sections_cmds(struct sb_image_ctx *ictx, FILE *fp) in sb_verify_sections_cmds() argument
1944 struct sb_boot_image_header *hdr = &ictx->payload; in sb_verify_sections_cmds()
1952 soprintf(ictx, "----- Verifying SB Sections and Commands -----\n"); in sb_verify_sections_cmds()
1958 if (!ictx->sect_head) { in sb_verify_sections_cmds()
1959 ictx->sect_head = sctx; in sb_verify_sections_cmds()
1960 ictx->sect_tail = sctx; in sb_verify_sections_cmds()
1962 ictx->sect_tail->sect = sctx; in sb_verify_sections_cmds()
1963 ictx->sect_tail = sctx; in sb_verify_sections_cmds()
1973 size = fread(&ictx->sb_dict_key, 1, sizeof(ictx->sb_dict_key), fp); in sb_verify_sections_cmds()
1974 if (size != sizeof(ictx->sb_dict_key)) { in sb_verify_sections_cmds()
1979 sb_encrypt_sb_sections_header(ictx); in sb_verify_sections_cmds()
1980 sb_aes_reinit(ictx, 0); in sb_verify_sections_cmds()
1981 sb_decrypt_key_dictionary_key(ictx); in sb_verify_sections_cmds()
1983 sb_aes_reinit(ictx, 0); in sb_verify_sections_cmds()
1985 sctx = ictx->sect_head; in sb_verify_sections_cmds()
1996 soprintf(ictx, "SECTION 0x%x%s # size = %i bytes\n", in sb_verify_sections_cmds()
2009 ret = sb_verify_commands(ictx, sctx, fp); in sb_verify_sections_cmds()
2023 static int sb_verify_image_end(struct sb_image_ctx *ictx, in sb_verify_image_end() argument
2031 soprintf(ictx, "------------- Verifying image end -------------\n"); in sb_verify_image_end()
2046 EVP_DigestFinal(ictx->md_ctx, ictx->digest, NULL); in sb_verify_image_end()
2047 EVP_MD_CTX_free(ictx->md_ctx); in sb_verify_image_end()
2050 sb_aes_reinit(ictx, 0); in sb_verify_image_end()
2051 sb_aes_crypt(ictx, digest, digest, sizeof(digest)); in sb_verify_image_end()
2054 ret = memcmp(digest, ictx->digest, 20) ? -EINVAL : 0; in sb_verify_image_end()
2057 soprintf(ictx, "[FAIL] Full-image checksum: BAD\n"); in sb_verify_image_end()
2059 soprintf(ictx, "[PASS] Full-image checksum: OK\n"); in sb_verify_image_end()
2065 static int sb_build_tree_from_img(struct sb_image_ctx *ictx) in sb_build_tree_from_img() argument
2071 if (!ictx->input_filename) { in sb_build_tree_from_img()
2076 fp = fopen(ictx->input_filename, "r"); in sb_build_tree_from_img()
2092 if (filesize < (signed)sizeof(ictx->payload)) { in sb_build_tree_from_img()
2103 ret = sb_verify_image_header(ictx, fp, filesize); in sb_build_tree_from_img()
2108 ret = sb_verify_sections_cmds(ictx, fp); in sb_build_tree_from_img()
2112 ret = sb_verify_image_end(ictx, fp, filesize); in sb_build_tree_from_img()
2119 soprintf(ictx, "-------------------- Result -------------------\n"); in sb_build_tree_from_img()
2120 soprintf(ictx, "Verification %s\n", ret ? "FAILED" : "PASSED"); in sb_build_tree_from_img()
2123 sb_aes_deinit(ictx->cipher_ctx); in sb_build_tree_from_img()
2132 ictx->input_filename); in sb_build_tree_from_img()
2136 static void sb_free_image(struct sb_image_ctx *ictx) in sb_free_image() argument
2138 struct sb_section_ctx *sctx = ictx->sect_head, *s_head; in sb_free_image()
2139 struct sb_dcd_ctx *dctx = ictx->dcd_head, *d_head; in sb_free_image()
2251 static int sb_build_image(struct sb_image_ctx *ictx, in sb_build_image() argument
2254 struct sb_boot_image_header *sb_header = &ictx->payload; in sb_build_image()
2258 struct sb_key_dictionary_key *sb_dict_key = &ictx->sb_dict_key; in sb_build_image()
2264 ictx->sect_count * sizeof(struct sb_sections_header) + in sb_build_image()
2265 sizeof(*sb_dict_key) + sizeof(ictx->digest); in sb_build_image()
2267 sctx = ictx->sect_head; in sb_build_image()
2281 sctx = ictx->sect_head; in sb_build_image()
2291 sctx = ictx->sect_head; in sb_build_image()
2311 memcpy(iptr, ictx->digest, sizeof(ictx->digest)); in sb_build_image()
2312 iptr += sizeof(ictx->digest); in sb_build_image()