xref: /optee_os/core/include/signed_hdr.h (revision b19db423a235b7125a92835be3a9ba0e0c9ec3ac)
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,
15e1afc439SSumit 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 
56*b19db423SJens Wiklander /**
57*b19db423SJens Wiklander  * struct shdr_bootstrap_ta - bootstrap TA subheader
58*b19db423SJens Wiklander  * @uuid:	UUID of the TA
59*b19db423SJens Wiklander  * @ta_version:	Version of the TA
60*b19db423SJens Wiklander  */
6182b5346dSJens Wiklander struct shdr_bootstrap_ta {
6282b5346dSJens Wiklander 	uint8_t uuid[sizeof(TEE_UUID)];
63a8f769f3SEtienne Carriere 	uint32_t ta_version;
6482b5346dSJens Wiklander };
6582b5346dSJens Wiklander 
66e1afc439SSumit Garg /**
67e1afc439SSumit Garg  * struct shdr_encrypted_ta - encrypted TA header
68e1afc439SSumit Garg  * @enc_algo:	authenticated encyption algorithm, defined by symmetric key
69e1afc439SSumit Garg  *		algorithms TEE_ALG_* from TEE Internal API
70e1afc439SSumit Garg  *		specification
71e1afc439SSumit Garg  * @flags:	authenticated encyption flags
72e1afc439SSumit Garg  * @iv_size:	size of the initialization vector
73e1afc439SSumit Garg  * @tag_size:	size of the authentication tag
74e1afc439SSumit Garg  * @iv:		initialization vector
75e1afc439SSumit Garg  * @tag:	authentication tag
76e1afc439SSumit Garg  */
77e1afc439SSumit Garg struct shdr_encrypted_ta {
78e1afc439SSumit Garg 	uint32_t enc_algo;
79e1afc439SSumit Garg 	uint32_t flags;
80e1afc439SSumit Garg 	uint16_t iv_size;
81e1afc439SSumit Garg 	uint16_t tag_size;
82e1afc439SSumit Garg 	/*
83e1afc439SSumit Garg 	 * Commented out element used to visualize the layout dynamic part
84e1afc439SSumit Garg 	 * of the struct.
85e1afc439SSumit Garg 	 *
86e1afc439SSumit Garg 	 * iv is accessed through the macro SHDR_ENC_GET_IV and
87e1afc439SSumit Garg 	 * tag is accessed through the macro SHDR_ENC_GET_TAG
88e1afc439SSumit Garg 	 *
89e1afc439SSumit Garg 	 * uint8_t iv[iv_size];
90e1afc439SSumit Garg 	 * uint8_t tag[tag_size];
91e1afc439SSumit Garg 	 */
92e1afc439SSumit Garg };
93e1afc439SSumit Garg 
94e1afc439SSumit Garg #define SHDR_ENC_KEY_TYPE_MASK	0x1
95e1afc439SSumit Garg 
96e1afc439SSumit Garg enum shdr_enc_key_type {
97e1afc439SSumit Garg 	SHDR_ENC_KEY_DEV_SPECIFIC = 0,
98e1afc439SSumit Garg 	SHDR_ENC_KEY_CLASS_WIDE = 1,
99e1afc439SSumit Garg };
100e1afc439SSumit Garg 
101e1afc439SSumit Garg #define SHDR_ENC_GET_SIZE(x)	({ typeof(x) _x = (x); \
102e1afc439SSumit Garg 				   (sizeof(struct shdr_encrypted_ta) + \
103e1afc439SSumit Garg 				   _x->iv_size + _x->tag_size); })
104e1afc439SSumit Garg #define SHDR_ENC_GET_IV(x)	((uint8_t *) \
105e1afc439SSumit Garg 				 (((struct shdr_encrypted_ta *)(x)) + 1))
106e1afc439SSumit Garg #define SHDR_ENC_GET_TAG(x)	({ typeof(x) _x = (x); \
107e1afc439SSumit Garg 				   (SHDR_ENC_GET_IV(_x) + _x->iv_size); })
108e1afc439SSumit Garg 
109064663e8SJens Wiklander /*
110064663e8SJens Wiklander  * Allocates a struct shdr large enough to hold the entire header,
111064663e8SJens Wiklander  * excluding a subheader like struct shdr_bootstrap_ta.
112064663e8SJens Wiklander  */
113064663e8SJens Wiklander struct shdr *shdr_alloc_and_copy(const struct shdr *img, size_t img_size);
114bc420748SJens Wiklander 
115064663e8SJens Wiklander /* Frees a previously allocated struct shdr */
116064663e8SJens Wiklander static inline void shdr_free(struct shdr *shdr)
117064663e8SJens Wiklander {
118064663e8SJens Wiklander 	free(shdr);
119064663e8SJens Wiklander }
120064663e8SJens Wiklander 
121064663e8SJens Wiklander /*
122064663e8SJens Wiklander  * Verifies the signature in the @shdr.
123064663e8SJens Wiklander  *
124064663e8SJens Wiklander  * Note that the static part of struct shdr and payload still need to be
125064663e8SJens Wiklander  * checked against the hash contained in the header.
126064663e8SJens Wiklander  *
127064663e8SJens Wiklander  * Returns TEE_SUCCESS on success or TEE_ERROR_SECURITY on failure
128064663e8SJens Wiklander  */
129064663e8SJens Wiklander TEE_Result shdr_verify_signature(const struct shdr *shdr);
130064663e8SJens Wiklander 
131064663e8SJens Wiklander #endif /*SIGNED_HDR_H*/
132