Daniel P. Berrange | 7d96901 | 2015-10-24 11:44:13 +0100 | [diff] [blame] | 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 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 | |
Markus Armbruster | 121d071 | 2016-06-29 10:12:57 +0200 | [diff] [blame] | 21 | #ifndef QCRYPTO_BLOCKPRIV_H |
| 22 | #define QCRYPTO_BLOCKPRIV_H |
Daniel P. Berrange | 7d96901 | 2015-10-24 11:44:13 +0100 | [diff] [blame] | 23 | |
| 24 | #include "crypto/block.h" |
Vladimir Sementsov-Ogievskiy | c972fa1 | 2018-12-07 19:13:51 +0300 | [diff] [blame] | 25 | #include "qemu/thread.h" |
Daniel P. Berrange | 7d96901 | 2015-10-24 11:44:13 +0100 | [diff] [blame] | 26 | |
| 27 | typedef struct QCryptoBlockDriver QCryptoBlockDriver; |
| 28 | |
| 29 | struct QCryptoBlock { |
| 30 | QCryptoBlockFormat format; |
| 31 | |
| 32 | const QCryptoBlockDriver *driver; |
| 33 | void *opaque; |
| 34 | |
Vladimir Sementsov-Ogievskiy | c972fa1 | 2018-12-07 19:13:51 +0300 | [diff] [blame] | 35 | QCryptoCipher **ciphers; |
| 36 | size_t n_ciphers; |
| 37 | size_t n_free_ciphers; |
Daniel P. Berrange | 7d96901 | 2015-10-24 11:44:13 +0100 | [diff] [blame] | 38 | QCryptoIVGen *ivgen; |
Vladimir Sementsov-Ogievskiy | c972fa1 | 2018-12-07 19:13:51 +0300 | [diff] [blame] | 39 | QemuMutex mutex; |
| 40 | |
Daniel P. Berrange | 7d96901 | 2015-10-24 11:44:13 +0100 | [diff] [blame] | 41 | QCryptoHashAlgorithm kdfhash; |
| 42 | size_t niv; |
| 43 | uint64_t payload_offset; /* In bytes */ |
Daniel P. Berrange | 850f49d | 2017-09-27 13:53:36 +0100 | [diff] [blame] | 44 | uint64_t sector_size; /* In bytes */ |
Daniel P. Berrange | 7d96901 | 2015-10-24 11:44:13 +0100 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | struct QCryptoBlockDriver { |
| 48 | int (*open)(QCryptoBlock *block, |
| 49 | QCryptoBlockOpenOptions *options, |
Daniel P. Berrange | 1cd9a78 | 2017-06-23 17:24:17 +0100 | [diff] [blame] | 50 | const char *optprefix, |
Daniel P. Berrange | 7d96901 | 2015-10-24 11:44:13 +0100 | [diff] [blame] | 51 | QCryptoBlockReadFunc readfunc, |
| 52 | void *opaque, |
| 53 | unsigned int flags, |
Vladimir Sementsov-Ogievskiy | c972fa1 | 2018-12-07 19:13:51 +0300 | [diff] [blame] | 54 | size_t n_threads, |
Daniel P. Berrange | 7d96901 | 2015-10-24 11:44:13 +0100 | [diff] [blame] | 55 | Error **errp); |
| 56 | |
| 57 | int (*create)(QCryptoBlock *block, |
| 58 | QCryptoBlockCreateOptions *options, |
Daniel P. Berrange | 1cd9a78 | 2017-06-23 17:24:17 +0100 | [diff] [blame] | 59 | const char *optprefix, |
Daniel P. Berrange | 7d96901 | 2015-10-24 11:44:13 +0100 | [diff] [blame] | 60 | QCryptoBlockInitFunc initfunc, |
| 61 | QCryptoBlockWriteFunc writefunc, |
| 62 | void *opaque, |
| 63 | Error **errp); |
| 64 | |
Daniel P. Berrange | 40c8502 | 2016-07-22 13:53:34 +0100 | [diff] [blame] | 65 | int (*get_info)(QCryptoBlock *block, |
| 66 | QCryptoBlockInfo *info, |
| 67 | Error **errp); |
| 68 | |
Daniel P. Berrange | 7d96901 | 2015-10-24 11:44:13 +0100 | [diff] [blame] | 69 | void (*cleanup)(QCryptoBlock *block); |
| 70 | |
| 71 | int (*encrypt)(QCryptoBlock *block, |
| 72 | uint64_t startsector, |
| 73 | uint8_t *buf, |
| 74 | size_t len, |
| 75 | Error **errp); |
| 76 | int (*decrypt)(QCryptoBlock *block, |
| 77 | uint64_t startsector, |
| 78 | uint8_t *buf, |
| 79 | size_t len, |
| 80 | Error **errp); |
| 81 | |
| 82 | bool (*has_format)(const uint8_t *buf, |
| 83 | size_t buflen); |
| 84 | }; |
| 85 | |
| 86 | |
Vladimir Sementsov-Ogievskiy | 0270417 | 2018-12-07 19:13:49 +0300 | [diff] [blame] | 87 | int qcrypto_block_cipher_decrypt_helper(QCryptoCipher *cipher, |
| 88 | size_t niv, |
| 89 | QCryptoIVGen *ivgen, |
| 90 | int sectorsize, |
| 91 | uint64_t offset, |
| 92 | uint8_t *buf, |
| 93 | size_t len, |
| 94 | Error **errp); |
Daniel P. Berrange | 7d96901 | 2015-10-24 11:44:13 +0100 | [diff] [blame] | 95 | |
Vladimir Sementsov-Ogievskiy | 0270417 | 2018-12-07 19:13:49 +0300 | [diff] [blame] | 96 | int qcrypto_block_cipher_encrypt_helper(QCryptoCipher *cipher, |
| 97 | size_t niv, |
| 98 | QCryptoIVGen *ivgen, |
| 99 | int sectorsize, |
| 100 | uint64_t offset, |
| 101 | uint8_t *buf, |
| 102 | size_t len, |
| 103 | Error **errp); |
Daniel P. Berrange | 7d96901 | 2015-10-24 11:44:13 +0100 | [diff] [blame] | 104 | |
Vladimir Sementsov-Ogievskiy | 0f0d596 | 2018-12-07 19:13:50 +0300 | [diff] [blame] | 105 | int qcrypto_block_decrypt_helper(QCryptoBlock *block, |
| 106 | int sectorsize, |
| 107 | uint64_t offset, |
| 108 | uint8_t *buf, |
| 109 | size_t len, |
| 110 | Error **errp); |
| 111 | |
| 112 | int qcrypto_block_encrypt_helper(QCryptoBlock *block, |
| 113 | int sectorsize, |
| 114 | uint64_t offset, |
| 115 | uint8_t *buf, |
| 116 | size_t len, |
| 117 | Error **errp); |
| 118 | |
Vladimir Sementsov-Ogievskiy | c972fa1 | 2018-12-07 19:13:51 +0300 | [diff] [blame] | 119 | int qcrypto_block_init_cipher(QCryptoBlock *block, |
| 120 | QCryptoCipherAlgorithm alg, |
| 121 | QCryptoCipherMode mode, |
| 122 | const uint8_t *key, size_t nkey, |
| 123 | size_t n_threads, Error **errp); |
| 124 | |
| 125 | void qcrypto_block_free_cipher(QCryptoBlock *block); |
| 126 | |
Markus Armbruster | 121d071 | 2016-06-29 10:12:57 +0200 | [diff] [blame] | 127 | #endif /* QCRYPTO_BLOCKPRIV_H */ |