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 #include <openssl/crypto.h> /* for CRYPTO_memcmp */
18 #include <string.h>
19
20 #include "block_device.h"
21 #include "block_mac.h"
22 #include "transaction.h"
23
block_mac_block_size(const struct fs * fs)24 static size_t block_mac_block_size(const struct fs* fs) {
25 size_t size = fs->block_num_size;
26 assert(size);
27 assert(size <= sizeof(data_block_t));
28 return size;
29 }
30
block_mac_mac_size(const struct fs * fs)31 static size_t block_mac_mac_size(const struct fs* fs) {
32 size_t size = fs->mac_size;
33 assert(size);
34 assert(size <= sizeof(struct mac));
35 return size;
36 }
37
block_mac_clear(const struct transaction * tr,struct block_mac * dest)38 void block_mac_clear(const struct transaction* tr, struct block_mac* dest) {
39 memset(dest, 0, block_mac_block_size(tr->fs) + block_mac_mac_size(tr->fs));
40 }
41
block_mac_to_block_fs(const struct fs * fs,const struct block_mac * block_mac)42 data_block_t block_mac_to_block_fs(const struct fs* fs,
43 const struct block_mac* block_mac) {
44 data_block_t block = 0;
45
46 memcpy(&block, block_mac->data, block_mac_block_size(fs));
47
48 return block;
49 }
50
block_mac_to_block(const struct transaction * tr,const struct block_mac * block_mac)51 data_block_t block_mac_to_block(const struct transaction* tr,
52 const struct block_mac* block_mac) {
53 assert(tr);
54 return block_mac_to_block_fs(tr->fs, block_mac);
55 }
56
block_mac_to_mac(const struct transaction * tr,const struct block_mac * block_mac)57 const void* block_mac_to_mac(const struct transaction* tr,
58 const struct block_mac* block_mac) {
59 return block_mac->data + block_mac_block_size(tr->fs);
60 }
61
block_mac_set_block(const struct transaction * tr,struct block_mac * block_mac,data_block_t block)62 void block_mac_set_block(const struct transaction* tr,
63 struct block_mac* block_mac,
64 data_block_t block) {
65 memcpy(block_mac->data, &block, block_mac_block_size(tr->fs));
66 }
67
block_mac_set_mac(const struct transaction * tr,struct block_mac * block_mac,const struct mac * mac)68 void block_mac_set_mac(const struct transaction* tr,
69 struct block_mac* block_mac,
70 const struct mac* mac) {
71 memcpy(block_mac->data + block_mac_block_size(tr->fs), mac,
72 block_mac_mac_size(tr->fs));
73 }
74
block_mac_eq(const struct transaction * tr,const struct block_mac * a,const struct block_mac * b)75 bool block_mac_eq(const struct transaction* tr,
76 const struct block_mac* a,
77 const struct block_mac* b) {
78 return !CRYPTO_memcmp(
79 a, b, block_mac_block_size(tr->fs) + block_mac_mac_size(tr->fs));
80 }
81
block_mac_copy(const struct transaction * tr,struct block_mac * dest,const struct block_mac * src)82 void block_mac_copy(const struct transaction* tr,
83 struct block_mac* dest,
84 const struct block_mac* src) {
85 memcpy(dest, src,
86 block_mac_block_size(tr->fs) + block_mac_mac_size(tr->fs));
87 }
88