1*05799ae0SJuan Castillo /* 2*05799ae0SJuan Castillo * Copyright (c) 2015, ARM Limited and Contributors. All rights reserved. 3*05799ae0SJuan Castillo * 4*05799ae0SJuan Castillo * Redistribution and use in source and binary forms, with or without 5*05799ae0SJuan Castillo * modification, are permitted provided that the following conditions are met: 6*05799ae0SJuan Castillo * 7*05799ae0SJuan Castillo * Redistributions of source code must retain the above copyright notice, this 8*05799ae0SJuan Castillo * list of conditions and the following disclaimer. 9*05799ae0SJuan Castillo * 10*05799ae0SJuan Castillo * Redistributions in binary form must reproduce the above copyright notice, 11*05799ae0SJuan Castillo * this list of conditions and the following disclaimer in the documentation 12*05799ae0SJuan Castillo * and/or other materials provided with the distribution. 13*05799ae0SJuan Castillo * 14*05799ae0SJuan Castillo * Neither the name of ARM nor the names of its contributors may be used 15*05799ae0SJuan Castillo * to endorse or promote products derived from this software without specific 16*05799ae0SJuan Castillo * prior written permission. 17*05799ae0SJuan Castillo * 18*05799ae0SJuan Castillo * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19*05799ae0SJuan Castillo * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20*05799ae0SJuan Castillo * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21*05799ae0SJuan Castillo * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 22*05799ae0SJuan Castillo * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23*05799ae0SJuan Castillo * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24*05799ae0SJuan Castillo * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25*05799ae0SJuan Castillo * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26*05799ae0SJuan Castillo * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27*05799ae0SJuan Castillo * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28*05799ae0SJuan Castillo * POSSIBILITY OF SUCH DAMAGE. 29*05799ae0SJuan Castillo */ 30*05799ae0SJuan Castillo 31*05799ae0SJuan Castillo #include <assert.h> 32*05799ae0SJuan Castillo #include <auth_common.h> 33*05799ae0SJuan Castillo #include <debug.h> 34*05799ae0SJuan Castillo #include <errno.h> 35*05799ae0SJuan Castillo #include <img_parser_mod.h> 36*05799ae0SJuan Castillo #include <limits.h> 37*05799ae0SJuan Castillo #include <stdint.h> 38*05799ae0SJuan Castillo #include <string.h> 39*05799ae0SJuan Castillo 40*05799ae0SJuan Castillo extern uintptr_t __PARSER_LIB_DESCS_START__; 41*05799ae0SJuan Castillo extern uintptr_t __PARSER_LIB_DESCS_END__; 42*05799ae0SJuan Castillo #define PARSER_LIB_DESCS_START ((uintptr_t) (&__PARSER_LIB_DESCS_START__)) 43*05799ae0SJuan Castillo #define PARSER_LIB_DESCS_END ((uintptr_t) (&__PARSER_LIB_DESCS_END__)) 44*05799ae0SJuan Castillo static unsigned int parser_lib_indices[IMG_MAX_TYPES]; 45*05799ae0SJuan Castillo static img_parser_lib_desc_t *parser_lib_descs; 46*05799ae0SJuan Castillo 47*05799ae0SJuan Castillo #define INVALID_IDX UINT_MAX 48*05799ae0SJuan Castillo 49*05799ae0SJuan Castillo static void validate_desc(img_parser_lib_desc_t *desc) 50*05799ae0SJuan Castillo { 51*05799ae0SJuan Castillo assert(desc != NULL); 52*05799ae0SJuan Castillo assert(desc->init != NULL); 53*05799ae0SJuan Castillo assert(desc->name != NULL); 54*05799ae0SJuan Castillo assert(desc->check_integrity != NULL); 55*05799ae0SJuan Castillo assert(desc->get_auth_param != NULL); 56*05799ae0SJuan Castillo } 57*05799ae0SJuan Castillo 58*05799ae0SJuan Castillo void img_parser_init(void) 59*05799ae0SJuan Castillo { 60*05799ae0SJuan Castillo unsigned int index, mod_num; 61*05799ae0SJuan Castillo 62*05799ae0SJuan Castillo /* Initialise internal variables to invalid state */ 63*05799ae0SJuan Castillo for (index = 0; index < IMG_MAX_TYPES; index++) { 64*05799ae0SJuan Castillo parser_lib_indices[index] = INVALID_IDX; 65*05799ae0SJuan Castillo } 66*05799ae0SJuan Castillo 67*05799ae0SJuan Castillo /* Calculate how many image parsers are registered. At least one parser 68*05799ae0SJuan Castillo * must be present */ 69*05799ae0SJuan Castillo mod_num = PARSER_LIB_DESCS_END - PARSER_LIB_DESCS_START; 70*05799ae0SJuan Castillo mod_num /= sizeof(img_parser_lib_desc_t); 71*05799ae0SJuan Castillo assert(mod_num > 0); 72*05799ae0SJuan Castillo 73*05799ae0SJuan Castillo parser_lib_descs = (img_parser_lib_desc_t *) PARSER_LIB_DESCS_START; 74*05799ae0SJuan Castillo for (index = 0; index < mod_num; index++) { 75*05799ae0SJuan Castillo 76*05799ae0SJuan Castillo /* Check that the image parser library descriptor is valid */ 77*05799ae0SJuan Castillo validate_desc(&parser_lib_descs[index]); 78*05799ae0SJuan Castillo 79*05799ae0SJuan Castillo /* Initialize image parser */ 80*05799ae0SJuan Castillo parser_lib_descs[index].init(); 81*05799ae0SJuan Castillo 82*05799ae0SJuan Castillo /* Ensure only one parser is registered for each image type */ 83*05799ae0SJuan Castillo assert(parser_lib_indices[parser_lib_descs[index].img_type] == 84*05799ae0SJuan Castillo INVALID_IDX); 85*05799ae0SJuan Castillo 86*05799ae0SJuan Castillo /* Keep the index of this hash calculator */ 87*05799ae0SJuan Castillo parser_lib_indices[parser_lib_descs[index].img_type] = index; 88*05799ae0SJuan Castillo } 89*05799ae0SJuan Castillo } 90*05799ae0SJuan Castillo 91*05799ae0SJuan Castillo int img_parser_check_integrity(img_type_t img_type, 92*05799ae0SJuan Castillo void *img_ptr, unsigned int img_len) 93*05799ae0SJuan Castillo { 94*05799ae0SJuan Castillo unsigned int idx; 95*05799ae0SJuan Castillo 96*05799ae0SJuan Castillo assert(img_ptr != NULL); 97*05799ae0SJuan Castillo assert(img_len != 0); 98*05799ae0SJuan Castillo 99*05799ae0SJuan Castillo /* No integrity checks on raw images */ 100*05799ae0SJuan Castillo if (img_type == IMG_RAW) { 101*05799ae0SJuan Castillo return IMG_PARSER_OK; 102*05799ae0SJuan Castillo } 103*05799ae0SJuan Castillo 104*05799ae0SJuan Castillo /* Find the index of the required image parser */ 105*05799ae0SJuan Castillo idx = parser_lib_indices[img_type]; 106*05799ae0SJuan Castillo assert(idx != INVALID_IDX); 107*05799ae0SJuan Castillo 108*05799ae0SJuan Castillo /* Call the function to check the image integrity */ 109*05799ae0SJuan Castillo return parser_lib_descs[idx].check_integrity(img_ptr, img_len); 110*05799ae0SJuan Castillo } 111*05799ae0SJuan Castillo 112*05799ae0SJuan Castillo /* 113*05799ae0SJuan Castillo * Extract an authentication parameter from an image 114*05799ae0SJuan Castillo * 115*05799ae0SJuan Castillo * Parameters: 116*05799ae0SJuan Castillo * img_type: image type (certificate, raw image, etc) 117*05799ae0SJuan Castillo * type_desc: provides info to obtain the parameter 118*05799ae0SJuan Castillo * img_ptr: pointer to image data 119*05799ae0SJuan Castillo * img_len: image length 120*05799ae0SJuan Castillo * param_ptr: [out] stores a pointer to the parameter 121*05799ae0SJuan Castillo * param_len: [out] stores the length of the parameter 122*05799ae0SJuan Castillo */ 123*05799ae0SJuan Castillo int img_parser_get_auth_param(img_type_t img_type, 124*05799ae0SJuan Castillo const auth_param_type_desc_t *type_desc, 125*05799ae0SJuan Castillo void *img_ptr, unsigned int img_len, 126*05799ae0SJuan Castillo void **param_ptr, unsigned int *param_len) 127*05799ae0SJuan Castillo { 128*05799ae0SJuan Castillo unsigned int idx; 129*05799ae0SJuan Castillo 130*05799ae0SJuan Castillo assert(type_desc != NULL); 131*05799ae0SJuan Castillo assert(img_ptr != NULL); 132*05799ae0SJuan Castillo assert(img_len != 0); 133*05799ae0SJuan Castillo assert(param_ptr != NULL); 134*05799ae0SJuan Castillo assert(param_len != NULL); 135*05799ae0SJuan Castillo 136*05799ae0SJuan Castillo /* In a raw image we can only get the data itself */ 137*05799ae0SJuan Castillo if (img_type == IMG_RAW) { 138*05799ae0SJuan Castillo assert(type_desc->type == AUTH_PARAM_RAW_DATA); 139*05799ae0SJuan Castillo *param_ptr = img_ptr; 140*05799ae0SJuan Castillo *param_len = img_len; 141*05799ae0SJuan Castillo return IMG_PARSER_OK; 142*05799ae0SJuan Castillo } 143*05799ae0SJuan Castillo 144*05799ae0SJuan Castillo /* Find the index of the required image parser library */ 145*05799ae0SJuan Castillo idx = parser_lib_indices[img_type]; 146*05799ae0SJuan Castillo assert(idx != INVALID_IDX); 147*05799ae0SJuan Castillo 148*05799ae0SJuan Castillo /* Call the function to obtain the parameter */ 149*05799ae0SJuan Castillo return parser_lib_descs[idx].get_auth_param(type_desc, img_ptr, img_len, 150*05799ae0SJuan Castillo param_ptr, param_len); 151*05799ae0SJuan Castillo } 152