block-luks.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568
  1. /*
  2. * QEMU Crypto block device encryption LUKS format
  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. #include "qemu/osdep.h"
  21. #include "qapi/error.h"
  22. #include "qemu/bswap.h"
  23. #include "block-luks.h"
  24. #include "crypto/hash.h"
  25. #include "crypto/afsplit.h"
  26. #include "crypto/pbkdf.h"
  27. #include "crypto/secret.h"
  28. #include "crypto/random.h"
  29. #include "qemu/uuid.h"
  30. #include "qemu/coroutine.h"
  31. /*
  32. * Reference for the LUKS format implemented here is
  33. *
  34. * docs/on-disk-format.pdf
  35. *
  36. * in 'cryptsetup' package source code
  37. *
  38. * This file implements the 1.2.1 specification, dated
  39. * Oct 16, 2011.
  40. */
  41. typedef struct QCryptoBlockLUKS QCryptoBlockLUKS;
  42. typedef struct QCryptoBlockLUKSHeader QCryptoBlockLUKSHeader;
  43. typedef struct QCryptoBlockLUKSKeySlot QCryptoBlockLUKSKeySlot;
  44. /* The following constants are all defined by the LUKS spec */
  45. #define QCRYPTO_BLOCK_LUKS_VERSION 1
  46. #define QCRYPTO_BLOCK_LUKS_MAGIC_LEN 6
  47. #define QCRYPTO_BLOCK_LUKS_CIPHER_NAME_LEN 32
  48. #define QCRYPTO_BLOCK_LUKS_CIPHER_MODE_LEN 32
  49. #define QCRYPTO_BLOCK_LUKS_HASH_SPEC_LEN 32
  50. #define QCRYPTO_BLOCK_LUKS_DIGEST_LEN 20
  51. #define QCRYPTO_BLOCK_LUKS_SALT_LEN 32
  52. #define QCRYPTO_BLOCK_LUKS_UUID_LEN 40
  53. #define QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS 8
  54. #define QCRYPTO_BLOCK_LUKS_STRIPES 4000
  55. #define QCRYPTO_BLOCK_LUKS_MIN_SLOT_KEY_ITERS 1000
  56. #define QCRYPTO_BLOCK_LUKS_MIN_MASTER_KEY_ITERS 1000
  57. #define QCRYPTO_BLOCK_LUKS_KEY_SLOT_OFFSET 4096
  58. #define QCRYPTO_BLOCK_LUKS_KEY_SLOT_DISABLED 0x0000DEAD
  59. #define QCRYPTO_BLOCK_LUKS_KEY_SLOT_ENABLED 0x00AC71F3
  60. #define QCRYPTO_BLOCK_LUKS_SECTOR_SIZE 512LL
  61. static const char qcrypto_block_luks_magic[QCRYPTO_BLOCK_LUKS_MAGIC_LEN] = {
  62. 'L', 'U', 'K', 'S', 0xBA, 0xBE
  63. };
  64. typedef struct QCryptoBlockLUKSNameMap QCryptoBlockLUKSNameMap;
  65. struct QCryptoBlockLUKSNameMap {
  66. const char *name;
  67. int id;
  68. };
  69. typedef struct QCryptoBlockLUKSCipherSizeMap QCryptoBlockLUKSCipherSizeMap;
  70. struct QCryptoBlockLUKSCipherSizeMap {
  71. uint32_t key_bytes;
  72. int id;
  73. };
  74. typedef struct QCryptoBlockLUKSCipherNameMap QCryptoBlockLUKSCipherNameMap;
  75. struct QCryptoBlockLUKSCipherNameMap {
  76. const char *name;
  77. const QCryptoBlockLUKSCipherSizeMap *sizes;
  78. };
  79. static const QCryptoBlockLUKSCipherSizeMap
  80. qcrypto_block_luks_cipher_size_map_aes[] = {
  81. { 16, QCRYPTO_CIPHER_ALG_AES_128 },
  82. { 24, QCRYPTO_CIPHER_ALG_AES_192 },
  83. { 32, QCRYPTO_CIPHER_ALG_AES_256 },
  84. { 0, 0 },
  85. };
  86. static const QCryptoBlockLUKSCipherSizeMap
  87. qcrypto_block_luks_cipher_size_map_cast5[] = {
  88. { 16, QCRYPTO_CIPHER_ALG_CAST5_128 },
  89. { 0, 0 },
  90. };
  91. static const QCryptoBlockLUKSCipherSizeMap
  92. qcrypto_block_luks_cipher_size_map_serpent[] = {
  93. { 16, QCRYPTO_CIPHER_ALG_SERPENT_128 },
  94. { 24, QCRYPTO_CIPHER_ALG_SERPENT_192 },
  95. { 32, QCRYPTO_CIPHER_ALG_SERPENT_256 },
  96. { 0, 0 },
  97. };
  98. static const QCryptoBlockLUKSCipherSizeMap
  99. qcrypto_block_luks_cipher_size_map_twofish[] = {
  100. { 16, QCRYPTO_CIPHER_ALG_TWOFISH_128 },
  101. { 24, QCRYPTO_CIPHER_ALG_TWOFISH_192 },
  102. { 32, QCRYPTO_CIPHER_ALG_TWOFISH_256 },
  103. { 0, 0 },
  104. };
  105. static const QCryptoBlockLUKSCipherNameMap
  106. qcrypto_block_luks_cipher_name_map[] = {
  107. { "aes", qcrypto_block_luks_cipher_size_map_aes },
  108. { "cast5", qcrypto_block_luks_cipher_size_map_cast5 },
  109. { "serpent", qcrypto_block_luks_cipher_size_map_serpent },
  110. { "twofish", qcrypto_block_luks_cipher_size_map_twofish },
  111. };
  112. /*
  113. * This struct is written to disk in big-endian format,
  114. * but operated upon in native-endian format.
  115. */
  116. struct QCryptoBlockLUKSKeySlot {
  117. /* state of keyslot, enabled/disable */
  118. uint32_t active;
  119. /* iterations for PBKDF2 */
  120. uint32_t iterations;
  121. /* salt for PBKDF2 */
  122. uint8_t salt[QCRYPTO_BLOCK_LUKS_SALT_LEN];
  123. /* start sector of key material */
  124. uint32_t key_offset_sector;
  125. /* number of anti-forensic stripes */
  126. uint32_t stripes;
  127. };
  128. QEMU_BUILD_BUG_ON(sizeof(struct QCryptoBlockLUKSKeySlot) != 48);
  129. /*
  130. * This struct is written to disk in big-endian format,
  131. * but operated upon in native-endian format.
  132. */
  133. struct QCryptoBlockLUKSHeader {
  134. /* 'L', 'U', 'K', 'S', '0xBA', '0xBE' */
  135. char magic[QCRYPTO_BLOCK_LUKS_MAGIC_LEN];
  136. /* LUKS version, currently 1 */
  137. uint16_t version;
  138. /* cipher name specification (aes, etc) */
  139. char cipher_name[QCRYPTO_BLOCK_LUKS_CIPHER_NAME_LEN];
  140. /* cipher mode specification (cbc-plain, xts-essiv:sha256, etc) */
  141. char cipher_mode[QCRYPTO_BLOCK_LUKS_CIPHER_MODE_LEN];
  142. /* hash specification (sha256, etc) */
  143. char hash_spec[QCRYPTO_BLOCK_LUKS_HASH_SPEC_LEN];
  144. /* start offset of the volume data (in 512 byte sectors) */
  145. uint32_t payload_offset_sector;
  146. /* Number of key bytes */
  147. uint32_t master_key_len;
  148. /* master key checksum after PBKDF2 */
  149. uint8_t master_key_digest[QCRYPTO_BLOCK_LUKS_DIGEST_LEN];
  150. /* salt for master key PBKDF2 */
  151. uint8_t master_key_salt[QCRYPTO_BLOCK_LUKS_SALT_LEN];
  152. /* iterations for master key PBKDF2 */
  153. uint32_t master_key_iterations;
  154. /* UUID of the partition in standard ASCII representation */
  155. uint8_t uuid[QCRYPTO_BLOCK_LUKS_UUID_LEN];
  156. /* key slots */
  157. QCryptoBlockLUKSKeySlot key_slots[QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS];
  158. };
  159. QEMU_BUILD_BUG_ON(sizeof(struct QCryptoBlockLUKSHeader) != 592);
  160. struct QCryptoBlockLUKS {
  161. QCryptoBlockLUKSHeader header;
  162. /* Main encryption algorithm used for encryption*/
  163. QCryptoCipherAlgorithm cipher_alg;
  164. /* Mode of encryption for the selected encryption algorithm */
  165. QCryptoCipherMode cipher_mode;
  166. /* Initialization vector generation algorithm */
  167. QCryptoIVGenAlgorithm ivgen_alg;
  168. /* Hash algorithm used for IV generation*/
  169. QCryptoHashAlgorithm ivgen_hash_alg;
  170. /*
  171. * Encryption algorithm used for IV generation.
  172. * Usually the same as main encryption algorithm
  173. */
  174. QCryptoCipherAlgorithm ivgen_cipher_alg;
  175. /* Hash algorithm used in pbkdf2 function */
  176. QCryptoHashAlgorithm hash_alg;
  177. };
  178. static int qcrypto_block_luks_cipher_name_lookup(const char *name,
  179. QCryptoCipherMode mode,
  180. uint32_t key_bytes,
  181. Error **errp)
  182. {
  183. const QCryptoBlockLUKSCipherNameMap *map =
  184. qcrypto_block_luks_cipher_name_map;
  185. size_t maplen = G_N_ELEMENTS(qcrypto_block_luks_cipher_name_map);
  186. size_t i, j;
  187. if (mode == QCRYPTO_CIPHER_MODE_XTS) {
  188. key_bytes /= 2;
  189. }
  190. for (i = 0; i < maplen; i++) {
  191. if (!g_str_equal(map[i].name, name)) {
  192. continue;
  193. }
  194. for (j = 0; j < map[i].sizes[j].key_bytes; j++) {
  195. if (map[i].sizes[j].key_bytes == key_bytes) {
  196. return map[i].sizes[j].id;
  197. }
  198. }
  199. }
  200. error_setg(errp, "Algorithm %s with key size %d bytes not supported",
  201. name, key_bytes);
  202. return 0;
  203. }
  204. static const char *
  205. qcrypto_block_luks_cipher_alg_lookup(QCryptoCipherAlgorithm alg,
  206. Error **errp)
  207. {
  208. const QCryptoBlockLUKSCipherNameMap *map =
  209. qcrypto_block_luks_cipher_name_map;
  210. size_t maplen = G_N_ELEMENTS(qcrypto_block_luks_cipher_name_map);
  211. size_t i, j;
  212. for (i = 0; i < maplen; i++) {
  213. for (j = 0; j < map[i].sizes[j].key_bytes; j++) {
  214. if (map[i].sizes[j].id == alg) {
  215. return map[i].name;
  216. }
  217. }
  218. }
  219. error_setg(errp, "Algorithm '%s' not supported",
  220. QCryptoCipherAlgorithm_str(alg));
  221. return NULL;
  222. }
  223. /* XXX replace with qapi_enum_parse() in future, when we can
  224. * make that function emit a more friendly error message */
  225. static int qcrypto_block_luks_name_lookup(const char *name,
  226. const QEnumLookup *map,
  227. const char *type,
  228. Error **errp)
  229. {
  230. int ret = qapi_enum_parse(map, name, -1, NULL);
  231. if (ret < 0) {
  232. error_setg(errp, "%s %s not supported", type, name);
  233. return 0;
  234. }
  235. return ret;
  236. }
  237. #define qcrypto_block_luks_cipher_mode_lookup(name, errp) \
  238. qcrypto_block_luks_name_lookup(name, \
  239. &QCryptoCipherMode_lookup, \
  240. "Cipher mode", \
  241. errp)
  242. #define qcrypto_block_luks_hash_name_lookup(name, errp) \
  243. qcrypto_block_luks_name_lookup(name, \
  244. &QCryptoHashAlgorithm_lookup, \
  245. "Hash algorithm", \
  246. errp)
  247. #define qcrypto_block_luks_ivgen_name_lookup(name, errp) \
  248. qcrypto_block_luks_name_lookup(name, \
  249. &QCryptoIVGenAlgorithm_lookup, \
  250. "IV generator", \
  251. errp)
  252. static bool
  253. qcrypto_block_luks_has_format(const uint8_t *buf,
  254. size_t buf_size)
  255. {
  256. const QCryptoBlockLUKSHeader *luks_header = (const void *)buf;
  257. if (buf_size >= offsetof(QCryptoBlockLUKSHeader, cipher_name) &&
  258. memcmp(luks_header->magic, qcrypto_block_luks_magic,
  259. QCRYPTO_BLOCK_LUKS_MAGIC_LEN) == 0 &&
  260. be16_to_cpu(luks_header->version) == QCRYPTO_BLOCK_LUKS_VERSION) {
  261. return true;
  262. } else {
  263. return false;
  264. }
  265. }
  266. /**
  267. * Deal with a quirk of dm-crypt usage of ESSIV.
  268. *
  269. * When calculating ESSIV IVs, the cipher length used by ESSIV
  270. * may be different from the cipher length used for the block
  271. * encryption, becauses dm-crypt uses the hash digest length
  272. * as the key size. ie, if you have AES 128 as the block cipher
  273. * and SHA 256 as ESSIV hash, then ESSIV will use AES 256 as
  274. * the cipher since that gets a key length matching the digest
  275. * size, not AES 128 with truncated digest as might be imagined
  276. */
  277. static QCryptoCipherAlgorithm
  278. qcrypto_block_luks_essiv_cipher(QCryptoCipherAlgorithm cipher,
  279. QCryptoHashAlgorithm hash,
  280. Error **errp)
  281. {
  282. size_t digestlen = qcrypto_hash_digest_len(hash);
  283. size_t keylen = qcrypto_cipher_get_key_len(cipher);
  284. if (digestlen == keylen) {
  285. return cipher;
  286. }
  287. switch (cipher) {
  288. case QCRYPTO_CIPHER_ALG_AES_128:
  289. case QCRYPTO_CIPHER_ALG_AES_192:
  290. case QCRYPTO_CIPHER_ALG_AES_256:
  291. if (digestlen == qcrypto_cipher_get_key_len(
  292. QCRYPTO_CIPHER_ALG_AES_128)) {
  293. return QCRYPTO_CIPHER_ALG_AES_128;
  294. } else if (digestlen == qcrypto_cipher_get_key_len(
  295. QCRYPTO_CIPHER_ALG_AES_192)) {
  296. return QCRYPTO_CIPHER_ALG_AES_192;
  297. } else if (digestlen == qcrypto_cipher_get_key_len(
  298. QCRYPTO_CIPHER_ALG_AES_256)) {
  299. return QCRYPTO_CIPHER_ALG_AES_256;
  300. } else {
  301. error_setg(errp, "No AES cipher with key size %zu available",
  302. digestlen);
  303. return 0;
  304. }
  305. break;
  306. case QCRYPTO_CIPHER_ALG_SERPENT_128:
  307. case QCRYPTO_CIPHER_ALG_SERPENT_192:
  308. case QCRYPTO_CIPHER_ALG_SERPENT_256:
  309. if (digestlen == qcrypto_cipher_get_key_len(
  310. QCRYPTO_CIPHER_ALG_SERPENT_128)) {
  311. return QCRYPTO_CIPHER_ALG_SERPENT_128;
  312. } else if (digestlen == qcrypto_cipher_get_key_len(
  313. QCRYPTO_CIPHER_ALG_SERPENT_192)) {
  314. return QCRYPTO_CIPHER_ALG_SERPENT_192;
  315. } else if (digestlen == qcrypto_cipher_get_key_len(
  316. QCRYPTO_CIPHER_ALG_SERPENT_256)) {
  317. return QCRYPTO_CIPHER_ALG_SERPENT_256;
  318. } else {
  319. error_setg(errp, "No Serpent cipher with key size %zu available",
  320. digestlen);
  321. return 0;
  322. }
  323. break;
  324. case QCRYPTO_CIPHER_ALG_TWOFISH_128:
  325. case QCRYPTO_CIPHER_ALG_TWOFISH_192:
  326. case QCRYPTO_CIPHER_ALG_TWOFISH_256:
  327. if (digestlen == qcrypto_cipher_get_key_len(
  328. QCRYPTO_CIPHER_ALG_TWOFISH_128)) {
  329. return QCRYPTO_CIPHER_ALG_TWOFISH_128;
  330. } else if (digestlen == qcrypto_cipher_get_key_len(
  331. QCRYPTO_CIPHER_ALG_TWOFISH_192)) {
  332. return QCRYPTO_CIPHER_ALG_TWOFISH_192;
  333. } else if (digestlen == qcrypto_cipher_get_key_len(
  334. QCRYPTO_CIPHER_ALG_TWOFISH_256)) {
  335. return QCRYPTO_CIPHER_ALG_TWOFISH_256;
  336. } else {
  337. error_setg(errp, "No Twofish cipher with key size %zu available",
  338. digestlen);
  339. return 0;
  340. }
  341. break;
  342. default:
  343. error_setg(errp, "Cipher %s not supported with essiv",
  344. QCryptoCipherAlgorithm_str(cipher));
  345. return 0;
  346. }
  347. }
  348. /*
  349. * Returns number of sectors needed to store the key material
  350. * given number of anti forensic stripes
  351. */
  352. static int
  353. qcrypto_block_luks_splitkeylen_sectors(const QCryptoBlockLUKS *luks,
  354. unsigned int header_sectors,
  355. unsigned int stripes)
  356. {
  357. /*
  358. * This calculation doesn't match that shown in the spec,
  359. * but instead follows the cryptsetup implementation.
  360. */
  361. size_t splitkeylen = luks->header.master_key_len * stripes;
  362. /* First align the key material size to block size*/
  363. size_t splitkeylen_sectors =
  364. DIV_ROUND_UP(splitkeylen, QCRYPTO_BLOCK_LUKS_SECTOR_SIZE);
  365. /* Then also align the key material size to the size of the header */
  366. return ROUND_UP(splitkeylen_sectors, header_sectors);
  367. }
  368. /*
  369. * Stores the main LUKS header, taking care of endianess
  370. */
  371. static int
  372. qcrypto_block_luks_store_header(QCryptoBlock *block,
  373. QCryptoBlockWriteFunc writefunc,
  374. void *opaque,
  375. Error **errp)
  376. {
  377. const QCryptoBlockLUKS *luks = block->opaque;
  378. Error *local_err = NULL;
  379. size_t i;
  380. g_autofree QCryptoBlockLUKSHeader *hdr_copy = NULL;
  381. /* Create a copy of the header */
  382. hdr_copy = g_new0(QCryptoBlockLUKSHeader, 1);
  383. memcpy(hdr_copy, &luks->header, sizeof(QCryptoBlockLUKSHeader));
  384. /*
  385. * Everything on disk uses Big Endian (tm), so flip header fields
  386. * before writing them
  387. */
  388. cpu_to_be16s(&hdr_copy->version);
  389. cpu_to_be32s(&hdr_copy->payload_offset_sector);
  390. cpu_to_be32s(&hdr_copy->master_key_len);
  391. cpu_to_be32s(&hdr_copy->master_key_iterations);
  392. for (i = 0; i < QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS; i++) {
  393. cpu_to_be32s(&hdr_copy->key_slots[i].active);
  394. cpu_to_be32s(&hdr_copy->key_slots[i].iterations);
  395. cpu_to_be32s(&hdr_copy->key_slots[i].key_offset_sector);
  396. cpu_to_be32s(&hdr_copy->key_slots[i].stripes);
  397. }
  398. /* Write out the partition header and key slot headers */
  399. writefunc(block, 0, (const uint8_t *)hdr_copy, sizeof(*hdr_copy),
  400. opaque, &local_err);
  401. if (local_err) {
  402. error_propagate(errp, local_err);
  403. return -1;
  404. }
  405. return 0;
  406. }
  407. /*
  408. * Loads the main LUKS header,and byteswaps it to native endianess
  409. * And run basic sanity checks on it
  410. */
  411. static int
  412. qcrypto_block_luks_load_header(QCryptoBlock *block,
  413. QCryptoBlockReadFunc readfunc,
  414. void *opaque,
  415. Error **errp)
  416. {
  417. ssize_t rv;
  418. size_t i;
  419. QCryptoBlockLUKS *luks = block->opaque;
  420. /*
  421. * Read the entire LUKS header, minus the key material from
  422. * the underlying device
  423. */
  424. rv = readfunc(block, 0,
  425. (uint8_t *)&luks->header,
  426. sizeof(luks->header),
  427. opaque,
  428. errp);
  429. if (rv < 0) {
  430. return rv;
  431. }
  432. /*
  433. * The header is always stored in big-endian format, so
  434. * convert everything to native
  435. */
  436. be16_to_cpus(&luks->header.version);
  437. be32_to_cpus(&luks->header.payload_offset_sector);
  438. be32_to_cpus(&luks->header.master_key_len);
  439. be32_to_cpus(&luks->header.master_key_iterations);
  440. for (i = 0; i < QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS; i++) {
  441. be32_to_cpus(&luks->header.key_slots[i].active);
  442. be32_to_cpus(&luks->header.key_slots[i].iterations);
  443. be32_to_cpus(&luks->header.key_slots[i].key_offset_sector);
  444. be32_to_cpus(&luks->header.key_slots[i].stripes);
  445. }
  446. return 0;
  447. }
  448. /*
  449. * Does basic sanity checks on the LUKS header
  450. */
  451. static int
  452. qcrypto_block_luks_check_header(const QCryptoBlockLUKS *luks, Error **errp)
  453. {
  454. size_t i, j;
  455. unsigned int header_sectors = QCRYPTO_BLOCK_LUKS_KEY_SLOT_OFFSET /
  456. QCRYPTO_BLOCK_LUKS_SECTOR_SIZE;
  457. if (memcmp(luks->header.magic, qcrypto_block_luks_magic,
  458. QCRYPTO_BLOCK_LUKS_MAGIC_LEN) != 0) {
  459. error_setg(errp, "Volume is not in LUKS format");
  460. return -1;
  461. }
  462. if (luks->header.version != QCRYPTO_BLOCK_LUKS_VERSION) {
  463. error_setg(errp, "LUKS version %" PRIu32 " is not supported",
  464. luks->header.version);
  465. return -1;
  466. }
  467. /* Check all keyslots for corruption */
  468. for (i = 0 ; i < QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS ; i++) {
  469. const QCryptoBlockLUKSKeySlot *slot1 = &luks->header.key_slots[i];
  470. unsigned int start1 = slot1->key_offset_sector;
  471. unsigned int len1 =
  472. qcrypto_block_luks_splitkeylen_sectors(luks,
  473. header_sectors,
  474. slot1->stripes);
  475. if (slot1->stripes == 0) {
  476. error_setg(errp, "Keyslot %zu is corrupted (stripes == 0)", i);
  477. return -1;
  478. }
  479. if (slot1->active != QCRYPTO_BLOCK_LUKS_KEY_SLOT_DISABLED &&
  480. slot1->active != QCRYPTO_BLOCK_LUKS_KEY_SLOT_ENABLED) {
  481. error_setg(errp,
  482. "Keyslot %zu state (active/disable) is corrupted", i);
  483. return -1;
  484. }
  485. if (start1 + len1 > luks->header.payload_offset_sector) {
  486. error_setg(errp,
  487. "Keyslot %zu is overlapping with the encrypted payload",
  488. i);
  489. return -1;
  490. }
  491. for (j = i + 1 ; j < QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS ; j++) {
  492. const QCryptoBlockLUKSKeySlot *slot2 = &luks->header.key_slots[j];
  493. unsigned int start2 = slot2->key_offset_sector;
  494. unsigned int len2 =
  495. qcrypto_block_luks_splitkeylen_sectors(luks,
  496. header_sectors,
  497. slot2->stripes);
  498. if (start1 + len1 > start2 && start2 + len2 > start1) {
  499. error_setg(errp,
  500. "Keyslots %zu and %zu are overlapping in the header",
  501. i, j);
  502. return -1;
  503. }
  504. }
  505. }
  506. return 0;
  507. }
  508. /*
  509. * Parses the crypto parameters that are stored in the LUKS header
  510. */
  511. static int
  512. qcrypto_block_luks_parse_header(QCryptoBlockLUKS *luks, Error **errp)
  513. {
  514. g_autofree char *cipher_mode = g_strdup(luks->header.cipher_mode);
  515. char *ivgen_name, *ivhash_name;
  516. Error *local_err = NULL;
  517. /*
  518. * The cipher_mode header contains a string that we have
  519. * to further parse, of the format
  520. *
  521. * <cipher-mode>-<iv-generator>[:<iv-hash>]
  522. *
  523. * eg cbc-essiv:sha256, cbc-plain64
  524. */
  525. ivgen_name = strchr(cipher_mode, '-');
  526. if (!ivgen_name) {
  527. error_setg(errp, "Unexpected cipher mode string format %s",
  528. luks->header.cipher_mode);
  529. return -1;
  530. }
  531. *ivgen_name = '\0';
  532. ivgen_name++;
  533. ivhash_name = strchr(ivgen_name, ':');
  534. if (!ivhash_name) {
  535. luks->ivgen_hash_alg = 0;
  536. } else {
  537. *ivhash_name = '\0';
  538. ivhash_name++;
  539. luks->ivgen_hash_alg = qcrypto_block_luks_hash_name_lookup(ivhash_name,
  540. &local_err);
  541. if (local_err) {
  542. error_propagate(errp, local_err);
  543. return -1;
  544. }
  545. }
  546. luks->cipher_mode = qcrypto_block_luks_cipher_mode_lookup(cipher_mode,
  547. &local_err);
  548. if (local_err) {
  549. error_propagate(errp, local_err);
  550. return -1;
  551. }
  552. luks->cipher_alg =
  553. qcrypto_block_luks_cipher_name_lookup(luks->header.cipher_name,
  554. luks->cipher_mode,
  555. luks->header.master_key_len,
  556. &local_err);
  557. if (local_err) {
  558. error_propagate(errp, local_err);
  559. return -1;
  560. }
  561. luks->hash_alg =
  562. qcrypto_block_luks_hash_name_lookup(luks->header.hash_spec,
  563. &local_err);
  564. if (local_err) {
  565. error_propagate(errp, local_err);
  566. return -1;
  567. }
  568. luks->ivgen_alg = qcrypto_block_luks_ivgen_name_lookup(ivgen_name,
  569. &local_err);
  570. if (local_err) {
  571. error_propagate(errp, local_err);
  572. return -1;
  573. }
  574. if (luks->ivgen_alg == QCRYPTO_IVGEN_ALG_ESSIV) {
  575. if (!ivhash_name) {
  576. error_setg(errp, "Missing IV generator hash specification");
  577. return -1;
  578. }
  579. luks->ivgen_cipher_alg =
  580. qcrypto_block_luks_essiv_cipher(luks->cipher_alg,
  581. luks->ivgen_hash_alg,
  582. &local_err);
  583. if (local_err) {
  584. error_propagate(errp, local_err);
  585. return -1;
  586. }
  587. } else {
  588. /*
  589. * Note we parsed the ivhash_name earlier in the cipher_mode
  590. * spec string even with plain/plain64 ivgens, but we
  591. * will ignore it, since it is irrelevant for these ivgens.
  592. * This is for compat with dm-crypt which will silently
  593. * ignore hash names with these ivgens rather than report
  594. * an error about the invalid usage
  595. */
  596. luks->ivgen_cipher_alg = luks->cipher_alg;
  597. }
  598. return 0;
  599. }
  600. /*
  601. * Given a key slot, user password, and the master key,
  602. * will store the encrypted master key there, and update the
  603. * in-memory header. User must then write the in-memory header
  604. *
  605. * Returns:
  606. * 0 if the keyslot was written successfully
  607. * with the provided password
  608. * -1 if a fatal error occurred while storing the key
  609. */
  610. static int
  611. qcrypto_block_luks_store_key(QCryptoBlock *block,
  612. unsigned int slot_idx,
  613. const char *password,
  614. uint8_t *masterkey,
  615. uint64_t iter_time,
  616. QCryptoBlockWriteFunc writefunc,
  617. void *opaque,
  618. Error **errp)
  619. {
  620. QCryptoBlockLUKS *luks = block->opaque;
  621. QCryptoBlockLUKSKeySlot *slot = &luks->header.key_slots[slot_idx];
  622. g_autofree uint8_t *splitkey = NULL;
  623. size_t splitkeylen;
  624. g_autofree uint8_t *slotkey = NULL;
  625. g_autoptr(QCryptoCipher) cipher = NULL;
  626. g_autoptr(QCryptoIVGen) ivgen = NULL;
  627. Error *local_err = NULL;
  628. uint64_t iters;
  629. int ret = -1;
  630. if (qcrypto_random_bytes(slot->salt,
  631. QCRYPTO_BLOCK_LUKS_SALT_LEN,
  632. errp) < 0) {
  633. goto cleanup;
  634. }
  635. splitkeylen = luks->header.master_key_len * slot->stripes;
  636. /*
  637. * Determine how many iterations are required to
  638. * hash the user password while consuming 1 second of compute
  639. * time
  640. */
  641. iters = qcrypto_pbkdf2_count_iters(luks->hash_alg,
  642. (uint8_t *)password, strlen(password),
  643. slot->salt,
  644. QCRYPTO_BLOCK_LUKS_SALT_LEN,
  645. luks->header.master_key_len,
  646. &local_err);
  647. if (local_err) {
  648. error_propagate(errp, local_err);
  649. goto cleanup;
  650. }
  651. if (iters > (ULLONG_MAX / iter_time)) {
  652. error_setg_errno(errp, ERANGE,
  653. "PBKDF iterations %llu too large to scale",
  654. (unsigned long long)iters);
  655. goto cleanup;
  656. }
  657. /* iter_time was in millis, but count_iters reported for secs */
  658. iters = iters * iter_time / 1000;
  659. if (iters > UINT32_MAX) {
  660. error_setg_errno(errp, ERANGE,
  661. "PBKDF iterations %llu larger than %u",
  662. (unsigned long long)iters, UINT32_MAX);
  663. goto cleanup;
  664. }
  665. slot->iterations =
  666. MAX(iters, QCRYPTO_BLOCK_LUKS_MIN_SLOT_KEY_ITERS);
  667. /*
  668. * Generate a key that we'll use to encrypt the master
  669. * key, from the user's password
  670. */
  671. slotkey = g_new0(uint8_t, luks->header.master_key_len);
  672. if (qcrypto_pbkdf2(luks->hash_alg,
  673. (uint8_t *)password, strlen(password),
  674. slot->salt,
  675. QCRYPTO_BLOCK_LUKS_SALT_LEN,
  676. slot->iterations,
  677. slotkey, luks->header.master_key_len,
  678. errp) < 0) {
  679. goto cleanup;
  680. }
  681. /*
  682. * Setup the encryption objects needed to encrypt the
  683. * master key material
  684. */
  685. cipher = qcrypto_cipher_new(luks->cipher_alg,
  686. luks->cipher_mode,
  687. slotkey, luks->header.master_key_len,
  688. errp);
  689. if (!cipher) {
  690. goto cleanup;
  691. }
  692. ivgen = qcrypto_ivgen_new(luks->ivgen_alg,
  693. luks->ivgen_cipher_alg,
  694. luks->ivgen_hash_alg,
  695. slotkey, luks->header.master_key_len,
  696. errp);
  697. if (!ivgen) {
  698. goto cleanup;
  699. }
  700. /*
  701. * Before storing the master key, we need to vastly
  702. * increase its size, as protection against forensic
  703. * disk data recovery
  704. */
  705. splitkey = g_new0(uint8_t, splitkeylen);
  706. if (qcrypto_afsplit_encode(luks->hash_alg,
  707. luks->header.master_key_len,
  708. slot->stripes,
  709. masterkey,
  710. splitkey,
  711. errp) < 0) {
  712. goto cleanup;
  713. }
  714. /*
  715. * Now we encrypt the split master key with the key generated
  716. * from the user's password, before storing it
  717. */
  718. if (qcrypto_block_cipher_encrypt_helper(cipher, block->niv, ivgen,
  719. QCRYPTO_BLOCK_LUKS_SECTOR_SIZE,
  720. 0,
  721. splitkey,
  722. splitkeylen,
  723. errp) < 0) {
  724. goto cleanup;
  725. }
  726. /* Write out the slot's master key material. */
  727. if (writefunc(block,
  728. slot->key_offset_sector *
  729. QCRYPTO_BLOCK_LUKS_SECTOR_SIZE,
  730. splitkey, splitkeylen,
  731. opaque,
  732. errp) != splitkeylen) {
  733. goto cleanup;
  734. }
  735. slot->active = QCRYPTO_BLOCK_LUKS_KEY_SLOT_ENABLED;
  736. if (qcrypto_block_luks_store_header(block, writefunc, opaque, errp) < 0) {
  737. goto cleanup;
  738. }
  739. ret = 0;
  740. cleanup:
  741. if (slotkey) {
  742. memset(slotkey, 0, luks->header.master_key_len);
  743. }
  744. if (splitkey) {
  745. memset(splitkey, 0, splitkeylen);
  746. }
  747. return ret;
  748. }
  749. /*
  750. * Given a key slot, and user password, this will attempt to unlock
  751. * the master encryption key from the key slot.
  752. *
  753. * Returns:
  754. * 0 if the key slot is disabled, or key could not be decrypted
  755. * with the provided password
  756. * 1 if the key slot is enabled, and key decrypted successfully
  757. * with the provided password
  758. * -1 if a fatal error occurred loading the key
  759. */
  760. static int
  761. qcrypto_block_luks_load_key(QCryptoBlock *block,
  762. size_t slot_idx,
  763. const char *password,
  764. uint8_t *masterkey,
  765. QCryptoBlockReadFunc readfunc,
  766. void *opaque,
  767. Error **errp)
  768. {
  769. QCryptoBlockLUKS *luks = block->opaque;
  770. const QCryptoBlockLUKSKeySlot *slot = &luks->header.key_slots[slot_idx];
  771. g_autofree uint8_t *splitkey = NULL;
  772. size_t splitkeylen;
  773. g_autofree uint8_t *possiblekey = NULL;
  774. ssize_t rv;
  775. g_autoptr(QCryptoCipher) cipher = NULL;
  776. uint8_t keydigest[QCRYPTO_BLOCK_LUKS_DIGEST_LEN];
  777. g_autoptr(QCryptoIVGen) ivgen = NULL;
  778. size_t niv;
  779. if (slot->active != QCRYPTO_BLOCK_LUKS_KEY_SLOT_ENABLED) {
  780. return 0;
  781. }
  782. splitkeylen = luks->header.master_key_len * slot->stripes;
  783. splitkey = g_new0(uint8_t, splitkeylen);
  784. possiblekey = g_new0(uint8_t, luks->header.master_key_len);
  785. /*
  786. * The user password is used to generate a (possible)
  787. * decryption key. This may or may not successfully
  788. * decrypt the master key - we just blindly assume
  789. * the key is correct and validate the results of
  790. * decryption later.
  791. */
  792. if (qcrypto_pbkdf2(luks->hash_alg,
  793. (const uint8_t *)password, strlen(password),
  794. slot->salt, QCRYPTO_BLOCK_LUKS_SALT_LEN,
  795. slot->iterations,
  796. possiblekey, luks->header.master_key_len,
  797. errp) < 0) {
  798. return -1;
  799. }
  800. /*
  801. * We need to read the master key material from the
  802. * LUKS key material header. What we're reading is
  803. * not the raw master key, but rather the data after
  804. * it has been passed through AFSplit and the result
  805. * then encrypted.
  806. */
  807. rv = readfunc(block,
  808. slot->key_offset_sector * QCRYPTO_BLOCK_LUKS_SECTOR_SIZE,
  809. splitkey, splitkeylen,
  810. opaque,
  811. errp);
  812. if (rv < 0) {
  813. return -1;
  814. }
  815. /* Setup the cipher/ivgen that we'll use to try to decrypt
  816. * the split master key material */
  817. cipher = qcrypto_cipher_new(luks->cipher_alg,
  818. luks->cipher_mode,
  819. possiblekey,
  820. luks->header.master_key_len,
  821. errp);
  822. if (!cipher) {
  823. return -1;
  824. }
  825. niv = qcrypto_cipher_get_iv_len(luks->cipher_alg,
  826. luks->cipher_mode);
  827. ivgen = qcrypto_ivgen_new(luks->ivgen_alg,
  828. luks->ivgen_cipher_alg,
  829. luks->ivgen_hash_alg,
  830. possiblekey,
  831. luks->header.master_key_len,
  832. errp);
  833. if (!ivgen) {
  834. return -1;
  835. }
  836. /*
  837. * The master key needs to be decrypted in the same
  838. * way that the block device payload will be decrypted
  839. * later. In particular we'll be using the IV generator
  840. * to reset the encryption cipher every time the master
  841. * key crosses a sector boundary.
  842. */
  843. if (qcrypto_block_cipher_decrypt_helper(cipher,
  844. niv,
  845. ivgen,
  846. QCRYPTO_BLOCK_LUKS_SECTOR_SIZE,
  847. 0,
  848. splitkey,
  849. splitkeylen,
  850. errp) < 0) {
  851. return -1;
  852. }
  853. /*
  854. * Now we've decrypted the split master key, join
  855. * it back together to get the actual master key.
  856. */
  857. if (qcrypto_afsplit_decode(luks->hash_alg,
  858. luks->header.master_key_len,
  859. slot->stripes,
  860. splitkey,
  861. masterkey,
  862. errp) < 0) {
  863. return -1;
  864. }
  865. /*
  866. * We still don't know that the masterkey we got is valid,
  867. * because we just blindly assumed the user's password
  868. * was correct. This is where we now verify it. We are
  869. * creating a hash of the master key using PBKDF and
  870. * then comparing that to the hash stored in the key slot
  871. * header
  872. */
  873. if (qcrypto_pbkdf2(luks->hash_alg,
  874. masterkey,
  875. luks->header.master_key_len,
  876. luks->header.master_key_salt,
  877. QCRYPTO_BLOCK_LUKS_SALT_LEN,
  878. luks->header.master_key_iterations,
  879. keydigest,
  880. G_N_ELEMENTS(keydigest),
  881. errp) < 0) {
  882. return -1;
  883. }
  884. if (memcmp(keydigest, luks->header.master_key_digest,
  885. QCRYPTO_BLOCK_LUKS_DIGEST_LEN) == 0) {
  886. /* Success, we got the right master key */
  887. return 1;
  888. }
  889. /* Fail, user's password was not valid for this key slot,
  890. * tell caller to try another slot */
  891. return 0;
  892. }
  893. /*
  894. * Given a user password, this will iterate over all key
  895. * slots and try to unlock each active key slot using the
  896. * password until it successfully obtains a master key.
  897. *
  898. * Returns 0 if a key was loaded, -1 if no keys could be loaded
  899. */
  900. static int
  901. qcrypto_block_luks_find_key(QCryptoBlock *block,
  902. const char *password,
  903. uint8_t *masterkey,
  904. QCryptoBlockReadFunc readfunc,
  905. void *opaque,
  906. Error **errp)
  907. {
  908. size_t i;
  909. int rv;
  910. for (i = 0; i < QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS; i++) {
  911. rv = qcrypto_block_luks_load_key(block,
  912. i,
  913. password,
  914. masterkey,
  915. readfunc,
  916. opaque,
  917. errp);
  918. if (rv < 0) {
  919. goto error;
  920. }
  921. if (rv == 1) {
  922. return 0;
  923. }
  924. }
  925. error_setg(errp, "Invalid password, cannot unlock any keyslot");
  926. error:
  927. return -1;
  928. }
  929. static int
  930. qcrypto_block_luks_open(QCryptoBlock *block,
  931. QCryptoBlockOpenOptions *options,
  932. const char *optprefix,
  933. QCryptoBlockReadFunc readfunc,
  934. void *opaque,
  935. unsigned int flags,
  936. size_t n_threads,
  937. Error **errp)
  938. {
  939. QCryptoBlockLUKS *luks = NULL;
  940. g_autofree uint8_t *masterkey = NULL;
  941. g_autofree char *password = NULL;
  942. if (!(flags & QCRYPTO_BLOCK_OPEN_NO_IO)) {
  943. if (!options->u.luks.key_secret) {
  944. error_setg(errp, "Parameter '%skey-secret' is required for cipher",
  945. optprefix ? optprefix : "");
  946. return -1;
  947. }
  948. password = qcrypto_secret_lookup_as_utf8(
  949. options->u.luks.key_secret, errp);
  950. if (!password) {
  951. return -1;
  952. }
  953. }
  954. luks = g_new0(QCryptoBlockLUKS, 1);
  955. block->opaque = luks;
  956. if (qcrypto_block_luks_load_header(block, readfunc, opaque, errp) < 0) {
  957. goto fail;
  958. }
  959. if (qcrypto_block_luks_check_header(luks, errp) < 0) {
  960. goto fail;
  961. }
  962. if (qcrypto_block_luks_parse_header(luks, errp) < 0) {
  963. goto fail;
  964. }
  965. if (!(flags & QCRYPTO_BLOCK_OPEN_NO_IO)) {
  966. /* Try to find which key slot our password is valid for
  967. * and unlock the master key from that slot.
  968. */
  969. masterkey = g_new0(uint8_t, luks->header.master_key_len);
  970. if (qcrypto_block_luks_find_key(block,
  971. password,
  972. masterkey,
  973. readfunc, opaque,
  974. errp) < 0) {
  975. goto fail;
  976. }
  977. /* We have a valid master key now, so can setup the
  978. * block device payload decryption objects
  979. */
  980. block->kdfhash = luks->hash_alg;
  981. block->niv = qcrypto_cipher_get_iv_len(luks->cipher_alg,
  982. luks->cipher_mode);
  983. block->ivgen = qcrypto_ivgen_new(luks->ivgen_alg,
  984. luks->ivgen_cipher_alg,
  985. luks->ivgen_hash_alg,
  986. masterkey,
  987. luks->header.master_key_len,
  988. errp);
  989. if (!block->ivgen) {
  990. goto fail;
  991. }
  992. if (qcrypto_block_init_cipher(block,
  993. luks->cipher_alg,
  994. luks->cipher_mode,
  995. masterkey,
  996. luks->header.master_key_len,
  997. n_threads,
  998. errp) < 0) {
  999. goto fail;
  1000. }
  1001. }
  1002. block->sector_size = QCRYPTO_BLOCK_LUKS_SECTOR_SIZE;
  1003. block->payload_offset = luks->header.payload_offset_sector *
  1004. block->sector_size;
  1005. return 0;
  1006. fail:
  1007. qcrypto_block_free_cipher(block);
  1008. qcrypto_ivgen_free(block->ivgen);
  1009. g_free(luks);
  1010. return -1;
  1011. }
  1012. static void
  1013. qcrypto_block_luks_uuid_gen(uint8_t *uuidstr)
  1014. {
  1015. QemuUUID uuid;
  1016. qemu_uuid_generate(&uuid);
  1017. qemu_uuid_unparse(&uuid, (char *)uuidstr);
  1018. }
  1019. static int
  1020. qcrypto_block_luks_create(QCryptoBlock *block,
  1021. QCryptoBlockCreateOptions *options,
  1022. const char *optprefix,
  1023. QCryptoBlockInitFunc initfunc,
  1024. QCryptoBlockWriteFunc writefunc,
  1025. void *opaque,
  1026. Error **errp)
  1027. {
  1028. QCryptoBlockLUKS *luks;
  1029. QCryptoBlockCreateOptionsLUKS luks_opts;
  1030. Error *local_err = NULL;
  1031. g_autofree uint8_t *masterkey = NULL;
  1032. size_t header_sectors;
  1033. size_t split_key_sectors;
  1034. size_t i;
  1035. g_autofree char *password = NULL;
  1036. const char *cipher_alg;
  1037. const char *cipher_mode;
  1038. const char *ivgen_alg;
  1039. const char *ivgen_hash_alg = NULL;
  1040. const char *hash_alg;
  1041. g_autofree char *cipher_mode_spec = NULL;
  1042. uint64_t iters;
  1043. memcpy(&luks_opts, &options->u.luks, sizeof(luks_opts));
  1044. if (!luks_opts.has_iter_time) {
  1045. luks_opts.iter_time = 2000;
  1046. }
  1047. if (!luks_opts.has_cipher_alg) {
  1048. luks_opts.cipher_alg = QCRYPTO_CIPHER_ALG_AES_256;
  1049. }
  1050. if (!luks_opts.has_cipher_mode) {
  1051. luks_opts.cipher_mode = QCRYPTO_CIPHER_MODE_XTS;
  1052. }
  1053. if (!luks_opts.has_ivgen_alg) {
  1054. luks_opts.ivgen_alg = QCRYPTO_IVGEN_ALG_PLAIN64;
  1055. }
  1056. if (!luks_opts.has_hash_alg) {
  1057. luks_opts.hash_alg = QCRYPTO_HASH_ALG_SHA256;
  1058. }
  1059. if (luks_opts.ivgen_alg == QCRYPTO_IVGEN_ALG_ESSIV) {
  1060. if (!luks_opts.has_ivgen_hash_alg) {
  1061. luks_opts.ivgen_hash_alg = QCRYPTO_HASH_ALG_SHA256;
  1062. luks_opts.has_ivgen_hash_alg = true;
  1063. }
  1064. }
  1065. luks = g_new0(QCryptoBlockLUKS, 1);
  1066. block->opaque = luks;
  1067. luks->cipher_alg = luks_opts.cipher_alg;
  1068. luks->cipher_mode = luks_opts.cipher_mode;
  1069. luks->ivgen_alg = luks_opts.ivgen_alg;
  1070. luks->ivgen_hash_alg = luks_opts.ivgen_hash_alg;
  1071. luks->hash_alg = luks_opts.hash_alg;
  1072. /* Note we're allowing ivgen_hash_alg to be set even for
  1073. * non-essiv iv generators that don't need a hash. It will
  1074. * be silently ignored, for compatibility with dm-crypt */
  1075. if (!options->u.luks.key_secret) {
  1076. error_setg(errp, "Parameter '%skey-secret' is required for cipher",
  1077. optprefix ? optprefix : "");
  1078. goto error;
  1079. }
  1080. password = qcrypto_secret_lookup_as_utf8(luks_opts.key_secret, errp);
  1081. if (!password) {
  1082. goto error;
  1083. }
  1084. memcpy(luks->header.magic, qcrypto_block_luks_magic,
  1085. QCRYPTO_BLOCK_LUKS_MAGIC_LEN);
  1086. /* We populate the header in native endianness initially and
  1087. * then convert everything to big endian just before writing
  1088. * it out to disk
  1089. */
  1090. luks->header.version = QCRYPTO_BLOCK_LUKS_VERSION;
  1091. qcrypto_block_luks_uuid_gen(luks->header.uuid);
  1092. cipher_alg = qcrypto_block_luks_cipher_alg_lookup(luks_opts.cipher_alg,
  1093. errp);
  1094. if (!cipher_alg) {
  1095. goto error;
  1096. }
  1097. cipher_mode = QCryptoCipherMode_str(luks_opts.cipher_mode);
  1098. ivgen_alg = QCryptoIVGenAlgorithm_str(luks_opts.ivgen_alg);
  1099. if (luks_opts.has_ivgen_hash_alg) {
  1100. ivgen_hash_alg = QCryptoHashAlgorithm_str(luks_opts.ivgen_hash_alg);
  1101. cipher_mode_spec = g_strdup_printf("%s-%s:%s", cipher_mode, ivgen_alg,
  1102. ivgen_hash_alg);
  1103. } else {
  1104. cipher_mode_spec = g_strdup_printf("%s-%s", cipher_mode, ivgen_alg);
  1105. }
  1106. hash_alg = QCryptoHashAlgorithm_str(luks_opts.hash_alg);
  1107. if (strlen(cipher_alg) >= QCRYPTO_BLOCK_LUKS_CIPHER_NAME_LEN) {
  1108. error_setg(errp, "Cipher name '%s' is too long for LUKS header",
  1109. cipher_alg);
  1110. goto error;
  1111. }
  1112. if (strlen(cipher_mode_spec) >= QCRYPTO_BLOCK_LUKS_CIPHER_MODE_LEN) {
  1113. error_setg(errp, "Cipher mode '%s' is too long for LUKS header",
  1114. cipher_mode_spec);
  1115. goto error;
  1116. }
  1117. if (strlen(hash_alg) >= QCRYPTO_BLOCK_LUKS_HASH_SPEC_LEN) {
  1118. error_setg(errp, "Hash name '%s' is too long for LUKS header",
  1119. hash_alg);
  1120. goto error;
  1121. }
  1122. if (luks_opts.ivgen_alg == QCRYPTO_IVGEN_ALG_ESSIV) {
  1123. luks->ivgen_cipher_alg =
  1124. qcrypto_block_luks_essiv_cipher(luks_opts.cipher_alg,
  1125. luks_opts.ivgen_hash_alg,
  1126. &local_err);
  1127. if (local_err) {
  1128. error_propagate(errp, local_err);
  1129. goto error;
  1130. }
  1131. } else {
  1132. luks->ivgen_cipher_alg = luks_opts.cipher_alg;
  1133. }
  1134. strcpy(luks->header.cipher_name, cipher_alg);
  1135. strcpy(luks->header.cipher_mode, cipher_mode_spec);
  1136. strcpy(luks->header.hash_spec, hash_alg);
  1137. luks->header.master_key_len =
  1138. qcrypto_cipher_get_key_len(luks_opts.cipher_alg);
  1139. if (luks_opts.cipher_mode == QCRYPTO_CIPHER_MODE_XTS) {
  1140. luks->header.master_key_len *= 2;
  1141. }
  1142. /* Generate the salt used for hashing the master key
  1143. * with PBKDF later
  1144. */
  1145. if (qcrypto_random_bytes(luks->header.master_key_salt,
  1146. QCRYPTO_BLOCK_LUKS_SALT_LEN,
  1147. errp) < 0) {
  1148. goto error;
  1149. }
  1150. /* Generate random master key */
  1151. masterkey = g_new0(uint8_t, luks->header.master_key_len);
  1152. if (qcrypto_random_bytes(masterkey,
  1153. luks->header.master_key_len, errp) < 0) {
  1154. goto error;
  1155. }
  1156. /* Setup the block device payload encryption objects */
  1157. if (qcrypto_block_init_cipher(block, luks_opts.cipher_alg,
  1158. luks_opts.cipher_mode, masterkey,
  1159. luks->header.master_key_len, 1, errp) < 0) {
  1160. goto error;
  1161. }
  1162. block->kdfhash = luks_opts.hash_alg;
  1163. block->niv = qcrypto_cipher_get_iv_len(luks_opts.cipher_alg,
  1164. luks_opts.cipher_mode);
  1165. block->ivgen = qcrypto_ivgen_new(luks_opts.ivgen_alg,
  1166. luks->ivgen_cipher_alg,
  1167. luks_opts.ivgen_hash_alg,
  1168. masterkey, luks->header.master_key_len,
  1169. errp);
  1170. if (!block->ivgen) {
  1171. goto error;
  1172. }
  1173. /* Determine how many iterations we need to hash the master
  1174. * key, in order to have 1 second of compute time used
  1175. */
  1176. iters = qcrypto_pbkdf2_count_iters(luks_opts.hash_alg,
  1177. masterkey, luks->header.master_key_len,
  1178. luks->header.master_key_salt,
  1179. QCRYPTO_BLOCK_LUKS_SALT_LEN,
  1180. QCRYPTO_BLOCK_LUKS_DIGEST_LEN,
  1181. &local_err);
  1182. if (local_err) {
  1183. error_propagate(errp, local_err);
  1184. goto error;
  1185. }
  1186. if (iters > (ULLONG_MAX / luks_opts.iter_time)) {
  1187. error_setg_errno(errp, ERANGE,
  1188. "PBKDF iterations %llu too large to scale",
  1189. (unsigned long long)iters);
  1190. goto error;
  1191. }
  1192. /* iter_time was in millis, but count_iters reported for secs */
  1193. iters = iters * luks_opts.iter_time / 1000;
  1194. /* Why /= 8 ? That matches cryptsetup, but there's no
  1195. * explanation why they chose /= 8... Probably so that
  1196. * if all 8 keyslots are active we only spend 1 second
  1197. * in total time to check all keys */
  1198. iters /= 8;
  1199. if (iters > UINT32_MAX) {
  1200. error_setg_errno(errp, ERANGE,
  1201. "PBKDF iterations %llu larger than %u",
  1202. (unsigned long long)iters, UINT32_MAX);
  1203. goto error;
  1204. }
  1205. iters = MAX(iters, QCRYPTO_BLOCK_LUKS_MIN_MASTER_KEY_ITERS);
  1206. luks->header.master_key_iterations = iters;
  1207. /* Hash the master key, saving the result in the LUKS
  1208. * header. This hash is used when opening the encrypted
  1209. * device to verify that the user password unlocked a
  1210. * valid master key
  1211. */
  1212. if (qcrypto_pbkdf2(luks_opts.hash_alg,
  1213. masterkey, luks->header.master_key_len,
  1214. luks->header.master_key_salt,
  1215. QCRYPTO_BLOCK_LUKS_SALT_LEN,
  1216. luks->header.master_key_iterations,
  1217. luks->header.master_key_digest,
  1218. QCRYPTO_BLOCK_LUKS_DIGEST_LEN,
  1219. errp) < 0) {
  1220. goto error;
  1221. }
  1222. /* start with the sector that follows the header*/
  1223. header_sectors = QCRYPTO_BLOCK_LUKS_KEY_SLOT_OFFSET /
  1224. QCRYPTO_BLOCK_LUKS_SECTOR_SIZE;
  1225. split_key_sectors =
  1226. qcrypto_block_luks_splitkeylen_sectors(luks,
  1227. header_sectors,
  1228. QCRYPTO_BLOCK_LUKS_STRIPES);
  1229. for (i = 0; i < QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS; i++) {
  1230. QCryptoBlockLUKSKeySlot *slot = &luks->header.key_slots[i];
  1231. slot->active = QCRYPTO_BLOCK_LUKS_KEY_SLOT_DISABLED;
  1232. slot->key_offset_sector = header_sectors + i * split_key_sectors;
  1233. slot->stripes = QCRYPTO_BLOCK_LUKS_STRIPES;
  1234. }
  1235. /* The total size of the LUKS headers is the partition header + key
  1236. * slot headers, rounded up to the nearest sector, combined with
  1237. * the size of each master key material region, also rounded up
  1238. * to the nearest sector */
  1239. luks->header.payload_offset_sector = header_sectors +
  1240. QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS * split_key_sectors;
  1241. block->sector_size = QCRYPTO_BLOCK_LUKS_SECTOR_SIZE;
  1242. block->payload_offset = luks->header.payload_offset_sector *
  1243. block->sector_size;
  1244. /* Reserve header space to match payload offset */
  1245. initfunc(block, block->payload_offset, opaque, &local_err);
  1246. if (local_err) {
  1247. error_propagate(errp, local_err);
  1248. goto error;
  1249. }
  1250. /* populate the slot 0 with the password encrypted master key*/
  1251. /* This will also store the header */
  1252. if (qcrypto_block_luks_store_key(block,
  1253. 0,
  1254. password,
  1255. masterkey,
  1256. luks_opts.iter_time,
  1257. writefunc,
  1258. opaque,
  1259. errp) < 0) {
  1260. goto error;
  1261. }
  1262. memset(masterkey, 0, luks->header.master_key_len);
  1263. return 0;
  1264. error:
  1265. if (masterkey) {
  1266. memset(masterkey, 0, luks->header.master_key_len);
  1267. }
  1268. qcrypto_block_free_cipher(block);
  1269. qcrypto_ivgen_free(block->ivgen);
  1270. g_free(luks);
  1271. return -1;
  1272. }
  1273. static int qcrypto_block_luks_get_info(QCryptoBlock *block,
  1274. QCryptoBlockInfo *info,
  1275. Error **errp)
  1276. {
  1277. QCryptoBlockLUKS *luks = block->opaque;
  1278. QCryptoBlockInfoLUKSSlot *slot;
  1279. QCryptoBlockInfoLUKSSlotList *slots = NULL, **prev = &info->u.luks.slots;
  1280. size_t i;
  1281. info->u.luks.cipher_alg = luks->cipher_alg;
  1282. info->u.luks.cipher_mode = luks->cipher_mode;
  1283. info->u.luks.ivgen_alg = luks->ivgen_alg;
  1284. if (info->u.luks.ivgen_alg == QCRYPTO_IVGEN_ALG_ESSIV) {
  1285. info->u.luks.has_ivgen_hash_alg = true;
  1286. info->u.luks.ivgen_hash_alg = luks->ivgen_hash_alg;
  1287. }
  1288. info->u.luks.hash_alg = luks->hash_alg;
  1289. info->u.luks.payload_offset = block->payload_offset;
  1290. info->u.luks.master_key_iters = luks->header.master_key_iterations;
  1291. info->u.luks.uuid = g_strndup((const char *)luks->header.uuid,
  1292. sizeof(luks->header.uuid));
  1293. for (i = 0; i < QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS; i++) {
  1294. slots = g_new0(QCryptoBlockInfoLUKSSlotList, 1);
  1295. *prev = slots;
  1296. slots->value = slot = g_new0(QCryptoBlockInfoLUKSSlot, 1);
  1297. slot->active = luks->header.key_slots[i].active ==
  1298. QCRYPTO_BLOCK_LUKS_KEY_SLOT_ENABLED;
  1299. slot->key_offset = luks->header.key_slots[i].key_offset_sector
  1300. * QCRYPTO_BLOCK_LUKS_SECTOR_SIZE;
  1301. if (slot->active) {
  1302. slot->has_iters = true;
  1303. slot->iters = luks->header.key_slots[i].iterations;
  1304. slot->has_stripes = true;
  1305. slot->stripes = luks->header.key_slots[i].stripes;
  1306. }
  1307. prev = &slots->next;
  1308. }
  1309. return 0;
  1310. }
  1311. static void qcrypto_block_luks_cleanup(QCryptoBlock *block)
  1312. {
  1313. g_free(block->opaque);
  1314. }
  1315. static int
  1316. qcrypto_block_luks_decrypt(QCryptoBlock *block,
  1317. uint64_t offset,
  1318. uint8_t *buf,
  1319. size_t len,
  1320. Error **errp)
  1321. {
  1322. assert(QEMU_IS_ALIGNED(offset, QCRYPTO_BLOCK_LUKS_SECTOR_SIZE));
  1323. assert(QEMU_IS_ALIGNED(len, QCRYPTO_BLOCK_LUKS_SECTOR_SIZE));
  1324. return qcrypto_block_decrypt_helper(block,
  1325. QCRYPTO_BLOCK_LUKS_SECTOR_SIZE,
  1326. offset, buf, len, errp);
  1327. }
  1328. static int
  1329. qcrypto_block_luks_encrypt(QCryptoBlock *block,
  1330. uint64_t offset,
  1331. uint8_t *buf,
  1332. size_t len,
  1333. Error **errp)
  1334. {
  1335. assert(QEMU_IS_ALIGNED(offset, QCRYPTO_BLOCK_LUKS_SECTOR_SIZE));
  1336. assert(QEMU_IS_ALIGNED(len, QCRYPTO_BLOCK_LUKS_SECTOR_SIZE));
  1337. return qcrypto_block_encrypt_helper(block,
  1338. QCRYPTO_BLOCK_LUKS_SECTOR_SIZE,
  1339. offset, buf, len, errp);
  1340. }
  1341. const QCryptoBlockDriver qcrypto_block_driver_luks = {
  1342. .open = qcrypto_block_luks_open,
  1343. .create = qcrypto_block_luks_create,
  1344. .get_info = qcrypto_block_luks_get_info,
  1345. .cleanup = qcrypto_block_luks_cleanup,
  1346. .decrypt = qcrypto_block_luks_decrypt,
  1347. .encrypt = qcrypto_block_luks_encrypt,
  1348. .has_format = qcrypto_block_luks_has_format,
  1349. };