13465 Commits

Author SHA1 Message Date
Gilles Peskine
7f9470ac02
Merge pull request #9451 from minosgalanakis/fix-v3.6-issues-9186-and-9188-bp
[Backport 3.6] Fix v3.6 issues 9186 and 9188 bp
2024-08-12 09:34:19 +00:00
Gilles Peskine
1dfb6b595a Clarify some internal documentation
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2024-08-09 14:04:46 +02:00
Gilles Peskine
ac43de0e52 Make integer downsizing explicit
Reassure both humans and compilers that the places where we assign an
integer to a smaller type are safe.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2024-08-09 13:55:05 +02:00
Gilles Peskine
3bc9d2b5b9 Dynamic key store: make full-key-store tests work effectively
Add a practical way to fill the dynamic key store by artificially limiting
the slice length through a test hook.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2024-08-09 13:54:54 +02:00
Gilles Peskine
a81282ce30 Microoptimizations when MBEDTLS_PSA_KEY_STORE_DYNAMIC is disabled
Compensate some of the code size increase from implementing dynamic key slots.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2024-08-09 13:54:19 +02:00
Gilles Peskine
e8199f574c Dynamic key store: implementation
When MBEDTLS_PSA_KEY_STORE_DYNAMIC is enabled, key slots are now organized in
multiple slices. The slices are allocated on demand, which allows the key
store to grow. The size of slices grows exponentially, which allows reaching
a large number of slots with a small (static) number of slices without too
much overhead.

Maintain a linked list of free slots in each slice. This way, allocating a
slot takes O(1) time unless a slice needs to be allocated.

In this commit, slices are only ever freed when deinitializing the key
store. This should be improved in the future to free empty slices.

To avoid growing the persistent key cache without control, the persistent
key cache has a fixed size (reusing MBEDTLS_PSA_KEY_SLOT_COUNT to avoid
creating yet another option).

When MBEDTLS_PSA_KEY_STORE_DYNAMIC is disabled. no semantic change and
minimal changes to the code.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2024-08-09 13:52:56 +02:00
Gilles Peskine
47ad2f7484 psa_key_slot_t: different fields in free vs occupied slots
Place some fields of psa_key_slot_t in a union, to prepare for a new field
in free slots that should not require extra memory.

For occupied slots, place only the registered_readers field in the union,
not other fields, to minimize textual changes. All fields could move to the
union except state (also needed in free slots) and attr (which must stay
first to reduce the code size, because it is accessed at many call sites).

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2024-08-09 13:52:54 +02:00
Gilles Peskine
5064af62b6 Dynamic key store: preparatory refactoring
Add some abstractions around code that traverses the key store, in
preparation for adding support for MBEDTLS_PSA_KEY_STORE_DYNAMIC.

No intended behavior change. The generated machine code should be
almost the same with an optimizing compiler (in principle, it could be the
same with sufficient constant folding and inlining, but in practice it will
likely be a few byes larger),

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2024-08-09 13:52:33 +02:00
Gilles Peskine
3b41e1d2a5
Merge pull request #9403 from gilles-peskine-arm/psa-keystore-dynamic-prep-3.6
Backport 3.6: prepare for dynamic key store
2024-08-09 08:00:03 +00:00
Manuel Pégourié-Gonnard
ef41d8ccbe Fix 1.3 failure to update flags for (ext)KeyUsage
Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2024-08-08 12:34:56 +02:00
Manuel Pégourié-Gonnard
e217673996 Merge remote-tracking branch 'public/mbedtls-3.6' into merge-3.6
* public/mbedtls-3.6: (251 commits)
  Call in_mbedtls_repo
  Move some proj detection code inside pre_check_environment
  Match spacing in pointer types in documentation with the code style
  Rename one more deprecated identifier
  Documentation improvements
  Rename internal function psa_key_production_parameters_are_default
  key_custom: update analyze_outcomes.py
  Test cpp_dummy_build in pedantic mode
  Changelog entry for the move from key_ext to key_custom functions
  Remove some tests of psa_generate_key_ext
  Document the key_ext functions as deprecated
  Documentation: point to key_custom instead of key_ext
  Update PSA wrappers
  Implement psa_generate_key_custom
  all.sh/components: Removed components.sh
  all.sh/components: Moved build_aes_via_padlock to platform component.
  all.sh/components: Moved driver components to configuration crypto.
  all.sh/components: Moved more components to configuration crypto.
  all.sh/components: Fixed a typo in configuration-tls.
  all.sh/components: Moved more components to configuration tls.
  ...
