Home | History | Annotate | Download | only in auth
      1 /*
      2  * Copyright (c) 2015, ARM Limited and Contributors. All rights reserved.
      3  *
      4  * SPDX-License-Identifier: BSD-3-Clause
      5  */
      6 
      7 #include <assert.h>
      8 #include <auth_common.h>
      9 #include <debug.h>
     10 #include <errno.h>
     11 #include <img_parser_mod.h>
     12 #include <limits.h>
     13 #include <stdint.h>
     14 #include <string.h>
     15 
     16 extern uintptr_t __PARSER_LIB_DESCS_START__;
     17 extern uintptr_t __PARSER_LIB_DESCS_END__;
     18 #define PARSER_LIB_DESCS_START	((uintptr_t) (&__PARSER_LIB_DESCS_START__))
     19 #define PARSER_LIB_DESCS_END	((uintptr_t) (&__PARSER_LIB_DESCS_END__))
     20 static unsigned int parser_lib_indices[IMG_MAX_TYPES];
     21 static img_parser_lib_desc_t *parser_lib_descs;
     22 
     23 #define INVALID_IDX		UINT_MAX
     24 
     25 static void validate_desc(img_parser_lib_desc_t *desc)
     26 {
     27 	assert(desc != NULL);
     28 	assert(desc->init != NULL);
     29 	assert(desc->name != NULL);
     30 	assert(desc->check_integrity != NULL);
     31 	assert(desc->get_auth_param != NULL);
     32 }
     33 
     34 void img_parser_init(void)
     35 {
     36 	unsigned int index, mod_num;
     37 
     38 	/* Initialise internal variables to invalid state */
     39 	for (index = 0; index < IMG_MAX_TYPES; index++) {
     40 		parser_lib_indices[index] = INVALID_IDX;
     41 	}
     42 
     43 	/* Calculate how many image parsers are registered. At least one parser
     44 	 * must be present */
     45 	mod_num = PARSER_LIB_DESCS_END - PARSER_LIB_DESCS_START;
     46 	mod_num /= sizeof(img_parser_lib_desc_t);
     47 	assert(mod_num > 0);
     48 
     49 	parser_lib_descs = (img_parser_lib_desc_t *) PARSER_LIB_DESCS_START;
     50 	for (index = 0; index < mod_num; index++) {
     51 
     52 		/* Check that the image parser library descriptor is valid */
     53 		validate_desc(&parser_lib_descs[index]);
     54 
     55 		/* Initialize image parser */
     56 		parser_lib_descs[index].init();
     57 
     58 		/* Ensure only one parser is registered for each image type */
     59 		assert(parser_lib_indices[parser_lib_descs[index].img_type] ==
     60 				INVALID_IDX);
     61 
     62 		/* Keep the index of this hash calculator */
     63 		parser_lib_indices[parser_lib_descs[index].img_type] = index;
     64 	}
     65 }
     66 
     67 int img_parser_check_integrity(img_type_t img_type,
     68 			       void *img_ptr, unsigned int img_len)
     69 {
     70 	unsigned int idx;
     71 
     72 	assert(img_ptr != NULL);
     73 	assert(img_len != 0);
     74 
     75 	/* No integrity checks on raw images */
     76 	if (img_type == IMG_RAW) {
     77 		return IMG_PARSER_OK;
     78 	}
     79 
     80 	/* Find the index of the required image parser */
     81 	idx = parser_lib_indices[img_type];
     82 	assert(idx != INVALID_IDX);
     83 
     84 	/* Call the function to check the image integrity */
     85 	return parser_lib_descs[idx].check_integrity(img_ptr, img_len);
     86 }
     87 
     88 /*
     89  * Extract an authentication parameter from an image
     90  *
     91  * Parameters:
     92  *   img_type: image type (certificate, raw image, etc)
     93  *   type_desc: provides info to obtain the parameter
     94  *   img_ptr: pointer to image data
     95  *   img_len: image length
     96  *   param_ptr: [out] stores a pointer to the parameter
     97  *   param_len: [out] stores the length of the parameter
     98  */
     99 int img_parser_get_auth_param(img_type_t img_type,
    100 			      const auth_param_type_desc_t *type_desc,
    101 			      void *img_ptr, unsigned int img_len,
    102 			      void **param_ptr, unsigned int *param_len)
    103 {
    104 	unsigned int idx;
    105 
    106 	assert(type_desc != NULL);
    107 	assert(img_ptr != NULL);
    108 	assert(img_len != 0);
    109 	assert(param_ptr != NULL);
    110 	assert(param_len != NULL);
    111 
    112 	/* In a raw image we can only get the data itself */
    113 	if (img_type == IMG_RAW) {
    114 		assert(type_desc->type == AUTH_PARAM_RAW_DATA);
    115 		*param_ptr = img_ptr;
    116 		*param_len = img_len;
    117 		return IMG_PARSER_OK;
    118 	}
    119 
    120 	/* Find the index of the required image parser library */
    121 	idx = parser_lib_indices[img_type];
    122 	assert(idx != INVALID_IDX);
    123 
    124 	/* Call the function to obtain the parameter */
    125 	return parser_lib_descs[idx].get_auth_param(type_desc, img_ptr, img_len,
    126 			param_ptr, param_len);
    127 }
    128