block-luks.c 65 KB

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