Lines Matching refs:ht
103 struct tee_fs_htree *ht; member
108 static TEE_Result rpc_read(struct tee_fs_htree *ht, enum tee_fs_htree_type type, in rpc_read() argument
116 res = ht->stor->rpc_read_init(ht->stor_aux, &op, type, idx, vers, &p); in rpc_read()
120 res = ht->stor->rpc_read_final(&op, &bytes); in rpc_read()
131 static TEE_Result rpc_read_head(struct tee_fs_htree *ht, size_t vers, in rpc_read_head() argument
134 return rpc_read(ht, TEE_FS_HTREE_TYPE_HEAD, 0, vers, in rpc_read_head()
138 static TEE_Result rpc_read_node(struct tee_fs_htree *ht, size_t node_id, in rpc_read_node() argument
142 return rpc_read(ht, TEE_FS_HTREE_TYPE_NODE, node_id - 1, vers, in rpc_read_node()
146 static TEE_Result rpc_write(struct tee_fs_htree *ht, in rpc_write() argument
154 res = ht->stor->rpc_write_init(ht->stor_aux, &op, type, idx, vers, &p); in rpc_write()
159 return ht->stor->rpc_write_final(&op); in rpc_write()
162 static TEE_Result rpc_write_head(struct tee_fs_htree *ht, size_t vers, in rpc_write_head() argument
165 return rpc_write(ht, TEE_FS_HTREE_TYPE_HEAD, 0, vers, in rpc_write_head()
169 static TEE_Result rpc_write_node(struct tee_fs_htree *ht, size_t node_id, in rpc_write_node() argument
173 return rpc_write(ht, TEE_FS_HTREE_TYPE_NODE, node_id - 1, vers, in rpc_write_node()
201 static TEE_Result htree_traverse_post_order(struct tee_fs_htree *ht, in htree_traverse_post_order() argument
204 struct traverse_arg targ = { ht, cb, arg }; in htree_traverse_post_order()
206 return traverse_post_order(&targ, &ht->root); in htree_traverse_post_order()
216 static struct htree_node *find_closest_node(struct tee_fs_htree *ht, in find_closest_node() argument
219 struct htree_node *node = &ht->root; in find_closest_node()
245 static struct htree_node *find_node(struct tee_fs_htree *ht, size_t node_id) in find_node() argument
247 struct htree_node *node = find_closest_node(ht, node_id); in find_node()
254 static TEE_Result get_node(struct tee_fs_htree *ht, bool create, in get_node() argument
261 node = find_closest_node(ht, node_id); in get_node()
279 node = find_closest_node(ht, n); in get_node()
295 if (node->id > ht->imeta.max_node_id) in get_node()
296 ht->imeta.max_node_id = node->id; in get_node()
320 static TEE_Result init_head_from_data(struct tee_fs_htree *ht, in init_head_from_data() argument
328 res = rpc_read_node(ht, 1, idx, &ht->root.node); in init_head_from_data()
332 if (!memcmp(ht->root.node.hash, hash, in init_head_from_data()
333 sizeof(ht->root.node.hash))) { in init_head_from_data()
334 res = rpc_read_head(ht, idx, &ht->head); in init_head_from_data()
347 res = rpc_read_head(ht, idx, head + idx); in init_head_from_data()
356 res = rpc_read_node(ht, 1, idx, &ht->root.node); in init_head_from_data()
360 ht->head = head[idx]; in init_head_from_data()
363 ht->root.id = 1; in init_head_from_data()
368 static TEE_Result init_tree_from_data(struct tee_fs_htree *ht) in init_tree_from_data() argument
377 while (node_id <= ht->imeta.max_node_id) { in init_tree_from_data()
378 node = find_node(ht, node_id >> 1); in init_tree_from_data()
384 res = rpc_read_node(ht, node_id, committed_version, in init_tree_from_data()
389 res = get_node(ht, true, node_id, &nc); in init_tree_from_data()
439 struct tee_fs_htree *ht, in authenc_init() argument
452 iv = ht->head.iv; in authenc_init()
453 aad_len += TEE_FS_HTREE_HASH_SIZE + sizeof(ht->head.counter); in authenc_init()
466 res = crypto_authenc_init(ctx, mode, ht->fek, TEE_FS_HTREE_FEK_SIZE, iv, in authenc_init()
473 res = crypto_authenc_update_aad(ctx, mode, ht->root.node.hash, in authenc_init()
479 (void *)&ht->head.counter, in authenc_init()
480 sizeof(ht->head.counter)); in authenc_init()
485 res = crypto_authenc_update_aad(ctx, mode, ht->head.enc_fek, in authenc_init()
544 static TEE_Result verify_root(struct tee_fs_htree *ht) in verify_root() argument
549 res = tee_fs_fek_crypt(ht->uuid, TEE_MODE_DECRYPT, ht->head.enc_fek, in verify_root()
550 sizeof(ht->fek), ht->fek); in verify_root()
554 res = authenc_init(&ctx, TEE_MODE_DECRYPT, ht, NULL, sizeof(ht->imeta)); in verify_root()
558 return authenc_decrypt_final(ctx, ht->head.tag, ht->head.imeta, in verify_root()
559 sizeof(ht->imeta), &ht->imeta); in verify_root()
572 res = calc_node_hash(node, &targ->ht->imeta.meta, ctx, digest); in verify_node()
580 static TEE_Result verify_tree(struct tee_fs_htree *ht) in verify_tree() argument
589 res = htree_traverse_post_order(ht, verify_node, ctx); in verify_tree()
595 static TEE_Result init_root_node(struct tee_fs_htree *ht) in init_root_node() argument
604 ht->root.id = 1; in init_root_node()
605 ht->root.dirty = true; in init_root_node()
607 res = calc_node_hash(&ht->root, &ht->imeta.meta, ctx, in init_root_node()
608 ht->root.node.hash); in init_root_node()
619 struct tee_fs_htree *ht = calloc(1, sizeof(*ht)); in tee_fs_htree_open() local
621 if (!ht) in tee_fs_htree_open()
624 ht->uuid = uuid; in tee_fs_htree_open()
625 ht->stor = stor; in tee_fs_htree_open()
626 ht->stor_aux = stor_aux; in tee_fs_htree_open()
631 res = crypto_rng_read(ht->fek, sizeof(ht->fek)); in tee_fs_htree_open()
635 res = tee_fs_fek_crypt(ht->uuid, TEE_MODE_ENCRYPT, ht->fek, in tee_fs_htree_open()
636 sizeof(ht->fek), ht->head.enc_fek); in tee_fs_htree_open()
640 res = init_root_node(ht); in tee_fs_htree_open()
644 ht->dirty = true; in tee_fs_htree_open()
645 res = tee_fs_htree_sync_to_storage(&ht, hash); in tee_fs_htree_open()
648 res = rpc_write_head(ht, 0, &dummy_head); in tee_fs_htree_open()
650 res = init_head_from_data(ht, hash); in tee_fs_htree_open()
654 res = verify_root(ht); in tee_fs_htree_open()
658 res = init_tree_from_data(ht); in tee_fs_htree_open()
662 res = verify_tree(ht); in tee_fs_htree_open()
666 *ht_ret = ht; in tee_fs_htree_open()
668 tee_fs_htree_close(&ht); in tee_fs_htree_open()
672 struct tee_fs_htree_meta *tee_fs_htree_get_meta(struct tee_fs_htree *ht) in tee_fs_htree_get_meta() argument
674 return &ht->imeta.meta; in tee_fs_htree_get_meta()
677 void tee_fs_htree_meta_set_dirty(struct tee_fs_htree *ht) in tee_fs_htree_meta_set_dirty() argument
679 ht->dirty = true; in tee_fs_htree_meta_set_dirty()
680 ht->root.dirty = true; in tee_fs_htree_meta_set_dirty()
691 void tee_fs_htree_close(struct tee_fs_htree **ht) in tee_fs_htree_close() argument
693 if (!*ht) in tee_fs_htree_close()
695 htree_traverse_post_order(*ht, free_node, NULL); in tee_fs_htree_close()
696 free(*ht); in tee_fs_htree_close()
697 *ht = NULL; in tee_fs_htree_close()
728 vers = !(targ->ht->head.counter & 1); in htree_sync_node_to_storage()
729 meta = &targ->ht->imeta.meta; in htree_sync_node_to_storage()
739 return rpc_write_node(targ->ht, node->id, vers, &node->node); in htree_sync_node_to_storage()
742 static TEE_Result update_root(struct tee_fs_htree *ht) in update_root() argument
747 ht->head.counter++; in update_root()
749 res = authenc_init(&ctx, TEE_MODE_ENCRYPT, ht, NULL, sizeof(ht->imeta)); in update_root()
753 return authenc_encrypt_final(ctx, ht->head.tag, &ht->imeta, in update_root()
754 sizeof(ht->imeta), &ht->head.imeta); in update_root()
761 struct tee_fs_htree *ht = *ht_arg; in tee_fs_htree_sync_to_storage() local
764 if (!ht) in tee_fs_htree_sync_to_storage()
767 if (!ht->dirty) in tee_fs_htree_sync_to_storage()
774 res = htree_traverse_post_order(ht, htree_sync_node_to_storage, ctx); in tee_fs_htree_sync_to_storage()
779 res = update_root(ht); in tee_fs_htree_sync_to_storage()
783 res = rpc_write_head(ht, ht->head.counter & 1, &ht->head); in tee_fs_htree_sync_to_storage()
787 ht->dirty = false; in tee_fs_htree_sync_to_storage()
789 memcpy(hash, ht->root.node.hash, sizeof(ht->root.node.hash)); in tee_fs_htree_sync_to_storage()
797 static TEE_Result get_block_node(struct tee_fs_htree *ht, bool create, in get_block_node() argument
803 res = get_node(ht, create, BLOCK_NUM_TO_NODE_ID(block_num), &nd); in get_block_node()
813 struct tee_fs_htree *ht = *ht_arg; in tee_fs_htree_write_block() local
821 if (!ht) in tee_fs_htree_write_block()
824 res = get_block_node(ht, true, block_num, &node); in tee_fs_htree_write_block()
832 res = ht->stor->rpc_write_init(ht->stor_aux, &op, in tee_fs_htree_write_block()
838 res = authenc_init(&ctx, TEE_MODE_ENCRYPT, ht, &node->node, in tee_fs_htree_write_block()
839 ht->stor->block_size); in tee_fs_htree_write_block()
843 ht->stor->block_size, enc_block); in tee_fs_htree_write_block()
847 res = ht->stor->rpc_write_final(&op); in tee_fs_htree_write_block()
853 ht->dirty = true; in tee_fs_htree_write_block()
863 struct tee_fs_htree *ht = *ht_arg; in tee_fs_htree_read_block() local
872 if (!ht) in tee_fs_htree_read_block()
875 res = get_block_node(ht, false, block_num, &node); in tee_fs_htree_read_block()
880 res = ht->stor->rpc_read_init(ht->stor_aux, &op, in tee_fs_htree_read_block()
886 res = ht->stor->rpc_read_final(&op, &len); in tee_fs_htree_read_block()
889 if (len != ht->stor->block_size) { in tee_fs_htree_read_block()
894 res = authenc_init(&ctx, TEE_MODE_DECRYPT, ht, &node->node, in tee_fs_htree_read_block()
895 ht->stor->block_size); in tee_fs_htree_read_block()
900 ht->stor->block_size, block); in tee_fs_htree_read_block()
909 struct tee_fs_htree *ht = *ht_arg; in tee_fs_htree_truncate() local
913 if (!ht) in tee_fs_htree_truncate()
916 while (node_id < ht->imeta.max_node_id) { in tee_fs_htree_truncate()
917 node = find_closest_node(ht, ht->imeta.max_node_id); in tee_fs_htree_truncate()
918 assert(node && node->id == ht->imeta.max_node_id); in tee_fs_htree_truncate()
924 ht->imeta.max_node_id--; in tee_fs_htree_truncate()
925 ht->dirty = true; in tee_fs_htree_truncate()