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 21 #include "qapi/error.h" 22 #include "qemu/bswap.h" 23 24 #include "block-luks.h" 25 26 #include "crypto/hash.h" 27 #include "crypto/afsplit.h" 28 #include "crypto/pbkdf.h" 29 #include "crypto/secret.h" 30 #include "crypto/random.h" 31 #include "qemu/uuid.h" 32 33 #include "qemu/bitmap.h" 34 35 /* 36 * Reference for the LUKS format implemented here is 37 * 38 * docs/on-disk-format.pdf 39 * 40 * in 'cryptsetup' package source code 41 * 42 * This file implements the 1.2.1 specification, dated 43 * Oct 16, 2011. 44 */ 45 46 typedef struct QCryptoBlockLUKSHeader QCryptoBlockLUKSHeader; 47 typedef struct QCryptoBlockLUKSKeySlot QCryptoBlockLUKSKeySlot; 48 49 50 /* The following constants are all defined by the LUKS spec */ 51 #define QCRYPTO_BLOCK_LUKS_VERSION 1 52 53 #define QCRYPTO_BLOCK_LUKS_MAGIC_LEN 6 54 #define QCRYPTO_BLOCK_LUKS_CIPHER_NAME_LEN 32 55 #define QCRYPTO_BLOCK_LUKS_CIPHER_MODE_LEN 32 56 #define QCRYPTO_BLOCK_LUKS_HASH_SPEC_LEN 32 57 #define QCRYPTO_BLOCK_LUKS_DIGEST_LEN 20 58 #define QCRYPTO_BLOCK_LUKS_SALT_LEN 32 59 #define QCRYPTO_BLOCK_LUKS_UUID_LEN 40 60 #define QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS 8 61 #define QCRYPTO_BLOCK_LUKS_STRIPES 4000 62 #define QCRYPTO_BLOCK_LUKS_MIN_SLOT_KEY_ITERS 1000 63 #define QCRYPTO_BLOCK_LUKS_MIN_MASTER_KEY_ITERS 1000 64 #define QCRYPTO_BLOCK_LUKS_KEY_SLOT_OFFSET 4096 65 66 #define QCRYPTO_BLOCK_LUKS_KEY_SLOT_DISABLED 0x0000DEAD 67 #define QCRYPTO_BLOCK_LUKS_KEY_SLOT_ENABLED 0x00AC71F3 68 69 #define QCRYPTO_BLOCK_LUKS_SECTOR_SIZE 512LL 70 71 #define QCRYPTO_BLOCK_LUKS_DEFAULT_ITER_TIME_MS 2000 72 #define QCRYPTO_BLOCK_LUKS_ERASE_ITERATIONS 40 73 74 static const char qcrypto_block_luks_magic[QCRYPTO_BLOCK_LUKS_MAGIC_LEN] = { 75 'L', 'U', 'K', 'S', 0xBA, 0xBE 76 }; 77 78 /* 79 * This struct is written to disk in big-endian format, 80 * but operated upon in native-endian format. 81 */ 82 struct QCryptoBlockLUKSKeySlot { 83 /* state of keyslot, enabled/disable */ 84 uint32_t active; 85 /* iterations for PBKDF2 */ 86 uint32_t iterations; 87 /* salt for PBKDF2 */ 88 uint8_t salt[QCRYPTO_BLOCK_LUKS_SALT_LEN]; 89 /* start sector of key material */ 90 uint32_t key_offset_sector; 91 /* number of anti-forensic stripes */ 92 uint32_t stripes; 93 }; 94 95 /* 96 * This struct is written to disk in big-endian format, 97 * but operated upon in native-endian format. 98 */ 99 struct QCryptoBlockLUKSHeader { 100 /* 'L', 'U', 'K', 'S', '0xBA', '0xBE' */ 101 char magic[QCRYPTO_BLOCK_LUKS_MAGIC_LEN]; 102 103 /* LUKS version, currently 1 */ 104 uint16_t version; 105 106 /* cipher name specification (aes, etc) */ 107 char cipher_name[QCRYPTO_BLOCK_LUKS_CIPHER_NAME_LEN]; 108 109 /* cipher mode specification (cbc-plain, xts-essiv:sha256, etc) */ 110 char cipher_mode[QCRYPTO_BLOCK_LUKS_CIPHER_MODE_LEN]; 111 112 /* hash specification (sha256, etc) */ 113 char hash_spec[QCRYPTO_BLOCK_LUKS_HASH_SPEC_LEN]; 114 115 /* start offset of the volume data (in 512 byte sectors) */ 116 uint32_t payload_offset_sector; 117 118 /* Number of key bytes */ 119 uint32_t master_key_len; 120 121 /* master key checksum after PBKDF2 */ 122 uint8_t master_key_digest[QCRYPTO_BLOCK_LUKS_DIGEST_LEN]; 123 124 /* salt for master key PBKDF2 */ 125 uint8_t master_key_salt[QCRYPTO_BLOCK_LUKS_SALT_LEN]; 126 127 /* iterations for master key PBKDF2 */ 128 uint32_t master_key_iterations; 129 130 /* UUID of the partition in standard ASCII representation */ 131 uint8_t uuid[QCRYPTO_BLOCK_LUKS_UUID_LEN]; 132 133 /* key slots */ 134 QCryptoBlockLUKSKeySlot key_slots[QCRYPTO_BLOCK_LUKS_NUM_KEY_SLOTS]; 135 }; 136 137 138 void 139 qcrypto_block_luks_to_disk_endian(QCryptoBlockLUKSHeader *hdr); 140 void 141 qcrypto_block_luks_from_disk_endian(QCryptoBlockLUKSHeader *hdr); 142