AirPods keys

AirPods broadcast their status over Bluetooth Low Energy (BLE) several times a second. Part of each broadcast is public and part is encrypted. Without keys, CAPod reads the public part: battery in 10% steps, charging state, case lid and in-ear status. Two keys from your own AirPods unlock more:

The keys stay the same no matter which phone or computer the AirPods are paired with.

These keys belong to your AirPods. Don’t post them in public issues, chats or screenshots.

Automatic retrieval

On phones that support the direct connection, CAPod asks the AirPods for both keys when it connects and saves them in the profile whose Paired device is those AirPods. Nothing to do on your part: open the profile and both keys show Configured.

Extract the keys manually if your phone doesn’t support the direct connection, or if the keys stay Not set.

Manual extraction

Android has no way to show you the keys, so manual extraction needs a computer the AirPods are paired with.

macOS with a script

This script reads the keys of every paired AirPods or Beats device from the macOS keychain. macOS asks for your password before it allows access.

get_airpod_keys.sh
#!/usr/bin/env bash

set -u

SYSTEM_KEYCHAIN="/Library/Keychains/System.keychain"

usage() {
  cat <<'EOF'
Usage:
  ./get_airpod_keys.sh
  ./get_airpod_keys.sh --address AA:BB:CC:DD:EE:FF

Extracts AirPods / Beats MagicAcc keys from the macOS MobileBluetooth
Keychain item for CAPod:
  - MagicAccIRK     -> CAPod Identity key
  - MagicAccEncKey  -> CAPod Encryption key

macOS may show Keychain permission prompts. These keys are sensitive.
EOF
}

normalize_mac() {
  printf '%s\n' "$1" | tr '[:lower:]' '[:upper:]'
}

discover_bluetooth_addresses() {
  system_profiler SPBluetoothDataType -json 2>/dev/null \
    | awk -F '"' '/"device_address"[[:space:]]*:/ { print $4 }' \
    | sort -u
}

lookup_mobilebluetooth_item() {
  local address="$1"

  security find-generic-password \
    -a "$address" \
    -s MobileBluetooth \
    -g "$SYSTEM_KEYCHAIN" 2>&1
}

extract_plist_value() {
  local key="$1"

  # security(1) prints the password plist as an escaped one-line string, so
  # this intentionally parses the <key>...</key><string>...</string> pattern.
  sed -nE "s/.*<key>${key}<\\/key>[^<]*<string>([^<]+)<\\/string>.*/\\1/p"
}

print_keys_for_address() {
  local address="$1"
  local output
  local irk
  local enc_key
  local local_address

  output="$(lookup_mobilebluetooth_item "$address")"

  irk="$(printf '%s\n' "$output" | extract_plist_value "MagicAccIRK")"
  enc_key="$(printf '%s\n' "$output" | extract_plist_value "MagicAccEncKey")"
  local_address="$(printf '%s\n' "$output" | extract_plist_value "LocalAddress")"

  if [[ -z "$irk" || -z "$enc_key" ]]; then
    return 1
  fi

  printf 'Bluetooth address: %s\n' "$address"
  if [[ -n "$local_address" ]]; then
    printf 'Local Mac address: %s\n' "$local_address"
  fi
  printf 'CAPod Identity key / MagicAccIRK:\n%s\n\n' "$irk"
  printf 'CAPod Encryption key / MagicAccEncKey:\n%s\n' "$enc_key"
  printf '%s\n' '---'
  return 0
}

