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 /* Cipher parameters */ 36 QCryptoCipherAlgorithm alg; 37 QCryptoCipherMode mode; 38 uint8_t *key; 39 size_t nkey; 40 41 QCryptoCipher **free_ciphers; 42 size_t max_free_ciphers; 43 size_t n_free_ciphers; 44 QCryptoIVGen *ivgen; 45 QemuMutex mutex; 46 47 QCryptoHashAlgorithm kdfhash; 48 size_t niv; 49 uint64_t payload_offset; /* In bytes */ 50 uint64_t sector_size; /* In bytes */ 51 52 bool detached_header; /* True if disk has a detached LUKS header */ 53 }; 54 55 struct QCryptoBlockDriver { 56 int (*open)(QCryptoBlock *block, 57 QCryptoBlockOpenOptions *options, 58 const char *optprefix, 59 QCryptoBlockReadFunc readfunc, 60 void *opaque, 61 unsigned int flags, 62 Error **errp); 63 64 int (*create)(QCryptoBlock *block, 65 QCryptoBlockCreateOptions *options, 66 const char *optprefix, 67 QCryptoBlockInitFunc initfunc, 68 QCryptoBlockWriteFunc writefunc, 69 void *opaque, 70 Error **errp); 71 72 int (*amend)(QCryptoBlock *block, 73 QCryptoBlockReadFunc readfunc, 74 QCryptoBlockWriteFunc writefunc, 75 void *opaque, 76 QCryptoBlockAmendOptions *options, 77 bool force, 78 Error **errp); 79 80 int (*get_info)(QCryptoBlock *block, 81 QCryptoBlockInfo *info, 82 Error **errp); 83 84 void (*cleanup)(QCryptoBlock *block); 85 86 int (*encrypt)(QCryptoBlock *block, 87 uint64_t startsector, 88 uint8_t *buf, 89 size_t len, 90 Error **errp); 91 int (*decrypt)(QCryptoBlock *block, 92 uint64_t startsector, 93 uint8_t *buf, 94 size_t len, 95 Error **errp); 96 97 bool (*has_format)(const uint8_t *buf, 98 size_t buflen); 99 }; 100 101 102 int qcrypto_block_cipher_decrypt_helper(QCryptoCipher *cipher, 103 size_t niv, 104 QCryptoIVGen *ivgen, 105 int sectorsize, 106 uint64_t offset, 107 uint8_t *buf, 108 size_t len, 109 Error **errp); 110 111 int qcrypto_block_cipher_encrypt_helper(QCryptoCipher *cipher, 112 size_t niv, 113 QCryptoIVGen *ivgen, 114 int sectorsize, 115 uint64_t offset, 116 uint8_t *buf, 117 size_t len, 118 Error **errp); 119 120 int qcrypto_block_decrypt_helper(QCryptoBlock *block, 121 int sectorsize, 122 uint64_t offset, 123 uint8_t *buf, 124 size_t len, 125 Error **errp); 126 127 int qcrypto_block_encrypt_helper(QCryptoBlock *block, 128 int sectorsize, 129 uint64_t offset, 130 uint8_t *buf, 131 size_t len, 132 Error **errp); 133 134 int qcrypto_block_init_cipher(QCryptoBlock *block, 135 QCryptoCipherAlgorithm alg, 136 QCryptoCipherMode mode, 137 const uint8_t *key, size_t nkey, 138 Error **errp); 139 140 void qcrypto_block_free_cipher(QCryptoBlock *block); 141 142 #endif /* QCRYPTO_BLOCKPRIV_H */ 143