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