main() {
  local addresses=()
  local found=0

  while [[ $# -gt 0 ]]; do
    case "$1" in
      --address|-a)
        if [[ $# -lt 2 ]]; then
          usage >&2
          exit 2
        fi
        addresses+=("$(normalize_mac "$2")")
        shift 2
        ;;
      --help|-h)
        usage
        exit 0
        ;;
      *)
        printf 'Unknown argument: %s\n\n' "$1" >&2
        usage >&2
        exit 2
        ;;
    esac
  done

  if [[ ${#addresses[@]} -eq 0 ]]; then
    while IFS= read -r address; do
      [[ -n "$address" ]] && addresses+=("$(normalize_mac "$address")")
    done < <(discover_bluetooth_addresses)
  fi

  if [[ ${#addresses[@]} -eq 0 ]]; then
    printf 'No Bluetooth device addresses found. Try --address AA:BB:CC:DD:EE:FF\n' >&2
    exit 1
  fi

  for address in "${addresses[@]}"; do
    if print_keys_for_address "$address"; then
      found=1
    fi
  done

  if [[ "$found" -eq 0 ]]; then
    printf 'No MobileBluetooth entries with MagicAccIRK/MagicAccEncKey were found.\n' >&2
    printf 'Make sure the AirPods are paired with this Mac, then try again.\n' >&2
    exit 1
  fi
}

main "$@"

Contributed by @akshaynexus in #597.

macOS with a shorter script

This shorter alternative runs with plain sh and needs python3. It reads the keys of the first connected AirPods or Beats device, so connect them to the Mac first. Run it with sh airpod_key_retrieval_posix.sh. If the key fields come out empty, macOS denied keychain access or the AirPods aren’t paired with this Mac.

airpod_key_retrieval_posix.sh
ADDR="$(
  system_profiler SPBluetoothDataType -json 2>/dev/null |
  python3 -c '
import json, sys, re

data = json.load(sys.stdin)

for controller in data.get("SPBluetoothDataType", []):
    for item in controller.get("device_connected", []):
        for name, info in item.items():
            if re.search(r"AirPods|Beats", name, re.I):
                print(info.get("device_address", ""))
                raise SystemExit
'
)"

if [ -z "$ADDR" ]; then
  echo "No connected AirPods or Beats device found."
  exit 1
fi

OUT="$(security find-generic-password \
  -a "$ADDR" \
  -s MobileBluetooth \
  -g /Library/Keychains/System.keychain 2>&1)"

IRK="$(printf '%s\n' "$OUT" |
  sed -nE 's/.*<key>MagicAccIRK<\/key>[^<]*<string>([^<]+)<\/string>.*/\1/p')"

ENC="$(printf '%s\n' "$OUT" |
  sed -nE 's/.*<key>MagicAccEncKey<\/key>[^<]*<string>([^<]+)<\/string>.*/\1/p')"

LOCAL="$(printf '%s\n' "$OUT" |
  sed -nE 's/.*<key>LocalAddress<\/key>[^<]*<string>([^<]+)<\/string>.*/\1/p')"

printf '\nAirPods Keys\n'
printf '%s\n' '--------------------------------------------'
printf 'AirPods Address : %s\n' "$ADDR"
printf 'Mac Address     : %s\n' "$LOCAL"
printf 'Identity Key    : %s\n' "$IRK"
printf 'Encryption Key  : %s\n' "$ENC"
printf '%s\n' '--------------------------------------------'

Contributed by @ikcv in #597.

macOS with Keychain Access

This works without a script, but not on every macOS version.

  1. Pair the AirPods with the Mac.
  2. Hold Option and click the Bluetooth icon in the menu bar. Note the address of your AirPods, for example 9C:FC:….
  3. Open Keychain Access and search for MobileBluetooth. There may be several entries. Open the one whose Account is the address from step 2.
  4. Tick Show password and enter your password when asked. macOS may ask more than once.
  5. Copy the text from the password field. It is a short XML document. MagicAccIRK is the identity key and MagicAccEncKey is the encryption key:
<key>MagicAccEncKey</key>
<string>3A-9C-81-57-E6-44-7F-F2-FB-8E-07-63-6C-99-E1-29</string>
<key>MagicAccIRK</key>
<string>71-44-65-1E-E2-CA-D1-26-F2-4E-20-EE-3E-CC-DE-77</string>

Linux

This Python script connects to paired AirPods and asks them for their keys. Pass the AirPods’ Bluetooth address as the argument.

airpod_key_retrieval.py
import sys
import socket

PROXIMITY_KEY_TYPES = {
    0x01: "IRK",
    0x04: "ENC_KEY",
}

def parse_proximity_keys_response(data):
    if len(data) < 7 or data[4] != 0x31:
        return None
    key_count = data[6]
    keys = []
    offset = 7
    for _ in range(key_count):
        if offset + 3 >= len(data):
            break
        key_type = data[offset]
        key_length = data[offset + 2]
        offset += 4
        if offset + key_length > len(data):
            break
        key_bytes = data[offset:offset + key_length]
        keys.append((PROXIMITY_KEY_TYPES.get(key_type, f"TYPE_{key_type:02X}"), key_bytes))
        offset += key_length
    return keys

def hexdump(data):
    return " ".join(f"{b:02X}" for b in data)

def main():
    if len(sys.argv) != 2:
        print(f"Usage: {sys.argv[0]} <MAC>")
        sys.exit(1)

    bdaddr = sys.argv[1]
    PSM = 0x1001

    handshake = bytes.fromhex("00 00 04 00 01 00 02 00 00 00 00 00 00 00 00 00")
    key_req = bytes.fromhex("04 00 04 00 30 00 05 00")

    sock = socket.socket(socket.AF_BLUETOOTH, socket.SOCK_SEQPACKET, socket.BTPROTO_L2CAP)
    sock.connect((bdaddr, PSM))
    sock.send(handshake)
    sock.send(key_req)

    try:
        while True:
            pkt = sock.recv(1024)
            keys = parse_proximity_keys_response(pkt)
            if keys is not None:
                print("Proximity Keys:")
                for name, key_bytes in keys:
                    print(f"  {name}: {hexdump(key_bytes)}")
                break
    finally:
        sock.close()

if __name__ == "__main__":
    main()

Contributed by @kavishdevar in #290.

Windows

Not confirmed to work for AirPods yet. The Home Assistant Private BLE Device guide shows how to read a device’s identity key from the Windows registry.

Reported in #757.

Any computer with a virtual machine

@2Elvin’s guide runs macOS in a virtual machine to extract the keys on any platform.

Enter the keys in CAPod

  1. Open Settings, Devices and tap your profile.
  2. Tap Identity Key and paste the identity key (MagicAccIRK, or IRK from the Linux script).
  3. Tap Encryption Key and paste the encryption key (MagicAccEncKey, or ENC_KEY).
  4. Save the profile.

CAPod accepts the keys with dashes, spaces or neither, for example 71-44-65-1E-…. Once both keys work, the dashboard shows exact battery percentages for your AirPods.