32839 Commits

Author SHA1 Message Date
Manuel Pégourié-Gonnard
075e76b65b bignum: compact primality check for small values
Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2026-05-22 00:49:08 +02:00
Manuel Pégourié-Gonnard
1a3cfa5fe7 bignum: make small primes product static
Avoid a heap allocation every time it is used.

Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2026-05-22 00:38:10 +02:00
Manuel Pégourié-Gonnard
7a29052238 Fix side-channel in prime checking/generation
Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2026-05-22 00:35:55 +02:00
Manuel Pégourié-Gonnard
72991252c8
Merge pull request #1621 from mpg/resricted-ecp-modp-polish-3.6
[3.6] Improve ECC optimized modp
2026-05-22 00:01:04 +02:00
Manuel Pégourié-Gonnard
05ce3e90eb ecp: fix a typo in a comment
Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2026-05-21 12:08:44 +02:00
Manuel Pégourié-Gonnard
51f6079c1b ecp: update doc for Koblitz raw quasi-reductions
Those no longer allocate, so they can no longer return that error. This
was missed in the commit that removed heap allocations.

Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2026-05-21 12:07:32 +02:00
Manuel Pégourié-Gonnard
6cf6172ff0 ecp: improve comments in mbedtls_ecp_mod_p256_raw()
Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2026-05-20 13:18:08 +02:00
Manuel Pégourié-Gonnard
ba50c86b7f ecp: minor simplification in ecp_mod_koblitz()
Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2026-05-20 13:18:08 +02:00
Manuel Pégourié-Gonnard
fa157d7d50 ecp: don't re-invent BITS_TO_LIMBS()
Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2026-05-20 13:18:08 +02:00
Manuel Pégourié-Gonnard
a540f097d8 ecp: 2nd pass over mbedtls_ecp_mod_p448_raw()
Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2026-05-20 13:18:08 +02:00
Manuel Pégourié-Gonnard
a4ec1e3e10 ecp: update #endif comment
Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2026-05-20 13:18:08 +02:00
Manuel Pégourié-Gonnard
e1f96dba80 ecp: pass over mbedtls_ecp_mod_p{192,224,256,384}_raw()
Add comments explaining why 3 passes are enough (except for 224 where 1 is enough).

For p256 and p384, this is a bit different
from some other functions as the carry is signed here.

Rm FIPS reference from the documentation, it's about the implementation.

Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2026-05-20 13:18:08 +02:00
Manuel Pégourié-Gonnard
42eac21181 ecp: use loops in mbedtls_ecp_mod_p{192,256,384}_raw()
On principle:
- If the code is written with a loop, the compiler can easily unroll the
  loop at high optimisation levels; if we unroll the loop in the source,
  the compiler is unlikely to roll it up at -Os/-Oz. So loops are more
  adaptable.
- All the other functions are using loops, and there's nothing special
  about those functions that would justify a different treatement.

Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2026-05-20 13:18:08 +02:00
Manuel Pégourié-Gonnard
48e255f31b ecp: pass over mbedtls_ecp_mod_p521_raw()
The comments already explained why the result is in the desired range.

However 2^P521_WIDTH didn't make sense as P521_WIDTH is a number of
limbs no bits, so fix that typo.

Remove the FIPS reference as we're not following it and it doesn't bring
anything useful.

Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2026-05-20 13:18:08 +02:00
Manuel Pégourié-Gonnard
0d638e42ba ecp: pass over mbedtls_ecp_mod_p255_raw()
The test `if (X_Limbs > P255_WIDTH)` was useless: we know that X_limbs =
2 * P255_WIDTH.

The implementation already had an explicit argument why the result is
in range at the end.

Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2026-05-20 13:18:08 +02:00
Manuel Pégourié-Gonnard
3b667e8a75 ecp: pass over mbedtls_ecp_mod_p448_raw()
The documentation used to say upon return X < N but that's wrong: if we
comment out the final conditional subtract in the test function
ecp_mod_p_generic_raw() then we have p448 cases that fail.
This was probably a typo. The actual guarantee is X < 2^448 < 2 * N.

Slightly optimize the code in the loop: we know that A1 = B0 is 1 limb,
so we don't need to memset() or memcpy() so many limbs.

Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2026-05-20 13:18:08 +02:00
Manuel Pégourié-Gonnard
676e95ec4b ecp: pass over ecp_mod_koblitz()
Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2026-05-20 12:29:13 +02:00
Manuel Pégourié-Gonnard
dc44a9b69c ecp: add general comment about fast quasi-reduction
The function-specific comments promised in the last line will follow.

