Below is a “from-scratch” walkthrough that shows one practical way to encrypt a file that lives in the i.MX93-FRDM Linux file-system while keeping the AES key inside the on-die EdgeLock Secure Enclave (SE).
Everything is based on public NXP components, so you don’t need any NDA documentation (although the SRM does give you deeper details).
⚠️ What you will NOT see here
• How to set up secure boot, life-cycle management, or EdgeLock 2GO cloud provisioning.
• CAAM “black keys” or HAB blobs (different, older mechanisms).
The focus is file encryption with a key that never leaves the Secure Enclave.
====================================================================
- Bring-up prerequisites
====================================================================
| Item | Version / Setting | Why |
|---|
| BSP | Latest NXP Yocto (kirkstone or mickledore) + meta-imx | Includes SE driver, OP-TEE, CAAM, etc. |
| Kernel | ≥ 6.1 with CONFIG_CRYPTO_DEV_FSL_CAAM and CONFIG_IMX_SE_VERSION_2 | Gives you both the SE character device and the CAAM crypto offload. |
| u-Boot | CONFIG_IMX_SE_SUPPORT enabled | Early access to the SE for key derivation if you need it. |
| Packages | optee-client imx-se-userspace pkcs11-tools openssl (with pkcs11 engine) | Userland plumbing. |
Yocto local.conf snippet:
DISTRO_FEATURES:append = " optee cryptodev pkcs11"
IMX_DEFAULT_SE_ENABLED = "1" # pulls in se050 + on-die IMX-SE v2 libs
IMAGE_INSTALL:append = " \
pkcs11-tools openssl-engine-devcrypto openssl-engine-pkcs11 \
"
After bitbake imx-image-full, flash and boot the board.
You should now have two character devices:
/dev/imx-se0 # EdgeLock SE v2 user-land gateway
/dev/caam_jr0 # Job ring for bulk crypto (not used for the key below)
====================================================================
2. One-time provisioning: create an AES-256 key inside the SE
EdgeLock SE presents itself in user-land through an NXP PKCS#11 library
(libimx_pkcs11.so). We’ll use that so we don’t write driver code.
- Tell OpenSSL where the module lives (once per session):
export PKCS11_MODULE=/usr/lib/libimx_pkcs11.so
export PKCS11_SLOT=0 # slot0 is on-die SE
export PKCS11_PIN=123456 # factory default SO-PIN; change it later
- Generate an AES-256 key that never leaves the chip:
pkcs11-tool --module $PKCS11_MODULE \
--slot $PKCS11_SLOT --login --pin $PKCS11_PIN \
--key-type aes:256 \
--label "fs_key" --id 01 \
--generate-key
Check it exists:
pkcs11-tool --module $PKCS11_MODULE -O
You should see something like:
Object 0:
label: fs_key
ID: 01
Usage: encrypt,decrypt
Type: AES (256 bits, sensitive, token-resident)
====================================================================
3. Encrypt a file from Linux user-space
OpenSSL (≥ 3.x) can speak symmetric ciphers through PKCS#11.
We will pipe through openssl enc -engine pkcs11.
⚠️ Note: Older OpenSSL engines understand only public-key ops.
If your BSP ships an older engine, install the OpenSSL 3 pkcs11-provider or use C code shown later.
Command-line only:
# plaintext -> ciphertext
openssl enc -aes-256-cbc \
-provider pkcs11 -provider default \
-pass "pkcs11:token=imx-se;id=%01" \
-pbkdf2 -iter 1 -salt \
-in plain.txt \
-out plain.txt.enc
Explanation
• -provider pkcs11 makes OpenSSL fetch the key material from the SE.
• -pass "pkcs11:..." references the object ID 0x01 (fs_key).
• -pbkdf2 -iter 1 is a dummy because we already own a 256-bit key; OpenSSL still wants a passphrase transform, so we do the cheapest one.
• The key bytes never leave the EdgeLock; OpenSSL calls into the module to run AES.
====================================================================
4. Decrypt the file (same machine / same SE)
openssl enc -d -aes-256-cbc \
-provider pkcs11 -provider default \
-pass "pkcs11:token=imx-se;id=%01" \
-pbkdf2 -iter 1 \
-in plain.txt.enc \
-out plain.txt.dec
diff -s plain.txt plain.txt.dec # should be identical
====================================================================
5. Programmatic example (C, PSA Crypto)
If you prefer to embed encryption in your own application, link
against the PSA Crypto layer that routes requests to the SE driver.
#include <psa/crypto.h>
#include <stdio.h>
#include <stdlib.h>
#define KEY_ID 0x8001 /* persistent ID inside SE */
#define ALGO PSA_ALG_CTR
#define IV_LEN 16
#define BUF_SZ 4096
static void die(const char *s, psa_status_t st)
{
fprintf(stderr,"%s: %d\n", s, st); exit(1);
}
int main(void)
{
psa_status_t st;
psa_key_attributes_t attr = PSA_KEY_ATTRIBUTES_INIT;
psa_key_id_t key = KEY_ID;
/* Initialise library */
if ((st = psa_crypto_init()) != PSA_SUCCESS)
die("psa_crypto_init", st);
/* Create or open persistent key */
psa_set_key_id(&attr, KEY_ID);
psa_set_key_type(&attr, PSA_KEY_TYPE_AES);
psa_set_key_bits(&attr, 256);
psa_set_key_usage_flags(&attr,
PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
psa_set_key_algorithm(&attr, ALGO);
psa_set_key_lifetime(&attr,
PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(
PSA_KEY_PERSISTENCE_DEFAULT,
PSA_KEY_LOCATION_SE_DRIVER_SLOT)); /* <- the magic */
st = psa_generate_key(&attr, &key);
if (st != PSA_SUCCESS && st != PSA_ERROR_ALREADY_EXISTS)
die("psa_generate_key", st);
/* Read stdin -> encrypt -> stdout */
uint8_t iv[IV_LEN];
psa_generate_random(iv, IV_LEN);
fwrite(iv,1,IV_LEN,stdout); /* prepend IV */
psa_cipher_operation_t op = PSA_CIPHER_OPERATION_INIT;
st = psa_cipher_encrypt_setup(&op, key, ALGO);
if (st) die("setup", st);
st = psa_cipher_set_iv(&op, iv, IV_LEN);
if (st) die("set_iv", st);
uint8_t in[BUF_SZ], out[BUF_SZ+16];
size_t len, out_len;
while ((len = fread(in,1,BUF_SZ,stdin)) > 0) {
st = psa_cipher_update(&op, in, len, out, sizeof(out), &out_len);
if (st) die("update", st);
fwrite(out,1,out_len,stdout);
}
st = psa_cipher_finish(&op, out, sizeof(out), &out_len);
if (st) die("finish", st);
fwrite(out,1,out_len,stdout);
psa_cipher_abort(&op);
return 0;
}
Build:
gcc edgelock_enc.c -o edgelock_enc -lpsa_crypto
Run:
cat plain.txt | ./edgelock_enc > plain.txt.enc
====================================================================
6. Going further (fs-level encryption)
If you want transparent encryption (ext4, f2fs, etc.):
- Enable
fscrypt in the kernel (CONFIG_FS_ENCRYPTION + CONFIG_FS_ENCRYPTION_INLINE_CRYPT).
- Build
fscryptctl in Yocto.
- Add a “hardware-wrapped” policy that points to the PKCS#11 key:
fscrypt keyctl add_key \
"fscrypt-provisioning" \
"pkcs11:token=imx-se;id=%01" @u
fscrypt encrypt /home/root/secret_dir \
--policy=hardware_wrapped
Under the hood fscrypt will ask the EdgeLock driver to unwrap the key when the directory is accessed.
====================================================================
7. Troubleshooting checklist
• lsmod | grep caam and lsmod | grep imx – drivers loaded?
• openssl engine -t -c pkcs11 – module visible to OpenSSL?
• strace -e openat openssl … – verify /dev/imx-se0 access.
• If you see CKR_USER_NOT_LOGGED_IN: export PKCS11_PIN or pass --pin on each call.
• If you get CKR_KEY_HANDLE_INVALID → ID mismatch (remember leading zeros).
• Performance looks too slow? Verify that the CAAM JR is actually executing; fall-back to software is common if parameters are unsupported.
====================================================================
8. Reference links
• NXP i.MX Linux User’s Guide – chapter “EdgeLock Secure Enclave”.
• AN14213 – Using EdgeLock 2GO on i.MX93 (for cloud provisioning).
• PSA Crypto API v1.1 – Arm.
• fscrypt documentation – https://www.kernel.org/doc/html/latest/filesystems/fscrypt.html
Happy (and safe) hacking!