Skip to content

Crypto and transport

Bench hardware only. Everything here can start or change insulin delivery. Use it only with pump hardware that is not connected to any living subject. See bench testing.

3. App-layer crypto

Link encryption alone is not enough. Every command frame is sealed with keys derived from a per-pump registration secret (LTSS) and the session material above:

appKey    = HKDF-SHA256(LTSS, salt = e5332024, info = e15e86a87e870407d581, L = 16)
risSigKey = HKDF-SHA256(LTSS, salt = e5332023[0:16], info = f093dbc006c3aa0b3eac, L = 16)

wire  = LE64(packet) || AES-CCM(appKey, nonce = LE64(packet), aad = LE64(packet))(frame)
frame = len(4 LE) || seq(4 LE) || opcode(2 BE) || payload || auth(8) || crc16(2 LE)
auth  = HMAC-SHA256(risSigKey, frame[0 : len-10])[0:8]
crc16 = CRC-16/MCRF4XX over frame[0 : len-2]   (little-endian on the wire)
  • LTSS is a 16-byte (sometimes observed as 32-byte) per-registration secret established when the pump is registered with its official app / cloud. TwiistOff does not obtain it for you. You enter your own pump's value on the Pump (Dev) screen; it stays on the phone and is never logged. How you obtain it for a unit you own is outside the scope of this document.
  • HKDF is RFC 5869 with SHA-256. AES-CCM is RFC 3610 (tag 16 bytes).
  • The crypto unit tests (app/src/test/.../TwiistCryptoTest.kt) use synthetic keying material and round-trip (derive → seal → open); no real pump key is in the repo. The HKDF info constants and the frame layout are pinned.

Transport numbering

Two counters, both required, with different meanings:

Counter Where Rule
packet 8-byte LE prefix of every sealed wire blob Shared by both directions. Fresh connection starts at 0 (pump) / 1 (app). Each frame either way advances it. The pump replies with the same packet number as the request.
seq 4-byte LE field inside the frame For app messages: starts at the pump's advertised start sequence (e5332023) and increments by 1 per app message. The pump echoes it in responses.

A stale packet number is silently ignored. Malformed frames can drop the link (the pump then re-advertises and a bonded peer reconnects).

4. Simple vs Therapy commands

There are two command families. They differ in exactly one way:

Family Examples Payload prefix
Simple getPumpIdentification 0x0310, getLogRecord 0x0502, verifyTherapySettings 0x800F, clearPumpAlert 0x0400 None — fields only
Therapy settings writes (setBasalPattern 0x8019, …), delivery (startBasal 0x8100, bolus 0x8400, …) 2-byte therapy number (little-endian), then fields
  • The next therapy number lives in pumpStatus payload bytes 12–13.
  • A Therapy command's response is typically echo(2) || nextTherapyNumber(2). When the command was accepted, that tail is the correct next number (measured 2026-12-02 through resetPrime, fill, prime, primeComplete, startBasal and alarmReset: each tail was accepted by the next command and matched the next status bytes 12–13).
  • Exception — bolus: the 0x8400 response is consumed(2) || next(2) || bolusId(2); the next number is bytes 2–3, not the last two bytes (those are the bolus id).
  • Every refused (NACKed) therapy-number-bearing command rotates the expected number. Take the next one from the NACK (bytes 4–5, below), not from the last accepted response or an older status.
  • Only these carry the next number: status bytes 12–13, the reply to a therapy-numbered command (bytes 2–3), and a NACK (bytes 4–5). A Simple command's reply never does: its bytes 2–3 are its own fields (a getLogRecord reply's record data). Taking them as the next number gets the next therapy command refused with 0x13F4 NackRejectTherapyNumber.
  • Which messages carry the prefix follows the message class (therapy vs simple command id), not the 0x8xxx/0x0xxx split: alarmReset 0x0D00, silenceAlarms 0x0700 and clearPumpNotice 0x0402 are therapy-numbered although they are 0x0xxx.
  • Sending a Simple command through the "prepend the token" path corrupts it. verifyTherapySettings sent as token || record (44 bytes) is rejected; the bare 42-byte record is accepted.

Responses use opcode + 0x80. NACKs arrive as opcode 0xFF80 with a 26-byte payload: risCommand(1) | risSubcommand(1) | nackCode(2 LE) | nextValidTherapyNumber(2) | debugValue1..4 (4 × u32) | fileNumber(2) | lineNumber(2). Known NACK codes are in protocol/ProtocolStatus.kt (NACK_CODES_BY_RAW, NACK_CASE_BY_RAW).

Reply matching (✅ implemented, 2026-12-02). A response echoes the request's seq; so does a NACK. A command must accept only a frame that is opcode == request | 0x80 with the request seq, or a 0xFF80 NACK that names the request opcode with the request seq. Anything else (a late answer to an earlier command, a stray frame) must be dropped, not handed to the next command. TwiistOff does this in pump/ReplyRouter.kt and serialises commands with a mutex; on the bench 95/95 replies matched.

The two useful fields for recovery:

  • nextValidTherapyNumber (bytes 4–5) is the token the pump will accept next. Re-seed the chain from it on a NACK; it is not at bytes 2–3 (those are the NACK code). Reading the wrong pair poisoned the chain and made the next command fail with 0x13F4 (measured 2026-11-30). Every refused therapy-number-bearing command rotates it (measured 2026-12-02).
  • Never resend a therapy command automatically — not after a NACK, and above all not after a timeout: a lost response does not mean the command was not executed, and a resend carries a fresh valid number, so a bolus could run twice. TwiistOff sends once and reports NACKED / OUTCOME_UNKNOWN (pump/TherapyCommand.kt).
  • 0x13ED (app case NackBadSequence) means "not valid in this state / at this step" in every measured case (unconfigured delivery, a second temp basal, a fill in cassette-change mode before the reboot, alarmReset with no alarm) — fix the state, do not re-sync.
  • alarmReset 0x0D00 carries the therapy number (empty → 0x1403 invalid length) and is accepted only while pumpStatus byte 90 alarmResetSupported = 1; its effect is a pump reboot (alarm cleared, resetCount +1, status seq restarts). After any reboot the old session keeps decrypting status but no longer answers commands until it is re-armed on the same link (§2.1).
  • 0x13F7 (NackInFailSafeMode) — seen on startBasal after a cassette was removed while delivering and put back, even with verify answering 01. Never retry; only a reboot clears it (§6.2).
  • 0x13FB (NackRISInvalidCmdOrSub) — seen for 0x0E00 (acknowledge notification audio). The pump refuses 0x0E00 (0x13FB); treat 0x0E00–0x0E02 as unsupported.
  • 0x1403 (NackInvalidMsgLength) — wrong payload length; the usual tell that a therapy-numbered command was sent without its number (empty 0x0D00 / 0x0700).
  • The debug values carry the values that failed a range check. For 0x13EE (NackRangeCheckFailed): debugValue1 = the offending value, debugValue3 = the limit it was compared against (live: 2560 vs 600 on a bolus, i.e. 25.60 U against the 6.00 U max bolus). 0x13EE is not the delivery gate — it still fires with verifyTherapySettings returning 01. The refusal site is fingerprinted by fileNumber/lineNumber (0x122A/0x1026 for that check on this firmware; the empty-payload length check was 0x0D11/0x0451).