1 /*
2  * Copyright (c) 2018-2022, Arm Limited. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  */
7 
8 #include <stddef.h>
9 #include <stdint.h>
10 #include <string.h>
11 #include "tfm_attest_hal.h"
12 #include "tfm_plat_boot_seed.h"
13 #include "tfm_plat_device_id.h"
14 #include "tfm_plat_otp.h"
15 #include "tfm_strnlen.h"
16 
map_otp_lcs_to_tfm_slc(enum plat_otp_lcs_t lcs)17 static enum tfm_security_lifecycle_t map_otp_lcs_to_tfm_slc(enum plat_otp_lcs_t lcs)
18 {
19     switch (lcs) {
20     case PLAT_OTP_LCS_ASSEMBLY_AND_TEST:
21         return TFM_SLC_ASSEMBLY_AND_TEST;
22     case PLAT_OTP_LCS_PSA_ROT_PROVISIONING:
23         return TFM_SLC_PSA_ROT_PROVISIONING;
24     case PLAT_OTP_LCS_SECURED:
25         return TFM_SLC_SECURED;
26     case PLAT_OTP_LCS_DECOMMISSIONED:
27         return TFM_SLC_DECOMMISSIONED;
28     case PLAT_OTP_LCS_UNKNOWN:
29     default:
30         return TFM_SLC_UNKNOWN;
31     }
32 }
33 
tfm_attest_hal_get_security_lifecycle(void)34 enum tfm_security_lifecycle_t tfm_attest_hal_get_security_lifecycle(void)
35 {
36     enum plat_otp_lcs_t otp_lcs;
37     enum tfm_plat_err_t err;
38 
39     err = tfm_plat_otp_read(PLAT_OTP_ID_LCS, sizeof(otp_lcs), (uint8_t*)&otp_lcs);
40     if (err != TFM_PLAT_ERR_SUCCESS) {
41         return TFM_SLC_UNKNOWN;
42     }
43 
44     return map_otp_lcs_to_tfm_slc(otp_lcs);
45 }
46 
47 enum tfm_plat_err_t
tfm_attest_hal_get_verification_service(uint32_t * size,uint8_t * buf)48 tfm_attest_hal_get_verification_service(uint32_t *size, uint8_t *buf)
49 {
50     enum tfm_plat_err_t err;
51     size_t otp_size;
52     size_t copy_size;
53 
54     err = tfm_plat_otp_read(PLAT_OTP_ID_VERIFICATION_SERVICE_URL, *size, buf);
55     if(err != TFM_PLAT_ERR_SUCCESS) {
56         return err;
57     }
58 
59     err =  tfm_plat_otp_get_size(PLAT_OTP_ID_VERIFICATION_SERVICE_URL, &otp_size);
60     if(err != TFM_PLAT_ERR_SUCCESS) {
61         return err;
62     }
63 
64     /* Actually copied data is always the smaller */
65     copy_size = *size < otp_size ? *size : otp_size;
66     /* String content */
67     *size = tfm_strnlen((char*)buf, copy_size);
68 
69     return TFM_PLAT_ERR_SUCCESS;
70 }
71 
72 enum tfm_plat_err_t
tfm_attest_hal_get_profile_definition(uint32_t * size,uint8_t * buf)73 tfm_attest_hal_get_profile_definition(uint32_t *size, uint8_t *buf)
74 {
75     enum tfm_plat_err_t err;
76     size_t otp_size;
77     size_t copy_size;
78 
79     err = tfm_plat_otp_read(PLAT_OTP_ID_PROFILE_DEFINITION, *size, buf);
80     if(err != TFM_PLAT_ERR_SUCCESS) {
81         return err;
82     }
83 
84     err =  tfm_plat_otp_get_size(PLAT_OTP_ID_PROFILE_DEFINITION, &otp_size);
85     if(err != TFM_PLAT_ERR_SUCCESS) {
86         return err;
87     }
88 
89     /* Actually copied data is always the smaller */
90     copy_size = *size < otp_size ? *size : otp_size;
91     /* String content */
92     *size = tfm_strnlen((char*)buf, copy_size);
93 
94     return TFM_PLAT_ERR_SUCCESS;
95 }
96 
tfm_plat_get_boot_seed(uint32_t size,uint8_t * buf)97 enum tfm_plat_err_t tfm_plat_get_boot_seed(uint32_t size, uint8_t *buf)
98 {
99     return TFM_PLAT_ERR_UNSUPPORTED;
100 }
101 
tfm_plat_get_implementation_id(uint32_t * size,uint8_t * buf)102 enum tfm_plat_err_t tfm_plat_get_implementation_id(uint32_t *size,
103                                                    uint8_t  *buf)
104 {
105     enum tfm_plat_err_t err;
106     size_t otp_size;
107     size_t copy_size;
108 
109     err = tfm_plat_otp_read(PLAT_OTP_ID_IMPLEMENTATION_ID, *size, buf);
110     if(err != TFM_PLAT_ERR_SUCCESS) {
111         return err;
112     }
113 
114     err =  tfm_plat_otp_get_size(PLAT_OTP_ID_IMPLEMENTATION_ID, &otp_size);
115     if(err != TFM_PLAT_ERR_SUCCESS) {
116         return err;
117     }
118 
119     /* Actually copied data is always the smaller */
120     copy_size = *size < otp_size ? *size : otp_size;
121     /* String content */
122     *size = copy_size;
123 
124     return TFM_PLAT_ERR_SUCCESS;
125 }
126 
tfm_plat_get_cert_ref(uint32_t * size,uint8_t * buf)127 enum tfm_plat_err_t tfm_plat_get_cert_ref(uint32_t *size, uint8_t *buf)
128 {
129     return TFM_PLAT_ERR_UNSUPPORTED;
130 }
131 
tfm_attest_hal_get_platform_config(uint32_t * size,uint8_t * buf)132 enum tfm_plat_err_t tfm_attest_hal_get_platform_config(uint32_t *size,
133                                                        uint8_t  *buf)
134 {
135     uint32_t dummy_plat_config = 0xDEADBEEF;
136 
137     if (*size < sizeof(dummy_plat_config)) {
138         return TFM_PLAT_ERR_SYSTEM_ERR;
139     }
140 
141      memcpy(buf, &dummy_plat_config, sizeof(dummy_plat_config));
142      *size = sizeof(dummy_plat_config);
143 
144     return TFM_PLAT_ERR_SUCCESS;
145 }
146 
tfm_attest_hal_get_platform_hash_algo(uint32_t * size,uint8_t * buf)147 enum tfm_plat_err_t tfm_attest_hal_get_platform_hash_algo(uint32_t *size,
148                                                           uint8_t *buf)
149 {
150 #ifdef MEASUREMENT_HASH_ALGO_NAME
151     const char hash_algo[] = MEASUREMENT_HASH_ALGO_NAME;
152 #else
153     const char hash_algo[] = "not-hash-extended";
154 #endif
155 
156     if (*size < sizeof(hash_algo) - 1) {
157         return TFM_PLAT_ERR_SYSTEM_ERR;
158     }
159 
160     /* Not including the null-terminator. */
161      memcpy(buf, hash_algo, sizeof(hash_algo) - 1);
162     *size = sizeof(hash_algo) - 1;
163 
164     return TFM_PLAT_ERR_SUCCESS;
165 }
166