11bb92983SJerome Forissier /* SPDX-License-Identifier: BSD-2-Clause */ 2bc420748SJens Wiklander /* 3bc420748SJens Wiklander * Copyright (c) 2015, Linaro Limited 4bc420748SJens Wiklander */ 5bc420748SJens Wiklander #ifndef SIGNED_HDR_H 6bc420748SJens Wiklander #define SIGNED_HDR_H 7bc420748SJens Wiklander 892ea2867SJens Wiklander #include <inttypes.h> 982b5346dSJens Wiklander #include <tee_api_types.h> 10064663e8SJens Wiklander #include <stdlib.h> 11bc420748SJens Wiklander 12bc420748SJens Wiklander enum shdr_img_type { 13bc420748SJens Wiklander SHDR_TA = 0, 1482b5346dSJens Wiklander SHDR_BOOTSTRAP_TA = 1, 15*e1afc439SSumit Garg SHDR_ENCRYPTED_TA = 2, 16bc420748SJens Wiklander }; 17bc420748SJens Wiklander 18bc420748SJens Wiklander #define SHDR_MAGIC 0x4f545348 19bc420748SJens Wiklander 20bc420748SJens Wiklander /** 21bc420748SJens Wiklander * struct shdr - signed header 22bc420748SJens Wiklander * @magic: magic number must match SHDR_MAGIC 23bc420748SJens Wiklander * @img_type: image type, values defined by enum shdr_img_type 24bc420748SJens Wiklander * @img_size: image size in bytes 25bc420748SJens Wiklander * @algo: algorithm, defined by public key algorithms TEE_ALG_* 26bc420748SJens Wiklander * from TEE Internal API specification 27bc420748SJens Wiklander * @hash_size: size of the signed hash 28bc420748SJens Wiklander * @sig_size: size of the signature 29bc420748SJens Wiklander * @hash: hash of an image 30bc420748SJens Wiklander * @sig: signature of @hash 31bc420748SJens Wiklander */ 32bc420748SJens Wiklander struct shdr { 33bc420748SJens Wiklander uint32_t magic; 34bc420748SJens Wiklander uint32_t img_type; 35bc420748SJens Wiklander uint32_t img_size; 36bc420748SJens Wiklander uint32_t algo; 37bc420748SJens Wiklander uint16_t hash_size; 38bc420748SJens Wiklander uint16_t sig_size; 39bc420748SJens Wiklander /* 40bc420748SJens Wiklander * Commented out element used to visualize the layout dynamic part 41bc420748SJens Wiklander * of the struct. 42bc420748SJens Wiklander * 43bc420748SJens Wiklander * hash is accessed through the macro SHDR_GET_HASH and 44bc420748SJens Wiklander * signature is accessed through the macro SHDR_GET_SIG 45bc420748SJens Wiklander * 46bc420748SJens Wiklander * uint8_t hash[hash_size]; 47bc420748SJens Wiklander * uint8_t sig[sig_size]; 48bc420748SJens Wiklander */ 49bc420748SJens Wiklander }; 50bc420748SJens Wiklander 51bc420748SJens Wiklander #define SHDR_GET_SIZE(x) (sizeof(struct shdr) + (x)->hash_size + \ 52bc420748SJens Wiklander (x)->sig_size) 53bc420748SJens Wiklander #define SHDR_GET_HASH(x) (uint8_t *)(((struct shdr *)(x)) + 1) 54bc420748SJens Wiklander #define SHDR_GET_SIG(x) (SHDR_GET_HASH(x) + (x)->hash_size) 55bc420748SJens Wiklander 5682b5346dSJens Wiklander struct shdr_bootstrap_ta { 5782b5346dSJens Wiklander uint8_t uuid[sizeof(TEE_UUID)]; 58a8f769f3SEtienne Carriere uint32_t ta_version; 5982b5346dSJens Wiklander }; 6082b5346dSJens Wiklander 61*e1afc439SSumit Garg /** 62*e1afc439SSumit Garg * struct shdr_encrypted_ta - encrypted TA header 63*e1afc439SSumit Garg * @enc_algo: authenticated encyption algorithm, defined by symmetric key 64*e1afc439SSumit Garg * algorithms TEE_ALG_* from TEE Internal API 65*e1afc439SSumit Garg * specification 66*e1afc439SSumit Garg * @flags: authenticated encyption flags 67*e1afc439SSumit Garg * @iv_size: size of the initialization vector 68*e1afc439SSumit Garg * @tag_size: size of the authentication tag 69*e1afc439SSumit Garg * @iv: initialization vector 70*e1afc439SSumit Garg * @tag: authentication tag 71*e1afc439SSumit Garg */ 72*e1afc439SSumit Garg struct shdr_encrypted_ta { 73*e1afc439SSumit Garg uint32_t enc_algo; 74*e1afc439SSumit Garg uint32_t flags; 75*e1afc439SSumit Garg uint16_t iv_size; 76*e1afc439SSumit Garg uint16_t tag_size; 77*e1afc439SSumit Garg /* 78*e1afc439SSumit Garg * Commented out element used to visualize the layout dynamic part 79*e1afc439SSumit Garg * of the struct. 80*e1afc439SSumit Garg * 81*e1afc439SSumit Garg * iv is accessed through the macro SHDR_ENC_GET_IV and 82*e1afc439SSumit Garg * tag is accessed through the macro SHDR_ENC_GET_TAG 83*e1afc439SSumit Garg * 84*e1afc439SSumit Garg * uint8_t iv[iv_size]; 85*e1afc439SSumit Garg * uint8_t tag[tag_size]; 86*e1afc439SSumit Garg */ 87*e1afc439SSumit Garg }; 88*e1afc439SSumit Garg 89*e1afc439SSumit Garg #define SHDR_ENC_KEY_TYPE_MASK 0x1 90*e1afc439SSumit Garg 91*e1afc439SSumit Garg enum shdr_enc_key_type { 92*e1afc439SSumit Garg SHDR_ENC_KEY_DEV_SPECIFIC = 0, 93*e1afc439SSumit Garg SHDR_ENC_KEY_CLASS_WIDE = 1, 94*e1afc439SSumit Garg }; 95*e1afc439SSumit Garg 96*e1afc439SSumit Garg #define SHDR_ENC_GET_SIZE(x) ({ typeof(x) _x = (x); \ 97*e1afc439SSumit Garg (sizeof(struct shdr_encrypted_ta) + \ 98*e1afc439SSumit Garg _x->iv_size + _x->tag_size); }) 99*e1afc439SSumit Garg #define SHDR_ENC_GET_IV(x) ((uint8_t *) \ 100*e1afc439SSumit Garg (((struct shdr_encrypted_ta *)(x)) + 1)) 101*e1afc439SSumit Garg #define SHDR_ENC_GET_TAG(x) ({ typeof(x) _x = (x); \ 102*e1afc439SSumit Garg (SHDR_ENC_GET_IV(_x) + _x->iv_size); }) 103*e1afc439SSumit Garg 104064663e8SJens Wiklander /* 105064663e8SJens Wiklander * Allocates a struct shdr large enough to hold the entire header, 106064663e8SJens Wiklander * excluding a subheader like struct shdr_bootstrap_ta. 107064663e8SJens Wiklander */ 108064663e8SJens Wiklander struct shdr *shdr_alloc_and_copy(const struct shdr *img, size_t img_size); 109bc420748SJens Wiklander 110064663e8SJens Wiklander /* Frees a previously allocated struct shdr */ 111064663e8SJens Wiklander static inline void shdr_free(struct shdr *shdr) 112064663e8SJens Wiklander { 113064663e8SJens Wiklander free(shdr); 114064663e8SJens Wiklander } 115064663e8SJens Wiklander 116064663e8SJens Wiklander /* 117064663e8SJens Wiklander * Verifies the signature in the @shdr. 118064663e8SJens Wiklander * 119064663e8SJens Wiklander * Note that the static part of struct shdr and payload still need to be 120064663e8SJens Wiklander * checked against the hash contained in the header. 121064663e8SJens Wiklander * 122064663e8SJens Wiklander * Returns TEE_SUCCESS on success or TEE_ERROR_SECURITY on failure 123064663e8SJens Wiklander */ 124064663e8SJens Wiklander TEE_Result shdr_verify_signature(const struct shdr *shdr); 125064663e8SJens Wiklander 126064663e8SJens Wiklander #endif /*SIGNED_HDR_H*/ 127