*other* reasons for "incorrect MAC" in AUTHENTICATE

I’m working on a VoWifi connection to a new provider. StrongSWAN is able to connect IPsec to their ePDG.

When my code tries to connect an inner IPsec tunnel to the P-CSCF the AUTHENTICATE fails with an incorrect MAC:

pySIM-shell (00:MF/ADF.USIM)> authenticate 65ac...308a 71ab...ac85
EXCEPTION of type 'SwMatchError' occurred with message: SW match failed!
Expected 9000 and got 9862: Security management - Authentication error, incorrect MAC

This would usually mean I’m using the wrong SIM card, like some other provider’s SIM card so the key material is wrong, but this is the only SIM in the system. I removed all other cards.


If I copy the Nonce from the StrongSWAN connection and try to re-run the authenticate in pySIM-shell, it all goes as expected: the MAC passes but the authentication fails due to the sequence number and returns an AUTS.

pySIM-shell (00:MF/ADF.USIM)> authenticate c2e7...f20f de29...fff4
{
    "synchronisation_failure": {
        "auts": "7020...5f50"
    }
}

What makes me really confused is a pcap from an iPhone using this same SIM card and successfully connecting to the provider’s P-CSCF. I see the iPhone send a REGISTER, I see the P-CSCF send back a 401 with a Nonce, I see the iPhone successfully establish a new IPsec tunnel — so the iPhone’s authenticate must have worked.

If I try to use the Nonce from the iPhone pcap with pySim-shell AUTHENTICATE I’d expect to get an AUTS since the sequence number will be wrong. But I don’t, I get incorrect MAC. That makes no sense, I don’t understand how the iPhone manages to authenticate.

pySIM-shell (00:MF/ADF.USIM)> authenticate 753b...1583 0c7e...d79b
EXCEPTION of type 'SwMatchError' occurred with message: SW match failed!
Expected 9000 and got 9862: Security management - Authentication error, incorrect MAC

I read in OPc vs OP in SIM keys | Nick vs Networking that recent SIM cards sometimes no longer have the same Operator key across all SIM cards, that they instead make Operator be just more random bits which are unique to each SIM. So every SIM card has completely unique keying material, and they have to look up both K and OP for each SIM.

It occurs to me that if I’ve gotten my IMSI wrong, and the provider is sending a Nonce which is intended some other IMSI, that this would fail with an Incorrect MAC because the OP key wouldn’t work.

I’ve checked and double-checked the IMSI and don’t see a problem, but I’m going to keep looking as this seems likely. I’ve also looked at the contents of the iPhone’s REGISTER, and I’m sending the same Authorization in mine with the same username and realm and other fields.

Are there other ways which I’m not thinking of where the symptom of the failure would be an incorrect MAC?


There is an odd thing about this Alcatel-Lucent P-CSCF: it includes a very long opaque field in the WWW-Authenticate of its 401, where the value starts with ALU:

WWW-Authenticate: Digest realm="one.att.net",
    nonce="ZazW...7rIU=",
    opaque="ALU:QbkR...AZGg__",
    algorithm=AKAv1-MD5,
    qop="auth"

I assume I’m not supposed to use opaque as part of the authenticate operation somehow. It looks like something I’m just supposed to send back at some point.

Maybe your SIM card is not just an USIM but also has an ISIM application on it? In that case, the IMS authentication would use the ADF.ISIM while LTE/EPC or 5GC authentication uses ADF.USIM.

You can check using pySim-shell. if select ADF.ISIM works, then your card has an ISIM application.

1 Like

Indeed, that is it. If I select ADF.ISIM and then authenticate the MAC passes and I get the AUTS failure I expect. This Nonce is from a pcap and has already been authenticated, the sequence number should fail.

pySIM-shell (00:MF/ADF.ISIM)> authenticate 753b...1583 0c7e...d79b 
{
    "synchronisation_failure": {
        "auts": "1819...7b8a"
    }
}

Interestingly when I received this SIM card the very first thing I did was export --json, where the ADF.ISIM does appear in the output.

After putting the card into an iPhone and an Android phone to gather pcaps, now when I put it back into my Linux system the export --json no longer shows an ADF.ISIM — but it is still there, because I can select it and run authentication on it. Weird.

Anyway, thank you very much, I need to work on my system to know about ADF.ISIM with some providers.

1 Like

The process should be with any provider:

  • use ADF.ISIM, if present
  • fall back to ADF.USIM, if ADF.ISIM absent
    This is only true for the IMS part. The ePDG IPswc should always use ADF.USIM