How to start with CCB Key-Wrapped ECDSA Signature on STM32H5F5J

1. Short description

This wiki explains how the CCB mechanism securely wraps an ECDSA private key, stores it as a protected blob, and later reuses it for signing and verification on the STM32H5F5J.

2. Literature

3. Introduction

This document explains how to get started with CCB on the STM32H5F5J using an example of key-wrapped ECDSA signatures. The workflow shows how a private key provided by the CPU is imported through PSA Crypto, converted into a protected blob, stored securely, and later reused for ECDSA signing and verification.

  • CCB: Coupling and Chaining Bridge
  • CCOP: Coupling and Chaining Operation
  • OPSTEP: Operation Step
  • PSA: Platform Security Architecture
  • SAES: Secure AES Engine
  • PKA: Public Key Accelerator
  • RNG: Random Number Generator

4. Overview of the Hardware and Software Environment

4.1. Hardware

Board: STM32H5F5J-DK (MB2114)

Figure 1: STM32H5F5J-DK Top view

The example requires a device supporting hardware cryptographic accelerators: CCB, PKA, SAES, and RNG

4.2. Software

  • Firmware: STM32Cube_FW_H5_V1.6.0 or later
  • IDE: IAR Embedded Workbench for Arm v9.20.1 or later, with the STM32H5Ex-Fx patch. The patch is available in STM32Cube_FW_H5_Vx.x.x\Utilities\PC_Software\IDEs_Patches. This getting-started guide uses IAR, but other IDEs are also supported.
  • Debug probe/setup: As required by the board.
  • Recommendation: Keep the STM32Cube firmware directory close to C:\ to avoid Windows path-length issues.

4.3. Environment setup

  1. Download and install the STM32CubeH5 package.
  2. Open the following project directory:STM32Cube_FW_H5_V1.6.0\Projects\STM32H5F5J-DK\Applications\MbedTLS_HW_KWE
  3. Select the ECC_ECDSA_Sign_KWE example.

5. Setup of the ECC_ECDSA_Sign_KWE application example

  1. Open the ECC_ECDSA_Sign_KWE example.
  2. Verify the device selection in the IDE (as shown in the figure2).
    Figure 2: Project option- Device selection
  3. Set the compilation switch for the target device.
    Figure 3: Project option - Compiler setup
  4. Set optimization to none to allow reliable single stepping
    Figure 4: project option - Compiler optimization
  5. Set the ST-LINK debug settings according to the following figures, if applicable.
    Figure 5: Project option- STlink settings
  6. Compile the project.
  7. Connect the board and start the debug session.

6. Example overview

This example performs a digital signature using a wrapped (encrypted) private key. It demonstrates a secure signing workflow in which the private key is not exposed as plaintext after import. The CPU provides the key material to the PSA layer, which wraps it into a blob. The blob is then retrieved and used for ECDSA signing.

6.1. Main objectives

  • Import a private key provided by the CPU.
  • Wrap the key in a protected blob.
  • Store the blob securely in non-volatile memory.
  • Use the blob to generate an ECDSA signature.
  • Verify the signature.

6.2. High-level architecture

The example relies on the following functional blocks:

  • CPU: Provides the plaintext key material.
  • PSA Crypto: Manages key objects and cryptographic operations.
  • CCB: Controls the secure execution flow.
  • SAES: Supports secure chaining and GCM-based protection.
  • PKA: Performs the ECDSA computation.
  • RNG: Generates the random nonce required for ECDSA.
  • Non-volatile memory: Stores the wrapped blob for later reuse.

6.3. Simplified data flow

  • The CPU provides the key material.
  • psa_import_key() creates a wrapped blob.
  • The blob is stored securely.
  • The message is hashed.
  • The blob is used to generate an ECDSA signature

6.4. Expected result

The example is successful if:

  • The blob is created correctly.
  • The message is signed successfully.
  • The generated signature is verified successfully.
  • The protected execution sequence completes without error.

7. Execution of ECDSA application example

The following section describes the complete workflow and the steps required to create, securely store, and later reuse the wrapped ECDSA key blob. It then presents the main.c file and explains its contents in detail.

