MCXN9xx Internal Signed + Encrypted Image (NPX / PRINCE)#

This notebook describes how to set up a signed and encrypted boot image on the internal flash of an MCXN9xx device (e.g. mcxn946/mcxn947) using the on-chip flash encryption engine (NPX, an on-the-fly PRINCE implementation) together with the SPSDK nxpimage and pfr applications.

With NPX, the boot image stored in internal flash is a normal signed Master Boot Image; the flash controller encrypts and decrypts the selected flash sectors transparently based on the CMPA configuration. This keeps the application confidential in flash while the boot ROM still authenticates it against the Root of Trust. No separate “encrypted image” artifact is produced — encryption is a property of the flash region, configured in the CMPA.

The flow is:

  1. establish the Root of Trust keys and a certificate block,

  2. build a signed MBI from a plain application image,

  3. generate the CMPA (with NPX PRINCE context enabled for the application sectors) and CFPA binaries,

  4. write CMPA/CFPA and the signed image to the device; NPX encrypts the flash sectors on the fly.

WARNING: The keys and configuration used in this example are meant only for demonstration purposes and must never be used in production. Enabling NPX and writing/locking the CMPA on a real device can permanently restrict it.

1. Prerequisites#

  • SPSDK is needed with examples extension. pip install spsdk[examples] (Please refer to the installation documentation.)

  • This example uses an MCXN9xx board (e.g. FRDM-MCXN947) connected via USB (ISP mode) or UART.

frdm-mcxn947
# Initialization cell
from spsdk.utils.jupyter_utils import YamlDiffWidget

# This env variable sets colored logger output to STDOUT
%env JUPYTER_SPSDK=1
# Set a magic for command execution and echo
%alias execute echo %l && %l
%alias_magic ! execute
env: JUPYTER_SPSDK=1
Created `%!` as an alias for `%execute`.
FAMILY = "mcxn946"  # change this to your MCXN9xx family (e.g. mcxn947)
WORKSPACE = "workspace/"  # change this to path to your workspace
INPUTS = "inputs/"  # input configurations and binaries
KEYS = "../../_data/keys/ecc256/"  # shared example keys
VERBOSITY = (
    ""  # verbosity of commands, might be -v or -vv for debug or blank for no additional info
)

2. Root of Trust Keys and Certificate Block#

Secure boot is anchored by up to four Root of Trust (RoT) key-pairs whose table hash (RKTH) is programmed into the CMPA. In this example we reuse the shared ECC-256 example key-pairs from the _data/keys/ecc256 folder; in a real scenario you would generate your own keys using the nxpcrypto application (refer to How-to-get-keys-using-nxpcrypto).

The certificate block is provided in cert_block_mcxn9xx.yaml. See the How-to-get-cert-block notebook for details.

3. Generate the signed Master Boot Image#

The image written to internal flash is a normal signed MBI (identical to a plain secure boot image); NPX handles encryption at the flash level. Below we build the signed MBI from the ready-made configuration (mbi_config.yaml) using a plain application image (frdm_mcxn947_led_blinky.bin).

import os

MBI_CONFIG = INPUTS + "mbi_config.yaml"
%! nxpimage $VERBOSITY mbi export -c $MBI_CONFIG
assert os.path.exists(WORKSPACE + "mcxn9xx_signed.bin")
nxpimage  mbi export -c inputs/mbi_config.yaml 
WARNING:spsdk.image.mbi.mbi:MBI-mcxn946-MbiImageTypeEnum.SIGNED_XIP_IMAGE-xip(Warning) 
  ======================================== TrustZone Configuration (Warning) =========================================
  Validates TrustZone settings and configuration type
  ====================================================================================================================
    Feature 'tz' verification(Warning) 
      Implementation(Warning): Not implemented
 (1407ms since start, mbi.py:689)
RKTH: e2cca7cf09a45d2f1942969fda1c68ecaad78fad416d143292dad2f618291ddd
Success. (Master Boot Image: workspace/mcxn9xx_signed.bin created.)

4. Generate CMPA (with NPX enabled) and CFPA#

NPX (on-chip flash PRINCE) is enabled purely through the CMPA. Each of the four NPX contexts owns a 64-bit sector bitmap (NPX_CTXn_WD0/WD1); a selected sector is encrypted on the fly. In this example context 0 covers the first 16 flash sectors (NPX_CTX0_WD0 = 0x0000FFFF), which holds the application at address 0x0. The diff below highlights the change against the default CMPA template.

