aboutsummaryrefslogtreecommitdiff
path: root/block_mac.c
blob: 8b4e054a9c7f534ccc5e4a82d05fa5ca7a9b8555 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <openssl/crypto.h> /* for CRYPTO_memcmp */
#include <string.h>

#include "block_device.h"
#include "block_mac.h"
#include "transaction.h"

static size_t block_mac_block_size(const struct fs* fs) {
    size_t size = fs->block_num_size;
    assert(size);
    assert(size <= sizeof(data_block_t));
    return size;
}

static size_t block_mac_mac_size(const struct fs* fs) {
    size_t size = fs->mac_size;
    assert(size);
    assert(size <= sizeof(struct mac));
    return size;
}

void block_mac_clear(const struct transaction* tr, struct block_mac* dest) {
    memset(dest, 0, block_mac_block_size(tr->fs) + block_mac_mac_size(tr->fs));
}

data_block_t block_mac_to_block_fs(const struct fs* fs,
                                   const struct block_mac* block_mac) {
    data_block_t block = 0;

    memcpy(&block, block_mac->data, block_mac_block_size(fs));

    return block;
}

data_block_t block_mac_to_block(const struct transaction* tr,
                                const struct block_mac* block_mac) {
    assert(tr);
    return block_mac_to_block_fs(tr->fs, block_mac);
}

const void* block_mac_to_mac(const struct transaction* tr,
                             const struct block_mac* block_mac) {
    return block_mac->data + block_mac_block_size(tr->fs);
}

void block_mac_set_block(const struct transaction* tr,
                         struct block_mac* block_mac,
                         data_block_t block) {
    memcpy(block_mac->data, &block, block_mac_block_size(tr->fs));
}

void block_mac_set_mac(const struct transaction* tr,
                       struct block_mac* block_mac,
                       const struct mac* mac) {
    memcpy(block_mac->data + block_mac_block_size(tr->fs), mac,
           block_mac_mac_size(tr->fs));
}

bool block_mac_eq(const struct transaction* tr,
                  const struct block_mac* a,
                  const struct block_mac* b) {
    return !CRYPTO_memcmp(
            a, b, block_mac_block_size(tr->fs) + block_mac_mac_size(tr->fs));
}

void block_mac_copy(const struct transaction* tr,
                    struct block_mac* dest,
                    const struct block_mac* src) {
    memcpy(dest, src,
           block_mac_block_size(tr->fs) + block_mac_mac_size(tr->fs));
}