|
SharkSSL C/C++ Reference
TLS, cryptography, certificates, and PSA integration
|
Connect a Zephyr device to an HTTPS server or control an onboard LED from your browser. These examples show how to use SharkSSL for TLS connections and certificate validation while using the board's PSA Crypto services for cryptographic operations.
The supplied integration targets the NXP FRDM-RW612. SharkSSL runs in the nonsecure application and calls Trusted Firmware-M (TF-M) through the PSA (Platform Security Architecture) Crypto API. TF-M provides the cryptographic services in the secure firmware. Networking uses Ethernet, and stacks and heap use internal SRAM. No application filesystem or external PSRAM is needed.
Choose the example that matches what you want to try:
| Example | What you can do |
|---|---|
| CertCheck | Inspect server certificates and see whether they pass certificate-chain, hostname, and date checks. |
| LED-SMQ | Use a browser to control the board's green LED over a TLS-protected SMQ connection. |
Both use the shared C examples supplied with SharkSSL. The board setup and Zephyr adapters are kept separate, giving you a starting point for your own application or another board.
You need the FRDM-RW612 board, a USB debug connection, and an Ethernet network with DHCP, DNS, and Internet access. The examples also need access to an SNTP time server so they can check certificate validity dates.
For building, use a Linux shell with CMake, Ninja, west, and an existing NXP ZSDK nxp-v4.4.1.1 workspace, including its modules and Python environment. Use a compatible Zephyr SDK toolchain; the paths below show Zephyr SDK 1.0.0. These commands build the application using your installed dependencies.
RW612 prerequisite: the NXP workspace must include the FlexSPI receive-buffer boundary fix required by this SDK. It is a change to the external SDK and is not supplied by this SharkSSL build directory.
For board connections and debug-probe setup, see the Zephyr FRDM-RW612 board guide. Use the SDK version and board target specified here for these examples.
Run the following from the SharkSSL root directory. Adjust the first three paths to match your installed Python environment, NXP workspace, and toolchain. Set example=LED-SMQ if you want to try the browser-controlled LED.
SHARKSSL_EXAMPLE defaults to CertCheck. Each example has its own output directory. Program the resulting build/zephyr/out/frdm_rw612/<example>/zephyr/tfm_merged.hex, which combines the bootloader, TF-M secure firmware, and SharkSSL application.
The vendor development profile uses dummy boot keys. Before deploying a product, replace these through the board's production provisioning process.
Connect Ethernet and the J10 debug USB port, leave JP3 open, and monitor the serial console at 115200 baud, 8 data bits, no parity, and one stop bit.
Choose the programming method that matches your debug probe's firmware. For MCU-Link running LinkServer firmware, use the installed LinkServer runner from the same shell as the build:
For MCU-Link running J-Link firmware, select device RW612, interface SWD, and speed 4000 kHz in J-Link Commander. Replace the image path below with your selected example's absolute path. The r0 and r1 commands reset the board through its reset pin so the complete secure boot chain runs:
Use normal sector programming to retain TF-M persistent keys. Avoid a whole-chip erase when you need to keep stored keys.
At startup, both examples initialize PSA Crypto and synchronize the clock with an SNTP time server. TLS connections start only after these steps succeed. The clock then advances locally for certificate-date checks. If startup reports No initialized UTC clock, check the Ethernet connection, DNS settings, and access to the time servers.
Watch the serial console. CertCheck connects to the servers listed in examples/CertCheck.c, prints each server's certificate chain and trust result, and sends an HTTP request only when the certificate chain, hostname, and date checks pass. It finishes without waiting for keyboard input.
This example uses TLS 1.3 with AES-GCM and RSA/ECDSA certificate verification. A server that supports only TLS 1.2 cannot connect with this example's configuration.
Once the board has connected, open the LED demo page, select SharkSSL FRDM-RW612, and toggle Green LED. Keep Ethernet connected so the board can reach the default broker at https://simplemq.com/smq.lsp.
The example uses examples/LED-SMQ.c and the SharkMQ client library in examples/SharkMQ.c. The green LED starts off. The serial console shows each requested state and GPIO readback; it also displays the device identity as SMQ ID: xx:xx:xx:xx:xx:xx. This board example provides remote LED control only; local button control and temperature reporting are disabled.
LED-SMQ uses TLS 1.2 with ECDHE-ECDSA/AES-128-GCM to connect to this demo broker. It checks the broker's certificate against the bundled RTL certificate authority (CA), verifies the hostname, and checks the certificate dates before starting SMQ communication.
The public demo lets anyone control the LED without signing in. For an application that needs access control, use a private broker with user authentication.
Paths in this guide are relative to the SharkSSL root unless stated otherwise. The source layout separates the reusable port from the board and examples:
To add a board, create build/zephyr/boards/<board>/ with its configuration, PSA provider include paths, and required C sources. The top-level build/zephyr/CMakeLists.txt selects the board folder from the first component of BOARD, such as frdm_rw612 in frdm_rw612/rw612/ns.
To add an example, create its entry point, example.cmake, and prj.conf under build/zephyr/examples/<example>/, then extend the supported-example list in build/zephyr/CMakeLists.txt. Keep portable application code in the root examples/ directory and hardware-specific code in the board directory. Use a fresh output directory when changing board qualifiers or PSA providers.
Keep reusable Zephyr platform code in inc/arch/Zephyr/ and socket and console adapters in examples/arch/Zephyr/. Shared example startup belongs in build/zephyr/common/.
build/zephyr/cmake/sharkssl.cmake selects the PSA consumer adapters and puts the provider's headers before SharkSSL's headers. This build uses TF-M as its crypto provider. Do not also compile native src/SharkSslCrypto.c, assembly backends, or SharkSSL's own src/psa provider into this target. For the shared integration rules, see the PSA consumer guide.
The supplied configuration disables ChaCha20, Poly1305, and Brainpool curves to match this provider profile. Review the algorithm selection when changing providers.
inc/arch/Zephyr/TargConfig.h supplies aligned allocation and Zephyr mutexes. examples/arch/Zephyr/seZephyr.c provides socket I/O with bounded connection and send waits. CMake selects both Zephyr port include directories and compiles seZephyr.c for both examples. Keep these configuration settings:
| Setting | Why it is needed |
|---|---|
CONFIG_NET_CONTEXT_SNDTIMEO=y | Enables socket send timeouts. |
CONFIG_LOG_PRINTK=n | Keeps CertCheck's character-by-character output out of the deferred log queue so certificate output is complete. |
CONFIG_ARM_NONSECURE_PREEMPTIBLE_SECURE_CALLS=n | On RW612, prevents thread switches during TF-M secure calls while leaving interrupts enabled. |
The TF-M configuration in build/zephyr/boards/frdm_rw612/tfm/config.h uses the vendor large profile with 64 concurrent crypto operations and 64 key slots. It also enables HKDF Extract/Expand and PBKDF2-HMAC.
The LED adapter in build/zephyr/boards/frdm_rw612/led.c exposes one green LED, ID 1, using the board's led0 alias (HSGPIO0 pin 12, active low). Status messages go to the console so they do not interfere with remote LED control.
The getUniqueId() implementation in build/zephyr/boards/frdm_rw612/identity.c returns a validated, retained copy of the six binary bytes of the Ethernet MAC address. By default, this address comes from the chip's one-time-programmed hardware ID and stays the same across boots. Random MAC configuration is rejected at build time. If you provision MAC addresses yourself, give each board a distinct address. SMQ's broker-assigned routing ID is separate from this device identity.