xref: /optee_os/core/include/tee/fs_htree.h (revision b72716ce78a1d1e9054683e2bec1377c1c1ae344)
1 /*
2  * Copyright (c) 2017, 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 
28 #ifndef __TEE_FS_HTREE_H
29 #define __TEE_FS_HTREE_H
30 
31 /*
32  * The purpose of this API is to provide file integrity and confidentiality
33  * in order to implement secure storage. On-disk data structures are
34  * duplicated to make updates atomic, an update is finalized to disk with
35  * tee_fs_htree_sync_to_storage().
36  *
37  * This implementation doesn't provide rollback protection, it only
38  * guarantees the integrity and confidentiality of the file.
39  */
40 
41 #include <tee_api_types.h>
42 #include <utee_defines.h>
43 
44 #define TEE_FS_HTREE_HASH_SIZE		TEE_SHA256_HASH_SIZE
45 #define TEE_FS_HTREE_IV_SIZE		16
46 #define TEE_FS_HTREE_FEK_SIZE		16
47 #define TEE_FS_HTREE_TAG_SIZE		16
48 
49 /* Internal struct provided to let the rpc callbacks know the size if needed */
50 struct tee_fs_htree_node_image {
51 	/* Note that calc_node_hash() depends on hash first in struct */
52 	uint8_t hash[TEE_FS_HTREE_HASH_SIZE];
53 	uint8_t iv[TEE_FS_HTREE_IV_SIZE];
54 	uint8_t tag[TEE_FS_HTREE_TAG_SIZE];
55 	uint16_t flags;
56 };
57 
58 /*
59  * This struct is not interpreted by the hash tree, it's up to the user of
60  * the interface to update etc if needed.
61  */
62 struct tee_fs_htree_meta {
63 	uint64_t length;
64 };
65 
66 /* Internal struct needed by struct tee_fs_htree_image */
67 struct tee_fs_htree_imeta {
68 	struct tee_fs_htree_meta meta;
69 	uint32_t max_node_id;
70 };
71 
72 /* Internal struct provided to let the rpc callbacks know the size if needed */
73 struct tee_fs_htree_image {
74 	uint8_t iv[TEE_FS_HTREE_IV_SIZE];
75 	uint8_t tag[TEE_FS_HTREE_TAG_SIZE];
76 	uint8_t enc_fek[TEE_FS_HTREE_FEK_SIZE];
77 	uint8_t imeta[sizeof(struct tee_fs_htree_imeta)];
78 	uint32_t counter;
79 };
80 
81 /**
82  * enum tee_fs_htree_type - type of hash tree element
83  * @TEE_FS_HTREE_TYPE_HEAD: indicates a struct tee_fs_htree_image
84  * @TEE_FS_HTREE_TYPE_NODE: indicates a struct tee_fs_htree_node_image
85  * @TEE_FS_HTREE_TYPE_BLOCK: indicates a data block
86  */
87 enum tee_fs_htree_type {
88 	TEE_FS_HTREE_TYPE_HEAD,
89 	TEE_FS_HTREE_TYPE_NODE,
90 	TEE_FS_HTREE_TYPE_BLOCK,
91 };
92 
93 struct tee_fs_rpc_operation;
94 
95 /**
96  * struct tee_fs_htree_storage - storage description supplied by user of
97  * this interface
98  * @block_size:		size of data blocks
99  * @rpc_read_init:	initialize a struct tee_fs_rpc_operation for an RPC read
100  *			operation
101  * @rpc_write_init:	initialize a struct tee_fs_rpc_operation for an RPC
102  *			write operation
103  *
104  * The @idx arguments starts counting from 0. The @vers arguments are either
105  * 0 or 1. The @data arguments is a pointer to a buffer in non-secure shared
106  * memory where the encrypted data is stored.
107  */
108 struct tee_fs_htree_storage {
109 	size_t block_size;
110 	TEE_Result (*rpc_read_init)(void *aux, struct tee_fs_rpc_operation *op,
111 				    enum tee_fs_htree_type type, size_t idx,
112 				    uint8_t vers, void **data);
113 	TEE_Result (*rpc_write_init)(void *aux, struct tee_fs_rpc_operation *op,
114 				     enum tee_fs_htree_type type, size_t idx,
115 				     uint8_t vers, void **data);
116 };
117 
118 struct tee_fs_htree;
119 
120 /**
121  * tee_fs_htree_open() - opens/creates a hash tree
122  * @create:	true if a new hash tree is to be created, else the hash tree
123  *		is read in and verified
124  * @stor:	storage description
125  * @stor_aux:	auxilary pointer supplied to callbacks in struct
126  *		tee_fs_htree_storage
127  * @ht:		returned hash tree on success
128  */
129 TEE_Result tee_fs_htree_open(bool create,
130 			     const struct tee_fs_htree_storage *stor,
131 			     void *stor_aux, struct tee_fs_htree **ht);
132 /**
133  * tee_fs_htree_close() - close a hash tree
134  * @ht:		hash tree
135  */
136 void tee_fs_htree_close(struct tee_fs_htree **ht);
137 
138 /**
139  * tee_fs_htree_get_meta() - get a pointer to associated struct
140  * tee_fs_htree_meta
141  * @ht:		hash tree
142  */
143 struct tee_fs_htree_meta *tee_fs_htree_get_meta(struct tee_fs_htree *ht);
144 
145 /**
146  * tee_fs_htree_sync_to_storage() - synchronize hash tree to storage
147  * @ht:		hash tree
148  *
149  * Frees the hash tree and sets *ht to NULL on failure and returns an error code
150  */
151 TEE_Result tee_fs_htree_sync_to_storage(struct tee_fs_htree **ht);
152 
153 /**
154  * tee_fs_htree_truncate() - truncate a hash tree
155  * @ht:		hash tree
156  * @block_num:	the number of nodes to truncate to
157  *
158  * Frees the hash tree and sets *ht to NULL on failure and returns an error code
159  */
160 TEE_Result tee_fs_htree_truncate(struct tee_fs_htree **ht, size_t block_num);
161 
162 /**
163  * tee_fs_htree_write_block() - encrypt and write a data block to storage
164  * @ht:		hash tree
165  * @block_num:	block number
166  * @block:	pointer to a block of stor->block_size size
167  *
168  * Frees the hash tree and sets *ht to NULL on failure and returns an error code
169  */
170 TEE_Result tee_fs_htree_write_block(struct tee_fs_htree **ht, size_t block_num,
171 				    const void *block);
172 /**
173  * tee_fs_htree_write_block() - read and decrypt a data block from storage
174  * @ht:		hash tree
175  * @block_num:	block number
176  * @block:	pointer to a block of stor->block_size size
177  *
178  * Frees the hash tree and sets *ht to NULL on failure and returns an error code
179  */
180 TEE_Result tee_fs_htree_read_block(struct tee_fs_htree **ht, size_t block_num,
181 				   void *block);
182 
183 #endif /*__TEE_FS_HTREE_H*/
184