Skip to main content

How SIM Authentication Works with AKA

Updated 4 min readMarkdown

Every time a phone attaches to a mobile network, the network and the SIM prove to each other that they share a secret, without that secret ever crossing the air. The protocol is AKA, Authentication and Key Agreement, defined by 3GPP. It is the reason a cloned IMSI is not enough to impersonate a subscriber. This post walks through the LTE-era flow and the values involved.

The shared secret

Two places hold a permanent key, called K, for each subscriber: the SIM (the USIM application in the profile) and the operator's authentication center, which is part of the subscriber database. K is never sent anywhere. Everything else in the protocol is derived from it, along with an operator-specific parameter and the algorithm set in use. The widely used example algorithm set is Milenage, and operators can use their own.

What the network sends

When the core network wants to authenticate a subscriber, it asks the subscriber database for an authentication vector. The database generates a random challenge and computes the answers for that challenge:

  • RAND: a 128-bit random number, the challenge.
  • AUTN: the network authentication token. It carries a masked sequence number, an authentication management field, and a message authentication code computed with K.
  • XRES: the response the network expects to receive from the SIM.
  • Key material: cipher and integrity keys (CK, IK) that lead to the session keys, which in LTE are combined into a key called K_ASME.

The network sends RAND and AUTN to the phone, which passes them to the SIM. It keeps XRES to compare against.

What the SIM does

The SIM receives RAND and AUTN and works in this order:

1. Compute the anonymity key from K and RAND, and unmask the sequence number (SQN).
2. Compute the expected MAC from K, RAND, SQN and AMF.
3. Compare it with the MAC in AUTN.
     mismatch  -> reject: the network is not genuine
4. Check that SQN is fresh (newer than the SIM has accepted).
     stale     -> ask for resynchronization
5. Compute RES and the keys CK and IK from K and RAND.
6. Return RES.

Steps 3 and 4 are what make the authentication mutual. The SIM authenticates the network by checking the MAC and by insisting on a fresh sequence number, which stops an attacker from replaying an old challenge.

Back at the network

The network compares the returned RES with the stored XRES. If they match, the subscriber is authenticated. Both sides now hold the same CK and IK without having transmitted them, and the session keys are derived from those. Later steps protect signaling and user data with keys derived from that material.

Resynchronization

The sequence number is the part of AKA that causes most operational trouble. The SIM keeps the highest sequence number it accepted, and the subscriber database keeps its own counter. If they drift, for example after a database restore, the SIM will reject valid-looking challenges as stale. In that case it returns a resynchronization token (AUTS), which lets the subscriber database work out where the SIM's counter is and reset its own.

From a systems point of view, this means the subscriber database has to persist that counter reliably. Losing it silently is an outage for the affected subscribers.

A sketch of the verification logic

This is pseudocode for the decision structure, not an implementation of the crypto functions:

def usim_authenticate(K, rand, autn, stored_sqn):
    sqn_xor_ak, amf, mac_a = split_autn(autn)
    ak  = f5(K, rand)
    sqn = sqn_xor_ak ^ ak
    if f1(K, rand, sqn, amf) != mac_a:
        return ("MAC_FAILURE", None)          # network not authentic
    if not sqn_is_fresh(sqn, stored_sqn):
        return ("SYNC_FAILURE", build_auts()) # ask to resync
    res = f2(K, rand)
    ck, ik = f3(K, rand), f4(K, rand)
    return ("OK", (res, ck, ik))

The functions f1 to f5 are the algorithm set's building blocks for the MAC, the response, the keys and the anonymity key.

In 5G

5G keeps the same idea and changes the surrounding plumbing. Authentication can be done with 5G AKA or with EAP-AKA prime, the home network takes a larger part in the final decision, and the permanent identifier is concealed before it goes over the air. The shared-secret logic is recognizably the same.

Takeaways

  • The key K never leaves the SIM and the subscriber database.
  • Authentication is mutual, using a MAC and a sequence number.
  • The sequence counter is a real piece of state that has to be stored durably.

About the author: I work on content and web development at eSIMCircle, a travel eSIM provider. This series comes from the engineering side of that work.

About the author

  • eSIMCircle provides affordable global eSIM plans with reliable data in 200+ countries.

No comments yet. Be the first to comment.

More from this blog