Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Mac OS Encrypted Encoding

Mac OS uses encrypted encoding (CEncryptedEncoding) to encrypt various formats, such as:

Overview

There are 2 known versions of Encrypted Encoding.

Encrypted Encoding version 1

A version 1 encrypted container consist of:

Format version 1 supports the following key protectors:

  • Passphrase

Encrypted Encoding version 2

A version 2 encrypted container consist of:

  • Encrypted container header at the start of the file
  • Key protectors
  • Unknown (empty values), probably reserved for the key protectors
  • Encrypted data, typically at offset 122368

Version 2 supports the following key protectors:

  • Passphrase
  • Public key
  • Unknown (keybag)

Characteristics

CharacteristicsDescription
Byte orderbig-endian
Date and time valuesN/A
Character stringsN/A

Encrypted container

The encrypted container footer is 1276 bytes in size and consists of:

OffsetSizeValueDescription
016Container identifier (UUID), used in Mac OS keychain as account identifier
164Block size, in number of bytes
204Key protector encryption method
244Key protector padding type
284Key protector encryption mode
324Key protector key size, in number of bits
364Key protector initialization vector size
404Key derivation method
444Unknown
484Key derivation number of iterations
524Key derivation salt size, in number of bytes
5632Key derivation salt
884Block initialization vector size
924Block encryption mode
964Block encryption method
1004Block key size, in number of bits
10432Unknown (Wrapped block (or master) data encryption key (DEK) initialization vector?)
1364Wrapped block (or master) data encryption key (DEK) size
140256Wrapped block (or master) data encryption key (DEK)
3964HMAC method
4004HMAC key size, in number of bits
40432Unknown (Wrapped block HMAC initialization vector?)
4364Wrapped block HMAC key size
440256Wrapped block HMAC key
6964Integrity encryption method
7004Integrity key size, in number of bits
70432Unknown (Wrapped integrity key initialization vector?)
7364Wrapped integrity key size
740256Wrapped integrity key
9964Unknown (data size)
1000256Unknown (data)
12564Data fork offset, where the offset is relative from the start of the container
12604Data fork size, in number of bytes
126441Encrypted Encoding format version
12688"cdsaencr"Signature

Note that “cdsaencr” presumably is short for Common Data Security Architecture (CDSA) encryption. Common Security Services Manager (CSSM) is part of CDSA.

Key data can be obtained from the wrapped key data using the following approach (presumably based on RFC 3537):

  • Use the specified key derivation method, e.g. PDBKDF2, with salt and number of iterations to determine the key encryption key (KEK) based on a passphrase.
  • Pad the initialization vector [0x4a, 0xdd, 0xa2, 0x2c, 0x79, 0xe8, 0x21, 0x05] with 0-byte values if necessesary, e.g. if initialization vector is 8 bytes but the encryption method (AES) requires an initialization vector of 16 bytes.
  • Decrypt the wrapped key data using the encryption method and mode, e.g. DES3-CBC, with the number of bits of the KEK (defined by encryption key size) and the initialization vector if applicable.
  • Remove the padding, specified by the padding type.
  • Reverse the resulting intermediate key data.

The intermediate key data is of variable size and consists of:

OffsetSizeValueDescription
08Initialization vector
8...Wrapped key data
  • Pad the initialization vector of the intermediate key data with 0-byte values if necessesary.
  • Decrypt the wrapped key data (of the intermediate key data) using the encryption method and mode, e.g. DES3-CBC, with the number of bits of the KEK (defined by encryption key size) and the initialization vector (of the intermediate key data) if applicable.
  • Remove the padding, specified by the padding type.

The decypted key data is of variable size and consists of:

OffsetSizeValueDescription
040Signature
4...Key data

Encrypted container header

The encrypted container header is of variable size and consists of:

