PSA_CRYPTO_GENERATOR_INIT does not initialize struct psa_crypto_generator_s completely
- Dominant language
- C
- Stars
- 108
- Forks
- 95
- PR merge metrics
- No merged PRs in 30d
Description
### Description
Mbed Crypto 1.1.1
Toolchain GCC 9.1.1 with CompCert 3.5
Target Linux 5.1.20 x86_64 (Fedora 30 5.1.20-300)
Related: https://github.com/AbsInt/CompCert/issues/312
When compiling tag mbedcrypto-1.1.1 with CompCert 3.5, host and target are Fedora 30 5.1.20-300 x86_64, toolchain is taken from GCC 9.1.1, using
```
make test CC=ccomp CFLAGS='-O1 -fbitfields -fstruct-passing -finline-asm' WARNING_CFLAGS= DEBUG=1 -j
```
The library's test suite test_suite_psa_crypto has two tests fail:
```
PSA key derivation: HKDF SHA-256, RFC5869 #1, output 42+0 ......... FAILED
( ( psa_key_derivation( &generator, handle, alg, salt->x, salt->len, label->x, label->len, requested_capacity ) ) ) == ( ((psa_status_t)0) )
at line 4248, suites/test_suite_psa_crypto.function
```
and
```
PSA key derivation: HKDF SHA-256, exercise HKDF-SHA-256 ........... FAILED
( ( psa_key_derivation( &generator, handle, alg, label, label_length, seed, seed_length, sizeof( output ) ) ) ) == ( ((psa_status_t)0) )
at line 532, suites/test_suite_psa_crypto.function
```
Both tests pass on GCC and clang because it appears that those simply zero out memory of the whole structure, but on CompCert, where only select fields are initialized, code will read uninitialized memory when it accesses some fields of hkdf or tls12_prf.
https://github.com/ARMmbed/mbed-crypto/blob/mbedcrypto-1.1.1/tests/suites/test_suite_psa_crypto.function#L523
https://github.com/ARMmbed/mbed-crypto/blob/mbedcrypto-1.1.1/tests/suites/test_suite_psa_crypto.function#L4216
Standard reference: ISO C 99, section 6.7.8, paragraph 17:
Each brace-enclosed initializer list has an associated current object. When no designations are present, subobjects of the current object are initialized in order according to the type of the current object: array elements in increasing subscript order, structure members in declaration order, **and the first named member of a union.**
### Issue request type
[ ] Question
[ ] Enhancement
[X] Bug
Contributor guide
Research direction
Find the PSA_CRYPTO_GENERATOR_INIT macro and the definition of struct psa_crypto_generator_s, then inspect the failing test_suite_psa_crypto cases around lines 532 and 4248. Compare the initializer's behavior under GCC, clang, and CompCert, and run the PSA crypto test suite with the reported toolchain. Done means all generator fields are initialized according to C99 and both HKDF tests pass without uninitialized reads.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- c
- Domain
- cryptography
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100