# Get difference of template and user CMPA configuration
YamlDiffWidget("inputs/mcxn9xx_encrypted_internal.diffc").html
pfr get-template -f mcxn946 -t cmpa -o workspace/cmpa_template.yaml --force 
The PFR cmpa template for mcxn946, Revision: latest has been saved into workspace/cmpa_template.yaml YAML file

Configuration Differences

import os

CMPA_CONFIG = INPUTS + "cmpa_mcxn9xx.yaml"
CFPA_CONFIG = INPUTS + "cfpa_mcxn9xx.yaml"
CERT_BLOCK_CONFIG = INPUTS + "cert_block_mcxn9xx.yaml"
CMPA_BIN = WORKSPACE + "cmpa.bin"
CFPA_BIN = WORKSPACE + "cfpa.bin"

%! pfr $VERBOSITY export -c $CMPA_CONFIG -e $CERT_BLOCK_CONFIG -o $CMPA_BIN
%! pfr $VERBOSITY export -c $CFPA_CONFIG -o $CFPA_BIN
assert os.path.exists(CMPA_BIN)
assert os.path.exists(CFPA_BIN)
pfr  export -c inputs/cmpa_mcxn9xx.yaml -e inputs/cert_block_mcxn9xx.yaml -o workspace/cmpa.bin 
Success. (CMPA binary has been generated and stored into workspace/cmpa.bin)
pfr  export -c inputs/cfpa_mcxn9xx.yaml -o workspace/cfpa.bin 
Success. (CFPA binary has been generated and stored into workspace/cfpa.bin)

5. Device preparation#

Put the board into In-System Programming (ISP) mode and use nxpdevscan to check that the device is detected by the PC.

# check if the device is connected and detected by the PC
%! nxpdevscan
nxpdevscan 
Scanning USB devices...
╔══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╗
║                                        Connected NXP USB Devices  (1 device)                                         ║
╠══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╣
║  VID       : 0x1fc9                                                                                                  ║
║  PID       : 0x014f                                                                                                  ║
║  Product   : USB COMPOSITE DEVICE                                                                                    ║
║  Name      : kw43b43zc7 | mcxn547 | mcxn537 | mcxn556s | mcxn246t | mcxn247t | mcxw706c | mcxw706a | mcxn246 | kw43b4║
║  Path      : HID\VID_1FC9&PID_014F\6&28545052&0&0000                                                                 ║
║  Path Hash : c6b1ac99                                                                                                ║
║  Serial    : N/A                                                                                                     ║
╚══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╝

Scanning UART devices...  Nothing found
Scanning LPCUSBSIO devices...  Nothing found
Scanning UUU devices...  Nothing found
# Select the interface to the target. USB (default MCXN9xx ISP VID:PID) or UART.
USB = "-u 0x1fc9,0x014f"  # change to your device, or use e.g. "-p com10" for UART

6. Write CMPA/CFPA and the signed image to the device#

Write the CFPA and CMPA (which enables NPX), erase the internal flash, and program the signed image. As the image is written, NPX encrypts the selected flash sectors on the fly. After reset the boot ROM authenticates the (transparently decrypted) image and boots it.

SIGNED_IMAGE = WORKSPACE + "mcxn9xx_signed.bin"
%! pfr $VERBOSITY write $USB -t cfpa -f $FAMILY -bin $CFPA_BIN
%! pfr $VERBOSITY write $USB -t cmpa -f $FAMILY -bin $CMPA_BIN
%! blhost $USB flash-erase-all
%! blhost $USB write-memory 0x0 $SIGNED_IMAGE
%! blhost $USB reset
pfr  write -u 0x1fc9,0x014f -t cfpa -f mcxn946 -bin workspace/cfpa.bin 
CFPA data written to device.
pfr  write -u 0x1fc9,0x014f -t cmpa -f mcxn946 -bin workspace/cmpa.bin 
CMPA data written to device.
blhost -u 0x1fc9,0x014f flash-erase-all 
Response status = 0 (0x0) Success.
blhost -u 0x1fc9,0x014f write-memory 0x0 workspace/mcxn9xx_signed.bin 
Response status = 0 (0x0) Success.
Response word 1 = 4368 (0x1110)
blhost -u 0x1fc9,0x014f reset 
Response status = 0 (0x0) Success.