OffsetSizeValueDescription
08"encrcdsa"Signature
842Encrypted Encrypted Encoding format version
124Block initialization vector size, in number of bytes
164Block encryption mode
204Block encryption method
244Block key size, in number of bits
284HMAC method
324HMAC key size, in number of bits
3616Container identifier (UUID), used in Mac OS keychain as account identifier
524Block size, in number of bytes
568Data fork size, in number of bytes
648Data fork offset, where the offset is relative from the start of the container
724Number of key protector descriptors
76...Array of key protector descriptors

Key protector descriptor

The key protector descriptor is 20 bytes in size and consists of:

OffsetSizeValueDescription
04Unlock type
48Data offset, where the offset is relative from the start of the container
128Data size

Unlock types

ValueIdentifierDescription
1CSSM_APPLE_UNLOCK_TYPE_KEY_DIRECTMaster key wrapped by passphrase, stored as passphrase wrapped key
2CSSM_APPLE_UNLOCK_TYPE_WRAPPED_PRIVATEMaster key wrapped by a public key, stored as public key wrapped key
3CSSM_APPLE_UNLOCK_TYPE_KEYBAGMaster key wrapped by keybag

Passphrase wrapped key

The passphrase wrapped key is 616 bytes in size and consists of:

OffsetSizeValueDescription
04Key derivation method
48Key derivation number of iterations
124Key derivation salt size, in number of bytes
1632Key derivation salt
484Encryption initialization vector size, in number of bytes
5232Encryption initialization vector
844Encryption key size, in number of bits
884Encryption method
924Padding type
964Encryption mode
1004Wrapped key data size
10464Wrapped key data
168448Unknown (empty values)

Key data can be obtained from the wrapped key data using the following approach:

  • Use the specified key derivation method, e.g. PDBKDF2, with salt and number of iterations to determine the key encryption key (KEK) based on a passphrase.
  • Pad the initialization vector with 0-byte values if necessesary, e.g. if initialization vector is 8 bytes but the encryption method (AES) requires an initialization vector of 16 bytes.
  • Decrypt the wrapped key data using the encryption method and mode, e.g. DES3-CBC, with the number of bits of the KEK (defined by encryption key size) and the initialization vector if applicable.
  • Remove the padding, specified by the padding type.

The decypted key data is of variable size and consists of:

OffsetSizeValueDescription
0...Block (or master) data encryption key (DEK)
......Block HMAC key
...5"CKIE\x00"Signature

Public key wrapped key

TODO: complete section

The public key wrapped key is 564 bytes in size and consists of:

OffsetSizeValueDescription
04Public key hash size
420Public key hash
244Unknown
284Unknown
324Unknown
364Unknown (encryption method?)
404Unknown
444Unknown
484Unknown (size)
52256Unknown
308256Unknown (empty values)

Keybag wrapped key

TODO: complete section

Algorithm identifiers

