|
| 1 | +package airplay |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/aes" |
| 5 | + "encoding/binary" |
| 6 | +) |
| 7 | + |
| 8 | +var fairplayInitialSessionKey = [16]byte{ |
| 9 | + 0xdc, 0xdc, 0xf3, 0xb9, 0x0b, 0x74, 0xdc, 0xfb, |
| 10 | + 0x86, 0x7f, 0xf7, 0x60, 0x16, 0x72, 0x90, 0x51, |
| 11 | +} |
| 12 | + |
| 13 | +var fairplayKDFPrefix = [17]byte{ |
| 14 | + 0xfa, 0x9c, 0xad, 0x4d, 0x4b, 0x68, 0x26, 0x8c, |
| 15 | + 0x7f, 0xf3, 0x88, 0x99, 0xde, 0x92, 0x2e, 0x95, 0x1e, |
| 16 | +} |
| 17 | + |
| 18 | +var fairplayKDFSuffix = [17]byte{ |
| 19 | + 0xec, 0x4e, 0x27, 0x5e, 0xfd, 0xf2, 0xe8, 0x30, |
| 20 | + 0x97, 0xae, 0x70, 0xfb, 0xe0, 0x00, 0x3f, 0x1c, 0x39, |
| 21 | +} |
| 22 | + |
| 23 | +// Only the second 128 bytes of the fixed SAP record participate in the KDF. |
| 24 | +var fairplayDefaultSAPTail = [128]byte{ |
| 25 | + 0x00, 0x01, 0xcc, 0x34, 0x2a, 0x5e, 0x5b, 0x1a, 0x67, 0x73, 0xc2, 0x0e, 0x21, 0xb8, 0x22, 0x4d, |
| 26 | + 0xf8, 0x62, 0x48, 0x18, 0x64, 0xef, 0x81, 0x0a, 0xae, 0x2e, 0x37, 0x03, 0xc8, 0x81, 0x9c, 0x23, |
| 27 | + 0x53, 0x9d, 0xe5, 0xf5, 0xd7, 0x49, 0xbc, 0x5b, 0x7a, 0x26, 0x6c, 0x49, 0x62, 0x83, 0xce, 0x7f, |
| 28 | + 0x03, 0x93, 0x7a, 0xe1, 0xf6, 0x16, 0xde, 0x0c, 0x15, 0xff, 0x33, 0x8c, 0xca, 0xff, 0xb0, 0x9e, |
| 29 | + 0xaa, 0xbb, 0xe4, 0x0f, 0x5d, 0x5f, 0x55, 0x8f, 0xb9, 0x7f, 0x17, 0x31, 0xf8, 0xf7, 0xda, 0x60, |
| 30 | + 0xa0, 0xec, 0x65, 0x79, 0xc3, 0x3e, 0xa9, 0x83, 0x12, 0xc3, 0xb6, 0x71, 0x35, 0xa6, 0x69, 0x4f, |
| 31 | + 0xf8, 0x23, 0x05, 0xd9, 0xba, 0x5c, 0x61, 0x5f, 0xa2, 0x54, 0xd2, 0xb1, 0x83, 0x45, 0x83, 0xce, |
| 32 | + 0xe4, 0x2d, 0x44, 0x26, 0xc8, 0x35, 0xa7, 0xa5, 0xf6, 0xc8, 0x42, 0x1c, 0x0d, 0xa3, 0xf1, 0xc7, |
| 33 | +} |
| 34 | + |
| 35 | +func deriveFairPlayWrappingKey(sapTail []byte, message []byte) [16]byte { |
| 36 | + var decrypted [128]byte |
| 37 | + decryptFairPlayMessage(message, decrypted[:]) |
| 38 | + |
| 39 | + // The KDF input is a 290-byte protocol record followed by ordinary MD5 |
| 40 | + // padding. The compression itself is FairPlay's modified MD5/SAP-hash |
| 41 | + // combination, not a standard MD5 digest. |
| 42 | + var material [320]byte |
| 43 | + offset := copy(material[:], fairplayKDFPrefix[:]) |
| 44 | + offset += copy(material[offset:], decrypted[:]) |
| 45 | + offset += copy(material[offset:], sapTail[:128]) |
| 46 | + offset += copy(material[offset:], fairplayKDFSuffix[:]) |
| 47 | + material[offset] = 0x80 |
| 48 | + binary.LittleEndian.PutUint64(material[len(material)-8:], uint64(offset)*8) |
| 49 | + |
| 50 | + state := fairplayWordsFromLittleEndian(fairplayInitialSessionKey) |
| 51 | + for offset := 0; offset < len(material); offset += 64 { |
| 52 | + block := material[offset : offset+64] |
| 53 | + modified := fairplayMD5Compress(state, block, fairplayKDFMutation) |
| 54 | + hashed := fairplaySAPHash(block) |
| 55 | + for word := range state { |
| 56 | + state[word] = modified[word] + binary.LittleEndian.Uint32(hashed[word*4:]) |
| 57 | + } |
| 58 | + } |
| 59 | + return fairplayWordsBigEndian(state) |
| 60 | +} |
| 61 | + |
| 62 | +func unwrapFairPlayKey(m3 []byte, ekey []byte) [16]byte { |
| 63 | + aesKey := deriveFairPlayWrappingKey(fairplayDefaultSAPTail[:], m3) |
| 64 | + cipher, err := aes.NewCipher(aesKey[:]) |
| 65 | + if err != nil { |
| 66 | + panic(err) // aesKey always has the fixed AES-128 length. |
| 67 | + } |
| 68 | + |
| 69 | + var keyOut [16]byte |
| 70 | + cipher.Decrypt(keyOut[:], ekey[56:72]) |
| 71 | + for i := range keyOut { |
| 72 | + keyOut[i] ^= ekey[16+i] |
| 73 | + } |
| 74 | + return keyOut |
| 75 | +} |
0 commit comments