xref: /rk3399_ARM-atf/drivers/auth/auth_mod.c (revision bde2836fcc23e8cb98dfa0250937e6d771b260b7)
1 /*
2  * Copyright (c) 2015-2019, ARM Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <assert.h>
8 #include <stdint.h>
9 #include <string.h>
10 
11 #include <platform_def.h>
12 
13 #include <common/debug.h>
14 #include <common/tbbr/cot_def.h>
15 #include <drivers/auth/auth_common.h>
16 #include <drivers/auth/auth_mod.h>
17 #include <drivers/auth/crypto_mod.h>
18 #include <drivers/auth/img_parser_mod.h>
19 #include <plat/common/platform.h>
20 
21 /* ASN.1 tags */
22 #define ASN1_INTEGER                 0x02
23 
24 #define return_if_error(rc) \
25 	do { \
26 		if (rc != 0) { \
27 			return rc; \
28 		} \
29 	} while (0)
30 
31 #pragma weak plat_set_nv_ctr2
32 
33 static int cmp_auth_param_type_desc(const auth_param_type_desc_t *a,
34 		const auth_param_type_desc_t *b)
35 {
36 	if ((a->type == b->type) && (a->cookie == b->cookie)) {
37 		return 0;
38 	}
39 	return 1;
40 }
41 
42 /*
43  * This function obtains the requested authentication parameter data from the
44  * information extracted from the parent image after its authentication.
45  */
46 static int auth_get_param(const auth_param_type_desc_t *param_type_desc,
47 			  const auth_img_desc_t *img_desc,
48 			  void **param, unsigned int *len)
49 {
50 	int i;
51 
52 	for (i = 0 ; i < COT_MAX_VERIFIED_PARAMS ; i++) {
53 		if (0 == cmp_auth_param_type_desc(param_type_desc,
54 				img_desc->authenticated_data[i].type_desc)) {
55 			*param = img_desc->authenticated_data[i].data.ptr;
56 			*len = img_desc->authenticated_data[i].data.len;
57 			return 0;
58 		}
59 	}
60 
61 	return 1;
62 }
63 
64 /*
65  * Authenticate an image by matching the data hash
66  *
67  * This function implements 'AUTH_METHOD_HASH'. To authenticate an image using
68  * this method, the image must contain:
69  *
70  *   - The data to calculate the hash from
71  *
72  * The parent image must contain:
73  *
74  *   - The hash to be matched with (including hash algorithm)
75  *
76  * For a successful authentication, both hashes must match. The function calls
77  * the crypto-module to check this matching.
78  *
79  * Parameters:
80  *   param: parameters to perform the hash authentication
81  *   img_desc: pointer to image descriptor so we can know the image type
82  *             and parent image
83  *   img: pointer to image in memory
84  *   img_len: length of image (in bytes)
85  *
86  * Return:
87  *   0 = success, Otherwise = error
88  */
89 static int auth_hash(const auth_method_param_hash_t *param,
90 		     const auth_img_desc_t *img_desc,
91 		     void *img, unsigned int img_len)
92 {
93 	void *data_ptr, *hash_der_ptr;
94 	unsigned int data_len, hash_der_len;
95 	int rc = 0;
96 
97 	/* Get the hash from the parent image. This hash will be DER encoded
98 	 * and contain the hash algorithm */
99 	rc = auth_get_param(param->hash, img_desc->parent,
100 			&hash_der_ptr, &hash_der_len);
101 	return_if_error(rc);
102 
103 	/* Get the data to be hashed from the current image */
104 	rc = img_parser_get_auth_param(img_desc->img_type, param->data,
105 			img, img_len, &data_ptr, &data_len);
106 	return_if_error(rc);
107 
108 	/* Ask the crypto module to verify this hash */
109 	rc = crypto_mod_verify_hash(data_ptr, data_len,
110 				    hash_der_ptr, hash_der_len);
111 
112 	return rc;
113 }
114 
115 /*
116  * Authenticate by digital signature
117  *
118  * This function implements 'AUTH_METHOD_SIG'. To authenticate an image using
119  * this method, the image must contain:
120  *
121  *   - Data to be signed
122  *   - Signature
123  *   - Signature algorithm
124  *
125  * We rely on the image parser module to extract this data from the image.
126  * The parent image must contain:
127  *
128  *   - Public key (or a hash of it)
129  *
130  * If the parent image contains only a hash of the key, we will try to obtain
131  * the public key from the image itself (i.e. self-signed certificates). In that
132  * case, the signature verification is considered just an integrity check and
133  * the authentication is established by calculating the hash of the key and
134  * comparing it with the hash obtained from the parent.
135  *
136  * If the image has no parent (NULL), it means it has to be authenticated using
137  * the ROTPK stored in the platform. Again, this ROTPK could be the key itself
138  * or a hash of it.
139  *
140  * Return: 0 = success, Otherwise = error
141  */
142 static int auth_signature(const auth_method_param_sig_t *param,
143 			  const auth_img_desc_t *img_desc,
144 			  void *img, unsigned int img_len)
145 {
146 	void *data_ptr, *pk_ptr, *pk_hash_ptr, *sig_ptr, *sig_alg_ptr;
147 	unsigned int data_len, pk_len, pk_hash_len, sig_len, sig_alg_len;
148 	unsigned int flags = 0;
149 	int rc = 0;
150 
151 	/* Get the data to be signed from current image */
152 	rc = img_parser_get_auth_param(img_desc->img_type, param->data,
153 			img, img_len, &data_ptr, &data_len);
154 	return_if_error(rc);
155 
156 	/* Get the signature from current image */
157 	rc = img_parser_get_auth_param(img_desc->img_type, param->sig,
158 			img, img_len, &sig_ptr, &sig_len);
159 	return_if_error(rc);
160 
161 	/* Get the signature algorithm from current image */
162 	rc = img_parser_get_auth_param(img_desc->img_type, param->alg,
163 			img, img_len, &sig_alg_ptr, &sig_alg_len);
164 	return_if_error(rc);
165 
166 	/* Get the public key from the parent. If there is no parent (NULL),
167 	 * the certificate has been signed with the ROTPK, so we have to get
168 	 * the PK from the platform */
169 	if (img_desc->parent) {
170 		rc = auth_get_param(param->pk, img_desc->parent,
171 				&pk_ptr, &pk_len);
172 	} else {
173 		rc = plat_get_rotpk_info(param->pk->cookie, &pk_ptr, &pk_len,
174 				&flags);
175 	}
176 	return_if_error(rc);
177 
178 	if (flags & (ROTPK_IS_HASH | ROTPK_NOT_DEPLOYED)) {
179 		/* If the PK is a hash of the key or if the ROTPK is not
180 		   deployed on the platform, retrieve the key from the image */
181 		pk_hash_ptr = pk_ptr;
182 		pk_hash_len = pk_len;
183 		rc = img_parser_get_auth_param(img_desc->img_type,
184 					param->pk, img, img_len,
185 					&pk_ptr, &pk_len);
186 		return_if_error(rc);
187 
188 		/* Ask the crypto module to verify the signature */
189 		rc = crypto_mod_verify_signature(data_ptr, data_len,
190 						 sig_ptr, sig_len,
191 						 sig_alg_ptr, sig_alg_len,
192 						 pk_ptr, pk_len);
193 		return_if_error(rc);
194 
195 		if (flags & ROTPK_NOT_DEPLOYED) {
196 			NOTICE("ROTPK is not deployed on platform. "
197 				"Skipping ROTPK verification.\n");
198 		} else {
199 			/* Ask the crypto-module to verify the key hash */
200 			rc = crypto_mod_verify_hash(pk_ptr, pk_len,
201 				    pk_hash_ptr, pk_hash_len);
202 		}
203 	} else {
204 		/* Ask the crypto module to verify the signature */
205 		rc = crypto_mod_verify_signature(data_ptr, data_len,
206 						 sig_ptr, sig_len,
207 						 sig_alg_ptr, sig_alg_len,
208 						 pk_ptr, pk_len);
209 	}
210 
211 	return rc;
212 }
213 
214 /*
215  * Authenticate by Non-Volatile counter
216  *
217  * To protect the system against rollback, the platform includes a non-volatile
218  * counter whose value can only be increased. All certificates include a counter
219  * value that should not be lower than the value stored in the platform. If the
220  * value is larger, the counter in the platform must be updated to the new
221  * value.
222  *
223  * Return: 0 = success, Otherwise = error
224  */
225 static int auth_nvctr(const auth_method_param_nv_ctr_t *param,
226 		      const auth_img_desc_t *img_desc,
227 		      void *img, unsigned int img_len)
228 {
229 	char *p;
230 	void *data_ptr = NULL;
231 	unsigned int data_len, len, i;
232 	unsigned int cert_nv_ctr, plat_nv_ctr;
233 	int rc = 0;
234 
235 	/* Get the counter value from current image. The AM expects the IPM
236 	 * to return the counter value as a DER encoded integer */
237 	rc = img_parser_get_auth_param(img_desc->img_type, param->cert_nv_ctr,
238 				       img, img_len, &data_ptr, &data_len);
239 	return_if_error(rc);
240 
241 	/* Parse the DER encoded integer */
242 	assert(data_ptr);
243 	p = (char *)data_ptr;
244 	if (*p != ASN1_INTEGER) {
245 		/* Invalid ASN.1 integer */
246 		return 1;
247 	}
248 	p++;
249 
250 	/* NV-counters are unsigned integers up to 32-bit */
251 	len = (unsigned int)(*p & 0x7f);
252 	if ((*p & 0x80) || (len > 4)) {
253 		return 1;
254 	}
255 	p++;
256 
257 	/* Check the number is not negative */
258 	if (*p & 0x80) {
259 		return 1;
260 	}
261 
262 	/* Convert to unsigned int. This code is for a little-endian CPU */
263 	cert_nv_ctr = 0;
264 	for (i = 0; i < len; i++) {
265 		cert_nv_ctr = (cert_nv_ctr << 8) | *p++;
266 	}
267 
268 	/* Get the counter from the platform */
269 	rc = plat_get_nv_ctr(param->plat_nv_ctr->cookie, &plat_nv_ctr);
270 	return_if_error(rc);
271 
272 	if (cert_nv_ctr < plat_nv_ctr) {
273 		/* Invalid NV-counter */
274 		return 1;
275 	} else if (cert_nv_ctr > plat_nv_ctr) {
276 		rc = plat_set_nv_ctr2(param->plat_nv_ctr->cookie,
277 			img_desc, cert_nv_ctr);
278 		return_if_error(rc);
279 	}
280 
281 	return 0;
282 }
283 
284 int plat_set_nv_ctr2(void *cookie, const auth_img_desc_t *img_desc __unused,
285 		unsigned int nv_ctr)
286 {
287 	return plat_set_nv_ctr(cookie, nv_ctr);
288 }
289 
290 /*
291  * Return the parent id in the output parameter '*parent_id'
292  *
293  * Return value:
294  *   0 = Image has parent, 1 = Image has no parent or parent is authenticated
295  */
296 int auth_mod_get_parent_id(unsigned int img_id, unsigned int *parent_id)
297 {
298 	const auth_img_desc_t *img_desc = NULL;
299 
300 	assert(parent_id != NULL);
301 
302 	/* Get the image descriptor */
303 	img_desc = &cot_desc_ptr[img_id];
304 
305 	/* Check if the image has no parent (ROT) */
306 	if (img_desc->parent == NULL) {
307 		*parent_id = 0;
308 		return 1;
309 	}
310 
311 	/* Check if the parent has already been authenticated */
312 	if (auth_img_flags[img_desc->parent->img_id] & IMG_FLAG_AUTHENTICATED) {
313 		*parent_id = 0;
314 		return 1;
315 	}
316 
317 	*parent_id = img_desc->parent->img_id;
318 	return 0;
319 }
320 
321 /*
322  * Initialize the different modules in the authentication framework
323  */
324 void auth_mod_init(void)
325 {
326 	/* Check we have a valid CoT registered */
327 	assert(cot_desc_ptr != NULL);
328 
329 	/* Crypto module */
330 	crypto_mod_init();
331 
332 	/* Image parser module */
333 	img_parser_init();
334 }
335 
336 /*
337  * Authenticate a certificate/image
338  *
339  * Return: 0 = success, Otherwise = error
340  */
341 int auth_mod_verify_img(unsigned int img_id,
342 			void *img_ptr,
343 			unsigned int img_len)
344 {
345 	const auth_img_desc_t *img_desc = NULL;
346 	const auth_method_desc_t *auth_method = NULL;
347 	void *param_ptr;
348 	unsigned int param_len;
349 	int rc, i;
350 
351 	/* Get the image descriptor from the chain of trust */
352 	img_desc = &cot_desc_ptr[img_id];
353 
354 	/* Ask the parser to check the image integrity */
355 	rc = img_parser_check_integrity(img_desc->img_type, img_ptr, img_len);
356 	return_if_error(rc);
357 
358 	/* Authenticate the image using the methods indicated in the image
359 	 * descriptor. */
360 	for (i = 0 ; i < AUTH_METHOD_NUM ; i++) {
361 		auth_method = &img_desc->img_auth_methods[i];
362 		switch (auth_method->type) {
363 		case AUTH_METHOD_NONE:
364 			rc = 0;
365 			break;
366 		case AUTH_METHOD_HASH:
367 			rc = auth_hash(&auth_method->param.hash,
368 					img_desc, img_ptr, img_len);
369 			break;
370 		case AUTH_METHOD_SIG:
371 			rc = auth_signature(&auth_method->param.sig,
372 					img_desc, img_ptr, img_len);
373 			break;
374 		case AUTH_METHOD_NV_CTR:
375 			rc = auth_nvctr(&auth_method->param.nv_ctr,
376 					img_desc, img_ptr, img_len);
377 			break;
378 		default:
379 			/* Unknown authentication method */
380 			rc = 1;
381 			break;
382 		}
383 		return_if_error(rc);
384 	}
385 
386 	/* Extract the parameters indicated in the image descriptor to
387 	 * authenticate the children images. */
388 	for (i = 0 ; i < COT_MAX_VERIFIED_PARAMS ; i++) {
389 		if (img_desc->authenticated_data[i].type_desc == NULL) {
390 			continue;
391 		}
392 
393 		/* Get the parameter from the image parser module */
394 		rc = img_parser_get_auth_param(img_desc->img_type,
395 				img_desc->authenticated_data[i].type_desc,
396 				img_ptr, img_len, &param_ptr, &param_len);
397 		return_if_error(rc);
398 
399 		/* Check parameter size */
400 		if (param_len > img_desc->authenticated_data[i].data.len) {
401 			return 1;
402 		}
403 
404 		/* Copy the parameter for later use */
405 		memcpy((void *)img_desc->authenticated_data[i].data.ptr,
406 				(void *)param_ptr, param_len);
407 	}
408 
409 	/* Mark image as authenticated */
410 	auth_img_flags[img_desc->img_id] |= IMG_FLAG_AUTHENTICATED;
411 
412 	return 0;
413 }
414