ValueIdentifierDescription
0CSSM_ALGID_NONENo algorithm (none)
1CSSM_ALGID_CUSTOMCustom algorithm
2CSSM_ALGID_DHDiffie Hellman key exchange
3CSSM_ALGID_PHPohlig Hellman key exchange
4CSSM_ALGID_KEAKey Exchange Algorithm
5CSSM_ALGID_MD2MD2
6CSSM_ALGID_MD4MD4
7CSSM_ALGID_MD5MD5
8CSSM_ALGID_SHA1SHA-1
9CSSM_ALGID_NHASHN-Hash
10CSSM_ALGID_HAVALHAVAL
11CSSM_ALGID_RIPEMDRIPE-MD
12CSSM_ALGID_IBCHASHIBC-Hash
13CSSM_ALGID_RIPEMACRIPE-MAC
14CSSM_ALGID_DESDES
15CSSM_ALGID_DESXDESX
16CSSM_ALGID_RDESRDES
17CSSM_ALGID_3DES_3KEY_EDE (or CSSM_ALGID_3DES_3KEY)Triple-DES with 3 keys applied encrypt, decrypt, encrypt (EDE)
18CSSM_ALGID_3DES_2KEY_EDE (or CSSM_ALGID_3DES_2KEY)Triple-DES with 2 keys applied encrypt, decrypt, encrypt (EDE), with the first key used for the first and last operation
19CSSM_ALGID_3DES_1KEY_EEETriple-DES with 1 keys applied encrypt, encrypt, encrypt (EEE), with the first key used for all operation
20CSSM_ALGID_3DES_3KEY_EEETriple-DES with 3 keys applied encrypt, encrypt, encrypt (EEE)
21CSSM_ALGID_3DES_2KEY_EEETriple-DES with 2 keys applied encrypt, encrypt, encrypt (EEE), with the first key used for the first and last operation
22CSSM_ALGID_IDEAIDEA
23CSSM_ALGID_RC2RC2
24CSSM_ALGID_RC5RC5
25CSSM_ALGID_RC4RC4
26CSSM_ALGID_SEALSEAL
27CSSM_ALGID_CASTCAST
28CSSM_ALGID_BLOWFISHBlowfish
29CSSM_ALGID_SKIPJACKSkipjac
30CSSM_ALGID_LUCIFERLucifer
31CSSM_ALGID_MADRYGAMadryga
32CSSM_ALGID_FEALFEAL
33CSSM_ALGID_REDOCREDOC 2
34CSSM_ALGID_REDOC3REDOC 3
35CSSM_ALGID_LOKILOKI
36CSSM_ALGID_KHUFUKHUFU
37CSSM_ALGID_KHAFREKHAFRE
38CSSM_ALGID_MMBMMB
39CSSM_ALGID_GOSTGOST
40CSSM_ALGID_SAFERSAFER (K-40, K-64, K-128)
41CSSM_ALGID_CRABCRAB
42CSSM_ALGID_RSARSA
43CSSM_ALGID_DSADSA
44CSSM_ALGID_MD5WithRSAMD5/RSA
45CSSM_ALGID_MD2WithRSAMD2/RSA
46CSSM_ALGID_ElGamalElGamal
47CSSM_ALGID_MD2RandomMD2-based random numbers
48CSSM_ALGID_MD5RandomMD5-based random numbers
49CSSM_ALGID_SHARandomSHA-based random numbers
50CSSM_ALGID_DESRandomDES-based random numbers
51CSSM_ALGID_SHA1WithRSASHA-1/RSA
52CSSM_ALGID_CDMFCDMF
53CSSM_ALGID_CAST3CAST3
54CSSM_ALGID_CAST5CAST5
55CSSM_ALGID_GenericSecretGeneric secret
56CSSM_ALGID_ConcatBaseAndKeyConcatenate base key with key
57CSSM_ALGID_ConcatKeyAndBaseConcatenate key with base key
58CSSM_ALGID_ConcatBaseAndDataConcatenate base key with data
59CSSM_ALGID_ConcatDataAndBaseConcatenate data with base key
60CSSM_ALGID_XORBaseAndDataXOR base key with data
61CSSM_ALGID_ExtractFromKeyExtract key from base key
62CSSM_ALGID_SSL3PreMasterGenSSL 3 with 48 byte pre-master key
63CSSM_ALGID_SSL3MasterDeriveDerive an SSL 3 key from a pre-master key
64CSSM_ALGID_SSL3KeyAndMacDeriveDerive SSL3 key and MAC
65CSSM_ALGID_SSL3MD5_MACSSL 3 with MD5 MAC
66CSSM_ALGID_SSL3SHA1_MACSSL 3 with SHA-1 MAC
67CSSM_ALGID_PKCS5_PBKDF1_MD5PKCS5 key derivation using PBKDF1 with MD5
68CSSM_ALGID_PKCS5_PBKDF1_MD2PKCS5 key derivation using PBKDF1 with MD2
69CSSM_ALGID_PKCS5_PBKDF1_SHA1PKCS5 key derivation using PBKDF1 with SHA-1
70CSSM_ALGID_WrapLynksSpyrus LYNKS DES based wrapping scheme with checksum
71CSSM_ALGID_WrapSET_OAEPSET key wrapping
72CSSM_ALGID_BATONFortezza BATON
73CSSM_ALGID_ECDSAElliptic Curve DSA
74CSSM_ALGID_MAYFLYFortezza MAYFLY
75CSSM_ALGID_JUNIPERFortezza JUNIPER
76CSSM_ALGID_FASTHASHFortezza FASTHASH
77CSSM_ALGID_3DESGenerix 3DES
78CSSM_ALGID_SSL3MD5SSL 3 with MD5
79CSSM_ALGID_SSL3SHA1SSL 3 with SHA-1
80CSSM_ALGID_FortezzaTimestampFortezza with timestamp
81CSSM_ALGID_SHA1WithDSASHA-1 with DSA
82CSSM_ALGID_SHA1WithECDSASHA-1 with Elliptic Curve DSA
83CSSM_ALGID_DSA_BSAFEDSA with BSAFE Key
84CSSM_ALGID_ECDHElliptic Curve DiffieHellman Key Exchange
85CSSM_ALGID_ECMQVElliptic Curve MQV key exchange
86CSSM_ALGID_PKCS12_SHA1_PBEPKCS12 SHA-1 PBE key derivation
87CSSM_ALGID_ECNRAElliptic Curve Nyberg-Rueppel
88CSSM_ALGID_SHA1WithECNRASHA-1 with Elliptic Curve Nyberg-Rueppel
89CSSM_ALGID_ECESElliptic Curve Encryption Scheme
90CSSM_ALGID_ECAESElliptic Curve Authenticate Encryption Scheme
91CSSM_ALGID_SHA1HMACSHA1-MAC
92CSSM_ALGID_FIPS186RandomFIPS186 Random
93CSSM_ALGID_ECCElliptic Curve Encryption (ECC)
94CSSM_ALGID_MQVDiscrete-Log MQV key exchange
95CSSM_ALGID_NRADiscrete-Log Nyberg-Rueppel Signature scheme
96CSSM_ALGID_IntelPlatformRandomIntel Platform Random Number Generator
97CSSM_ALGID_UTCDate and time value in the form: "YYYYMMDDhhmmss"
98CSSM_ALGID_HAVAL3HAVAL3 Digest
99CSSM_ALGID_HAVAL4HAVAL4 Digest
100CSSM_ALGID_HAVAL5HAVAL5 Digest
101CSSM_ALGID_TIGERTIGER Digest
102CSSM_ALGID_MD5HMACHMAC-MD5
103CSSM_ALGID_PKCS5_PBKDF2PKCS5 key derivation using PBKDF2 with SHA-1 (PBKDF2-HMAC-SHA1)
104CSSM_ALGID_RUNNING_COUNTERRunning hardware counter
0x80000000CSSM_ALGID_VENDOR_DEFINEDVendor defined algorithm
0x80000001CSSM_ALGID_AESAdvanced Encryption Standard (AES)

