1 /*
2  * Copyright (c) 2022-2024, Arm Limited and Contributors. All rights reserved.
3  * Copyright (c) 2024, Linaro Limited and Contributors. All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  */
7 
8 #include <errno.h>
9 #include <string.h>
10 
11 #include <plat/common/platform.h>
12 
13 /*
14  * This is the CBOR serialization of the CCA platform token described at
15  * https://git.trustedfirmware.org/TF-M/tf-m-tools/+/refs/heads/main/iat-verifier/tests/data/cca_example_platform_token.yaml
16  */
17 static const uint8_t sample_platform_token[] = {
18 	0xd2, 0x84, 0x44, 0xa1, 0x01, 0x38, 0x22, 0xa0,
19 	0x59, 0x05, 0x81, 0xa9, 0x19, 0x01, 0x09, 0x78,
20 	0x23, 0x74, 0x61, 0x67, 0x3a, 0x61, 0x72, 0x6d,
21 	0x2e, 0x63, 0x6f, 0x6d, 0x2c, 0x32, 0x30, 0x32,
22 	0x33, 0x3a, 0x63, 0x63, 0x61, 0x5f, 0x70, 0x6c,
23 	0x61, 0x74, 0x66, 0x6f, 0x72, 0x6d, 0x23, 0x31,
24 	0x2e, 0x30, 0x2e, 0x30, 0x0a, 0x58, 0x20, 0x0d,
25 	0x22, 0xe0, 0x8a, 0x98, 0x46, 0x90, 0x58, 0x48,
26 	0x63, 0x18, 0x28, 0x34, 0x89, 0xbd, 0xb3, 0x6f,
27 	0x09, 0xdb, 0xef, 0xeb, 0x18, 0x64, 0xdf, 0x43,
28 	0x3f, 0xa6, 0xe5, 0x4e, 0xa2, 0xd7, 0x11, 0x19,
29 	0x09, 0x5c, 0x58, 0x20, 0x7f, 0x45, 0x4c, 0x46,
30 	0x02, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
31 	0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x3e, 0x00,
32 	0x01, 0x00, 0x00, 0x00, 0x50, 0x58, 0x00, 0x00,
33 	0x00, 0x00, 0x00, 0x00, 0x19, 0x01, 0x00, 0x58,
34 	0x21, 0x01, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02,
35 	0x01, 0x00, 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a,
36 	0x09, 0x08, 0x17, 0x16, 0x15, 0x14, 0x13, 0x12,
37 	0x11, 0x10, 0x1f, 0x1e, 0x1d, 0x1c, 0x1b, 0x1a,
38 	0x19, 0x18, 0x19, 0x09, 0x61, 0x44, 0xcf, 0xcf,
39 	0xcf, 0xcf, 0x19, 0x09, 0x5b, 0x19, 0x30, 0x03,
40 	0x19, 0x09, 0x62, 0x67, 0x73, 0x68, 0x61, 0x2d,
41 	0x32, 0x35, 0x36, 0x19, 0x09, 0x60, 0x78, 0x3a,
42 	0x68, 0x74, 0x74, 0x70, 0x73, 0x3a, 0x2f, 0x2f,
43 	0x76, 0x65, 0x72, 0x61, 0x69, 0x73, 0x6f, 0x6e,
44 	0x2e, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65,
45 	0x2f, 0x2e, 0x77, 0x65, 0x6c, 0x6c, 0x2d, 0x6b,
46 	0x6e, 0x6f, 0x77, 0x6e, 0x2f, 0x76, 0x65, 0x72,
47 	0x61, 0x69, 0x73, 0x6f, 0x6e, 0x2f, 0x76, 0x65,
48 	0x72, 0x69, 0x66, 0x69, 0x63, 0x61, 0x74, 0x69,
49 	0x6f, 0x6e, 0x19, 0x09, 0x5f, 0x8d, 0xa4, 0x01,
50 	0x69, 0x52, 0x53, 0x45, 0x5f, 0x42, 0x4c, 0x31,
51 	0x5f, 0x32, 0x05, 0x58, 0x20, 0x53, 0x78, 0x79,
52 	0x63, 0x07, 0x53, 0x5d, 0xf3, 0xec, 0x8d, 0x8b,
53 	0x15, 0xa2, 0xe2, 0xdc, 0x56, 0x41, 0x41, 0x9c,
54 	0x3d, 0x30, 0x60, 0xcf, 0xe3, 0x22, 0x38, 0xc0,
55 	0xfa, 0x97, 0x3f, 0x7a, 0xa3, 0x02, 0x58, 0x20,
56 	0x9a, 0x27, 0x1f, 0x2a, 0x91, 0x6b, 0x0b, 0x6e,
57 	0xe6, 0xce, 0xcb, 0x24, 0x26, 0xf0, 0xb3, 0x20,
58 	0x6e, 0xf0, 0x74, 0x57, 0x8b, 0xe5, 0x5d, 0x9b,
59 	0xc9, 0x4f, 0x6f, 0x3f, 0xe3, 0xab, 0x86, 0xaa,
60 	0x06, 0x67, 0x73, 0x68, 0x61, 0x2d, 0x32, 0x35,
61 	0x36, 0xa4, 0x01, 0x67, 0x52, 0x53, 0x45, 0x5f,
62 	0x42, 0x4c, 0x32, 0x05, 0x58, 0x20, 0x53, 0x78,
63 	0x79, 0x63, 0x07, 0x53, 0x5d, 0xf3, 0xec, 0x8d,
64 	0x8b, 0x15, 0xa2, 0xe2, 0xdc, 0x56, 0x41, 0x41,
65 	0x9c, 0x3d, 0x30, 0x60, 0xcf, 0xe3, 0x22, 0x38,
66 	0xc0, 0xfa, 0x97, 0x3f, 0x7a, 0xa3, 0x02, 0x58,
67 	0x20, 0x53, 0xc2, 0x34, 0xe5, 0xe8, 0x47, 0x2b,
68 	0x6a, 0xc5, 0x1c, 0x1a, 0xe1, 0xca, 0xb3, 0xfe,
69 	0x06, 0xfa, 0xd0, 0x53, 0xbe, 0xb8, 0xeb, 0xfd,
70 	0x89, 0x77, 0xb0, 0x10, 0x65, 0x5b, 0xfd, 0xd3,
71 	0xc3, 0x06, 0x67, 0x73, 0x68, 0x61, 0x2d, 0x32,
72 	0x35, 0x36, 0xa4, 0x01, 0x65, 0x52, 0x53, 0x45,
73 	0x5f, 0x53, 0x05, 0x58, 0x20, 0x53, 0x78, 0x79,
74 	0x63, 0x07, 0x53, 0x5d, 0xf3, 0xec, 0x8d, 0x8b,
75 	0x15, 0xa2, 0xe2, 0xdc, 0x56, 0x41, 0x41, 0x9c,
76 	0x3d, 0x30, 0x60, 0xcf, 0xe3, 0x22, 0x38, 0xc0,
77 	0xfa, 0x97, 0x3f, 0x7a, 0xa3, 0x02, 0x58, 0x20,
78 	0x11, 0x21, 0xcf, 0xcc, 0xd5, 0x91, 0x3f, 0x0a,
79 	0x63, 0xfe, 0xc4, 0x0a, 0x6f, 0xfd, 0x44, 0xea,
80 	0x64, 0xf9, 0xdc, 0x13, 0x5c, 0x66, 0x63, 0x4b,
81 	0xa0, 0x01, 0xd1, 0x0b, 0xcf, 0x43, 0x02, 0xa2,
82 	0x06, 0x67, 0x73, 0x68, 0x61, 0x2d, 0x32, 0x35,
83 	0x36, 0xa4, 0x01, 0x66, 0x41, 0x50, 0x5f, 0x42,
84 	0x4c, 0x31, 0x05, 0x58, 0x20, 0x53, 0x78, 0x79,
85 	0x63, 0x07, 0x53, 0x5d, 0xf3, 0xec, 0x8d, 0x8b,
86 	0x15, 0xa2, 0xe2, 0xdc, 0x56, 0x41, 0x41, 0x9c,
87 	0x3d, 0x30, 0x60, 0xcf, 0xe3, 0x22, 0x38, 0xc0,
88 	0xfa, 0x97, 0x3f, 0x7a, 0xa3, 0x02, 0x58, 0x20,
89 	0x15, 0x71, 0xb5, 0xec, 0x78, 0xbd, 0x68, 0x51,
90 	0x2b, 0xf7, 0x83, 0x0b, 0xb6, 0xa2, 0xa4, 0x4b,
91 	0x20, 0x47, 0xc7, 0xdf, 0x57, 0xbc, 0xe7, 0x9e,
92 	0xb8, 0xa1, 0xc0, 0xe5, 0xbe, 0xa0, 0xa5, 0x01,
93 	0x06, 0x67, 0x73, 0x68, 0x61, 0x2d, 0x32, 0x35,
94 	0x36, 0xa4, 0x01, 0x66, 0x41, 0x50, 0x5f, 0x42,
95 	0x4c, 0x32, 0x05, 0x58, 0x20, 0x53, 0x78, 0x79,
96 	0x63, 0x07, 0x53, 0x5d, 0xf3, 0xec, 0x8d, 0x8b,
97 	0x15, 0xa2, 0xe2, 0xdc, 0x56, 0x41, 0x41, 0x9c,
98 	0x3d, 0x30, 0x60, 0xcf, 0xe3, 0x22, 0x38, 0xc0,
99 	0xfa, 0x97, 0x3f, 0x7a, 0xa3, 0x02, 0x58, 0x20,
100 	0x10, 0x15, 0x9b, 0xaf, 0x26, 0x2b, 0x43, 0xa9,
101 	0x2d, 0x95, 0xdb, 0x59, 0xda, 0xe1, 0xf7, 0x2c,
102 	0x64, 0x51, 0x27, 0x30, 0x16, 0x61, 0xe0, 0xa3,
103 	0xce, 0x4e, 0x38, 0xb2, 0x95, 0xa9, 0x7c, 0x58,
104 	0x06, 0x67, 0x73, 0x68, 0x61, 0x2d, 0x32, 0x35,
105 	0x36, 0xa4, 0x01, 0x67, 0x53, 0x43, 0x50, 0x5f,
106 	0x42, 0x4c, 0x31, 0x05, 0x58, 0x20, 0x53, 0x78,
107 	0x79, 0x63, 0x07, 0x53, 0x5d, 0xf3, 0xec, 0x8d,
108 	0x8b, 0x15, 0xa2, 0xe2, 0xdc, 0x56, 0x41, 0x41,
109 	0x9c, 0x3d, 0x30, 0x60, 0xcf, 0xe3, 0x22, 0x38,
110 	0xc0, 0xfa, 0x97, 0x3f, 0x7a, 0xa3, 0x02, 0x58,
111 	0x20, 0x10, 0x12, 0x2e, 0x85, 0x6b, 0x3f, 0xcd,
112 	0x49, 0xf0, 0x63, 0x63, 0x63, 0x17, 0x47, 0x61,
113 	0x49, 0xcb, 0x73, 0x0a, 0x1a, 0xa1, 0xcf, 0xaa,
114 	0xd8, 0x18, 0x55, 0x2b, 0x72, 0xf5, 0x6d, 0x6f,
115 	0x68, 0x06, 0x67, 0x73, 0x68, 0x61, 0x2d, 0x32,
116 	0x35, 0x36, 0xa4, 0x01, 0x67, 0x53, 0x43, 0x50,
117 	0x5f, 0x42, 0x4c, 0x32, 0x05, 0x58, 0x20, 0xf1,
118 	0x4b, 0x49, 0x87, 0x90, 0x4b, 0xcb, 0x58, 0x14,
119 	0xe4, 0x45, 0x9a, 0x05, 0x7e, 0xd4, 0xd2, 0x0f,
120 	0x58, 0xa6, 0x33, 0x15, 0x22, 0x88, 0xa7, 0x61,
121 	0x21, 0x4d, 0xcd, 0x28, 0x78, 0x0b, 0x56, 0x02,
122 	0x58, 0x20, 0xaa, 0x67, 0xa1, 0x69, 0xb0, 0xbb,
123 	0xa2, 0x17, 0xaa, 0x0a, 0xa8, 0x8a, 0x65, 0x34,
124 	0x69, 0x20, 0xc8, 0x4c, 0x42, 0x44, 0x7c, 0x36,
125 	0xba, 0x5f, 0x7e, 0xa6, 0x5f, 0x42, 0x2c, 0x1f,
126 	0xe5, 0xd8, 0x06, 0x67, 0x73, 0x68, 0x61, 0x2d,
127 	0x32, 0x35, 0x36, 0xa4, 0x01, 0x67, 0x41, 0x50,
128 	0x5f, 0x42, 0x4c, 0x33, 0x31, 0x05, 0x58, 0x20,
129 	0x53, 0x78, 0x79, 0x63, 0x07, 0x53, 0x5d, 0xf3,
130 	0xec, 0x8d, 0x8b, 0x15, 0xa2, 0xe2, 0xdc, 0x56,
131 	0x41, 0x41, 0x9c, 0x3d, 0x30, 0x60, 0xcf, 0xe3,
132 	0x22, 0x38, 0xc0, 0xfa, 0x97, 0x3f, 0x7a, 0xa3,
133 	0x02, 0x58, 0x20, 0x2e, 0x6d, 0x31, 0xa5, 0x98,
134 	0x3a, 0x91, 0x25, 0x1b, 0xfa, 0xe5, 0xae, 0xfa,
135 	0x1c, 0x0a, 0x19, 0xd8, 0xba, 0x3c, 0xf6, 0x01,
136 	0xd0, 0xe8, 0xa7, 0x06, 0xb4, 0xcf, 0xa9, 0x66,
137 	0x1a, 0x6b, 0x8a, 0x06, 0x67, 0x73, 0x68, 0x61,
138 	0x2d, 0x32, 0x35, 0x36, 0xa4, 0x01, 0x63, 0x52,
139 	0x4d, 0x4d, 0x05, 0x58, 0x20, 0x53, 0x78, 0x79,
140 	0x63, 0x07, 0x53, 0x5d, 0xf3, 0xec, 0x8d, 0x8b,
141 	0x15, 0xa2, 0xe2, 0xdc, 0x56, 0x41, 0x41, 0x9c,
142 	0x3d, 0x30, 0x60, 0xcf, 0xe3, 0x22, 0x38, 0xc0,
143 	0xfa, 0x97, 0x3f, 0x7a, 0xa3, 0x02, 0x58, 0x20,
144 	0xa1, 0xfb, 0x50, 0xe6, 0xc8, 0x6f, 0xae, 0x16,
145 	0x79, 0xef, 0x33, 0x51, 0x29, 0x6f, 0xd6, 0x71,
146 	0x34, 0x11, 0xa0, 0x8c, 0xf8, 0xdd, 0x17, 0x90,
147 	0xa4, 0xfd, 0x05, 0xfa, 0xe8, 0x68, 0x81, 0x64,
148 	0x06, 0x67, 0x73, 0x68, 0x61, 0x2d, 0x32, 0x35,
149 	0x36, 0xa4, 0x01, 0x69, 0x48, 0x57, 0x5f, 0x43,
150 	0x4f, 0x4e, 0x46, 0x49, 0x47, 0x05, 0x58, 0x20,
151 	0x53, 0x78, 0x79, 0x63, 0x07, 0x53, 0x5d, 0xf3,
152 	0xec, 0x8d, 0x8b, 0x15, 0xa2, 0xe2, 0xdc, 0x56,
153 	0x41, 0x41, 0x9c, 0x3d, 0x30, 0x60, 0xcf, 0xe3,
154 	0x22, 0x38, 0xc0, 0xfa, 0x97, 0x3f, 0x7a, 0xa3,
155 	0x02, 0x58, 0x20, 0x1a, 0x25, 0x24, 0x02, 0x97,
156 	0x2f, 0x60, 0x57, 0xfa, 0x53, 0xcc, 0x17, 0x2b,
157 	0x52, 0xb9, 0xff, 0xca, 0x69, 0x8e, 0x18, 0x31,
158 	0x1f, 0xac, 0xd0, 0xf3, 0xb0, 0x6e, 0xca, 0xae,
159 	0xf7, 0x9e, 0x17, 0x06, 0x67, 0x73, 0x68, 0x61,
160 	0x2d, 0x32, 0x35, 0x36, 0xa4, 0x01, 0x69, 0x46,
161 	0x57, 0x5f, 0x43, 0x4f, 0x4e, 0x46, 0x49, 0x47,
162 	0x05, 0x58, 0x20, 0x53, 0x78, 0x79, 0x63, 0x07,
163 	0x53, 0x5d, 0xf3, 0xec, 0x8d, 0x8b, 0x15, 0xa2,
164 	0xe2, 0xdc, 0x56, 0x41, 0x41, 0x9c, 0x3d, 0x30,
165 	0x60, 0xcf, 0xe3, 0x22, 0x38, 0xc0, 0xfa, 0x97,
166 	0x3f, 0x7a, 0xa3, 0x02, 0x58, 0x20, 0x9a, 0x92,
167 	0xad, 0xbc, 0x0c, 0xee, 0x38, 0xef, 0x65, 0x8c,
168 	0x71, 0xce, 0x1b, 0x1b, 0xf8, 0xc6, 0x56, 0x68,
169 	0xf1, 0x66, 0xbf, 0xb2, 0x13, 0x64, 0x4c, 0x89,
170 	0x5c, 0xcb, 0x1a, 0xd0, 0x7a, 0x25, 0x06, 0x67,
171 	0x73, 0x68, 0x61, 0x2d, 0x32, 0x35, 0x36, 0xa4,
172 	0x01, 0x6c, 0x54, 0x42, 0x5f, 0x46, 0x57, 0x5f,
173 	0x43, 0x4f, 0x4e, 0x46, 0x49, 0x47, 0x05, 0x58,
174 	0x20, 0x53, 0x78, 0x79, 0x63, 0x07, 0x53, 0x5d,
175 	0xf3, 0xec, 0x8d, 0x8b, 0x15, 0xa2, 0xe2, 0xdc,
176 	0x56, 0x41, 0x41, 0x9c, 0x3d, 0x30, 0x60, 0xcf,
177 	0xe3, 0x22, 0x38, 0xc0, 0xfa, 0x97, 0x3f, 0x7a,
178 	0xa3, 0x02, 0x58, 0x20, 0x23, 0x89, 0x03, 0x18,
179 	0x0c, 0xc1, 0x04, 0xec, 0x2c, 0x5d, 0x8b, 0x3f,
180 	0x20, 0xc5, 0xbc, 0x61, 0xb3, 0x89, 0xec, 0x0a,
181 	0x96, 0x7d, 0xf8, 0xcc, 0x20, 0x8c, 0xdc, 0x7c,
182 	0xd4, 0x54, 0x17, 0x4f, 0x06, 0x67, 0x73, 0x68,
183 	0x61, 0x2d, 0x32, 0x35, 0x36, 0xa4, 0x01, 0x6d,
184 	0x53, 0x4f, 0x43, 0x5f, 0x46, 0x57, 0x5f, 0x43,
185 	0x4f, 0x4e, 0x46, 0x49, 0x47, 0x05, 0x58, 0x20,
186 	0x53, 0x78, 0x79, 0x63, 0x07, 0x53, 0x5d, 0xf3,
187 	0xec, 0x8d, 0x8b, 0x15, 0xa2, 0xe2, 0xdc, 0x56,
188 	0x41, 0x41, 0x9c, 0x3d, 0x30, 0x60, 0xcf, 0xe3,
189 	0x22, 0x38, 0xc0, 0xfa, 0x97, 0x3f, 0x7a, 0xa3,
190 	0x02, 0x58, 0x20, 0xe6, 0xc2, 0x1e, 0x8d, 0x26,
191 	0x0f, 0xe7, 0x18, 0x82, 0xde, 0xbd, 0xb3, 0x39,
192 	0xd2, 0x40, 0x2a, 0x2c, 0xa7, 0x64, 0x85, 0x29,
193 	0xbc, 0x23, 0x03, 0xf4, 0x86, 0x49, 0xbc, 0xe0,
194 	0x38, 0x00, 0x17, 0x06, 0x67, 0x73, 0x68, 0x61,
195 	0x2d, 0x32, 0x35, 0x36, 0x58, 0x60, 0x31, 0xd0,
196 	0x4d, 0x52, 0xcc, 0xde, 0x95, 0x2c, 0x1e, 0x32,
197 	0xcb, 0xa1, 0x81, 0x88, 0x5a, 0x40, 0xb8, 0xcc,
198 	0x38, 0xe0, 0x52, 0x8c, 0x1e, 0x89, 0x58, 0x98,
199 	0x07, 0x64, 0x2a, 0xa5, 0xe3, 0xf2, 0xbc, 0x37,
200 	0xf9, 0x53, 0x74, 0x50, 0x6b, 0xff, 0x4d, 0x2e,
201 	0x4b, 0xe7, 0x06, 0x3c, 0x4d, 0x72, 0x41, 0x92,
202 	0x70, 0xc7, 0x22, 0xe8, 0xd4, 0xd9, 0x3e, 0xe8,
203 	0xb6, 0xc9, 0xfa, 0xce, 0x3b, 0x43, 0xc9, 0x76,
204 	0x1a, 0x49, 0x94, 0x1a, 0xb6, 0xf3, 0x8f, 0xfd,
205 	0xff, 0x49, 0x6a, 0xd4, 0x63, 0xb4, 0xcb, 0xfa,
206 	0x11, 0xd8, 0x3e, 0x23, 0xe3, 0x1f, 0x7f, 0x62,
207 	0x32, 0x9d, 0xe3, 0x0c, 0x1c, 0xc8
208 };
209 static uint64_t platform_token_offset;
210 
211 /*
212  * Get the hardcoded platform attestation token as FVP does not support
213  * RSE.
214  *
215  * Note: This implementation caters for retrieval of the platform token
216  * in hunks to facilitate EL3-RMM interface testing. For most platforms,
217  * since the shared buffer size is known, the implementation can be more
218  * optimized.
219  */
plat_rmmd_get_cca_attest_token(uintptr_t buf,size_t * len,uintptr_t hash,size_t hash_size,size_t * remaining_len)220 int plat_rmmd_get_cca_attest_token(uintptr_t buf, size_t *len,
221 				   uintptr_t hash, size_t hash_size,
222 				   size_t *remaining_len)
223 {
224 	(void)hash;
225 	(void)hash_size;
226 	size_t platform_token_size = sizeof(sample_platform_token);
227 	size_t local_hunk_len;
228 	size_t local_remaining_len;
229 
230 	if (hash_size != 0) {
231 		platform_token_offset = 0;
232 	} else if (platform_token_offset == 0) {
233 		return -EINVAL;
234 	}
235 
236 	local_hunk_len = *len;
237 	local_remaining_len = platform_token_size - platform_token_offset;
238 
239 	/*
240 	 * If the buffer is enough to fit the remaining bytes of the token,
241 	 * return only the remaining bytes of the token.
242 	 */
243 	if (local_hunk_len >= local_remaining_len) {
244 		local_hunk_len = local_remaining_len;
245 	}
246 	/* Update remaining bytes according to hunk size */
247 	local_remaining_len -= local_hunk_len;
248 
249 	(void)memcpy((void *)buf,
250 			(const void *)sample_platform_token
251 				+ platform_token_offset,
252 			local_hunk_len);
253 
254 	platform_token_offset += local_hunk_len;
255 	*len = local_hunk_len;
256 	*remaining_len = local_remaining_len;
257 
258 	return 0;
259 }
260