Changing a SIM card over the air, step by step
Choose SMS (GSM 03.48, now ETSI TS 102 225 and 3GPP TS 31.115) or HTTPS over BIP with TLS-PSK (GlobalPlatform SCP81) for one remote update from the operator's OTA server to a file on the SIM and back. Every packet, key and checksum is computed in your browser. Change a value or choose a scenario to see how the update travels.
Runs in your browser; nothing you enter is sent anywhere. Test keys only.
- ETSI TS 102 225
- 3GPP TS 31.115
- ETSI TS 102 226
- ETSI TS 102 223
- GP Amendment B
- RFC 4279 · RFC 5246
What this walkthrough models
- Route
- SCP80: a TS 102 225 secured packet in SMS (3GPP TS 31.115), through an SMSC over SMPP, an ENVELOPE (SMS-PP download) to the card, and the Proof of Receipt back
- Card
- A USIM with the RFM application of TS 102 226 and a small file system (EF_IMSI, EF_SPN, EF_FPLMN, EF_OPLMNwAcT). The commands run on it.
- Computed
- Every byte: the RFM script, the packet with each CMAC or CBC-MAC and cipher block, SMPP, SMS-DELIVER, ENVELOPE, the card's checks, the PoR and its way back
- Illustrative
- Network timings, the core network's messages (explained, not encoded), the SMSC's message ID and delivery receipt, the server's timeout
All values: card, addresses, keys, counters, SCP81 session
The operator decides what to change
OTA serverSet the service provider name the phone shows to "SimProbe". The operator's OTA platform runs this as a campaign over many cards; this walkthrough follows one card, ICCID 8955050000000123456, phone number +5511987654321. The file is EF_SPN (6F46) in the USIM application, and Remote File Management (RFM, ETSI TS 102 226) is the card application that writes files on the operator's behalf.
- Card
- 8955050000000123456ICCID
- Subscriber
- +5511987654321
- File
- 6F46 EF_SPNTS 31.102 §4.2.12
The commands for the card: an RFM script
OTA serverRemote File Management sends ordinary card commands (ETSI TS 102 221) inside the OTA message. This script selects EF_SPN, then writes it. In the compact format of TS 102 226 §5.1 the commands follow one another. The TAR B0 00 01 picks the application that runs them: RFM, an ADF such as the USIM (TS 31.116), compact format (TS 101 220 annex D). For an ADF's RFM application the ADF is selected at the start of the command string (TS 102 226 §7.3), so the script can select 6F46 directly.
- 1. SELECT 6F46
- 00 A4 00 0C 02 + 2 data bytesP2 0C: select, no data back
- 2. UPDATE BINARY
- 00 D6 00 00 11 + 17 data byteswrite 17 bytes from offset 0
- TAR
- B0 00 01RFM, an ADF such as the USIM (TS 31.116), compact format
The security header: SPI, KIc, KID, TAR, counter
OTA serverThe server builds a TS 102 225 command packet. Its header says how the packet is protected (SPI), which keys protect it (KIc for ciphering, KID for the checksum, each with an algorithm and a key version), which application gets it (TAR), and a counter (CNTR). With SPI1 b5b4 = 10 the card runs the packet only if the counter is higher than the last one it accepted, so a recorded message cannot be replayed (TS 102 225 §5.1.4). AES needs a counter check (§5.1.2).
- SPI
- 16 39cryptographic checksum (CC), ciphered, process only if the counter is higher than the card's; PoR always
- KIc / KID
- 12 / 12AES-128: CBC ciphering, CMAC checksum
- TAR
- B0 00 01
- CNTR
- 00 00 00 00 05
The checksum first, then the ciphering
OTA serverThe checksum (CC) is computed first, over the header and the data: CPL and CHL are included (TS 31.115 §4.2), and so is the padding that ciphering will need, counted by PCNTR. Then everything from the counter on is ciphered with the KIc key in CBC mode, with a zero initial chaining value (TS 102 225 §5.1.2). The CPL, CHL, SPI, KIc, KID and TAR stay readable: the card needs them to find the keys.
- Padding
- 5 bytesPCNTR 05: fills the last 16-byte block
- CC
- 98 B2 C1 7F 6F B5 F7 A0AES CMAC with the KID key
- Packet
- 00 38 15 16 39 12 12 B0 00 01 69 52 5B 94 21 E7 09 58 0F 3E 05 AD 2F D1 8D C5 30 51 D4 33 …CPL to TAR in clear, the rest ciphered
Into SMS user data
OTA serverThe packet goes into the SMS user data behind a user data header: UDHL 02, IEI 70 (Command Packet Identifier), length 00 (TS 31.115 §4.2). Header and packet are 61 of the 140 octets one SMS can carry.
- User data
- 02 70 00 …61 octets; UDH 02 70 00
The OTA server hands the SMS to the SMSC
OTA server → SMSCOTA servers usually reach the SMS centre over SMPP. The submit_sm (SMPP 3.4 §4.4.1) says where the SMS goes, that its short message starts with a user data header (esm_class 40), that it is (U)SIM data download (protocol_id 7F) and 8-bit class 2 data (data_coding F6), and asks for a delivery receipt. The SMSC answers submit_sm_resp with its message ID and stores the SMS until it can deliver it.
- destination_addr
- 5511987654321international, ISDN
- esm_class / protocol_id / data_coding
- 40 / 7F / F6
- registered_delivery
- 01delivery receipt requested
The SMSC finds the subscriber and forwards the SMS
SMSC → Mobile networkThe SMSC asks the subscriber's home register where the phone is (MAP SendRoutingInfoForSM to the HLR, or its equivalent in the HSS or UDM), then sends the message to the node serving the phone: the MSC with MAP MT-ForwardSM, the MME over SGs, an IP-SM-GW for SMS over IMS, or the SMSF in 5G. That node pages the phone and delivers the SMS over the radio. These network messages are shown with the fields that matter, not byte by byte.
- SRI-for-SM
- MSISDN 5511987654321answer: IMSI 724050000012345 and the serving node's address
- MT-ForwardSM
- SM-RP-DA = IMSI, SM-RP-OA = SMSC addressSM-RP-UI: the SMS-DELIVER of the next step (3GPP TS 29.002)
Delivered to the handset as an SMS-DELIVER
Mobile network → HandsetThe handset receives an ordinary SMS-DELIVER (TS 23.040 §9.2.2.1). Its protocol identifier 7F, (U)SIM data download, with data coding class 2, identifies data for the card (TS 31.111 §7.1.1.1). The handset treats it as data rather than user text, without storing or displaying it.
- First octet
- 44SMS-DELIVER, no more messages waiting, user data header present
- TP-OA
- +5511900000123the OTA server's address
- TP-PID / TP-DCS
- 7F / F6(U)SIM data download · 8-bit, class 2
- TP-SCTS
- 62 11 03 41 03 50 292026-11-30 14:30:05 UTC-03:00
The handset passes it to the card: ENVELOPE (SMS-PP download)
Handset → SIM cardThe handset wraps the whole SMS-DELIVER in an ENVELOPE command with tag D1, SMS-PP download, with device identities (network to UICC) and the service centre address (TS 31.111 §7.1.1.2). The status word the card answers tells the handset what comes next.
- Command
- 80 C2 00 00 63CLA 80, INS C2 ENVELOPE, Lc
- Tags
- D1 · 82 · 06 · 8BSMS-PP download · device identities · address · SMS TPDU
- Card's answer
- 91 46a proactive command of 70 bytes is waiting
The card checks it: TAR, keys, deciphering, checksum, counter
SIM cardThe card finds the application by its TAR and the keys by the key version in KIc and KID, deciphers from the counter on with its KIc key, computes the CC with its own KID key and compares, and checks the counter. All checks pass in this case. It stores the new counter and runs the commands only after all checks pass (TS 102 225 §5.1.4).
- TAR
- passedB00001 is the card's RFM application of the USIM.
- Keys
- passedKIc 12 and KID 12 name key version 1, which the card holds.
- Deciphering
- passedDeciphered with the card's KIc key (AES CBC).
- Cryptographic checksum
- passedCC matches (AES CMAC). The card computed 98B2C17F6FB5F7A0 with its own KID key.
- Counter
- passed0000000005 is higher than the card's 0000000004. The card stores 0000000005.
The RFM application runs the commands on the file system
SIM card → RFM application + filesThe card hands the secured data to the RFM application of the USIM, which runs the commands one by one from the ADF of the USIM and stops at the first that fails (TS 102 226 §4, §7.3). EF_SPN changes on the card; the phone reads it again after a refresh or a restart.
- 1. 00 A4 00 0C 02…
- 90 00 (OK)SELECT 6F46: EF_SPN is now the current EF.
- 2. 00 D6 00 00 11…
- 90 00 (OK)UPDATE BINARY: 17 bytes written to EF_SPN at offset 0.
The card builds its Proof of Receipt
SIM cardThe PoR (TS 102 225 §5.2) repeats the TAR and the counter, gives a status (00: PoR OK) and, after success, the RFM result: how many commands ran and the last status word (TS 102 226 §5.1.2). It is secured as SPI2 asks: cryptographic checksum (CC), ciphered. The CC also covers the user data header 02 71 00 (TS 31.115 §4.4).
- Status
- 00PoR OK
- Additional data
- 02 90 002 commands run, last status 90 00 (TS 102 226 §5.1.2)
- Response packet
- 00 24 12 B0 00 01 F5 35 4E A0 59 5F 6F 9C 2C 6B EB FE CE A4 DA 98 B9 A7 FA 9B C7 99 36 25 D1 04 9A D8 47 0D 69 4F
The PoR goes back: SEND SHORT MESSAGE
SIM card → HandsetSPI2 b6 asked for an SMS-SUBMIT, so the card answered the ENVELOPE with 91 46: a proactive command is waiting. The handset FETCHes it: SEND SHORT MESSAGE (TS 102 223 §6.4.10) with an SMS-SUBMIT addressed to the OTA server and a null alpha identifier, so nothing shows on the screen. The handset sends it and reports the result with TERMINAL RESPONSE. TS 31.115 §4.1 only asks for 8-bit data; the card chose TP-PID 7F and TP-DCS F6 here, and the address of the incoming message as the destination.
- Card → handset
- 91 46proactive command waiting
- FETCH
- 80 12 00 00 46
- SMS-SUBMIT to
- +5511900000123TP-MR 00: the handset assigns the message reference
Back through the network and the SMSC to the server
Handset → network → SMSC → OTA serverThe SMS-SUBMIT goes from the handset to the SMSC like any mobile-originated SMS (RP-DATA, MAP MO-ForwardSM or its 4G and 5G equivalents). The SMSC delivers it to the OTA server's SMPP connection as a deliver_sm (SMPP 3.4 §4.6.1). Separately, the delivery receipt for the original message confirms only that the handset acknowledged it.
- source_addr
- 5511987654321
- short_message
- 41 octets02 71 00 and the response packet
The server checks the PoR and records the result
OTA serverThe server deciphers the PoR and checks its CC with the same keys, matches TAR and counter to the command it sent, and records the outcome for the campaign. A status other than 00 says why the card refused the packet; with 00, the RFM result says whether the commands themselves worked.
- TAR / CNTR
- B0 00 01 / 00 00 00 00 05match the command
- PoR CC
- verified
- Status
- 00PoR OK
- RFM result
- 2 commands9000 OK
- Next counter for this card
- 00 00 00 00 06the server keeps one counter per card and key set
§ simplifies
What this walkthrough simplifies
- The card is a model: one USIM with an RFM application, one key set, and four files. It checks TAR, keys, ciphering, checksum and counter in that order; TS 102 225 does not fix the order. Access conditions are not modelled: RFM runs with the rights its TAR's keys grant (TS 102 226 §6).
- When the card cannot authenticate the sender (wrong KID key, unknown TAR) it sends no Proof of Receipt, as 3GPP TS 31.115 §4.2 requires over SMS. TS 102 225 §4.1 also allows an unsecured PoR with the counter set to zero; choose it under "All values" to see one with status 01 or 09.
- The SMSC, the core network (MAP, SGs, IMS, 5G SMSF) and the radio are explained with their key fields, not encoded. The SMSC's message ID, the delivery receipt's text and every timing are illustrative.
- The PoR's SMS-SUBMIT uses TP-PID 7F and TP-DCS F6 and goes back to the address the command came from; TS 31.115 only asks for 8-bit data, so both are the card's choice. When the PoR travels in the SMS-DELIVER-REPORT, how the SMSC hands it to the OTA server is SMSC-specific (it is not in SMPP 3.4).
- SCP81 uses TLS 1.2 with TLS_PSK_WITH_AES_128_CBC_SHA256, no extensions besides renegotiation_info, and fixed randoms and IVs so the page shows the same bytes each time; a real card and server use random ones. TLS 1.3 PSK, CAT_TP, the CoAP/DTLS route (SCP82) and the eSIM flows (SGP.02, SGP.22, SGP.32) are not covered.
- The push carries all the session parameters; a real one may leave some out and the Security Domain fills them in from its own settings (Amendment B §3.7). The retry policy values, the Agent ID format and the RFM application's AID are illustrative. The server's fall back to SMS after a failed session is a platform choice, not a standard.
- The TLS module was checked against OpenSSL with a real handshake in both directions, and every route and scenario against an independent Python implementation (the oracle in the site's repository).
The tools behind it
The SMS route uses the same OTA library as the SIM Explorer OTA packets window. Decode the commands and status words with the APDU decoder, the files with the SIM file-system reference, and read why campaigns fail in Debugging SIM OTA failures in SMS-PP and PoR.