SM4 support through the PSA API II #909
Open
opened 2026-09-15 03:15:47 -05:00 by ronald-cron-arm
·
0 comments
No Branch/Tag specified
development
tf-psa-crypto-1.1
archive/20261005T153337Z-force/pull/927
archive/20261001T141055Z-force/pull/927
archive/20261005T153337Z-force/pull/931
archive/20261001T083616Z-force/pull/927
archive/20260930T232327Z-force/pull/927
archive/20260930T154618Z-force/pull/927
archive/20260929T231919Z-rescue/rohankl-arm/mldsa-pubkey-899
archive/20260929T231919Z-rescue/sigvartmh/pr/spake2p-9342-public-key-import-export
archive/20260929T231919Z-rescue/ManoharPaturi/fix-pem-ct-padding
archive/20260929T231919Z-rescue/ManoharPaturi/fix-asn1write-null-guard
archive/20260929T231919Z-rescue/ManoharPaturi/fix-pbes2-params-validation
archive/20260929T231919Z-rescue/valeriosetti/blake2
archive/20260929T231919Z-rescue/jfdev001/v1.1.1_bump-cmake-minimum-version
archive/20260929T231919Z-rescue/jfdev001/bump-cmake-minimum-version
archive/20260929T231919Z-rescue/ronald-cron-arm/sm3-base
archive/20260929T231919Z-rescue/valeriosetti/blake2-hmac
archive/20260929T231919Z-rescue/v1rtu3x/enforce-hash-length-sign-verify
archive/20260929T231919Z-rescue/bjwtaylor/file-backed-keystore-doc
archive/20260929T231919Z-rescue/sigvartmh/pr/spake2p-9344-set-context
archive/20260929T231919Z-rescue/sigvartmh/pr/spake2p-9343-key-pair-import-export-depends-on-9342
archive/20260929T231919Z-rescue/romart/development
archive/20260929T231919Z-rescue/sweetlilmre/constant-time-embedded-perf
archive/20260929T231919Z-rescue/JulioLoayzaM/add-wycheproof-rsa-pss
archive/20260929T231919Z-rescue/JulioLoayzaM/add-wycheproof-ecdsa
archive/20260929T231919Z-rescue/JulioLoayzaM/add-wycheproof-tests
archive/20260929T231919Z-rescue/oubaid-a/dev_psa_driver_wrapper
archive/20260929T231919Z-rescue/gilles-peskine-arm/keystoreless-prototype-1
archive/20260929T231919Z-rescue/valeriosetti/issue9580
archive/20260929T231919Z-rescue/LeonieReichertABB/hpke-functionality
archive/20260929T231919Z-rescue/gilles-peskine-arm/psa-hash-no-buffer-copy-1.1
archive/20260929T231919Z-rescue/vanhauser-thc/development
archive/20260929T231919Z-rescue/tpambor/key-wrap
archive/20260929T231919Z-rescue/ruiliio/psa_xts
archive/20261011T063515Z-force/pull/538
archive/20260929T231919Z-rescue/gilles-peskine-arm/fix-broken-links-1.1
archive/20260929T231919Z-rescue/valeriosetti/issue669
archive/20260929T231919Z-rescue/gilles-peskine-arm/prepare-release-test
archive/20260929T231919Z-rescue/krish2718/wpa3-sae-design-note
archive/20260929T231919Z-rescue/nzzlinh/fix_iar_endianness_1.1
archive/20260929T231919Z-rescue/nzzlinh/fix-iar-endianness
archive/20260929T231919Z-rescue/abtom87/fix-pbkdf2-iter-count
archive/20260929T231919Z-rescue/minosgalanakis/tools/prepare_release_lite
archive/20260929T231919Z-rescue/Harshal5/fix/aes-xts-in-place-cts
archive/20260929T231919Z-rescue/sigvartmh/pr/spake2p-9347-prover-depends-on-9342-9343-9344
archive/20260929T231919Z-rescue/crabel99/async-hardware-support-c35df962
tf-psa-crypto-1.2.0_mergeback
tf-psa-crypto-1.1.1_mergeback
archive/20260929T231919Z-rescue/asmellby/feature/dispatch-library
tf-psa-crypto-release-sync
archive/20260929T231919Z-rescue/ChakshuGupta13/fix/ecdsa-zeroize-stack-key
archive/20260929T231919Z-rescue/gilles-peskine-arm/security-md-option-names
archive/20260929T231919Z-rescue/parmi93/patch-2
archive/20260929T231919Z-rescue/bjwtaylor/remove-mldsa-sign-from-build
archive/20260929T231919Z-rescue/bjwtaylor/tf-m-build-issues
archive/20260929T231919Z-rescue/bjwtaylor/test-context-population
archive/20260929T231919Z-rescue/gilles-peskine-arm/iar-1.1.0
archive/20260929T231919Z-rescue/gilles-peskine-arm/fork-helper-unit-tests-create-crypto
archive/20260929T231919Z-rescue/bjwtaylor/redundant-declarations
archive/20260929T231919Z-rescue/hasheddan/docs/fix-broken-links
main
archive/20260929T231919Z-rescue/bjwtaylor/reintroduce-dh
archive/20260929T231919Z-rescue/bjwtaylor/cipher-update-incorrect-error
archive/20260929T231919Z-rescue/Marandil/fix/mbedtls-4314-parse-explicit-curve
archive/20260929T231919Z-rescue/schneider-electric/dev/plaj-se/parse-rsassa-pss
archive/20260929T231919Z-rescue/minosgalanakis/bugfix/program-fuzz-mscv
archive/20260929T231919Z-rescue/daverodgman/aesgcm
archive/20260929T231919Z-rescue/gilles-peskine-arm/sha3-coverity-20260224
archive/20260929T231919Z-rescue/bensze01/undef
archive/20260929T231919Z-rescue/valeriosetti/issue523
archive/20260929T231919Z-rescue/dannytsen/ppc-dev
archive/20260929T231919Z-rescue/daverodgman/aes12
archive/20260929T231919Z-rescue/gilles-peskine-arm/ascon-prototype
archive/20260929T231919Z-rescue/daverodgman/chacha10
archive/20260929T231919Z-rescue/kusumitg/implement_psa_pake_set_context_for_spake2p
archive/20260929T231919Z-rescue/davidhorstmann-arm/add-code-size-target
archive/20260929T231919Z-rescue/bjwtaylor/simplify-sign_func
archive/20260929T231919Z-rescue/mfil/feature/aes_ni_parallel
archive/20260929T231919Z-rescue/ivv19041994/patch-1
archive/20260929T231919Z-rescue/gilles-peskine-arm/psa-verify-policy-relax
public_2/main
archive/20260929T231919Z-rescue/mpg/pk-sign-proto
release/tf-psa-crypto1.0.0-documentation
archive/20260929T231919Z-rescue/gilles-peskine-arm/0e-plans-july-updates
archive/20260929T231919Z-rescue/davidhorstmann-arm/guard-context-types-in-client-only
archive/20260929T231919Z-rescue/gilles-peskine-arm/psa-driver-interface-spec-remove
archive/20260929T231919Z-rescue/gilles-peskine-arm/ecp-self-test-race-crypto
archive/20260929T231919Z-rescue/KiruthikPrakash/feature/9342_add-support-for-spake2p-public-key-import-export
archive/20260929T231919Z-rescue/ruiliio/chacha20_16
archive/20260929T231919Z-rescue/irwir/winsock2
archive/20260929T231919Z-rescue/aphroteus/development
archive/20260929T231919Z-rescue/amjoul01/amjoul01/psa-mac-out-of-bound-fix
archive/20260929T231919Z-rescue/gilles-peskine-arm/everest-submodule-crypto
archive/20260929T231919Z-rescue/yamt/xtensa-ccount
archive/20260929T231919Z-rescue/gilles-peskine-arm/all.sh-generate-only-before-checks-crypto
archive/20260929T231919Z-rescue/Harshal5/feat/builtin_cmac_driver_use_psa_crypto_interface
archive/20260929T231919Z-rescue/gilles-peskine-arm/test-data_t-empty_is_empty-crypto
archive/20260929T231919Z-rescue/jean-baptisteboric-eaton/oids-pkcs7-authenticated-attributes
dev/bensze01/public-only-commit
archive/20260929T231919Z-rescue/waleed-elmelegy-arm/psa-key-wrap-aes-kw
archive/20260929T231919Z-rescue/tfloch/psa_add_xts_algorithm
archive/20260929T231919Z-rescue/gilles-peskine-arm/msancf-clang16-crypto
archive/20260929T231919Z-rescue/DemiMarie/document-unexpected-tags
archive/20260929T231919Z-rescue/DemiMarie/strict-len
archive/20260929T231919Z-rescue/valeriosetti/issue9255-tfpsacrypto
archive/20260929T231919Z-rescue/daantimmer/use-compiler-frontend
archive/20260929T231919Z-rescue/benmcollins/enable-rsassa-pss-pkparse
dev/paul-elliott-arm/add_iop_key_agreement_negative_tests
add-iop-export-pub-key-get-num-ops
dev/rc/new-2-d635ea1
dev/bensze01/jenkins
v1.2.0
tf-psa-crypto-1.2.0
v1.1.1
tf-psa-crypto-1.1.1
v1.1.0
tf-psa-crypto-1.1.0
v1.0.0
tf-psa-crypto-1.0.0
v1.0.0-beta
tf-psa-crypto-1.0.0-beta
Labels
Clear labels
api-break
This issue/PR breaks the API and must wait for a new major version
approved
Design and code approved - may be waiting for CI or backports
bug
Something isn't working
component-docs
Docs / website issues filed here for tracking
component-platform
Portability layer and build scripts
component-test
Test framework and CI scripts
dependencies
Pull requests that update a dependency file
deprecation
needs deprecation announcement
DO-NOT-MERGE
enhancement
New feature or request
good first issue
Good for newcomers
help wanted
This issue is not being actively worked on, but PRs welcome.
needs-adoption
stalled PR that someone should pick up and complete
needs-backports
Backports are missing or are pending review and approval.
needs-ci
Needs to pass CI tests
needs-design-approval
Needs design discussion / approval
needs-info
An issue or PR which needs further info from the reporter / author
needs-preceding-pr
Requires another PR to be merged first
needs-review
Every commit must be reviewed by at least two team members
needs-reviewer
This PR needs someone to pick it up for review
needs-work
priority-high
High priority - will be reviewed soon
priority-low
Low priority - this may not receive review soon
priority-medium
Medium priority - this can be reviewed as time permits
priority-scheduled
This PR is big - it will require time to be scheduled for review
priority-very-high
Highest priority - prioritise this over other review work
resolved-after-1.1
The issue is resolved in development, but still present in 1.1 LTS.
single-reviewer
This PR qualifies for having only one reviewer
size-l
Estimated task size: large (2w+)
size-m
Estimated task size: medium (~1w)
size-optimisation
size-s
Estimated task size: small (~2d)
size-xs
Estimated task size: extra small (a few hours at most)
No labels
api-break
approved
bug
component-docs
component-platform
component-test
dependencies
deprecation
DO-NOT-MERGE
enhancement
good first issue
help wanted
needs-adoption
needs-backports
needs-ci
needs-design-approval
needs-info
needs-preceding-pr
needs-review
needs-reviewer
needs-work
priority-high
priority-low
priority-medium
priority-scheduled
priority-very-high
resolved-after-1.1
single-reviewer
size-l
size-m
size-optimisation
size-s
size-xs
Milestone
Clear milestone
No items
No milestone
Assignees
Clear assignees
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".
No due date set.
Dependencies
No dependencies set
Reference
gh-Mbed-TLS/TF-PSA-Crypto#909
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Freely available English translation of the Chinese SM4 block cipher and its mode of operations: https://datatracker.ietf.org/doc/html/draft-ribose-cfrg-sm4-10 (HTML) or https://www.ietf.org/archive/id/draft-ribose-cfrg-sm4-10.txt (txt).
An implementation has been proposed for SM4 in Mbed TLS: https://github.com/Mbed-TLS/mbedtls/pull/1165. No
CLA validlabel or DCO thus we cannot use it for the time being.PSA cryptography API specification:
PSA_KEY_TYPE_SM4 == 0x2405, with a 128-bit key and 16-byte block, introduced in 1.0.1. Support for SM4 key derivation throughpsa_key_derivation_output_key().SM4 can reuse the existing mode machinery. The main work is adding SM4 as a
128-bit block primitive, registering ECB/CBC/CTR with the generic cipher layer,
and exposing GCM/CCM only through PSA's lightweight block-cipher path.
The PSA identifier is already standardized:
PSA_KEY_TYPE_SM4 == 0x2405, witha 128-bit key and 16-byte block. PSA explicitly permits ECB, CBC, CTR, GCM, and
CCM for it. PSA key-type specification
The following sequence adds the SM4-based algorithms with their initial positive
test coverage. Each step should remain buildable and testable on its own.
SM4 implementation
The following sequence form a dependency-ordered implementation stack. At each
step, the library should build and pass its test suites. For the pre-PSA commits, test
with the default configuration plus a temporary definition of
MBEDTLS_PSA_BUILTIN_KEY_TYPE_SM4. From the PSA commit onward,PSA_WANT_KEY_TYPE_SM4is enabled by the default PSA configuration and theconfiguration adjustment code derives
MBEDTLS_PSA_BUILTIN_KEY_TYPE_SM4.1. Add the SM4 ECB primitive
Add:
drivers/builtin/src/sm4.cdrivers/builtin/include/tf-psa-crypto/private/sm4.hThe private API should follow Camellia/ARIA with tf_psa_crypto_ prefix:
tf_psa_crypto_sm4_init()/free()tf_psa_crypto_sm4_setkey_enc()/setkey_dec()tf_psa_crypto_sm4_crypt_ecb()Ordinary sbox-table-based implementation.
Open question: Add a constant-time implementation without and with neon-based
acceleration.
Testing: add an SM4 self-test suite. Run the published single-block encryption
and decryption examples and the one-million-encryption vector from the SM4
specification/draft. The test suite itself only needs to invoke the self-test.
2. Register SM4-ECB in the cipher abstraction
Add the SM4 cipher identifier MBEDTLS_CIPHER_ID_SM4 and the identifier for the
SM4 cipher with 128-bit key and ECB mode MBEDTLS_CIPHER_SM4_128_ECB in cipher.h.
Follow the CAMELLIA registration in
cipher_wrap.c: providesm4_info(no specific mode CBC, CFB ... for the time being) and
sm4_128_ecb_info.Reference them in the relevant tables.
Testing (test_suite_cipher.sm4.data): add cipher-abstraction known-answer tests
for ECB encryption and decryption using SM4 Example 1 from the specification.
The test function in
test_vec_ecb.3. Add the low-level SM4 cipher modes
In
sm4.handsm4.c, add:tf_psa_crypto_sm4_crypt_cbc()tf_psa_crypto_sm4_crypt_cfb128()tf_psa_crypto_sm4_crypt_ofb()tf_psa_crypto_sm4_crypt_ctr()The SM4 draft
defines CBC, CFB, OFB and CTR in Sections 8.4 through 8.7, based on the modes in
NIST SP 800-38A. Use those definitions as the algorithmic reference and the
existing Camellia/AES code as the Mbed TLS implementation and API reference.
The differences to account for are that the draft also defines CFB-1, CFB-8
and CFB-64, whereas only CFB-128 needs to be implemented, and that the draft
permits any non-repeating CTR sequence, while the Mbed TLS convention increments
the complete 128-bit counter as a big-endian integer.
Follow the Camellia implementations for CBC, CFB128 and CTR, and the AES
implementation for OFB, while checking each operation against the SM4 draft.
Use the common counter increment helper for CTR and the existing
MBEDTLS_CIPHER_MODE_*guards.Testing: extend the self-test in
sm4.c;test_suite_sm4remains unchanged andcontinues to invoke it. Test CBC, CFB128, OFB and CTR encryption and decryption
with both vector sets from Appendix A of the SM4 draft, including the complete
64-byte CTR vectors. These known-answer tests confirm that the selected Mbed TLS
profiles agree with the SM4 mode definitions.
4. Register the SM4 cipher modes in the cipher abstraction
In
cipher.h, addMBEDTLS_CIPHER_SM4_128_CBC,MBEDTLS_CIPHER_SM4_128_CFB128,MBEDTLS_CIPHER_SM4_128_OFBandMBEDTLS_CIPHER_SM4_128_CTR.In
cipher_wrap.c, add the corresponding callbacks tosm4_info, providesm4_128_cbc_info,sm4_128_cfb128_info,sm4_128_ofb_infoandsm4_128_ctr_info, and reference them in the cipher tables. Follow theCAMELLIA registrations for CBC, CFB and CTR and the AES registration for OFB.
Do not add XTS, GCM or CCM here.
Testing (
test_suite_cipher.sm4.data): add encryption and decryptiontest_vec_cryptcases for CBC, CFB128, OFB and CTR using Appendix A vectors.Increase the
test_vec_cryptoutput buffer to 64 bytes and use the completepublished CTR vector.
5. Add SM4 to the block-cipher abstraction
In
block_cipher.h, addMBEDTLS_BLOCK_CIPHER_ID_SM4and the SM4 context tothe context union. In
block_cipher.c, add the direct-software cases forcontext initialization/free, encryption-key setup and block encryption. Follow
the CAMELLIA
MBEDTLS_BLOCK_CIPHER_ENGINE_LEGACYimplementation and useMBEDTLS_PSA_BUILTIN_KEY_TYPE_SM4as the SM4 implementation guard.Update
block_cipher_internal.h,crypto_adjust_config_tweak_builtins.handtf_psa_crypto_check_config.hso CCM/GCM can select SM4 throughblock_cipher.c. Add theMBEDTLS_BLOCK_CIPHER_SM4_VIA_LEGACY,MBEDTLS_BLOCK_CIPHER_CAN_SM4andMBEDTLS_CCM_GCM_CAN_SM4capabilitymacros. “Legacy” here only identifies the direct-software block-cipher engine.
There is no public legacy SM4 API.
Testing (
test_suite_block_cipher.data): add atest_veccase that performsSM4 block encryption with specification Example 1.
6. Add SM4 CCM-star-no-tag to the cipher abstraction
In
cipher.h, addMBEDTLS_CIPHER_SM4_128_CCM_STAR_NO_TAG. Incipher_wrap.h, addMBEDTLS_CIPHER_HAVE_CCM_STAR_NO_TAG_SM4_VIA_LEGACY_OR_USE_PSA.In
cipher_wrap.c, provide the CCM-backedccm_sm4_infobase andsm4_128_ccm_star_no_tag_info. The set-key wrapper callsmbedtls_ccm_setkey()withMBEDTLS_CIPHER_ID_SM4. Follow the existingAES/ARIA/CAMELLIA CCM-star-no-tag registrations and shared CCM context handling.
This does not add tagged CCM or GCM to
cipher.c.Testing (
test_suite_cipher.sm4.data): add encryption and decryptiontest_vec_cryptknown-answer cases forMBEDTLS_CIPHER_SM4_128_CCM_STAR_NO_TAG, using the RFC 8998 Appendix A.2 keyand plaintext with independently derived ciphertext for a 13-byte nonce.
7. Add built-in PSA SM4 support
In
crypto_values.h, definePSA_KEY_TYPE_SM4as a fixed 128-bit block-cipherkey type. Update its documentation in
crypto.h, enablePSA_WANT_KEY_TYPE_SM4incrypto_config.h, updatecrypto_sizes.h(definitionof PSA_CIPHER_MAX_KEY_LENGTH), and add the option to
scripts/data_files/config-options-current.txt.Follow
PSA_KEY_TYPE_CAMELLIAinpsa_crypto.cfor key-size validation and incrypto_adjust_config_enable_builtins.hfor configuration derivation. Thelatter should derive
MBEDTLS_PSA_BUILTIN_KEY_TYPE_SM4. Updatecheck_crypto_config.hfor CCM/GCM prerequisites.In
psa_crypto_cipher.c, updatembedtls_cipher_validate_values()andmbedtls_cipher_values_from_psa()to map SM4 and permit ECB, CBC withoutpadding, CBC-PKCS7, CFB, OFB, CTR, CMAC, CCM-star-no-tag, CCM and GCM. Follow
the mapping for CAMELLIA and the implementation of the relevant mode. In
cmac.c, acceptMBEDTLS_CIPHER_SM4_128_ECBas a CMAC block cipher.Once all SM4-based algorithms are available, update
framework/tests/include/test/psa_exercise_key.handframework/scripts/mbedtls_framework/crypto_knowledge.pyso the frameworkrecognizes SM4 as a 128-bit block cipher. Do not advance the framework pointer
in the earlier commits.
Testing (
test_suite_psa_crypto.sm4.data): cover valid and invalid key imports;one-shot and multipart ECB/CBC/CFB/OFB/CTR operations, partial final blocks,
generated IVs, CBC-PKCS7 and invalid padding, CCM-star-no-tag, and CMAC. For
CCM/GCM, use the RFC 8998 known-answer vectors and cover one-shot and multipart
encryption/decryption, shortened tags, nonce limits, generated nonces and
authentication failures. Use:
.
import_with_datafor key import tests..
cipher_alg_without_iv,cipher_encrypt_multipart,cipher_decrypt,cipher_decrypt_multipart,cipher_verify_output_multipart,cipher_encrypt_validationandcipher_decrypt_failfor cipher coverage..
mac_signfor CMAC..
aead_encrypt,aead_decrypt,aead_encrypt_decrypt,aead_multipart_encrypt,aead_multipart_decryptandaead_multipart_generate_noncefor CCM/GCM..
block_cipher_key_typeandaead_algorithmcases totest_suite_psa_crypto_metadata.data.Document the provenance of all known-answer values.
8. Add PSA/external-driver acceleration for SM4
Add
MBEDTLS_PSA_ACCEL_KEY_TYPE_SM4to the configuration adjustment and testdriver configuration files:
config_adjust_test_accelerators.hcrypto_adjust_config_enable_builtins.hcrypto_adjust_config_tweak_builtins.htests/configs/crypto_config_test_driver_extension.htests/configs/user-config-test-driver-extension.hFollow the AES/ARIA/CAMELLIA key-type acceleration pattern. Define
MBEDTLS_BLOCK_CIPHER_SM4_VIA_PSA, include it inMBEDTLS_BLOCK_CIPHER_CAN_SM4, and retainMBEDTLS_PSA_BUILTIN_KEY_TYPE_SM4when a software block mode still requiresthe built-in primitive.
In
block_cipher.c, mapMBEDTLS_BLOCK_CIPHER_ID_SM4toPSA_KEY_TYPE_SM4inpsa_key_type_from_block_cipher_id(). This supplies thePSA engine used by CCM/GCM with externally stored or accelerated SM4 keys while
preserving the direct-software engine for built-in keys. Update
tests/scripts/components-configuration-crypto.sh, including theno-decryption checks.
Testing (
test_suite_psa_crypto_driver_wrappers.data): add anSM4-CTR entry-point/failure case. Add SM4-CCM and SM4-GCM cases covering
one-shot encryption/decryption, multipart setup, RFC 8998 outputs, injected
allocation failures, and fallback. The cases should verify that the external
driver was actually reached. Use
cipher_entry_pointsfor SM4-CTR,aead_encryptandaead_decryptfor one-shot CCM/GCM, andaead_encrypt_setupandaead_decrypt_setupfor multipart setup.9. Additional testing