Commit Graph

4429 Commits

Author SHA1 Message Date
Gilles Peskine
90b10c379f Merge pull request #4847 from mstarzyk-mobica/ecb-alt-ret-2.16
Backport 2.16: Catch failures of mbedtls_aes_crypt_ecb and its DES equivalents
2021-10-14 12:10:58 +02:00
Kenneth Soerensen
806ac52be3 Backport 2.16: Remove compiler warning if only MBEDTLS_PK_PARSE_C is defined
Warning reported with IAR compiler:
"mbedtls\library\pkparse.c",1167  Warning[Pe550]: variable "ret" was set but never used

Signed-off-by: Kenneth Soerensen <knnthsrnsn@gmail.com>
2021-09-01 11:27:17 +02:00
Janos Follath
f1b0c70faf Merge pull request #4044 from darrenkrahn/mbedtls-2.16
[Backport 2.16] Mark basic constraints critical as appropriate.
2021-08-26 16:23:58 +01:00
Gilles Peskine
621333f41b Catch failures of AES or DES operations
A DES or AES block operation can fail in alternative implementations of
mbedtls_internal_aes_encrypt() (under MBEDTLS_AES_ENCRYPT_ALT),
mbedtls_internal_aes_decrypt() (under MBEDTLS_AES_DECRYPT_ALT),
mbedtls_des_crypt_ecb() (under MBEDTLS_DES_CRYPT_ECB_ALT),
mbedtls_des3_crypt_ecb() (under MBEDTLS_DES3_CRYPT_ECB_ALT).
A failure can happen if the accelerator peripheral is in a bad state.
Several block modes were not catching the error.

This commit does the following code changes:

* Fix DES and AES API calls which ignored the return values:
    * In library code: on failure, goto exit and return ret.
    * In pkey programs: goto exit.
    * In the benchmark program: exit (not ideal since there's no error
      message, but it's what the code currently does for failures).
    * In test code: TEST_ASSERT.
* Changelog entry.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
Signed-off-by: Mateusz Starzyk <mateusz.starzyk@mobica.com>
2021-08-05 15:31:48 +02:00
Ronald Cron
ae466e78f4 Merge pull request #4787 from gilles-peskine-arm/fix-clang12-Wstring-concatenation-2.16
Backport 2.16: Prevent triggering Clang 12 -Wstring-concatenation warning
2021-07-23 14:07:58 +02:00
Guido Vranken
70bdf8d1df Use single-line string literals.
Signed-off-by: Guido Vranken <guidovranken@gmail.com>
2021-07-18 16:16:44 +02:00
Guido Vranken
4a78d58f25 Prevent triggering Clang 12 -Wstring-concatenation warning
Wrap multi-line string literals in parentheses
to prevent a Clang 12 -Wstring-concatenation warning
(activated by -Wall), which caused the build to fail.

Fixes https://github.com/ARMmbed/mbedtls/issues/3586

Signed-off-by: Guido Vranken <guidovranken@gmail.com>
2021-07-18 16:16:43 +02:00
Yuto Takano
1cded872a8 Replace _RR with prec_RR to prevent reserved identifier clashes
Signed-off-by: Yuto Takano <yuto.takano@arm.com>
2021-07-14 15:04:11 +01:00
Yuto Takano
d7cd60fba0 Replace _B with B to prevent reserved identifier clashes
Signed-off-by: Yuto Takano <yuto.takano@arm.com>
2021-07-14 15:03:09 +01:00
Bence Szépkúti
124a87ea6f Bump library version numbers
Signed-off-by: Bence Szépkúti <bence.szepkuti@arm.com>
2021-07-05 18:47:36 +02:00
Bence Szépkúti
726a8ccb6d Merge branch 'mbedtls-2.16-restricted' into mbedtls-2.16.11rc0-pr 2021-07-02 14:00:19 +01:00
Dave Rodgman
4c20c774a1 Merge pull request #4735 from daverodgman/alert_bugfixes_2.16
Backport 2.16: Fix alert raised for invalid fragment length
2021-06-30 09:02:45 +01:00
Dave Rodgman
ffbbeee284 TLS UNSUPPORTED_EXTENSION error code changes
Signed-off-by: Dave Rodgman <dave.rodgman@arm.com>
2021-06-29 15:19:26 +01:00
Nick Child
c15e31d355 pk.c: Ensure min hash_len in pk_hashlen_helper
The function `pk_hashlen_helper` exists to ensure a valid hash_len is
used in pk_verify and pk_sign functions. This function has been
used to adjust to the corrsponding hash_len if the user passes in 0
for the hash_len argument based on the md algorithm given. If the user
does not pass in 0 as the hash_len, then it is not adjusted. This is
problematic if the user gives a hash_len and hash buffer that is less than the
associated length of the md algorithm. This error would go unchecked
and eventually lead to buffer overread when given to specific pk_sign/verify
functions, since they both ignore the hash_len argument if md_alg is not
MBEDTLS_MD_NONE.

This commit, adds a conditional to `pk_hashlen_helper` so that an
error is thrown if the user specifies a hash_length (not 0) and it is
less than the expected for the associated message digest algorithm.
This aligns better with the api documentation where it states "If
hash_len is 0, then the length associated with md_alg is used instead,
or an error returned if it is invalid"

Signed-off-by: Nick Child <nick.child@ibm.com>
Signed-off-by: Nayna Jain <nayna@linux.ibm.com>
2021-06-29 09:44:04 -04:00
Dave Rodgman
459a46102d Fix TLS alert codes
Signed-off-by: Dave Rodgman <dave.rodgman@arm.com>
2021-06-29 09:44:35 +01:00
Janos Follath
1001d2c711 Fix unused parameter warning
Signed-off-by: Janos Follath <janos.follath@arm.com>
2021-06-28 10:24:20 +01:00
Janos Follath
9a64d3e0ca Add prefix to BYTES_TO_T_UINT_*
These macros were moved into a header and now check-names.sh is failing.
Add an MBEDTLS_ prefix to the macro names to make it pass.

Signed-off-by: Janos Follath <janos.follath@arm.com>
2021-06-28 10:24:20 +01:00
Janos Follath
5f9b667396 Reject low-order points on Curve448 early
We were already rejecting them at the end, due to the fact that with the
usual (x, z) formulas they lead to the result (0, 0) so when we want to
normalize at the end, trying to compute the modular inverse of z will
give an error.

If we wanted to support those points, we'd a special case in
ecp_normalize_mxz(). But it's actually permitted by all sources (RFC
7748 say we MAY reject 0 as a result) and recommended by some to reject
those points (either to ensure contributory behaviour, or to protect
against timing attack when the underlying field arithmetic is not
constant-time).

