Free Ebook cover Edge AI in Practice: Building Privacy-Preserving, Low-Latency Intelligence on Devices

Edge AI in Practice: Building Privacy-Preserving, Low-Latency Intelligence on Devices

New course

16 pages

Secure Model Packaging, Delivery, and On-Device Updates

Capítulo 10

Estimated reading time: 0 minutes

+ Exercise

Why secure packaging and updates matter for Edge AI

Edge AI models are valuable assets: they encode proprietary training data, engineering effort, and sometimes sensitive behavior (for example, fraud detection thresholds or safety rules). Once a model is shipped to devices, it becomes part of the application’s attack surface. A weak delivery pipeline can allow model theft, tampering (changing outputs), rollback to vulnerable versions, or denial of service by pushing malformed artifacts. Secure model packaging, delivery, and on-device updates aim to guarantee three properties: authenticity (the model really comes from you), integrity (it was not modified), and freshness (the device is not tricked into using an old, vulnerable model). In practice, you implement these properties with signed artifacts, verified metadata, controlled rollout, and robust update logic that can recover from failures.

Threat model: what can go wrong

Before designing mechanisms, enumerate realistic threats. Common ones include: (1) network attacker intercepts and replaces a model download; (2) compromised CDN or mirror serves a modified model; (3) attacker with filesystem access replaces the on-device model file; (4) attacker forces a rollback to an older model that has known weaknesses; (5) attacker extracts the model to clone your IP; (6) attacker triggers repeated downloads to drain battery or data; (7) update process crashes mid-way and bricks inference; (8) model metadata is manipulated to bypass compatibility checks. Your defenses should assume the device may be in hostile hands and that the network is untrusted, while still keeping updates reliable and bandwidth-efficient.

What “secure model packaging” means

Secure packaging is the process of turning a trained model into an installable artifact that includes: the model weights, a manifest describing what it is, cryptographic signatures, and sometimes encryption. The key idea is that the device should never “trust” a model file just because it exists locally. It should trust only artifacts that pass verification against a public key embedded in the app or provisioned in secure hardware. Packaging also includes compatibility constraints (supported runtime, expected input/output schema, required pre/post-processing parameters) so that the device can reject artifacts that would crash or silently misbehave.

Recommended artifact structure

A practical and auditable structure is a bundle with a manifest and one or more payload files. For example:

  • manifest.json: model id, semantic version, build timestamp, target platforms, required runtime version, input/output schema hash, preprocessing parameters, quantization info (if relevant), and a list of payload files with their SHA-256 hashes and sizes.
  • model.bin (or runtime-specific format): the actual weights/graph.
  • labels.txt or auxiliary resources: class labels, tokenizers, feature configs.
  • signature.sig: signature over the canonical manifest (or over a digest of the entire bundle).
The manifest is the “source of truth” for verification and compatibility checks. The device verifies the signature first, then validates hashes of each payload file, then checks compatibility and policy (for example, minimum allowed version).

Signing: the backbone of authenticity and integrity

Digital signatures let devices verify that an artifact was produced by your release process. Use asymmetric cryptography: the signing private key stays in your build/release environment, and the public key is embedded in the app (or stored in a hardware-backed keystore). A typical choice is Ed25519 for simplicity and speed, or ECDSA P-256 if you need compatibility with existing tooling. Avoid “rolling your own” crypto; use well-reviewed libraries and standard formats.

Continue in our app.

You can listen to the audiobook with the screen off, receive a free certificate for this course, and also have access to 5,000 other free online courses.

Or continue reading below...
Download App

Download the app

Step-by-step: signing a model bundle

1) Canonicalize the manifest: ensure stable JSON serialization (sorted keys, no whitespace differences) or use a binary format with deterministic encoding. 2) Compute payload hashes: SHA-256 for each file; store hash and size in the manifest. 3) Compute manifest digest: hash the canonical manifest bytes. 4) Sign the digest: produce signature.sig using the private key. 5) Attach key id: include a key identifier in the manifest so devices know which public key to use (useful for key rotation). 6) Verify in CI: as a release gate, run an independent verification step that uses only the public key to validate the signature and payload hashes.

On-device verification flow

On the device, verification should be strict and fail-closed. The flow is: read manifest, verify signature with embedded public key, verify each payload hash and size, then enforce policy checks (minimum version, device compatibility, required runtime version). Only after all checks pass should the artifact be eligible for activation. Treat any parsing errors, missing fields, or hash mismatches as a hard failure.

Encryption: when and why to use it

Signatures prevent tampering but do not prevent copying. If model confidentiality matters (for example, you want to slow down casual extraction), you can encrypt the payload. Encryption is not a silver bullet on consumer devices because a determined attacker may still extract keys from memory at runtime, but it raises the bar and can be effective when combined with hardware-backed key storage.

