1 /*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #pragma once
18
19 #include <assert.h>
20
21 #include "block_device.h"
22 #include "crypt.h"
23
24 /**
25 * struct block_mac - Struct for storing packed block and mac values.
26 * @data: Packed block and mac values. Size of entries is fs specific.
27 *
28 * The size of this struct is large enough to store any supported block and mac
29 * value, but data outside the fs specific block_num_size + mac_size range is
30 * not touched. This means the full struct can be allocated where the fs
31 * specific sizes are not yet known, and a block_num_size + mac_size size memory
32 * block can be allocated where that size is known.
33 */
34 struct block_mac {
35 uint8_t data[sizeof(data_block_t) + sizeof(struct mac)];
36 };
37 #define BLOCK_MAC_INITIAL_VALUE(block_mac) \
38 { \
39 { 0 } \
40 }
41
42 struct fs;
43 struct transaction;
44
45 void block_mac_clear(const struct transaction* tr, struct block_mac* dest);
46 data_block_t block_mac_to_block_fs(const struct fs* fs,
47 const struct block_mac* block_mac);
48 data_block_t block_mac_to_block(const struct transaction* tr,
49 const struct block_mac* block_mac);
50 const void* block_mac_to_mac_fs(const struct fs* fs,
51 const struct block_mac* block_mac);
52 const void* block_mac_to_mac(const struct transaction* tr,
53 const struct block_mac* block_mac);
54 void block_mac_set_block_fs(const struct fs* fs,
55 struct block_mac* block_mac,
56 data_block_t block);
57 void block_mac_set_block(const struct transaction* tr,
58 struct block_mac* block_mac,
59 data_block_t block);
60 void block_mac_set_mac_fs(const struct fs* fs,
61 struct block_mac* block_mac,
62 const struct mac* mac);
63 void block_mac_set_mac(const struct transaction* tr,
64 struct block_mac* block_mac,
65 const struct mac* mac);
66 bool block_mac_eq(const struct transaction* tr,
67 const struct block_mac* a,
68 const struct block_mac* b);
69 void block_mac_copy(const struct transaction* tr,
70 struct block_mac* dest,
71 const struct block_mac* src);
72
block_mac_valid_fs(const struct fs * fs,const struct block_mac * block_mac)73 static inline bool block_mac_valid_fs(const struct fs* fs,
74 const struct block_mac* block_mac) {
75 return block_mac_to_block_fs(fs, block_mac);
76 }
77
block_mac_valid(const struct transaction * tr,const struct block_mac * block_mac)78 static inline bool block_mac_valid(const struct transaction* tr,
79 const struct block_mac* block_mac) {
80 return block_mac_to_block(tr, block_mac);
81 }
82
block_mac_same_block(const struct transaction * tr,const struct block_mac * a,const struct block_mac * b)83 static inline bool block_mac_same_block(const struct transaction* tr,
84 const struct block_mac* a,
85 const struct block_mac* b) {
86 return block_mac_to_block(tr, a) == block_mac_to_block(tr, b);
87 }
88
block_mac_copy_mac(const struct transaction * tr,struct block_mac * dest,const struct block_mac * src)89 static inline void block_mac_copy_mac(const struct transaction* tr,
90 struct block_mac* dest,
91 const struct block_mac* src) {
92 assert(block_mac_same_block(tr, dest, src));
93 block_mac_set_mac(tr, dest, (const struct mac*)block_mac_to_mac(tr, src));
94 }
95