Intro

All Arm-based Apple devices include a "Secure Enclave", a built-in hardware security module. You can store passkeys, SSH keys, age keys, and more in there, making extraction of those private keys functionally impossible.

This is how to set up MacOS on an Arm-based Central Processing Unit (CPU), such as an M1 MacBook Pro, M4 Mac Mini, etc.

This is one of the "implementation" details of my "Why use hardware security tokens" blog post.

Requirements

  • You must install openssl rather than using the bundled openssl from Apple (probably via homebrew.)

  • MacOS Sequoia or later

Use Secretive

I do this setup without any additional software. I used to use Secretive and it is very nice software, and is what I’d generally recommend, but I prefer to avoid additional software dependencies where I can. If you want a fully-native approach you may continue with this post, otherwise just use Secretive.

Additional notes

I use Fish, so I have the following code setup as two fish functions, and you can find them on Codeberg. But for clarity, they are here as two bash snippets:

Creating an ssh key using sc_auth

Because, as Alex Haydock also notes, MacOS doesn’t honor the KEYCHAIN_CERTIFICATES env var (and I tried a lot of different ways, at least on Sequoia) so all certificate handles are exported every time you generate a new cert.

To account for this, there are two functions:

  1. Generate a new SSH identity on the Secure Enclave

  2. Idempotently copy each cert handle while being exported

Environment

Set your environment via .profile or similar:

export SSH_SK_PROVIDER=/usr/lib/ssh-keychain.dylib

Or in your ssh config, something like:

Host *
    SecurityKeyProvider /usr/lib/ssh-keychain.dylib

Generate Keys

label="some label"  # change this!
require_touch=1  # set to 0, if you don't need touch confirmation for this key
ssh_dir="$HOME/.ssh"

if not test -d "$ssh_dir"
    mkdir -p "$ssh_dir"
    chmod 700 "$ssh_dir"
end

# Check if an identity with this label already exists idempotently
if sc_auth list-ctk-identities 2>/dev/null | grep -q "$label"
    echo "Idempotent check: Secure Enclave identity '$label' already exists."
else
    # Build arguments for sc_auth
    sc_args=("create-ctk-identity" "-l" "$key_label" "-k" "p-256-ne")

    if test "$require_touch" -ne 0; then
        sc_args+=("-t" "bio")
        echo "Creating Secure Enclave key '$key_label' with Touch ID enforcement..."
    else
        echo "Creating Secure Enclave key '$key_label' without Touch ID requirement..."
    fi

    # Execute creation command
    printf "+ sc_auth %s\n" "$sc_args"
    if ! sc_auth "${sc_args[@]}"; then
        echo "Error: Failed to create Secure Enclave identity via sc_auth." >&2
        return 1 2>/dev/null
    else
      echo "Successfully created Secure Enclave identity: $key_label"
    fi
fi

Copy handles

The private keys can never leave the Secure Enclave, however you can export "handles" so openssh knows which key to use. PINs are ignored on Secure Enclave so you should ignore any prompt for that and just leave it blank.

This portion is more annoying because it requires running two open terminal tabs/windows.

ssh_dir="$HOME/.ssh"
echo "Exporting Secure Enclave key handles to $ssh_dir..."
# Note: ssh-keygen -K may prompt for a PIN/passcode which can be left blank for CTK keys
tmp_dir=$(mktemp -d)
cd "$tmp_dir" || exit 1

printf "Exporting all keys in the following order (MacOS doesn't seem to honor KEYCHAIN_CERTIFICATES):\n"
sc_auth list-ctk-identities -t ssh

printf "\nIn another tab, run the other code snippet (in the '%s' directory) after each export (and press 'y' to overwrite)\n" "$(pwd)"
printf "NOTE: Leave PIN empty!\n\n"
ssh-keygen -w /usr/lib/ssh-keychain.dylib -K -N ""

This is the second portion where you run after each key gets exported. This is idempotent and gets the label from sc_auth, so you don’t have to worry about remembering labels, etc.

function copy_secure_enclave_key() {
    # these are the defaults
    export pubkey="id_ecdsa_sk_rk.pub"
    export privkey="id_ecdsa_sk_rk"

    # Verify input file exists
    if [ -z "$pubkey" ] || [ ! -f "$pubkey" ]; then
        echo "Error: Please provide a valid public key file path." >&2
        return 1
    fi

    # Extract the SHA256 fingerprint hash from ssh-keygen using grep regex matching
    target_hash=$(ssh-keygen -lf "$pubkey" 2>/dev/null | grep -oE 'SHA256:[^[:space:]]+')
    if [ -z "$target_hash" ]; then
        return 1
    fi

    printf "pubkey has hash: %s\n" "$target_hash"

    # Query sc_auth and parse columns using awk
    export result=$(sc_auth list-ctk-identities -t ssh 2>/dev/null | awk -v target="$target_hash" '
        $2 == target {
            label = $4
            print label
            found = 1
            exit
        }
        END {
            if (!found) exit 1
        }
    ')
    export exit_code=$?

    if [ "$exit_code" -eq 0 ] && [ -n "$result" ]; then
        printf "sc_auth list has a match on label: '%s'\n" "$result"
        cp -v "$privkey" "$HOME/.ssh/id_ecdsa_sk_rk_$result"
        sed "s/ ssh:/ ssh: $result/g" id_ecdsa_sk_rk.pub | tee ~/.ssh/id_ecdsa_sk_rk_%s.pub

        printf "\nPublic key successfully generated and located at: ~/.ssh/id_ecdsa_sk_rk_%s\n" "$result"
        printf "To configure OpenSSH to recognize this key explicitly, add the following to your ~/.ssh/config:\n"
        printf "  Host *\n"
        printf "     IdentityFile id_ecdsa_sk_rk_%s\n" "$result"
    else
        printf "No match found! Is this key an sc_auth ctk-identity?\n"
        return 1
    fi
}

# run this command after each newly generated handle (from the first snippet)
copy_secure_enclave_key
Some terminal emulators struggle with big copy-paste blocks in MacOS Bash. Try writing this to a file then bash yourfile.sh.

age key

This requires using age-plugin-se, which requires MacOS 26+. I am not a fan of the new OS so I’m stuck on Sequoia and cannot test it. The linked page is fairly straightforward on the standard use case.

The Apple secure enclave does not generally support uploading already-created keys like we can on YubiKey, so the approach would be to:

  1. Create a "standard" recipient key.

  2. Encrypt the identity to that key to multiple recipients, such as:

    1. The YubiKey age key

    2. Another MacOS Secure Enclave age key

    3. An offline "recovery" age key

    4. or so on…​

Then any of those keys can be used to decrypt files as long as the sender used that "standard" key as the recipient.