KBAG

Apple's IMG3 and IMG4 security scheme uses a data format called a KBAG. At the bottom of a firmware file, you will see something that will, on the ASCII side of your hex editor, say "GABK", which, as ARM is little-endian based, is "KBAG" flipped. Look on the hex side and you will see the KBAG according to this format:

How it works
It boils down to using the GID Key to decrypt  and , then using that key and IV to decrypt the DATA section of the file (the code itself).

Because of the circumstances with the IMG3 File Format, the kernel never needs to even touch the GID Key anymore, as its job is to just flash the image to the NOR as is, with container and all.

To grab the KBAG for img3 files, you'd run.

This is different with img4 files. For these, you can use img4lib and run the following command:.

KBAG Format
typedef struct Unparsed_KBAG { uint32_t magic;      // string with bytes flipped. "KBAG" in little-endian. uint32_t fullSize;   // size of KBAG, its contents, and it's padding. uint32_t tagDataSize; // size of KBAG without this (0xC byte sized) header. uint32_t cryptState; // 1 if the key and IV in the KBAG are encrypted with the production GID Key. // 2 if the key and IV are encrypted with the development GID Key, used when the processor is demoted. uint32_t aesType;    // 0x80 = AES128 / 0xC0 = AES192 / 0x100 = AES256 uint8_t encIV[16];   // IV for the firmware file, encrypted with the GID Key. uint8_t encKey[0];   // Key for the firmware file, encrypted with the GID Key. Size determined by aesType/8. } UnparsedKbag_t;