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