2024-08-08 09:49:51 +02:00
Gilles Peskine
5eca4029c2 Fix inverted assertion message
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2024-08-07 20:08:23 +02:00
Elena Uziunaite
dc74d8effc Tiny fix in library/constant_time_impl.h
Signed-off-by: Elena Uziunaite <elena.uziunaite@arm.com>
2024-08-06 16:26:41 +01:00
Elena Uziunaite
81d6b6b576 Remove the hack in library/constant_time_impl.h
Signed-off-by: Elena Uziunaite <elena.uziunaite@arm.com>
2024-08-06 16:26:41 +01:00
Gilles Peskine
4a85ff3997 Rename one more deprecated identifier
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2024-08-06 13:13:05 +02:00
Gilles Peskine
0d619b2dc7 Documentation improvements
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2024-08-06 13:13:05 +02:00
Gilles Peskine
52504f8568 Rename internal function psa_key_production_parameters_are_default
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2024-08-06 13:13:05 +02:00
Gilles Peskine
f36d785188 Implement psa_generate_key_custom
Implement `psa_generate_key_custom()` and
`psa_key_derivation_output_key_custom()`. These functions replace
`psa_generate_key_ext()` and `psa_key_derivation_output_key_ext()`.
They have the same functionality, but a slightly different interface:
the `ext` functions use a structure with a flexible array member to pass
variable-length data, while the `custom` functions use a separate parameter.

Keep the `ext` functions for backward compatibility with Mbed TLS 3.6.0.
But make them a thin wrapper around the new `custom` functions.

