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(const struct transaction* tr,
51 const struct block_mac* block_mac);
52 void block_mac_set_block(const struct transaction* tr,
53 struct block_mac* block_mac,
54 data_block_t block);
55 void block_mac_set_mac(const struct transaction* tr,
56 struct block_mac* block_mac,
57 const struct mac* mac);
58 bool block_mac_eq(const struct transaction* tr,
59 const struct block_mac* a,
60 const struct block_mac* b);
61 void block_mac_copy(const struct transaction* tr,
62 struct block_mac* dest,
63 const struct block_mac* src);
64
block_mac_valid_fs(const struct fs * fs,const struct block_mac * block_mac)65 static inline bool block_mac_valid_fs(const struct fs* fs,
66 const struct block_mac* block_mac) {
67 return block_mac_to_block_fs(fs, block_mac);
68 }
69
block_mac_valid(const struct transaction * tr,const struct block_mac * block_mac)70 static inline bool block_mac_valid(const struct transaction* tr,
71 const struct block_mac* block_mac) {
72 return block_mac_to_block(tr, block_mac);
73 }
74
block_mac_same_block(const struct transaction * tr,const struct block_mac * a,const struct block_mac * b)75 static inline bool block_mac_same_block(const struct transaction* tr,
76 const struct block_mac* a,
77 const struct block_mac* b) {
78 return block_mac_to_block(tr, a) == block_mac_to_block(tr, b);
79 }
80
block_mac_copy_mac(const struct transaction * tr,struct block_mac * dest,const struct block_mac * src)81 static inline void block_mac_copy_mac(const struct transaction* tr,
82 struct block_mac* dest,
83 const struct block_mac* src) {
84 assert(block_mac_same_block(tr, dest, src));
85 block_mac_set_mac(tr, dest, (const struct mac*)block_mac_to_mac(tr, src));
86 }
87