7.1. Private and public keys declaration

Figure 6: Private and public key declaration in main.c file

Private key

The private key is defined in main.c for demonstration purposes only. In a real product, the private key should not be stored in plaintext in flash or duplicated in RAM.

This example demonstrates how to import the key once, wrap it into a protected blob, and then erase the plaintext key material.

Public key declaration

The public key is also defined as a constant in main.c. It contains the X and Y coordinates of the elliptic-curve public point.

The first byte, 0x04, indicates that the public key is an uncompressed point containing the X and Y coordinates on a Weierstrass elliptic curve.

The public key is later used to verify the generated ECDSA signature.

7.2. Blob creation and persistence

This step creates a protected ECDSA key blob from the plaintext private key provided by the CPU. The blob can be stored persistently and reused for future signing operations while keeping the secret key material protected.

In this example, the plaintext private key is used only to demonstrate the wrapping process. The recommended end state is to retain the wrapped blob as the long-term protected asset and erase the plaintext key material after wrapping.

The following sections explain how this process is performed in the example.

7.3. CPU key provisioning

The process begins when the CPU provides the private key material to the PSA API. In this example, the private key is provided in plaintext for demonstration purposes only. This approach is not recommended for production use.

7.3.1. Key handle and attributes

After HAL initialization, clock configuration, and BSP setup, the ECDSA configuration is defined in main.c using the key_attributes structure. This structure specifies the key’s cryptographic policy, usage constraints, lifetime, and location.

In this example, the attributes authorize hash signing, select ECDSA with SHA-224, assign the key to the STM32 Key Wrap Engine, and set a persistent key identifier for later reuse.

7.3.2. Key policy

  • PSA_KEY_USAGE_SIGN_HASH authorizes the key to sign hashes.
  • PSA_ALG_ECDSA(PSA_ALG_SHA_224) selects ECDSA with SHA-224 as the hash algorithm.
  • PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1) defines an elliptic-curve key pair using the secp_r1 curve family.
  • The 256-bit private key corresponds to the secp256r1 curve.
  • PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(PSA_KEY_PERSISTENCE_DEFAULT, PSA_CRYPTO_KWE_DRIVER_LOCATION) configures the key as persistent and assigns it to the STM32 Key Wrap Engine location.
  • PSA_ECC_KEY_ID_USER specifies the persistent key identifier.

This configuration prepares the private key for secure import, wrapping, storage, and subsequent ECDSA signing.

Figure 7: Private Key policy

When the private key is imported, key_handle_private stores the handle returned by PSA Crypto. The variable is stored in SRAM.

In the debugger, key_handle_private is located at 0x2000A438 in this example. This address may vary between runs. To find the address in your run, add key_handle_private to the watch list and inspect its address in the debugger.

7.4. Blob generation with psa_import_key().

psa_import_key() imports the plaintext private key and creates a protected blob. The wrapping process protects the key when it is stored outside the application domain. The original plaintext key remains in the input buffer until the application securely erases it.

During key import, psa_import_key() initiates the secure blob creation sequence. In this example, CCOP = 0xC0 corresponds to creating an ECDSA signing-key blob from the private key provided by the CPU.

Note
After the import operation, the key_attributes structure is no longer required and can be cleared.

7.4.1. Recommended breakpoint strategy

  1. Open \STM32Cube_FW_H5_V1.6.0\Drivers\STM32H5xx_HAL_Driver\Src\stm32h5xx_hal_ccb.c.
  2. Find the CCB_ECDSA_SignBlobCreation() function to observe the progress of the internal secure sequence.
  3. For a clean debugging session, set breakpoints at the locations listed in Table 1. These breakpoints correspond to the following stages:
    • Start of secure initialization
    • After IV generation
    • After loading the ECDSA parameters
    • After writing the private key, d
    • Start of the PKA-to-SAES transfer
    • GCM finalization and tag generation
    • Cleanup and reset

