block.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. #ifndef QCRYPTO_BLOCK_H
  21. #define QCRYPTO_BLOCK_H
  22. #include "crypto/cipher.h"
  23. #include "crypto/ivgen.h"
  24. typedef struct QCryptoBlock QCryptoBlock;
  25. /* See also QCryptoBlockFormat, QCryptoBlockCreateOptions
  26. * and QCryptoBlockOpenOptions in qapi/crypto.json */
  27. typedef int (*QCryptoBlockReadFunc)(QCryptoBlock *block,
  28. size_t offset,
  29. uint8_t *buf,
  30. size_t buflen,
  31. void *opaque,
  32. Error **errp);
  33. typedef int (*QCryptoBlockInitFunc)(QCryptoBlock *block,
  34. size_t headerlen,
  35. void *opaque,
  36. Error **errp);
  37. typedef int (*QCryptoBlockWriteFunc)(QCryptoBlock *block,
  38. size_t offset,
  39. const uint8_t *buf,
  40. size_t buflen,
  41. void *opaque,
  42. Error **errp);
  43. /**
  44. * qcrypto_block_has_format:
  45. * @format: the encryption format
  46. * @buf: the data from head of the volume
  47. * @len: the length of @buf in bytes
  48. *
  49. * Given @len bytes of data from the head of a storage volume
  50. * in @buf, probe to determine if the volume has the encryption
  51. * format specified in @format.
  52. *
  53. * Returns: true if the data in @buf matches @format
  54. */
  55. bool qcrypto_block_has_format(QCryptoBlockFormat format,
  56. const uint8_t *buf,
  57. size_t buflen);
  58. typedef enum {
  59. QCRYPTO_BLOCK_OPEN_NO_IO = (1 << 0),
  60. QCRYPTO_BLOCK_OPEN_DETACHED = (1 << 1),
  61. } QCryptoBlockOpenFlags;
  62. /**
  63. * qcrypto_block_open:
  64. * @options: the encryption options
  65. * @optprefix: name prefix for options
  66. * @readfunc: callback for reading data from the volume
  67. * @opaque: data to pass to @readfunc
  68. * @flags: bitmask of QCryptoBlockOpenFlags values
  69. * @errp: pointer to a NULL-initialized error object
  70. *
  71. * Create a new block encryption object for an existing
  72. * storage volume encrypted with format identified by
  73. * the parameters in @options.
  74. *
  75. * This will use @readfunc to initialize the encryption
  76. * context based on the volume header(s), extracting the
  77. * master key(s) as required.
  78. *
  79. * If @flags contains QCRYPTO_BLOCK_OPEN_NO_IO then
  80. * the open process will be optimized to skip any parts
  81. * that are only required to perform I/O. In particular
  82. * this would usually avoid the need to decrypt any
  83. * master keys. The only thing that can be done with
  84. * the resulting QCryptoBlock object would be to query
  85. * metadata such as the payload offset. There will be
  86. * no cipher or ivgen objects available.
  87. *
  88. * If @flags contains QCRYPTO_BLOCK_OPEN_DETACHED then
  89. * the open process will be optimized to skip the LUKS
  90. * payload overlap check.
  91. *
  92. * If any part of initializing the encryption context
  93. * fails an error will be returned. This could be due
  94. * to the volume being in the wrong format, a cipher
  95. * or IV generator algorithm that is not supported,
  96. * or incorrect passphrases.
  97. *
  98. * Returns: a block encryption format, or NULL on error
  99. */
  100. QCryptoBlock *qcrypto_block_open(QCryptoBlockOpenOptions *options,
  101. const char *optprefix,
  102. QCryptoBlockReadFunc readfunc,
  103. void *opaque,
  104. unsigned int flags,
  105. Error **errp);
  106. typedef enum {
  107. QCRYPTO_BLOCK_CREATE_DETACHED = (1 << 0),
  108. } QCryptoBlockCreateFlags;
  109. /**
  110. * qcrypto_block_create:
  111. * @options: the encryption options
  112. * @optprefix: name prefix for options
  113. * @initfunc: callback for initializing volume header
  114. * @writefunc: callback for writing data to the volume header
  115. * @opaque: data to pass to @initfunc and @writefunc
  116. * @flags: bitmask of QCryptoBlockCreateFlags values
  117. * @errp: pointer to a NULL-initialized error object
  118. *
  119. * Create a new block encryption object for initializing
  120. * a storage volume to be encrypted with format identified
  121. * by the parameters in @options.
  122. *
  123. * This method will allocate space for a new volume header
  124. * using @initfunc and then write header data using @writefunc,
  125. * generating new master keys, etc as required. Any existing
  126. * data present on the volume will be irrevocably destroyed.
  127. *
  128. * If @flags contains QCRYPTO_BLOCK_CREATE_DETACHED then
  129. * the open process will set the payload_offset_sector to 0
  130. * to specify the starting point for the read/write of a
  131. * detached LUKS header image.
  132. *
  133. * If any part of initializing the encryption context
  134. * fails an error will be returned. This could be due
  135. * to the volume being in the wrong format, a cipher
  136. * or IV generator algorithm that is not supported,
  137. * or incorrect passphrases.
  138. *
  139. * Returns: a block encryption format, or NULL on error
  140. */
  141. QCryptoBlock *qcrypto_block_create(QCryptoBlockCreateOptions *options,
  142. const char *optprefix,
  143. QCryptoBlockInitFunc initfunc,
  144. QCryptoBlockWriteFunc writefunc,
  145. void *opaque,
  146. unsigned int flags,
  147. Error **errp);
  148. /**
  149. * qcrypto_block_amend_options:
  150. * @block: the block encryption object
  151. *
  152. * @readfunc: callback for reading data from the volume header
  153. * @writefunc: callback for writing data to the volume header
  154. * @opaque: data to pass to @readfunc and @writefunc
  155. * @options: the new/amended encryption options
  156. * @force: hint for the driver to allow unsafe operation
  157. * @errp: error pointer
  158. *
  159. * Changes the crypto options of the encryption format
  160. *
  161. */
  162. int qcrypto_block_amend_options(QCryptoBlock *block,
  163. QCryptoBlockReadFunc readfunc,
  164. QCryptoBlockWriteFunc writefunc,
  165. void *opaque,
  166. QCryptoBlockAmendOptions *options,
  167. bool force,
  168. Error **errp);
  169. /**
  170. * qcrypto_block_calculate_payload_offset:
  171. * @create_opts: the encryption options
  172. * @optprefix: name prefix for options
  173. * @len: output for number of header bytes before payload
  174. * @errp: pointer to a NULL-initialized error object
  175. *
  176. * Calculate the number of header bytes before the payload in an encrypted
  177. * storage volume. The header is an area before the payload that is reserved
  178. * for encryption metadata.
  179. *
  180. * Returns: true on success, false on error
  181. */
  182. bool
  183. qcrypto_block_calculate_payload_offset(QCryptoBlockCreateOptions *create_opts,
  184. const char *optprefix,
  185. size_t *len,
  186. Error **errp);
  187. /**
  188. * qcrypto_block_get_info:
  189. * @block: the block encryption object
  190. * @errp: pointer to a NULL-initialized error object
  191. *
  192. * Get information about the configuration options for the
  193. * block encryption object. This includes details such as
  194. * the cipher algorithms, modes, and initialization vector
  195. * generators.
  196. *
  197. * Returns: a block encryption info object, or NULL on error
  198. */
  199. QCryptoBlockInfo *qcrypto_block_get_info(QCryptoBlock *block,
  200. Error **errp);
  201. /**
  202. * @qcrypto_block_decrypt:
  203. * @block: the block encryption object
  204. * @offset: the position at which @iov was read
  205. * @buf: the buffer to decrypt
  206. * @len: the length of @buf in bytes
  207. * @errp: pointer to a NULL-initialized error object
  208. *
  209. * Decrypt @len bytes of cipher text in @buf, writing
  210. * plain text back into @buf. @len and @offset must be
  211. * a multiple of the encryption format sector size.
  212. *
  213. * Returns 0 on success, -1 on failure
  214. */
  215. int qcrypto_block_decrypt(QCryptoBlock *block,
  216. uint64_t offset,
  217. uint8_t *buf,
  218. size_t len,
  219. Error **errp);
  220. /**
  221. * @qcrypto_block_encrypt:
  222. * @block: the block encryption object
  223. * @offset: the position at which @iov will be written
  224. * @buf: the buffer to decrypt
  225. * @len: the length of @buf in bytes
  226. * @errp: pointer to a NULL-initialized error object
  227. *
  228. * Encrypt @len bytes of plain text in @buf, writing
  229. * cipher text back into @buf. @len and @offset must be
  230. * a multiple of the encryption format sector size.
  231. *
  232. * Returns 0 on success, -1 on failure
  233. */
  234. int qcrypto_block_encrypt(QCryptoBlock *block,
  235. uint64_t offset,
  236. uint8_t *buf,
  237. size_t len,
  238. Error **errp);
  239. /**
  240. * qcrypto_block_get_cipher:
  241. * @block: the block encryption object
  242. *
  243. * Get the cipher to use for payload encryption
  244. *
  245. * Returns: the cipher object
  246. */
  247. QCryptoCipher *qcrypto_block_get_cipher(QCryptoBlock *block);
  248. /**
  249. * qcrypto_block_get_ivgen:
  250. * @block: the block encryption object
  251. *
  252. * Get the initialization vector generator to use for
  253. * payload encryption
  254. *
  255. * Returns: the IV generator object
  256. */
  257. QCryptoIVGen *qcrypto_block_get_ivgen(QCryptoBlock *block);
  258. /**
  259. * qcrypto_block_get_kdf_hash:
  260. * @block: the block encryption object
  261. *
  262. * Get the hash algorithm used with the key derivation
  263. * function
  264. *
  265. * Returns: the hash algorithm
  266. */
  267. QCryptoHashAlgo qcrypto_block_get_kdf_hash(QCryptoBlock *block);
  268. /**
  269. * qcrypto_block_get_payload_offset:
  270. * @block: the block encryption object
  271. *
  272. * Get the offset to the payload indicated by the
  273. * encryption header, in bytes.
  274. *
  275. * Returns: the payload offset in bytes
  276. */
  277. uint64_t qcrypto_block_get_payload_offset(QCryptoBlock *block);
  278. /**
  279. * qcrypto_block_get_sector_size:
  280. * @block: the block encryption object
  281. *
  282. * Get the size of sectors used for payload encryption. A new
  283. * IV is used at the start of each sector. The encryption
  284. * sector size is not required to match the sector size of the
  285. * underlying storage. For example LUKS will always use a 512
  286. * byte sector size, even if the volume is on a disk with 4k
  287. * sectors.
  288. *
  289. * Returns: the sector in bytes
  290. */
  291. uint64_t qcrypto_block_get_sector_size(QCryptoBlock *block);
  292. /**
  293. * qcrypto_block_free:
  294. * @block: the block encryption object
  295. *
  296. * Release all resources associated with the encryption
  297. * object
  298. */
  299. void qcrypto_block_free(QCryptoBlock *block);
  300. G_DEFINE_AUTOPTR_CLEANUP_FUNC(QCryptoBlock, qcrypto_block_free)
  301. #endif /* QCRYPTO_BLOCK_H */