Practical encryption approach

Encrypt only the payload files (model.bin, tokenizer) and keep the manifest in plaintext so the device can decide whether it should download and attempt decryption. Use an authenticated encryption mode such as AES-GCM or ChaCha20-Poly1305. The device obtains a decryption key via one of these patterns:

  • Per-app key in secure hardware: provision a key into Secure Enclave/TEE/KeyStore; decrypt locally.
  • Per-device wrapped key: server encrypts (wraps) a content key for each device using a device public key; device unwraps in hardware.
  • Per-session key via attestation: device proves integrity (attestation), server releases a short-lived key.
Even with encryption, keep signature verification: you still need authenticity and integrity, and authenticated encryption alone does not prove who produced the ciphertext.

Metadata and compatibility: preventing silent breakage

Many update failures are not malicious; they are mismatches. A secure update system should prevent installing a model that is incompatible with the app’s preprocessing, expected tensor shapes, or postprocessing thresholds. Put explicit compatibility fields in the manifest:

  • model_id and variant (for example, “object_detector_v3”, “lite”, “high_accuracy”).
  • semantic_version and min_app_version.
  • input_schema_hash and output_schema_hash computed from a canonical schema definition.
  • preprocess_config_hash for normalization, resizing, tokenization settings.
  • required_runtime and min_runtime_version.
The device should reject artifacts that do not match the expected schema hashes. This avoids subtle bugs where inference runs but outputs are meaningless because preprocessing changed.

Delivery: secure transport plus secure content

Use HTTPS/TLS for transport, but do not rely on TLS alone. TLS protects against many network attacks, yet it does not protect you if a server is compromised or if a device is tricked into trusting a malicious certificate. Content signing provides end-to-end security: even if the transport is compromised, the device rejects unsigned or altered artifacts.

Practical delivery architecture

A common pattern is:

  • Update metadata endpoint: returns the latest eligible version for a device, plus URLs and hashes. This response should itself be signed (or delivered from a trusted channel) to prevent downgrade and targeting attacks.
  • Artifact storage: CDN/object storage hosting the bundle files; immutable URLs with content hashes in the path are helpful.
  • Policy service: decides eligibility based on device model, region, app version, and rollout cohort.
Separate “what to install” (metadata) from “download bytes” (CDN). Sign the metadata response or embed a signed “update manifest” that points to the artifact bundle.

Freshness and anti-rollback protections

Anti-rollback ensures an attacker cannot force a device to install an older model that is known to be weak or exploitable. Implement freshness with a combination of version rules and trusted state.

Techniques for anti-rollback

  • Minimum version policy: ship a “minimum allowed model version” with the app, and update it via app updates. The device refuses anything below that version.
  • Monotonic counters: store the highest installed version in a tamper-resistant location (hardware-backed storage if available). Refuse installs with a lower version.
  • Signed timestamp/epoch: include a signed build timestamp or epoch in the manifest; device enforces monotonic increase. This is weaker if the attacker can reset device time, so prefer counters.
  • Server-side gating: the metadata service never offers older versions, but do not rely on this alone because the attacker may bypass the service.

On-device update lifecycle: download, verify, stage, activate

Robust updates use a staged approach so that a failed download or verification never breaks the currently working model. Think of it as an A/B system for models: one active slot and one staging slot.

Step-by-step: a safe update algorithm

1) Check eligibility: device requests update metadata; applies local policy (battery level, network type, storage availability, user settings). 2) Download to staging: write to a temporary directory; use resumable downloads and verify size constraints to prevent storage exhaustion. 3) Verify signature and hashes: verify manifest signature, then payload hashes. 4) Compatibility checks: validate schema hashes, required runtime, min app version. 5) Optional decrypt: decrypt payload into a protected staging location; verify authenticated encryption tags. 6) Atomic activation: switch a pointer (for example, a symlink or a small “active_model.json” file) to the new version in a single atomic filesystem operation. 7) Health check: run a lightweight self-test inference on known test vectors or sanity checks (output ranges, non-NaN). 8) Rollback on failure: if health check fails, revert pointer to previous model and mark the new version as bad to avoid repeated attempts.

Atomicity patterns

Use atomic rename operations where possible: write a new pointer file, fsync, then rename over the old pointer. Avoid partially written model files being treated as valid. Keep the previous model until the new one is proven healthy, and implement a cleanup policy that retains N previous versions to allow safe rollback during incidents.

Delta updates and bandwidth control

Models can be large, and frequent updates can be expensive. Delta updates send only the difference between versions. However, deltas complicate security because you must ensure the reconstructed model is exactly what was signed.