7.4.2. Debug  Procedure

  • Use OPSTEP to identify the current execution stage.
    • Monitor the RNG during IV generation.
    • Monitor the PKA during ECDSA parameter loading and private-key handling.
    • Monitor the SAES during GCM setup, encrypted data transfer, and tag generation. Open the Registers 1, Registers 2, Registers 3, and Registers 4 windows. In these windows, display the CCB, SAES, PKA, and RNG register groups. Monitor the CCB registers to observe the CCOP bits and the OPSTEP field. Pay particular attention to the PKA and SAES registers, which reflect the progress of the secure processing sequence. Run the program until it reaches the first breakpoint. Then continue execution from breakpoint to breakpoint, following the steps in Table 1. Table 1 summarizes the secure sequence in execution order, linking each breakpoint to the corresponding hardware action. The sequence can be tracked through the OPSTEP bits in the CCB_SR register. The table also indicates the execution path and whether the operation uses coupling or chaining. For this secure operation, CCOP is set to 0xC0<.

.

Table 1: OPSTEP sequence for blob creation
OPSTEP Breakpoint in CCB_ECDSA_SignBlobCreation() function Main action PATH Coupling / chaining
0x1 CCB_WaitOperStep(hccb, 0x01, HAL_CCB_TIMEOUT_DEFAULT_VALUE) Initialize the secure hardware context Setup None
0x2 CCB_WaitOperStep(hccb, 0x02, HAL_CCB_TIMEOUT_DEFAULT_VALUE) Generate IV through RNG and prepare GCM in SAES RNG → SAES Chaining
0x3 CCB_WaitOperStep(hccb, 0x03, HAL_CCB_TIMEOUT_DEFAULT_VALUE) Load ECDSA parameters into PKA and SAES CPU → PKA + SAES Coupling
0x4 CCB_WaitOperStep(hccb, 0x04, HAL_CCB_TIMEOUT_DEFAULT_VALUE) Write private key `d` into protected PKA RAM CPU → PKA Coupling
0x8 CCB_WaitOperStep(hccb, 0x08, HAL_CCB_TIMEOUT_DEFAULT_VALUE) Transfer and encrypt private key through SAES PKA → SAES Chaining
0xA CCB_WaitOperStep(hccb, 0x0A, HAL_CCB_TIMEOUT_DEFAULT_VALUE) Finalize GCM and generate the reference tag SAES → CCB Chaining
0x0 CLEAR_BIT(hccb->Instance->CR, CCB_CR_IPRST); hccb->State = HAL_CCB_STATE_READY; Reset and end the sequence Cleanup None

During ECDSA blob creation, Figures 6a and 6b illustrate the protected sequence executed by the CCB and the corresponding PKA, SAES, and RNG register states.

Figure 6a shows the CCB register group. CCB_CR should indicate CCOP = 0xC0, and CCB_SR should be monitored to track the current OPSTEP value. This is the main register to follow during the secure sequence: each OPSTEP corresponds to a specific hardware action, and progress through the protected flow is reflected in OPSTEP[4:0].

For example, when OPSTEP = 0x2, the RNG is expected to generate the IV material for AES-GCM initialization. At this stage, the RNG registers should indicate that the random number generator is enabled and ready: RNG_CR.RNGEN = 1, and RNG_SR.DRDY = 1 when random data are available.

At the same time, the SAES registers should indicate that GCM initialization is in progress. SAES_CR should be configured for AES-GCM encryption with a 256-bit key, and GCMPH = 0x1 after the initial setup. The IV registers SAES_IVR1 to SAES_IVR3 are then loaded with random values provided through the CCB/RNG path, while SAES_IVR0 remains fixed as required by the sequence. CCB_SR should show CCB_BUSY = 1 and OPSTEP = 0x2, confirming that the protected operation is still running.

Figure 6b shows the PKA, SAES, and RNG register groups to monitor during blob creation. The PKA registers confirm that the protected operation is configured in the expected mode, with PKA_CR.MODE = 0x24 for ECDSA signature blob creation. PKA_SR indicates that the accelerator is ready and progressing normally.

Check the SAES registers to verify the chaining configuration and track the GCM phase. Monitor the RNG registers to confirm that the random values required by the blob creation sequence are being generated.

