xref: /openbmc/qemu/crypto/blockpriv.h (revision ea2fde5b)
1 /*
2  * QEMU Crypto block device encryption
3  *
4  * Copyright (c) 2015-2016 Red Hat, Inc.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 #ifndef QCRYPTO_BLOCKPRIV_H
22 #define QCRYPTO_BLOCKPRIV_H
23 
24 #include "crypto/block.h"
25 #include "qemu/thread.h"
26 
27 typedef struct QCryptoBlockDriver QCryptoBlockDriver;
28 
29 struct QCryptoBlock {
30     QCryptoBlockFormat format;
31 
32     const QCryptoBlockDriver *driver;
33     void *opaque;
34 
35     QCryptoCipher **ciphers;
36     size_t n_ciphers;
37     size_t n_free_ciphers;
38     QCryptoIVGen *ivgen;
39     QemuMutex mutex;
40 
41     QCryptoHashAlgorithm kdfhash;
42     size_t niv;
43     uint64_t payload_offset; /* In bytes */
44     uint64_t sector_size; /* In bytes */
45 
46     bool detached_header; /* True if disk has a detached LUKS header */
47 };
48 
49 struct QCryptoBlockDriver {
50     int (*open)(QCryptoBlock *block,
51                 QCryptoBlockOpenOptions *options,
52                 const char *optprefix,
53                 QCryptoBlockReadFunc readfunc,
54                 void *opaque,
55                 unsigned int flags,
56                 size_t n_threads,
57                 Error **errp);
58 
59     int (*create)(QCryptoBlock *block,
60                   QCryptoBlockCreateOptions *options,
61                   const char *optprefix,
62                   QCryptoBlockInitFunc initfunc,
63                   QCryptoBlockWriteFunc writefunc,
64                   void *opaque,
65                   Error **errp);
66 
67     int (*amend)(QCryptoBlock *block,
68                  QCryptoBlockReadFunc readfunc,
69                  QCryptoBlockWriteFunc writefunc,
70                  void *opaque,
71                  QCryptoBlockAmendOptions *options,
72                  bool force,
73                  Error **errp);
74 
75     int (*get_info)(QCryptoBlock *block,
76                     QCryptoBlockInfo *info,
77                     Error **errp);
78 
79     void (*cleanup)(QCryptoBlock *block);
80 
81     int (*encrypt)(QCryptoBlock *block,
82                    uint64_t startsector,
83                    uint8_t *buf,
84                    size_t len,
85                    Error **errp);
86     int (*decrypt)(QCryptoBlock *block,
87                    uint64_t startsector,
88                    uint8_t *buf,
89                    size_t len,
90                    Error **errp);
91 
92     bool (*has_format)(const uint8_t *buf,
93                        size_t buflen);
94 };
95 
96 
97 int qcrypto_block_cipher_decrypt_helper(QCryptoCipher *cipher,
98                                         size_t niv,
99                                         QCryptoIVGen *ivgen,
100                                         int sectorsize,
101                                         uint64_t offset,
102                                         uint8_t *buf,
103                                         size_t len,
104                                         Error **errp);
105 
106 int qcrypto_block_cipher_encrypt_helper(QCryptoCipher *cipher,
107                                         size_t niv,
108                                         QCryptoIVGen *ivgen,
109                                         int sectorsize,
110                                         uint64_t offset,
111                                         uint8_t *buf,
112                                         size_t len,
113                                         Error **errp);
114 
115 int qcrypto_block_decrypt_helper(QCryptoBlock *block,
116                                  int sectorsize,
117                                  uint64_t offset,
118                                  uint8_t *buf,
119                                  size_t len,
120                                  Error **errp);
121 
122 int qcrypto_block_encrypt_helper(QCryptoBlock *block,
123                                  int sectorsize,
124                                  uint64_t offset,
125                                  uint8_t *buf,
126                                  size_t len,
127                                  Error **errp);
128 
129 int qcrypto_block_init_cipher(QCryptoBlock *block,
130                               QCryptoCipherAlgorithm alg,
131                               QCryptoCipherMode mode,
132                               const uint8_t *key, size_t nkey,
133                               size_t n_threads, Error **errp);
134 
135 void qcrypto_block_free_cipher(QCryptoBlock *block);
136 
137 #endif /* QCRYPTO_BLOCKPRIV_H */
138