Padding types

ValueIdentifierDescription
0CSSM_PADDING_NONENo padding
1CSSM_PADDING_CUSTOMUnknown
2CSSM_PADDING_ZEROPad with 0
3CSSM_PADDING_ONEPad with 1
4CSSM_PADDING_ALTERNATEUnknown
5CSSM_PADDING_FFUnknown (Pad with 0xff?)
6CSSM_PADDING_PKCS5Pad using Public-Key Cryptography Standard (PKCS) 5 (RFC 2898)
7CSSM_PADDING_PKCS7Pad using Public-Key Cryptography Standard (PKCS) 7 (RFC 2315)
8CSSM_PADDING_CIPHERSTEALINGUnknown
9CSSM_PADDING_RANDOMUnknown
10CSSM_PADDING_PKCS1Pad using Public-Key Cryptography Standard (PKCS) 1 (RFC 2437)

Encryption modes

ValueIdentifierDescription
0CSSM_ALGMODE_NONEUnknown (Null algorithm mode)
1CSSM_ALGMODE_CUSTOMUnknown (Custom mode)
2CSSM_ALGMODE_ECBElectronic CodeBook (ECB) mode, without padding
3CSSM_ALGMODE_ECBPadElectronic CodeBook (ECB) mode with padding
4CSSM_ALGMODE_CBCCipher Block Chaining (CBC) mode, without padding
5CSSM_ALGMODE_CBC_IV8Cipher Block Chaining (CBC) mode with 8 byte initialization vector, without padding
6CSSM_ALGMODE_CBCPadIV8Cipher Block Chaining (CBC) mode with 8 byte initialization vector, with padding
7CSSM_ALGMODE_CFBCipher feedback (CFB) mode
8CSSM_ALGMODE_CFB_IV8Cipher feedback (CFB) mode with 8 byte initialization vector
9CSSM_ALGMODE_CFBPadIV8Cipher feedback (CFB) mode with 8 byte initialization vector, with padding
10CSSM_ALGMODE_OFBOutput FeedBack (OFB) mode
11CSSM_ALGMODE_OFB_IV8Output FeedBack (OFB) mode mode with 8 byte initialization vector
12CSSM_ALGMODE_OFBPadIV8Output FeedBack (OFB) mode with 8 byte initialization vector, with padding
13CSSM_ALGMODE_COUNTERCounter mode
14CSSM_ALGMODE_BCBlock Chaining mode
15CSSM_ALGMODE_PCBCPropagating Cipher Block Chaining (CBC) mode
16CSSM_ALGMODE_CBCCCipher Block Chaining (CBC) with checksum mode
17CSSM_ALGMODE_OFBNLFOutput FeedBack (OFB) with non-linear function mode
18CSSM_ALGMODE_PBCPlaintext Block Chaining (PBC) mode
19CSSM_ALGMODE_PFBPlaintext FeedBack (PFB) mode
20CSSM_ALGMODE_CBCPDCipher Block Chaining (CBC) if Plaintext Difference mode
21CSSM_ALGMODE_PUBLIC_KEYPublic key mode
22CSSM_ALGMODE_PRIVATE_KEYPrivate key mode
23CSSM_ALGMODE_SHUFFLEFortezza shuffle mode
24CSSM_ALGMODE_ECB6464 byte Electronic CodeBook (ECB) mode
25CSSM_ALGMODE_CBC6464 byte Cipher Block Chaining (CBC) mode
26CSSM_ALGMODE_OFB6464 byte Output FeedBack (OFB) mode
28CSSM_ALGMODE_CFB3232 byte Cipher feedback (CFB) mode
29CSSM_ALGMODE_CFB1616 byte Cipher feedback (CFB) mode
30CSSM_ALGMODE_CFB88 byte Cipher feedback (CFB) mode
31CSSM_ALGMODE_WRAPUnknown
32CSSM_ALGMODE_PRIVATE_WRAPUnknown
33CSSM_ALGMODE_RELAYXUnknown
34CSSM_ALGMODE_ECB128128 byte Electronic CodeBook (ECB) mode
35CSSM_ALGMODE_ECB9696 byte Electronic CodeBook (ECB) mode
36CSSM_ALGMODE_CBC128128 byte Cipher Block Chaining (CBC) mode
37CSSM_ALGMODE_OAEP_HASHUnknown (Algorithm mode for SET key wrapping?)
38CSSM_ALGMODE_PKCS1_EME_V15Public-Key Cryptography Standard (PKCS) 1 version 1.5
39CSSM_ALGMODE_PKCS1_EME_OAEPPublic-Key Cryptography Standard (PKCS) 1 version 2.0
40CSSM_ALGMODE_PKCS1_EMSA_V15Unknown
41CSSM_ALGMODE_ISO_9796Unknown
42CSSM_ALGMODE_X9_31Unknown

Encrypted block data

The encrypted block data can be decrypted using the following approach:

  • Calculate the specified block HMAC, e.g. HMAC-SHA-1, with the block HMAC key and the block number stored as a 32-bit big-endian value, where 0 represents the first block. This HMAC is used as the initialization vector for decryption.
  • Decrypt the encrypted data using the block encryption method and mode, e.g. AES-CBC, with the number of bits of the block DEK (defined by the block encryption key size) and the initialization vector if applicable.