Figure 8-a: Example of CCB registers values during blob creation - OPSTEP= 0x2
Figure 8-b: SAES, PKA and RNG registers values during OPSTEP= 0x2

7.5. Blob protection and persistence

Once created, the blob can be stored in non-volatile memory. In this form, the key remains protected and can later be retrieved for secure use.

What to observe

At each step, verify:

  • The key_attributes content,
  • The key_handle_private value
  • The ecdsa_blob layout,
  • The temporary key copy in RAM,
  • The stored wrapped key in the Flash.

Rerun the code to observe in detail the needed expressions:

  • Open main.c file :
    • Put a breakpoint @ retval = psa_import_key(&key_attributes, Private_Key, sizeof(Private_Key), &key_handle_private);
  • Add to watch the following expression to observe their location
    • Key_attributes
    • Private_Key and
    • Key_handle_private

The figure below shows the observed addresses in the executed example.

Figure 9: Key attributes location in RAM


Debug procedure for private key wrapping

Files to open and breakpoints to set for debugging

Figure 10: Breakpoints for private key observation


  • Open the kwe_core.c and set the breakpoints
    • Middlewares/ST/mbedtls_key_wrap_engine/kwe_core.c
      • Search: /* Configure Wrapping Key : DHUK */
      • Set two breakpoints as indicated in the figure 8 (the code line number can differ depending on the CubeFw release)
  • Open the psa_crypoto.c:  Middlewares/mbedtls/psa_crypto.c
    • Search: /* Key material is saved in export representation in the slot
    • Set a breakpoint as indicated in the figure below (the code line number can differ depending on the Cube Firware release)

Execute the code till the first breakpoint.

Figure 11-a: Private key in RAM memory

The 32-byte private key is temporarily stored in embedded RAM while the blob is being created.

To inspect the copy in RAM:

  1. Open the Memory view.
  2. Right-click in the view and select Find (or press Ctrl+F).
  3. Enter the first word of the known private key, then select Find Next.
  4. Record the address of the copy. In this example, it is 0x20012B2C.
Note
Record this address so you can later check whether the key material has been erased.

In this example, the private key is also stored in flash. To inspect it, search the flash address space. The example address is 0x080130CC

.

Figure 11-b: Private key in Flash memory


Note
The private key copy in RAM is optional since this key is already available in flash. This is a generic example since for some use cases this copy can be useful.

Execute the code till the second breakpoint.

Figure 12: ECDSA Blob content


ECDSA_Blob is a structure containing three pointer fields, located at 0x2000A348. Each field stores the address of the corresponding data item in memory (see Figure 9).

The fields point to the initialization vector (IV), the authentication tag, and the wrapped private key. Their values are shown in Figure 10:

  • IV: The initialization vector used by the AES-GCM key-wrapping mechanism. In this example, it is located at 0x2000AAC0. The first four bytes of the IV are fixed to 0x2, and the remaining bytes are generated by the RNG (see Figure 11).
  • Tag: Used to verify the key’s authenticity during unwrapping. During key wrapping, the hardware generates a tag associated with the key and IV. During unwrapping, the hardware generates a tag for comparison with the stored tag. A match confirms the key’s authenticity. The tag is located at 0x2000AAD0.
  • pWrappedKey: Points to the encrypted 256-bit private key, which is the same size as the original private key. It is located at 0x2000AAE0.
    Figure 13: ECDSA_Blob values in embedded RAM
Note
  • The wrapped key is device specific. It is generated from the DHUK (Derived Hardware Unique Key), for more details see: Coupling and Chaining Bridge (CCB) key wrapping - stm32mcu).
  • Running twice the code, the wrapped key is different.

Key point:

The DHUK is associated with the

  • HDPLx security level,
  • the secure or non-secure execution domain,
  • and the variable context settings.
  • Execute the code till the third breakpoint set previously.
    Figure 14-a: Saving the wrapped key in flash
    Step over the current line.

The wrapped key is stored in embedded flash because it is configured as persistent (see the /* Setup the key policy for the private key */ section in main.c).

To locate the wrapped key, search the memory view starting at 0x08000000. In this example, the wrapped key is located at 0x080FC034.


Figure14-b: Wrapped private key storage in Flash


