Beispiel #1
0
class DebugAuthenticateResponse:
    """Class for DAR packet."""

    def __init__(
        self,
        debug_credential: DebugCredential,
        auth_beacon: int,
        dac: DebugAuthenticationChallenge,
        path_dck_private: str,
    ) -> None:
        """Initialize the DebugAuthenticateResponse object.

        :param debug_credential: the path, where the dc is store
        :param auth_beacon: authentication beacon value
        :param dac: the path, where the dac is store
        :param path_dck_private: the path, where the dck private key is store
        """
        self.debug_credential = debug_credential
        self.auth_beacon = auth_beacon
        self.dac = dac
        self.dck_priv = path_dck_private
        self.sig_provider = PlainFileSP(path_dck_private)

    def info(self) -> str:
        """String representation of DebugAuthenticateResponse."""
        msg = f"DAC:\n{self.dac.info()}\n"
        msg += f"DC:\n{self.debug_credential.info()}\n"
        msg += f"Authentication Beacon: {self.auth_beacon}\n"
        return msg

    def _get_data_for_signature(self) -> bytes:
        """Collects the data for signature in bytes format."""
        data = self._get_common_data()
        data += self.dac.challenge
        return data

    def _get_signature(self) -> bytes:
        if not self.sig_provider:
            raise SPSDKError("Signature provider is not set")
        signature = self.sig_provider.sign(self._get_data_for_signature())
        if not signature:
            raise SPSDKError("Signature is not present")
        return signature

    def export(self) -> bytes:
        """Export to binary form (serialization).

        :return: the exported bytes from object
        """
        data = self._get_common_data()
        data += self._get_signature()
        return data

    def _get_common_data(self) -> bytes:
        """Collects dc, auth_beacon."""
        data = self.debug_credential.export()
        data += pack("<L", self.auth_beacon)
        return data

    @classmethod
    def parse(cls, data: bytes, offset: int = 0) -> "DebugAuthenticateResponse":
        """Parse the DAR.

        :param data: Raw data as bytes
        :param offset: Offset of input data
        :return: DebugAuthenticateResponse object
        :raises NotImplementedError: Derived class has to implement this method
        """
        raise NotImplementedError("Derived class has to implement this method.")

    @classmethod
    def _get_class(cls, version: str, socc: int) -> "Type[DebugAuthenticateResponse]":
        if socc == 4:
            return _lpc55s3x_version_mapping[version]
        return _version_mapping[version]

    @classmethod
    def create(
        cls,
        version: str,
        socc: int,
        dc: DebugCredential,
        auth_beacon: int,
        dac: DebugAuthenticationChallenge,
        dck: str,
    ) -> "DebugAuthenticateResponse":
        """Create a dar object out of input parameters.

        :param version: protocol version
        :param socc: SoC Class
        :param dc: debug credential object
        :param auth_beacon: authentication beacon value
        :param dac: DebugAuthenticationChallenge object
        :param dck: string containing path to dck key
        :return: DAR object
        """
        klass = DebugAuthenticateResponse._get_class(version=version, socc=socc)
        dar_obj = klass(debug_credential=dc, auth_beacon=auth_beacon, dac=dac, path_dck_private=dck)
        return dar_obj
Beispiel #2
0
class DebugAuthenticateResponse:
    """Class for DAR packet."""
    def __init__(self, debug_credential: DebugCredential, auth_beacon: int,
                 dac: DebugAuthenticationChallenge,
                 path_dck_private: str) -> None:
        """Initialize the DebugAuthenticateResponse object.

        :param debug_credential:the path, where the dc is store
        :param auth_beacon: authentication beacon value
        :param dac: the path, where the dac is store
        :param path_dck_private: the path, where the dck private key is store
        """
        self.debug_credential = debug_credential
        self.auth_beacon = auth_beacon
        self.dac = dac
        self.dck_priv = path_dck_private
        self.is_n4analog = self.debug_credential.socc == 0x04
        self.sig_provider = PlainFileSP(path_dck_private)

    def info(self) -> str:
        """String representation of DebugAuthenticateResponse."""
        # pylint: disable=bad-whitespace
        msg = f"DAC:\n{self.dac.info()}\n"
        msg += f"DC:\n{self.debug_credential.info()}\n"
        msg += f"Authentication Beacon: {self.auth_beacon}\n"
        return msg

    def _get_data_for_signature(self) -> bytes:
        """Collects the data for signature in bytes format."""
        data = self._get_common_data()
        data += self.dac.challenge
        return data

    def _get_signature(self) -> bytes:
        assert self.sig_provider, f"Signature provider is not set"
        signature = self.sig_provider.sign(self._get_data_for_signature())
        assert signature
        return signature

    def export(self) -> bytes:
        """Export to binary form (serialization).

        :return: the exported bytes from object
        """
        data = self._get_common_data()
        data += self._get_signature()
        return data

    def _get_common_data(self) -> bytes:
        """Collects dc, auth_beacon and in case of n4analog devices - UUID."""
        data = self.debug_credential.export()
        data += pack("<L", self.auth_beacon)
        if self.is_n4analog:
            data += pack("<16s", self.debug_credential.uuid)
        return data

    @classmethod
    def parse(cls,
              data: bytes,
              offset: int = 0) -> 'DebugAuthenticateResponse':
        """Parse the DAR.

        :param data: Raw data as bytes
        :param offset: Offset of input data
        :return: DebugAuthenticateResponse object
        """
        raise NotImplementedError

    @classmethod
    def _get_class(cls, version: str,
                   socc: int) -> 'Type[DebugAuthenticateResponse]':
        if socc == 4:
            return _n4analog_version_mapping[version]
        return _version_mapping[version]

    @classmethod
    def create(cls, version: str, socc: int, dc: DebugCredential,
               auth_beacon: int, dac: DebugAuthenticationChallenge,
               dck: str) -> 'DebugAuthenticateResponse':
        """Create a dar object out of input parameters.

        :param version: protocol version
        :param socc: SoC Class
        :param dc: debug credential object
        :param auth_beacon: authentication beacon value
        :param dac: DebugAuthenticationChallenge object
        :param dck: string containing path to dck key
        :return: DAR object
        """
        klass = DebugAuthenticateResponse._get_class(version=version,
                                                     socc=socc)
        dar_obj = klass(debug_credential=dc,
                        auth_beacon=auth_beacon,
                        dac=dac,
                        path_dck_private=dck)
        return dar_obj