{"data":{"slug":"discovered-071ee04c0a2868019bb2db875e71d2926aba5792d830922d","title":"Mbedtls To Psa Test Conversion","summary":"Convert mbedtls_* calls in the v4 test tree of ra\/fsp\/src\/rm_tf_psa_crypto\/!test to the PSA API for ChaCha20, ChaCha20-Poly1305, AES-CMAC-PRF, and AES-ECB. This includes handling open items and conversion pitfalls.","url":"https:\/\/skillshunter.net\/skills\/discovered-071ee04c0a2868019bb2db875e71d2926aba5792d830922d","json_url":"https:\/\/skillshunter.net\/api\/public\/v1\/skills\/discovered-071ee04c0a2868019bb2db875e71d2926aba5792d830922d","markdown_url":"https:\/\/skillshunter.net\/skills\/discovered-071ee04c0a2868019bb2db875e71d2926aba5792d830922d.md","download_url":"https:\/\/skillshunter.net\/skills\/discovered-071ee04c0a2868019bb2db875e71d2926aba5792d830922d\/download","category":{"slug":"software-development","name":"Software & Development"},"editor_note":"Before conversion, ensure that `psa_crypto_init()` is called after setting key attributes and before importing keys. Also, verify that test files are in CRLF format and that PEM keys are replaced with DER format where required by PSA.","instructions":"---\nname: mbedtls-to-psa-test-conversion\ndescription: Converting or removing mbedtls_* calls in ra\/fsp\/src\/rm_tf_psa_crypto\/!test to the PSA API (ChaCha20, ChaCha20-Poly1305, AES-CMAC-PRF, AES-ECB), the open items, and the conversion pitfalls. Use when editing those test files.\nmetadata:\n  type: reference\n  branch: FSPRA-5629_MbedTLS_v4.1.0_TRNG_Sync\n  compacted: 2026-11-01\n---\n\n# mbedtls_* to PSA conversion in rm_tf_psa_crypto\/!test\n\nScope: the v4 test tree only. `ra\/fsp\/src\/rm_psa_crypto\/!test` (v3) keeps mbedTLS.\n\n## Status (as of 2026-10-20; compile-checked with stubs, not run on hardware)\nConverted:\n- tg2: `TC_API_AESEncryptDecryptCMACPRF128` (RFC 4615)\n- tg6: `TC_API_AESForCTRDRBG` (AES-ECB, one block)\n- tg10: `TC_API_CHACHA20`, `_CHACHA20_stream_unaligned_data`, `_CHACHA20_stream`, `TC_API_CHACHAPOLY`, `_CHACHAPOLY_state_check`, `_CHACHAPOLY_Stream_EncryptDecrypt`\n\nRemoved (test, requirement comment, `RUN_TEST_CASE`, usage-note `@anchor`):\n- tg1 `TC_API_SHA256MbedTLSCommonSelfTest` (`tf_sha256_hash`)\n- tg10 `TC_API_POLY1305` (`tf_poly1305_mac`): PSA has no standalone Poly1305\n\nOpen, still mbedtls:\n- tg3 `TC_API_RSAEncryptDecryptPKCS1V15` and `_OAEP`: the key is PEM. `psa_import_key` needs DER. Kept on mbedtls by decision.\n- tg1 `TC_API_GenerateRandom`, `TC_API_ImportRawData`: `mbedtls_platform_get_entropy`, `mbedtls_psa_crypto_free`.\n- tg3 helper `import_sign_verify_rsa` and the `RSAImportKeyPair*` tests.\n- tg9 and tg11 PQC helpers (`mbedtls_mlkem_*`, `mbedtls_mldsa_*`).\n- `MBEDTLS_PUT_UINT32_BE` (a macro from `core\/alignment.h`). Left as is.\n\n## Conversion patterns\n- **ChaCha20:** PSA cannot set the initial counter. For counter N, call `psa_cipher_update` on N x 64 zero bytes and discard the output, then encrypt. Set the 12-byte nonce with `psa_cipher_set_iv`. Matches RFC 7539 vectors (checked in Python).\n- **ChaCha20-Poly1305 one-shot:** `psa_aead_encrypt` writes ciphertext then a 16-byte tag. `psa_aead_decrypt` needs ciphertext and tag in one buffer.\n- **Multipart and state checks:** `psa_aead_encrypt_setup`, `_set_nonce`, `_update_ad`, `_update`, `_finish`. `MBEDTLS_ERR_CHACHAPOLY_BAD_STATE` maps to `PSA_ERROR_BAD_STATE`. Use `PSA_AEAD_OPERATION_INIT` and `psa_aead_abort` between sub-cases.\n- **AES-CMAC-PRF-128:** if the key is not 16 bytes, `K' = CMAC(0^128, K)`, then `CMAC(K', M)`. Both use `psa_mac_compute(PSA_ALG_CMAC)`. Usage `PSA_KEY_USAGE_SIGN_MESSAGE`. Checked against the three RFC vectors.\n- **AES-ECB one block:** `psa_cipher_encrypt` with `PSA_ALG_ECB_NO_PADDING`, usage `PSA_KEY_USAGE_ENCRYPT`.\n- **Key attributes:** declare with `PSA_KEY_ATTRIBUTES_INIT`. To reset in a loop: `attributes = (psa_key_attributes_t) PSA_KEY_ATTRIBUTES_INIT;`.\n\n## Pitfalls\n- Missing `psa_crypto_init()` gives -137 (`PSA_ERROR_BAD_STATE`) on `psa_import_key`. Call it after setting the attributes and before the first import (seen on ra2a1_ek and ra2a2_ek).\n- PEM is not PSA input. Test keys in `ra\/fsp\/src\/!test\/crypto\/test_data_rsa.c` are PEM. PSA needs PKCS#1 DER.\n- Raw RSA components (`private_e_2048`, `public_e`) are numbers, not an importable key.\n- Removing a test needs four edits: the test with its requirement comment, the `RUN_TEST_CASE` line in `psa_crypto_tests_runner.c`, the `@anchor` in `!dsn\/rm_tf_psa_crypto_usage_notes.md`, and any unused `#include`.\n- A scanner that reads to the next `TEST(` counts helpers as tests. Use \"body ends at the next line that is exactly `}`\".\n- Files are CRLF. Python writes must keep `\\r\\n`. Check with `grep -c $'\\r$'` against `wc -l`.\n- zsh: write `\"${VAR}:path\"`, not `\"$VAR:path\"`. Run multi-line bash through a file.\n\n## Verification without a full build\nGenerated BSP headers (`bsp_cfg.h`, `bsp_mcu_ofs_cfg.h`) are not in the repo, so the test files cannot compile on the host. Extract the test body, replace `TEST()` with a `void` function, add stubs for the `TEST_*` macros and `BSP_FEATURE_*`, then:\n`gcc -std=c99 -Wall -Wextra -fsyntax-only -I ra\/arm\/mbedtls-v4\/tf-psa-crypto\/include -I ra\/arm\/mbedtls-v4\/tf-psa-crypto\/drivers\/builtin\/include`\nAlgorithm correctness: check against vectors with Python `cryptography`.\n\n## Rules\n- Do not touch the v3 tree.\n- Keep test names and requirement IDs unless the test is removed.\n","use_cases":["Converting mbedtls_* calls to PSA API in test files","Removing tests that are not compatible with PSA","Handling conversion pitfalls such as key format requirements and state management","Verifying test correctness without a full build using stubs and Python"],"limitations":[],"compatibility":[],"setup_tip":null,"sample_input":null,"sample_output":null,"source_url":"https:\/\/github.com\/minhnguyenyf\/skills\/blob\/7213d81500a27f3a96028144f5342ccfe4efbe4a\/psa\/mbedtls-to-psa-test-conversion\/SKILL.md","source_commit":"7213d81500a27f3a96028144f5342ccfe4efbe4a","license":null,"version":null,"listing_approved_at":"2026-11-30","created_at":"2026-11-29T11:12:18.000000Z","updated_at":"2026-11-30T06:52:26.000000Z","seo_title":"Convert Mbedtls to PSA Test Files","seo_description":"Convert mbedtls_* calls to PSA API in test files for ChaCha20, ChaCha20-Poly1305, AES-CMAC-PRF, and AES-ECB. Handle conversion pitfalls and ensure compatibility with PSA.","seo_keywords":["mbedtls to psa conversion","psa api test conversion","chaCha20 test conversion","aes-cmac-prf test conversion","test file conversion","mbedtls test removal","psa key import requirements"]}}