FAST_QUASI_REDUCTION and NIST_QUASI_REDUCTION will be referenced by
specific functions. They're a single word to simplify looking them up.

Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2026-05-20 12:12:53 +02:00
Manuel Pégourié-Gonnard
7689f99697 ecp: avoid heap usage in ecp_mod_koblitz()
That's one 32-byte buffer and one 40-byte buffer (at most, when the
256-bit "Koblitz" curve is enabled).

Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2026-05-20 12:10:52 +02:00
Manuel Pégourié-Gonnard
37279c1f95 ecp: avoid heap usage in mbedtls_ecp_mod_p255_raw()
The buffer is only 32 bytes...

Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2026-05-20 12:01:49 +02:00
Manuel Pégourié-Gonnard
581f773c99 ecp: avoid heap usage in mbedtls_ecp_mod_p448_raw()
With 64-bit limbs, we're using two 64-byte arrays on the stack.
With 32-bit limbs, that's two 60-byte arrays.

This is entirely acceptable, and much better than allocating all the
time in a performance-critical function.

Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2026-05-20 12:01:49 +02:00
Manuel Pégourié-Gonnard
1b531ff7e5 ecp: make mpi_one const again
That way, it can be stored in read-only memory.

Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2026-05-20 12:01:49 +02:00
Manuel Pégourié-Gonnard
275a44ec98 ecp: use same guards for declaration as definition
Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2026-05-20 12:01:49 +02:00
Manuel Pégourié-Gonnard
ee5c265464 ecp: rm repeated include
This header is already included a few lines above

Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2026-05-20 12:01:49 +02:00
Manuel Pégourié-Gonnard
1e3c1db81c ecp: rm obsolete doc about alt implementations
Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2026-05-20 12:01:49 +02:00
Manuel Pégourié-Gonnard
3221798c67 ecp: improve test function in case of failure
Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2026-05-20 12:01:49 +02:00
Ronald Cron
5f62b3fa56
Merge pull request #1616 from mpg/restricted-ecp-modp-fix-3.6
Restricted ecp modp fix 3.6
2026-05-19 08:43:25 +02:00
Manuel Pégourié-Gonnard
10f9bd4add ecp: add back dummy ecp_curves_new.c for compat
Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2026-05-18 10:49:04 +02:00
Manuel Pégourié-Gonnard
672705bd00 Add ChangeLog entry for ECC side channel fix
Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2026-05-18 10:45:11 +02:00
Manuel Pégourié-Gonnard
07db30533a ecp: minor optimisation
Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2026-05-18 10:45:11 +02:00
Manuel Pégourié-Gonnard
0395de165b bignum: update comment about users of bignum_core
Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2026-05-18 10:45:11 +02:00
Manuel Pégourié-Gonnard
8f682e8f8e ecp: clarify contract of internal function
The "shortcut" implementation was using the fact that this function is
called in a single place where we know that the top limb of A is zero.

But the function's documentation was not stating that as a requirement.

Align the documentation and "non-shortcut" implementation on the
"shortcut" implementation. This should be a tiny bit better for
performance: using the fact that A's top limb is zero means we have one
limb less to load from memory. (Copying is load + store, while setting
to 0 is only a store.)

Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2026-05-18 10:45:11 +02:00
Manuel Pégourié-Gonnard
422dcad498 ecp: test mod_pXXX_raw() also without WITH_MPI_UINT
The functions being tested are now used regardless of whether
MBEDTLS_ECP_WITH_MPI_UINT is defined, so they should also be tested
regardless of that option.

Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2026-05-18 10:45:11 +02:00
Manuel Pégourié-Gonnard
6b4b14ae71 ecp: fix implementation-defined behaviour
The comment about optimizing compiler's likely behaviour has been
checked with GCC and Clang on x86-64, 32-bit Arm and 64-bit Arm.

Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2026-05-18 10:45:11 +02:00
Manuel Pégourié-Gonnard
ec70994ba9 ecp: mbedtls_ecp_mod_p448_raw(): fix big-endian bug
Using memcpy / memmove works only when:
- the size being copied is a multiple of the limb size -> 224 is a
  multiple of 32 but not of 64;