Secure delta update strategy

Prefer this approach: sign the final reconstructed artifact (or its manifest hash), not just the patch. The device applies the patch in staging, reconstructs the new payload, then verifies the payload hash against the signed manifest. If the hash matches, activation proceeds. If not, discard and optionally fall back to full download. Also enforce limits: maximum patch size ratio, maximum CPU time for patching, and only allow deltas from specific base versions listed in the manifest.

Key management and rotation

Signing keys are high-value. Protect them with hardware security modules (HSM) or cloud KMS with strict access controls. Plan for rotation: you may need to replace a key if it is compromised or if you want to change algorithms.

Practical rotation design

Embed multiple trusted public keys (or a small certificate chain) in the app. Each artifact includes a key id. Devices accept signatures from any currently trusted key. To rotate: start signing new artifacts with the new key while still trusting the old key; ship an app update that removes trust in the old key after a transition period. If you need emergency revocation, maintain a signed “revocation list” or “minimum key epoch” in update metadata that the device enforces.

Operational rollout: cohorts, canaries, and safety valves

Security is not only cryptography; it is also operational control. A bad model can cause harm even if it is correctly signed. Use staged rollouts: canary to a small cohort, then gradual expansion. The update metadata service can assign cohorts deterministically (for example, hash of device id) so devices stay in the same cohort across sessions.

Safety valves

Include mechanisms to stop or slow updates:

  • Kill switch: signed metadata can mark a version as revoked so devices stop activating it and roll back.
  • Rate limits: prevent repeated download attempts; exponential backoff on failures.
  • Constraints: only update on Wi‑Fi, only when charging, or only above a battery threshold.
Ensure the kill switch is itself authenticated (signed) to prevent attackers from disabling models.

Telemetry for update integrity (without leaking user data)

To operate updates safely, you need feedback: success rates, verification failures, crash rates after activation, and performance regressions. Keep telemetry minimal and privacy-preserving: report only model version, update state transitions (downloaded, verified, activated, rolled back), error codes, and coarse device characteristics (OS version, device class). Avoid sending raw inputs or inference outputs. Use sampling and batching to reduce overhead.

Useful on-device logs and error codes

Define structured error codes for: signature invalid, hash mismatch, incompatible schema, insufficient storage, decrypt failure, health check failure, rollback triggered, metadata signature invalid, and network errors. These codes help distinguish attacks from ordinary failures and speed up incident response.

Reference pseudocode: verifier and activator

// High-level pseudocode (language-agnostic) for secure model update apply() function
function applyUpdate(bundlePath):
  manifestBytes = readFile(bundlePath + "/manifest.json")
  sig = readFile(bundlePath + "/signature.sig")

  if !verifySignature(publicKeys, manifestBytes, sig):
    return ERROR_SIGNATURE_INVALID

  manifest = parseJson(manifestBytes)
  if !policyAllows(manifest):
    return ERROR_POLICY_REJECTED

  for each fileEntry in manifest.files:
    bytes = readFile(bundlePath + "/" + fileEntry.name)
    if sha256(bytes) != fileEntry.sha256 or size(bytes) != fileEntry.size:
      return ERROR_HASH_MISMATCH

  if manifest.encrypted:
    if !decryptPayloads(bundlePath, keyStore):
      return ERROR_DECRYPT_FAILED

  if !runHealthCheck(bundlePath, manifest):
    markBadVersion(manifest.version)
    return ERROR_HEALTHCHECK_FAILED

  atomicSetActiveModel(bundlePath, manifest.version)
  recordInstalledVersionMonotonic(manifest.version)
  return OK

Testing the security properties

Build tests that simulate real attacks and failures. For authenticity/integrity: modify one byte in model.bin and ensure verification fails; replace manifest fields and ensure signature fails; attempt to install an unsigned bundle and ensure rejection. For anti-rollback: attempt to install version N-1 after N is active and ensure rejection. For reliability: kill the app mid-download and ensure the active model remains usable; corrupt staging files and ensure cleanup; simulate low storage and ensure graceful failure. For operational controls: verify cohort assignment stability and that kill switch revokes a version even if it is already downloaded but not activated.

Now answer the exercise about the content:

Which on-device verification order best ensures a model update is authentic and unmodified before it can be activated?

You are right! Congratulations, now go to the next page

You missed! Try again.

A strict flow is to verify the signature on the manifest first, then check each payload hash/size, then apply compatibility and version policies. Only after all checks pass should activation occur.

Next chapter

Edge Monitoring, Drift Detection, and Field Debugging

Arrow Right Icon
Download the app to earn free Certification and listen to the courses in the background, even with the screen off.