Beispiel #1
0
    def export(self, dbg_info: DebugInfo = DebugInfo.disabled()) -> bytes:
        """Export to binary form (serialization).

        :param dbg_info: debug information about exported data
        :return: binary representation of the command
        """
        hdr_data = self._header.export()
        dbg_info.append_binary_data('header', hdr_data)
        return hdr_data
Beispiel #2
0
 def export(self, dbg_info: DebugInfo = DebugInfo.disabled()) -> bytes:
     """Return command in binary form (serialization)."""
     # export cmd
     data = super().export(dbg_info)
     # export additional data
     if len(self._pattern) > 4:
         data += bytes(self._pattern[4:])
         dbg_info.append_binary_data('pattern', self._pattern[4:])
     data = SecBootBlckSize.align_block_fill_random(data)
     return data
Beispiel #3
0
def _log_test_output(dbg_info: DebugInfo) -> None:
    """Uses all methods of DebugInfo class to append data in different format

    :param dbg_info: instance used for logging
    """
    dbg_info.append_section("SECTION")
    dbg_info.append("-test-line-")
    dbg_info.append_binary_section("bin", b"\x00\x11\x22\xFF")
    dbg_info.append_binary_data("data", b"\x00\x11\x22")
    dbg_info.append_hex_data(b"\x00\x11\x22\x00\x11\x22\x00\x11\x22\x00\x11\x22")
Beispiel #4
0
    def export(self, dbg_info: DebugInfo = DebugInfo.disabled()) -> bytes:
        """Export to binary form (serialization).

        :param dbg_info: debug information about exported data
        :return: binary representation of the command
        """
        raw_data = super().export(dbg_info=dbg_info)
        raw_data += pack("4B", 0x00, self.hash_algorithm, self.engine, self.engine_cfg)
        dbg_info.append_binary_data('data', raw_data)
        return raw_data
Beispiel #5
0
def _log_test_output(dbg_info: DebugInfo) -> None:
    """Uses all methods of DebugInfo class to append data in different format

    :param dbg_info: instance used for logging
    """
    dbg_info.append_section('SECTION')
    dbg_info.append('-test-line-')
    dbg_info.append_binary_section('bin', b'\x00\x11\x22\xFF')
    dbg_info.append_binary_data('data', b'\x00\x11\x22')
    dbg_info.append_hex_data(
        b'\x00\x11\x22\x00\x11\x22\x00\x11\x22\x00\x11\x22')
Beispiel #6
0
 def export(self, dbg_info: DebugInfo = DebugInfo.disabled()) -> bytes:
     """Return binary representation of the class (serialization)."""
     self.update()
     dbg_info.append_section('Section')
     data = self._header.export()
     dbg_info.append_binary_data('Section-header', data)
     dbg_info.append_section('Commands')
     for cmd in self._commands:
         cmd_data = cmd.export(dbg_info)
         data += cmd_data
     return data
Beispiel #7
0
    def export(self, dbg_info: DebugInfo = DebugInfo.disabled()) -> bytes:
        """Export to binary form (serialization).

        :param dbg_info: debug information about exported data
        :return: binary representation of the command
        """
        raw_data = super().export(dbg_info=dbg_info)
        data = pack(">4BL", self.certificate_format, self.hash_algorithm, self.source_index, self.target_index,
                    self.cmd_data_location)
        raw_data += data
        dbg_info.append_binary_data('data', data)
        return raw_data
Beispiel #8
0
    def export(self, dbg_info: DebugInfo = DebugInfo.disabled()) -> bytes:
        """Export to binary form (serialization).

        :param dbg_info: debug information about exported data
        :return: binary representation of the command
        """
        assert self.size == CmdHeader.SIZE + 4
        raw_data = super().export(dbg_info=dbg_info)
        data = pack(">L", self.features)
        dbg_info.append_binary_data('data', data)
        raw_data += data
        return raw_data
Beispiel #9
0
    def export(self, dbg_info: DebugInfo = DebugInfo.disabled()) -> bytes:
        """Export instance into binary form (serialization).

        :param dbg_info: optional instance providing debug info about exported content
        :return: binary form
        """
        self._validate_data()
        self._header.length = self.size
        raw_data = self._header.export()
        dbg_info.append_binary_data("header", raw_data)
        raw_data += pack(">4B", 0, self.nonce_len, 0, self.mac_len)
        dbg_info.append("nonce=" + self.nonce.hex())
        dbg_info.append("mac=" + self.mac.hex())
        raw_data += self.data
        return raw_data