- or the endianness is the same as the order we store limbs, which is
  little-endian (that is, X[0] is the least significant limb).

Said otherwise, it breaks on big-endian 64-bit platforms.

Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2026-05-18 10:45:11 +02:00
Manuel Pégourié-Gonnard
83ed698713 ecp: align mbedtls_ecp_mod_p448_raw() with others
The size checks were wrong, and the removed comment was misleading:
this is not about tests, it's just the normal way to wrap a "new bignum"
(aka "raw") function, that expects a fixed number of limbs, to a "legacy
bignum" function, that accepts any size (and allocates).

Also, this aligns ecp_mod_p448() and mbedtls_ecp_mod_p448_raw() with all
other ecp_mod_pXXX() and mbedtls_ecp_mod_pXXX_raw() functions.

Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2026-05-18 10:45:11 +02:00
Manuel Pégourié-Gonnard
0f8a09a99c ecp: clarify static constants in grp
This is not really necessary for 1.0+, but for the 3.6 branch where the
structure and these constants are still public, it is cleaner.

Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2026-05-18 10:45:11 +02:00
Manuel Pégourié-Gonnard
0ebb3a9ff0 ecp: take advantage of new modp functions
The old functions only guaranteed an output in the range [-a * P, b * P]
for a and b "small enough".

The new functions have a stricter contract, allowing the final reduction
to be done efficiently in constant time.

Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2026-05-18 10:45:11 +02:00
Manuel Pégourié-Gonnard
a7840f4b5e ecp: rename ecp_curves_new to ecp_curves
Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2026-05-18 10:43:59 +02:00
Manuel Pégourié-Gonnard
8c806d3cf4 ecp: rm old ecp_curves.c
Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2026-05-18 10:37:40 +02:00
Manuel Pégourié-Gonnard
a3edad8422 ecp: use ecp_curves_new always
Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2026-05-18 10:36:13 +02:00
Manuel Pégourié-Gonnard
4f8212ab7e ecp: rm mbedtls_ecp_get_variant()
Currently there's only one file that has two variants, and we are going
to merge it back, so variant checking no longer really makes sense.

Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2026-05-18 10:35:59 +02:00
Manuel Pégourié-Gonnard
aecc26ac7c
Merge pull request #1593 from gilles-peskine-arm/3.6-restricted-merge-20260506
3.6 only: Merge public into restricted
2026-05-07 09:12:06 +02:00
Gilles Peskine
30f9444cd0 Merge remote-tracking branch 'mbedtls-3.6' into mbedtls-3.6-restricted 2026-05-06 22:40:58 +02:00
Gilles Peskine
2f2b202f8e
Merge pull request #10637 from yiwu0b11/destdir_install_env_support-3.6
Backport 3.6: tests: add CMake DESTDIR install coverage in components-build-system
2026-04-29 14:37:21 +00:00
Gilles Peskine
f1f1d414ed
Merge pull request #10202 from LoveKarlsson/iar-alignment-fix
[3.6] Fix alignment problems with IAR and Zephyr
2026-04-29 14:16:47 +00:00
Lars-Ove Karlsson
e7fd09246d Updated the url to the IAR technical note about unaligned access
To be the same link as in the tf-psa-crypto file.

Signed-off-by: Lars-Ove Karlsson <lars-ove.karlsson@qt.io>
2026-04-29 12:16:35 +02:00
Lars-Ove Karlsson
05890c71c8 Added a changelog entry for IAR unalignment fix.
Signed-off-by: Lars-Ove Karlsson <lars-ove.karlsson@iar.com>
2026-04-27 08:38:52 +02:00
Lars-Ove Karlsson
595b0b577b * [3.6] Fix alignment problems with IAR and Zephyr
Since __packed is a reserved keyword for IAR compilers, and
Zephyr defines it to attribute(__packed__), some typedef constructs
in mbedtls does not work with attribute(packed), only with the
keyword packed.

This fix temporary undefs the macro and restores it after the typedefs.

Signed-off-by: Lars-Ove Karlsson <lars-ove.karlsson@iar.com>
2026-04-27 08:38:51 +02:00
Valerio Setti
9576d65f0f
Merge pull request #10692 from valeriosetti/backport-pr743
[backport 3.6] platform: fix -Wcast-align warnings in memory_buffer_alloc.c
2026-04-22 14:42:06 +00:00