Note
The representation in figure 14-b is with 4x Units

key point:

Once the key has been wrapped, the wrapped key can be stored in the designated memory area. The key material used during wrapping, including the private and public keys, can then be removed from RAM and, where applicable, from flash. This cleanup helps minimize the exposure of sensitive material and reduce the risk of unauthorized access or reuse.

If the wrapped key must be retained, an additional copy step may be needed to move it from the memory area managed by the mbedTLS library to another memory area. This copy can then be used for the signing operation.

If the data is no longer needed, call mbed_crypto_free() to release the associated library-managed resources. Warning: This function frees only the instance managed by the library. It does not erase other copies of the wrapped key; delete those separately.

In this example, you can inspect the wrapped key’s erasure by opening main.c, setting a breakpoint, and stepping over the relevant code. The Memory view then shows the result, as illustrated in the figure below.

Note
Confirm that the cleanup code also deletes any persistent PSA key stored in flash. Freeing a library-managed instance alone may not remove a persistent key.
Figure 15: Reset of the private key attributes
Warning
In this example, the private and public keys are not erased after psa_crypto_free() is called (see the figure below). In a real application, the developer must define a key-management strategy to ensure that the private key remains secret.
Figure 16: Persistance of the private key in memories
Note
Once the private key is wrapped all operations can be done with this wrapped key and the private key can be erased.

8. Blob usage for signing

Before signing, the input message is hashed to produce the digest used by the ECDSA algorithm.

During signing, the wrapped blob is used instead of the plaintext private key. The blob is retrieved from protected storage and processed through the CCB-orchestrated execution flow. The SAES restores the protected context, the RNG provides the ephemeral nonce, and the PKA computes the ECDSA signature.

The following sections describe the CCB operation steps and how the CCB interacts with the SAES and PKA. They show that signing is not a standalone operation, but part of a blob-use flow orchestrated by the CCB, protected by the SAES, and executed by the PKA. CCOP = 0x0C3 identifies the protected ECDSA signing workflow.

8.1. Debug procedure for signing

  1. Open stm32h5xx_hal_ccb.c and locate the HAL_CCB_ECDSA_Sign() function.
  2. Set breakpoints at the locations indicated in the corresponding table.
  3. Rerun the example and continue execution until the signing phase begins.
  4. Step through the protected signing sequence, monitoring the OPSTEP progression in the CCB registers.
  5. Continue until the signature is generated.

8.2. OPSTEP sequence for blob retrieving

The signing workflow is organized into a sequence of OPSTEP values. It begins with configuration at 0x01, where the secure context is initialized. The flow then proceeds to input preparation at 0x12 and parameter setup at 0x13, involving coupling between the CPU and the secure hardware blocks. At 0x14, the encrypted private key is transferred from the SAES to the PKA. Next, the RNG provides a random nonce to the PKA at 0x16, followed by signature computation by the PKA at 0x17. Finally, 0x1A marks the finalization stage, when the operation completes and the signature is obtained.

The table summarizes the successive OPSTEP breakpoints used by the CCB during the protected ECDSA signing flow. Each step corresponds to an operation, from secure-context setup and blob handling to private-key restoration, nonce loading, signature authorization, PKA execution, and retrieval of the final signature.

Table 2: OPSTEP sequence for blob usage
OPSTEP Breakpoint in CCB_ECDSA_SignBlobCreation() function Main action PATH Coupling / chaining
0x01 CCB_WaitOperStep(hccb, 0x01, ...) Initialize the secure hardware context Setup None
0x12 CCB_WaitOperStep(hccb, 0x12, ...) Configure wrapping key and signing context CPU → CCB Coupling
0x13 CCB_WaitOperStep(hccb, 0x13, ...) Load hash, IV, and tag SAES → CCB Chaining
0x14 CCB_WaitOperStep(hccb, 0x14, ...) Restore encrypted private key CPU → PKA + SAES / SAES → PKA Coupling + Chaining
0x16 CCB_WaitOperStep(hccb, 0x16, ...) Load RNG nonce `k` RNG → PKA Chaining
0x17 CCB_WaitOperStep(hccb, 0x17, ...) Authorize protected signature execution SAES → CCB Chaining
0x18 CCB_WaitOperStep(hccb, 0x18, ...) Finalize blob usage CCB → PKA Chaining
0x19 SET_BIT(PKA->CR, PKA_CR_START) then CCB_WaitOperStep(hccb, 0x19, ...) Start PKA ECDSA computation CCB → PKA Chaining
0x1A CCB_WaitOperStep(hccb, 0x1A, ...) Read signature `(r, s)` and complete operation PKA execution None
0x0 CLEAR_BIT(hccb->Instance->CR, CCB_CR_IPRST); Reset CCB and restore state to READY Cleanup None

