1# Copyright (c) 2019-2024, Arm Limited.
2# Copyright (c) 2020, Linaro Limited
3#
4# SPDX-License-Identifier: Apache-2.0
5#
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10#     http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17
18from enum import Enum
19
20try:
21    from cbor2 import dumps
22except ImportError:
23    from cbor import dumps
24
25
26class SwComponent(int, Enum):
27    """
28    Software component property IDs specified by
29    Arm's PSA Attestation API 1.0 document.
30    """
31    TYPE = 1
32    MEASUREMENT_VALUE = 2
33    VERSION = 4
34    SIGNER_ID = 5
35    MEASUREMENT_DESCRIPTION = 6
36
37
38def create_sw_component_data(sw_type, sw_version, sw_measurement_description,
39                             sw_measurement_value, sw_signer_id):
40    # List of software component properties (Key ID + value)
41    properties = {SwComponent.TYPE: sw_type,
42                  SwComponent.VERSION: sw_version,
43                  SwComponent.SIGNER_ID: sw_signer_id,
44                  SwComponent.MEASUREMENT_DESCRIPTION: sw_measurement_description,
45                  SwComponent.MEASUREMENT_VALUE: sw_measurement_value,
46                  }
47
48    # Note: The measurement value must be the last item of the property
49    #       list because later it will be modified by the bootloader.
50    last_key = list(properties.keys())[-1]
51    assert SwComponent.MEASUREMENT_VALUE == last_key, 'Measurement value is not the last item of the property list'
52    return dumps(properties)
53