1 /* 2 * Copyright (c) 2015, Linaro Limited 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, 9 * this list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 * POSSIBILITY OF SUCH DAMAGE. 26 */ 27 #ifndef SIGNED_HDR_H 28 #define SIGNED_HDR_H 29 30 #include <inttypes.h> 31 #include <tee_api_types.h> 32 #include <stdlib.h> 33 34 enum shdr_img_type { 35 SHDR_TA = 0, 36 SHDR_BOOTSTRAP_TA = 1, 37 }; 38 39 #define SHDR_MAGIC 0x4f545348 40 41 /** 42 * struct shdr - signed header 43 * @magic: magic number must match SHDR_MAGIC 44 * @img_type: image type, values defined by enum shdr_img_type 45 * @img_size: image size in bytes 46 * @algo: algorithm, defined by public key algorithms TEE_ALG_* 47 * from TEE Internal API specification 48 * @hash_size: size of the signed hash 49 * @sig_size: size of the signature 50 * @hash: hash of an image 51 * @sig: signature of @hash 52 */ 53 struct shdr { 54 uint32_t magic; 55 uint32_t img_type; 56 uint32_t img_size; 57 uint32_t algo; 58 uint16_t hash_size; 59 uint16_t sig_size; 60 /* 61 * Commented out element used to visualize the layout dynamic part 62 * of the struct. 63 * 64 * hash is accessed through the macro SHDR_GET_HASH and 65 * signature is accessed through the macro SHDR_GET_SIG 66 * 67 * uint8_t hash[hash_size]; 68 * uint8_t sig[sig_size]; 69 */ 70 }; 71 72 #define SHDR_GET_SIZE(x) (sizeof(struct shdr) + (x)->hash_size + \ 73 (x)->sig_size) 74 #define SHDR_GET_HASH(x) (uint8_t *)(((struct shdr *)(x)) + 1) 75 #define SHDR_GET_SIG(x) (SHDR_GET_HASH(x) + (x)->hash_size) 76 77 struct shdr_bootstrap_ta { 78 uint8_t uuid[sizeof(TEE_UUID)]; 79 uint32_t version; 80 }; 81 82 /* 83 * Allocates a struct shdr large enough to hold the entire header, 84 * excluding a subheader like struct shdr_bootstrap_ta. 85 */ 86 struct shdr *shdr_alloc_and_copy(const struct shdr *img, size_t img_size); 87 88 /* Frees a previously allocated struct shdr */ 89 static inline void shdr_free(struct shdr *shdr) 90 { 91 free(shdr); 92 } 93 94 /* 95 * Verifies the signature in the @shdr. 96 * 97 * Note that the static part of struct shdr and payload still need to be 98 * checked against the hash contained in the header. 99 * 100 * Returns TEE_SUCCESS on success or TEE_ERROR_SECURITY on failure 101 */ 102 TEE_Result shdr_verify_signature(const struct shdr *shdr); 103 104 #endif /*SIGNED_HDR_H*/ 105