8.3. Blob usage monitoring

During the ECDSA blob-usage phase, the CCB controls the execution flow, the SAES restores the protected context, the PKA performs the cryptographic operation, and the RNG provides the random nonce. According to RM0517, the ECDSA signature blob-usage sequence starts when CCOP = 0xC3 is set in CCB_CR. The OPSTEP field in CCB_SR identifies the current state-machine step and indicates the operation’s progress. Monitor the CCB_BUSY and OPERR bits to check that the operation is running and that no security or configuration errors have occurred.

In the debugger, first monitor the CCB register group, particularly:

  • CCB_CR, to verify that CCOP = 0xC3 is selected.
  • CCB_SR, to track the OPSTEP progression and monitor the CCB_BUSY and OPERR[5:0] fields.
  • CCB_REFTAGRx, when the reference tag is loaded and checked during GCM authentication.

The second figure highlights the PKA, SAES, and RNG registers to monitor during blob usage:

  • Check PKA_CR and PKA_SR to verify that the PKA is enabled in the expected mode (MODE = 0x24) and to track the START, INITOK, and PROCENDF status bits.
  • Monitor SAES_CR and SAES_ISR to check the selected GCM mode, the GCMPH progression, and the completion of the AES-GCM phases.
  • Monitor RNG_CR and RNG_SR to verify that the random number generator is initialized correctly and that random data are available when required.

These registers provide a complete view of the protected ECDSA blob usage flow and allow the user to validate each step of the chaining and coupling sequence (more details can be found in RM0517[1])

Figure 17-b: SAES, PKA and RNG registers values during OPSTEP= 0x12
Figure 17-a: Example of CCB registers values during blob usage - OPSTEP= 0x12


9. Signature verification

The objective is to confirm that a signature was generated for a given hash using the corresponding private key, and that the matching public key can successfully verify it.

The process follows the standard public-key cryptography model:

  1. Hash the message to produce a fixed-size digest.
  2. Use the private key to generate a digital signature for the digest.
  3. Import the corresponding public key.
  4. Verify the signature against the hash and public key.

In this example, psa_verify_hash() verifies the generated signature using the hash and the public key provided in the code. It does not hash or verify the original message directly. Successful verification confirms that the signature is valid for the given hash and was generated using the private key corresponding to the public key.

The receiver first computes the message hash, which serves as a compact representation of the message. The verification function then checks the signature against that hash and the public key. If verification succeeds, the signature is valid for the supplied hash and key. This supports message-integrity and signer-authenticity checks, provided the public key is trusted. Otherwise, verification fails and the signature is rejected.

If verification succeeds:

  • Sensitive data is cleaned up.
  • The green LED turns on.
  • The example reports successful completion.

10. Security considerations

  • After import, the private key is represented by a protected blob. Ensure that any plaintext copies are securely erased; they may otherwise remain in RAM or flash.
  • The blob is stored in protected form.
  • The signing flow is controlled by CCOP and OPSTEP.
  • The SAES supports secure context restoration and integrity checks.
  • The PKA performs the cryptographic operation without exposing the secret key material.

11. Conclusion

This example demonstrates a secure ECDSA signing workflow in which a CPU-provided key is wrapped into a protected blob. The document describes the blob’s lifecycle, from creation with psa_import_key() to protected signing and signature verification. It also explains the internal CCOP and OPSTEP execution model and the roles of coupling and chaining in coordinating the CCB, SAES, PKA, and RNG.

12. References