An encrypted disk image holds a whole disk image (UDIF, raw or sparse) in encrypted
blocks. The password is not the key: it is stretched into a key that unwraps a small
blob, and the blob holds the real keys. Apple has not published the format. It is known
from the reverse engineering in vilefault and libdmg-hfsplus, from John the Ripper's
dmg2john, and from images hdiutil makes, and this page
follows all of them. Everything in version 2 below was checked by decrypting images that
hdiutil create -encryption made with a throwaway password (AES-128 and
AES-256, UDZO, UDRO, raw and sparse), against an independent implementation (OpenSSL)
and against the unencrypted image made from the same files. All numbers are
big-endian.
How it fits together
- The header names a salt and an iteration count. The password, as UTF-8 bytes, goes through PBKDF2-HMAC-SHA1 with them.
- The first bytes of that output are the key that unwraps the key blob
in the header (AES-192 in CBC mode for images from
hdiutil; triple DES for older ones). The blob ends in padding and a marker, which is how a wrong password is told: it almost never unwraps to something with valid padding and the marker. - The blob holds the data key (AES, 128 or 256 bits) and the IV key (20 bytes, for HMAC-SHA1).
- The data is cut into blocks (512 bytes in version 2, 4,096 in version 1). Each block is AES-CBC decrypted with its own IV, made from the block number and the IV key.
- The plaintext of the data is a disk image of its own: a UDIF file (it ends in the
koly trailer), a raw disk image or a sparse
image. It is read exactly as an unencrypted
.dmgis.
Version 2: encrcdsa
A version 2 image starts with the eight bytes encrcdsa, which is what the current
hdiutil writes. The header is followed by key headers
and, at the data offset (122,368 bytes in the images built for this page, the space
between being zero), the encrypted data. The file is as long as the data offset plus the
data size rounded up to a whole block.
0Fixed header signature, version, ciphers, UUID, block size, data size and offset, key header count76 bytes76Key header pointers one per way of unlocking (usually one)20 bytes each96Password key header the key derivation and the wrapped keys616 bytes712Reserved zero bytesto the data offsetdata offsetEncrypted data the disk image, in blocksdata size, padded to a block
hdiutil writes; the pointers are what a reader follows.Fixed header
| Offset | Size | Field | Meaning |
|---|---|---|---|
| 0 | 8 | Signature | encrcdsa |
| 8 | 4 | Version | 2 |
| 12 | 4 | IV size | 16, the size of an AES block. The IV itself is made per block (below). |
| 16 | 4 | Data cipher mode | 5. A number from Apple's CDSA (the older cryptographic API): the value for cipher block chaining with an 8-byte IV. The data is AES in CBC mode. |
| 20 | 4 | Data cipher | 0x80000001, AES (a vendor-defined CDSA algorithm number) |
| 24 | 4 | Key bits | 128 or 256: the size of the data key |
| 28 | 4 | IV key algorithm | 91, the number of HMAC-SHA1 in CDSA's list as I recall it. What it does is settled: the block IVs are HMAC-SHA1. |
| 32 | 4 | IV key bits | 160: the size of the HMAC key, 20 bytes |
| 36 | 16 | UUID | An identifier of the image |
| 52 | 4 | Block size | 512 |
| 56 | 8 | Data size | The size of the plaintext image in bytes; not always a multiple of the block size |
| 64 | 8 | Data offset | Where the encrypted data starts in the file |
| 72 | 4 | Key header count | 1 |
The four fields at 16, 20, 28 and 32 were unnamed in libdmg-hfsplus (unk1 to
unk4); vilefault and dmg2john call them the encryption mode, the
encryption algorithm, the PRNG algorithm and the PRNG key size. Every image built here has the
same values in them (5, 0x80000001, 91 and 160) whichever key size is chosen at 24,
so what they mean was settled by what the decryption
needs: the data cipher and mode, and the algorithm and key size of the IV key. That the
numbers 5 and 91 are CDSA's CBC-with-IV8 and HMAC-SHA1 identifiers is my recollection of
Apple's list; the constants are not in the Mac SDK any more, and nothing in the files depends on it.
Key header pointers
At offset 76, one 20-byte record per key header:
| Offset | Size | Field | Meaning |
|---|---|---|---|
| 0 | 4 | Type | 1 is a password header. 2 is a header for a certificate (the keys are wrapped for a public key held in a keychain). An image can have several. |
| 4 | 8 | Offset | Where the key header is in the file (96) |
| 12 | 8 | Size | Its size (616) |
The two 8-byte numbers were read as pairs of 4-byte values by earlier descriptions, the first half of each being always zero, which is why they looked like unknown fields.
Password key header
| Offset | Size | Field | Meaning |
|---|---|---|---|
| 0 | 4 | Key derivation | 103: PBKDF2 (PKCS #5 version 2) |
| 4 | 4 | Pseudo-random function | 0: HMAC-SHA1 |
| 8 | 4 | Iterations | Recent images from hdiutil use a count chosen to take a fraction of a second, for example 256,410 |
| 12 | 4 | Salt size | 20 |
| 16 | 32 | Salt | The first Salt size bytes; the rest is zero |
| 48 | 4 | Wrapping IV size | 8 |
| 52 | 32 | Wrapping IV | The IV for the key blob: the first 16 bytes of this field (8 bytes of IV, then zeros). For triple DES wrapping the first 8 bytes. |
| 84 | 4 | Wrapping key bits | 192 |
| 88 | 4 | Wrapping cipher | 0x80000001: AES |
| 92 | 4 | Wrapping padding | 7: PKCS #7 |
| 96 | 4 | Wrapping mode | 6: cipher block chaining with padding |
| 100 | 4 | Key blob size | 48 for AES-128, 64 for AES-256 |
| 104 | blob size | Key blob | The wrapped keys |
| after | to 616 | Reserved | Zero |
The salt and iteration count stand in the file in the clear. Because 192 bits are asked for, the key that unwraps the blob is the first 24 bytes of the PBKDF2 output.
The key blob
Unwrapped (AES-192, CBC, no padding applied, IV as above), the blob is:
| Offset | Size | Content |
|---|---|---|
| 0 | key bits / 8 | The data key (16 or 32 bytes) |
| key size | 20 | The IV key for HMAC-SHA1. In every image checked it starts with the data key: its first 20 bytes for AES-256, the whole key for AES-128 followed by 4 more bytes (one image had 0D 00 00 00). |
| key size + 20 | 5 | The marker CKIE and a zero byte |
| then | 1 to 16 | PKCS #7 padding: n bytes each of value n |
Older John the Ripper notes take the IV key from the first 20 bytes of the blob. That is the same thing for AES-256 and wrong for AES-128, where the IV key starts after the 16-byte data key (checked: an AES-128 image only decrypts with that).
Checking the password. After unwrapping, the padding must be valid PKCS #7 and, when there is room, the marker must follow the keys. For a wrong password the chance of both is about 2-48. The blob has no MAC of its own, so a change in its first block (which garbles the data key without touching the padding or the marker) is not noticed: the password is accepted and the data decrypts to noise. The data has no MAC either: a changed data block gives a block of noise inside a readable image.
Data blocks and the IV rule
Block n (counting from 0, block size bytes at
data offset + n × block size) is decrypted with AES-CBC, the data key, and the IV
IV(n) = first 16 bytes of HMAC-SHA1(IV key, n as 4 big-endian bytes)
There is no padding. The last block of the image is full in the file and the data size says how much of it counts. The decrypted data is the disk image itself.
Worked example
The AES-256 image used for the checks starts
00 65 6E 63 72 63 64 73 61 00 00 00 02 00 00 00 10 encrcdsa, version 2, IV size 16
10 00 00 00 05 80 00 00 01 00 00 01 00 00 00 00 5B mode 5, AES, 256 bits, MAC 91
20 00 00 00 A0 D8 9F F4 74 F8 BE 4E AC B2 8D 19 B0 BA 2E 29 18 160 bits, UUID
34 00 00 02 00 block size 512
38 00 00 00 00 00 04 EC B5 data size 322,741
40 00 00 00 00 00 01 DE 00 data offset 0x1DE00
48 00 00 00 01 one key header
4C 00 00 00 01 00 00 00 00 00 00 00 60 00 00 00 00 00 00 02 68 type 1, offset 0x60, size 0x268
60 00 00 00 67 00 00 00 00 00 03 E9 9A 00 00 00 14 PBKDF2, HMAC-SHA1, 256,410 iterations, salt 20
70 72 90 23 91 63 F6 00 AD E0 33 26 5D BF A7 AC B4 C9 EF 40 F6 … salt
90 00 00 00 08 9A 8A BD 29 D9 99 C8 B7 … wrapping IV size 8, the IV
B4 00 00 00 C0 80 00 00 01 00 00 00 07 00 00 00 06 00 00 00 40 192 bits, AES, PKCS #7, CBC, 64 bytes
C8 74 52 28 28 7F 44 F7 8A 44 C0 21 9D 8B 2A 24 7B … the key blob
The password is the UTF-8 bytes of what was typed (here dmgtestpass-7731 followed by
a newline: hdiutil -stdinpass keeps everything it is given, so a passphrase piped in with
echo includes the newline). PBKDF2-HMAC-SHA1 over it, the salt and 256,410
iterations gives the 24-byte key; AES-192-CBC decrypts the blob to the AES-256 key
AEE862D5 67E25711 447EEC7B 97B6FFA5 15F29A54 55BCCA0E 1D9CEBF8 8713FF60, the IV key
(its first 20 bytes), CKIE, 00 and seven bytes of 07. Block 0 at 0x1DE00 then decrypts, with
IV = HMAC-SHA1(IV key, 00 00 00 00)[0..16], to 78 01 ED D0 …, the start of a zlib
stream: the plaintext is a UDIF image.
Version 1: cdsaencr
A version 1 image (the format of the early FileVault and of old encrypted images, as the sources
describe it) keeps its header at the end of the file, 1,276 bytes long, ending in the eight bytes
cdsaencr. The encrypted data is everything before it, from offset 0, in 4,096-byte
blocks.
| Offset in the header | Size | Field |
|---|---|---|
| 0 | 48 | Filler |
| 48 | 4 | Iterations of PBKDF2-HMAC-SHA1 |
| 52 | 4 | Salt size (up to 48) |
| 56 | 48 | Salt |
| 104 | 32 | Unwrap IV (not used by the unwrapping below) |
| 136 | 4 | Wrapped AES key size |
| 140 | 296 | Wrapped AES key |
| 436 | 4 | Wrapped HMAC key size |
| 440 | 300 | Wrapped HMAC key |
| 740 | 4 | Wrapped integrity key size |
| 744 | 48 | Wrapped integrity key (no reader uses it) |
| 792 | 484 | Filler, ending with cdsaencr |
Keys. PBKDF2-HMAC-SHA1 of the password with the salt gives 24 bytes, the key of a
triple-DES key wrap in the style of RFC 3217. To unwrap each of the two keys: decrypt the
wrapped bytes with 3DES-CBC, IV 4A DD A2 2C 79 E8 21 05, remove the PKCS #7 padding,
reverse the bytes, take the first 8 as an IV, decrypt the rest with 3DES-CBC under it and remove
the padding again. The first key is the 128-bit AES key, the second the 20-byte HMAC-SHA1 key.
Both paddings must be valid, which is how a wrong password is told.
Data. The same rule as version 2: each block of 4,096 bytes is AES-128-CBC with IV = HMAC-SHA1(HMAC key, block number as 4 big-endian bytes) cut to 16 bytes.
This version is described from the sources (vilefault, libdmg-hfsplus, dmg2john) only:
no real version 1 image was available, and the current hdiutil cannot write one.
The explorer's reader for it is tested against a writer built from the same description,
which shows the reading is consistent, not that the description is right.
In the explorer
Drop an encrypted image and the explorer shows the header, key header pointers and password header as a tree, and asks for the password. The password is used in memory for the open file and is never stored. A wrong password is refused with the tree still shown. A right one unlocks the data, and the row Decrypted image (also the Encrypted data node) opens it as a disk image of its own: its partitions, volumes and files are read exactly as for an unencrypted image. Decryption is block by block, as the reader asks for bytes. The decrypted image opens as a file of its own up to 256 MB, the limit for any decoded file in the explorer; a larger image shows its header and what it contains, but is not opened.
Images locked with a certificate (key header type 2) have no password; their keys are wrapped for a private key in a keychain, and the explorer shows their header only.
Not described
Known, not yet covered
- Reference: the contents of a certificate key header (type 2): the field layout of
cencrypted_v2_private_key_headerindmg2john's header is known (a salt, three unnamed numbers and a key blob), but no image built for a certificate was available to check it. - Viewer: images with a certificate key header cannot be opened.
- Viewer: a decrypted image over 256 MB is not opened as a level; only its header and identification are shown.
Unknown
- Reference: that the numbers 5 (mode), 91 (IV key algorithm) and 103 (key derivation) are the CDSA identifiers I recall; the Mac SDK no longer ships them, and the files give the numbers only.
- Reference: whether version 2 images made by old systems (10.4 to 10.6) wrap the blob with
3DES (as
dmg2johnassumes) and keep the IV key at the start of the blob; no such image was available, so that path is implemented from the sources and not checked against a real file. - Reference: version 1 (
cdsaencr) is described from the sources only; no real image was available. - Reference: whether the marker
CKIEis always present in blobs written byhdiutilof every macOS version; it is in all images built here (AES-128 and AES-256).