Beispiel #10
0
    def export(self, dbg_info: DebugInfo = DebugInfo.disabled()) -> bytes:
        """Export to binary form (serialization).

        :param dbg_info: debug information about exported data
        :return: binary representation of the command
        """
        self._header.length = self.size
        raw_data = super().export(dbg_info=dbg_info)
        data = pack(">4BL", self.key_index, self.sig_format, self.engine, self.engine_cfg, self.location)
        dbg_info.append_binary_data('data', data)
        raw_data += data
        for blk in self._blocks:
            blk_data = pack(">2L", blk[0], blk[1])
            dbg_info.append_binary_data('block', blk_data)
            raw_data += blk_data
        return raw_data
Beispiel #11
0
    def export(
            self,
            header_padding8: Optional[bytes] = None,
            auth_padding: Optional[bytes] = None,
            dbg_info: DebugInfo = DebugInfo.disabled(),
    ) -> bytes:
        """Serialization to binary form.

        :param header_padding8: optional header padding, 8-bytes; recommended to use None to apply random value
        :param auth_padding: optional padding used after authentication; recommended to use None to apply random value
        :param dbg_info: instance allowing to debug generated output
        :return: serialize the instance into binary data
        :raises SPSDKError: Invalid section data
        :raises SPSDKError: Invalid padding length
        """
        self.update()
        self.validate()
        dbg_info.append_section("SB-FILE-1.x")
        data = self._header.export(padding8=header_padding8, dbg_info=dbg_info)
        # header table
        dbg_info.append_section("Sections-Header-Table")
        for sect_hdr in self._sections_hdr_table:
            sect_hdr_data = sect_hdr.export()
            dbg_info.append_binary_data("Section-Header-Item", sect_hdr_data)
            data += sect_hdr_data
        # sections
        dbg_info.append_section("Sections")
        for sect in self._sections:
            sect_data = sect.export(dbg_info)
            if len(sect_data) != sect.size:
                raise SPSDKError("Invalid section data")
            data += sect_data
        # authentication: SHA1
        auth_code = crypto_backend().hash(data, "sha1")
        dbg_info.append_binary_section("SHA1", auth_code)
        data += auth_code
        # padding
        padding_len = align(len(auth_code),
                            SecBootBlckSize.BLOCK_SIZE) - len(auth_code)
        if auth_padding is None:
            auth_padding = crypto_backend().random_bytes(padding_len)
        if padding_len != len(auth_padding):
            raise SPSDKError("Invalid padding length")
        data += auth_padding
        dbg_info.append_binary_section("padding", auth_padding)
        return data
Beispiel #12
0
    def export(
        self,
        header_padding8: Optional[bytes] = None,
        auth_padding: Optional[bytes] = None,
        dbg_info: DebugInfo = DebugInfo.disabled()
    ) -> bytes:
        """Serialization to binary form.

        :param header_padding8: optional header padding, 8-bytes; recommended to use None to apply random value
        :param auth_padding: optional padding used after authentication; recommended to use None to apply random value
        :param dbg_info: instance allowing to debug generated output
        :return: serialize the instance into binary data
        """
        self.update()
        self.validate()
        dbg_info.append_section('SB-FILE-1.x')
        data = self._header.export(padding8=header_padding8, dbg_info=dbg_info)
        # header table
        dbg_info.append_section('Sections-Header-Table')
        for sect_hdr in self._sections_hdr_table:
            sect_hdr_data = sect_hdr.export()
            dbg_info.append_binary_data('Section-Header-Item', sect_hdr_data)
            data += sect_hdr_data
        # sections
        dbg_info.append_section('Sections')
        for sect in self._sections:
            sect_data = sect.export(dbg_info)
            assert len(sect_data) == sect.size
            data += sect_data
        # authentication: SHA1
        auth_code = crypto_backend().hash(data, 'sha1')
        dbg_info.append_binary_section('SHA1', auth_code)
        data += auth_code
        # padding
        padding_len = align(len(auth_code),
                            SecBootBlckSize.BLOCK_SIZE) - len(auth_code)
        if auth_padding is None:
            auth_padding = crypto_backend().random_bytes(padding_len)
        assert padding_len == len(auth_padding)
        data += auth_padding
        dbg_info.append_binary_section('padding', auth_padding)
        return data
Beispiel #13
0
 def export(self, dbg_info: DebugInfo = DebugInfo.disabled()) -> bytes:
     """Return object serialized into bytes."""
     dbg_info.append_section("Command:" + EnumCmdTag.name(self.header.tag))
     cmd_data = self._header.export()  # default implementation
     dbg_info.append_binary_data("cmd-header", cmd_data)
     return cmd_data
Beispiel #14
0
def test_debug_info_invalid():
    dbg_info = DebugInfo()
    with pytest.raises(SPSDKError, match="Incorrect data length"):
        dbg_info.append_binary_data("data", bytes(20))