Duplicate the test code and data. The test cases have to be duplicated
anyway, and the test functions are individually more readable this way.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2024-08-06 13:12:06 +02:00
Michael Schuster
4d0d0ec028 Move the -Wmissing-prototypes option from library/CMakeLists.txt to the top-level CMakeLists.txt for GCC & Clang
Signed-off-by: Michael Schuster <michael@schuster.ms>
2024-08-06 12:09:13 +01:00
Michael Schuster
5be4fd784e Fix build of v3.6 with unset MBEDTLS_DHM_C but MBEDTLS_USE_PSA_CRYPTO set (fixes #9188)
Avoid compiler warning about size comparison (like in commit 7910cdd):

Clang builds fail, warning about comparing uint8_t to a size that may be >255.

Signed-off-by: Michael Schuster <michael@schuster.ms>
2024-08-06 11:26:00 +01:00
Michael Schuster
c9184fe7ab Fix server mode only build of v3.6 with MBEDTLS_SSL_CLI_C unset (fixes #9186)
Signed-off-by: Michael Schuster <michael@schuster.ms>
2024-08-06 11:26:00 +01:00
Gilles Peskine
e55bc34a2c
Merge pull request #1257 from gilles-peskine-arm/psa-drbg-no-hmac-document-3.6
Backport 3.6: Document that MBEDTLS_PSA_HMAC_DRBG_MD_TYPE does not force HMAC
2024-07-31 16:47:32 +02:00
Gilles Peskine
4269ee6f2d Fix stack buffer overflow in ECDSA signature format conversions
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2024-07-25 18:39:53 +02:00
Gilles Peskine
5d660396ec Force MBEDTLS_PSA_HMAC_DRBG_MD_TYPE based on CTR_DRBG
If MBEDTLS_CTR_DRBG_C is enabled, force MBEDTLS_PSA_HMAC_DRBG_MD_TYPE to be
disabled. This resolves the former inconsistency in builds where
MBEDTLS_PSA_HMAC_DRBG_MD_TYPE is explicitly defined but MBEDTLS_CTR_DRBG_C
remains enabled, where PSA called the CTR_DRBG functions but other parts of
the code based assumed that HMAC was in use, in particular error code
conversions (leading to a test failure in test_suite_psa_crypto_init).

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2024-07-25 18:24:59 +02:00
Gilles Peskine
7dea096086 Fix overlap between volatile keys and built-in keys
Fix interference between PSA volatile keys and built-in keys
when MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS is enabled and
MBEDTLS_PSA_KEY_SLOT_COUNT is more than 4096. This overlap used to make it
possible that a volatile key would receive the identifier of a built-in key,
and is now caught by a static assertion.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2024-07-17 12:21:21 +02:00
Gilles Peskine
708ec09e30 Assert that the key ID range for volatile keys is large enough
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2024-07-17 12:21:21 +02:00
Gilles Peskine
b6bf370159 Assert that key ID ranges don't overlap
Ensure that a key ID can't be in range for more than one of volatile keys,
persistent (i.e. user-chosen) keys or built-in keys.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2024-07-17 12:21:21 +02:00
Gilles Peskine
d72ad738bd Prevent mbedtls_psa_register_se_key with volatile keys
mbedtls_psa_register_se_key() is not usable with volatile keys, since there
is no way to return the implementation-chosen key identifier which would be
needed to use the key. Document this limitation. Reject an attempt to create
such an unusable key. Fixes #9253.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2024-07-17 12:21:21 +02:00
Gilles Peskine
86c603702e Reorder blocks to avoid double negations
Convert `#if !... A #else B #endif` to `#if ... B #else A`. No semantic change.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2024-07-17 12:21:21 +02:00
Gilles Peskine
4804847b15 Make it possible to enable CTR_DRBG/PSA without a PSA AES driver
Make it possible, but not officially supported, to switch the CTR_DRBG
module to PSA mode even if MBEDTLS_AES_C is defined. This is not really
useful in practice, but is convenient to test the PSA mode without setting
up drivers.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2024-07-17 12:21:21 +02:00
Gilles Peskine
cd693c36fd MBEDTLS_STATIC_ASSERT: make it work outside of a function
At the top level, the macro would have had to be used without a following
semicolon (except with permissive compilers that accept spurious semicolons
outside of a function), which is confusing to humans and indenters. Fix
that.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2024-07-17 12:21:21 +02:00
Michael Grand (TrustnGo)
83da5abdd6 Fix typo in platform_util.c
Fix a typo in a conditional include.

Signed-off-by: Michael Grand (TrustnGo) <m.grand@trustngo.tech>
2024-07-11 17:31:22 +03:00
Gilles Peskine
422a276c5e
Merge pull request #9045 from Troy-Butler/handle-null-args
[Backport 3.6] Fix NULL argument handling in mbedtls_xxx_free() functions
2024-07-04 14:50:59 +00:00
Gilles Peskine
41204b86d8
Merge pull request #9272 from sezrab/silence-3.6
[Backport 3.6] Silence gcc 12.2.0 warning
2024-07-04 14:49:41 +00:00
Gilles Peskine
c03041844b
Merge pull request #9341 from gilles-peskine-arm/psa_cipher_decrypt-ccm_star-iv_length_enforcement-3.6
Backport 3.6: psa_cipher_decrypt CCM*: fix rejection of messages shorter than 3 bytes
2024-07-04 14:39:29 +00:00
Gilles Peskine
acef7b3894
Merge pull request #9132 from andre-rosa/check-overflow-when-reading-padding-len-on-aes-128-cbc-decryption-for-mbedtls-3.6
Backport 3.6: Add invalid `padding_len` check in `get_pkcs_padding`
2024-07-03 14:41:17 +00:00
Gilles Peskine
b47c3b3111 psa_cipher_decrypt CCM*: fix rejection of messages shorter than 3 bytes
Credit to Cryptofuzz. Fixes #9314.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2024-07-03 09:42:35 +02:00
Manuel Pégourié-Gonnard
75ed58723e Add optionally unsafe variant of exp_mod for perf
Attempt to partially solve the performance regression in 3.6.0 without
adding too much code size.

Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2024-06-18 12:52:45 +02:00
Patrick Wildt
38bc960ecf Silence gcc 12.2.0 warning
Unfortunately this compiler complains about a variable potentially being
used un-initialized.  Silence the warning by initializing it to a sane
default.

Signed-off-by: Patrick Wildt <pwildt@google.com>
2024-06-17 12:24:02 +01:00
Tom Cosgrove
8fb5df859c
Merge pull request #9248 from sezrab/fix-function-parameter-3.6
[Backport 3.6] Fix incorrect array length in function prototype
2024-06-13 07:55:57 +00:00
Tom Cosgrove
07f9459a24
Merge pull request #9245 from lhuang04/mbedtls_3_6_psk_null_back_port
Set psk to NULL in ssl_psk_remove -backpor to 3.6
2024-06-12 12:00:28 +00:00
Sam Berry
9722fd133d Fix incorrect array length in function prototype
Issue #9179 (MBEDTLS_SSL_CID_OUT_LEN_MAX changed to
MBEDTLS_SSL_CID_IN_LEN_MAX in library\ssl.h and library\ssl_tls.c)

Signed-off-by: Sam Berry <sam.berry@arm.com>
2024-06-12 11:16:46 +01:00
lhuang04
6d4d94f3d0 Set psk to NULL in ssl_psk_remove -backpor to 3.6
Summary:
Backport [PR 9241](https://github.com/Mbed-TLS/mbedtls/pull/9241) to 3.6
branch.

Test Plan:

Reviewers:

Subscribers:

Tasks:

Tags:
Signed-off-by: lhuang04 <lhuang04@fb.com>
2024-06-11 13:05:02 -07:00
Gilles Peskine
400659b565 Use unsigned long rather than size_t for format string readability
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2024-06-05 11:49:51 +02:00
Gilles Peskine
eeb4ff5662 Fix uint32_t printed as unsigned int
This is ok in practice since we don't support 16-bit platforms, but it makes
`arm-none-eabi-gcc-10 -mthumb -Wformat` complain.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2024-06-05 11:49:50 +02:00
Gilles Peskine
dc49258c8a Guard configuration-specific code
A large block of code is only reachable if MBEDTLS_PK_USE_PSA_EC_DATA is
enabled, i.e. if MBEDTLS_USE_PSA_CRYPTO is enabled with driver-only ECC.
Compilers are likely to figure it out, but still, for clarity and
robustness, do guard that block of code with the appropriate conditional
compilation guard.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2024-05-29 20:09:36 +02:00
Gilles Peskine
b69757ab61 Fix a compilation warning in pk.c when PSA is enabled and RSA is disabled
It isn't detected on the CI because we only test this with an ancient Clang
that doesn't warn. Old GCC, modern GCC and modern Clang do
warn (-Wunused-but-set-variable).

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2024-05-14 12:06:20 +02:00
Andre Goddard Rosa
3711734a0a Add invalid padding_len check in get_pkcs_padding
When trying to decrypt data with an invalid key, we found that `mbedtls`
returned `0x6200` (`-25088`), which means "_CIPHER - Input data contains
invalid padding and is rejected_" from `mbedtls_cipher_finish`, but it also
set the output len as `18446744073709551516`.

In case we detect an error with padding, we leave the output len zero'ed
and return `MBEDTLS_ERR_CIPHER_INVALID_PADDING`.

Here's a reference for the way `openssl` checks the padding length:
  - 1848c561ec/crypto/evp/evp_enc.c (L1023)
  - b554eef43b

So add a check ensuring output is set to the least-harmful value in the
error cases.

With the robustness fix:
`PASSED (125 suites, 26644 tests run)`

Without the robustness fix:
`FAILED (125 suites, 26644 tests run)`

Signed-off-by: Andre Goddard Rosa <andre.goddard@gmail.com>
Signed-off-by: Andre Goddard Rosa <agoddardrosa@roku.com>
2024-05-13 09:27:57 -05:00
Gilles Peskine
c4f4ff9210
Merge pull request #9085 from nileshkale123/fix/redefinition_warning_for_gnu_source_backport_3.6
Backport 3.6: Fixed issue of redefinition warning messages for _GNU_SOURCE
2024-05-06 12:40:45 +00:00
Manuel Pégourié-Gonnard
67a92b7442
Merge pull request #9090 from valeriosetti/issue9068-backport
[Backport 3.6] Undefined reference to mbedtls_md_error_from_psa() function
2024-05-03 07:52:41 +00:00