Since our field arithmetic is indeed not constant-time, let's reject
those points before they get mixed with sensitive data (in
ecp_mul_mxz()), in order to avoid exploitable leaks caused by the
special cases they would trigger. (See the "May the Fourth" paper
https://eprint.iacr.org/2017/806.pdf)

Signed-off-by: Janos Follath <janos.follath@arm.com>
2021-06-28 10:24:20 +01:00
Janos Follath
b741e8d263 Use mbedtls_mpi_lset() more
Signed-off-by: Janos Follath <janos.follath@arm.com>
2021-06-28 10:24:20 +01:00
Janos Follath
7d34e2e655 Move mpi constant macros to bn_mul.h
Signed-off-by: Janos Follath <janos.follath@arm.com>
2021-06-28 10:23:57 +01:00
Janos Follath
c16ec6be85 Prevent memory leak in ecp_check_pubkey_x25519()
Signed-off-by: Janos Follath <janos.follath@arm.com>
2021-06-28 10:05:31 +01:00
Manuel Pégourié-Gonnard
9f12b11be0 Avoid complaints about undeclared non-static symbols
Clang was complaining and check-names.sh too

This only duplicates macros, so no impact on code size. In 3.0 we can
probably avoid the duplication by using an internal header under
library/ but this won't work for 2.16.

Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2021-06-28 10:05:31 +01:00
Manuel Pégourié-Gonnard
89ce7d2445 Use more compact encoding of Montgomery curve constants
Base 256 beats base 16.

Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2021-06-28 10:05:31 +01:00
Manuel Pégourié-Gonnard
6ec1535148 Use a more compact encoding of bad points
Base 10 is horrible, base 256 is much better.

Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2021-06-28 10:05:31 +01:00
Manuel Pégourié-Gonnard
4d0b9da37d Reject low-order points on Curve25519 early
We were already rejecting them at the end, due to the fact that with the
usual (x, z) formulas they lead to the result (0, 0) so when we want to
normalize at the end, trying to compute the modular inverse of z will
give an error.

If we wanted to support those points, we'd a special case in
ecp_normalize_mxz(). But it's actually permitted by all sources
(RFC 7748 say we MAY reject 0 as a result) and recommended by some to
reject those points (either to ensure contributory behaviour, or to
protect against timing attack when the underlying field arithmetic is
not constant-time).

Since our field arithmetic is indeed not constant-time, let's reject
those points before they get mixed with sensitive data (in
ecp_mul_mxz()), in order to avoid exploitable leaks caused by the
special cases they would trigger. (See the "May the Fourth" paper
https://eprint.iacr.org/2017/806.pdf)

Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2021-06-28 10:05:29 +01:00
Gilles Peskine
18efd1c2c3 Correct some statements about the ordering of A and B
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2021-06-22 18:48:37 +02:00
Gilles Peskine
f95d433655 Clarification in a comment
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2021-06-22 18:48:37 +02:00
Gilles Peskine
1d6b1dc955 Simplify is-zero check
The loop exits early iff there is a nonzero limb, so i==0 means that
all limbs are 0, whether the number of limbs is 0 or not.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2021-06-22 18:48:37 +02:00
Gilles Peskine
afbf191b17 Write a proof of correctness for mbedtls_mpi_gcd
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2021-06-22 18:48:37 +02:00
Gilles Peskine
2949d3ac1b Explain how the code relates to the description in HAC
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2021-06-22 18:48:37 +02:00
Gilles Peskine
44e6bb6b38 Fix multiplication with negative result and a low-order 0 limb
Fix a bug introduced in "Fix multiplication producing a negative zero" that
caused the sign to be forced to +1 when A > 0, B < 0 and B's low-order limb
is 0.

Add a non-regression test. More generally, systematically test combinations
of leading zeros, trailing zeros and signs.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2021-06-22 18:48:37 +02:00
Gilles Peskine
ab6ab6aaf0 Fix multiplication producing a negative zero
Fix mbedtls_mpi_mul_mpi() when one of the operands is zero and the
other is negative. The sign of the result must be 1, since some
library functions do not treat {-1, 0, NULL} or {-1, n, {0}} as
representing the value 0.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2021-06-22 18:48:37 +02:00
Gilles Peskine
5504d1725b mbedtls_mpi_gcd: fix the case B==0
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2021-06-22 18:48:37 +02:00
Gilles Peskine
c559eac574 Fix null pointer dereference in mbedtls_mpi_exp_mod
Fix a null pointer dereference in mbedtls_mpi_exp_mod(X, A, N, E, _RR) when
A is the value 0 represented with 0 limbs.

Make the code a little more robust against similar bugs.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2021-06-22 18:48:37 +02:00
Manuel Pégourié-Gonnard
07941f45e6 Merge pull request #4690 from gilles-peskine-arm/debug-print-mpi-null-2.16
Backport 2.16: Fix mbedtls_debug_print_mpi crash on 0
2021-06-22 12:09:05 +02:00
Manuel Pégourié-Gonnard
c9807ea0cc Merge pull request #4622 from gilles-peskine-arm/default-hashes-curves-2.16
Backport 2.16: Curve and hash selection for X.509 and TLS
2021-06-22 12:08:49 +02:00
Manuel Pégourié-Gonnard
fa719f7415 Merge branch 'mbedtls-2.16' into mbedtls-2.16-restricted
* mbedtls-2.16: (21 commits)
  Reword changelog - Test Resource Leak
  Update changelog formatting - Missing Free Context
  Fix fd range for select on Windows
  Refactor file descriptor checks into a common function
  Update changelog formatting - Missing Free Context
  Update changelog formatting - Missing Free Context
  Changelog entry for Free Context in test_suite_aes fix
  Free context at the end of aes_crypt_xts_size()
  Add changelog entry for non-uniform MPI random generation
  ecp: Fix bias in the generation of blinding values
  DHM: add test case with x_size < 0
  DHM tests: add some explanations
  DHM: add notes about leading zeros
  dhm: Fix bias in private key generation and blinding
  dhm_check_range: microoptimization
  DHM refactoring: use dhm_random_below in dhm_make_common
  DHM blinding: don't accept P-1 as a blinding value
  DHM refactoring: unify mbedtls_dhm_make_{params,public}
  Test mbedtls_dhm_make_params with different x_size
  Repeat a few DH tests
  ...
2021-06-22 10:57:13 +02:00
Gilles Peskine
3db875e66a Add missing parentheses
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2021-06-21 10:14:41 +02:00
Gilles Peskine
5eace4c826 Indicate that the truncation from size_t to int is deliberate
MPI sizes do fit in int. Let MSVC know this conversion is deliberate.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2021-06-21 10:14:41 +02:00
Gilles Peskine
e1a31284de Simplify mbedtls_debug_print_mpi and fix the case of empty bignums
Rewrite mbedtls_debug_print_mpi to be simpler and smaller. Leverage
mbedtls_mpi_bitlen() instead of manually looking for the leading
zeros.

Fix #4608: the old code made an invalid memory dereference when
X->n==0 (freshly initialized bignum with the value 0).

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2021-06-21 10:14:41 +02:00
Gilles Peskine
8297657759 Fix fd range for select on Windows
Fix mbedtls_net_poll() and mbedtls_net_recv_timeout() often failing with
MBEDTLS_ERR_NET_POLL_FAILED on Windows: they were testing that the file
descriptor is in range for fd_set, but on Windows socket descriptors are not
limited to a small range. Fixes #4465.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2021-06-20 23:19:05 +02:00
Gilles Peskine
9065d786fd Refactor file descriptor checks into a common function
This will make it easier to change the behavior uniformly.

No behavior change.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2021-06-20 23:19:04 +02:00
Manuel Pégourié-Gonnard
e9eca7fe8d Homogenize coding patterns
Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2021-06-17 16:40:22 +02:00
Manuel Pégourié-Gonnard
56efc52d6b Merge pull request #4628 from ronald-cron-arm/dhm-key-generation-bias
dhm: Fix bias in private key generation
2021-06-16 13:13:34 +02:00
Manuel Pégourié-Gonnard
6aba8fc230 No C99 loops in this branch
Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2021-06-15 13:28:50 +02:00
Manuel Pégourié-Gonnard
de2ab2a4bd Fix GCC warning
We know that we'll never call this function with T_size == 0, but the
compiler doesn't.

Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2021-06-15 12:37:23 +02:00
Manuel Pégourié-Gonnard
4fc96dff3d Silence MSVC type conversion warnings
Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2021-06-11 10:22:56 +02:00
Manuel Pégourié-Gonnard
12f0238c7f Simplify sign selection
Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2021-06-11 10:22:56 +02:00
Manuel Pégourié-Gonnard
dc6a5f2f68 Avoid UB caused by conversion to int
Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2021-06-11 10:22:56 +02:00
Manuel Pégourié-Gonnard
a1283cc638 Use bit operations for mpi_safe_cond_swap()
Unrelated to RSA (only used in ECP), but while improving one
mbedtls_safe_cond_xxx function, let's improve the other as well.

Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2021-06-11 10:22:56 +02:00